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_EXPR
18 #define ZORBA_COMPILER_EXPR
19 
20 #include <string>
21 #include <vector>
22 
23 #include <zorba/store_consts.h>
24 
25 #include "zorbautils/checked_vector.h"
26 
27 #include "zorbatypes/schema_types.h"
28 
29 #include "functions/signature.h"
30 
31 #include "compiler/expression/var_expr.h"
32 
33 #include "context/static_context.h"
34 #include "context/namespace_context.h"
35 
36 #include "types/node_test.h"
37 
38 #include "store/api/item.h"
39 
40 #include "runtime/core/sequencetypes.h"
41 
42 namespace zorba
43 {
44 
45 class ExprManager;
46 class expr_visitor;
47 class NodeNameTest;
48 class signature;
49 class pragma;
50 
51 /*******************************************************************************
52   [68] IfExpr ::= "if" "(" Expr ")" "then" ExprSingle "else" ExprSingle
53 ********************************************************************************/
54 class if_expr : public expr
55 {
56   friend class ExprManager;
57   friend class ExprIterator;
58   friend class expr;
59 
60 protected:
61   expr* theCondExpr;
62   expr* theThenExpr;
63   expr* theElseExpr;
64 
65 protected:
66   if_expr(
67         CompilerCB* ccb,
68         static_context* sctx,
69         const QueryLoc& loc,
70         expr* c,
71         expr* t,
72         expr* e);
73 
74 public:
get_cond_expr()75   expr* get_cond_expr() const { return theCondExpr; }
76 
get_then_expr()77   expr* get_then_expr() const { return theThenExpr; }
78 
get_else_expr()79   expr* get_else_expr() const { return theElseExpr; }
80 
81   void compute_scripting_kind();
82 
83   expr* cloneImpl(substitution_t& s) const;
84 
85   void accept(expr_visitor&);
86 
87   std::ostream& put(std::ostream&) const;
88 };
89 
90 
91 /***************************************************************************//**
92 
93 ********************************************************************************/
94 class order_expr : public expr
95 {
96   friend class ExprManager;
97   friend class ExprIterator;
98   friend class expr;
99 
100 public:
101   enum order_type_t
102   {
103     ordered,
104     unordered
105   };
106 
107 protected:
108   order_type_t theType;
109   expr*       theExpr;
110 
111 protected:
112   order_expr(CompilerCB* ccb, static_context* sctx, const QueryLoc&, order_type_t, expr*);
113 
114 public:
get_type()115   order_type_t get_type() const { return theType; }
116 
get_expr()117   expr* get_expr() const { return theExpr; }
118 
119   void compute_scripting_kind();
120 
121   expr* cloneImpl(substitution_t& s) const;
122 
123   void accept(expr_visitor&);
124 
125   std::ostream& put(std::ostream&) const;
126 };
127 
128 
129 /***************************************************************************//**
130 
131 ********************************************************************************/
132 class validate_expr : public expr
133 {
134   friend class ExprManager;
135   friend class ExprIterator;
136   friend class expr;
137 
138 protected:
139   ParseConstants::validation_mode_t theMode;
140   store::Item_t                     theTypeName;
141   rchandle<TypeManager>             theTypeMgr;
142   expr*                            theExpr;
143 
144 protected:
145   validate_expr(
146         CompilerCB* ccb,
147         static_context* sctx,
148         const QueryLoc&,
149         ParseConstants::validation_mode_t,
150         const store::Item_t& aTypeName,
151         expr*,
152         rchandle<TypeManager>);
153 
154 public:
get_expr()155   expr* get_expr() const { return theExpr; }
156 
get_type_name()157   const store::Item* get_type_name() const { return theTypeName; }
158 
get_typemgr()159   TypeManager* get_typemgr() const { return theTypeMgr.getp(); }
160 
get_valmode()161   ParseConstants::validation_mode_t get_valmode() const { return theMode; }
162 
163   void compute_scripting_kind();
164 
165   expr* cloneImpl(substitution_t& s) const;
166 
167   void accept(expr_visitor&);
168 
169   std::ostream& put(std::ostream&) const;
170 };
171 
172 
173 /***************************************************************************//**
174   Base for expression classes that require a namespace context
175 ********************************************************************************/
176 class namespace_context_base_expr : public expr
177 {
178   friend class expr;
179 
180 protected:
181   NamespaceContext_t theNSCtx;
182 
183 protected:
184   namespace_context_base_expr(
185       CompilerCB* ccb,
186       static_context* sctx,
187       const QueryLoc& loc,
188       expr_kind_t kind,
189       const namespace_context* aNSCtx);
190 
191 public:
192   const namespace_context* getNSCtx() const;
193 };
194 
195 
196 /***************************************************************************//**
197   Base for cast, treat, promote, castable, instanceof
198 ********************************************************************************/
199 class cast_or_castable_base_expr : public expr
200 {
201   friend class ExprIterator;
202   friend class expr;
203 
204 protected:
205   expr     * theInputExpr;
206   xqtref_t   theTargetType;
207 
208 protected:
209   cast_or_castable_base_expr(
210         CompilerCB* ccb,
211         static_context* sctx,
212         const QueryLoc& loc,
213         expr_kind_t kind,
214         expr* input,
215         const xqtref_t& type);
216 
217 public:
get_input()218   expr* get_input() const { return theInputExpr; }
219 
220   xqtref_t get_target_type() const;
221 
222   void set_target_type(xqtref_t target);
223 
224   void compute_scripting_kind();
225 };
226 
227 
228 /***************************************************************************//**
229   Base for cast, treat, promote
230 ********************************************************************************/
231 class cast_base_expr : public cast_or_castable_base_expr
232 {
233   friend class ExprIterator;
234 
235 protected:
236   cast_base_expr(
237         CompilerCB* ccb,
238         static_context* sctx,
239         const QueryLoc& loc,
240         expr_kind_t kind,
241         expr* input,
242         const xqtref_t& type);
243 };
244 
245 
246 /***************************************************************************//**
247   CastExpr ::= UnaryExpr ( "cast" "as" SingleType )?
248 
249   SingleType ::= AtomicType "?"?
250 ********************************************************************************/
251 class cast_expr : public cast_base_expr
252 {
253   friend class ExprManager;
254   friend class ExprIterator;
255   friend class expr;
256 
257 protected:
258   cast_expr(
259       CompilerCB* ccb,
260       static_context* sctx,
261       const QueryLoc&,
262       expr*,
263       const xqtref_t&);
264 
265 public:
266   bool is_optional() const;
267 
268   expr* cloneImpl(substitution_t& s) const;
269 
270   void accept(expr_visitor&);
271 
272   std::ostream& put(std::ostream&) const;
273 };
274 
275 
276 /***************************************************************************//**
277   TreatExpr ::= CastableExpr ( "treat" "as" SequenceType )?
278 
279   theCheckPrime : Normally, this is true. If false, then during runtime, only
280                   the cardinality of theInputExpr will be checked w.r.t. the
281                   quantifier of theTargetType. theCheckPrime is set to false
282                   by the optimizer, if it discovers that the prime type of the
283                   static type of theInputExpr is a subtype of the prime type of
284                   theTargetType.
285 
286   theFnQName    : Stores the QName of the function, if the treat expr is used
287                   to cast the function's body to its result type
288 ********************************************************************************/
289 class treat_expr : public cast_base_expr
290 {
291   friend class ExprManager;
292   friend class ExprIterator;
293   friend class expr;
294 
295 protected:
296   TreatIterator::ErrorKind theErrorKind;
297   bool                     theCheckPrime;
298   store::Item_t            theQName;
299 
300 protected:
301   treat_expr(
302         CompilerCB* ccb,
303         static_context* sctx,
304         const QueryLoc&,
305         expr*,
306         const xqtref_t&,
307         TreatIterator::ErrorKind err,
308         bool check_prime = true,
309         store::Item* qname = NULL);
310 
311 public:
get_err()312   TreatIterator::ErrorKind get_err() const { return theErrorKind; }
313 
get_check_prime()314   bool get_check_prime() const { return theCheckPrime; }
315 
set_check_prime(bool check_prime)316   void set_check_prime(bool check_prime) { theCheckPrime = check_prime; }
317 
set_qname(const store::Item_t & qname)318   void set_qname(const store::Item_t& qname) { theQName = qname; }
319 
get_qname()320   store::Item_t get_qname() const { return theQName; }
321 
322   expr* cloneImpl(substitution_t& s) const;
323 
324   void accept(expr_visitor&);
325 
326   std::ostream& put(std::ostream&) const;
327 };
328 
329 
330 /***************************************************************************//**
331   This is an internal zorba expr. Its semantics are the following:
332 
333   1. Let "input sequence" be the result of theInputExpr, and "output sequence"
334      be the result of the promote_expr.
335 
336   2. The input sequence is assumed to be mepty or consist of atomic items only.
337 
338   4. theTargetType is always a subtype of xs:anyAtomicType*
339 
340   5. Raise error if the cardinality of the input sequence is not compatible with
341      the quantifier of theTargetType.
342 
343   6. For each item I in the input sequence, let F(I) be the result of the
344      function defined as follows:
345 
346      - Let "actual type" be the dynamic type of I, and "target type" be the prime
347        type of theTargetType.
348      - If the target type is the NONE type, F(I) = raise error, else
349      - If the actual type is a subtype of the target type, F(I) = I, else
350      - If the actual type is untypedAtomic and the target type is not QName,
351        F(I) = cast(I, target type), else
352      - If the actual type is (subtype of) decimal and the target type is float,
353        F(I) = cast(I, target type), else
354      - If the actual type is (subtype of) decimal or float and the target type is double,
355        F(I) = cast(I, target type), else
356      - If the actual type is anyURI and the target type is string,
357        F(I) = cast(I, string), else
358      - F(I) = raise error
359 
360   4. Put F(I) in the output sequence.
361 
362   theFnQName:
363   -----------
364   Stores the QName of the function, if the promote expr is used to cast the
365   function's body to its result type
366 
367 ********************************************************************************/
368 class promote_expr : public cast_base_expr
369 {
370   friend class ExprManager;
371   friend class ExprIterator;
372   friend class expr;
373 
374 protected:
375   PromoteIterator::ErrorKind theErrorKind;
376   store::Item_t              theQName;
377 
378 protected:
379   promote_expr(
380       CompilerCB* ccb,
381       static_context* sctx,
382       const QueryLoc& loc,
383       expr* input,
384       const xqtref_t& type,
385       PromoteIterator::ErrorKind err,
386       store::Item* qname);
387 
388 public:
389   expr* cloneImpl(substitution_t& s) const;
390 
get_err()391   PromoteIterator::ErrorKind get_err() const { return theErrorKind; }
392 
set_qname(const store::Item_t & qname)393   void set_qname(const store::Item_t& qname) { theQName = qname; }
394 
get_qname()395   store::Item_t get_qname() const { return theQName; }
396 
397   void accept(expr_visitor&);
398 
399   std::ostream& put(std::ostream&) const;
400 };
401 
402 
403 /***************************************************************************//**
404   Base for castable, instanceof
405 ********************************************************************************/
406 class castable_base_expr : public cast_or_castable_base_expr
407 {
408   friend class ExprIterator;
409 
410 protected:
411   castable_base_expr(
412         CompilerCB* ccb,
413         static_context* sctx,
414         const QueryLoc&,
415         expr_kind_t kind,
416         expr*,
417         const xqtref_t&);
418 };
419 
420 
421 /***************************************************************************//**
422   CastableExpr ::= CastExpr ( "castable" "as" SingleType )?
423 
424   SingleType ::= AtomicType "?"?
425 ********************************************************************************/
426 class castable_expr : public castable_base_expr
427 {
428   friend class ExprManager;
429   friend class ExprIterator;
430   friend class expr;
431 
432 protected:
433   castable_expr(
434       CompilerCB* ccb,
435       static_context* sctx,
436       const QueryLoc&,
437       expr*,
438       const xqtref_t&);
439 
440 public:
441   bool is_optional() const;
442 
443   expr* cloneImpl(substitution_t& s) const;
444 
445   void accept(expr_visitor&);
446 
447   std::ostream& put(std::ostream&) const;
448 };
449 
450 
451 /***************************************************************************//**
452   InstanceofExpr ::= TreatExpr ( "instance" "of" SequenceType )?
453 
454   theCheckPrimeOnly :
455   Normally, this is false. It is set to true only if this is an instanceof expr
456   that is created during the translation of a PredicateList (see translator.cpp).
457   This flag is used during the PartialEval rule.
458 
459 ********************************************************************************/
460 class instanceof_expr : public castable_base_expr
461 {
462   friend class ExprManager;
463   friend class ExprIterator;
464   friend class expr;
465 
466 protected:
467   bool theCheckPrimeOnly;
468 
469 protected:
470   instanceof_expr(
471       CompilerCB* ccb,
472       static_context* sctx,
473       const QueryLoc&,
474       expr*,
475       const xqtref_t&,
476       bool checkPrimeOnly = false);
477 
478 public:
getCheckPrimeOnly()479   bool getCheckPrimeOnly() const { return theCheckPrimeOnly; }
480 
481   expr* cloneImpl(substitution_t& s) const;
482 
483   void accept(expr_visitor&);
484 
485   std::ostream& put(std::ostream&) const;
486 };
487 
488 
489 /***************************************************************************//**
490   Casts the result of theInputExpr to a qname item. theInputExpr must return
491   exactly one item. If that item is a qname, it is simply returned to the
492   consumer iterator. If the input item is a string or an untyped item, it is
493   cast, if possible, to qname using the prefix-to-ns bindings in theNCtx.
494 
495   This is an internal zorba expr. It is used in the case of computed element or
496   attribute constructors when the name of the element/attribute is not a const
497   expr.
498 ********************************************************************************/
499 class name_cast_expr : public namespace_context_base_expr
500 {
501   friend class ExprManager;
502   friend class ExprIterator;
503   friend class expr;
504 
505 private:
506   expr*             theInputExpr;
507   bool               theIsAttrName;
508 
509 protected:
510   name_cast_expr(
511       CompilerCB* ccb,
512       static_context* sctx,
513       const QueryLoc&,
514       expr*,
515       const namespace_context*,
516       bool isAttr);
517 
518 public:
get_input()519   expr* get_input() const { return theInputExpr; }
520 
is_attr_name()521   bool is_attr_name() const { return theIsAttrName; }
522 
523   void compute_scripting_kind();
524 
525   expr* cloneImpl(substitution_t& s) const;
526 
527   void accept(expr_visitor&);
528 
529   std::ostream& put(std::ostream&) const;
530 };
531 
532 
533 /***************************************************************************//**
534   CompDocConstructor ::= "document" "{" Expr "}"
535 ********************************************************************************/
536 class doc_expr : public expr
537 {
538   friend class ExprManager;
539   friend class ExprIterator;
540   friend class expr;
541 
542 protected:
543   expr* theContent;
544   bool   theCopyInputNodes;
545 
546 protected:
547   doc_expr(CompilerCB* ccb, static_context* sctx, const QueryLoc&, expr* content, bool copyNodes);
548 
549 public:
getContent()550   expr* getContent() const { return theContent; }
551 
copyInputNodes()552   bool copyInputNodes() const { return theCopyInputNodes; }
553 
setCopyInputNodes()554   void setCopyInputNodes() { theCopyInputNodes = true; }
555 
556   void compute_scripting_kind();
557 
558   expr* cloneImpl(substitution_t& s) const;
559 
560   void accept(expr_visitor&);
561 
562   std::ostream& put(std::ostream&) const;
563 };
564 
565 
566 
567 /***************************************************************************//**
568 
569   CompElemConstructor ::= "element" (QName | ("{" Expr "}")) "{" Expr? "}"
570 
571 
572   DirElemConstructor ::= "<" QName DirAttributeList?
573                          ("/>" |
574                           (">" DirElemContentList? "</" QName S? ">"))
575 
576   DirElemContentList ::= DirElemContent+
577 
578   DirElemContent ::= DirectConstructor |
579                      CDataSection |
580                      CommonContent |
581                      ElementContentChar
582 
583   CDataSection ::= "<![CDATA[" CDataSectionContents "]]>"
584 
585   CDataSectionContents ::= (Char* - (Char* ']]>' Char*))
586 
587   ElementContentChar ::= Char - [{}<&]
588 
589   CommonContent ::= PredefinedEntityRef | CharRef | "{{" | "}}" | EnclosedExpr
590 
591 ********************************************************************************/
592 class elem_expr : public namespace_context_base_expr
593 {
594   friend class ExprManager;
595   friend class ExprIterator;
596   friend class expr;
597 
598 protected:
599   expr* theQNameExpr;
600   expr* theAttrs;
601   expr* theContent;
602   bool   theCopyInputNodes;
603 
604 protected:
605   elem_expr(
606         CompilerCB* ccb,
607         static_context* sctx,
608         const QueryLoc&,
609         expr* qnameExpr,
610         expr* attrs,
611         expr* content,
612         const namespace_context* nsCtx,
613         bool copyNodes);
614 
615   elem_expr(
616         CompilerCB* ccb,
617         static_context* sctx,
618         const QueryLoc&,
619         expr* qnameExpr,
620         expr* content,
621         const namespace_context* nsCtx,
622         bool copyNodes);
623 
624 public:
getQNameExpr()625   expr* getQNameExpr() const { return theQNameExpr; }
626 
getContent()627   expr* getContent() const { return theContent; }
628 
getAttrs()629   expr* getAttrs() const { return theAttrs; }
630 
copyInputNodes()631   bool copyInputNodes() const { return theCopyInputNodes; }
632 
setCopyInputNodes()633   void setCopyInputNodes() { theCopyInputNodes = true; }
634 
635   void compute_scripting_kind();
636 
637   expr* cloneImpl(substitution_t& s) const;
638 
639   void accept(expr_visitor&);
640 
641   std::ostream& put(std::ostream&) const;
642 };
643 
644 
645 /******************************************************************************
646 
647   CompAttrConstructor ::= "attribute" (QName | ("{" Expr "}")) "{" Expr? "}"
648 
649   DirAttr ::= (S (QName S? "=" S? DirAttributeValue)
650 
651   DirAttributeValue ::= '"' QuoteAttrContentList? '"' |
652                         "'" AposAttrContentList? "'"
653 
654   QuoteAttrContentList ::= QuotAttrValueContent+
655 
656   AposAttrContentList ::= AposAttrValueContent+
657 
658   QuotAttrValueContent ::= EscapeQuot | QuotAttrContentChar | CommonContent
659 
660   AposAttrValueContent ::= EscapeApos | AposAttrContentChar | CommonContent
661 
662   QuotAttrContentChar ::= Char - ["{}<&]
663 
664   AposAttrContentChar ::= Char - ['{}<&]
665 
666   CommonContent ::= PredefinedEntityRef | CharRef | "{{" | "}}" | EnclosedExpr
667 
668 ********************************************************************************/
669 class attr_expr : public expr
670 {
671   friend class ExprManager;
672   friend class ExprIterator;
673   friend class expr;
674 
675 protected:
676   expr* theQNameExpr;
677   expr* theValueExpr;
678 
679 protected:
680   attr_expr(
681     CompilerCB* ccb,
682     static_context* sctx,
683     const QueryLoc& loc,
684     expr* aQNameExpr,
685     expr* aValueExpr);
686 
687 public:
getQNameExpr()688   expr* getQNameExpr() const { return theQNameExpr; }
689 
getValueExpr()690   expr* getValueExpr() const { return theValueExpr; }
691 
692   const store::Item* getQName() const;
693 
694   void compute_scripting_kind();
695 
696   expr* cloneImpl(substitution_t& s) const;
697 
698   void accept(expr_visitor&);
699 
700   std::ostream& put(std::ostream&) const;
701 };
702 
703 
704 /***************************************************************************//**
705 
706 ********************************************************************************/
707 class text_expr : public expr
708 {
709   friend class ExprManager;
710   friend class ExprIterator;
711   friend class expr;
712 
713 public:
714   typedef enum
715   {
716     text_constructor,
717     comment_constructor
718   } text_constructor_type;
719 
720 protected:
721   text_constructor_type type;
722   expr*                theContentExpr;
723 
724 protected:
725   text_expr(
726       CompilerCB* ccb,
727       static_context* sctx,
728       const QueryLoc&,
729       text_constructor_type,
730       expr*);
731 
732 public:
get_text()733   expr* get_text() const { return theContentExpr; }
734 
get_type()735   text_constructor_type get_type() const { return type; }
736 
737   void compute_scripting_kind();
738 
739   expr* cloneImpl(substitution_t& s) const;
740 
741   void accept(expr_visitor&);
742 
743   std::ostream& put(std::ostream&) const;
744 };
745 
746 
747 /***************************************************************************//**
748 
749 ********************************************************************************/
750 class pi_expr : public expr
751 {
752   friend class ExprManager;
753   friend class ExprIterator;
754   friend class expr;
755 
756 protected:
757   expr* theTargetExpr;
758   expr* theContentExpr;
759 
760 protected:
761   pi_expr(CompilerCB* ccb, static_context* sctx, const QueryLoc&, expr*, expr*);
762 
763 public:
get_target_expr()764   expr* get_target_expr() const { return theTargetExpr; }
765 
get_content_expr()766   expr* get_content_expr() const { return theContentExpr; }
767 
768   void compute_scripting_kind();
769 
770   expr* cloneImpl(substitution_t& s) const;
771 
772   void accept(expr_visitor&);
773 
774   std::ostream& put(std::ostream&) const;
775 };
776 
777 
778 /***************************************************************************//**
779 
780 ********************************************************************************/
781 class const_expr : public expr
782 {
783   friend class ExprManager;
784   friend class ExprIterator;
785   friend class expr;
786 
787 protected:
788   store::Item_t theValue;
789 
790 protected:
791   const_expr(CompilerCB*, static_context*, const QueryLoc&, zstring& sval);
792 
793   const_expr(CompilerCB*, static_context*, const QueryLoc&, const std::string& sval);
794 
795   const_expr(CompilerCB*, static_context*, const QueryLoc&, const char* sval);
796 
797   const_expr(CompilerCB*, static_context*, const QueryLoc&, xs_integer);
798 
799   const_expr(CompilerCB*, static_context*, const QueryLoc&, xs_decimal);
800 
801   const_expr(CompilerCB*, static_context*, const QueryLoc&, xs_double);
802 
803   const_expr(CompilerCB*, static_context*, const QueryLoc&, xs_boolean);
804 
805   const_expr(CompilerCB*, static_context*, const QueryLoc&, const store::Item_t&);
806 
807   const_expr(CompilerCB*, static_context*, const QueryLoc&, const char* ns, const char* pre, const char* local);
808 
809 public:
get_val()810   store::Item* get_val() const { return theValue.getp(); }
811 
812   void compute_scripting_kind();
813 
814   expr* cloneImpl(substitution_t& s) const;
815 
816   void accept(expr_visitor&);
817 
818   std::ostream& put(std::ostream&) const;
819 };
820 
821 
822 /***************************************************************************//**
823 
824 ********************************************************************************/
825 class extension_expr : public expr
826 {
827   friend class ExprManager;
828   friend class ExprIterator;
829   friend class expr;
830 
831 protected:
832   std::vector<pragma*> thePragmas;
833   expr*                theExpr;
834 
835 protected:
836   extension_expr(CompilerCB* ccb, static_context* sctx, const QueryLoc&);
837 
838   extension_expr(CompilerCB* ccb, static_context* sctx, const QueryLoc&, expr*);
839 
840 public:
add(pragma * p)841   void add(pragma* p) { thePragmas.push_back(p); }
842 
get_expr()843   expr* get_expr() const { return theExpr; }
844 
845   void compute_scripting_kind();
846 
847   expr* cloneImpl(substitution_t& subst) const;
848 
849   void accept(expr_visitor&);
850 
851   std::ostream& put(std::ostream&) const;
852 };
853 
854 
855 /////////////////////////////////////////////////////////////////////////
856 //                                                                     //
857 //  XQuery 3.0 expressions                                             //
858 //  [http://www.w3.org/TR/xquery-3/]                                   //
859 //                                                                     //
860 /////////////////////////////////////////////////////////////////////////
861 
862 
863 /***************************************************************************//**
864   [169] TryCatchExpr ::= TryClause CatchClauseList
865 
866   [170] TryClause ::= "try" "{" TryTargetExpr "}"
867 
868   [171] TryTargetExpr ::= Expr
869 
870         CatchClauseList := CatchClause+
871 
872   [172] CatchClause ::= "catch" CatchErrorList "{" Expr "}"
873 
874   [173] CatchErrorList ::= NameTest ("|" NameTest)*
875 
876 ********************************************************************************/
877 
878 class catch_clause
879 {
880   friend class expr;
881   friend class trycatch_expr;
882   friend class ExprManager;
883 
884 public:
885   enum var_type
886   {
887     err_code = 0,
888     err_desc,
889     err_value,
890     err_module,
891     err_line_no,
892     err_column_no,
893     zerr_stack_trace
894   };
895 
896 public:
897   typedef rchandle<NodeNameTest> nt_t;
898   typedef std::vector<nt_t> nt_list_t;
899   typedef std::map<int, var_expr*> var_map_t;
900 
901 protected:
902   nt_list_t         theNameTests;
903   var_map_t         theVarMap;
904   CompilerCB *const theCCB;
905 
906   catch_clause(CompilerCB* ccb);
907 
908 public:
set_nametests(nt_list_t & a)909   void set_nametests(nt_list_t& a) { theNameTests = a; }
910 
get_nametests()911   nt_list_t& get_nametests() { return theNameTests; }
912 
add_nametest_h(nt_t n)913   void add_nametest_h(nt_t n) { theNameTests.push_back(n); }
914 
set_vars(var_map_t & a)915   void set_vars(var_map_t& a) { theVarMap = a; }
916 
get_vars()917   var_map_t& get_vars() { return theVarMap; }
918 
add_var(var_type v,var_expr * n)919   void add_var(var_type v, var_expr* n) { theVarMap[v] = n; }
920 
921   catch_clause* clone(expr::substitution_t& subst) const;
922 };
923 
924 
925 class trycatch_expr : public expr
926 {
927   friend class ExprManager;
928   friend class ExprIterator;
929   friend class expr;
930 
931 protected:
932   expr*                      theTryExpr;
933   std::vector<expr*>         theCatchExprs;
934   std::vector<catch_clause*> theCatchClauses;
935 
936 protected:
937   trycatch_expr(CompilerCB* ccb, static_context* sctx, const QueryLoc&, expr* tryExpr);
938 
939 public:
get_try_expr()940   expr* get_try_expr() const { return theTryExpr; }
941 
set_try_expr(expr * e)942   void set_try_expr(expr* e) { theTryExpr = e; }
943 
get_catch_expr(csize i)944   expr* get_catch_expr(csize i) const { return theCatchExprs[i]; }
945 
946   void add_catch_expr(expr* e);
947 
948   void add_clause(catch_clause* cc);
949 
clause_count()950   csize clause_count() const { return theCatchClauses.size(); }
951 
952   const catch_clause* operator[](csize i) const { return theCatchClauses[i]; }
953 
954   void compute_scripting_kind();
955 
956   expr* cloneImpl(substitution_t& subst) const;
957 
958   void accept(expr_visitor&);
959 
960   std::ostream& put(std::ostream&) const;
961 };
962 
963 
964 /////////////////////////////////////////////////////////////////////////
965 //                                                                     //
966 //  Zorba expressions                                                  //
967 //                                                                     //
968 /////////////////////////////////////////////////////////////////////////
969 
970 
971 /*******************************************************************************
972   Normally, it is used to wrap a var_expr in order to represent a var reference
973   (see var_expr.h). But it may wrap any other kind of expr as well.
974 ********************************************************************************/
975 class wrapper_expr : public expr
976 {
977   friend class ExprIterator;
978   friend class expr;
979   friend class ExprManager;
980 
981 protected:
982   expr* theWrappedExpr;
983 
984 protected:
985   wrapper_expr(CompilerCB* ccb, static_context* sctx, const QueryLoc& loc, expr* wrapped);
986 
987 public:
get_expr()988   expr* get_expr() const { return theWrappedExpr; }
989 
set_expr(expr * e)990   void set_expr(expr* e) { theWrappedExpr = e;}
991 
992   void compute_scripting_kind();
993 
994   void accept(expr_visitor&);
995 
996   expr* cloneImpl(substitution_t& s) const;
997 
998   std::ostream& put(std::ostream&) const;
999 };
1000 
1001 
1002 /***************************************************************************//**
1003   dummy expression for call stack traces
1004 ********************************************************************************/
1005 class function_trace_expr : public expr
1006 {
1007   friend class ExprIterator;
1008   friend class expr;
1009   friend class ExprManager;
1010 
1011 protected:
1012   expr*        theExpr;
1013   store::Item_t theFunctionName;
1014   QueryLoc      theFunctionLocation;
1015   QueryLoc      theFunctionCallLocation;
1016   unsigned int  theFunctionArity;
1017 
1018 protected:
1019   function_trace_expr(
1020       CompilerCB* ccb,
1021       static_context* sctx,
1022       const QueryLoc& loc,
1023       expr* aChild);
1024 
1025   function_trace_expr(expr* aExpr);
1026 
1027 public:
1028   virtual ~function_trace_expr();
1029 
1030   void compute_scripting_kind();
1031 
1032   void accept(expr_visitor&);
1033 
1034   expr* cloneImpl(substitution_t& s) const;
1035 
1036   std::ostream& put(std::ostream&) const;
1037 
get_expr()1038   expr* get_expr() const { return theExpr; }
1039 
setFunctionName(store::Item_t aFunctionName)1040   void setFunctionName(store::Item_t aFunctionName)
1041   {
1042     theFunctionName = aFunctionName;
1043   }
1044 
getFunctionName()1045   store::Item_t getFunctionName() const
1046   {
1047     return theFunctionName;
1048   }
1049 
setFunctionLocation(const QueryLoc & loc)1050   void setFunctionLocation(const QueryLoc& loc)
1051   {
1052     theFunctionLocation = loc;
1053   }
1054 
getFunctionLocation()1055   const QueryLoc& getFunctionLocation() const
1056   {
1057     return theFunctionLocation;
1058   }
1059 
setFunctionCallLocation(const QueryLoc & loc)1060   void setFunctionCallLocation(const QueryLoc& loc)
1061   {
1062     theFunctionCallLocation = loc;
1063   }
1064 
getFunctionCallLocation()1065   const QueryLoc& getFunctionCallLocation() const
1066   {
1067     return theFunctionCallLocation;
1068   }
1069 
setFunctionArity(unsigned int arity)1070   void setFunctionArity(unsigned int arity)
1071   {
1072     theFunctionArity = arity;
1073   }
1074 
getFunctionArity()1075   unsigned int getFunctionArity() const
1076   {
1077     return theFunctionArity;
1078   }
1079 };
1080 
1081 
1082 /***************************************************************************//**
1083   There is no syntax corresponding to the eval_expr. Instead, an eval_expr is
1084   created by the translator whenever a call to the eval() function is reached.
1085 
1086   theExpr:
1087   --------
1088   The expr that computes the query string to be evaluated by eval.
1089 
1090   theOuterVarNames:
1091   -----------------
1092   The names of all the in-scope variables at the place where the call to the
1093   eval function appears at.
1094 
1095   theOuterVarTypes:
1096   -----------------
1097   The types of all the in-scope variables at the place where the call to the
1098   eval function appears at.
1099 
1100   theArgs:
1101   --------
1102   For each in-scope var, the vector contains an expr that returns the value of
1103   the var. The expr is either a reference to the var itself, or the domain expr
1104   of that var, if that var was inlined.
1105 
1106   theInnerScriptingKind:
1107   ----------------------
1108 
1109   theDoNodeCopy:
1110   --------------
1111 
1112 ********************************************************************************/
1113 class eval_expr : public namespace_context_base_expr
1114 {
1115   friend class ExprIterator;
1116   friend class expr;
1117   friend class ExprManager;
1118 
1119 protected:
1120   expr                      * theExpr;
1121 
1122   std::vector<store::Item_t>  theOuterVarNames;
1123 
1124   std::vector<xqtref_t>       theOuterVarTypes;
1125 
1126   std::vector<expr*>          theArgs;
1127 
1128   expr_script_kind_t          theInnerScriptingKind;
1129 
1130   bool                        theDoNodeCopy;
1131 
1132 protected:
1133   eval_expr(
1134       CompilerCB* ccb,
1135       static_context* sctx,
1136       const QueryLoc& loc,
1137       expr* e,
1138       expr_script_kind_t scriptingKind,
1139       namespace_context* nsCtx);
1140 
1141 public:
get_expr()1142   expr* get_expr() const { return theExpr; }
1143 
get_arg_expr(csize i)1144   expr* get_arg_expr(csize i) { return theArgs[i]; }
1145 
num_vars()1146   csize num_vars() const { return theOuterVarNames.size(); }
1147 
get_var_names()1148   const std::vector<store::Item_t>& get_var_names() const { return theOuterVarNames; }
1149 
get_var_types()1150   const std::vector<xqtref_t>& get_var_types() const { return theOuterVarTypes; }
1151 
add_var(var_expr * var)1152   void add_var(var_expr* var)
1153   {
1154     theOuterVarNames.push_back(var->get_name());
1155     theOuterVarTypes.push_back(var->get_return_type());
1156     theArgs.push_back(var);
1157   }
1158 
1159   expr_script_kind_t get_inner_scripting_kind() const;
1160 
getNodeCopy()1161   bool getNodeCopy() const { return theDoNodeCopy; }
1162 
setNodeCopy(bool v)1163   void setNodeCopy(bool v) { theDoNodeCopy = true; }
1164 
1165   void compute_scripting_kind();
1166 
1167   void accept(expr_visitor&);
1168 
1169   expr* cloneImpl(substitution_t& s) const;
1170 
1171   std::ostream& put(std::ostream&) const;
1172 };
1173 
1174 
1175 #ifdef ZORBA_WITH_DEBUGGER
1176 /***************************************************************************//**
1177   debugger expression
1178 
1179   theExpr:
1180   --------
1181   The wrapped expr
1182 
1183   theVars:
1184   --------
1185 
1186   theArgs:
1187   --------
1188 
1189   theIsVarDeclaration:
1190   --------------------
1191 ********************************************************************************/
1192 class debugger_expr : public namespace_context_base_expr
1193 {
1194   friend class ExprIterator;
1195   friend class expr;
1196   friend class ExprManager;
1197 
1198 private:
1199   expr*                      theExpr;
1200   checked_vector<var_expr*>  theVars;
1201   std::vector<expr*>         theArgs;
1202   bool                       theIsVarDeclaration;
1203 
1204 protected:
1205   debugger_expr(
1206       CompilerCB* ccb,
1207       static_context* sctx,
1208       const QueryLoc& loc,
1209       expr* aChild,
1210       namespace_context* nsCtx,
1211       bool aIsVarDeclaration);
1212 
1213 public:
get_expr()1214   expr* get_expr() const { return theExpr; }
1215 
isVarDeclaration()1216   bool isVarDeclaration() const { return theIsVarDeclaration; }
1217 
1218   void accept(expr_visitor&);
1219 
1220   std::ostream& put(std::ostream&) const;
1221 
var_count()1222   csize var_count() const { return theVars.size(); }
1223 
get_var(csize i)1224   const var_expr* get_var(csize i) const { return theVars[i]; }
1225 
add_var(var_expr * var,expr * arg)1226   void add_var(var_expr* var, expr* arg)
1227   {
1228     theVars.push_back(var);
1229     theArgs.push_back(arg);
1230   }
1231 
1232   void compute_scripting_kind();
1233 };
1234 #endif
1235 
1236 
1237 }
1238 #endif
1239 
1240 /*
1241  * Local variables:
1242  * mode: c++
1243  * End:
1244  */
1245 /* vim:set et sw=2 ts=2: */
1246