1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 #ifndef ZORBA_COMPILER_FUNCTION_ITEM_EXPR_H
18 #define ZORBA_COMPILER_FUNCTION_ITEM_EXPR_H
19 
20 #include <vector>
21 
22 #include "compiler/expression/expr_base.h"
23 
24 #include "store/naive/shared_types.h"
25 
26 namespace zorba {
27 
28 /*******************************************************************************
29 
30   [121] FilterExpr ::= PrimaryExpr (Predicate | DynamicFunctionInvocation)*
31 
32   [164] DynamicFunctionInvocation ::= "(" (ExprSingle ("," ExprSingle)*)? ")"
33 
34   theExpr : The input expr that produces a function item
35   theArgs : The arg exprs to pass to the function.
36 ********************************************************************************/
37 class dynamic_function_invocation_expr : public expr
38 {
39   friend class ExprIterator;
40   friend class expr;
41   friend class ExprManager;
42 
43 protected:
44   expr                * theExpr;
45   std::vector<expr*>    theArgs;
46 
47 protected:
48   dynamic_function_invocation_expr(
49       CompilerCB* ccb,
50       static_context* sctx,
51       const QueryLoc& loc,
52       expr* anExpr,
53       const std::vector<expr*>& args);
54 
55 public:
get_function()56   const expr* get_function() const { return theExpr; }
57 
get_args()58   const std::vector<expr*>& get_args() const { return theArgs; }
59 
60   void compute_scripting_kind();
61 
62   expr* cloneImpl(substitution_t& s) const;
63 
64   void accept(expr_visitor&);
65 
66   std::ostream& put(std::ostream& os) const;
67 };
68 
69 
70 
71 /*******************************************************************************
72   Represents a LiteralFunctionItem expr or an InlineFunction expr.
73 
74   LiteralFunctionItem ::= QName "#" IntegerLiteral
75 
76   InlineFunction ::= "function" "(" ParamList? ")" ("as" SequenceType)? EnclosedExpr
77 
78   theQName :
79   NULL in case of inline function. Otherwise, the qname of the named function
80   in the LiteralFunctionItem.
81 
82   theFunction :
83   This is always a pointer to a user_function obj. In case of an inline function
84   expr, it is an anonymous user_function obj that is created on-the-fly by the
85   translator to represent the body and signature of the inline function. In case
86   of LiteralFunctionItem where the named function is a UDF, it is the
87   user_function obj of that UDF. Finally, in case of LiteralFunctionItem where
88   the named function F is not a UDF, it is an anonymous user_function obj UF
89   that is created on-the-fly by the translator. The signature of UF is the same
90   as that of F, and its body simply invokes F. The reason why UF is built is to
91   unify the implemenation of dynamic function invocation.
92 
93   theArity :
94   We need to store the arity also here because the function above doesn't know
95   about its arity in case it's a variadic function.
96 
97   theScopedVariables :
98   Empty in the case of LiteralFunctionItem. Otherwise, the FLWOR vars that are
99   in scope at the place where the InlineFunction expr appears at.
100 ********************************************************************************/
101 class function_item_expr: public expr
102 {
103   friend class ExprIterator;
104   friend class expr;
105   friend class ExprManager;
106 
107 private:
108   store::Item_t        theQName;
109   function_t           theFunction;
110   uint32_t             theArity;
111   std::vector<expr*>  theScopedVariables;
112 
113 public:
114 
115 protected:
116   function_item_expr(
117       CompilerCB* ccb,
118       static_context* sctx,
119       const QueryLoc& loc,
120       const store::Item* aQName,
121       function* f,
122       uint32_t aArity);
123 
124   function_item_expr(
125       CompilerCB* ccb,
126       static_context* sctx,
127       const QueryLoc& loc);
128 
129 public:
130   ~function_item_expr();
131 
132   void add_variable(expr* var);
133 
134   void set_function(user_function_t& udf);
135 
get_function()136   function* get_function() const { return theFunction.getp(); }
137 
get_qname()138   const store::Item_t& get_qname() const { return theQName; }
139 
get_arity()140   uint32_t get_arity() const { return theArity; }
141 
142   const std::vector<expr*>& get_vars() const;
143 
144   void compute_scripting_kind();
145 
146   expr* cloneImpl(substitution_t& s) const;
147 
148   void accept(expr_visitor&);
149 
150   std::ostream& put(std::ostream& os) const;
151 };
152 
153 } //end of namespace
154 
155 #endif
156 
157 /*
158  * Local variables:
159  * mode: c++
160  * End:
161  */
162 /* vim:set et sw=2 ts=2: */
163