Type_chk.cc 263 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
Lenard Nagy's avatar
Lenard Nagy committed
2
 * Copyright (c) 2000-2017 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
4
5
6
7
8
 * 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:
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *   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
 *   Szabo, Bence Janos
 *   Szalai, Gabor
 *   Tatarka, Gabor
 *   Zalanyi, Balazs Andor
Elemer Lelik's avatar
Elemer Lelik committed
29
30
 *
 ******************************************************************************/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "../common/dbgnew.hh"
#include "Type.hh"
#include "Typestuff.hh" // FIXME CTs
#include "CompField.hh"
#include "CompType.hh"
#include "TypeCompat.hh"
#include "EnumItem.hh"
#include "SigParam.hh"

#include "Valuestuff.hh"
#include "main.hh"

#include "ttcn3/Ttcnstuff.hh"
#include "ttcn3/TtcnTemplate.hh"
#include "ttcn3/Templatestuff.hh"
#include "ttcn3/Attributes.hh"
#include "ttcn3/ArrayDimensions.hh"
#include "ttcn3/PatternString.hh"

#include "asn1/Tag.hh"
#include "XerAttributes.hh"

#include <ctype.h>
#include <stdlib.h> // for qsort
#include <string.h>

extern int rawAST_debug;

namespace Common {

using Ttcn::MultiWithAttrib;
using Ttcn::SingleWithAttrib;
using Ttcn::WithAttribPath;
using Ttcn::Qualifier;
using Ttcn::Qualifiers;

void Type::chk()
{
  if(w_attrib_path) w_attrib_path->chk_global_attrib();
70
71
72
  if (legacy_codec_handling) {
    parse_attributes();
  }
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
  if(!tags_checked) {
    tags_checked = true;
    if(tags) tags->chk();
  }
  if (checked) return;
  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:
    chk_Int_A();
    break;
  case T_ENUM_A:
    chk_Enum_A();
    break;
  case T_ENUM_T:
    chk_Enum_T();
    break;
  case T_BSTR_A:
    chk_BStr_A();
    break;
  case T_OPENTYPE:
    chk_SeCho_T();
    break;
  case T_ANYTYPE:
    // TODO maybe check for address type and add it automagically, then fall through
BenceJanosSzabo's avatar
BenceJanosSzabo committed
129
130
131
132
    if(!xerattrib) {
      xerattrib = new XerAttributes;
    }
    xerattrib->untagged_ = true;
Elemer Lelik's avatar
Elemer Lelik committed
133
134
135
  case T_SEQ_T:
  case T_SET_T:
  case T_CHOICE_T:
136
    chk_SeCho_T();    
Elemer Lelik's avatar
Elemer Lelik committed
137
138
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    break;
  case T_CHOICE_A:
    chk_Choice_A();
    break;
  case T_SEQ_A:
  case T_SET_A:
    chk_Se_A();
    break;
  case T_SEQOF:
  case T_SETOF:
    chk_SeOf();
    break;
  case T_REFD:
  case T_ADDRESS:
    chk_refd();
    break;
  case T_SELTYPE:
    chk_seltype();
    break;
  case T_REFDSPEC:
  case T_OCFT:
    u.ref.type_refd->chk();
    u.ref.component_internal = u.ref.type_refd->is_component_internal();
    break;
  case T_ARRAY:
    chk_Array();
    break;
  case T_PORT:
    u.port->chk();
    if (w_attrib_path) u.port->chk_attributes(w_attrib_path);
    break;
  case T_SIGNATURE:
    chk_Signature();
    break;
  case T_COMPONENT:
    u.component->chk();
    break;
  case T_FUNCTION:
  case T_ALTSTEP:
  case T_TESTCASE:
    chk_Fat();
    break;
  default:
    FATAL_ERROR("Type::chk()");
  } // switch

  if(w_attrib_path) {
    switch (get_type_refd_last()->typetype) {
    case T_SEQ_T:
    case T_SET_T:
    case T_CHOICE_T:
      // These types may have qualified attributes
      break;
    case T_SEQOF: case T_SETOF:
      break;
    default:
      w_attrib_path->chk_no_qualif();
      break;
    }
  }
  checked = true;
  if(tags) tags->set_plicit(this);

  /*
  Check all non-table subtype constraints. Table/relational constraints
  are ignored here.
  TODO: non-relational table constraints shall not be ignored.
  */
  if (check_subtype) check_subtype_constraints();

  /*
   * Checking the constraints can be done only if the entire type
   * (including the nested typedefs) is checked, because the
   * component relation constraint has to 'look' into other
   * components.
   */
  if (!parent_type) chk_table_constraints();

215
216
217
218
219
220
221
222
  if (legacy_codec_handling) {
    chk_coding_attribs();
  }
  else if (my_scope != NULL) {
    chk_encodings();
    chk_variants();
  }
  
223
224
  chk_oer();
  
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  chk_finished = true;
}

void Type::chk_coding_attribs()
{
  if (typetype == T_SEQ_T || typetype == T_SET_T || typetype == T_CHOICE_T) {
    // If this record/set/union type has no attributes but one of its fields does,
    // create an empty attribute structure.
    // TODO: do the same for ASN.1 sequence/set/choice types, when ASN.1 gets XER ?
    // The code was originally for TTCN-only encodings.
    if (!rawattrib && hasVariantAttrs() && hasNeedofRawAttrs()) {
      rawattrib = new RawAST(get_default_raw_fieldlength());
    }
    if (!textattrib && hasVariantAttrs() && hasNeedofTextAttrs()) {
      textattrib = new TextAST;
    }
    if (!xerattrib && hasVariantAttrs() && hasNeedofXerAttrs()) {
      xerattrib = new XerAttributes;
    }
    if (!jsonattrib && hasVariantAttrs() && hasNeedofJsonAttrs()) {
      jsonattrib = new JsonAST;
    }
  }
  
  if (rawattrib != NULL || is_root_basic()) {
Elemer Lelik's avatar
Elemer Lelik committed
250
251
    chk_raw();
  }
252
  if (textattrib != NULL || is_root_basic()) {
Elemer Lelik's avatar
Elemer Lelik committed
253
254
255
    chk_text();
  }
  
256
  if (jsonattrib != NULL || is_root_basic()) {
Elemer Lelik's avatar
Elemer Lelik committed
257
258
259
260
261
262
263
    chk_json();
  }
  
  // We need to call chk_xer() always because it is collecting
  // XER attributes from parent types.
  chk_xer();
  
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
  if (!legacy_codec_handling) {
    switch (typetype) {
    case T_SEQ_T:
    case T_SEQ_A:
    case T_SET_T:
    case T_SET_A:
    case T_CHOICE_T:
    case T_CHOICE_A:
    case T_ANYTYPE:
    case T_OPENTYPE:
      for (size_t i = 0; i < get_nof_comps(); ++i) {
        get_comp_byIndex(i)->get_type()->chk_coding_attribs();
      }
      break;

    case T_ARRAY:
    case T_SEQOF:
    case T_SETOF:
      get_ofType()->chk_coding_attribs();
      break;

    default:
      break;
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
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
}

void Type::parse_attributes()
{
  if (raw_parsed) return;

  // The type has no attributes of its own; connect it to the nearest group
  // or the module. This allows global attributes to propagate.
  for (Type *t = this; t && w_attrib_path == 0; t = t->parent_type) {
    switch (t->ownertype) {
    case OT_TYPE_DEF: {
      Ttcn::Def_Type *pwn = static_cast<Ttcn::Def_Type*>(t->owner);
      Ttcn::Group *nearest_group = pwn->get_parent_group();

      w_attrib_path = new WithAttribPath;
      if (nearest_group) { // there is a group
        w_attrib_path->set_parent(nearest_group->get_attrib_path());
      }
      else { // no group, use the module
        Common::Module *mymod = t->my_scope->get_scope_mod();
        // OT_TYPE_DEF is always from a TTCN-3 module
        Ttcn::Module *my_ttcn_module = static_cast<Ttcn::Module *>(mymod);
        w_attrib_path->set_parent(my_ttcn_module->get_attrib_path());
      }
      break; }

    case OT_RECORD_OF:
    case OT_COMP_FIELD:
      continue; // try the enclosing type

    default:
      break;
    }
    break;
  }

  if ((hasVariantAttrs())
326
      && (enable_text() || enable_raw() || enable_xer() || enable_json())) {
Elemer Lelik's avatar
Elemer Lelik committed
327
328
#ifndef NDEBUG
    if (rawAST_debug) {
329
      const char *fn = get_fullname().c_str();
Elemer Lelik's avatar
Elemer Lelik committed
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
      fprintf(stderr, "parse_attributes for %s\n", fn);
    }
#endif
    bool new_raw=false;      // a RawAST  object was allocated here
    bool new_text=false;     // a TextAST object was allocated here
    bool new_xer=false;      // a XerAttribute object was allocated here
    bool new_ber=false;      // a BerAST object was allocated here
    bool new_json = false;   // a JsonAST object was allocated here
    bool raw_found=false;    // a raw  attribute was found by the parser
    bool text_found=false;   // a text attribute was found by the parser
    bool xer_found=false;    // a XER  attribute was found by the parser
    bool ber_found=false;    // a BER attribute was found by the parser
    bool json_found = false; // a JSON attribute was found by the parser
    raw_checked=false;
    text_checked=false;
    json_checked = false;
    xer_checked=false;
    bool override_ref=false;
    // Parse RAW attributes
    switch(typetype) {
    case T_REFD: {
      ReferenceChain refch(this, "While checking attributes");
      if(w_attrib_path)
      {
        // Get all the attributes that apply (from outer scopes too).
        // Outer (generic) first, inner (specific) last.
        const vector<SingleWithAttrib> & real_attribs
          = w_attrib_path->get_real_attrib();

        // see if there's an encode with override
        for(size_t i = real_attribs.size(); i > 0 && !override_ref; i--)
        {
362
          if(real_attribs[i-1]->get_modifier() == Ttcn::MOD_OVERRIDE
Elemer Lelik's avatar
Elemer Lelik committed
363
364
365
366
367
368
            && real_attribs[i-1]->get_attribKeyword()
              != SingleWithAttrib::AT_ENCODE)
            override_ref = true;
        }
      }
      if(!rawattrib && !override_ref){
369
        Type *t=this; 
Elemer Lelik's avatar
Elemer Lelik committed
370
371
        refch.reset();
        while(!t->rawattrib && t->is_ref()) t=t->get_type_refd(&refch);
372
        rawattrib=new RawAST(t->rawattrib,t->get_default_raw_fieldlength());
Elemer Lelik's avatar
Elemer Lelik committed
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
        new_raw=true;
      }
      if(!textattrib && !override_ref){
        Type *t=this;
        refch.reset();
        while(!t->textattrib && t->is_ref()) t=t->get_type_refd(&refch);
        textattrib=new TextAST(t->textattrib);
        new_text=true;
      }
      if (!jsonattrib && !override_ref){
        Type *t = this;
        refch.reset();
        while (!t->jsonattrib && t->is_ref()) {
          t = t->get_type_refd(&refch);
        }
        jsonattrib = new JsonAST(t->jsonattrib);
        new_json = true;
      }
    }
    // no break
    case T_BOOL:
    case T_INT:
    case T_REAL:
    case T_BSTR:
    case T_HSTR:
    case T_OSTR:
    case T_CSTR: // TTCN-3 charstring
    case T_ENUM_T:
      /* The list of types supporting RAW encoding is described in the
       * API guide, section 3.3; this batch of labels plus the next three
       * accurately match the list.
       *
       * For TEXT, it's section 3.4; the list does not include real and
       * bin/hex/octet strings
       */
    case T_USTR: // TTCN-3 universal charstring, this is an addition for XER attributes
    case T_VERDICT: // TTCN-3 verdict, for XER

      if(rawattrib==NULL){
412
        rawattrib= new RawAST(get_default_raw_fieldlength());
Elemer Lelik's avatar
Elemer Lelik committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
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
        new_raw=true;
      }
      if(textattrib==NULL){textattrib= new TextAST; new_text=true;}
      if (xerattrib==NULL) {
        xerattrib = new XerAttributes; new_xer = true;
      }
      if (berattrib==NULL) {
        berattrib = new BerAST;
        new_ber = true;
      }
      
      if (NULL == jsonattrib) {
        jsonattrib = new JsonAST;
        new_json = true;
      }

      if(w_attrib_path)
      {
        vector<SingleWithAttrib> const &real_attribs
          = w_attrib_path->get_real_attrib();
        // These are attributes without qualifiers.

        size_t nof_elements = real_attribs.size();
        for(size_t i = 0; i < nof_elements; i++)
        {
          SingleWithAttrib *temp_single = real_attribs[i];
          if (temp_single->get_attribKeyword() ==
            SingleWithAttrib::AT_VARIANT) { // only "variant" is parsed
            parse_rawAST(rawattrib, textattrib, xerattrib, berattrib, jsonattrib,
              temp_single->get_attribSpec(), get_length_multiplier(),
              my_scope->get_scope_mod(), 
              raw_found, text_found, xer_found, ber_found, json_found);
//            textattrib->print_TextAST();
//            rawattrib->print_RawAST();
//            xerattrib->print(get_fullname().c_str());
          } // if AT_VARIANT
        } // next i
      }

      if(!raw_found && new_raw){ delete rawattrib; rawattrib=NULL;}
      if(!text_found && new_text){ delete textattrib; textattrib=NULL;}
      if(!xer_found && new_xer){ delete xerattrib; xerattrib = NULL; }
      if(!ber_found && new_ber){ delete berattrib; berattrib = NULL; }
      if (!json_found && new_json) {
        delete jsonattrib;
        jsonattrib = NULL;
      }
      break;
    case T_SEQOF:
    case T_SETOF:
    case T_CHOICE_T:
    case T_SEQ_T:
    case T_SET_T:
    case T_ANYTYPE:
Elemer Lelik's avatar
Elemer Lelik committed
467
    case T_ARRAY:
468
      if(rawattrib==NULL) {rawattrib= new RawAST(get_default_raw_fieldlength()); new_raw=true;}
Elemer Lelik's avatar
Elemer Lelik committed
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
      if(textattrib==NULL){textattrib= new TextAST; new_text=true;}
      if(xerattrib==NULL) {xerattrib = new XerAttributes; new_xer = true;}
      if(berattrib==NULL) {berattrib = new BerAST; new_ber = true;}
      if (NULL == jsonattrib) {
        jsonattrib = new JsonAST;
        new_json = true;
      }

      if(w_attrib_path)
      {
        vector<SingleWithAttrib> const &real_attribs
          = w_attrib_path->get_real_attrib();

        //calculate the type's attributes (the qualifierless ones)
        size_t nof_elements = real_attribs.size();
        for(size_t i = 0; i < nof_elements; i++)
        {
          SingleWithAttrib *temp_single = real_attribs[i];
          if(temp_single->get_attribKeyword() == SingleWithAttrib::AT_VARIANT
            && (!temp_single->get_attribQualifiers()
              || temp_single->get_attribQualifiers()->get_nof_qualifiers()
                  == 0)){
            // raw/text/xer/ber/json attributes for the whole record/seq.
            // (own or inherited)
            parse_rawAST(rawattrib, textattrib, xerattrib, berattrib, jsonattrib,
              temp_single->get_attribSpec(), get_length_multiplier(),
              my_scope->get_scope_mod(),
              raw_found, text_found, xer_found, ber_found, json_found);
          }
        }
        //calculate the components attributes
        MultiWithAttrib* self_attribs = w_attrib_path->get_with_attr();
        if(self_attribs)
        {
          MultiWithAttrib* new_self_attribs = new MultiWithAttrib;
          SingleWithAttrib* swa = 0;

          // copy all the "encode" attributes
          for(size_t i = 0; i < self_attribs->get_nof_elements(); i++)
          {
            if(self_attribs->get_element(i)->get_attribKeyword()
              == SingleWithAttrib::AT_ENCODE)
            {
              // Copy the attribute without qualifiers
              const SingleWithAttrib* swaref = self_attribs->get_element(i);
              swa = new SingleWithAttrib(swaref->get_attribKeyword(),
515
                swaref->get_modifier(), 0, swaref->get_attribSpec().clone());
Elemer Lelik's avatar
Elemer Lelik committed
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
              new_self_attribs->add_element(swa);
            }
          }

          if(new_self_attribs->get_nof_elements() > 0)
          { // One or more "encode"s were copied; create a context for them.
            // This is a member because is has to be owned by this Type
            // (the components only refer to it).
            encode_attrib_path = new WithAttribPath;
            encode_attrib_path->set_with_attr(new_self_attribs);
            encode_attrib_path->set_parent(w_attrib_path->get_parent());
          }
          else delete new_self_attribs;

          // This should be bool, but gcc 4.1.2-sol8 generates incorrect code
          // with -O2 :(
Elemer Lelik's avatar
Elemer Lelik committed
532
533
          const int se_of = (typetype == T_SEQOF || typetype == T_SETOF ||
            typetype == T_ARRAY);
Elemer Lelik's avatar
Elemer Lelik committed
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
          const size_t nof_comps = se_of ? 1 : get_nof_comps();

          // Distribute the attributes with qualifiers to the components.
          // If the type is a sequence-of or set-of, we pretend it to have
          // one component with the name "_0" (a valid TTCN-3 identifier
          // can't begin with an underscore). compiler.y has created
          // the appropriate identifier in the qualifier for a "[-]".
          for(size_t i = 0; i < nof_comps; i++)
          {
            const Identifier& comp_id =
              se_of ? underscore_zero : get_comp_id_byIndex(i);
            MultiWithAttrib* component_attribs = new MultiWithAttrib;

            for(size_t j = 0; j < self_attribs->get_nof_elements(); j++)
            {
              const SingleWithAttrib *temp_single =
                self_attribs->get_element(j);
              Qualifiers* temp_qualifiers =
                temp_single->get_attribQualifiers();
              if( !temp_qualifiers
                || temp_qualifiers->get_nof_qualifiers() == 0) continue;

              Qualifiers* calculated_qualifiers = new Qualifiers;
              bool qualifier_added = false;
              for(size_t k=0; k < temp_qualifiers->get_nof_qualifiers(); )
              {
                const Qualifier* temp_qualifier =
                  temp_qualifiers->get_qualifier(k);
                if(temp_qualifier->get_nof_identifiers() > 0
                  && (*temp_qualifier->get_identifier(0) == comp_id))
                {
                  // Found a qualifier whose first identifier matches
                  //  the component name. Remove the qualifier from the
                  //  enclosing type, chop off its head,
                  //  and add it to the component's qualifiers.
                  calculated_qualifiers->add_qualifier(
                    temp_qualifier->get_qualifier_without_first_id());
                  temp_qualifiers->delete_qualifier(k);
                  qualifier_added = true;
                }
                else k++;
              } // next qualifier

              if(qualifier_added)
              {
                // A copy of temp_single, with new qualifiers
                SingleWithAttrib* temp_single2
                = new SingleWithAttrib(temp_single->get_attribKeyword(),
582
                  temp_single->get_modifier(),
Elemer Lelik's avatar
Elemer Lelik committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
                  calculated_qualifiers,
                  temp_single->get_attribSpec().clone());
                temp_single2->set_location(*temp_single);
                component_attribs->add_element(temp_single2);
              }
              else delete calculated_qualifiers;
            } // next attrib

            if (component_attribs->get_nof_elements() > 0) {
              Type* component_type = se_of ?
                get_ofType() : get_comp_byIndex(i)->get_type();

              if(encode_attrib_path)
                // The record's "encode" attributes (only) apply to the fields.
                // Interpose them in the path of the field.
                component_type->set_parent_path(encode_attrib_path);
              else
                component_type->set_parent_path(w_attrib_path->get_parent());

              component_type->set_with_attr(component_attribs);
            }
            else delete component_attribs;
          } // next component index

          // Any remaining attributes with qualifiers are erroneous
          for(size_t i = 0; i < self_attribs->get_nof_elements();)
          {
            Qualifiers *temp_qualifiers = self_attribs->get_element(i)
                ->get_attribQualifiers();
            if(temp_qualifiers && temp_qualifiers->get_nof_qualifiers() != 0)
            {
              size_t nof_qualifiers = temp_qualifiers->get_nof_qualifiers();
              for(size_t j = 0; j < nof_qualifiers; j++)
              {
                const Qualifier *temp_qualifier =
                  temp_qualifiers->get_qualifier(j);
                const Identifier& tmp_id = *temp_qualifier->get_identifier(0);
                // Special case when trying to reference the inner type
                // of a record-of when it wasn't a record-of.
                if (tmp_id == underscore_zero) temp_qualifier->error(
623
624
625
626
                  "Type `%s' cannot be indexed", get_typename().c_str());
                else temp_qualifier->error(
                  "Reference to non-existent field `%s' in type `%s'",
                  tmp_id.get_dispname().c_str(), get_typename().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
627
628
629
630
631
632
633
634
635
636
637
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
              }
              self_attribs->delete_element(i);
            }else{
              i++;
            }
          } // next i
        } // end if(self_attribs)
      } // end if(w_attrib_path)
      if (!raw_found && new_raw)  { delete rawattrib;  rawattrib = NULL; }
      if (!text_found && new_text){ delete textattrib; textattrib= NULL; }
      if (!xer_found && new_xer)  { delete xerattrib;  xerattrib = NULL; }
      if (!ber_found && new_ber)  { delete berattrib;  berattrib = NULL; }
      if (!json_found && new_json) {
        delete jsonattrib;
        jsonattrib = NULL;
      }
      break;
    default:
      // nothing to do, ASN1 types or types without defined raw attribute
      break;
    } // switch
    if (rawattrib && !enable_raw()) { delete rawattrib;  rawattrib = NULL;}
    if (textattrib&& !enable_text()){ delete textattrib; textattrib= NULL;}
    if (xerattrib && !enable_xer()) { delete xerattrib;  xerattrib = NULL;}
    if (berattrib && !enable_ber()) { delete berattrib;  berattrib = NULL;}
    if (NULL != jsonattrib && !enable_json()) {
      delete jsonattrib;
      jsonattrib = NULL;
    }
  } // endif( hasVariantAttrs && enable_{raw,text,xer} )

  raw_parsed = true;
}

// Implements "NAME AS ..." transformations.
void change_name(string &name, XerAttributes::NameChange change) {
  switch (change.kw_) {
  case NamespaceSpecification::NO_MANGLING:
    break; // cool, nothing to do!

  case NamespaceSpecification::UPPERCASED:
    // Walking backwards calls size() only once. Loop var must be signed.
    for (int i = name.size()-1; i >= 0; --i) {
670
      name[i] = (char)toupper(name[i]);
Elemer Lelik's avatar
Elemer Lelik committed
671
672
673
674
675
    }
    break;

  case NamespaceSpecification::LOWERCASED:
    for (int i = name.size()-1; i >= 0; --i) {
676
      name[i] = (char)tolower(name[i]);
Elemer Lelik's avatar
Elemer Lelik committed
677
678
679
680
    }
    break;

  case NamespaceSpecification::CAPITALIZED:
681
    name[0] = (char)toupper(name[0]);
Elemer Lelik's avatar
Elemer Lelik committed
682
683
684
    break;

  case NamespaceSpecification::UNCAPITALIZED:
685
    name[0] = (char)tolower(name[0]);
Elemer Lelik's avatar
Elemer Lelik committed
686
687
688
689
690
691
692
693
    break;

  default: // explicitly specified name
    name = change.nn_;
    break;
  } // switch for NAME
}

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
void Type::get_types_w_no_coding_table(vector<Type>& type_list, bool only_own_table)
{
  // this helps avoid infinite recursions in self-referencing types
  if (RecursionTracker::is_happening(this)) {
    return;
  }
  RecursionTracker tracker(this);
  if ((only_own_table && coding_table.size() == 0) ||
      (!only_own_table && get_type_w_coding_table() == NULL)) {
    type_list.add(this);
  }
  switch (get_typetype_ttcn3()) {
  case T_SEQ_T:
  case T_SET_T:
  case T_CHOICE_T:
  case T_ANYTYPE:
  case T_OPENTYPE:
    for (size_t j = 0; j < get_nof_comps(); ++j) {
      get_comp_byIndex(j)->get_type()->get_types_w_no_coding_table(type_list, only_own_table);
    }
    break;
  case T_SEQOF:
  case T_SETOF:
  case T_ARRAY:
    get_ofType()->get_types_w_no_coding_table(type_list, only_own_table);
    break;
  default:
    break;
  }
}

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
void Type::chk_encodings()
{
  if (legacy_codec_handling) {
    FATAL_ERROR("Type::chk_encodings");
  }
  switch (get_type_refd_last()->get_typetype_ttcn3()) {
  case T_NULL:
  case T_BOOL:
  case T_INT:
  case T_REAL:
  case T_ENUM_T:
  case T_BSTR:
  case T_HSTR:
  case T_OSTR:
  case T_CSTR:
  case T_USTR:
  case T_OID:
  case T_CHOICE_T:
  case T_SEQOF:
  case T_SETOF:
  case T_SEQ_T:
  case T_SET_T:
  case T_VERDICT:
  case T_ARRAY:
  case T_ANYTYPE:
    if (!is_asn1()) {
      WithAttribPath* ap = get_attrib_path();
      if (ap != NULL) {
        MultiWithAttrib* mwa = ap->get_with_attr();
        if (mwa != NULL) {
          for (size_t i = 0; i < mwa->get_nof_elements(); ++i) {
            const SingleWithAttrib* swa = mwa->get_element(i);
            if (swa->get_attribKeyword() == SingleWithAttrib::AT_ENCODE) {
758
              Ttcn::attribute_modifier_t mod = swa->get_modifier();
759
760
761
762
763
              Ttcn::Qualifiers* quals = swa->get_attribQualifiers();
              if (quals != NULL && quals->get_nof_qualifiers() != 0) {
                for (size_t j = 0; j < quals->get_nof_qualifiers(); ++j) {
                  Ttcn::Qualifier* qual = const_cast<Ttcn::Qualifier*>(
                    quals->get_qualifier(j));
764
765
766
767
768
769
770
                  Type* t = get_field_type(qual, EXPECTED_CONSTANT);
                  if (t != NULL) {
                    if (t->my_scope != my_scope) {
                      qual->warning("Encode attribute is ignored, because it "
                        "refers to a type from a different type definition");
                    }
                    else {
771
                      t->add_coding(swa->get_attribSpec().get_spec(), mod, false);
772
773
                    }
                  }
774
775
776
                }
              }
              else {
777
                add_coding(swa->get_attribSpec().get_spec(), mod, false);
778
779
780
781
              }
            }
          }
        }
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
        WithAttribPath* global_ap = NULL;
        Ttcn::Def_Type* def = static_cast<Ttcn::Def_Type*>(owner);
        Ttcn::Group* nearest_group = def->get_parent_group();

        if (nearest_group != NULL) { // there is a group
          global_ap = nearest_group->get_attrib_path();
        }
        else { // no group, use the module
          Common::Module* mymod = my_scope->get_scope_mod();
          // OT_TYPE_DEF is always from a TTCN-3 module
          Ttcn::Module* my_ttcn_module = static_cast<Ttcn::Module *>(mymod);
          global_ap = my_ttcn_module->get_attrib_path();
        }
        if (global_ap != NULL) {
          bool has_global_override = false;
          bool modifier_conflict = false;
          Ttcn::attribute_modifier_t first_mod = Ttcn::MOD_NONE;
          const vector<SingleWithAttrib>& real = global_ap->get_real_attrib();
800
801
802
          for (size_t i = 0; i < real.size(); ++i) {
            const SingleWithAttrib* swa = real[i];
            if (swa->get_attribKeyword() == SingleWithAttrib::AT_ENCODE) {
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
              Ttcn::attribute_modifier_t mod = swa->get_modifier();
              if (i == 0) {
                first_mod = mod;
              }
              else if (!modifier_conflict && mod != first_mod) {
                modifier_conflict = true;
                swa->error("All 'encode' attributes of a group or module must "
                  "have the same modifier ('override', '@local' or none)");
              }
              if (mod == Ttcn::MOD_OVERRIDE) {
                has_global_override = true;
              }
              if (has_global_override && modifier_conflict) {
                break;
              }
            }
          }
          // make a list of the type and its field and element types that inherit
          // the global 'encode' attributes
          // overriding global attributes are inherited by types with no coding
          // table (no 'encode' attributes) of their own
          // non-overriding global attributes are inherited by types that have
          // no coding table of their own and cannot use the coding table of any 
          // other type (get_type_w_coding_table() == NULL)
          vector<Type> type_list;
          get_types_w_no_coding_table(type_list, has_global_override);
          if (type_list.size() != 0) {
            for (size_t i = 0; i < real.size(); ++i) {
              const SingleWithAttrib* swa = real[i];
              if (swa->get_attribKeyword() == SingleWithAttrib::AT_ENCODE) {
                for (size_t j = 0; j < type_list.size(); ++j) {
                  type_list[j]->add_coding(swa->get_attribSpec().get_spec(),
                    Ttcn::MOD_NONE, true);
                }
              }
838
            }
839
            type_list.clear();
840
841
842
843
844
845
846
847
848
849
          }
        }
      }
    }
    else {
      switch (ownertype) {
      case OT_TYPE_ASS:
      case OT_RECORD_OF:
      case OT_COMP_FIELD:
      case OT_SELTYPE:
850
        // ASN.1 types automatically have BER, PER, XER, OER and JSON encoding
851
852
853
        add_coding(string("BER:2002"), Ttcn::MOD_NONE, true);
        add_coding(string(get_encoding_name(CT_PER)), Ttcn::MOD_NONE, true);
        add_coding(string(get_encoding_name(CT_JSON)), Ttcn::MOD_NONE, true);
854
        add_coding(string(get_encoding_name(CT_OER)), Ttcn::MOD_NONE, true);
855
856
        if (asn1_xer) {
          // XER encoding for ASN.1 types can be disabled with a command line option
857
          add_coding(string(get_encoding_name(CT_XER)), Ttcn::MOD_NONE, true);
858
859
860
861
862
863
864
865
866
867
868
869
870
        }
        break;
      default:
        break;
      }      
    }
    break;
  default:
    // the rest of the types can't have 'encode' attributes
    break;
  }
}

871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
void Type::chk_variants()
{
  if (legacy_codec_handling) {
    FATAL_ERROR("Type::chk_encodings");
  }
  if (is_asn1() || ownertype != OT_TYPE_DEF) {
    return;
  }
  // check global (group/module) variant attributes first
  WithAttribPath* global_ap = NULL;
  Ttcn::Def_Type* def = static_cast<Ttcn::Def_Type*>(owner);
  Ttcn::Group* nearest_group = def->get_parent_group();

  if (nearest_group != NULL) { // there is a group
    global_ap = nearest_group->get_attrib_path();
  }
  else { // no group, use the module
    Common::Module* mymod = my_scope->get_scope_mod();
    // OT_TYPE_DEF is always from a TTCN-3 module
    Ttcn::Module* my_ttcn_module = static_cast<Ttcn::Module *>(mymod);
    global_ap = my_ttcn_module->get_attrib_path();
  }
  if (global_ap != NULL) {
894
895
896
897
898
899
    // process all global variants, not just the closest group
    const vector<SingleWithAttrib>& real = global_ap->get_real_attrib();
    for (size_t i = 0; i < real.size(); ++i) {
      const SingleWithAttrib* swa = real[i];
      if (swa->get_attribKeyword() == SingleWithAttrib::AT_VARIANT) {
        chk_this_variant(swa, true);
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
      }
    }
  }
  // check local variant attributes second, so they overwrite global ones if they
  // conflict with each other
  WithAttribPath* ap = get_attrib_path();
  if (ap != NULL) {
    MultiWithAttrib* mwa = ap->get_with_attr();
    if (mwa != NULL) {
      for (size_t i = 0; i < mwa->get_nof_elements(); ++i) {
        const SingleWithAttrib* swa = mwa->get_element(i);
        if (swa->get_attribKeyword() == SingleWithAttrib::AT_VARIANT) {
          Ttcn::Qualifiers* quals = swa->get_attribQualifiers();
          if (quals != NULL && quals->get_nof_qualifiers() != 0) {
            for (size_t j = 0; j < quals->get_nof_qualifiers(); ++j) {
              Ttcn::Qualifier* qual = const_cast<Ttcn::Qualifier*>(
                quals->get_qualifier(j));
              Type* t = get_field_type(qual, EXPECTED_CONSTANT);
              if (t != NULL) {
                if (t->my_scope != my_scope) {
                  qual->warning("Variant attribute is ignored, because it refers "
                    "to a type from a different type definition");
                }
                else {
                  t->chk_this_variant(swa, false);
                }
              }
            }
          }
          else {
            chk_this_variant(swa, false);
          }
        }
      }
    }
  }
  // check the coding attributes set by the variants
  chk_coding_attribs();
}

void Type::chk_this_variant(const Ttcn::SingleWithAttrib* swa, bool global)
{
  // TODO: use something other than get_typename...
  Type* t = get_type_w_coding_table();
  if (t == NULL) {
    if (!global) {
      swa->error("No encoding rules defined for type `%s'",
        get_typename().c_str());
    }
  }
  else {
    const string& enc_str = swa->get_attribSpec().get_encoding();
    MessageEncodingType_t coding = get_enc_type(enc_str);
    bool erroneous = false;
    if (enc_str.empty()) {
      if (t->coding_table.size() > 1) {
        if (!global) {
          swa->error("The encoding reference is mandatory for variant attributes "
            "of type `%s', which has multiple encodings", get_typename().c_str());
        }
        erroneous = true;
      }
      else if (t->coding_table[0]->built_in) {
        coding = t->coding_table[0]->built_in_coding;
      }
      else if (strcmp(t->coding_table[0]->custom_coding.name, "PER") == 0) {
        coding = CT_PER;
      }
      // else leave it as CT_CUSTOM
    }
    else {
      if (!has_encoding(coding, &enc_str)) {
        erroneous = true;
        if (!global) {
          if (coding != CT_CUSTOM) {
            swa->error("Type `%s' does not support %s encoding",
              get_typename().c_str(), get_encoding_name(coding));
          }
          else {
            swa->error("Type `%s' does not support custom encoding `%s'",
              get_typename().c_str(), enc_str.c_str());
          }
        }
      }
    }
    if (!erroneous && coding != CT_PER && coding != CT_CUSTOM) {
      bool new_ber = false;    // a BerAST object was allocated here
      bool new_raw = false;    // a RawAST object was allocated here
      bool new_text = false;   // a TextAST object was allocated here
      bool new_xer = false;    // a XerAttribute object was allocated here
      bool new_json = false;   // a JsonAST object was allocated here
      bool ber_found = false;  // a BER attribute was found by the parser
      bool raw_found = false;  // a RAW attribute was found by the parser
      bool text_found = false; // a TEXT attribute was found by the parser
      bool xer_found = false;  // a XER attribute was found by the parser
      bool json_found = false; // a JSON attribute was found by the parser
      if (berattrib == NULL) {
        berattrib = new BerAST;
        new_ber = true;
      }
      if (rawattrib == NULL) {
For faster browsing, not all history is shown. View entire blame