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 ///////////////////////////////////////////////////////////////////////////////
11 #include "../string.hh"
12 #include "../vector.hh"
13 #include "../Identifier.hh"
14 #include "../Setting.hh"
15 #include "AST_ttcn3.hh"
19 using namespace Common;
25 class TemplateInstance;
27 /** Attribute qualifier (DefOrFieldRef). */
28 class Qualifier: public FieldOrArrayRefs, public Location
30 /// Assignment disabled.
31 Qualifier& operator=(const Qualifier& p);
33 Qualifier(): FieldOrArrayRefs(), Location() { }
34 Qualifier(const Qualifier& p): FieldOrArrayRefs(p), Location(p) { }
35 virtual Qualifier* clone() const;
36 /** return array indexes as "_0" identifiers TODO: remove */
37 const Identifier* get_identifier(size_t p_index) const;
38 /** use with get_identifier() TODO: remove */
39 size_t get_nof_identifiers() const { return get_nof_refs(); }
40 /** Returns a newly allocated Qualifier, without the first identifier.
41 * Used by Common::Type::parse_raw to propagate an attribute
42 * with a qualifier from a type (record,union,rec-of) to its component */
43 Qualifier* get_qualifier_without_first_id() const;
44 string get_stringRepr() const;
45 void dump(unsigned level) const;
48 /** Attribute qualifiers, when there were multiple qualifiers
49 * in the DefOrFieldRefList.
51 * E.g. the three-element list from below:
52 * @code variant (foo.f00, bar, baz[-]) "something" @endcode */
53 class Qualifiers: public Node{
54 /// Copy constructor disabled.
55 Qualifiers(const Qualifiers& p);
56 /// Assignment disabled.
57 Qualifiers& operator=(const Qualifiers& p);
59 vector<Qualifier> qualifiers;
61 Qualifiers() : Node() { }
63 size_t get_nof_qualifiers() const { return qualifiers.size(); }
64 void add_qualifier(Qualifier* p_qualifier);
65 void delete_qualifier(size_t p_index);
66 const Qualifier* get_qualifier(size_t p_index) const;
67 virtual Qualifiers* clone() const;
68 virtual void set_fullname(const string& p_fullname);
69 bool has_qualifier(Qualifier* p_qualifier)const;
70 void dump(unsigned level) const;
74 * The errouneous attribute spec. string is parsed into this AST node
76 class ErroneousAttributeSpec : public Node, public Location
86 ErroneousAttributeSpec(const ErroneousAttributeSpec& p);
87 /// Assignment disabled.
88 ErroneousAttributeSpec& operator=(const ErroneousAttributeSpec& p);
91 indicator_t indicator;
92 TemplateInstance* tmpl_inst;
93 Type* type; // set by chk or NULL if tmpl_inst is invalid or omit
94 Value* value; // set by chk or NULL if tmpl_inst is invalid or omit
95 string first_genname; // used by generate_code_str()to avoid generating
96 // multiple C++ objects for the same TTCN-3 constant value,
97 // after the first object all are only references
99 ErroneousAttributeSpec(bool p_is_raw, indicator_t p_indicator, TemplateInstance* p_tmpl_inst, bool p_has_all_keyword);
100 ~ErroneousAttributeSpec();
101 ErroneousAttributeSpec *clone() const;
102 void set_fullname(const string& p_fullname);
103 void set_my_scope(Scope *p_scope);
104 void dump(unsigned level) const;
105 /** basic check, the qualifier of the field is not known here */
107 indicator_t get_indicator() const { return indicator; }
108 Type* get_type() const { return type; }
109 bool get_is_raw() const { return is_raw; }
110 bool get_is_omit() const;
111 static const char* get_indicator_str(indicator_t i);
112 char* generate_code_str(char *str, string genname);
113 char* generate_code_init_str(char *str, string genname);
114 string get_typedescriptor_str();
115 void chk_recursions(ReferenceChain& refch);
119 * helper to construct the tree of erroneous attributes
121 struct ErroneousValues {
122 ErroneousAttributeSpec *before, *value, *after; // NULL if not specified
123 string field_name; // qualifier string
124 ErroneousValues(const string& p_field_name): before(0), value(0), after(0), field_name(p_field_name) {}
125 char* generate_code_embedded_str(char *str, string genname);
126 char* generate_code_init_str(char *str, string genname);
127 char* generate_code_embedded_str(char *str, string genname, ErroneousAttributeSpec* attr_spec);
128 char* generate_code_struct_str(char *str, string genname, int field_index);
129 void chk_recursions(ReferenceChain& refch);
133 * helper to construct the tree of erroneous attributes
135 struct ErroneousDescriptor {
136 int omit_before, omit_after; // -1 if not set
137 string omit_before_name, omit_after_name; // qualifier string or empty
138 map<size_t,ErroneousDescriptor> descr_m; // descriptors for the fields
139 map<size_t,ErroneousValues> values_m; // erroneous values for the fields
140 ErroneousDescriptor(const ErroneousDescriptor& p); // disabled
141 ErroneousDescriptor& operator=(const ErroneousDescriptor& p); // disabled
143 ErroneousDescriptor(): omit_before(-1), omit_after(-1) {}
144 ~ErroneousDescriptor();
145 char* generate_code_embedded_str(char *str, string genname);
146 char* generate_code_init_str(char *str, string genname);
147 char* generate_code_struct_str(char *str, string genname, int field_index);
148 char* generate_code_str(char *str, string genname);
149 void chk_recursions(ReferenceChain& refch);
153 * Contains erroneous attr. specs and the pointers to the qualifiers
155 class ErroneousAttributes : public Node
159 const Qualifier* qualifier; // not owned
160 ErroneousAttributeSpec* err_attr; // not owned
161 dynamic_array<size_t> subrefs_array; // used in chk()
162 dynamic_array<Type*> type_array; // used in chk()
165 Type* type; // not owned
166 vector<ErroneousAttributeSpec> spec_vec; // all elements owned
167 dynamic_array<field_err_t> field_array;
168 ErroneousDescriptor* err_descr_tree; // owned, constructed in chk()
169 ErroneousAttributes(const ErroneousAttributes& p);
170 /// Assignment disabled.
171 ErroneousAttributes& operator=(const ErroneousAttributes& p);
172 /** builds a tree of descriptors and err.values calling itself recursively, reports errors */
173 ErroneousDescriptor* build_descr_tree(dynamic_array<field_err_t>& fld_array);
175 ErroneousAttributes(Type* p_type);
176 ~ErroneousAttributes();
177 ErroneousAttributes *clone() const;
178 void set_fullname(const string& p_fullname);
179 void dump(unsigned level) const;
181 void add_spec(ErroneousAttributeSpec* err_attr_spec);
182 void add_pair(const Qualifier* qualifier, ErroneousAttributeSpec* err_attr_spec);
183 /** check every field_err_t value */
185 ErroneousDescriptor* get_err_descr() const { return err_descr_tree; }
189 * Stores the attribute specification and its location
191 class AttributeSpec : public Node, public Location {
192 /// Assignment disabled, even though copy constructor isn't
193 AttributeSpec& operator=(const AttributeSpec& p);
195 string spec; ///< The attribute specification (free text)
196 /// Copy constructor, for clone() only
197 AttributeSpec(const AttributeSpec& p)
198 : Node(p), Location(p), spec(p.spec) { }
200 AttributeSpec(const string& p_spec)
201 : Node(), Location(), spec(p_spec) { }
202 virtual AttributeSpec* clone() const;
203 virtual void set_fullname(const string& p_fullname);
204 const string& get_spec() const { return spec; }
205 virtual void dump(unsigned level) const;
209 * Stores a single attribute
211 class SingleWithAttrib : public Node, public Location {
212 /// Copy constructor disabled.
213 SingleWithAttrib(const SingleWithAttrib& p);
214 /// Assignment disabled.
215 SingleWithAttrib& operator=(const SingleWithAttrib& p);
224 AT_INVALID /// invalid keyword was used
227 attribtype_t attribKeyword;
228 /// True if the \c override keyword was used
230 /// The stuff in parenthesis before the attribute text. Owned.
231 Qualifiers *attribQualifiers;
232 /// The attribute text (FreeText). Owned.
233 AttributeSpec* attribSpec;
236 SingleWithAttrib(attribtype_t p_attribKeyword, bool p_hasOverride,
237 Qualifiers *p_attribQualifiers, AttributeSpec *p_attribSpec);
239 virtual SingleWithAttrib* clone() const;
240 virtual void set_fullname(const string& p_fullname);
241 attribtype_t get_attribKeyword() const{ return attribKeyword; }
242 bool has_override() const { return hasOverride; }
243 AttributeSpec const& get_attribSpec() const { return *attribSpec; }
244 Qualifiers *get_attribQualifiers() const { return attribQualifiers; }
245 virtual void dump(unsigned level) const;
249 * Stores all the attributes found in one \c with statement
251 class MultiWithAttrib: public Node, public Location{
252 /// Copy constructor disabled.
253 MultiWithAttrib(const MultiWithAttrib& p);
254 /// Assignment disabled.
255 MultiWithAttrib& operator=(const MultiWithAttrib& p);
257 vector<SingleWithAttrib> elements;
259 MultiWithAttrib() : Node(), Location() { }
261 virtual MultiWithAttrib* clone() const;
262 virtual void set_fullname(const string& p_fullname);
263 size_t get_nof_elements() const{ return elements.size();}
264 void add_element(SingleWithAttrib* p_single){ elements.add(p_single);}
265 void add_element_front(SingleWithAttrib* p_single)
266 { elements.add_front(p_single);}
267 const SingleWithAttrib* get_element(size_t p_index) const;
268 SingleWithAttrib* get_element_for_modification(size_t p_index);
269 void delete_element(size_t p_index);
270 virtual void dump(unsigned level) const;
274 * With this class we create a path from the actual type to the module
275 * including all groups.
279 * - Ttcn::ControlPart
286 class WithAttribPath : public Node {
287 /// Copy constructor disabled.
288 WithAttribPath(const WithAttribPath& p);
289 /// Assignment disabled.
290 WithAttribPath& operator=(const WithAttribPath& p);
292 bool had_global_variants;
293 bool attributes_checked;
296 WithAttribPath* parent;
297 MultiWithAttrib* m_w_attrib;
298 vector<SingleWithAttrib> cache;
300 void qualifierless_attrib_finder(vector<SingleWithAttrib>& p_result,
301 bool& stepped_over_encode);
303 WithAttribPath() : Node(), had_global_variants(false),
304 attributes_checked(false), cached(false), s_o_encode(false),
305 parent(0), m_w_attrib(0) { }
307 virtual WithAttribPath* clone() const;
308 virtual void set_fullname(const string& p_fullname);
309 void set_had_global_variants(bool has) { had_global_variants = has; }
310 bool get_had_global_variants() { return had_global_variants; }
311 void chk_no_qualif();
312 void chk_global_attrib(bool erroneous_allowed=false);
313 void set_parent(WithAttribPath* p_parent) { parent = p_parent; }
314 WithAttribPath* get_parent() { return parent; }
315 /** Set the contained attribute.
318 * @pre \c add_with_attr has not been called before with a non-NULL arg
319 * @post \c attributes_checked is \c false
321 void set_with_attr(MultiWithAttrib* p_m_w_attr);
322 MultiWithAttrib* get_with_attr() { return m_w_attrib;}
323 /** Fills the cache and returns it.
325 * @return a reference to the cached list of qualifier-less attributes.
327 const vector<SingleWithAttrib>& get_real_attrib();
328 const MultiWithAttrib* get_local_attrib() const { return m_w_attrib; }
330 virtual void dump(unsigned int level) const;