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 ///////////////////////////////////////////////////////////////////////////////
11 #include "ttcn3/compiler.h"
19 #include "subtypestuff.hh"
22 class LengthRestriction;
36 * class for parsing type restrictions
40 enum STPselection { STP_SINGLE, STP_RANGE, STP_LENGTH, STP_PATTERN };
43 STPselection selection;
44 /// Restriction info, owned and freed by the object.
46 Value *single; ///< for STP_SINGLE
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
54 /** Copy constructor disabled */
55 SubTypeParse(const SubTypeParse& p);
56 /** Assignment disabled */
57 SubTypeParse& operator=(const SubTypeParse& p);
59 /** Construct a single value restriction */
60 SubTypeParse(Value *p_single);
61 /** Construct a range restriction
63 * @param p_min lower bound
64 * @param p_max upper bound
67 SubTypeParse(Value *p_min, bool p_min_exclusive, Value *p_max, bool p_max_exclusive);
68 /** Construct from a length restriction
70 * @param p_length length restriction, SubTypeParse becomes owner
72 SubTypeParse(Ttcn::LengthRestriction *p_length);
73 /** Construct from a pattern
75 * @param p_pattern pattern, SubTypeParse becomes owner
77 SubTypeParse(Ttcn::PatternString *p_pattern);
81 /// Return the restriction type
82 STPselection get_selection() const { return selection; }
84 /** Return the single value of the restriction.
86 * @pre selection is STP_SINGLE, or else FATAL_ERROR
88 Value *Single() const;
89 /** Return the lower bound of the range.
91 * @pre selection is STP_RANGE, or else FATAL_ERROR
94 bool MinExclusive() const;
95 /** Return the upper bound of the range.
97 * @pre selection is STP_RANGE, or else FATAL_ERROR
100 bool MaxExclusive() const;
101 /** Return the length restriction object.
103 * @ore selection is STP_LENGTH, or else FATAL_ERROR
105 Ttcn::LengthRestriction *Length() const;
106 Ttcn::PatternString *Pattern() const;
110 * Container for all possible subtype constraint classes
112 class SubtypeConstraint
126 ST_UNIVERSAL_CHARSTRING,
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
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;
157 void set_to_error(); // clears the subtype information and sets value to ST_ERROR
159 /// Copy constructor disabled
160 SubtypeConstraint(const SubtypeConstraint&);
161 /// Assignment disabled
162 SubtypeConstraint& operator=(const SubtypeConstraint&);
164 SubtypeConstraint(subtype_t st);
165 virtual ~SubtypeConstraint() { set_to_error(); }
167 /** copy the content of other to this (deletes content of this) */
168 void copy(const SubtypeConstraint* other);
170 /** set operations */
171 void intersection(const SubtypeConstraint* other);
172 void union_(const SubtypeConstraint* other);
173 void except(const SubtypeConstraint* other);
175 tribool is_subset(const SubtypeConstraint* other) const;
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);
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;
205 * class for semantic analysis of subtypes
207 class SubType : public SubtypeConstraint {
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
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
224 map<SubType*,void> my_parents; // used to check for circular references
226 /// Copy constructor disabled
227 SubType(const SubType&);
228 /// Assignment disabled
229 SubType& operator=(const SubType&);
232 SubType(subtype_t st, Type *p_my_owner, SubType* p_parent_subtype,
233 vector<SubTypeParse> *p_parsed, Constraints* p_asn_constraints);
236 SubtypeConstraint* get_root() { return root ? root : this; }
237 bool is_extendable() { return extendable; }
238 SubtypeConstraint* get_extension() { return extension; }
240 string to_string() const;
242 /** Set restrictions.
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; }
251 void chk_this_value(Value *value);
252 void chk_this_template_generic(Ttcn::Template *templ);
255 void generate_code(output_struct &);
257 void dump(unsigned level) const;
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;
264 bool add_parent_subtype(SubType* st);
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);
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);
289 #endif // #ifndef _Subtype_HH