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_REWRITER_CONTEXT_H
18 #define ZORBA_REWRITER_CONTEXT_H
19 
20 #include <map>
21 #include <vector>
22 
23 #include "common/shared_types.h"
24 
25 #include "util/dynamic_bitset.h"
26 
27 #include "compiler/expression/var_expr.h"
28 #include "compiler/expression/expr_manager.h"
29 
30 namespace zorba
31 {
32 
33 class user_function;
34 
35 typedef std::map<var_expr *, ulong> VarIdMap;
36 typedef std::vector<var_expr*> IdVarMap;
37 typedef std::map<const expr *, DynamicBitset> ExprVarsMap;
38 
39 
40 /*******************************************************************************
41 
42   theRoot:
43   --------
44   The root node of the expr DAG that is going to be optimized using this context.
45 
46   theVarIdMap:
47   ------------
48   Maps a var_expr to its unique "prefix" id. The "prefix" id has the following
49   property: for 2 vars v1 and v2, v1 is defined before v2 if and only if
50   prefix-id(v1) < prefix-id(v2). See index_flwor_vars() function in
51   tools/expr_tools.cpp for more details.
52 
53   theIdVarMap:
54   ------------
55   This is the reverse mapping of theVarIdMap.
56 
57   theExprVarsMap:
58   ---------------
59   An entry into this map maps an expr to the variables that are referenced by
60   that expr and/or its sub-exprs. (Note: given that the domain expr of a var
61   $x is not considered a sub-expr of $x, if $x is referenced by an expr E and
62   the domain expr of $x references another var $y, $y is NOT considered to be
63   referenced by E). Only variables that have been assigned a prolog id (i.e.,
64   the ones that appear in theVarIdMap) are considered. The set of vars referenced
65   by an expr is implemented by a bitset that is indexed by prolog var ids and
66   whose size (in number of bits) is equal to the size of theVarIdMap.
67 
68   theFlworStack:
69   --------------
70   The current "in-scope" flwor exprs, ie., flwor exprs that the rule has
71   entered but not exited yet.
72 ********************************************************************************/
73 class RewriterContext
74 {
75 public:
76   CompilerCB                 * theCCB;
77 
78   ExprManager                * theEM;
79 
80   expr                       * theRoot;
81 
82   user_function              * theUDF;
83 
84   zstring                      theMessage;
85 
86   int                          m_tempvarCounter;
87 
88   bool                         theIsInOrderedMode;
89 
90   VarIdMap                   * theVarIdMap;
91   IdVarMap                   * theIdVarMap;
92   ExprVarsMap                * theExprVarsMap;
93   std::vector<expr*>           theFlworStack;
94   std::vector<bool>            theInReturnClause;
95 
96 public:
97   RewriterContext(
98       CompilerCB* cb,
99       expr* root,
100       user_function* udf,
101       const zstring& msg,
102       bool orderedMode);
103 
104   ~RewriterContext();
105 
getCompilerCB()106   CompilerCB* getCompilerCB() const { return theCCB; }
107 
108   expr* getRoot();
109 
110   void setRoot(expr* root);
111 
112   var_expr* createTempVar(
113       static_context* sctx,
114       const QueryLoc& loc,
115       var_expr::var_kind kind);
116 };
117 
118 
119 /*******************************************************************************
120 
121 ********************************************************************************/
122 struct UDFCallChain
123 {
124   fo_expr      * theFo;
125   UDFCallChain * thePrev;
126 
UDFCallChainUDFCallChain127   UDFCallChain() : theFo(NULL), thePrev(NULL) {}
128 
UDFCallChainUDFCallChain129   UDFCallChain(fo_expr* caller, UDFCallChain* prevCaller)
130     :
131     theFo(caller),
132     thePrev(prevCaller)
133   {
134   }
135 };
136 
137 
138 }
139 
140 #endif /* ZORBA_REWRITER_CONTEXT_H */
141 
142 /*
143  * Local variables:
144  * mode: c++
145  * End:
146  */
147 /* vim:set et sw=2 ts=2: */
148