Sync with 5.4.0
[deliverable/titan.core.git] / compiler2 / subtype.cc
CommitLineData
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#include "subtype.hh"
9#include "../common/dbgnew.hh"
10#include "Identifier.hh"
11#include "Value.hh"
12#include "Setting.hh"
13#include "Type.hh"
14#include "CompilerError.hh"
15#include "Valuestuff.hh"
16#include "ttcn3/TtcnTemplate.hh"
17#include "ttcn3/Templatestuff.hh"
18#include "ttcn3/PatternString.hh"
19#include "Constraint.hh"
3abe9331 20#include "../common/JSON_Tokenizer.hh"
970ed795
EL
21
22#include <limits.h>
23
24namespace Common {
25
26/**************************
27class SubTypeParse
28**************************/
29
30SubTypeParse::SubTypeParse(Value *p_single)
31: selection(STP_SINGLE)
32{
33 if (!p_single) FATAL_ERROR("SubTypeParse::SubTypeParse()");
34 single = p_single;
35}
36
37SubTypeParse::SubTypeParse(Value *p_min, bool p_min_exclusive, Value *p_max, bool p_max_exclusive)
38: selection(STP_RANGE)
39{
40 range.min = p_min;
41 range.min_exclusive = p_min_exclusive;
42 range.max = p_max;
43 range.max_exclusive = p_max_exclusive;
44}
45
46SubTypeParse::SubTypeParse(Ttcn::LengthRestriction *p_length)
47: selection(STP_LENGTH)
48{
49 if (!p_length) FATAL_ERROR("SubTypeParse::SubTypeParse()");
50 length = p_length;
51}
52
53SubTypeParse::SubTypeParse(Ttcn::PatternString *p_pattern)
54: selection(STP_PATTERN)
55{
56 if (!p_pattern) FATAL_ERROR("SubTypeParse::SubTypeParse()");
57 pattern = p_pattern;
58}
59
60SubTypeParse::~SubTypeParse()
61{
62 switch (selection) {
63 case STP_SINGLE:
64 delete single;
65 break;
66 case STP_RANGE:
67 delete range.min;
68 delete range.max;
69 break;
70 case STP_LENGTH:
71 delete length;
72 break;
73 case STP_PATTERN:
74 delete pattern;
75 break;
76 default:
77 FATAL_ERROR("SubTypeParse::~SubTypeParse()");
78 }
79}
80
81Value *SubTypeParse::Single() const
82{
83 if (selection != STP_SINGLE) FATAL_ERROR("SubTypeParse::Single()");
84 return single;
85}
86
87Value *SubTypeParse::Min() const
88{
89 if (selection != STP_RANGE) FATAL_ERROR("SubTypeParse::Min()");
90 return range.min;
91}
92
93bool SubTypeParse::MinExclusive() const
94{
95 if (selection != STP_RANGE) FATAL_ERROR("SubTypeParse::MinExclusive()");
96 return range.min_exclusive;
97}
98
99Value *SubTypeParse::Max() const
100{
101 if (selection != STP_RANGE) FATAL_ERROR("SubTypeParse::Max()");
102 return range.max;
103}
104
105bool SubTypeParse::MaxExclusive() const
106{
107 if (selection != STP_RANGE) FATAL_ERROR("SubTypeParse::MaxExclusive()");
108 return range.max_exclusive;
109}
110
111Ttcn::LengthRestriction *SubTypeParse::Length() const
112{
113 if (selection != STP_LENGTH) FATAL_ERROR("SubTypeParse::Length()");
114 return length;
115}
116
117Ttcn::PatternString *SubTypeParse::Pattern() const
118{
119 if (selection != STP_PATTERN) FATAL_ERROR("SubTypeParse::Pattern()");
120 return pattern;
121}
122
123/********************
124class SubtypeConstraint
125********************/
126
127SubtypeConstraint::SubtypeConstraint(subtype_t st)
128{
129 subtype = st;
130 length_restriction = NULL;
131 switch (subtype) {
132 case ST_INTEGER:
133 integer_st = NULL;
134 break;
135 case ST_FLOAT:
136 float_st = NULL;
137 break;
138 case ST_BOOLEAN:
139 boolean_st = NULL;
140 break;
141 case ST_VERDICTTYPE:
142 verdict_st = NULL;
143 break;
144 case ST_BITSTRING:
145 bitstring_st = NULL;
146 break;
147 case ST_HEXSTRING:
148 hexstring_st = NULL;
149 break;
150 case ST_OCTETSTRING:
151 octetstring_st = NULL;
152 break;
153 case ST_CHARSTRING:
154 charstring_st = NULL;
155 break;
156 case ST_UNIVERSAL_CHARSTRING:
157 universal_charstring_st = NULL;
158 break;
159 case ST_OBJID:
160 case ST_RECORD:
161 case ST_SET:
162 case ST_ENUM:
163 case ST_UNION:
164 case ST_FUNCTION:
165 case ST_ALTSTEP:
166 case ST_TESTCASE:
167 value_st = NULL;
168 break;
169 case ST_RECORDOF:
170 case ST_SETOF:
171 recof_st = NULL;
172 break;
173 default:
174 FATAL_ERROR("SubtypeConstraint::SubtypeConstraint()");
175 }
176}
177
178void SubtypeConstraint::copy(const SubtypeConstraint* other)
179{
180 if ((other==NULL) || (other->subtype!=subtype)) FATAL_ERROR("SubtypeConstraint::copy()");
181 switch (subtype) {
182 case ST_INTEGER:
183 delete integer_st;
184 integer_st = other->integer_st ? new IntegerRangeListConstraint(*(other->integer_st)) : NULL;
185 break;
186 case ST_FLOAT:
187 delete float_st;
188 float_st = other->float_st ? new RealRangeListConstraint(*(other->float_st)) : NULL;
189 break;
190 case ST_BOOLEAN:
191 delete boolean_st;
192 boolean_st = other->boolean_st ? new BooleanListConstraint(*(other->boolean_st)) : NULL;
193 break;
194 case ST_VERDICTTYPE:
195 delete verdict_st;
196 verdict_st = other->verdict_st ? new VerdicttypeListConstraint(*(other->verdict_st)) : NULL;
197 break;
198 case ST_BITSTRING:
199 delete bitstring_st;
200 bitstring_st = other->bitstring_st ? new BitstringConstraint(*(other->bitstring_st)) : NULL;
201 break;
202 case ST_HEXSTRING:
203 delete hexstring_st;
204 hexstring_st = other->hexstring_st ? new HexstringConstraint(*(other->hexstring_st)) : NULL;
205 break;
206 case ST_OCTETSTRING:
207 delete octetstring_st;
208 octetstring_st = other->octetstring_st ? new OctetstringConstraint(*(other->octetstring_st)) : NULL;
209 break;
210 case ST_CHARSTRING:
211 delete charstring_st;
212 charstring_st = other->charstring_st ? new CharstringSubtypeTreeElement(*(other->charstring_st)) : NULL;
213 break;
214 case ST_UNIVERSAL_CHARSTRING:
215 delete universal_charstring_st;
216 universal_charstring_st = other->universal_charstring_st ? new UniversalCharstringSubtypeTreeElement(*(other->universal_charstring_st)) : NULL;
217 break;
218 case ST_OBJID:
219 case ST_RECORD:
220 case ST_SET:
221 case ST_ENUM:
222 case ST_UNION:
223 case ST_FUNCTION:
224 case ST_ALTSTEP:
225 case ST_TESTCASE:
226 delete value_st;
227 value_st = other->value_st ? new ValueListConstraint(*(other->value_st)) : NULL;
228 break;
229 case ST_RECORDOF:
230 case ST_SETOF:
231 delete recof_st;
232 recof_st = other->recof_st ? new RecofConstraint(*(other->recof_st)) : NULL;
233 break;
234 default:
235 FATAL_ERROR("SubtypeConstraint::copy()");
236 }
237 delete length_restriction;
238 length_restriction = other->length_restriction ? new SizeRangeListConstraint(*(other->length_restriction)) : NULL;
239}
240
241// used by get_asn_type_constraint() to store singleton objects and delete them on program exit
242struct AsnTypeConstraintSingleton
243{
244 SubtypeConstraint *printablestring_stc, *numericstring_stc, *bmpstring_stc;
245 AsnTypeConstraintSingleton():
246 printablestring_stc(NULL), numericstring_stc(NULL), bmpstring_stc(NULL) {}
247 ~AsnTypeConstraintSingleton();
248};
249
250AsnTypeConstraintSingleton::~AsnTypeConstraintSingleton()
251{
252 delete printablestring_stc;
253 delete numericstring_stc;
254 delete bmpstring_stc;
255}
256
257SubtypeConstraint* SubtypeConstraint::get_asn_type_constraint(Type* type)
258{
259 static AsnTypeConstraintSingleton asn_tcs;
260 static const char_limit_t zero('0'), nine('9'),
261 bigA('A'), bigZ('Z'), smalla('a'), smallz('z');
262 static const universal_char_limit_t uni_zero(0);
263 static const universal_char_limit_t uni_ffff((1<<16)-1);
264 static const CharRangeListConstraint numeric_string_char_range(
265 CharRangeListConstraint(zero, nine) +
266 CharRangeListConstraint(char_limit_t(' ')));
267 static const CharRangeListConstraint printable_string_char_range(
268 CharRangeListConstraint(bigA, bigZ) +
269 CharRangeListConstraint(smalla, smallz) +
270 CharRangeListConstraint(zero , nine) +
271 CharRangeListConstraint(char_limit_t(' ')) +
272 CharRangeListConstraint(char_limit_t('\'')) +
273 CharRangeListConstraint(char_limit_t('(')) +
274 CharRangeListConstraint(char_limit_t(')')) +
275 CharRangeListConstraint(char_limit_t('+')) +
276 CharRangeListConstraint(char_limit_t(',')) +
277 CharRangeListConstraint(char_limit_t('-')) +
278 CharRangeListConstraint(char_limit_t('.')) +
279 CharRangeListConstraint(char_limit_t('/')) +
280 CharRangeListConstraint(char_limit_t(':')) +
281 CharRangeListConstraint(char_limit_t('=')) +
282 CharRangeListConstraint(char_limit_t('?')));
283 static const UniversalCharRangeListConstraint bmp_string_char_range(
284 UniversalCharRangeListConstraint(uni_zero, uni_ffff));
285
286 switch (type->get_typetype()) {
287 case Type::T_TELETEXSTRING:
288 // TODO: based on ITU-T Recommendation T.61
289 return NULL;
290 case Type::T_VIDEOTEXSTRING:
291 // TODO: based on ITU-T Recommendation T.100 and T.101
292 return NULL;
293 case Type::T_NUMERICSTRING:
294 if (asn_tcs.numericstring_stc==NULL) {
295 asn_tcs.numericstring_stc = new SubtypeConstraint(ST_CHARSTRING);
296 asn_tcs.numericstring_stc->charstring_st = new CharstringSubtypeTreeElement(numeric_string_char_range, false);
297 }
298 return asn_tcs.numericstring_stc;
299 case Type::T_PRINTABLESTRING:
300 if (asn_tcs.printablestring_stc==NULL) {
301 asn_tcs.printablestring_stc = new SubtypeConstraint(ST_CHARSTRING);
302 asn_tcs.printablestring_stc->charstring_st = new CharstringSubtypeTreeElement(printable_string_char_range, false);
303 }
304 return asn_tcs.printablestring_stc;
305 case Type::T_BMPSTRING:
306 if (asn_tcs.bmpstring_stc==NULL) {
307 asn_tcs.bmpstring_stc = new SubtypeConstraint(ST_UNIVERSAL_CHARSTRING);
308 asn_tcs.bmpstring_stc->universal_charstring_st = new UniversalCharstringSubtypeTreeElement(bmp_string_char_range, false);
309 }
310 return asn_tcs.bmpstring_stc;
311 default:
312 return NULL;
313 }
314}
315
316SubtypeConstraint* SubtypeConstraint::create_from_asn_value(Type* type, Value* value)
317{
318 Value* v = value->get_value_refd_last();
319 subtype_t st_t = type->get_subtype_type();
320 if ( (st_t==ST_ERROR) || (v->get_valuetype()==Value::V_ERROR) ) return NULL;
321 SubtypeConstraint* stc = new SubtypeConstraint(st_t);
322 switch (v->get_valuetype()) {
323 case Value::V_INT:
324 if (st_t!=ST_INTEGER) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
325 stc->integer_st = new IntegerRangeListConstraint(int_limit_t(*(v->get_val_Int())));
326 break;
327 case Value::V_REAL: {
328 if (st_t!=ST_FLOAT) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
329 ttcn3float r = v->get_val_Real();
330 if (r!=r) stc->float_st = new RealRangeListConstraint(true);
331 else stc->float_st = new RealRangeListConstraint(real_limit_t(r));
332 } break;
333 case Value::V_BOOL:
334 if (st_t!=ST_BOOLEAN) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
335 stc->boolean_st = new BooleanListConstraint(v->get_val_bool());
336 break;
337 case Value::V_OID:
338 case Value::V_ROID:
339 if (v->has_oid_error()) goto invalid_value;
340 if (st_t!=ST_OBJID) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
341 stc->value_st = new ValueListConstraint(v);
342 break;
343 case Value::V_BSTR:
344 if (st_t!=ST_BITSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
345 stc->bitstring_st = new BitstringConstraint(v->get_val_str());
346 break;
347 case Value::V_HSTR:
348 if (st_t!=ST_HEXSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
349 stc->hexstring_st = new HexstringConstraint(v->get_val_str());
350 break;
351 case Value::V_OSTR:
352 if (st_t!=ST_OCTETSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
353 stc->octetstring_st = new OctetstringConstraint(v->get_val_str());
354 break;
355 case Value::V_CSTR:
356 if (st_t!=ST_CHARSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
357 stc->charstring_st = new CharstringSubtypeTreeElement(StringValueConstraint<string>(v->get_val_str()));
358 break;
359 case Value::V_ISO2022STR:
360 if (st_t!=ST_CHARSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
361 stc->charstring_st = new CharstringSubtypeTreeElement(StringValueConstraint<string>(v->get_val_iso2022str()));
362 break;
363 case Value::V_CHARSYMS:
364 case Value::V_USTR:
365 if (st_t!=ST_UNIVERSAL_CHARSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
366 stc->universal_charstring_st = new UniversalCharstringSubtypeTreeElement(StringValueConstraint<ustring>(v->get_val_ustr()));
367 break;
368 case Value::V_ENUM:
369 case Value::V_NULL: // FIXME: should go to ST_NULL
370 if (st_t!=ST_ENUM) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
371 stc->value_st = new ValueListConstraint(v);
372 break;
373 case Value::V_CHOICE:
374 case Value::V_OPENTYPE: // FIXME?
375 if (st_t!=ST_UNION) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
376 stc->value_st = new ValueListConstraint(v);
377 break;
378 case Value::V_SEQ:
379 if (st_t!=ST_RECORD) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
380 stc->value_st = new ValueListConstraint(v);
381 break;
382 case Value::V_SET:
383 if (st_t!=ST_SET) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
384 stc->value_st = new ValueListConstraint(v);
385 break;
386 case Value::V_SEQOF:
387 if (st_t!=ST_RECORDOF) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
388 stc->recof_st = new RecofConstraint(v);
389 break;
390 case Value::V_SETOF:
391 if (st_t!=ST_SETOF) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
392 stc->recof_st = new RecofConstraint(v);
393 break;
394 default:
395 goto invalid_value;
396 }
397 return stc;
398invalid_value:
399 delete stc;
400 return NULL;
401}
402
403SubtypeConstraint* SubtypeConstraint::create_from_asn_charvalues(Type* type, Value* value)
404{
405 Value* v = value->get_value_refd_last();
406 subtype_t st_t = type->get_subtype_type();
407 if ( (st_t==ST_ERROR) || (v->get_valuetype()==Value::V_ERROR) ) return NULL;
408 SubtypeConstraint* stc = new SubtypeConstraint(st_t);
409 switch (v->get_valuetype()) {
410 case Value::V_CSTR:
411 case Value::V_ISO2022STR: {
412 if (st_t!=ST_CHARSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_charvalues()");
413 CharRangeListConstraint charvalues;
414 string val_str = (v->get_valuetype()==Value::V_CSTR) ? v->get_val_str() : v->get_val_iso2022str();
415 for (size_t i=0; i<val_str.size(); i++) {
416 if (!char_limit_t::is_valid_value(val_str[i])) {
417 value->error("Invalid char in string %s at index %lu",
418 value->get_stringRepr().c_str(), (unsigned long)i);
419 goto invalid_value;
420 }
421 charvalues = charvalues + CharRangeListConstraint(val_str[i]);
422 }
423 stc->charstring_st = new CharstringSubtypeTreeElement(charvalues, true);
424 } break;
425 case Value::V_CHARSYMS: {
426 case Value::V_USTR:
427 if (st_t!=ST_UNIVERSAL_CHARSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_charvalues()");
428 UniversalCharRangeListConstraint ucharvalues;
429 ustring val_ustr = v->get_val_ustr();
430 for (size_t i=0; i<val_ustr.size(); i++) {
431 if (!universal_char_limit_t::is_valid_value(val_ustr[i])) {
432 value->error("Invalid universal char in string %s at index %lu",
433 value->get_stringRepr().c_str(), (unsigned long)i);
434 goto invalid_value;
435 }
436 ucharvalues = ucharvalues + UniversalCharRangeListConstraint(val_ustr[i]);
437 }
438 stc->universal_charstring_st = new UniversalCharstringSubtypeTreeElement(ucharvalues, true);
439 } break;
440 default:
441 // error was already reported
442 goto invalid_value;
443 }
444 return stc;
445invalid_value:
446 delete stc;
447 return NULL;
448}
449
450int_limit_t SubtypeConstraint::get_int_limit(bool is_upper, Location* loc)
451{
452 int_limit_t default_limit = is_upper ?
453 int_limit_t::maximum :
454 ((subtype==ST_INTEGER) ? int_limit_t::minimum : int_limit_t(int_val_t((Int)0)));
455 switch (subtype) {
456 case ST_INTEGER:
457 if (integer_st) {
458 if (integer_st->is_empty()==TTRUE) {
459 loc->error("Cannot determine the value of %s: the parent subtype is an empty set.",
460 is_upper?"MAX":"MIN");
461 return default_limit;
462 } else {
463 return is_upper ? integer_st->get_maximal() : integer_st->get_minimal();
464 }
465 }
466 return default_limit;
467 case ST_BITSTRING:
468 if (bitstring_st) {
469 size_limit_t sl;
470 tribool tb = bitstring_st->get_size_limit(is_upper, sl);
471 if (tb==TTRUE) return sl.to_int_limit();
472 break;
473 }
474 return default_limit;
475 case ST_HEXSTRING:
476 if (hexstring_st) {
477 size_limit_t sl;
478 tribool tb = hexstring_st->get_size_limit(is_upper, sl);
479 if (tb==TTRUE) return sl.to_int_limit();
480 break;
481 }
482 return default_limit;
483 case ST_OCTETSTRING:
484 if (octetstring_st) {
485 size_limit_t sl;
486 tribool tb = octetstring_st->get_size_limit(is_upper, sl);
487 if (tb==TTRUE) return sl.to_int_limit();
488 break;
489 }
490 return default_limit;
491 case ST_CHARSTRING:
492 if (charstring_st) {
493 size_limit_t sl;
494 tribool tb = charstring_st->get_size_limit(is_upper, sl);
495 switch (tb) {
496 case TFALSE:
497 loc->error("Cannot determine the value of %s: the parent subtype does "
498 "not define a %simal size value", is_upper?"MAX":"MIN", is_upper?"max":"min");
499 break;
500 case TUNKNOWN:
501 loc->warning("Cannot determine the value of %s from parent subtype %s",
502 is_upper?"MAX":"MIN", to_string().c_str());
503 break;
504 case TTRUE:
505 return sl.to_int_limit();
506 default:
507 FATAL_ERROR("SubtypeConstraint::get_int_limit()");
508 }
509 }
510 return default_limit;
511 case ST_UNIVERSAL_CHARSTRING:
512 if (universal_charstring_st) {
513 size_limit_t sl;
514 tribool tb = universal_charstring_st->get_size_limit(is_upper, sl);
515 switch (tb) {
516 case TFALSE:
517 loc->error("Cannot determine the value of %s: the parent subtype does "
518 "not define a %simal size value", is_upper?"MAX":"MIN", is_upper?"max":"min");
519 break;
520 case TUNKNOWN:
521 loc->warning("Cannot determine the value of %s from parent subtype %s",
522 is_upper?"MAX":"MIN", to_string().c_str());
523 break;
524 case TTRUE:
525 return sl.to_int_limit();
526 default:
527 FATAL_ERROR("SubtypeConstraint::get_int_limit()");
528 }
529 }
530 return default_limit;
531 case ST_RECORDOF:
532 case ST_SETOF:
533 if (recof_st) {
534 size_limit_t sl;
535 tribool tb = recof_st->get_size_limit(is_upper, sl);
536 if (tb==TTRUE) return sl.to_int_limit();
537 break;
538 }
539 return default_limit;
540 default:
541 FATAL_ERROR("SubtypeConstraint::get_int_limit()");
542 }
543 loc->error("Cannot determine the value of %s from parent subtype %s",
544 is_upper?"MAX":"MIN", to_string().c_str());
545 return default_limit;
546}
547
548SubtypeConstraint* SubtypeConstraint::create_from_asn_range(
549 Value* vmin, bool min_exclusive, Value* vmax, bool max_exclusive,
550 Location* loc, subtype_t st_t, SubtypeConstraint* parent_subtype)
551{
552 switch (st_t) {
553 case SubtypeConstraint::ST_INTEGER: {
554 if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_INT)) ||
555 ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_INT))) return NULL;
556
557 int_limit_t min_limit;
558 if (vmin) {
559 min_limit = int_limit_t(*(vmin->get_val_Int()));
560 } else { // MIN was used
561 if (parent_subtype) {
562 min_limit = parent_subtype->get_int_limit(false, loc);
563 } else {
564 min_limit = int_limit_t::minimum;
565 }
566 }
567
568 if (min_exclusive) {
569 if (min_limit==int_limit_t::minimum) {
570 loc->error("invalid lower boundary, -infinity cannot be excluded from an INTEGER value range constraint");
571 return NULL;
572 } else {
573 min_limit = min_limit.next();
574 }
575 }
576
577 int_limit_t max_limit;
578 if (vmax) {
579 max_limit = int_limit_t(*(vmax->get_val_Int()));
580 } else { // MAX was used
581 if (parent_subtype) {
582 max_limit = parent_subtype->get_int_limit(true, loc);
583 } else {
584 max_limit = int_limit_t::maximum;
585 }
586 }
587
588 if (max_exclusive) {
589 if (max_limit==int_limit_t::maximum) {
590 loc->error("invalid upper boundary, infinity cannot be excluded from an INTEGER value range constraint");
591 return NULL;
592 } else {
593 max_limit = max_limit.previous();
594 }
595 }
596 if (max_limit<min_limit) {
597 loc->error("lower boundary is bigger than upper boundary in INTEGER value range constraint");
598 return NULL;
599 }
600 SubtypeConstraint* stc = new SubtypeConstraint(st_t);
601 stc->integer_st = new IntegerRangeListConstraint(min_limit, max_limit);
602 return stc;
603 } break;
604 case ST_FLOAT: {
605 if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_REAL)) ||
606 ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_REAL))) return NULL;
607 if ((vmin!=NULL) && (vmin->get_val_Real()!=vmin->get_val_Real())) {
608 loc->error("lower boundary cannot be NOT-A-NUMBER in REAL value range constraint");
609 return NULL;
610 }
611 if ((vmax!=NULL) && (vmax->get_val_Real()!=vmax->get_val_Real())) {
612 loc->error("upper boundary cannot be NOT-A-NUMBER in REAL value range constraint");
613 return NULL;
614 }
615
616 if (parent_subtype && (parent_subtype->subtype!=ST_FLOAT)) FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
617 real_limit_t min_limit;
618 if (vmin) {
619 min_limit = real_limit_t(vmin->get_val_Real());
620 } else { // MIN was used
621 if (parent_subtype && parent_subtype->float_st) {
622 if (parent_subtype->float_st->is_range_empty()==TTRUE) {
623 loc->error("Cannot determine the value of MIN: the parent subtype has no range");
624 min_limit = real_limit_t::minimum;
625 } else {
626 min_limit = parent_subtype->float_st->get_minimal();
627 }
628 } else {
629 min_limit = real_limit_t::minimum;
630 }
631 }
632
633 if (min_exclusive) {
634 min_limit = min_limit.next();
635 }
636
637 real_limit_t max_limit;
638 if (vmax) {
639 max_limit = real_limit_t(vmax->get_val_Real());
640 } else { // MAX was used
641 if (parent_subtype && parent_subtype->float_st) {
642 if (parent_subtype->float_st->is_range_empty()==TTRUE) {
643 loc->error("Cannot determine the value of MAX: the parent subtype has no range");
644 max_limit = real_limit_t::maximum;
645 } else {
646 max_limit = parent_subtype->float_st->get_maximal();
647 }
648 } else {
649 max_limit = real_limit_t::maximum;
650 }
651 }
652
653 if (max_exclusive) {
654 max_limit = max_limit.previous();
655 }
656 if (max_limit<min_limit) {
657 loc->error("lower boundary is bigger than upper boundary in REAL value range constraint");
658 return NULL;
659 }
660 SubtypeConstraint* stc = new SubtypeConstraint(st_t);
661 stc->float_st = new RealRangeListConstraint(min_limit, max_limit);
662 return stc;
663 } break;
664 case ST_CHARSTRING: {
665 if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_CSTR)) ||
666 ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_CSTR))) return NULL;
667 if (vmin && (vmin->get_val_str().size()!=1)) {
668 vmin->error("lower boundary of string value range constraint must be a single element string");
669 return NULL;
670 }
671 if (vmax && (vmax->get_val_str().size()!=1)) {
672 vmax->error("upper boundary of string value range constraint must be a single element string");
673 return NULL;
674 }
675 if (vmin && !char_limit_t::is_valid_value(*vmin->get_val_str().c_str())) {
676 vmin->error("lower boundary of string value range constraint is an invalid char");
677 return NULL;
678 }
679 if (vmax && !char_limit_t::is_valid_value(*vmax->get_val_str().c_str())) {
680 vmax->error("upper boundary of string value range constraint is an invalid char");
681 return NULL;
682 }
683
684 if (parent_subtype && (parent_subtype->subtype!=ST_CHARSTRING)) FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
685
686 char_limit_t min_limit;
687 if (vmin) {
688 min_limit = char_limit_t(*vmin->get_val_str().c_str());
689 } else { // MIN was used
690 if (parent_subtype && parent_subtype->charstring_st) {
691 tribool tb = parent_subtype->charstring_st->get_alphabet_limit(false, min_limit);
692 switch (tb) {
693 case TFALSE:
694 loc->error("Cannot determine the value of MIN: the parent subtype does not define a minimal char value");
695 min_limit = char_limit_t::minimum;
696 break;
697 case TUNKNOWN:
698 loc->warning("Cannot determine the value of MIN, using the minimal char value of the type");
699 min_limit = char_limit_t::minimum;
700 break;
701 case TTRUE:
702 // min_limit was set to the correct value
703 break;
704 default:
705 FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
706 }
707 } else {
708 min_limit = char_limit_t::minimum;
709 }
710 }
711
712 if (min_exclusive) {
713 if (min_limit==char_limit_t::maximum) {
714 loc->error("exclusive lower boundary is not a legal character");
715 return NULL;
716 }
717 min_limit = min_limit.next();
718 }
719
720 char_limit_t max_limit;
721 if (vmax) {
722 max_limit = char_limit_t(*vmax->get_val_str().c_str());
723 } else { // MAX was used
724 if (parent_subtype && parent_subtype->charstring_st) {
725 tribool tb = parent_subtype->charstring_st->get_alphabet_limit(true, max_limit);
726 switch (tb) {
727 case TFALSE:
728 loc->error("Cannot determine the value of MAX: the parent subtype does not define a maximal char value");
729 max_limit = char_limit_t::maximum;
730 break;
731 case TUNKNOWN:
732 loc->warning("Cannot determine the value of MAX, using the maximal char value of the type");
733 max_limit = char_limit_t::maximum;
734 break;
735 case TTRUE:
736 // max_limit was set to the correct value
737 break;
738 default:
739 FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
740 }
741 } else {
742 max_limit = char_limit_t::maximum;
743 }
744 }
745
746 if (max_exclusive) {
747 if (max_limit==char_limit_t::minimum) {
748 loc->error("exclusive upper boundary is not a legal character");
749 return NULL;
750 }
751 max_limit = max_limit.previous();
752 }
753 if (max_limit<min_limit) {
754 loc->error("lower boundary is bigger than upper boundary in string value range constraint");
755 return NULL;
756 }
757 SubtypeConstraint* stc = new SubtypeConstraint(st_t);
758 stc->charstring_st = new CharstringSubtypeTreeElement(CharRangeListConstraint(min_limit,max_limit), true);
759 return stc;
760 } break;
761 case ST_UNIVERSAL_CHARSTRING: {
762 if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_USTR)) ||
763 ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_USTR))) return NULL;
764 if (vmin && (vmin->get_val_ustr().size()!=1)) {
765 vmin->error("lower boundary of string value range constraint must be a single element string");
766 return NULL;
767 }
768 if (vmax && (vmax->get_val_ustr().size()!=1)) {
769 vmax->error("upper boundary of string value range constraint must be a single element string");
770 return NULL;
771 }
772 if (vmin && !universal_char_limit_t::is_valid_value(*vmin->get_val_ustr().u_str())) {
773 vmin->error("lower boundary of string value range constraint is an invalid universal char");
774 return NULL;
775 }
776 if (vmax && !universal_char_limit_t::is_valid_value(*vmax->get_val_ustr().u_str())) {
777 vmax->error("upper boundary of string value range constraint is an invalid universal char");
778 return NULL;
779 }
780
781 if (parent_subtype && (parent_subtype->subtype!=ST_UNIVERSAL_CHARSTRING)) FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
782 universal_char_limit_t min_limit;
783 if (vmin) {
784 min_limit = universal_char_limit_t(*vmin->get_val_ustr().u_str());
785 } else { // MIN was used
786 if (parent_subtype && parent_subtype->universal_charstring_st) {
787 tribool tb = parent_subtype->universal_charstring_st->get_alphabet_limit(false, min_limit);
788 switch (tb) {
789 case TFALSE:
790 loc->error("Cannot determine the value of MIN: the parent subtype does not define a minimal char value");
791 min_limit = universal_char_limit_t::minimum;
792 break;
793 case TUNKNOWN:
794 loc->warning("Cannot determine the value of MIN, using the minimal char value of the type");
795 min_limit = universal_char_limit_t::minimum;
796 break;
797 case TTRUE:
798 // min_limit was set to the correct value
799 break;
800 default:
801 FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
802 }
803 } else {
804 min_limit = universal_char_limit_t::minimum;
805 }
806 }
807
808 if (min_exclusive) {
809 if (min_limit==universal_char_limit_t::maximum) {
810 loc->error("exclusive lower boundary is not a legal character");
811 return NULL;
812 }
813 min_limit = min_limit.next();
814 }
815
816 universal_char_limit_t max_limit;
817 if (vmax) {
818 max_limit = universal_char_limit_t(*vmax->get_val_ustr().u_str());
819 } else { // MAX was used
820 if (parent_subtype && parent_subtype->universal_charstring_st) {
821 tribool tb = parent_subtype->universal_charstring_st->get_alphabet_limit(true, max_limit);
822 switch (tb) {
823 case TFALSE:
824 loc->error("Cannot determine the value of MAX: the parent subtype does not define a maximal char value");
825 max_limit = universal_char_limit_t::maximum;
826 break;
827 case TUNKNOWN:
828 loc->warning("Cannot determine the value of MAX, using the maximal char value of the type");
829 max_limit = universal_char_limit_t::maximum;
830 break;
831 case TTRUE:
832 // max_limit was set to the correct value
833 break;
834 default:
835 FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
836 }
837 } else {
838 max_limit = universal_char_limit_t::maximum;
839 }
840 }
841
842 if (max_exclusive) {
843 if (max_limit==universal_char_limit_t::minimum) {
844 loc->error("exclusive upper boundary is not a legal character");
845 return NULL;
846 }
847 max_limit = max_limit.previous();
848 }
849 if (max_limit<min_limit) {
850 loc->error("lower boundary is bigger than upper boundary in string value range constraint");
851 return NULL;
852 }
853 SubtypeConstraint* stc = new SubtypeConstraint(st_t);
854 stc->universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharRangeListConstraint(min_limit,max_limit), true);
855 return stc;
856 } break;
857 default:
858 FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
859 }
860 return NULL;
861}
862
863SubtypeConstraint* SubtypeConstraint::create_from_contained_subtype(SubtypeConstraint* contained_stc, bool char_context, Location* loc)
864{
865 if (contained_stc==NULL) return NULL;
866 SubtypeConstraint* rv_stc = NULL;
867 if (char_context) {
868 switch (contained_stc->get_subtypetype()) {
869 case ST_CHARSTRING:
870 if (contained_stc->charstring_st==NULL) {
871 rv_stc = new SubtypeConstraint(contained_stc->get_subtypetype()); // full set
872 } else {
873 if (contained_stc->charstring_st->is_valid_range()) {
874 rv_stc = new SubtypeConstraint(contained_stc->get_subtypetype());
875 rv_stc->copy(contained_stc);
876 rv_stc->charstring_st->set_char_context(true);
877 } else {
878 loc->error("The type of the contained subtype constraint cannot be used in a permitted alphabet constraint");
879 }
880 }
881 break;
882 case ST_UNIVERSAL_CHARSTRING:
883 if (contained_stc->universal_charstring_st==NULL) {
884 rv_stc = new SubtypeConstraint(contained_stc->get_subtypetype()); // full set
885 } else {
886 if (contained_stc->universal_charstring_st->is_valid_range()) {
887 rv_stc = new SubtypeConstraint(contained_stc->get_subtypetype());
888 rv_stc->copy(contained_stc);
889 rv_stc->universal_charstring_st->set_char_context(true);
890 } else {
891 loc->error("The type of the contained subtype constraint cannot be used in a permitted alphabet constraint");
892 }
893 }
894 break;
895 default:
896 // error was already reported
897 break;
898 }
899 } else {
900 rv_stc = new SubtypeConstraint(contained_stc->get_subtypetype());
901 rv_stc->copy(contained_stc);
902 }
903 return rv_stc;
904}
905
906SubtypeConstraint* SubtypeConstraint::create_asn_size_constraint(
907 SubtypeConstraint* integer_stc, bool char_context, Type* type, Location* loc)
908{
909 if (integer_stc==NULL) return NULL;
910 // convert IntegerRangeListConstraint to SizeRangeListConstraint
911 if (integer_stc->subtype!=ST_INTEGER) FATAL_ERROR("SubtypeConstraint::create_asn_size_constraint()");
912 SizeRangeListConstraint size_constraint(size_limit_t::minimum, size_limit_t::maximum);
913 if (integer_stc->integer_st) {
914 static const int_val_t zero((Int)0);
915 static const int_limit_t ilt0(zero);
916 IntegerRangeListConstraint valid_range(ilt0, int_limit_t::maximum);
917 if (integer_stc->integer_st->is_subset(valid_range)==TFALSE) {
918 loc->error("Range %s is not a valid range for a size constraint", integer_stc->to_string().c_str());
919 } else {
920 bool success = convert_int_to_size(*(integer_stc->integer_st), size_constraint);
921 if (!success) {
922 loc->error("One or more INTEGER values of range %s are too large to be used in a size constraint", integer_stc->to_string().c_str());
923 }
924 }
925 }
926 subtype_t st_t = type->get_subtype_type();
927 if (st_t==ST_ERROR) return NULL;
928 SubtypeConstraint* stc = new SubtypeConstraint(st_t);
929
930 if (!char_context) {
931 stc->length_restriction = new SizeRangeListConstraint(size_constraint); // FIXME? : is this Ok if not a top level constraint?
932 }
933
934 switch (st_t) {
935 case ST_BITSTRING:
936 stc->bitstring_st = new BitstringConstraint(size_constraint);
937 break;
938 case ST_HEXSTRING:
939 stc->hexstring_st = new HexstringConstraint(size_constraint);
940 break;
941 case ST_OCTETSTRING:
942 stc->octetstring_st = new OctetstringConstraint(size_constraint);
943 break;
944 case ST_CHARSTRING:
945 if (char_context) {
946 if (size_constraint.is_equal(SizeRangeListConstraint(size_limit_t(1)))==TFALSE) {
947 loc->error("Only SIZE(1) constraint can be used inside a permitted alphabet constraint");
948 delete stc;
949 return NULL;
950 }
951 // SIZE(1) is allowed in char context, it means ALL
952 } else {
953 stc->charstring_st = new CharstringSubtypeTreeElement(size_constraint);
954 }
955 break;
956 case ST_UNIVERSAL_CHARSTRING:
957 if (char_context) {
958 if (size_constraint.is_equal(SizeRangeListConstraint(size_limit_t(1)))==TFALSE) {
959 loc->error("Only SIZE(1) constraint can be used inside a permitted alphabet constraint");
960 delete stc;
961 return NULL;
962 }
963 // SIZE(1) is allowed in char context, it means ALL
964 } else {
965 stc->universal_charstring_st = new UniversalCharstringSubtypeTreeElement(size_constraint);
966 }
967 break;
968 case ST_RECORDOF:
969 case ST_SETOF:
970 stc->recof_st = new RecofConstraint(size_constraint);
971 break;
972 default:
973 loc->error("Size constraint is not allowed for type `%s'", type->get_typename().c_str());
974 delete stc;
975 return NULL;
976 }
977 return stc;
978}
979
980SubtypeConstraint* SubtypeConstraint::create_permitted_alphabet_constraint(
981 SubtypeConstraint* stc, bool char_context, Type* type, Location* loc)
982{
983 if (char_context) {
984 loc->error("Permitted alphabet constraint not allowed inside a permitted alphabet constraint");
985 return NULL;
986 }
987 subtype_t st_t = type->get_subtype_type();
988 switch (st_t) {
989 case ST_CHARSTRING:
990 case ST_UNIVERSAL_CHARSTRING: {
991 if (stc==NULL) return NULL; // error was reported there
992 if (st_t!=stc->get_subtypetype()) FATAL_ERROR("SubtypeConstraint::create_permitted_alphabet_constraint()");
993 SubtypeConstraint* rv_stc = new SubtypeConstraint(st_t);
994 if (st_t==ST_CHARSTRING) {
995 if (stc->charstring_st) {
996 rv_stc->charstring_st = new CharstringSubtypeTreeElement(*(stc->charstring_st));
997 rv_stc->charstring_st->set_char_context(false);
998 }
999 } else {
1000 if (stc->universal_charstring_st) {
1001 rv_stc->universal_charstring_st = new UniversalCharstringSubtypeTreeElement(*(stc->universal_charstring_st));
1002 rv_stc->universal_charstring_st->set_char_context(false);
1003 }
1004 }
1005 return rv_stc;
1006 } break;
1007 case ST_ERROR:
1008 // error already reported
1009 break;
1010 default:
1011 loc->error("Permitted alphabet constraint is not allowed for type `%s'", type->get_typename().c_str());
1012 break;
1013 }
1014 return NULL;
1015}
1016
1017void SubtypeConstraint::set_to_error()
1018{
1019 switch (subtype) {
1020 case ST_ERROR:
1021 break;
1022 case ST_INTEGER:
1023 delete integer_st;
1024 break;
1025 case ST_FLOAT:
1026 delete float_st;
1027 break;
1028 case ST_BOOLEAN:
1029 delete boolean_st;
1030 break;
1031 case ST_VERDICTTYPE:
1032 delete verdict_st;
1033 break;
1034 case ST_BITSTRING:
1035 delete bitstring_st;
1036 break;
1037 case ST_HEXSTRING:
1038 delete hexstring_st;
1039 break;
1040 case ST_OCTETSTRING:
1041 delete octetstring_st;
1042 break;
1043 case ST_CHARSTRING:
1044 delete charstring_st;
1045 break;
1046 case ST_UNIVERSAL_CHARSTRING:
1047 delete universal_charstring_st;
1048 break;
1049 case ST_OBJID:
1050 case ST_RECORD:
1051 case ST_SET:
1052 case ST_ENUM:
1053 case ST_UNION:
1054 case ST_FUNCTION:
1055 case ST_ALTSTEP:
1056 case ST_TESTCASE:
1057 delete value_st;
1058 break;
1059 case ST_RECORDOF:
1060 case ST_SETOF:
1061 delete recof_st;
1062 break;
1063 default:
1064 FATAL_ERROR("SubtypeConstraint::set_to_error()");
1065 }
1066 subtype = ST_ERROR;
1067 delete length_restriction;
1068 length_restriction = NULL;
1069}
1070
1071string SubtypeConstraint::to_string() const
1072{
1073 switch (subtype) {
1074 case ST_ERROR:
1075 return string("<error>");
1076 case ST_INTEGER:
1077 return (integer_st==NULL) ? string() : integer_st->to_string();
1078 case ST_FLOAT:
1079 return (float_st==NULL) ? string() : float_st->to_string();
1080 case ST_BOOLEAN:
1081 return (boolean_st==NULL) ? string() : boolean_st->to_string();
1082 case ST_VERDICTTYPE:
1083 return (verdict_st==NULL) ? string() : verdict_st->to_string();
1084 case ST_BITSTRING:
1085 return (bitstring_st==NULL) ? string() : bitstring_st->to_string();
1086 case ST_HEXSTRING:
1087 return (hexstring_st==NULL) ? string() : hexstring_st->to_string();
1088 case ST_OCTETSTRING:
1089 return (octetstring_st==NULL) ? string() : octetstring_st->to_string();
1090 case ST_CHARSTRING:
1091 return (charstring_st==NULL) ? string() : charstring_st->to_string();
1092 case ST_UNIVERSAL_CHARSTRING:
1093 return (universal_charstring_st==NULL) ? string() : universal_charstring_st->to_string();
1094 case ST_OBJID:
1095 case ST_RECORD:
1096 case ST_SET:
1097 case ST_ENUM:
1098 case ST_UNION:
1099 case ST_FUNCTION:
1100 case ST_ALTSTEP:
1101 case ST_TESTCASE:
1102 return (value_st==NULL) ? string() : value_st->to_string();
1103 case ST_RECORDOF:
1104 case ST_SETOF:
1105 return (recof_st==NULL) ? string() : recof_st->to_string();
1106 default:
1107 FATAL_ERROR("SubtypeConstraint::to_string()");
1108 }
1109}
1110
1111bool SubtypeConstraint::is_compatible(const SubtypeConstraint *p_st) const
1112{
1113 if (p_st==NULL) return true; // the other type has no subtype restriction
1114 if ( (subtype==ST_ERROR) || (p_st->subtype==ST_ERROR) ) return true;
1115 if (subtype!=p_st->subtype) FATAL_ERROR("SubtypeConstraint::is_compatible()");
1116 // if the resulting set.is_empty()==TUNKNOWN then remain silent
1117 switch (subtype) {
1118 case ST_INTEGER:
1119 if ((integer_st==NULL) || (p_st->integer_st==NULL)) return true;
1120 return ((*integer_st**(p_st->integer_st)).is_empty()!=TTRUE);
1121 case ST_FLOAT:
1122 if ((float_st==NULL) || (p_st->float_st==NULL)) return true;
1123 return ((*float_st**(p_st->float_st)).is_empty()!=TTRUE);
1124 case ST_BOOLEAN:
1125 if ((boolean_st==NULL) || (p_st->boolean_st==NULL)) return true;
1126 return ((*boolean_st**(p_st->boolean_st)).is_empty()!=TTRUE);
1127 case ST_VERDICTTYPE:
1128 if ((verdict_st==NULL) || (p_st->verdict_st==NULL)) return true;
1129 return ((*verdict_st**(p_st->verdict_st)).is_empty()!=TTRUE);
1130 case ST_BITSTRING:
1131 if ((bitstring_st==NULL) || (p_st->bitstring_st==NULL)) return true;
1132 return ((*bitstring_st**(p_st->bitstring_st)).is_empty()!=TTRUE);
1133 case ST_HEXSTRING:
1134 if ((hexstring_st==NULL) || (p_st->hexstring_st==NULL)) return true;
1135 return ((*hexstring_st**(p_st->hexstring_st)).is_empty()!=TTRUE);
1136 case ST_OCTETSTRING:
1137 if ((octetstring_st==NULL) || (p_st->octetstring_st==NULL)) return true;
1138 return ((*octetstring_st**(p_st->octetstring_st)).is_empty()!=TTRUE);
1139 case ST_CHARSTRING: {
1140 if ((charstring_st==NULL) || (p_st->charstring_st==NULL)) return true;
1141 CharstringSubtypeTreeElement* cc = new CharstringSubtypeTreeElement(
1142 CharstringSubtypeTreeElement::ET_INTERSECTION,
1143 new CharstringSubtypeTreeElement(*charstring_st),
1144 new CharstringSubtypeTreeElement(*(p_st->charstring_st))
1145 );
1146 bool rv = (cc->is_empty()!=TTRUE);
1147 delete cc;
1148 return rv;
1149 }
1150 case ST_UNIVERSAL_CHARSTRING: {
1151 if ((universal_charstring_st==NULL) || (p_st->universal_charstring_st==NULL)) return true;
1152 UniversalCharstringSubtypeTreeElement* ucc = new UniversalCharstringSubtypeTreeElement(
1153 UniversalCharstringSubtypeTreeElement::ET_INTERSECTION,
1154 new UniversalCharstringSubtypeTreeElement(*universal_charstring_st),
1155 new UniversalCharstringSubtypeTreeElement(*(p_st->universal_charstring_st))
1156 );
1157 bool rv = (ucc->is_empty()!=TTRUE);
1158 delete ucc;
1159 return rv;
1160 }
1161 case ST_OBJID:
1162 case ST_RECORD:
1163 case ST_SET:
1164 case ST_ENUM:
1165 case ST_UNION:
1166 case ST_FUNCTION:
1167 case ST_ALTSTEP:
1168 case ST_TESTCASE:
1169 if ((value_st==NULL) || (p_st->value_st==NULL)) return true;
1170 return ((*value_st**(p_st->value_st)).is_empty()!=TTRUE);
1171 case ST_RECORDOF:
1172 case ST_SETOF:
1173 if ((recof_st==NULL) || (p_st->recof_st==NULL)) return true;
1174 return ((*recof_st**(p_st->recof_st)).is_empty()!=TTRUE);
1175 default:
1176 FATAL_ERROR("SubtypeConstraint::is_compatible()");
1177 }
1178 return true;
1179}
1180
1181bool SubtypeConstraint::is_compatible_with_elem() const
1182{
1183 if (subtype==ST_ERROR) return true;
1184 switch (subtype) {
1185 case ST_BITSTRING: {
1186 static BitstringConstraint str_elem(size_limit_t(1));
1187 if (bitstring_st==NULL) return true;
1188 return ((*bitstring_st*str_elem).is_empty()!=TTRUE);
1189 }
1190 case ST_HEXSTRING: {
1191 static HexstringConstraint str_elem(size_limit_t(1));
1192 if (hexstring_st==NULL) return true;
1193 return ((*hexstring_st*str_elem).is_empty()!=TTRUE);
1194 }
1195 case ST_OCTETSTRING: {
1196 static OctetstringConstraint str_elem(size_limit_t(1));
1197 if (octetstring_st==NULL) return true;
1198 return ((*octetstring_st*str_elem).is_empty()!=TTRUE);
1199 }
1200 case ST_CHARSTRING: {
1201 size_limit_t t = size_limit_t(1);
1202 SizeRangeListConstraint temp = SizeRangeListConstraint(t);
1203 static CharstringSubtypeTreeElement str_elem(temp);
1204 if (charstring_st==NULL) return true;
1205 CharstringSubtypeTreeElement* cc = new CharstringSubtypeTreeElement(
1206 CharstringSubtypeTreeElement::ET_INTERSECTION,
1207 new CharstringSubtypeTreeElement(*charstring_st),
1208 new CharstringSubtypeTreeElement(str_elem)
1209 );
1210 bool rv = (cc->is_empty()!=TTRUE);
1211 delete cc;
1212 return rv;
1213 }
1214 case ST_UNIVERSAL_CHARSTRING: {
1215 size_limit_t t = size_limit_t(1);
1216 SizeRangeListConstraint temp = SizeRangeListConstraint(t);
1217 static UniversalCharstringSubtypeTreeElement str_elem(temp);
1218 if (universal_charstring_st==NULL) return true;
1219 UniversalCharstringSubtypeTreeElement* ucc = new UniversalCharstringSubtypeTreeElement(
1220 UniversalCharstringSubtypeTreeElement::ET_INTERSECTION,
1221 new UniversalCharstringSubtypeTreeElement(*universal_charstring_st),
1222 new UniversalCharstringSubtypeTreeElement(str_elem)
1223 );
1224 bool rv = (ucc->is_empty()!=TTRUE);
1225 delete ucc;
1226 return rv;
1227 }
1228 default:
1229 FATAL_ERROR("SubtypeConstraint::is_compatible_with_elem()");
1230 }
1231 return true;
1232}
1233
1234bool SubtypeConstraint::is_length_compatible(const SubtypeConstraint *p_st) const
1235{
1236 if (p_st==NULL) FATAL_ERROR("SubtypeConstraint::is_length_compatible()");
1237 if ((subtype==ST_ERROR) || (p_st->subtype==ST_ERROR)) return true;
1238 if (subtype != ST_RECORDOF && subtype != ST_SETOF &&
1239 p_st->subtype != ST_RECORDOF && p_st->subtype != ST_SETOF)
1240 FATAL_ERROR("SubtypeConstraint::is_length_compatible()");
1241 if (length_restriction==NULL || p_st->length_restriction==NULL) return true;
1242 return ((*length_restriction * *(p_st->length_restriction)).is_empty()!=TTRUE);
1243}
1244
af710487 1245bool SubtypeConstraint::is_upper_limit_infinity() const
1246{
1247 if (ST_INTEGER == subtype && integer_st) {
1248 return integer_st->is_upper_limit_infinity();
1249 }
1250 if (ST_FLOAT == subtype && float_st) {
1251 return float_st->is_upper_limit_infinity();
1252 }
1253 return false;
1254}
1255
1256bool SubtypeConstraint::is_lower_limit_infinity() const
1257{
1258 if (ST_INTEGER == subtype && integer_st) {
1259 return integer_st->is_lower_limit_infinity();
1260 }
1261
1262 if (ST_FLOAT == subtype && float_st) {
1263 return float_st->is_lower_limit_infinity();
1264 }
1265 return false;
1266}
1267
1268
970ed795
EL
1269void SubtypeConstraint::except(const SubtypeConstraint* other)
1270{
1271 if (other==NULL) FATAL_ERROR("SubtypeConstraint::except()");
1272 if (subtype!=other->subtype) FATAL_ERROR("SubtypeConstraint::except()");
1273 switch (subtype) {
1274 case ST_INTEGER:
1275 if (other->integer_st==NULL) {
1276 if (integer_st==NULL) {
1277 integer_st = new IntegerRangeListConstraint();
1278 } else {
1279 *integer_st = IntegerRangeListConstraint();
1280 }
1281 } else {
1282 if (integer_st==NULL) {
1283 integer_st = new IntegerRangeListConstraint(~*(other->integer_st));
1284 } else {
1285 *integer_st = *integer_st - *(other->integer_st);
1286 }
1287 }
1288 break;
1289 case ST_FLOAT:
1290 if (other->float_st==NULL) {
1291 if (float_st==NULL) {
1292 float_st = new RealRangeListConstraint();
1293 } else {
1294 *float_st = RealRangeListConstraint();
1295 }
1296 } else {
1297 if (float_st==NULL) {
1298 float_st = new RealRangeListConstraint(~*(other->float_st));
1299 } else {
1300 *float_st = *float_st - *(other->float_st);
1301 }
1302 }
1303 break;
1304 case ST_BOOLEAN:
1305 if (other->boolean_st==NULL) {
1306 if (boolean_st==NULL) {
1307 boolean_st = new BooleanListConstraint();
1308 } else {
1309 *boolean_st = BooleanListConstraint();
1310 }
1311 } else {
1312 if (boolean_st==NULL) {
1313 boolean_st = new BooleanListConstraint(~*(other->boolean_st));
1314 } else {
1315 *boolean_st = *boolean_st - *(other->boolean_st);
1316 }
1317 }
1318 break;
1319 case ST_VERDICTTYPE:
1320 if (other->verdict_st==NULL) {
1321 if (verdict_st==NULL) {
1322 verdict_st = new VerdicttypeListConstraint();
1323 } else {
1324 *verdict_st = VerdicttypeListConstraint();
1325 }
1326 } else {
1327 if (verdict_st==NULL) {
1328 verdict_st = new VerdicttypeListConstraint(~*(other->verdict_st));
1329 } else {
1330 *verdict_st = *verdict_st - *(other->verdict_st);
1331 }
1332 }
1333 break;
1334 case ST_BITSTRING:
1335 if (other->bitstring_st==NULL) {
1336 if (bitstring_st==NULL) {
1337 bitstring_st = new BitstringConstraint();
1338 } else {
1339 *bitstring_st = BitstringConstraint();
1340 }
1341 } else {
1342 if (bitstring_st==NULL) {
1343 bitstring_st = new BitstringConstraint(~*(other->bitstring_st));
1344 } else {
1345 *bitstring_st = *bitstring_st - *(other->bitstring_st);
1346 }
1347 }
1348 break;
1349 case ST_HEXSTRING:
1350 if (other->hexstring_st==NULL) {
1351 if (hexstring_st==NULL) {
1352 hexstring_st = new HexstringConstraint();
1353 } else {
1354 *hexstring_st = HexstringConstraint();
1355 }
1356 } else {
1357 if (hexstring_st==NULL) {
1358 hexstring_st = new HexstringConstraint(~*(other->hexstring_st));
1359 } else {
1360 *hexstring_st = *hexstring_st - *(other->hexstring_st);
1361 }
1362 }
1363 break;
1364 case ST_OCTETSTRING:
1365 if (other->octetstring_st==NULL) {
1366 if (octetstring_st==NULL) {
1367 octetstring_st = new OctetstringConstraint();
1368 } else {
1369 *octetstring_st = OctetstringConstraint();
1370 }
1371 } else {
1372 if (octetstring_st==NULL) {
1373 octetstring_st = new OctetstringConstraint(~*(other->octetstring_st));
1374 } else {
1375 *octetstring_st = *octetstring_st - *(other->octetstring_st);
1376 }
1377 }
1378 break;
1379 case ST_CHARSTRING:
1380 if (other->charstring_st==NULL) {
1381 if (charstring_st==NULL) {
1382 charstring_st = new CharstringSubtypeTreeElement();
1383 } else {
1384 *charstring_st = CharstringSubtypeTreeElement();
1385 }
1386 } else {
1387 if (charstring_st==NULL) {
1388 CharstringSubtypeTreeElement* call_st = new CharstringSubtypeTreeElement();
1389 call_st->set_all();
1390 charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_EXCEPT,
1391 call_st, new CharstringSubtypeTreeElement(*(other->charstring_st)));
1392 } else {
1393 charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_EXCEPT,
1394 charstring_st, new CharstringSubtypeTreeElement(*(other->charstring_st)));
1395 }
1396 }
1397 break;
1398 case ST_UNIVERSAL_CHARSTRING:
1399 if (other->universal_charstring_st==NULL) {
1400 if (universal_charstring_st==NULL) {
1401 universal_charstring_st = new UniversalCharstringSubtypeTreeElement();
1402 } else {
1403 *universal_charstring_st = UniversalCharstringSubtypeTreeElement();
1404 }
1405 } else {
1406 if (universal_charstring_st==NULL) {
1407 UniversalCharstringSubtypeTreeElement* ucall_st = new UniversalCharstringSubtypeTreeElement();
1408 ucall_st->set_all();
1409 universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_EXCEPT,
1410 ucall_st, new UniversalCharstringSubtypeTreeElement(*(other->universal_charstring_st)));
1411 } else {
1412 universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_EXCEPT,
1413 universal_charstring_st, new UniversalCharstringSubtypeTreeElement(*(other->universal_charstring_st)));
1414 }
1415 }
1416 break;
1417 case ST_OBJID:
1418 case ST_RECORD:
1419 case ST_SET:
1420 case ST_ENUM:
1421 case ST_UNION:
1422 case ST_FUNCTION:
1423 case ST_ALTSTEP:
1424 case ST_TESTCASE:
1425 if (other->value_st==NULL) {
1426 if (value_st==NULL) {
1427 value_st = new ValueListConstraint();
1428 } else {
1429 *value_st = ValueListConstraint();
1430 }
1431 } else {
1432 if (value_st==NULL) {
1433 value_st = new ValueListConstraint(~*(other->value_st));
1434 } else {
1435 *value_st = *value_st - *(other->value_st);
1436 }
1437 }
1438 break;
1439 case ST_RECORDOF:
1440 case ST_SETOF:
1441 if (other->recof_st==NULL) {
1442 if (recof_st==NULL) {
1443 recof_st = new RecofConstraint();
1444 } else {
1445 *recof_st = RecofConstraint();
1446 }
1447 } else {
1448 if (recof_st==NULL) {
1449 recof_st = new RecofConstraint(~*(other->recof_st));
1450 } else {
1451 *recof_st = *recof_st - *(other->recof_st);
1452 }
1453 }
1454 break;
1455 default:
1456 FATAL_ERROR("SubtypeConstraint::except()");
1457 }
1458 if (other->length_restriction==NULL) {
1459 if (length_restriction==NULL) {
1460 length_restriction = new SizeRangeListConstraint();
1461 } else {
1462 *length_restriction = SizeRangeListConstraint();
1463 }
1464 } else {
1465 if (length_restriction==NULL) {
1466 length_restriction = new SizeRangeListConstraint(~*(other->length_restriction));
1467 } else {
1468 *length_restriction = *length_restriction - *(other->length_restriction);
1469 }
1470 }
1471}
1472
1473void SubtypeConstraint::union_(const SubtypeConstraint* other)
1474{
1475 if (other==NULL) FATAL_ERROR("SubtypeConstraint::union_()");
1476 if (subtype!=other->subtype) FATAL_ERROR("SubtypeConstraint::union_()");
1477 switch (subtype) {
1478 case ST_INTEGER:
1479 if (integer_st==NULL) break;
1480 if (other->integer_st==NULL) { delete integer_st; integer_st = NULL; break; }
1481 *integer_st = *integer_st + *(other->integer_st);
1482 break;
1483 case ST_FLOAT:
1484 if (float_st==NULL) break;
1485 if (other->float_st==NULL) { delete float_st; float_st = NULL; break; }
1486 *float_st = *float_st + *(other->float_st);
1487 break;
1488 case ST_BOOLEAN:
1489 if (boolean_st==NULL) break;
1490 if (other->boolean_st==NULL) { delete boolean_st; boolean_st = NULL; break; }
1491 *boolean_st = *boolean_st + *(other->boolean_st);
1492 break;
1493 case ST_VERDICTTYPE:
1494 if (verdict_st==NULL) break;
1495 if (other->verdict_st==NULL) { delete verdict_st; verdict_st = NULL; break; }
1496 *verdict_st = *verdict_st + *(other->verdict_st);
1497 break;
1498 case ST_BITSTRING:
1499 if (bitstring_st==NULL) break;
1500 if (other->bitstring_st==NULL) { delete bitstring_st; bitstring_st = NULL; break; }
1501 *bitstring_st = *bitstring_st + *(other->bitstring_st);
1502 break;
1503 case ST_HEXSTRING:
1504 if (hexstring_st==NULL) break;
1505 if (other->hexstring_st==NULL) { delete hexstring_st; hexstring_st = NULL; break; }
1506 *hexstring_st = *hexstring_st + *(other->hexstring_st);
1507 break;
1508 case ST_OCTETSTRING:
1509 if (octetstring_st==NULL) break;
1510 if (other->octetstring_st==NULL) { delete octetstring_st; octetstring_st = NULL; break; }
1511 *octetstring_st = *octetstring_st + *(other->octetstring_st);
1512 break;
1513 case ST_CHARSTRING:
1514 if (charstring_st==NULL) break;
1515 if (other->charstring_st==NULL) { delete charstring_st; charstring_st = NULL; break; }
1516 charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_UNION,
1517 charstring_st, new CharstringSubtypeTreeElement(*(other->charstring_st)));
1518 break;
1519 case ST_UNIVERSAL_CHARSTRING:
1520 if (universal_charstring_st==NULL) break;
1521 if (other->universal_charstring_st==NULL) { delete universal_charstring_st; universal_charstring_st = NULL; break; }
1522 universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_UNION,
1523 universal_charstring_st, new UniversalCharstringSubtypeTreeElement(*(other->universal_charstring_st)));
1524 break;
1525 case ST_OBJID:
1526 case ST_RECORD:
1527 case ST_SET:
1528 case ST_ENUM:
1529 case ST_UNION:
1530 case ST_FUNCTION:
1531 case ST_ALTSTEP:
1532 case ST_TESTCASE:
1533 if (value_st==NULL) break;
1534 if (other->value_st==NULL) { delete value_st; value_st = NULL; break; }
1535 *value_st = *value_st + *(other->value_st);
1536 break;
1537 case ST_RECORDOF:
1538 case ST_SETOF:
1539 if (recof_st==NULL) break;
1540 if (other->recof_st==NULL) { delete recof_st; recof_st = NULL; break; }
1541 *recof_st = *recof_st + *(other->recof_st);
1542 break;
1543 default:
1544 FATAL_ERROR("SubtypeConstraint::union_()");
1545 }
1546 if (length_restriction!=NULL) {
1547 if (other->length_restriction==NULL) {
1548 delete length_restriction;
1549 length_restriction = NULL;
1550 } else {
1551 *length_restriction = *length_restriction + *(other->length_restriction);
1552 }
1553 }
1554}
1555
1556void SubtypeConstraint::intersection(const SubtypeConstraint* other)
1557{
1558 if (other==NULL) FATAL_ERROR("SubtypeConstraint::intersection()");
1559 if (subtype!=other->subtype) FATAL_ERROR("SubtypeConstraint::intersection()");
1560 switch (subtype) {
1561 case ST_INTEGER:
1562 if (other->integer_st!=NULL) {
1563 if (integer_st==NULL) {
1564 integer_st = new IntegerRangeListConstraint(*(other->integer_st));
1565 } else {
1566 *integer_st = *integer_st * *(other->integer_st);
1567 }
1568 }
1569 break;
1570 case ST_FLOAT:
1571 if (other->float_st!=NULL) {
1572 if (float_st==NULL) {
1573 float_st = new RealRangeListConstraint(*(other->float_st));
1574 } else {
1575 *float_st = *float_st * *(other->float_st);
1576 }
1577 }
1578 break;
1579 case ST_BOOLEAN:
1580 if (other->boolean_st!=NULL) {
1581 if (boolean_st==NULL) {
1582 boolean_st = new BooleanListConstraint(*(other->boolean_st));
1583 } else {
1584 *boolean_st = *boolean_st * *(other->boolean_st);
1585 }
1586 }
1587 break;
1588 case ST_VERDICTTYPE:
1589 if (other->verdict_st!=NULL) {
1590 if (verdict_st==NULL) {
1591 verdict_st = new VerdicttypeListConstraint(*(other->verdict_st));
1592 } else {
1593 *verdict_st = *verdict_st * *(other->verdict_st);
1594 }
1595 }
1596 break;
1597 case ST_BITSTRING:
1598 if (other->bitstring_st!=NULL) {
1599 if (bitstring_st==NULL) {
1600 bitstring_st = new BitstringConstraint(*(other->bitstring_st));
1601 } else {
1602 *bitstring_st = *bitstring_st * *(other->bitstring_st);
1603 }
1604 }
1605 break;
1606 case ST_HEXSTRING:
1607 if (other->hexstring_st!=NULL) {
1608 if (hexstring_st==NULL) {
1609 hexstring_st = new HexstringConstraint(*(other->hexstring_st));
1610 } else {
1611 *hexstring_st = *hexstring_st * *(other->hexstring_st);
1612 }
1613 }
1614 break;
1615 case ST_OCTETSTRING:
1616 if (other->octetstring_st!=NULL) {
1617 if (octetstring_st==NULL) {
1618 octetstring_st = new OctetstringConstraint(*(other->octetstring_st));
1619 } else {
1620 *octetstring_st = *octetstring_st * *(other->octetstring_st);
1621 }
1622 }
1623 break;
1624 case ST_CHARSTRING:
1625 if (other->charstring_st!=NULL) {
1626 if (charstring_st==NULL) {
1627 charstring_st = new CharstringSubtypeTreeElement(*(other->charstring_st));
1628 } else {
1629 charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_INTERSECTION,
1630 charstring_st, new CharstringSubtypeTreeElement(*(other->charstring_st)));
1631 }
1632 }
1633 break;
1634 case ST_UNIVERSAL_CHARSTRING:
1635 if (other->universal_charstring_st!=NULL) {
1636 if (universal_charstring_st==NULL) {
1637 universal_charstring_st = new UniversalCharstringSubtypeTreeElement(*(other->universal_charstring_st));
1638 } else {
1639 universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_INTERSECTION,
1640 universal_charstring_st, new UniversalCharstringSubtypeTreeElement(*(other->universal_charstring_st)));
1641 }
1642 }
1643 break;
1644 case ST_OBJID:
1645 case ST_RECORD:
1646 case ST_SET:
1647 case ST_ENUM:
1648 case ST_UNION:
1649 case ST_FUNCTION:
1650 case ST_ALTSTEP:
1651 case ST_TESTCASE:
1652 if (other->value_st!=NULL) {
1653 if (value_st==NULL) {
1654 value_st = new ValueListConstraint(*(other->value_st));
1655 } else {
1656 *value_st = *value_st * *(other->value_st);
1657 }
1658 }
1659 break;
1660 case ST_RECORDOF:
1661 case ST_SETOF:
1662 if (other->recof_st!=NULL) {
1663 if (recof_st==NULL) {
1664 recof_st = new RecofConstraint(*(other->recof_st));
1665 } else {
1666 *recof_st = *recof_st * *(other->recof_st);
1667 }
1668 }
1669 break;
1670 default:
1671 FATAL_ERROR("SubtypeConstraint::intersection()");
1672 }
1673 if (other->length_restriction!=NULL) {
1674 if (length_restriction==NULL) {
1675 length_restriction = new SizeRangeListConstraint(*(other->length_restriction));
1676 } else {
1677 *length_restriction = *length_restriction * *(other->length_restriction);
1678 }
1679 }
1680}
1681
1682tribool SubtypeConstraint::is_subset(const SubtypeConstraint* other) const
1683{
1684 if (other==NULL) return TTRUE;
1685 if (other->subtype!=subtype) FATAL_ERROR("SubtypeConstraint::is_subset()");
1686 switch (subtype) {
1687 case ST_INTEGER:
1688 if (other->integer_st==NULL) return TTRUE;
1689 return integer_st ? integer_st->is_subset(*(other->integer_st)) : TTRUE;
1690 case ST_FLOAT:
1691 if (other->float_st==NULL) return TTRUE;
1692 return float_st ? float_st->is_subset(*(other->float_st)) : TTRUE;
1693 case ST_BOOLEAN:
1694 if (other->boolean_st==NULL) return TTRUE;
1695 return boolean_st ? boolean_st->is_subset(*(other->boolean_st)) : TTRUE;
1696 case ST_VERDICTTYPE:
1697 if (other->verdict_st==NULL) return TTRUE;
1698 return verdict_st ? verdict_st->is_subset(*(other->verdict_st)) : TTRUE;
1699 case ST_BITSTRING:
1700 if (other->bitstring_st==NULL) return TTRUE;
1701 return bitstring_st ? bitstring_st->is_subset(*(other->bitstring_st)) : TTRUE;
1702 case ST_HEXSTRING:
1703 if (other->hexstring_st==NULL) return TTRUE;
1704 return hexstring_st ? hexstring_st->is_subset(*(other->hexstring_st)) : TTRUE;
1705 case ST_OCTETSTRING:
1706 if (other->octetstring_st==NULL) return TTRUE;
1707 return octetstring_st ? octetstring_st->is_subset(*(other->octetstring_st)) : TTRUE;
1708 case ST_CHARSTRING:
1709 if (other->charstring_st==NULL) return TTRUE;
1710 return charstring_st ? charstring_st->is_subset(other->charstring_st) : TTRUE;
1711 case ST_UNIVERSAL_CHARSTRING:
1712 if (other->universal_charstring_st==NULL) return TTRUE;
1713 return universal_charstring_st ? universal_charstring_st->is_subset(other->universal_charstring_st) : TTRUE;
1714 case ST_OBJID:
1715 case ST_RECORD:
1716 case ST_SET:
1717 case ST_ENUM:
1718 case ST_UNION:
1719 case ST_FUNCTION:
1720 case ST_ALTSTEP:
1721 case ST_TESTCASE:
1722 if (other->value_st==NULL) return TTRUE;
1723 return value_st ? value_st->is_subset(*(other->value_st)) : TTRUE;
1724 case ST_RECORDOF:
1725 case ST_SETOF:
1726 if (other->recof_st==NULL) return TTRUE;
1727 return recof_st ? recof_st->is_subset(*(other->recof_st)) : TTRUE;
1728 default:
1729 FATAL_ERROR("SubtypeConstraint::is_subset()");
1730 }
1731 return TUNKNOWN;
1732}
1733
1734/********************
1735class SubType
1736********************/
1737
1738SubType::SubType(subtype_t st, Type *p_my_owner, SubType* p_parent_subtype,
1739 vector<SubTypeParse> *p_parsed, Constraints* p_asn_constraints)
1740: SubtypeConstraint(st), my_owner(p_my_owner), parent_subtype(p_parent_subtype)
1741, parsed(p_parsed), asn_constraints(p_asn_constraints)
1742, root(0), extendable(false), extension(0), checked(STC_NO)
1743, my_parents()
1744{
1745 if (p_my_owner==NULL) FATAL_ERROR("SubType::SubType()");
1746}
1747
1748SubType::~SubType()
1749{
1750 my_parents.clear();
1751}
1752
1753void SubType::chk_this_value(Value *value)
1754{
1755 if (checked==STC_NO) FATAL_ERROR("SubType::chk_this_value()");
1756 if ((checked==STC_CHECKING) || (subtype==ST_ERROR)) return;
1757 Value *val = value->get_value_refd_last();
1758 bool is_invalid = false;
1759 switch (val->get_valuetype()) {
1760 case Value::V_INT:
1761 if (subtype!=ST_INTEGER) FATAL_ERROR("SubType::chk_this_value()");
1762 is_invalid = (integer_st!=NULL) && !integer_st->is_element(int_limit_t(*(val->get_val_Int())));
1763 break;
1764 case Value::V_REAL:
1765 if (subtype!=ST_FLOAT) FATAL_ERROR("SubType::chk_this_value()");
1766 is_invalid = (float_st!=NULL) && !float_st->is_element(val->get_val_Real());
1767 break;
1768 case Value::V_BOOL:
1769 if (subtype!=ST_BOOLEAN) FATAL_ERROR("SubType::chk_this_value()");
1770 is_invalid = (boolean_st!=NULL) && !boolean_st->is_element(val->get_val_bool());
1771 break;
1772 case Value::V_VERDICT: {
1773 if (subtype!=ST_VERDICTTYPE) FATAL_ERROR("SubType::chk_this_value()");
1774 VerdicttypeListConstraint::verdicttype_constraint_t vtc;
1775 switch (val->get_val_verdict()) {
1776 case Value::Verdict_NONE: vtc = VerdicttypeListConstraint::VC_NONE; break;
1777 case Value::Verdict_PASS: vtc = VerdicttypeListConstraint::VC_PASS; break;
1778 case Value::Verdict_INCONC: vtc = VerdicttypeListConstraint::VC_INCONC; break;
1779 case Value::Verdict_FAIL: vtc = VerdicttypeListConstraint::VC_FAIL; break;
1780 case Value::Verdict_ERROR: vtc = VerdicttypeListConstraint::VC_ERROR; break;
1781 default: FATAL_ERROR("SubType::chk_this_value()");
1782 }
1783 is_invalid = (verdict_st!=NULL) && !verdict_st->is_element(vtc);
1784 } break;
1785 case Value::V_BSTR:
1786 if (subtype!=ST_BITSTRING) FATAL_ERROR("SubType::chk_this_value()");
1787 is_invalid = (bitstring_st!=NULL) && !bitstring_st->is_element(val->get_val_str());
1788 break;
1789 case Value::V_HSTR:
1790 if (subtype!=ST_HEXSTRING) FATAL_ERROR("SubType::chk_this_value()");
1791 is_invalid = (hexstring_st!=NULL) && !hexstring_st->is_element(val->get_val_str());
1792 break;
1793 case Value::V_OSTR:
1794 if (subtype!=ST_OCTETSTRING) FATAL_ERROR("SubType::chk_this_value()");
1795 is_invalid = (octetstring_st!=NULL) && !octetstring_st->is_element(val->get_val_str());
1796 break;
1797 case Value::V_CSTR:
1798 case Value::V_ISO2022STR:
1799 if (subtype!=ST_CHARSTRING) FATAL_ERROR("SubType::chk_this_value()");
1800 is_invalid = (charstring_st!=NULL) && !charstring_st->is_element(val->get_val_str());
1801 break;
1802 case Value::V_USTR:
1803 case Value::V_CHARSYMS:
1804 if (subtype!=ST_UNIVERSAL_CHARSTRING) FATAL_ERROR("SubType::chk_this_value()");
1805 is_invalid = (universal_charstring_st!=NULL) && !universal_charstring_st->is_element(val->get_val_ustr());
1806 break;
1807 case Value::V_SEQOF:
1808 if (subtype!=ST_RECORDOF) FATAL_ERROR("SubType::chk_this_value()");
1809 if (value->is_unfoldable()) return;
1810 is_invalid = (recof_st!=NULL) && !recof_st->is_element(val);
1811 break;
1812 case Value::V_SETOF:
1813 if (subtype!=ST_SETOF) FATAL_ERROR("SubType::chk_this_value()");
1814 if (value->is_unfoldable()) return;
1815 is_invalid = (recof_st!=NULL) && !recof_st->is_element(val);
1816 break;
1817 case Value::V_OID:
1818 case Value::V_ROID:
1819 if (subtype!=ST_OBJID) FATAL_ERROR("SubType::chk_this_value()");
1820 if (value->is_unfoldable()) return;
1821 is_invalid = (value_st!=NULL) && !value_st->is_element(val);
1822 break;
1823 case Value::V_ENUM:
1824 case Value::V_NULL: // FIXME: should go to ST_NULL
1825 if (subtype!=ST_ENUM) FATAL_ERROR("SubType::chk_this_value()");
1826 if (value->is_unfoldable()) return;
1827 is_invalid = (value_st!=NULL) && !value_st->is_element(val);
1828 break;
1829 case Value::V_CHOICE:
1830 case Value::V_OPENTYPE: // FIXME?
1831 if (subtype!=ST_UNION) FATAL_ERROR("SubType::chk_this_value()");
1832 if (value->is_unfoldable()) return;
1833 is_invalid = (value_st!=NULL) && !value_st->is_element(val);
1834 break;
1835 case Value::V_SEQ:
1836 if (subtype!=ST_RECORD) FATAL_ERROR("SubType::chk_this_value()");
1837 if (value->is_unfoldable()) return;
1838 is_invalid = (value_st!=NULL) && !value_st->is_element(val);
1839 break;
1840 case Value::V_SET:
1841 if (subtype!=ST_SET) FATAL_ERROR("SubType::chk_this_value()");
1842 if (value->is_unfoldable()) return;
1843 is_invalid = (value_st!=NULL) && !value_st->is_element(val);
1844 break;
1845 case Value::V_FUNCTION:
1846 if (subtype!=ST_FUNCTION) FATAL_ERROR("SubType::chk_this_value()");
1847 if (value->is_unfoldable()) return;
1848 is_invalid = (value_st!=NULL) && !value_st->is_element(val);
1849 break;
1850 case Value::V_ALTSTEP:
1851 if (subtype!=ST_ALTSTEP) FATAL_ERROR("SubType::chk_this_value()");
1852 if (value->is_unfoldable()) return;
1853 is_invalid = (value_st!=NULL) && !value_st->is_element(val);
1854 break;
1855 case Value::V_TESTCASE:
1856 if (subtype!=ST_TESTCASE) FATAL_ERROR("SubType::chk_this_value()");
1857 if (value->is_unfoldable()) return;
1858 is_invalid = (value_st!=NULL) && !value_st->is_element(val);
1859 break;
1860 case Value::V_ERROR:
1861 return;
1862 default:
1863 return;
1864 }
1865 if (is_invalid) {
1866 value->error("%s is not a valid value for type `%s' which has subtype %s",
1867 val->get_stringRepr().c_str(),
1868 my_owner->get_typename().c_str(),
1869 to_string().c_str());
1870 }
1871}
1872
1873/** \todo revise */
1874void SubType::chk_this_template_generic(Template *templ)
1875{
1876 if (checked==STC_NO) FATAL_ERROR("SubType::chk_this_template_generic()");
1877 if ((checked==STC_CHECKING) || (subtype==ST_ERROR)) return;
1878 templ = templ->get_template_refd_last();
1879 switch (templ->get_templatetype()) {
1880 case Ttcn::Template::OMIT_VALUE:
1881 case Ttcn::Template::ANY_OR_OMIT:
1882 case Ttcn::Template::TEMPLATE_ERROR:
1883 case Ttcn::Template::ANY_VALUE:
1884 break;
1885 case Ttcn::Template::VALUE_LIST:
1886 case Ttcn::Template::COMPLEMENTED_LIST:
1887 /* Should be canonical before */
1888 break;
1889 case Ttcn::Template::SPECIFIC_VALUE:
1890 /* SPECIFIC_VALUE must be already checked in Type::chk_this_template() */
1891 break;
1892 case Ttcn::Template::TEMPLATE_REFD:
1893 /* unfoldable reference: cannot be checked at compile time */
1894 break;
1895 case Ttcn::Template::TEMPLATE_INVOKE:
1896 /* should be already checked in Type::chk_this_template() */
1897 break;
1898 default:
1899 chk_this_template(templ);
1900 break;
1901 }
1902 chk_this_template_length_restriction(templ);
1903}
1904
1905/** \todo revise */
1906void SubType::chk_this_template(Template *templ)
1907{
1908 switch (templ->get_templatetype()) {
1909 case Template::TEMPLATE_LIST:
1910 if ( (length_restriction!=NULL) && !length_restriction->is_empty() ) {
1911 size_t nof_comp_woaon = templ->get_nof_comps_not_anyornone();
1912 if (!templ->temps_contains_anyornone_symbol() &&
1913 nof_comp_woaon < length_restriction->get_minimal().get_size()) {
1914 templ->error("At least %s elements must be present in the list",
1915 Int2string((Int)(length_restriction->get_minimal().get_size())).c_str());
1916 return;
1917 } else if ( !length_restriction->get_maximal().get_infinity() && (nof_comp_woaon > length_restriction->get_maximal().get_size()) ) {
1918 templ->error("There must not be more than %s elements in the list",
1919 Int2string((Int)(length_restriction->get_maximal().get_size())).c_str());
1920 return;
1921 }
1922 }
1923 break;
1924 /* Simply break. We don't know how many elements are there.
1925 SUPERSET_MATCH/SUBSET_MATCH is not possible to be an
1926 INDEXED_TEMPLATE_LIST. */
1927 case Template::INDEXED_TEMPLATE_LIST:
1928 break;
1929 case Template::NAMED_TEMPLATE_LIST:
1930 break;
1931 case Template::VALUE_RANGE:
1932 /* Should be canonical before */
1933 break;
a38c6d4c 1934 case Template::ALL_FROM:
1935 case Template::VALUE_LIST_ALL_FROM:
1936 break;
970ed795
EL
1937 case Template::SUPERSET_MATCH:
1938 case Template::SUBSET_MATCH:
1939 if (subtype!=ST_SETOF){
1940 templ->error("'subset' template matching mechanism can be used "
1941 "only with 'set of' types");
1942 return;
1943 }
1944 for (size_t i=0;i<templ->get_nof_comps();i++)
1945 chk_this_template_generic(templ->get_temp_byIndex(i));
1946 break;
1947 case Template::BSTR_PATTERN:
1948 chk_this_template_pattern("bitstring", templ);
1949 break;
1950 case Template::HSTR_PATTERN:
1951 chk_this_template_pattern("hexstring", templ);
1952 break;
1953 case Template::OSTR_PATTERN:
1954 chk_this_template_pattern("octetstring", templ);
1955 break;
1956 case Template::CSTR_PATTERN:
1957 chk_this_template_pattern("charstring", templ);
1958 break;
1959 case Template::USTR_PATTERN:
1960 chk_this_template_pattern("universal charstring", templ);
1961 break;
1962 case Template::TEMPLATE_ERROR:
1963 break;
1964 default:
1965 FATAL_ERROR("SubType::chk_this_template()");
1966 break;
1967 }
1968}
1969
1970void SubType::chk_this_template_length_restriction(Template *templ)
1971{
1972 if (!templ->is_length_restricted()) return;
1973 // if there is a length restriction on the template then check if
1974 // the intersection of the two restrictions is not empty
1975 size_limit_t tmpl_min_len(size_limit_t(0));
1976 size_limit_t tmpl_max_len(size_limit_t::INFINITE_SIZE);
1977 Ttcn::LengthRestriction *lr=templ->get_length_restriction();
1978 lr->chk(Type::EXPECTED_DYNAMIC_VALUE);
1979 if (!lr->get_is_range()) { //Template's lr is single
1980 Value *tmp_val=lr->get_single_value();
1981 if (tmp_val->get_valuetype()!=Value::V_INT) return;
1982 Int templ_len = tmp_val->get_val_Int()->get_val();
1983 tmpl_min_len = tmpl_max_len = size_limit_t((size_t)templ_len);
1984 } else { //Template's lr is range
1985 Value *tmp_lower=lr->get_lower_value();
1986 if (tmp_lower->get_valuetype()!=Value::V_INT) return;
1987 Int templ_lower = tmp_lower->get_val_Int()->get_val();
1988 tmpl_min_len = size_limit_t((size_t)templ_lower);
1989 Value *tmp_upper=lr->get_upper_value();
1990 if (tmp_upper && tmp_upper->get_valuetype()!=Value::V_INT) return;
1991 if (tmp_upper) tmpl_max_len = size_limit_t((size_t)tmp_upper->get_val_Int()->get_val());
1992 }
1993
1994 bool is_err = false;
1995 switch (subtype) {
1996 case ST_BITSTRING:
1997 if (bitstring_st!=NULL) {
1998 BitstringConstraint bc = *bitstring_st * BitstringConstraint(tmpl_min_len,tmpl_max_len);
1999 if (bc.is_empty()==TTRUE) is_err = true;
2000 }
2001 break;
2002 case ST_HEXSTRING:
2003 if (hexstring_st!=NULL) {
2004 HexstringConstraint hc = *hexstring_st * HexstringConstraint(tmpl_min_len,tmpl_max_len);
2005 if (hc.is_empty()==TTRUE) is_err = true;
2006 }
2007 break;
2008 case ST_OCTETSTRING:
2009 if (octetstring_st!=NULL) {
2010 OctetstringConstraint oc = *octetstring_st * OctetstringConstraint(tmpl_min_len,tmpl_max_len);
2011 if (oc.is_empty()==TTRUE) is_err = true;
2012 }
2013 break;
2014 case ST_CHARSTRING:
2015 if (charstring_st!=NULL) {
2016 CharstringSubtypeTreeElement* cc = new CharstringSubtypeTreeElement(
2017 CharstringSubtypeTreeElement::ET_INTERSECTION,
2018 new CharstringSubtypeTreeElement(*charstring_st),
2019 new CharstringSubtypeTreeElement(SizeRangeListConstraint(tmpl_min_len,tmpl_max_len))
2020 );
2021 if (cc->is_empty()==TTRUE) is_err = true;
2022 delete cc;
2023 }
2024 break;
2025 case ST_UNIVERSAL_CHARSTRING:
2026 if (universal_charstring_st!=NULL) {
2027 UniversalCharstringSubtypeTreeElement* ucc = new UniversalCharstringSubtypeTreeElement(
2028 UniversalCharstringSubtypeTreeElement::ET_INTERSECTION,
2029 new UniversalCharstringSubtypeTreeElement(*universal_charstring_st),
2030 new UniversalCharstringSubtypeTreeElement(SizeRangeListConstraint(tmpl_min_len,tmpl_max_len))
2031 );
2032 if (ucc->is_empty()==TTRUE) is_err = true;
2033 delete ucc;
2034 }
2035 break;
2036 case ST_RECORDOF:
2037 case ST_SETOF:
2038 if (recof_st!=NULL) {
2039 RecofConstraint rc = *recof_st * RecofConstraint(tmpl_min_len,tmpl_max_len);
2040 if (rc.is_empty()==TTRUE) is_err = true;
2041 }
2042 break;
2043 default:
2044 break;
2045 }
2046 if (is_err) {
2047 templ->error("Template's length restriction %s is outside of the type's subtype constraint %s",
2048 SizeRangeListConstraint(tmpl_min_len,tmpl_max_len).to_string().c_str(), to_string().c_str());
2049 }
2050}
2051
2052void SubType::chk_this_template_pattern(const char *patt_type, Template *templ)
2053{
2054 Template::templatetype_t temptype= templ->get_templatetype();
2055 if ((temptype==Template::BSTR_PATTERN && subtype!=ST_BITSTRING) ||
2056 (temptype==Template::HSTR_PATTERN && subtype!=ST_HEXSTRING) ||
2057 (temptype==Template::OSTR_PATTERN && subtype!=ST_OCTETSTRING) ||
2058 (temptype==Template::CSTR_PATTERN && subtype!=ST_CHARSTRING) ||
2059 (temptype==Template::USTR_PATTERN && subtype!=ST_UNIVERSAL_CHARSTRING))
2060 {
2061 templ->error("Template is incompatible with subtype");
2062 return;
2063 }
2064 if ( (length_restriction!=NULL) && !length_restriction->is_empty() ) {
2065 Int patt_min_len = static_cast<Int>(templ->get_min_length_of_pattern());
2066 if (patt_min_len < (Int)(length_restriction->get_minimal().get_size()) &&
2067 !templ->pattern_contains_anyornone_symbol()) {
2068 templ->error("At least %s string elements must be present in the %s",
2069 Int2string((Int)(length_restriction->get_minimal().get_size())).c_str(), patt_type);
2070 } else if ( !length_restriction->get_maximal().get_infinity() && (patt_min_len > (Int)(length_restriction->get_maximal().get_size())) ) {
2071 templ->error("There must not be more than %s string elements in the %s",
2072 Int2string((Int)(length_restriction->get_maximal().get_size())).c_str(), patt_type);
2073 }
2074 }
2075}
2076
2077void SubType::add_ttcn_value(Value *v)
2078{
2079 if (value_st==NULL) value_st = new ValueListConstraint(v);
2080 else *value_st = *value_st + ValueListConstraint(v);
2081}
2082
2083void SubType::add_ttcn_recof(Value *v)
2084{
2085 if (recof_st==NULL) recof_st = new RecofConstraint(v);
2086 else *recof_st = *recof_st + RecofConstraint(v);
2087}
2088
2089bool SubType::add_ttcn_type_list_subtype(SubType* p_st)
2090{
2091 switch (subtype) {
2092 case ST_INTEGER:
2093 if (p_st->integer_st==NULL) return false;
2094 if (integer_st==NULL) integer_st = new IntegerRangeListConstraint(*(p_st->integer_st));
2095 else *integer_st = *integer_st + *(p_st->integer_st);
2096 break;
2097 case ST_FLOAT:
2098 if (p_st->float_st==NULL) return false;
2099 if (float_st==NULL) float_st = new RealRangeListConstraint(*(p_st->float_st));
2100 else *float_st = *float_st + *(p_st->float_st);
2101 break;
2102 case ST_BOOLEAN:
2103 if (p_st->boolean_st==NULL) return false;
2104 if (boolean_st==NULL) boolean_st = new BooleanListConstraint(*(p_st->boolean_st));
2105 else *boolean_st = *boolean_st + *(p_st->boolean_st);
2106 break;
2107 case ST_VERDICTTYPE:
2108 if (p_st->verdict_st==NULL) return false;
2109 if (verdict_st==NULL) verdict_st = new VerdicttypeListConstraint(*(p_st->verdict_st));
2110 else *verdict_st = *verdict_st + *(p_st->verdict_st);
2111 break;
2112 case ST_BITSTRING:
2113 if (p_st->bitstring_st==NULL) return false;
2114 if (bitstring_st==NULL) bitstring_st = new BitstringConstraint(*(p_st->bitstring_st));
2115 else *bitstring_st = *bitstring_st + *(p_st->bitstring_st);
2116 break;
2117 case ST_HEXSTRING:
2118 if (p_st->hexstring_st==NULL) return false;
2119 if (hexstring_st==NULL) hexstring_st = new HexstringConstraint(*(p_st->hexstring_st));
2120 else *hexstring_st = *hexstring_st + *(p_st->hexstring_st);
2121 break;
2122 case ST_OCTETSTRING:
2123 if (p_st->octetstring_st==NULL) return false;
2124 if (octetstring_st==NULL) octetstring_st = new OctetstringConstraint(*(p_st->octetstring_st));
2125 else *octetstring_st = *octetstring_st + *(p_st->octetstring_st);
2126 break;
2127 case ST_CHARSTRING:
2128 if (p_st->charstring_st==NULL) return false;
2129 if (charstring_st==NULL) {
2130 charstring_st = new CharstringSubtypeTreeElement(*(p_st->charstring_st));
2131 } else {
2132 charstring_st = new CharstringSubtypeTreeElement(
2133 CharstringSubtypeTreeElement::ET_UNION,
2134 charstring_st,
2135 new CharstringSubtypeTreeElement(*(p_st->charstring_st)));
2136 }
2137 break;
2138 case ST_UNIVERSAL_CHARSTRING:
2139 if (p_st->universal_charstring_st==NULL) return false;
2140 if (universal_charstring_st==NULL) {
2141 universal_charstring_st = new UniversalCharstringSubtypeTreeElement(*(p_st->universal_charstring_st));
2142 } else {
2143 universal_charstring_st = new UniversalCharstringSubtypeTreeElement(
2144 UniversalCharstringSubtypeTreeElement::ET_UNION,
2145 universal_charstring_st,
2146 new UniversalCharstringSubtypeTreeElement(*(p_st->universal_charstring_st)));
2147 }
2148 break;
2149 case ST_OBJID:
2150 case ST_RECORD:
2151 case ST_SET:
2152 case ST_ENUM:
2153 case ST_UNION:
2154 case ST_FUNCTION:
2155 case ST_ALTSTEP:
2156 case ST_TESTCASE:
2157 if (p_st->value_st==NULL) return false;
2158 if (value_st==NULL) value_st = new ValueListConstraint(*(p_st->value_st));
2159 else *value_st = *value_st + *(p_st->value_st);
2160 break;
2161 case ST_RECORDOF:
2162 case ST_SETOF:
2163 if (p_st->recof_st==NULL) return false;
2164 if (recof_st==NULL) recof_st = new RecofConstraint(*(p_st->recof_st));
2165 else *recof_st = *recof_st + *(p_st->recof_st);
2166 break;
2167 default:
2168 FATAL_ERROR("SubType::add_ttcn_type_list_subtype()");
2169 }
2170 return true;
2171}
2172
2173
2174bool SubType::add_parent_subtype(SubType* st)
2175{
2176 if (st==NULL) FATAL_ERROR("SubType::add_parent_subtype()");
2177 if (my_parents.has_key(st)) return true; // it was already successfully added -> ignore
2178 ReferenceChain refch(my_owner, "While checking circular type references in subtype definitions");
2179 refch.add(my_owner->get_fullname()); // current type
2180 // recursive check for all parents of referenced type
2181 if (!st->chk_recursion(refch)) return false;
2182 // if no recursion was detected then add the referenced type as parent
2183 my_parents.add(st,NULL);
2184 return true;
2185}
2186
2187bool SubType::chk_recursion(ReferenceChain& refch)
2188{
2189 if (!refch.add(my_owner->get_fullname())) return false; // try the referenced type
2190 for (size_t i = 0; i < my_parents.size(); i++) {
2191 refch.mark_state();
2192 if (!my_parents.get_nth_key(i)->chk_recursion(refch)) return false;
2193 refch.prev_state();
2194 }
2195 return true;
2196}
2197
2198bool SubType::add_ttcn_single(Value *val, size_t restriction_index)
2199{
2200 val->set_my_scope(my_owner->get_my_scope());
2201 val->set_my_governor(my_owner);
2202 val->set_fullname(my_owner->get_fullname()+".<single_restriction_"+Int2string(restriction_index) + ">");
2203 my_owner->chk_this_value_ref(val);
2204
2205 // check if this is type reference, if not then fall through
2206 if (val->get_valuetype()==Value::V_REFD) {
2207 Reference* ref = val->get_reference();
2208 Assignment *ass = ref->get_refd_assignment();
2209 if (ass==NULL) return false; // defintion was not found, error was reported
2210 if (ass->get_asstype()==Assignment::A_TYPE) {
2211 Type* t = ass->get_Type();
2212 t->chk();
2213 if (t->get_typetype()==Type::T_ERROR) return false;
2214 // if there were subreferences then get the referenced field's type
2215 if (ref->get_subrefs()) {
2216 t = t->get_field_type(ref->get_subrefs(), Type::EXPECTED_CONSTANT);
2217 if ( (t==NULL) || (t->get_typetype()==Type::T_ERROR) ) return false;
2218 t->chk();
2219 if (t->get_typetype()==Type::T_ERROR) return false;
2220 }
2221 if (!t->is_identical(my_owner)) {
2222 val->error("Reference `%s' must refer to a type which has the same root type as this type",
2223 val->get_reference()->get_dispname().c_str());
2224 return false;
2225 }
2226 // check subtype of referenced type
2227 SubType* t_st = t->get_sub_type();
2228 if (t_st==NULL) {
2229 val->error("Type referenced by `%s' does not have a subtype",
2230 val->get_reference()->get_dispname().c_str());
2231 return false;
2232 }
2233
2234 // check circular subtype reference
2235 if (!add_parent_subtype(t_st)) return false;
2236
2237 if (t_st->get_subtypetype()==ST_ERROR) return false;
2238 if (t_st->get_subtypetype()!=subtype) FATAL_ERROR("SubType::add_ttcn_single()");
2239 // add the subtype as union
2240 bool added = add_ttcn_type_list_subtype(t_st);
2241 if (!added) {
2242 val->error("Type referenced by `%s' does not have a subtype",
2243 val->get_reference()->get_dispname().c_str());
2244 }
2245 return added;
2246 }
2247 }
2248
2249 my_owner->chk_this_value(val, 0, Type::EXPECTED_CONSTANT,
2250 INCOMPLETE_NOT_ALLOWED, OMIT_NOT_ALLOWED, NO_SUB_CHK);
2251
2252 Value *v=val->get_value_refd_last();
2253
2254 switch (v->get_valuetype()) {
2255 case Value::V_INT:
2256 if (subtype!=ST_INTEGER) FATAL_ERROR("SubType::add_ttcn_single()");
2257 if (integer_st==NULL) integer_st = new IntegerRangeListConstraint(int_limit_t(*(v->get_val_Int())));
2258 else *integer_st = *integer_st + IntegerRangeListConstraint(int_limit_t(*(v->get_val_Int())));
2259 break;
2260 case Value::V_REAL: {
2261 if (subtype!=ST_FLOAT) FATAL_ERROR("SubType::add_ttcn_single()");
2262 ttcn3float r = v->get_val_Real();
2263 if (r!=r) {
2264 if (float_st==NULL) float_st = new RealRangeListConstraint(true);
2265 else *float_st = *float_st + RealRangeListConstraint(true);
2266 } else {
2267 if (float_st==NULL) float_st = new RealRangeListConstraint(real_limit_t(r));
2268 else *float_st = *float_st + RealRangeListConstraint(real_limit_t(r));
2269 }
2270 } break;
2271 case Value::V_BOOL:
2272 if (subtype!=ST_BOOLEAN) FATAL_ERROR("SubType::add_ttcn_single()");
2273 if (boolean_st==NULL) boolean_st = new BooleanListConstraint(v->get_val_bool());
2274 else *boolean_st = *boolean_st + BooleanListConstraint(v->get_val_bool());
2275 break;
2276 case Value::V_VERDICT: {
2277 if (subtype!=ST_VERDICTTYPE) FATAL_ERROR("SubType::add_ttcn_single()");
2278 VerdicttypeListConstraint::verdicttype_constraint_t vtc;
2279 switch (v->get_val_verdict()) {
2280 case Value::Verdict_NONE: vtc = VerdicttypeListConstraint::VC_NONE; break;
2281 case Value::Verdict_PASS: vtc = VerdicttypeListConstraint::VC_PASS; break;
2282 case Value::Verdict_INCONC: vtc = VerdicttypeListConstraint::VC_INCONC; break;
2283 case Value::Verdict_FAIL: vtc = VerdicttypeListConstraint::VC_FAIL; break;
2284 case Value::Verdict_ERROR: vtc = VerdicttypeListConstraint::VC_ERROR; break;
2285 default: FATAL_ERROR("SubType::add_ttcn_single()");
2286 }
2287 if (verdict_st==NULL) verdict_st = new VerdicttypeListConstraint(vtc);
2288 else *verdict_st = *verdict_st + VerdicttypeListConstraint(vtc);
2289 } break;
2290 case Value::V_OID:
2291 if (v->has_oid_error()) return false;
2292 if (subtype!=ST_OBJID) FATAL_ERROR("SubType::add_ttcn_single()");
2293 if (value_st==NULL) value_st = new ValueListConstraint(v);
2294 else *value_st = *value_st + ValueListConstraint(v);
2295 break;
2296 case Value::V_BSTR:
2297 if (subtype!=ST_BITSTRING) FATAL_ERROR("SubType::add_ttcn_single()");
2298 if (bitstring_st==NULL) bitstring_st = new BitstringConstraint(v->get_val_str());
2299 else *bitstring_st = *bitstring_st + BitstringConstraint(v->get_val_str());
2300 break;
2301 case Value::V_HSTR:
2302 if (subtype!=ST_HEXSTRING) FATAL_ERROR("SubType::add_ttcn_single()");
2303 if (hexstring_st==NULL) hexstring_st = new HexstringConstraint(v->get_val_str());
2304 else *hexstring_st = *hexstring_st + HexstringConstraint(v->get_val_str());
2305 break;
2306 case Value::V_OSTR:
2307 if (subtype!=ST_OCTETSTRING) FATAL_ERROR("SubType::add_ttcn_single()");
2308 if (octetstring_st==NULL) octetstring_st = new OctetstringConstraint(v->get_val_str());
2309 else *octetstring_st = *octetstring_st + OctetstringConstraint(v->get_val_str());
2310 break;
2311 case Value::V_CSTR: {
2312 if (subtype!=ST_CHARSTRING) FATAL_ERROR("SubType::add_ttcn_single()");
2313 CharstringSubtypeTreeElement* cst_elem = new CharstringSubtypeTreeElement(StringValueConstraint<string>(v->get_val_str()));
2314 if (charstring_st==NULL) charstring_st = cst_elem;
2315 else charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_UNION, charstring_st, cst_elem);
2316 } break;
2317 case Value::V_USTR: {
2318 if (subtype!=ST_UNIVERSAL_CHARSTRING) FATAL_ERROR("SubType::add_ttcn_single()");
2319 UniversalCharstringSubtypeTreeElement* ucst_elem = new UniversalCharstringSubtypeTreeElement(StringValueConstraint<ustring>(v->get_val_ustr()));
2320 if (universal_charstring_st==NULL) universal_charstring_st = ucst_elem;
2321 else universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_UNION, universal_charstring_st, ucst_elem);
2322 } break;
2323 case Value::V_ENUM:
2324 case Value::V_NULL: // FIXME: should go to ST_NULL
2325 if (subtype!=ST_ENUM) FATAL_ERROR("SubType::add_ttcn_single()");
2326 add_ttcn_value(v);
2327 break;
2328 case Value::V_CHOICE:
2329 if (subtype!=ST_UNION) FATAL_ERROR("SubType::add_ttcn_single()");
2330 add_ttcn_value(v);
2331 break;
2332 case Value::V_SEQ:
2333 if (subtype!=ST_RECORD) FATAL_ERROR("SubType::add_ttcn_single()");
2334 add_ttcn_value(v);
2335 break;
2336 case Value::V_SET:
2337 if (subtype!=ST_SET) FATAL_ERROR("SubType::add_ttcn_single()");
2338 add_ttcn_value(v);
2339 break;
2340 case Value::V_FUNCTION:
2341 if (subtype!=ST_FUNCTION) FATAL_ERROR("SubType::add_ttcn_single()");
2342 add_ttcn_value(v);
2343 break;
2344 case Value::V_ALTSTEP:
2345 if (subtype!=ST_ALTSTEP) FATAL_ERROR("SubType::add_ttcn_single()");
2346 add_ttcn_value(v);
2347 break;
2348 case Value::V_TESTCASE:
2349 if (subtype!=ST_TESTCASE) FATAL_ERROR("SubType::add_ttcn_single()");
2350 add_ttcn_value(v);
2351 break;
2352 case Value::V_SEQOF:
2353 if (subtype!=ST_RECORDOF) FATAL_ERROR("SubType::add_ttcn_single()");
2354 add_ttcn_recof(v);
2355 break;
2356 case Value::V_SETOF:
2357 if (subtype!=ST_SETOF) FATAL_ERROR("SubType::add_ttcn_single()");
2358 add_ttcn_recof(v);
2359 break;
2360 case Value::V_ERROR:
2361 return false;
2362 default:
2363 return false;
2364 }
2365 return true;
2366}
2367
2368bool SubType::add_ttcn_range(Value *min, bool min_exclusive,
2369 Value *max, bool max_exclusive, size_t restriction_index, bool has_other)
2370{
2371 switch (subtype) {
2372 case ST_INTEGER:
2373 case ST_FLOAT:
2374 case ST_CHARSTRING:
2375 case ST_UNIVERSAL_CHARSTRING:
2376 break;
2377 default:
2378 my_owner->error("Range subtyping is not allowed for type `%s'",
2379 my_owner->get_typename().c_str());
2380 return false;
2381 }
2382
2383 Value *vmin,*vmax;
2384 if (min==NULL) vmin=NULL;
2385 else {
2386 min->set_my_scope(my_owner->get_my_scope());
2387 min->set_fullname(my_owner->get_fullname()+".<range_restriction_"+Int2string(restriction_index)+"_lower>");
2388 my_owner->chk_this_value_ref(min);
2389 my_owner->chk_this_value(min, 0, Type::EXPECTED_CONSTANT,
2390 INCOMPLETE_NOT_ALLOWED, OMIT_NOT_ALLOWED, NO_SUB_CHK);
2391 vmin=min->get_value_refd_last();
2392 }
2393 if (max==NULL) vmax=NULL;
2394 else {
2395 max->set_my_scope(my_owner->get_my_scope());
2396 max->set_fullname(my_owner->get_fullname()+".<range_restriction_"+Int2string(restriction_index)+"_upper>");
2397 my_owner->chk_this_value_ref(max);
2398 my_owner->chk_this_value(max, 0, Type::EXPECTED_CONSTANT,
2399 INCOMPLETE_NOT_ALLOWED, OMIT_NOT_ALLOWED, NO_SUB_CHK);
2400 vmax=max->get_value_refd_last();
2401 }
2402
2403 if ( (vmin!=NULL) && (vmax!=NULL) && (vmin->get_valuetype()!=vmax->get_valuetype()) ) return false;
2404
2405 switch (subtype) {
2406 case ST_INTEGER: {
2407 if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_INT)) ||
2408 ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_INT))) return false;
2409 int_limit_t min_limit = (vmin!=NULL) ? int_limit_t(*(vmin->get_val_Int())) : int_limit_t::minimum;
2410 if (min_exclusive) {
2411 if (min_limit==int_limit_t::minimum) {
2412 my_owner->error("invalid lower boundary, -infinity cannot be excluded from an integer subtype range");
2413 return false;
2414 } else {
2415 if (min_limit==int_limit_t::maximum) {
2416 my_owner->error("!infinity is not a valid lower boundary");
2417 return false;
2418 }
2419 min_limit = min_limit.next();
2420 }
2421 }
2422 int_limit_t max_limit = (vmax!=NULL) ? int_limit_t(*(vmax->get_val_Int())) : int_limit_t::maximum;
2423 if (max_exclusive) {
2424 if (max_limit==int_limit_t::maximum) {
2425 my_owner->error("invalid upper boundary, infinity cannot be excluded from an integer subtype range");
2426 return false;
2427 } else {
2428 if (max_limit==int_limit_t::minimum) {
2429 my_owner->error("!-infinity is not a valid upper boundary");
2430 return false;
2431 }
2432 max_limit = max_limit.previous();
2433 }
2434 }
2435 if (max_limit<min_limit) {
2436 my_owner->error("lower boundary is bigger than upper boundary in integer subtype range");
2437 return false;
2438 }
2439 if (integer_st==NULL) integer_st = new IntegerRangeListConstraint(min_limit, max_limit);
2440 else *integer_st = *integer_st + IntegerRangeListConstraint(min_limit, max_limit);
2441 } break;
2442 case ST_FLOAT: {
2443 if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_REAL)) ||
2444 ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_REAL))) return false;
2445 if ((vmin!=NULL) && (vmin->get_val_Real()!=vmin->get_val_Real())) {
2446 my_owner->error("lower boundary cannot be not_a_number in float subtype range");
2447 return false;
2448 }
2449 if ((vmax!=NULL) && (vmax->get_val_Real()!=vmax->get_val_Real())) {
2450 my_owner->error("upper boundary cannot be not_a_number in float subtype range");
2451 return false;
2452 }
2453 real_limit_t min_limit = (vmin!=NULL) ? real_limit_t(vmin->get_val_Real()) : real_limit_t::minimum;
2454 if (min_exclusive) {
2455 if (min_limit==real_limit_t::maximum) {
2456 my_owner->error("!infinity is not a valid lower boundary");
2457 return false;
2458 }
2459 min_limit = min_limit.next();
2460 }
2461 real_limit_t max_limit = (vmax!=NULL) ? real_limit_t(vmax->get_val_Real()) : real_limit_t::maximum;
2462 if (max_exclusive) {
2463 if (max_limit==real_limit_t::minimum) {
2464 my_owner->error("!-infinity is not a valid upper boundary");
2465 return false;
2466 }
2467 max_limit = max_limit.previous();
2468 }
2469 if (max_limit<min_limit) {
2470 my_owner->error("lower boundary is bigger than upper boundary in float subtype range");
2471 return false;
2472 }
2473 if (float_st==NULL) float_st = new RealRangeListConstraint(min_limit, max_limit);
2474 else *float_st = *float_st + RealRangeListConstraint(min_limit, max_limit);
2475 } break;
2476 case ST_CHARSTRING: {
2477 if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_CSTR)) ||
2478 ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_CSTR))) return false;
2479 if ((vmin==NULL)&&(vmax==NULL)) {
2480 my_owner->error("a range subtype of a charstring cannot be (-infinity..infinity)");
2481 return false;
2482 }
2483 if (vmin==NULL) {
2484 my_owner->error("lower boundary of a charstring subtype range cannot be -infinity");
2485 return false;
2486 }
2487 if (vmax==NULL) {
2488 my_owner->error("upper boundary of a charstring subtype range cannot be infinity");
2489 return false;
2490 }
2491 if (vmin->get_val_str().size()!=1) {
2492 min->error("lower boundary of charstring subtype range must be a single element string");
2493 return false;
2494 }
2495 if (vmax->get_val_str().size()!=1) {
2496 max->error("upper boundary of charstring subtype range must be a single element string");
2497 return false;
2498 }
2499 if (!char_limit_t::is_valid_value(*vmin->get_val_str().c_str())) {
2500 min->error("lower boundary of charstring subtype range is an invalid char");
2501 return false;
2502 }
2503 if (!char_limit_t::is_valid_value(*vmax->get_val_str().c_str())) {
2504 max->error("upper boundary of charstring subtype range is an invalid char");
2505 return false;
2506 }
2507 char_limit_t min_limit(*vmin->get_val_str().c_str()), max_limit(*vmax->get_val_str().c_str());
2508 if (min_exclusive) {
2509 if (min_limit==char_limit_t::maximum) {
2510 min->error("exclusive lower boundary is not a legal charstring character");
2511 return false;
2512 }
2513 min_limit = min_limit.next();
2514 }
2515 if (max_exclusive) {
2516 if (max_limit==char_limit_t::minimum) {
2517 max->error("exclusive upper boundary is not a legal charstring character");
2518 return false;
2519 }
2520 max_limit = max_limit.previous();
2521 }
2522 if (max_limit<min_limit) {
2523 my_owner->error("lower boundary is bigger than upper boundary in charstring subtype range");
2524 return false;
2525 }
2526 if (charstring_st==NULL) charstring_st = new CharstringSubtypeTreeElement(CharRangeListConstraint(min_limit,max_limit), false);
2527 else {
2528 if (!has_other) { // union in char context can be done only with range constraints
2529 charstring_st->set_char_context(true);
2530 charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_UNION,
2531 charstring_st,
2532 new CharstringSubtypeTreeElement(CharRangeListConstraint(min_limit,max_limit), true));
2533 charstring_st->set_char_context(false);
2534 } else {
2535 // ignore it, error reported elsewhere
2536 return false;
2537 }
2538 }
2539 } break;
2540 case ST_UNIVERSAL_CHARSTRING: {
2541 if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_USTR)) ||
2542 ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_USTR))) return false;
2543 if ((vmin==NULL)&&(vmax==NULL)) {
2544 my_owner->error("a range subtype of a universal charstring cannot be (-infinity..infinity)");
2545 return false;
2546 }
2547 if (vmin==NULL) {
2548 my_owner->error("lower boundary of a universal charstring subtype range cannot be -infinity");
2549 return false;
2550 }
2551 if (vmax==NULL) {
2552 my_owner->error("upper boundary of a universal charstring subtype range cannot be infinity");
2553 return false;
2554 }
2555 if (vmin->get_val_ustr().size()!=1) {
2556 min->error("lower boundary of universal charstring subtype range must be a single element string");
2557 return false;
2558 }
2559 if (vmax->get_val_ustr().size()!=1) {
2560 max->error("upper boundary of universal charstring subtype range must be a single element string");
2561 return false;
2562 }
2563 if (!universal_char_limit_t::is_valid_value(*vmin->get_val_ustr().u_str())) {
2564 min->error("lower boundary of universal charstring subtype range is an invalid char");
2565 return false;
2566 }
2567 if (!universal_char_limit_t::is_valid_value(*vmax->get_val_ustr().u_str())) {
2568 max->error("upper boundary of universal charstring subtype range is an invalid char");
2569 return false;
2570 }
2571 universal_char_limit_t min_limit(*vmin->get_val_ustr().u_str()), max_limit(*vmax->get_val_ustr().u_str());
2572 if (min_exclusive) {
2573 if (min_limit==universal_char_limit_t::maximum) {
2574 min->error("exclusive lower boundary is not a legal universal charstring character");
2575 return false;
2576 }
2577 min_limit = min_limit.next();
2578 }
2579 if (max_exclusive) {
2580 if (max_limit==universal_char_limit_t::minimum) {
2581 max->error("exclusive upper boundary is not a legal universal charstring character");
2582 return false;
2583 }
2584 max_limit = max_limit.previous();
2585 }
2586 if (max_limit<min_limit) {
2587 my_owner->error("lower boundary is bigger than upper boundary in universal charstring subtype range");
2588 return false;
2589 }
2590
2591 if (universal_charstring_st==NULL) universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharRangeListConstraint(min_limit,max_limit), false);
2592 else {
2593 if (!has_other) { // union in char context can be done only with range constraints
2594 universal_charstring_st->set_char_context(true);
2595 universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_UNION,
2596 universal_charstring_st,
2597 new UniversalCharstringSubtypeTreeElement(UniversalCharRangeListConstraint(min_limit,max_limit), true));
2598 universal_charstring_st->set_char_context(false);
2599 } else {
2600 // ignore it, error reported elsewhere
2601 return false;
2602 }
2603 }
2604 } break;
2605 default:
2606 FATAL_ERROR("SubType::add_ttcn_range()");
2607 }
2608 return true;
2609}
2610
2611bool SubType::set_ttcn_length(const size_limit_t& min, const size_limit_t& max)
2612{
2613 switch (subtype) {
2614 case ST_BITSTRING: {
2615 if (bitstring_st==NULL) bitstring_st = new BitstringConstraint(min,max);
2616 else *bitstring_st = *bitstring_st * BitstringConstraint(min,max);
2617 } break;
2618 case ST_HEXSTRING: {
2619 if (hexstring_st==NULL) hexstring_st = new HexstringConstraint(min,max);
2620 else *hexstring_st = *hexstring_st * HexstringConstraint(min,max);
2621 } break;
2622 case ST_OCTETSTRING: {
2623 if (octetstring_st==NULL) octetstring_st = new OctetstringConstraint(min,max);
2624 else *octetstring_st = *octetstring_st * OctetstringConstraint(min,max);
2625 } break;
2626 case ST_CHARSTRING: {
2627 CharstringSubtypeTreeElement* cst_elem = new CharstringSubtypeTreeElement(SizeRangeListConstraint(min,max));
2628 if (charstring_st==NULL) {
2629 charstring_st = cst_elem;
2630 } else {
2631 charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_INTERSECTION, charstring_st, cst_elem);
2632 }
2633 } break;
2634 case ST_UNIVERSAL_CHARSTRING: {
2635 UniversalCharstringSubtypeTreeElement* ucst_elem = new UniversalCharstringSubtypeTreeElement(SizeRangeListConstraint(min,max));
2636 if (universal_charstring_st==NULL) {
2637 universal_charstring_st = ucst_elem;
2638 } else {
2639 universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_INTERSECTION, universal_charstring_st, ucst_elem);
2640 }
2641 } break;
2642 case ST_RECORDOF:
2643 case ST_SETOF: {
2644 if (recof_st==NULL) recof_st = new RecofConstraint(min,max);
2645 else *recof_st = *recof_st * RecofConstraint(min,max);
2646 } break;
2647 default:
2648 my_owner->error("Length subtyping is not allowed for type `%s'",
2649 my_owner->get_typename().c_str());
2650 return false;
2651 }
2652 if (length_restriction==NULL) length_restriction = new SizeRangeListConstraint(min,max);
2653 else *length_restriction = *length_restriction * SizeRangeListConstraint(min,max);
2654 return true;
2655}
2656
2657void SubType::chk_boundary_valid(Value* boundary, Int max_value, const char* boundary_name)
2658{
2659 const int_val_t *int_val = boundary->get_val_Int();
2660 if (*int_val > int_val_t(max_value)) {
2661 boundary->error("The %s should be less than `%s' instead of `%s'",
2662 boundary_name,
2663 int_val_t(max_value).t_str().c_str(),
2664 int_val->t_str().c_str());
2665 boundary->set_valuetype(Value::V_ERROR);
2666 }
2667}
2668
2669bool SubType::add_ttcn_length(Ttcn::LengthRestriction *lr, size_t restriction_index)
2670{
2671 string s;
2672 lr->append_stringRepr(s);
2673 Value *lower=NULL,*upper=NULL;
2674 lr->set_my_scope(my_owner->get_my_scope());
2675 lr->set_fullname(my_owner->get_fullname()+".<length_restriction_"+Int2string(restriction_index)+">");
2676 lr->chk(Type::EXPECTED_CONSTANT);
2677 lower = lr->get_is_range() ? lr->get_lower_value() : lr->get_single_value();
2678 if (!lower->get_my_scope()) FATAL_ERROR("no scope");
2679 if (lower->get_valuetype() != Value::V_INT) return false;
2680 if (lr->get_is_range()) {
2681 upper = lr->get_upper_value();
2682 if (upper) {//HAS_UPPER
2683 if (upper->get_valuetype()!=Value::V_INT) return false;
2684 if (!upper->get_my_scope()) upper->set_my_scope(my_owner->get_my_scope());
2685 chk_boundary_valid(upper, INT_MAX, "upper boundary");
2686 if (upper->get_valuetype()!=Value::V_INT) return false;
2687 return set_ttcn_length(size_limit_t((size_t)lower->get_val_Int()->get_val()),
2688 size_limit_t((size_t)upper->get_val_Int()->get_val()));
2689 } else {//INFINITY:
2690 chk_boundary_valid(lower, INT_MAX, "lower boundary");
2691 if (lower->get_valuetype()!=Value::V_INT) return false;
2692 return set_ttcn_length(size_limit_t((size_t)lower->get_val_Int()->get_val()),
2693 size_limit_t(size_limit_t::INFINITE_SIZE));
2694 }
2695 }
2696 else {//SINGLE:
2697 chk_boundary_valid(lower, INT_MAX, "length restriction value");
2698 if (lower->get_valuetype()!=Value::V_INT) return false;
2699 return set_ttcn_length(size_limit_t((size_t)lower->get_val_Int()->get_val()),
2700 size_limit_t((size_t)lower->get_val_Int()->get_val()));
2701 }
2702}
2703
2704bool SubType::add_ttcn_pattern(Ttcn::PatternString* pattern, size_t restriction_index)
2705{
2706 pattern->set_my_scope(my_owner->get_my_scope());
2707 pattern->set_fullname(my_owner->get_fullname()+".<pattern_restriction_"+Int2string(restriction_index) + ">");
2708 switch (subtype) {
2709 case ST_CHARSTRING: {
2710 Error_Context cntxt(my_owner, "In character string pattern");
2711 pattern->chk_refs(Type::EXPECTED_CONSTANT);
2712 pattern->join_strings();
2713 if (!pattern->has_refs()) { // if chk_refs didn't remove all references then ignore
2714 pattern->chk_pattern();
2715 CharstringSubtypeTreeElement* cst_elem = new CharstringSubtypeTreeElement(StringPatternConstraint(pattern));
2716 if (charstring_st==NULL) {
2717 charstring_st = cst_elem;
2718 } else {
2719 charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_INTERSECTION, charstring_st, cst_elem);
2720 }
2721 }
2722 } break;
2723 case ST_UNIVERSAL_CHARSTRING: {
2724 Error_Context cntxt(my_owner, "In universal string pattern");
2725 pattern->set_pattern_type(Ttcn::PatternString::USTR_PATTERN);
2726 pattern->chk_refs(Type::EXPECTED_CONSTANT);
2727 pattern->join_strings();
2728 if (!pattern->has_refs()) { // if chk_refs didn't remove all references then ignore
2729 pattern->chk_pattern();
2730 UniversalCharstringSubtypeTreeElement* ucst_elem = new UniversalCharstringSubtypeTreeElement(StringPatternConstraint(pattern));
2731 if (universal_charstring_st==NULL) {
2732 universal_charstring_st = ucst_elem;
2733 } else {
2734 universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_INTERSECTION, universal_charstring_st, ucst_elem);
2735 }
2736 }
2737 } break;
2738 default:
2739 my_owner->error("Pattern subtyping of type `%s' is not allowed", my_owner->get_typename().c_str());
2740 return false;
2741 }
2742 return true;
2743}
2744
2745void SubType::print_full_warning() const
2746{
2747 my_owner->warning("The subtype of type `%s' is a full set, "
2748 "it does not constrain the root type.", my_owner->get_typename().c_str());
2749}
2750
2751void SubType::chk()
2752{
2753 if ((checked!=STC_NO) || (subtype==ST_ERROR)) FATAL_ERROR("SubType::chk()");
2754 checked = STC_CHECKING;
2755
2756 // check for circular subtype reference
2757 if (parent_subtype && !add_parent_subtype(parent_subtype)) {
2758 set_to_error();
2759 checked = STC_YES;
2760 return;
2761 }
2762
2763 if (parsed) { // has TTCN-3 subtype constraint
2764 size_t added_count = 0;
2765 bool has_single = false, has_range = false,
2766 has_length = false, has_pattern = false;
2767 for (size_t i = 0; i < parsed->size(); i++) {
2768 bool added = false;
2769 SubTypeParse *parse = (*parsed)[i];
2770 switch (parse->get_selection()) {
2771 case SubTypeParse::STP_SINGLE:
2772 has_single = true;
2773 added = add_ttcn_single(parse->Single(),i);
2774 break;
2775 case SubTypeParse::STP_RANGE:
2776 has_range = true;
2777 added = add_ttcn_range(parse->Min(), parse->MinExclusive(),
2778 parse->Max(), parse->MaxExclusive(), i,
2779 has_single || has_length || has_pattern);
2780 break;
2781 case SubTypeParse::STP_LENGTH:
2782 has_length = true;
2783 added = add_ttcn_length(parse->Length(),i);
2784 break;
2785 case SubTypeParse::STP_PATTERN:
2786 has_pattern = true;
2787 added = add_ttcn_pattern(parse->Pattern(),i);
2788 break;
2789 default:
2790 FATAL_ERROR("SubType::chk(): invalid SubTypeParse selection");
2791 } // switch
2792 if (added) added_count++;
2793 }//for
2794 switch (subtype) {
2795 case ST_CHARSTRING:
2796 case ST_UNIVERSAL_CHARSTRING:
2797 if (has_single && has_range) {
2798 my_owner->error(
2799 "Mixing of value list and range subtyping is not allowed for type `%s'",
2800 my_owner->get_typename().c_str());
2801 set_to_error();
2802 checked = STC_YES;
2803 return;
2804 }
2805 break;
2806 default:
2807 // in other cases mixing of different restrictions (which are legal for
2808 // this type) is properly regulated by the TTCN-3 BNF itself
2809 break;
2810 }
2811 if (added_count<parsed->size()) {
2812 set_to_error();
2813 checked = STC_YES;
2814 return;
2815 }
2816 if (subtype==ST_ERROR) { checked = STC_YES; return; }
2817
2818 if (parent_subtype) {
2819 if (is_subset(parent_subtype->get_root())==TFALSE) {
2820 my_owner->error("The subtype restriction is not a subset of the restriction on the parent type. "
2821 "Subtype %s is not subset of subtype %s", to_string().c_str(), parent_subtype->get_root()->to_string().c_str());
2822 set_to_error();
2823 checked = STC_YES;
2824 return;
2825 }
2826 intersection(parent_subtype->get_root());
2827 }
2828 } else if (asn_constraints) { // has ASN.1 subtype constraint
2829 SubtypeConstraint* asn_parent_subtype = NULL;
2830 if (parent_subtype) {
2831 // the type constraint of the ASN.1 type is already in the parent_subtype,
2832 // don't add it multiple times
2833 asn_parent_subtype = parent_subtype->get_root();
2834 } else {
2835 asn_parent_subtype = get_asn_type_constraint(my_owner);
2836 }
2837 asn_constraints->chk(asn_parent_subtype);
2838 root = asn_constraints->get_subtype();
2839 extendable = asn_constraints->is_extendable();
2840 extension = asn_constraints->get_extension();
2841 // the TTCN-3 subtype will be the union of the root and extension parts
2842 // the ETSI ES 201 873-7 V4.1.2 (2009-07) document says to "ignore any extension markers"
2843 // but titan now works this way :)
2844 if (root) copy(root);
2845 if (extension) union_(extension);
2846 } else { // no constraints on this type -> this is an alias type, just copy the subtype from the other
2847 if (parent_subtype) {
2848 root = parent_subtype->root;
2849 extendable = parent_subtype->extendable;
2850 extension = parent_subtype->extension;
2851 copy(parent_subtype);
2852 } else {
2853 SubtypeConstraint* asn_parent_subtype = get_asn_type_constraint(my_owner);
2854 if (asn_parent_subtype) copy(asn_parent_subtype);
2855 }
2856 }
2857
2858 // check if subtype is valid: it must not be an empty set (is_empty==TTRUE)
2859 // issue warning if subtype is given but is full set (is_full==TTRUE)
2860 // ignore cases of TUNKNOWN when compiler can't figure out if the aggregate
2861 // set is empty or full
2862 switch (subtype) {
2863 case ST_INTEGER:
2864 if (integer_st!=NULL) {
2865 if (integer_st->is_empty()==TTRUE) goto empty_error;
2866 if (integer_st->is_full()==TTRUE) {
2867 print_full_warning();
2868 delete integer_st;
2869 integer_st = NULL;
2870 }
2871 }
2872 break;
2873 case ST_FLOAT:
2874 if (float_st!=NULL) {
2875 if (float_st->is_empty()==TTRUE) goto empty_error;
2876 if (float_st->is_full()==TTRUE) {
2877 print_full_warning();
2878 delete float_st;
2879 float_st = NULL;
2880 }
2881 }
2882 break;
2883 case ST_BOOLEAN:
2884 if (boolean_st!=NULL) {
2885 if (boolean_st->is_empty()==TTRUE) goto empty_error;
2886 if (boolean_st->is_full()==TTRUE) {
2887 print_full_warning();
2888 delete boolean_st;
2889 boolean_st = NULL;
2890 }
2891 }
2892 break;
2893 case ST_VERDICTTYPE:
2894 if (verdict_st!=NULL) {
2895 if (verdict_st->is_empty()==TTRUE) goto empty_error;
2896 if (verdict_st->is_full()==TTRUE) {
2897 print_full_warning();
2898 delete verdict_st;
2899 verdict_st = NULL;
2900 }
2901 }
2902 break;
2903 case ST_BITSTRING:
2904 if (bitstring_st!=NULL) {
2905 if (bitstring_st->is_empty()==TTRUE) goto empty_error;
2906 if (bitstring_st->is_full()==TTRUE) {
2907 print_full_warning();
2908 delete bitstring_st;
2909 bitstring_st = NULL;
2910 }
2911 }
2912 break;
2913 case ST_HEXSTRING:
2914 if (hexstring_st!=NULL) {
2915 if (hexstring_st->is_empty()==TTRUE) goto empty_error;
2916 if (hexstring_st->is_full()==TTRUE) {
2917 print_full_warning();
2918 delete hexstring_st;
2919 hexstring_st = NULL;
2920 }
2921 }
2922 break;
2923 case ST_OCTETSTRING:
2924 if (octetstring_st!=NULL) {
2925 if (octetstring_st->is_empty()==TTRUE) goto empty_error;
2926 if (octetstring_st->is_full()==TTRUE) {
2927 print_full_warning();
2928 delete octetstring_st;
2929 octetstring_st = NULL;
2930 }
2931 }
2932 break;
2933 case ST_CHARSTRING:
2934 if (charstring_st!=NULL) {
2935 if (charstring_st->is_empty()==TTRUE) goto empty_error;
2936 if (charstring_st->is_full()==TTRUE) {
2937 print_full_warning();
2938 delete charstring_st;
2939 charstring_st = NULL;
2940 }
2941 }
2942 break;
2943 case ST_UNIVERSAL_CHARSTRING:
2944 if (universal_charstring_st!=NULL) {
2945 if (universal_charstring_st->is_empty()==TTRUE) goto empty_error;
2946 if (universal_charstring_st->is_full()==TTRUE) {
2947 print_full_warning();
2948 delete universal_charstring_st;
2949 universal_charstring_st = NULL;
2950 }
2951 }
2952 break;
2953 case ST_OBJID:
2954 case ST_RECORD:
2955 case ST_SET:
2956 case ST_ENUM:
2957 case ST_UNION:
2958 case ST_FUNCTION:
2959 case ST_ALTSTEP:
2960 case ST_TESTCASE:
2961 if (value_st!=NULL) {
2962 if (value_st->is_empty()==TTRUE) goto empty_error;
2963 if (value_st->is_full()==TTRUE) {
2964 print_full_warning();
2965 delete value_st;
2966 value_st = NULL;
2967 }
2968 }
2969 break;
2970 case ST_RECORDOF:
2971 case ST_SETOF:
2972 if (recof_st!=NULL) {
2973 if (recof_st->is_empty()==TTRUE) goto empty_error;
2974 if (recof_st->is_full()==TTRUE) {
2975 print_full_warning();
2976 delete recof_st;
2977 recof_st = NULL;
2978 }
2979 }
2980 break;
2981 default:
2982 FATAL_ERROR("SubType::chk()");
2983 }
2984 if ((length_restriction!=NULL) && (length_restriction->is_full()==TTRUE)) {
2985 delete length_restriction;
2986 length_restriction = NULL;
2987 }
2988 checked = STC_YES;
2989 return;
2990
2991empty_error:
2992 my_owner->error("The subtype is an empty set");
2993 set_to_error();
2994 checked = STC_YES;
2995 return;
2996}
2997
2998void SubType::dump(unsigned level) const
2999{
3000 string str = to_string();
3001 if (str.size()>0) DEBUG(level, "restriction(s): %s", str.c_str());
3002}
3003
3004Int SubType::get_length_restriction() const
3005{
3006 if (checked!=STC_YES) FATAL_ERROR("SubType::get_length_restriction()");
3007 if (parsed==NULL) return -1; // only own length restriction counts
3008 if (length_restriction==NULL) return -1;
3009 if (length_restriction->is_empty()) return -1;
3010 return ( (length_restriction->get_minimal()==length_restriction->get_maximal()) ?
3011 (Int)(length_restriction->get_minimal().get_size()) :
3012 -1 );
3013}
3014
3015bool SubType::zero_length_allowed() const
3016{
3017 if (checked!=STC_YES) FATAL_ERROR("SubType::zero_length_allowed()");
3018 if (parsed==NULL) return true; // only own length restriction counts
3019 if (length_restriction==NULL) return true;
3020 return length_restriction->is_element(size_limit_t(0));
3021}
3022
3023string SubType::to_string() const
3024{
3025 if (root) {
3026 string ret_val(root->to_string());
3027 if (extendable) ret_val += ", ...";
3028 if (extension) {
3029 ret_val += ", ";
3030 ret_val += extension->to_string();
3031 }
3032 return ret_val;
3033 }
3034 return SubtypeConstraint::to_string();
3035}
3036
3037////////////////////////////////////////////////////////////////////////////////
3038
3039void SubType::generate_code(output_struct &)
3040{
3041 if (checked!=STC_YES) FATAL_ERROR("SubType::generate_code()");
3042}
3043
3abe9331 3044void SubType::generate_json_schema(JSON_Tokenizer& json, bool allow_special_float /* = true */)
3045{
3046 bool has_value_list = false;
3047 size_t nof_ranges = 0;
3048 for (size_t i = 0; i < parsed->size(); ++i) {
3049 SubTypeParse *parse = (*parsed)[i];
3050 switch (parse->get_selection()) {
3051 case SubTypeParse::STP_SINGLE:
3052 // single values will be added later, all at once
3053 has_value_list = true;
3054 break;
3055 case SubTypeParse::STP_RANGE:
3056 ++nof_ranges;
3057 break;
3058 case SubTypeParse::STP_LENGTH: {
3059 Ttcn::LengthRestriction* len_res = parse->Length();
3060 Value* min_val = len_res->get_is_range() ? len_res->get_lower_value() :
3061 len_res->get_single_value();
3062 Value* max_val = len_res->get_is_range() ? len_res->get_upper_value() :
3063 len_res->get_single_value();
3064 const char* json_min = NULL;
3065 const char* json_max = NULL;
3066 switch (subtype) {
3067 case ST_RECORDOF:
3068 case ST_SETOF:
3069 // use minItems and maxItems for record of/set of
3070 json_min = "minItems";
3071 json_max = "maxItems";
3072 break;
3073 case ST_BITSTRING:
3074 case ST_HEXSTRING:
3075 case ST_OCTETSTRING:
3076 case ST_CHARSTRING:
3077 case ST_UNIVERSAL_CHARSTRING:
3078 // use minLength and maxLength for string types
3079 json_min = "minLength";
3080 json_max = "maxLength";
3081 break;
3082 default:
3083 FATAL_ERROR("SubType::generate_json_schema - length %d", subtype);
3084 }
3085 json.put_next_token(JSON_TOKEN_NAME, json_min);
3086 min_val->generate_json_value(json);
3087 if (max_val != NULL) {
3088 json.put_next_token(JSON_TOKEN_NAME, json_max);
3089 max_val->generate_json_value(json);
3090 }
3091 break; }
3092 case SubTypeParse::STP_PATTERN: {
3093 json.put_next_token(JSON_TOKEN_NAME, "pattern");
3094 char* json_pattern = parse->Pattern()->convert_to_json();
3095 json.put_next_token(JSON_TOKEN_STRING, json_pattern);
3096 Free(json_pattern);
3097 break; }
3098 default:
3099 break;
3100 }
3101 }
3102
3103 bool need_anyOf = (subtype == ST_INTEGER || subtype == ST_FLOAT) &&
3104 (nof_ranges + (has_value_list ? 1 : 0) > 1);
3105 if (need_anyOf) {
3106 // there are multiple value range/value list restrictions,
3107 // they need to be grouped in an 'anyOf' structure
3108 json.put_next_token(JSON_TOKEN_NAME, "anyOf");
3109 json.put_next_token(JSON_TOKEN_ARRAY_START);
3110 json.put_next_token(JSON_TOKEN_OBJECT_START);
3111 }
3112 if (has_value_list) {
3113 // generate the value list into an enum
3114 json.put_next_token(JSON_TOKEN_NAME, "enum");
3115 json.put_next_token(JSON_TOKEN_ARRAY_START);
3116 generate_json_schema_value_list(json, allow_special_float);
3117 json.put_next_token(JSON_TOKEN_ARRAY_END);
3118 }
3119 if (need_anyOf && has_value_list) {
3120 // end of the value list and beginning of the first value range
3121 json.put_next_token(JSON_TOKEN_OBJECT_END);
3122 json.put_next_token(JSON_TOKEN_OBJECT_START);
3123 }
3124 if (nof_ranges > 0) {
3125 switch (subtype) {
3126 case ST_INTEGER:
3127 case ST_FLOAT:
3128 generate_json_schema_number_ranges(json);
3129 break;
3130 case ST_CHARSTRING:
3131 case ST_UNIVERSAL_CHARSTRING: {
3132 // merge all string range restrictions into one JSON schema pattern
3133 char* pattern_str = mcopystrn("\"^[", 3);
3134 pattern_str = generate_json_schema_string_ranges(pattern_str);
3135 pattern_str = mputstrn(pattern_str, "]*$\"", 4);
3136 json.put_next_token(JSON_TOKEN_NAME, "pattern");
3137 json.put_next_token(JSON_TOKEN_STRING, pattern_str);
3138 Free(pattern_str);
3139 break; }
3140 default:
3141 FATAL_ERROR("SubType::generate_json_schema - range %d", subtype);
3142 }
3143 }
3144 if (need_anyOf) {
3145 // end of the 'anyOf' structure
3146 json.put_next_token(JSON_TOKEN_OBJECT_END);
3147 json.put_next_token(JSON_TOKEN_ARRAY_END);
3148 }
3149}
3150
3151void SubType::generate_json_schema_value_list(JSON_Tokenizer& json, bool allow_special_float)
3152{
3153 for (size_t i = 0; i < parsed->size(); ++i) {
3154 SubTypeParse *parse = (*parsed)[i];
3155 if (parse->get_selection() == SubTypeParse::STP_SINGLE) {
3156 if (parse->Single()->get_valuetype() == Value::V_REFD) {
3157 Common::Assignment* ass = parse->Single()->get_reference()->get_refd_assignment();
3158 if (ass->get_asstype() == Common::Assignment::A_TYPE) {
3159 // it's a reference to another subtype, insert its value list here
3160 ass->get_Type()->get_sub_type()->generate_json_schema_value_list(json, allow_special_float);
3161 }
3162 }
3163 else {
3164 parse->Single()->generate_json_value(json, allow_special_float);
3165 }
3166 }
3167 }
3168}
3169
3170bool SubType::generate_json_schema_number_ranges(JSON_Tokenizer& json, bool first /* = true */)
3171{
3172 for (size_t i = 0; i < parsed->size(); ++i) {
3173 SubTypeParse *parse = (*parsed)[i];
3174 if (parse->get_selection() == SubTypeParse::STP_SINGLE) {
3175 if (parse->Single()->get_valuetype() == Value::V_REFD) {
3176 Common::Assignment* ass = parse->Single()->get_reference()->get_refd_assignment();
3177 if (ass->get_asstype() == Common::Assignment::A_TYPE) {
3178 // it's a reference to another subtype, insert its value ranges here
3179 first = ass->get_Type()->get_sub_type()->generate_json_schema_number_ranges(json, first);
3180 }
3181 }
3182 }
3183 else if (parse->get_selection() == SubTypeParse::STP_RANGE) {
3184 if (!first) {
3185 // the ranges are in an 'anyOf' structure, they need to be placed in an object
3186 json.put_next_token(JSON_TOKEN_OBJECT_END);
3187 json.put_next_token(JSON_TOKEN_OBJECT_START);
3188 }
3189 else {
3190 first = false;
3191 }
3192 // add the minimum and/or maximum values as numbers
3193 if (parse->Min() != NULL) {
3194 json.put_next_token(JSON_TOKEN_NAME, "minimum");
3195 parse->Min()->generate_json_value(json);
3196 json.put_next_token(JSON_TOKEN_NAME, "exclusiveMinimum");
3197 json.put_next_token(parse->MinExclusive() ? JSON_TOKEN_LITERAL_TRUE : JSON_TOKEN_LITERAL_FALSE);
3198 }
3199 if (parse->Max() != NULL) {
3200 json.put_next_token(JSON_TOKEN_NAME, "maximum");
3201 parse->Max()->generate_json_value(json);
3202 json.put_next_token(JSON_TOKEN_NAME, "exclusiveMaximum");
3203 json.put_next_token(parse->MaxExclusive() ? JSON_TOKEN_LITERAL_TRUE : JSON_TOKEN_LITERAL_FALSE);
3204 }
3205 }
3206 }
3207 return first;
3208}
3209
3210char* SubType::generate_json_schema_string_ranges(char* pattern_str)
3211{
3212 for (size_t i = 0; i < parsed->size(); ++i) {
3213 SubTypeParse *parse = (*parsed)[i];
3214 if (parse->get_selection() == SubTypeParse::STP_SINGLE) {
3215 if (parse->Single()->get_valuetype() == Value::V_REFD) {
3216 Common::Assignment* ass = parse->Single()->get_reference()->get_refd_assignment();
3217 if (ass->get_asstype() == Common::Assignment::A_TYPE) {
3218 // it's a reference to another subtype, insert its string ranges here
3219 pattern_str = ass->get_Type()->get_sub_type()->generate_json_schema_string_ranges(pattern_str);
3220 }
3221 }
3222 }
3223 else if (parse->get_selection() == SubTypeParse::STP_RANGE) {
3224 // insert the string range into the pattern string
3225 string lower_str = (subtype == ST_CHARSTRING) ? parse->Min()->get_val_str() :
3226 ustring_to_uft8(parse->Min()->get_val_ustr());
3227 string upper_str = (subtype == ST_CHARSTRING) ? parse->Max()->get_val_str() :
3228 ustring_to_uft8(parse->Max()->get_val_ustr());
3229 pattern_str = mputprintf(pattern_str, "%s-%s", lower_str.c_str(), upper_str.c_str());
3230 }
3231 }
3232 return pattern_str;
3233}
3234
3235void SubType::generate_json_schema_float(JSON_Tokenizer& json)
3236{
3237 bool has_nan = float_st->is_element(make_ttcn3float(REAL_NAN));
3238 bool has_pos_inf = float_st->is_element(make_ttcn3float(REAL_INFINITY));
3239 bool has_neg_inf = float_st->is_element(make_ttcn3float(-REAL_INFINITY));
3240 bool has_special = has_nan || has_pos_inf || has_neg_inf;
3241 bool has_number = false;
3242 for (size_t i = 0; i < parsed->size() && !has_number; ++i) {
3243 // go through the restrictions and check if at least one number is allowed
3244 SubTypeParse *parse = (*parsed)[i];
3245 switch (parse->get_selection()) {
3246 case SubTypeParse::STP_SINGLE: {
3247 Real r = parse->Single()->get_val_Real();
3248 if (r == r && r != REAL_INFINITY && r != -REAL_INFINITY) {
3249 // a single value other than NaN, INF and -INF is a number
3250 has_number = true;
3251 }
3252 break; }
3253 case SubTypeParse::STP_RANGE: {
3254 if (parse->Min() != NULL) {
3255 if (parse->Min()->get_val_Real() != REAL_INFINITY) {
3256 // a minimum value other than INF means a number is allowed
3257 has_number = true;
3258 }
3259 }
3260 if (parse->Max() != NULL) {
3261 // a maximum value other than -INF means a number is allowed
3262 if (parse->Max()->get_val_Real() != -REAL_INFINITY) {
3263 has_number = true;
3264 }
3265 }
3266 break; }
3267 default:
3268 break;
3269 }
3270 }
3271 if (has_number && has_special) {
3272 json.put_next_token(JSON_TOKEN_NAME, "anyOf");
3273 json.put_next_token(JSON_TOKEN_ARRAY_START);
3274 json.put_next_token(JSON_TOKEN_OBJECT_START);
3275 }
3276 if (has_number) {
3277 json.put_next_token(JSON_TOKEN_NAME, "type");
3278 json.put_next_token(JSON_TOKEN_STRING, "\"number\"");
3279 // generate the restrictions' schema elements here
3280 // (the 2nd parameter makes sure that NaN, INF and -INF are ignored)
3281 generate_json_schema(json, false);
3282 }
3283 if (has_number && has_special) {
3284 json.put_next_token(JSON_TOKEN_OBJECT_END);
3285 json.put_next_token(JSON_TOKEN_OBJECT_START);
3286 }
3287 if (has_special) {
3288 json.put_next_token(JSON_TOKEN_NAME, "enum");
3289 json.put_next_token(JSON_TOKEN_ARRAY_START);
3290 if (has_nan) {
3291 json.put_next_token(JSON_TOKEN_STRING, "\"not_a_number\"");
3292 }
3293 if (has_pos_inf) {
3294 json.put_next_token(JSON_TOKEN_STRING, "\"infinity\"");
3295 }
3296 if (has_neg_inf) {
3297 json.put_next_token(JSON_TOKEN_STRING, "\"-infinity\"");
3298 }
3299 json.put_next_token(JSON_TOKEN_ARRAY_END);
3300 }
3301 if (has_number && has_special) {
3302 json.put_next_token(JSON_TOKEN_OBJECT_END);
3303 json.put_next_token(JSON_TOKEN_ARRAY_END);
3304 }
3305}
3306
970ed795 3307} // namespace Common
This page took 0.294205 seconds and 5 git commands to generate.