Merge pull request #29 from BotondBaranyi/master
[deliverable/titan.core.git] / compiler2 / subtype.hh
CommitLineData
d44e3c4f 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
7 *
8 * Contributors:
9 * Baji, Laszlo
10 * Balasko, Jeno
11 * Baranyi, Botond
12 * Cserveni, Akos
13 * Delic, Adam
14 * Forstner, Matyas
15 * Kovacs, Ferenc
16 * Raduly, Csaba
17 * Szabados, Kristof
18 * Szabo, Janos Zoltan – initial implementation
19 * Szalai, Gabor
20 * Tatarka, Gabor
21 *
22 ******************************************************************************/
970ed795
EL
23#ifndef _Subtype_HH
24#define _Subtype_HH
25
26#include "ttcn3/compiler.h"
27#include "vector.hh"
28#include "map.hh"
29#include "Int.hh"
30#include "Real.hh"
31#include "ustring.hh"
32#include "Setting.hh"
33
34#include "subtypestuff.hh"
35
3abe9331 36class JSON_Tokenizer;
37
970ed795
EL
38namespace Ttcn {
39 class LengthRestriction;
40 class Template;
41 class ValueRange;
42 class PatternString;
43}
44
45namespace Common {
46
47 class Identifier;
48 class Value;
49 class Type;
50 class Constraints;
51
52/**
53 * class for parsing type restrictions
54 */
55class SubTypeParse {
56public:
57 enum STPselection { STP_SINGLE, STP_RANGE, STP_LENGTH, STP_PATTERN };
58private:
59 /// Type selector
60 STPselection selection;
61 /// Restriction info, owned and freed by the object.
62 union {
63 Value *single; ///< for STP_SINGLE
64 struct {
65 Value *min, *max;
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
70 };
71 /** Copy constructor disabled */
72 SubTypeParse(const SubTypeParse& p);
73 /** Assignment disabled */
74 SubTypeParse& operator=(const SubTypeParse& p);
75public:
76 /** Construct a single value restriction */
77 SubTypeParse(Value *p_single);
78 /** Construct a range restriction
79 *
80 * @param p_min lower bound
81 * @param p_max upper bound
82 *
83 */
84 SubTypeParse(Value *p_min, bool p_min_exclusive, Value *p_max, bool p_max_exclusive);
85 /** Construct from a length restriction
86 *
87 * @param p_length length restriction, SubTypeParse becomes owner
88 */
89 SubTypeParse(Ttcn::LengthRestriction *p_length);
90 /** Construct from a pattern
91 *
92 * @param p_pattern pattern, SubTypeParse becomes owner
93 */
94 SubTypeParse(Ttcn::PatternString *p_pattern);
95 /// Destructor
96 ~SubTypeParse();
97
98 /// Return the restriction type
99 STPselection get_selection() const { return selection; }
100
101 /** Return the single value of the restriction.
102 *
103 * @pre selection is STP_SINGLE, or else FATAL_ERROR
104 */
105 Value *Single() const;
106 /** Return the lower bound of the range.
107 *
108 * @pre selection is STP_RANGE, or else FATAL_ERROR
109 */
110 Value *Min() const;
111 bool MinExclusive() const;
112 /** Return the upper bound of the range.
113 *
114 * @pre selection is STP_RANGE, or else FATAL_ERROR
115 */
116 Value *Max() const;
117 bool MaxExclusive() const;
118 /** Return the length restriction object.
119 *
120 * @ore selection is STP_LENGTH, or else FATAL_ERROR
121 */
122 Ttcn::LengthRestriction *Length() const;
123 Ttcn::PatternString *Pattern() const;
124};
125
126/**
127 * Container for all possible subtype constraint classes
128 */
129class SubtypeConstraint
130{
131public:
132 enum subtype_t {
133 ST_ERROR,
134 ST_INTEGER,
135 ST_FLOAT,
136 ST_BOOLEAN,
137 ST_OBJID,
138 ST_VERDICTTYPE,
139 ST_BITSTRING,
140 ST_HEXSTRING,
141 ST_OCTETSTRING,
142 ST_CHARSTRING,
143 ST_UNIVERSAL_CHARSTRING,
144 ST_RECORD,
145 ST_RECORDOF,
146 ST_SET,
147 ST_SETOF,
148 ST_ENUM,
149 ST_UNION,
150 ST_FUNCTION,
151 ST_ALTSTEP,
152 ST_TESTCASE
153 };
154protected:
155 subtype_t subtype;
156 union {
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
169 };
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;
173
174 void set_to_error(); // clears the subtype information and sets value to ST_ERROR
175
176 /// Copy constructor disabled
177 SubtypeConstraint(const SubtypeConstraint&);
178 /// Assignment disabled
179 SubtypeConstraint& operator=(const SubtypeConstraint&);
180public:
181 SubtypeConstraint(subtype_t st);
182 virtual ~SubtypeConstraint() { set_to_error(); }
183
184 /** copy the content of other to this (deletes content of this) */
185 void copy(const SubtypeConstraint* other);
186
187 /** set operations */
188 void intersection(const SubtypeConstraint* other);
189 void union_(const SubtypeConstraint* other);
190 void except(const SubtypeConstraint* other);
191
192 tribool is_subset(const SubtypeConstraint* other) const;
193
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);
210
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;
af710487 219 bool is_upper_limit_infinity() const;
220 bool is_lower_limit_infinity() const;
970ed795
EL
221};
222
223/**
224 * class for semantic analysis of subtypes
225 */
226class SubType : public SubtypeConstraint {
227
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
232
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
236
237 enum checked_t {
238 STC_NO,
239 STC_CHECKING,
240 STC_YES
241 } checked;
242
243 map<SubType*,void> my_parents; // used to check for circular references
244
245 /// Copy constructor disabled
246 SubType(const SubType&);
247 /// Assignment disabled
248 SubType& operator=(const SubType&);
249public:
250
251 SubType(subtype_t st, Type *p_my_owner, SubType* p_parent_subtype,
252 vector<SubTypeParse> *p_parsed, Constraints* p_asn_constraints);
253 ~SubType();
254
255 SubtypeConstraint* get_root() { return root ? root : this; }
256 bool is_extendable() { return extendable; }
257 SubtypeConstraint* get_extension() { return extension; }
258
259 string to_string() const;
260
261 /** Set restrictions.
262 *
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; }
267
268 void chk();
269
270 void chk_this_value(Value *value);
271 void chk_this_template_generic(Ttcn::Template *templ);
272
273 /// No-op.
274 void generate_code(output_struct &);
3abe9331 275
276 /** Returns true if there are JSON schema elements to be generated for this subtype */
277 boolean has_json_schema() const { return parsed != NULL; }
278
279 /** Generates the JSON schema segment for the type restrictions.
280 *
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);
284
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).
287 *
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. */
3f84031e 290 void generate_json_schema_value_list(JSON_Tokenizer& json, bool allow_special_float,
291 bool union_value_list);
3abe9331 292
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).
297 *
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);
301
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
305 * range restriction.
306 *
307 * Recursive (it also inserts the string ranges of referenced subtypes). */
308 char* generate_json_schema_string_ranges(char* pattern_str);
309
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
312 * restrictions).
313 * This replaces the schema segment generated by Type::generate_json_schema().*/
314 void generate_json_schema_float(JSON_Tokenizer& json);
970ed795
EL
315
316 void dump(unsigned level) const;
317
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;
322
323 bool add_parent_subtype(SubType* st);
324private:
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);
340
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);
344};// class SubType
345
346}// namespace Common
347
348#endif // #ifndef _Subtype_HH
This page took 0.036746 seconds and 5 git commands to generate.