AST_ttcn3.cc 422 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
Adam Knapp's avatar
Adam Knapp committed
2
 * Copyright (c) 2000-2021 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
13
14
15
16
 *
 * 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
#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:
114
115
116
117
118
119
120
121
122
    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
123
124
125
126
127
128
129
130
131
      break;
    case ARRAY_REF:
      u.arp = p.u.arp->clone();
      break;
    default:
      FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
    }
  }

132
133
134
  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
135
136
  {
    if (!p_id) FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
137
138
139
    u.ff.id = p_id;
    u.ff.parsed_pars = p_params;
    u.ff.checked = false;
Elemer Lelik's avatar
Elemer Lelik committed
140
141
142
143
144
145
146
147
148
149
150
151
152
  }

  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:
153
154
155
156
157
158
159
160
    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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
      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>");
180
181
182
183
184
185
186
187
    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
188
189
190
191
192
  }

  void FieldOrArrayRef::set_my_scope(Scope *p_scope)
  {
    if (ref_type == ARRAY_REF) u.arp->set_my_scope(p_scope);
193
194
195
196
197
198
199
200
    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
201
202
203
204
  }

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

  Value *FieldOrArrayRef::get_val() const
  {
    if (ref_type != ARRAY_REF) FATAL_ERROR("FieldOrArrayRef::get_val()");
    return u.arp;
  }
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  
  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;
  }
  
239
240
241
242
243
244
245
246
247
  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)
248
249
  {
    if (ref_type != FUNCTION_REF || u.ff.checked) {
250
      FATAL_ERROR("FieldOrArrayRef::set_parameter_list()");
251
252
253
254
    }
    u.ff.checked = true;
    delete u.ff.parsed_pars;
    u.ff.ap_list = p_ap_list;
255
    u.ff.fp_list = p_fp_list;
256
  }
Elemer Lelik's avatar
Elemer Lelik committed
257
258
259
260
261

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

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

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

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

  // =================================
  // ===== 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;
595
    params_checked = p.params_checked;
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
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
  }

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

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

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

  Reference::Reference(Identifier *p_id)
736
737
    : Ref_base(), reftype(REF_BASIC), parlist(NULL), params(NULL),
    gen_const_prefix(false), expr_cache(NULL)
Elemer Lelik's avatar
Elemer Lelik committed
738
739
740
  {
    subrefs.add(new FieldOrArrayRef(p_id));
  }
741
  
742
743
744
745
746
747
748
749
750
  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");
    }
  }
  
751
  Reference::Reference(Identifier *p_modid, Identifier *p_id,
752
753
                       ParsedActualParameters *p_params, reftype_t p_reftype)
    : Ref_base(p_modid, p_id), reftype(p_reftype), parlist(NULL), params(p_params),
754
755
756
757
758
759
    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
760
761
762

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

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

  string Reference::get_dispname()
  {
807
    if (is_erroneous) return string("erroneous");
Elemer Lelik's avatar
Elemer Lelik committed
808
    string ret_val;
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
    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
825
      if (modid) {
Elemer Lelik's avatar
Elemer Lelik committed
826
827
        ret_val += modid->get_dispname();
        ret_val += '.';
Elemer Lelik's avatar
Elemer Lelik committed
828
829
      }
      ret_val += id->get_dispname();
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
      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
845
846
847
848
849
850
851
852
853
854
855
      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;
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
      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
887
            ass = type->get_class_type_body()->get_constructor();
888
889
890
            if (ass == NULL) {
              return NULL;
            }
891
892
          }
        }
893
        FormalParList* fplist = ass->get_FormalParList();
894
895
896
        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
897
          parlist = new ActualParList();
898
          is_erroneous = fplist->fold_named_and_chk(params, parlist);
Elemer Lelik's avatar
Elemer Lelik committed
899
900
          parlist->set_fullname(get_fullname());
          parlist->set_my_scope(my_scope);
901
902
903
          // the parsed parameter list is no longer needed
          delete params;
          params = NULL;
Elemer Lelik's avatar
Elemer Lelik committed
904
        } else {
905
906
          params->error("The referenced %s cannot have actual parameters",
            ass->get_description().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
907
908
909
910
911
        }
      }
    }
    return ass;
  }
912
913
914
915
  
  Common::Assignment* Reference::get_refd_assignment_last(bool check_parlist)
  {
    Common::Assignment* ass = get_refd_assignment(check_parlist);
916
917
918
    if (ass != NULL && subrefs.get_nof_refs() != 0) {
      switch (ass->get_asstype()) {
      case Common::Assignment::A_VAR:
919
      case Common::Assignment::A_EXCEPTION:
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
      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;
950
951
952
            }
          }
        }
953
954
955
        break; }
      default:
        break;
956
957
958
959
      }
    }
    return ass;
  }
Elemer Lelik's avatar
Elemer Lelik committed
960
961
962
963
964
965

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

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

  bool Reference::chk_activate_argument()
  {
1042
    Common::Assignment *t_ass = get_refd_assignment();
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
    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
1062
1063
1064
1065
1066
1067
  
  bool Reference::has_single_expr()
  {
    if (!Ref_base::has_single_expr()) {
      return false;
    }
1068
1069
1070
    if (reftype == REF_THIS && id == NULL) {
      return false;
    }
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
    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
1081
    if (parlist != NULL) {
1082
      const FormalParList* fplist = (ass != NULL) ? ass->get_FormalParList() : NULL;
Elemer Lelik's avatar
Elemer Lelik committed
1083
      for (size_t i = 0; i < parlist->get_nof_pars(); i++) {
1084
1085
        if (!parlist->get_par(i)->has_single_expr(
            fplist != NULL ? fplist->get_fp_byIndex(i) : NULL)) {
Elemer Lelik's avatar
Elemer Lelik committed
1086
1087
1088
          return false;
        }
      }
1089
1090
1091
1092
1093
1094
1095
1096
1097
      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
1098
    }
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
    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
1112
1113
    return true;
  }
Elemer Lelik's avatar
Elemer Lelik committed
1114
  
1115
  void Reference::ref_usage_found()
Elemer Lelik's avatar
Elemer Lelik committed
1116
1117
  {
    Common::Assignment *ass = get_refd_assignment();
1118
    if (!ass) FATAL_ERROR("Reference::ref_usage_found()");
Elemer Lelik's avatar
Elemer Lelik committed
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
    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);
1130
1131
1132
      if (fpar == NULL) {
        FATAL_ERROR("Reference::ref_usage_found()");
      }
Elemer Lelik's avatar
Elemer Lelik committed
1133
1134
      fpar->set_usage_found();
      break; }
1135
1136
1137
1138
1139
1140
1141
    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
1142
1143
1144
1145
    default:
      break;
    }
  }
1146
  
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
  
  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);
  }
  
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
  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
1167
1168
1169

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

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

1268
1269
1270
1271
    // 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
1272
    if (is_template) {
1273
      this_expr.expr = mputprintf(this_expr.expr, "const_cast< const %s&>(",
Elemer Lelik's avatar
Elemer Lelik committed
1274
1275
        refd_gov->get_genname_template(get_my_scope()).c_str() );
    } else {
1276
1277
      // 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) {
1278
        this_expr.expr = mputprintf(this_expr.expr, "const_cast< const %s&>(",
1279
          refd_gov->get_genname_value(get_my_scope()).c_str());
1280
      }
Elemer Lelik's avatar
Elemer Lelik committed
1281
    }
1282
1283
1284
1285
    string exception_postfix;
    if (ass->get_asstype() == Common::Assignment::A_EXCEPTION) {
      exception_postfix = "()";
    }
1286
    if (parlist != NULL) {
Elemer Lelik's avatar
Elemer Lelik committed
1287
1288
      // 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()
1289
      this_expr.expr = mputprintf(this_expr.expr, "%s(",
Elemer Lelik's avatar
Elemer Lelik committed
1290
        ass->get_genname_from_scope(my_scope).c_str());
1291
1292
      parlist->generate_code_alias(&this_expr, ass->get_FormalParList(),
        ass->get_RunsOnType(), false);
1293
      this_expr.expr = mputc(this_expr.expr, ')');
Elemer Lelik's avatar
Elemer Lelik committed
1294
    } else {
1295
      this_expr.expr = mputstr(this_expr.expr,
1296
1297
        LazyFuzzyParamData::in_lazy_or_fuzzy() ?
        LazyFuzzyParamData::add_ref_genname(ass, my_scope).c_str() :
1298
        (ass->get_genname_from_scope(my_scope) + exception_postfix).c_str());
Elemer Lelik's avatar
Elemer Lelik committed
1299
    }
1300
    if (refd_gov->get_type_refd_last()->get_typetype() != Common::Type::T_CLASS) {
1301
1302
      this_expr.expr = mputstr(this_expr.expr, ")");
    }
Elemer Lelik's avatar
Elemer Lelik committed
1303

1304
1305
1306
1307
1308
    t_subrefs->generate_code(&this_expr, ass, my_scope, true);
    
    if (this_expr.preamble != NULL) {
      expr->preamble = mputstr(expr->preamble, this_expr.preamble);
    }
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
    
    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);
      }
1334
1335
    }
    Code::free_expr(&this_expr);
Elemer Lelik's avatar
Elemer Lelik committed
1336
1337
1338
1339
1340
  }

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

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

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

1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
  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
1439
1440
1441
  void Reference::detect_modid()
  {
    // do nothing if detection is already performed
1442
    if (id || reftype == REF_THIS) return;
Elemer Lelik's avatar
Elemer Lelik committed
1443
1444
    // the first element of subrefs must be an <id>
    const Identifier *first_id = subrefs.get_ref(0)->get_id(), *second_id = 0;
1445
    const ParsedActualParameters* second_params = 0;
Elemer Lelik's avatar
Elemer Lelik committed
1446
1447
    if (subrefs.get_nof_refs() > 1) {
      FieldOrArrayRef *second_ref = subrefs.get_ref(1);
1448
1449
1450
      if (second_ref->get_type() != FieldOrArrayRef::ARRAY_REF) {
        // the reference begins with <id>.<id> or <id>.<id>(<params>)
        // (most complicated case); there are 3 possible situations:
Elemer Lelik's avatar
Elemer Lelik committed
1451
1452
1453
1454
1455
1456
1457
        // 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
1458
1459
1460
        // the notation <id>.objid { ... }.<id> or <id>.objid { ... }.<id>(<params>)
        // (modid and id are set in the constructor), but there is no work-around
        // in the reverse way.
Elemer Lelik's avatar
Elemer Lelik committed
1461
1462
1463
1464
        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();
1465
1466
1467
1468
          if (second_ref->get_type() == FieldOrArrayRef::FUNCTION_REF) {
            // <id>.<id>(<params>) case
            second_params = second_ref->get_parsed_pars();
          }
Elemer Lelik's avatar
Elemer Lelik committed
1469
        }
Elemer Lelik's avatar
Elemer Lelik committed
1470
1471
1472
1473
1474
      } // 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();
1475
1476
1477
1478
1479
      if (second_params != 0) {
        params = second_params->clone();
        params->set_fullname(second_params->get_fullname());
        params->set_my_scope(second_params->get_my_scope());
      }
Elemer Lelik's avatar
Elemer Lelik committed
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506