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 #include "stdafx.h"
17 
18 #include "compiler/api/compilercb.h"
19 #include "compiler/expression/expr_base.h"
20 
21 #ifdef ZORBA_WITH_DEBUGGER
22 #include "debugger/debugger_commons.h"
23 #endif
24 
25 #include "context/static_context.h"
26 
27 #include "system/properties.h"
28 
29 #include "zorbaserialization/serialize_template_types.h"
30 #include "zorbaserialization/serialize_zorba_types.h"
31 
32 
33 namespace zorba
34 {
35 
36 SERIALIZABLE_CLASS_VERSIONS(CompilerCB)
37 
38 SERIALIZABLE_CLASS_VERSIONS_2(CompilerCB::config, TYPE_CompilerCB_config)
39 
40 
41 #define DEF_PRINT_EXPR_TREE( phase )                                    \
42 static void print_expr_tree_##phase(const expr* e, const std::string& name) \
43 {                                                                       \
44   std::ostream& os = Properties::instance()->debug_out();               \
45   os << "Expression tree after " << #phase                              \
46      << " for " << name << "\n";                                        \
47   e->put(os) << std::endl;                                              \
48 }
49 
50 
51 DEF_PRINT_EXPR_TREE(translation);
52 DEF_PRINT_EXPR_TREE(optimization);
53 
54 
55 /*******************************************************************************
56 
57 ********************************************************************************/
config()58 CompilerCB::config::config()
59   :
60   opt_level(O1),
61   lib_module(false),
62   for_serialization_only(false),
63   parse_cb(NULL)
64 {
65   translate_cb = optimize_cb = NULL;
66 
67   // TODO: move these out
68   print_item_flow = Properties::instance()->printItemFlow();
69 
70   if (Properties::instance()->printTranslated())
71     translate_cb = print_expr_tree_translation;
72 
73   if (Properties::instance()->printOptimized())
74     optimize_cb = print_expr_tree_optimization;
75 
76   force_gflwor = Properties::instance()->forceGflwor();
77 }
78 
79 
80 /*******************************************************************************
81 
82 ********************************************************************************/
config(::zorba::serialization::Archiver & ar)83 CompilerCB::config::config(::zorba::serialization::Archiver& ar)
84   :
85   parse_cb(NULL),
86   translate_cb(NULL),
87   optimize_cb(NULL)
88 {
89 }
90 
91 
92 /*******************************************************************************
93 
94 ********************************************************************************/
serialize(::zorba::serialization::Archiver & ar)95 void CompilerCB::config::serialize(::zorba::serialization::Archiver& ar)
96 {
97   ar & force_gflwor;
98   SERIALIZE_ENUM(opt_level_t, opt_level);
99   ar & lib_module;
100   ar & for_serialization_only;
101   ar & print_item_flow;
102 }
103 
104 
105 /*******************************************************************************
106 
107 ********************************************************************************/
CompilerCB(XQueryDiagnostics * errmgr,long timeout)108 CompilerCB::CompilerCB(XQueryDiagnostics* errmgr, long timeout)
109   :
110   theXQueryDiagnostics(errmgr),
111   theRootSctx(0),
112 #ifdef ZORBA_WITH_DEBUGGER
113   theDebuggerCommons(0),
114 #endif
115   theHasEval(false),
116   theIsEval(false),
117   theIsLoadProlog(false),
118   theIsUpdating(false),
119   theIsSequential(false),
120   theHaveTimeout(false),
121   theTimeout(timeout),
122   theTempIndexCounter(0),
123   theEM(new ExprManager(this))
124 {
125 
126   if (timeout >= 0)
127     theHaveTimeout = true;
128 }
129 
130 
131 /*******************************************************************************
132   Used by the eval iterator to create a new ccb as a copy of the ccb of the
133   enclosing query.
134 *******************************************************************************/
CompilerCB(const CompilerCB & cb)135 CompilerCB::CompilerCB(const CompilerCB& cb)
136   :
137   zorba::serialization::SerializeBaseClass(cb),
138   theXQueryDiagnostics(cb.theXQueryDiagnostics),
139   theRootSctx(NULL),
140 #ifdef ZORBA_WITH_DEBUGGER
141   theDebuggerCommons(cb.theDebuggerCommons),
142 #endif
143   theHasEval(false),
144   theIsEval(false),
145   theIsLoadProlog(false),
146   theIsUpdating(false),
147   theIsSequential(false),
148   theHaveTimeout(cb.theHaveTimeout),
149   theTimeout(cb.theTimeout),
150   theTempIndexCounter(0),
151   theConfig(cb.theConfig),
152   theEM(new ExprManager(this))
153 {
154 }
155 
156 
157 /*******************************************************************************
158 
159 ********************************************************************************/
CompilerCB(::zorba::serialization::Archiver & ar)160 CompilerCB::CompilerCB(::zorba::serialization::Archiver& ar)
161   :
162   ::zorba::serialization::SerializeBaseClass(),
163   theXQueryDiagnostics(NULL),
164   theRootSctx(NULL),
165 #ifdef ZORBA_WITH_DEBUGGER
166   theDebuggerCommons(NULL),
167 #endif
168   theHasEval(false),
169   theIsEval(false),
170   theEM(new ExprManager(this))
171 {
172 }
173 
174 
175 /*******************************************************************************
176 
177 ********************************************************************************/
~CompilerCB()178 CompilerCB::~CompilerCB()
179 {
180   delete theEM;
181   theSctxMap.clear();
182 }
183 
184 
185 /*******************************************************************************
186 
187 ********************************************************************************/
serialize(::zorba::serialization::Archiver & ar)188 void CompilerCB::serialize(::zorba::serialization::Archiver& ar)
189 {
190   ar.set_ccb(this);
191 
192   ar & theHasEval;
193   ar & theIsEval;
194   ar & theIsLoadProlog;
195   ar & theIsUpdating;
196   ar & theSctxMap;
197   ar & theRootSctx;
198 #ifdef ZORBA_WITH_DEBUGGER
199   ar & theDebuggerCommons;
200 #endif
201   if (!ar.is_serializing_out())
202   {
203     //don't serialize this
204     theXQueryDiagnostics = NULL;
205   }
206   ar & theConfig;
207   ar & theHaveTimeout;
208   ar & theTimeout;
209   ar & theTempIndexCounter;
210 }
211 
212 
213 
214 /*******************************************************************************
215 
216 ********************************************************************************/
getStaticContext(int c)217 static_context* CompilerCB::getStaticContext(int c)
218 {
219   SctxMap::iterator lIter;
220   lIter = theSctxMap.find(c);
221   assert(lIter != theSctxMap.end());
222   return lIter->second.getp();
223 }
224 
225 
226 /***************************************************************************//**
227 
228 ********************************************************************************/
add_pragma(const expr * e,pragma * p)229 void CompilerCB::add_pragma(const expr* e, pragma* p)
230 {
231   thePragmas.insert(std::make_pair(e, p));
232 }
233 
234 
235 void
lookup_pragmas(const expr * e,std::vector<pragma * > & pragmas) const236 CompilerCB::lookup_pragmas(const expr* e, std::vector<pragma*>& pragmas) const
237 {
238   pragmas.clear();
239 
240   std::pair<PragmaMapIter, PragmaMapIter> lRange = thePragmas.equal_range(e);
241   while (lRange.first != lRange.second)
242   {
243     pragmas.push_back(lRange.first->second);
244     ++lRange.first;
245   }
246 }
247 
248 bool
lookup_pragma(const expr * e,const zstring & localname,pragma * & p) const249 CompilerCB::lookup_pragma(
250     const expr* e,
251     const zstring& localname,
252     pragma*& p) const
253 {
254   std::pair<PragmaMapIter, PragmaMapIter> lRange = thePragmas.equal_range(e);
255   while (lRange.first != lRange.second)
256   {
257     if (lRange.first->second->theQName->getLocalName() == localname)
258     {
259       p = lRange.first->second;
260       return true;
261     }
262     ++lRange.first;
263   }
264   return false;
265 }
266 
267 
268 
269 } /* namespace zorba */
270 /* vim:set et sw=2 ts=2: */
271