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 <assert.h>
19 
20 #include <zorba/identtypes.h>
21 #include <zorba/typeident.h>
22 
23 #include "diagnostics/assert.h"
24 #include "diagnostics/util_macros.h"
25 
26 #include "system/globalenv.h"
27 #include "zorbamisc/ns_consts.h"
28 
29 #include "types/node_test.h"
30 #include "types/typemanagerimpl.h"
31 #include "types/typeimpl.h"
32 #include "types/root_typemanager.h"
33 #include "types/schema/schema.h"
34 
35 #include "store/api/iterator.h"
36 #include "store/api/item_factory.h"
37 
38 #include "runtime/function_item/function_item.h"
39 
40 #include "compiler/parser/query_loc.h"
41 
42 #include "functions/signature.h"
43 
44 #include "util/string_util.h"
45 
46 #include "zorbaserialization/serialize_template_types.h"
47 #include "zorbaserialization/serialize_zorba_types.h"
48 
49 #ifdef ZORBA_XBROWSER
50 #include "DOMQName.h"
51 #endif
52 
53 
54 using namespace zorba;
55 #ifndef ZORBA_NO_XMLSCHEMA
56 XERCES_CPP_NAMESPACE_USE
57 #endif
58 
59 
SERIALIZE_INTERNAL_METHOD(TypeManager)60 SERIALIZE_INTERNAL_METHOD(TypeManager)
61 
62 SERIALIZABLE_CLASS_VERSIONS(TypeManagerImpl)
63 
64 
65 /***************************************************************************//**
66 
67 ********************************************************************************/
68 void TypeManager::serialize(::zorba::serialization::Archiver& ar)
69 {
70   ar & m_level;
71 }
72 
73 
74 /***************************************************************************//**
75 
76 ********************************************************************************/
serialize(::zorba::serialization::Archiver & ar)77 void TypeManagerImpl::serialize(::zorba::serialization::Archiver& ar)
78 {
79   serialize_baseclass(ar, (TypeManager*)this);
80   SERIALIZE_TYPEMANAGER(TypeManager, m_parent);
81   ar & m_schema;
82 }
83 
84 
85 /***************************************************************************//**
86 
87 ********************************************************************************/
~TypeManagerImpl()88 TypeManagerImpl::~TypeManagerImpl()
89 {
90   delete m_schema;
91 }
92 
93 
94 /***************************************************************************//**
95 
96 ********************************************************************************/
initializeSchema()97 void TypeManagerImpl::initializeSchema()
98 {
99   if ( m_schema == NULL )
100     m_schema = new Schema(this);
101 }
102 
103 
terminateSchema()104 void TypeManagerImpl::terminateSchema()
105 {
106   if ( m_schema != NULL )
107   {
108     delete m_schema;
109     m_schema = NULL;
110   }
111 }
112 
113 
114 /***************************************************************************//**
115 
116 ********************************************************************************/
create_any_type() const117 xqtref_t TypeManagerImpl::create_any_type() const
118 {
119   return GENV_TYPESYSTEM.ANY_TYPE;
120 }
121 
122 
123 /***************************************************************************//**
124 
125 ********************************************************************************/
create_untyped_type() const126 xqtref_t TypeManagerImpl::create_untyped_type() const
127 {
128   return GENV_TYPESYSTEM.UNTYPED_TYPE;
129 }
130 
131 
132 /***************************************************************************//**
133 
134 ********************************************************************************/
create_any_simple_type() const135 xqtref_t TypeManagerImpl::create_any_simple_type() const
136 {
137   return GENV_TYPESYSTEM.ANY_SIMPLE_TYPE;
138 }
139 
140 
141 /***************************************************************************//**
142 
143 ********************************************************************************/
create_empty_type() const144 xqtref_t TypeManagerImpl::create_empty_type() const
145 {
146   return GENV_TYPESYSTEM.EMPTY_TYPE;
147 }
148 
149 
150 /***************************************************************************//**
151 
152 ********************************************************************************/
create_none_type() const153 xqtref_t TypeManagerImpl::create_none_type() const
154 {
155   return GENV_TYPESYSTEM.NONE_TYPE;
156 }
157 
158 
159 /***************************************************************************//**
160 
161 ********************************************************************************/
create_any_item_type(TypeConstants::quantifier_t q) const162 xqtref_t TypeManagerImpl::create_any_item_type(TypeConstants::quantifier_t q) const
163 {
164   switch(q)
165   {
166     case TypeConstants::QUANT_ONE:
167       return GENV_TYPESYSTEM.ITEM_TYPE_ONE;
168     case TypeConstants::QUANT_QUESTION:
169       return GENV_TYPESYSTEM.ITEM_TYPE_QUESTION;
170     case TypeConstants::QUANT_STAR:
171       return GENV_TYPESYSTEM.ITEM_TYPE_STAR;
172     case TypeConstants::QUANT_PLUS:
173       return GENV_TYPESYSTEM.ITEM_TYPE_PLUS;
174     default:
175       return xqtref_t(0);
176   }
177 }
178 
179 
180 /***************************************************************************//**
181 
182 ********************************************************************************/
create_any_function_type(TypeConstants::quantifier_t quantifier) const183 xqtref_t TypeManagerImpl::create_any_function_type(
184     TypeConstants::quantifier_t quantifier) const
185 {
186   switch(quantifier)
187   {
188     case TypeConstants::QUANT_ONE:
189       return GENV_TYPESYSTEM.ANY_FUNCTION_TYPE_ONE;
190     case TypeConstants::QUANT_QUESTION:
191       return GENV_TYPESYSTEM.ANY_FUNCTION_TYPE_QUESTION;
192     case TypeConstants::QUANT_STAR:
193       return GENV_TYPESYSTEM.ANY_FUNCTION_TYPE_STAR;
194     case TypeConstants::QUANT_PLUS:
195       return GENV_TYPESYSTEM.ANY_FUNCTION_TYPE_PLUS;
196     default:
197       return xqtref_t(0);
198   }
199 }
200 
201 
202 /******************************************************************************
203 
204 *******************************************************************************/
create_function_type(const std::vector<xqtref_t> & paramTypes,const xqtref_t & returnType,TypeConstants::quantifier_t quant) const205 xqtref_t TypeManagerImpl::create_function_type(
206         const std::vector<xqtref_t>& paramTypes,
207         const xqtref_t& returnType,
208         TypeConstants::quantifier_t quant) const
209 {
210   return new FunctionXQType(this, paramTypes, returnType, quant);
211 }
212 
213 
214 /***************************************************************************//**
215   Return the builtin sequence type corresponding to the given typecode and
216   quantifier. The typecode identifies a builtin atomic type.
217 ********************************************************************************/
create_builtin_atomic_type(store::SchemaTypeCode type_code,TypeConstants::quantifier_t quantifier) const218 xqtref_t TypeManagerImpl::create_builtin_atomic_type(
219     store::SchemaTypeCode type_code,
220     TypeConstants::quantifier_t quantifier) const
221 {
222   return *GENV_TYPESYSTEM.m_atomic_typecode_map[type_code][quantifier];
223 }
224 
225 
226 /***************************************************************************//**
227   Create a sequence type from the given typename and quantifier. The typename
228   is assumed to be of an atomic type. If not, or if no type with this name is
229   found in the in-scope schema, the method will return NULL or raise an error,
230   depending on the value of the "error" param.
231 ********************************************************************************/
create_named_atomic_type(store::Item * qname,TypeConstants::quantifier_t quantifier,const QueryLoc & loc,const Error & error) const232 xqtref_t TypeManagerImpl::create_named_atomic_type(
233     store::Item* qname,
234     TypeConstants::quantifier_t quantifier,
235     const QueryLoc& loc,
236     const Error& error) const
237 {
238   // Try to resolve the type name as a builtin atomic type
239   RootTypeManager::qnametype_map_t& myMap = GENV_TYPESYSTEM.m_atomic_qnametype_map;
240 
241   store::SchemaTypeCode code = store::XS_LAST;
242 
243   if (myMap.get(qname, code))
244     return create_builtin_atomic_type(code, quantifier);
245 
246   // If the type name is an XML Schema builtin type, then it cannot be an atomic
247   // type (because, otherwise it would have been found above). So we return NULL.
248   if (ZSTREQ(qname->getNamespace(), XML_SCHEMA_NS))
249   {
250     if (error != zerr::ZXQP0000_NO_ERROR)
251     {
252 			throw XQUERY_EXCEPTION_VAR(error,
253 			ERROR_PARAMS(qname->getStringValue(), ZED(NotAmongInScopeSchemaTypes)),
254 			ERROR_LOC(loc));
255     }
256     else
257     {
258       return NULL;
259     }
260   }
261 
262 #ifndef ZORBA_NO_XMLSCHEMA
263   // See if there is a type declaration for this type name in the in-scope
264   // schema, if any.
265   if (m_schema != NULL)
266   {
267     xqtref_t namedType = m_schema->createXQTypeFromTypeName(this, qname);
268 
269     if (namedType == NULL)
270     {
271       if (error != zerr::ZXQP0000_NO_ERROR)
272       {
273 				throw XQUERY_EXCEPTION_VAR(error,
274 				ERROR_PARAMS(qname->getStringValue(), ZED( NotAmongInScopeSchemaTypes)),
275 				ERROR_LOC(loc));
276       }
277       else
278       {
279         return NULL;
280       }
281     }
282 
283     ZORBA_ASSERT(namedType->type_kind() == XQType::USER_DEFINED_KIND);
284 
285     const UserDefinedXQType* udt =
286     reinterpret_cast<const UserDefinedXQType*>(namedType.getp());
287 
288     if (udt->isAtomic())
289       return create_type(*namedType, quantifier);
290   }
291 #endif
292 
293   if (error != zerr::ZXQP0000_NO_ERROR)
294   {
295 		throw XQUERY_EXCEPTION_VAR(error,
296 		ERROR_PARAMS(qname->getStringValue(), ZED( NotAmongInScopeSchemaTypes)),
297 		ERROR_LOC(loc));
298   }
299   else
300   {
301     return NULL;
302   }
303 
304   return NULL;
305 }
306 
307 
308 /***************************************************************************//**
309   Create an XMLSchema type from the given typename and quantifier.
310 ********************************************************************************/
create_named_type(store::Item * qname,TypeConstants::quantifier_t quant,const QueryLoc & loc,const Error & error) const311 xqtref_t TypeManagerImpl::create_named_type(
312     store::Item* qname,
313     TypeConstants::quantifier_t quant,
314     const QueryLoc& loc,
315     const Error& error) const
316 {
317   RootTypeManager& RTM = GENV_TYPESYSTEM;
318 
319   if (qname->equals(RTM.XS_ANY_TYPE_QNAME.getp()))
320   {
321     return create_any_type();
322   }
323   else if (qname->equals(RTM.XS_ANY_SIMPLE_TYPE_QNAME.getp()))
324   {
325     return create_any_simple_type();
326   }
327   else if (qname->equals(RTM.XS_UNTYPED_QNAME.getp()))
328   {
329     return create_untyped_type();
330   }
331   else
332   {
333     // Try to resolve the type name as a builtin atomic type
334     RootTypeManager::qnametype_map_t& myMap = RTM.m_atomic_qnametype_map;
335 
336     store::SchemaTypeCode code = store::XS_LAST;
337 
338     if (myMap.get(qname, code))
339       return create_builtin_atomic_type(code, quant);
340 
341 #ifndef ZORBA_NO_XMLSCHEMA
342     // See if there is a type declaration for this type name in the in-scope
343     // schemas, if any.
344     if (m_schema != NULL)
345     {
346       xqtref_t namedType = m_schema->createXQTypeFromTypeName(this, qname);
347 
348       if (namedType == NULL)
349       {
350         if (error != zerr::ZXQP0000_NO_ERROR)
351         {
352 					throw XQUERY_EXCEPTION_VAR(error,
353 					ERROR_PARAMS(qname->getStringValue(), ZED(NotAmongInScopeSchemaTypes)),
354 					ERROR_LOC(loc));
355         }
356         else
357         {
358           return NULL;
359         }
360       }
361 
362       ZORBA_ASSERT(namedType->type_kind() == XQType::USER_DEFINED_KIND);
363 
364       return create_type(*namedType, quant);
365     }
366 #endif
367 
368     if (error != zerr::ZXQP0000_NO_ERROR)
369     {
370 			throw XQUERY_EXCEPTION_VAR(error,
371 			ERROR_PARAMS(qname->getStringValue(), ZED(NotAmongInScopeSchemaTypes)),
372 			ERROR_LOC(loc));
373     }
374     else
375     {
376       return NULL;
377     }
378   }
379 }
380 
381 
382 /***************************************************************************//**
383 
384 ********************************************************************************/
create_structured_item_type(TypeConstants::quantifier_t q) const385 xqtref_t TypeManagerImpl::create_structured_item_type(
386     TypeConstants::quantifier_t q) const
387 {
388   switch(q)
389   {
390     case TypeConstants::QUANT_ONE:
391       return GENV_TYPESYSTEM.STRUCTURED_ITEM_TYPE_ONE;
392     case TypeConstants::QUANT_QUESTION:
393       return GENV_TYPESYSTEM.STRUCTURED_ITEM_TYPE_QUESTION;
394     case TypeConstants::QUANT_STAR:
395       return GENV_TYPESYSTEM.STRUCTURED_ITEM_TYPE_STAR;
396     case TypeConstants::QUANT_PLUS:
397       return GENV_TYPESYSTEM.STRUCTURED_ITEM_TYPE_PLUS;
398     default:
399       return xqtref_t(0);
400   }
401 }
402 
403 
404 #ifdef ZORBA_WITH_JSON
405 /***************************************************************************//**
406   Create a sequence type based on json item kind and a quantifier
407 ********************************************************************************/
create_json_type(store::StoreConsts::JSONItemKind kind,TypeConstants::quantifier_t quantifier) const408 xqtref_t TypeManagerImpl::create_json_type(
409     store::StoreConsts::JSONItemKind kind,
410     TypeConstants::quantifier_t quantifier) const
411 {
412   return GENV_TYPESYSTEM.JSON_TYPES_MAP[kind][quantifier];
413 }
414 #endif
415 
416 
417 /***************************************************************************//**
418   Create a sequence type whose ItemType is a KindTest and has a given quantifier.
419 ********************************************************************************/
create_node_type(store::StoreConsts::NodeKind nodeKind,const store::Item_t & nodeName,const xqtref_t & contType,TypeConstants::quantifier_t quant,bool nillable,bool schematest) const420 xqtref_t TypeManagerImpl::create_node_type(
421     store::StoreConsts::NodeKind nodeKind,
422     const store::Item_t& nodeName,
423     const xqtref_t& contType,
424     TypeConstants::quantifier_t quant,
425     bool nillable,
426     bool schematest) const
427 {
428   RootTypeManager& RTM = GENV_TYPESYSTEM;
429 
430   bool untyped = (contType == RTM.UNTYPED_TYPE ||
431                   (nodeKind == store::StoreConsts::attributeNode &&
432                    contType == RTM.UNTYPED_ATOMIC_TYPE_ONE));
433 
434   xqtref_t contentType = contType;
435 
436   if (contentType == NULL)
437     contentType = RTM.ANY_TYPE;
438 
439   switch (nodeKind)
440   {
441   case store::StoreConsts::anyNode:
442     return create_builtin_node_type(nodeKind, quant, untyped);
443 
444   case store::StoreConsts::documentNode:
445   {
446     if (contentType == RTM.UNTYPED_TYPE || contentType == RTM.ANY_TYPE)
447     {
448       return create_builtin_node_type(nodeKind, quant, untyped);
449     }
450     else
451     {
452       return new NodeXQType(this, nodeKind, NULL, contentType, quant, false, false);
453     }
454   }
455 
456   case store::StoreConsts::elementNode:
457   {
458     if (nodeName != NULL ||
459         nillable ||
460         (contentType != RTM.UNTYPED_TYPE && contentType != RTM.ANY_TYPE))
461     {
462       return new NodeXQType(this,
463                             nodeKind,
464                             nodeName,
465                             contentType,
466                             quant,
467                             nillable,
468                             schematest);
469     }
470     else
471     {
472       return create_builtin_node_type(nodeKind, quant, untyped);
473     }
474   }
475   case store::StoreConsts::attributeNode:
476   {
477     if (nodeName != NULL ||
478         (contentType != RTM.UNTYPED_ATOMIC_TYPE_ONE &&
479          contentType != RTM.ANY_SIMPLE_TYPE))
480     {
481       return new NodeXQType(this,
482                             nodeKind,
483                             nodeName,
484                             contentType,
485                             quant,
486                             false,
487                             schematest);
488     }
489     else
490     {
491       return create_builtin_node_type(nodeKind, quant, untyped);
492     }
493   }
494 
495   case store::StoreConsts::textNode:
496   case store::StoreConsts::commentNode:
497     return create_builtin_node_type(nodeKind, quant, true);
498 
499   case store::StoreConsts::piNode:
500   {
501     if (nodeName == NULL)
502     {
503       return create_builtin_node_type(nodeKind, quant, true);
504     }
505     else
506     {
507       return new NodeXQType(this,
508                             nodeKind,
509                             nodeName,
510                             RTM.STRING_TYPE_ONE,
511                             quant,
512                             nillable,
513                             false);
514     }
515   }
516 
517   default:
518     ZORBA_ASSERT(false);
519     return RTM.NONE_TYPE;
520   }
521 }
522 
523 
524 /***************************************************************************//**
525 
526 ********************************************************************************/
527 
528 #define createBuiltinNodeType(quantifier, untyped, kind)    \
529   if (untyped)                                              \
530   {                                                         \
531     switch(quantifier)                                      \
532     {                                                       \
533     case TypeConstants::QUANT_ONE:                          \
534       return GENV_TYPESYSTEM.kind##_UNTYPED_TYPE_ONE;       \
535     case TypeConstants::QUANT_QUESTION:                     \
536       return GENV_TYPESYSTEM.kind##_UNTYPED_TYPE_QUESTION;  \
537     case TypeConstants::QUANT_STAR:                         \
538       return GENV_TYPESYSTEM.kind##_UNTYPED_TYPE_STAR;      \
539     case TypeConstants::QUANT_PLUS:                         \
540       return GENV_TYPESYSTEM.kind##_UNTYPED_TYPE_PLUS;      \
541     default:                                                \
542       ZORBA_ASSERT(false);                                  \
543     }                                                       \
544   }                                                         \
545   else                                                      \
546   {                                                         \
547     switch(quantifier)                                      \
548     {                                                       \
549     case TypeConstants::QUANT_ONE:                          \
550       return GENV_TYPESYSTEM.kind##_TYPE_ONE;               \
551     case TypeConstants::QUANT_QUESTION:                     \
552       return GENV_TYPESYSTEM.kind##_TYPE_QUESTION;          \
553     case TypeConstants::QUANT_STAR:                         \
554       return GENV_TYPESYSTEM.kind##_TYPE_STAR;              \
555     case TypeConstants::QUANT_PLUS:                         \
556       return GENV_TYPESYSTEM.kind##_TYPE_PLUS;              \
557     default:                                                \
558       ZORBA_ASSERT(false);                                  \
559     }                                                       \
560   }
561 
562 
create_builtin_node_type(store::StoreConsts::NodeKind nodeKind,TypeConstants::quantifier_t quantifier,bool untyped) const563 xqtref_t TypeManagerImpl::create_builtin_node_type(
564     store::StoreConsts::NodeKind nodeKind,
565     TypeConstants::quantifier_t quantifier,
566     bool untyped) const
567 {
568   switch (nodeKind)
569   {
570   case store::StoreConsts::anyNode:
571     createBuiltinNodeType(quantifier, untyped, ANY_NODE);
572 
573   case store::StoreConsts::documentNode:
574     createBuiltinNodeType(quantifier, untyped, DOCUMENT);
575 
576   case store::StoreConsts::elementNode:
577     createBuiltinNodeType(quantifier, untyped, ELEMENT);
578 
579   case store::StoreConsts::attributeNode:
580     createBuiltinNodeType(quantifier, untyped, ATTRIBUTE);
581 
582   case store::StoreConsts::textNode:
583   {
584     switch(quantifier)
585     {
586     case TypeConstants::QUANT_ONE:
587       return GENV_TYPESYSTEM.TEXT_TYPE_ONE;
588     case TypeConstants::QUANT_QUESTION:
589       return GENV_TYPESYSTEM.TEXT_TYPE_QUESTION;
590     case TypeConstants::QUANT_STAR:
591       return GENV_TYPESYSTEM.TEXT_TYPE_STAR;
592     case TypeConstants::QUANT_PLUS:
593       return GENV_TYPESYSTEM.TEXT_TYPE_PLUS;
594     default:
595       ZORBA_ASSERT(false);
596     }
597   }
598 
599   case store::StoreConsts::piNode:
600   {
601     switch(quantifier)
602     {
603     case TypeConstants::QUANT_ONE:
604       return GENV_TYPESYSTEM.PI_TYPE_ONE;
605     case TypeConstants::QUANT_QUESTION:
606       return GENV_TYPESYSTEM.PI_TYPE_QUESTION;
607     case TypeConstants::QUANT_STAR:
608       return GENV_TYPESYSTEM.PI_TYPE_STAR;
609     case TypeConstants::QUANT_PLUS:
610       return GENV_TYPESYSTEM.PI_TYPE_PLUS;
611     default:
612       ZORBA_ASSERT(false);
613     }
614   }
615 
616   case store::StoreConsts::commentNode:
617   {
618     switch(quantifier)
619     {
620     case TypeConstants::QUANT_ONE:
621     return GENV_TYPESYSTEM.COMMENT_TYPE_ONE;
622     case TypeConstants::QUANT_QUESTION:
623       return GENV_TYPESYSTEM.COMMENT_TYPE_QUESTION;
624     case TypeConstants::QUANT_STAR:
625       return GENV_TYPESYSTEM.COMMENT_TYPE_STAR;
626     case TypeConstants::QUANT_PLUS:
627       return GENV_TYPESYSTEM.COMMENT_TYPE_PLUS;
628     default:
629       ZORBA_ASSERT(false);
630     }
631   }
632 
633   default:
634     ZORBA_ASSERT(false);
635     return GENV_TYPESYSTEM.NONE_TYPE;
636   }
637 }
638 
639 
640 /***************************************************************************//**
641   Create a sequence type based on the kind and content of an item.
642 ********************************************************************************/
create_value_type(const store::Item * item,const QueryLoc & loc) const643 xqtref_t TypeManagerImpl::create_value_type(
644     const store::Item* item,
645     const QueryLoc& loc) const
646 {
647   TypeConstants::quantifier_t quant = TypeConstants::QUANT_ONE;
648 
649   if (item->isAtomic())
650   {
651     return create_named_atomic_type(item->getType(),
652                                     quant,
653                                     loc,
654                                     err::XPTY0004);
655   }
656   else if (item->isNode())
657   {
658     store::NodeKind nodeKind = item->getNodeKind();
659 
660     switch (nodeKind)
661     {
662     case store::StoreConsts::elementNode:
663     case store::StoreConsts::attributeNode:
664     {
665       xqtref_t contentType = create_named_type(item->getType(),
666                                                quant,
667                                                loc,
668                                                err::XPTY0004);
669 
670       return create_node_type(nodeKind,
671                               item->getNodeName(),
672                               contentType,
673                               quant,
674                               false,
675                               false);
676     }
677     case store::StoreConsts::documentNode:
678     {
679       store::Iterator_t childrenIte = item->getChildren();
680       store::Item_t child;
681       store::Item_t elemChild;
682       bool foundElemChild = false;
683       childrenIte->open();
684       while (childrenIte->next(child))
685       {
686         if (child->getNodeKind() == store::StoreConsts::elementNode)
687         {
688           if (!foundElemChild)
689             elemChild.transfer(child);
690           else
691             elemChild = NULL;
692         }
693       }
694       childrenIte->close();
695 
696       if (elemChild == NULL)
697         return GENV_TYPESYSTEM.DOCUMENT_TYPE_ONE;
698 
699       xqtref_t elemType = create_value_type(elemChild);
700 
701       return create_node_type(store::StoreConsts::documentNode,
702                               NULL,
703                               elemType,
704                               quant,
705                               false,
706                               false);
707     }
708     case store::StoreConsts::textNode:
709     {
710       return GENV_TYPESYSTEM.TEXT_TYPE_ONE;
711     }
712     case store::StoreConsts::piNode:
713     {
714       return create_node_type(store::StoreConsts::piNode,
715                               item->getNodeName(),
716                               GENV_TYPESYSTEM.STRING_TYPE_ONE,
717                               quant,
718                               false,
719                               false);
720     }
721     case store::StoreConsts::commentNode:
722     {
723       return GENV_TYPESYSTEM.COMMENT_TYPE_ONE;
724     }
725     default:
726     {
727       ZORBA_ASSERT(false);
728     }
729     }
730   }
731 
732 #ifdef ZORBA_WITH_JSON
733   else if (item->isJSONItem())
734   {
735     return create_json_type(item->getJSONItemKind(), quant);
736   }
737 #endif
738 
739   else if (item->isFunction())
740   {
741     const FunctionItem* lFItem = static_cast<const FunctionItem*>(item);
742     const signature& lSig = lFItem->getSignature();
743     const xqtref_t& lRetType = lSig.returnType();
744     std::vector<xqtref_t> lParamTypes;
745     for (uint32_t i = 0; i < lSig.paramCount(); ++i)
746     {
747       lParamTypes.push_back(lSig[i]);
748     }
749 
750     return new FunctionXQType(this, lParamTypes, lRetType, quant);
751   }
752 
753   else
754   {
755     ZORBA_ASSERT(false);
756     return NULL;
757   }
758 }
759 
760 
761 #ifndef ZORBA_NO_XMLSCHEMA
762 
763 /***************************************************************************//**
764   Create a sequence type of the form "element(elemName, tname) quant", where
765   quant is a given quantifier, elemName is a given element qname, elemName is a
766   globaly declared element name, and tname is the name of the type associated
767   with elemName in the in-scope schema declarations.
768 ********************************************************************************/
create_schema_element_type(const store::Item_t & elemName,TypeConstants::quantifier_t quant,const QueryLoc & loc) const769 xqtref_t TypeManagerImpl::create_schema_element_type(
770     const store::Item_t& elemName,
771     TypeConstants::quantifier_t quant,
772     const QueryLoc& loc) const
773 {
774   if (m_schema == NULL)
775   {
776     throw XQUERY_EXCEPTION(
777       err::XPST0008,
778       ERROR_PARAMS( elemName->getStringValue(), ZED( SchemaElementName ) ),
779       ERROR_LOC( loc )
780     );
781   }
782 
783   xqtref_t contentType =
784   m_schema->createXQTypeFromElementName(this, elemName, true, loc);
785 
786   return create_node_type(store::StoreConsts::elementNode,
787                           elemName,
788                           contentType,
789                           quant,
790                           false, // nillable
791                           true); // schematest
792 }
793 
794 
795 /***************************************************************************//**
796   Get the name of the type associated with a given globally declared element name.
797 ********************************************************************************/
get_schema_element_typename(const store::Item * elemName,store::Item_t & typeName,const QueryLoc & loc)798 void TypeManagerImpl::get_schema_element_typename(
799     const store::Item* elemName,
800     store::Item_t& typeName,
801     const QueryLoc& loc)
802 {
803   if (m_schema == NULL)
804   {
805     RAISE_ERROR(err::XPST0008, loc,
806     ERROR_PARAMS(elemName->getStringValue(), ZED(SchemaElementName)));
807   }
808 
809   m_schema->getTypeNameFromElementName(elemName, typeName, loc);
810 }
811 
812 
813 /***************************************************************************//**
814   Create a sequence type of the form "attribute(attrName, tname) quant", where
815   quant is a given quantifier, attrName is a given attribute qname, attrName is
816   a globaly declared attribute name, and tname is the name of the type associated
817   with attrName in the in-scope schema declarations.
818 ********************************************************************************/
create_schema_attribute_type(const store::Item_t & attrName,TypeConstants::quantifier_t quant,const QueryLoc & loc) const819 xqtref_t TypeManagerImpl::create_schema_attribute_type(
820     const store::Item_t& attrName,
821     TypeConstants::quantifier_t quant,
822     const QueryLoc& loc) const
823 {
824   if (m_schema == NULL)
825   {
826     RAISE_ERROR(err::XPST0008, loc,
827     ERROR_PARAMS(attrName->getStringValue(), ZED(SchemaAttributeName)));
828   }
829 
830   xqtref_t contentType =
831   m_schema->createXQTypeFromAttributeName(this, attrName, true, loc);
832 
833   return create_node_type(store::StoreConsts::attributeNode,
834                           attrName,
835                           contentType,
836                           quant,
837                           false,
838                           true);
839 }
840 
841 
842 /***************************************************************************//**
843   Get the name of the type associated with a given globally declared attribute
844   name.
845 ********************************************************************************/
get_schema_attribute_typename(const store::Item * attrName,store::Item_t & typeName,const QueryLoc & loc)846 void TypeManagerImpl::get_schema_attribute_typename(
847     const store::Item* attrName,
848     store::Item_t& typeName,
849     const QueryLoc& loc)
850 {
851   if (m_schema == NULL)
852   {
853     RAISE_ERROR(err::XPST0008, loc,
854     ERROR_PARAMS(attrName->getStringValue(), ZED(SchemaAttributeName)));
855   }
856 
857   m_schema->getTypeNameFromAttributeName(attrName, typeName, loc);
858 }
859 
860 
861 #endif // ZORBA_NO_XMLSCHEMA
862 
863 
864 /***************************************************************************//**
865   Create a sequence type with a given quantifier and the same ItemType as the
866   one of a given type.
867 ********************************************************************************/
create_type(const XQType & type,TypeConstants::quantifier_t quantifier) const868 xqtref_t TypeManagerImpl::create_type(
869     const XQType& type,
870     TypeConstants::quantifier_t quantifier) const
871 {
872   if (type.get_quantifier() == quantifier)
873     return &type;
874 
875   switch(type.type_kind())
876   {
877   case XQType::ATOMIC_TYPE_KIND:
878   {
879     return create_builtin_atomic_type(
880                   static_cast<const AtomicXQType*>(&type)->get_type_code(),
881                   quantifier);
882   }
883 
884   case XQType::NODE_TYPE_KIND:
885   {
886     const NodeXQType& nt = static_cast<const NodeXQType&>(type);
887     if (type.is_builtin())
888     {
889       return create_builtin_node_type(nt.get_node_kind(),
890                                       quantifier,
891                                       nt.is_untyped());
892     }
893     else
894     {
895       return new NodeXQType(nt, quantifier);
896     }
897   }
898 
899   case XQType::STRUCTURED_ITEM_KIND:
900   {
901     return create_structured_item_type(quantifier);
902   }
903 
904 #ifdef ZORBA_WITH_JSON
905   case XQType::JSON_TYPE_KIND:
906   {
907     const JSONXQType& jt = static_cast<const JSONXQType&>(type);
908     return create_json_type(jt.get_json_kind(), quantifier);
909   }
910 #endif
911 
912   case XQType::FUNCTION_TYPE_KIND:
913   {
914     const FunctionXQType& ft = static_cast<const FunctionXQType&>(type);
915     return new FunctionXQType(this,
916                               ft.get_param_types(),
917                               ft.get_return_type(),
918                               quantifier);
919   }
920 
921   case XQType::ITEM_KIND:
922     return create_any_item_type(quantifier);
923 
924   case XQType::ANY_TYPE_KIND:
925     return GENV_TYPESYSTEM.ANY_TYPE;
926 
927   case XQType::ANY_SIMPLE_TYPE_KIND:
928     return GENV_TYPESYSTEM.ANY_SIMPLE_TYPE;
929 
930   case XQType::ANY_FUNCTION_TYPE_KIND:
931     return create_any_function_type(quantifier);
932 
933   case XQType::UNTYPED_KIND:
934     return GENV_TYPESYSTEM.UNTYPED_TYPE;
935 
936   case XQType::EMPTY_KIND:
937     return GENV_TYPESYSTEM.EMPTY_TYPE;
938 
939   case XQType::NONE_KIND:
940     return (quantifier == TypeConstants::QUANT_ONE ||
941             quantifier == TypeConstants::QUANT_PLUS ?
942             GENV_TYPESYSTEM.NONE_TYPE :
943             GENV_TYPESYSTEM.EMPTY_TYPE);
944 
945   case XQType::USER_DEFINED_KIND:
946   {
947     const UserDefinedXQType& udt = static_cast<const UserDefinedXQType&>(type);
948     return xqtref_t(new UserDefinedXQType(this,
949                                           udt.get_qname(),
950                                           udt.getBaseType(),
951                                           quantifier,
952                                           udt.getTypeCategory(),
953                                           udt.content_kind()));
954   }
955   default:
956     ZORBA_ASSERT(false);
957   }
958 
959   ZORBA_ASSERT (false);
960   return 0;
961 }
962 
963 
964 /***************************************************************************//**
965   Create a sequence type whose ItemType is the same as the one of a given type
966   T, and whose quantifier is the union of T's quantifier and another given
967   quantifier.
968 ********************************************************************************/
create_type_x_quant(const XQType & type,TypeConstants::quantifier_t quantifier) const969 xqtref_t TypeManagerImpl::create_type_x_quant(
970     const XQType& type,
971     TypeConstants::quantifier_t quantifier) const
972 {
973   return create_type(type,
974                      RootTypeManager::QUANT_UNION_MATRIX[type.get_quantifier()]
975                                                         [quantifier]);
976 }
977 
978 
979 /***************************************************************************//**
980 
981 ********************************************************************************/
create_type(const TypeIdentifier & ident) const982 xqtref_t TypeManagerImpl::create_type(const TypeIdentifier& ident) const
983 {
984   TypeConstants::quantifier_t q = TypeConstants::QUANT_ONE;
985 
986   switch (ident.getQuantifier())
987   {
988     case IdentTypes::QUANT_ONE:
989       q = TypeConstants::QUANT_ONE;
990       break;
991 
992     case IdentTypes::QUANT_QUESTION:
993       q = TypeConstants::QUANT_QUESTION;
994       break;
995 
996     case IdentTypes::QUANT_PLUS:
997       q = TypeConstants::QUANT_PLUS;
998       break;
999 
1000     case IdentTypes::QUANT_STAR:
1001       q = TypeConstants::QUANT_STAR;
1002       break;
1003   }
1004 
1005   switch (ident.getKind())
1006   {
1007   case IdentTypes::NAMED_TYPE:
1008   {
1009     store::Item_t i;
1010     GENV_ITEMFACTORY->createQName(i,
1011                                   ident.getUri().c_str(),
1012                                   NULL,
1013                                   ident.getLocalName().c_str());
1014     return create_named_type(i, q, QueryLoc::null, err::XPTY0004);
1015   }
1016 
1017   case IdentTypes::ELEMENT_TYPE:
1018   {
1019     store::Item_t ename;
1020     GENV_ITEMFACTORY->createQName(ename,
1021                                   ident.getUri().c_str(),
1022                                   NULL,
1023                                   ident.getLocalName().c_str());
1024 
1025     TypeIdentifier_t ci = ident.getContentType();
1026     xqtref_t content_type = (ci != NULL ? create_type(*ci) : xqtref_t(0));
1027 
1028     return create_node_type(store::StoreConsts::elementNode,
1029                             ename.getp(),
1030                             content_type,
1031                             q,
1032                             false,
1033                             false);
1034   }
1035 
1036   case IdentTypes::ATTRIBUTE_TYPE:
1037   {
1038     store::Item_t aname;
1039     GENV_ITEMFACTORY->createQName(aname,
1040                                   ident.getUri().c_str(),
1041                                   NULL,
1042                                   ident.getLocalName().c_str());
1043 
1044     TypeIdentifier_t ci = ident.getContentType();
1045     xqtref_t content_type = (ci != NULL ? create_type(*ci) : xqtref_t(0));
1046 
1047     return create_node_type(store::StoreConsts::attributeNode,
1048                             aname.getp(),
1049                             content_type,
1050                             q,
1051                             false,
1052                             false);
1053   }
1054 
1055   case IdentTypes::DOCUMENT_TYPE:
1056   {
1057     TypeIdentifier_t ci = ident.getContentType();
1058     xqtref_t content_type = (ci != NULL ? create_type(*ci) : xqtref_t(0));
1059 
1060     return create_node_type(store::StoreConsts::documentNode,
1061                             NULL,
1062                             content_type,
1063                             q,
1064                             false,
1065                             false);
1066   }
1067 
1068   case IdentTypes::PI_TYPE:
1069     return create_builtin_node_type(store::StoreConsts::piNode, q, false);
1070 
1071   case IdentTypes::TEXT_TYPE:
1072     return create_builtin_node_type(store::StoreConsts::textNode, q, false);
1073 
1074   case IdentTypes::COMMENT_TYPE:
1075     return create_builtin_node_type(store::StoreConsts::commentNode, q, false);
1076 
1077   case IdentTypes::ANY_NODE_TYPE:
1078     return create_builtin_node_type(store::StoreConsts::anyNode, q, false);
1079 
1080 #ifdef ZORBA_WITH_JSON
1081   case IdentTypes::JSON_ITEM_TYPE:
1082     return create_json_type(store::StoreConsts::jsonItem, q);
1083 
1084   case IdentTypes::JSON_OBJECT_TYPE:
1085     return create_json_type(store::StoreConsts::jsonObject, q);
1086 
1087   case IdentTypes::JSON_ARRAY_TYPE:
1088     return create_json_type(store::StoreConsts::jsonArray, q);
1089 #endif // #ifdef ZORBA_WITH_JSON
1090 
1091   case IdentTypes::ITEM_TYPE:
1092     return create_any_item_type(q);
1093 
1094   case IdentTypes::EMPTY_TYPE:
1095     return create_empty_type();
1096 
1097   case IdentTypes::SCHEMA_ELEMENT_TYPE:
1098   {
1099     store::Item_t ename;
1100     GENV_ITEMFACTORY->createQName(ename,
1101                                   ident.getUri().c_str(),
1102                                   NULL,
1103                                   ident.getLocalName().c_str());
1104 
1105     return create_schema_element_type(ename.getp(),
1106                                       q,
1107                                       QueryLoc::null);
1108   }
1109 
1110   case IdentTypes::SCHEMA_ATTRIBUTE_TYPE:
1111   {
1112     store::Item_t aname;
1113     GENV_ITEMFACTORY->createQName(aname,
1114                                   ident.getUri().c_str(),
1115                                   NULL,
1116                                   ident.getLocalName().c_str());
1117 
1118     return create_schema_attribute_type(aname,
1119                                         q,
1120                                         QueryLoc::null);
1121   }
1122 
1123   default:
1124     break;
1125   }
1126 
1127   return xqtref_t(0);
1128 }
1129 /* vim:set et sw=2 ts=2: */
1130