Commit | Line | Data |
---|---|---|
970ed795 EL |
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 _Subtype_HH | |
9 | #define _Subtype_HH | |
10 | ||
11 | #include "ttcn3/compiler.h" | |
12 | #include "vector.hh" | |
13 | #include "map.hh" | |
14 | #include "Int.hh" | |
15 | #include "Real.hh" | |
16 | #include "ustring.hh" | |
17 | #include "Setting.hh" | |
18 | ||
19 | #include "subtypestuff.hh" | |
20 | ||
21 | namespace Ttcn { | |
22 | class LengthRestriction; | |
23 | class Template; | |
24 | class ValueRange; | |
25 | class PatternString; | |
26 | } | |
27 | ||
28 | namespace Common { | |
29 | ||
30 | class Identifier; | |
31 | class Value; | |
32 | class Type; | |
33 | class Constraints; | |
34 | ||
35 | /** | |
36 | * class for parsing type restrictions | |
37 | */ | |
38 | class SubTypeParse { | |
39 | public: | |
40 | enum STPselection { STP_SINGLE, STP_RANGE, STP_LENGTH, STP_PATTERN }; | |
41 | private: | |
42 | /// Type selector | |
43 | STPselection selection; | |
44 | /// Restriction info, owned and freed by the object. | |
45 | union { | |
46 | Value *single; ///< for STP_SINGLE | |
47 | struct { | |
48 | Value *min, *max; | |
49 | bool min_exclusive, max_exclusive; | |
50 | } range; ///< for STP_RANGE | |
51 | Ttcn::LengthRestriction *length; ///< for STP_LENGTH | |
52 | Ttcn::PatternString* pattern; ///< for STP_PATTERN | |
53 | }; | |
54 | /** Copy constructor disabled */ | |
55 | SubTypeParse(const SubTypeParse& p); | |
56 | /** Assignment disabled */ | |
57 | SubTypeParse& operator=(const SubTypeParse& p); | |
58 | public: | |
59 | /** Construct a single value restriction */ | |
60 | SubTypeParse(Value *p_single); | |
61 | /** Construct a range restriction | |
62 | * | |
63 | * @param p_min lower bound | |
64 | * @param p_max upper bound | |
65 | * | |
66 | */ | |
67 | SubTypeParse(Value *p_min, bool p_min_exclusive, Value *p_max, bool p_max_exclusive); | |
68 | /** Construct from a length restriction | |
69 | * | |
70 | * @param p_length length restriction, SubTypeParse becomes owner | |
71 | */ | |
72 | SubTypeParse(Ttcn::LengthRestriction *p_length); | |
73 | /** Construct from a pattern | |
74 | * | |
75 | * @param p_pattern pattern, SubTypeParse becomes owner | |
76 | */ | |
77 | SubTypeParse(Ttcn::PatternString *p_pattern); | |
78 | /// Destructor | |
79 | ~SubTypeParse(); | |
80 | ||
81 | /// Return the restriction type | |
82 | STPselection get_selection() const { return selection; } | |
83 | ||
84 | /** Return the single value of the restriction. | |
85 | * | |
86 | * @pre selection is STP_SINGLE, or else FATAL_ERROR | |
87 | */ | |
88 | Value *Single() const; | |
89 | /** Return the lower bound of the range. | |
90 | * | |
91 | * @pre selection is STP_RANGE, or else FATAL_ERROR | |
92 | */ | |
93 | Value *Min() const; | |
94 | bool MinExclusive() const; | |
95 | /** Return the upper bound of the range. | |
96 | * | |
97 | * @pre selection is STP_RANGE, or else FATAL_ERROR | |
98 | */ | |
99 | Value *Max() const; | |
100 | bool MaxExclusive() const; | |
101 | /** Return the length restriction object. | |
102 | * | |
103 | * @ore selection is STP_LENGTH, or else FATAL_ERROR | |
104 | */ | |
105 | Ttcn::LengthRestriction *Length() const; | |
106 | Ttcn::PatternString *Pattern() const; | |
107 | }; | |
108 | ||
109 | /** | |
110 | * Container for all possible subtype constraint classes | |
111 | */ | |
112 | class SubtypeConstraint | |
113 | { | |
114 | public: | |
115 | enum subtype_t { | |
116 | ST_ERROR, | |
117 | ST_INTEGER, | |
118 | ST_FLOAT, | |
119 | ST_BOOLEAN, | |
120 | ST_OBJID, | |
121 | ST_VERDICTTYPE, | |
122 | ST_BITSTRING, | |
123 | ST_HEXSTRING, | |
124 | ST_OCTETSTRING, | |
125 | ST_CHARSTRING, | |
126 | ST_UNIVERSAL_CHARSTRING, | |
127 | ST_RECORD, | |
128 | ST_RECORDOF, | |
129 | ST_SET, | |
130 | ST_SETOF, | |
131 | ST_ENUM, | |
132 | ST_UNION, | |
133 | ST_FUNCTION, | |
134 | ST_ALTSTEP, | |
135 | ST_TESTCASE | |
136 | }; | |
137 | protected: | |
138 | subtype_t subtype; | |
139 | union { | |
140 | IntegerRangeListConstraint* integer_st; // ST_INTEGER | |
141 | RealRangeListConstraint* float_st; // ST_FLOAT | |
142 | BooleanListConstraint* boolean_st; // ST_BOOLEAN | |
143 | VerdicttypeListConstraint* verdict_st; // ST_VERDICTTYPE | |
144 | BitstringConstraint* bitstring_st; // ST_BITSTRING | |
145 | HexstringConstraint* hexstring_st; // ST_HEXSTRING | |
146 | OctetstringConstraint* octetstring_st; // ST_OCTETSTRING | |
147 | CharstringSubtypeTreeElement* charstring_st; // ST_CHARSTRING | |
148 | UniversalCharstringSubtypeTreeElement* universal_charstring_st; // ST_UNIVERSAL_CHARSTRING | |
149 | RecofConstraint* recof_st; // ST_RECORDOF, ST_SETOF | |
150 | ValueListConstraint* value_st; // ST_RECORD, ST_SET, ST_UNION, ST_FUNCTION, ST_ALTSTEP, ST_TESTCASE, ST_OBJID, ST_ENUM | |
151 | // TODO: use more precise subtype classes for ST_OBJID and ST_ENUM | |
152 | }; | |
153 | // the xxx_st values are aggregate subtype restriction sets, the distinct | |
154 | // length restrictions are lost in them, the length restriction is also stored separately | |
155 | SizeRangeListConstraint* length_restriction; | |
156 | ||
157 | void set_to_error(); // clears the subtype information and sets value to ST_ERROR | |
158 | ||
159 | /// Copy constructor disabled | |
160 | SubtypeConstraint(const SubtypeConstraint&); | |
161 | /// Assignment disabled | |
162 | SubtypeConstraint& operator=(const SubtypeConstraint&); | |
163 | public: | |
164 | SubtypeConstraint(subtype_t st); | |
165 | virtual ~SubtypeConstraint() { set_to_error(); } | |
166 | ||
167 | /** copy the content of other to this (deletes content of this) */ | |
168 | void copy(const SubtypeConstraint* other); | |
169 | ||
170 | /** set operations */ | |
171 | void intersection(const SubtypeConstraint* other); | |
172 | void union_(const SubtypeConstraint* other); | |
173 | void except(const SubtypeConstraint* other); | |
174 | ||
175 | tribool is_subset(const SubtypeConstraint* other) const; | |
176 | ||
177 | /** special ASN.1 types (NumericString, etc.) have default subtype constraints, | |
178 | return default constraint or NULL */ | |
179 | static SubtypeConstraint* get_asn_type_constraint(Type* type); | |
180 | static SubtypeConstraint* create_from_asn_value(Type* type, Value* value); | |
181 | static SubtypeConstraint* create_from_asn_charvalues(Type* type, Value* value); | |
182 | static SubtypeConstraint* create_from_contained_subtype( | |
183 | SubtypeConstraint* contained_stc, bool char_context, Location* loc); | |
184 | int_limit_t get_int_limit(bool is_upper, Location* loc); ///< helper func. of create_from_asn_range() | |
185 | static SubtypeConstraint* create_from_asn_range( | |
186 | Value* vmin, bool min_exclusive, | |
187 | Value* vmax, bool max_exclusive, | |
188 | Location* loc, subtype_t st_t, SubtypeConstraint* parent_subtype); | |
189 | static SubtypeConstraint* create_asn_size_constraint( | |
190 | SubtypeConstraint* integer_stc, bool char_context, Type* type, Location* loc); | |
191 | static SubtypeConstraint* create_permitted_alphabet_constraint( | |
192 | SubtypeConstraint* stc, bool char_context, Type* type, Location* loc); | |
193 | ||
194 | subtype_t get_subtypetype() const { return subtype; } | |
195 | virtual string to_string() const; | |
196 | /** Two subtypes are compatible if their intersection is not an empty set */ | |
197 | bool is_compatible(const SubtypeConstraint *p_stp) const; | |
198 | /** Check if this string subtype is compatible with a string element */ | |
199 | bool is_compatible_with_elem() const; | |
200 | // used to check compatibility of structured types | |
201 | bool is_length_compatible(const SubtypeConstraint *p_st) const; | |
af710487 | 202 | bool is_upper_limit_infinity() const; |
203 | bool is_lower_limit_infinity() const; | |
970ed795 EL |
204 | }; |
205 | ||
206 | /** | |
207 | * class for semantic analysis of subtypes | |
208 | */ | |
209 | class SubType : public SubtypeConstraint { | |
210 | ||
211 | Type *my_owner; ///< Pointer to the type, object not owned | |
212 | SubType* parent_subtype; ///< pointer to the inherited subtype, not owned | |
213 | vector<SubTypeParse> *parsed; ///< TTCN-3 parsed subtype data, not owned | |
214 | Constraints* asn_constraints; ///< constraints of ASN.1 type or NULL, not owned | |
215 | ||
216 | SubtypeConstraint* root; ///< root part of the ASN.1 subtype, owned by asn_constraints | |
217 | bool extendable; ///< ASN.1 extendable type | |
218 | SubtypeConstraint* extension; ///< ASN.1 extension addition, owned by asn_constraints | |
219 | ||
220 | enum checked_t { | |
221 | STC_NO, | |
222 | STC_CHECKING, | |
223 | STC_YES | |
224 | } checked; | |
225 | ||
226 | map<SubType*,void> my_parents; // used to check for circular references | |
227 | ||
228 | /// Copy constructor disabled | |
229 | SubType(const SubType&); | |
230 | /// Assignment disabled | |
231 | SubType& operator=(const SubType&); | |
232 | public: | |
233 | ||
234 | SubType(subtype_t st, Type *p_my_owner, SubType* p_parent_subtype, | |
235 | vector<SubTypeParse> *p_parsed, Constraints* p_asn_constraints); | |
236 | ~SubType(); | |
237 | ||
238 | SubtypeConstraint* get_root() { return root ? root : this; } | |
239 | bool is_extendable() { return extendable; } | |
240 | SubtypeConstraint* get_extension() { return extension; } | |
241 | ||
242 | string to_string() const; | |
243 | ||
244 | /** Set restrictions. | |
245 | * | |
246 | * @param r list of restrictions | |
247 | * @pre set_restrictions() has not been called before. */ | |
248 | void set_restrictions(vector<SubTypeParse> *r); | |
249 | const Type *get_owner() const { return my_owner; } | |
250 | ||
251 | void chk(); | |
252 | ||
253 | void chk_this_value(Value *value); | |
254 | void chk_this_template_generic(Ttcn::Template *templ); | |
255 | ||
256 | /// No-op. | |
257 | void generate_code(output_struct &); | |
258 | ||
259 | void dump(unsigned level) const; | |
260 | ||
261 | /// return single length restriction or -1 | |
262 | Int get_length_restriction() const; | |
263 | /// true unless a length restriction prohibits zero elements | |
264 | bool zero_length_allowed() const; | |
265 | ||
266 | bool add_parent_subtype(SubType* st); | |
267 | private: | |
268 | void print_full_warning() const; | |
269 | bool chk_recursion(ReferenceChain& refch); | |
270 | // adding a single value/type constraint, union (works only in case of TTCN-3 BNF) | |
271 | bool add_ttcn_type_list_subtype(SubType* p_st); | |
272 | void add_ttcn_value(Value *v); | |
273 | void add_ttcn_recof(Value *v); | |
274 | bool add_ttcn_single(Value *val, size_t restriction_index); | |
275 | // adding a value range constraint, makes a union (works for TTCN-3 only) | |
276 | bool add_ttcn_range(Value *min, bool min_exclusive, Value *max, bool max_exclusive, size_t restriction_index, bool has_other); | |
277 | // adding a length constraint, makes an intersection | |
278 | bool set_ttcn_length(const size_limit_t& min, const size_limit_t& max); | |
279 | void chk_boundary_valid(Value* boundary, Int max_value, const char* boundary_name); | |
280 | bool add_ttcn_length(Ttcn::LengthRestriction *lr, size_t restriction_index); | |
281 | // adding a pattern constraint, add with intersection | |
282 | bool add_ttcn_pattern(Ttcn::PatternString* pattern, size_t restriction_index); | |
283 | ||
284 | void chk_this_template(Ttcn::Template *templ); | |
285 | void chk_this_template_pattern(const char *patt_type,Ttcn::Template *templ); | |
286 | void chk_this_template_length_restriction(Ttcn::Template *templ); | |
287 | };// class SubType | |
288 | ||
289 | }// namespace Common | |
290 | ||
291 | #endif // #ifndef _Subtype_HH |