Beispiel #1
0
def get_value(self, logic_var):
    """
    Extract the value out of a logic variable. The returned type is always the
    root entity type. If the variable is not defined, return a null entity.

    :param AbstractExpression logic_var: The logic var from which we want to
        extract the value.
    """
    from langkit.expressions import If

    PropertyDef.get()._gets_logic_var_value = True

    rtype = T.root_node.entity

    logic_var_expr = construct(logic_var, T.LogicVarType)
    logic_var_ref = logic_var_expr.create_result_var('Logic_Var_Value')

    return If.Expr(cond=CallExpr('Is_Logic_Var_Defined',
                                 'Eq_Node.Refs.Is_Defined', T.BoolType,
                                 [logic_var_expr]),
                   then=CallExpr('Eq_Solution', 'Eq_Node.Refs.Get_Value',
                                 rtype, [logic_var_ref]),
                   else_then=NullExpr(T.root_node.entity),
                   rtype=rtype,
                   abstract_expr=self)
 def construct(self):
     from langkit.expressions import If, IsNull, EmptyArray
     expr = construct(self.expr)
     ret = CollectionSingleton.Expr(expr)
     if self.coerce_null:
         return If.Expr(IsNull.construct_static(expr),
                        EmptyArray.construct_static(expr.type.array_type()),
                        ret, ret.type)
     else:
         return ret
Beispiel #3
0
def make_as_entity(node_expr,
                   entity_info=None,
                   null_check=True,
                   abstract_expr=None):
    """
    Helper for as_entity. Takes a resolved expression instead of an abstract
    one.

    :param ResolvedExpression node_expr: The AST node expression to wrap as an
        entity.
    :param ResolvedExpression|None entity_info: Expression to use as the entity
        information. If provided, its type must be T.entity_info. Otherwise,
        the ambient entity info is used.
    """
    from langkit.expressions import If, IsNull, New

    entity_type = node_expr.type.entity

    # If we use the ambient entity info, make the current property an entity
    # one.
    if entity_info is None:
        p = PropertyDef.get()
        p.set_uses_entity_info()
        entity_info = construct(p.entity_info_arg)

    # Expression tree sharing is forbidden, so if we need to reference the
    # result of the input node expression multiple times, create a variable to
    # hold the input node.
    node_ref = (node_expr.create_result_var('Node_For_Entity')
                if null_check else node_expr)

    entity_expr = New.StructExpr(
        entity_type,
        {
            names.Name('El'): node_ref,
            names.Name('Info'): entity_info
        },
        result_var_name=names.Name.from_lower('as_entity'),
    )

    result = If.Expr(
        IsNull.construct_static(node_expr),
        NullExpr(entity_type),
        entity_expr,
        entity_type,
        abstract_expr=abstract_expr) if null_check else entity_expr

    result.abstract_expr = abstract_expr
    return result
Beispiel #4
0
    def construct(self):
        from langkit.expressions import If, IsNull, ArrayLiteral

        expr = construct(self.expr)
        expr_var = expr.create_result_var('To_Array_Prefix')

        # Use "expr" only for first evaluation, and then use expr_var to refer
        # to the result. We do this to avoid resolved expression sharing in the
        # expression tree.
        ret = CollectionSingleton.Expr(expr_var if self.coerce_null else expr)
        if self.coerce_null:
            return If.Expr(IsNull.construct_static(expr),
                           ArrayLiteral.construct_static([], expr.type.array),
                           ret, ret.type)
        else:
            return ret