AST_ttcn3.cc 389 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
balaskoa's avatar
balaskoa committed
2
 * Copyright (c) 2000-2018 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
 *
 * Contributors:
 *   Baji, Laszlo
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Beres, Szabolcs
 *   Delic, Adam
 *   Kovacs, Ferenc
 *   Raduly, Csaba
 *   Szabados, Kristof
17
 *   Szabo, Bence Janos
Elemer Lelik's avatar
Elemer Lelik committed
18
19
20
21
22
 *   Szalai, Gabor
 *   Zalanyi, Balazs Andor
 *   Pandi, Krisztian
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "../../common/dbgnew.hh"
#include "AST_ttcn3.hh"
#include "../Identifier.hh"
#include "../CompilerError.hh"
#include "../Setting.hh"
#include "../Type.hh"
#include "../CompField.hh"
#include "../CompType.hh"
#include "../TypeCompat.hh"
#include "../Valuestuff.hh"
#include "../Value.hh"
#include "Ttcnstuff.hh"
#include "TtcnTemplate.hh"
#include "Templatestuff.hh"
#include "ArrayDimensions.hh"
#include "compiler.h"
#include "../main.hh"
#include "Statement.hh"
#include "ILT.hh"
#include "Attributes.hh"
#include "PatternString.hh"
#include "../../common/version_internal.h"
#include "../CodeGenHelper.hh"
#include "../../common/JSON_Tokenizer.hh"
47
#include "../DebuggerStuff.hh"
Elemer Lelik's avatar
Elemer Lelik committed
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
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
#include <limits.h>

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

// implemented in compiler.y
extern Ttcn::ErroneousAttributeSpec* ttcn3_parse_erroneous_attr_spec_string(
  const char* p_str, const Common::Location& str_loc);


extern void init_coding_attrib_lex(const Ttcn::AttributeSpec& attrib);
extern int coding_attrib_parse();
extern void cleanup_coding_attrib_lex();
extern Ttcn::ExtensionAttributes *extatrs;

/** Create a field name in the anytype
 *
 * The output of this function will be used to create an identifier
 * to be used as the field name in the anytype.
 * The type_name may be a built-in type (e.g. "integer") or a user-defined
 * type.
 *
 * If the name has multiple components (a fullname?), it keeps just the last
 * component without any dots. *
 * Also, the space in "universal charstring" needs to be replaced
 * with an underscore to make it an identifier.
 *
 * Note: Prefixing with "AT_" is not done here, but in defUnionClass().
 *
 * @param type_name string
 * @return string to be used as the identifier.
 */
string anytype_field(const string& type_name)
{
  string retval(type_name);

  // keep just the last part of the name
  // TODO check if there's a way to get just the last component (note that fetching the string is done outside of this function)
  size_t dot = retval.rfind('.');
  if (dot >= retval.size()) dot = 0;
  else ++dot;
  retval.replace(0, dot, "");

  return retval;
}

extern Common::Modules *modules; // in main.cc

namespace {
static const string _T_("_T_");
}

namespace Ttcn {

  using namespace Common;

  // =================================
  // ===== FieldOrArrayRef
  // =================================

  FieldOrArrayRef::FieldOrArrayRef(const FieldOrArrayRef& p)
    : Node(p), Location(p), ref_type(p.ref_type)
  {
    switch (p.ref_type) {
    case FIELD_REF:
      u.id = p.u.id->clone();
      break;
    case ARRAY_REF:
      u.arp = p.u.arp->clone();
      break;
    default:
      FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
    }
  }

  FieldOrArrayRef::FieldOrArrayRef(Identifier *p_id)
    : Node(), Location(), ref_type(FIELD_REF)
  {
    if (!p_id) FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
    u.id = p_id;
  }

  FieldOrArrayRef::FieldOrArrayRef(Value *p_arp)
    : Node(), Location(), ref_type(ARRAY_REF)
  {
    if (!p_arp) FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
    u.arp = p_arp;
  }

  FieldOrArrayRef::~FieldOrArrayRef()
  {
    switch (ref_type) {
    case FIELD_REF:
      delete u.id;
      break;
    case ARRAY_REF:
      delete u.arp;
      break;
    default:
      FATAL_ERROR("FieldOrArrayRef::~FieldOrArrayRef()");
    }
  }

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

  void FieldOrArrayRef::set_fullname(const string& p_fullname)
  {
    Node::set_fullname(p_fullname);
    if (ref_type == ARRAY_REF)
      u.arp->set_fullname(p_fullname + ".<array_index>");
  }

  void FieldOrArrayRef::set_my_scope(Scope *p_scope)
  {
    if (ref_type == ARRAY_REF) u.arp->set_my_scope(p_scope);
  }

  const Identifier* FieldOrArrayRef::get_id() const
  {
    if (ref_type != FIELD_REF) FATAL_ERROR("FieldOrArrayRef::get_id()");
    return u.id;
  }

  Value *FieldOrArrayRef::get_val() const
  {
    if (ref_type != ARRAY_REF) FATAL_ERROR("FieldOrArrayRef::get_val()");
    return u.arp;
  }

  void FieldOrArrayRef::append_stringRepr(string& str) const
  {
    switch (ref_type) {
    case FIELD_REF:
      str += '.';
      str += u.id->get_dispname();
      break;
    case ARRAY_REF:
      str += '[';
      str += u.arp->get_stringRepr();
      str += ']';
      break;
    default:
      str += "<unknown sub-reference>";
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
197
198
199
200
201
202
  
  void FieldOrArrayRef::set_field_name_to_lowercase()
  {
    if (ref_type != FIELD_REF) FATAL_ERROR("FieldOrArrayRef::set_field_name_to_lowercase()");
    string new_name = u.id->get_name();
    if (isupper(new_name[0])) {
203
      new_name[0] = static_cast<char>( tolower(new_name[0] ));
Elemer Lelik's avatar
Elemer Lelik committed
204
205
206
207
208
209
210
211
212
213
214
215
      if (new_name[new_name.size() - 1] == '_') {
        // an underscore is inserted at the end of the field name if it's
        // a basic type's name (since it would conflict with the class generated
        // for that type)
        // remove the underscore, it won't conflict with anything if its name
        // starts with a lowercase letter
        new_name.replace(new_name.size() - 1, 1, "");
      }
      delete u.id;
      u.id = new Identifier(Identifier::ID_NAME, new_name);
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

  // =================================
  // ===== FieldOrArrayRefs
  // =================================

  FieldOrArrayRefs::FieldOrArrayRefs(const FieldOrArrayRefs& p)
    : Node(p), refs_str_element(false)
  {
    for (size_t i = 0; i < p.refs.size(); i++) refs.add(p.refs[i]->clone());
  }

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

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

  void FieldOrArrayRefs::set_fullname(const string& p_fullname)
  {
    Node::set_fullname(p_fullname);
    for (size_t i = 0; i < refs.size(); i++)
      refs[i]->set_fullname(p_fullname +
Elemer Lelik's avatar
Elemer Lelik committed
243
        ".<sub_reference" + Int2string(i + 1) + ">");
Elemer Lelik's avatar
Elemer Lelik committed
244
245
246
247
248
249
250
251
252
253
254
255
  }

  void FieldOrArrayRefs::set_my_scope(Scope *p_scope)
  {
    for (size_t i = 0; i < refs.size(); i++) refs[i]->set_my_scope(p_scope);
  }

  bool FieldOrArrayRefs::has_unfoldable_index() const
  {
    for (size_t i = 0; i < refs.size(); i++) {
      FieldOrArrayRef *ref = refs[i];
      if (ref->get_type() == FieldOrArrayRef::ARRAY_REF) {
Elemer Lelik's avatar
Elemer Lelik committed
256
257
258
        Value *v = ref->get_val();
        v->set_lowerid_to_ref();
        if (v->is_unfoldable()) return true;
Elemer Lelik's avatar
Elemer Lelik committed
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
      }
    }
    return false;
  }

  void FieldOrArrayRefs::remove_refs(size_t n)
  {
    for (size_t i = 0; i < n; i++) delete refs[i];
    refs.replace(0, n, NULL);
    set_fullname(get_fullname());
  }

  /* remove_last_field is used when unfolding references for
     ischosen and ispresent function operands.
     In this case it is NOT sure the last field exists.
     Calling remove_last_field previously
     will avoid getting the "variable...Has no member called..." error message.
     The last field component will be checked as a separate step.
     Warning: the removed Identifier has to be deleted later */

  Identifier* FieldOrArrayRefs::remove_last_field()
  {
    if (refs.size() == 0) return 0;
    size_t last_elem_ind = refs.size() - 1;
    FieldOrArrayRef* last_elem = refs[last_elem_ind];
    if (last_elem->get_type() == FieldOrArrayRef::FIELD_REF) {
      Identifier *ret_val = last_elem->get_id()->clone();
      delete last_elem;
      refs.replace(last_elem_ind, 1, NULL);
      return ret_val;
    } else return 0;
  }

  void FieldOrArrayRefs::generate_code(expression_struct *expr,
    Common::Assignment *ass, size_t nof_subrefs /* = UINT_MAX*/)
  {
    Type *type = 0;
    bool is_template = false;
    switch (ass->get_asstype()) {
    case Common::Assignment::A_CONST:             // a Def_Const
    case Common::Assignment::A_EXT_CONST:         // a Def_ExtConst
    case Common::Assignment::A_MODULEPAR:         // a Def_Modulepar
    case Common::Assignment::A_VAR:               // a Def_Var
    case Common::Assignment::A_FUNCTION_RVAL:     // a Def_Function
    case Common::Assignment::A_EXT_FUNCTION_RVAL: // a Def_ExtFunction
    case Common::Assignment::A_PAR_VAL_IN:        // a FormalPar
    case Common::Assignment::A_PAR_VAL_OUT:       // a FormalPar
    case Common::Assignment::A_PAR_VAL_INOUT:     // a FormalPar
      // The type is important since the referred entities are value objects.
      type = ass->get_Type();
      break;
    case Common::Assignment::A_MODULEPAR_TEMP:  // a Def_Modulepar_Template
    case Common::Assignment::A_TEMPLATE:        // a Def_Template
    case Common::Assignment::A_VAR_TEMPLATE:    // a Def_Var_Template
    case Common::Assignment::A_PAR_TEMPL_IN:    // a FormalPar
    case Common::Assignment::A_PAR_TEMPL_OUT:   // a FormalPar
    case Common::Assignment::A_PAR_TEMPL_INOUT: // a FormalPar
      // The type is semi-important because fields of anytype templates
      // need the prefix.
      type = ass->get_Type();
      is_template = true;
      break;
    case Common::Assignment::A_TIMER:              // a Def_Timer
    case Common::Assignment::A_PORT:               // a Def_Port
    case Common::Assignment::A_FUNCTION_RTEMP:     // a Def_Function
    case Common::Assignment::A_EXT_FUNCTION_RTEMP: // a Def_ExtFunction
    case Common::Assignment::A_PAR_TIMER:          // a FormalPar
    case Common::Assignment::A_PAR_PORT:           // a FormalPar
      // The type is not relevant (i.e. the optional fields do not require
      // special handling).
      type = 0;
      break;
    default:
      // Reference to other definitions cannot occur during code generation.
      FATAL_ERROR("FieldOrArrayRefs::generate_code()");
      type = 0;
    }
336
337
338
339
340
341
342
    generate_code(expr, type, is_template, nof_subrefs);
  }
  
  void FieldOrArrayRefs::generate_code(expression_struct* expr, Type* type,
                                       bool is_template, /* = false */
                                       size_t nof_subrefs /* = UINT_MAX */)
  {
Elemer Lelik's avatar
Elemer Lelik committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
    size_t n_refs = (nof_subrefs != UINT_MAX) ? nof_subrefs : refs.size();
    for (size_t i = 0; i < n_refs; i++) {
      if (type) type = type->get_type_refd_last();
      //  type changes inside the loop; need to recompute "last" every time.
      FieldOrArrayRef *ref = refs[i];
      if (ref->get_type() == FieldOrArrayRef::FIELD_REF) {
        // Write a call to the field accessor method.
        // Fields of the anytype get a special prefix; see also:
        // Template::generate_code_init_se, TypeConv::gen_conv_func_choice_anytype,
        // defUnionClass and defUnionTemplate.
        const Identifier& id = *ref->get_id();
        expr->expr = mputprintf(expr->expr, ".%s%s()",
          ((type!=0 && type->get_typetype()==Type::T_ANYTYPE) ? "AT_" : ""),
          id.get_name().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
357
358
359
360
361
362
363
364
365
366
        if (type) {
          CompField *cf = type->get_comp_byName(id);
          // If the field is optional, the return type of the accessor is an
          // OPTIONAL<T>. Write a call to OPTIONAL<T>::operator(),
          // which "reaches into" the OPTIONAL to get the contained type T.
          // Don't do this at the end of the reference chain.
          // Accessor methods for a foo_template return a bar_template
          // and OPTIONAL<> is not involved, hence no "()".
          if (!is_template && i < n_refs - 1 && cf->get_is_optional())
            expr->expr = mputstr(expr->expr, "()");
Elemer Lelik's avatar
Elemer Lelik committed
367
          // Follow the field type.
Elemer Lelik's avatar
Elemer Lelik committed
368
369
          type = cf->get_type();
        }
Elemer Lelik's avatar
Elemer Lelik committed
370
371
      } else {
        // Generate code for array reference.
372
373
374
375
376
377
378
        Value* v = ref->get_val();
        Type * pt = v->get_expr_governor_last();
        // If the value is indexed with an array or record of then generate
        // the indexes of the array or record of into the code, one by one.
        if (pt->get_typetype() == Type::T_ARRAY || pt->get_typetype() == Type::T_SEQOF) {
          int len = 0, start = 0;
          if (pt->get_typetype() == Type::T_ARRAY) {
379
            len = static_cast<int>( pt->get_dimension()->get_size() );
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
            start = pt->get_dimension()->get_offset();
          } else if (pt->get_typetype() == Type::T_SEQOF) {
            len = pt->get_sub_type()->get_length_restriction();
          }
          // Generate the indexes as [x][y]...
          for (int j = start; j < start + len; j++) {
            expr->expr = mputc(expr->expr, '[');
            v->generate_code_expr(expr);
            expr->expr = mputprintf(expr->expr, "[%i]]", j);
          }
        } else {
          expr->expr = mputc(expr->expr, '[');
          v->generate_code_expr(expr);
          expr->expr = mputc(expr->expr, ']');
        }
Elemer Lelik's avatar
Elemer Lelik committed
395
        if (type) {
Elemer Lelik's avatar
Elemer Lelik committed
396
          // Follow the embedded type.
Elemer Lelik's avatar
Elemer Lelik committed
397
398
399
400
401
402
403
404
405
406
407
408
          switch (type->get_typetype()) {
          case Type::T_SEQOF:
          case Type::T_SETOF:
          case Type::T_ARRAY:
            type = type->get_ofType();
            break;
          default:
            // The index points to a string element.
            // There are no further sub-references.
            type = 0;
          } // switch
        } // if (type)
Elemer Lelik's avatar
Elemer Lelik committed
409
410
411
412
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
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
      } // if (ref->get_type)
    } // next reference
  }

  void FieldOrArrayRefs::append_stringRepr(string& str) const
  {
    for (size_t i = 0; i < refs.size(); i++) refs[i]->append_stringRepr(str);
  }

  // =================================
  // ===== Ref_base
  // =================================

  Ref_base::Ref_base(const Ref_base& p)
    : Ref_simple(p), subrefs(p.subrefs)
  {
    modid = p.modid ? p.modid->clone() : 0;
    id = p.id ? p.id->clone() : 0;
    params_checked = p.is_erroneous;
  }

  Ref_base::Ref_base(Identifier *p_modid, Identifier *p_id)
    : Ref_simple(), modid(p_modid), id(p_id), params_checked(false)
      , usedInIsbound(false)
  {
    if (!p_id)
      FATAL_ERROR("NULL parameter: Ttcn::Ref_base::Ref_base()");
  }

  Ref_base::~Ref_base()
  {
    delete modid;
    delete id;
  }

  void Ref_base::set_fullname(const string& p_fullname)
  {
    Ref_simple::set_fullname(p_fullname);
    subrefs.set_fullname(p_fullname);
  }

  void Ref_base::set_my_scope(Scope *p_scope)
  {
    Ref_simple::set_my_scope(p_scope);
    subrefs.set_my_scope(p_scope);
  }

  /* returns the referenced variable's base type or value */
  Setting* Ref_base::get_refd_setting()
  {
    Common::Assignment *ass = get_refd_assignment();
    if (ass) return ass->get_Setting();
    else return 0;
  }

  FieldOrArrayRefs *Ref_base::get_subrefs()
  {
    if (!id) get_modid();
    if (subrefs.get_nof_refs() == 0) return 0;
    else return &subrefs;
  }

  bool Ref_base::has_single_expr()
  {
    Common::Assignment *ass = get_refd_assignment();
    if (!ass) FATAL_ERROR("Ref_base::has_single_expr()");
    for (size_t i = 0; i < subrefs.get_nof_refs(); i++) {
      FieldOrArrayRef *ref = subrefs.get_ref(i);
      if (ref->get_type() == FieldOrArrayRef::ARRAY_REF &&
        !ref->get_val()->has_single_expr()) return false;
    }
    return true;
  }

  void Ref_base::set_code_section(
    GovernedSimple::code_section_t p_code_section)
  {
    for (size_t i = 0; i < subrefs.get_nof_refs(); i++) {
      FieldOrArrayRef *ref = subrefs.get_ref(i);
      if (ref->get_type() == FieldOrArrayRef::ARRAY_REF)
        ref->get_val()->set_code_section(p_code_section);
    }
  }
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
  
  void Ref_base::chk_immutability()
  {
    Common::Assignment* ass = get_refd_assignment();
    switch (ass->get_asstype()) {
    case Common::Assignment::A_TYPE:           /**< type */
    case Common::Assignment::A_CONST:          /**< value (const) */
    case Common::Assignment::A_UNDEF:          /**< undefined/undecided (ASN.1) */
    case Common::Assignment::A_ERROR:          /**< erroneous; the kind cannot be deduced (ASN.1) */
    case Common::Assignment::A_OC:             /**< information object class (ASN.1) */
    case Common::Assignment::A_OBJECT:         /**< information object (ASN.1) */
    case Common::Assignment::A_OS:             /**< information object set (ASN.1) */
    case Common::Assignment::A_VS:             /**< value set (ASN.1) */
    case Common::Assignment::A_EXT_CONST:      /**< external constant (TTCN-3) */
    case Common::Assignment::A_MODULEPAR:      /**< module parameter (TTCN-3) */
    case Common::Assignment::A_MODULEPAR_TEMP: /**< template module parameter */
    case Common::Assignment::A_VAR:            /**< variable (TTCN-3) */
    case Common::Assignment::A_VAR_TEMPLATE:   /**< template variable: dynamic template (TTCN-3) */
    case Common::Assignment::A_TIMER:          /**< timer (TTCN-3) */
    case Common::Assignment::A_PORT:           /**< port (TTCN-3) */
    case Common::Assignment::A_ALTSTEP:        /**< altstep (TTCN-3) */
    case Common::Assignment::A_TESTCASE:       /**< testcase Assignment::(TTCN-3) */
    case Common::Assignment::A_PAR_TIMER:      /**< formal parameter (timer) (TTCN-3) */
    case Common::Assignment::A_PAR_PORT:        /**< formal parameter (port) (TTCN-3) */
      break;
    case Common::Assignment::A_TEMPLATE:
      if (get_parlist() != NULL) {
        get_parlist()->chk_immutability();
      }
      break;
    case Common::Assignment::A_FUNCTION:       /**< function without return type (TTCN-3) */
    case Common::Assignment::A_FUNCTION_RVAL:  /**< function that returns a value (TTCN-3) */
    case Common::Assignment::A_FUNCTION_RTEMP: /**< function that returns a template (TTCN-3) */
    case Common::Assignment::A_EXT_FUNCTION:   /**< external function without return type (TTCN-3) */
    case Common::Assignment::A_EXT_FUNCTION_RVAL:  /**< ext. func that returns a value (TTCN-3) */
    case Common::Assignment::A_EXT_FUNCTION_RTEMP: /**< ext. func that returns a template (TTCN-3) */
      warning("Function invocation '%s' may change the actual snapshot.",
        get_dispname().c_str());
      break;
    case Common::Assignment::A_PAR_VAL:        /**< formal parameter (value) (TTCN-3) */
    case Common::Assignment::A_PAR_VAL_IN:     /**< formal parameter (in value) (TTCN-3) */
    case Common::Assignment::A_PAR_VAL_OUT:    /**< formal parameter (out value) (TTCN-3) */
      // TODO: @fuzzy INOUT parameter is not valid
    case Common::Assignment::A_PAR_VAL_INOUT:  /**< formal parameter (inout value) (TTCN-3) */
    case Common::Assignment::A_PAR_TEMPL_IN:   /**< formal parameter ([in] template) (TTCN-3) */
    case Common::Assignment::A_PAR_TEMPL_OUT:  /**< formal parameter (out template) (TTCN-3) */
    case Common::Assignment::A_PAR_TEMPL_INOUT:/**< formal parameter (inout template) (TTCN-3) */
      if (ass->get_eval_type() == FUZZY_EVAL) {
        warning("Fuzzy parameter '%s' may change (during) the actual snapshot.",
          get_dispname().c_str());
      }
      break;
    default:
      FATAL_ERROR("Ref_base::chk_immutability()");
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571

  void Ref_base::generate_code_const_ref(expression_struct_t */*expr*/)
  {
    FATAL_ERROR("Ref_base::generate_code_const_ref()");
  }

  // =================================
  // ===== Reference
  // =================================

  Reference::Reference(Identifier *p_id)
    : Ref_base(), parlist(0)
  {
    subrefs.add(new FieldOrArrayRef(p_id));
  }

  Reference::~Reference()
  {
    if (parlist) {
      delete parlist;
    }
  }

  /* Called by:
Elemer Lelik's avatar
Elemer Lelik committed
572
573
574
575
   *         Common::PortTypeBody::PortTypeBody
   *         Common::Type::Type
   *         Common::TypeMappingTarget::TypeMappingTarget
   *         Common::PatternString::ps_elem_t::chk_ref */
Elemer Lelik's avatar
Elemer Lelik committed
576
577
578
579
  Reference *Reference::clone() const
  {
    return new Reference(*this);
  }
580
581
582
583
584
585
586
587
  
  void Reference::set_my_scope(Scope *p_scope)
  {
    Ref_base::set_my_scope(p_scope);
    if (parlist != NULL) {
      parlist->set_my_scope(p_scope);
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
588
589
590
591
592
593

  string Reference::get_dispname()
  {
    string ret_val;
    if (id) {
      if (modid) {
Elemer Lelik's avatar
Elemer Lelik committed
594
595
        ret_val += modid->get_dispname();
        ret_val += '.';
Elemer Lelik's avatar
Elemer Lelik committed
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
      }
      ret_val += id->get_dispname();
      subrefs.append_stringRepr(ret_val);
    } else {
      subrefs.append_stringRepr(ret_val);
      // cut the leading dot
      if (!ret_val.empty() && ret_val[0] == '.')
        ret_val.replace(0, 1, "");
    }
    return ret_val;
  }

  Common::Assignment* Reference::get_refd_assignment(bool check_parlist)
  {
    Common::Assignment *ass = Ref_base::get_refd_assignment(check_parlist);
    // In fact calls        Ref_simple::get_refd_assignment
    if (ass && check_parlist && !params_checked) {
      params_checked = true;
      FormalParList *fplist = ass->get_FormalParList();
      if (fplist) {
        if (fplist->has_only_default_values()
          && Common::Assignment::A_TEMPLATE == ass->get_asstype()) {
          Ttcn::ParsedActualParameters params;
          Error_Context cntxt(&params, "In actual parameter list of %s",
              ass->get_description().c_str());
          parlist = new ActualParList();
          is_erroneous = fplist->fold_named_and_chk(&params, parlist);
          parlist->set_fullname(get_fullname());
          parlist->set_my_scope(my_scope);
        } else {
          error("Reference to parameterized definition `%s' without "
Elemer Lelik's avatar
Elemer Lelik committed
627
            "actual parameter list", ass->get_id().get_dispname().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
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
        }
      }
    }
    return ass;
  }

  const Identifier* Reference::get_modid()
  {
    if (!id) detect_modid();
    return modid;
  }

  const Identifier* Reference::get_id()
  {
    if (!id) detect_modid();
    return id;
  }

  Type *Reference::chk_variable_ref()
  {
    Common::Assignment *t_ass = get_refd_assignment();
    if (!t_ass) return 0;
    switch (t_ass->get_asstype()) {
    case Common::Assignment::A_PAR_VAL_IN:
      t_ass->use_as_lvalue(*this);
      // no break
    case Common::Assignment::A_VAR:
    case Common::Assignment::A_PAR_VAL_OUT:
    case Common::Assignment::A_PAR_VAL_INOUT:
      break;
    default:
      error("Reference to a variable or value parameter was "
Elemer Lelik's avatar
Elemer Lelik committed
660
        "expected instead of %s", t_ass->get_description().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
661
662
663
664
665
666
667
      return 0;
    }
    FieldOrArrayRefs *t_subrefs = get_subrefs();
    Type *ret_val = t_ass->get_Type()->get_field_type(t_subrefs,
      Type::EXPECTED_DYNAMIC_VALUE);
    if (ret_val && t_subrefs && t_subrefs->refers_to_string_element()) {
      error("Reference to a string element of type `%s' cannot be used in "
Elemer Lelik's avatar
Elemer Lelik committed
668
        "this context", ret_val->get_typename().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
669
670
671
672
673
674
675
676
677
678
    }
    return ret_val;
  }

  Type *Reference::chk_comptype_ref()
  {
    Common::Assignment *ass = get_refd_assignment();
    if (ass) {
      if (ass->get_asstype() == Common::Assignment::A_TYPE) {
        Type *t = ass->get_Type()->get_type_refd_last();
Elemer Lelik's avatar
Elemer Lelik committed
679
680
681
682
683
684
685
686
687
688
        switch (t->get_typetype()) {
        case Type::T_ERROR:
          // remain silent
          break;
        case Type::T_COMPONENT:
          return t;
        default:
          error("Reference `%s' does not refer to a component type",
            get_dispname().c_str());
        }
Elemer Lelik's avatar
Elemer Lelik committed
689
      } else {
Elemer Lelik's avatar
Elemer Lelik committed
690
691
        error("Reference `%s' does not refer to a type",
          get_dispname().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
      }
    }
    return 0;
  }
  
  bool Reference::has_single_expr()
  {
    if (!Ref_base::has_single_expr()) {
      return false;
    }
    if (parlist != NULL) {
      for (size_t i = 0; i < parlist->get_nof_pars(); i++) {
        if (!parlist->get_par(i)->has_single_expr()) {
          return false;
        }
      }
    }
    return true;
  }
Elemer Lelik's avatar
Elemer Lelik committed
711
  
712
  void Reference::ref_usage_found()
Elemer Lelik's avatar
Elemer Lelik committed
713
714
  {
    Common::Assignment *ass = get_refd_assignment();
715
    if (!ass) FATAL_ERROR("Reference::ref_usage_found()");
Elemer Lelik's avatar
Elemer Lelik committed
716
717
718
719
720
721
722
723
724
725
726
    switch (ass->get_asstype()) {
    case Common::Assignment::A_PAR_VAL_OUT:
    case Common::Assignment::A_PAR_TEMPL_OUT:
    case Common::Assignment::A_PAR_VAL:
    case Common::Assignment::A_PAR_VAL_IN:
    case Common::Assignment::A_PAR_VAL_INOUT:
    case Common::Assignment::A_PAR_TEMPL_IN:
    case Common::Assignment::A_PAR_TEMPL_INOUT:
    case Common::Assignment::A_PAR_PORT:
    case Common::Assignment::A_PAR_TIMER: {
      FormalPar *fpar = dynamic_cast<FormalPar*>(ass);
727
728
729
      if (fpar == NULL) {
        FATAL_ERROR("Reference::ref_usage_found()");
      }
Elemer Lelik's avatar
Elemer Lelik committed
730
731
      fpar->set_usage_found();
      break; }
732
733
734
735
736
737
738
    case Common::Assignment::A_EXT_CONST: {
      Def_ExtConst* def = dynamic_cast<Def_ExtConst*>(ass);
      if (def == NULL) {
        FATAL_ERROR("Reference::ref_usage_found()");
      }
      def->set_usage_found();
      break; }
Elemer Lelik's avatar
Elemer Lelik committed
739
740
741
742
    default:
      break;
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
743
744
745

  void Reference::generate_code(expression_struct_t *expr)
  {
746
    ref_usage_found();
Elemer Lelik's avatar
Elemer Lelik committed
747
748
749
750
751
752
753
754
755
756
757
758
    Common::Assignment *ass = get_refd_assignment();
    if (!ass) FATAL_ERROR("Reference::generate_code()");
    if (parlist) {
      // reference without parameters to a template that has only default formal parameters.
      // if @lazy: nothing to do, it's a C++ function call just like in case of Ref_pard::generate_code()
      expr->expr = mputprintf(expr->expr, "%s(",
        ass->get_genname_from_scope(my_scope).c_str());
      parlist->generate_code_alias(expr, ass->get_FormalParList(),
          ass->get_RunsOnType(), false);
      expr->expr = mputc(expr->expr, ')');
    } else {
      expr->expr = mputstr(expr->expr,
759
760
        LazyFuzzyParamData::in_lazy_or_fuzzy() ?
        LazyFuzzyParamData::add_ref_genname(ass, my_scope).c_str() :
Elemer Lelik's avatar
Elemer Lelik committed
761
762
763
764
765
766
767
768
769
770
771
772
        ass->get_genname_from_scope(my_scope).c_str());
    }
    if (subrefs.get_nof_refs() > 0) subrefs.generate_code(expr, ass);
  }

  void Reference::generate_code_const_ref(expression_struct_t *expr)
  {
    FieldOrArrayRefs *t_subrefs = get_subrefs();
    if (!t_subrefs || t_subrefs->get_nof_refs() == 0) {
      generate_code(expr);
      return;
    }
Elemer Lelik's avatar
Elemer Lelik committed
773
    
774
    ref_usage_found();
Elemer Lelik's avatar
Elemer Lelik committed
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
    Common::Assignment *ass = get_refd_assignment();
    if (!ass) FATAL_ERROR("Reference::generate_code_const_ref()");

    bool is_template;
    switch (ass->get_asstype()) {
    case Common::Assignment::A_MODULEPAR:
    case Common::Assignment::A_VAR:
    case Common::Assignment::A_FUNCTION_RVAL:
    case Common::Assignment::A_EXT_FUNCTION_RVAL:
    case Common::Assignment::A_PAR_VAL_IN:
    case Common::Assignment::A_PAR_VAL_OUT:
    case Common::Assignment::A_PAR_VAL_INOUT: {
      is_template = false;
      break; }
    case Common::Assignment::A_MODULEPAR_TEMP:
    case Common::Assignment::A_TEMPLATE:
    case Common::Assignment::A_VAR_TEMPLATE:
    case Common::Assignment::A_PAR_TEMPL_IN:
    case Common::Assignment::A_PAR_TEMPL_OUT:
    case Common::Assignment::A_PAR_TEMPL_INOUT: {
      is_template = true;
      break; }
    case Common::Assignment::A_CONST:
    case Common::Assignment::A_EXT_CONST:
    default:
       generate_code(expr);
       return;
    }

    Type *refd_gov = ass->get_Type();
    if (is_template) {
      expr->expr = mputprintf(expr->expr, "const_cast< const %s&>(",
        refd_gov->get_genname_template(get_my_scope()).c_str() );
    } else {
      expr->expr = mputprintf(expr->expr, "const_cast< const %s&>(",
        refd_gov->get_genname_value(get_my_scope()).c_str());
    }
    if (parlist) {
      // reference without parameters to a template that has only default formal parameters.
      // if @lazy: nothing to do, it's a C++ function call just like in case of Ref_pard::generate_code()
      expr->expr = mputprintf(expr->expr, "%s(",
        ass->get_genname_from_scope(my_scope).c_str());
      parlist->generate_code_alias(expr, ass->get_FormalParList(),
          ass->get_RunsOnType(), false);
      expr->expr = mputc(expr->expr, ')');
    } else {
      expr->expr = mputstr(expr->expr,
822
823
        LazyFuzzyParamData::in_lazy_or_fuzzy() ?
        LazyFuzzyParamData::add_ref_genname(ass, my_scope).c_str() :
Elemer Lelik's avatar
Elemer Lelik committed
824
825
826
827
828
829
830
831
832
833
834
        ass->get_genname_from_scope(my_scope).c_str());
    }
    expr->expr = mputstr(expr->expr, ")");

    if (t_subrefs && t_subrefs->get_nof_refs() > 0)
      t_subrefs->generate_code(expr, ass);
  }

  void Reference::generate_code_portref(expression_struct_t *expr,
    Scope *p_scope)
  {
835
    ref_usage_found();
Elemer Lelik's avatar
Elemer Lelik committed
836
837
838
839
840
841
842
843
    Common::Assignment *ass = get_refd_assignment();
    if (!ass) FATAL_ERROR("Reference::generate_code_portref()");
    expr->expr = mputstr(expr->expr,
      ass->get_genname_from_scope(p_scope).c_str());
    if (subrefs.get_nof_refs() > 0) subrefs.generate_code(expr, ass);
  }

  //FIXME quick hack
844
845
  void Reference::generate_code_ispresentboundchosen(expression_struct_t *expr,
    bool is_template, const Value::operationtype_t optype, const char* field)
Elemer Lelik's avatar
Elemer Lelik committed
846
  {
847
    ref_usage_found();
Elemer Lelik's avatar
Elemer Lelik committed
848
849
850
    Common::Assignment *ass = get_refd_assignment();
    const string& ass_id = ass->get_genname_from_scope(my_scope);
    const char *ass_id_str = ass_id.c_str();
851
852
853
854
855
856
857
858
859
    
    expression_struct_t t;
    Code::init_expr(&t);
    // Generate the default parameters if needed
    if (parlist) {
      parlist->generate_code_alias(&t, ass->get_FormalParList(),
        ass->get_RunsOnType(), false);
    }
    string ass_id2 = ass_id;
860
861
862
    if (t.preamble != NULL) {
      expr->preamble = mputstr(expr->preamble, t.preamble);
    }
863
864
865
866
867
868
    if (t.expr != NULL) {
      ass_id2 = ass_id2 + "(" + t.expr + ")";
      ass_id_str = ass_id2.c_str();
    }
    Code::free_expr(&t);
    
Elemer Lelik's avatar
Elemer Lelik committed
869
870
871
872
    if (subrefs.get_nof_refs() > 0) {
      const string& tmp_generalid = my_scope->get_scope_mod_gen()
          ->get_temporary_id();
      const char *tmp_generalid_str = tmp_generalid.c_str();
873
      
Elemer Lelik's avatar
Elemer Lelik committed
874
875
876
877
878
      expression_struct isbound_expr;
      Code::init_expr(&isbound_expr);
      isbound_expr.preamble = mputprintf(isbound_expr.preamble,
          "boolean %s = %s.is_bound();\n", tmp_generalid_str,
          ass_id_str);
879
880
881
882
883
884
885
886
887
888
889
890
      namedbool p_optype;
      if (optype == Value::OPTYPE_ISBOUND) {
        p_optype = ISBOUND;
      } else if (optype == Value::OPTYPE_ISPRESENT) {
        p_optype = ISPRESENT;
      } else if (optype == Value::OPTYPE_ISCHOSEN_T || optype == Value::OPTYPE_ISCHOSEN_V) {
        p_optype = ISCHOSEN;
      } else {
        FATAL_ERROR("AST_ttcn3.cc::generate_code_ispresentboundchosen()");
      }
      ass->get_Type()->generate_code_ispresentboundchosen(&isbound_expr, &subrefs, my_scope->get_scope_mod_gen(),
          tmp_generalid, ass_id2, is_template, p_optype, field);
Elemer Lelik's avatar
Elemer Lelik committed
891
892
893
894
895
896
897

      expr->preamble = mputstr(expr->preamble, isbound_expr.preamble);
      expr->preamble = mputstr(expr->preamble, isbound_expr.expr);
      Code::free_expr(&isbound_expr);

      expr->expr = mputprintf(expr->expr, "%s", tmp_generalid_str);
    } else {
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
      expr->expr = mputprintf(expr->expr, "%s.", ass_id_str);
      switch (optype) {
        case Value::OPTYPE_ISBOUND:
          expr->expr = mputstr(expr->expr, "is_bound()");
          break;
        case Value::OPTYPE_ISPRESENT:
          expr->expr = mputprintf(expr->expr, "is_present(%s)",
            is_template && omit_in_value_list ? "TRUE" : "");
          break;
        case Value::OPTYPE_ISCHOSEN_T:
        case Value::OPTYPE_ISCHOSEN_V:
          expr->expr = mputprintf(expr->expr, "ischosen(%s)", field);
          break;
        default:
          FATAL_ERROR("AST_ttcn3.cc::generate_code_ispresentboundchosen()");
      }
Elemer Lelik's avatar
Elemer Lelik committed
914
915
916
917
918
919
920
921
922
923
924
925
926
    }
  }

  void Reference::detect_modid()
  {
    // do nothing if detection is already performed
    if (id) return;
    // the first element of subrefs must be an <id>
    const Identifier *first_id = subrefs.get_ref(0)->get_id(), *second_id = 0;
    if (subrefs.get_nof_refs() > 1) {
      FieldOrArrayRef *second_ref = subrefs.get_ref(1);
      if (second_ref->get_type() == FieldOrArrayRef::FIELD_REF) {
        // the reference begins with <id>.<id> (most complicated case)
Elemer Lelik's avatar
Elemer Lelik committed
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
        // there are 3 possible situations:
        // 1. first_id points to a local definition (this has the priority)
        //      modid: 0, id: first_id
        // 2. first_id points to an imported module (trivial case)
        //      modid: first_id, id: second_id
        // 3. none of the above (first_id might be an imported symbol)
        //      modid: 0, id: first_id
        // Note: Rule 1 has the priority because it can be overridden using
        // the notation <id>.objid { ... }.<id> (modid and id are set in the
        // constructor), but there is no work-around in the reverse way.
        if (!my_scope->has_ass_withId(*first_id)
            && my_scope->is_valid_moduleid(*first_id)) {
          // rule 1 is not fulfilled, but rule 2 is fulfilled
          second_id = second_ref->get_id();
        }
Elemer Lelik's avatar
Elemer Lelik committed
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
      } // else: the reference begins with <id>[<arrayref>] -> there is no modid
    } // else: the reference consists of a single <id>  -> there is no modid
    if (second_id) {
      modid = first_id->clone();
      id = second_id->clone();
      subrefs.remove_refs(2);
    } else {
      modid = 0;
      id = first_id->clone();
      subrefs.remove_refs(1);
    }
  }

  // =================================
  // ===== Ref_pard
  // =================================

  Ref_pard::Ref_pard(const Ref_pard& p)
    : Ref_base(p), parlist(p.parlist), expr_cache(0)
  {
    params = p.params ? p.params->clone() : 0;
  }

  Ref_pard::Ref_pard(Identifier *p_modid, Identifier *p_id,
    ParsedActualParameters *p_params)
    : Ref_base(p_modid, p_id), parlist(), params(p_params), expr_cache(0)
  {
    if (!p_params)
      FATAL_ERROR("Ttcn::Ref_pard::Ref_pard(): NULL parameter");
  }

  Ref_pard::~Ref_pard()
  {
    delete params;
    Free(expr_cache);
  }

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

  void Ref_pard::set_fullname(const string& p_fullname)
  {
    Ref_base::set_fullname(p_fullname);
    parlist.set_fullname(p_fullname);
    if (params) params->set_fullname(p_fullname);
  }

  void Ref_pard::set_my_scope(Scope *p_scope)
  {
    Ref_base::set_my_scope(p_scope);
    parlist.set_my_scope(p_scope);
    if (params) params->set_my_scope(p_scope);
  }

  string Ref_pard::get_dispname()
  {
    if (is_erroneous) return string("erroneous");
For faster browsing, not all history is shown. View entire blame