AST_ttcn3.cc 424 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
Adam Knapp's avatar
Adam Knapp committed
2
 * Copyright (c) 2000-2021 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
13
 *
 * Contributors:
 *   Baji, Laszlo
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Beres, Szabolcs
 *   Delic, Adam
14
 *   Knapp, Adam
Elemer Lelik's avatar
Elemer Lelik committed
15
16
17
 *   Kovacs, Ferenc
 *   Raduly, Csaba
 *   Szabados, Kristof
18
 *   Szabo, Bence Janos
Elemer Lelik's avatar
Elemer Lelik committed
19
20
21
22
23
 *   Szalai, Gabor
 *   Zalanyi, Balazs Andor
 *   Pandi, Krisztian
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
24
#include "../../common/dbgnew.hh"
25
#include "../../common/version.h"
Elemer Lelik's avatar
Elemer Lelik committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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"
49
#include "../DebuggerStuff.hh"
Elemer Lelik's avatar
Elemer Lelik committed
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
#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:
116
117
118
119
120
121
122
123
124
    case FUNCTION_REF:
      u.ff.id = p.u.ff.id->clone();
      u.ff.checked = p.u.ff.checked;
      if (u.ff.checked) {
        u.ff.ap_list = p.u.ff.ap_list != NULL ? p.u.ff.ap_list->clone() : NULL;
      }
      else {
        u.ff.parsed_pars = p.u.ff.parsed_pars != NULL ? p.u.ff.parsed_pars->clone() : NULL;
      }
Elemer Lelik's avatar
Elemer Lelik committed
125
126
127
128
129
130
131
132
133
      break;
    case ARRAY_REF:
      u.arp = p.u.arp->clone();
      break;
    default:
      FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
    }
  }

134
135
136
  FieldOrArrayRef::FieldOrArrayRef(Identifier *p_id,
                                   ParsedActualParameters* p_params /* = NULL */)
    : Node(), Location(), ref_type(p_params != NULL ? FUNCTION_REF : FIELD_REF)
Elemer Lelik's avatar
Elemer Lelik committed
137
138
  {
    if (!p_id) FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
139
140
141
    u.ff.id = p_id;
    u.ff.parsed_pars = p_params;
    u.ff.checked = false;
Elemer Lelik's avatar
Elemer Lelik committed
142
143
144
145
146
147
148
149
150
151
152
153
154
  }

  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:
155
156
157
158
159
160
161
162
    case FUNCTION_REF:
      delete u.ff.id;
      if (u.ff.checked) {
        delete u.ff.ap_list;
      }
      else {
        delete u.ff.parsed_pars;
      }
Elemer Lelik's avatar
Elemer Lelik committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
      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>");
182
183
184
185
186
187
188
189
    else if (ref_type == FUNCTION_REF) {
      if (u.ff.checked) {
        u.ff.ap_list->set_fullname(p_fullname + ".<parameterlist>");
      }
      else {
        u.ff.parsed_pars->set_fullname(p_fullname + ".<parameterlist>");
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
190
191
192
193
194
  }

  void FieldOrArrayRef::set_my_scope(Scope *p_scope)
  {
    if (ref_type == ARRAY_REF) u.arp->set_my_scope(p_scope);
195
196
197
198
199
200
201
202
    else if (ref_type == FUNCTION_REF) {
      if (u.ff.checked) {
        u.ff.ap_list->set_my_scope(p_scope);
      }
      else {
        u.ff.parsed_pars->set_my_scope(p_scope);
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
203
204
205
206
  }

  const Identifier* FieldOrArrayRef::get_id() const
  {
207
208
    if (ref_type == ARRAY_REF) FATAL_ERROR("FieldOrArrayRef::get_id()");
    return u.ff.id;
Elemer Lelik's avatar
Elemer Lelik committed
209
210
211
212
213
214
215
  }

  Value *FieldOrArrayRef::get_val() const
  {
    if (ref_type != ARRAY_REF) FATAL_ERROR("FieldOrArrayRef::get_val()");
    return u.arp;
  }
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
  
  ParsedActualParameters* FieldOrArrayRef::get_parsed_pars() const
  {
    if (ref_type != FUNCTION_REF || u.ff.checked) {
      FATAL_ERROR("FieldOrArrayRef::get_parsed_pars()");
    }
    return u.ff.parsed_pars;
  }
  
  bool FieldOrArrayRef::parameters_checked() const
  {
    if (ref_type != FUNCTION_REF) {
      FATAL_ERROR("FieldOrArrayRef::parameters_checked()");
    }
    return u.ff.checked;
  }
  
  ActualParList* FieldOrArrayRef::get_actual_par_list() const
  {
    if (ref_type != FUNCTION_REF || !u.ff.checked) {
      FATAL_ERROR("FieldOrArrayRef::get_actual_par_list()");
    }
    return u.ff.ap_list;
  }
  
241
242
243
244
245
246
247
248
249
  FormalParList* FieldOrArrayRef::get_formal_par_list() const
  {
    if (ref_type != FUNCTION_REF || !u.ff.checked) {
      FATAL_ERROR("FieldOrArrayRef::get_formal_par_list()");
    }
    return u.ff.fp_list;
  }
  
  void FieldOrArrayRef::set_parameter_list(ActualParList* p_ap_list, FormalParList* p_fp_list)
250
251
  {
    if (ref_type != FUNCTION_REF || u.ff.checked) {
252
      FATAL_ERROR("FieldOrArrayRef::set_parameter_list()");
253
254
255
256
    }
    u.ff.checked = true;
    delete u.ff.parsed_pars;
    u.ff.ap_list = p_ap_list;
257
    u.ff.fp_list = p_fp_list;
258
  }
Elemer Lelik's avatar
Elemer Lelik committed
259
260
261
262
263

  void FieldOrArrayRef::append_stringRepr(string& str) const
  {
    switch (ref_type) {
    case FIELD_REF:
264
    case FUNCTION_REF:
Elemer Lelik's avatar
Elemer Lelik committed
265
      str += '.';
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
      str += u.ff.id->get_dispname();
      if (ref_type == FUNCTION_REF) {
        size_t nof_pars = u.ff.checked ? u.ff.ap_list->get_nof_pars() :
          u.ff.parsed_pars->get_nof_tis();
        if (nof_pars > 0) {
          str += '(';
          for (size_t i = 0; i < nof_pars; ++i) {
            if (i > 0) {
              str += ", ";
            }
            if (u.ff.checked) {
              u.ff.ap_list->get_par(i)->append_stringRepr(str);
            }
            else {
              u.ff.parsed_pars->get_ti_byIndex(i)->append_stringRepr(str);
            }
          }
          str += ')';
        }
      }
Elemer Lelik's avatar
Elemer Lelik committed
286
287
288
289
290
291
292
293
294
295
      break;
    case ARRAY_REF:
      str += '[';
      str += u.arp->get_stringRepr();
      str += ']';
      break;
    default:
      str += "<unknown sub-reference>";
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
296
297
298
299
  
  void FieldOrArrayRef::set_field_name_to_lowercase()
  {
    if (ref_type != FIELD_REF) FATAL_ERROR("FieldOrArrayRef::set_field_name_to_lowercase()");
300
    string new_name = u.ff.id->get_name();
Elemer Lelik's avatar
Elemer Lelik committed
301
    if (isupper(new_name[0])) {
302
      new_name[0] = static_cast<char>( tolower(new_name[0] ));
Elemer Lelik's avatar
Elemer Lelik committed
303
304
305
306
307
308
309
310
      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, "");
      }
311
312
      delete u.ff.id;
      u.ff.id = new Identifier(Identifier::ID_NAME, new_name);
Elemer Lelik's avatar
Elemer Lelik committed
313
314
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
315
316
317
318
319
320

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

  FieldOrArrayRefs::FieldOrArrayRefs(const FieldOrArrayRefs& p)
321
    : Node(p), refs_str_element(false), my_scope(NULL), checked(false)
Elemer Lelik's avatar
Elemer Lelik committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
  {
    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
342
        ".<sub_reference" + Int2string(i + 1) + ">");
Elemer Lelik's avatar
Elemer Lelik committed
343
344
345
346
  }

  void FieldOrArrayRefs::set_my_scope(Scope *p_scope)
  {
347
    my_scope = p_scope;
Elemer Lelik's avatar
Elemer Lelik committed
348
349
350
351
352
353
354
355
    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
356
357
358
        Value *v = ref->get_val();
        v->set_lowerid_to_ref();
        if (v->is_unfoldable()) return true;
Elemer Lelik's avatar
Elemer Lelik committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
      }
    }
    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,
393
394
    Common::Assignment *ass, Common::Scope* ref_scope, 
    bool const_ref /* = false */, size_t nof_subrefs /* = UINT_MAX*/)
Elemer Lelik's avatar
Elemer Lelik committed
395
396
397
398
399
400
401
402
  {
    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
403
    case Common::Assignment::A_EXCEPTION:         // a Def_Exception
Elemer Lelik's avatar
Elemer Lelik committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
    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;
    }
438
    generate_code(expr, type, ref_scope, const_ref, is_template, nof_subrefs);
439
440
441
  }
  
  void FieldOrArrayRefs::generate_code(expression_struct* expr, Type* type,
442
443
                                       Common::Scope* ref_scope,
                                       bool const_ref, /* = false */
444
445
                                       bool is_template, /* = false */
                                       size_t nof_subrefs /* = UINT_MAX */)
446
  {    
Elemer Lelik's avatar
Elemer Lelik committed
447
448
449
450
451
452
453
454
455
456
457
    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();
458
        // todo: convert back to non-const if the previous type wasn't a class and the current one is
459
460
        expr->expr = mputprintf(expr->expr, "%s%s%s%s",
          (type != NULL && type->get_typetype() == Type::T_CLASS) ? "->" : ".",
Elemer Lelik's avatar
Elemer Lelik committed
461
          ((type!=0 && type->get_typetype()==Type::T_ANYTYPE) ? "AT_" : ""),
462
463
          id.get_name().c_str(),
          (type != NULL && type->get_typetype() == Type::T_CLASS) ? "" : "()");
Elemer Lelik's avatar
Elemer Lelik committed
464
        if (type) {
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
          if (type->get_type_refd_last()->get_typetype() != Type::T_CLASS) {
            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, "()");
            // Follow the field type.
            type = cf->get_type();
          }
          else { // class
            type = type->get_class_type_body()->
              get_local_ass_byId(*ref->get_id())->get_Type();
          }
        }
      } else if (ref->get_type() == FieldOrArrayRef::FUNCTION_REF) {
        if (type == NULL) {
          FATAL_ERROR("FieldOrArrayRefs::generate_code");
        }
        ClassTypeBody* class_ = type->get_class_type_body();
        if (const_ref && i > 0 &&
489
            refs[i - 1]->get_type() != FieldOrArrayRef::FUNCTION_REF) { // todo: convert if previous type wasn't a class
490
491
492
493
494
495
496
497
          // all class methods are non-const, have to convert the current object
          // to non-const
          char* prev_expr = expr->expr;
          expr->expr = mprintf("const_cast< %s&>(%s)",
            type->get_genname_value(ref_scope).c_str(), prev_expr);
          Free(prev_expr);
        }
        const Identifier& id = *ref->get_id();
498
499
500
        // 'ass' is null if the 'toString' method from the 'object' class is called
        Common::Assignment* ass = class_->has_local_ass_withId(id) ?
          class_->get_local_ass_byId(id) : NULL;
501
        expr->expr = mputprintf(expr->expr, "->%s(", id.get_name().c_str());
502
503
504
505
506
507
        FormalParList* fp_list = ass != NULL ? ass->get_FormalParList() :
          new FormalParList; // the formal parameter list of 'toString' is empty
        ref->get_actual_par_list()->generate_code_noalias(expr, fp_list);
        if (ass == NULL) {
          delete fp_list;
        }
508
        expr->expr = mputc(expr->expr, ')');
509
510
        type = ass != NULL ? ass->get_Type() :
          Common::Type::get_pooltype(Common::Type::T_USTR);
511
512
513
514
515
516
517
518
519
520
        if (const_ref && i < n_refs - 1 &&
            refs[i + 1]->get_type() != FieldOrArrayRef::FUNCTION_REF) {
          // the next subreference is a field name or array index, have to
          // convert the resulting object back to const
          // (use static_cast, since methods return temporary objects, not
          // references)
          char* prev_expr = expr->expr;
          expr->expr = mprintf("static_cast<const %s>(%s)",
            type->get_genname_value(ref_scope).c_str(), prev_expr);
          Free(prev_expr);
Elemer Lelik's avatar
Elemer Lelik committed
521
        }
Elemer Lelik's avatar
Elemer Lelik committed
522
523
      } else {
        // Generate code for array reference.
524
525
526
527
528
529
530
        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) {
531
            len = static_cast<int>( pt->get_dimension()->get_size() );
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
            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
547
        if (type) {
Elemer Lelik's avatar
Elemer Lelik committed
548
          // Follow the embedded type.
Elemer Lelik's avatar
Elemer Lelik committed
549
550
551
552
553
554
555
556
557
558
559
560
          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
561
562
563
564
565
566
567
568
      } // 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);
  }
569
  
570
571
572
573
574
575
576
577
578
579
  bool FieldOrArrayRefs::has_function_ref() const
  {
    for (size_t i = 0; i < refs.size(); ++i) {
      if (refs[i]->get_type() == FieldOrArrayRef::FUNCTION_REF) {
        return true;
      }
    }
    return false;
  }
  
580
581
582
583
584
585
586
  void FieldOrArrayRefs::use_default_alternative(size_t p_idx, const Identifier& p_alt_name)
  {
    FieldOrArrayRef* alt_ref = new FieldOrArrayRef(new Identifier(p_alt_name));
    alt_ref->set_my_scope(my_scope);
    refs.insert(alt_ref, p_idx);
    set_fullname(get_fullname());
  }
Elemer Lelik's avatar
Elemer Lelik committed
587
588
589
590
591
592
593
594
595
596

  // =================================
  // ===== 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;
597
    params_checked = p.params_checked;
Elemer Lelik's avatar
Elemer Lelik committed
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
  }

  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);
    }
  }
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
  
  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) */
679
    case Common::Assignment::A_EXCEPTION:      /**< exception (TTCN-3) */
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
    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
719
720
721
722
723
724
725
726
727

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

  // =================================
  // ===== Reference
  // =================================
728
729
  
  Reference::Reference(const Reference& p)
730
    : Ref_base(p), reftype(p.reftype), gen_const_prefix(false),
731
    expr_cache(NULL)
732
733
  {
    params = p.params != NULL ? p.params->clone() : NULL;
734
    parlist = p.parlist != NULL ? p.parlist->clone() : NULL;
735
  }
Elemer Lelik's avatar
Elemer Lelik committed
736
737

  Reference::Reference(Identifier *p_id)
738
739
    : Ref_base(), reftype(REF_BASIC), parlist(NULL), params(NULL),
    gen_const_prefix(false), expr_cache(NULL)
Elemer Lelik's avatar
Elemer Lelik committed
740
741
742
  {
    subrefs.add(new FieldOrArrayRef(p_id));
  }
743
  
744
745
746
747
748
749
750
751
752
  Reference::Reference(reftype_t p_reftype)
  : Ref_base(), reftype(p_reftype), parlist(NULL), params(NULL),
    gen_const_prefix(false), expr_cache(NULL)
  {
    if (reftype != REF_THIS) {
      FATAL_ERROR("Ttcn::Reference(): basic or 'super' reference with no ID");
    }
  }
  
753
  Reference::Reference(Identifier *p_modid, Identifier *p_id,
754
755
                       ParsedActualParameters *p_params, reftype_t p_reftype)
    : Ref_base(p_modid, p_id), reftype(p_reftype), parlist(NULL), params(p_params),
756
757
758
759
760
761
    gen_const_prefix(false), expr_cache(NULL)
  {
    if (p_params == NULL) {
      FATAL_ERROR("Ttcn::Reference::Reference(): NULL parameter");
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
762
763
764

  Reference::~Reference()
  {
765
766
767
768
769
    delete parlist;
    delete params;
    Free(expr_cache);
  }
  
770
  bool Reference::has_parameters() const
771
772
  {
    return params_checked ? parlist != NULL : params != NULL;
Elemer Lelik's avatar
Elemer Lelik committed
773
774
775
  }

  /* Called by:
Elemer Lelik's avatar
Elemer Lelik committed
776
777
778
779
   *         Common::PortTypeBody::PortTypeBody
   *         Common::Type::Type
   *         Common::TypeMappingTarget::TypeMappingTarget
   *         Common::PatternString::ps_elem_t::chk_ref */
Elemer Lelik's avatar
Elemer Lelik committed
780
781
782
783
  Reference *Reference::clone() const
  {
    return new Reference(*this);
  }
784
  
785
786
787
788
789
790
791
792
793
794
795
  void Reference::set_fullname(const string& p_fullname)
  {
    Ref_base::set_fullname(p_fullname);
    if (parlist != NULL) {
      parlist->set_fullname(p_fullname);
    }
    if (params != NULL) {
      params->set_fullname(p_fullname);
    }
  }
  
796
797
798
799
800
801
  void Reference::set_my_scope(Scope *p_scope)
  {
    Ref_base::set_my_scope(p_scope);
    if (parlist != NULL) {
      parlist->set_my_scope(p_scope);
    }
802
803
804
    if (params != NULL) {
      params->set_my_scope(p_scope);
    }
805
  }
Elemer Lelik's avatar
Elemer Lelik committed
806
807
808

  string Reference::get_dispname()
  {
809
    if (is_erroneous) return string("erroneous");
Elemer Lelik's avatar
Elemer Lelik committed
810
    string ret_val;
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
    if (!has_parameters()) { // TODO: simplify
      if (id) {
        if (modid) {
          ret_val += modid->get_dispname();
          ret_val += '.';
        }
        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, "");
      }
    }
    else { // has_parameters()
Elemer Lelik's avatar
Elemer Lelik committed
827
      if (modid) {
Elemer Lelik's avatar
Elemer Lelik committed
828
829
        ret_val += modid->get_dispname();
        ret_val += '.';
Elemer Lelik's avatar
Elemer Lelik committed
830
831
      }
      ret_val += id->get_dispname();
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
      ret_val += '(';
      if (params_checked) {
        // used after semantic analysis
        for (size_t i = 0; i < parlist->get_nof_pars(); i++) {
          if (i > 0) ret_val += ", ";
          parlist->get_par(i)->append_stringRepr(ret_val);
        }
      } else {
        // used before semantic analysis
        for (size_t i = 0; i < params->get_nof_tis(); i++) {
          if (i > 0) ret_val += ", ";
          params->get_ti_byIndex(i)->append_stringRepr(ret_val);
        }
      }
      ret_val += ')';
Elemer Lelik's avatar
Elemer Lelik committed
847
848
849
850
851
852
853
854
855
856
857
      subrefs.append_stringRepr(ret_val);
    }
    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;
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
      if (params == NULL) {
        // tricky: ass->get_FormalParList() can delete this object (e.x. in case of
        // erroneous recursive templates), avoid touching members for as long as
        // possible...
        FormalParList *fplist = ass->get_FormalParList();
        if (fplist != NULL) {
          if (fplist->has_only_default_values() &&
              Common::Assignment::A_TEMPLATE == ass->get_asstype()) {
            Ttcn::ParsedActualParameters dummy_params;
            Error_Context cntxt(&dummy_params, "In actual parameter list of %s",
                ass->get_description().c_str());
            parlist = new ActualParList();
            is_erroneous = fplist->fold_named_and_chk(&dummy_params, parlist);
            parlist->set_fullname(get_fullname());
            parlist->set_my_scope(my_scope);
          } else {
            error("Reference to parameterized definition `%s' without "
              "actual parameter list", ass->get_id().get_dispname().c_str());
          }
        }
      }
      else { // params != NULL
        if (ass->get_asstype() == Common::Assignment::A_TYPE) {
          Def_Type* def = dynamic_cast<Def_Type*>(ass);
          if (def == NULL) {
            FATAL_ERROR("Reference::get_refd_assignment");
          }
          Type* type = def->get_Type();
          if (type->get_typetype() == Common::Type::T_CLASS) {
            // if the referred assignment is a class type, then the reference and
            // its parameters are meant for the constructor instead
889
            ass = type->get_class_type_body()->get_constructor();
890
891
892
            if (ass == NULL) {
              return NULL;
            }
893
894
          }
        }
895
        FormalParList* fplist = ass->get_FormalParList();
896
897
898
        if (fplist != NULL) {
          Error_Context cntxt(params, "In actual parameter list of %s",
            ass->get_description().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
899
          parlist = new ActualParList();
900
          is_erroneous = fplist->fold_named_and_chk(params, parlist);
Elemer Lelik's avatar
Elemer Lelik committed
901
902
          parlist->set_fullname(get_fullname());
          parlist->set_my_scope(my_scope);
903
904
905
          // the parsed parameter list is no longer needed
          delete params;
          params = NULL;
Elemer Lelik's avatar
Elemer Lelik committed
906
        } else {
907
908
          params->error("The referenced %s cannot have actual parameters",
            ass->get_description().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
909
910
911
912
913
        }
      }
    }
    return ass;
  }
914
915
916
917
  
  Common::Assignment* Reference::get_refd_assignment_last(bool check_parlist)
  {
    Common::Assignment* ass = get_refd_assignment(check_parlist);
918
919
920
    if (ass != NULL && subrefs.get_nof_refs() != 0) {
      switch (ass->get_asstype()) {
      case Common::Assignment::A_VAR:
921
      case Common::Assignment::A_EXCEPTION:
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
      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_VAL_OUT: {
        // there could be class objects in the subreferences, which would change
        // the type of the last assignment
        Type* type = ass->get_Type();
        if (type->get_field_type(&subrefs, Common::Type::EXPECTED_DYNAMIC_VALUE) != NULL) {
          // subrefs are valid
          // TODO: EXPECTED_DYNAMIC_VALUE in all cases?
          for (size_t i = 0; i < subrefs.get_nof_refs(); ++i) {
            type = type->get_type_refd_last();
            FieldOrArrayRef* subref = subrefs.get_ref(i);
            switch (subref->get_type()) {
            case FieldOrArrayRef::FIELD_REF:
            case FieldOrArrayRef::FUNCTION_REF:
              if (type->get_typetype() == Common::Type::T_CLASS) {
                ass = type->get_class_type_body()->
                  get_local_ass_byId(*subref->get_id());
                type = ass->get_Type();
              }
              else {
                type = type->get_comp_byName(*subref->get_id())->get_type();
              }
              break;
            case FieldOrArrayRef::ARRAY_REF:
              if (type->is_structured_type()) {
                type = type->get_ofType();
              }
              break;
952
953
954
            }
          }
        }
955
956
957
        break; }
      default:
        break;
958
959
960
961
      }
    }
    return ass;
  }
Elemer Lelik's avatar
Elemer Lelik committed
962
963
964
965
966
967

  const Identifier* Reference::get_modid()
  {
    if (!id) detect_modid();
    return modid;
  }
968
  
Elemer Lelik's avatar
Elemer Lelik committed
969
970
971
972
973
  const Identifier* Reference::get_id()
  {
    if (!id) detect_modid();
    return id;
  }
974
975
976
977
978
  
  ActualParList* Reference::get_parlist()
  {
    return parlist;
  }
Elemer Lelik's avatar
Elemer Lelik committed
979
980
981

  Type *Reference::chk_variable_ref()
  {
982
    Common::Assignment *t_ass = get_refd_assignment();
Elemer Lelik's avatar
Elemer Lelik committed
983
984
985
986
987
988
    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:
989
    case Common::Assignment::A_EXCEPTION:
Elemer Lelik's avatar
Elemer Lelik committed
990
991
    case Common::Assignment::A_PAR_VAL_OUT:
    case Common::Assignment::A_PAR_VAL_INOUT:
992
993
994
995
      if (has_parameters()) {
        error("Reference without actual parameter list was expected");
        return NULL;
      }
Elemer Lelik's avatar
Elemer Lelik committed
996
997
998
      break;
    default:
      error("Reference to a variable or value parameter was "
Elemer Lelik's avatar
Elemer Lelik committed
999
        "expected instead of %s", t_ass->get_description().c_str());
1000
      return NULL;
Elemer Lelik's avatar
Elemer Lelik committed
1001
1002
1003
1004
1005
1006
    }
    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
1007
        "this context", ret_val->get_typename().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
    }
    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
1018
1019
1020
1021
1022
        switch (t->get_typetype()) {
        case Type::T_ERROR:
          // remain silent
          break;
        case Type::T_COMPONENT:
1023
1024
1025
1026
1027
1028
1029
          if (has_parameters()) {
            error("Reference without actual parameter list was expected");
          }
          else {
            return t;
          }
          break;
Elemer Lelik's avatar
Elemer Lelik committed
1030
1031
1032
1033
        default:
          error("Reference `%s' does not refer to a component type",
            get_dispname().c_str());
        }
Elemer Lelik's avatar
Elemer Lelik committed
1034
      } else {
Elemer Lelik's avatar
Elemer Lelik committed
1035
1036
        error("Reference `%s' does not refer to a type",
          get_dispname().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
1037
1038
1039
1040
      }
    }
    return 0;
  }
1041
1042
1043

  bool Reference::chk_activate_argument()
  {
1044
    Common::Assignment *t_ass = get_refd_assignment();
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
    if (!has_parameters()) {
      error("Reference with actual parameter list was expected in the argument");
      return false;
    }
    if (!t_ass) return false;
    if (t_ass->get_asstype() != Common::Assignment::A_ALTSTEP) {
      error("Reference to an altstep was expected in the argument instead of "
        "%s", t_ass->get_description().c_str());
      return false;
    }
    my_scope->chk_runs_on_clause(t_ass, *this, "activate");
    // the altstep reference cannot have sub-references
    if (get_subrefs()) FATAL_ERROR("Reference::chk_activate_argument()");
    FormalParList *fp_list = t_ass->get_FormalParList();
    // the altstep must have formal parameter list
    if (!fp_list) FATAL_ERROR("Reference::chk_activate_argument()");
    return fp_list->chk_activate_argument(parlist,
      t_ass->get_description().c_str());
  }
Elemer Lelik's avatar
Elemer Lelik committed
1064
1065
1066
1067
1068
1069
  
  bool Reference::has_single_expr()
  {
    if (!Ref_base::has_single_expr()) {
      return false;
    }
1070
1071
1072
    if (reftype == REF_THIS && id == NULL) {
      return false;
    }
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
    Common::Assignment* ass = get_refd_assignment();
    if (subrefs.has_function_ref()) {
      Common::Assignment* last_method = NULL;
      Type* field_type = ass->get_Type()->get_field_type(&subrefs,
        Common::Type::EXPECTED_DYNAMIC_VALUE, NULL, false, &last_method);
      if (field_type != NULL &&
          field_type->get_type_refd_last()->get_typetype() == Common::Type::T_CLASS) {
        return false;
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
1083
    if (parlist != NULL) {
1084
      const FormalParList* fplist = (ass != NULL) ? ass->get_FormalParList() : NULL;
Elemer Lelik's avatar
Elemer Lelik committed
1085
      for (size_t i = 0; i < parlist->get_nof_pars(); i++) {
1086
1087
        if (!parlist->get_par(i)->has_single_expr(
            fplist != NULL ? fplist->get_fp_byIndex(i) : NULL)) {
Elemer Lelik's avatar
Elemer Lelik committed
1088
1089
1090
          return false;
        }
      }
1091
1092
1093
1094
1095
1096
1097
1098
1099
      if (fplist != NULL) {
        size_t num_formal = fplist->get_nof_fps();
        for (size_t i = 0; i < num_formal; ++i) {
          const FormalPar *fp = fplist->get_fp_byIndex(i);
          if (fp->get_eval_type() != NORMAL_EVAL) {
            return false;
          }
        }
      }
Elemer Lelik's avatar
Elemer Lelik committed
1100
    }
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
    for (size_t i = 0; i < subrefs.get_nof_refs(); ++i) {
      FieldOrArrayRef* subref = subrefs.get_ref(i);
      if (subref->get_type() == FieldOrArrayRef::FUNCTION_REF) {
        ActualParList* ap_list = subref->get_actual_par_list();
        FormalParList* fp_list = subref->get_formal_par_list();
        for (size_t j = 0; j < ap_list->get_nof_pars(); ++j) {
          if (!ap_list->get_par(j)->has_single_expr(
              fp_list != NULL ? fp_list->get_fp_byIndex(i) : NULL)) {
            return false;
          }
        }
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
1114
1115
    return true;
  }
Elemer Lelik's avatar
Elemer Lelik committed
1116
  
1117
  void Reference::ref_usage_found()
Elemer Lelik's avatar
Elemer Lelik committed
1118
1119
  {
    Common::Assignment *ass = get_refd_assignment();
1120
    if (!ass) FATAL_ERROR("Reference::ref_usage_found()");
Elemer Lelik's avatar
Elemer Lelik committed
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
    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);
1132
1133
1134
      if (fpar == NULL) {
        FATAL_ERROR("Reference::ref_usage_found()");
      }
Elemer Lelik's avatar
Elemer Lelik committed
1135
1136
      fpar->set_usage_found();
      break; }
1137
1138
1139
1140
1141
1142
1143
    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
1144
1145
1146
1147
    default:
      break;
    }
  }
1148
  
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
  
  void Reference::use_default_alternative(const Identifier& p_alt_name)
  {
    FieldOrArrayRef* alt_ref = new FieldOrArrayRef(new Identifier(p_alt_name));
    alt_ref->set_my_scope(my_scope);
    alt_ref->set_fullname(get_fullname() +
      ".<sub_reference" + Int2string(subrefs.get_nof_refs()) + ">");
    subrefs.add(alt_ref);
  }
  
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
  void Reference::set_code_section(
    GovernedSimple::code_section_t p_code_section)
  {
    Ref_base::set_code_section(p_code_section);
    if (parlist != NULL) {
      for (size_t i = 0; i < parlist->get_nof_pars(); i++) {
        parlist->get_par(i)->set_code_section(p_code_section);
      }
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
1169
1170
1171

  void Reference::generate_code(expression_struct_t *expr)
  {
1172
    ref_usage_found();
Elemer Lelik's avatar
Elemer Lelik committed
1173
1174
    Common::Assignment *ass = get_refd_assignment();
    if (!ass) FATAL_ERROR("Reference::generate_code()");
1175
1176
1177
1178
1179
    if (reftype == REF_THIS) {
      if (id != NULL) {
        expr->expr = mputstr(expr->expr, "this->");
      }
      else { // no 'id' means it's just a 'this' reference
1180
1181
1182
1183
        string tmp_id = my_scope->get_scope_mod_gen()->get_temporary_id();
        expr->preamble = mputprintf(expr->preamble, "%s %s(this);\n",
          ass->get_Type()->get_genname_value(my_scope).c_str(), tmp_id.c_str());
        expr->expr = mputprintf(expr->expr, "%s", tmp_id.c_str());
1184
1185
1186
1187
        return;
      }
    }
    else if (reftype == REF_SUPER) {
1188
1189
      Common::Type* base_type = my_scope->get_scope_class()->get_base_type()->
        get_type_refd_last();
1190
      expr->expr = mputprintf(expr->expr, "%s::",
1191
1192
        base_type->get_class_type_body()->is_built_in() ? "OBJECT" :
        base_type->get_genname_own(my_scope).c_str());
1193
    }
1194
    string const_prefix; // empty by default
1195
    string exception_postfix; // also empty by default
1196
1197
1198
1199
1200
1201
1202
1203
    if (gen_const_prefix) {
      if (ass->get_asstype() == Common::Assignment::A_CONST) {
        const_prefix = "const_";
      }
      else if (ass->get_asstype() == Common::Assignment::A_TEMPLATE) {
        const_prefix = "template_";
      }
    }
1204
1205
1206
    if (ass->get_asstype() == Common::Assignment::A_EXCEPTION) {
      exception_postfix = "()";
    }
1207
    if (parlist != NULL) {
Elemer Lelik's avatar
Elemer Lelik committed
1208
1209
1210
1211
1212
1213
1214
      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,
1215
1216
        LazyFuzzyParamData::in_lazy_or_fuzzy() ?
        LazyFuzzyParamData::add_ref_genname(ass, my_scope).c_str() :
1217
        (const_prefix + ass->get_genname_from_scope(my_scope) + exception_postfix).c_str());
Elemer Lelik's avatar
Elemer Lelik committed
1218
    }
1219
    if (subrefs.get_nof_refs() > 0) subrefs.generate_code(expr, ass, my_scope);
Elemer Lelik's avatar
Elemer Lelik committed
1220
1221
1222
1223
1224
1225
1226
1227
1228
  }

  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
1229
    
1230
    ref_usage_found();
Elemer Lelik's avatar
Elemer Lelik committed
1231
1232
1233
1234
1235
1236
1237
    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:
1238
    case Common::Assignment::A_EXCEPTION:
1239
    case Common::Assignment::A_PAR_VAL:
Elemer Lelik's avatar
Elemer Lelik committed
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
    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_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; }
1252
1253
1254
1255
1256
1257
1258
    case Common::Assignment::A_TEMPLATE:
      if (ass->get_FormalParList() == NULL) {
        // not a parameterized template
        is_template = true;
        break;
      }
      // else fall through
Elemer Lelik's avatar
Elemer Lelik committed
1259
1260
1261
1262
    default:
       generate_code(expr);
       return;
    }
1263
1264
1265
1266
1267
1268
    
    Type *refd_gov = ass->get_Type();
    if (refd_gov == NULL) {
      generate_code(expr);
      return;
    }
Elemer Lelik's avatar
Elemer Lelik committed
1269

1270
1271
1272
1273
    // use a separate expression struct for this reference in case any of the 
    // subreferences need to convert the object further
    expression_struct_t this_expr;
    Code::init_expr(&this_expr);
Elemer Lelik's avatar
Elemer Lelik committed
1274
    if (is_template) {
1275
      this_expr.expr = mputprintf(this_expr.expr, "const_cast< const %s&>(",
Elemer Lelik's avatar
Elemer Lelik committed
1276
1277
        refd_gov->get_genname_template(get_my_scope()).c_str() );
    } else {
1278
1279
      // don't convert to const object if the base reference is of class type
      if (refd_gov->get_type_refd_last()->get_typetype() != Common::Type::T_CLASS) {
1280
        this_expr.expr = mputprintf(this_expr.expr, "const_cast< const %s&>(",
1281
          refd_gov->get_genname_value(get_my_scope()).c_str());
1282
      }
Elemer Lelik's avatar
Elemer Lelik committed
1283
    }
1284
1285
1286
1287
    string exception_postfix;
    if (ass->get_asstype() == Common::Assignment::A_EXCEPTION) {
      exception_postfix = "()";
    }
1288
    if (parlist != NULL) {
Elemer Lelik's avatar
Elemer Lelik committed
1289
1290
      // 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()
1291
      this_expr.expr = mputprintf(this_expr.expr, "%s(",
Elemer Lelik's avatar
Elemer Lelik committed
1292
        ass->get_genname_from_scope(my_scope).c_str());
1293
1294
      parlist->generate_code_alias(&this_expr, ass->get_FormalParList(),
        ass->get_RunsOnType(), false);
1295
      this_expr.expr = mputc(this_expr.expr, ')');
Elemer Lelik's avatar
Elemer Lelik committed
1296
    } else {
1297
      this_expr.expr = mputstr(this_expr.expr,
1298
1299
        LazyFuzzyParamData::in_lazy_or_fuzzy() ?
        LazyFuzzyParamData::add_ref_genname(ass, my_scope).c_str() :
1300
        (ass->get_genname_from_scope(my_scope) + exception_postfix).c_str());
Elemer Lelik's avatar
Elemer Lelik committed
1301
    }
1302
    if (refd_gov->get_type_refd_last()->get_typetype() != Common::Type::T_CLASS) {
1303
1304
      this_expr.expr = mputstr(this_expr.expr, ")");
    }
Elemer Lelik's avatar
Elemer Lelik committed
1305

1306
1307
1308
1309
1310
    t_subrefs->generate_code(&this_expr, ass, my_scope, true);
    
    if (this_expr.preamble != NULL) {
      expr->preamble = mputstr(expr->preamble, this_expr.preamble);
    }
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
    
    Common::Assignment* last_method = NULL;
    Type* field_type = refd_gov->get_field_type(t_subrefs,
      Common::Type::EXPECTED_DYNAMIC_VALUE, NULL, false, &last_method);
    if (field_type != NULL && t_subrefs->has_function_ref() &&
        field_type->get_type_refd_last()->get_typetype() == Common::Type::T_CLASS) {
      // If the reference contains a class method call, then the end result is a temporary
      // object in C++. If it's of a class type, then the C++ compiler can't automatically
      // convert the temporary object to a constant reference of a base class (in case the
      // reference is used as an 'in' actual parameter).
      // For safety, the end result is copied in these cases.
      string tmp_str = my_scope->get_scope_mod_gen()->get_temporary_id();
      expr->preamble = mputprintf(expr->preamble,
        "%s %s(%s);\n",
        field_type->get_genname_value(my_scope).c_str(), tmp_str.c_str(), this_expr.expr);
      if (this_expr.postamble != NULL) {
        expr->preamble = mputstr(expr->preamble, this_expr.postamble);
      }
      expr->expr = mputstr(expr->expr, tmp_str.c_str());
    }
    else {
      expr->expr = mputstr(expr->expr, this_expr.expr);
      if (this_expr.postamble != NULL) {
        expr->postamble = mputstr(expr->postamble, this_expr.postamble);
      }
1336
1337
    }
    Code::free_expr(&this_expr);
Elemer Lelik's avatar
Elemer Lelik committed
1338
1339
1340
1341
1342
  }

  void Reference::generate_code_portref(expression_struct_t *expr,
    Scope *p_scope)
  {
1343
    ref_usage_found();
Elemer Lelik's avatar
Elemer Lelik committed
1344
1345
1346
1347
    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());
1348
    if (subrefs.get_nof_refs() > 0) subrefs.generate_code(expr, ass, my_scope);
Elemer Lelik's avatar
Elemer Lelik committed
1349
1350
1351
  }

  //FIXME quick hack
1352
1353
  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
1354
  {
1355
    ref_usage_found();
Elemer Lelik's avatar
Elemer Lelik committed
1356
1357
1358
    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();
1359
1360
1361
1362
1363
1364
1365
1366
1367
    
    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;
1368
1369
1370
    if (t.preamble != NULL) {
      expr->preamble = mputstr(expr->preamble, t.preamble);
    }
1371
1372
1373
1374
1375
1376
    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
1377
1378
1379
1380
    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();
1381
      
Elemer Lelik's avatar
Elemer Lelik committed
1382
1383
      expression_struct isbound_expr;
      Code::init_expr(&isbound_expr);
1384
1385
1386
      isbound_expr.preamble = mputprintf(isbound_expr.preamble,
        "boolean %s = %s.is_bound();\n", tmp_generalid_str,
        ass_id_str);
1387
1388
1389
1390
1391
1392
1393
      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;
1394
1395
      } else if (optype == Value::OPTYPE_ISVALUE) {
        p_optype = ISVALUE;
1396
1397
1398
1399
1400
      } 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
1401
1402
1403
1404
1405
1406
1407

      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 {
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
      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;
1421
1422
1423
        case Value::OPTYPE_ISVALUE:
          expr->expr = mputprintf(expr->expr, "is_value()");
          break;
1424
1425
1426
        default:
          FATAL_ERROR("AST_ttcn3.cc::generate_code_ispresentboundchosen()");
      }
Elemer Lelik's avatar
Elemer Lelik committed
1427
1428
1429
    }
  }

1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
  void Reference::generate_code_cached(expression_struct_t *expr)
  {
    if (expr_cache) {
      expr->expr = mputstr(expr->expr, expr_cache);
    }
    else {
      generate_code(expr);
      expr_cache = mputstr(expr_cache, expr->expr);
    }
  }

Elemer Lelik's avatar
Elemer Lelik committed
1441
1442
1443
  void Reference::detect_modid()
  {
    // do nothing if detection is already performed
1444
    if (id || reftype == REF_THIS) return;
Elemer Lelik's avatar
Elemer Lelik committed
1445
1446
    // the first element of subrefs must be an <id>
    const Identifier *first_id = subrefs.get_ref(0)->get_id(), *second_id = 0;
1447
    const ParsedActualParameters* second_params = 0;
Elemer Lelik's avatar
Elemer Lelik committed
1448
1449
    if (subrefs.get_nof_refs() > 1) {
      FieldOrArrayRef *second_ref = subrefs.get_ref(1);