Titan Core Initial Contribution
[deliverable/titan.core.git] / compiler2 / ttcn3 / Templatestuff.hh
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 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
10
11 #include "AST_ttcn3.hh"
12 #include "../Value.hh"
13 #include "../Type.hh"
14 #include "../vector.hh"
15
16 namespace Ttcn {
17
18 using namespace Common;
19
20 class Template;
21 class TemplateInstance;
22 class ArrayDimension;
23 class ParsedActualParameters;
24
25 /**
26 * Class to represent a TTCN-3 ValueRange objects. These are used in
27 * value range templates in e.g. (-3.8 .. infinity) format.
28 */
29 class ValueRange : public Node {
30 private:
31 Value *min_v;
32 Value *max_v;
33
34 /// Copy constructor for clone() only
35 ValueRange(const ValueRange& p);
36 /// %Assignment disabled
37 ValueRange& operator=(const ValueRange& p);
38 public:
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
58 * string. */
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;
63 };
64
65 /**
66 * Class to represent TemplateList.
67 * Owns and deletes the elements.
68 *
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.
73 */
74 class Templates : public Node {
75 private:
76 vector<Template> ts;
77
78 /// Copy constructor for clone() only
79 Templates(const Templates& p);
80 /// %Assignment disabled
81 Templates& operator=(const Templates& p);
82 public:
83 Templates() : Node(), ts() { }
84 virtual ~Templates();
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
90 * one position. */
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;
96 };
97
98 /**
99 * Class to represent an IndexedTemplate.
100 */
101 class IndexedTemplate : public Node, public Location {
102 private:
103 FieldOrArrayRef *index;
104 Template *temp;
105 /// Copy constructor disabled
106 IndexedTemplate(const IndexedTemplate& p);
107 /// %Assignment disabled
108 IndexedTemplate& operator=(const IndexedTemplate& p);
109 public:
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;
119 };
120
121 /**
122 * Class to represent IndexedTemplateList.
123 * Owns and deletes the elements.
124 */
125 class IndexedTemplates : public Node {
126 private:
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);
133 public:
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);
142 };
143
144 /**
145 * Class to represent a NamedTemplate.
146 */
147 class NamedTemplate : public Node , public Location {
148 private:
149 Identifier *name;
150 Template *temp;
151
152 /* Copy constructor disabled. */
153 NamedTemplate(const NamedTemplate& p);
154
155 /** Copy assignment disabled */
156 NamedTemplate& operator=(const NamedTemplate& p);
157 public:
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 /** Remove the template from the ownership of NamedTemplate.
167 * @return \a temp
168 * @post \a temp == 0 */
169 Template *extract_template();
170 virtual void dump(unsigned level) const;
171 };
172
173 /**
174 * Class to represent NamedTemplateList.
175 */
176 class NamedTemplates : public Node {
177 private:
178 /** named templates */
179 vector<NamedTemplate> nts_v;
180 /** Stores the first occurrence of NamedTemplate with id. The string
181 * parameter refers to the id of the nt, the size_t param refers
182 * to the index in nts. */
183 map<string, NamedTemplate> nts_m;
184 /** True if NamedTemplates::chk_dupl_id() has been called, else false */
185 bool checked;
186 /** Copy constructor disabled. */
187 NamedTemplates(const NamedTemplates& p);
188 /** Copy assignment disabled. */
189 NamedTemplates& operator=(const NamedTemplates& p);
190 public:
191 NamedTemplates() : Node(), nts_v(), nts_m(), checked(false) { }
192 virtual ~NamedTemplates();
193 virtual NamedTemplates* clone() const;
194 virtual void set_fullname(const string& p_fullname);
195 virtual void set_my_scope(Scope *p_scope);
196 void add_nt(NamedTemplate *p_nt);
197 size_t get_nof_nts() const { return nts_v.size(); }
198 NamedTemplate *get_nt_byIndex(size_t p_i) { return nts_v[p_i]; }
199 bool has_nt_withName(const Identifier& p_name);
200 NamedTemplate *get_nt_byName(const Identifier& p_name);
201 void chk_dupl_id(bool report_errors = true);
202 };
203
204 /** Class to represent length restrictions associated with string,
205 * set of, record of and array values */
206 class LengthRestriction : public Node, public Location {
207 bool checked;
208 bool is_range;
209 union {
210 Value *single; // owned
211 struct {
212 Value *lower, *upper; // both owned
213 } range;
214 };
215
216 /** Copy constructor disabled. */
217 LengthRestriction(const LengthRestriction& p);
218 /** Copy assignment disabled */
219 LengthRestriction& operator=(const LengthRestriction& p);
220 public:
221 LengthRestriction(Value* p_val);
222 LengthRestriction(Value* p_lower, Value* p_upper);
223 virtual ~LengthRestriction();
224 virtual LengthRestriction* clone() const;
225 virtual void set_fullname(const string& p_fullname);
226 virtual void set_my_scope(Scope *p_scope);
227 bool get_is_range() const { return is_range; }
228 void chk(Type::expected_value_t expected_value);
229 /** Checks whether the length restriction contradicts array dimension
230 * \a p_dim. If no contradiction is found an error message is
231 * displayed.*/
232 void chk_array_size(ArrayDimension *p_dim);
233 /** Checks the number of elements in the template against the length
234 * restriction. Issues a warning if there are too few or too many elements
235 * thus the template will not match anything. Argument \a nof_elements
236 * shows the minimal number of elements in the template (embedded * symbols
237 * are not considered) and flag \a has_anyornone indicates if there is at
238 * least one * in the template. Arguments \a p_loc (containing the location
239 * of the template that the length restriction belongs to) and \a p_what
240 * (containing the word "template", "value" or "string") are used for error
241 * reporting . */
242 void chk_nof_elements(size_t nof_elements, bool has_anyornone,
243 const Location& p_loc, const char *p_what);
244 Value *get_single_value();
245 Value *get_lower_value();
246 Value *get_upper_value();
247 void set_code_section(GovernedSimple::code_section_t p_code_section);
248 /** Generates a C++ code sequence, which sets the appropriate length
249 * restriction attributes in C++ object named \a name, which represents the
250 * owner template. The code sequence is appended to argument \a str and the
251 * resulting string is returned. */
252 char *generate_code_init(char *str, const char *name);
253 /** Appends the initialization sequence of all (directly or indirectly)
254 * referred non-parameterized templates to \a str and returns the resulting
255 * string. */
256 char *rearrange_init_code(char *str);
257 /** Appends the string representation of the length restriction to
258 * \a str. */
259 void append_stringRepr(string& str) const;
260 virtual void dump(unsigned level) const;
261 };
262
263 /**
264 * Represents a list of template instances.
265 */
266 class TemplateInstances : public Node, public Location {
267 private:
268 vector<TemplateInstance> tis;
269 /** Copy constructor disabled. */
270 TemplateInstances(const TemplateInstances& p);
271 /** Copy assignment not implemented: disabled */
272 TemplateInstances& operator=(const TemplateInstances& p);
273
274 friend class ParsedActualParameters;
275 public:
276 TemplateInstances() : Node(), Location(), tis() { }
277 virtual ~TemplateInstances();
278 virtual TemplateInstances *clone() const;
279 virtual void dump(unsigned level) const;
280 virtual void set_fullname(const string& p_fullname);
281 virtual void set_my_scope(Scope *p_scope);
282 void add_ti(TemplateInstance *p_ti);
283 size_t get_nof_tis() const { return tis.size(); }
284 TemplateInstance *get_ti_byIndex(size_t p_i) const { return tis[p_i]; }
285 void set_code_section(GovernedSimple::code_section_t p_code_section);
286 };
287
288 /** A named actual parameter */
289 class NamedParam : public Node, public Location {
290 Identifier *name;
291 TemplateInstance *tmpl;
292
293 NamedParam(const NamedParam& p);
294 /** Copy assignment disabled. */
295 NamedParam& operator=(const NamedParam& p);
296 public:
297 NamedParam(Identifier *id, TemplateInstance *t);
298 virtual ~NamedParam();
299 /** "Virtual copy constructor" */
300 virtual NamedParam *clone() const;
301 virtual void set_fullname(const string& p_fullname);
302 virtual void set_my_scope(Scope *p_scope);
303
304 Identifier *get_name() const { return name; }
305 TemplateInstance *get_ti() const { return tmpl; }
306 TemplateInstance *extract_ti();
307
308 virtual void dump(unsigned int level) const;
309 };
310
311 /** A collection of named actual parameters */
312 class NamedParams : public Node, public Location {
313 private:
314 vector<NamedParam> nps;
315
316 NamedParams(const NamedParams& p);
317 /** Copy assignment disabled. */
318 NamedParams& operator=(const NamedParams& p);
319 public:
320 NamedParams();
321 /** Destructor. Empties \p nps and frees its elements. */
322 virtual ~NamedParams();
323 /** "Virtual copy constructor" */
324 virtual NamedParams *clone() const;
325 void set_my_scope(Scope *p_scope);
326 void set_fullname(const string& p_fullname);
327
328 /** Append \p p to the list of named actual parameters. */
329 void add_np(NamedParam *p);
330
331 size_t get_nof_nps() const;
332
333 /** Replace the named parameter at index \p p_i with NULL and
334 * return its previous value. */
335 NamedParam *extract_np_byIndex(size_t p_i);
336
337 virtual void dump(unsigned int level) const;
338 };
339
340 /** Actual parameters from the parser in "raw form".
341 *
342 * Contains both positional and named parameters.
343 * There is not enough information during parsing to construct a "full"
344 * ActualParameters object (information about formal parameters is needed).
345 * This object holds the available information until the ActualParameters
346 * object can be constructed during semantic analysis.
347 *
348 * TODO think of a shorter name */
349 class ParsedActualParameters : public Node, public Location {
350 TemplateInstances *unnamedpart; ///< the "classic" unnamed parameters
351 NamedParams *namedpart; ///< the named parameters
352
353 ParsedActualParameters(const ParsedActualParameters& p);
354 /** Copy assignment disabled. */
355 ParsedActualParameters& operator=(const ParsedActualParameters& p);
356 public:
357 ParsedActualParameters(TemplateInstances *p_ti = 0, NamedParams *p_np = 0);
358 virtual ~ParsedActualParameters();
359 virtual ParsedActualParameters *clone() const;
360
361 // @name Distributors. Pass on the call to \a namedpart and \a unnamedpart.
362 // @{
363 virtual void set_fullname(const string& p_fullname);
364 virtual void set_my_scope(Scope *p_scope);
365 void set_location(const char *p_filename, int p_lineno=0);
366 void set_location(const char *p_filename, const YYLTYPE& p_yyloc);
367 void set_location(const char *p_filename, const YYLTYPE& p_firstloc,
368 const YYLTYPE& p_lastloc);
369 void set_location(const char *p_filename, int p_first_line,
370 int p_first_column, int p_last_line, int p_last_column);
371 virtual void dump(unsigned int level) const;
372 // @}
373
374 /** Return the TemplateInstances member.
375 * \note ParsedActualParameters object owns the TemplateInstances object;
376 * the caller should clone() it if it wants a copy.
377 *
378 * The return value should be a const reference to prevent the caller
379 * from freeing it. */
380 TemplateInstances *get_tis() const { return unnamedpart; }
381
382 TemplateInstances* steal_tis();
383
384 size_t get_nof_tis() const { return unnamedpart->get_nof_tis(); }
385
386 TemplateInstance *get_ti_byIndex(size_t p_i) const {
387 return unnamedpart->get_ti_byIndex(p_i); }
388
389 TemplateInstance *set_ti_byIndex(size_t p_i, TemplateInstance *p_np) {
390 TemplateInstance *retval = unnamedpart->get_ti_byIndex(p_i);
391 unnamedpart->tis[p_i] = p_np;
392 return retval;
393 }
394
395 /// Append \p p_ti to the internal list of TemplateInstance s
396 void add_ti(TemplateInstance *p_ti){
397 unnamedpart->add_ti(p_ti);
398 }
399
400 /// Calls TemplateInstances::set_code_section() for \p unnamedpart
401 void set_code_section(GovernedSimple::code_section_t p_code_section) {
402 unnamedpart->set_code_section(p_code_section);
403 }
404
405 // Named params section
406
407 /// Append \p np to the list in \p namedpart
408 void add_np(NamedParam *np);
409
410 size_t get_nof_nps() const;
411
412 NamedParam *extract_np_byIndex(size_t p_i) {
413 return namedpart->extract_np_byIndex(p_i);
414 }
415 };
416
417 } // namespace Ttcn
418
419 #endif // _Ttcn_Templatestuff_HH
This page took 0.042406 seconds and 5 git commands to generate.