AST_ttcn3.cc 409 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
balaskoa's avatar
balaskoa committed
2
 * Copyright (c) 2000-2020 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
239
240
241
242
243
244
245
246
247
  
  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;
  }
  
  void FieldOrArrayRef::set_actual_par_list(ActualParList* p_ap_list)
  {
    if (ref_type != FUNCTION_REF || u.ff.checked) {
      FATAL_ERROR("FieldOrArrayRef::set_actual_par_list()");
    }
    u.ff.checked = true;
    delete u.ff.parsed_pars;
    u.ff.ap_list = p_ap_list;
  }
Elemer Lelik's avatar
Elemer Lelik committed
248
249
250
251
252

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

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

  FieldOrArrayRefs::FieldOrArrayRefs(const FieldOrArrayRefs& p)
310
    : Node(p), refs_str_element(false), my_scope(NULL)
Elemer Lelik's avatar
Elemer Lelik committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  {
    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
331
        ".<sub_reference" + Int2string(i + 1) + ">");
Elemer Lelik's avatar
Elemer Lelik committed
332
333
334
335
  }

  void FieldOrArrayRefs::set_my_scope(Scope *p_scope)
  {
336
    my_scope = p_scope;
Elemer Lelik's avatar
Elemer Lelik committed
337
338
339
340
341
342
343
344
    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
345
346
347
        Value *v = ref->get_val();
        v->set_lowerid_to_ref();
        if (v->is_unfoldable()) return true;
Elemer Lelik's avatar
Elemer Lelik committed
348
349
350
351
352
353
354
355
356
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
      }
    }
    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,
382
383
    Common::Assignment *ass, Common::Scope* ref_scope, 
    bool const_ref /* = false */, size_t nof_subrefs /* = UINT_MAX*/)
Elemer Lelik's avatar
Elemer Lelik committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
  {
    Type *type = 0;
    bool is_template = false;
    switch (ass->get_asstype()) {
    case Common::Assignment::A_CONST:             // a Def_Const
    case Common::Assignment::A_EXT_CONST:         // a Def_ExtConst
    case Common::Assignment::A_MODULEPAR:         // a Def_Modulepar
    case Common::Assignment::A_VAR:               // a Def_Var
    case Common::Assignment::A_FUNCTION_RVAL:     // a Def_Function
    case Common::Assignment::A_EXT_FUNCTION_RVAL: // a Def_ExtFunction
    case Common::Assignment::A_PAR_VAL_IN:        // a FormalPar
    case Common::Assignment::A_PAR_VAL_OUT:       // a FormalPar
    case Common::Assignment::A_PAR_VAL_INOUT:     // a FormalPar
      // The type is important since the referred entities are value objects.
      type = ass->get_Type();
      break;
    case Common::Assignment::A_MODULEPAR_TEMP:  // a Def_Modulepar_Template
    case Common::Assignment::A_TEMPLATE:        // a Def_Template
    case Common::Assignment::A_VAR_TEMPLATE:    // a Def_Var_Template
    case Common::Assignment::A_PAR_TEMPL_IN:    // a FormalPar
    case Common::Assignment::A_PAR_TEMPL_OUT:   // a FormalPar
    case Common::Assignment::A_PAR_TEMPL_INOUT: // a FormalPar
      // The type is semi-important because fields of anytype templates
      // need the prefix.
      type = ass->get_Type();
      is_template = true;
      break;
    case Common::Assignment::A_TIMER:              // a Def_Timer
    case Common::Assignment::A_PORT:               // a Def_Port
    case Common::Assignment::A_FUNCTION_RTEMP:     // a Def_Function
    case Common::Assignment::A_EXT_FUNCTION_RTEMP: // a Def_ExtFunction
    case Common::Assignment::A_PAR_TIMER:          // a FormalPar
    case Common::Assignment::A_PAR_PORT:           // a FormalPar
      // The type is not relevant (i.e. the optional fields do not require
      // special handling).
      type = 0;
      break;
    default:
      // Reference to other definitions cannot occur during code generation.
      FATAL_ERROR("FieldOrArrayRefs::generate_code()");
      type = 0;
    }
426
    generate_code(expr, type, ref_scope, const_ref, is_template, nof_subrefs);
427
428
429
  }
  
  void FieldOrArrayRefs::generate_code(expression_struct* expr, Type* type,
430
431
                                       Common::Scope* ref_scope,
                                       bool const_ref, /* = false */
432
433
                                       bool is_template, /* = false */
                                       size_t nof_subrefs /* = UINT_MAX */)
434
  {    
Elemer Lelik's avatar
Elemer Lelik committed
435
436
437
438
439
440
441
442
443
444
445
    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();
446
447
        expr->expr = mputprintf(expr->expr, "%s%s%s%s",
          (type != NULL && type->get_typetype() == Type::T_CLASS) ? "->" : ".",
Elemer Lelik's avatar
Elemer Lelik committed
448
          ((type!=0 && type->get_typetype()==Type::T_ANYTYPE) ? "AT_" : ""),
449
450
          id.get_name().c_str(),
          (type != NULL && type->get_typetype() == Type::T_CLASS) ? "" : "()");
Elemer Lelik's avatar
Elemer Lelik committed
451
        if (type) {
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
          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 &&
            refs[i - 1]->get_type() != FieldOrArrayRef::FUNCTION_REF) {
          // 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();
485
486
487
        // '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;
488
        expr->expr = mputprintf(expr->expr, "->%s(", id.get_name().c_str());
489
490
491
492
493
494
        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;
        }
495
        expr->expr = mputc(expr->expr, ')');
496
497
        type = ass != NULL ? ass->get_Type() :
          Common::Type::get_pooltype(Common::Type::T_USTR);
498
499
500
501
502
503
504
505
506
507
        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
508
        }
Elemer Lelik's avatar
Elemer Lelik committed
509
510
      } else {
        // Generate code for array reference.
511
512
513
514
515
516
517
        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) {
518
            len = static_cast<int>( pt->get_dimension()->get_size() );
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
            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
534
        if (type) {
Elemer Lelik's avatar
Elemer Lelik committed
535
          // Follow the embedded type.
Elemer Lelik's avatar
Elemer Lelik committed
536
537
538
539
540
541
542
543
544
545
546
547
          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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
      } // if (ref->get_type)
    } // next reference
  }

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

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

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

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

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

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

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

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

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

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

  void Ref_base::set_code_section(
    GovernedSimple::code_section_t p_code_section)
  {
    for (size_t i = 0; i < subrefs.get_nof_refs(); i++) {
      FieldOrArrayRef *ref = subrefs.get_ref(i);
      if (ref->get_type() == FieldOrArrayRef::ARRAY_REF)
        ref->get_val()->set_code_section(p_code_section);
    }
  }
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
  
  void Ref_base::chk_immutability()
  {
    Common::Assignment* ass = get_refd_assignment();
    switch (ass->get_asstype()) {
    case Common::Assignment::A_TYPE:           /**< type */
    case Common::Assignment::A_CONST:          /**< value (const) */
    case Common::Assignment::A_UNDEF:          /**< undefined/undecided (ASN.1) */
    case Common::Assignment::A_ERROR:          /**< erroneous; the kind cannot be deduced (ASN.1) */
    case Common::Assignment::A_OC:             /**< information object class (ASN.1) */
    case Common::Assignment::A_OBJECT:         /**< information object (ASN.1) */
    case Common::Assignment::A_OS:             /**< information object set (ASN.1) */
    case Common::Assignment::A_VS:             /**< value set (ASN.1) */
    case Common::Assignment::A_EXT_CONST:      /**< external constant (TTCN-3) */
    case Common::Assignment::A_MODULEPAR:      /**< module parameter (TTCN-3) */
    case Common::Assignment::A_MODULEPAR_TEMP: /**< template module parameter */
    case Common::Assignment::A_VAR:            /**< variable (TTCN-3) */
    case Common::Assignment::A_VAR_TEMPLATE:   /**< template variable: dynamic template (TTCN-3) */
    case Common::Assignment::A_TIMER:          /**< timer (TTCN-3) */
    case Common::Assignment::A_PORT:           /**< port (TTCN-3) */
    case Common::Assignment::A_ALTSTEP:        /**< altstep (TTCN-3) */
    case Common::Assignment::A_TESTCASE:       /**< testcase Assignment::(TTCN-3) */
    case Common::Assignment::A_PAR_TIMER:      /**< formal parameter (timer) (TTCN-3) */
    case Common::Assignment::A_PAR_PORT:        /**< formal parameter (port) (TTCN-3) */
      break;
    case Common::Assignment::A_TEMPLATE:
      if (get_parlist() != NULL) {
        get_parlist()->chk_immutability();
      }
      break;
    case Common::Assignment::A_FUNCTION:       /**< function without return type (TTCN-3) */
    case Common::Assignment::A_FUNCTION_RVAL:  /**< function that returns a value (TTCN-3) */
    case Common::Assignment::A_FUNCTION_RTEMP: /**< function that returns a template (TTCN-3) */
    case Common::Assignment::A_EXT_FUNCTION:   /**< external function without return type (TTCN-3) */
    case Common::Assignment::A_EXT_FUNCTION_RVAL:  /**< ext. func that returns a value (TTCN-3) */
    case Common::Assignment::A_EXT_FUNCTION_RTEMP: /**< ext. func that returns a template (TTCN-3) */
      warning("Function invocation '%s' may change the actual snapshot.",
        get_dispname().c_str());
      break;
    case Common::Assignment::A_PAR_VAL:        /**< formal parameter (value) (TTCN-3) */
    case Common::Assignment::A_PAR_VAL_IN:     /**< formal parameter (in value) (TTCN-3) */
    case Common::Assignment::A_PAR_VAL_OUT:    /**< formal parameter (out value) (TTCN-3) */
      // TODO: @fuzzy INOUT parameter is not valid
    case Common::Assignment::A_PAR_VAL_INOUT:  /**< formal parameter (inout value) (TTCN-3) */
    case Common::Assignment::A_PAR_TEMPL_IN:   /**< formal parameter ([in] template) (TTCN-3) */
    case Common::Assignment::A_PAR_TEMPL_OUT:  /**< formal parameter (out template) (TTCN-3) */
    case Common::Assignment::A_PAR_TEMPL_INOUT:/**< formal parameter (inout template) (TTCN-3) */
      if (ass->get_eval_type() == FUZZY_EVAL) {
        warning("Fuzzy parameter '%s' may change (during) the actual snapshot.",
          get_dispname().c_str());
      }
      break;
    default:
      FATAL_ERROR("Ref_base::chk_immutability()");
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
687
688
689
690
691
692
693
694
695

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

  // =================================
  // ===== Reference
  // =================================
696
697
  
  Reference::Reference(const Reference& p)
698
699
    : Ref_base(p), reftype(p.reftype), parlist(NULL), gen_const_prefix(false),
    expr_cache(NULL)
700
701
702
  {
    params = p.params != NULL ? p.params->clone() : NULL;
  }
Elemer Lelik's avatar
Elemer Lelik committed
703
704

  Reference::Reference(Identifier *p_id)
705
706
    : Ref_base(), reftype(REF_BASIC), parlist(NULL), params(NULL),
    gen_const_prefix(false), expr_cache(NULL)
Elemer Lelik's avatar
Elemer Lelik committed
707
708
709
  {
    subrefs.add(new FieldOrArrayRef(p_id));
  }
710
  
711
712
713
714
715
716
717
718
719
  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");
    }
  }
  
720
  Reference::Reference(Identifier *p_modid, Identifier *p_id,
721
722
                       ParsedActualParameters *p_params, reftype_t p_reftype)
    : Ref_base(p_modid, p_id), reftype(p_reftype), parlist(NULL), params(p_params),
723
724
725
726
727
728
    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
729
730
731

  Reference::~Reference()
  {
732
733
734
735
736
737
738
739
    delete parlist;
    delete params;
    Free(expr_cache);
  }
  
  bool Reference::has_parameters() const
  {
    return params_checked ? parlist != NULL : params != NULL;
Elemer Lelik's avatar
Elemer Lelik committed
740
741
742
  }

  /* Called by:
Elemer Lelik's avatar
Elemer Lelik committed
743
744
745
746
   *         Common::PortTypeBody::PortTypeBody
   *         Common::Type::Type
   *         Common::TypeMappingTarget::TypeMappingTarget
   *         Common::PatternString::ps_elem_t::chk_ref */
Elemer Lelik's avatar
Elemer Lelik committed
747
748
749
750
  Reference *Reference::clone() const
  {
    return new Reference(*this);
  }
751
  
752
753
754
755
756
757
758
759
760
761
762
  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);
    }
  }
  
763
764
765
766
767
768
  void Reference::set_my_scope(Scope *p_scope)
  {
    Ref_base::set_my_scope(p_scope);
    if (parlist != NULL) {
      parlist->set_my_scope(p_scope);
    }
769
770
771
    if (params != NULL) {
      params->set_my_scope(p_scope);
    }
772
  }
Elemer Lelik's avatar
Elemer Lelik committed
773
774
775

  string Reference::get_dispname()
  {
776
    if (is_erroneous) return string("erroneous");
Elemer Lelik's avatar
Elemer Lelik committed
777
    string ret_val;
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
    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
794
      if (modid) {
Elemer Lelik's avatar
Elemer Lelik committed
795
796
        ret_val += modid->get_dispname();
        ret_val += '.';
Elemer Lelik's avatar
Elemer Lelik committed
797
798
      }
      ret_val += id->get_dispname();
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
      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
814
815
816
817
818
819
820
821
822
823
824
      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;
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
      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
        FormalParList *fplist = 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
            fplist = type->get_class_type_body()->get_constructor()->
              get_FormalParList();
          }
        }
        if (fplist == NULL) {
          // if it's not a class type, retrieve the formal parameter list normally
          fplist = ass->get_FormalParList();
        }
        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
868
          parlist = new ActualParList();
869
          is_erroneous = fplist->fold_named_and_chk(params, parlist);
Elemer Lelik's avatar
Elemer Lelik committed
870
871
          parlist->set_fullname(get_fullname());
          parlist->set_my_scope(my_scope);
872
873
874
          // the parsed parameter list is no longer needed
          delete params;
          params = NULL;
Elemer Lelik's avatar
Elemer Lelik committed
875
        } else {
876
877
          params->error("The referenced %s cannot have actual parameters",
            ass->get_description().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
878
879
880
881
882
883
884
885
886
887
888
        }
      }
    }
    return ass;
  }

  const Identifier* Reference::get_modid()
  {
    if (!id) detect_modid();
    return modid;
  }
889
  
Elemer Lelik's avatar
Elemer Lelik committed
890
891
892
893
894
  const Identifier* Reference::get_id()
  {
    if (!id) detect_modid();
    return id;
  }
895
896
897
898
899
  
  ActualParList* Reference::get_parlist()
  {
    return parlist;
  }
Elemer Lelik's avatar
Elemer Lelik committed
900
901
902
903
904
905
906
907
908
909
910
911

  Type *Reference::chk_variable_ref()
  {
    Common::Assignment *t_ass = get_refd_assignment();
    if (!t_ass) return 0;
    switch (t_ass->get_asstype()) {
    case Common::Assignment::A_PAR_VAL_IN:
      t_ass->use_as_lvalue(*this);
      // no break
    case Common::Assignment::A_VAR:
    case Common::Assignment::A_PAR_VAL_OUT:
    case Common::Assignment::A_PAR_VAL_INOUT:
912
913
914
915
      if (has_parameters()) {
        error("Reference without actual parameter list was expected");
        return NULL;
      }
Elemer Lelik's avatar
Elemer Lelik committed
916
917
918
      break;
    default:
      error("Reference to a variable or value parameter was "
Elemer Lelik's avatar
Elemer Lelik committed
919
        "expected instead of %s", t_ass->get_description().c_str());
920
      return NULL;
Elemer Lelik's avatar
Elemer Lelik committed
921
922
923
924
925
926
    }
    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
927
        "this context", ret_val->get_typename().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
928
929
930
931
932
933
934
935
936
937
    }
    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
938
939
940
941
942
        switch (t->get_typetype()) {
        case Type::T_ERROR:
          // remain silent
          break;
        case Type::T_COMPONENT:
943
944
945
946
947
948
949
          if (has_parameters()) {
            error("Reference without actual parameter list was expected");
          }
          else {
            return t;
          }
          break;
Elemer Lelik's avatar
Elemer Lelik committed
950
951
952
953
        default:
          error("Reference `%s' does not refer to a component type",
            get_dispname().c_str());
        }
Elemer Lelik's avatar
Elemer Lelik committed
954
      } else {
Elemer Lelik's avatar
Elemer Lelik committed
955
956
        error("Reference `%s' does not refer to a type",
          get_dispname().c_str());
Elemer Lelik's avatar
Elemer Lelik committed
957
958
959
960
      }
    }
    return 0;
  }
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983

  bool Reference::chk_activate_argument()
  {
    if (!has_parameters()) {
      error("Reference with actual parameter list was expected in the argument");
      return false;
    }
    Common::Assignment *t_ass = get_refd_assignment();
    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
984
985
986
987
988
989
990
  
  bool Reference::has_single_expr()
  {
    if (!Ref_base::has_single_expr()) {
      return false;
    }
    if (parlist != NULL) {
991
992
      Common::Assignment* ass = get_refd_assignment();
      const FormalParList* fplist = (ass != NULL) ? ass->get_FormalParList() : NULL;
Elemer Lelik's avatar
Elemer Lelik committed
993
      for (size_t i = 0; i < parlist->get_nof_pars(); i++) {
994
995
        if (!parlist->get_par(i)->has_single_expr(
            fplist != NULL ? fplist->get_fp_byIndex(i) : NULL)) {
Elemer Lelik's avatar
Elemer Lelik committed
996
997
998
          return false;
        }
      }
999
1000
      if (fplist != NULL) {
        size_t num_formal = fplist->get_nof_fps();
For faster browsing, not all history is shown. View entire blame