Example #1
0
def build_field_access(self, node_expr, builtin_field_name,
                       bare_node_expr_constructor):
    """
    Helper for abstract expressions below. Return a resolved expression to
    evaluate either `node_expr`'s builtin property `field_name` (if `node_expr`
    is an entity) or the builtin field `field_name` (if `node_expr` is a bare
    node).

    We don't use the builtin property in the bare node case as the expression
    must return the same type as its input, while the property always returns
    an entity.

    :param langkit.expressions.ResolvedExpression node_expr: Expression for the
        input node/entity.
    :param str builtin_field_name: Name of the builtin field to access.
    :param bare_node_expr_constructor: Callback used to build the expression
        that computes the field access in the case we have a bare node input.
    :rtype: langkit.expressions.ResolvedExpression
    """
    if node_expr.type.is_entity_type:
        return FieldAccess.Expr(node_expr,
                                get_builtin_field(builtin_field_name), [],
                                implicit_deref=True,
                                abstract_expr=self)
    else:
        return bare_node_expr_constructor(node_expr, abstract_expr=self)
Example #2
0
def parent(self, node):
    """
    Return `node`'s parent in the AST.

    This works on both bare nodes and entities.

    .. todo::

        Implement rebindings shedding.
    """
    node_expr = construct(node)
    check_source_language(
        node_expr.type.is_ast_node or node_expr.type.is_entity_type,
        'Invalid prefix for "parent": got {} but AST node or entity'
        ' expected'.format(node_expr.type.dsl_name))

    if node_expr.type.is_entity_type:
        return FieldAccess.Expr(node_expr,
                                get_builtin_field('parent'), [],
                                implicit_deref=True,
                                abstract_expr=self)
    else:
        return FieldAccessExpr(node_expr,
                               'Parent',
                               T.root_node,
                               do_explicit_incref=False,
                               abstract_expr=self)
Example #3
0
def parents(self, node):
    """
    Return an array that contains the lexical parents (this node included).
    Nearer parents are first in the list.

    This works on both bare nodes and entities.

    .. todo::

        Implement rebindings shedding.
    """
    node_expr = construct(node)
    check_source_language(
        node_expr.type.is_ast_node or node_expr.type.is_entity_type,
        'Invalid prefix for "parents": got {} but AST node or entity'
        ' expected'.format(node_expr.type.dsl_name))

    if node_expr.type.is_entity_type:
        return FieldAccess.Expr(node_expr,
                                get_builtin_field('parents'), [],
                                implicit_deref=True,
                                abstract_expr=self)
    else:
        return CallExpr('Node_Parents',
                        'Parents',
                        T.root_node.array, [node_expr],
                        abstract_expr=self)
Example #4
0
def build_field_access(
    node_expr: ResolvedExpression,
    builtin_field_name: str,
    args: Sequence[Optional[ResolvedExpression]],
    bare_node_expr_constructor: Callable[[], ResolvedExpression],
    abstract_expr: Optional[AbstractExpression],
) -> ResolvedExpression:
    """
    Helper for abstract expressions below. Return a resolved expression to
    evaluate either `node_expr`'s builtin property `field_name` (if `node_expr`
    is an entity) or the builtin field `field_name` (if `node_expr` is a bare
    node).

    We don't use the builtin property in the bare node case as the expression
    must return the same type as its input, while the property always returns
    an entity.

    :param prefix: Expression for the input node/entity.
    :param builtin_field_name: Name of the builtin field to access.
    :param args: Arguments for the field access.
    :param bare_node_expr_constructor: Callback used to build the expression
        that computes the field access in the case we have a bare node input.
    :param abstract_expr: Abstract expression corresponding to this field
        access.
    """
    if node_expr.type.is_entity_type:
        return FieldAccess.Expr(node_expr,
                                get_builtin_field(builtin_field_name),
                                args,
                                implicit_deref=True,
                                abstract_expr=abstract_expr)
    else:
        return bare_node_expr_constructor()
Example #5
0
def children(self, node):
    """
    Return `node`'s children in the AST.

    This works on both bare nodes and entities.
    """
    node_expr = construct(node)
    check_source_language(
        node_expr.type.is_ast_node or node_expr.type.is_entity_type,
        'Invalid prefix for "children": got {} but AST node or entity'
        ' expected'.format(node_expr.type.dsl_name)
    )

    if node_expr.type.is_entity_type:
        return FieldAccess.Expr(node_expr, get_builtin_field('children'),
                                [], implicit_deref=True, abstract_expr=self)
    else:
        return CallExpr('Node_Children', 'Children', T.root_node.array,
                        [node_expr], abstract_expr=self)