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