Type.cc 288 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
Adam Knapp's avatar
Adam Knapp committed
2
 * Copyright (c) 2000-2021 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Contributors:
 *   Baji, Laszlo
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Beres, Szabolcs
 *   Bibo, Zoltan
 *   Cserveni, Akos
 *   Delic, Adam
 *   Dimitrov, Peter
 *   Feher, Csaba
 *   Forstner, Matyas
 *   Gecse, Roland
 *   Kovacs, Ferenc
 *   Nagy, Lenard
 *   Raduly, Csaba
 *   Szabados, Kristof
 *   Szabo, Janos Zoltan – initial implementation
25
 *   Szabo, Bence Janos
Elemer Lelik's avatar
Elemer Lelik committed
26 27 28 29 30
 *   Szalai, Gabor
 *   Tatarka, Gabor
 *   Zalanyi, Balazs Andor
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "../common/dbgnew.hh"
#include "Type.hh"
#include <ctype.h>
#include "Typestuff.hh" // FIXME CTs
#include "CompType.hh"
#include "TypeCompat.hh"
#include "CompField.hh"
#include "SigParam.hh"
#include "EnumItem.hh"

#include "Valuestuff.hh"
#include "ttcn3/ArrayDimensions.hh"
#include "asn1/Tag.hh"
#include "asn1/Block.hh"
#include "asn1/Ref.hh"
#include "Constraint.hh"
#include "main.hh"
#include "../common/pattern.hh"
#include "ttcn3/Attributes.hh"
#include "XerAttributes.hh"
#include "ttcn3/Ttcnstuff.hh"
#include "ttcn3/TtcnTemplate.hh"
#include "ttcn3/Templatestuff.hh"
54
#include "ttcn3/RawAST.hh"
55
#include "ttcn3/JsonAST.hh"
56
#include "ttcn3/Statement.hh"
Elemer Lelik's avatar
Elemer Lelik committed
57 58 59 60 61 62 63 64

#include "../common/static_check.h"
#include "PredefFunc.hh"

// implemented in coding_attrib_p.y
extern Ttcn::ExtensionAttributes * parse_extattributes(
  Ttcn::WithAttribPath *w_attrib_path);

65 66 67 68
// implemented in compiler.y
extern Common::Value* ttcn3_parse_json_default(
  const char* p_str, const Common::Location& str_loc);

Elemer Lelik's avatar
Elemer Lelik committed
69
namespace Common {
70
  
71
  map<Type*, void> Type::RecursionTracker::types;
72
  dynamic_array<Type*> Type::coding_attrib_check_stack;
Elemer Lelik's avatar
Elemer Lelik committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

  using Ttcn::MultiWithAttrib;
  using Ttcn::SingleWithAttrib;
  using Ttcn::WithAttribPath;

  const char* Type::type_as_string[] = {
  "undefined",                                   // T_UNDEF
  "erroneous",                                   // T_ERROR
  "null(ASN)",                                   // T_NULL
  "boolean",                                     // T_BOOL
  "integer",                                     // T_INT
  "integer(ASN.1)",                              // T_INT_A
  "real/float",                                  // T_REAL
  "enumerated(ASN.1)",                           // T_ENUM_A
  "enumerated(TTCN-3)",                          // T_ENUM_T
  "bitstring",                                   // T_BSTR
  "bitstring(ASN)",                              // T_BSTR_A
  "hexstring(TTCN-3)",                           // T_HSTR
  "octetstring",                                 // T_OSTR
  "charstring (TTCN-3)",                         // T_CSTR
  "universal charstring(TTCN-3)",                // T_USTR
  "UTF8String(ASN.1)",                           // T_UTF8STRING
  "NumericString(ASN.1)",                        // T_NUMERICSTRING
  "PrintableString(ASN.1)",                      // T_PRINTABLESTRING
  "TeletexString(ASN.1)",                        //T_TELETEXSTRING
  "VideotexString(ASN.1)",                       // T_VIDEOTEXSTRING
  "IA5String(ASN.1)",                            // T_IA5STRING
  "GraphicString(ASN.1)",                        // T_GRAPHICSTRING,
  "VisibleString(ASN.1)",                        // T_VISIBLESTRING
  "GeneralString (ASN.1)",                       // T_GENERALSTRING
  "UniversalString (ASN.1)",                     // T_UNIVERSALSTRING
  "BMPString (ASN.1)",                           // T_BMPSTRING
  "UnrestrictedCharacterString(ASN.1)",          // T_UNRESTRICTEDSTRING
  "UTCTime(ASN.1)",                              // T_UTCTIME
  "GeneralizedTime(ASN.1)",                      // T_GENERALIZEDTIME
  "Object descriptor, a kind of string (ASN.1)", // T_OBJECTDESCRIPTOR
  "object identifier",                           // T_OID
  "relative OID(ASN.1)",                         // T_ROID
  "choice(ASN-1)",                               // T_CHOICE_A
  "union(TTCN-3)",                               // T_CHOICE_T
  "sequence (record) of",                        // T_SEQOF
  "set of",                                      // T_SETOF
  "sequence(ASN-1)",                             // T_SEQ_A
  "record(TTCN-3)",                              // T_SEQ_T
  "set(ASN.1)",                                  // T_SET_A
  "set(TTCN-3)",                                 // T_SET_T
  "ObjectClassFieldType(ASN.1)",                 // T_OCFT
  "open type(ASN.1)",                            // T_OPENTYPE
  "ANY(deprecated ASN.1)",                       // T_ANY
  "external(ASN.1)",                             // T_EXTERNAL
  "embedded PDV(ASN.1)",                         // T_EMBEDDED_PDV
  "referenced",                                  // T_REFD
  "special referenced(by pointer, not by name)", // T_REFDSPEC
  "selection type(ASN.1)",                       // T_SELTYPE
  "verdict type(TTCN-3)",                        // T_VERDICT
  "port type(TTCN-3)",                           // T_PORT
  "component type(TTCN-3)",                      // T_COMPONENT
  "address type(TTCN-3)",                        // T_ADDRESS
  "default type (TTCN-3)",                       // T_DEFAULT
  "array(TTCN-3)",                               // T_ARRAY
  "signature(TTCN-3)",                           // T_SIGNATURE
  "function reference(TTCN-3)",                  // T_FUNCTION
  "altstep reference(TTCN-3)",                   // T_ALTSTEP
  "testcase reference(TTCN-3)",                  // T_TESTCASE
  "anytype(TTCN-3)",                             // T_ANYTYPE
138
  "class(TTCN-3)"                                // T_CLASS
Elemer Lelik's avatar
Elemer Lelik committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  };

  // =================================
  // ===== Type
  // =================================
  const char* Type::asString() const {
    if (this->get_typetype() < Type::T_LAST && Type::T_UNDEF < this->get_typetype()) {
      return type_as_string[this->get_typetype()];
    }
    else {
      return type_as_string[Type::T_UNDEF];
    }
  }

  const char* Type::asString(Type::typetype_t type) {
    if (type < Type::T_LAST && Type::T_UNDEF < type) {
      return type_as_string[type];
    }
    else {
      return type_as_string[Type::T_UNDEF];
    }
  }

  // Used by dump() for user-readable messages, by Def_ExtFunction::generate_*
  // The text returned must match the case label without the "CT_" !
  const char *Type::get_encoding_name(MessageEncodingType_t encoding_type)
  {
    ENSURE_EQUAL(Type::T_UNDEF, 0);
    ENSURE_EQUAL(Type::OT_UNKNOWN, 0);
    switch (encoding_type) {
    case CT_BER:
      return "BER";
    case CT_PER:
      return "PER";
    case CT_XER:
      return "XER";
    case CT_RAW:
      return "RAW";
    case CT_TEXT:
      return "TEXT";
    case CT_JSON:
      return "JSON";
181 182
    case CT_OER:
      return "OER";
Elemer Lelik's avatar
Elemer Lelik committed
183 184
    case CT_CUSTOM:
      return "custom";
Elemer Lelik's avatar
Elemer Lelik committed
185 186 187 188 189
    default:
      return "<unknown encoding>";
    }
  }

Elemer Lelik's avatar
Elemer Lelik committed
190
  Type *Type::get_stream_type(MessageEncodingType_t encoding_type, int stream_variant)
Elemer Lelik's avatar
Elemer Lelik committed
191
  {
192 193 194
    if (stream_variant == 0) {
      return get_pooltype(T_BSTR);
    }
Elemer Lelik's avatar
Elemer Lelik committed
195 196 197 198 199 200
    switch (encoding_type) {
    case CT_BER:
    case CT_PER:
    case CT_RAW:
    case CT_XER: // UTF-8 doesn't fit into charstring and universal is wasteful
    case CT_JSON:
201
    case CT_OER:
Elemer Lelik's avatar
Elemer Lelik committed
202 203
      return get_pooltype(T_OSTR);
    case CT_TEXT:
204
      if(stream_variant==1){
Elemer Lelik's avatar
Elemer Lelik committed
205 206 207 208
        return get_pooltype(T_CSTR);
      } else {
        return get_pooltype(T_OSTR);
      }
Elemer Lelik's avatar
Elemer Lelik committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    default:
      FATAL_ERROR("Type::get_stream_type()");
      return 0;
    }
  }

  map<Type::typetype_t, Type> *Type::pooltypes = 0;

  Type* Type::get_pooltype(typetype_t p_typetype)
  {
    p_typetype=get_typetype_ttcn3(p_typetype);
    switch(p_typetype) {
    case T_NULL:
    case T_BOOL:
    case T_INT:
    case T_REAL:
    case T_BSTR:
    case T_HSTR:
    case T_OSTR:
    case T_CSTR:
    case T_USTR:
    case T_OID:
    case T_ERROR:
    case T_VERDICT:
    case T_COMPONENT:
    case T_DEFAULT:
235
    case T_CLASS: // for the built-in class 'object'
Elemer Lelik's avatar
Elemer Lelik committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
      break; // we have a pool type
    default:
      return 0; // no pool type for you!
    } // switch
    if (!pooltypes) pooltypes = new map<typetype_t, Type>; // lazy init
    else if (pooltypes->has_key(p_typetype)) return (*pooltypes)[p_typetype];
    Type *t;
    if (p_typetype == T_COMPONENT)
      t = new Type(T_COMPONENT, new ComponentTypeBody());
    else t = new Type(p_typetype);
    t->ownertype = OT_POOL;
    pooltypes->add(p_typetype, t);
    return t;
  }

  void Type::destroy_pooltypes()
  {
    if(pooltypes) {
      for(size_t i=0; i<pooltypes->size(); i++)
        delete pooltypes->get_nth_elem(i);
      pooltypes->clear();
      delete pooltypes;
      pooltypes=0;
    }
  }

  Tag *Type::get_default_tag()
  {
    typetype_t t_typetype;
    switch (typetype) {
    case T_INT:
      t_typetype = T_INT_A;
      break;
    case T_BSTR:
      t_typetype = T_BSTR_A;
      break;
    case T_ENUM_T:
      t_typetype = T_ENUM_A;
      break;
    case T_SEQ_T:
    case T_SEQOF:
      t_typetype = T_SEQ_A;
      break;
    case T_SET_T:
    case T_SETOF:
      t_typetype = T_SET_A;
      break;
    case T_OPENTYPE:
      t_typetype = T_ANY;
      break;
    case T_REFD:
    case T_REFDSPEC:
    case T_SELTYPE:
    case T_OCFT:
      return get_type_refd()->get_tag();
    default:
      t_typetype = typetype;
      break;
    }
    if (!default_tags) default_tags = new map<typetype_t, Tag>;
    else if (default_tags->has_key(t_typetype))
      return (*default_tags)[t_typetype];
    Tag *tag;
    switch (t_typetype) {
    case T_ANY:
      tag = new Tag(Tag::TAG_EXPLICIT, Tag::TAG_ALL, (Int)0);
      break;
    case T_ERROR:
      tag = new Tag(Tag::TAG_EXPLICIT, Tag::TAG_ERROR, (Int)0);
      break;
    default: {
      int tagnumber = get_default_tagnumber(t_typetype);
      if (tagnumber < 0) FATAL_ERROR ("Type::get_default_tag():type `%s' "
        "does not have default tag", get_typename().c_str());
      tag = new Tag(Tag::TAG_EXPLICIT, Tag::TAG_UNIVERSAL, Int(tagnumber));
      break; }
    }
    default_tags->add(t_typetype, tag);
    return tag;
  }

  int Type::get_default_tagnumber(typetype_t p_tt)
  {
    switch (p_tt) {
    // note: tag number 0 is reserved for internal use
    case T_BOOL:
      return 1;
    case T_INT_A:
      return 2;
    case T_BSTR_A:
      return 3;
    case T_OSTR:
      return 4;
    case T_NULL:
      return 5;
    case T_OID:
      return 6;
    case T_OBJECTDESCRIPTOR:
      return 7;
    case T_EXTERNAL:
      return 8;
    case T_REAL:
      return 9;
    case T_ENUM_A:
      return 10;
    case T_EMBEDDED_PDV:
      return 11;
    case T_UTF8STRING:
      return 12;
    case T_ROID:
      return 13;
    // note: tag numbers 14 and 15 are reserved for future use
    case T_SEQ_A:
      return 16;
    case T_SET_A:
      return 17;
    case T_NUMERICSTRING:
      return 18;
    case T_PRINTABLESTRING:
      return 19;
    case T_TELETEXSTRING:
      return 20;
    case T_VIDEOTEXSTRING:
      return 21;
    case T_IA5STRING:
      return 22;
    case T_UTCTIME:
      return 23;
    case T_GENERALIZEDTIME:
      return 24;
    case T_GRAPHICSTRING:
      return 25;
    case T_VISIBLESTRING:
      return 26;
    case T_GENERALSTRING:
      return 27;
    case T_UNIVERSALSTRING:
      return 28;
    case T_UNRESTRICTEDSTRING:
      return 29;
    case T_BMPSTRING:
      return 30;
    default:
      return -1;
    }
  }

  map<Type::typetype_t, Tag> *Type::default_tags = 0;

  void Type::destroy_default_tags()
  {
    if (default_tags) {
      size_t nof_tags = default_tags->size();
      for (size_t i = 0; i < nof_tags; i++)
        delete default_tags->get_nth_elem(i);
      default_tags->clear();
      delete default_tags;
      default_tags = 0;
    }
  }

  Type::Type(const Type& p)
    : Governor(p), typetype(p.typetype)
  {
    init();
    if (p.w_attrib_path != NULL) FATAL_ERROR("Type::Type()");
    tags=p.tags?p.tags->clone():0;
    if(p.constraints) {
      constraints=p.constraints->clone();
      constraints->set_my_type(this);
    }
    else constraints=0;
    if(p.parsed_restr!=NULL) {
      parsed_restr=new vector<SubTypeParse>;
      for(size_t i=0;i<p.parsed_restr->size();i++) {
        SubTypeParse *stp = 0;
        switch((*p.parsed_restr)[i]->get_selection()) {
        case SubTypeParse::STP_SINGLE:
          stp=new SubTypeParse((*p.parsed_restr)[i]->Single());
          break;
        case SubTypeParse::STP_RANGE:
          stp=new SubTypeParse((*p.parsed_restr)[i]->Min(),
            (*p.parsed_restr)[i]->MinExclusive(),
            (*p.parsed_restr)[i]->Max(),
            (*p.parsed_restr)[i]->MaxExclusive());
          break;
        case SubTypeParse::STP_LENGTH:
          FATAL_ERROR("Type::Type(Type&): STP_LENGTH");
          break;
        default: FATAL_ERROR("Type::Type()");
        }
        parsed_restr->add(stp);
      }
    }
    else parsed_restr=0;
431 432 433
    if (coding_table.size() != 0 || coders_to_generate.size() != 0) {
      FATAL_ERROR("Type::Type()");
    }
Elemer Lelik's avatar
Elemer Lelik committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
    switch(typetype) {
    case T_ERROR:
    case T_NULL:
    case T_BOOL:
    case T_INT:
    case T_REAL:
    case T_BSTR:
    case T_HSTR:
    case T_OSTR:
    case T_CSTR:
    case T_USTR:
    case T_UTF8STRING:
    case T_NUMERICSTRING:
    case T_PRINTABLESTRING:
    case T_TELETEXSTRING:
    case T_VIDEOTEXSTRING:
    case T_IA5STRING:
    case T_GRAPHICSTRING:
    case T_VISIBLESTRING:
    case T_GENERALSTRING:
    case T_UNIVERSALSTRING:
    case T_BMPSTRING:
    case T_UTCTIME:
    case T_GENERALIZEDTIME:
    case T_OBJECTDESCRIPTOR:
    case T_OID:
    case T_ROID:
    case T_ANY:
    case T_EXTERNAL:
    case T_EMBEDDED_PDV:
    case T_UNRESTRICTEDSTRING:
    case T_VERDICT:
    case T_DEFAULT:
      break;
    case T_INT_A:
    case T_BSTR_A:
      u.namednums.block=p.u.namednums.block?p.u.namednums.block->clone():0;
      u.namednums.nvs=p.u.namednums.nvs?p.u.namednums.nvs->clone():0;
      break;
    case T_ENUM_A:
      u.enums.block=p.u.enums.block?p.u.enums.block->clone():0;
      u.enums.eis1=p.u.enums.eis1?p.u.enums.eis1->clone():0;
      u.enums.ellipsis=p.u.enums.ellipsis;
      u.enums.excSpec=p.u.enums.excSpec?p.u.enums.excSpec->clone():0;
      u.enums.eis2=p.u.enums.eis2?p.u.enums.eis2->clone():0;
      // no break
    case T_ENUM_T:
      u.enums.eis=p.u.enums.eis->clone();
      u.enums.eis_by_name=0;
      break;
    case T_CHOICE_T:
    case T_SEQ_T:
    case T_SET_T:
    case T_ANYTYPE:
      u.secho.cfm=p.u.secho.cfm->clone();
      u.secho.field_by_name = 0;
      u.secho.component_internal = false;
      u.secho.has_single_charenc = false;
      break;
    case T_SEQ_A:
    case T_SET_A:
      u.secho.tr_compsof_ready=p.u.secho.tr_compsof_ready;
      // no break
    case T_CHOICE_A:
      u.secho.cfm = 0;
      u.secho.block=p.u.secho.block?p.u.secho.block->clone():0;
      u.secho.ctss=p.u.secho.ctss?p.u.secho.ctss->clone():0;
      u.secho.field_by_name = 0;
      u.secho.component_internal = false;
      u.secho.has_single_charenc = false;
      break;
    case T_SEQOF:
    case T_SETOF:
      u.seof.ofType=p.u.seof.ofType->clone();
      u.seof.component_internal = false;
      break;
    case T_REFD:
      u.ref.ref=p.u.ref.ref->clone();
      u.ref.type_refd=0;
      u.ref.component_internal = false;
      break;
    case T_OCFT:
      u.ref.oc_defn=p.u.ref.oc_defn;
      u.ref.oc_fieldname=p.u.ref.oc_fieldname;
      // no break
    case T_REFDSPEC:
      u.ref.type_refd=p.u.ref.type_refd;
      u.ref.component_internal = false;
      break;
    case T_SELTYPE:
      u.seltype.id=p.u.seltype.id->clone();
      u.seltype.type=p.u.seltype.type->clone();
      u.seltype.type_refd=0;
      break;
    case T_OPENTYPE:
      u.secho.cfm=new CompFieldMap();
      u.secho.cfm->set_my_type(this);
      u.secho.oc_defn=p.u.secho.oc_defn;
      u.secho.oc_fieldname=p.u.secho.oc_fieldname;
      u.secho.my_tableconstraint=0;
      u.secho.field_by_name = 0;
      u.secho.component_internal = false;
      u.secho.has_single_charenc = false;
      break;
    case T_ARRAY:
      u.array.element_type=p.u.array.element_type->clone();
      u.array.dimension = p.u.array.dimension->clone();
      u.array.in_typedef = p.u.array.in_typedef;
      u.array.component_internal = false;
      break;
    case T_PORT:
      u.port = p.u.port->clone();
      break;
    case T_COMPONENT:
      u.component = p.u.component->clone();
      break;
    case T_ADDRESS:
      u.address = 0;
      break;
    case T_SIGNATURE:
      u.signature.parameters = p.u.signature.parameters ?
        p.u.signature.parameters->clone() : 0;
      u.signature.return_type = p.u.signature.return_type ?
        p.u.signature.return_type->clone() : 0;
      u.signature.no_block = p.u.signature.no_block;
      u.signature.exceptions = p.u.signature.exceptions ?
        p.u.signature.exceptions->clone() : 0;
      u.signature.component_internal = false;
      break;
    case T_FUNCTION:
    case T_ALTSTEP:
      u.fatref.fp_list = p.u.fatref.fp_list->clone();
      u.fatref.runs_on.ref = p.u.fatref.runs_on.ref ?
        p.u.fatref.runs_on.ref->clone() : 0;
      u.fatref.runs_on.self = p.u.fatref.runs_on.self;
      u.fatref.runs_on.type = 0;
      u.fatref.return_type = p.u.fatref.return_type ?
        p.u.fatref.return_type->clone() : 0;
      u.fatref.is_startable = false;
      u.fatref.returns_template = p.u.fatref.returns_template;
      u.fatref.template_restriction = p.u.fatref.template_restriction;
      break;
    case T_TESTCASE:
      u.fatref.fp_list = p.u.fatref.fp_list->clone();
      u.fatref.runs_on.ref = p.u.fatref.runs_on.ref ?
        p.u.fatref.runs_on.ref->clone() : 0;
      u.fatref.runs_on.self = false;
      u.fatref.runs_on.type = 0;
      u.fatref.system.ref = p.u.fatref.system.ref ?
          p.u.fatref.system.ref->clone() : 0;
      u.fatref.system.type = 0;
      u.fatref.is_startable = false;
      u.fatref.returns_template = false;
      u.fatref.template_restriction = TR_NONE;
      break;
589 590 591
    case T_CLASS:
      u.class_ = p.u.class_->clone();
      break;
Elemer Lelik's avatar
Elemer Lelik committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605
    default:
      FATAL_ERROR("Type::Type()");
    } // switch
  }

  void Type::init()
  {
    tags_checked = false;
    tbl_cons_checked = false;
    text_checked = false;
    json_checked = false;
    raw_parsed  = false;
    raw_checked = false;
    xer_checked = false;
606
    variants_checked = false;
607
    coding_attribs_checked = false;
Elemer Lelik's avatar
Elemer Lelik committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
    raw_length_calculated = false;
    has_opentypes = false;
    opentype_outermost = false;
    code_generated = false;
    embed_values_possible = false;
    use_nil_possible = false;
    use_order_possible = false;
    raw_length = -1;
    parent_type = 0;
    tags = 0;
    constraints = 0;
    w_attrib_path = 0;
    encode_attrib_path = 0;
    rawattrib = 0;
    textattrib = 0;
    xerattrib = 0;
    berattrib = 0;
    jsonattrib = 0;
626
    oerattrib = 0;
Elemer Lelik's avatar
Elemer Lelik committed
627 628 629 630 631
    sub_type = 0;
    parsed_restr = 0;
    ownertype = OT_UNKNOWN;
    owner = 0;
    chk_finished = false;
Elemer Lelik's avatar
Elemer Lelik committed
632
    pard_type_instance = false;
633
    needs_any_from_done = false;
634 635
    default_encoding.type = CODING_UNSET;
    default_decoding.type = CODING_UNSET;
636
    checked_incorrect_field = false;
637
    encode_attrib_mod_conflict = false;
Elemer Lelik's avatar
Elemer Lelik committed
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
  }

  void Type::clean_up()
  {
    switch (typetype) {
    case T_ERROR:
    case T_NULL:
    case T_BOOL:
    case T_INT:
    case T_REAL:
    case T_BSTR:
    case T_HSTR:
    case T_OSTR:
    case T_CSTR:
    case T_USTR:
    case T_UTF8STRING:
    case T_NUMERICSTRING:
    case T_PRINTABLESTRING:
    case T_TELETEXSTRING:
    case T_VIDEOTEXSTRING:
    case T_IA5STRING:
    case T_GRAPHICSTRING:
    case T_VISIBLESTRING:
    case T_GENERALSTRING:
    case T_UNIVERSALSTRING:
    case T_BMPSTRING:
    case T_UTCTIME:
    case T_GENERALIZEDTIME:
    case T_OBJECTDESCRIPTOR:
    case T_OID:
    case T_ROID:
    case T_ANY:
    case T_EXTERNAL:
    case T_EMBEDDED_PDV:
    case T_UNRESTRICTEDSTRING:
    case T_REFDSPEC:
    case T_OCFT:
    case T_VERDICT:
    case T_ADDRESS:
    case T_DEFAULT:
      break;
    case T_INT_A:
    case T_BSTR_A:
      delete u.namednums.block;
      delete u.namednums.nvs;
      break;
    case T_ENUM_A:
      delete u.enums.block;
      if(u.enums.eis1) {
        u.enums.eis1->release_eis();
        delete u.enums.eis1;
      }
      if(u.enums.eis2) {
        u.enums.eis2->release_eis();
        delete u.enums.eis2;
      }
      /* no break */
    case T_ENUM_T:
      delete u.enums.eis;
      if (u.enums.eis_by_name) {
        for (size_t a = 0; a < u.enums.eis_by_name->size(); a++) {
          delete u.enums.eis_by_name->get_nth_elem(a);
        }
        u.enums.eis_by_name->clear();
        delete u.enums.eis_by_name;
      }
      break;
    case T_CHOICE_A:
    case T_SEQ_A:
    case T_SET_A:
      delete u.secho.block;
      delete u.secho.ctss;
      /* no break */
    case T_ANYTYPE:
    case T_CHOICE_T:
    case T_SEQ_T:
    case T_SET_T:
    case T_OPENTYPE:
      delete u.secho.cfm;
      if (u.secho.field_by_name) {
        for(size_t a = 0; a < u.secho.field_by_name->size(); a++) {
          delete u.secho.field_by_name->get_nth_elem(a);
        }
        u.secho.field_by_name->clear();
        delete u.secho.field_by_name;
      }
      break;
    case T_SEQOF:
    case T_SETOF:
      delete u.seof.ofType;
      break;
    case T_REFD:
      delete u.ref.ref;
      break;
    case T_SELTYPE:
      delete u.seltype.id;
      delete u.seltype.type;
      break;
    case T_ARRAY:
      delete u.array.element_type;
      delete u.array.dimension;
      break;
    case T_PORT:
      delete u.port;
      break;
    case T_COMPONENT:
      delete u.component;
      break;
    case T_SIGNATURE:
      delete u.signature.parameters;
      delete u.signature.return_type;
      delete u.signature.exceptions;
      break;
    case T_FUNCTION:
    case T_ALTSTEP:
      delete u.fatref.fp_list;
      delete u.fatref.runs_on.ref;
      delete u.fatref.return_type;
      break;
    case T_TESTCASE:
      delete u.fatref.fp_list;
      delete u.fatref.runs_on.ref;
      delete u.fatref.system.ref;
      break;
762 763 764
    case T_CLASS:
      delete u.class_;
      break;
Elemer Lelik's avatar
Elemer Lelik committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
    default:
      FATAL_ERROR("Type::clean_up()");
    } // switch
    typetype = T_ERROR;
    delete tags;
    tags = 0;
    delete constraints;
    constraints = 0;
    delete rawattrib;
    rawattrib = 0;
    delete textattrib;
    textattrib = 0;
    delete xerattrib;
    xerattrib = 0;
    delete sub_type;
    sub_type = 0;
    delete berattrib;
    berattrib = 0;
    delete jsonattrib;
    jsonattrib = 0;
785 786
    delete oerattrib;
    oerattrib = 0;
Elemer Lelik's avatar
Elemer Lelik committed
787 788 789 790 791 792 793 794 795 796 797
    if (parsed_restr) {
      for (size_t i = 0; i < parsed_restr->size(); i++)
        delete (*parsed_restr)[i];
      parsed_restr->clear();
      delete parsed_restr;
      parsed_restr = 0;
    }
    delete w_attrib_path;
    w_attrib_path = 0;
    delete encode_attrib_path;
    encode_attrib_path = 0;
798 799 800
    for (size_t i = 0; i < coding_table.size(); ++i) {
      if (!coding_table[i]->built_in) {
        Free(coding_table[i]->custom_coding.name);
801 802 803 804 805 806 807 808 809 810
        for (size_t j = 0; j < coding_table[i]->custom_coding.encoders->size(); ++j) {
          delete coding_table[i]->custom_coding.encoders->get_nth_elem(j);
        }
        coding_table[i]->custom_coding.encoders->clear();
        delete coding_table[i]->custom_coding.encoders;
        for (size_t j = 0; j < coding_table[i]->custom_coding.decoders->size(); ++j) {
          delete coding_table[i]->custom_coding.decoders->get_nth_elem(j);
        }
        coding_table[i]->custom_coding.decoders->clear();
        delete coding_table[i]->custom_coding.decoders;
811 812 813 814 815 816 817 818
      }
      delete coding_table[i];
    }
    coding_table.clear();
    for (size_t i = 0; i < coders_to_generate.size(); ++i) {
      delete coders_to_generate[i];
    }
    coders_to_generate.clear();
Elemer Lelik's avatar
Elemer Lelik committed
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
  }

  Type::Type(typetype_t p_tt)
    : Governor(S_T), typetype(p_tt)
  {
    init();
    switch(p_tt) {
    case T_ERROR:
    case T_NULL:
    case T_BOOL:
    case T_INT:
    case T_REAL:
    case T_BSTR:
    case T_HSTR:
    case T_OSTR:
    case T_CSTR:
    case T_USTR:
    case T_UTF8STRING:
    case T_NUMERICSTRING:
    case T_PRINTABLESTRING:
    case T_TELETEXSTRING:
    case T_VIDEOTEXSTRING:
    case T_IA5STRING:
    case T_GRAPHICSTRING:
    case T_VISIBLESTRING:
    case T_GENERALSTRING:
    case T_UNIVERSALSTRING:
    case T_BMPSTRING:
    case T_UTCTIME:
    case T_GENERALIZEDTIME:
    case T_OBJECTDESCRIPTOR:
    case T_OID:
    case T_ROID:
    case T_ANY:
    case T_EXTERNAL:
    case T_EMBEDDED_PDV:
    case T_UNRESTRICTEDSTRING:
    case T_VERDICT:
    case T_DEFAULT:
      break;
    case T_ANYTYPE: {
      u.secho.cfm = new CompFieldMap;
      u.secho.cfm->set_my_type(this);
      u.secho.block=0;
      u.secho.ctss=0;
      u.secho.field_by_name = 0;
      u.secho.component_internal = false;
      u.secho.has_single_charenc = false;
      break; }
    case T_INT_A:
    case T_BSTR_A:
      u.namednums.block=0;
      u.namednums.nvs=0;
      break;
    case T_ADDRESS:
      u.address = 0;
      break;
876 877 878
    case T_CLASS:
      u.class_ = new Ttcn::ClassTypeBody();
      break;
Elemer Lelik's avatar
Elemer Lelik committed
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
    default:
      FATAL_ERROR("Type::Type()");
    } // switch
  }

  Type::Type(typetype_t p_tt, EnumItems *p_eis)
    : Governor(S_T), typetype(p_tt)
  {
    if (p_tt != T_ENUM_T || !p_eis) FATAL_ERROR("Type::Type()");
    init();
    u.enums.eis=p_eis;
    u.enums.eis_by_name=0;
  }

  Type::Type(typetype_t p_tt, Block *p_block)
    : Governor(S_T), typetype(p_tt)
  {
    if (!p_block) FATAL_ERROR("NULL parameter");
    init();
    switch(p_tt) {
    case T_INT_A:
    case T_BSTR_A:
      u.namednums.block=p_block;
      u.namednums.nvs=0;
      break;
    case T_ENUM_A:
      u.enums.eis=new EnumItems();
      u.enums.block=p_block;
      u.enums.eis1=0;
      u.enums.ellipsis=false;
      u.enums.excSpec=0;
      u.enums.eis2=0;
      u.enums.eis_by_name=0;
      break;
    case T_SEQ_A:
    case T_SET_A:
      u.secho.tr_compsof_ready=false;
      // no break
    case T_CHOICE_A:
      u.secho.cfm = 0;
      u.secho.block=p_block;
      u.secho.ctss=0;
      u.secho.field_by_name = 0;
      u.secho.component_internal = false;
      u.secho.has_single_charenc = false;
      break;
    default:
      FATAL_ERROR("Type::Type()");
    } // switch
  }

  Type::Type(typetype_t p_tt,
             EnumItems *p_eis1, bool p_ellipsis, EnumItems *p_eis2)
    : Governor(S_T), typetype(p_tt)
  {
    if (p_tt != T_ENUM_A || !p_eis1) FATAL_ERROR("Type::Type()");
    init();
    u.enums.eis=new EnumItems();
    u.enums.block=0;
    u.enums.eis1=p_eis1;
    u.enums.ellipsis=p_ellipsis;
    u.enums.eis2=p_eis2;
    u.enums.eis_by_name=0;
  }

  Type::Type(typetype_t p_tt, CompFieldMap *p_cfm)
    : Governor(S_T), typetype(p_tt)
  {
    if (!p_cfm) FATAL_ERROR("NULL parameter");
    init();
    switch (p_tt) {
    case T_CHOICE_T:
    case T_SEQ_T:
    case T_SET_T:
      u.secho.cfm=p_cfm;
      u.secho.field_by_name = 0;
      u.secho.component_internal = false;
      u.secho.has_single_charenc = false;
      break;
    default:
      FATAL_ERROR("Type::Type()");
    } // switch
  }

  Type::Type(typetype_t p_tt, Type *p_type)
    : Governor(S_T), typetype(p_tt)
  {
    if (!p_type) FATAL_ERROR("NULL parameter");
    init();
    switch (p_tt) {
    case T_SEQOF:
    case T_SETOF:
      u.seof.ofType=p_type;
      u.seof.ofType->set_ownertype(OT_RECORD_OF, this);
      u.seof.component_internal = false;
      break;
    case T_REFDSPEC:
      u.ref.type_refd=p_type;
      u.ref.type_refd->set_ownertype(OT_REF_SPEC, this);
      u.ref.component_internal = false;
      break;
    default:
      FATAL_ERROR("Type::Type()");
    } // switch
  }

  Type::Type(typetype_t p_tt, Identifier *p_id, Type *p_type)
    : Governor(S_T), typetype(p_tt)
  {
    if (p_tt != T_SELTYPE || !p_id || !p_type) FATAL_ERROR("Type::Type()");
    init();
    u.seltype.id=p_id;
    u.seltype.type=p_type;
    u.seltype.type->set_ownertype(OT_SELTYPE, this);
    u.seltype.type_refd=0;
  }

  Type::Type(typetype_t p_tt, Type *p_type, Ttcn::ArrayDimension *p_dim,
             bool p_in_typedef)
    : Governor(S_T), typetype(p_tt)
  {
    if (p_tt != T_ARRAY || !p_type || !p_dim) FATAL_ERROR("Type::Type()");
    init();
    u.array.element_type = p_type;
    u.array.element_type->set_ownertype(OT_ARRAY, this);
    u.array.dimension = p_dim;
    u.array.in_typedef = p_in_typedef;
    u.array.component_internal = false;
  }

  Type::Type(typetype_t p_tt, Type *p_type, OC_defn *p_oc_defn,
             const Identifier *p_id)
    : Governor(S_T), typetype(p_tt)
  {
    if (p_tt != T_OCFT || !p_type ||!p_oc_defn || !p_id)
      FATAL_ERROR("Type::Type()");
    init();
    u.ref.type_refd=p_type;
    u.ref.type_refd->set_ownertype(OT_OCFT, this);
    u.ref.oc_defn=p_oc_defn;
    u.ref.oc_fieldname=p_id;
    u.ref.component_internal = false;
  }

  Type::Type(typetype_t p_tt, OC_defn *p_oc_defn,
             const Identifier *p_id)
    : Governor(S_T), typetype(p_tt)
  {
    if (p_tt != T_OPENTYPE || !p_oc_defn || !p_id) FATAL_ERROR("Type::Type()");
    init();
    has_opentypes=true;
    u.secho.cfm=new CompFieldMap();
    u.secho.cfm->set_my_type(this);
    u.secho.oc_defn=p_oc_defn;
    u.secho.oc_fieldname=p_id;
    u.secho.my_tableconstraint=0;
    u.secho.field_by_name = 0;
    u.secho.component_internal = false;
    u.secho.has_single_charenc = false;
  }

  Type::Type(typetype_t p_tt, Reference *p_ref)
    : Governor(S_T), typetype(p_tt)
  {
    if (p_tt != T_REFD || !p_ref) FATAL_ERROR("Type::Type()");
    init();
    u.ref.ref=p_ref;
    u.ref.type_refd=0;
    u.ref.component_internal = false;
  }

  Type::Type(typetype_t p_tt, Ttcn::PortTypeBody *p_pb)
    : Governor(S_T), typetype(p_tt)
  {
    if (p_tt != T_PORT || !p_pb) FATAL_ERROR("Type::Type()");
    init();
    u.port = p_pb;
    p_pb->set_my_type(this);
  }

  Type::Type(typetype_t p_tt, ComponentTypeBody *p_cb)
    : Governor(S_T), typetype(p_tt)
  {
    if (p_tt != T_COMPONENT || !p_cb) FATAL_ERROR("Type::Type()");
    init();
    u.component = p_cb;
    p_cb->set_my_type(this);
  }

  Type::Type(typetype_t p_tt, SignatureParamList *p_params, Type *p_returntype,
             bool p_noblock, SignatureExceptions *p_exceptions)
    : Governor(S_T), typetype(p_tt)
  {
    if (p_tt != T_SIGNATURE || (p_returntype && p_noblock))
      FATAL_ERROR("Type::Type()");
    init();
    u.signature.parameters = p_params;
    if ((u.signature.return_type = p_returntype)) { // check assignment for 0
      u.signature.return_type->set_ownertype(OT_SIGNATURE, this);
    }
    u.signature.no_block = p_noblock;
    u.signature.exceptions = p_exceptions;
    u.signature.component_internal = false;
  }

  Type::Type(typetype_t p_tt,Ttcn::FormalParList *p_params,
        Ttcn::Reference* p_runs_on_ref, bool p_runs_on_self,
        Type *p_returntype, bool p_returns_template,
        template_restriction_t p_template_restriction)
    : Governor(S_T), typetype(p_tt)
  {
    if (p_tt != T_FUNCTION || !p_params || (!p_returntype && p_returns_template)
        || (p_runs_on_ref && p_runs_on_self)) FATAL_ERROR("Type::Type()");
    init();
    u.fatref.fp_list = p_params;
    u.fatref.runs_on.ref = p_runs_on_ref;
    u.fatref.runs_on.self = p_runs_on_self;
    u.fatref.runs_on.type = 0;
    if ((u.fatref.return_type = p_returntype)) { // check assignment for 0
      u.fatref.return_type->set_ownertype(OT_FUNCTION, this);
    }
    u.fatref.is_startable = false;
    u.fatref.returns_template = p_returns_template;
    u.fatref.template_restriction = p_template_restriction;
  }

  Type::Type(typetype_t p_tt,Ttcn::FormalParList *p_params,
        Ttcn::Reference* p_runs_on_ref, bool p_runs_on_self)
    : Governor(S_T), typetype(p_tt)
  {
    if(p_tt != T_ALTSTEP || !p_params || (p_runs_on_ref && p_runs_on_self))
      FATAL_ERROR("Type::Type()");
    init();
    u.fatref.fp_list = p_params;
    u.fatref.runs_on.ref = p_runs_on_ref;
    u.fatref.runs_on.self = p_runs_on_self;
    u.fatref.runs_on.type = 0;
    u.fatref.return_type = 0;
    u.fatref.is_startable = false;
    u.fatref.returns_template = false;
    u.fatref.template_restriction = TR_NONE;
  }

  Type::Type(typetype_t p_tt,Ttcn::FormalParList *p_params,
        Ttcn::Reference* p_runs_on_ref, Ttcn::Reference *p_system_ref)
    : Governor(S_T), typetype(p_tt)
  {
    if(p_tt != T_TESTCASE || !p_params || !p_runs_on_ref)
      FATAL_ERROR("Type::Type()");
    init();
    u.fatref.fp_list = p_params;
    u.fatref.runs_on.ref = p_runs_on_ref;
    u.fatref.runs_on.self = false;
    u.fatref.runs_on.type = 0;
    u.fatref.system.ref = p_system_ref;
    u.fatref.system.type = 0;
    u.fatref.is_startable = false;
    u.fatref.returns_template = false;
    u.fatref.template_restriction = TR_NONE;
  }
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
  
  Type::Type(typetype_t p_tt, Ttcn::ClassTypeBody* p_class)
  : Governor(S_T), typetype(p_tt)
  {
    init();
    switch (typetype) {
    case T_CLASS:
      if (p_class == NULL) {
        FATAL_ERROR("Type::Type");
      }
      u.class_ = p_class;
      break;
    default:
      FATAL_ERROR("Type::Type");
    }
  }
  
Elemer Lelik's avatar
Elemer Lelik committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
  Type::~Type()
  {
    clean_up();
  }

  void Type::free_pools()
  {
    destroy_default_tags();// Additionally: R&S license warning
    destroy_pooltypes();// Additionally: R&S license checkin/disconnect/shutdown
  }

  Type *Type::clone() const
  {
    return new Type(*this);
  }

  Type::typetype_t Type::get_typetype_ttcn3(typetype_t p_tt)
  {
    switch (p_tt) {
    case T_INT_A:
      return T_INT;
    case T_ENUM_A:
      return T_ENUM_T;
    case T_BSTR_A:
      return T_BSTR;
    case T_UTF8STRING:
    case T_BMPSTRING:
    case T_UNIVERSALSTRING:
      return T_USTR;
    case T_TELETEXSTRING:
    case T_VIDEOTEXSTRING:
    case T_GRAPHICSTRING:
    case T_OBJECTDESCRIPTOR:
    case T_GENERALSTRING:
      // iso2022str
    case T_NUMERICSTRING:
    case T_PRINTABLESTRING:
    case T_IA5STRING:
    case T_VISIBLESTRING:
    case T_UTCTIME:
    case T_GENERALIZEDTIME:
      return T_CSTR;
    case T_ROID:
      return T_OID;
    case T_ANY:
      return T_OSTR;
    case T_CHOICE_A:
    case T_OPENTYPE:
      return T_CHOICE_T;
    case T_SEQ_A:
    case T_EXTERNAL:
    case T_EMBEDDED_PDV:
    case T_UNRESTRICTEDSTRING:
      return T_SEQ_T;
    case T_SET_A:
      return T_SET_T;
    default:
      return p_tt;
    } // switch typetype
  }

  bool Type::is_asn1() const
  {
    if (my_scope) return Setting::is_asn1();
    // the type might be a pool type, which is considered to be a TTCN-3 type
    typetype_t t_typetype = get_typetype_ttcn3(typetype);
    if (pooltypes && pooltypes->has_key(t_typetype) &&
        (*pooltypes)[t_typetype] == this) return false;
    else FATAL_ERROR("Type::is_asn1()");
  }

  bool Type::is_ref() const
  {
    switch(typetype) {
    case T_UNRESTRICTEDSTRING:
    case T_OCFT:
    case T_EXTERNAL:
    case T_EMBEDDED_PDV:
    case T_REFD:
    case T_REFDSPEC:
    case T_SELTYPE:
    case T_ADDRESS:
      return true;
    default:
      return false;
    } // switch
  }

  bool Type::is_secho() const
  {
    switch(typetype) {
    case T_ANYTYPE:
    case T_CHOICE_A:
    case T_CHOICE_T:
    case T_OPENTYPE:
    case T_SEQ_A:
    case T_SEQ_T:
    case T_SET_A:
    case T_SET_T:
      return true;
    default:
      return false;
    } // switch
  }

  Type::truth Type::is_charenc()
  {
1263 1264 1265 1266 1267
    // this helps avoid infinite recursions in self-referencing types
    if (RecursionTracker::is_happening(this)) {
      return Maybe;
    }
    RecursionTracker tracker(this);
Elemer Lelik's avatar
Elemer Lelik committed
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
    switch(typetype) {
    case T_CHOICE_A:
    case T_CHOICE_T:
    {
      bool possible = true;
      size_t ncomp = u.secho.cfm->get_nof_comps();
      for (size_t i=0; i<ncomp; ++i) {
        CompField * cf = u.secho.cfm->get_comp_byIndex(i);
        if (cf->get_type()->is_charenc() == No) {
          possible = false; break;
        }
      } // next i
      if (possible) {
        return (xerattrib && (xerattrib->useUnion_ || xerattrib->useType_)) ? Yes : No;
      }
    }
    // no break
    case T_SEQ_A:
    case T_SEQ_T:
    case T_SET_A:
    case T_SET_T:
    case T_OPENTYPE:
      // UNTAGGED cannot be used to make a type character-encodable!
      // But USE-QNAME can!
      return (xerattrib && xerattrib->useQName_) ? Yes : No;

    case T_SEQOF: // A record-of is character-encodable if it has the "list"
    case T_SETOF: // attribute and its element is character-encodable.
      return (xerattrib && xerattrib->list_ && (u.seof.ofType->is_charenc()==Yes))
      ? Yes : No;

    case T_ENUM_A:
    case T_ENUM_T:
    case T_VERDICT:
      return Yes;

    default:
      if (is_ref()) {
        truth retval = get_type_refd_last()->is_charenc();
        if (retval == Yes) return Yes;
        else if (retval == Maybe) {
          if (xerattrib && xerattrib->useUnion_) return Yes;
        }
        // else fall through to No
      }
      return No;

    case T_BSTR:
    case T_OSTR:
    case T_HSTR:
    case T_CSTR:
    case T_USTR:
    case T_UTF8STRING:
      // TODO ASN.1 restricted character string types when (if) ASN.1 gets XER
      // TODO check subtype; strings must be restricted to not contain
      // control characters (0..0x1F except 9,0x0A,0x0D)
    case T_INT_A:
    case T_INT:
    case T_BOOL:
    case T_REAL:
      // TODO more types
      /* FIXME : this kind of check should be applied to elements of secho,
       *  not to the type of the element ! */
      return Yes;
    }
  }

  bool Type::has_empty_xml() {
    bool answer = false;
    switch (typetype) {
    case T_SEQ_A: case T_SEQ_T:
    case T_SET_A: case T_SET_T: {
      answer = true; // If all components are optional.
      size_t n_comps = get_nof_comps();
      for (size_t i = 0; i < n_comps; ++i) {
        CompField* cf = get_comp_byIndex(i);
        if (!cf->get_is_optional()) {
          answer = false;
          break; // the loop
        }
      }
      break; }
    case T_SEQOF: case T_SETOF:
      // _If_ there is a length restriction, 0 length must be allowed.
      // By this time parsed_restr has been absorbed into sub_type.
      answer = (sub_type==0) || sub_type->zero_length_allowed();
      break;
    default:
      break;
    } // switch
    return answer;
  }

  void Type::set_fullname(const string& p_fullname)
  {
    Governor::set_fullname(p_fullname);
    switch(typetype) {
    case T_INT_A:
    case T_BSTR_A:
      if(u.namednums.block) u.namednums.block->set_fullname(p_fullname);
      if(u.namednums.nvs)
        u.namednums.nvs->set_fullname(p_fullname+".<namedvalues>");
      break;
    case T_ENUM_A:
      if(u.enums.eis1) u.enums.eis1->set_fullname(p_fullname);
      if(u.enums.eis2) u.enums.eis2->set_fullname(p_fullname);
      // no break
    case T_ENUM_T:
      u.enums.eis->set_fullname(p_fullname);
      break;
    case T_ANYTYPE:
    case T_CHOICE_T:
    case T_SEQ_T:
    case T_SET_T:
    case T_OPENTYPE:
      u.secho.cfm->set_fullname(p_fullname);
      break;
    case T_SEQ_A:
    case T_SET_A:
    case T_CHOICE_A:
      if (u.secho.ctss) u.secho.ctss->set_fullname(p_fullname);
      break;
    case T_SEQOF:
    case T_SETOF: {
      string subtypename(".<oftype>");
      Type * t = u.seof.ofType;
      /* Do NOT call get_type_refd_last() or else fatal_error !
       * The AST is not fully set up. */

      /* XER will use these strings */
      switch (t->typetype)
      {
      case T_EMBEDDED_PDV: case T_EXTERNAL:
      case T_SEQ_A: case T_SEQ_T:
        subtypename = ".SEQUENCE";
        break;

      case T_SET_A: case T_SET_T:
        subtypename = ".SET";
        break;

      case T_SEQOF:
        subtypename = ".SEQUENCE_OF";
        break;

      case T_SETOF:
        subtypename = ".SET_OF";
        break;

      case T_BSTR_A:
        subtypename = ".BITSTRING";
        break;

      case T_BOOL:
        subtypename = ".BOOLEAN";
        break;

      case T_CHOICE_A: case T_CHOICE_T:
        subtypename = ".CHOICE";
        break;

      case T_ENUM_A: case T_ENUM_T:
        subtypename = ".ENUMERATED";
        break;

      case T_INT_A: case T_INT:
        subtypename = ".INTEGER";
        break;

      default:
        break;
      }
      u.seof.ofType->set_fullname(p_fullname+subtypename);
      break; }
    case T_REFD:
      u.ref.ref->set_fullname(p_fullname);
      break;
    case T_SELTYPE:
      u.seltype.type->set_fullname(p_fullname+".<selection>");
      break;
    case T_PORT:
      u.port->set_fullname(p_fullname);
      break;
    case T_COMPONENT:
      u.component->set_fullname(p_fullname);
      break;
    case T_ARRAY:
      u.array.element_type->set_fullname(p_fullname + ".<element_type>");
      u.array.dimension->set_fullname(p_fullname + ".<dimension>");
      break;
    case T_SIGNATURE:
      if (u.signature.parameters)
        u.signature.parameters->set_fullname(p_fullname);
      if (u.signature.return_type)
        u.signature.return_type->set_fullname(p_fullname + ".<return_type>");
      if (u.signature.exceptions)
        u.signature.exceptions->set_fullname(p_fullname + ".<exception_list>");
      break;
    case T_FUNCTION:
    case T_ALTSTEP:
      u.fatref.fp_list->set_fullname(p_fullname + "<formal_par_list>");
      if (u.fatref.runs_on.ref)
        u.fatref.runs_on.ref->set_fullname(p_fullname + "<runs_on_type>");
      if (u.fatref.return_type)
        u.fatref.return_type->set_fullname(p_fullname + "<return type>");
      break;
    case T_TESTCASE:
      u.fatref.fp_list->set_fullname(p_fullname + ".<formal_par_list>");
      if (u.fatref.runs_on.ref)
        u.fatref.runs_on.ref->set_fullname(p_fullname+".<runs_on_type>");
      if (u.fatref.system.ref)
        u.fatref.system.ref->set_fullname(p_fullname + ".<system_type>");
      break;
1481 1482 1483
    case T_CLASS:
      u.class_->set_fullname(p_fullname);
      break;
Elemer Lelik's avatar
Elemer Lelik committed
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
    default:
      break;
    } // switch
  }

  void Type::set_my_scope(Scope *p_scope)
  {
    Governor::set_my_scope(p_scope);
    if(tags) tags->set_my_scope(p_scope);
    switch(typetype) {
    case T_INT_A:
    case T_BSTR_A:
      if(u.namednums.nvs) u.namednums.nvs->set_my_scope(p_scope);
      break;
    case T_ENUM_A:
      if(u.enums.eis1) u.enums.eis1->set_my_scope(p_scope);
      if(u.enums.eis2) u.enums.eis2->set_my_scope(p_scope);
      // no break
    case T_ENUM_T:
      u.enums.eis->set_my_scope(p_scope);
      break;
    case T_CHOICE_T:
    case T_SEQ_T:
    case T_SET_T:
    case T_OPENTYPE:
    case T_ANYTYPE:
      u.secho.cfm->set_my_scope(p_scope);
      break;
    case T_SEQ_A:
    case T_SET_A:
    case T_CHOICE_A:
      if(u.secho.ctss) u.secho.ctss->set_my_scope(p_scope);
      break;
    case T_SEQOF:
    case T_SETOF:
      u.seof.ofType->set_my_scope(p_scope);
      break;
    case T_REFD:
      u.ref.ref->set_my_scope(p_scope);
      break;
    case T_SELTYPE:
      u.seltype.type->set_my_scope(p_scope);
      break;
    case T_ARRAY:
      u.array.element_type->set_my_scope(p_scope);
      u.array.dimension->set_my_scope(p_scope);
      break;
    case T_PORT:
      u.port->set_my_scope(p_scope);
      break;
    case T_SIGNATURE:
      if (u.signature.parameters)
        u.signature.parameters->set_my_scope(p_scope);
      if (u.signature.return_type)
        u.signature.return_type->set_my_scope(p_scope);
      if (u.signature.exceptions)
        u.signature.exceptions->set_my_scope(p_scope);
      break;
    case T_COMPONENT:
      u.component->set_my_scope(p_scope);
      break;
    case T_FUNCTION:
    case T_ALTSTEP:
      // the scope of parameter list is set later in chk_Fat()
      if (u.fatref.runs_on.ref)
        u.fatref.runs_on.ref->set_my_scope(p_scope);
      if (u.fatref.return_type)
        u.fatref.return_type->set_my_scope(p_scope);
      break;
    case T_TESTCASE:
      // the scope of parameter list is set later in chk_Fat()
      if (u.fatref.runs_on.ref)
        u.fatref.runs_on.ref->set_my_scope(p_scope);
      if (u.fatref.system.ref)
        u.fatref.system.ref->set_my_scope(p_scope);
      break;
1560 1561 1562
    case T_CLASS:
      u.class_->set_my_scope(p_scope);
      break;
Elemer Lelik's avatar
Elemer Lelik committed
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
    default:
      break;
    } // switch
  }

  Type* Type::get_type_refd(ReferenceChain *refch)
  {
    switch(typetype) {
    case T_REFD: {
      if(refch && !refch->add(get_fullname())) goto error;
      if(!u.ref.type_refd) {
        Assignment *ass = u.ref.ref->get_refd_assignment();
        if (!ass) goto error; // The referenced assignment is not found
        switch (ass->get_asstype()) {
        case Assignment::A_ERROR:
          goto error;
        case Assignment::A_TYPE:
        case Assignment::A_VS:
          u.ref.type_refd = ass->get_Type()->get_field_type(
            u.ref.ref->get_subrefs(), EXPECTED_DYNAMIC_VALUE, refch);
          if (!u.ref.type_refd) goto error;
          break;
          //case Assignment::A_VS:
          //u.ref.type_refd = ass->get_Type();
          //      if(!u.ref.type_refd) goto error;
          //break;
        case Assignment::A_OC:
        case Assignment::A_OBJECT:
        case Assignment::A_OS: {
          Setting *setting = u.ref.ref->get_refd_setting();
          if (!setting || setting->get_st() == Setting::S_ERROR) goto error;
          /* valueset? */
          u.ref.type_refd = dynamic_cast<Type*>(setting);
          if(!u.ref.type_refd) {
            error("`%s' is not a reference to a type",
              u.ref.ref->get_dispname().c_str());
            goto error;
          }

          if (u.ref.type_refd->ownertype == OT_UNKNOWN) {
            u.ref.type_refd->set_ownertype(OT_REF, this);
          }

          break;}
        default:
          error("`%s' is not a reference to a type",
            u.ref.ref->get_dispname().c_str());
          goto error;
        } // switch
        if(!u.ref.type_refd->get_my_scope()) {
          // opentype or OCFT
          u.ref.type_refd->set_my_scope(get_my_scope());
          u.ref.type_refd->set_parent_type(get_parent_type());
          u.ref.type_refd->set_genname(get_genname_own(), string("type"));
          u.ref.type_refd->set_fullname(get_fullname()+".type");
        }
        if (u.ref.type_refd->typetype == T_OPENTYPE && !constraints)
          warning("An open type without table constraint is useless in TTCN-3");
      }
      return u.ref.type_refd;
      break;}
    case T_SELTYPE: {
      if(refch && !refch->add(get_fullname())) goto error;
      if(!u.seltype.type_refd) {
        Type *t=u.seltype.type->get_type_refd_last(refch);
        if(t->typetype==T_ERROR) goto error;
        if(t->typetype!=T_CHOICE_A) {
          error("(Reference to) a CHOICE type was expected"
            " in selection type.");
          goto error;
        }
        if(!t->has_comp_withName(*u.seltype.id)) {
          error("No alternative with name `%s' in the given type `%s'.",
            u.seltype.id->get_dispname().c_str(),
            t->get_fullname().c_str());
          goto error;
        }
        u.seltype.type_refd=t->get_comp_byName(*u.seltype.id)->get_type();
      }
      return u.seltype.type_refd;
      break;}
    case T_REFDSPEC:
    case T_OCFT:
      if(refch && !refch->add(get_fullname())) goto error;
      return u.ref.type_refd;
      break;
    case T_EXTERNAL: {
      if (!my_scope) FATAL_ERROR("Type::get_type_refd()");
      Identifier t_id(Identifier::ID_ASN, string("EXTERNAL"));
      return my_scope->get_scope_asss()->get_local_ass_byId(t_id)->get_Type(); }
    case T_EMBEDDED_PDV: {
      if (!my_scope) FATAL_ERROR("Type::get_type_refd()");
      Identifier t_id(Identifier::ID_ASN, string("EMBEDDED PDV"));
      return my_scope->get_scope_asss()->get_local_ass_byId(t_id)->get_Type(); }
    case T_UNRESTRICTEDSTRING: {
      if (!my_scope) FATAL_ERROR("Type::get_type_refd()");
      Identifier t_id(Identifier::ID_ASN, string("CHARACTER STRING"));
      return my_scope->get_scope_asss()->get_local_ass_byId(t_id)->get_Type(); }
    case T_ADDRESS:
      if (refch && !refch->add(get_fullname())) goto error;
      if (u.address) return u.address;
      if (!my_scope) FATAL_ERROR("Type::get_type_refd()");
      u.address = my_scope->get_scope_mod()->get_address_type();
      if (!u.address) {
        error("Type `address' is not defined in this module");
        goto error;
      }
      return u.address;
    default:
      FATAL_ERROR("Type::get_type_refd()");
      return 0;
    } // switch
    error:
    clean_up();
    return this;
  }

  Type* Type::get_type_refd_last(ReferenceChain *refch)
  {
    Type *t=this;
    while(t->is_ref()) t=t->get_type_refd(refch);
    return t;
  }

  Type *Type::get_field_type(Ttcn::FieldOrArrayRefs *subrefs,
    expected_value_t expected_index, ReferenceChain *refch,
1689
    bool interrupt_if_optional, Assignment** last_method)
Elemer Lelik's avatar
Elemer Lelik committed
1690 1691
  {
    if (!subrefs) return this;
1692 1693
    bool silent = subrefs->is_checked();
    subrefs->set_checked();
Elemer Lelik's avatar
Elemer Lelik committed
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
    Type *t = this;
    if (expected_index == EXPECTED_TEMPLATE)
      expected_index = EXPECTED_DYNAMIC_VALUE;
    size_t nof_refs = subrefs->get_nof_refs();
    subrefs->clear_string_element_ref();
    for (size_t i = 0; i < nof_refs; i++) {
      if (refch) refch->mark_state();
      t = t->get_type_refd_last(refch);
      if (refch) refch->prev_state();
      // stop immediately if current type t is erroneous
      // (e.g. because of circular reference)
      if (t->typetype == T_ERROR) return 0;
      Ttcn::FieldOrArrayRef *ref = subrefs->get_ref(i);
      switch (ref->get_type()) {
      case Ttcn::FieldOrArrayRef::FIELD_REF: {
Elemer Lelik's avatar
Elemer Lelik committed
1709 1710 1711 1712
        if (t->typetype == T_OPENTYPE) {
          // allow the alternatives of open types as both lower and upper identifiers
          ref->set_field_name_to_lowercase();
        }
Elemer Lelik's avatar
Elemer Lelik committed
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
        const Identifier& id = *ref->get_id();
        switch (t->typetype) {
        case T_CHOICE_A:
        case T_CHOICE_T:
        case T_OPENTYPE:
        case T_SEQ_A:
        case T_SEQ_T:
        case T_SET_A:
        case T_SET_T:
        case T_ANYTYPE:
1723
        case T_CLASS:
Elemer Lelik's avatar
Elemer Lelik committed
1724
          break;
Elemer Lelik's avatar
Elemer Lelik committed
1725
	case T_COMPONENT:
1726 1727 1728
    if (!silent) {
      ref->error("Referencing fields of a component is not allowed");
    }
Elemer Lelik's avatar
Elemer Lelik committed
1729
	  return 0;
Elemer Lelik's avatar
Elemer Lelik committed
1730
        default:
1731 1732 1733 1734 1735
          if (!silent) {
            ref->error("Invalid field reference `%s': type `%s' "
              "does not have fields", id.get_dispname().c_str(),
              t->get_typename().c_str());
          }
Elemer Lelik's avatar
Elemer Lelik committed
1736 1737
          return 0;
        }
1738 1739 1740
        if (t->typetype == T_CLASS) {
          Ttcn::ClassTypeBody* class_ = t->get_class_type_body();
          if (!class_->has_local_ass_withId(id)) {
1741 1742 1743 1744
            if (!silent) {
              ref->error("Reference to non-existent member `%s' in class type `%s'",
                id.get_dispname().c_str(), t->get_typename().c_str());
            }
1745 1746 1747
            return 0;
          }
          Assignment* ass = class_->get_local_ass_byId(id);
1748 1749 1750 1751
          if (!class_->chk_visibility(ass, ref, subrefs->get_my_scope())) {
            // the member is not visible (the error has already been reported)
            return 0;
          }
1752 1753 1754 1755 1756 1757 1758
          switch (ass->get_asstype()) {
          case Assignment::A_VAR:
          case Assignment::A_VAR_TEMPLATE:
          case Assignment::A_CONST:
          case Assignment::A_TEMPLATE:
            t = ass->get_Type();
            break;
1759
          case Assignment::A_FUNCTION:
1760 1761
          case Assignment::A_FUNCTION_RVAL:
          case Assignment::A_FUNCTION_RTEMP: