AST_ttcn3.hh 80.7 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
balaskoa's avatar
balaskoa committed
2
 * Copyright (c) 2000-2018 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * Contributors:
 *   Baji, Laszlo
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Delic, Adam
 *   Kovacs, Ferenc
 *   Raduly, Csaba
 *   Szabados, Kristof
 *   Zalanyi, Balazs Andor
 *   Pandi, Krisztian
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef _Ttcn_AST_HH
#define _Ttcn_AST_HH

#include "../AST.hh"

namespace Common {
class CodeGenHelper;
}

/**
 * This namespace contains classes unique to TTCN-3 and some specializations
 * of classes from Common.
 */
namespace Ttcn {

  /**
   * \addtogroup AST
   *
   * @{
   */

  using namespace Common;

  class Module;
  class Definition;
  class FriendMod;
  class Imports;
  class ImpMod;
  class ActualPar;
  class ActualParList;
  class FormalParList;
  class ParsedActualParameters;
  class Template;
  class TemplateInstance;
  class TemplateInstances;
  class ArrayDimensions;
  class Timer;
  class StatementBlock;
  class AltGuards;
  class ILT;
  class Group;
  class WithAttribPath;
  class ErrorBehaviorList;
  class ErroneousAttributes;
  class ErroneousAttributeSpec;
  class PrintingType;

  /** Class to represent an actual parameter */
  class ActualPar : public Node {
  public:
    enum ap_selection_t {
      AP_ERROR,    ///< erroneous
      AP_VALUE,    ///< "in" value parameter
      AP_TEMPLATE, ///< "in" template parameter
      AP_REF,      ///< out/inout value or template parameter
      AP_DEFAULT   ///< created from the default value of a formal parameter
    };
  private:
    ap_selection_t selection;
    union {
      Value *val; ///< Value for AP_VALUE. Owned by ActualPar
      TemplateInstance *temp; ///< %Template for AP_TEMPLATE. Owned by ActualPar
      Ref_base  *ref; ///< %Reference for AP_REF. Owned by ActualPar
      ActualPar *act; ///< For AP_DEFAULT. \b Not owned by ActualPar
    };
    Scope *my_scope; ///< %Scope. Not owned
    /** tells what runtime template restriction check to generate,
     *  TR_NONE means that no check is needed because it could be determined
     *  in compile time */
    template_restriction_t gen_restriction_check;
    /** if this is an actual template parameter of an external function add
     *  runtime checks for out and inout parameters after the call */
    template_restriction_t gen_post_restriction_check;
  private:
    /** Copy constructor not implemented */
    ActualPar(const ActualPar& p);
    /** %Assignment disabled */
    ActualPar& operator=(const ActualPar& p);
  public:
    /// Constructor for an erroneous object (fallback)
    ActualPar()
    : Node(), selection(AP_ERROR), my_scope(0),
      gen_restriction_check(TR_NONE), gen_post_restriction_check(TR_NONE) {}
    /// Actual par for an in value parameter
    ActualPar(Value *v);
    /// Actual par for an in template parameter
    ActualPar(TemplateInstance *t);
    /// Actual par for an {out or inout} {value or template} parameter
    ActualPar(Ref_base *r);
    /// Created from the default value of a formal par, at the call site,
    ///
    ActualPar(ActualPar *a);
    virtual ~ActualPar();
    virtual ActualPar *clone() const;
    virtual void set_fullname(const string& p_fullname);
    virtual void set_my_scope(Scope *p_scope);
    bool is_erroneous() const { return selection == AP_ERROR; }
    ap_selection_t get_selection() const { return selection; }
    Value *get_Value() const;
    TemplateInstance *get_TemplateInstance() const;
    Ref_base *get_Ref() const;
    ActualPar *get_ActualPar() const;
    /** Checks the embedded recursions within the value or template instance. */
    void chk_recursions(ReferenceChain& refch);
    /** Returns whether the actual parameter can be represented by an in-line
     * C++ expression. */
    bool has_single_expr();
    void set_code_section(GovernedSimple::code_section_t p_code_section);
    /** Generates the C++ equivalent of \a this into \a expr.
     * Flag \a copy_needed indicates whether to add an extra copy constructor
     * call if \a this contains a referenced value or template to avoid
     * aliasing problems with other out/inout parameters. */
132
    void generate_code(expression_struct *expr, bool copy_needed, FormalPar* formal_par) const;
Elemer Lelik's avatar
Elemer Lelik committed
133
    /** Appends the initialization sequence of all (directly or indirectly)
Elemer Lelik's avatar
Elemer Lelik committed
134
135
136
137
138
     * referred non-parameterized templates and the default values of all
     *  parameterized templates to \a str and returns the resulting string. 
     *  Only objects belonging to module \a usage_mod are initialized. */
    char *rearrange_init_code(char *str, Common::Module* usage_mod);
    char *rearrange_init_code_defval(char *str, Common::Module* usage_mod);
Elemer Lelik's avatar
Elemer Lelik committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    /** Appends the string representation of the actual parameter to \a str. */
    void append_stringRepr(string& str) const;
    virtual void dump(unsigned level) const;
    void set_gen_restriction_check(template_restriction_t tr)
      { gen_restriction_check = tr; }
    template_restriction_t get_gen_restriction_check()
      { return gen_restriction_check; }
    void set_gen_post_restriction_check(
      template_restriction_t p_gen_post_restriction_check)
      { gen_post_restriction_check = p_gen_post_restriction_check; }
  };

  /// A collection of actual parameters (parameter list)
  class ActualParList : public Node {
    vector<ActualPar> params;
  public:
    ActualParList(): Node(), params() { }
    ActualParList(const ActualParList& p);
    ~ActualParList();
    ActualParList* clone() const;
    virtual void set_fullname(const string& p_fullname);
    virtual void set_my_scope(Scope *p_scope);
    size_t get_nof_pars() const { return params.size(); }
    void add(ActualPar* p) { params.add(p); }
    ActualPar* get_par(size_t i) const { return params[i]; }
    /** Checks the embedded recursions within the values and template
     * instances of actual parameters. */
    void chk_recursions(ReferenceChain& refch);
167
168

    void chk_immutability();
Elemer Lelik's avatar
Elemer Lelik committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    /** Generates the C++ equivalent of the actual parameter list without
     * considering any aliasing between variables and 'in' parameters. */
    void generate_code_noalias(expression_struct *expr, FormalParList *p_fpl);
    /** Generates the C++ equivalent of the actual parameter list considering
     * aliasing problems between the 'in' parameters and 'out'/'inout'
     * parameters as well as component variables seen by the called definition.
     * Argument \a p_fpl points to the formal parameter list of the referred
     * definition if it is known, otherwise it is NULL. Argument \a p_comptype
     * points to the component type identified by the 'runs on' clause of the
     * referred definition (if exists and relevant for alias analysis,
     * otherwise NULL).
     * When invoke() is used with FAT types: the special case of 'runs on self'
     * has the argument \a p_compself set to true and \a p_comptype is NULL,
     * but the component is 'self' or any compatible component. */
    void generate_code_alias(expression_struct *expr, FormalParList *p_fpl,
      Type *p_comptype, bool p_compself);
    /** Walks through the parameter list and appends the initialization
     * sequence of all (directly or indirectly) referred non-parameterized
Elemer Lelik's avatar
Elemer Lelik committed
187
188
189
190
     * templates and the default values of all parameterized templates to
     * \a str and returns the resulting string. 
     * Only objects belonging to module \a usage_mod are initialized. */
    char *rearrange_init_code(char *str, Common::Module* usage_mod);
Elemer Lelik's avatar
Elemer Lelik committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    virtual void dump(unsigned level) const;
  };

  /** Helper class for the Ttcn::Reference */
  class FieldOrArrayRef : public Node, public Location {
  public:
    enum reftype { FIELD_REF, ARRAY_REF };
  private:
    reftype ref_type; ///< reference type
    /** The stored reference. Owned and destroyed by FieldOrArrayRef */
    union {
       Identifier *id; ///< name of the field, used by FIELD_REF
       Value *arp; ///< value of the index, used by ARRAY_REF
    } u;
    /** Copy constructor for clone() only */
    FieldOrArrayRef(const FieldOrArrayRef& p);
    /** %Assignment disabled */
    FieldOrArrayRef& operator=(const FieldOrArrayRef& p);
  public:
    FieldOrArrayRef(Identifier *p_id);
    FieldOrArrayRef(Value *p_arp);
    ~FieldOrArrayRef();
    virtual FieldOrArrayRef* clone() const;
    virtual void set_fullname(const string& p_fullname);
    virtual void set_my_scope(Scope *p_scope);
    reftype get_type() const { return ref_type; }
    /** Return the identifier.
     * @pre reftype is FIELD_REF, or else FATAL_ERROR */
    const Identifier* get_id() const;
    /** Returns the value.
     * @pre reftype is ARRAY_REF, or else FATAL_ERROR */
    Value* get_val() const;
    /** Appends the string representation of the sub-reference to \a str. */
    void append_stringRepr(string& str) const;
Elemer Lelik's avatar
Elemer Lelik committed
225
226
227
228
229
230
    /** Sets the first letter in the name of the field to lowercase if it's an
      * uppercase letter.
      * Used on open types (the name of their alternatives can be given with both
      * an uppercase or a lowercase first letter, and the generated code will need
      * to use the lowercase version). */
    void set_field_name_to_lowercase();
Elemer Lelik's avatar
Elemer Lelik committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  };

  /** A vector of FieldOrArrayRef objects */
  class FieldOrArrayRefs : public Node {
    /** Element holder. This FieldOrArrayRefs object owns the elements
     * and will free them in the destructor. */
    vector<FieldOrArrayRef> refs;
    /** Indicates whether the last array index refers to an element of a
     * string value. */
    bool refs_str_element;
  public:
    FieldOrArrayRefs() : Node(), refs(), refs_str_element(false) { }
    FieldOrArrayRefs(const FieldOrArrayRefs& p);
    ~FieldOrArrayRefs();
    FieldOrArrayRefs *clone() const;
    virtual void set_fullname(const string& p_fullname);
    virtual void set_my_scope(Scope *p_scope);
    void add(FieldOrArrayRef *p_ref) { refs.add(p_ref); }
    size_t get_nof_refs() const { return refs.size(); }
    FieldOrArrayRef* get_ref(size_t i) const { return refs[i]; }
    bool has_unfoldable_index() const;
    /** Removes (deletes) the first \a n field references. */
    void remove_refs(size_t n);
    Identifier *remove_last_field();
    /** Generates the C++ sub-expression that accesses
     * the given sub-references of definition \a ass. 
     * @param nof_subrefs indicates the number of sub-references
     * to generate code from (UINT_MAX means all of them) */
    void generate_code(expression_struct *expr, Common::Assignment *ass, size_t nof_subrefs = UINT_MAX);
260
261
262
263
264
    /** Generates the C++ sub-expression that could access the
      * sub-references of a reference of type \a type
      * @param nof_subrefs indicates the number of sub-references
      * to generate code from (UINT_MAX means all of them) */
    void generate_code(expression_struct *expr, Type *type, bool is_template = false, size_t nof_subrefs = UINT_MAX);
Elemer Lelik's avatar
Elemer Lelik committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    /** Appends the string representation of sub-references to \a str. */
    void append_stringRepr(string &str) const;
    bool refers_to_string_element() const { return refs_str_element; }
    void set_string_element_ref() { refs_str_element = true; }
    void clear_string_element_ref() { refs_str_element = false; }
  };

  /**
   * Base class for all TTCN-3 references.
   * Includes the common functionality for parameterized and non-parameterized
   * references (e.g. handling of field or array subreferences).
   */
  class Ref_base : public Ref_simple {
  protected: // Ttcn::Reference and Ttcn::Ref_pard need access
    Identifier *modid;
    /** If id is a NULL pointer all components are stored in subrefs */
    Identifier *id;
    FieldOrArrayRefs subrefs;
    /** Indicates whether the consistency of formal and actual parameter lists
     * has been verified. */
    bool params_checked;
    bool usedInIsbound;
    Ref_base(const Ref_base& p);
  private:
    Ref_base& operator=(const Ref_base& p);
  public:
    /** Default constructor: sets \a modid and \a id to NULL. Used by
     * non-parameterized references only. It is automatically guessed whether
     * the first component of \a subrefs is a module id or not. */
    Ref_base() : Ref_simple(), modid(0), id(0), subrefs(), params_checked(0)
      , usedInIsbound(false) {}
    Ref_base(Identifier *p_modid, Identifier *p_id);
    ~Ref_base();
    virtual Ref_base *clone() const = 0;
    virtual void set_fullname(const string& p_fullname);
    virtual void set_my_scope(Scope *p_scope);
    /** Sets the scope of the base reference to \a p_scope.
     * The scope of array indices in \a subrefs remains unchanged. */
    void set_base_scope(Scope *p_scope) { Ref_simple::set_my_scope(p_scope); }
    virtual bool getUsedInIsbound() {return usedInIsbound;}
    virtual void setUsedInIsbound() {usedInIsbound = true;}
    Setting *get_refd_setting();
    FieldOrArrayRefs *get_subrefs();
    /** Appends \a p_ref to the sub-references */
    void add(FieldOrArrayRef *p_ref) { subrefs.add(p_ref); }
    virtual bool has_single_expr();
    virtual void set_code_section(
      GovernedSimple::code_section_t p_code_section);
313
    void chk_immutability();
Elemer Lelik's avatar
Elemer Lelik committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    /** Generates the C++ equivalent of the reference (including the parameter
     * list and sub-references) as an access to a constant resource.
     */
    virtual void generate_code_const_ref(expression_struct_t *expr);
  };

  /**
   * TTCN-3 reference without parameters.
   * Implements the automatic detection whether the first identifier is a
   * module name or not.
   */
  class Reference : public Ref_base {
    ActualParList *parlist;
  public:
    Reference(Identifier *p_id);
    Reference(Identifier *p_modid, Identifier *p_id)
      : Ref_base(p_modid, p_id), parlist(0) { }
    ~Reference();
    virtual Reference *clone() const;
333
    virtual void set_my_scope(Scope* p_scope);
Elemer Lelik's avatar
Elemer Lelik committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
    virtual string get_dispname();
    virtual Common::Assignment *get_refd_assignment(bool check_parlist = true);
    virtual const Identifier* get_modid();
    virtual const Identifier* get_id();
    /** Checks whether \a this points to a variable or value parameter.
     * Returns the type of the respective variable or variable field or NULL
     * in case of error. */
    Type *chk_variable_ref();
    /** Checks if \a this points to a component.
     *  Returns the type of the component if so or NULL in case of error. */
    Type *chk_comptype_ref();
    virtual bool has_single_expr();
    virtual void generate_code(expression_struct_t *expr);
    /** Generates the C++ equivalent of port references within
     * connect/disconnect/map/unmap statements into \a expr.
     * Argument \a p_scope shall point to the scope of the statement. */
    void generate_code_portref(expression_struct_t *expr, Scope *p_scope);
    virtual void generate_code_const_ref(expression_struct_t *expr);
    /**
     * Generates code for checking if the reference
     * and the referred objects are bound or not.*/
355
356
    void generate_code_ispresentboundchosen(expression_struct_t *expr,
      bool is_template, const Value::operationtype_t optype, const char* field);
357
358
359
    /** Lets the referenced assignment object know, that the reference is used
      * at least once (only relevant for formal parameters and external constants). */
    void ref_usage_found();
Elemer Lelik's avatar
Elemer Lelik committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
  private:
    /** Detects whether the first identifier in subrefs is a module id */
    void detect_modid();
  };

  /**
   * Parameterized TTCN-3 reference
   */
  class Ref_pard : public Ref_base {
    /** "Processed" parameter list, after the semantic check. */
    ActualParList parlist;
    /** "Raw" parameter list, before the semantic check. */
    Ttcn::ParsedActualParameters *params;
    /** Used by generate_code_cached(). Stores the generated expression string, 
      * so it doesn't get regenerated every time. */
    char* expr_cache;
    /** Copy constructor. Private, used by Ref_pard::clone() only */
    Ref_pard(const Ref_pard& p);
    /// %Assignment disabled
    Ref_pard& operator=(const Ref_pard& p);
  public:
    /** Constructor
     * \param p_modid the module in which it resides
     * \param p_id the identifier
     * \param p_params parameters. For a function, this is the list constructed
     * for the actual parameters.
     * */
    Ref_pard(Identifier *p_modid, Identifier *p_id,
             ParsedActualParameters *p_params);
    ~Ref_pard();
    virtual Ref_pard *clone() const;
    virtual void set_fullname(const string& p_fullname);
    virtual void set_my_scope(Scope *p_scope);
    string get_dispname();
    virtual Common::Assignment *get_refd_assignment(bool check_parlist = true);
    virtual const Identifier *get_modid();
    virtual const Identifier *get_id();
    virtual ActualParList *get_parlist();
    /** Checks whether \a this is a correct argument of an activate operation
     * or statement. The reference shall point to an altstep with proper
     * `runs on' clause and the actual parameters that are passed by reference
     * shall not point to local definitions. The function returns true if the
     * altstep reference is correct and false in case of any error. */
    bool chk_activate_argument();
    virtual bool has_single_expr();
    virtual void set_code_section(
      GovernedSimple::code_section_t p_code_section);
    virtual void generate_code          (expression_struct_t *expr);
    virtual void generate_code_const_ref(expression_struct_t *expr);
    
    /** Used when an 'all from' is called on a function or parametrised template,
      * generate_code would generate new temporaries for the function's parameters
      * each call. This method makes sure the same temporaries are used every time
      * the function is called in the generated code.
      * On the first run calls generate_code and stores its result (only expr.expr
      * is cached, since the preamble is only needed after the first call). 
      * On further runs the cached expression is returned.*/
    virtual void generate_code_cached (expression_struct_t *expr);
  };

  /**
  * Class Ttcn::NameBridgingScope.
  * This scope unit is NOT A REAL SCOPE UNIT,
  * its only purpose is to serve as a bridge with a name between two real scope
  * units. All operations are transfered automatically.
  */
  class NameBridgingScope : public Scope {
    virtual string get_scopeMacro_name() const;
    virtual NameBridgingScope* clone() const;
    virtual Common::Assignment* get_ass_bySRef(Ref_simple *p_ref);
  };

  /**
   * Class Ttcn::RunsOnScope.
   * Implements the scoping rules for functions, altsteps and testcases that
   * have a 'runs on' clause. First looks for the definitions in the given
   * component type first then it searches in its parent scope.
   * Note: This scope unit cannot access the parent scope of the component type
   * (which is a module Definitions) unless the component type and the
   * 'runs on' clause is defined in the same module.
   */
  class RunsOnScope : public Scope {
    /** Points to the component type. */
    Type *component_type;
    /** Shortcut to the definitions within \a component_type. */
    ComponentTypeBody *component_defs;

    /** Not implemented. Causes \a FATAL_ERROR. */
    RunsOnScope(const RunsOnScope& p);
    /** %Assignment not implemented */
    RunsOnScope& operator=(const RunsOnScope& p);
  public:
    RunsOnScope(Type *p_comptype);
    virtual RunsOnScope *clone() const;

    Type *get_component_type() const { return component_type; }
    /** Checks the uniqueness of definitions within \a component_defs and
     * reports warnings in case of hiding. */
    void chk_uniq();

    virtual RunsOnScope *get_scope_runs_on();
    virtual Common::Assignment *get_ass_bySRef(Ref_simple *p_ref);
    virtual bool has_ass_withId(const Identifier& p_id);
  };
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
  
  /**
   * Class Ttcn::PortScope.
   * Implements the scoping rules for functions that
   * have a 'port' clause. First looks for the variable definitions in the given
   * port type first then it searches in its parent scope.
   * Note: This scope unit cannot access the parent scope of the port type.
   */
  class PortScope : public Scope {
    /** Points to the port type. */
    Type *port_type;
    /** Shortcut to the definitions within \a port_type. */
    Definitions *vardefs;

    /** Not implemented. Causes \a FATAL_ERROR. */
    PortScope(const PortScope& p);
    /** %Assignment not implemented */
    PortScope& operator=(const PortScope& p);
  public:
    PortScope(Type *p_porttype);
    virtual PortScope *clone() const;

    Type *get_port_type() const { return port_type; }
    /** Checks the uniqueness of definitions within \a portdefs and
     * reports warnings in case of hiding. */
    void chk_uniq();

    virtual PortScope *get_scope_port();
    virtual Common::Assignment *get_ass_bySRef(Ref_simple *p_ref);
    virtual bool has_ass_withId(const Identifier& p_id);
  };
Elemer Lelik's avatar
Elemer Lelik committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
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
631
632
633
634
635
636

  /**
   * Class Ttcn::Definitions.
   *
   * Owns the contained Definition objects.
   */
  class Definitions : public Common::Assignments {
  protected:
    /** Searchable map of definitions. Used after chk_uniq. */
    map<string, Definition> ass_m;
    /** Indicates whether the uniqueness of identifiers has been checked. */
    bool checked;
    /** Vector containing all definitions. Used for building. */
    vector<Definition> ass_v;

    Definitions(const Definitions& p);
  public:

    Definitions() : Common::Assignments(), ass_m(), checked(false), ass_v() {}
    ~Definitions();
    Definitions *clone() const;
    virtual void set_fullname(const string& p_fullname);
    /** Adds the assignment p_ass and becomes the owner of it.
     *  The uniqueness of the identifier is not checked. */
    void add_ass(Definition *p_ass);
    virtual bool has_local_ass_withId(const Identifier& p_id);
    virtual Common::Assignment* get_local_ass_byId(const Identifier& p_id);
    virtual size_t get_nof_asss();
    virtual Common::Assignment* get_ass_byIndex(size_t p_i);
    size_t get_nof_raw_asss();
    Definition *get_raw_ass_byIndex(size_t p_i);
    /** Checks the uniqueness of identifiers. */
    void chk_uniq();
    /** Checks all definitions. */
    void chk();
    /** Checks the definitions within the header of a for loop. */
    void chk_for();
    /** Sets the genname of embedded definitions using \a prefix. */
    void set_genname(const string& prefix);
    /** Generates code for all assignments into \a target. */
    void generate_code(output_struct *target);
    void generate_code(CodeGenHelper& cgh);
    char* generate_code_str(char *str);
    void ilt_generate_code(ILT *ilt);
    /** Prints the contents of all assignments. */
    virtual void dump(unsigned level) const;
  };

  /**
   * Class Ttcn::Definitions.
   *
   * Owns the contained Definition objects.
   */
/*  class Definitions : public OtherDefinitions {
  public:
    Definitions() : OtherDefinitions() {}
    ~Definitions();
    Definitions *clone() const;
    void add_ass(Definition *p_ass);
  };*/

  /** Represents a TTCN-3 group
   *
   *  @note a Group is not a Scope */
  class Group : public Node, public Location {
  private:
    Group* parent_group;
    WithAttribPath* w_attrib_path;
    /// Definitions that belong to this group (directly)
    vector<Definition> ass_v;
    /// Map the name to the definition (filled in chk_uniq)
    map<string,Definition> ass_m;
    /// Subgroups
    vector<Group> group_v;
    /// Map the name to the subgroup
    map<string,Group> group_m;
    vector<ImpMod> impmods_v;
    vector<FriendMod> friendmods_v;
    Identifier *id;
    bool checked;
  private:
    /** Copy constructor not implemented */
    Group(const Group& p);
    /** %Assignment not implemented */
    Group& operator=(const Group& p);
  public:
    Group(Identifier *p_id);
    ~Group();
    virtual Group* clone() const;
    virtual void set_fullname(const string& p_fullname);
    void add_ass(Definition* p_ass);
    void add_group(Group* p_group);
    void set_parent_group(Group* p_parent_group);
    Group* get_parent_group() const { return parent_group; }
    void set_attrib_path(WithAttribPath* p_path);
    const Identifier& get_id() const { return *id; }
    void chk_uniq();
    virtual void chk();
    void add_impmod(ImpMod *p_impmod);
    void add_friendmod(FriendMod *p_friendmod);
    virtual void dump(unsigned level) const;
    void set_with_attr(MultiWithAttrib* p_attrib);
    WithAttribPath* get_attrib_path();
    void set_parent_path(WithAttribPath* p_path);
  };

  class ControlPart : public Node, public Location {
  private:
    StatementBlock* block;
    WithAttribPath* w_attrib_path;

    NameBridgingScope bridgeScope;

    /// Copy constructor disabled
    ControlPart(const ControlPart& p);
    /// %Assignment disabled
    ControlPart& operator=(const ControlPart& p);
  public:
    ControlPart(StatementBlock* p_block);
    ~ControlPart();
    virtual ControlPart* clone() const;
    virtual void set_fullname(const string& p_fullname);
    virtual void set_my_scope(Scope *p_scope);
    void chk();
    void generate_code(output_struct *target, Module *my_module);
    void set_with_attr(MultiWithAttrib* p_attrib);
    WithAttribPath* get_attrib_path();
    void set_parent_path(WithAttribPath* p_path);
    void dump(unsigned level) const;
  };

  /** A TTCN-3 module */
  class Module : public Common::Module {
  private:
    string *language_spec;
    Definitions *asss;
    vector<Group> group_v;
    map<string, Group> group_m;
    WithAttribPath* w_attrib_path;
    Imports *imp;
    ControlPart* controlpart;
    /** For caching the scope objects that are created in
637
     * \a get_runs_on_scope() and get_port_scope(). */
Elemer Lelik's avatar
Elemer Lelik committed
638
    vector<RunsOnScope> runs_on_scopes;
639
    vector<PortScope> port_scopes;
Elemer Lelik's avatar
Elemer Lelik committed
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
  private:
    /** Copy constructor not implemented */
    Module(const Module& p);
    /** %Assignment not implemented */
    Module& operator=(const Module& p);
  public:
    vector<FriendMod> friendmods_v;
    Module(Identifier *p_modid);
    ~Module();
    void add_group(Group* p_group);
    void add_friendmod(FriendMod *p_friendmod);
    virtual Module* clone() const;
    virtual Common::Assignment* importAssignment(
      const Identifier& p_source_modid, const Identifier& p_id) const;
    virtual void set_fullname(const string& p_fullname);
    virtual Common::Assignments* get_scope_asss();
    virtual bool has_imported_ass_withId(const Identifier& p_id);
    virtual Common::Assignment* get_ass_bySRef(Ref_simple *p_ref);
    virtual bool is_valid_moduleid(const Identifier& p_id);
    virtual Common::Assignments *get_asss();
    virtual bool exports_sym(const Identifier& p_id);
    virtual Type *get_address_type();
    virtual void chk_imp(ReferenceChain& refch, vector<Common::Module>& moduleStack);
    virtual void chk();
  private:
    void chk_friends();
    void chk_groups();
    virtual void get_imported_mods(module_set_t& p_imported_mods);
    virtual void generate_code_internal(CodeGenHelper& cgh);
  public:
    /** Returns a scope that can access the definitions within component type
     * \a comptype (which is imported from another module) and its parent scope
     * is \a asss. Note that this scope cannot see the scope of \a comptype.
     * The function uses \a runs_on_scopes for caching the scope objects: if an
     * object has been created for a component type it will be returned later
     * instead of creating a new one. */
    RunsOnScope *get_runs_on_scope(Type *comptype);
677
678
679
680
    /* The same as get_runs_on_scope except that the returned scope can access
     * the port types definitions.
     */
    PortScope *get_port_scope(Type *porttype);
Elemer Lelik's avatar
Elemer Lelik committed
681
682
683
684
685
686
687
688
    virtual void dump(unsigned level) const;
    void set_language_spec(const char *p_language_spec);
    void add_ass(Definition* p_ass);
    void add_impmod(ImpMod *p_impmod);
    void add_controlpart(ControlPart* p_controlpart);
    void set_with_attr(MultiWithAttrib* p_attrib);
    WithAttribPath* get_attrib_path();
    void set_parent_path(WithAttribPath* p_path);
689
    Imports& get_imports() { return *imp; }
Elemer Lelik's avatar
Elemer Lelik committed
690
691
692

    bool is_visible(const Identifier& id, visibility_t visibility);
    
Elemer Lelik's avatar
Elemer Lelik committed
693
694
695
696
697
698
699
700
701
    /** Generates JSON schema segments for the types defined in the modules,
      * and references to these types. Information related to the types' 
      * JSON encoding and decoding functions is also inserted after the references.
      *
      * @param json JSON document containing the main schema, schema segments for 
      * the types will be inserted here
      * @param json_refs map of JSON documents containing the references and function
      * info related to each type */
    virtual void generate_json_schema(JSON_Tokenizer& json, map<Type*, JSON_Tokenizer>& json_refs);
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
    
    /** Generates the debugger initialization function for this module.
      * The function creates the global debug scope associated with this module,
      * and initializes it with all the global variables visible in the module
      * (including imported variables).
      * The debug scopes of all component types defined in the module are also
      * created and initialized with their variables. */
    virtual void generate_debugger_init(output_struct *output);
    
    /** Generates the variable adding code for all global variables defined
      * in this module. This function is called by generate_debugger_init()
      * for both the current module and all imported modules. */
    virtual char* generate_debugger_global_vars(char* str, Common::Module* current_mod);
    
    /** Generates the debugger variable printing function, which can print values
      * and templates of all types defined in this module (excluding subtypes). */
    virtual void generate_debugger_functions(output_struct *output);
Elemer Lelik's avatar
Elemer Lelik committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
  };

  /**
  * Module friendship declaration.
  */
  class FriendMod : public Node, public Location {
  private:
    Module *my_mod;
    Identifier *modid;
    WithAttribPath* w_attrib_path;
    Group* parentgroup;
    /** Indicates whether this friend module declaration was checked */
    bool checked;
  private:
    /** Copy constructor not implemented. */
    FriendMod(const FriendMod&);
    /** %Assignment not implemented. */
    FriendMod& operator=(const FriendMod&);
  public:
    FriendMod(Identifier *p_modid);
    ~FriendMod();
    virtual FriendMod* clone() const;
    virtual void set_fullname(const string& p_fullname);
    virtual void chk();
    void set_my_mod(Module *p_mod) { my_mod = p_mod; }
    const Identifier& get_modid() const {return *modid;}
    void set_with_attr(MultiWithAttrib* p_attrib);
    WithAttribPath* get_attrib_path();
    void set_parent_path(WithAttribPath* p_path);
    void set_parent_group(Group* p_group);
  };


  /**
   * Imported module. Represents an import statement.
   */
  class ImpMod : public Node, public Location {
  public:
    enum imptype_t {
      I_UNDEF,
      I_ERROR,
      I_ALL,
      I_IMPORTSPEC,
762
763
764
765
766
767
768
769
      I_IMPORTIMPORT,
      /** Phantom import for when the 'include' is needed in the generated C++
        * code, but the TTCN-3 'import' statement is optional.
        * Currently this can only happen when using coding values of a type that
        * has custom or PER coder functions set (the module containing the coder
        * function declarations does not have to be imported to the module that
        * contains the coding operation). */
      I_DEPENDENCY
Elemer Lelik's avatar
Elemer Lelik committed
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
    };
  private:
    /** Points to the target (imported) module. This is initially NULL;
     *  set during semantic analysis by Ttcn::Imports::chk_uniq() */
    Common::Module *mod;
    /** The importing module (indirectly our owner) */
    Module *my_mod;
    /** Import type: "import all", selective import, import of import, etc. */
    imptype_t imptype;
    /** The name given in the import statement;
     *  hopefully an actual module name */
    Identifier *modid;
    /** The text after "import from name language" */
    string *language_spec;
    /** Recursive import (already deprecated in v3.2.1) */
    bool is_recursive;
    WithAttribPath* w_attrib_path;
    /** Group in which the import statement is located, if any */
    Group* parentgroup;
    visibility_t visibility;
  private:
    /** Copy constructor not implemented. */
    ImpMod(const ImpMod&);
    /** %Assignment not implemented */
    ImpMod& operator=(const ImpMod&);
  public:
    ImpMod(Identifier *p_modid);
    ~ImpMod();
    virtual ImpMod* clone() const;
    virtual void set_fullname(const string& p_fullname);
    virtual void chk();
    /** Checks the existence of imported symbols and checks import definitions
     * in the imported modules recursively. */
    void chk_imp(ReferenceChain& refch, vector<Common::Module>& moduleStack);
    void set_my_mod(Module *p_mod) { my_mod = p_mod; }
    const Identifier& get_modid() const {return *modid;}
    void set_mod(Common::Module *p_mod) { mod = p_mod; }
    Common::Module *get_mod() const { return mod; }
    /** Returns the imported definition with name \a p_id if it is imported or
     * NULL otherwise.
     * \a loc is used to report an error if it is needed */
    Common::Assignment *get_imported_def(const Identifier& p_source_modid,
        const Identifier& p_id, const Location *loc,
        ReferenceChain* refch, vector<ImpMod>& usedImpMods) const;
    bool has_imported_def(const Identifier& p_source_modid,
            const Identifier& p_id, const Location *loc) const;
    void set_imptype(imptype_t p_imptype) { imptype = p_imptype; }
    void set_language_spec(const char *p_language_spec);
    void set_recursive() { is_recursive = true; }
    void generate_code(output_struct *target);
    virtual void dump(unsigned level) const;
    void set_with_attr(MultiWithAttrib* p_attrib);
    WithAttribPath* get_attrib_path();
    void set_parent_path(WithAttribPath* p_path);
    void set_parent_group(Group* p_group);
    imptype_t get_imptype() {
      return imptype;
    }
    void set_visibility(visibility_t p_visibility){
      visibility = p_visibility;
    }
    visibility_t get_visibility() const{
      return visibility;
    }
  };

  /**
   * Class Imports.
   */
  class Imports : public Node {
  private:
    /** my module */
    Module *my_mod;
    /** imported modules */
    vector<ImpMod> impmods_v;
    /** Indicates whether the import list has been checked. */
    bool checked;

    friend class ImpMod;
  private:
    /** Copy constructor not implemented. */
    Imports(const Imports&);
    /** %Assignment not implemented */
    Imports& operator=(const Imports&);
  public:
    Imports() : Node(), my_mod(0), impmods_v(), checked(false) {}
    virtual ~Imports();
    virtual Imports* clone() const;
    void add_impmod(ImpMod *p_impmod);
    void set_my_mod(Module *p_mod);
860
861
    size_t get_imports_size() const {return impmods_v.size();}
    ImpMod* get_impmod(size_t index) const {return impmods_v[index];}
Elemer Lelik's avatar
Elemer Lelik committed
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
    /** Checks the existence of imported modules and detects duplicated imports
     * from the same module. Initializes \a impmods_m. */
    void chk_uniq();
    /** Checks the existence of imported symbols and checks import definitions
     * in the imported modules recursively. */
    void chk_imp(ReferenceChain& refch, vector<Common::Module>& moduleStack);
    /** Returns \p true if an imported module with the given name exists,
     *  else returns \p false. */
    bool has_impmod_withId(const Identifier& p_id) const;
    /** Returns whether a definition with identifier \a p_id is imported
     * from one or more modules */
    bool has_imported_def(const Identifier& p_id, const Location *loc) const;
    /** Returns the imported definition with name \a p_id if it is
     * unambiguous or NULL otherwise.
     * \a loc is used to report an error if it is needed */
    Common::Assignment *get_imported_def(const Identifier& p_source_modid,
        const Identifier& p_id, const Location *loc, ReferenceChain* refch);
    void get_imported_mods(Module::module_set_t& p_imported_mods) const;
    void generate_code(output_struct *target);
    void generate_code(CodeGenHelper& cgh);
    virtual void dump(unsigned level) const;
  };

  class Definition : public Common::Assignment {
  protected: // many derived classes
    /** Contains the C++ identifier of the definition. If empty the C++
     * identifier is generated from \a id. */
    string genname;
    /** The group it's in, if any */
    Group *parentgroup;
    WithAttribPath* w_attrib_path;
    ErroneousAttributes* erroneous_attrs; // set by chk_erroneous_attr() or NULL
    /** True if function/altstep/default scope, not module scope */
    bool local_scope;

    Definition(const Definition& p)
      : Common::Assignment(p), genname(), parentgroup(0),
        w_attrib_path(0), erroneous_attrs(0), local_scope(false)
      { }

    namedbool has_implicit_omit_attr() const;
  private:
    /// %Assignment disabled
    Definition& operator=(const Definition& p);
  public:
    Definition(asstype_t p_asstype, Identifier *p_id)
      : Common::Assignment(p_asstype, p_id), genname(), parentgroup(0),
        w_attrib_path(0), erroneous_attrs(0), local_scope(false)
      { }
    virtual ~Definition();
    virtual Definition* clone() const = 0;
    virtual void set_fullname(const string& p_fullname);
    virtual bool is_local() const;
    /** Sets the visibility type of the definition */
    void set_visibility(const visibility_t p_visibility)
      { visibilitytype = p_visibility; }
    /** Marks the (template) definition as local to a func/altstep/default */
    inline void set_local() { local_scope = true; }

921
    virtual string get_genname() const;
Elemer Lelik's avatar
Elemer Lelik committed
922
923
924
925
926
927
    void set_genname(const string& p_genname) { genname = p_genname; }
    /** Check if two definitions are (almost) identical, the type and dimensions
     * must always be identical, the initial values can be different depending
     * on the definition type. If error was reported the return value is false.
     * The initial values (if applicable) may be present/absent, different or
     * unfoldable. The function must be overridden to be used.
928
     */ 
Elemer Lelik's avatar
Elemer Lelik committed
929
930
    virtual bool chk_identical(Definition *p_def);
    /** Parse and check the erroneous attribute data,
931
932
933
      * returns erroneous attributes or NULL */
    static ErroneousAttributes* chk_erroneous_attr(WithAttribPath* p_attrib_path,
      Type* p_type, Scope* p_scope, string p_fullname, bool in_update_stmt);
Elemer Lelik's avatar
Elemer Lelik committed
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
    /** This code generation is used when this definition is embedded
     * in a statement block. */
    virtual char* generate_code_str(char *str);
    virtual void ilt_generate_code(ILT *ilt);
    /** Generates the C++ initializer sequence for a definition of a component
     * type, appends to \a str and returns the resulting string. The function
     * is used when \a this is realized using the C++ objects of definition
     * \a base_defn inherited from another component type. The function is
     * implemented only for those definitions that can appear within component
     * types, the generic version causes \a FATAL_ERROR. */
    virtual char *generate_code_init_comp(char *str, Definition *base_defn);
    virtual void dump_internal(unsigned level) const;
    virtual void dump(unsigned level) const;
    virtual void set_with_attr(MultiWithAttrib* p_attrib);
    virtual WithAttribPath* get_attrib_path();
    virtual void set_parent_path(WithAttribPath* p_path);
    virtual void set_parent_group(Group* p_group);
    virtual Group* get_parent_group();
  };

  /**
   * TTCN-3 type definition (including signatures and port types).
   */
  class Def_Type : public Definition {
  private:
    Type *type;

    NameBridgingScope bridgeScope;

    /** Copy constructor not implemented */
    Def_Type(const Def_Type& p);
    /** %Assignment disabled */
    Def_Type& operator=(const Def_Type& p);
  public:
    Def_Type(Identifier *p_id, Type *p_type);
    virtual ~Def_Type();
    virtual Def_Type* clone() const;
    virtual void set_fullname(const string& p_fullname);
    virtual void set_my_scope(Scope *p_scope);
    virtual Setting *get_Setting();
    virtual Type *get_Type();
    virtual void chk();
    virtual void generate_code(output_struct *target, bool clean_up = false);
    virtual void generate_code(CodeGenHelper& cgh);
    virtual void dump_internal(unsigned level) const;
    virtual void set_with_attr(MultiWithAttrib* p_attrib);
    virtual WithAttribPath* get_attrib_path();
    virtual void set_parent_path(WithAttribPath* p_path);
  };

  /**
   * TTCN-3 constant definition.
   */
  class Def_Const : public Definition {
  private:
    Type *type;
    Value *value;
    bool value_under_check;

    /// Copy constructor disabled
    Def_Const(const Def_Const& p);
    /// %Assignment disabled
    Def_Const& operator=(const Def_Const& p);
  public:
    Def_Const(Identifier *p_id, Type *p_type, Value *p_value);
    virtual ~Def_Const();
    virtual Def_Const *clone() const;
For faster browsing, not all history is shown. View entire blame