예제 #1
0
def parse_body(code, context, ensure_terminated=False):
    if not isinstance(code, list):
        return parse_stmt(code, context)

    lll_node = ["seq"]
    for stmt in code:
        lll = parse_stmt(stmt, context)
        lll_node.append(lll)

    # force using the return routine / exit_to cleanup for end of function
    if ensure_terminated and context.return_type is None and not _is_terminated(code):
        lll_node.append(parse_stmt(vy_ast.Return(value=None), context))

    # force zerovalent, even last statement
    lll_node.append("pass")  # CMC 2022-01-16 is this necessary?
    return LLLnode.from_list(lll_node, pos=getpos(code[0]) if code else None)
예제 #2
0
def generate_public_variable_getters(vyper_module: vy_ast.Module) -> None:
    """
    Create getter functions for public variables.

    Arguments
    ---------
    vyper_module : Module
        Top-level Vyper AST node.
    """

    for node in vyper_module.get_children(vy_ast.AnnAssign, {"annotation.func.id": "public"}):
        func_type = node._metadata["type"]
        input_types, return_type = func_type.get_signature()
        input_nodes = []

        # use the annotation node as a base to build the input args and return type
        # starting with `args[0]` to remove the surrounding `public()` call`
        annotation = copy.deepcopy(node.annotation.args[0])

        # the base return statement is an `Attribute` node, e.g. `self.<var_name>`
        # for each input type we wrap it in a `Subscript` to access a specific member
        return_stmt: vy_ast.VyperNode = vy_ast.Attribute(
            value=vy_ast.Name(id="self"), attr=func_type.name
        )

        for i, type_ in enumerate(input_types):
            if not isinstance(annotation, vy_ast.Subscript):
                # if we get here something has failed in type checking
                raise CompilerPanic("Mismatch between node and input type while building getter")
            if annotation.value.get("id") == "HashMap":  # type: ignore
                # for a HashMap, split the key/value types and use the key type as the next arg
                arg, annotation = annotation.slice.value.elements  # type: ignore
            else:
                # for other types, build an input arg node from the expected type
                # and remove the outer `Subscript` from the annotation
                arg = vy_ast.Name(id=type_._id)
                annotation = annotation.value
            input_nodes.append(vy_ast.arg(arg=f"arg{i}", annotation=arg))

            # wrap the return statement in a `Subscript`
            return_stmt = vy_ast.Subscript(
                value=return_stmt, slice=vy_ast.Index(value=vy_ast.Name(id=f"arg{i}"))
            )

        # after iterating the input types, the remaining annotation node is our return type
        return_node = annotation
        if isinstance(return_node, vy_ast.Name) and return_node.id != return_type._id:
            # special case when the return type is an interface
            # TODO allow interfaces as return types and remove this
            return_node.id = return_type._id

        # join everything together as a new `FunctionDef` node, annotate it
        # with the type, and append it to the existing `Module` node
        expanded = vy_ast.FunctionDef.from_node(
            node.annotation,
            name=func_type.name,
            args=vy_ast.arguments(args=input_nodes, defaults=[],),
            body=[vy_ast.Return(value=return_stmt)],
            decorator_list=[vy_ast.Name(id="external"), vy_ast.Name(id="view")],
            returns=return_node,
        )
        expanded._metadata["type"] = func_type
        vyper_module.add_to_body(expanded)