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_ROOT_TYPEMANAGER_H
18 #define ZORBA_ROOT_TYPEMANAGER_H
19 
20 #include <assert.h>
21 #include "types/typeconstants.h"
22 #include "types/typeimpl.h"
23 #include "types/typemanagerimpl.h"
24 #include "zorbautils/hashmap.h"
25 
26 namespace zorba {
27 
28 
29 /*******************************************************************************
30   RootTypeManager instantiates and stores all of the zorba built-in types. It
31   also defines various global, const data structs that are useful in various
32   type-related operations.
33 ********************************************************************************/
34 class RootTypeManager : public TypeManagerImpl
35 {
36   friend class GlobalEnvironment;
37   friend class TypeOps;
38   friend class XQType;
39   friend class TypeManagerImpl;
40   friend class AtomicXQType;
41 
42 public:
43   /**
44    * Pre-allocated XQType object for the "none" type
45    */
46   xqtref_t NONE_TYPE;
47 
48   /**
49    * Pre-allocated XQType object for the () sequence type (the empty sequence).
50    */
51   xqtref_t EMPTY_TYPE;
52 
53   /**
54    *  Pre-allocate XQType objects for item(), item()?, item()+, and item()*.
55    */
56   xqtref_t ITEM_TYPE_ONE;
57   xqtref_t ITEM_TYPE_QUESTION;
58   xqtref_t ITEM_TYPE_STAR;
59   xqtref_t ITEM_TYPE_PLUS;
60 
61   /**
62    * Pre-allocated any function item objects
63    */
64   xqtref_t ANY_FUNCTION_TYPE_ONE;
65   xqtref_t ANY_FUNCTION_TYPE_QUESTION;
66   xqtref_t ANY_FUNCTION_TYPE_STAR;
67   xqtref_t ANY_FUNCTION_TYPE_PLUS;
68 
69   /**
70    * Pre-allocated XQType and QNameItem objects for all of the 45 built-in atomic
71    * types of XQDM. Specifically, for each built-in atomic XQDM type T, we pre-
72    * allocate a QNameItem for the name of T (as defined by XMLSchema), and 4
73    * XQType objects representing the sequence types T, T?, T*, and T+.
74    */
75 #define ATOMIC_DECL(basename) \
76     store::Item_t XS_##basename##_QNAME; \
77     xqtref_t basename##_TYPE_ONE;\
78     xqtref_t basename##_TYPE_QUESTION; \
79     xqtref_t basename##_TYPE_STAR; \
80     xqtref_t basename##_TYPE_PLUS;
81 
82   ATOMIC_DECL(ANY_ATOMIC)
83   ATOMIC_DECL(UNTYPED_ATOMIC)
84   ATOMIC_DECL(STRING)
85   ATOMIC_DECL(NORMALIZED_STRING)      // derived
86   ATOMIC_DECL(TOKEN)                  // derived
87   ATOMIC_DECL(LANGUAGE)               // derived
88   ATOMIC_DECL(NMTOKEN)                // derived
89   ATOMIC_DECL(NAME)                   // derived
90   ATOMIC_DECL(NCNAME)                 // derived
91   ATOMIC_DECL(ID)                     // derived
92   ATOMIC_DECL(IDREF)                  // derived
93   ATOMIC_DECL(ENTITY)                 // derived
94   ATOMIC_DECL(DATETIME)
95   ATOMIC_DECL(DATE)
96   ATOMIC_DECL(TIME)
97   ATOMIC_DECL(DURATION)
98   ATOMIC_DECL(DT_DURATION)            // derived
99   ATOMIC_DECL(YM_DURATION)            // derived
100   ATOMIC_DECL(FLOAT)
101   ATOMIC_DECL(DOUBLE)
102   ATOMIC_DECL(DECIMAL)
103   ATOMIC_DECL(INTEGER)                // derived
104   ATOMIC_DECL(NON_POSITIVE_INTEGER)   // derived
105   ATOMIC_DECL(NEGATIVE_INTEGER)       // derived
106   ATOMIC_DECL(LONG)                   // derived
107   ATOMIC_DECL(INT)                    // derived
108   ATOMIC_DECL(SHORT)                  // derived
109   ATOMIC_DECL(BYTE)                   // derived
110   ATOMIC_DECL(NON_NEGATIVE_INTEGER)   // derived
111   ATOMIC_DECL(UNSIGNED_LONG)          // derived
112   ATOMIC_DECL(UNSIGNED_INT)           // derived
113   ATOMIC_DECL(UNSIGNED_SHORT)         // derived
114   ATOMIC_DECL(UNSIGNED_BYTE)          // derived
115   ATOMIC_DECL(POSITIVE_INTEGER)       // derived
116   ATOMIC_DECL(GYEAR_MONTH)
117   ATOMIC_DECL(GYEAR)
118   ATOMIC_DECL(GMONTH_DAY)
119   ATOMIC_DECL(GDAY)
120   ATOMIC_DECL(GMONTH)
121   ATOMIC_DECL(BOOLEAN)
122   ATOMIC_DECL(BASE64BINARY)
123   ATOMIC_DECL(HEXBINARY)
124   ATOMIC_DECL(ANY_URI)
125   ATOMIC_DECL(QNAME)
126   ATOMIC_DECL(NOTATION)
127 #undef ATOMIC_DECL
128 
129 #ifdef ZORBA_WITH_JSON
130   /**
131    * Pre-allocate XQType objects for the following sequence types:
132    *
133    * N, N?, N+, N*, where N is the jdm::null atomic type
134    */
135   store::Item_t JS_NULL_QNAME;
136   xqtref_t JS_NULL_TYPE_ONE;
137   xqtref_t JS_NULL_TYPE_QUESTION;
138   xqtref_t JS_NULL_TYPE_STAR;
139   xqtref_t JS_NULL_TYPE_PLUS;
140 #endif
141 
142   /**
143    *  Pre-allocate XQType objects for structured-item(), structured-item()?,
144    *  structured-item()+, and structured-item()*.
145    */
146   xqtref_t STRUCTURED_ITEM_TYPE_ONE;
147   xqtref_t STRUCTURED_ITEM_TYPE_QUESTION;
148   xqtref_t STRUCTURED_ITEM_TYPE_STAR;
149   xqtref_t STRUCTURED_ITEM_TYPE_PLUS;
150 
151 #ifdef ZORBA_WITH_JSON
152   /**
153    * Pre-allocate XQType objects for the following JSONTest sequence types:
154    *
155    * N(), N()?, N()+, N()*, where N is one of json-item, object, or array
156    */
157 #define JSON_DECL(basename) \
158     xqtref_t basename##_TYPE_ONE;\
159     xqtref_t basename##_TYPE_QUESTION; \
160     xqtref_t basename##_TYPE_STAR; \
161     xqtref_t basename##_TYPE_PLUS;\
162 
163   JSON_DECL(JSON_ITEM);
164   JSON_DECL(JSON_OBJECT);
165   JSON_DECL(JSON_ARRAY);
166 
167 #undef JSON_DECL
168 
169   /**
170    * Maps a [json item kind, quantifier] to the corresponding pre-allocated json type
171    */
172   static const XQType* JSON_TYPES_MAP[3][4];
173 
174 #endif // ZORBA_WITH_JSON
175 
176   /**
177    * Pre-allocate XQType objects for the following KindTest sequence types:
178    *
179    * N(), N()?, N()+, N()*, where N is one of node, document-node, text, comment,
180    * or processing-instruction.
181    *
182    * N(xs:untyped), N(xs:untyped)?, N(xs:untyped)+, N(xs:untyped)*, where N is
183    * one of node or document.
184    *
185    * element(*, xs:anyType), element(*, xs:anyType)?, element(*, xs:anyType)+,
186    * element(*, xs:anyType)*
187    *
188    * element(*, xs:untyped), element(*, xs:untyped)?, element(*, xs:untyped)+,
189    * element(*, xs:untyped)*
190    *
191    * attribute(*, xs:anySimpleType), attribute(*, xs:anySimpleType)?,
192    * attribute(*, xs:anySimpleType)+, attribute(*, xs:anySimpleType)*
193    *
194    * attribute(*, xs:untypedAtomic), attribute(*, xs:untypedAtomic)?,
195    * attribute(*, xs:untypedAtomic)+, attribute(*, xs:untypedAtomic)*
196    *
197    * Note: these types are not part of XMLSchema, and as a result, do not have qnames.
198    */
199 #define ALL_NODE_TYPE_DECL(basename)            \
200   xqtref_t basename##_TYPE_ONE;                 \
201   xqtref_t basename##_TYPE_QUESTION;            \
202   xqtref_t basename##_TYPE_STAR;                \
203   xqtref_t basename##_TYPE_PLUS
204 
205   ALL_NODE_TYPE_DECL(ANY_NODE);
206   ALL_NODE_TYPE_DECL(DOCUMENT);
207   ALL_NODE_TYPE_DECL(ELEMENT);
208   ALL_NODE_TYPE_DECL(ATTRIBUTE);
209   ALL_NODE_TYPE_DECL(TEXT);
210   ALL_NODE_TYPE_DECL(PI);
211   ALL_NODE_TYPE_DECL(COMMENT);
212 
213   ALL_NODE_TYPE_DECL(ANY_NODE_UNTYPED);
214   ALL_NODE_TYPE_DECL(DOCUMENT_UNTYPED);
215   ALL_NODE_TYPE_DECL(ELEMENT_UNTYPED);
216   ALL_NODE_TYPE_DECL(ATTRIBUTE_UNTYPED);
217 
218 #undef ALL_NODE_TYPE_DECL
219 
220   /**
221    * Pre-allocated XQType and QNameItem objects for the remaining build-in
222    * XQDM types (not including the built-in list types xs:IDREFS, xs:NMTOKENS,
223    * and xs:ENTITIES).
224    */
225   xqtref_t ANY_TYPE;
226   xqtref_t UNTYPED_TYPE;
227   xqtref_t ANY_SIMPLE_TYPE;
228 
229   store::Item_t XS_ANY_TYPE_QNAME;
230   store::Item_t XS_UNTYPED_QNAME;
231   store::Item_t XS_ANY_SIMPLE_TYPE_QNAME;
232 
233 private:
234 
235   /**
236    * Maps each atomic type code and each quantifier code to a built-in XQType
237    * object for that built-in atomic type and quantifier.
238    */
239   xqtref_t* m_atomic_typecode_map[store::XS_LAST][TypeConstants::QUANTIFIER_LIST_SIZE];
240 
241   /**
242    * Maps the typecode of a built-in atomic type to its qname.
243    */
244   store::Item* m_atomic_typecode_qname_map[store::XS_LAST];
245 
246   /**
247    *  Maps the qname of a built-in atomic type to its typecode.
248    */
249   typedef zorba::HashMap<store::Item*,
250                          store::SchemaTypeCode,
251                          qname_hash_equals> qnametype_map_t;
252 
253   qnametype_map_t m_atomic_qnametype_map;
254 
255   /**
256    * For each pair T1, T2 of built-in atomic types, ATOMIC_SUBTYPE_MATRIX[T1][T2]
257    * contains true if T1 is a subtype of T2; otherwise it contains false.
258    */
259   static const bool
260   ATOMIC_SUBTYPE_MATRIX[store::XS_LAST][store::XS_LAST];
261 
262   /**
263    * For each quantifier Q, let S(Q) be the following function:
264    *
265    * S(one) = { 1 }
266    * S(?)   = { 0, 1 }
267    * S(*)   = { 0, 1, INF }
268    * S(+)   = { 1, INF }
269    *
270    * Then, QUANT_SUBTYPE_MATRIX[q1][q2] is true iff S(q1) is a subset of S(q2).
271    */
272   static const bool
273   QUANT_SUBTYPE_MATRIX[TypeConstants::QUANTIFIER_LIST_SIZE]
274                       [TypeConstants::QUANTIFIER_LIST_SIZE];
275 
276   /**
277    * Let IS be the inverse of the function S defined above. Then:
278    *
279    * QUANT_UNION_MATRIX[q1][q2] = q <==> q is IS(S(q1) union S(q2))
280    *
281    * In other words, q is the "minimum" quantifier that is equally or more
282    *  permissive than both q1 and q2.
283    */
284   static const TypeConstants::quantifier_t
285   QUANT_UNION_MATRIX[TypeConstants::QUANTIFIER_LIST_SIZE]
286                     [TypeConstants::QUANTIFIER_LIST_SIZE];
287 
288   /**
289    *  QUANT_INTERS_MATRIX[q1][q2] = q <==> q is IS(S(q1) intersect S(q2))
290    */
291   static const TypeConstants::quantifier_t
292   QUANT_INTERS_MATRIX[TypeConstants::QUANTIFIER_LIST_SIZE]
293                      [TypeConstants::QUANTIFIER_LIST_SIZE];
294 
295   /**
296    * For each quatifier q, QUANT_MAX_CNT[q] gives the maximum number of items
297    * that can appear in an instance of a sequence type quantified with q.
298    * QUANT_MAX_CNT[q] is either 1 or 2, with 2 meaning infinity.
299    */
300   static const int QUANT_MAX_CNT[TypeConstants::QUANTIFIER_LIST_SIZE];
301 
302   /**
303    * For each quatifier q, QUANT_MIN_CNT[q] gives the minimum number of items
304    * that can appear in an instance of a sequence type quantified with q.
305    */
306   static const int QUANT_MIN_CNT[TypeConstants::QUANTIFIER_LIST_SIZE];
307 
308   /**
309    * For each pair T1, T2 of built-in atomic types, ATOMIC_CAST_MATRIX[T1][T2]
310    * says whether an instance of T1 can be cast to an instance of T2. There are
311    * 3 posiblities: YES means that such a cast is always possible, MAYBE means
312    * that a cast may be possible depending on the particular value of the T1
313    * instance, and NO means that the cast is never possible.
314    */
315   static const TypeConstants::castable_t
316   ATOMIC_CAST_MATRIX[store::XS_LAST][store::XS_LAST];
317 
318 public:
319   ~RootTypeManager();
320 
321 private:
322   RootTypeManager();
323 };
324 
325 }
326 
327 #endif /* ZORBA_TYPESYSTEM_H */
328 /*
329  * Local variables:
330  * mode: c++
331  * End:
332  */
333 /* vim:set et sw=2 ts=2: */
334