Example #1
0
        def __init__(self, expr, astnode, do_raise=False, result_var=None):
            """
            :type expr: ResolvedExpression
            :type astnode: ASTNode
            :type do_raise: bool

            :param ResolvedExpr result_var: If provided, the cast will use it
                to store the cast result. Otherwise, a dedicated variable is
                created for this.
            """
            self.do_raise = do_raise
            self.expr = expr
            self.static_type = astnode

            p = PropertyDef.get()
            self.expr_var = p.vars.create('Cast_Expr', self.expr.type)
            self.result_var = (result_var or
                               p.vars.create('Cast_Result', astnode))
            assert self.result_var.type == astnode, (
                'Cast temporaries must have exactly the cast type: {} expected'
                ' but got {} instead'.format(
                    astnode.name().camel,
                    self.result_var.type.name().camel
                )
            )

            super(Cast.Expr, self).__init__()
Example #2
0
        def __init__(self, element_var, index_var, collection, expr,
                     iter_scope, filter=None, concat=False, take_while=None):
            """
            :type element_var: VarExpr
            :type index_var: None|VarExpr
            :type collection: ResolvedExpression
            :type expr: ResolvedExpression
            :type iter_scope: langkit.expressions.base.LocalVars.Scope
            :type filter: ResolvedExpression
            :type concat: bool
            :type take_while: ResolvedExpression
            """
            self.take_while = take_while
            self.element_var = element_var
            self.index_var = index_var
            self.collection = collection
            self.expr = expr
            self.filter = filter
            self.concat = concat
            self.iter_scope = iter_scope

            element_type = (self.expr.type.element_type()
                            if self.concat else
                            self.expr.type)
            self.static_type = element_type.array_type()
            self.static_type.add_to_context()

            self.array_var = PropertyDef.get().vars.create_scopeless(
                'Map', self.type
            )
            iter_scope.parent.add(self.array_var)

            super(Map.Expr, self).__init__()
Example #3
0
        def __init__(self, expr, var_expr, then_expr, default_expr):
            self.expr = expr
            self.var_expr = var_expr
            self.then_expr = then_expr
            self.default_expr = default_expr
            self.static_type = self.then_expr.type
            self.result_var = PropertyDef.get().vars.create("Result_Var",
                                                            self.type)

            super(Then.Expr, self).__init__()
Example #4
0
    def __init__(self, domain, logic_var_expr):
        self.domain = domain
        ":type: ResolvedExpression"

        self.logic_var_expr = logic_var_expr
        ":type: ResolvedExpression"

        self.res_var = PropertyDef.get().vars.create("Var", EquationType)

        super(DomainExpr, self).__init__()
Example #5
0
    def __init__(self, env_expr, to_eval_expr):
        self.to_eval_expr = to_eval_expr
        self.env_expr = env_expr

        # Declare a variable that will hold the value of the
        # bound environment.
        self.static_type = self.to_eval_expr.type
        self.env_var = PropertyDef.get().vars.create("New_Env",
                                                     LexicalEnvType)

        super(EnvBindExpr, self).__init__()
Example #6
0
        def __init__(self, expr):
            """
            :type expr: ResolvedExpression
            """
            self.expr = expr

            self.expr.type.array_type().add_to_context()
            self.static_type = self.expr.type.array_type()

            self.array_var = PropertyDef.get().vars.create('Singleton',
                                                           self.type)

            super(CollectionSingleton.Expr, self).__init__()
Example #7
0
        def __init__(self, cond, then, else_then, rtype):
            """
            :param ResolvedExpression cond: A boolean expression.
            :param ResolvedExpression then: If "cond" is evaluated to true,
                this part is returned.
            :param ResolvedExpression else_then: If "cond" is evaluated to
                false, this part is returned.
            :param langkit.compiled_types.CompiledType rtype: Type parameter.
                The type that is returned by then and else_then.
            """
            self.cond = cond
            self.then = then
            self.else_then = else_then
            self.static_type = rtype
            self.result_var = PropertyDef.get().vars.create('Result', rtype)

            super(If.Expr, self).__init__()
Example #8
0
def as_entity(self, node):
    """
    Wrap `node` into an entity. This uses environment rebindings from the
    context.
    """

    p = PropertyDef.get()
    check_source_language(p, "as_entity has to be used in a property")

    check_source_language(
        p._uses_entity_info is not False,
        'This property has been explicitly tagged as not using entity info, so'
        ' .as_entity is invalid here')

    # We want to keep original type of node, so no downcast
    node_expr = construct(node, T.root_node, downcast=False)

    ret = make_as_entity(node_expr, abstract_expr=self)
    ret.create_result_var('Ent')
    return ret
Example #9
0
def solve(self, equation):
    """
    Call ``solve`` on the given `equation` and return whether any solution was
    found or not. The solutions are not returned, instead, logic variables are
    bound to their values in the current solution.

    .. todo::

        For the moment, since properties returning equations will reconstruct
        them everytime, there is no way to get the second solution if there is
        one. Also you cannot do that manually either since a property exposing
        equations cannot be public at the moment.

    :param AbstractExpression equation: The equation to solve.
    """
    PropertyDef.get()._solves_equation = True
    return CallExpr('Solve_Success', 'Solve_Wrapper', T.BoolType,
                    [construct(equation, T.EquationType),
                     construct(Self, T.root_node)],
                    abstract_expr=self)
        def __init__(self,
                     list_element_var,
                     element_var,
                     index_var,
                     collection,
                     expr,
                     iter_scope,
                     filter=None,
                     concat=False,
                     take_while=None):
            """
            :type list_element_var: VarExpr|None
            :type element_var: VarExpr
            :type index_var: None|VarExpr
            :type collection: ResolvedExpression
            :type expr: ResolvedExpression
            :type iter_scope: langkit.expressions.base.LocalVars.Scope
            :type filter: ResolvedExpression
            :type concat: bool
            :type take_while: ResolvedExpression
            """
            self.take_while = take_while
            self.list_element_var = list_element_var
            self.element_var = element_var
            self.index_var = index_var
            self.collection = collection
            self.expr = expr
            self.filter = filter
            self.concat = concat
            self.iter_scope = iter_scope

            element_type = (self.expr.type.element_type()
                            if self.concat else self.expr.type)
            self.static_type = element_type.array_type()
            self.static_type.add_to_context()

            self.array_var = PropertyDef.get().vars.create_scopeless(
                'Map', self.type)
            iter_scope.parent.add(self.array_var)

            super(Map.Expr, self).__init__()
Example #11
0
        def __init__(self, receiver_expr, node_data, arguments):
            """
            :param ResolvedExpression receiver_expr: The receiver of the field
                access.

            :param langkit.compiled_types.AbstracNodeData node_data: The
                accessed property or field.

            :param list[ResolvedExpression] arguments: If non-empty, this field
                access will actually be a primitive call.
            """
            self.receiver_expr = receiver_expr
            self.node_data = node_data
            self.static_type = self.node_data.type
            self.arguments = arguments
            self.simple_field_access = False

            # After EnvSpec.create_properties has been run, expressions in
            # environment specifications only allow field accesses. These are
            # not evaluated in a property context, so they cannot create local
            # variables.
            #
            # TODO: in this context, it would still be useful to emit a null
            # check so that we raise a special exception instead of a
            # Storage_Error.

            p = PropertyDef.get()

            if p:
                self.prefix_var = p.vars.create('Pfx', self.receiver_expr.type)
            else:
                self.simple_field_access = True

            # Create a variable for all field accesses in properties. This is
            # needed because the property will return an owning reference, so
            # we need it to be attached to the scope. In other cases, this can
            # make debugging easier.
            super(FieldAccess.Expr, self).__init__(
                'Field_Access_Result' if p else None
            )
Example #12
0
        def __init__(self, kind, collection, expr, element_var, index_var,
                     iter_scope):
            """
            :param str kind: Kind for this quantifier expression. 'all' will
                check that all items in "collection" fullfill "expr" while
                'any' will check that at least one of them does.

            :param ResolvedExpression expr: Expression to evaluate for each
                item in "collection".

            :param ResolvedExpression collection: Collection on which this map
                operation works.

            :param ResolvedExpression expr: A boolean expression to evaluate on
                the collection's items.

            :param element_var: Variable to use in "expr".
            :type element_var: ResolvedExpression

            :param index_var: Index variable to use in "expr".
            :type index_var: None|ResolvedExpression

            :param iter_scope: Scope for local variables internal to the
                iteration.
            :type iter_scope: langkit.expressions.base.LocalVars.Scope
            """
            self.kind = kind
            self.collection = collection
            self.expr = expr
            self.element_var = element_var
            self.index_var = index_var
            self.iter_scope = iter_scope
            self.result_var = PropertyDef.get().vars.create_scopeless(
                'Quantifier_Result', BoolType
            )
            iter_scope.parent.add(self.result_var)

            super(Quantifier.Expr, self).__init__()
Example #13
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.LogicVar)
    logic_var_ref = logic_var_expr.create_result_var('Logic_Var_Value')

    return If.Expr(cond=CallExpr('Is_Logic_Var_Defined',
                                 'Entity_Vars.Is_Defined', T.Bool,
                                 [logic_var_expr]),
                   then=CallExpr('Eq_Solution', 'Entity_Vars.Get_Value', rtype,
                                 [logic_var_ref]),
                   else_then=NullExpr(T.root_node.entity),
                   abstract_expr=self)
Example #14
0
        def __init__(self, astnode, assocs):
            p = PropertyDef.get()
            self.result_var = p.vars.create('New_Node', astnode)

            super(New.NodeExpr, self).__init__(astnode, assocs)