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