Sync with 5.4.1
[deliverable/titan.core.git] / compiler2 / ttcn3 / TtcnTemplate.hh
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 Ericsson Telecom AB
3 // All rights reserved. This program and the accompanying materials
4 // are made available under the terms of the Eclipse Public License v1.0
5 // which accompanies this distribution, and is available at
6 // http://www.eclipse.org/legal/epl-v10.html
7 ///////////////////////////////////////////////////////////////////////////////
8 #ifndef _Ttcn_Template_HH
9 #define _Ttcn_Template_HH
10
11 #include "../Int.hh"
12 #include "../Value.hh"
13 #include "../vector.hh"
14 #include "AST_ttcn3.hh"
15 #include "Templatestuff.hh"
16
17 namespace Ttcn {
18
19 using namespace Common;
20
21 class PatternString;
22 class ValueRange;
23 class LengthRestriction;
24 class Templates;
25 class NamedTemplate;
26 class NamedTemplates;
27 class IndexedTemplate;
28 class IndexedTemplates;
29
30 /** Represents a (single or compound) template body. */
31 class Template : public GovernedSimple {
32 public:
33
34
35 /** type of template */
36 enum templatetype_t {
37 TEMPLATE_ERROR, /**< erroneous template */
38 TEMPLATE_NOTUSED, /**< not used symbol (-) */
39 OMIT_VALUE, /**< omit */
40 ANY_VALUE, /**< any value (?) */
41 ANY_OR_OMIT, /**< any or omit (*) */
42 SPECIFIC_VALUE, /**< specific value */
43 TEMPLATE_REFD, /**< reference to another template */
44 TEMPLATE_INVOKE, /** template returning invoke */
45 TEMPLATE_LIST, /**< value list notation */
46 NAMED_TEMPLATE_LIST, /**< assignment notation */
47 INDEXED_TEMPLATE_LIST, /**< assignment notation with array indices */
48 VALUE_LIST, /**< value list match */
49 COMPLEMENTED_LIST, /**< complemented list match */
50 VALUE_RANGE, /**< value range match */
51 SUPERSET_MATCH, /**< superset match */
52 SUBSET_MATCH, /**< subset match */
53 PERMUTATION_MATCH, /**< permutation match */
54 ALL_FROM, /**< "all from" as part of a larger list e.g. permutation, superset, subset, etc. */
55 VALUE_LIST_ALL_FROM, /**< "all from" in a value list, itself in a larger list */
56 BSTR_PATTERN, /**< bitstring pattern */
57 HSTR_PATTERN, /**< hexstring pattern */
58 OSTR_PATTERN, /**< octetstring pattern */
59 CSTR_PATTERN, /**< character string pattern */
60 USTR_PATTERN /**< universal charstring pattern */
61 };
62
63 /** Status codes for the verification of template body completeness. */
64 enum completeness_t {
65 C_MUST_COMPLETE, /**< the body must be completely specified */
66 C_MAY_INCOMPLETE, /**< the body may be incompletely specified */
67 C_PARTIAL /**< some parts of the body may be incomplete,
68 * the others must be complete */
69 };
70
71 private:
72 /** Determines type of the template. Only this field is used at
73 * ANY_VALUE, ANY_OR_OMIT and OMIT_VALUE templates */
74 templatetype_t templatetype;
75
76 /** This union holds the ``body'' of the template. The
77 * templatetype field is used to select between different
78 * containers. */
79 union {
80 /** Holds the concrete single value of the template for single
81 * types */
82 Value *specific_value;
83 struct {
84 Ref_base *ref;
85 Template *refd; /**< cache */
86 Template *refd_last; /**< cache */
87 } ref;
88 /** Used for TEMPLATE_LIST, VALUE_LIST, COMPLEMENTED_LIST,
89 * SUPERSET_MATCH, SUBSET_MATCH and PERMUTATION_MATCH constructs. */
90 Templates *templates;
91 /** Used by NAMED_TEMPLATE_LIST */
92 NamedTemplates *named_templates;
93 /** Used by INDEXED_TEMPLATE_LIST */
94 IndexedTemplates *indexed_templates;
95 /** Used by ALL_FROM */
96 Template *all_from;
97 /** This is used for VALUE_RANGE template upper and lower
98 * bound. */
99 ValueRange *value_range;
100 /** Pattern contains basic string templates containing the any
101 * element (?), any element or no element (*) wildcards .
102 * Used by BSTR_PATTERN, HSTR_PATTERN, OSTR_PATTERN */
103 string *pattern;
104 /** PatternString contains the character string pattern matching
105 * constructions (including references).
106 * Used by CSTR_PATTERN, USTR_PATTERN */
107 PatternString *pstring;
108 /** Used by TEMPLATE_INVOKE */
109 struct {
110 Value *v;
111 Ttcn::ParsedActualParameters *t_list;
112 Ttcn::ActualParList *ap_list;
113 } invoke;
114 } u;
115
116 /** This points to the type of the template */
117 Type *my_governor;
118
119 /** Length restriction */
120 LengthRestriction *length_restriction;
121
122 /** Ifpresent flag. */
123 bool is_ifpresent;
124
125 /** Indicates whether it has been verified that the template is free of
126 * matching symbols. */
127 bool specific_value_checked;
128
129 /** Indicates whether the embedded templates contain PERMUTATION_MATCH.
130 * Used only in case of TEMPLATE_LIST */
131 bool has_permutation;
132
133 /** If false, it indicates that some all from could not be computed
134 * at compile-time, so it will need to be computed at runtime.
135 * It starts out as true. TODO find a better name */
136 bool flattened;
137
138 /** Pointer to the base template (or template field) that this
139 * template is derived from. It is set in modified templates
140 * (including in-line modified templates) and only if the
141 * compiler is able to determine the base template. It is NULL
142 * otherwise. */
143 Template *base_template;
144
145 /** Copy constructor. For Template::clone() only.
146 * This is a hit-and-miss affair. Some template types can be cloned;
147 * others cause an immediate FATAL_ERROR. */
148 Template(const Template& p);
149
150 /** Copy assignment disabled */
151 Template& operator=(const Template& p);
152
153 /** Destroys the innards of the template */
154 void clean_up();
155
156 string create_stringRepr();
157
158 public:
159 /** Constructor for TEMPLATE_ERROR, TEMPLATE_NOTUSED, OMIT_VALUE,
160 * ANY_VALUE, ANY_OR_OMIT and */
161 Template(templatetype_t tt);
162
163 /** Constructor for SPECIFIC_VALUE */
164 Template(Value *v);
165
166 /** Constructor for TEMPLATE_REFD */
167 Template(Ref_base *p_ref);
168
169 /** Constructor for TEMPLATE_LIST, VALUE_LIST, COMPLEMENTED_LIST,
170 * SUPERSET_MATCH, SUBSET_MATCH and PERMUTATION_MATCH */
171 Template(templatetype_t tt, Templates *ts);
172
173 /** Constructor for ALL_FROM */
174 Template(Template*);
175
176 /** Constructor for VALUE_LIST_ALL_FROM
177 * Takes over the innards of \a t, then deletes it.
178 * @pre tt == VALUE_LIST_ALL_FROM */
179 Template(templatetype_t tt, Template *t);
180
181 /** Constructor for NAMED_TEMPLATE_LIST */
182 Template(NamedTemplates *nts);
183
184 /** Constructor for INDEXED_TEMPLATE_LIST */
185 Template(IndexedTemplates *its);
186
187 /** Constructor for VALUE_RANGE */
188 Template(ValueRange *vr);
189
190 /** Constructor for BSTR_PATTERN, HSTR_PATTERN and OSTR_PATTERN */
191 Template(templatetype_t tt, string *p_patt);
192
193 /** Constructor for CSTR_PATTERN. */
194 Template(PatternString *p_ps);
195
196 virtual ~Template();
197
198 virtual Template* clone() const;
199
200 virtual void set_fullname(const string& p_fullname);
201 virtual void set_my_scope(Scope *p_scope);
202 void set_genname_recursive(const string& p_genname);
203 void set_genname_prefix(const char *p_genname_prefix);
204 void set_code_section(code_section_t p_code_section);
205
206 /** Transforms the template to type \a p_templatetype.
207 * Works only if the change is possible */
208 void set_templatetype(templatetype_t p_templatetype);
209 templatetype_t get_templatetype() const { return templatetype; }
210 /** Returns a C string containing the type (e.g. "any or omit")
211 * of the template. */
212 const char *get_templatetype_str() const;
213
214 /** Returns whether the template is a specific value containing a single
215 * identifier. */
216 bool is_undef_lowerid();
217 /** If the embedded specific value contains a single identifier it converts
218 * that to a referenced value or referenced template. */
219 void set_lowerid_to_ref();
220
221 /** If this template is used in an expression, what is its return type? */
222 Type::typetype_t get_expr_returntype(Type::expected_value_t exp_val);
223 /** If this template is used in an expression, what is its governor?
224 * If has no governor, but is reference, then tries the
225 * referenced stuff... If not known, returns NULL. */
226 Type *get_expr_governor(Type::expected_value_t exp_val);
227
228 /** Sets the governor type. */
229 virtual void set_my_governor(Type *p_gov);
230 /** Gets the governor type. */
231 virtual Type* get_my_governor() const;
232
233 void set_length_restriction(LengthRestriction *p_lr);
234 LengthRestriction *get_length_restriction() const
235 { return length_restriction; }
236 bool is_length_restricted() const { return length_restriction != 0; }
237
238 void set_ifpresent(bool p_ifpr) { is_ifpresent = p_ifpr; }
239 bool get_ifpresent() const { return is_ifpresent; }
240
241 void set_base_template(Template *p_base) { base_template = p_base; }
242 Template *get_base_template() const { return base_template; }
243
244 /** Returns the condition for the completeness of template body \a this,
245 * which is a 'record of' or 'set of' template.
246 * Argument \a incomplete_allowed indicates whether incomplete template
247 * bodies are allowed in the context at all. */
248 completeness_t get_completeness_condition_seof(bool incomplete_allowed);
249 /** Returns the condition for the completeness of template body \a this,
250 * which is a union template.
251 * Argument \a incomplete_allowed indicates whether incomplete template
252 * bodies are allowed in the context at all. Argument \a p_fieldname points
253 * to a field of \a this that is examined. It is useful when \a this is
254 * erroneous and contains multiple fields. */
255 completeness_t get_completeness_condition_choice(bool incomplete_allowed,
256 const Identifier& p_fieldname);
257
258 void add_named_temp(NamedTemplate* nt);
259
260 Value *get_specific_value() const;
261 Ref_base *get_reference() const;
262 ValueRange *get_value_range() const;
263 PatternString* get_cstr_pattern() const;
264 PatternString* get_ustr_pattern() const;
265 size_t get_nof_comps() const;
266 Template *get_temp_byIndex(size_t n) const;
267 NamedTemplate *get_namedtemp_byIndex(size_t n) const;
268 IndexedTemplate *get_indexedtemp_byIndex(size_t n) const;
269 Template *get_all_from() const;
270 /** Returns the number of elements in a VALUE_LIST. The elements of
271 * embedded PERMUTATION_MATCH constructs are counted recursively. */
272 size_t get_nof_listitems() const;
273 /** Returns the \a n th element of a VALUE_LIST. The embedded
274 * PERMUTATION_MATCH constructs are also counted. */
275 Template *get_listitem_byIndex(size_t n) const;
276
277 Template* get_template_refd_last(ReferenceChain *refch=0);
278 Template* get_refd_sub_template(Ttcn::FieldOrArrayRefs *subrefs,
279 bool usedInIsbound,
280 ReferenceChain *refch);
281 private:
282 Template* get_template_refd(ReferenceChain *refch);
283 Template* get_refd_field_template(const Identifier& field_id,
284 const Location& loc, bool usedInIsbound, ReferenceChain *refch);
285 Template* get_refd_array_template(Value *array_index, bool usedInIsbound,
286 ReferenceChain *refch);
287
288 bool compile_time() const;
289 public:
290 /** Returns whether \a this contains '*' as inside value. Applicable to:
291 * TEMPLATE_LIST, SUPERSET_MATCH, SUBSET_MATCH, PERMUTATION_MATCH */
292 bool temps_contains_anyornone_symbol() const;
293 /** Returns the number of elements not counting the '*'s. Applicable to:
294 * TEMPLATE_LIST, SUPERSET_MATCH, SUBSET_MATCH, PERMUTATION_MATCH */
295 size_t get_nof_comps_not_anyornone() const;
296 /** Returns whether the string pattern contains '*' as inside value.
297 * Applicable to: BSTR_PATTERN, HSTR_PATTERN, OSTR_PATTERN, CSTR_PATTERN */
298 bool pattern_contains_anyornone_symbol() const;
299 /** Returns the length of string pattern not counting the '*'s.
300 * Applicable to: BSTR_PATTERN, HSTR_PATTERN, OSTR_PATTERN, CSTR_PATTERN */
301 size_t get_min_length_of_pattern() const;
302
303 /** Returns whether the template can be converted to a value
304 * (i.e. it is a specific value without wildcards). */
305 bool is_Value() const;
306 /** Converts the template to a value recursively. The returned
307 * value is newly allocated (i.e. the caller is responsible for
308 * the deallocation).
309 * If the template type was SPECIFIC_VALUE, the value returned is
310 * extracted from the template, which becomes TEMPLATE_ERROR. */
311 Value *get_Value();
312
313 /** Returns whether the template consists of a single reference
314 * (i.e. it is a single specific value with an embedded
315 * non-parameterized reference). */
316 bool is_Ref() const;
317 /** Converts the template to a reference. The returned reference
318 * is newly allocated (i.e. the caller is responsible for the
319 * deallocation). */
320 Ref_base *get_Ref();
321
322 /** Checks for circular references within embedded templates */
323 void chk_recursions(ReferenceChain& refch);
324
325 /** Checks whether the template (including the embedded fields) contains
326 * no matching symbols. Argument \a allow_omit is used because OMIT_VALUE
327 * is allowed only in embedded fields. */
328 void chk_specific_value(bool allow_omit);
329 void chk_specific_value_generic();
330 void chk_invoke();
331
332 /** Copy template elements from the "all from" into the template.
333 *
334 * @param from_permutation \a true if the "all from" occurs inside
335 * a permutation_match (in which case the target of the "all from"
336 * is allowed to be AnyElementsOrNone).
337 * @return \a false if some elements could not be computed at compile time,
338 * else \a true.
339 */
340 bool flatten(bool from_permutation);
341 bool is_flattened() const { return flattened; }
342 bool has_allfrom() const;
343
344 static Templates *harbinger(Template *t, bool from_permutation, bool killer);
345
346 /** helper functions used by definitions which check their template restr.*/
347 /** returns the restriction name token */
348 static const char* get_restriction_name(template_restriction_t tr);
349 /** if a template has restriction \a tr then what is the restriction
350 on it's subfields */
351 static template_restriction_t get_sub_restriction(
352 template_restriction_t tr, Ref_base* ref);
353 /** returns C++ code that checks the restriction */
354 static char* generate_restriction_check_code(char* str,
355 const char* name, template_restriction_t tr);
356 /** Return if the required template restriction \a needed_tr is
357 * not satisfied by the given \a refd_tr */
358 static bool is_less_restrictive(template_restriction_t needed_tr,
359 template_restriction_t refd_tr);
360 private:
361 /** helper functions used by chk_restriction() */
362 bool chk_restriction_named_list(const char* definition_name,
363 map<string, void>& checked_map, size_t needed_checked_cnt,
364 const Location* usage_loc);
365 bool chk_restriction_refd(const char* definition_name,
366 template_restriction_t template_restriction, const Location* usage_loc);
367 public:
368 /** Checks if this template conforms to the restriction, return value:
369 * false = always satisfies restriction -> no runtime check needed or
370 * never satisfies restriction -> compiler error(s)
371 * true = possibly violates restriction, cannot be determined at compile
372 * time -> runtime check needed and compiler warning given when
373 * inadequate restrictions are used, in other cases there's
374 * no warning
375 * The return value is used by code generation to avoid useless checks
376 * @param usage_loc contains the location, where the template is used
377 * (errors are issued here, instead of where the template is declared) */
378 bool chk_restriction(const char* definition_name,
379 template_restriction_t template_restriction,
380 const Location* usage_loc);
381
382 /** Public entry points for code generation. */
383 /** Generates the equivalent C++ code for the template. It is used
384 * when the template is part of a complex expression
385 * (e.g. argument of a function that accepts a template). The
386 * generated code fragments are appended to the fields of visitor
387 * \a expr. */
388 void generate_code_expr(expression_struct *expr,
389 template_restriction_t template_restriction = TR_NONE);
390 /** Generates a C++ code sequence, which initializes the C++
391 * object named \a name with the contents of the template. The
392 * code sequence is appended to argument \a str and the resulting
393 * string is returned. */
394 char *generate_code_init(char *str, const char *name);
395 /** Walks through the template recursively and appends the C++
396 * initialization sequence of all (directly or indirectly)
397 * referenced non-parameterized templates and the default values of all
398 * parameterized templates to \a str and returns the resulting string.
399 * Only objects belonging to module \a usage_mod are initialized. */
400 char *rearrange_init_code(char *str, Common::Module* usage_mod);
401
402 private:
403 /** Private helper functions for code generation. */
404
405 /** Helper function for \a generate_code_init_refd(). Returns whether
406 * the string returned by \a get_single_expr() can be used to initialize
407 * the template instead of using the re-arrangement algorithm.
408 * Applicable to referenced templates only. */
409 bool use_single_expr_for_init();
410
411 /** Helper function for \a generate_code_init(). It handles the
412 * referenced templates. */
413 char *generate_code_init_refd(char *str, const char *name);
414 char *generate_code_init_invoke(char *str, const char *name);
415 /** Generates the C++ equivalent of \a this into \a expr and
416 * performs the necessary reordering: appends the initializer
417 * sequence of the dependent templates to \a str and returns the
418 * resulting string. It is used when \a this is a referenced
419 * template that is embedded into the body of a non-parameterized
420 * template. Note that the final content of \a expr is not
421 * necessarily identical to the C++ equivalent of \a
422 * u.ref.ref. */
423 char *generate_rearrange_init_code_refd(char *str, expression_struct *expr);
424 /** Helper function for \a generate_code_init(). It handles the value list
425 * notation and the assignment notation with array indices (for 'record
426 * of' and 'set of' types). Embedded permutation matching constructs are
427 * also handled. */
428 char *generate_code_init_seof(char *str, const char *name);
429 /** Helper function for \a generate_code_init_seof(). Used when \a this is
430 * a member of a value list notation or an embedded permutation match.
431 * Parameter \a name contains the C++ reference that points to the parent
432 * template. Parameter \a index is a straightened index in case of
433 * embedded permutations. Parameter \a element_type_genname contains a
434 * C++ class name that represents the element type of the 'record of' or
435 * 'set of' construct (which is the governor of \a this); if a temporary
436 * variable is created, this would be used as its type. */
437 char *generate_code_init_seof_element(char *str, const char *name,
438 const char* index, const char *element_type_genname);
439 /** Helper function for \a generate_code_init(). It handles the
440 * assignment notation for record/set/union types. */
441 char *generate_code_init_se(char *str, const char *name);
442 /** Helper function for \a generate_code_init(). It handles the
443 * value list and complemented list matching constructs. Flag \a
444 * is_complemented is set to true for complemented lists and
445 * false for value lists. */
446 char *generate_code_init_list(char *str, const char *name,
447 bool is_complemented);
448 /** Helper function for \a generate_code_init(). It handles the superset
449 * and subset constructs. Flag \a is_superset is set to true for superset
450 * and false for subset. Note that both constructs have similar data
451 * representations in runtime. */
452 char *generate_code_init_set(char *str, const char *name,
453 bool is_superset);
454
455 char *generate_code_init_all_from(char *str, const char *name);
456 char *generate_code_init_all_from_list(char *str, const char *name);
457
458 /** Helper function for \a generate_code_expr() and get_single_expr().
459 * It handles the invoke operation. */
460 void generate_code_expr_invoke(expression_struct *expr);
461
462 /** Helper function for \a rearrange_init_code(). It handles the
463 * referenced templates (i.e. it does the real work). */
464 char *rearrange_init_code_refd(char *str, Common::Module* usage_mod);
465 char *rearrange_init_code_invoke(char *str, Common::Module* usage_mod);
466
467 /** Returns whether the C++ initialization sequence requires a
468 * temporary variable reference to be introduced for efficiency
469 * reasons. */
470 bool needs_temp_ref();
471
472 public:
473 /** Returns whether the template can be represented by an in-line
474 * C++ expression. */
475 bool has_single_expr();
476 /** Returns the equivalent C++ expression. It can be used only if
477 * \a has_single_expr() returns true. Argument \a cast_needed
478 * indicates whether the generic wildcards have to be explicitly
479 * converted to the appropriate type. */
480 string get_single_expr(bool cast_needed);
481
482 virtual void dump(unsigned level) const;
483 };
484
485 /** Represents an in-line template. Consists of:
486 * - an optional type
487 * - an optional template reference with or without actual parameter list
488 * (in case of in-line modified template)
489 * - a mandatory template body
490 */
491 class TemplateInstance : public Node, public Location {
492 private: // all pointers owned
493 Type *type; // type before the colon, may be null
494 Ref_base *derived_reference; // base template, may be null
495 Template *template_body; // must not be null
496
497 /** Copy constructor disabled. */
498 TemplateInstance(const TemplateInstance& p);
499 /** Copy assignment disabled. */
500 TemplateInstance& operator=(const TemplateInstance& p);
501 public:
502 /** Constructor
503 *
504 * @param p_type type (the one before the colon, optional)
505 * @param p_ref reference to the base template (for a modified template)
506 * @param p_body template body (must not be NULL)
507 */
508 TemplateInstance(Type *p_type, Ref_base *p_ref, Template *p_body);
509 ~TemplateInstance();
510
511 virtual TemplateInstance *clone() const;
512 virtual void set_fullname(const string& p_fullname);
513 virtual void set_my_scope(Scope *p_scope);
514
515 Type* get_Type() const { return type; }
516 Ref_base* get_DerivedRef() const { return derived_reference; }
517 Template* get_Template() const { return template_body; }
518 // it can return null pointer
519 Def_Template* get_Referenced_Base_Template();
520
521 /** Give up ownership of \a type, \a derived_reference and
522 * \a template_body */
523 void release();
524
525 /** If this template instance is used in an expression, what is its
526 * return type? */
527 Type::typetype_t get_expr_returntype(Type::expected_value_t exp_val);
528 /** If this template instance is used in an expression, what is its
529 * governor? Returns NULL if the governor cannot be determined. */
530 Type *get_expr_governor(Type::expected_value_t exp_val);
531
532 /** Checks the entire template instance against \a governor. */
533 void chk(Type *governor);
534 /** Checks the member \a type (if present) against \a governor.
535 * Returns the type that shall be considered in further checks. */
536 Type *chk_Type(Type *governor);
537 /** Checks the derived reference against \a governor.
538 * If \a governor is NULL it is checked against the \a type member.
539 * Returns the type that shall be considered when checking
540 * \a template_body. */
541 Type *chk_DerivedRef(Type *governor);
542 /** Checks for circular references. */
543 void chk_recursions(ReferenceChain& refch);
544
545 bool is_string_type(Type::expected_value_t exp_val);
546
547 bool chk_restriction(const char* definition_name,
548 template_restriction_t template_restriction, const Location* usage_loc);
549
550 /** Returns whether the template instance can be represented by an in-line
551 * C++ expression. */
552 bool has_single_expr();
553 void set_code_section(GovernedSimple::code_section_t p_code_section);
554 bool needs_temp_ref();
555 void generate_code(expression_struct *expr,
556 template_restriction_t template_restriction = TR_NONE);
557 /** Appends the initialization sequence of the referred templates
558 * and their default values to \a str. Only templates from module
559 * \a usage_mod are considered. */
560 char *rearrange_init_code(char *str, Common::Module* usage_mod);
561
562 /** Appends the string representation of the template instance to
563 * \a str. */
564 void append_stringRepr(string& str) const;
565
566 virtual void dump(unsigned level) const;
567
568 /** Returns the single value if the template is single value or NULL. */
569 Value* get_specific_value() const;
570
571 bool is_only_specific_value() const {
572 return (derived_reference==NULL &&
573 template_body->get_templatetype()==Template::SPECIFIC_VALUE &&
574 !template_body->is_length_restricted() &&
575 !template_body->get_ifpresent());
576 }
577 };
578
579 } // namespace Ttcn
580
581 #endif // _Ttcn_Template_HH
This page took 0.044103 seconds and 5 git commands to generate.