Exemplo n.º 1
0
    def visit_FunctionCall(self, node):
        args = node.args

        args = [qlast.FuncArg(arg=arg) for arg in self.visit(args)]

        result = qlast.FunctionCall(
            func=(node.func_shortname.module, node.func_shortname.name),
            args=args,
        )

        return result
Exemplo n.º 2
0
    def visit_FunctionCall(self, node):
        # FIXME: this is a temporary solution to bridge the gap to EdgeQL
        if node.agg_set_modifier == qlast.AggDISTINCT:
            args = qlast.UnaryOp(op=qlast.DISTINCT, operand=node.args[0])
        else:
            args = node.args

        # FIXME: hack to reconstruct args for a trivial aggregate function
        args = [qlast.FuncArg(arg=arg) for arg in self.visit(args)]
        if node.agg_filter or node.agg_sort:
            args[0].sort = node.agg_sort
            args[0].filter = (self.visit(node.agg_filter)
                              if node.agg_filter is not None else None)

        result = qlast.FunctionCall(
            func=(node.func.shortname.module, node.func.shortname.name),
            args=args,
        )

        return result
Exemplo n.º 3
0
 def reduce_Expr(self, *kids):
     self.val = qlast.FuncArg(arg=kids[0].val)
Exemplo n.º 4
0
 def reduce_AnyIdentifier_ASSIGN_Expr(self, *kids):
     self.val = (
         kids[0].val,
         kids[0].context,
         qlast.FuncArg(arg=kids[2].val, context=kids[2].context)
     )
Exemplo n.º 5
0
 def reduce_Expr(self, *kids):
     self.val = (
         None,
         None,
         qlast.FuncArg(arg=kids[0].val, context=kids[0].context))
Exemplo n.º 6
0
 def reduce_Identifier_TURNSTILE_Expr(self, *kids):
     self.val = qlast.FuncArg(name=kids[0].val, arg=kids[2].val)
Exemplo n.º 7
0
def compile_func_to_ir(func,
                       schema,
                       *,
                       anchors=None,
                       security_context=None,
                       modaliases=None,
                       implicit_id_in_shapes=False):
    """Compile an EdgeQL function into EdgeDB IR."""

    if debug.flags.edgeql_compile:
        debug.header('EdgeQL Function')
        debug.print(func.get_code(schema))

    trees = ql_parser.parse_block(func.get_code(schema) + ';')
    if len(trees) != 1:
        raise errors.InvalidFunctionDefinitionError(
            'functions can only contain one statement')

    tree = trees[0]
    if modaliases:
        ql_parser.append_module_aliases(tree, modaliases)

    if anchors is None:
        anchors = {}

    anchors['__defaults_mask__'] = irast.Parameter(
        name='__defaults_mask__', stype=schema.get('std::bytes'))

    func_params = func.get_params(schema)
    pg_params = s_func.PgParams.from_params(schema, func_params)
    for pi, p in enumerate(pg_params.params):
        p_shortname = p.get_shortname(schema)
        anchors[p_shortname] = irast.Parameter(name=p_shortname,
                                               stype=p.get_type(schema))

        if p.get_default(schema) is None:
            continue

        tree.aliases.append(
            qlast.AliasedExpr(
                alias=p_shortname,
                expr=qlast.
                IfElse(condition=qlast.BinOp(left=qlast.FunctionCall(
                    func=('std', 'bytes_get_bit'),
                    args=[
                        qlast.FuncArg(arg=qlast.Path(
                            steps=[qlast.ObjectRef(
                                name='__defaults_mask__')])),
                        qlast.FuncArg(arg=qlast.IntegerConstant(value=str(pi)))
                    ]),
                                             right=qlast.IntegerConstant(
                                                 value='0'),
                                             op='='),
                       if_expr=qlast.Path(
                           steps=[qlast.ObjectRef(name=p_shortname)]),
                       else_expr=qlast._Optional(
                           expr=p.get_ql_default(schema)))))

    ir = compile_ast_to_ir(tree,
                           schema,
                           anchors=anchors,
                           func=func,
                           security_context=security_context,
                           modaliases=modaliases,
                           implicit_id_in_shapes=implicit_id_in_shapes)

    return ir