← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~zorba-coders/zorba/markos-scratch2 into lp:zorba

 

Markos Zaharioudakis has proposed merging lp:~zorba-coders/zorba/markos-scratch2 into lp:zorba.

Requested reviews:
  Markos Zaharioudakis (markos-za)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/markos-scratch2/+merge/107797

fixed bug #854506 and partial fix for bug #867008
-- 
https://code.launchpad.net/~zorba-coders/zorba/markos-scratch2/+merge/107797
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'include/zorba/store_consts.h'
--- include/zorba/store_consts.h	2012-05-03 12:31:51 +0000
+++ include/zorba/store_consts.h	2012-05-29 14:32:32 +0000
@@ -110,7 +110,8 @@
 
   static std::string toString(NodeKind k)
   {
-    switch(k) {
+    switch(k) 
+    {
       case anyNode:
         return "anyNode";
 
@@ -136,6 +137,36 @@
         return "<unknown NodeKind>";
     }
   }
+
+  static std::string toSchemaString(NodeKind k)
+  {
+    switch(k) 
+    {
+      case anyNode:
+        return "node";
+
+      case documentNode:
+        return "document-node";
+
+      case elementNode:
+        return "element";
+
+      case attributeNode:
+        return "attribute";
+
+      case textNode:
+        return "text";
+
+      case piNode:
+        return "processing-instruction";
+
+      case commentNode:
+        return "comment";
+
+      default:
+        return "<unknown NodeKind>";
+    }
+  }
 };
 
 } // namespace store

=== modified file 'src/compiler/codegen/plan_visitor.cpp'
--- src/compiler/codegen/plan_visitor.cpp	2012-05-03 12:31:51 +0000
+++ src/compiler/codegen/plan_visitor.cpp	2012-05-29 14:32:32 +0000
@@ -1963,9 +1963,14 @@
 void end_visit(promote_expr& v)
 {
   CODEGEN_TRACE_OUT("");
-  PlanIter_t lChild = pop_itstack();
+  PlanIter_t child = pop_itstack();
   // TODO: Currently we use cast. Promotion may be more efficient.
-  push_itstack(new PromoteIterator(sctx, qloc, lChild, v.get_target_type(), v.get_fn_qname()));
+  push_itstack(new PromoteIterator(sctx,
+                                   qloc,
+                                   child,
+                                   v.get_target_type(),
+                                   v.get_err(),
+                                   v.get_qname()));
 }
 
 
@@ -2355,6 +2360,7 @@
   return true;
 }
 
+
 void end_visit(instanceof_expr& v)
 {
   CODEGEN_TRACE_OUT("");
@@ -2369,20 +2375,27 @@
   return true;
 }
 
+
 void end_visit(treat_expr& v)
 {
   CODEGEN_TRACE_OUT("");
   PlanIter_t arg;
   arg = pop_itstack();
-  push_itstack(new TreatIterator(sctx, qloc, arg, v.get_target_type(), v.get_check_prime(), v.get_err(), v.get_fn_qname()));
+  push_itstack(new TreatIterator(sctx, qloc, arg, 
+                                 v.get_target_type(),
+                                 v.get_check_prime(),
+                                 v.get_err(),
+                                 v.get_qname()));
 }
 
+
 bool begin_visit(castable_expr& v)
 {
   CODEGEN_TRACE_IN("");
   return true;
 }
 
+
 void end_visit(castable_expr& v)
 {
   CODEGEN_TRACE_OUT("");
@@ -2390,12 +2403,14 @@
   push_itstack(new CastableIterator(sctx, qloc, lChild, v.get_target_type()));
 }
 
+
 bool begin_visit(cast_expr& v)
 {
   CODEGEN_TRACE_IN("");
   return true;
 }
 
+
 void end_visit(cast_expr& v)
 {
   CODEGEN_TRACE_OUT("");

=== modified file 'src/compiler/expression/expr.cpp'
--- src/compiler/expression/expr.cpp	2012-05-16 17:25:48 +0000
+++ src/compiler/expression/expr.cpp	2012-05-29 14:32:32 +0000
@@ -364,8 +364,8 @@
     static_context* sctx,
     const QueryLoc& loc,
     expr_kind_t kind,
-    expr_t input,
-    xqtref_t type)
+    const expr_t& input,
+    const xqtref_t& type)
   :
   expr(sctx, loc, kind),
   theInputExpr(input),
@@ -416,8 +416,8 @@
     static_context* sctx,
     const QueryLoc& loc,
     expr_kind_t kind,
-    expr_t input,
-    xqtref_t type)
+    const expr_t& input,
+    const xqtref_t& type)
   : 
   cast_or_castable_base_expr(sctx, loc, kind, input, type)
 {
@@ -439,8 +439,8 @@
 cast_expr::cast_expr(
     static_context* sctx,
     const QueryLoc& loc,
-    expr_t inputExpr,
-    xqtref_t type)
+    const expr_t& inputExpr,
+    const xqtref_t& type)
   :
   cast_base_expr(sctx, loc, cast_expr_kind, inputExpr, type)
 {
@@ -476,16 +476,16 @@
 treat_expr::treat_expr(
     static_context* sctx,
     const QueryLoc& loc,
-    expr_t inputExpr,
-    xqtref_t type,
-    Error const &err,
+    const expr_t& inputExpr,
+    const xqtref_t& type,
+    TreatIterator::ErrorKind err,
     bool check_prime,
-    store::Item_t fnQname)
+    store::Item* qname)
   :
   cast_base_expr(sctx, loc, treat_expr_kind, inputExpr, type),
-  theError(&err),
+  theErrorKind(err),
   theCheckPrime(check_prime),
-  theFnQName(fnQname)
+  theQName(qname)
 {
 }
 
@@ -494,8 +494,8 @@
 {
   serialize_baseclass(ar, (cast_base_expr*)this);
   ar & theCheckPrime;
-  ar & theError;
-  ar & theFnQName;
+  SERIALIZE_ENUM(TreatIterator::ErrorKind, theErrorKind);
+  ar & theQName;
 }
 
 
@@ -507,7 +507,7 @@
                         get_target_type(),
                         get_err(),
                         get_check_prime(),
-                        get_fn_qname());
+                        get_qname());
 }
 
 
@@ -517,12 +517,14 @@
 promote_expr::promote_expr(
     static_context* sctx,
     const QueryLoc& loc,
-    expr_t input,
-    xqtref_t type,
-    store::Item_t fnQName)
+    const expr_t& input,
+    const xqtref_t& type,
+    PromoteIterator::ErrorKind err,
+    store::Item* qname)
   :
   cast_base_expr(sctx, loc, promote_expr_kind, input, type),
-  theFnQName(fnQName)
+  theErrorKind(err),
+  theQName(qname)
 {
 }
 
@@ -530,7 +532,8 @@
 void promote_expr::serialize(::zorba::serialization::Archiver& ar)
 {
   serialize_baseclass(ar, (cast_base_expr*)this);
-  ar & theFnQName;
+  SERIALIZE_ENUM(PromoteIterator::ErrorKind, theErrorKind);
+  ar & theQName;
 }
 
 
@@ -540,7 +543,8 @@
                           get_loc(),
                           get_input()->clone(subst),
                           get_target_type(),
-                          theFnQName);
+                          theErrorKind,
+                          theQName.getp());
 }
 
 
@@ -551,8 +555,8 @@
     static_context* sctx,
     const QueryLoc& loc,
     expr_kind_t kind,
-    expr_t input,
-    xqtref_t type)
+    const expr_t& input,
+    const xqtref_t& type)
   :
   cast_or_castable_base_expr(sctx, loc, kind, input, type)
 {
@@ -573,8 +577,8 @@
 castable_expr::castable_expr(
     static_context* sctx,
     const QueryLoc& loc,
-    expr_t inputExpr,
-    xqtref_t type)
+    const expr_t& inputExpr,
+    const xqtref_t& type)
   :
   castable_base_expr (sctx, loc, castable_expr_kind, inputExpr, type)
 {
@@ -608,8 +612,8 @@
 instanceof_expr::instanceof_expr(
     static_context* sctx,
     const QueryLoc& loc,
-    expr_t inputExpr,
-    xqtref_t type,
+    const expr_t& inputExpr,
+    const xqtref_t& type,
     bool checkPrimeOnly)
   :
   castable_base_expr(sctx, loc, instanceof_expr_kind, inputExpr, type),

=== modified file 'src/compiler/expression/expr.h'
--- src/compiler/expression/expr.h	2012-05-16 17:25:48 +0000
+++ src/compiler/expression/expr.h	2012-05-29 14:32:32 +0000
@@ -39,6 +39,8 @@
 
 #include "store/api/item.h"
 
+#include "runtime/core/sequencetypes.h"
+
 namespace zorba 
 {
 
@@ -221,8 +223,8 @@
         static_context* sctx,
         const QueryLoc& loc,
         expr_kind_t kind,
-        expr_t input,
-        xqtref_t type);
+        const expr_t& input,
+        const xqtref_t& type);
   
 public:
   SERIALIZABLE_ABSTRACT_CLASS(cast_or_castable_base_expr)
@@ -257,8 +259,8 @@
         static_context* sctx, 
         const QueryLoc& loc,
         expr_kind_t kind,
-        expr_t input,
-        xqtref_t type);
+        const expr_t& input,
+        const xqtref_t& type);
 };
 
 
@@ -278,7 +280,7 @@
   void serialize(::zorba::serialization::Archiver& ar);
 
 public:
-  cast_expr(static_context* sctx, const QueryLoc&, expr_t, xqtref_t);
+  cast_expr(static_context* sctx, const QueryLoc&, const expr_t&, const xqtref_t&);
 
   bool is_optional() const;
 
@@ -309,9 +311,9 @@
   friend class expr;
 
 protected:
-  const Error  * theError;
-  bool           theCheckPrime;
-  store::Item_t  theFnQName;
+  TreatIterator::ErrorKind theErrorKind;
+  bool                     theCheckPrime;
+  store::Item_t            theQName;
 
 public:
   SERIALIZABLE_CLASS(treat_expr)
@@ -321,22 +323,22 @@
 public:
   treat_expr(
         static_context* sctx, 
-        const QueryLoc&,
-        expr_t,
-        xqtref_t,
-        Error const&,
+        const QueryLoc& loc,
+        const expr_t& input,
+        const xqtref_t& type,
+        TreatIterator::ErrorKind err,
         bool check_prime = true,
-        store::Item_t fnQname = NULL);
+        store::Item* qname = NULL);
 
-  Error const& get_err() const { return *theError; }
+  TreatIterator::ErrorKind get_err() const { return theErrorKind; }
 
   bool get_check_prime() const { return theCheckPrime; }
 
   void set_check_prime(bool check_prime) { theCheckPrime = check_prime; }
 
-  void set_fn_qname(store::Item_t fnQName) { theFnQName = fnQName; }
+  void set_qname(const store::Item_t& qname) { theQName = qname; }
 
-  store::Item_t get_fn_qname() const { return theFnQName; }
+  store::Item_t get_qname() const { return theQName; }
 
   expr_t clone(substitution_t& s) const;
 
@@ -381,8 +383,8 @@
   friend class expr;
 
 protected:
-  store::Item_t theFnQName;    // Stores the QName of the function, if the promote expr
-                               // is used to cast the function's body to its result type
+  PromoteIterator::ErrorKind theErrorKind;
+  store::Item_t              theQName; 
   
 public:
   SERIALIZABLE_CLASS(promote_expr)
@@ -393,15 +395,18 @@
   promote_expr(
       static_context* sctx, 
       const QueryLoc& loc, 
-      expr_t input, 
-      xqtref_t type, 
-      store::Item_t fnQname = NULL);
+      const expr_t& input, 
+      const xqtref_t& type, 
+      PromoteIterator::ErrorKind err,
+      store::Item* qname);
 
   expr_t clone(substitution_t& s) const;
 
-  void set_fn_qname(store::Item_t fnQName) { theFnQName = fnQName; }
-
-  store::Item_t get_fn_qname() const { return theFnQName; }
+  PromoteIterator::ErrorKind get_err() const { return theErrorKind; }
+
+  void set_qname(const store::Item_t& qname) { theQName = qname; }
+
+  store::Item_t get_qname() const { return theQName; }
 
   void accept(expr_visitor&);
 
@@ -426,8 +431,8 @@
         static_context* sctx,
         const QueryLoc&,
         expr_kind_t kind,
-        expr_t,
-        xqtref_t);
+        const expr_t&,
+        const xqtref_t&);
 };
 
 
@@ -447,7 +452,7 @@
   void serialize(::zorba::serialization::Archiver& ar);
 
 public:
-  castable_expr(static_context* sctx, const QueryLoc&, expr_t, xqtref_t);
+  castable_expr(static_context* sctx, const QueryLoc&, const expr_t&, const xqtref_t&);
   
   bool is_optional() const;
 
@@ -485,8 +490,8 @@
   instanceof_expr(
       static_context* sctx,
       const QueryLoc&, 
-      expr_t, 
-      xqtref_t,
+      const expr_t&, 
+      const xqtref_t&,
       bool checkPrimeOnly = false);
 
   bool getCheckPrimeOnly() const { return theCheckPrimeOnly; }

=== modified file 'src/compiler/expression/flwor_expr.cpp'
--- src/compiler/expression/flwor_expr.cpp	2012-05-03 12:31:51 +0000
+++ src/compiler/expression/flwor_expr.cpp	2012-05-29 14:32:32 +0000
@@ -161,8 +161,10 @@
     if (declaredType != NULL)
     {
       if (TypeOps::is_empty(tm, *declaredType))
+      {
         RAISE_ERROR(err::XPTY0004, loc,
         ERROR_PARAMS(ZED(BadType_23o), "empty-sequence"));
+      }
 
       xqtref_t domainType = domainExpr->get_return_type();
 
@@ -182,7 +184,11 @@
                          *declaredType));
           }
 
-          domainExpr = new treat_expr(sctx, loc, domainExpr, declaredType, err::XPTY0004);
+          domainExpr = new treat_expr(sctx,
+                                      loc,
+                                      domainExpr,
+                                      declaredType,
+                                      TreatIterator::TYPE_MATCH);
 
           set_expr(domainExpr);
         }
@@ -309,7 +315,11 @@
           ERROR_PARAMS(ZED(BadType_23o), *domainType, ZED(NoTreatAs_4), *declaredType));
         }
 
-        domainExpr = new treat_expr(sctx, loc, domainExpr, declaredType, err::XPTY0004);
+        domainExpr = new treat_expr(sctx,
+                                    loc,
+                                    domainExpr,
+                                    declaredType,
+                                    TreatIterator::TYPE_MATCH);
 
         set_expr(domainExpr);
       }
@@ -407,7 +417,11 @@
       if (!TypeOps::is_subtype(tm, *rtm.ITEM_TYPE_STAR, *varType, loc) &&
           !TypeOps::is_subtype(tm, *domainType, *varType, loc))
       {
-        domainExpr = new treat_expr(sctx, loc, domainExpr, varType, err::XPTY0004);
+        domainExpr = new treat_expr(sctx,
+                                    loc,
+                                    domainExpr,
+                                    varType,
+                                    TreatIterator::TYPE_MATCH);
 
         set_expr(domainExpr);
       }

=== modified file 'src/compiler/rewriter/rules/type_rules.cpp'
--- src/compiler/rewriter/rules/type_rules.cpp	2012-05-03 12:31:51 +0000
+++ src/compiler/rewriter/rules/type_rules.cpp	2012-05-29 14:32:32 +0000
@@ -43,7 +43,11 @@
 namespace zorba
 {
 
-static expr_t wrap_in_num_promotion(expr* arg, xqtref_t oldt, xqtref_t t);
+static expr_t wrap_in_num_promotion(
+    function* fn,
+    expr* arg,
+    const xqtref_t& oldt,
+    const xqtref_t& t);
 
 static xqtref_t specialize_numeric(fo_expr* fo, static_context* sctx);
 
@@ -158,7 +162,7 @@
                             node->get_loc(),
                             arg,
                             target_type,
-                            err::XPTY0004,
+                            TreatIterator::TYPE_MATCH,
                             false); // do not check the prime types
     }
 
@@ -235,7 +239,9 @@
           expr_t promoteExpr = new promote_expr(argExpr->get_sctx(),
                                                 argExpr->get_loc(),
                                                 argExpr,
-                                                rtm.DOUBLE_TYPE_STAR);
+                                                rtm.DOUBLE_TYPE_STAR,
+                                                PromoteIterator::FUNC_PARAM,
+                                                replacement->getName());
 
           fo->set_arg(0, promoteExpr);
         }
@@ -484,8 +490,8 @@
   {
     fo->set_func(replacement);
 
-    expr_t newArg0 = wrap_in_num_promotion(arg0, t0, aType);
-    expr_t newArg1 = wrap_in_num_promotion(arg1, t1, aType);
+    expr_t newArg0 = wrap_in_num_promotion(replacement, arg0, t0, aType);
+    expr_t newArg1 = wrap_in_num_promotion(replacement, arg1, t1, aType);
 
     if (newArg0 != NULL)
       fo->set_arg(0, newArg0);
@@ -500,7 +506,11 @@
 }
 
 
-static expr_t wrap_in_num_promotion(expr* arg, xqtref_t oldt, xqtref_t t)
+static expr_t wrap_in_num_promotion(
+    function* fn,
+    expr* arg,
+    const xqtref_t& oldt,
+    const xqtref_t& t)
 {
   TypeManager* tm = arg->get_type_manager();
 
@@ -518,7 +528,12 @@
       arg = pe->get_input();
   }
 
-  return new promote_expr(arg->get_sctx(), arg->get_loc(), arg, t);
+  return new promote_expr(arg->get_sctx(),
+                          arg->get_loc(),
+                          arg,
+                          t,
+                          PromoteIterator::FUNC_PARAM,
+                          fn->getName());
 }
 
 

=== modified file 'src/compiler/translator/translator.cpp'
--- src/compiler/translator/translator.cpp	2012-05-09 23:06:41 +0000
+++ src/compiler/translator/translator.cpp	2012-05-29 14:32:32 +0000
@@ -1311,7 +1311,7 @@
 
   const signature& sign = foExpr->get_signature();
 
-  ulong n = foExpr->num_args();
+  csize n = foExpr->num_args();
 
   const function* func = foExpr->get_func();
 
@@ -1326,29 +1326,19 @@
     if (qname != NULL)
     {
       RAISE_ERROR(zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS, loc,
-      ERROR_PARAMS(
-        qname->getStringValue(),
-        "index",
-        n-1,
-        "multiple of 6"
-      )
-      );
+      ERROR_PARAMS(qname->getStringValue(),
+                   "index",
+                   n-1,
+                   "multiple of 6"));
     }
     else
     {
-      RAISE_ERROR(
-          zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS, loc,
-          ERROR_PARAMS(
-            "anonymous",
-            "index",
-            n-1,
-            "multiple of 6"
-          )
-      );
+      RAISE_ERROR(zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS, loc,
+      ERROR_PARAMS("anonymous", "index", n-1, "multiple of 6"));
     }
   }
 
-  for (ulong i = 0; i < n; ++i)
+  for (csize i = 0; i < n; ++i)
   {
     expr::expr_t argExpr = foExpr->get_arg(i);
 
@@ -1395,11 +1385,18 @@
                               loc))
       {
         argExpr = wrap_in_atomization(argExpr);
-        argExpr = wrap_in_type_promotion(argExpr, paramType);
+        argExpr = wrap_in_type_promotion(argExpr,
+                                         paramType, 
+                                         PromoteIterator::FUNC_PARAM,
+                                         func->getName());
       }
       else
       {
-        argExpr = wrap_in_type_match(argExpr, paramType, loc);
+        argExpr = wrap_in_type_match(argExpr,
+                                     paramType,
+                                     loc,
+                                     TreatIterator::FUNC_PARAM,
+                                     func->getName());
       }
     }
 
@@ -1423,9 +1420,13 @@
 /*******************************************************************************
 
 ********************************************************************************/
-expr_t wrap_in_type_promotion(expr_t e, xqtref_t type, store::Item_t fnQName = NULL)
+expr_t wrap_in_type_promotion(
+    const expr_t& e,
+    const xqtref_t& type,
+    PromoteIterator::ErrorKind errorKind,
+    store::Item* qname = NULL)
 {
-  return new promote_expr(theRootSctx, e->get_loc(), e, type, fnQName);
+  return new promote_expr(theRootSctx, e->get_loc(), e, type, errorKind, qname);
 }
 
 
@@ -1434,18 +1435,18 @@
 ********************************************************************************/
 expr_t wrap_in_type_match(
     expr_t e,
-    xqtref_t type,
+    const xqtref_t& type,
     const QueryLoc& loc,
-    const Error& errorCode = err::XPTY0004,
-    store::Item_t fnQName = NULL)
+    TreatIterator::ErrorKind errorKind,
+    store::Item_t qname = NULL)
 {
   TypeManager* tm = e->get_type_manager();
 
-  // treat_expr should be avoided for updating expressions too,
-  // but in that case "type" will be item()* anyway
+  // treat_expr should be avoided for updating expressions, but in that case
+  // "type" will be item()* anyway
   return (TypeOps::is_subtype(tm, *theRTM.ITEM_TYPE_STAR, *type, loc) ?
           e :
-          new treat_expr(theRootSctx, e->get_loc(), e, type, errorCode, true, fnQName));
+          new treat_expr(theRootSctx, e->get_loc(), e, type, errorKind, true, qname));
 }
 
 
@@ -1920,7 +1921,11 @@
   {
     expr_t getExpr = new fo_expr(theRootSctx, loc, varGet, varExpr);
 
-    stmts.push_back(new treat_expr(theRootSctx, loc, getExpr, varType, err::XPTY0004));
+    stmts.push_back(new treat_expr(theRootSctx,
+                                   loc,
+                                   getExpr,
+                                   varType, 
+                                   TreatIterator::TYPE_MATCH));
   }
 }
 
@@ -3542,12 +3547,21 @@
     if (TypeOps::is_builtin_simple(CTX_TM, *returnType))
     {
       body = wrap_in_atomization(body);
-      body = wrap_in_type_promotion(body, returnType, udf->getName());
+
+      body = wrap_in_type_promotion(body,
+                                    returnType,
+                                    PromoteIterator::FUNC_RETURN,
+                                    udf->getName());
+
       body->set_loc(v.get_return_type()->get_location());
     }
     else
     {
-      body = wrap_in_type_match(body, returnType, loc, err::XPTY0004, udf->getName());
+      body = wrap_in_type_match(body,
+                                returnType,
+                                loc, 
+                                TreatIterator::FUNC_RETURN,
+                                udf->getName());
     }
 
     udf->setBody(body);
@@ -4307,7 +4321,8 @@
   domainExpr = wrap_in_type_match(domainExpr,
                                   theRTM.ANY_NODE_TYPE_STAR,
                                   loc,
-                                  zerr::ZDTY0010_INDEX_DOMAIN_TYPE_ERROR);
+                                  TreatIterator::INDEX_DOMAIN,
+                                  index->getName());
 
   // For general indexes, the domain expression must not return duplicate nodes.
   // To see why, consider the following examples:
@@ -4466,7 +4481,8 @@
       keyExpr = wrap_in_type_match(keyExpr,
                                    type,
                                    loc,
-                                   zerr::ZDTY0011_INDEX_KEY_TYPE_ERROR);
+                                   TreatIterator::INDEX_KEY,
+                                   index->getName());
 
       keyTypes[i] = ptype->getBaseBuiltinType();
     }
@@ -5515,7 +5531,11 @@
   expr_t valueExpr = pop_nodestack();
 
   if (varType != NULL)
-    valueExpr = new treat_expr(theRootSctx, loc, valueExpr, varType, err::XPTY0004);
+    valueExpr = new treat_expr(theRootSctx,
+                               loc,
+                               valueExpr,
+                               varType,
+                               TreatIterator::TYPE_MATCH);
 
   push_nodestack(new var_set_expr(theRootSctx, loc, ve, valueExpr));
 
@@ -7048,7 +7068,7 @@
                                         loc,
                                         sv.getp(),
                                         type,
-                                        err::XPDY0050);
+                                        TreatIterator::TREAT_EXPR);
 
       // clauseExpr = [let $caseVar := treat_as($sv, caseType) return NULL]
       clauseExpr = wrap_in_let_flwor(treatExpr, caseVar, NULL);
@@ -7904,7 +7924,7 @@
                                 loc,
                                 pop_nodestack(),
                                 pop_tstack(),
-                                err::XPDY0050));
+                                TreatIterator::TREAT_EXPR));
 }
 
 
@@ -7989,7 +8009,11 @@
       // when casting to type T, where T is QName or subtype of, and the input
       // is not a const expr, then the input MUST be of type T or subtype of.
       if (isCast)
-        return new treat_expr(theRootSctx, loc, node, qnameType, err::XPTY0004);
+        return new treat_expr(theRootSctx,
+                              loc,
+                              node,
+                              qnameType,
+                              TreatIterator::TYPE_MATCH);
       else
         return new instanceof_expr(theRootSctx, loc, node, qnameType);
     }
@@ -8338,7 +8362,7 @@
                                        loc,
                                        DOT_REF,
                                        GENV_TYPESYSTEM.ANY_NODE_TYPE_ONE,
-                                       err::XPTY0020);
+                                       TreatIterator::PATH_DOT);
 
     ctx_path_expr->add_back(sourceExpr);
 
@@ -8371,7 +8395,7 @@
                                      loc,
                                      fnroot.getp(),
                                      GENV_TYPESYSTEM.DOCUMENT_TYPE_ONE,
-                                     err::XPDY0050);
+                                     TreatIterator::TREAT_EXPR);
       push_nodestack(result.getp());
     }
   }
@@ -8462,7 +8486,7 @@
                                          loc,
                                          DOT_REF,
                                          GENV_TYPESYSTEM.ANY_NODE_TYPE_ONE,
-                                         err::XPTY0020);
+                                         TreatIterator::PATH_DOT);
       pathExpr->add_back(sourceExpr);
 
       if (axisStep->get_predicate_list() == NULL)
@@ -8535,21 +8559,21 @@
 #ifdef NODE_SORT_OPT
     if (pathExpr->size() == 0)
     {
-      Error const *errCode = &err::XPTY0019;
+      TreatIterator::ErrorKind errKind = TreatIterator::PATH_STEP;
 
       if (stepExpr->get_expr_kind() == wrapper_expr_kind)
       {
         wrapper_expr* tmp = static_cast<wrapper_expr*>(stepExpr.getp());
         var_expr* dotVar = lookup_var(DOT_VARNAME, loc, zerr::ZXQP0000_NO_ERROR);
         if (tmp->get_expr() == dotVar)
-          errCode = &err::XPTY0020;
+          errKind = TreatIterator::PATH_DOT;
       }
 
       expr_t sourceExpr = new treat_expr(theRootSctx,
                                          stepExpr->get_loc(),
                                          stepExpr,
                                          GENV_TYPESYSTEM.ANY_NODE_TYPE_STAR,
-                                         *errCode);
+                                         errKind);
 
       if (TypeOps::type_max_cnt(pathExpr->get_type_manager(),
                                 *sourceExpr->get_return_type()) > 1)
@@ -9883,7 +9907,7 @@
                                                           loc,
                                                           data_expr,
                                                           theRTM.ANY_ATOMIC_TYPE_QUESTION,
-                                                          err::XPTY0004),
+                                                          TreatIterator::TYPE_MATCH),
                                            tv,
                                            ret));
         return;
@@ -10108,7 +10132,11 @@
     {
       const xqtref_t& resultType = f->getSignature().returnType();
 
-      resultExpr = wrap_in_type_match(foExpr.getp(), resultType, loc);
+      resultExpr = wrap_in_type_match(foExpr.getp(),
+                                      resultType,
+                                      loc,
+                                      TreatIterator::FUNC_RETURN,
+                                      f->getName());
     }
 
     // Some further normalization is required for certain builtin functions
@@ -10259,9 +10287,11 @@
 
         // wrap function's QName
         expr_t qnameExpr = wrap_in_atomization(arguments[0]);
-        qnameExpr        = wrap_in_type_promotion(arguments[0], theRTM.QNAME_TYPE_ONE);
+        qnameExpr        = wrap_in_type_promotion(arguments[0],
+                                                  theRTM.QNAME_TYPE_ONE,
+                                                  PromoteIterator::TYPE_PROMOTION);
 
-        for (unsigned int i = 0; i<numArgs ; i++)
+        for (csize i = 0; i < numArgs ; ++i)
         {
           let_clause_t lc;
           store::Item_t qnameItem;
@@ -10638,11 +10668,11 @@
   if (TypeOps::is_builtin_simple(CTX_TM, *returnType))
   {
     body = wrap_in_atomization(body);
-    body = wrap_in_type_promotion(body, returnType);
+    body = wrap_in_type_promotion(body, returnType, PromoteIterator::TYPE_PROMOTION);
   }
   else
   {
-    body = wrap_in_type_match(body, returnType, loc);
+    body = wrap_in_type_match(body, returnType, loc, TreatIterator::TYPE_MATCH);
   }
 
   // Make the body be the return expr of the flwor that binds the function params.
@@ -11713,7 +11743,7 @@
 
 void end_visit (const SequenceType& v, void* /*visit_state*/)
 {
-  TRACE_VISIT_OUT ();
+  TRACE_VISIT_OUT();
 }
 
 
@@ -11936,11 +11966,8 @@
 
     if (contentType == NULL)
     {
-      throw XQUERY_EXCEPTION(
-        err::XPST0008,
-        ERROR_PARAMS( typeNameItem->getStringValue(), ZED( ElementName ) ),
-        ERROR_LOC( loc )
-      );
+      RAISE_ERROR(err::XPST0008, loc,
+      ERROR_PARAMS(typeNameItem->getStringValue(), ZED(ElementName)));
     }
   }
 
@@ -12219,10 +12246,8 @@
 
     if (!GenericCast::instance()->castableToNCName(lNormalizedTarget))
     {
-      throw XQUERY_EXCEPTION(
-        err::XPTY0004,
-        ERROR_PARAMS(
-          ZED( BadType_23o ), lNormalizedTarget,
+      throw XQUERY_EXCEPTION(err::XPTY0004,
+        ERROR_PARAMS(ZED(BadType_23o), lNormalizedTarget,
           ZED( NoCastTo_45o ), "NCName"
         ),
         ERROR_LOC( loc )
@@ -12733,7 +12758,9 @@
   TRACE_VISIT_OUT ();
 #ifndef ZORBA_NO_FULL_TEXT
   expr_t e( pop_nodestack() );
-  push_nodestack( wrap_in_type_promotion( e, theRTM.ANY_NODE_TYPE_STAR ) );
+  push_nodestack( wrap_in_type_promotion(e,
+                                         theRTM.ANY_NODE_TYPE_STAR,
+                                         PromoteIterator::TYPE_PROMOTION));
 #endif /* ZORBA_NO_FULL_TEXT */
 }
 
@@ -12909,11 +12936,15 @@
 
   if ( e1 ) {
     e1 = wrap_in_atomization( e1 );
-    e1 = wrap_in_type_promotion( e1, theRTM.INTEGER_TYPE_ONE );
+    e1 = wrap_in_type_promotion(e1,
+                                theRTM.INTEGER_TYPE_ONE,
+                                PromoteIterator::TYPE_PROMOTION);
   }
   if ( e2 ) {
     e2 = wrap_in_atomization( e2 );
-    e2 = wrap_in_type_promotion( e2, theRTM.INTEGER_TYPE_ONE );
+    e2 = wrap_in_type_promotion(e2,
+                                theRTM.INTEGER_TYPE_ONE,
+                                PromoteIterator::TYPE_PROMOTION);
   }
 
   ftrange *const r = new ftrange( loc, v.get_mode(), e1, e2 );
@@ -13163,7 +13194,9 @@
 #ifndef ZORBA_NO_FULL_TEXT
   expr_t e( pop_nodestack() );
   e = wrap_in_atomization( e );
-  e = wrap_in_type_promotion( e, theRTM.DOUBLE_TYPE_ONE );
+  e = wrap_in_type_promotion(e,
+                             theRTM.DOUBLE_TYPE_ONE,
+                             PromoteIterator::TYPE_PROMOTION);
   push_ftstack( new ftweight( loc, e ) );
 #endif /* ZORBA_NO_FULL_TEXT */
 }
@@ -13198,7 +13231,9 @@
 #ifndef ZORBA_NO_FULL_TEXT
   expr_t e( pop_nodestack() );
   e = wrap_in_atomization( e );
-  e = wrap_in_type_promotion( e, theRTM.INTEGER_TYPE_ONE );
+  e = wrap_in_type_promotion(e,
+                             theRTM.INTEGER_TYPE_ONE,
+                             PromoteIterator::TYPE_PROMOTION);
   push_ftstack( new ftwindow_filter( loc, e, v.get_unit()->get_unit() ) );
 #endif /* ZORBA_NO_FULL_TEXT */
 }
@@ -13214,7 +13249,9 @@
 #ifndef ZORBA_NO_FULL_TEXT
   expr_t e( pop_nodestack() );
   e = wrap_in_atomization( e );
-  e = wrap_in_type_promotion( e, theRTM.STRING_TYPE_STAR );
+  e = wrap_in_type_promotion(e,
+                             theRTM.STRING_TYPE_STAR,
+                             PromoteIterator::TYPE_PROMOTION);
   push_ftstack( new ftwords( loc, e, v.get_any_all_option()->get_option() ) );
 #endif /* ZORBA_NO_FULL_TEXT */
 }

=== modified file 'src/diagnostics/diagnostic_en.xml'
--- src/diagnostics/diagnostic_en.xml	2012-05-18 22:01:56 +0000
+++ src/diagnostics/diagnostic_en.xml	2012-05-29 14:32:32 +0000
@@ -26,14 +26,6 @@
       <value>${"1": }undefined value${: 2}</value>
     </diagnostic>
 
-    <diagnostic code="XPDY0002">
-      <comment>
-       It is a dynamic error if evaluation of an expression relies on some part
-       of the dynamic context that has not been assigned a value.
-      </comment>
-      <value>"$1": $2</value>
-    </diagnostic>
-
     <diagnostic code="XPST0003">
       <comment>
        It is a static error if an expression is not a valid instance of the
@@ -79,22 +71,6 @@
       </entry>
     </diagnostic>
 
-    <diagnostic code="XPTY0004">
-      <comment>
-       It is a type error if, during the static analysis phase, an expression
-       is found to have a static type that is not appropriate for the context
-       in which the expression occurs, or during the dynamic evaluation phase,
-       the dynamic type of a value does not match a required type as specified
-       by the matching rules in 2.5.4 SequenceType Matching.
-      </comment>
-      <value>$1</value>
-
-      <entry key="FormatNumber_2">
-        <value>"the first parameter to the format-number() function is of type $2, which is not allowed</value>
-      </entry>
-
-    </diagnostic>
-
     <diagnostic code="XPST0005">
       <comment>
        During the analysis phase, it is a static error if the static type
@@ -114,32 +90,6 @@
       <value>"$1": undefined $2</value>
     </diagnostic>
 
-    <diagnostic code="XQST0009">
-      <comment>
-       An implementation that does not support the Schema Import Feature must
-       raise a static error if a Prolog contains a schema import.
-      </comment>
-      <value>schema import not supported</value>
-    </diagnostic>
-
-    <diagnostic code="XQST0012">
-      <comment>
-       It is a static error if the set of definitions contained in all schemas
-       imported by a Prolog do not satisfy the conditions for schema validity
-       specified in Sections 3 and 5 of [XML Schema] Part 1--i.e., each
-       definition must be valid, complete, and unique.
-      </comment>
-      <value>invalid schema definitions</value>
-    </diagnostic>
-
-    <diagnostic code="XQST0013">
-      <comment>
-       It is a static error if an implementation recognizes a pragma but
-       determines that its content is invalid.
-      </comment>
-      <value>invalid pragma content</value>
-    </diagnostic>
-
     <diagnostic code="XPST0017">
       <comment>
        It is a static error if the expanded QName and number of arguments in a
@@ -149,6 +99,79 @@
       <value>"$1": $2</value>
     </diagnostic>
 
+    <diagnostic code="XPST0051">
+      <comment>
+       It is a static error if a QName that is used as an AtomicType in a
+       SequenceType is not defined in the in-scope schema types as an atomic
+       type.
+      </comment>
+      <value>"$1": not defined as atomic type${: 2}</value>
+    </diagnostic>
+
+    <diagnostic code="XPST0080">
+      <comment>
+       It is a static error if the target type of a \c cast or \c castable
+       expression is \c xs:NOTATION or \c xs:anyAtomicType.
+      </comment>
+      <value>"$1": invalid type for "cast" or "castable" exression</value>
+    </diagnostic>
+
+    <diagnostic code="XPST0081">
+      <comment>
+       It is a static error if a QName used in a query contains a namespace
+       prefix that cannot be expanded into a namespace URI by using the
+       statically known namespaces.
+      </comment>
+      <value>"$1": can not expand namespace prefix to URI</value>
+    </diagnostic>
+
+    <diagnostic code="XPST0083">
+      <comment>
+       It is a static error if the target type of a \c cast expression or
+       constructor function is \c xs:QName or a type derived from \c xs:QName
+       or \c xs:NOTATION, and the argument of the cast expression or
+       constructor function is not a string literal.
+      </comment>
+      <value>"$1": not a string literal</value>
+    </diagnostic>
+
+    <diagnostic code="XPTY0004">
+      <comment>
+       It is a type error if, during the static analysis phase, an expression
+       is found to have a static type that is not appropriate for the context
+       in which the expression occurs, or during the dynamic evaluation phase,
+       the dynamic type of a value does not match a required type as specified
+       by the matching rules in 2.5.4 SequenceType Matching.
+      </comment>
+
+      <value>$1</value>
+
+      <entry key="FuncReturn">
+        <value>$2 can not be promoted to return type $3 of function $4()</value>
+      </entry>
+
+      <entry key="FuncParam">
+        <value>$2 can not be promoted to parameter type $3 of function $4()</value>
+      </entry>
+
+      <entry key="TypePromotion">
+        <value>$2 can not be promoted to type $3</value>
+      </entry>
+
+      <entry key="NoMultiSeqTypePromotion">
+        <value>sequence of more than one item can not be promoted to type $2</value>
+      </entry>
+
+      <entry key="TypeMatch">
+        <value>$2 can not be treated as type $3</value>
+      </entry>
+
+      <entry key="FormatNumber_2">
+        <value>the first parameter to the format-number() function is of type $2, which is not allowed</value>
+      </entry>
+
+    </diagnostic>
+
     <diagnostic code="XPTY0018">
       <comment>
        It is a type error if the result of the last step in a path expression
@@ -162,22 +185,15 @@
        It is a type error if the result of a step (other than the last step) in a
        path expression contains an atomic value.
       </comment>
-      <value>path expression non-last step must not be an atomic value</value>
+
+      <value>a non-last axis step produces non-node items</value>
     </diagnostic>
 
     <diagnostic code="XPTY0020">
       <comment>
        It is a type error if, in an axis step, the context item is not a node.
       </comment>
-      <value>axis step context item is not a node</value>
-    </diagnostic>
-
-    <diagnostic code="XQST0022">
-      <comment>
-       It is a static error if the value of a namespace declaration attribute is
-       not a URILiteral.
-      </comment>
-      <value>namespace declaration attribute is not a URI literal</value>
+      <value>context item in axis step is not a node</value>
     </diagnostic>
 
     <diagnostic code="XQTY0024">
@@ -188,33 +204,6 @@
       <value>element constructor content sequence must not have an attribute node following a non-attribute node</value>
     </diagnostic>
 
-    <diagnostic code="XQDY0025">
-      <comment>
-       It is a dynamic error if any attribute of a constructed element does not
-       have a name that is distinct from the names of all other attributes of
-       the constructed element.
-      </comment>
-      <value>"$1": duplicate attribute name</value>
-    </diagnostic>
-
-    <diagnostic code="XQDY0026">
-      <comment>
-       It is a dynamic error if the result of the content expression of a
-       computed processing instruction constructor contains the string "?>".
-      </comment>
-      <value>computed processing instrucion must not contain "?&gt;"</value>
-    </diagnostic>
-
-    <diagnostic code="XQDY0027">
-      <comment>
-       In a validate expression, it is a dynamic error if the root element
-       information item in the PSVI resulting from validation does not have the
-       expected validity property: \c valid if validation mode is \c strict, or
-       either \c valid or \c notKnown if validation mode is \c lax.
-      </comment>
-      <value>"$1": unexpected validity property${: 2}</value>
-    </diagnostic>
-
     <diagnostic code="XQTY0030">
       <comment>
        It is a type error if the argument of a validate expression does not
@@ -223,6 +212,49 @@
       <value>validate argument must be exactly one document or element node</value>
     </diagnostic>
 
+    <diagnostic code="XQTY0086">
+      <comment>
+       It is a type error if the typed value of a copied element or attribute
+       node is namespace-sensitive when construction mode is \c preserve and
+       copy-namespaces mode is \c no-preserve.
+      </comment>
+      <value>typed value of copied element or attribute node is namespace-sensitive when construction mode is preserve and copy-namespaces mode is no-preserve</value>
+    </diagnostic>
+
+    <diagnostic code="XQST0009">
+      <comment>
+       An implementation that does not support the Schema Import Feature must
+       raise a static error if a Prolog contains a schema import.
+      </comment>
+      <value>schema import not supported</value>
+    </diagnostic>
+
+    <diagnostic code="XQST0012">
+      <comment>
+       It is a static error if the set of definitions contained in all schemas
+       imported by a Prolog do not satisfy the conditions for schema validity
+       specified in Sections 3 and 5 of [XML Schema] Part 1--i.e., each
+       definition must be valid, complete, and unique.
+      </comment>
+      <value>invalid schema definitions</value>
+    </diagnostic>
+
+    <diagnostic code="XQST0013">
+      <comment>
+       It is a static error if an implementation recognizes a pragma but
+       determines that its content is invalid.
+      </comment>
+      <value>invalid pragma content</value>
+    </diagnostic>
+
+    <diagnostic code="XQST0022">
+      <comment>
+       It is a static error if the value of a namespace declaration attribute is
+       not a URILiteral.
+      </comment>
+      <value>namespace declaration attribute is not a URI literal</value>
+    </diagnostic>
+
     <diagnostic code="XQST0031">
       <comment>
        It is a static error if the version number specified in a version
@@ -318,30 +350,6 @@
       <value>"$1": non-distinct expanded attribute QName</value>
     </diagnostic>
 
-    <diagnostic code="XQDY0041">
-      <comment>
-       It is a dynamic error if the value of the name expression in a computed
-       processing instruction constructor cannot be cast to the type
-       \c xs:NCName.
-      </comment>
-      <value>can not cast to xs:NCName</value>
-    </diagnostic>
-
-    <diagnostic code="XQDY0044">
-      <comment>
-       It is a static error the node-name of a node constructed by a computed
-       attribute constructor has any of the following properties:
-       - Its namespace prefix is \c xmlns.
-       - It has no namespace prefix and its local name is \c xmlns.
-       - Its namespace URI is <code>http://www.w3.org/2000/xmlns/</code>.
-       - Its namespace prefix is \c xml and its namespace URI is not
-         <code>http://www.w3.org/XML/1998/namespace</code>.
-       - Its namespace prefix is other than \c xml and its namespace URI is
-         <code>http://www.w3.org/XML/1998/namespace</code>.
-      </comment>
-      <value>"$1": invalid attibute node-name</value>
-    </diagnostic>
-
     <diagnostic code="XQST0045">
       <comment>
        It is a static error if the function name in a function declaration is in
@@ -386,29 +394,6 @@
       <value>"$1": duplicate variable declaration</value>
     </diagnostic>
 
-    <diagnostic code="XPDY0050">
-      <comment>
-       It is a dynamic error if the dynamic type of the operand of a treat
-       expression does not match the sequence type specified by the treat
-       expression. This error might also be raised by a path expression
-       beginning with "<code>/</code>" or "<code>//</code>" if the context node
-       is not in a tree that is rooted at a document node. This is because a
-       leading "<code>/</code>" or "<code>//</code>" in a path expression is an
-       abbreviation for an initial step that includes the clause \c treat as \c
-       document-node().
-      </comment>
-      <value>invalid treat expression type</value>
-    </diagnostic>
-
-    <diagnostic code="XPST0051">
-      <comment>
-       It is a static error if a QName that is used as an AtomicType in a
-       SequenceType is not defined in the in-scope schema types as an atomic
-       type.
-      </comment>
-      <value>"$1": not defined as atomic type${: 2}</value>
-    </diagnostic>
-
     <diagnostic code="XQST0054">
       <comment>
        It is a static error if a variable depends on itself.
@@ -457,24 +442,6 @@
       <value>"$1": function name has a null namespace URI</value>
     </diagnostic>
 
-    <diagnostic code="XQDY0061">
-      <comment>
-       It is a dynamic error if the operand of a validate expression is a
-       document node whose children do not consist of exactly one element node
-       and zero or more comment and processing instruction nodes, in any order.
-      </comment>
-      <value>invalid validate expression operand${: 1}</value>
-    </diagnostic>
-
-    <diagnostic code="XQDY0064">
-      <comment>
-       It is a dynamic error if the value of the name expression in a computed
-       processing instruction constructor is equal to "XML" (in any combination
-       of upper and lower case).
-      </comment>
-      <value>"XML": invalid name expression</value>
-    </diagnostic>
-
     <diagnostic code="XQST0065">
       <comment>
        A static error is raised if a Prolog contains more than one ordering mode
@@ -540,25 +507,6 @@
       <value>"$1": duplicate namespace declaration attribute</value>
     </diagnostic>
 
-    <diagnostic code="XQDY0072">
-      <comment>
-       It is a dynamic error if the result of the content expression of a
-       computed comment constructor contains two adjacent hyphens or ends with
-       a hyphen.
-      </comment>
-      <value>comment must not contain "--" or end with "-"</value>
-    </diagnostic>
-
-    <diagnostic code="XQDY0074">
-      <comment>
-       It is a dynamic error if the value of the name expression in a computed
-       element or attribute constructor cannot be converted to an expanded
-       QName (for example, because it contains a namespace prefix not found in
-       statically known namespaces).
-      </comment>
-      <value>${"1": }can not convert to expanded QName${: 2}</value>
-    </diagnostic>
-
     <diagnostic code="XQST0076">
       <comment>
        It is a static error if a \c collation subclause in an order by clause
@@ -577,42 +525,6 @@
       <value>unknown pragma or empty expression</value>
     </diagnostic>
 
-    <diagnostic code="XPST0080">
-      <comment>
-       It is a static error if the target type of a \c cast or \c castable
-       expression is \c xs:NOTATION or \c xs:anyAtomicType.
-      </comment>
-      <value>"$1": invalid type for "cast" or "castable" exression</value>
-    </diagnostic>
-
-    <diagnostic code="XPST0081">
-      <comment>
-       It is a static error if a QName used in a query contains a namespace
-       prefix that cannot be expanded into a namespace URI by using the
-       statically known namespaces.
-      </comment>
-      <value>"$1": can not expand namespace prefix to URI</value>
-    </diagnostic>
-
-    <diagnostic code="XPST0083">
-      <comment>
-       It is a static error if the target type of a \c cast expression or
-       constructor function is \c xs:QName or a type derived from \c xs:QName
-       or \c xs:NOTATION, and the argument of the cast expression or
-       constructor function is not a string literal.
-      </comment>
-      <value>"$1": not a string literal</value>
-    </diagnostic>
-
-    <diagnostic code="XQDY0084">
-      <comment>
-       It is a dynamic error if the element validated by a \c validate statement
-       does not have a top-level element declaration in the in-scope element
-       declarations, if validation mode is \c strict.
-      </comment>
-      <value>validated element does not have a top-level element declaration in the in-scope element declarations${: 1}</value>
-    </diagnostic>
-
     <diagnostic code="XQST0085">
       <comment>
        It is a static error if the namespace URI in a namespace declaration
@@ -622,15 +534,6 @@
       <value>namespace URI is empty</value>
     </diagnostic>
 
-    <diagnostic code="XQTY0086">
-      <comment>
-       It is a type error if the typed value of a copied element or attribute
-       node is namespace-sensitive when construction mode is \c preserve and
-       copy-namespaces mode is \c no-preserve.
-      </comment>
-      <value>typed value of copied element or attribute node is namespace-sensitive when construction mode is preserve and copy-namespaces mode is no-preserve</value>
-    </diagnostic>
-
     <diagnostic code="XQST0087">
       <comment>
        It is a static error if the encoding specified in a Version Declaration
@@ -664,23 +567,6 @@
       <value>"$1": invalid character reference in XML $2</value>
     </diagnostic>
 
-    <diagnostic code="XQDY0091">
-      <comment>
-       An implementation MAY raise a dynamic error if an \c xml:id error, as
-       defined in [XML ID], is encountered during construction of an attribute
-       named \c xml:id.
-      </comment>
-      <value>"xml:id" encountered</value>
-    </diagnostic>
-
-    <diagnostic code="XQDY0092">
-      <comment>
-       An implementation MAY raise a dynamic error if a constructed attribute
-       named \c xml:space has a value other than \c preserve or \c default.
-      </comment>
-      <value>"$1": must be either "preserve" or "default"</value>
-    </diagnostic>
-
     <diagnostic code="XQST0093">
       <comment>
        It is a static error to import a module M1 if there exists a sequence of
@@ -691,20 +577,6 @@
       <value>"$1": module must not depend on itself</value>
     </diagnostic>
 
-    <diagnostic code="XQDY0096">
-      <comment>
-       It is a dynamic error the node-name of a node constructed by a computed
-       element constructor has any of the following properties:
-       - Its namespace prefix is \c xmlns.
-       - Its namespace URI is <code>http://www.w3.org/2000/xmlns/</code>.
-       - Its namespace prefix is \c xml and its namespace URI is not
-         <code>http://www.w3.org/XML/1998/namespace</code>.
-       - Its namespace prefix is other than \c xml and its namespace URI is
-         <code>http://www.w3.org/XML/1998/namespace</code>.
-      </comment>
-      <value>"$1": invalid node-name</value>
-    </diagnostic>
-
     <diagnostic code="XQST0098">
       <comment>
        It is a static error if, for any named or unnamed decimal format, the
@@ -747,6 +619,157 @@
       <value>$1</value>
     </diagnostic>
 
+    <diagnostic code="XPDY0002">
+      <comment>
+       It is a dynamic error if evaluation of an expression relies on some part
+       of the dynamic context that has not been assigned a value.
+      </comment>
+      <value>"$1": $2</value>
+    </diagnostic>
+
+    <diagnostic code="XPDY0050">
+      <comment>
+       It is a dynamic error if the dynamic type of the operand of a treat
+       expression does not match the sequence type specified by the treat
+       expression. This error might also be raised by a path expression
+       beginning with "<code>/</code>" or "<code>//</code>" if the context node
+       is not in a tree that is rooted at a document node. This is because a
+       leading "<code>/</code>" or "<code>//</code>" in a path expression is an
+       abbreviation for an initial step that includes the clause \c treat as \c
+       document-node().
+      </comment>
+
+      <value>"$1" cannot be treated as type $2</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0025">
+      <comment>
+       It is a dynamic error if any attribute of a constructed element does not
+       have a name that is distinct from the names of all other attributes of
+       the constructed element.
+      </comment>
+      <value>"$1": duplicate attribute name</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0026">
+      <comment>
+       It is a dynamic error if the result of the content expression of a
+       computed processing instruction constructor contains the string "?>".
+      </comment>
+      <value>computed processing instrucion must not contain "?&gt;"</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0027">
+      <comment>
+       In a validate expression, it is a dynamic error if the root element
+       information item in the PSVI resulting from validation does not have the
+       expected validity property: \c valid if validation mode is \c strict, or
+       either \c valid or \c notKnown if validation mode is \c lax.
+      </comment>
+      <value>"$1": unexpected validity property${: 2}</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0041">
+      <comment>
+       It is a dynamic error if the value of the name expression in a computed
+       processing instruction constructor cannot be cast to the type
+       \c xs:NCName.
+      </comment>
+      <value>can not cast to xs:NCName</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0044">
+      <comment>
+       It is a static error the node-name of a node constructed by a computed
+       attribute constructor has any of the following properties:
+       - Its namespace prefix is \c xmlns.
+       - It has no namespace prefix and its local name is \c xmlns.
+       - Its namespace URI is <code>http://www.w3.org/2000/xmlns/</code>.
+       - Its namespace prefix is \c xml and its namespace URI is not
+         <code>http://www.w3.org/XML/1998/namespace</code>.
+       - Its namespace prefix is other than \c xml and its namespace URI is
+         <code>http://www.w3.org/XML/1998/namespace</code>.
+      </comment>
+      <value>"$1": invalid attibute node-name</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0061">
+      <comment>
+       It is a dynamic error if the operand of a validate expression is a
+       document node whose children do not consist of exactly one element node
+       and zero or more comment and processing instruction nodes, in any order.
+      </comment>
+      <value>invalid validate expression operand${: 1}</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0064">
+      <comment>
+       It is a dynamic error if the value of the name expression in a computed
+       processing instruction constructor is equal to "XML" (in any combination
+       of upper and lower case).
+      </comment>
+      <value>"XML": invalid name expression</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0072">
+      <comment>
+       It is a dynamic error if the result of the content expression of a
+       computed comment constructor contains two adjacent hyphens or ends with
+       a hyphen.
+      </comment>
+      <value>comment must not contain "--" or end with "-"</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0074">
+      <comment>
+       It is a dynamic error if the value of the name expression in a computed
+       element or attribute constructor cannot be converted to an expanded
+       QName (for example, because it contains a namespace prefix not found in
+       statically known namespaces).
+      </comment>
+      <value>${"1": }can not convert to expanded QName${: 2}</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0084">
+      <comment>
+       It is a dynamic error if the element validated by a \c validate statement
+       does not have a top-level element declaration in the in-scope element
+       declarations, if validation mode is \c strict.
+      </comment>
+      <value>validated element does not have a top-level element declaration in the in-scope element declarations${: 1}</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0091">
+      <comment>
+       An implementation MAY raise a dynamic error if an \c xml:id error, as
+       defined in [XML ID], is encountered during construction of an attribute
+       named \c xml:id.
+      </comment>
+      <value>"xml:id" encountered</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0092">
+      <comment>
+       An implementation MAY raise a dynamic error if a constructed attribute
+       named \c xml:space has a value other than \c preserve or \c default.
+      </comment>
+      <value>"$1": must be either "preserve" or "default"</value>
+    </diagnostic>
+
+    <diagnostic code="XQDY0096">
+      <comment>
+       It is a dynamic error the node-name of a node constructed by a computed
+       element constructor has any of the following properties:
+       - Its namespace prefix is \c xmlns.
+       - Its namespace URI is <code>http://www.w3.org/2000/xmlns/</code>.
+       - Its namespace prefix is \c xml and its namespace URI is not
+         <code>http://www.w3.org/XML/1998/namespace</code>.
+       - Its namespace prefix is other than \c xml and its namespace URI is
+         <code>http://www.w3.org/XML/1998/namespace</code>.
+      </comment>
+      <value>"$1": invalid node-name</value>
+    </diagnostic>
+
     <diagnostic code="XTDE1310">
       <comment>
        It is a non-recoverable dynamic error if the picture string does not
@@ -2243,17 +2266,18 @@
     </diagnostic>
 
     <diagnostic code="ZDTY0010" name="INDEX_DOMAIN_TYPE_ERROR">
-      <value>"$1": index domain expression yields a non-node item</value>
+      <value>non-node item in domain expression of index $1</value>
     </diagnostic>
 
     <diagnostic code="ZDTY0011" name="INDEX_KEY_TYPE_ERROR">
-      <value>result of some key expression of index $1 does not match its declared type</value>
+       <value>item type "$1" does not match declared type "$2" for key expression of index $3</value>
     </diagnostic>
 
     <diagnostic code="ZDTY0012" name="INDEX_KEY_TYPE_ERROR">
       <value>"$1": general range index key item has type for which no ordering relationship exists</value>
     </diagnostic>
 
+
     <!--////////// Zorba Debugger Errors ///////////////////////////////////-->
 
     <diagnostic code="ZGDB0001" name="CANNOT_CONNECT_TO_CLIENT"
@@ -2762,19 +2786,11 @@
       <value>empty sequence not allowed as 2nd argument of fn:remove()</value>
     </entry>
 
-    <entry key="EmptySeqNoPromoteTo">
-      <value>empty sequence can not be promoted to type "$2"</value>
-    </entry>
-
     <entry key="EmptySeqNoSearchItem">
       <value>empty sequence not allowed as search item of fn:index-of()</value>
     </entry>
 
-    <entry key="EmptySeqNotAsFunctionResult_23">
-      <value>empty sequence not allowed as result of function $2() that returns "$3"</value>
-    </entry>
-
-    <entry key="EmptySequence">
+     <entry key="EmptySequence">
       <value>empty sequence</value>
     </entry>
 
@@ -2994,14 +3010,6 @@
       <value>sequence of more than one item can not be tested for atomic value equivalence</value>
     </entry>
 
-    <entry key="NoSeqTypePromotion">
-      <value>type promotion not possible on sequence of more than one item</value>
-    </entry>
-
-    <entry key="NoSeqTypePromotion_23">
-      <value>sequence of more than one item can not be promoted to the return type "$2" of function $3()</value>
-    </entry>
-
     <entry key="NoSerializationCallbackForDocColMod">
       <value>document, collection, or module resolver required but no SerializationCallback given</value>
     </entry>
@@ -3034,14 +3042,6 @@
       <value>type of variable "$4" is not among the in-scope types module "$5"</value>
     </entry>
 
-    <entry key="NoTypePromotion_23">
-      <value>"$2": can not promote to type "$3"</value>
-    </entry>
-
-    <entry key="NoTypePromotion_234">
-      <value>"$2": can not promote to return type "$3" of function $4()</value>
-    </entry>
-
     <entry key="NoURIAuthority">
       <value>no authority</value>
     </entry>
@@ -3679,8 +3679,7 @@
       <value>: a sub-picture must contain at least one character that is an optional-digit-sign or a member of the decimal-digit-family</value>
     </entry>
     
-
-  </subvalues>
+   </subvalues>
 
 </diagnostic-list>
 <!-- vim:set et sw=2 ts=2: -->

=== modified file 'src/diagnostics/dict.cpp'
--- src/diagnostics/dict.cpp	2012-05-03 12:31:51 +0000
+++ src/diagnostics/dict.cpp	2012-05-29 14:32:32 +0000
@@ -18,6 +18,7 @@
 #include <cstring>
 #include <functional>
 #include <utility>                      /* for pair */
+#include <cassert>
 
 #include "zorbautils/locale.h"
 
@@ -76,6 +77,8 @@
   range_type const result =
     ::equal_range( begin, end, entry_to_find, less_entry() );
 
+  assert(result.first != result.second);
+
   return result.first == result.second ? key : result.first->value;
 }
 

=== modified file 'src/diagnostics/pregenerated/diagnostic_list.cpp'
--- src/diagnostics/pregenerated/diagnostic_list.cpp	2012-05-18 22:01:56 +0000
+++ src/diagnostics/pregenerated/diagnostic_list.cpp	2012-05-29 14:32:32 +0000
@@ -31,33 +31,33 @@
 XQueryErrorCode XPST0001( "XPST0001" );
 
 
-XQueryErrorCode XPDY0002( "XPDY0002" );
-
-
 XQueryErrorCode XPST0003( "XPST0003" );
 
 
-XQueryErrorCode XPTY0004( "XPTY0004" );
-
-
 XQueryErrorCode XPST0005( "XPST0005" );
 
 
 XQueryErrorCode XPST0008( "XPST0008" );
 
 
-XQueryErrorCode XQST0009( "XQST0009" );
-
-
-XQueryErrorCode XQST0012( "XQST0012" );
-
-
-XQueryErrorCode XQST0013( "XQST0013" );
-
-
 XQueryErrorCode XPST0017( "XPST0017" );
 
 
+XQueryErrorCode XPST0051( "XPST0051" );
+
+
+XQueryErrorCode XPST0080( "XPST0080" );
+
+
+XQueryErrorCode XPST0081( "XPST0081" );
+
+
+XQueryErrorCode XPST0083( "XPST0083" );
+
+
+XQueryErrorCode XPTY0004( "XPTY0004" );
+
+
 XQueryErrorCode XPTY0018( "XPTY0018" );
 
 
@@ -67,24 +67,27 @@
 XQueryErrorCode XPTY0020( "XPTY0020" );
 
 
-XQueryErrorCode XQST0022( "XQST0022" );
-
-
 XQueryErrorCode XQTY0024( "XQTY0024" );
 
 
-XQueryErrorCode XQDY0025( "XQDY0025" );
-
-
-XQueryErrorCode XQDY0026( "XQDY0026" );
-
-
-XQueryErrorCode XQDY0027( "XQDY0027" );
-
-
 XQueryErrorCode XQTY0030( "XQTY0030" );
 
 
+XQueryErrorCode XQTY0086( "XQTY0086" );
+
+
+XQueryErrorCode XQST0009( "XQST0009" );
+
+
+XQueryErrorCode XQST0012( "XQST0012" );
+
+
+XQueryErrorCode XQST0013( "XQST0013" );
+
+
+XQueryErrorCode XQST0022( "XQST0022" );
+
+
 XQueryErrorCode XQST0031( "XQST0031" );
 
 
@@ -112,12 +115,6 @@
 XQueryErrorCode XQST0040( "XQST0040" );
 
 
-XQueryErrorCode XQDY0041( "XQDY0041" );
-
-
-XQueryErrorCode XQDY0044( "XQDY0044" );
-
-
 XQueryErrorCode XQST0045( "XQST0045" );
 
 
@@ -133,12 +130,6 @@
 XQueryErrorCode XQST0049( "XQST0049" );
 
 
-XQueryErrorCode XPDY0050( "XPDY0050" );
-
-
-XQueryErrorCode XPST0051( "XPST0051" );
-
-
 XQueryErrorCode XQST0054( "XQST0054" );
 
 
@@ -157,12 +148,6 @@
 XQueryErrorCode XQST0060( "XQST0060" );
 
 
-XQueryErrorCode XQDY0061( "XQDY0061" );
-
-
-XQueryErrorCode XQDY0064( "XQDY0064" );
-
-
 XQueryErrorCode XQST0065( "XQST0065" );
 
 
@@ -184,36 +169,15 @@
 XQueryErrorCode XQST0071( "XQST0071" );
 
 
-XQueryErrorCode XQDY0072( "XQDY0072" );
-
-
-XQueryErrorCode XQDY0074( "XQDY0074" );
-
-
 XQueryErrorCode XQST0076( "XQST0076" );
 
 
 XQueryErrorCode XQST0079( "XQST0079" );
 
 
-XQueryErrorCode XPST0080( "XPST0080" );
-
-
-XQueryErrorCode XPST0081( "XPST0081" );
-
-
-XQueryErrorCode XPST0083( "XPST0083" );
-
-
-XQueryErrorCode XQDY0084( "XQDY0084" );
-
-
 XQueryErrorCode XQST0085( "XQST0085" );
 
 
-XQueryErrorCode XQTY0086( "XQTY0086" );
-
-
 XQueryErrorCode XQST0087( "XQST0087" );
 
 
@@ -226,18 +190,9 @@
 XQueryErrorCode XQST0090( "XQST0090" );
 
 
-XQueryErrorCode XQDY0091( "XQDY0091" );
-
-
-XQueryErrorCode XQDY0092( "XQDY0092" );
-
-
 XQueryErrorCode XQST0093( "XQST0093" );
 
 
-XQueryErrorCode XQDY0096( "XQDY0096" );
-
-
 XQueryErrorCode XQST0098( "XQST0098" );
 
 
@@ -250,6 +205,51 @@
 XQueryErrorCode XQST0111( "XQST0111" );
 
 
+XQueryErrorCode XPDY0002( "XPDY0002" );
+
+
+XQueryErrorCode XPDY0050( "XPDY0050" );
+
+
+XQueryErrorCode XQDY0025( "XQDY0025" );
+
+
+XQueryErrorCode XQDY0026( "XQDY0026" );
+
+
+XQueryErrorCode XQDY0027( "XQDY0027" );
+
+
+XQueryErrorCode XQDY0041( "XQDY0041" );
+
+
+XQueryErrorCode XQDY0044( "XQDY0044" );
+
+
+XQueryErrorCode XQDY0061( "XQDY0061" );
+
+
+XQueryErrorCode XQDY0064( "XQDY0064" );
+
+
+XQueryErrorCode XQDY0072( "XQDY0072" );
+
+
+XQueryErrorCode XQDY0074( "XQDY0074" );
+
+
+XQueryErrorCode XQDY0084( "XQDY0084" );
+
+
+XQueryErrorCode XQDY0091( "XQDY0091" );
+
+
+XQueryErrorCode XQDY0092( "XQDY0092" );
+
+
+XQueryErrorCode XQDY0096( "XQDY0096" );
+
+
 XQueryErrorCode XTDE1310( "XTDE1310" );
 
 

=== modified file 'src/diagnostics/pregenerated/dict_en.cpp'
--- src/diagnostics/pregenerated/dict_en.cpp	2012-05-18 22:01:56 +0000
+++ src/diagnostics/pregenerated/dict_en.cpp	2012-05-29 14:32:32 +0000
@@ -113,7 +113,7 @@
   { "SESU0011", "\"$1\": unsupported normalization form" },
   { "SESU0013", "\"$1\": unsupported $2 version; supported versions are: $3" },
   { "XPDY0002", "\"$1\": $2" },
-  { "XPDY0050", "invalid treat expression type" },
+  { "XPDY0050", "\"$1\" cannot be treated as type $2" },
   { "XPST0001", "${\"1\": }undefined value${: 2}" },
   { "XPST0003", "invalid expression${: 1}" },
   { "XPST0005", "static type must not be empty-sequence()" },
@@ -125,8 +125,8 @@
   { "XPST0083", "\"$1\": not a string literal" },
   { "XPTY0004", "$1" },
   { "XPTY0018", "path expression last step must not have nodes and atomic values" },
-  { "XPTY0019", "path expression non-last step must not be an atomic value" },
-  { "XPTY0020", "axis step context item is not a node" },
+  { "XPTY0019", "a non-last axis step produces non-node items" },
+  { "XPTY0020", "context item in axis step is not a node" },
   { "XQDY0025", "\"$1\": duplicate attribute name" },
   { "XQDY0026", "computed processing instrucion must not contain \"?>\"" },
   { "XQDY0027", "\"$1\": unexpected validity property${: 2}" },
@@ -331,8 +331,8 @@
   { "ZDST0048", "\"$1\": integrity constraint declaration in foreign module" },
   { "ZDST0060", "\"$1\": feature not supported; $2" },
   { "ZDTY0001", "\"$1\": invalid node type in collection \"$2\"" },
-  { "ZDTY0010", "\"$1\": index domain expression yields a non-node item" },
-  { "ZDTY0011", "result of some key expression of index $1 does not match its declared type" },
+  { "ZDTY0010", "non-node item in domain expression of index $1" },
+  { "ZDTY0011", "item type \"$1\" does not match declared type \"$2\" for key expression of index $3" },
   { "ZDTY0012", "\"$1\": general range index key item has type for which no ordering relationship exists" },
 #if defined(ZORBA_WITH_DEBUGGER)
   { "ZGDB0001", "" },
@@ -498,9 +498,7 @@
   { "~EmptySeqNoCastToQName", "empty sequence can not be cast to QName" },
   { "~EmptySeqNoCastToTypeWithQuantOne", "empty sequence can not be cast to type with quantifier '1'" },
   { "~EmptySeqNoFnRemoveArg", "empty sequence not allowed as 2nd argument of fn:remove()" },
-  { "~EmptySeqNoPromoteTo", "empty sequence can not be promoted to type \"$2\"" },
   { "~EmptySeqNoSearchItem", "empty sequence not allowed as search item of fn:index-of()" },
-  { "~EmptySeqNotAsFunctionResult_23", "empty sequence not allowed as result of function $2() that returns \"$3\"" },
   { "~EmptySequence", "empty sequence" },
   { "~ErrorCodeMessage_12", "error $2: $3" },
   { "~ExpectedNumericOrDurationType", "expected numeric or duration type" },
@@ -568,8 +566,6 @@
   { "~NoSeqForFnOp_2", "sequence of more than one item can not be operand for function \"$2()\"" },
   { "~NoSeqInValueComp", "sequnce of more than one item can not be in value comparisons" },
   { "~NoSeqTestedForAtomicEquiv", "sequence of more than one item can not be tested for atomic value equivalence" },
-  { "~NoSeqTypePromotion", "type promotion not possible on sequence of more than one item" },
-  { "~NoSeqTypePromotion_23", "sequence of more than one item can not be promoted to the return type \"$2\" of function $3()" },
   { "~NoSerializationCallbackForDocColMod", "document, collection, or module resolver required but no SerializationCallback given" },
   { "~NoSerializationCallbackForModule", "no SerializationCallback for required external module \"$2\"" },
   { "~NoSerializationCallbackForTraceStream", "no SerializationCallback for required trace stream" },
@@ -578,8 +574,6 @@
   { "~NoTypeInCtx", "undefined type in current context" },
   { "~NoTypeInMainModule_4", "type of variable \"$4\" is not among the in-scope types of the main module" },
   { "~NoTypeInModule_45", "type of variable \"$4\" is not among the in-scope types module \"$5\"" },
-  { "~NoTypePromotion_23", "\"$2\": can not promote to type \"$3\"" },
-  { "~NoTypePromotion_234", "\"$2\": can not promote to return type \"$3\" of function $4()" },
   { "~NoURIAuthority", "no authority" },
   { "~NoURIInStore", "URI for document not found in store" },
   { "~NoURIScheme", "no URI scheme" },
@@ -778,7 +772,12 @@
   { "~XPST0003_TryCatchExpr11", "\"try/catch\" expressions only available in XQuery 3.0 or later" },
   { "~XPST0003_WindowClause11", "\"window\" clause only available in XQuery 3.0 or later" },
   { "~XPST0003_XQueryVersionAtLeast30_2", "\"$2\": XQuery version must be at least 3.0" },
-  { "~XPTY0004_FormatNumber_2", "\"the first parameter to the format-number() function is of type $2, which is not allowed" },
+  { "~XPTY0004_FormatNumber_2", "the first parameter to the format-number() function is of type $2, which is not allowed" },
+  { "~XPTY0004_FuncParam", "$2 can not be promoted to parameter type $3 of function $4()" },
+  { "~XPTY0004_FuncReturn", "$2 can not be promoted to return type $3 of function $4()" },
+  { "~XPTY0004_NoMultiSeqTypePromotion", "sequence of more than one item can not be promoted to type $2" },
+  { "~XPTY0004_TypeMatch", "$2 can not be treated as type $3" },
+  { "~XPTY0004_TypePromotion", "$2 can not be promoted to type $3" },
   { "~XQST0106_CONFLICTING", "conflicting" },
   { "~XQST0106_THE_SAME", "the same" },
   { "~XUST0001_CONCAT", "comma expression with updating and non-updating branches" },

=== modified file 'src/diagnostics/util_macros.h'
--- src/diagnostics/util_macros.h	2012-05-03 12:31:51 +0000
+++ src/diagnostics/util_macros.h	2012-05-29 14:32:32 +0000
@@ -19,6 +19,11 @@
                          params,                              \
                          ERROR_LOC(loc))
 
+#define RAISE_ERROR_VAR(errcode, loc, params)                 \
+  throw XQUERY_EXCEPTION_VAR(errcode,                         \
+                             params,                          \
+                             ERROR_LOC(loc))
+
 
 #define RAISE_ERROR_NO_LOC(errcode, params)                   \
   throw XQUERY_EXCEPTION(errcode,                             \

=== modified file 'src/diagnostics/xquery_diagnostics.h'
--- src/diagnostics/xquery_diagnostics.h	2012-05-03 12:31:51 +0000
+++ src/diagnostics/xquery_diagnostics.h	2012-05-29 14:32:32 +0000
@@ -132,13 +132,6 @@
     throw XQUERY_EXCEPTION( LOCALNAME, ERROR_PARAMS( oss.str() ) ); \
   } while (0)
 
-#define ZORBA_ERROR_VAR_LOC_OSS(ERROR_VAR,LOC,MSG)                      \
-  do {                                                                  \
-    std::ostringstream oss;                                             \
-    oss << MSG;                                                         \
-    throw XQUERY_EXCEPTION_VAR( ERROR_VAR, ERROR_PARAMS(oss.str()), ERROR_LOC(LOC) ); \
-  } while (0)
-
 } // namespace zorba
 
 #endif /* ZORBA_XQUERY_DIAGNOSTICS_H */

=== modified file 'src/runtime/core/path_iterators.cpp'
--- src/runtime/core/path_iterators.cpp	2012-05-03 12:31:51 +0000
+++ src/runtime/core/path_iterators.cpp	2012-05-29 14:32:32 +0000
@@ -412,6 +412,7 @@
 
     if (!result->isNode())
     {
+      assert(false);
       throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
     }
 
@@ -472,6 +473,7 @@
 
       if (!state->theContextNode->isNode())
       {
+        assert(false);
         throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
       }
     }
@@ -530,6 +532,7 @@
 
     if (!state->theContextNode->isNode())
     {
+      assert(false);
       throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
     }
 
@@ -572,6 +575,7 @@
 
     if (!state->theContextNode->isNode())
     {
+      assert(false);
       throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
     }
 
@@ -630,6 +634,7 @@
 
     if (!state->theContextNode->isNode())
     {
+      assert(false);
       throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
     }
 
@@ -697,6 +702,7 @@
 
     if (!state->theContextNode->isNode())
     {
+      assert(false);
       throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
     }
 
@@ -748,6 +754,7 @@
 
     if (!state->theContextNode->isNode())
     {
+      assert(false);
       throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
     }
 
@@ -821,6 +828,7 @@
 
       if (!state->theContextNode->isNode())
       {
+        assert(false);
         throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
       }
     }
@@ -884,6 +892,7 @@
 
       if (!state->theContextNode->isNode())
       {
+        assert(false);
         throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
       }
     }
@@ -967,6 +976,7 @@
 
       if (!state->theContextNode->isNode())
       {
+        assert(false);
         throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
       }
     }
@@ -1046,6 +1056,7 @@
       
       if (!state->theContextNode->isNode())
       {
+        assert(false);
         throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
       }
     }
@@ -1149,6 +1160,7 @@
 
       if (!state->theContextNode->isNode())
       {
+        assert(false);
         throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
       }
     }
@@ -1232,6 +1244,7 @@
 
       if (!state->theContextNode->isNode())
       {
+        assert(false);
         throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
       }
     }
@@ -1337,6 +1350,7 @@
 
     if (!state->theContextNode->isNode())
     {
+      assert(false);
       throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
     }
 
@@ -1551,6 +1565,7 @@
 
     if (!state->theContextNode->isNode())
     {
+      assert(false);
       throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
     }
 
@@ -1722,6 +1737,7 @@
 
     if (!state->theContextNode->isNode())
     {
+      assert(false);
       throw XQUERY_EXCEPTION( err::XPTY0020, ERROR_LOC( loc ) );
     }
 

=== modified file 'src/runtime/core/sequencetypes.cpp'
--- src/runtime/core/sequencetypes.cpp	2012-05-03 12:31:51 +0000
+++ src/runtime/core/sequencetypes.cpp	2012-05-29 14:32:32 +0000
@@ -324,16 +324,18 @@
 
 PromoteIterator::PromoteIterator(
     static_context* sctx,
-    const QueryLoc& aLoc,
-    PlanIter_t& aChild,
-    const xqtref_t& aPromoteType,
-    store::Item_t fnQName)
+    const QueryLoc& loc,
+    PlanIter_t& child,
+    const xqtref_t& promoteType,
+    ErrorKind err,
+    store::Item_t qname)
   :
-  UnaryBaseIterator<PromoteIterator, PlanIteratorState>(sctx, aLoc, aChild),
-  theFnQName(fnQName)
+  UnaryBaseIterator<PromoteIterator, PlanIteratorState>(sctx, loc, child),
+  theErrorKind(err),
+  theQName(qname)
 {
-  thePromoteType = TypeOps::prime_type(sctx->get_typemanager(), *aPromoteType);
-  theQuantifier = TypeOps::quantifier(*aPromoteType); 
+  thePromoteType = TypeOps::prime_type(sctx->get_typemanager(), *promoteType);
+  theQuantifier = TypeOps::quantifier(*promoteType); 
 }
 
 
@@ -342,6 +344,18 @@
 }
 
 
+void PromoteIterator::serialize(::zorba::serialization::Archiver& ar)
+{
+  serialize_baseclass(ar,
+  (UnaryBaseIterator<PromoteIterator, PlanIteratorState>*)this);
+
+  ar & thePromoteType;
+  SERIALIZE_ENUM(TypeConstants::quantifier_t, theQuantifier);
+  SERIALIZE_ENUM(ErrorKind, theErrorKind);
+  ar & theQName;
+}
+
+
 bool PromoteIterator::nextImpl(store::Item_t& result, PlanState& planState) const
 {
   store::Item_t lItem;
@@ -357,21 +371,7 @@
     if (theQuantifier == TypeConstants::QUANT_PLUS ||
         theQuantifier == TypeConstants::QUANT_ONE)
     {
-      zstring type = thePromoteType->toSchemaString() + 
-                     (theQuantifier == TypeConstants::QUANT_PLUS? "+" : "");
-
-      if (theFnQName.getp())
-      {
-        RAISE_ERROR(err::XPTY0004, loc,
-        ERROR_PARAMS(ZED(EmptySeqNotAsFunctionResult_23),
-                     theFnQName->getStringValue(),
-                     type));
-      }
-      else
-      {
-        RAISE_ERROR(err::XPTY0004, loc,
-        ERROR_PARAMS(ZED(EmptySeqNoPromoteTo), type));
-      }
+      raiseError("empty-sequence()");
     }
   }
   else if (theQuantifier == TypeConstants::QUANT_QUESTION ||
@@ -379,45 +379,14 @@
   {
     if (consumeNext(temp, theChild.getp(), planState))
     {
-      if (theFnQName.getp())
-      {
-        std::string tmp = BUILD_STRING(thePromoteType->toSchemaString(),
-                                       (theQuantifier == TypeConstants::QUANT_QUESTION ?
-                                        "?" : ""));
-
-        RAISE_ERROR(err::XPTY0004, loc,
-        ERROR_PARAMS(ZED(NoSeqTypePromotion_23),
-                     tmp,
-                     theFnQName->getStringValue()));
-      }
-      else
-      {
-        RAISE_ERROR(err::XPTY0004, loc, ERROR_PARAMS(ZED(NoSeqTypePromotion)));
-      }
+      raiseError("sequence of more than one item");
     }
 
     // catch exceptions to add/change the error location
     if (! GenericCast::promote(result, lItem, thePromoteType, tm, loc))
     {
-      xqtref_t valueType = tm->create_value_type(lItem);
-
-      if ( theFnQName.getp() )
-      {
-        throw XQUERY_EXCEPTION(err::XPTY0004,
-                               ERROR_PARAMS(ZED(NoTypePromotion_234),
-                                            valueType->toSchemaString(),
-                                            thePromoteType->toSchemaString(),
-                                            theFnQName->getStringValue()),
-                               ERROR_LOC(loc));
-      }
-      else
-      {
-        throw XQUERY_EXCEPTION(err::XPTY0004,
-                               ERROR_PARAMS(ZED(NoTypePromotion_23),
-                                            valueType->toSchemaString(),
-                                            thePromoteType->toSchemaString()),
-                               ERROR_LOC(loc));
-      }
+      zstring valueType = tm->create_value_type(lItem)->toSchemaString();
+      raiseError(valueType);
     }
 
     STACK_PUSH(true, state);
@@ -428,21 +397,8 @@
     {
       if (! GenericCast::promote(result, lItem, thePromoteType, tm, loc))
       {
-        if (theFnQName.getp())
-        {
-          RAISE_ERROR(err::XPTY0004, loc,
-          ERROR_PARAMS(ZED(NoTypePromotion_234),
-                       tm->create_value_type(lItem)->toSchemaString(),
-                       thePromoteType->toSchemaString(),
-                       theFnQName->getStringValue()));
-        }
-        else
-        {
-          RAISE_ERROR(err::XPTY0004, loc,
-          ERROR_PARAMS(ZED(NoTypePromotion_23),
-                       tm->create_value_type(lItem)->toSchemaString(),
-                       thePromoteType->toSchemaString()));
-        }
+        zstring valueType = tm->create_value_type(lItem)->toSchemaString();
+        raiseError(valueType);
       }
       else
       {
@@ -456,6 +412,61 @@
 }
 
 
+void PromoteIterator::raiseError(const zstring& valueType) const
+{
+  zstring targetType;
+
+  if (thePromoteType->type_kind() == XQType::NONE_KIND && 
+      theQuantifier == TypeConstants::QUANT_QUESTION)
+  {
+    targetType = "empty-sequence()";
+  }
+  else
+  {
+    targetType = thePromoteType->toSchemaString();
+
+    if (theQuantifier == TypeConstants::QUANT_PLUS)
+      targetType += "+";
+    else if (theQuantifier == TypeConstants::QUANT_STAR)
+      targetType += "*";
+    else if (theQuantifier == TypeConstants::QUANT_QUESTION)
+      targetType += "?";
+  }
+
+  switch (theErrorKind)
+  {
+  case FUNC_RETURN:
+  {
+    assert(theQName != NULL);
+
+    RAISE_ERROR(err::XPTY0004, loc, 
+    ERROR_PARAMS(ZED(XPTY0004_FuncReturn),
+                 valueType, targetType, theQName->getStringValue()));
+    break;
+  }
+  case FUNC_PARAM:
+  {
+    assert(theQName != NULL);
+
+    RAISE_ERROR(err::XPTY0004, loc, 
+    ERROR_PARAMS(ZED(XPTY0004_FuncParam),
+                 valueType, targetType, theQName->getStringValue()));
+    break;
+  }
+  case TYPE_PROMOTION:
+  {
+    RAISE_ERROR(err::XPTY0004, loc, 
+    ERROR_PARAMS(ZED(XPTY0004_TypePromotion), valueType, targetType));
+    break;
+  }
+  default:
+  {
+    ZORBA_ASSERT(false);
+  }
+  }
+}
+
+
 UNARY_ACCEPT(PromoteIterator);
 
 
@@ -466,19 +477,19 @@
 TreatIterator::TreatIterator(
     static_context* sctx,
     const QueryLoc& loc,
-    PlanIter_t& aChild,
-    const xqtref_t& aTreatType,
+    PlanIter_t& child,
+    const xqtref_t& treatType,
     bool checkPrime,
-    const Error& aErrorCode,
-    store::Item_t fnQName)
+    ErrorKind errorKind,
+    store::Item_t qname)
   :
-  UnaryBaseIterator<TreatIterator, PlanIteratorState>(sctx, loc, aChild),
+  UnaryBaseIterator<TreatIterator, PlanIteratorState>(sctx, loc, child),
   theCheckPrime(checkPrime),
-  theErrorCode(&aErrorCode),
-  theFnQName(fnQName)
+  theErrorKind(errorKind),
+  theQName(qname)
 {
-  theTreatType = TypeOps::prime_type(sctx->get_typemanager(), *aTreatType);
-  theQuantifier = TypeOps::quantifier(*aTreatType);
+  theTreatType = TypeOps::prime_type(sctx->get_typemanager(), *treatType);
+  theQuantifier = TypeOps::quantifier(*treatType);
 }
 
 
@@ -490,8 +501,8 @@
   ar & theTreatType;
   SERIALIZE_ENUM(TypeConstants::quantifier_t, theQuantifier);
   ar & theCheckPrime;
-  ar & theErrorCode;
-  ar & theFnQName;
+  SERIALIZE_ENUM(ErrorKind, theErrorKind);
+  ar & theQName;
 }
 
 
@@ -509,22 +520,7 @@
     if (theQuantifier == TypeConstants::QUANT_PLUS || 
         theQuantifier == TypeConstants::QUANT_ONE)
     {
-      zstring type = theTreatType->toSchemaString() + 
-                     (theQuantifier == TypeConstants::QUANT_PLUS? "+" : "");
-
-      if (theFnQName.getp() != NULL)
-      {
-        ZORBA_ERROR_VAR_LOC_OSS(*theErrorCode, loc, 
-                                "An empty sequence is not allowed as the result "
-                                << "of the function " 
-                                << theFnQName->getStringValue() 
-                                << "() which returns " << type << ".");
-      }
-      else
-      {
-        ZORBA_ERROR_VAR_LOC_OSS(*theErrorCode, loc, 
-                                "Cannot treat empty sequence as " << type << ".");
-      }
+      raiseError("empty-sequence()");
     }
   }
   else if (theQuantifier == TypeConstants::QUANT_QUESTION ||
@@ -532,23 +528,7 @@
   {
     if (consumeNext(temp, theChild.getp(), planState))
     {
-      zstring type = theTreatType->toSchemaString() + 
-                     (theQuantifier == TypeConstants::QUANT_PLUS? "?" : "");
-
-      if (theFnQName.getp() != NULL)
-      {
-        ZORBA_ERROR_VAR_LOC_OSS(*theErrorCode, loc, 
-                                "A sequence with more than one item cannot be "
-                                << "the result of the function " 
-                                << theFnQName->getStringValue() 
-                                << "() which returns " << type << ".");
-      }
-      else
-      {
-        ZORBA_ERROR_VAR_LOC_OSS(*theErrorCode, loc, 
-                                "Cannot treat sequence with more than one item as "
-                                << type << ".");
-      }
+      raiseError("sequence of more than one item");
     }
 
     if (theCheckPrime)
@@ -570,23 +550,8 @@
       
       if (!res)
       {
-        xqtref_t valueType = tm->create_value_type(result);
-
-        if (theFnQName.getp() != NULL)
-        {
-          ZORBA_ERROR_VAR_LOC_OSS(*theErrorCode, loc,
-                                  "Cannot treat " << valueType->toSchemaString() 
-                                  << " as " << theTreatType->toSchemaString()
-                                  << " when returning the result of the function "
-                                  << theFnQName->getStringValue() << "().");
-        }
-        else
-        {
-          ZORBA_ERROR_VAR_LOC_OSS(*theErrorCode, loc, 
-                                  "Cannot treat " << valueType->toSchemaString() 
-                                  << " as " << theTreatType->toSchemaString()
-                                  << ".");
-        }
+        zstring valueType = tm->create_value_type(result)->toSchemaString();
+        raiseError(valueType);
       }
     }
 
@@ -615,22 +580,8 @@
         
         if (!res)
         {
-          xqtref_t valueType = tm->create_value_type(result);
-
-          if (theFnQName.getp() != NULL)
-          {
-            ZORBA_ERROR_VAR_LOC_OSS(*theErrorCode, loc,
-                                    "Cannot treat " + valueType->toSchemaString() 
-                                    << " as " << theTreatType->toSchemaString()
-                                    << " when returning the result of the function "
-                                    << theFnQName->getStringValue() << "().");
-          }
-          else
-          {
-            ZORBA_ERROR_VAR_LOC_OSS(*theErrorCode, loc,
-                                    "Cannot treat " << valueType->toSchemaString() 
-                                    << " as " << theTreatType->toSchemaString());
-          }
+          zstring valueType = tm->create_value_type(result)->toSchemaString();
+          raiseError(valueType);
         }
       }
 
@@ -643,6 +594,88 @@
 }
 
 
+void TreatIterator::raiseError(const zstring& valueType) const
+{
+  zstring targetType;
+
+  if (theTreatType->type_kind() == XQType::NONE_KIND && 
+      theQuantifier == TypeConstants::QUANT_QUESTION)
+  {
+    targetType = "empty-sequence()";
+  }
+  else
+  {
+    targetType = theTreatType->toSchemaString();
+
+    if (theQuantifier == TypeConstants::QUANT_PLUS)
+      targetType += "+";
+    else if (theQuantifier == TypeConstants::QUANT_STAR)
+      targetType += "*";
+    else if (theQuantifier == TypeConstants::QUANT_QUESTION)
+      targetType += "?";
+  }
+
+  switch (theErrorKind)
+  {
+  case FUNC_RETURN:
+  {
+    assert(theQName != NULL);
+
+    RAISE_ERROR(err::XPTY0004, loc, 
+    ERROR_PARAMS(ZED(XPTY0004_FuncReturn),
+                 valueType, targetType, theQName->getStringValue()));
+    break;
+  }
+  case FUNC_PARAM:
+  {
+    assert(theQName != NULL);
+
+    RAISE_ERROR(err::XPTY0004, loc, 
+    ERROR_PARAMS(ZED(XPTY0004_FuncParam),
+                 valueType, targetType, theQName->getStringValue()));
+    break;
+  }
+  case TYPE_MATCH:
+  {
+    RAISE_ERROR(err::XPTY0004, loc, 
+    ERROR_PARAMS(ZED(XPTY0004_TypeMatch), valueType, targetType));
+    break;
+  }
+  case TREAT_EXPR:
+  {
+    RAISE_ERROR(err::XPDY0050, loc, ERROR_PARAMS(valueType, targetType));
+    break;
+  }
+  case INDEX_DOMAIN:
+  {
+    RAISE_ERROR(zerr::ZDTY0010_INDEX_DOMAIN_TYPE_ERROR, loc,
+    ERROR_PARAMS(theQName->getStringValue()));
+    break;
+  }
+  case INDEX_KEY:
+  {
+    RAISE_ERROR(zerr::ZDTY0011_INDEX_KEY_TYPE_ERROR, loc,
+    ERROR_PARAMS(valueType, targetType, theQName->getStringValue()));
+    break;
+  }
+  case PATH_STEP:
+  {
+    RAISE_ERROR_NO_PARAMS(err::XPTY0019, loc);
+    break;
+  }
+  case PATH_DOT:
+  {
+    RAISE_ERROR_NO_PARAMS(err::XPTY0020, loc);
+    break;
+  }
+  default:
+  {
+    ZORBA_ASSERT(false);
+  }
+  }
+}
+
+
 UNARY_ACCEPT(TreatIterator);
 
 

=== modified file 'src/runtime/core/sequencetypes.h'
--- src/runtime/core/sequencetypes.h	2012-05-03 12:31:51 +0000
+++ src/runtime/core/sequencetypes.h	2012-05-29 14:32:32 +0000
@@ -133,7 +133,7 @@
   CastableIterator,
   UnaryBaseIterator<CastableIterator, PlanIteratorState>);
 
-  void serialize(::zorba::serialization::Archiver &ar)
+  void serialize(::zorba::serialization::Archiver& ar)
   {
     serialize_baseclass(ar,
     (UnaryBaseIterator<CastableIterator, PlanIteratorState>*)this);
@@ -167,11 +167,19 @@
 {
   friend class PrinterVisitor;
 
+public:
+  typedef enum
+  {
+    FUNC_RETURN,
+    FUNC_PARAM,
+    TYPE_PROMOTION
+  } ErrorKind;
+
 private:
   xqtref_t                    thePromoteType;
   TypeConstants::quantifier_t theQuantifier;
-  store::Item_t								theFnQName;      // Stores the QName of the function, if the promote expr
-                                               // is used to cast the function's body to its result type
+  ErrorKind                   theErrorKind;
+  store::Item_t								theQName; 
 
 public:
   SERIALIZABLE_CLASS(PromoteIterator);
@@ -180,29 +188,25 @@
   PromoteIterator,
   UnaryBaseIterator<PromoteIterator, PlanIteratorState>);
 
-  void serialize(::zorba::serialization::Archiver &ar)
-  {
-    serialize_baseclass(ar,
-    (UnaryBaseIterator<PromoteIterator, PlanIteratorState>*)this);
-
-    ar & thePromoteType;
-    SERIALIZE_ENUM(TypeConstants::quantifier_t, theQuantifier);
-    ar & theFnQName;
-  }
+  void serialize(::zorba::serialization::Archiver& ar);
 
 public:
   PromoteIterator(
         static_context* sctx,
-        const QueryLoc&,
-        PlanIter_t&,
-        const xqtref_t& aPromoteType,
-        store::Item_t fnQName = NULL);
+        const QueryLoc& loc,
+        PlanIter_t& child,
+        const xqtref_t& promoteType,
+        ErrorKind err,
+        store::Item_t qname = NULL);
 
   ~PromoteIterator();
 
   void accept(PlanIterVisitor& v) const;
 
   bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
+
+protected:
+  void raiseError(const zstring& valueType) const;
 };
 
 
@@ -221,12 +225,25 @@
 {
   friend class PrinterVisitor;
 
+public:
+  typedef enum
+  {
+    FUNC_RETURN,
+    FUNC_PARAM,
+    TYPE_MATCH,
+    TREAT_EXPR,
+    INDEX_DOMAIN,
+    INDEX_KEY,
+    PATH_STEP,
+    PATH_DOT,
+  } ErrorKind;
+
 private:
   xqtref_t                    theTreatType;
   TypeConstants::quantifier_t theQuantifier;
   bool                        theCheckPrime;
-  const Error               * theErrorCode;
-  store::Item_t								theFnQName;
+  ErrorKind                   theErrorKind;
+  store::Item_t								theQName;
 
 public:
   SERIALIZABLE_CLASS(TreatIterator);
@@ -240,16 +257,19 @@
 public:
   TreatIterator(
         static_context* sctx,
-        const QueryLoc&,
-        PlanIter_t& aChild,
-        const xqtref_t& aTreatType,
+        const QueryLoc& loc,
+        PlanIter_t& child,
+        const xqtref_t& treatType,
         bool check_prime,
-        Error const&,
-        store::Item_t fnQName = NULL);
+        ErrorKind errorKind,
+        store::Item_t qname);
 
   void accept(PlanIterVisitor& v) const;
 
   bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
+
+protected:
+  void raiseError(const zstring& valueType) const;
 };
 
 

=== modified file 'src/runtime/function_item/dynamic_fncall_iterator.cpp'
--- src/runtime/function_item/dynamic_fncall_iterator.cpp	2012-05-03 12:31:51 +0000
+++ src/runtime/function_item/dynamic_fncall_iterator.cpp	2012-05-29 14:32:32 +0000
@@ -157,13 +157,16 @@
   if (!consumeNext(funcItem, theChildren[0], planState))
   {
     RAISE_ERROR(err::XPTY0004, loc, 
-    ERROR_PARAMS(ZED(EmptySeqNoPromoteTo),
+    ERROR_PARAMS(ZED(XPTY0004_TypePromotion),
+                 "empty-sequence()",
                  GENV_TYPESYSTEM.ANY_FUNCTION_TYPE_ONE->toSchemaString()));
   }
 
   if (consumeNext(item, theChildren[0], planState))
   {
-    RAISE_ERROR(err::XPTY0004, loc, ERROR_PARAMS(ZED(NoSeqTypePromotion)));
+    RAISE_ERROR(err::XPTY0004, loc, 
+    ERROR_PARAMS(ZED(XPTY0004_NoMultiSeqTypePromotion),
+                 GENV_TYPESYSTEM.ANY_FUNCTION_TYPE_ONE->toSchemaString()));
   }
 
   if (!funcItem->isFunction())
@@ -172,7 +175,7 @@
     xqtref_t type = tm->create_value_type(funcItem);
 
     RAISE_ERROR(err::XPTY0004, loc, 
-    ERROR_PARAMS(ZED(NoTypePromotion_23),
+    ERROR_PARAMS(ZED(XPTY0004_TypePromotion),
                  type->toSchemaString(),
                  GENV_TYPESYSTEM.ANY_FUNCTION_TYPE_ONE->toSchemaString()));
   }

=== modified file 'src/store/naive/store.cpp'
--- src/store/naive/store.cpp	2012-05-05 03:17:35 +0000
+++ src/store/naive/store.cpp	2012-05-29 14:32:32 +0000
@@ -550,6 +550,7 @@
         ERROR_PARAMS(index->getName()->getStringValue()));
       }
 
+      // If the index took ownership of the last allocated key, allocate a new one.
       if (key2 == key)
         key = new store::IndexKey(numColumns);
 

=== modified file 'src/types/typeimpl.cpp'
--- src/types/typeimpl.cpp	2012-05-03 12:31:51 +0000
+++ src/types/typeimpl.cpp	2012-05-29 14:32:32 +0000
@@ -222,32 +222,50 @@
   switch (type_kind())
   {
   case EMPTY_KIND:
+  {
     result = "empty-sequence()";
     break;
+  }
   case ATOMIC_TYPE_KIND:
+  {
     result = toString();
     break;
+  }
   case ITEM_KIND:
+  {
     result = "item()";
     break;
+  }
   case NODE_TYPE_KIND:
-    result = toString();
+  {
+    result = static_cast<const NodeXQType*>(this)->toSchemaStringInternal();
     break;
+  }
   case FUNCTION_TYPE_KIND:
+  {
     result = toString();
     break;
+  }
   case ANY_TYPE_KIND:
+  {
     result = "xs:anyType";
     break;
+  }
   case ANY_SIMPLE_TYPE_KIND:
+  {
     result = "xs:anySimpleType";
     break;
+  }
   case ANY_FUNCTION_TYPE_KIND:
+  {
     result = "function(*)";
     break;
+  }
   case UNTYPED_KIND:
+  {
     result = toString();
     break;
+  }
   default:
     return toString();
     break;
@@ -260,22 +278,6 @@
 /*******************************************************************************
 
 ********************************************************************************/
-std::ostream& AtomicXQType::serialize_ostream(std::ostream& os) const
-{
-  return os << ATOMIC_TYPE_CODE_STRINGS[get_type_code()]
-            << TypeOps::decode_quantifier (get_quantifier());
-}
-
-
-store::Item_t AtomicXQType::get_qname() const
-{
-  return GENV_TYPESYSTEM.m_atomic_typecode_qname_map[m_type_code];
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
 void NoneXQType::serialize(::zorba::serialization::Archiver& ar)
 {
   serialize_baseclass(ar, (XQType*)this);
@@ -364,6 +366,19 @@
 }
 
 
+std::ostream& AtomicXQType::serialize_ostream(std::ostream& os) const
+{
+  return os << ATOMIC_TYPE_CODE_STRINGS[get_type_code()]
+            << TypeOps::decode_quantifier(get_quantifier());
+}
+
+
+store::Item_t AtomicXQType::get_qname() const
+{
+  return GENV_TYPESYSTEM.m_atomic_typecode_qname_map[m_type_code];
+}
+
+
 /*******************************************************************************
 
 ********************************************************************************/
@@ -635,6 +650,58 @@
 }
 
 
+std::string NodeXQType::toSchemaStringInternal() const
+{
+  std::ostringstream os;
+
+  if (m_node_kind == store::StoreConsts::documentNode)
+  {
+    os << "document-node(";
+    
+    if (m_content_type != NULL &&
+        m_content_type->type_kind() == XQType::NODE_TYPE_KIND)
+    {
+      os << ", " << m_content_type->toSchemaString();
+    }
+  }
+  else if (m_schema_test)
+  {
+    assert(m_content_type != NULL);
+
+    os << "schema-" << store::StoreConsts::toSchemaString(get_node_kind()) << "("
+       << m_node_name->getStringValue();
+  }
+  else
+  {
+    os << store::StoreConsts::toSchemaString(get_node_kind()) << "(";
+    
+    if (m_node_name != NULL)
+    {
+      os << m_node_name->getStringValue();
+
+      if (m_content_type != NULL)
+      {
+        os << ", " << m_content_type->toSchemaString();
+
+        if (m_nillable)
+          os << "?";
+      }
+    }
+    else if (m_content_type != NULL)
+    {
+      os << "*, " << m_content_type->toSchemaString();
+
+      if (m_nillable)
+        os << "?";
+    }
+  }
+  
+  os << ")";
+  
+  return os.str();
+}
+
+
 /*******************************************************************************
 
 ********************************************************************************/

=== modified file 'src/types/typeimpl.h'
--- src/types/typeimpl.h	2012-05-03 12:31:51 +0000
+++ src/types/typeimpl.h	2012-05-29 14:32:32 +0000
@@ -319,9 +319,9 @@
 
   virtual std::ostream& serialize_ostream(std::ostream& os) const;
 
-  virtual std::string toString() const;
+  std::string toString() const;
 
-  virtual std::string toSchemaString() const;
+  std::string toSchemaString() const;
 
 protected:
   XQType(
@@ -485,6 +485,8 @@
 ********************************************************************************/
 class NodeXQType : public XQType
 {
+  friend class XQType;
+
 private:
   store::StoreConsts::NodeKind  m_node_kind;
   store::Item_t                 m_node_name;
@@ -539,6 +541,9 @@
       const QueryLoc& loc) const;
 
   virtual std::ostream& serialize_ostream(std::ostream& os) const;
+
+protected:
+  std::string toSchemaStringInternal() const;
 };
 
 


Follow ups