a9618aa057fcbfdd166ac10b17d9f60eaf1cbda4
[deliverable/titan.core.git] / compiler2 / subtype.hh
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;
202 };
203
204 /**
205 * class for semantic analysis of subtypes
206 */
207 class SubType : public SubtypeConstraint {
208
209 Type *my_owner; ///< Pointer to the type, object not owned
210 SubType* parent_subtype; ///< pointer to the inherited subtype, not owned
211 vector<SubTypeParse> *parsed; ///< TTCN-3 parsed subtype data, not owned
212 Constraints* asn_constraints; ///< constraints of ASN.1 type or NULL, not owned
213
214 SubtypeConstraint* root; ///< root part of the ASN.1 subtype, owned by asn_constraints
215 bool extendable; ///< ASN.1 extendable type
216 SubtypeConstraint* extension; ///< ASN.1 extension addition, owned by asn_constraints
217
218 enum checked_t {
219 STC_NO,
220 STC_CHECKING,
221 STC_YES
222 } checked;
223
224 map<SubType*,void> my_parents; // used to check for circular references
225
226 /// Copy constructor disabled
227 SubType(const SubType&);
228 /// Assignment disabled
229 SubType& operator=(const SubType&);
230 public:
231
232 SubType(subtype_t st, Type *p_my_owner, SubType* p_parent_subtype,
233 vector<SubTypeParse> *p_parsed, Constraints* p_asn_constraints);
234 ~SubType();
235
236 SubtypeConstraint* get_root() { return root ? root : this; }
237 bool is_extendable() { return extendable; }
238 SubtypeConstraint* get_extension() { return extension; }
239
240 string to_string() const;
241
242 /** Set restrictions.
243 *
244 * @param r list of restrictions
245 * @pre set_restrictions() has not been called before. */
246 void set_restrictions(vector<SubTypeParse> *r);
247 const Type *get_owner() const { return my_owner; }
248
249 void chk();
250
251 void chk_this_value(Value *value);
252 void chk_this_template_generic(Ttcn::Template *templ);
253
254 /// No-op.
255 void generate_code(output_struct &);
256
257 void dump(unsigned level) const;
258
259 /// return single length restriction or -1
260 Int get_length_restriction() const;
261 /// true unless a length restriction prohibits zero elements
262 bool zero_length_allowed() const;
263
264 bool add_parent_subtype(SubType* st);
265 private:
266 void print_full_warning() const;
267 bool chk_recursion(ReferenceChain& refch);
268 // adding a single value/type constraint, union (works only in case of TTCN-3 BNF)
269 bool add_ttcn_type_list_subtype(SubType* p_st);
270 void add_ttcn_value(Value *v);
271 void add_ttcn_recof(Value *v);
272 bool add_ttcn_single(Value *val, size_t restriction_index);
273 // adding a value range constraint, makes a union (works for TTCN-3 only)
274 bool add_ttcn_range(Value *min, bool min_exclusive, Value *max, bool max_exclusive, size_t restriction_index, bool has_other);
275 // adding a length constraint, makes an intersection
276 bool set_ttcn_length(const size_limit_t& min, const size_limit_t& max);
277 void chk_boundary_valid(Value* boundary, Int max_value, const char* boundary_name);
278 bool add_ttcn_length(Ttcn::LengthRestriction *lr, size_t restriction_index);
279 // adding a pattern constraint, add with intersection
280 bool add_ttcn_pattern(Ttcn::PatternString* pattern, size_t restriction_index);
281
282 void chk_this_template(Ttcn::Template *templ);
283 void chk_this_template_pattern(const char *patt_type,Ttcn::Template *templ);
284 void chk_this_template_length_restriction(Ttcn::Template *templ);
285 };// class SubType
286
287 }// namespace Common
288
289 #endif // #ifndef _Subtype_HH
This page took 0.059916 seconds and 4 git commands to generate.