Attributes.cc 53.4 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
9
10
11
12
13
14
 * 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:
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Delic, Adam
 *   Feher, Csaba
 *   Raduly, Csaba
 *   Szabados, Kristof
15
 *   Szabo, Bence Janos
Elemer Lelik's avatar
Elemer Lelik committed
16
17
18
19
 *   Szabo, Janos Zoltan – initial implementation
 *   Zalanyi, Balazs Andor
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
20
21
22
23
#include "Attributes.hh"
#include "../map.hh"
#include "../CompilerError.hh"
#include "../Type.hh"
24
#include "../main.hh"
Elemer Lelik's avatar
Elemer Lelik committed
25
#include "TtcnTemplate.hh"
26
#include "Statement.hh"
Elemer Lelik's avatar
Elemer Lelik committed
27
28
29
30
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
70
71
72
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
138
139
140
141
142
143
144
145
146
147

namespace Ttcn {

  // ==== Qualifier ====

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

  const Identifier* Qualifier::get_identifier(size_t p_index) const
  {
    FieldOrArrayRef* ref = get_ref(p_index);
    return (ref->get_type()==FieldOrArrayRef::FIELD_REF) ?
           ref->get_id() :
           &underscore_zero ;
  }

  Qualifier* Qualifier::get_qualifier_without_first_id() const
  {
    Qualifier* temp = this->clone();
    temp->remove_refs(1);
    return temp;
  }

  string Qualifier::get_stringRepr() const
  {
    string str_repr;
    append_stringRepr(str_repr);
    if (str_repr[0]=='.') str_repr.replace(0, 1, ""); // remove leading dot if present
    return str_repr;
  }

  void Qualifier::dump(unsigned level) const
  {
    const string& rep = get_stringRepr();
    DEBUG(level, "(%s)", rep.c_str());
  }

  // ==== Qualifiers ====

  Qualifiers::~Qualifiers()
  {
    for(size_t i = 0; i < qualifiers.size(); i++)
    {
      delete qualifiers[i];
    }
    qualifiers.clear();
  }

  Qualifiers::Qualifiers(const Qualifiers& p)
    : Node(p)
  {
    for(size_t i = 0; i < p.get_nof_qualifiers(); i++)
    {
      qualifiers.add(p.get_qualifier(i)->clone());
    }
  }

  void Qualifiers::add_qualifier(Qualifier* p_qualifier)
  {
    if(p_qualifier->get_nof_identifiers() > 0)
      qualifiers.add(p_qualifier);
    else
      delete p_qualifier;
  }

  void Qualifiers::delete_qualifier(size_t p_index)
  {
    delete qualifiers[p_index];
    qualifiers.replace(p_index,1,NULL);
  }

  const Qualifier* Qualifiers::get_qualifier(size_t p_index) const
  {
    return qualifiers[p_index];
  }

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

  void Qualifiers::set_fullname(const string& p_fullname)
  {
    Node::set_fullname(p_fullname);
    for(size_t i = 0; i < qualifiers.size(); i++)
    {
      qualifiers[i]->set_fullname(p_fullname + ".<qualifier "
        + Int2string(i) + ">");
    }
  }

  bool Qualifiers::has_qualifier(Qualifier* p_qualifier) const
  {
    bool result = false;
    size_t index;
    bool same;
    for(size_t i = 0; i < qualifiers.size() && !result; i++)
    {
      if(qualifiers[i]->get_nof_identifiers()
          ==p_qualifier->get_nof_identifiers())
      {
        index = 0;
        same = true;
        while(index < p_qualifier->get_nof_identifiers() && same)
        {
          same = (*qualifiers[i]->get_identifier(index)
                == *p_qualifier->get_identifier(index));
          index++;
        }

        result |= same;
      }
    }
    return result;
  }

  void Qualifiers::dump(unsigned level) const
  {
    DEBUG(level, "has %lu qualifiers",
148
        static_cast<unsigned long>( get_nof_qualifiers() ));
Elemer Lelik's avatar
Elemer Lelik committed
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    for(size_t i = 0; i < qualifiers.size(); i++)
      qualifiers[i]->dump(level);
  }

  // ==== ErroneousDescriptor ====

  ErroneousDescriptor::~ErroneousDescriptor()
  {
    for (size_t i=0; i<descr_m.size(); i++) delete descr_m.get_nth_elem(i);
    descr_m.clear();
    for (size_t i=0; i<values_m.size(); i++) delete values_m.get_nth_elem(i);
    values_m.clear();
  }

  // ==== ErroneousAttributeSpec ====

  ErroneousAttributeSpec::ErroneousAttributeSpec(bool p_is_raw, indicator_t p_indicator, TemplateInstance* p_tmpl_inst, bool p_has_all_keyword):
    is_raw(p_is_raw), has_all_keyword(p_has_all_keyword), indicator(p_indicator),
    tmpl_inst(p_tmpl_inst), type(0), value(0)
  {
    if (!tmpl_inst) FATAL_ERROR("ErroneousAttributeSpec::ErroneousAttributeSpec()");
  }

  ErroneousAttributeSpec::ErroneousAttributeSpec(const ErroneousAttributeSpec& p)
    : Node(p), Location(p), is_raw(p.is_raw), has_all_keyword(p.has_all_keyword),
    indicator(p.indicator), type(0), value(0)
  {
    tmpl_inst = p.tmpl_inst ? tmpl_inst->clone() : 0;
  }

  ErroneousAttributeSpec::~ErroneousAttributeSpec()
  {
    delete tmpl_inst;
    delete value;
  }

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

  void ErroneousAttributeSpec::set_fullname(const string& p_fullname)
  {
    Node::set_fullname(p_fullname);
    tmpl_inst->set_fullname(p_fullname+".<template_instance>");
  }

  void ErroneousAttributeSpec::set_my_scope(Scope *p_scope)
  {
    tmpl_inst->set_my_scope(p_scope);
  }

  void ErroneousAttributeSpec::dump(unsigned level) const
  {
    DEBUG(level, "raw: %s", is_raw ? "yes" : "no");
    DEBUG(level, "indicator:");
    switch (indicator) {
    case I_BEFORE:
      DEBUG(level+1, "before");
      break;
    case I_VALUE:
      DEBUG(level+1, "value");
      break;
    case I_AFTER:
      DEBUG(level+1, "after");
      break;
    case I_INVALID:
      DEBUG(level+1, "<invalid>");
      break;
    default:
      FATAL_ERROR("ErroneousAttributeSpec::dump()");
    }
    DEBUG(level, "template instance:");
    tmpl_inst->dump(level+1);
  }

  bool ErroneousAttributeSpec::get_is_omit() const
  {
    return (tmpl_inst->get_Template()->get_templatetype()==Template::OMIT_VALUE);
  }

230
  void ErroneousAttributeSpec::chk(bool in_update_stmt)
Elemer Lelik's avatar
Elemer Lelik committed
231
232
233
234
235
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
  {
    if (get_is_omit()) { // special case, no type needed
      if ((indicator==I_BEFORE)||(indicator==I_AFTER)) {
        if (!has_all_keyword) {
          tmpl_inst->error(
            "Keyword `all' is expected after `omit' when omitting all fields %s the specified field",
            get_indicator_str(indicator));
        }
      } else {
        if (has_all_keyword) {
          tmpl_inst->error(
            "Unexpected `all' keyword after `omit' when omitting one field");
        }
      }
      type = 0;
      return;
    }
    if (has_all_keyword) {
      tmpl_inst->error(
        "Unexpected `all' keyword after the in-line template");
    }

    // determine the type of the tmpl_inst
    type = tmpl_inst->get_expr_governor(Type::EXPECTED_TEMPLATE);
    if (!type) {
      tmpl_inst->get_Template()->set_lowerid_to_ref();
      type = tmpl_inst->get_expr_governor(Type::EXPECTED_TEMPLATE);
    }
    if (!type) {
      tmpl_inst->error("Cannot determine the type of the in-line template");
      return;
    }
    type->chk();
    Type* type_last = type->get_type_refd_last();
    if (!type_last || type_last->get_typetype()==Type::T_ERROR) {
      type = 0;
      return;
    }
    if (is_raw) {
      switch (type_last->get_typetype_ttcn3()) {
      case Type::T_BSTR:
      case Type::T_OSTR:
      case Type::T_CSTR:
      case Type::T_USTR:
        break;
      default:
        tmpl_inst->error("An in-line template of type `%s' cannot be used as a `raw' erroneous value",
                         type_last->get_typename().c_str());
      }
    }

    if (tmpl_inst->get_DerivedRef()) {
      tmpl_inst->error("Reference to a constant value was expected instead of an in-line modified template");
      type = 0;
      return;
    }

    Template* templ = tmpl_inst->get_Template();

    if (templ->is_Value()) {
      value = templ->get_Value();
      value->set_my_governor(type);
      type->chk_this_value_ref(value);
294
295
296
297
      // dynamic values are allowed for attribute specs in '@update' statements,
      // but not for the initial attribute specs of constants and templates
      type->chk_this_value(value, 0, in_update_stmt ?
        Type::EXPECTED_DYNAMIC_VALUE : Type::EXPECTED_CONSTANT,
Elemer Lelik's avatar
Elemer Lelik committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
        INCOMPLETE_NOT_ALLOWED, OMIT_NOT_ALLOWED, SUB_CHK);
      //{ FIXME: make this work
      //  ReferenceChain refch(type, "While checking embedded recursions");
      //  value->chk_recursions(refch);
      //}
      value->set_code_section(GovernedSimple::CS_PRE_INIT);
    } else {
      tmpl_inst->error("A specific value without matching symbols was expected");
      type = 0;
      return;
    }
  }

  const char* ErroneousAttributeSpec::get_indicator_str(indicator_t i)
  {
    switch (i) {
    case I_BEFORE: return "before";
    case I_VALUE: return "value";
    case I_AFTER: return "after";
    default: FATAL_ERROR("ErroneousAttributeSpec::get_indicator_str()");
    }
    return "";
  }

322
  char* ErroneousAttributeSpec::generate_code_str(char *str, char *& def, string genname)
Elemer Lelik's avatar
Elemer Lelik committed
323
324
325
326
327
  {
    if (get_is_omit()) return str;
    if (!type) FATAL_ERROR("ErroneousAttributeSpec::generate_code_str()");
    if (!value) FATAL_ERROR("ErroneousAttributeSpec::generate_code_str()");
    if (first_genname.empty()) { // this is the first use
328
      str = mputprintf(str, "%s%s %s;\n", split_to_slices ? "" : "static ",
Elemer Lelik's avatar
Elemer Lelik committed
329
330
        type->get_genname_value(value->get_my_scope()).c_str(), genname.c_str());
      first_genname = genname;
331
      if (split_to_slices) {
332
333
334
        def = mputprintf(def, "extern %s %s;\n",
          type->get_genname_value(value->get_my_scope()).c_str(), genname.c_str());
      }
Elemer Lelik's avatar
Elemer Lelik committed
335
    } else {
336
      str = mputprintf(str, "%s& %s = %s;\n",
Elemer Lelik's avatar
Elemer Lelik committed
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
        type->get_genname_value(value->get_my_scope()).c_str(),
        genname.c_str(), first_genname.c_str());
    }
    return str;
  }

  char* ErroneousAttributeSpec::generate_code_init_str(char *str, string genname)
  {
    if (get_is_omit()) return str;
    if (!type) FATAL_ERROR("ErroneousAttributeSpec::generate_code_init_str()");
    if (!value) FATAL_ERROR("ErroneousAttributeSpec::generate_code_init_str()");
    str = value->generate_code_init(str, genname.c_str());
    return str;
  }

  string ErroneousAttributeSpec::get_typedescriptor_str()
  {
    if (get_is_omit() || is_raw) return string("NULL");
    if (!type) FATAL_ERROR("ErroneousAttributeSpec::get_typedescriptor_str()");
    if (!value) FATAL_ERROR("ErroneousAttributeSpec::generate_code_ti_str()");
    return ( "&" + type->get_genname_typedescriptor(value->get_my_scope()) + "_descr_" );
  }

  void ErroneousAttributeSpec::chk_recursions(ReferenceChain& refch)
  {
    if (value) {
      refch.mark_state();
      value->chk_recursions(refch);
      refch.prev_state();
    }
  }

  // ==== ErroneousValues ====

371
  char* ErroneousValues::generate_code_embedded_str(char *str, char *& def, string genname)
Elemer Lelik's avatar
Elemer Lelik committed
372
  {
373
374
375
    if (before) str = generate_code_embedded_str(str, def, genname+"_before", before);
    if (value) str = generate_code_embedded_str(str, def, genname+"_value", value);
    if (after) str = generate_code_embedded_str(str, def, genname+"_after", after);
Elemer Lelik's avatar
Elemer Lelik committed
376
377
378
    return str;
  }

379
  char* ErroneousValues::generate_code_embedded_str(char *str, char *& def, string genname, ErroneousAttributeSpec* attr_spec)
Elemer Lelik's avatar
Elemer Lelik committed
380
  {
381
382
383
    str = attr_spec->generate_code_str(str, def, genname+"_errval");
    str = mputprintf(str, "%sErroneous_value_t %s = { %s, %s, %s };\n",
      split_to_slices ? "" : "static ", genname.c_str(),
Elemer Lelik's avatar
Elemer Lelik committed
384
385
386
      attr_spec->get_is_raw() ? "true" : "false",
      attr_spec->get_is_omit() ? "NULL" : ("&"+genname+"_errval").c_str(),
      attr_spec->get_typedescriptor_str().c_str());
387
    if (split_to_slices) {
388
389
      def = mputprintf(def, "extern Erroneous_value_t %s;\n", genname.c_str());
    }
Elemer Lelik's avatar
Elemer Lelik committed
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
    return str;
  }

  char* ErroneousValues::generate_code_init_str(char *str, string genname)
  {
    if (before) str = before->generate_code_init_str(str, genname+"_before_errval");
    if (value) str = value->generate_code_init_str(str, genname+"_value_errval");
    if (after) str = after->generate_code_init_str(str, genname+"_after_errval");
    return str;
  }

  char* ErroneousValues::generate_code_struct_str(char *str, string genname, int field_index)
  {
    str = mputprintf(str, "{ %d, %s, %s, %s, %s }", field_index,
      ("\""+field_name+"\"").c_str(),
      before ? ("&"+genname+"_before").c_str() : "NULL",
      value ? ("&"+genname+"_value").c_str() : "NULL",
      after ? ("&"+genname+"_after").c_str() : "NULL");
    return str;
  }

  void ErroneousValues::chk_recursions(ReferenceChain& refch)
  {
    if (before) before->chk_recursions(refch);
    if (value) value->chk_recursions(refch);
    if (after) after->chk_recursions(refch);
  }

  // ==== ErroneousDescriptor ====

420
  char* ErroneousDescriptor::generate_code_embedded_str(char *str, char *& def, string genname)
Elemer Lelik's avatar
Elemer Lelik committed
421
422
423
  {
    // values
    for (size_t i=0; i<values_m.size(); i++) {
424
      str = values_m.get_nth_elem(i)->generate_code_embedded_str(str, def, genname+"_v"+Int2string(static_cast<int>( values_m.get_nth_key(i) )));
Elemer Lelik's avatar
Elemer Lelik committed
425
426
427
    }
    // embedded descriptors
    for (size_t i=0; i<descr_m.size(); i++) {
428
      str = descr_m.get_nth_elem(i)->generate_code_embedded_str(str, def, genname+"_d"+Int2string(static_cast<int>( descr_m.get_nth_key(i) )));
Elemer Lelik's avatar
Elemer Lelik committed
429
430
431
    }
    // values vector
    if (values_m.size()>0) {
432
      str = mputprintf(str, "%sErroneous_values_t %s_valsvec[%d] = { ",
433
        split_to_slices ? "" : "static ", genname.c_str(), static_cast<int>( values_m.size() ));
Elemer Lelik's avatar
Elemer Lelik committed
434
435
      for (size_t i=0; i<values_m.size(); i++) {
        if (i>0) str = mputstr(str, ", ");
436
        int key_i = static_cast<int>( values_m.get_nth_key(i) );
Elemer Lelik's avatar
Elemer Lelik committed
437
438
439
        str = values_m.get_nth_elem(i)->generate_code_struct_str(str, genname+"_v"+Int2string(key_i), key_i);
      }
      str = mputstr(str, " };\n");
440
      if (split_to_slices) {
441
        def = mputprintf(def, "extern Erroneous_values_t %s_valsvec[%d];\n", genname.c_str(), static_cast<int>( values_m.size() ));
442
      }
Elemer Lelik's avatar
Elemer Lelik committed
443
444
445
    }
    // embedded descriptor vector
    if (descr_m.size()>0) {
446
      str = mputprintf(str, "%sErroneous_descriptor_t %s_embvec[%d] = { ",
447
        split_to_slices ? "" : "static ", genname.c_str(), static_cast<int>( descr_m.size() ));
Elemer Lelik's avatar
Elemer Lelik committed
448
449
      for (size_t i=0; i<descr_m.size(); i++) {
        if (i>0) str = mputstr(str, ", ");
450
        int key_i = static_cast<int>( descr_m.get_nth_key(i) );
451
        str = descr_m.get_nth_elem(i)->generate_code_struct_str(str, def, genname+"_d"+Int2string(key_i), key_i);
Elemer Lelik's avatar
Elemer Lelik committed
452
453
      }
      str = mputstr(str, " };\n");
454
      if (split_to_slices) {
455
        def = mputprintf(def, "extern Erroneous_descriptor_t %s_embvec[%d];\n", genname.c_str(), static_cast<int>( descr_m.size() ));
456
      }
Elemer Lelik's avatar
Elemer Lelik committed
457
458
459
460
461
462
463
    }
    return str;
  }

  char* ErroneousDescriptor::generate_code_init_str(char *str, string genname)
  {
    for (size_t i=0; i<values_m.size(); i++) {
464
      str = values_m.get_nth_elem(i)->generate_code_init_str(str, genname+"_v"+Int2string(static_cast<int>( values_m.get_nth_key(i) )));
Elemer Lelik's avatar
Elemer Lelik committed
465
466
    }
    for (size_t i=0; i<descr_m.size(); i++) {
467
      str = descr_m.get_nth_elem(i)->generate_code_init_str(str, genname+"_d"+Int2string(static_cast<int>( descr_m.get_nth_key(i) )));
Elemer Lelik's avatar
Elemer Lelik committed
468
469
470
471
    }
    return str;
  }

472
  char* ErroneousDescriptor::generate_code_struct_str(char *str, char *& /*def*/, string genname, int field_index)
Elemer Lelik's avatar
Elemer Lelik committed
473
474
475
476
477
478
479
  {
    string genname_values_vec = genname + "_valsvec";
    string genname_embedded_vec = genname + "_embvec";
    str = mputprintf(str, "{ %d, %d, %s, %d, %s, %d, %s, %d, %s }",
      field_index,
      omit_before, (omit_before==-1)?"NULL":("\""+omit_before_name+"\"").c_str(),
      omit_after,  (omit_after==-1)?"NULL":("\""+omit_after_name+"\"").c_str(),
480
481
      static_cast<int>( values_m.size() ), (values_m.size()>0) ? genname_values_vec.c_str(): "NULL",
      static_cast<int>( descr_m.size() ), (descr_m.size()>0) ? genname_embedded_vec.c_str(): "NULL");
Elemer Lelik's avatar
Elemer Lelik committed
482
483
484
485
486
487
488
489
    return str;
  }

  void ErroneousDescriptor::chk_recursions(ReferenceChain& refch)
  {
    for (size_t i=0; i<values_m.size(); i++) {
      values_m.get_nth_elem(i)->chk_recursions(refch);
    }
490
      
Elemer Lelik's avatar
Elemer Lelik committed
491
492
493
494
495
    for (size_t i=0; i<descr_m.size(); i++) {
      descr_m.get_nth_elem(i)->chk_recursions(refch);
    }
  }

496
  char* ErroneousDescriptor::generate_code_str(char *str, char *& def, string genname)
Elemer Lelik's avatar
Elemer Lelik committed
497
498
  {
    genname += "_err_descr";
499
500
501
    str = generate_code_embedded_str(str, def, genname);
    str = mputprintf(str, "%sErroneous_descriptor_t %s = ",
      split_to_slices ? "" : "static ", genname.c_str());
502
    str = generate_code_struct_str(str, def, genname, -1);
Elemer Lelik's avatar
Elemer Lelik committed
503
    str = mputstr(str, ";\n");
504
    if (split_to_slices) {
505
506
      def = mputprintf(def, "extern Erroneous_descriptor_t %s;\n", genname.c_str());
    }
Elemer Lelik's avatar
Elemer Lelik committed
507
508
    return str;
  }
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
  
  ErroneousDescriptors::~ErroneousDescriptors()
  {
    descr_map.clear();
  }
  
  void ErroneousDescriptors::add(Statement* p_update_statement, ErroneousDescriptor* p_descr)
  {
    descr_map.add(p_update_statement, p_descr);
  }
  
  bool ErroneousDescriptors::has_descr(Statement* p_update_statement)
  {
    return descr_map.has_key(p_update_statement);
  }
  
  size_t ErroneousDescriptors::get_descr_index(Statement* p_update_statement)
  {
    return descr_map.find_key(p_update_statement);
  }
  
  char* ErroneousDescriptors::generate_code_init_str(Statement* p_update_statement, char *str, string genname)
  {
    size_t i = descr_map.find_key(p_update_statement);
    return descr_map.get_nth_elem(i)->generate_code_init_str(str,
534
      genname + string("_") + Int2string(static_cast<int>(i)) + string("_err_descr"));
535
536
537
538
539
540
  }
  
  char* ErroneousDescriptors::generate_code_str(Statement* p_update_statement, char *str, char *& def, string genname)
  {
    size_t i = descr_map.find_key(p_update_statement);
    return descr_map.get_nth_elem(i)->generate_code_str(str, def,
541
      genname + string("_") + Int2string(static_cast<int>(i)));
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
  }
  
  void ErroneousDescriptors::chk_recursions(ReferenceChain& refch)
  {
    for (size_t i = 0; i < descr_map.size(); ++i) {
      descr_map.get_nth_elem(i)->chk_recursions(refch);
    }
  }
  
  boolean ErroneousDescriptors::can_have_err_attribs(Type* t)
  {
    if (t == NULL) {
      FATAL_ERROR("ErroneousDescriptors::can_have_err_attribs");
    }
    t = t->get_type_refd_last();
    switch (t->get_typetype_ttcn3()) {
    case Type::T_SEQ_T:
    case Type::T_SET_T:
      if (t->get_nof_comps() == 0) {
        // empty records/sets can't have erroneous attributes
        return FALSE;
      }
      // else fall through
    case Type::T_SEQOF:
    case Type::T_SETOF:
    case Type::T_CHOICE_T:
      return use_runtime_2;
    default:
      return FALSE;
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
573
574
575
576
577
578
579
580
581
582
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641

  // ==== ErroneousAttributes ====

  ErroneousAttributes::ErroneousAttributes(Type* p_type):
    type(p_type), err_descr_tree(NULL)
  {
    if (!type) FATAL_ERROR("ErroneousAttributes::ErroneousAttributes()");
  }

  ErroneousAttributes::~ErroneousAttributes()
  {
    for (size_t i=0; i<spec_vec.size(); i++) {
      delete spec_vec[i];
    }
    spec_vec.clear();
    if (err_descr_tree) delete err_descr_tree;
  }

  ErroneousAttributes::ErroneousAttributes(const ErroneousAttributes& p)
    : Node(p), type(p.type), err_descr_tree(NULL)
  {
  }

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

  void ErroneousAttributes::set_fullname(const string& p_fullname)
  {
    Node::set_fullname(p_fullname);
    for (size_t i=0; i<spec_vec.size(); i++) {
      spec_vec[i]->set_fullname(p_fullname+".<erroneous_attr_spec_"+Int2string(i)+">");
    }
  }

  void ErroneousAttributes::dump(unsigned level) const
  {
    DEBUG(level, "erroneous attributes:");
    for (size_t i=0; i<spec_vec.size(); i++) {
      spec_vec[i]->dump(level+1);
    }
  }

  void ErroneousAttributes::add_spec(ErroneousAttributeSpec* err_attr_spec)
  {
    if (!err_attr_spec) FATAL_ERROR("ErroneousAttributes::add_spec()");
    spec_vec.add(err_attr_spec);
  }

  void ErroneousAttributes::add_pair(const Qualifier* qualifier, ErroneousAttributeSpec* err_attr_spec)
  {
    if (!qualifier || !err_attr_spec) FATAL_ERROR("ErroneousAttributes::add()");
    field_err_t f;
    f.qualifier = qualifier;
    f.err_attr = err_attr_spec;
    field_array.add(f);
  }

  void ErroneousAttributes::chk()
  {
    // check that encodings of erroneous type and templateinstance type match
    for (size_t i=0; i<spec_vec.size(); i++) {
      ErroneousAttributeSpec* act_attr = spec_vec[i];
      Type* ti_type = act_attr->get_type();
      if ((act_attr->get_indicator()!=ErroneousAttributeSpec::I_INVALID) && ti_type) {
        if (act_attr->get_is_raw()) {
          switch (ti_type->get_typetype_ttcn3()) {
          case Type::T_BSTR:
642
643
            if (!type->has_encoding(Type::CT_RAW)) {
              act_attr->error("A `raw' %s value was used for erroneous type `%s' which has no RAW encoding.",
Elemer Lelik's avatar
Elemer Lelik committed
644
645
646
647
                              ti_type->get_typename().c_str(), type->get_typename().c_str());
            }
            break;
          case Type::T_CSTR:
Elemer Lelik's avatar
Elemer Lelik committed
648
649
650
            if (!type->has_encoding(Type::CT_TEXT) && !type->has_encoding(Type::CT_XER) &&
                !type->has_encoding(Type::CT_JSON)) {
              act_attr->error("A `raw' %s value was used for erroneous type `%s' which has no TEXT, XER or JSON encodings.",
Elemer Lelik's avatar
Elemer Lelik committed
651
652
653
654
                              ti_type->get_typename().c_str(), type->get_typename().c_str());
            }
            break;
          case Type::T_USTR:
Elemer Lelik's avatar
Elemer Lelik committed
655
            if (!type->has_encoding(Type::CT_XER) && !type->has_encoding(Type::CT_JSON)) {
656
              act_attr->error("A `raw' %s value was used for erroneous type `%s' which has no XER or JSON encodings.",
Elemer Lelik's avatar
Elemer Lelik committed
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
                              ti_type->get_typename().c_str(), type->get_typename().c_str());
            }
            break;
          default:
            break;
          }
        } else {
          // the two types must have at least one common encoding
          if (!((type->has_encoding(Type::CT_BER)&&ti_type->has_encoding(Type::CT_BER)) ||
                (type->has_encoding(Type::CT_RAW)&&ti_type->has_encoding(Type::CT_RAW)) ||
                (type->has_encoding(Type::CT_TEXT)&&ti_type->has_encoding(Type::CT_TEXT)) ||
                (type->has_encoding(Type::CT_XER)&&ti_type->has_encoding(Type::CT_XER)) ||
                (type->has_encoding(Type::CT_JSON)&&ti_type->has_encoding(Type::CT_JSON)))) {
            act_attr->error("Type `%s' and type `%s' have no common encoding",
              ti_type->get_typename().c_str(), type->get_typename().c_str());
          }
        }
      }
    }

    // for every erroneous field calculate the corresponding index and type arrays
    // for example: x[5].z -> [3,5,2] and [MyRec,MyRecOf,MyUnion]
    // MyRec.x field has index 3, etc.
    for (size_t i=0; i<field_array.size(); i++) {
      bool b = type->get_subrefs_as_array(field_array[i].qualifier, field_array[i].subrefs_array, field_array[i].type_array);
      if (!b) FATAL_ERROR("ErroneousAttributes::chk()");
    }
    // check the qualifiers and build the tree
    err_descr_tree = build_descr_tree(field_array);
  }

  ErroneousDescriptor* ErroneousAttributes::build_descr_tree(dynamic_array<field_err_t>& fld_array)
  {
    ErroneousDescriptor* err_descr = new ErroneousDescriptor();
    const Qualifier * omit_before_qual = NULL, * omit_after_qual = NULL;
    map< size_t, dynamic_array<field_err_t> > embedded_fld_array_m; // used for recursive calls
    for (size_t i=0; i<fld_array.size(); i++) {
      field_err_t& act_field_err = fld_array[i];
      if (act_field_err.subrefs_array.size()<1) FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
      size_t fld_idx = act_field_err.subrefs_array[0];
697
      if (omit_before_qual && (err_descr->omit_before!=-1) && (err_descr->omit_before>static_cast<int>(fld_idx))) {
Elemer Lelik's avatar
Elemer Lelik committed
698
699
700
701
702
        act_field_err.qualifier->error(
          "Field `%s' cannot be referenced because all fields before field `%s' have been omitted",
          act_field_err.qualifier->get_stringRepr().c_str(), omit_before_qual->get_stringRepr().c_str());
        continue;
      }
703
      if (omit_after_qual && (err_descr->omit_after!=-1) && (err_descr->omit_after<static_cast<int>(fld_idx))) {
Elemer Lelik's avatar
Elemer Lelik committed
704
705
706
707
708
709
        act_field_err.qualifier->error(
          "Field `%s' cannot be referenced because all fields after field `%s' have been omitted",
          act_field_err.qualifier->get_stringRepr().c_str(), omit_after_qual->get_stringRepr().c_str());
        continue;
      }
      if (act_field_err.subrefs_array.size()==1) { // erroneous value
710
711
        ErroneousAttributeSpec::indicator_t act_indicator = act_field_err.err_attr->get_indicator();
        bool is_omit = act_field_err.err_attr->get_is_omit();
Elemer Lelik's avatar
Elemer Lelik committed
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
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
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
        if (act_field_err.type_array.size()!=1) FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
        if ((act_field_err.type_array[0]->get_typetype()==Type::T_SET_A) &&
            is_omit && (act_indicator!=ErroneousAttributeSpec::I_VALUE)) {
          act_field_err.qualifier->error(
            "Cannot omit all fields %s `%s' which is a field of an ASN.1 SET type",
            ErroneousAttributeSpec::get_indicator_str(act_indicator), act_field_err.qualifier->get_stringRepr().c_str());
          act_field_err.qualifier->note(
            "The order of fields in ASN.1 SET types changes depending on tagging (see X.690 9.3). "
            "Fields can be omitted individually, independently of the field order which depends on tagging");
            continue;
        }
        switch (act_field_err.type_array[0]->get_typetype_ttcn3()) {
        case Type::T_CHOICE_T:
          if (act_indicator!=ErroneousAttributeSpec::I_VALUE) {
            act_field_err.qualifier->error(
              "Indicator `%s' cannot be used with reference `%s' which points to a field of a union type",
              ErroneousAttributeSpec::get_indicator_str(act_indicator), act_field_err.qualifier->get_stringRepr().c_str());
            continue;
          }
          break;
        case Type::T_SEQ_T:
        case Type::T_SET_T:
          if (is_omit && (act_indicator==ErroneousAttributeSpec::I_AFTER) &&
              (fld_idx==act_field_err.type_array[0]->get_nof_comps()-1)) {
            act_field_err.qualifier->error(
              "There is nothing to omit after the last field (%s) of a record/set type",
              act_field_err.qualifier->get_stringRepr().c_str());
            continue;
          }
          // no break
        case Type::T_SEQOF:
        case Type::T_SETOF:
          if (is_omit && (act_indicator==ErroneousAttributeSpec::I_BEFORE) &&
              (fld_idx==0)) {
            act_field_err.qualifier->error(
              "There is nothing to omit before the first field (%s)",
              act_field_err.qualifier->get_stringRepr().c_str());
            continue;
          }
          break;
        default:
          break;
        }
        // check for duplicate value+indicator
        if (err_descr->values_m.has_key(fld_idx)) {
          ErroneousValues* evs = err_descr->values_m[fld_idx];
          if ( (evs->before && (act_indicator==ErroneousAttributeSpec::I_BEFORE)) ||
               (evs->value && (act_indicator==ErroneousAttributeSpec::I_VALUE)) ||
               (evs->after && (act_indicator==ErroneousAttributeSpec::I_AFTER)) ) {
            act_field_err.qualifier->error(
              "Duplicate reference to field `%s' with indicator `%s'",
              act_field_err.qualifier->get_stringRepr().c_str(), ErroneousAttributeSpec::get_indicator_str(act_indicator));
            continue;
          }
        }
        // when overwriting a value check if embedded values were used
        if ((act_indicator==ErroneousAttributeSpec::I_VALUE) && embedded_fld_array_m.has_key(fld_idx)) {
          act_field_err.qualifier->error(
            "Reference to field `%s' with indicator `value' would invalidate previously specified erroneous data",
            act_field_err.qualifier->get_stringRepr().c_str());
          continue;
        }
        // if before/after omit then check that no references to omitted regions and no duplication of omit before/after rule
        if ((act_indicator==ErroneousAttributeSpec::I_BEFORE) && is_omit) {
          if (omit_before_qual && (err_descr->omit_before!=-1)) {
            act_field_err.qualifier->error(
              "Duplicate rule for omitting all fields before the specified field. Used on field `%s' but previously already used on field `%s'",
              act_field_err.qualifier->get_stringRepr().c_str(), omit_before_qual->get_stringRepr().c_str());
            continue;
          }
          bool is_invalid = false;
          for (size_t j=0; j<err_descr->values_m.size(); j++) {
            if (err_descr->values_m.get_nth_key(j)<fld_idx) {
              is_invalid = true;
              break;
            }
          }
          if (!is_invalid) {
            for (size_t j=0; j<embedded_fld_array_m.size(); j++) {
              if (embedded_fld_array_m.get_nth_key(j)<fld_idx) {
                is_invalid = true;
                break;
              }
            }
          }
          if (is_invalid) {
            act_field_err.qualifier->error(
              "Omitting fields before field `%s' would invalidate previously specified erroneous data",
              act_field_err.qualifier->get_stringRepr().c_str());
            continue;
          }
          // save valid omit before data
          omit_before_qual = act_field_err.qualifier;
          err_descr->omit_before = fld_idx;
          err_descr->omit_before_name = omit_before_qual->get_stringRepr();
          continue;
        }
        if ((act_indicator==ErroneousAttributeSpec::I_AFTER) && is_omit) {
          if (omit_after_qual && (err_descr->omit_after!=-1)) {
            act_field_err.qualifier->error(
              "Duplicate rule for omitting all fields after the specified field. Used on field `%s' but previously already used on field `%s'",
              act_field_err.qualifier->get_stringRepr().c_str(), omit_after_qual->get_stringRepr().c_str());
            continue;
          }
          bool is_invalid = false;
          for (size_t j=0; j<err_descr->values_m.size(); j++) {
            if (err_descr->values_m.get_nth_key(j)>fld_idx) {
              is_invalid = true;
              break;
            }
          }
          if (!is_invalid) {
            for (size_t j=0; j<embedded_fld_array_m.size(); j++) {
              if (embedded_fld_array_m.get_nth_key(j)>fld_idx) {
                is_invalid = true;
                break;
              }
            }
          }
          if (is_invalid) {
            act_field_err.qualifier->error(
              "Omitting fields after field `%s' would invalidate previously specified erroneous data",
              act_field_err.qualifier->get_stringRepr().c_str());
            continue;
          }
          // save valid omit after data
          omit_after_qual = act_field_err.qualifier;
          err_descr->omit_after = fld_idx;
          err_descr->omit_after_name = omit_after_qual->get_stringRepr();
          continue;
        }
        // if not before/after omit then save this into values_m
        bool has_key = err_descr->values_m.has_key(fld_idx);
        ErroneousValues* evs = has_key ? err_descr->values_m[fld_idx] : new ErroneousValues(act_field_err.qualifier->get_stringRepr());
        switch (act_indicator) {
        case ErroneousAttributeSpec::I_BEFORE: evs->before = act_field_err.err_attr; break;
        case ErroneousAttributeSpec::I_VALUE: evs->value = act_field_err.err_attr; break;
        case ErroneousAttributeSpec::I_AFTER: evs->after = act_field_err.err_attr; break;
        default: FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
        }
        if (!has_key) {
          err_descr->values_m.add(fld_idx, evs);
        }
      } else { // embedded err.value
        if ((err_descr->values_m.has_key(fld_idx)) && (err_descr->values_m[fld_idx]->value)) {
          act_field_err.qualifier->error(
            "Field `%s' is embedded into a field which was previously overwritten or omitted",
            act_field_err.qualifier->get_stringRepr().c_str());
          continue;
        }
        // add the embedded field to the map
        bool has_idx = embedded_fld_array_m.has_key(fld_idx);
        dynamic_array<field_err_t>* emb_fld_array = has_idx ? embedded_fld_array_m[fld_idx] : new dynamic_array<field_err_t>();
        field_err_t emb_field_err = act_field_err;
        emb_field_err.subrefs_array.remove(0); // remove the first reference
        emb_field_err.type_array.remove(0);
        emb_fld_array->add(emb_field_err);
        if (!has_idx) {
          embedded_fld_array_m.add(fld_idx, emb_fld_array);
        }
      }
    }
    // recursive calls to create embedded descriptors
    for (size_t i=0; i<embedded_fld_array_m.size(); i++) {
      dynamic_array<field_err_t>* emb_fld_array = embedded_fld_array_m.get_nth_elem(i);
      err_descr->descr_m.add(embedded_fld_array_m.get_nth_key(i), build_descr_tree(*emb_fld_array));
      delete emb_fld_array;
    }
    embedded_fld_array_m.clear();
    return err_descr;
  }

  // ==== AttributeSpec ====

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

  void AttributeSpec::set_fullname(const string& p_fullname)
  {
    Node::set_fullname(p_fullname);
  }

  void AttributeSpec::dump(unsigned level) const
  {
    DEBUG(level,"spec: %s", spec.c_str());
899
900
901
    if (!encoding.empty()) {
      DEBUG(level, "encoding: %s", encoding.c_str());
    }
Elemer Lelik's avatar
Elemer Lelik committed
902
903
904
905
906
907
  }

  // ==== SingleWithAttrib ====

  SingleWithAttrib::SingleWithAttrib(const SingleWithAttrib& p)
    : Node(p), Location(p), attribKeyword(p.attribKeyword),
908
      modifier(p.modifier)
Elemer Lelik's avatar
Elemer Lelik committed
909
910
911
912
913
914
  {
    attribQualifiers = p.attribQualifiers ? p.attribQualifiers->clone() : 0;
    attribSpec = p.attribSpec->clone();
  }

  SingleWithAttrib::SingleWithAttrib(
915
        attribtype_t p_attribKeyword, attribute_modifier_t p_modifier,
Elemer Lelik's avatar
Elemer Lelik committed
916
917
        Qualifiers *p_attribQualifiers, AttributeSpec* p_attribSpec)
    : Node(), Location(), attribKeyword(p_attribKeyword),
918
      modifier(p_modifier), attribQualifiers(p_attribQualifiers),
Elemer Lelik's avatar
Elemer Lelik committed
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
      attribSpec(p_attribSpec)
  {
    if(!p_attribSpec)
      FATAL_ERROR("SingleWithAttrib::SingleWithAttrib()");
  }

  SingleWithAttrib::~SingleWithAttrib()
  {
    delete attribQualifiers;
    delete attribSpec;
  }

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

  void SingleWithAttrib::set_fullname(const string& p_fullname)
  {
    Node::set_fullname(p_fullname);
    if(attribQualifiers)
      attribQualifiers->set_fullname(p_fullname + ".<attribute qualifiers>");
    attribSpec->set_fullname(p_fullname + ".<attribute specification>");
  }

  void SingleWithAttrib::dump(unsigned level) const
  {
    DEBUG(level,"attribute");
    switch(attribKeyword)
    {
      case AT_ENCODE:
	DEBUG(level + 1,"keyword: encode");
	break;
      case AT_VARIANT:
	DEBUG(level + 1,"keyword: variant");
	break;
      case AT_DISPLAY:
       DEBUG(level + 1,"keyword: display");
       break;
      case AT_EXTENSION:
	DEBUG(level + 1,"keyword: extension");
	break;
      case AT_OPTIONAL:
        DEBUG(level + 1,"keyword: optional");
        break;
      case AT_ERRONEOUS:
        DEBUG(level+1, "keyword: erroneous");
        break;
      case AT_INVALID:
        DEBUG(level+1, "invalid keyword");
        break;
      default:
	FATAL_ERROR("SingleWithAttrib::dump()");
    }

974
975
    DEBUG(level + 1, "modifier: %s", modifier == MOD_NONE ? "none" :
      (modifier == MOD_OVERRIDE ? "override" : "@local"));
Elemer Lelik's avatar
Elemer Lelik committed
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

    if(attribSpec)
      attribSpec->dump(level + 1);
    if(attribQualifiers)
      attribQualifiers->dump(level + 1);
  }

  // ==== MultiWithAttrib ====

  MultiWithAttrib::MultiWithAttrib(const MultiWithAttrib& p)
    : Node(p), Location(p)
  {
    for(size_t i = 0; i < p.get_nof_elements(); i++)
    {
      elements.add(p.get_element(i)->clone());
    }
  }

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

  void MultiWithAttrib::set_fullname(const string& p_fullname)
  {
For faster browsing, not all history is shown. View entire blame