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
18 * Szabo, Janos Zoltan – initial implementation
22 ******************************************************************************/
26 #include "ttcn3/compiler.h"
34 #include "subtypestuff.hh"
39 class LengthRestriction;
53 * class for parsing type restrictions
57 enum STPselection { STP_SINGLE, STP_RANGE, STP_LENGTH, STP_PATTERN };
60 STPselection selection;
61 /// Restriction info, owned and freed by the object.
63 Value *single; ///< for STP_SINGLE
66 bool min_exclusive, max_exclusive;
67 } range; ///< for STP_RANGE
68 Ttcn::LengthRestriction *length; ///< for STP_LENGTH
69 Ttcn::PatternString* pattern; ///< for STP_PATTERN
71 /** Copy constructor disabled */
72 SubTypeParse(const SubTypeParse& p);
73 /** Assignment disabled */
74 SubTypeParse& operator=(const SubTypeParse& p);
76 /** Construct a single value restriction */
77 SubTypeParse(Value *p_single);
78 /** Construct a range restriction
80 * @param p_min lower bound
81 * @param p_max upper bound
84 SubTypeParse(Value *p_min, bool p_min_exclusive, Value *p_max, bool p_max_exclusive);
85 /** Construct from a length restriction
87 * @param p_length length restriction, SubTypeParse becomes owner
89 SubTypeParse(Ttcn::LengthRestriction *p_length);
90 /** Construct from a pattern
92 * @param p_pattern pattern, SubTypeParse becomes owner
94 SubTypeParse(Ttcn::PatternString *p_pattern);
98 /// Return the restriction type
99 STPselection get_selection() const { return selection; }
101 /** Return the single value of the restriction.
103 * @pre selection is STP_SINGLE, or else FATAL_ERROR
105 Value *Single() const;
106 /** Return the lower bound of the range.
108 * @pre selection is STP_RANGE, or else FATAL_ERROR
111 bool MinExclusive() const;
112 /** Return the upper bound of the range.
114 * @pre selection is STP_RANGE, or else FATAL_ERROR
117 bool MaxExclusive() const;
118 /** Return the length restriction object.
120 * @ore selection is STP_LENGTH, or else FATAL_ERROR
122 Ttcn::LengthRestriction *Length() const;
123 Ttcn::PatternString *Pattern() const;
127 * Container for all possible subtype constraint classes
129 class SubtypeConstraint
143 ST_UNIVERSAL_CHARSTRING,
157 IntegerRangeListConstraint* integer_st; // ST_INTEGER
158 RealRangeListConstraint* float_st; // ST_FLOAT
159 BooleanListConstraint* boolean_st; // ST_BOOLEAN
160 VerdicttypeListConstraint* verdict_st; // ST_VERDICTTYPE
161 BitstringConstraint* bitstring_st; // ST_BITSTRING
162 HexstringConstraint* hexstring_st; // ST_HEXSTRING
163 OctetstringConstraint* octetstring_st; // ST_OCTETSTRING
164 CharstringSubtypeTreeElement* charstring_st; // ST_CHARSTRING
165 UniversalCharstringSubtypeTreeElement* universal_charstring_st; // ST_UNIVERSAL_CHARSTRING
166 RecofConstraint* recof_st; // ST_RECORDOF, ST_SETOF
167 ValueListConstraint* value_st; // ST_RECORD, ST_SET, ST_UNION, ST_FUNCTION, ST_ALTSTEP, ST_TESTCASE, ST_OBJID, ST_ENUM
168 // TODO: use more precise subtype classes for ST_OBJID and ST_ENUM
170 // the xxx_st values are aggregate subtype restriction sets, the distinct
171 // length restrictions are lost in them, the length restriction is also stored separately
172 SizeRangeListConstraint* length_restriction;
174 void set_to_error(); // clears the subtype information and sets value to ST_ERROR
176 /// Copy constructor disabled
177 SubtypeConstraint(const SubtypeConstraint&);
178 /// Assignment disabled
179 SubtypeConstraint& operator=(const SubtypeConstraint&);
181 SubtypeConstraint(subtype_t st);
182 virtual ~SubtypeConstraint() { set_to_error(); }
184 /** copy the content of other to this (deletes content of this) */
185 void copy(const SubtypeConstraint* other);
187 /** set operations */
188 void intersection(const SubtypeConstraint* other);
189 void union_(const SubtypeConstraint* other);
190 void except(const SubtypeConstraint* other);
192 tribool is_subset(const SubtypeConstraint* other) const;
194 /** special ASN.1 types (NumericString, etc.) have default subtype constraints,
195 return default constraint or NULL */
196 static SubtypeConstraint* get_asn_type_constraint(Type* type);
197 static SubtypeConstraint* create_from_asn_value(Type* type, Value* value);
198 static SubtypeConstraint* create_from_asn_charvalues(Type* type, Value* value);
199 static SubtypeConstraint* create_from_contained_subtype(
200 SubtypeConstraint* contained_stc, bool char_context, Location* loc);
201 int_limit_t get_int_limit(bool is_upper, Location* loc); ///< helper func. of create_from_asn_range()
202 static SubtypeConstraint* create_from_asn_range(
203 Value* vmin, bool min_exclusive,
204 Value* vmax, bool max_exclusive,
205 Location* loc, subtype_t st_t, SubtypeConstraint* parent_subtype);
206 static SubtypeConstraint* create_asn_size_constraint(
207 SubtypeConstraint* integer_stc, bool char_context, Type* type, Location* loc);
208 static SubtypeConstraint* create_permitted_alphabet_constraint(
209 SubtypeConstraint* stc, bool char_context, Type* type, Location* loc);
211 subtype_t get_subtypetype() const { return subtype; }
212 virtual string to_string() const;
213 /** Two subtypes are compatible if their intersection is not an empty set */
214 bool is_compatible(const SubtypeConstraint *p_stp) const;
215 /** Check if this string subtype is compatible with a string element */
216 bool is_compatible_with_elem() const;
217 // used to check compatibility of structured types
218 bool is_length_compatible(const SubtypeConstraint *p_st) const;
219 bool is_upper_limit_infinity() const;
220 bool is_lower_limit_infinity() const;
224 * class for semantic analysis of subtypes
226 class SubType : public SubtypeConstraint {
228 Type *my_owner; ///< Pointer to the type, object not owned
229 SubType* parent_subtype; ///< pointer to the inherited subtype, not owned
230 vector<SubTypeParse> *parsed; ///< TTCN-3 parsed subtype data, not owned
231 Constraints* asn_constraints; ///< constraints of ASN.1 type or NULL, not owned
233 SubtypeConstraint* root; ///< root part of the ASN.1 subtype, owned by asn_constraints
234 bool extendable; ///< ASN.1 extendable type
235 SubtypeConstraint* extension; ///< ASN.1 extension addition, owned by asn_constraints
243 map<SubType*,void> my_parents; // used to check for circular references
245 /// Copy constructor disabled
246 SubType(const SubType&);
247 /// Assignment disabled
248 SubType& operator=(const SubType&);
251 SubType(subtype_t st, Type *p_my_owner, SubType* p_parent_subtype,
252 vector<SubTypeParse> *p_parsed, Constraints* p_asn_constraints);
255 SubtypeConstraint* get_root() { return root ? root : this; }
256 bool is_extendable() { return extendable; }
257 SubtypeConstraint* get_extension() { return extension; }
259 string to_string() const;
261 /** Set restrictions.
263 * @param r list of restrictions
264 * @pre set_restrictions() has not been called before. */
265 void set_restrictions(vector<SubTypeParse> *r);
266 const Type *get_owner() const { return my_owner; }
270 void chk_this_value(Value *value);
271 void chk_this_template_generic(Ttcn::Template *templ);
274 void generate_code(output_struct &);
276 /** Returns true if there are JSON schema elements to be generated for this subtype */
277 boolean has_json_schema() const { return parsed != NULL; }
279 /** Generates the JSON schema segment for the type restrictions.
281 * The float special values NaN, INF and -INF are not included in the code
282 * generated for float value list restrictions if the 2nd parameter is false. */
283 void generate_json_schema(JSON_Tokenizer& json, bool allow_special_float = true);
285 /** Generates the JSON values inside the subtype's value list restriction.
286 * Recursive (it also inserts the values of referenced subtypes into the list).
288 * The float special values NaN, INF and -INF are not included in the code
289 * generated for float value lists if the 2nd parameter is false. */
290 void generate_json_schema_value_list(JSON_Tokenizer& json, bool allow_special_float,
291 bool union_value_list);
293 /** Generates the JSON schema elements for integer and float range restrictions.
294 * If there are multiple restrictions, then they are placed in an 'anyOf' structure,
295 * each one in a JSON object. The function also inserts the separators between these
296 * objects (the 2nd parameter indicates whether the first range has been inserted).
298 * Recursive (it also inserts the value ranges of referenced subtypes).
299 * @return true, if the first value range has not been inserted yet */
300 bool generate_json_schema_number_ranges(JSON_Tokenizer& json, bool first = true);
302 /** Generates the segments of the JSON schema string pattern (regex) used for
303 * representing the range restrictions of charstrings and universal charstrings.
304 * A value range (inside a regex set expression) is generated for each TTCN-3
307 * Recursive (it also inserts the string ranges of referenced subtypes). */
308 char* generate_json_schema_string_ranges(char* pattern_str);
310 /** Generates the JSON schema segment of the float type this subtype belongs to
311 * (the schema segment for the whole type is generated, not only the type's
313 * This replaces the schema segment generated by Type::generate_json_schema().*/
314 void generate_json_schema_float(JSON_Tokenizer& json);
316 void dump(unsigned level) const;
318 /// return single length restriction or -1
319 Int get_length_restriction() const;
320 /// true unless a length restriction prohibits zero elements
321 bool zero_length_allowed() const;
323 bool add_parent_subtype(SubType* st);
325 void print_full_warning() const;
326 bool chk_recursion(ReferenceChain& refch);
327 // adding a single value/type constraint, union (works only in case of TTCN-3 BNF)
328 bool add_ttcn_type_list_subtype(SubType* p_st);
329 void add_ttcn_value(Value *v);
330 void add_ttcn_recof(Value *v);
331 bool add_ttcn_single(Value *val, size_t restriction_index);
332 // adding a value range constraint, makes a union (works for TTCN-3 only)
333 bool add_ttcn_range(Value *min, bool min_exclusive, Value *max, bool max_exclusive, size_t restriction_index, bool has_other);
334 // adding a length constraint, makes an intersection
335 bool set_ttcn_length(const size_limit_t& min, const size_limit_t& max);
336 void chk_boundary_valid(Value* boundary, Int max_value, const char* boundary_name);
337 bool add_ttcn_length(Ttcn::LengthRestriction *lr, size_t restriction_index);
338 // adding a pattern constraint, add with intersection
339 bool add_ttcn_pattern(Ttcn::PatternString* pattern, size_t restriction_index);
341 void chk_this_template(Ttcn::Template *templ);
342 void chk_this_template_pattern(const char *patt_type,Ttcn::Template *templ);
343 void chk_this_template_length_restriction(Ttcn::Template *templ);
348 #endif // #ifndef _Subtype_HH