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_Templatestuff_HH
9 #define _Ttcn_Templatestuff_HH
11 #include "AST_ttcn3.hh"
12 #include "../Value.hh"
14 #include "../vector.hh"
18 using namespace Common;
21 class TemplateInstance;
23 class ParsedActualParameters;
26 * Class to represent a TTCN-3 ValueRange objects. These are used in
27 * value range templates in e.g. (-3.8 .. infinity) format.
29 class ValueRange : public Node {
34 /// Copy constructor for clone() only
35 ValueRange(const ValueRange& p);
36 /// %Assignment disabled
37 ValueRange& operator=(const ValueRange& p);
39 ValueRange(Value *p_min_v, Value *p_max_v)
40 : Node(), min_v(p_min_v), max_v(p_max_v) { }
41 virtual ~ValueRange();
42 virtual ValueRange* clone() const;
43 virtual void set_fullname(const string& p_fullname);
44 virtual void set_my_scope(Scope *p_scope);
45 Value *get_min_v() const { return min_v; }
46 Value *get_max_v() const { return max_v; }
47 void set_lowerid_to_ref();
48 Type::typetype_t get_expr_returntype(Type::expected_value_t exp_val);
49 Type *get_expr_governor(Type::expected_value_t exp_val);
50 void set_code_section(GovernedSimple::code_section_t p_code_section);
51 /** Generates a C++ code sequence, which sets the appropriate value range
52 * in C++ object named \a name, which represents the template itself.
53 * The code sequence is appended to argument \a str and the
54 * resulting string is returned. */
55 char *generate_code_init(char *str, const char *name);
56 /** Appends the initialization sequence of all (directly or indirectly)
57 * referred non-parameterized templates to \a str and returns the resulting
59 char *rearrange_init_code(char *str);
60 /** Appends the string representation of the value range to \a str. */
61 void append_stringRepr(string& str) const;
62 virtual void dump(unsigned level) const;
66 * Class to represent TemplateList.
67 * Owns and deletes the elements.
69 * Note that it doesn't have its own set_fullname() method, only the one
70 * inherited from Common::Node. This means that you _also_ have to call
71 * set_fullname in a loop on all elements;
72 * see Ttcn::Template::set_fullname, case TEMPLATE_LIST.
74 class Templates : public Node {
78 /// Copy constructor for clone() only
79 Templates(const Templates& p);
80 /// %Assignment disabled
81 Templates& operator=(const Templates& p);
83 Templates() : Node(), ts() { }
85 virtual Templates *clone() const;
86 virtual void set_my_scope(Scope *p_scope);
87 /** Appends \a p_t at the end of list. */
88 void add_t(Template *p_t);
89 /** Adds \a p_t in the front of list and shifts existing elements back by
91 void add_front_t(Template *p_t);
92 size_t get_nof_ts() const { return ts.size(); }
93 Template*& get_t_byIndex(size_t p_i) { return ts[p_i]; }
94 /** Appends the string representation of the template list to \a str. */
95 void append_stringRepr(string& str) const;
99 * Class to represent an IndexedTemplate.
101 class IndexedTemplate : public Node, public Location {
103 FieldOrArrayRef *index;
105 /// Copy constructor disabled
106 IndexedTemplate(const IndexedTemplate& p);
107 /// %Assignment disabled
108 IndexedTemplate& operator=(const IndexedTemplate& p);
110 IndexedTemplate(FieldOrArrayRef *p_i, Template *p_t);
111 virtual ~IndexedTemplate();
112 virtual IndexedTemplate* clone() const;
113 virtual void set_fullname(const string& p_fullname);
114 virtual void set_my_scope(Scope *p_scope);
115 void set_code_section(GovernedSimple::code_section_t p_code_section);
116 const FieldOrArrayRef& get_index() const { return *index; }
117 Template *get_template() const { return temp; }
118 virtual void dump(unsigned level) const;
122 * Class to represent IndexedTemplateList.
123 * Owns and deletes the elements.
125 class IndexedTemplates : public Node {
127 /** Indexed templates. */
128 vector<IndexedTemplate> its_v;
129 /// Copy constructor disabled
130 IndexedTemplates(const IndexedTemplates& p);
131 /// %Assignment disabled
132 IndexedTemplates& operator=(const IndexedTemplates& p);
134 IndexedTemplates() : Node(), its_v() { }
135 virtual ~IndexedTemplates();
136 virtual IndexedTemplates* clone() const;
137 virtual void set_fullname(const string& p_fullname);
138 virtual void set_my_scope(Scope *p_scope);
139 void add_it(IndexedTemplate *p_it);
140 size_t get_nof_its() const { return its_v.size(); }
141 IndexedTemplate *get_it_byIndex(size_t p_i);
145 * Class to represent a NamedTemplate.
147 class NamedTemplate : public Node , public Location {
152 /* Copy constructor disabled. */
153 NamedTemplate(const NamedTemplate& p);
155 /** Copy assignment disabled */
156 NamedTemplate& operator=(const NamedTemplate& p);
158 NamedTemplate(Identifier *p_n, Template *p_t);
159 virtual ~NamedTemplate();
160 virtual NamedTemplate* clone() const;
161 virtual void set_fullname(const string& p_fullname);
162 virtual void set_my_scope(Scope *p_scope);
163 const Identifier& get_name() const { return *name; }
164 /* \todo this should be called get_Template, like in TemplateInstance */
165 Template *get_template() const { return temp; }
166 /** Sets the first letter in the name of the named template to lowercase
167 * if it's an uppercase letter.
168 * Used on open types (the name of their alternatives can be given with both
169 * an uppercase or a lowercase first letter, and the generated code will need
170 * to use the lowercase version). */
171 void set_name_to_lowercase();
172 /** Remove the template from the ownership of NamedTemplate.
174 * @post \a temp == 0 */
175 Template *extract_template();
176 virtual void dump(unsigned level) const;
180 * Class to represent NamedTemplateList.
182 class NamedTemplates : public Node {
184 /** named templates */
185 vector<NamedTemplate> nts_v;
186 /** Stores the first occurrence of NamedTemplate with id. The string
187 * parameter refers to the id of the nt, the size_t param refers
188 * to the index in nts. */
189 map<string, NamedTemplate> nts_m;
190 /** True if NamedTemplates::chk_dupl_id() has been called, else false */
192 /** Copy constructor disabled. */
193 NamedTemplates(const NamedTemplates& p);
194 /** Copy assignment disabled. */
195 NamedTemplates& operator=(const NamedTemplates& p);
197 NamedTemplates() : Node(), nts_v(), nts_m(), checked(false) { }
198 virtual ~NamedTemplates();
199 virtual NamedTemplates* clone() const;
200 virtual void set_fullname(const string& p_fullname);
201 virtual void set_my_scope(Scope *p_scope);
202 void add_nt(NamedTemplate *p_nt);
203 size_t get_nof_nts() const { return nts_v.size(); }
204 NamedTemplate *get_nt_byIndex(size_t p_i) { return nts_v[p_i]; }
205 bool has_nt_withName(const Identifier& p_name);
206 NamedTemplate *get_nt_byName(const Identifier& p_name);
207 void chk_dupl_id(bool report_errors = true);
210 /** Class to represent length restrictions associated with string,
211 * set of, record of and array values */
212 class LengthRestriction : public Node, public Location {
216 Value *single; // owned
218 Value *lower, *upper; // both owned
222 /** Copy constructor disabled. */
223 LengthRestriction(const LengthRestriction& p);
224 /** Copy assignment disabled */
225 LengthRestriction& operator=(const LengthRestriction& p);
227 LengthRestriction(Value* p_val);
228 LengthRestriction(Value* p_lower, Value* p_upper);
229 virtual ~LengthRestriction();
230 virtual LengthRestriction* clone() const;
231 virtual void set_fullname(const string& p_fullname);
232 virtual void set_my_scope(Scope *p_scope);
233 bool get_is_range() const { return is_range; }
234 void chk(Type::expected_value_t expected_value);
235 /** Checks whether the length restriction contradicts array dimension
236 * \a p_dim. If no contradiction is found an error message is
238 void chk_array_size(ArrayDimension *p_dim);
239 /** Checks the number of elements in the template against the length
240 * restriction. Issues a warning if there are too few or too many elements
241 * thus the template will not match anything. Argument \a nof_elements
242 * shows the minimal number of elements in the template (embedded * symbols
243 * are not considered) and flag \a has_anyornone indicates if there is at
244 * least one * in the template. Arguments \a p_loc (containing the location
245 * of the template that the length restriction belongs to) and \a p_what
246 * (containing the word "template", "value" or "string") are used for error
248 void chk_nof_elements(size_t nof_elements, bool has_anyornone,
249 const Location& p_loc, const char *p_what);
250 Value *get_single_value();
251 Value *get_lower_value();
252 Value *get_upper_value();
253 void set_code_section(GovernedSimple::code_section_t p_code_section);
254 /** Generates a C++ code sequence, which sets the appropriate length
255 * restriction attributes in C++ object named \a name, which represents the
256 * owner template. The code sequence is appended to argument \a str and the
257 * resulting string is returned. */
258 char *generate_code_init(char *str, const char *name);
259 /** Appends the initialization sequence of all (directly or indirectly)
260 * referred non-parameterized templates to \a str and returns the resulting
262 char *rearrange_init_code(char *str);
263 /** Appends the string representation of the length restriction to
265 void append_stringRepr(string& str) const;
266 virtual void dump(unsigned level) const;
270 * Represents a list of template instances.
272 class TemplateInstances : public Node, public Location {
274 vector<TemplateInstance> tis;
275 /** Copy constructor disabled. */
276 TemplateInstances(const TemplateInstances& p);
277 /** Copy assignment not implemented: disabled */
278 TemplateInstances& operator=(const TemplateInstances& p);
280 friend class ParsedActualParameters;
282 TemplateInstances() : Node(), Location(), tis() { }
283 virtual ~TemplateInstances();
284 virtual TemplateInstances *clone() const;
285 virtual void dump(unsigned level) const;
286 virtual void set_fullname(const string& p_fullname);
287 virtual void set_my_scope(Scope *p_scope);
288 void add_ti(TemplateInstance *p_ti);
289 size_t get_nof_tis() const { return tis.size(); }
290 TemplateInstance *get_ti_byIndex(size_t p_i) const { return tis[p_i]; }
291 void set_code_section(GovernedSimple::code_section_t p_code_section);
294 /** A named actual parameter */
295 class NamedParam : public Node, public Location {
297 TemplateInstance *tmpl;
299 NamedParam(const NamedParam& p);
300 /** Copy assignment disabled. */
301 NamedParam& operator=(const NamedParam& p);
303 NamedParam(Identifier *id, TemplateInstance *t);
304 virtual ~NamedParam();
305 /** "Virtual copy constructor" */
306 virtual NamedParam *clone() const;
307 virtual void set_fullname(const string& p_fullname);
308 virtual void set_my_scope(Scope *p_scope);
310 Identifier *get_name() const { return name; }
311 TemplateInstance *get_ti() const { return tmpl; }
312 TemplateInstance *extract_ti();
314 virtual void dump(unsigned int level) const;
317 /** A collection of named actual parameters */
318 class NamedParams : public Node, public Location {
320 vector<NamedParam> nps;
322 NamedParams(const NamedParams& p);
323 /** Copy assignment disabled. */
324 NamedParams& operator=(const NamedParams& p);
327 /** Destructor. Empties \p nps and frees its elements. */
328 virtual ~NamedParams();
329 /** "Virtual copy constructor" */
330 virtual NamedParams *clone() const;
331 void set_my_scope(Scope *p_scope);
332 void set_fullname(const string& p_fullname);
334 /** Append \p p to the list of named actual parameters. */
335 void add_np(NamedParam *p);
337 size_t get_nof_nps() const;
339 /** Replace the named parameter at index \p p_i with NULL and
340 * return its previous value. */
341 NamedParam *extract_np_byIndex(size_t p_i);
343 virtual void dump(unsigned int level) const;
346 /** Actual parameters from the parser in "raw form".
348 * Contains both positional and named parameters.
349 * There is not enough information during parsing to construct a "full"
350 * ActualParameters object (information about formal parameters is needed).
351 * This object holds the available information until the ActualParameters
352 * object can be constructed during semantic analysis.
354 * TODO think of a shorter name */
355 class ParsedActualParameters : public Node, public Location {
356 TemplateInstances *unnamedpart; ///< the "classic" unnamed parameters
357 NamedParams *namedpart; ///< the named parameters
359 ParsedActualParameters(const ParsedActualParameters& p);
360 /** Copy assignment disabled. */
361 ParsedActualParameters& operator=(const ParsedActualParameters& p);
363 ParsedActualParameters(TemplateInstances *p_ti = 0, NamedParams *p_np = 0);
364 virtual ~ParsedActualParameters();
365 virtual ParsedActualParameters *clone() const;
367 // @name Distributors. Pass on the call to \a namedpart and \a unnamedpart.
369 virtual void set_fullname(const string& p_fullname);
370 virtual void set_my_scope(Scope *p_scope);
371 void set_location(const char *p_filename, int p_lineno=0);
372 void set_location(const char *p_filename, const YYLTYPE& p_yyloc);
373 void set_location(const char *p_filename, const YYLTYPE& p_firstloc,
374 const YYLTYPE& p_lastloc);
375 void set_location(const char *p_filename, int p_first_line,
376 int p_first_column, int p_last_line, int p_last_column);
377 virtual void dump(unsigned int level) const;
380 /** Return the TemplateInstances member.
381 * \note ParsedActualParameters object owns the TemplateInstances object;
382 * the caller should clone() it if it wants a copy.
384 * The return value should be a const reference to prevent the caller
385 * from freeing it. */
386 TemplateInstances *get_tis() const { return unnamedpart; }
388 TemplateInstances* steal_tis();
390 size_t get_nof_tis() const { return unnamedpart->get_nof_tis(); }
392 TemplateInstance *get_ti_byIndex(size_t p_i) const {
393 return unnamedpart->get_ti_byIndex(p_i); }
395 TemplateInstance *set_ti_byIndex(size_t p_i, TemplateInstance *p_np) {
396 TemplateInstance *retval = unnamedpart->get_ti_byIndex(p_i);
397 unnamedpart->tis[p_i] = p_np;
401 /// Append \p p_ti to the internal list of TemplateInstance s
402 void add_ti(TemplateInstance *p_ti){
403 unnamedpart->add_ti(p_ti);
406 /// Calls TemplateInstances::set_code_section() for \p unnamedpart
407 void set_code_section(GovernedSimple::code_section_t p_code_section) {
408 unnamedpart->set_code_section(p_code_section);
411 // Named params section
413 /// Append \p np to the list in \p namedpart
414 void add_np(NamedParam *np);
416 size_t get_nof_nps() const;
418 NamedParam *extract_np_byIndex(size_t p_i) {
419 return namedpart->extract_np_byIndex(p_i);
425 #endif // _Ttcn_Templatestuff_HH