warning elimination reported by clang
[deliverable/titan.core.git] / core2 / Basetype2.cc
index 189fa743f54e78132097a53377bd4c384c638389..1ddac029f5783313b5f287a4906a5fc9638c986f 100644 (file)
@@ -1,10 +1,23 @@
-///////////////////////////////////////////////////////////////////////////////
-// Copyright (c) 2000-2014 Ericsson Telecom AB
-// All rights reserved. This program and the accompanying materials
-// are made available under the terms of the Eclipse Public License v1.0
-// which accompanies this distribution, and is available at
-// http://www.eclipse.org/legal/epl-v10.html
-///////////////////////////////////////////////////////////////////////////////
+/******************************************************************************
+ * Copyright (c) 2000-2016 Ericsson Telecom AB
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Baji, Laszlo
+ *   Balasko, Jeno
+ *   Baranyi, Botond
+ *   Beres, Szabolcs
+ *   Delic, Adam
+ *   Kovacs, Ferenc
+ *   Raduly, Csaba
+ *   Szabados, Kristof
+ *   Szabo, Bence Janos
+ *   Pandi, Krisztian
+ *
+ ******************************************************************************/
 #include "Basetype.hh"
 #include "TEXT.hh"
 #include "BER.hh"
@@ -21,6 +34,7 @@
 #include "Charstring.hh"
 #include "Universal_charstring.hh"
 #include "Addfunc.hh"
+#include "PreGenRecordOf.hh"
 
 ////////////////////////////////////////////////////////////////////////////////
 
@@ -175,6 +189,13 @@ int Base_Type::RAW_encode_negtest_raw(RAW_enc_tree&) const
   return 0;
 }
 
+int Base_Type::JSON_encode_negtest_raw(JSON_Tokenizer&) const
+{
+  TTCN_error("A value of type %s cannot be used as erroneous raw value for JSON encoding.",
+             get_descriptor()->name);
+  return 0;
+}
+
 int Base_Type::XER_encode_negtest(const Erroneous_descriptor_t* /*p_err_descr*/,
   const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t*) const
 {
@@ -188,6 +209,13 @@ int Base_Type::RAW_encode_negtest(const Erroneous_descriptor_t *,
   return 0;
 }
 
+int Base_Type::JSON_encode_negtest(const Erroneous_descriptor_t* /*p_err_descr*/,
+  const TTCN_Typedescriptor_t& /*p_td*/, JSON_Tokenizer& /*p_tok*/) const
+{
+  TTCN_error("Internal error: calling Base_Type::JSON_encode_negtest().");
+  return 0;
+}
+
 #else
 #error this is for RT2 only
 #endif
@@ -255,7 +283,8 @@ Record_Of_Type::Record_Of_Type(null_type /*other_value*/)
 }
 
 Record_Of_Type::Record_Of_Type(const Record_Of_Type& other_value)
-: Base_Type(other_value), val_ptr(NULL), err_descr(other_value.err_descr), refd_ind_ptr(NULL)
+: Base_Type(other_value), RefdIndexInterface(other_value)
+, val_ptr(NULL), err_descr(other_value.err_descr), refd_ind_ptr(NULL)
 {
   if (!other_value.is_bound())
     TTCN_error("Copying an unbound record of/set of value.");
@@ -1421,6 +1450,10 @@ int Record_Of_Type::RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr
 
 int Record_Of_Type::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const
 {
+  if (err_descr) {
+         return JSON_encode_negtest(err_descr, p_td, p_tok);
+  }
+  
   if (!is_bound()) {
     TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
       "Encoding an unbound %s of value.", is_set() ? "set" : "record");
@@ -1429,10 +1462,109 @@ int Record_Of_Type::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenize
   
   int enc_len = p_tok.put_next_token(JSON_TOKEN_ARRAY_START, NULL);
   
-  for(int i = 0; i < get_nof_elements(); ++i) {
-    int ret_val = get_at(i)->JSON_encode(*p_td.oftype_descr, p_tok);
-    if (0 > ret_val) break;
-    enc_len += ret_val;
+  for (int i = 0; i < get_nof_elements(); ++i) {
+    if (NULL != p_td.json && p_td.json->metainfo_unbound && !get_at(i)->is_bound()) {
+      // unbound elements are encoded as { "metainfo []" : "unbound" }
+      enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_START, NULL);
+      enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, "metainfo []");
+      enc_len += p_tok.put_next_token(JSON_TOKEN_STRING, "\"unbound\"");
+      enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_END, NULL);
+    }
+    else {
+      int ret_val = get_at(i)->JSON_encode(*p_td.oftype_descr, p_tok);
+      if (0 > ret_val) break;
+      enc_len += ret_val;
+    }
+  }
+  
+  enc_len += p_tok.put_next_token(JSON_TOKEN_ARRAY_END, NULL);
+  return enc_len;
+}
+
+int Record_Of_Type::JSON_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
+                                        const TTCN_Typedescriptor_t& p_td,
+                                        JSON_Tokenizer& p_tok) const 
+{
+  if (!is_bound()) {
+    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
+      "Encoding an unbound %s of value.", is_set() ? "set" : "record");
+    return -1;
+  }
+  
+  int enc_len = p_tok.put_next_token(JSON_TOKEN_ARRAY_START, NULL);
+  
+  int values_idx = 0;
+  int edescr_idx = 0;
+  
+  for (int i = 0; i < get_nof_elements(); ++i) {
+    if (-1 != p_err_descr->omit_before && p_err_descr->omit_before > i) {
+      continue;
+    }
+    
+    const Erroneous_values_t* err_vals = p_err_descr->next_field_err_values(i, values_idx);
+    const Erroneous_descriptor_t* emb_descr = p_err_descr->next_field_emb_descr(i, edescr_idx);
+    
+    if (NULL != err_vals && NULL != err_vals->before) {
+      if (NULL == err_vals->before->errval) {
+        TTCN_error("internal error: erroneous before value missing");
+      }
+      if (err_vals->before->raw) {
+        enc_len += err_vals->before->errval->JSON_encode_negtest_raw(p_tok);
+      } else {
+        if (NULL == err_vals->before->type_descr) {
+          TTCN_error("internal error: erroneous before typedescriptor missing");
+        }
+        enc_len += err_vals->before->errval->JSON_encode(*(err_vals->before->type_descr), p_tok);
+      }
+    }
+    
+    if (NULL != err_vals && NULL != err_vals->value) {
+      if (NULL != err_vals->value->errval) {
+        if (err_vals->value->raw) {
+          enc_len += err_vals->value->errval->JSON_encode_negtest_raw(p_tok);
+        } else {
+          if (NULL == err_vals->value->type_descr) {
+            TTCN_error("internal error: erroneous before typedescriptor missing");
+          }
+          enc_len += err_vals->value->errval->JSON_encode(*(err_vals->value->type_descr), p_tok);
+        }
+      }
+    }
+    else if (NULL != p_td.json && p_td.json->metainfo_unbound && !get_at(i)->is_bound()) {
+      // unbound elements are encoded as { "metainfo []" : "unbound" }
+      enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_START, NULL);
+      enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, "metainfo []");
+      enc_len += p_tok.put_next_token(JSON_TOKEN_STRING, "\"unbound\"");
+      enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_END, NULL);
+    }
+    else {
+      int ret_val;
+      if (NULL != emb_descr) {
+        ret_val = get_at(i)->JSON_encode_negtest(emb_descr, *p_td.oftype_descr, p_tok);
+      } else {
+        ret_val = get_at(i)->JSON_encode(*p_td.oftype_descr, p_tok);
+      }
+      if (0 > ret_val) break;
+      enc_len += ret_val;
+    }
+    
+    if (NULL != err_vals && NULL != err_vals->after) {
+      if (NULL == err_vals->after->errval) {
+        TTCN_error("internal error: erroneous after value missing");
+      }
+      if (err_vals->after->raw) {
+        enc_len += err_vals->after->errval->JSON_encode_negtest_raw(p_tok);
+      } else {
+        if (NULL == err_vals->after->type_descr) {
+          TTCN_error("internal error: erroneous before typedescriptor missing");
+        }
+        enc_len += err_vals->after->errval->JSON_encode(*(err_vals->after->type_descr), p_tok);
+      }
+    }
+    
+    if (-1 != p_err_descr->omit_after && p_err_descr->omit_after <= i) {
+      break;
+    }
   }
   
   enc_len += p_tok.put_next_token(JSON_TOKEN_ARRAY_END, NULL);
@@ -1452,11 +1584,35 @@ int Record_Of_Type::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenize
   } 
   
   set_size(0);
-  while (true) {
+  for (int nof_elements = 0; true; ++nof_elements) {
     // Read value tokens until we reach some other token
     size_t buf_pos = p_tok.get_buf_pos();
+    int ret_val;
+    if (NULL != p_td.json && p_td.json->metainfo_unbound) {
+      // check for metainfo object
+      ret_val = p_tok.get_next_token(&token, NULL, NULL);
+      if (JSON_TOKEN_OBJECT_START == token) {
+        char* value = NULL;
+        size_t value_len = 0;
+        ret_val += p_tok.get_next_token(&token, &value, &value_len);
+        if (JSON_TOKEN_NAME == token && 11 == value_len &&
+            0 == strncmp(value, "metainfo []", 11)) {
+          ret_val += p_tok.get_next_token(&token, &value, &value_len);
+          if (JSON_TOKEN_STRING == token && 9 == value_len &&
+              0 == strncmp(value, "\"unbound\"", 9)) {
+            ret_val = p_tok.get_next_token(&token, NULL, NULL);
+            if (JSON_TOKEN_OBJECT_END == token) {
+              dec_len += ret_val;
+              continue;
+            }
+          }
+        }
+      }
+      // metainfo object not found, jump back and let the element type decode it
+      p_tok.set_buf_pos(buf_pos);
+    }
     Base_Type* val = create_elem();
-    int ret_val = val->JSON_decode(*p_td.oftype_descr, p_tok, p_silent);
+    ret_val = val->JSON_decode(*p_td.oftype_descr, p_tok, p_silent);
     if (JSON_ERROR_INVALID_TOKEN == ret_val) {
       // undo the last action on the buffer
       p_tok.set_buf_pos(buf_pos);
@@ -1472,12 +1628,12 @@ int Record_Of_Type::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenize
     }
     if (NULL == refd_ind_ptr) {
       val_ptr->value_elements = (Base_Type**)reallocate_pointers(
-        (void**)val_ptr->value_elements, val_ptr->n_elements, val_ptr->n_elements + 1);
-      val_ptr->value_elements[val_ptr->n_elements] = val;
-      val_ptr->n_elements++;
+        (void**)val_ptr->value_elements, val_ptr->n_elements, nof_elements + 1);
+      val_ptr->value_elements[nof_elements] = val;
+      val_ptr->n_elements = nof_elements + 1;
     }
     else {
-      get_at(get_nof_elements())->set_value(val);
+      get_at(nof_elements)->set_value(val);
       delete val;
     }
     dec_len += ret_val;
@@ -1599,7 +1755,7 @@ void Record_Of_Type::decode(const TTCN_Typedescriptor_t& p_td,
     for (int success=reader.Read(); success==1; success=reader.Read()) {
       if (reader.NodeType() == XML_READER_TYPE_ELEMENT) break;
     }
-    XER_decode(*(p_td.xer), reader, XER_coding | XER_TOPLEVEL, 0);
+    XER_decode(*(p_td.xer), reader, XER_coding | XER_TOPLEVEL, XER_NONE, 0);
     size_t bytes = reader.ByteConsumed();
     p_buf.set_pos(bytes);
     break;}
@@ -2097,7 +2253,7 @@ int Record_Of_Type::XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr
 }
 
 int Record_Of_Type::XER_decode(const XERdescriptor_t& p_td,
-  XmlReaderWrap& reader, unsigned int flavor, embed_values_dec_struct_t* emb_val)
+  XmlReaderWrap& reader, unsigned int flavor, unsigned int flavor2, embed_values_dec_struct_t* emb_val)
 {
   int exer = is_exer(flavor);
   int xerbits = p_td.xer_bits;
@@ -2176,7 +2332,7 @@ int Record_Of_Type::XER_decode(const XERdescriptor_t& p_td,
       // The call to the non-const operator[], I mean get_at(), creates
       // a new element (because it is indexing one past the last element).
       // Then we call its XER_decode with the temporary XML reader.
-      get_at(get_nof_elements())->XER_decode(sub_xer, reader2, flavor, 0);
+      get_at(get_nof_elements())->XER_decode(sub_xer, reader2, flavor, flavor2, 0);
       if (flavor & EXIT_ON_ERROR && !is_elem_bound(get_nof_elements() - 1)) {
         if (1 == get_nof_elements()) {
           // Failed to decode even the first element
@@ -2242,10 +2398,10 @@ int Record_Of_Type::XER_decode(const XERdescriptor_t& p_td,
             }
             ec_1.set_msg("%d: ", get_nof_elements());
             /* The call to the non-const get_at() creates the element */
-            get_at(get_nof_elements())->XER_decode(*p_td.oftype_descr, reader, flavor, emb_val);
-            if (0 != emb_val && !own_tag && get_nof_elements() > 1) {
-              ++emb_val->embval_index;
-            }
+            get_at(get_nof_elements())->XER_decode(*p_td.oftype_descr, reader, flavor, flavor2, emb_val);
+          }
+          if (0 != emb_val && !own_tag && get_nof_elements() > 1) {
+            ++emb_val->embval_index;
           }
         }
         else if (XML_READER_TYPE_END_ELEMENT == type) {
@@ -2269,6 +2425,10 @@ int Record_Of_Type::XER_decode(const XERdescriptor_t& p_td,
       } /* next read */
     } /* if not empty element */
   } /* if not LIST */
+  if (!own_tag && exer && (p_td.xer_bits & XER_OPTIONAL) && get_nof_elements() == 0) {
+    // set it to unbound, so the OPTIONAL class sets it to omit
+    clean_up();
+  }
   return 1; // decode successful
 }
 
@@ -2289,25 +2449,31 @@ void Record_Of_Type::set_param(Module_Param& param) {
   }
   
   param.basic_check(Module_Param::BC_VALUE|Module_Param::BC_LIST, is_set()?"set of value":"record of value");
+  
+  Module_Param_Ptr mp = &param;
+  if (param.get_type() == Module_Param::MP_Reference) {
+    mp = param.get_referenced_param();
+  }
+  
   switch (param.get_operation_type()) {
   case Module_Param::OT_ASSIGN:
-    if (param.get_type()==Module_Param::MP_Value_List && param.get_size()==0) {
+    if (mp->get_type()==Module_Param::MP_Value_List && mp->get_size()==0) {
       set_val(NULL_VALUE);
       return;
     }
-    switch (param.get_type()) {
+    switch (mp->get_type()) {
     case Module_Param::MP_Value_List:
-      set_size(param.get_size());
-      for (size_t i=0; i<param.get_size(); ++i) {
-        Module_Param* const curr = param.get_elem(i);
+      set_size(mp->get_size());
+      for (size_t i=0; i<mp->get_size(); ++i) {
+        Module_Param* const curr = mp->get_elem(i);
         if (curr->get_type()!=Module_Param::MP_NotUsed) {
           get_at(i)->set_param(*curr);
         }
       }
       break;
     case Module_Param::MP_Indexed_List:
-      for (size_t i=0; i<param.get_size(); ++i) {
-        Module_Param* const current = param.get_elem(i);
+      for (size_t i=0; i<mp->get_size(); ++i) {
+        Module_Param* const current = mp->get_elem(i);
         get_at(current->get_id()->get_index())->set_param(*current);
       }
       break;
@@ -2316,12 +2482,12 @@ void Record_Of_Type::set_param(Module_Param& param) {
     }
     break;
   case Module_Param::OT_CONCAT:
-    switch (param.get_type()) {
+    switch (mp->get_type()) {
     case Module_Param::MP_Value_List: {
       if (!is_bound()) set_val(NULL_VALUE);
       int start_idx = lengthof();
-      for (size_t i=0; i<param.get_size(); ++i) {
-        Module_Param* const curr = param.get_elem(i);
+      for (size_t i=0; i<mp->get_size(); ++i) {
+        Module_Param* const curr = mp->get_elem(i);
         if ((curr->get_type()!=Module_Param::MP_NotUsed)) {
           get_at(start_idx+(int)i)->set_param(*curr);
         }
@@ -2339,6 +2505,34 @@ void Record_Of_Type::set_param(Module_Param& param) {
   }
 }
 
+Module_Param* Record_Of_Type::get_param(Module_Param_Name& param_name) const
+{
+  if (!is_bound()) {
+    return new Module_Param_Unbound();
+  }
+  if (param_name.next_name()) {
+    // Haven't reached the end of the module parameter name
+    // => the name refers to one of the elements, not to the whole record of
+    char* param_field = param_name.get_current_name();
+    if (param_field[0] < '0' || param_field[0] > '9') {
+      TTCN_error("Unexpected record field name in module parameter reference, "
+        "expected a valid index for %s type `%s'",
+        is_set() ? "set of" : "record of", get_descriptor()->name);
+    }
+    int param_index = -1;
+    sscanf(param_field, "%d", &param_index);
+    return get_at(param_index)->get_param(param_name);
+  }
+  Vector<Module_Param*> values;
+  for (int i = 0; i < val_ptr->n_elements; ++i) {
+    values.push_back(val_ptr->value_elements[i]->get_param(param_name));
+  }
+  Module_Param_Value_List* mp = new Module_Param_Value_List();
+  mp->add_list_with_implicit_ids(&values);
+  values.clear();
+  return mp;
+}
+
 void Record_Of_Type::set_implicit_omit()
 {
   for (int i = 0; i < get_nof_elements(); ++i) {
@@ -2394,7 +2588,6 @@ boolean operator!=(null_type null_value,
 
 boolean Record_Type::is_bound() const
 {
-  if (bound_flag) return TRUE;
   int field_cnt = get_count();
   for (int field_idx=0; field_idx<field_cnt; field_idx++) {
     const Base_Type* temp = get_at(field_idx);
@@ -2408,9 +2601,6 @@ boolean Record_Type::is_bound() const
 
 boolean Record_Type::is_value() const
 {
-  if (!is_bound()) {
-    return FALSE;
-  }
   int field_cnt = get_count();
   for (int field_idx=0; field_idx<field_cnt; field_idx++) {
     const Base_Type* temp = get_at(field_idx);
@@ -2430,7 +2620,6 @@ void Record_Type::clean_up()
   for (int field_idx=0; field_idx<field_cnt; field_idx++) {
     get_at(field_idx)->clean_up();
   }
-  bound_flag = FALSE;
 }
 
 void Record_Type::log() const
@@ -2452,7 +2641,6 @@ void Record_Type::log() const
 }
 
 void Record_Type::set_param(Module_Param& param) {
-  bound_flag = TRUE;
   if (dynamic_cast<Module_Param_Name*>(param.get_id()) != NULL &&
       param.get_id()->next_name()) {
     // Haven't reached the end of the module parameter name
@@ -2474,21 +2662,27 @@ void Record_Type::set_param(Module_Param& param) {
   }
 
   param.basic_check(Module_Param::BC_VALUE, is_set()?"set value":"record value");
-  switch (param.get_type()) {
+  
+  Module_Param_Ptr mp = &param;
+  if (param.get_type() == Module_Param::MP_Reference) {
+    mp = param.get_referenced_param();
+  }
+  
+  switch (mp->get_type()) {
   case Module_Param::MP_Value_List:
-    if (get_count()<(int)param.get_size()) {
-      param.error("%s value of type %s has %d fields but list value has %d fields", is_set()?"Set":"Record", get_descriptor()->name, get_count(), (int)param.get_size());
+    if (get_count()<(int)mp->get_size()) {
+      param.error("%s value of type %s has %d fields but list value has %d fields", is_set()?"Set":"Record", get_descriptor()->name, get_count(), (int)mp->get_size());
     }
-    for (size_t i=0; i<param.get_size(); i++) {
-      Module_Param* mp = param.get_elem(i);
-      if (mp->get_type()!=Module_Param::MP_NotUsed) {
-        get_at((int)i)->set_param(*mp);
+    for (size_t i=0; i<mp->get_size(); i++) {
+      Module_Param* mp_elem = mp->get_elem(i);
+      if (mp_elem->get_type()!=Module_Param::MP_NotUsed) {
+        get_at((int)i)->set_param(*mp_elem);
       }
     }
     break;
   case Module_Param::MP_Assignment_List:
-    for (size_t i=0; i<param.get_size(); ++i) {
-      Module_Param* const current = param.get_elem(i);
+    for (size_t i=0; i<mp->get_size(); ++i) {
+      Module_Param* const current = mp->get_elem(i);
       bool found = false;
       for (int j=0; j<get_count(); ++j) {
         if (!strcmp(fld_name(j), current->get_id()->get_name())) {
@@ -2509,6 +2703,38 @@ void Record_Type::set_param(Module_Param& param) {
   }
 }
 
+Module_Param* Record_Type::get_param(Module_Param_Name& param_name) const
+{
+  if (!is_bound()) {
+    return new Module_Param_Unbound();
+  }
+  if (param_name.next_name()) {
+    // Haven't reached the end of the module parameter name
+    // => the name refers to one of the fields, not to the whole record
+    char* param_field = param_name.get_current_name();
+    if (param_field[0] >= '0' && param_field[0] <= '9') {
+      TTCN_error("Unexpected array index in module parameter reference, "
+        "expected a valid field name for %s type `%s'",
+        is_set() ? "set" : "record", get_descriptor()->name);
+    }
+    int field_cnt = get_count();
+    for (int field_idx = 0; field_idx < field_cnt; field_idx++) {
+      if (strcmp(fld_name(field_idx), param_field) == 0) {
+        return get_at(field_idx)->get_param(param_name);
+      }
+    }
+    TTCN_error("Field `%s' not found in %s type `%s'",
+      param_field, is_set() ? "set" : "record", get_descriptor()->name);
+  }
+  Module_Param_Assignment_List* mp = new Module_Param_Assignment_List();
+  for (int i = 0; i < get_count(); ++i) {
+    Module_Param* mp_field = get_at(i)->get_param(param_name);
+    mp_field->set_id(new Module_Param_FieldName(mcopystr(fld_name(i))));
+    mp->add_elem(mp_field);
+  }
+  return mp;
+}
+
 void Record_Type::set_implicit_omit()
 {
   int field_cnt = get_count();
@@ -2525,10 +2751,6 @@ void Record_Type::set_implicit_omit()
 
 int Record_Type::size_of() const
 {
-  if (!is_bound()) {
-    TTCN_error("Calculating the size of an unbound record/set value of type %s",
-      get_descriptor()->name);
-  }
   int opt_count = optional_count();
   if (opt_count==0) return get_count();
   const int* optional_indexes = get_optional_indexes();
@@ -2552,7 +2774,6 @@ void Record_Type::encode_text(Text_Buf& text_buf) const
 
 void Record_Type::decode_text(Text_Buf& text_buf)
 {
-  bound_flag = TRUE;
   int field_cnt = get_count();
   for (int field_idx=0; field_idx<field_cnt; field_idx++)
     get_at(field_idx)->decode_text(text_buf);
@@ -2561,9 +2782,6 @@ void Record_Type::decode_text(Text_Buf& text_buf)
 boolean Record_Type::is_equal(const Base_Type* other_value) const
 {
   const Record_Type* other_record = static_cast<const Record_Type*>(other_value);
-  if (!is_bound() && !other_record->is_bound()) {
-    return TRUE;
-  }
   int field_cnt = get_count();
   for (int field_idx=0; field_idx<field_cnt; field_idx++) {
     const Base_Type* elem = get_at(field_idx);
@@ -2595,7 +2813,6 @@ void Record_Type::set_value(const Base_Type* other_value)
     }
   }
   err_descr = other_record->err_descr;
-  bound_flag = TRUE;
 }
 
 void Record_Type::encode(const TTCN_Typedescriptor_t& p_td,
@@ -2718,7 +2935,7 @@ void Record_Type::decode(const TTCN_Typedescriptor_t& p_td,
     for (int success=reader.Read(); success==1; success=reader.Read()) {
       if (reader.NodeType() == XML_READER_TYPE_ELEMENT) break;
     }
-    XER_decode(*(p_td.xer), reader, XER_coding | XER_TOPLEVEL, 0);
+    XER_decode(*(p_td.xer), reader, XER_coding | XER_TOPLEVEL, XER_NONE, 0);
     size_t bytes = reader.ByteConsumed();
     p_buf.set_pos(bytes);
     break;}
@@ -2876,7 +3093,6 @@ ASN_BER_TLV_t* Record_Type::BER_encode_TLV_negtest(const Erroneous_descriptor_t*
 boolean Record_Type::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
   const ASN_BER_TLV_t& p_tlv, unsigned L_form)
 {
-  bound_flag = TRUE;
   BER_chk_descr(p_td);
   ASN_BER_TLV_t stripped_tlv;
   BER_decode_strip_tags(*p_td.ber, p_tlv, L_form, stripped_tlv);
@@ -2983,7 +3199,6 @@ boolean Record_Type::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
 
 void Record_Type::BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form)
 {
-  bound_flag = TRUE;
   p_typelist.push(this);
   TTCN_EncDec_ErrorContext ec_0("Component '");
   TTCN_EncDec_ErrorContext ec_1;
@@ -3180,7 +3395,6 @@ int Record_Type::RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr,
 int Record_Type::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff,
   int limit, raw_order_t top_bit_ord, boolean no_err, int, boolean)
 {
-  bound_flag = TRUE;
   int field_cnt = get_count();
   int opt_cnt = optional_count();
   int mand_num = field_cnt - opt_cnt; // expected mandatory fields
@@ -3454,7 +3668,6 @@ int Record_Type::TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
 int Record_Type::TEXT_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff,
   Limit_Token_List& limit, boolean no_err, boolean /*first_call*/)
 {
-  bound_flag = TRUE;
   if (is_set()) {
     int decoded_length=0;
     int decoded_field_length=0;
@@ -3919,8 +4132,16 @@ int Record_Type::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
   // start tag, i.e. the ">\n". This is used later to "back up" over it.
   int start_tag_len = 1 + indenting;
   // The EMBED-VALUES member, if applicable
-  const Record_Of_Type* const embed_values = (p_td.xer_bits & EMBED_VALUES)
-  ? static_cast<const Record_Of_Type*>(get_at(0)) : 0;
+  const Record_Of_Type* embed_values = 0;
+  if (p_td.xer_bits & EMBED_VALUES) {
+    embed_values = dynamic_cast<const Record_Of_Type*>(get_at(0));
+    if (NULL == embed_values) {
+      const OPTIONAL<Record_Of_Type>* const embed_opt = static_cast<const OPTIONAL<Record_Of_Type>*>(get_at(0));
+      if(embed_opt->is_present()) {
+        embed_values = &(*embed_opt)();
+      }
+    }
+  }
   // The USE-ORDER member, if applicable
   const Record_Of_Type* const use_order = (p_td.xer_bits & USE_ORDER)
   ? static_cast<const Record_Of_Type*>(get_at(uo_index)) : 0;
@@ -3980,7 +4201,7 @@ int Record_Type::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
       - (!indenting || delay_close || (exer && (p_td.xer_bits & HAS_1UNTAGGED))),
       (cbyte*)p_td.names[exer]);
   }
-  else if (flavor & USE_TYPE_ATTR) {
+  else if (flavor & (USE_NIL|USE_TYPE_ATTR)) {
     // reopen the parent's start tag by overwriting the '>'
     size_t buf_len = p_buf.get_len();
     const unsigned char * const buf_data = p_buf.get_data();
@@ -4017,7 +4238,7 @@ int Record_Type::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
     if (p_td.xer_bits & XER_ATTRIBUTE) p_buf.put_c('\'');
   }
   else { // not USE-QNAME
-    if (!exer && (p_td.xer_bits & EMBED_VALUES)) {
+    if (!exer && (p_td.xer_bits & EMBED_VALUES) && embed_values != NULL) {
       // The EMBED-VALUES member as an ordinary record of string
       sub_len += embed_values->XER_encode(*xer_descr(0), p_buf, flavor, indent+1, 0);
     }
@@ -4085,7 +4306,7 @@ int Record_Type::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
 
     if (exer && (p_td.xer_bits & EMBED_VALUES)) {
       /* write the first string */
-      if (embed_values->size_of() > 0) {
+      if (embed_values != NULL && embed_values->size_of() > 0) {
         sub_len += embed_values->get_at(0)->XER_encode(UNIVERSAL_CHARSTRING_xer_,
           p_buf, flavor | EMBED_VALUES, indent+1, 0);
       }
@@ -4113,9 +4334,8 @@ int Record_Type::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
         ++n_optionals;
       }
 
-      int expected_min = field_cnt - first_nonattr - n_optionals;
       int expected_max = field_cnt - first_nonattr;
-
+      int expected_min = expected_max - n_optionals;
 
       if ((p_td.xer_bits & USE_NIL) && get_at(field_cnt-1)->ispresent()) {
         // The special case when USE_ORDER refers to the fields of a field,
@@ -4169,7 +4389,8 @@ int Record_Type::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
     // early_to_bed can only be true if exer is true (transitive through nil_attribute)
     if (!early_to_bed) {
       embed_values_enc_struct_t* emb_val = 0;
-      if (exer && (p_td.xer_bits & EMBED_VALUES) && embed_values->size_of() > 1) {
+      if (exer && (p_td.xer_bits & EMBED_VALUES) && 
+          embed_values != NULL && embed_values->size_of() > 1) {
         emb_val = new embed_values_enc_struct_t;
         emb_val->embval_array = embed_values;
         emb_val->embval_index = 1;
@@ -4199,7 +4420,7 @@ int Record_Type::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
 
         // Now the next embed-values string (NOT affected by USE-ORDER!)
         if (exer && (p_td.xer_bits & EMBED_VALUES) && 0 != emb_val &&
-            emb_val->embval_index < embed_values->size_of()) {
+            embed_values != NULL && emb_val->embval_index < embed_values->size_of()) {
           embed_values->get_at(emb_val->embval_index)->XER_encode(UNIVERSAL_CHARSTRING_xer_
             , p_buf, flavor | EMBED_VALUES, indent+1, 0);
           ++emb_val->embval_index;
@@ -4207,7 +4428,7 @@ int Record_Type::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
       } //for
       
       if (0 != emb_val) {
-        if (emb_val->embval_index < embed_values->size_of()) {
+        if (embed_values != NULL && emb_val->embval_index < embed_values->size_of()) {
           ec_1.set_msg("%s': ", fld_name(0));
           TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_CONSTRAINT,
             "Too many EMBED-VALUEs specified: %d (expected %d or less)",
@@ -4896,9 +5117,8 @@ int Record_Type::XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
 }
 
 int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
-                            unsigned int flavor, embed_values_dec_struct_t*)
+                            unsigned int flavor, unsigned int flavor2, embed_values_dec_struct_t*)
 {
-  bound_flag = TRUE;
   int exer = is_exer(flavor);
   int success, type;
   int depth=-1; // depth of the start tag
@@ -4910,10 +5130,11 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
   boolean tag_closed = (flavor & PARENT_CLOSED) != 0;
   // If the parent has USE-TYPE, our ATTRIBUTE members can be found
   // in the parent's tag (the reader is sitting on it).
-  const boolean parent_tag = exer && (flavor & (/*USE_NIL|*/ USE_TYPE_ATTR));
+  const boolean parent_tag = exer && ((flavor & USE_TYPE_ATTR) || (flavor2 & USE_NIL_PARENT_TAG));
 
   // Filter out flags passed by our parent. These are not for the fields.
   flavor &= XER_MASK; // also removes XER_TOPLEVEL
+  flavor2 = XER_NONE; // Remove only bit: USE_NIL_PARENT_TAG (for now)
 
   const int field_cnt = get_count();
   const int num_attributes = get_xer_num_attr();
@@ -4926,7 +5147,7 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
   // fields); normal processing start at this field.
   const int start_at = uo_index + ((p_td.xer_bits & USE_ORDER) != 0);
   const int first_nonattr = start_at + num_attributes;
-
+  
   // The index of the ANY-ATTRIBUTES member, if any
   int aa_index = -1;
   for (int k = 0; k < first_nonattr; ++k) {
@@ -4994,12 +5215,15 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
     TTCN_EncDec_ErrorContext ec_0("Component '");
     TTCN_EncDec_ErrorContext ec_1;
     boolean usenil_attribute = FALSE; // true if found and said yes
+    // If nillable and the nillable field is a record type, that has attributes
+    // then it will become true, and skips the processing of the fields after
+    boolean already_processed = FALSE;
     if (!exer) {
       if (!reader.IsEmptyElement()) reader.Read();
       // First, the (would-be) attributes (unaffected by USE-ORDER)
       for (i = 0; i < first_nonattr; i++) {
         ec_1.set_msg("%s': ", fld_name(i));
-        get_at(i)->XER_decode(*xer_descr(i), reader, flavor, 0);
+        get_at(i)->XER_decode(*xer_descr(i), reader, flavor, flavor2, 0);
       } // next field
     }
     else if (own_tag || parent_tag) { // EXER and not UNTAGGED: do attributes
@@ -5027,7 +5251,12 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
       }
 
       /* * * * * * * * * Attributes * * * * * * * * * * * * * */
-      for (success = reader.MoveToFirstAttribute();
+      if(parent_tag && reader.NodeType() == XML_READER_TYPE_ATTRIBUTE) {
+        success = reader.Ok();
+      } else {
+        success = reader.MoveToFirstAttribute();
+      }
+      for (;
         success == 1 && reader.NodeType() == XML_READER_TYPE_ATTRIBUTE;
         success = reader.AdvanceAttribute())
       {
@@ -5041,7 +5270,7 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
         if (field_index != -1) {
           // There is a field. Let it decode the attribute.
           ec_1.set_msg("%s': ", fld_name(field_index));
-          get_at(field_index)->XER_decode(*xer_descr(field_index), reader, flavor, 0);
+          get_at(field_index)->XER_decode(*xer_descr(field_index), reader, flavor, flavor2, 0);
           continue;
         }
 
@@ -5063,8 +5292,13 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
 
             continue;
           } // it is the "nil" attribute
+          // else, let the nillable field decode the next attributes, it is possible
+          // that it belongs to him
+          get_at(field_cnt-1)->XER_decode(*xer_descr(field_cnt-1), reader, flavor | USE_NIL, flavor2 | USE_NIL_PARENT_TAG, 0);
+          already_processed = TRUE;
+          break;
         } // type has USE-NIL
-
+        
         if (parent_tag) {
           const char *prefix = (const char*)reader.Prefix();
           // prefix may be NULL, control_ns->px is never NULL or empty
@@ -5125,7 +5359,7 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
             continue;
           }
         }
-
+        
         // Nobody wanted the attribute. That is an error.
         ec_0.set_msg(" "); ec_1.set_msg(" ");
         TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INVAL_MSG,
@@ -5144,15 +5378,23 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
       }
 
       i = first_nonattr; // finished with attributes
-      // AdvanceAttribute did MoveToElement. Move into the content (if any).
-      if (!reader.IsEmptyElement()) reader.Read();
+      // AdvanceAttribute did MoveToElement. Move into the content (if any),
+      // except when the reader is already moved in(already_processed).
+      if (!reader.IsEmptyElement() && !already_processed) reader.Read();
     } // end if (own_tag)
-
+    
     /* * * * * * * * Non-attributes (elements) * * * * * * * * * * * */
     embed_values_dec_struct_t* emb_val = 0;
+    bool emb_val_optional = false;
     if (exer && (p_td.xer_bits & EMBED_VALUES)) {
       emb_val = new embed_values_dec_struct_t;
-      emb_val->embval_array = static_cast<Record_Of_Type*>(get_at(0));
+      emb_val->embval_array = dynamic_cast<Record_Of_Type*>(get_at(0));
+      if (NULL == emb_val->embval_array) {
+        OPTIONAL<PreGenRecordOf::PREGEN__RECORD__OF__UNIVERSAL__CHARSTRING>* embed_value = static_cast<OPTIONAL<PreGenRecordOf::PREGEN__RECORD__OF__UNIVERSAL__CHARSTRING>*>(get_at(0));
+        embed_value->set_to_present();
+        emb_val->embval_array = static_cast<Record_Of_Type*>((*embed_value).get_opt_value());
+        emb_val_optional = true;
+      }
       emb_val->embval_array->set_size(0);
       emb_val->embval_index = 0;
     }
@@ -5167,98 +5409,79 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
         get_at(oi)->set_to_omit();
         ++n_optionals;
       }
+
       Record_Of_Type *use_order = static_cast<Record_Of_Type*>(get_at(uo_index));
       // Initialize the use_order field to empty. Let it grow on demand.
       // (setting it to the minimum acceptable size may leave unbound elements
       // if the XML was incomplete).
       use_order->set_size(0);
 
-      Record_Type *jumbled = this; // the record affected by USE_ORDER
-      int begin = first_nonattr;
-      int end   = field_cnt; // "one past"
-      if (p_td.xer_bits & USE_NIL) {
-        Base_Type *last_optional = get_at(field_cnt-1);
-        if (!usenil_attribute) { // exer known true
-          last_optional->set_to_present();
-          jumbled = static_cast<Record_Type*>(last_optional->get_opt_value());
-          // We will operate on the members of last_optional,
-          // effectively bypassing last_optional->XER_decode() itself.
-          begin = 0;
-          end   = jumbled->get_count();
-          ec_1.set_msg("%s': ", fld_name(field_cnt-1));
-        }
-      }
-      if (num_attributes > 0
-        && first_nonattr != field_cnt
-        && i == first_nonattr - 1) { // exer known true
-        // If there were attributes and their processing just finished,
-        // the reader is positioned on the start tag of the record.
-        // Move ahead, unless there are no non-attribute fields.
-        reader.Read();
-      }
-      // Then, the non-attributes
-
-      // The index runs over the members affected by USE-ORDER.
-      // This is [first_nonattr,field_cnt) unless USE-NIL is involved,
-      // in which case it's [0,optional_sequence::field_cnt)
-      int *seen = new int[end-begin];
-      int num_seen = 0;
-      int last_any_elem = begin - 1;
-      // The index of the latest embedded value can change outside of this function
-      // (if the field is a untagged record of), in this case the next value should
-      // be ignored, as it's already been handled by the record of
-      int last_embval_index = 0;
-      for (i = begin; i < end; i++) {
-        for (success = reader.Ok(); success == 1; success = reader.Read()) {
-          type = reader.NodeType();
-          if (0 != emb_val && reader.NodeType()==XML_READER_TYPE_TEXT) {
-            UNIVERSAL_CHARSTRING emb_ustr((const char*)reader.Value());
-            emb_val->embval_array->get_at(emb_val->embval_index)->set_value(&emb_ustr);
+      // Nothing to order if there are no child elements
+      if (!tag_closed) {
+        Record_Type *jumbled = this; // the record affected by USE_ORDER
+        int begin = first_nonattr;
+        int end   = field_cnt; // "one past"
+        if (p_td.xer_bits & USE_NIL) {
+          Base_Type *last_optional = get_at(field_cnt-1);
+          if (!usenil_attribute) { // exer known true
+            last_optional->set_to_present();
+            jumbled = static_cast<Record_Type*>(last_optional->get_opt_value());
+            // We will operate on the members of last_optional,
+            // effectively bypassing last_optional->XER_decode() itself.
+            begin = 0;
+            end   = jumbled->get_count();
+            ec_1.set_msg("%s': ", fld_name(field_cnt-1));
           }
-          // The non-attribute components must not be UNTAGGED
-          if (type == XML_READER_TYPE_ELEMENT) break;
-          // else if (type==XML_READER_TYPE_END_ELEMENT) panic?
         }
-        if (0 != emb_val) {
-          if (last_embval_index == emb_val->embval_index) {
-            ++emb_val->embval_index;
-          }
-          last_embval_index = emb_val->embval_index;
+        if (num_attributes > 0
+          && first_nonattr != field_cnt
+          && i == first_nonattr - 1) { // exer known true
+          // If there were attributes and their processing just finished,
+          // the reader is positioned on the start tag of the record.
+          // Move ahead, unless there are no non-attribute fields.
+          reader.Read();
         }
-        if (success != 1) break;
-        const char *name = (const char *)reader.LocalName();
-        boolean field_name_found = false;
-        // Find out which member it is.
-        // FIXME some hashing should be implemented
-        for (int k = begin; k < end; k++) {
-          if (!(jumbled->xer_descr(k)->xer_bits & ANY_ELEMENT) &&
-              check_name(name, *jumbled->xer_descr(k), 1)) {
-            ec_1.set_msg("%s': ", jumbled->fld_name(k));
-
-            // Check for the same field being decoded twice.
-            // We can't use the field's is_bound()/is_present(),
-            // because the field may be bound on input, e.g. for
-            // prototype(fast) or prototype(backtrack).
-            int in_dex = k - begin;
-            for (int o = 0; o < num_seen ;++o) {
-              if (in_dex == seen[o]) TTCN_EncDec_ErrorContext::error(
-                TTCN_EncDec::ET_INVAL_MSG, "Duplicate element");
+        // Then, the non-attributes
+
+        // The index runs over the members affected by USE-ORDER.
+        // This is [first_nonattr,field_cnt) unless USE-NIL is involved,
+        // in which case it's [0,optional_sequence::field_cnt)
+        int *seen = new int[end-begin];
+        int num_seen = 0;
+        int last_any_elem = begin - 1;
+        // The index of the latest embedded value can change outside of this function
+        // (if the field is an untagged record of), in this case the next value should
+        // be ignored, as it's already been handled by the record of
+        int last_embval_index = 0;
+        bool early_exit = false;
+        for (i = begin; i < end; i++) {
+          for (success = reader.Ok(); success == 1; success = reader.Read()) {
+            type = reader.NodeType();
+            if (0 != emb_val && reader.NodeType()==XML_READER_TYPE_TEXT) {
+              UNIVERSAL_CHARSTRING emb_ustr((const char*)reader.Value());
+              emb_val->embval_array->get_at(emb_val->embval_index)->set_value(&emb_ustr);
+            }
+            // The non-attribute components must not be UNTAGGED
+            if (type == XML_READER_TYPE_ELEMENT) break;
+            if (type == XML_READER_TYPE_END_ELEMENT) {
+              early_exit = true;
+              break;
             }
-            seen[num_seen++] = in_dex;
-            // Set the next use-order member.
-            // Non-const get_at creates the object in the record-of.
-            static_cast<Enum_Type*>(use_order->get_at(i - begin))
-            ->from_int(in_dex);
-            Base_Type *b = jumbled->get_at(k);
-            b->XER_decode(*jumbled->xer_descr(k), reader, flavor, emb_val);
-            field_name_found = true;
-            break;
           }
-        }
-        if (!field_name_found) {
-          // Check the anyElement fields
-          for (int k = last_any_elem + 1; k < end; k++) {
-            if (jumbled->xer_descr(k)->xer_bits & ANY_ELEMENT) {
+          if (0 != emb_val) {
+            if (last_embval_index == emb_val->embval_index) {
+              ++emb_val->embval_index;
+            }
+            last_embval_index = emb_val->embval_index;
+          }
+          if (success != 1 || early_exit) break;
+          const char *name = (const char *)reader.LocalName();
+          bool field_name_found = false;
+          // Find out which member it is.
+          // FIXME some hashing should be implemented
+          for (int k = begin; k < end; k++) {
+            if (!(jumbled->xer_descr(k)->xer_bits & ANY_ELEMENT) &&
+                check_name(name, *jumbled->xer_descr(k), 1)) {
               ec_1.set_msg("%s': ", jumbled->fld_name(k));
 
               // Check for the same field being decoded twice.
@@ -5276,45 +5499,78 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
               static_cast<Enum_Type*>(use_order->get_at(i - begin))
               ->from_int(in_dex);
               Base_Type *b = jumbled->get_at(k);
-              b->XER_decode(*jumbled->xer_descr(k), reader, flavor, emb_val);
-              last_any_elem = k;
+              b->XER_decode(*jumbled->xer_descr(k), reader, flavor, flavor2, emb_val);
+              field_name_found = true;
               break;
             }
           }
+          if (!field_name_found) {
+            // Check the anyElement fields
+            for (int k = last_any_elem + 1; k < end; k++) {
+              if (jumbled->xer_descr(k)->xer_bits & ANY_ELEMENT) {
+                ec_1.set_msg("%s': ", jumbled->fld_name(k));
+
+                // Check for the same field being decoded twice.
+                // We can't use the field's is_bound()/is_present(),
+                // because the field may be bound on input, e.g. for
+                // prototype(fast) or prototype(backtrack).
+                int in_dex = k - begin;
+                for (int o = 0; o < num_seen ;++o) {
+                  if (in_dex == seen[o]) TTCN_EncDec_ErrorContext::error(
+                    TTCN_EncDec::ET_INVAL_MSG, "Duplicate element");
+                }
+                seen[num_seen++] = in_dex;
+                // Set the next use-order member.
+                // Non-const get_at creates the object in the record-of.
+                static_cast<Enum_Type*>(use_order->get_at(i - begin))
+                ->from_int(in_dex);
+                Base_Type *b = jumbled->get_at(k);
+                b->XER_decode(*jumbled->xer_descr(k), reader, flavor, flavor2, emb_val);
+                last_any_elem = k;
+                field_name_found = true;
+                break;
+              }
+            }
+          }
+          if (!field_name_found) {
+            TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INVAL_MSG,
+              "Bad XML tag '%s' instead of a valid field", name);
+            break;
+          }
+        } // next field
+        if (0 != emb_val) {
+          if (reader.NodeType()==XML_READER_TYPE_TEXT) {
+            UNIVERSAL_CHARSTRING emb_ustr((const char*)reader.Value());
+            emb_val->embval_array->get_at(emb_val->embval_index)->set_value(&emb_ustr);
+          }
+          if (last_embval_index == emb_val->embval_index) {
+            ++emb_val->embval_index;
+          }
         }
-      } // next field
-      if (0 != emb_val) {
-        if (reader.NodeType()==XML_READER_TYPE_TEXT) {
-          UNIVERSAL_CHARSTRING emb_ustr((const char*)reader.Value());
-          emb_val->embval_array->get_at(emb_val->embval_index)->set_value(&emb_ustr);
-        }
-        if (last_embval_index == emb_val->embval_index) {
-          ++emb_val->embval_index;
-        }
-      }
-      delete [] seen;
-      ec_1.set_msg(" "); // no active component
-      ec_0.set_msg(" ");
-
-      // Check that we collected the required number of children
-      int num_collected = use_order->size_of();
-      if (p_td.xer_bits & USE_NIL) {
-        int expected = usenil_attribute ? 0 : jumbled->get_count();
-        if (num_collected != expected) {
-          TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INCOMPL_MSG,
-            "Incorrect number of fields %d, expected %d",
-            num_collected, expected);
+        delete [] seen;
+        ec_1.set_msg(" "); // no active component
+        ec_0.set_msg(" ");
+
+        // Check that we collected the required number of children
+        int num_collected = use_order->size_of();
+        if (p_td.xer_bits & USE_NIL) {
+          int expected = usenil_attribute ? 0 : jumbled->get_count();
+          if (num_collected != expected) {
+            TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INCOMPL_MSG,
+              "Incorrect number of fields %d, expected %d",
+              num_collected, expected);
+          }
         }
-      }
-      else {
-        if (num_collected < field_cnt - first_nonattr - n_optionals
-          ||num_collected > field_cnt - first_nonattr) {
-          TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INCOMPL_MSG,
-            "Wrong number of fields! size = %d, expected %d..%d",
-            use_order->size_of(), field_cnt - first_nonattr - n_optionals,
-            field_cnt - first_nonattr);
+        else {
+          if (num_collected < field_cnt - first_nonattr - n_optionals
+            ||num_collected > field_cnt - first_nonattr) {
+            TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INCOMPL_MSG,
+              "Wrong number of fields! size = %d, expected %d..%d",
+              use_order->size_of(), field_cnt - first_nonattr - n_optionals,
+              field_cnt - first_nonattr);
+          }
         }
-      }
+      } // not empty element
     }
     else { // not USE-ORDER, simpler code      
       if (usenil_attribute) {
@@ -5354,12 +5610,12 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
                 optional_any_elem_check = opt_field->XER_check_any_elem(reader, next_field_name, tag_closed);
               }
             }
-            if (optional_any_elem_check) {
+            if (optional_any_elem_check && !already_processed) {
               int new_flavor = flavor ;
               if (i == field_cnt-1) new_flavor |= (p_td.xer_bits & USE_NIL);
               if (tag_closed)       new_flavor |= PARENT_CLOSED;
 
-              get_at(i)->XER_decode(*xer_descr(i), reader, new_flavor, emb_val);
+              get_at(i)->XER_decode(*xer_descr(i), reader, new_flavor, flavor2, emb_val);
             }
           }
           if (!get_at(i)->is_present()) {
@@ -5381,17 +5637,31 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
     } // if use-order
 
     if (0 != emb_val) {
+      bool all_unbound = true;
       static const UNIVERSAL_CHARSTRING emptystring(0, (const char*)NULL);
       for (int j = 0; j < emb_val->embval_index; ++j) {
         if (!emb_val->embval_array->get_at(j)->is_bound()) {
           emb_val->embval_array->get_at(j)->set_value(&emptystring);
+        }else if((static_cast<const UNIVERSAL_CHARSTRING*>(emb_val->embval_array->get_at(j)))->lengthof() !=0) {
+          all_unbound = false;
         }
       }
+      if(emb_val_optional && all_unbound){
+        static_cast<OPTIONAL<PreGenRecordOf::PREGEN__RECORD__OF__UNIVERSAL__CHARSTRING>*>(get_at(0))->set_to_omit();
+      }
       delete emb_val;
     } // if embed-values
 
   } // if use-qname
-
+  
+  // Check if every non-optional field has been set
+  for (i = 0; i < field_cnt; ++i) {
+    if (!get_at(i)->is_optional() && !get_at(i)->is_bound()) {
+      TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INCOMPL_MSG,
+        "No data found for non-optional field '%s'", fld_name(i));
+    }
+  }
+  
   if (own_tag) {
     // We had our start tag. Then our fields did their thing.
     // Now we expect the end tag. And it better be our end tag!
@@ -5438,8 +5708,12 @@ int Record_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
   return 1; // decode successful
 }
 
-int Record_Type::JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer& p_tok) const
+int Record_Type::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const
 {
+  if (err_descr) {             
+    return JSON_encode_negtest(err_descr, p_td, p_tok);                
+  }
+
   if (!is_bound()) {
     TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
       "Encoding an unbound %s value.", is_set() ? "set" : "record");
@@ -5449,15 +5723,123 @@ int Record_Type::JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer& p_tok
   int enc_len = p_tok.put_next_token(JSON_TOKEN_OBJECT_START, NULL);
   
   int field_count = get_count();
-  for(int i = 0; i < field_count; ++i) {
+  for (int i = 0; i < field_count; ++i) {
+    boolean metainfo_unbound = NULL != fld_descr(i)->json && fld_descr(i)->json->metainfo_unbound;
     if ((NULL != fld_descr(i)->json && fld_descr(i)->json->omit_as_null) || 
-        !get_at(i)->is_optional() || get_at(i)->is_present()) {
-      if (NULL != fld_descr(i)->json && NULL != fld_descr(i)->json->alias) {
-        enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, fld_descr(i)->json->alias);
+        get_at(i)->is_present() || metainfo_unbound) {
+      const char* field_name = (NULL != fld_descr(i)->json && NULL != fld_descr(i)->json->alias) ?
+        fld_descr(i)->json->alias : fld_name(i);
+      enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, field_name);
+      if (metainfo_unbound && !get_at(i)->is_bound()) {
+        enc_len += p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL);
+        char* metainfo_str = mprintf("metainfo %s", field_name);
+        enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, metainfo_str);
+        Free(metainfo_str);
+        enc_len += p_tok.put_next_token(JSON_TOKEN_STRING, "\"unbound\"");
+      }
+      else {
+        enc_len += get_at(i)->JSON_encode(*fld_descr(i), p_tok);
+      }
+    }
+  }
+  
+  enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_END, NULL);
+  return enc_len;
+}
+
+int Record_Type::JSON_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
+                                     const TTCN_Typedescriptor_t& p_td,
+                                     JSON_Tokenizer& p_tok) const 
+{
+  if (!is_bound()) {
+    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
+      "Encoding an unbound %s value.", is_set() ? "set" : "record");
+    return -1;
+  }
+  
+  int enc_len = p_tok.put_next_token(JSON_TOKEN_OBJECT_START, NULL);
+  
+  int values_idx = 0;
+  int edescr_idx = 0;
+  
+  int field_count = get_count();
+  for (int i = 0; i < field_count; ++i) {
+    if (-1 != p_err_descr->omit_before && p_err_descr->omit_before > i) {
+      continue;
+    }
+    
+    const Erroneous_values_t* err_vals = p_err_descr->next_field_err_values(i, values_idx);
+    const Erroneous_descriptor_t* emb_descr = p_err_descr->next_field_emb_descr(i, edescr_idx);
+    
+    if (NULL != err_vals && NULL != err_vals->before) {
+      if (NULL == err_vals->before->errval) {
+        TTCN_error("internal error: erroneous before value missing");
+      }
+      if (err_vals->before->raw) {
+        enc_len += err_vals->before->errval->JSON_encode_negtest_raw(p_tok);
+      } else {
+        if (NULL == err_vals->before->type_descr) {
+          TTCN_error("internal error: erroneous before typedescriptor missing");
+        }
+        // it's an extra field, so use the erroneous type's name as the field name
+        enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, err_vals->before->type_descr->name);
+        enc_len += err_vals->before->errval->JSON_encode(*(err_vals->before->type_descr), p_tok);
+      }
+    }
+    
+    const char* field_name = (NULL != fld_descr(i)->json && NULL != fld_descr(i)->json->alias) ?
+      fld_descr(i)->json->alias : fld_name(i);
+    if (NULL != err_vals && NULL != err_vals->value) {
+      if (NULL != err_vals->value->errval) {
+        if (err_vals->value->raw) {
+          enc_len += err_vals->value->errval->JSON_encode_negtest_raw(p_tok);
+        } else {
+          if (NULL == err_vals->value->type_descr) {
+            TTCN_error("internal error: erroneous before typedescriptor missing");
+          }
+          // only replace the field's value, keep the field name
+          enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, field_name);
+          enc_len += err_vals->value->errval->JSON_encode(*(err_vals->value->type_descr), p_tok);
+        }
+      }
+    } else {
+      boolean metainfo_unbound = NULL != fld_descr(i)->json && fld_descr(i)->json->metainfo_unbound;
+      if ((NULL != fld_descr(i)->json && fld_descr(i)->json->omit_as_null) || 
+          get_at(i)->is_present() || metainfo_unbound) {
+        enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, field_name);
+        if (metainfo_unbound && !get_at(i)->is_bound()) {
+          enc_len += p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL);
+          char* metainfo_str = mprintf("metainfo %s", field_name);
+          enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, metainfo_str);
+          Free(metainfo_str);
+          enc_len += p_tok.put_next_token(JSON_TOKEN_STRING, "\"unbound\"");
+        }
+        else if (NULL != emb_descr) {
+          enc_len += get_at(i)->JSON_encode_negtest(emb_descr, *fld_descr(i), p_tok);
+        } else {
+          enc_len += get_at(i)->JSON_encode(*fld_descr(i), p_tok);
+        }
+      }
+    }
+    
+    if (NULL != err_vals && NULL != err_vals->after) {
+      if (NULL == err_vals->after->errval) {
+        TTCN_error("internal error: erroneous after value missing");
+      }
+      if (err_vals->after->raw) {
+        enc_len += err_vals->after->errval->JSON_encode_negtest_raw(p_tok);
       } else {
-        enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, fld_name(i));
+        if (NULL == err_vals->after->type_descr) {
+          TTCN_error("internal error: erroneous before typedescriptor missing");
+        }
+        // it's an extra field, so use the erroneous type's name as the field name
+        enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, err_vals->after->type_descr->name);
+        enc_len += err_vals->after->errval->JSON_encode(*(err_vals->after->type_descr), p_tok);
       }
-      enc_len += get_at(i)->JSON_encode(*fld_descr(i), p_tok);
+    }
+    
+    if (-1 != p_err_descr->omit_after && p_err_descr->omit_after <= i) {
+      break;
     }
   }
   
@@ -5475,11 +5857,19 @@ int Record_Type::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&
   }
   else if (JSON_TOKEN_OBJECT_START != token) {
     return JSON_ERROR_INVALID_TOKEN;
-  }
-  bound_flag = TRUE;
+  } 
   
   const int field_count = get_count();
   
+  // initialize meta info states
+  int* metainfo = new int[field_count];
+  boolean* field_found = new boolean[field_count];
+  for (int i = 0; i < field_count; ++i) {
+    field_found[i] = FALSE;
+    metainfo[i] = (NULL != fld_descr(i)->json && fld_descr(i)->json->metainfo_unbound) ?
+      JSON_METAINFO_NONE : JSON_METAINFO_NOT_APPLICABLE;
+  }
+  
   while (true) {
     // Read name - value token pairs until we reach some other token
     char* name = 0;
@@ -5496,6 +5886,14 @@ int Record_Type::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&
       break;
     }
     else {
+      // check for meta info
+      boolean is_metainfo = FALSE;
+      if (name_len > 9 && 0 == strncmp(name, "metainfo ", 9)) {
+        name += 9;
+        name_len -= 9;
+        is_metainfo = TRUE;
+      }
+      
       // check field name
       int field_idx;
       for (field_idx = 0; field_idx < field_count; ++field_idx) {
@@ -5507,13 +5905,15 @@ int Record_Type::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&
         }
         if (strlen(expected_name) == name_len &&
             0 == strncmp(expected_name, name, name_len)) {
+          field_found[field_idx] = TRUE;
           break;
         }
       }
       if (field_count == field_idx) {
         // invalid field name
         char* name2 = mcopystrn(name, name_len);
-        JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_INVALID_NAME_ERROR, name2);
+        JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, is_metainfo ?
+          JSON_DEC_METAINFO_NAME_ERROR : JSON_DEC_INVALID_NAME_ERROR, name2);
         // if this is set to a warning, skip the value of the field
         dec_len += p_tok.get_next_token(&token, NULL, NULL);
         if (JSON_TOKEN_NUMBER != token && JSON_TOKEN_STRING != token &&
@@ -5527,14 +5927,53 @@ int Record_Type::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&
         continue;
       }
       
-      int ret_val = get_at(field_idx)->JSON_decode(*fld_descr(field_idx), p_tok, p_silent);
-      if (0 > ret_val) {
-        if (JSON_ERROR_INVALID_TOKEN) {
-          JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_FIELD_TOKEN_ERROR, fld_name(field_idx));
+      if (is_metainfo) {
+        if (JSON_METAINFO_NOT_APPLICABLE != metainfo[field_idx]) {
+          // check meta info
+          char* info_value = 0;
+          size_t info_len = 0;
+          dec_len += p_tok.get_next_token(&token, &info_value, &info_len);
+          if (JSON_TOKEN_STRING == token && 9 == info_len &&
+              0 == strncmp(info_value, "\"unbound\"", 9)) {
+            metainfo[field_idx] = JSON_METAINFO_UNBOUND;
+          }
+          else {
+            JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_METAINFO_VALUE_ERROR,
+              fld_name(field_idx));
+            return JSON_ERROR_FATAL;
+          }
         }
-        return JSON_ERROR_FATAL;
+        else {
+          JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_METAINFO_NOT_APPLICABLE,
+            fld_name(field_idx));
+          return JSON_ERROR_FATAL;
+        }
+      }
+      else {
+        buf_pos = p_tok.get_buf_pos();
+        int ret_val = get_at(field_idx)->JSON_decode(*fld_descr(field_idx), p_tok, p_silent);
+        if (0 > ret_val) {
+          if (JSON_ERROR_INVALID_TOKEN == ret_val) {
+            // undo the last action on the buffer, check if the invalid token was a null token 
+            p_tok.set_buf_pos(buf_pos);
+            p_tok.get_next_token(&token, NULL, NULL);
+            if (JSON_TOKEN_LITERAL_NULL == token) {
+              if (JSON_METAINFO_NONE == metainfo[field_idx]) {
+                // delay reporting an error for now, there might be meta info later
+                metainfo[field_idx] = JSON_METAINFO_NEEDED;
+                continue;
+              }
+              else if (JSON_METAINFO_UNBOUND == metainfo[field_idx]) {
+                // meta info already found
+                continue;
+              }
+            }
+            JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_FIELD_TOKEN_ERROR, fld_name(field_idx));
+          }
+          return JSON_ERROR_FATAL;
+        }
+        dec_len += ret_val;
       }
-      dec_len += ret_val;
     }
   }
   
@@ -5544,10 +5983,17 @@ int Record_Type::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&
     return JSON_ERROR_FATAL;
   }
   
-  // Check if every field has been set
+  // Check if every field has been set and handle meta info
   for (int field_idx = 0; field_idx < field_count; ++field_idx) {
     Base_Type* field = get_at(field_idx);
-    if (!field->is_bound()) {
+    if (JSON_METAINFO_UNBOUND == metainfo[field_idx]) {
+      field->clean_up();
+    }
+    else if (JSON_METAINFO_NEEDED == metainfo[field_idx]) {
+      // no meta info was found for this field, report the delayed error
+      JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_FIELD_TOKEN_ERROR, fld_name(field_idx));
+    }
+    else if (!field_found[field_idx]) {
       if (NULL != fld_descr(field_idx)->json && NULL != fld_descr(field_idx)->json->default_value) {
         get_at(field_idx)->JSON_decode(*fld_descr(field_idx), DUMMY_BUFFER, p_silent);
       }
@@ -5560,6 +6006,8 @@ int Record_Type::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&
     }
   }
   
+  delete[] metainfo;
+  
   return dec_len;
 }
 
@@ -5593,12 +6041,24 @@ void Empty_Record_Type::log() const
 
 void Empty_Record_Type::set_param(Module_Param& param) {
   param.basic_check(Module_Param::BC_VALUE, "empty record/set value (i.e. { })");
-  if (param.get_type()!=Module_Param::MP_Value_List || param.get_size()>0) {
+  Module_Param_Ptr mp = &param;
+  if (param.get_type() == Module_Param::MP_Reference) {
+    mp = param.get_referenced_param();
+  }
+  if (mp->get_type()!=Module_Param::MP_Value_List || mp->get_size()>0) {
     param.type_error("empty record/set value (i.e. { })", get_descriptor()->name);
   }
   bound_flag = TRUE;
 }
 
+Module_Param* Empty_Record_Type::get_param(Module_Param_Name& /* param_name */) const
+{
+  if (!is_bound()) {
+    return new Module_Param_Unbound();
+  }
+  return new Module_Param_Value_List();
+}
+
 void Empty_Record_Type::encode_text(Text_Buf& /*text_buf*/) const
 {
   if (!bound_flag)
@@ -5736,7 +6196,7 @@ void Empty_Record_Type::decode(const TTCN_Typedescriptor_t& p_td,
     for (int success=reader.Read(); success==1; success=reader.Read()) {
       if (reader.NodeType() == XML_READER_TYPE_ELEMENT) break;
     }
-    XER_decode(*(p_td.xer), reader, XER_coding | XER_TOPLEVEL, 0);
+    XER_decode(*(p_td.xer), reader, XER_coding | XER_TOPLEVEL, XER_NONE, 0);
     size_t bytes = reader.ByteConsumed();
     p_buf.set_pos(bytes);
     break;}
@@ -5859,7 +6319,7 @@ int Empty_Record_Type::XER_encode(const XERdescriptor_t& p_td,
 }
 
 int Empty_Record_Type::XER_decode(const XERdescriptor_t& p_td,
-  XmlReaderWrap& reader, unsigned int flavor, embed_values_dec_struct_t*)
+  XmlReaderWrap& reader, unsigned int flavor, unsigned int /*flavor2*/, embed_values_dec_struct_t*)
 {
   int exer = is_exer(flavor);
   bound_flag = true;
This page took 0.042131 seconds and 5 git commands to generate.