File: subtype.cc

package info (click to toggle)
eclipse-titan 8.2.0-1
  • links: PTS
  • area: main
  • in suites: bookworm, sid
  • size: 103,544 kB
  • sloc: cpp: 271,008; ansic: 33,683; yacc: 23,419; makefile: 15,483; lex: 9,204; java: 4,848; perl: 4,555; sh: 2,242; xml: 1,378; javascript: 85; awk: 48; php: 32; python: 13
file content (3438 lines) | stat: -rw-r--r-- 125,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
/******************************************************************************
 * Copyright (c) 2000-2021 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 *
 * Contributors:
 *   Baji, Laszlo
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Cserveni, Akos
 *   Delic, Adam
 *   Feher, Csaba
 *   Forstner, Matyas
 *   Kovacs, Ferenc
 *   Raduly, Csaba
 *   Szabados, Kristof
 *   Szabo, Janos Zoltan – initial implementation
 *   Szabo, Bence Janos
 *   Szalai, Gabor
 *   Tatarka, Gabor
 *   Zalanyi, Balazs Andor
 *
 ******************************************************************************/
#include "subtype.hh"
#include "../common/dbgnew.hh"
#include "Identifier.hh"
#include "Value.hh"
#include "Setting.hh"
#include "Type.hh"
#include "CompilerError.hh"
#include "Valuestuff.hh"
#include "ttcn3/TtcnTemplate.hh"
#include "ttcn3/Templatestuff.hh"
#include "ttcn3/PatternString.hh"
#include "Constraint.hh"
#include "../common/JSON_Tokenizer.hh"
#include "ttcn3/Ttcn2Json.hh"
#include "main.hh"

#include <limits.h>

namespace Common {

/**************************
class SubTypeParse
**************************/

SubTypeParse::SubTypeParse(Value *p_single)
: selection(STP_SINGLE)
{
  if (!p_single) FATAL_ERROR("SubTypeParse::SubTypeParse()");
  single = p_single;
}

SubTypeParse::SubTypeParse(Value *p_min, bool p_min_exclusive, Value *p_max, bool p_max_exclusive)
: selection(STP_RANGE)
{
  range.min = p_min;
  range.min_exclusive = p_min_exclusive;
  range.max = p_max;
  range.max_exclusive = p_max_exclusive;
}

SubTypeParse::SubTypeParse(Ttcn::LengthRestriction *p_length)
: selection(STP_LENGTH)
{
  if (!p_length) FATAL_ERROR("SubTypeParse::SubTypeParse()");
  length = p_length;
}

SubTypeParse::SubTypeParse(Ttcn::PatternString *p_pattern)
: selection(STP_PATTERN)
{
  if (!p_pattern) FATAL_ERROR("SubTypeParse::SubTypeParse()");
  pattern = p_pattern;
}

SubTypeParse::~SubTypeParse()
{
  switch (selection) {
  case STP_SINGLE:
    delete single;
    break;
  case STP_RANGE:
    delete range.min;
    delete range.max;
    break;
  case STP_LENGTH:
    delete length;
    break;
  case STP_PATTERN:
    delete pattern;
    break;
  default:
    FATAL_ERROR("SubTypeParse::~SubTypeParse()");
  }
}

Value *SubTypeParse::Single() const
{
  if (selection != STP_SINGLE) FATAL_ERROR("SubTypeParse::Single()");
  return single;
}

Value *SubTypeParse::Min() const
{
  if (selection != STP_RANGE) FATAL_ERROR("SubTypeParse::Min()");
  return range.min;
}

bool SubTypeParse::MinExclusive() const
{
  if (selection != STP_RANGE) FATAL_ERROR("SubTypeParse::MinExclusive()");
  return range.min_exclusive;
}

Value *SubTypeParse::Max() const
{
  if (selection != STP_RANGE) FATAL_ERROR("SubTypeParse::Max()");
  return range.max;
}

bool SubTypeParse::MaxExclusive() const
{
  if (selection != STP_RANGE) FATAL_ERROR("SubTypeParse::MaxExclusive()");
  return range.max_exclusive;
}

Ttcn::LengthRestriction *SubTypeParse::Length() const
{
  if (selection != STP_LENGTH) FATAL_ERROR("SubTypeParse::Length()");
  return length;
}

Ttcn::PatternString *SubTypeParse::Pattern() const
{
  if (selection != STP_PATTERN) FATAL_ERROR("SubTypeParse::Pattern()");
  return pattern;
}

/********************
class SubtypeConstraint
********************/

SubtypeConstraint::SubtypeConstraint(subtype_t st)
{
  subtype  = st;
  length_restriction = NULL;
  switch (subtype) {
  case ST_INTEGER:
    integer_st = NULL;
    break;
  case ST_FLOAT:
    float_st = NULL;
    break;
  case ST_BOOLEAN:
    boolean_st = NULL;
    break;
  case ST_VERDICTTYPE:
    verdict_st = NULL;
    break;
  case ST_BITSTRING:
    bitstring_st = NULL;
    break;
  case ST_HEXSTRING:
    hexstring_st = NULL;
    break;
  case ST_OCTETSTRING:
    octetstring_st = NULL;
    break;
  case ST_CHARSTRING:
    charstring_st = NULL;
    break;
  case ST_UNIVERSAL_CHARSTRING:
    universal_charstring_st = NULL;
    break;
  case ST_OBJID:
  case ST_RECORD:
  case ST_SET:
  case ST_ENUM:
  case ST_UNION:
  case ST_FUNCTION:
  case ST_ALTSTEP:
  case ST_TESTCASE:
    value_st = NULL;
    break;
  case ST_RECORDOF:
  case ST_SETOF:
    recof_st = NULL;
    break;
  case ST_ERROR:
    break;
  default:
    FATAL_ERROR("SubtypeConstraint::SubtypeConstraint()");
  }
}

void SubtypeConstraint::copy(const SubtypeConstraint* other)
{
  if ((other==NULL) || (other->subtype!=subtype)) FATAL_ERROR("SubtypeConstraint::copy()");
  switch (subtype) {
  case ST_INTEGER:
    delete integer_st;
    integer_st = other->integer_st ? new IntegerRangeListConstraint(*(other->integer_st)) : NULL;
    break;
  case ST_FLOAT:
    delete float_st;
    float_st = other->float_st ? new RealRangeListConstraint(*(other->float_st)) : NULL;
    break;
  case ST_BOOLEAN:
    delete boolean_st;
    boolean_st = other->boolean_st ? new BooleanListConstraint(*(other->boolean_st)) : NULL;
    break;
  case ST_VERDICTTYPE:
    delete verdict_st;
    verdict_st = other->verdict_st ? new VerdicttypeListConstraint(*(other->verdict_st)) : NULL;
    break;
  case ST_BITSTRING:
    delete bitstring_st;
    bitstring_st = other->bitstring_st ? new BitstringConstraint(*(other->bitstring_st)) : NULL;
    break;
  case ST_HEXSTRING:
    delete hexstring_st;
    hexstring_st = other->hexstring_st ? new HexstringConstraint(*(other->hexstring_st)) : NULL;
    break;
  case ST_OCTETSTRING:
    delete octetstring_st;
    octetstring_st = other->octetstring_st ? new OctetstringConstraint(*(other->octetstring_st)) : NULL;
    break;
  case ST_CHARSTRING:
    delete charstring_st;
    charstring_st = other->charstring_st ? new CharstringSubtypeTreeElement(*(other->charstring_st)) : NULL;
    break;
  case ST_UNIVERSAL_CHARSTRING:
    delete universal_charstring_st;
    universal_charstring_st = other->universal_charstring_st ? new UniversalCharstringSubtypeTreeElement(*(other->universal_charstring_st)) : NULL;
    break;
  case ST_OBJID:
  case ST_RECORD:
  case ST_SET:
  case ST_ENUM:
  case ST_UNION:
  case ST_FUNCTION:
  case ST_ALTSTEP:
  case ST_TESTCASE:
    delete value_st;
    value_st = other->value_st ? new ValueListConstraint(*(other->value_st)) : NULL;
    break;
  case ST_RECORDOF:
  case ST_SETOF:
    delete recof_st;
    recof_st = other->recof_st ? new RecofConstraint(*(other->recof_st)) : NULL;
    break;
  default:
    FATAL_ERROR("SubtypeConstraint::copy()");
  }
  delete length_restriction;
  length_restriction = other->length_restriction ? new SizeRangeListConstraint(*(other->length_restriction)) : NULL;
}

// used by get_asn_type_constraint() to store singleton objects and delete them on program exit
struct AsnTypeConstraintSingleton
{
  SubtypeConstraint *printablestring_stc, *numericstring_stc, *bmpstring_stc;
  AsnTypeConstraintSingleton():
    printablestring_stc(NULL), numericstring_stc(NULL), bmpstring_stc(NULL) {}
  ~AsnTypeConstraintSingleton();
};

AsnTypeConstraintSingleton::~AsnTypeConstraintSingleton()
{
  delete printablestring_stc;
  delete numericstring_stc;
  delete bmpstring_stc;
}

SubtypeConstraint* SubtypeConstraint::get_asn_type_constraint(Type* type)
{
  static AsnTypeConstraintSingleton asn_tcs;
  static const char_limit_t zero('0'), nine('9'),
    bigA('A'), bigZ('Z'), smalla('a'), smallz('z');
  static const universal_char_limit_t uni_zero(0);
  static const universal_char_limit_t uni_ffff((1<<16)-1);
  static const CharRangeListConstraint numeric_string_char_range(
    CharRangeListConstraint(zero, nine) +
    CharRangeListConstraint(char_limit_t(' ')));
  static const CharRangeListConstraint printable_string_char_range(
    CharRangeListConstraint(bigA, bigZ) +
    CharRangeListConstraint(smalla, smallz) +
    CharRangeListConstraint(zero , nine) +
    CharRangeListConstraint(char_limit_t(' ')) +
    CharRangeListConstraint(char_limit_t('\'')) +
    CharRangeListConstraint(char_limit_t('(')) +
    CharRangeListConstraint(char_limit_t(')')) +
    CharRangeListConstraint(char_limit_t('+')) +
    CharRangeListConstraint(char_limit_t(',')) +
    CharRangeListConstraint(char_limit_t('-')) +
    CharRangeListConstraint(char_limit_t('.')) +
    CharRangeListConstraint(char_limit_t('/')) +
    CharRangeListConstraint(char_limit_t(':')) +
    CharRangeListConstraint(char_limit_t('=')) +
    CharRangeListConstraint(char_limit_t('?')));
  static const UniversalCharRangeListConstraint bmp_string_char_range(
    UniversalCharRangeListConstraint(uni_zero, uni_ffff));

  switch (type->get_typetype()) {
  case Type::T_TELETEXSTRING:
    // TODO: based on ITU-T Recommendation T.61
    return NULL;
  case Type::T_VIDEOTEXSTRING:
    // TODO: based on ITU-T Recommendation T.100 and T.101
    return NULL;
  case Type::T_NUMERICSTRING:
    if (asn_tcs.numericstring_stc==NULL) {
      asn_tcs.numericstring_stc = new SubtypeConstraint(ST_CHARSTRING);
      asn_tcs.numericstring_stc->charstring_st = new CharstringSubtypeTreeElement(numeric_string_char_range, false);
    }
    return asn_tcs.numericstring_stc;
  case Type::T_PRINTABLESTRING:
    if (asn_tcs.printablestring_stc==NULL) {
      asn_tcs.printablestring_stc = new SubtypeConstraint(ST_CHARSTRING);
      asn_tcs.printablestring_stc->charstring_st = new CharstringSubtypeTreeElement(printable_string_char_range, false);
    }
    return asn_tcs.printablestring_stc;
  case Type::T_BMPSTRING:
    if (asn_tcs.bmpstring_stc==NULL) {
      asn_tcs.bmpstring_stc = new SubtypeConstraint(ST_UNIVERSAL_CHARSTRING);
      asn_tcs.bmpstring_stc->universal_charstring_st = new UniversalCharstringSubtypeTreeElement(bmp_string_char_range, false);
    }
    return asn_tcs.bmpstring_stc;
  default:
    return NULL;
  }
}

SubtypeConstraint* SubtypeConstraint::create_from_asn_value(Type* type, Value* value)
{
  Value* v = value->get_value_refd_last();
  subtype_t st_t = type->get_subtype_type();
  if ( (st_t==ST_ERROR) || (v->get_valuetype()==Value::V_ERROR) ) return NULL;
  SubtypeConstraint* stc = new SubtypeConstraint(st_t);
  switch (v->get_valuetype()) {
  case Value::V_INT:
    if (st_t!=ST_INTEGER) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->integer_st = new IntegerRangeListConstraint(int_limit_t(*(v->get_val_Int())));
    break;
  case Value::V_REAL: {
    if (st_t!=ST_FLOAT) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    ttcn3float r = v->get_val_Real();
    if (r!=r) stc->float_st = new RealRangeListConstraint(true);
    else stc->float_st = new RealRangeListConstraint(real_limit_t(r));
  } break;
  case Value::V_BOOL:
    if (st_t!=ST_BOOLEAN) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->boolean_st = new BooleanListConstraint(v->get_val_bool());
    break;
  case Value::V_OID:
  case Value::V_ROID:
    if (v->has_oid_error()) goto invalid_value;
    if (st_t!=ST_OBJID) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->value_st = new ValueListConstraint(v);
    break;
  case Value::V_BSTR:
    if (st_t!=ST_BITSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->bitstring_st = new BitstringConstraint(v->get_val_str());
    break;
  case Value::V_HSTR:
    if (st_t!=ST_HEXSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->hexstring_st = new HexstringConstraint(v->get_val_str());
    break;
  case Value::V_OSTR:
    if (st_t!=ST_OCTETSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->octetstring_st = new OctetstringConstraint(v->get_val_str());
    break;
  case Value::V_CSTR:
    if (st_t!=ST_CHARSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->charstring_st = new CharstringSubtypeTreeElement(StringValueConstraint<string>(v->get_val_str()));
    break;
  case Value::V_ISO2022STR:
    if (st_t!=ST_CHARSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->charstring_st = new CharstringSubtypeTreeElement(StringValueConstraint<string>(v->get_val_iso2022str()));
    break;
  case Value::V_CHARSYMS:
  case Value::V_USTR:
    if (st_t!=ST_UNIVERSAL_CHARSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->universal_charstring_st = new UniversalCharstringSubtypeTreeElement(StringValueConstraint<ustring>(v->get_val_ustr()));
    break;
  case Value::V_ENUM:
  case Value::V_NULL: // FIXME: should go to ST_NULL
    if (st_t!=ST_ENUM) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->value_st = new ValueListConstraint(v);
    break;
  case Value::V_CHOICE:
  case Value::V_OPENTYPE: // FIXME?
    if (st_t!=ST_UNION) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->value_st = new ValueListConstraint(v);
    break;
  case Value::V_SEQ:
    if (st_t!=ST_RECORD) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->value_st = new ValueListConstraint(v);
    break;
  case Value::V_SET:
    if (st_t!=ST_SET) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->value_st = new ValueListConstraint(v);
    break;
  case Value::V_SEQOF:
    if (st_t!=ST_RECORDOF) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->recof_st = new RecofConstraint(v);
    break;
  case Value::V_SETOF:
    if (st_t!=ST_SETOF) FATAL_ERROR("SubtypeConstraint::create_from_asn_value()");
    stc->recof_st = new RecofConstraint(v);
    break;
  default:
    goto invalid_value;
  }
  return stc;
invalid_value:
  delete stc;
  return NULL;
}

SubtypeConstraint* SubtypeConstraint::create_from_asn_charvalues(Type* type, Value* value)
{
  Value* v = value->get_value_refd_last();
  subtype_t st_t = type->get_subtype_type();
  if ( (st_t==ST_ERROR) || (v->get_valuetype()==Value::V_ERROR) ) return NULL;
  SubtypeConstraint* stc = new SubtypeConstraint(st_t);
  switch (v->get_valuetype()) {
  case Value::V_CSTR:
  case Value::V_ISO2022STR: {
    if (st_t!=ST_CHARSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_charvalues()");
    CharRangeListConstraint charvalues;
    string val_str = (v->get_valuetype()==Value::V_CSTR) ? v->get_val_str() : v->get_val_iso2022str();
    for (size_t i=0; i<val_str.size(); i++) {
      if (!char_limit_t::is_valid_value(val_str[i])) {
        value->error("Invalid char in string %s at index %lu",
          value->get_stringRepr().c_str(), (unsigned long)i);
        goto invalid_value;
      }
      charvalues = charvalues + CharRangeListConstraint(val_str[i]);
    }
    stc->charstring_st = new CharstringSubtypeTreeElement(charvalues, true);
  } break;
  case Value::V_CHARSYMS: {
  case Value::V_USTR:
    if (st_t!=ST_UNIVERSAL_CHARSTRING) FATAL_ERROR("SubtypeConstraint::create_from_asn_charvalues()");
    UniversalCharRangeListConstraint ucharvalues;
    ustring val_ustr = v->get_val_ustr();
    for (size_t i=0; i<val_ustr.size(); i++) {
      if (!universal_char_limit_t::is_valid_value(val_ustr[i])) {
        value->error("Invalid universal char in string %s at index %lu",
          value->get_stringRepr().c_str(), (unsigned long)i);
        goto invalid_value;
      }
      ucharvalues = ucharvalues + UniversalCharRangeListConstraint(val_ustr[i]);
    }
    stc->universal_charstring_st = new UniversalCharstringSubtypeTreeElement(ucharvalues, true);
  } break;
  default:
    // error was already reported
    goto invalid_value;
  }
  return stc;
invalid_value:
  delete stc;
  return NULL;
}

int_limit_t SubtypeConstraint::get_int_limit(bool is_upper, Location* loc)
{
  int_limit_t default_limit = is_upper ?
    int_limit_t::maximum :
    ((subtype==ST_INTEGER) ? int_limit_t::minimum : int_limit_t(int_val_t((Int)0)));
  switch (subtype) {
  case ST_INTEGER:
    if (integer_st) {
      if (integer_st->is_empty()==TTRUE) {
        loc->error("Cannot determine the value of %s: the parent subtype is an empty set.",
                   is_upper?"MAX":"MIN");
        return default_limit;
      } else {
        return is_upper ? integer_st->get_maximal() : integer_st->get_minimal();
      }
    }
    return default_limit;
  case ST_BITSTRING:
    if (bitstring_st) {
      size_limit_t sl;
      tribool tb = bitstring_st->get_size_limit(is_upper, sl);
      if (tb==TTRUE) return sl.to_int_limit();
      break;
    }
    return default_limit;
  case ST_HEXSTRING:
    if (hexstring_st) {
      size_limit_t sl;
      tribool tb = hexstring_st->get_size_limit(is_upper, sl);
      if (tb==TTRUE) return sl.to_int_limit();
      break;
    }
    return default_limit;
  case ST_OCTETSTRING:
    if (octetstring_st) {
      size_limit_t sl;
      tribool tb = octetstring_st->get_size_limit(is_upper, sl);
      if (tb==TTRUE) return sl.to_int_limit();
      break;
    }
    return default_limit;
  case ST_CHARSTRING:
    if (charstring_st) {
      size_limit_t sl;
      tribool tb = charstring_st->get_size_limit(is_upper, sl);
      switch (tb) {
      case TFALSE:
        loc->error("Cannot determine the value of %s: the parent subtype does "
          "not define a %simal size value", is_upper?"MAX":"MIN", is_upper?"max":"min");
        break;
      case TUNKNOWN:
        loc->warning("Cannot determine the value of %s from parent subtype %s",
          is_upper?"MAX":"MIN", to_string().c_str());
        break;
      case TTRUE:
        return sl.to_int_limit();
      default:
        FATAL_ERROR("SubtypeConstraint::get_int_limit()");
      }
    }
    return default_limit;
  case ST_UNIVERSAL_CHARSTRING:
    if (universal_charstring_st) {
      size_limit_t sl;
      tribool tb = universal_charstring_st->get_size_limit(is_upper, sl);
      switch (tb) {
      case TFALSE:
        loc->error("Cannot determine the value of %s: the parent subtype does "
          "not define a %simal size value", is_upper?"MAX":"MIN", is_upper?"max":"min");
        break;
      case TUNKNOWN:
        loc->warning("Cannot determine the value of %s from parent subtype %s",
          is_upper?"MAX":"MIN", to_string().c_str());
        break;
      case TTRUE:
        return sl.to_int_limit();
      default:
        FATAL_ERROR("SubtypeConstraint::get_int_limit()");
      }
    }
    return default_limit;
  case ST_RECORDOF:
  case ST_SETOF:
    if (recof_st) {
      size_limit_t sl;
      tribool tb = recof_st->get_size_limit(is_upper, sl);
      if (tb==TTRUE) return sl.to_int_limit();
      break;
    }
    return default_limit;
  default:
    FATAL_ERROR("SubtypeConstraint::get_int_limit()");
  }
  loc->error("Cannot determine the value of %s from parent subtype %s",
    is_upper?"MAX":"MIN", to_string().c_str());
  return default_limit;
}

SubtypeConstraint* SubtypeConstraint::create_from_asn_range(
  Value* vmin, bool min_exclusive, Value* vmax, bool max_exclusive,
  Location* loc, subtype_t st_t, SubtypeConstraint* parent_subtype)
{
  switch (st_t) {
  case SubtypeConstraint::ST_INTEGER: {
    if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_INT)) ||
        ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_INT))) return NULL;

    int_limit_t min_limit;
    if (vmin) {
      min_limit = int_limit_t(*(vmin->get_val_Int()));
    } else { // MIN was used
      if (parent_subtype) {
        min_limit = parent_subtype->get_int_limit(false, loc);
      } else {
        min_limit = int_limit_t::minimum;
      }
    }

    if (min_exclusive) {
      if (min_limit==int_limit_t::minimum) {
        loc->error("invalid lower boundary, -infinity cannot be excluded from an INTEGER value range constraint");
        return NULL;
      } else {
        min_limit = min_limit.next();
      }
    }

    int_limit_t max_limit;
    if (vmax) {
      max_limit = int_limit_t(*(vmax->get_val_Int()));
    } else { // MAX was used
      if (parent_subtype) {
        max_limit = parent_subtype->get_int_limit(true, loc);
      } else {
        max_limit = int_limit_t::maximum;
      }
    }

    if (max_exclusive) {
      if (max_limit==int_limit_t::maximum) {
        loc->error("invalid upper boundary, infinity cannot be excluded from an INTEGER value range constraint");
        return NULL;
      } else {
        max_limit = max_limit.previous();
      }
    }
    if (max_limit<min_limit) {
      loc->error("lower boundary is bigger than upper boundary in INTEGER value range constraint");
      return NULL;
    }
    SubtypeConstraint* stc = new SubtypeConstraint(st_t);
    stc->integer_st = new IntegerRangeListConstraint(min_limit, max_limit);
    return stc;
  } break;
  case ST_FLOAT: {
    if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_REAL)) ||
        ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_REAL))) return NULL;
    if ((vmin!=NULL) && (vmin->get_val_Real()!=vmin->get_val_Real())) {
      loc->error("lower boundary cannot be NOT-A-NUMBER in REAL value range constraint");
      return NULL;
    }
    if ((vmax!=NULL) && (vmax->get_val_Real()!=vmax->get_val_Real())) {
      loc->error("upper boundary cannot be NOT-A-NUMBER in REAL value range constraint");
      return NULL;
    }

    if (parent_subtype && (parent_subtype->subtype!=ST_FLOAT)) FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
    real_limit_t min_limit;
    if (vmin) {
      min_limit = real_limit_t(vmin->get_val_Real());
    } else { // MIN was used
      if (parent_subtype && parent_subtype->float_st) {
        if (parent_subtype->float_st->is_range_empty()==TTRUE) {
          loc->error("Cannot determine the value of MIN: the parent subtype has no range");
          min_limit = real_limit_t::minimum;
        } else {
          min_limit = parent_subtype->float_st->get_minimal();
        }
      } else {
        min_limit = real_limit_t::minimum;
      }
    }

    if (min_exclusive) {
      min_limit = min_limit.next();
    }

    real_limit_t max_limit;
    if (vmax) {
      max_limit = real_limit_t(vmax->get_val_Real());
    } else { // MAX was used
      if (parent_subtype && parent_subtype->float_st) {
        if (parent_subtype->float_st->is_range_empty()==TTRUE) {
          loc->error("Cannot determine the value of MAX: the parent subtype has no range");
          max_limit = real_limit_t::maximum;
        } else {
          max_limit = parent_subtype->float_st->get_maximal();
        }
      } else {
        max_limit = real_limit_t::maximum;
      }
    }

    if (max_exclusive) {
      max_limit = max_limit.previous();
    }
    if (max_limit<min_limit) {
      loc->error("lower boundary is bigger than upper boundary in REAL value range constraint");
      return NULL;
    }
    SubtypeConstraint* stc = new SubtypeConstraint(st_t);
    stc->float_st = new RealRangeListConstraint(min_limit, max_limit);
    return stc;
  } break;
  case ST_CHARSTRING: {
    if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_CSTR)) ||
        ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_CSTR))) return NULL;
    if (vmin && (vmin->get_val_str().size()!=1)) {
      vmin->error("lower boundary of string value range constraint must be a single element string");
      return NULL;
    }
    if (vmax && (vmax->get_val_str().size()!=1)) {
      vmax->error("upper boundary of string value range constraint must be a single element string");
      return NULL;
    }
    if (vmin && !char_limit_t::is_valid_value(*vmin->get_val_str().c_str())) {
      vmin->error("lower boundary of string value range constraint is an invalid char");
      return NULL;
    }
    if (vmax && !char_limit_t::is_valid_value(*vmax->get_val_str().c_str())) {
      vmax->error("upper boundary of string value range constraint is an invalid char");
      return NULL;
    }

    if (parent_subtype && (parent_subtype->subtype!=ST_CHARSTRING)) FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");

    char_limit_t min_limit;
    if (vmin) {
      min_limit = char_limit_t(*vmin->get_val_str().c_str());
    } else { // MIN was used
      if (parent_subtype && parent_subtype->charstring_st) {
        tribool tb = parent_subtype->charstring_st->get_alphabet_limit(false, min_limit);
        switch (tb) {
        case TFALSE:
          loc->error("Cannot determine the value of MIN: the parent subtype does not define a minimal char value");
          min_limit = char_limit_t::minimum;
          break;
        case TUNKNOWN:
          loc->warning("Cannot determine the value of MIN, using the minimal char value of the type");
          min_limit = char_limit_t::minimum;
          break;
        case TTRUE:
          // min_limit was set to the correct value
          break;
        default:
          FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
        }
      } else {
        min_limit = char_limit_t::minimum;
      }
    }

    if (min_exclusive) {
      if (min_limit==char_limit_t::maximum) {
        loc->error("exclusive lower boundary is not a legal character");
        return NULL;
      }
      min_limit = min_limit.next();
    }

    char_limit_t max_limit;
    if (vmax) {
      max_limit = char_limit_t(*vmax->get_val_str().c_str());
    } else { // MAX was used
      if (parent_subtype && parent_subtype->charstring_st) {
        tribool tb = parent_subtype->charstring_st->get_alphabet_limit(true, max_limit);
        switch (tb) {
        case TFALSE:
          loc->error("Cannot determine the value of MAX: the parent subtype does not define a maximal char value");
          max_limit = char_limit_t::maximum;
          break;
        case TUNKNOWN:
          loc->warning("Cannot determine the value of MAX, using the maximal char value of the type");
          max_limit = char_limit_t::maximum;
          break;
        case TTRUE:
          // max_limit was set to the correct value
          break;
        default:
          FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
        }
      } else {
        max_limit = char_limit_t::maximum;
      }
    }

    if (max_exclusive) {
      if (max_limit==char_limit_t::minimum) {
        loc->error("exclusive upper boundary is not a legal character");
        return NULL;
      }
      max_limit = max_limit.previous();
    }
    if (max_limit<min_limit) {
      loc->error("lower boundary is bigger than upper boundary in string value range constraint");
      return NULL;
    }
    SubtypeConstraint* stc = new SubtypeConstraint(st_t);
    stc->charstring_st = new CharstringSubtypeTreeElement(CharRangeListConstraint(min_limit,max_limit), true);
    return stc;
  } break;
  case ST_UNIVERSAL_CHARSTRING: {
    if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_USTR)) ||
        ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_USTR))) return NULL;
    if (vmin && (vmin->get_val_ustr().size()!=1)) {
      vmin->error("lower boundary of string value range constraint must be a single element string");
      return NULL;
    }
    if (vmax && (vmax->get_val_ustr().size()!=1)) {
      vmax->error("upper boundary of string value range constraint must be a single element string");
      return NULL;
    }
    if (vmin && !universal_char_limit_t::is_valid_value(*vmin->get_val_ustr().u_str())) {
      vmin->error("lower boundary of string value range constraint is an invalid universal char");
      return NULL;
    }
    if (vmax && !universal_char_limit_t::is_valid_value(*vmax->get_val_ustr().u_str())) {
      vmax->error("upper boundary of string value range constraint is an invalid universal char");
      return NULL;
    }

    if (parent_subtype && (parent_subtype->subtype!=ST_UNIVERSAL_CHARSTRING)) FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
    universal_char_limit_t min_limit;
    if (vmin) {
      min_limit = universal_char_limit_t(*vmin->get_val_ustr().u_str());
    } else { // MIN was used
      if (parent_subtype && parent_subtype->universal_charstring_st) {
        tribool tb = parent_subtype->universal_charstring_st->get_alphabet_limit(false, min_limit);
        switch (tb) {
        case TFALSE:
          loc->error("Cannot determine the value of MIN: the parent subtype does not define a minimal char value");
          min_limit = universal_char_limit_t::minimum;
          break;
        case TUNKNOWN:
          loc->warning("Cannot determine the value of MIN, using the minimal char value of the type");
          min_limit = universal_char_limit_t::minimum;
          break;
        case TTRUE:
          // min_limit was set to the correct value
          break;
        default:
          FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
        }
      } else {
        min_limit = universal_char_limit_t::minimum;
      }
    }

    if (min_exclusive) {
      if (min_limit==universal_char_limit_t::maximum) {
        loc->error("exclusive lower boundary is not a legal character");
        return NULL;
      }
      min_limit = min_limit.next();
    }

    universal_char_limit_t max_limit;
    if (vmax) {
      max_limit = universal_char_limit_t(*vmax->get_val_ustr().u_str());
    } else { // MAX was used
      if (parent_subtype && parent_subtype->universal_charstring_st) {
        tribool tb = parent_subtype->universal_charstring_st->get_alphabet_limit(true, max_limit);
        switch (tb) {
        case TFALSE:
          loc->error("Cannot determine the value of MAX: the parent subtype does not define a maximal char value");
          max_limit = universal_char_limit_t::maximum;
          break;
        case TUNKNOWN:
          loc->warning("Cannot determine the value of MAX, using the maximal char value of the type");
          max_limit = universal_char_limit_t::maximum;
          break;
        case TTRUE:
          // max_limit was set to the correct value
          break;
        default:
          FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
        }
      } else {
        max_limit = universal_char_limit_t::maximum;
      }
    }

    if (max_exclusive) {
      if (max_limit==universal_char_limit_t::minimum) {
        loc->error("exclusive upper boundary is not a legal character");
        return NULL;
      }
      max_limit = max_limit.previous();
    }
    if (max_limit<min_limit) {
      loc->error("lower boundary is bigger than upper boundary in string value range constraint");
      return NULL;
    }
    SubtypeConstraint* stc = new SubtypeConstraint(st_t);
    stc->universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharRangeListConstraint(min_limit,max_limit), true);
    return stc;
  } break;
  default:
    FATAL_ERROR("SubtypeConstraint::create_from_asn_range()");
  }
  return NULL;
}

SubtypeConstraint* SubtypeConstraint::create_from_contained_subtype(SubtypeConstraint* contained_stc, bool char_context, Location* loc)
{
  if (contained_stc==NULL) return NULL;
  SubtypeConstraint* rv_stc = NULL;
  if (char_context) {
    switch (contained_stc->get_subtypetype()) {
    case ST_CHARSTRING:
      if (contained_stc->charstring_st==NULL) {
        rv_stc = new SubtypeConstraint(contained_stc->get_subtypetype()); // full set
      } else {
        if (contained_stc->charstring_st->is_valid_range()) {
          rv_stc = new SubtypeConstraint(contained_stc->get_subtypetype());
          rv_stc->copy(contained_stc);
          rv_stc->charstring_st->set_char_context(true);
        } else {
          loc->error("The type of the contained subtype constraint cannot be used in a permitted alphabet constraint");
        }
      }
      break;
    case ST_UNIVERSAL_CHARSTRING:
      if (contained_stc->universal_charstring_st==NULL) {
        rv_stc = new SubtypeConstraint(contained_stc->get_subtypetype()); // full set
      } else {
        if (contained_stc->universal_charstring_st->is_valid_range()) {
          rv_stc = new SubtypeConstraint(contained_stc->get_subtypetype());
          rv_stc->copy(contained_stc);
          rv_stc->universal_charstring_st->set_char_context(true);
        } else {
          loc->error("The type of the contained subtype constraint cannot be used in a permitted alphabet constraint");
        }
      }
      break;
    default:
      // error was already reported
      break;
    }
  } else {
    rv_stc = new SubtypeConstraint(contained_stc->get_subtypetype());
    rv_stc->copy(contained_stc);
  }
  return rv_stc;
}

SubtypeConstraint* SubtypeConstraint::create_asn_size_constraint(
  SubtypeConstraint* integer_stc, bool char_context, Type* type, Location* loc)
{
  if (integer_stc==NULL) return NULL;
  // convert IntegerRangeListConstraint to SizeRangeListConstraint
  if (integer_stc->subtype!=ST_INTEGER) FATAL_ERROR("SubtypeConstraint::create_asn_size_constraint()");
  SizeRangeListConstraint size_constraint(size_limit_t::minimum, size_limit_t::maximum);
  if (integer_stc->integer_st) {
    static const int_val_t zero((Int)0);
    static const int_limit_t ilt0(zero);
    IntegerRangeListConstraint valid_range(ilt0, int_limit_t::maximum);
    if (integer_stc->integer_st->is_subset(valid_range)==TFALSE) {
      loc->error("Range %s is not a valid range for a size constraint", integer_stc->to_string().c_str());
    } else {
      bool success = convert_int_to_size(*(integer_stc->integer_st), size_constraint);
      if (!success) {
        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());
      }
    }
  }
  subtype_t st_t = type->get_subtype_type();
  if (st_t==ST_ERROR) return NULL;
  SubtypeConstraint* stc = new SubtypeConstraint(st_t);

  if (!char_context) {
    stc->length_restriction = new SizeRangeListConstraint(size_constraint); // FIXME? : is this Ok if not a top level constraint?
  }

  switch (st_t) {
  case ST_BITSTRING:
      stc->bitstring_st = new BitstringConstraint(size_constraint);
      break;
    case ST_HEXSTRING:
      stc->hexstring_st = new HexstringConstraint(size_constraint);
      break;
    case ST_OCTETSTRING:
      stc->octetstring_st = new OctetstringConstraint(size_constraint);
      break;
    case ST_CHARSTRING:
      if (char_context) {
        if (size_constraint.is_equal(SizeRangeListConstraint(size_limit_t(1)))==TFALSE) {
          loc->error("Only SIZE(1) constraint can be used inside a permitted alphabet constraint");
          delete stc;
          return NULL;
        }
        // SIZE(1) is allowed in char context, it means ALL
      } else {
        stc->charstring_st = new CharstringSubtypeTreeElement(size_constraint);
      }
      break;
    case ST_UNIVERSAL_CHARSTRING:
      if (char_context) {
        if (size_constraint.is_equal(SizeRangeListConstraint(size_limit_t(1)))==TFALSE) {
          loc->error("Only SIZE(1) constraint can be used inside a permitted alphabet constraint");
          delete stc;
          return NULL;
        }
        // SIZE(1) is allowed in char context, it means ALL
      } else {
        stc->universal_charstring_st = new UniversalCharstringSubtypeTreeElement(size_constraint);
      }
      break;
    case ST_RECORDOF:
    case ST_SETOF:
      stc->recof_st = new RecofConstraint(size_constraint);
      break;
  default:
    loc->error("Size constraint is not allowed for type `%s'", type->get_typename().c_str());
    delete stc;
    return NULL;
  }
  return stc;
}

SubtypeConstraint* SubtypeConstraint::create_permitted_alphabet_constraint(
  SubtypeConstraint* stc, bool char_context, Type* type, Location* loc)
{
  if (char_context) {
    loc->error("Permitted alphabet constraint not allowed inside a permitted alphabet constraint");
    return NULL;
  }
  subtype_t st_t = type->get_subtype_type();
  switch (st_t) {
  case ST_CHARSTRING:
  case ST_UNIVERSAL_CHARSTRING: {
    if (stc==NULL) return NULL; // error was reported there
    if (st_t!=stc->get_subtypetype()) FATAL_ERROR("SubtypeConstraint::create_permitted_alphabet_constraint()");
    SubtypeConstraint* rv_stc = new SubtypeConstraint(st_t);
    if (st_t==ST_CHARSTRING) {
      if (stc->charstring_st) {
        rv_stc->charstring_st = new CharstringSubtypeTreeElement(*(stc->charstring_st));
        rv_stc->charstring_st->set_char_context(false);
      }
    } else {
      if (stc->universal_charstring_st) {
        rv_stc->universal_charstring_st = new UniversalCharstringSubtypeTreeElement(*(stc->universal_charstring_st));
        rv_stc->universal_charstring_st->set_char_context(false);
      }
    }
    return rv_stc;
  } break;
  case ST_ERROR:
    // error already reported
    break;
  default:
    loc->error("Permitted alphabet constraint is not allowed for type `%s'", type->get_typename().c_str());
    break;
  }
  return NULL;
}

void SubtypeConstraint::set_to_error()
{
  switch (subtype) {
  case ST_ERROR:
    break;
  case ST_INTEGER:
    delete integer_st;
    break;
  case ST_FLOAT:
    delete float_st;
    break;
  case ST_BOOLEAN:
    delete boolean_st;
    break;
  case ST_VERDICTTYPE:
    delete verdict_st;
    break;
  case ST_BITSTRING:
    delete bitstring_st;
    break;
  case ST_HEXSTRING:
    delete hexstring_st;
    break;
  case ST_OCTETSTRING:
    delete octetstring_st;
    break;
  case ST_CHARSTRING:
    delete charstring_st;
    break;
  case ST_UNIVERSAL_CHARSTRING:
    delete universal_charstring_st;
    break;
  case ST_OBJID:
  case ST_RECORD:
  case ST_SET:
  case ST_ENUM:
  case ST_UNION:
  case ST_FUNCTION:
  case ST_ALTSTEP:
  case ST_TESTCASE:
    delete value_st;
    break;
  case ST_RECORDOF:
  case ST_SETOF:
    delete recof_st;
    break;
  default:
    FATAL_ERROR("SubtypeConstraint::set_to_error()");
  }
  subtype = ST_ERROR;
  delete length_restriction;
  length_restriction = NULL;
}

string SubtypeConstraint::to_string() const
{
  switch (subtype) {
  case ST_ERROR:
    return string("<error>");
  case ST_INTEGER:
    return (integer_st==NULL) ? string() : integer_st->to_string();
  case ST_FLOAT:
    return (float_st==NULL) ? string() : float_st->to_string();
  case ST_BOOLEAN:
    return (boolean_st==NULL) ? string() : boolean_st->to_string();
  case ST_VERDICTTYPE:
    return (verdict_st==NULL) ? string() : verdict_st->to_string();
  case ST_BITSTRING:
    return (bitstring_st==NULL) ? string() : bitstring_st->to_string();
  case ST_HEXSTRING:
    return (hexstring_st==NULL) ? string() : hexstring_st->to_string();
  case ST_OCTETSTRING:
    return (octetstring_st==NULL) ? string() : octetstring_st->to_string();
  case ST_CHARSTRING:
    return (charstring_st==NULL) ? string() : charstring_st->to_string();
  case ST_UNIVERSAL_CHARSTRING:
    return (universal_charstring_st==NULL) ? string() : universal_charstring_st->to_string();
  case ST_OBJID:
  case ST_RECORD:
  case ST_SET:
  case ST_ENUM:
  case ST_UNION:
  case ST_FUNCTION:
  case ST_ALTSTEP:
  case ST_TESTCASE:
    return (value_st==NULL) ? string() : value_st->to_string();
  case ST_RECORDOF:
  case ST_SETOF:
    return (recof_st==NULL) ? string() : recof_st->to_string();
  default:
    FATAL_ERROR("SubtypeConstraint::to_string()");
  }
}

bool SubtypeConstraint::is_compatible(const SubtypeConstraint *p_st) const
{
  if (p_st==NULL) return true; // the other type has no subtype restriction
  if ( (subtype==ST_ERROR) || (p_st->subtype==ST_ERROR) ) return true;
  if (subtype!=p_st->subtype) FATAL_ERROR("SubtypeConstraint::is_compatible()");
  // if the resulting set.is_empty()==TUNKNOWN then remain silent
  switch (subtype) {
  case ST_INTEGER:
    if ((integer_st==NULL) || (p_st->integer_st==NULL)) return true;
    return ((*integer_st**(p_st->integer_st)).is_empty()!=TTRUE);
  case ST_FLOAT:
    if ((float_st==NULL) || (p_st->float_st==NULL)) return true;
    return ((*float_st**(p_st->float_st)).is_empty()!=TTRUE);
  case ST_BOOLEAN:
    if ((boolean_st==NULL) || (p_st->boolean_st==NULL)) return true;
    return ((*boolean_st**(p_st->boolean_st)).is_empty()!=TTRUE);
  case ST_VERDICTTYPE:
    if ((verdict_st==NULL) || (p_st->verdict_st==NULL)) return true;
    return ((*verdict_st**(p_st->verdict_st)).is_empty()!=TTRUE);
  case ST_BITSTRING:
    if ((bitstring_st==NULL) || (p_st->bitstring_st==NULL)) return true;
    return ((*bitstring_st**(p_st->bitstring_st)).is_empty()!=TTRUE);
  case ST_HEXSTRING:
    if ((hexstring_st==NULL) || (p_st->hexstring_st==NULL)) return true;
    return ((*hexstring_st**(p_st->hexstring_st)).is_empty()!=TTRUE);
  case ST_OCTETSTRING:
    if ((octetstring_st==NULL) || (p_st->octetstring_st==NULL)) return true;
    return ((*octetstring_st**(p_st->octetstring_st)).is_empty()!=TTRUE);
  case ST_CHARSTRING: {
    if ((charstring_st==NULL) || (p_st->charstring_st==NULL)) return true;
    CharstringSubtypeTreeElement* cc = new CharstringSubtypeTreeElement(
      CharstringSubtypeTreeElement::ET_INTERSECTION,
      new CharstringSubtypeTreeElement(*charstring_st),
      new CharstringSubtypeTreeElement(*(p_st->charstring_st))
    );
    bool rv = (cc->is_empty()!=TTRUE);
    delete cc;
    return rv;
  }
  case ST_UNIVERSAL_CHARSTRING: {
    if ((universal_charstring_st==NULL) || (p_st->universal_charstring_st==NULL)) return true;
    UniversalCharstringSubtypeTreeElement* ucc = new UniversalCharstringSubtypeTreeElement(
      UniversalCharstringSubtypeTreeElement::ET_INTERSECTION,
      new UniversalCharstringSubtypeTreeElement(*universal_charstring_st),
      new UniversalCharstringSubtypeTreeElement(*(p_st->universal_charstring_st))
    );
    bool rv = (ucc->is_empty()!=TTRUE);
    delete ucc;
    return rv;
  }
  case ST_OBJID:
  case ST_RECORD:
  case ST_SET:
  case ST_ENUM:
  case ST_UNION:
  case ST_FUNCTION:
  case ST_ALTSTEP:
  case ST_TESTCASE:
    if ((value_st==NULL) || (p_st->value_st==NULL)) return true;
    return ((*value_st**(p_st->value_st)).is_empty()!=TTRUE);
  case ST_RECORDOF:
  case ST_SETOF:
    if ((recof_st==NULL) || (p_st->recof_st==NULL)) return true;
    return ((*recof_st**(p_st->recof_st)).is_empty()!=TTRUE);
  default:
    FATAL_ERROR("SubtypeConstraint::is_compatible()");
  }
  return true;
}

bool SubtypeConstraint::is_compatible_with_elem() const
{
  if (subtype==ST_ERROR) return true;
  switch (subtype) {
  case ST_BITSTRING: {
    static BitstringConstraint str_elem(size_limit_t(1));
    if (bitstring_st==NULL) return true;
    return ((*bitstring_st*str_elem).is_empty()!=TTRUE);
  }
  case ST_HEXSTRING: {
    static HexstringConstraint str_elem(size_limit_t(1));
    if (hexstring_st==NULL) return true;
    return ((*hexstring_st*str_elem).is_empty()!=TTRUE);
  }
  case ST_OCTETSTRING: {
    static OctetstringConstraint str_elem(size_limit_t(1));
    if (octetstring_st==NULL) return true;
    return ((*octetstring_st*str_elem).is_empty()!=TTRUE);
  }
  case ST_CHARSTRING: {
    size_limit_t t = size_limit_t(1);
    SizeRangeListConstraint temp = SizeRangeListConstraint(t);
    static CharstringSubtypeTreeElement str_elem(temp);
    if (charstring_st==NULL) return true;
    CharstringSubtypeTreeElement* cc = new CharstringSubtypeTreeElement(
      CharstringSubtypeTreeElement::ET_INTERSECTION,
      new CharstringSubtypeTreeElement(*charstring_st),
      new CharstringSubtypeTreeElement(str_elem)
    );
    bool rv = (cc->is_empty()!=TTRUE);
    delete cc;
    return rv;
  }
  case ST_UNIVERSAL_CHARSTRING: {
    size_limit_t t = size_limit_t(1);
    SizeRangeListConstraint temp = SizeRangeListConstraint(t);
    static UniversalCharstringSubtypeTreeElement str_elem(temp);
    if (universal_charstring_st==NULL) return true;
    UniversalCharstringSubtypeTreeElement* ucc = new UniversalCharstringSubtypeTreeElement(
      UniversalCharstringSubtypeTreeElement::ET_INTERSECTION,
      new UniversalCharstringSubtypeTreeElement(*universal_charstring_st),
      new UniversalCharstringSubtypeTreeElement(str_elem)
    );
    bool rv = (ucc->is_empty()!=TTRUE);
    delete ucc;
    return rv;
  }
  default:
    FATAL_ERROR("SubtypeConstraint::is_compatible_with_elem()");
  }
  return true;
}

bool SubtypeConstraint::is_length_compatible(const SubtypeConstraint *p_st) const
{
  if (p_st==NULL) FATAL_ERROR("SubtypeConstraint::is_length_compatible()");
  if ((subtype==ST_ERROR) || (p_st->subtype==ST_ERROR)) return true;
  if (subtype != ST_RECORDOF && subtype != ST_SETOF &&
      p_st->subtype != ST_RECORDOF && p_st->subtype != ST_SETOF)
    FATAL_ERROR("SubtypeConstraint::is_length_compatible()");
  if (length_restriction==NULL || p_st->length_restriction==NULL) return true;
  return ((*length_restriction * *(p_st->length_restriction)).is_empty()!=TTRUE);
}

bool SubtypeConstraint::is_upper_limit_infinity() const
{
  if (ST_INTEGER == subtype && integer_st) {
    return integer_st->is_upper_limit_infinity();
  }
  if (ST_FLOAT == subtype && float_st) {
    return float_st->is_upper_limit_infinity();
  }
  return false;
}

bool SubtypeConstraint::is_lower_limit_infinity() const
{
  if (ST_INTEGER == subtype && integer_st) {
    return integer_st->is_lower_limit_infinity();
  }
  
  if (ST_FLOAT == subtype && float_st) {
    return float_st->is_lower_limit_infinity();
  }
  return false;
}

size_t SubtypeConstraint::get_min_length() const
{
  if (subtype != ST_BITSTRING) {
    FATAL_ERROR("SubtypeConstraint::get_min_length()");
  }
  return bitstring_st != NULL ? bitstring_st->get_min_length() : 0;
}


void SubtypeConstraint::except(const SubtypeConstraint* other)
{
  if (other==NULL) FATAL_ERROR("SubtypeConstraint::except()");
  if (subtype!=other->subtype) FATAL_ERROR("SubtypeConstraint::except()");
  switch (subtype) {
  case ST_INTEGER:
    if (other->integer_st==NULL) {
      if (integer_st==NULL) {
        integer_st = new IntegerRangeListConstraint();
      } else {
        *integer_st = IntegerRangeListConstraint();
      }
    } else {
      if (integer_st==NULL) {
        integer_st = new IntegerRangeListConstraint(~*(other->integer_st));
      } else {
        *integer_st = *integer_st - *(other->integer_st);
      }
    }
    break;
  case ST_FLOAT:
    if (other->float_st==NULL) {
      if (float_st==NULL) {
        float_st = new RealRangeListConstraint();
      } else {
        *float_st = RealRangeListConstraint();
      }
    } else {
      if (float_st==NULL) {
        float_st = new RealRangeListConstraint(~*(other->float_st));
      } else {
        *float_st = *float_st - *(other->float_st);
      }
    }
    break;
  case ST_BOOLEAN:
    if (other->boolean_st==NULL) {
      if (boolean_st==NULL) {
        boolean_st = new BooleanListConstraint();
      } else {
        *boolean_st = BooleanListConstraint();
      }
    } else {
      if (boolean_st==NULL) {
        boolean_st = new BooleanListConstraint(~*(other->boolean_st));
      } else {
        *boolean_st = *boolean_st - *(other->boolean_st);
      }
    }
    break;
  case ST_VERDICTTYPE:
    if (other->verdict_st==NULL) {
      if (verdict_st==NULL) {
        verdict_st = new VerdicttypeListConstraint();
      } else {
        *verdict_st = VerdicttypeListConstraint();
      }
    } else {
      if (verdict_st==NULL) {
        verdict_st = new VerdicttypeListConstraint(~*(other->verdict_st));
      } else {
        *verdict_st = *verdict_st - *(other->verdict_st);
      }
    }
    break;
  case ST_BITSTRING:
    if (other->bitstring_st==NULL) {
      if (bitstring_st==NULL) {
        bitstring_st = new BitstringConstraint();
      } else {
        *bitstring_st = BitstringConstraint();
      }
    } else {
      if (bitstring_st==NULL) {
        bitstring_st = new BitstringConstraint(~*(other->bitstring_st));
      } else {
        *bitstring_st = *bitstring_st - *(other->bitstring_st);
      }
    }
    break;
  case ST_HEXSTRING:
    if (other->hexstring_st==NULL) {
      if (hexstring_st==NULL) {
        hexstring_st = new HexstringConstraint();
      } else {
        *hexstring_st = HexstringConstraint();
      }
    } else {
      if (hexstring_st==NULL) {
        hexstring_st = new HexstringConstraint(~*(other->hexstring_st));
      } else {
        *hexstring_st = *hexstring_st - *(other->hexstring_st);
      }
    }
    break;
  case ST_OCTETSTRING:
    if (other->octetstring_st==NULL) {
      if (octetstring_st==NULL) {
        octetstring_st = new OctetstringConstraint();
      } else {
        *octetstring_st = OctetstringConstraint();
      }
    } else {
      if (octetstring_st==NULL) {
        octetstring_st = new OctetstringConstraint(~*(other->octetstring_st));
      } else {
        *octetstring_st = *octetstring_st - *(other->octetstring_st);
      }
    }
    break;
  case ST_CHARSTRING:
    if (other->charstring_st==NULL) {
      if (charstring_st==NULL) {
        charstring_st = new CharstringSubtypeTreeElement();
      } else {
        *charstring_st = CharstringSubtypeTreeElement();
      }
    } else {
      if (charstring_st==NULL) {
        CharstringSubtypeTreeElement* call_st = new CharstringSubtypeTreeElement();
        call_st->set_all();
        charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_EXCEPT,
          call_st, new CharstringSubtypeTreeElement(*(other->charstring_st)));
      } else {
        charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_EXCEPT,
          charstring_st, new CharstringSubtypeTreeElement(*(other->charstring_st)));
      }
    }
    break;
  case ST_UNIVERSAL_CHARSTRING:
    if (other->universal_charstring_st==NULL) {
      if (universal_charstring_st==NULL) {
        universal_charstring_st = new UniversalCharstringSubtypeTreeElement();
      } else {
        *universal_charstring_st = UniversalCharstringSubtypeTreeElement();
      }
    } else {
      if (universal_charstring_st==NULL) {
        UniversalCharstringSubtypeTreeElement* ucall_st = new UniversalCharstringSubtypeTreeElement();
        ucall_st->set_all();
        universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_EXCEPT,
          ucall_st, new UniversalCharstringSubtypeTreeElement(*(other->universal_charstring_st)));
      } else {
        universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_EXCEPT,
          universal_charstring_st, new UniversalCharstringSubtypeTreeElement(*(other->universal_charstring_st)));
      }
    }
    break;
  case ST_OBJID:
  case ST_RECORD:
  case ST_SET:
  case ST_ENUM:
  case ST_UNION:
  case ST_FUNCTION:
  case ST_ALTSTEP:
  case ST_TESTCASE:
    if (other->value_st==NULL) {
      if (value_st==NULL) {
        value_st = new ValueListConstraint();
      } else {
        *value_st = ValueListConstraint();
      }
    } else {
      if (value_st==NULL) {
        value_st = new ValueListConstraint(~*(other->value_st));
      } else {
        *value_st = *value_st - *(other->value_st);
      }
    }
    break;
  case ST_RECORDOF:
  case ST_SETOF:
    if (other->recof_st==NULL) {
      if (recof_st==NULL) {
        recof_st = new RecofConstraint();
      } else {
        *recof_st = RecofConstraint();
      }
    } else {
      if (recof_st==NULL) {
        recof_st = new RecofConstraint(~*(other->recof_st));
      } else {
        *recof_st = *recof_st - *(other->recof_st);
      }
    }
    break;
  default:
    FATAL_ERROR("SubtypeConstraint::except()");
  }
  if (other->length_restriction==NULL) {
    if (length_restriction==NULL) {
      length_restriction = new SizeRangeListConstraint();
    } else {
      *length_restriction = SizeRangeListConstraint();
    }
  } else {
    if (length_restriction==NULL) {
      length_restriction = new SizeRangeListConstraint(~*(other->length_restriction));
    } else {
      *length_restriction = *length_restriction - *(other->length_restriction);
    }
  }
}

void SubtypeConstraint::union_(const SubtypeConstraint* other)
{
  if (other==NULL) FATAL_ERROR("SubtypeConstraint::union_()");
  if (subtype!=other->subtype) FATAL_ERROR("SubtypeConstraint::union_()");
  switch (subtype) {
  case ST_INTEGER:
    if (integer_st==NULL) break;
    if (other->integer_st==NULL) { delete integer_st; integer_st = NULL; break; }
    *integer_st = *integer_st + *(other->integer_st);
    break;
  case ST_FLOAT:
    if (float_st==NULL) break;
    if (other->float_st==NULL) { delete float_st; float_st = NULL; break; }
    *float_st = *float_st + *(other->float_st);
    break;
  case ST_BOOLEAN:
    if (boolean_st==NULL) break;
    if (other->boolean_st==NULL) { delete boolean_st; boolean_st = NULL; break; }
    *boolean_st = *boolean_st + *(other->boolean_st);
    break;
  case ST_VERDICTTYPE:
    if (verdict_st==NULL) break;
    if (other->verdict_st==NULL) { delete verdict_st; verdict_st = NULL; break; }
    *verdict_st = *verdict_st + *(other->verdict_st);
    break;
  case ST_BITSTRING:
    if (bitstring_st==NULL) break;
    if (other->bitstring_st==NULL) { delete bitstring_st; bitstring_st = NULL; break; }
    *bitstring_st = *bitstring_st + *(other->bitstring_st);
    break;
  case ST_HEXSTRING:
    if (hexstring_st==NULL) break;
    if (other->hexstring_st==NULL) { delete hexstring_st; hexstring_st = NULL; break; }
    *hexstring_st = *hexstring_st + *(other->hexstring_st);
    break;
  case ST_OCTETSTRING:
    if (octetstring_st==NULL) break;
    if (other->octetstring_st==NULL) { delete octetstring_st; octetstring_st = NULL; break; }
    *octetstring_st = *octetstring_st + *(other->octetstring_st);
    break;
  case ST_CHARSTRING:
    if (charstring_st==NULL) break;
    if (other->charstring_st==NULL) { delete charstring_st; charstring_st = NULL; break; }
    charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_UNION,
      charstring_st, new CharstringSubtypeTreeElement(*(other->charstring_st)));
    break;
  case ST_UNIVERSAL_CHARSTRING:
    if (universal_charstring_st==NULL) break;
    if (other->universal_charstring_st==NULL) { delete universal_charstring_st; universal_charstring_st = NULL; break; }
    universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_UNION,
      universal_charstring_st, new UniversalCharstringSubtypeTreeElement(*(other->universal_charstring_st)));
    break;
  case ST_OBJID:
  case ST_RECORD:
  case ST_SET:
  case ST_ENUM:
  case ST_UNION:
  case ST_FUNCTION:
  case ST_ALTSTEP:
  case ST_TESTCASE:
    if (value_st==NULL) break;
    if (other->value_st==NULL) { delete value_st; value_st = NULL; break; }
    *value_st = *value_st + *(other->value_st);
    break;
  case ST_RECORDOF:
  case ST_SETOF:
    if (recof_st==NULL) break;
    if (other->recof_st==NULL) { delete recof_st; recof_st = NULL; break; }
    *recof_st = *recof_st + *(other->recof_st);
    break;
  default:
    FATAL_ERROR("SubtypeConstraint::union_()");
  }
  if (length_restriction!=NULL) {
    if (other->length_restriction==NULL) {
      delete length_restriction;
      length_restriction = NULL;
    } else {
      *length_restriction = *length_restriction + *(other->length_restriction);
    }
  }
}

void SubtypeConstraint::intersection(const SubtypeConstraint* other)
{
  if (other==NULL) FATAL_ERROR("SubtypeConstraint::intersection()");
  if (subtype!=other->subtype) FATAL_ERROR("SubtypeConstraint::intersection()");
  switch (subtype) {
  case ST_INTEGER:
    if (other->integer_st!=NULL) {
      if (integer_st==NULL) {
        integer_st = new IntegerRangeListConstraint(*(other->integer_st));
      } else {
        *integer_st = *integer_st * *(other->integer_st);
      }
    }
    break;
  case ST_FLOAT:
    if (other->float_st!=NULL) {
      if (float_st==NULL) {
        float_st = new RealRangeListConstraint(*(other->float_st));
      } else {
        *float_st = *float_st * *(other->float_st);
      }
    }
    break;
  case ST_BOOLEAN:
    if (other->boolean_st!=NULL) {
      if (boolean_st==NULL) {
        boolean_st = new BooleanListConstraint(*(other->boolean_st));
      } else {
        *boolean_st = *boolean_st * *(other->boolean_st);
      }
    }
    break;
  case ST_VERDICTTYPE:
    if (other->verdict_st!=NULL) {
      if (verdict_st==NULL) {
        verdict_st = new VerdicttypeListConstraint(*(other->verdict_st));
      } else {
        *verdict_st = *verdict_st * *(other->verdict_st);
      }
    }
    break;
  case ST_BITSTRING:
    if (other->bitstring_st!=NULL) {
      if (bitstring_st==NULL) {
        bitstring_st = new BitstringConstraint(*(other->bitstring_st));
      } else {
        *bitstring_st = *bitstring_st * *(other->bitstring_st);
      }
    }
    break;
  case ST_HEXSTRING:
    if (other->hexstring_st!=NULL) {
      if (hexstring_st==NULL) {
        hexstring_st = new HexstringConstraint(*(other->hexstring_st));
      } else {
        *hexstring_st = *hexstring_st * *(other->hexstring_st);
      }
    }
    break;
  case ST_OCTETSTRING:
    if (other->octetstring_st!=NULL) {
      if (octetstring_st==NULL) {
        octetstring_st = new OctetstringConstraint(*(other->octetstring_st));
      } else {
        *octetstring_st = *octetstring_st * *(other->octetstring_st);
      }
    }
    break;
  case ST_CHARSTRING:
    if (other->charstring_st!=NULL) {
      if (charstring_st==NULL) {
        charstring_st = new CharstringSubtypeTreeElement(*(other->charstring_st));
      } else {
        charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_INTERSECTION,
          charstring_st, new CharstringSubtypeTreeElement(*(other->charstring_st)));
      }
    }
    break;
  case ST_UNIVERSAL_CHARSTRING:
    if (other->universal_charstring_st!=NULL) {
      if (universal_charstring_st==NULL) {
        universal_charstring_st = new UniversalCharstringSubtypeTreeElement(*(other->universal_charstring_st));
      } else {
        universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_INTERSECTION,
          universal_charstring_st, new UniversalCharstringSubtypeTreeElement(*(other->universal_charstring_st)));
      }
    }
    break;
  case ST_OBJID:
  case ST_RECORD:
  case ST_SET:
  case ST_ENUM:
  case ST_UNION:
  case ST_FUNCTION:
  case ST_ALTSTEP:
  case ST_TESTCASE:
    if (other->value_st!=NULL) {
      if (value_st==NULL) {
        value_st = new ValueListConstraint(*(other->value_st));
      } else {
        *value_st = *value_st * *(other->value_st);
      }
    }
    break;
  case ST_RECORDOF:
  case ST_SETOF:
    if (other->recof_st!=NULL) {
      if (recof_st==NULL) {
        recof_st = new RecofConstraint(*(other->recof_st));
      } else {
        *recof_st = *recof_st * *(other->recof_st);
      }
    }
    break;
  default:
    FATAL_ERROR("SubtypeConstraint::intersection()");
  }
  if (other->length_restriction!=NULL) {
    if (length_restriction==NULL) {
      length_restriction = new SizeRangeListConstraint(*(other->length_restriction));
    } else {
      *length_restriction = *length_restriction * *(other->length_restriction);
    }
  }
}

tribool SubtypeConstraint::is_subset(const SubtypeConstraint* other) const
{
  if (other==NULL) return TTRUE;
  if (other->subtype!=subtype) FATAL_ERROR("SubtypeConstraint::is_subset()");
  switch (subtype) {
  case ST_INTEGER:
    if (other->integer_st==NULL) return TTRUE;
    return integer_st ? integer_st->is_subset(*(other->integer_st)) : TTRUE;
  case ST_FLOAT:
    if (other->float_st==NULL) return TTRUE;
    return float_st ? float_st->is_subset(*(other->float_st)) : TTRUE;
  case ST_BOOLEAN:
    if (other->boolean_st==NULL) return TTRUE;
    return boolean_st ? boolean_st->is_subset(*(other->boolean_st)) : TTRUE;
  case ST_VERDICTTYPE:
    if (other->verdict_st==NULL) return TTRUE;
    return verdict_st ? verdict_st->is_subset(*(other->verdict_st)) : TTRUE;
  case ST_BITSTRING:
    if (other->bitstring_st==NULL) return TTRUE;
    return bitstring_st ? bitstring_st->is_subset(*(other->bitstring_st)) : TTRUE;
  case ST_HEXSTRING:
    if (other->hexstring_st==NULL) return TTRUE;
    return hexstring_st ? hexstring_st->is_subset(*(other->hexstring_st)) : TTRUE;
  case ST_OCTETSTRING:
    if (other->octetstring_st==NULL) return TTRUE;
    return octetstring_st ? octetstring_st->is_subset(*(other->octetstring_st)) : TTRUE;
  case ST_CHARSTRING:
    if (other->charstring_st==NULL) return TTRUE;
    return charstring_st ? charstring_st->is_subset(other->charstring_st) : TTRUE;
  case ST_UNIVERSAL_CHARSTRING:
    if (other->universal_charstring_st==NULL) return TTRUE;
    return universal_charstring_st ? universal_charstring_st->is_subset(other->universal_charstring_st) : TTRUE;
  case ST_OBJID:
  case ST_RECORD:
  case ST_SET:
  case ST_ENUM:
  case ST_UNION:
  case ST_FUNCTION:
  case ST_ALTSTEP:
  case ST_TESTCASE:
    if (other->value_st==NULL) return TTRUE;
    return value_st ? value_st->is_subset(*(other->value_st)) : TTRUE;
  case ST_RECORDOF:
  case ST_SETOF:
    if (other->recof_st==NULL) return TTRUE;
    return recof_st ? recof_st->is_subset(*(other->recof_st)) : TTRUE;
  default:
    FATAL_ERROR("SubtypeConstraint::is_subset()");
  }
  return TUNKNOWN;
}

tribool SubtypeConstraint::can_intersect(const SubtypeConstraint* other) const
{
  if (other==NULL) return TTRUE;
  if (other->subtype!=subtype) FATAL_ERROR("SubtypeConstraint::can_intersect()");
  switch (subtype) {
  case ST_INTEGER:
    if (other->integer_st==NULL) return TTRUE;
    return integer_st ? integer_st->can_intersect(*(other->integer_st)) : TTRUE;
  case ST_FLOAT:
    if (other->float_st==NULL) return TTRUE;
    return float_st ? float_st->can_intersect(*(other->float_st)) : TTRUE;
  case ST_BOOLEAN:
    if (other->boolean_st==NULL) return TTRUE;
    return boolean_st ? boolean_st->can_intersect(*(other->boolean_st)) : TTRUE;
  case ST_VERDICTTYPE:
    if (other->verdict_st==NULL) return TTRUE;
    return verdict_st ? verdict_st->can_intersect(*(other->verdict_st)) : TTRUE;
  case ST_BITSTRING:
    if (other->bitstring_st==NULL) return TTRUE;
    return bitstring_st ? bitstring_st->can_intersect(*(other->bitstring_st)) : TTRUE;
  case ST_HEXSTRING:
    if (other->hexstring_st==NULL) return TTRUE;
    return hexstring_st ? hexstring_st->can_intersect(*(other->hexstring_st)) : TTRUE;
  case ST_OCTETSTRING:
    if (other->octetstring_st==NULL) return TTRUE;
    return octetstring_st ? octetstring_st->can_intersect(*(other->octetstring_st)) : TTRUE;
  case ST_CHARSTRING:
    if (other->charstring_st==NULL) return TTRUE;
    return charstring_st ? charstring_st->can_intersect(other->charstring_st) : TTRUE;
  case ST_UNIVERSAL_CHARSTRING:
    if (other->universal_charstring_st==NULL) return TTRUE;
    return universal_charstring_st ? universal_charstring_st->can_intersect(other->universal_charstring_st) : TTRUE;
  case ST_OBJID:
  case ST_RECORD:
  case ST_SET:
  case ST_ENUM:
  case ST_UNION:
  case ST_FUNCTION:
  case ST_ALTSTEP:
  case ST_TESTCASE:
    if (other->value_st==NULL) return TTRUE;
    return value_st ? value_st->can_intersect(*(other->value_st)) : TTRUE;
  case ST_RECORDOF:
  case ST_SETOF:
    if (other->recof_st==NULL) return TTRUE;
    return recof_st ? recof_st->can_intersect(*(other->recof_st)) : TTRUE;
  default:
    FATAL_ERROR("SubtypeConstraint::can_intersect()");
  }
  return TUNKNOWN;
}

/********************
class SubType
********************/

SubType::SubType(subtype_t st, Type *p_my_owner, SubType* p_parent_subtype,
  vector<SubTypeParse> *p_parsed, Constraints* p_asn_constraints)
: SubtypeConstraint(st), my_owner(p_my_owner), parent_subtype(p_parent_subtype)
, parsed(p_parsed), asn_constraints(p_asn_constraints)
, root(0), extendable(false), extension(0), checked(STC_NO)
, my_parents()
{
  if (p_my_owner==NULL) FATAL_ERROR("SubType::SubType()");
}

SubType::~SubType()
{
  my_parents.clear();
}

void SubType::chk_this_value(Value *value)
{
  if (checked==STC_NO) FATAL_ERROR("SubType::chk_this_value()");
  if ((checked==STC_CHECKING) || (subtype==ST_ERROR)) return;
  Value *val = value->get_value_refd_last();
  bool is_invalid = false;
  switch (val->get_valuetype()) {
  case Value::V_INT:
    if (subtype!=ST_INTEGER) FATAL_ERROR("SubType::chk_this_value()");
    is_invalid = (integer_st!=NULL) && !integer_st->is_element(int_limit_t(*(val->get_val_Int())));
    break;
  case Value::V_REAL:
    if (subtype!=ST_FLOAT) FATAL_ERROR("SubType::chk_this_value()");
    is_invalid = (float_st!=NULL) && !float_st->is_element(val->get_val_Real());
    break;
  case Value::V_BOOL:
    if (subtype!=ST_BOOLEAN) FATAL_ERROR("SubType::chk_this_value()");
    is_invalid = (boolean_st!=NULL) && !boolean_st->is_element(val->get_val_bool());
    break;
  case Value::V_VERDICT: {
    if (subtype!=ST_VERDICTTYPE) FATAL_ERROR("SubType::chk_this_value()");
    VerdicttypeListConstraint::verdicttype_constraint_t vtc;
    switch (val->get_val_verdict()) {
    case Value::Verdict_NONE: vtc = VerdicttypeListConstraint::VC_NONE; break;
    case Value::Verdict_PASS: vtc = VerdicttypeListConstraint::VC_PASS; break;
    case Value::Verdict_INCONC: vtc = VerdicttypeListConstraint::VC_INCONC; break;
    case Value::Verdict_FAIL: vtc = VerdicttypeListConstraint::VC_FAIL; break;
    case Value::Verdict_ERROR: vtc = VerdicttypeListConstraint::VC_ERROR; break;
    default: FATAL_ERROR("SubType::chk_this_value()");
    }
    is_invalid = (verdict_st!=NULL) && !verdict_st->is_element(vtc);
  } break;
  case Value::V_BSTR:
    if (subtype!=ST_BITSTRING) FATAL_ERROR("SubType::chk_this_value()");
    is_invalid = (bitstring_st!=NULL) && !bitstring_st->is_element(val->get_val_str());
    break;
  case Value::V_HSTR:
    if (subtype!=ST_HEXSTRING) FATAL_ERROR("SubType::chk_this_value()");
    is_invalid = (hexstring_st!=NULL) && !hexstring_st->is_element(val->get_val_str());
    break;
  case Value::V_OSTR:
    if (subtype!=ST_OCTETSTRING) FATAL_ERROR("SubType::chk_this_value()");
    is_invalid = (octetstring_st!=NULL) && !octetstring_st->is_element(val->get_val_str());
    break;
  case Value::V_CSTR:
  case Value::V_ISO2022STR:
    if (subtype!=ST_CHARSTRING) FATAL_ERROR("SubType::chk_this_value()");
    is_invalid = (charstring_st!=NULL) && (val->get_valuetype() == Value::V_CSTR ?
      !charstring_st->is_element(val->get_val_str()) :
      !charstring_st->is_element(val->get_val_iso2022str()));
    break;
  case Value::V_USTR:
  case Value::V_CHARSYMS:
    if (subtype!=ST_UNIVERSAL_CHARSTRING) FATAL_ERROR("SubType::chk_this_value()");
    is_invalid = (universal_charstring_st!=NULL) && !universal_charstring_st->is_element(val->get_val_ustr());
    break;
  case Value::V_SEQOF:
    if (subtype!=ST_RECORDOF) FATAL_ERROR("SubType::chk_this_value()");
    if (value->is_unfoldable()) return;
    is_invalid = (recof_st!=NULL) && !recof_st->is_element(val);
    break;
  case Value::V_SETOF:
    if (subtype!=ST_SETOF) FATAL_ERROR("SubType::chk_this_value()");
    if (value->is_unfoldable()) return;
    is_invalid = (recof_st!=NULL) && !recof_st->is_element(val);
    break;
  case Value::V_OID:
  case Value::V_ROID:
    if (subtype!=ST_OBJID) FATAL_ERROR("SubType::chk_this_value()");
    if (value->is_unfoldable()) return;
    is_invalid = (value_st!=NULL) && !value_st->is_element(val);
    break;
  case Value::V_ENUM:
  case Value::V_NULL: // FIXME: should go to ST_NULL
    if (subtype!=ST_ENUM) FATAL_ERROR("SubType::chk_this_value()");
    if (value->is_unfoldable()) return;
    is_invalid = (value_st!=NULL) && !value_st->is_element(val);
    break;
  case Value::V_CHOICE:
  case Value::V_OPENTYPE: // FIXME?
    if (subtype!=ST_UNION) FATAL_ERROR("SubType::chk_this_value()");
    if (value->is_unfoldable()) return;
    is_invalid = (value_st!=NULL) && !value_st->is_element(val);
    break;
  case Value::V_SEQ:
    if (subtype!=ST_RECORD) FATAL_ERROR("SubType::chk_this_value()");
    if (value->is_unfoldable()) return;
    is_invalid = (value_st!=NULL) && !value_st->is_element(val);
    break;
  case Value::V_SET:
    if (subtype!=ST_SET) FATAL_ERROR("SubType::chk_this_value()");
    if (value->is_unfoldable()) return;
    is_invalid = (value_st!=NULL) && !value_st->is_element(val);
    break;
  case Value::V_FUNCTION:
    if (subtype!=ST_FUNCTION) FATAL_ERROR("SubType::chk_this_value()");
    if (value->is_unfoldable()) return;
    is_invalid = (value_st!=NULL) && !value_st->is_element(val);
    break;
  case Value::V_ALTSTEP:
    if (subtype!=ST_ALTSTEP) FATAL_ERROR("SubType::chk_this_value()");
    if (value->is_unfoldable()) return;
    is_invalid = (value_st!=NULL) && !value_st->is_element(val);
    break;
  case Value::V_TESTCASE:
    if (subtype!=ST_TESTCASE) FATAL_ERROR("SubType::chk_this_value()");
    if (value->is_unfoldable()) return;
    is_invalid = (value_st!=NULL) && !value_st->is_element(val);
    break;
  case Value::V_ERROR:
    return;
  default:
    return;
  }
  if (is_invalid) {
    value->error("%s is not a valid value for type `%s' which has subtype %s",
      val->get_stringRepr().c_str(),
      my_owner->get_typename().c_str(),
      to_string().c_str());
  }
}

/** \todo revise */
void SubType::chk_this_template_generic(Template *templ)
{
  if (checked==STC_NO) FATAL_ERROR("SubType::chk_this_template_generic()");
  if ((checked==STC_CHECKING) || (subtype==ST_ERROR)) return;
  templ = templ->get_template_refd_last();
  switch (templ->get_templatetype()) {
  case Ttcn::Template::OMIT_VALUE:
  case Ttcn::Template::ANY_OR_OMIT:
  case Ttcn::Template::TEMPLATE_ERROR:
  case Ttcn::Template::ANY_VALUE:
    break;
  case Ttcn::Template::VALUE_LIST:
  case Ttcn::Template::COMPLEMENTED_LIST:
    /* Should be canonical before */
    break;
  case Ttcn::Template::SPECIFIC_VALUE:
    /* SPECIFIC_VALUE must be already checked in Type::chk_this_template() */
    break;
  case Ttcn::Template::TEMPLATE_REFD:
    /* unfoldable reference: cannot be checked at compile time */
    break;
  case Ttcn::Template::TEMPLATE_INVOKE:
    /* should be already checked in Type::chk_this_template() */
    break;
  default:
    chk_this_template(templ);
    break;
  }
  chk_this_template_length_restriction(templ);
}

/** \todo revise */
void SubType::chk_this_template(Template *templ)
{
  switch (templ->get_templatetype()) {
  case Template::TEMPLATE_LIST:
    if ( (length_restriction!=NULL) && TTRUE != length_restriction->is_empty() ) {
      size_t nof_comp_woaon = templ->get_nof_comps_not_anyornone();
      if (!templ->temps_contains_anyornone_symbol() &&
        nof_comp_woaon < length_restriction->get_minimal().get_size()) {
        templ->error("At least %s elements must be present in the list",
          Int2string((Int)(length_restriction->get_minimal().get_size())).c_str());
        return;
      } else if ( !length_restriction->get_maximal().get_infinity() && (nof_comp_woaon > length_restriction->get_maximal().get_size()) ) {
        templ->error("There must not be more than %s elements in the list",
          Int2string((Int)(length_restriction->get_maximal().get_size())).c_str());
        return;
      }
    }
    break;
  /* Simply break.  We don't know how many elements are there.
       SUPERSET_MATCH/SUBSET_MATCH is not possible to be an
       INDEXED_TEMPLATE_LIST.  */
  case Template::INDEXED_TEMPLATE_LIST:
    break;
  case Template::NAMED_TEMPLATE_LIST:
    break;
  case Template::VALUE_RANGE:
    /* Should be canonical before */
    break;
  case Template::ALL_FROM:
  case Template::DECODE_MATCH:
    break;
  case Template::TEMPLATE_CONCAT:
    if (!use_runtime_2) {
      FATAL_ERROR("SubType::chk_this_template()");
    }
    break;
  case Template::SUPERSET_MATCH:
  case Template::SUBSET_MATCH:
    if (subtype!=ST_SETOF){
      templ->error("'subset' template matching mechanism can be used "
        "only with 'set of' types");
      return;
    }
    for (size_t i=0;i<templ->get_nof_comps();i++)
      chk_this_template_generic(templ->get_temp_byIndex(i));
    break;
  case Template::BSTR_PATTERN:
    chk_this_template_pattern("bitstring", templ);
    break;
  case Template::HSTR_PATTERN:
    chk_this_template_pattern("hexstring", templ);
    break;
  case Template::OSTR_PATTERN:
    chk_this_template_pattern("octetstring", templ);
    break;
  case Template::CSTR_PATTERN:
    chk_this_template_pattern("charstring", templ);
    break;
  case Template::USTR_PATTERN:
	  chk_this_template_pattern("universal charstring", templ);
	  break;
  case Template::TEMPLATE_NOTUSED:
    break;
  case Template::TEMPLATE_ERROR:
    break;
  default:
    FATAL_ERROR("SubType::chk_this_template()");
    break;
  }
}

void SubType::chk_this_template_length_restriction(Template *templ)
{
  if (!templ->is_length_restricted()) return;
  // if there is a length restriction on the template then check if
  // the intersection of the two restrictions is not empty
  size_limit_t tmpl_min_len(size_limit_t(0));
  size_limit_t tmpl_max_len(size_limit_t::INFINITE_SIZE);
  Ttcn::LengthRestriction *lr=templ->get_length_restriction();
  lr->chk(Type::EXPECTED_DYNAMIC_VALUE);
  if (!lr->get_is_range()) { //Template's lr is single
    Value *tmp_val=lr->get_single_value();
    if (tmp_val->get_valuetype()!=Value::V_INT) return;
    Int templ_len = tmp_val->get_val_Int()->get_val();
    tmpl_min_len = tmpl_max_len = size_limit_t((size_t)templ_len);
  } else { //Template's lr is range
    Value *tmp_lower=lr->get_lower_value();
    if (tmp_lower->get_valuetype()!=Value::V_INT) return;
    Int templ_lower = tmp_lower->get_val_Int()->get_val();
    tmpl_min_len = size_limit_t((size_t)templ_lower);
    Value *tmp_upper=lr->get_upper_value();
    if (tmp_upper && tmp_upper->get_valuetype()!=Value::V_INT) return;
    if (tmp_upper) tmpl_max_len = size_limit_t((size_t)tmp_upper->get_val_Int()->get_val());
  }

  bool is_err = false;
  switch (subtype) {
  case ST_BITSTRING:
    if (bitstring_st!=NULL) {
      BitstringConstraint bc = *bitstring_st * BitstringConstraint(tmpl_min_len,tmpl_max_len);
      if (bc.is_empty()==TTRUE) is_err = true;
    }
    break;
  case ST_HEXSTRING:
    if (hexstring_st!=NULL) {
      HexstringConstraint hc = *hexstring_st * HexstringConstraint(tmpl_min_len,tmpl_max_len);
      if (hc.is_empty()==TTRUE) is_err = true;
    }
    break;
  case ST_OCTETSTRING:
    if (octetstring_st!=NULL) {
      OctetstringConstraint oc = *octetstring_st * OctetstringConstraint(tmpl_min_len,tmpl_max_len);
      if (oc.is_empty()==TTRUE) is_err = true;
    }
    break;
  case ST_CHARSTRING:
    if (charstring_st!=NULL) {
      CharstringSubtypeTreeElement* cc = new CharstringSubtypeTreeElement(
        CharstringSubtypeTreeElement::ET_INTERSECTION,
        new CharstringSubtypeTreeElement(*charstring_st),
        new CharstringSubtypeTreeElement(SizeRangeListConstraint(tmpl_min_len,tmpl_max_len))
      );
      if (cc->is_empty()==TTRUE) is_err = true;
      delete cc;
    }
    break;
  case ST_UNIVERSAL_CHARSTRING:
    if (universal_charstring_st!=NULL) {
      UniversalCharstringSubtypeTreeElement* ucc = new UniversalCharstringSubtypeTreeElement(
        UniversalCharstringSubtypeTreeElement::ET_INTERSECTION,
        new UniversalCharstringSubtypeTreeElement(*universal_charstring_st),
        new UniversalCharstringSubtypeTreeElement(SizeRangeListConstraint(tmpl_min_len,tmpl_max_len))
      );
      if (ucc->is_empty()==TTRUE) is_err = true;
      delete ucc;
    }
    break;
  case ST_RECORDOF:
  case ST_SETOF:
    if (recof_st!=NULL) {
      RecofConstraint rc = *recof_st * RecofConstraint(tmpl_min_len,tmpl_max_len);
      if (rc.is_empty()==TTRUE) is_err = true;
    }
    break;
  default:
    break;
  }
  if (is_err) {
    templ->error("Template's length restriction %s is outside of the type's subtype constraint %s",
      SizeRangeListConstraint(tmpl_min_len,tmpl_max_len).to_string().c_str(), to_string().c_str());
  }
}

void SubType::chk_this_template_pattern(const char *patt_type, Template *templ)
{
  Template::templatetype_t temptype= templ->get_templatetype();
  if ((temptype==Template::BSTR_PATTERN && subtype!=ST_BITSTRING) ||
    (temptype==Template::HSTR_PATTERN && subtype!=ST_HEXSTRING) ||
    (temptype==Template::OSTR_PATTERN && subtype!=ST_OCTETSTRING) ||
    (temptype==Template::CSTR_PATTERN && subtype!=ST_CHARSTRING) ||
    (temptype==Template::USTR_PATTERN && subtype!=ST_UNIVERSAL_CHARSTRING))
  {
    templ->error("Template is incompatible with subtype");
    return;
  }
  if ( (length_restriction!=NULL) && TTRUE != length_restriction->is_empty() ) {
    Int patt_min_len = static_cast<Int>(templ->get_min_length_of_pattern());
    if (patt_min_len < (Int)(length_restriction->get_minimal().get_size()) &&
      !templ->pattern_contains_anyornone_symbol()) {
      templ->error("At least %s string elements must be present in the %s",
        Int2string((Int)(length_restriction->get_minimal().get_size())).c_str(), patt_type);
    } else if ( !length_restriction->get_maximal().get_infinity() && (patt_min_len > (Int)(length_restriction->get_maximal().get_size())) ) {
      templ->error("There must not be more than %s string elements in the %s",
        Int2string((Int)(length_restriction->get_maximal().get_size())).c_str(), patt_type);
    }
  }
}

void SubType::add_ttcn_value(Value *v)
{
  if (value_st==NULL) value_st = new ValueListConstraint(v);
  else *value_st = *value_st + ValueListConstraint(v);
}

void SubType::add_ttcn_recof(Value *v)
{
  if (recof_st==NULL) recof_st = new RecofConstraint(v);
  else *recof_st = *recof_st + RecofConstraint(v);
}

bool SubType::add_ttcn_type_list_subtype(SubType* p_st)
{
  switch (subtype) {
  case ST_INTEGER:
    if (p_st->integer_st==NULL) return false;
    if (integer_st==NULL) integer_st = new IntegerRangeListConstraint(*(p_st->integer_st));
    else *integer_st = *integer_st + *(p_st->integer_st);
    break;
  case ST_FLOAT:
    if (p_st->float_st==NULL) return false;
    if (float_st==NULL) float_st = new RealRangeListConstraint(*(p_st->float_st));
    else *float_st = *float_st + *(p_st->float_st);
    break;
  case ST_BOOLEAN:
    if (p_st->boolean_st==NULL) return false;
    if (boolean_st==NULL) boolean_st = new BooleanListConstraint(*(p_st->boolean_st));
    else *boolean_st = *boolean_st + *(p_st->boolean_st);
    break;
  case ST_VERDICTTYPE:
    if (p_st->verdict_st==NULL) return false;
    if (verdict_st==NULL) verdict_st = new VerdicttypeListConstraint(*(p_st->verdict_st));
    else *verdict_st = *verdict_st + *(p_st->verdict_st);
    break;
  case ST_BITSTRING:
    if (p_st->bitstring_st==NULL) return false;
    if (bitstring_st==NULL) bitstring_st = new BitstringConstraint(*(p_st->bitstring_st));
    else *bitstring_st = *bitstring_st + *(p_st->bitstring_st);
    break;
  case ST_HEXSTRING:
    if (p_st->hexstring_st==NULL) return false;
    if (hexstring_st==NULL) hexstring_st = new HexstringConstraint(*(p_st->hexstring_st));
    else *hexstring_st = *hexstring_st + *(p_st->hexstring_st);
    break;
  case ST_OCTETSTRING:
    if (p_st->octetstring_st==NULL) return false;
    if (octetstring_st==NULL) octetstring_st = new OctetstringConstraint(*(p_st->octetstring_st));
    else *octetstring_st = *octetstring_st + *(p_st->octetstring_st);
    break;
  case ST_CHARSTRING:
    if (p_st->charstring_st==NULL) return false;
    if (charstring_st==NULL) {
      charstring_st = new CharstringSubtypeTreeElement(*(p_st->charstring_st));
    } else {
      charstring_st = new CharstringSubtypeTreeElement(
        CharstringSubtypeTreeElement::ET_UNION,
        charstring_st,
        new CharstringSubtypeTreeElement(*(p_st->charstring_st)));
    }
    break;
  case ST_UNIVERSAL_CHARSTRING:
    if (p_st->universal_charstring_st==NULL) return false;
    if (universal_charstring_st==NULL) {
      universal_charstring_st = new UniversalCharstringSubtypeTreeElement(*(p_st->universal_charstring_st));
    } else {
      universal_charstring_st = new UniversalCharstringSubtypeTreeElement(
        UniversalCharstringSubtypeTreeElement::ET_UNION,
        universal_charstring_st,
        new UniversalCharstringSubtypeTreeElement(*(p_st->universal_charstring_st)));
    }
    break;
  case ST_OBJID:
  case ST_RECORD:
  case ST_SET:
  case ST_ENUM:
  case ST_UNION:
  case ST_FUNCTION:
  case ST_ALTSTEP:
  case ST_TESTCASE:
    if (p_st->value_st==NULL) return false;
    if (value_st==NULL) value_st = new ValueListConstraint(*(p_st->value_st));
    else *value_st = *value_st + *(p_st->value_st);
    break;
  case ST_RECORDOF:
  case ST_SETOF:
    if (p_st->recof_st==NULL) return false;
    if (recof_st==NULL) recof_st = new RecofConstraint(*(p_st->recof_st));
    else *recof_st = *recof_st + *(p_st->recof_st);
    break;
  default:
    FATAL_ERROR("SubType::add_ttcn_type_list_subtype()");
  }
  return true;
}


bool SubType::add_parent_subtype(SubType* st)
{
  if (st==NULL) FATAL_ERROR("SubType::add_parent_subtype()");
  if (my_parents.has_key(st)) return true; // it was already successfully added -> ignore
  ReferenceChain refch(my_owner, "While checking circular type references in subtype definitions");
  refch.add(my_owner->get_fullname()); // current type
  // recursive check for all parents of referenced type
  if (!st->chk_recursion(refch)) return false;
  // if no recursion was detected then add the referenced type as parent
  my_parents.add(st,NULL);
  return true;
}

bool SubType::chk_recursion(ReferenceChain& refch)
{
  if (!refch.add(my_owner->get_fullname())) return false; // try the referenced type
  for (size_t i = 0; i < my_parents.size(); i++) {
    refch.mark_state();
    if (!my_parents.get_nth_key(i)->chk_recursion(refch)) return false;
    refch.prev_state();
  }
  return true;
}

bool SubType::add_ttcn_single(Value *val, size_t restriction_index)
{
  val->set_my_scope(my_owner->get_my_scope());
  val->set_my_governor(my_owner);
  val->set_fullname(my_owner->get_fullname()+".<single_restriction_"+Int2string(restriction_index) + ">");
  my_owner->chk_this_value_ref(val);

  // check if this is type reference, if not then fall through
  if (val->get_valuetype()==Value::V_REFD) {
    Reference* ref = val->get_reference();
    Assignment *ass = ref->get_refd_assignment();
    if (ass==NULL) return false; // defintion was not found, error was reported
    if (ass->get_asstype()==Assignment::A_TYPE) {
      Type* t = ass->get_Type();
      t->chk();
      if (t->get_typetype()==Type::T_ERROR) return false;
      // if there were subreferences then get the referenced field's type
      if (ref->get_subrefs()) {
        t = t->get_field_type(ref->get_subrefs(), Type::EXPECTED_CONSTANT);
        if ( (t==NULL) || (t->get_typetype()==Type::T_ERROR) ) return false;
        t->chk();
        if (t->get_typetype()==Type::T_ERROR) return false;
      }
      if (!t->is_identical(my_owner)) {
        val->error("Reference `%s' must refer to a type which has the same root type as this type",
                   val->get_reference()->get_dispname().c_str());
        return false;
      }
      // check subtype of referenced type
      SubType* t_st = t->get_sub_type();
      if (t_st==NULL) {
        val->error("Type referenced by `%s' does not have a subtype",
                   val->get_reference()->get_dispname().c_str());
        return false;
      }
      if (this->get_subtypetype() == ST_ENUM) {
        val->error("Type references are not allowed in the template list of enumerated types");
        return false;
      }

      // check circular subtype reference
      if (!add_parent_subtype(t_st)) return false;

      if (t_st->get_subtypetype()==ST_ERROR) return false;
      if (t_st->get_subtypetype()!=subtype) FATAL_ERROR("SubType::add_ttcn_single()");
      // add the subtype as union
      bool added = add_ttcn_type_list_subtype(t_st);
      if (!added) {
        val->error("Type referenced by `%s' does not have a subtype",
                   val->get_reference()->get_dispname().c_str());
      }
      return added;
    }
  }

  my_owner->chk_this_value(val, 0, Type::EXPECTED_CONSTANT,
    INCOMPLETE_NOT_ALLOWED, OMIT_NOT_ALLOWED, NO_SUB_CHK, NOT_IMPLICIT_OMIT,
    NOT_STR_ELEM, NOT_CLASS_MEMBER_INIT, FROM_SUBTYPE);

  Value *v=val->get_value_refd_last();

  switch (v->get_valuetype()) {
  case Value::V_INT:
    if (subtype!=ST_INTEGER) FATAL_ERROR("SubType::add_ttcn_single()");
    if (integer_st==NULL) integer_st = new IntegerRangeListConstraint(int_limit_t(*(v->get_val_Int())));
    else *integer_st = *integer_st + IntegerRangeListConstraint(int_limit_t(*(v->get_val_Int())));
    break;
  case Value::V_REAL: {
    if (subtype!=ST_FLOAT) FATAL_ERROR("SubType::add_ttcn_single()");
    ttcn3float r = v->get_val_Real();
    if (r!=r) {
      if (float_st==NULL) float_st = new RealRangeListConstraint(true);
      else *float_st = *float_st + RealRangeListConstraint(true);
    } else {
      if (float_st==NULL) float_st = new RealRangeListConstraint(real_limit_t(r));
      else *float_st = *float_st + RealRangeListConstraint(real_limit_t(r));
    }
  } break;
  case Value::V_BOOL:
    if (subtype!=ST_BOOLEAN) FATAL_ERROR("SubType::add_ttcn_single()");
    if (boolean_st==NULL) boolean_st = new BooleanListConstraint(v->get_val_bool());
    else *boolean_st = *boolean_st + BooleanListConstraint(v->get_val_bool());
    break;
  case Value::V_VERDICT: {
    if (subtype!=ST_VERDICTTYPE) FATAL_ERROR("SubType::add_ttcn_single()");
    VerdicttypeListConstraint::verdicttype_constraint_t vtc;
    switch (v->get_val_verdict()) {
    case Value::Verdict_NONE: vtc = VerdicttypeListConstraint::VC_NONE; break;
    case Value::Verdict_PASS: vtc = VerdicttypeListConstraint::VC_PASS; break;
    case Value::Verdict_INCONC: vtc = VerdicttypeListConstraint::VC_INCONC; break;
    case Value::Verdict_FAIL: vtc = VerdicttypeListConstraint::VC_FAIL; break;
    case Value::Verdict_ERROR: vtc = VerdicttypeListConstraint::VC_ERROR; break;
    default: FATAL_ERROR("SubType::add_ttcn_single()");
    }
    if (verdict_st==NULL) verdict_st = new VerdicttypeListConstraint(vtc);
    else *verdict_st = *verdict_st + VerdicttypeListConstraint(vtc);
  } break;
  case Value::V_OID:
    if (v->has_oid_error()) return false;
    if (subtype!=ST_OBJID) FATAL_ERROR("SubType::add_ttcn_single()");
    if (value_st==NULL) value_st = new ValueListConstraint(v);
    else *value_st = *value_st + ValueListConstraint(v);
    break;
  case Value::V_BSTR:
    if (subtype!=ST_BITSTRING) FATAL_ERROR("SubType::add_ttcn_single()");
    if (bitstring_st==NULL) bitstring_st = new BitstringConstraint(v->get_val_str());
    else *bitstring_st = *bitstring_st + BitstringConstraint(v->get_val_str());
    break;
  case Value::V_HSTR:
    if (subtype!=ST_HEXSTRING) FATAL_ERROR("SubType::add_ttcn_single()");
    if (hexstring_st==NULL) hexstring_st = new HexstringConstraint(v->get_val_str());
    else *hexstring_st = *hexstring_st + HexstringConstraint(v->get_val_str());
    break;
  case Value::V_OSTR:
    if (subtype!=ST_OCTETSTRING) FATAL_ERROR("SubType::add_ttcn_single()");
    if (octetstring_st==NULL) octetstring_st = new OctetstringConstraint(v->get_val_str());
    else *octetstring_st = *octetstring_st + OctetstringConstraint(v->get_val_str());
    break;
  case Value::V_CSTR: {
    if (subtype!=ST_CHARSTRING) FATAL_ERROR("SubType::add_ttcn_single()");
    CharstringSubtypeTreeElement* cst_elem = new CharstringSubtypeTreeElement(StringValueConstraint<string>(v->get_val_str()));
    if (charstring_st==NULL) charstring_st = cst_elem;
    else charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_UNION, charstring_st, cst_elem);
  } break;
  case Value::V_USTR: {
    if (subtype!=ST_UNIVERSAL_CHARSTRING) FATAL_ERROR("SubType::add_ttcn_single()");
    UniversalCharstringSubtypeTreeElement* ucst_elem = new UniversalCharstringSubtypeTreeElement(StringValueConstraint<ustring>(v->get_val_ustr()));
    if (universal_charstring_st==NULL) universal_charstring_st = ucst_elem;
    else universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_UNION, universal_charstring_st, ucst_elem);
  } break;
  case Value::V_ENUM:
  case Value::V_NULL: // FIXME: should go to ST_NULL
    if (subtype!=ST_ENUM) FATAL_ERROR("SubType::add_ttcn_single()");
    add_ttcn_value(v);
    break;
  case Value::V_CHOICE:
    if (subtype!=ST_UNION) FATAL_ERROR("SubType::add_ttcn_single()");
    add_ttcn_value(v);
    break;
  case Value::V_SEQ:
    if (subtype!=ST_RECORD) FATAL_ERROR("SubType::add_ttcn_single()");
    add_ttcn_value(v);
    break;
  case Value::V_SET:
    if (subtype!=ST_SET) FATAL_ERROR("SubType::add_ttcn_single()");
    add_ttcn_value(v);
    break;
  case Value::V_FUNCTION:
    if (subtype!=ST_FUNCTION) FATAL_ERROR("SubType::add_ttcn_single()");
    add_ttcn_value(v);
    break;
  case Value::V_ALTSTEP:
    if (subtype!=ST_ALTSTEP) FATAL_ERROR("SubType::add_ttcn_single()");
    add_ttcn_value(v);
    break;
  case Value::V_TESTCASE:
    if (subtype!=ST_TESTCASE) FATAL_ERROR("SubType::add_ttcn_single()");
    add_ttcn_value(v);
    break;
  case Value::V_SEQOF:
    if (subtype!=ST_RECORDOF) FATAL_ERROR("SubType::add_ttcn_single()");
    add_ttcn_recof(v);
    break;
  case Value::V_SETOF:
    if (subtype!=ST_SETOF) FATAL_ERROR("SubType::add_ttcn_single()");
    add_ttcn_recof(v);
    break;
  case Value::V_ERROR:
    return false;
  default:
    return false;
  }
  return true;
}

bool SubType::add_ttcn_range(Value *min, bool min_exclusive,
  Value *max, bool max_exclusive, size_t restriction_index, bool has_other)
{
  switch (subtype) {
  case ST_INTEGER:
  case ST_FLOAT:
  case ST_CHARSTRING:
  case ST_UNIVERSAL_CHARSTRING:
    break;
  default:
    my_owner->error("Range subtyping is not allowed for type `%s'",
      my_owner->get_typename().c_str());
    return false;
  }

  Value *vmin,*vmax;
  if (min==NULL) vmin=NULL;
  else {
    min->set_my_scope(my_owner->get_my_scope());
    min->set_fullname(my_owner->get_fullname()+".<range_restriction_"+Int2string(restriction_index)+"_lower>");
    my_owner->chk_this_value_ref(min);
    my_owner->chk_this_value(min, 0, Type::EXPECTED_CONSTANT,
      INCOMPLETE_NOT_ALLOWED, OMIT_NOT_ALLOWED, NO_SUB_CHK);
    vmin=min->get_value_refd_last();
  }
  if (max==NULL) vmax=NULL;
  else {
    max->set_my_scope(my_owner->get_my_scope());
    max->set_fullname(my_owner->get_fullname()+".<range_restriction_"+Int2string(restriction_index)+"_upper>");
    my_owner->chk_this_value_ref(max);
    my_owner->chk_this_value(max, 0, Type::EXPECTED_CONSTANT,
      INCOMPLETE_NOT_ALLOWED, OMIT_NOT_ALLOWED, NO_SUB_CHK);
    vmax=max->get_value_refd_last();
  }

  if ( (vmin!=NULL) && (vmax!=NULL) && (vmin->get_valuetype()!=vmax->get_valuetype()) ) return false;

  switch (subtype) {
  case ST_INTEGER: {
    if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_INT)) ||
        ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_INT))) return false;
    int_limit_t min_limit = (vmin!=NULL) ? int_limit_t(*(vmin->get_val_Int())) : int_limit_t::minimum;
    if (min_exclusive) {
      if (min_limit==int_limit_t::minimum) {
        my_owner->error("invalid lower boundary, -infinity cannot be excluded from an integer subtype range");
        return false;
      } else {
        if (min_limit==int_limit_t::maximum) {
          my_owner->error("!infinity is not a valid lower boundary");
          return false;
        }
        min_limit = min_limit.next();
      }
    }
    int_limit_t max_limit = (vmax!=NULL) ? int_limit_t(*(vmax->get_val_Int())) : int_limit_t::maximum;
    if (max_exclusive) {
      if (max_limit==int_limit_t::maximum) {
        my_owner->error("invalid upper boundary, infinity cannot be excluded from an integer subtype range");
        return false;
      } else {
        if (max_limit==int_limit_t::minimum) {
          my_owner->error("!-infinity is not a valid upper boundary");
          return false;
        }
        max_limit = max_limit.previous();
      }
    }
    if (max_limit<min_limit) {
      my_owner->error("lower boundary is bigger than upper boundary in integer subtype range");
      return false;
    }
    if (integer_st==NULL) integer_st = new IntegerRangeListConstraint(min_limit, max_limit);
    else *integer_st = *integer_st + IntegerRangeListConstraint(min_limit, max_limit);
  } break;
  case ST_FLOAT: {
    if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_REAL)) ||
        ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_REAL))) return false;
    if ((vmin!=NULL) && (vmin->get_val_Real()!=vmin->get_val_Real())) {
      my_owner->error("lower boundary cannot be not_a_number in float subtype range");
      return false;
    }
    if ((vmax!=NULL) && (vmax->get_val_Real()!=vmax->get_val_Real())) {
      my_owner->error("upper boundary cannot be not_a_number in float subtype range");
      return false;
    }
    real_limit_t min_limit = (vmin!=NULL) ? real_limit_t(vmin->get_val_Real()) : real_limit_t::minimum;
    if (min_exclusive) {
      if (min_limit==real_limit_t::maximum) {
        my_owner->error("!infinity is not a valid lower boundary");
        return false;
      }
      min_limit = min_limit.next();
    }
    real_limit_t max_limit = (vmax!=NULL) ? real_limit_t(vmax->get_val_Real()) : real_limit_t::maximum;
    if (max_exclusive) {
      if (max_limit==real_limit_t::minimum) {
        my_owner->error("!-infinity is not a valid upper boundary");
        return false;
      }
      max_limit = max_limit.previous();
    }
    if (max_limit<min_limit) {
      my_owner->error("lower boundary is bigger than upper boundary in float subtype range");
      return false;
    }
    if (float_st==NULL) float_st = new RealRangeListConstraint(min_limit, max_limit);
    else *float_st = *float_st + RealRangeListConstraint(min_limit, max_limit);
  } break;
  case ST_CHARSTRING: {
    if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_CSTR)) ||
        ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_CSTR))) return false;
    if ((vmin==NULL)&&(vmax==NULL)) {
      my_owner->error("a range subtype of a charstring cannot be (-infinity..infinity)");
      return false;
    }
    if (vmin==NULL) {
      my_owner->error("lower boundary of a charstring subtype range cannot be -infinity");
      return false;
    }
    if (vmax==NULL) {
      my_owner->error("upper boundary of a charstring subtype range cannot be infinity");
      return false;
    }
    if (vmin->get_val_str().size()!=1) {
      min->error("lower boundary of charstring subtype range must be a single element string");
      return false;
    }
    if (vmax->get_val_str().size()!=1) {
      max->error("upper boundary of charstring subtype range must be a single element string");
      return false;
    }
    if (!char_limit_t::is_valid_value(*vmin->get_val_str().c_str())) {
      min->error("lower boundary of charstring subtype range is an invalid char");
      return false;
    }
    if (!char_limit_t::is_valid_value(*vmax->get_val_str().c_str())) {
      max->error("upper boundary of charstring subtype range is an invalid char");
      return false;
    }
    char_limit_t min_limit(*vmin->get_val_str().c_str()), max_limit(*vmax->get_val_str().c_str());
    if (min_exclusive) {
      if (min_limit==char_limit_t::maximum) {
        min->error("exclusive lower boundary is not a legal charstring character");
        return false;
      }
      min_limit = min_limit.next();
    }
    if (max_exclusive) {
      if (max_limit==char_limit_t::minimum) {
        max->error("exclusive upper boundary is not a legal charstring character");
        return false;
      }
      max_limit = max_limit.previous();
    }
    if (max_limit<min_limit) {
      my_owner->error("lower boundary is bigger than upper boundary in charstring subtype range");
      return false;
    }
    if (charstring_st==NULL) charstring_st = new CharstringSubtypeTreeElement(CharRangeListConstraint(min_limit,max_limit), false);
    else {
      if (!has_other) { // union in char context can be done only with range constraints
        charstring_st->set_char_context(true);
        charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_UNION,
          charstring_st,
          new CharstringSubtypeTreeElement(CharRangeListConstraint(min_limit,max_limit), true));
        charstring_st->set_char_context(false);
      } else {
        // ignore it, error reported elsewhere
        return false;
      }
    }
  } break;
  case ST_UNIVERSAL_CHARSTRING: {
    if (((vmin!=NULL) && (vmin->get_valuetype()!=Value::V_USTR)) ||
        ((vmax!=NULL) && (vmax->get_valuetype()!=Value::V_USTR))) return false;
    if ((vmin==NULL)&&(vmax==NULL)) {
      my_owner->error("a range subtype of a universal charstring cannot be (-infinity..infinity)");
      return false;
    }
    if (vmin==NULL) {
      my_owner->error("lower boundary of a universal charstring subtype range cannot be -infinity");
      return false;
    }
    if (vmax==NULL) {
      my_owner->error("upper boundary of a universal charstring subtype range cannot be infinity");
      return false;
    }
    if (vmin->get_val_ustr().size()!=1) {
      min->error("lower boundary of universal charstring subtype range must be a single element string");
      return false;
    }
    if (vmax->get_val_ustr().size()!=1) {
      max->error("upper boundary of universal charstring subtype range must be a single element string");
      return false;
    }
    if (!universal_char_limit_t::is_valid_value(*vmin->get_val_ustr().u_str())) {
      min->error("lower boundary of universal charstring subtype range is an invalid char");
      return false;
    }
    if (!universal_char_limit_t::is_valid_value(*vmax->get_val_ustr().u_str())) {
      max->error("upper boundary of universal charstring subtype range is an invalid char");
      return false;
    }
    universal_char_limit_t min_limit(*vmin->get_val_ustr().u_str()), max_limit(*vmax->get_val_ustr().u_str());
    if (min_exclusive) {
      if (min_limit==universal_char_limit_t::maximum) {
        min->error("exclusive lower boundary is not a legal universal charstring character");
        return false;
      }
      min_limit = min_limit.next();
    }
    if (max_exclusive) {
      if (max_limit==universal_char_limit_t::minimum) {
        max->error("exclusive upper boundary is not a legal universal charstring character");
        return false;
      }
      max_limit = max_limit.previous();
    }
    if (max_limit<min_limit) {
      my_owner->error("lower boundary is bigger than upper boundary in universal charstring subtype range");
      return false;
    }

    if (universal_charstring_st==NULL) universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharRangeListConstraint(min_limit,max_limit), false);
    else {
      if (!has_other) { // union in char context can be done only with range constraints
        universal_charstring_st->set_char_context(true);
        universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_UNION,
          universal_charstring_st,
          new UniversalCharstringSubtypeTreeElement(UniversalCharRangeListConstraint(min_limit,max_limit), true));
        universal_charstring_st->set_char_context(false);
      } else {
        // ignore it, error reported elsewhere
        return false;
      }
    }
  } break;
  default:
    FATAL_ERROR("SubType::add_ttcn_range()");
  }
  return true;
}

bool SubType::set_ttcn_length(const size_limit_t& min, const size_limit_t& max)
{
  switch (subtype) {
  case ST_BITSTRING: {
    if (bitstring_st==NULL) bitstring_st = new BitstringConstraint(min,max);
    else *bitstring_st = *bitstring_st * BitstringConstraint(min,max);
  } break;
  case ST_HEXSTRING: {
    if (hexstring_st==NULL) hexstring_st = new HexstringConstraint(min,max);
    else *hexstring_st = *hexstring_st * HexstringConstraint(min,max);
  } break;
  case ST_OCTETSTRING: {
    if (octetstring_st==NULL) octetstring_st = new OctetstringConstraint(min,max);
    else *octetstring_st = *octetstring_st * OctetstringConstraint(min,max);
  } break;
  case ST_CHARSTRING: {
    CharstringSubtypeTreeElement* cst_elem = new CharstringSubtypeTreeElement(SizeRangeListConstraint(min,max));
    if (charstring_st==NULL) {
      charstring_st = cst_elem;
    } else {
      charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_INTERSECTION, charstring_st, cst_elem);
    }
  } break;
  case ST_UNIVERSAL_CHARSTRING: {
    UniversalCharstringSubtypeTreeElement* ucst_elem = new UniversalCharstringSubtypeTreeElement(SizeRangeListConstraint(min,max));
    if (universal_charstring_st==NULL) {
      universal_charstring_st = ucst_elem;
    } else {
      universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_INTERSECTION, universal_charstring_st, ucst_elem);
    }
  } break;
  case ST_RECORDOF:
  case ST_SETOF: {
    if (recof_st==NULL) recof_st = new RecofConstraint(min,max);
    else *recof_st = *recof_st * RecofConstraint(min,max);
  } break;
  default:
    my_owner->error("Length subtyping is not allowed for type `%s'",
                    my_owner->get_typename().c_str());
    return false;
  }
  if (length_restriction==NULL) length_restriction = new SizeRangeListConstraint(min,max);
  else *length_restriction = *length_restriction * SizeRangeListConstraint(min,max);
  return true;
}

void SubType::chk_boundary_valid(Value* boundary, Int max_value, const char* boundary_name)
{
  const int_val_t *int_val = boundary->get_val_Int();
  if (*int_val > int_val_t(max_value)) {
    boundary->error("The %s should be less than `%s' instead of `%s'",
      boundary_name,
      int_val_t(max_value).t_str().c_str(),
      int_val->t_str().c_str());
    boundary->set_valuetype(Value::V_ERROR);
  }
}

bool SubType::add_ttcn_length(Ttcn::LengthRestriction *lr, size_t restriction_index)
{
  string s;
  lr->append_stringRepr(s);
  Value *lower=NULL;
  lr->set_my_scope(my_owner->get_my_scope());
  lr->set_fullname(my_owner->get_fullname()+".<length_restriction_"+Int2string(restriction_index)+">");
  lr->chk(Type::EXPECTED_CONSTANT);
  lower = lr->get_is_range() ? lr->get_lower_value() : lr->get_single_value();
  if (!lower->get_my_scope()) FATAL_ERROR("no scope");
  if (lower->get_valuetype() != Value::V_INT) return false;
  if (lr->get_is_range()) {
    Value *upper = lr->get_upper_value();
    if (upper) {//HAS_UPPER
      if (upper->get_valuetype()!=Value::V_INT) return false;
      if (!upper->get_my_scope()) upper->set_my_scope(my_owner->get_my_scope());
      chk_boundary_valid(upper, INT_MAX, "upper boundary");
      if (upper->get_valuetype()!=Value::V_INT) return false;
      return set_ttcn_length(size_limit_t((size_t)lower->get_val_Int()->get_val()),
                             size_limit_t((size_t)upper->get_val_Int()->get_val()));
    } else {//INFINITY:
      chk_boundary_valid(lower, INT_MAX, "lower boundary");
      if (lower->get_valuetype()!=Value::V_INT) return false;
      return set_ttcn_length(size_limit_t((size_t)lower->get_val_Int()->get_val()),
                             size_limit_t(size_limit_t::INFINITE_SIZE));
    }
  }
  else {//SINGLE:
    chk_boundary_valid(lower, INT_MAX, "length restriction value");
    if (lower->get_valuetype()!=Value::V_INT) return false;
    return set_ttcn_length(size_limit_t((size_t)lower->get_val_Int()->get_val()),
                           size_limit_t((size_t)lower->get_val_Int()->get_val()));
  }
}

bool SubType::add_ttcn_pattern(Ttcn::PatternString* pattern, size_t restriction_index)
{
  pattern->set_my_scope(my_owner->get_my_scope());
  pattern->set_fullname(my_owner->get_fullname()+".<pattern_restriction_"+Int2string(restriction_index) + ">");
  switch (subtype) {
  case ST_CHARSTRING: {
    Error_Context cntxt(my_owner, "In character string pattern");
    pattern->chk_refs(Type::EXPECTED_CONSTANT);
	  pattern->join_strings();
  	if (!pattern->has_refs()) { // if chk_refs didn't remove all references then ignore
      pattern->chk_pattern();
      CharstringSubtypeTreeElement* cst_elem = new CharstringSubtypeTreeElement(StringPatternConstraint(pattern));
      if (charstring_st==NULL) {
        charstring_st = cst_elem;
      } else {
        charstring_st = new CharstringSubtypeTreeElement(CharstringSubtypeTreeElement::ET_INTERSECTION, charstring_st, cst_elem);
      }
    }
  } break;
  case ST_UNIVERSAL_CHARSTRING: {
    Error_Context cntxt(my_owner, "In universal string pattern");
    pattern->set_pattern_type(Ttcn::PatternString::USTR_PATTERN);
    pattern->chk_refs(Type::EXPECTED_CONSTANT);
	  pattern->join_strings();
  	if (!pattern->has_refs()) { // if chk_refs didn't remove all references then ignore
      pattern->chk_pattern();
      UniversalCharstringSubtypeTreeElement* ucst_elem = new UniversalCharstringSubtypeTreeElement(StringPatternConstraint(pattern));
      if (universal_charstring_st==NULL) {
        universal_charstring_st = ucst_elem;
      } else {
        universal_charstring_st = new UniversalCharstringSubtypeTreeElement(UniversalCharstringSubtypeTreeElement::ET_INTERSECTION, universal_charstring_st, ucst_elem);
      }
    }
  } break;
  default:
    my_owner->error("Pattern subtyping of type `%s' is not allowed", my_owner->get_typename().c_str());
    return false;
  }
  return true;
}

void SubType::print_full_warning() const
{
  my_owner->warning("The subtype of type `%s' is a full set, "
    "it does not constrain the root type.", my_owner->get_typename().c_str());
}

vector<SubTypeParse> * SubType::get_subtype_parsed() const {
  return parsed;
}

void SubType::chk()
{
  if ((checked!=STC_NO) || (subtype==ST_ERROR)) FATAL_ERROR("SubType::chk()");
  checked = STC_CHECKING;

  // check for circular subtype reference
  if (parent_subtype && !add_parent_subtype(parent_subtype)) {
    set_to_error();
    checked = STC_YES;
    return;
  }

  if (parsed) { // has TTCN-3 subtype constraint
    size_t added_count = 0;
    bool has_single = false, has_range = false,
         has_length = false, has_pattern = false;
    for (size_t i = 0; i < parsed->size(); i++) {
      bool added = false;
      SubTypeParse *parse = (*parsed)[i];
      switch (parse->get_selection()) {
      case SubTypeParse::STP_SINGLE:
        has_single = true;
        added = add_ttcn_single(parse->Single(),i);
        break;
      case SubTypeParse::STP_RANGE:
        has_range = true;
        added = add_ttcn_range(parse->Min(), parse->MinExclusive(),
                               parse->Max(), parse->MaxExclusive(), i,
                               has_single || has_length || has_pattern);
        break;
      case SubTypeParse::STP_LENGTH:
        has_length = true;
        added = add_ttcn_length(parse->Length(),i);
        break;
      case SubTypeParse::STP_PATTERN:
        has_pattern = true;
        added = add_ttcn_pattern(parse->Pattern(),i);
        break;
      default:
        FATAL_ERROR("SubType::chk(): invalid SubTypeParse selection");
      } // switch
      if (added) added_count++;
    }//for
    switch (subtype) {
    case ST_CHARSTRING:
    case ST_UNIVERSAL_CHARSTRING:
      if (has_single && has_range) {
        my_owner->error(
          "Mixing of value list and range subtyping is not allowed for type `%s'",
          my_owner->get_typename().c_str());
        set_to_error();
        checked = STC_YES;
        return;
      }
      break;
    default:
      // in other cases mixing of different restrictions (which are legal for
      // this type) is properly regulated by the TTCN-3 BNF itself
      break;
    }
    if (added_count<parsed->size()) {
      set_to_error();
      checked = STC_YES;
      return;
    }
    if (subtype==ST_ERROR) { checked = STC_YES; return; }

    if (parent_subtype) {
      if (is_subset(parent_subtype->get_root())==TFALSE) {
        my_owner->error("The subtype restriction is not a subset of the restriction on the parent type. "
          "Subtype %s is not subset of subtype %s", to_string().c_str(), parent_subtype->get_root()->to_string().c_str());
        set_to_error();
        checked = STC_YES;
        return;
      }
      intersection(parent_subtype->get_root());
    }
  } else if (asn_constraints) { // has ASN.1 subtype constraint
    SubtypeConstraint* asn_parent_subtype = NULL;
    if (parent_subtype) {
      // the type constraint of the ASN.1 type is already in the parent_subtype,
      // don't add it multiple times
      asn_parent_subtype = parent_subtype->get_root();
    } else {
      asn_parent_subtype = get_asn_type_constraint(my_owner);
    }
    asn_constraints->chk(asn_parent_subtype);
    root = asn_constraints->get_subtype();
    extendable = asn_constraints->is_extendable();
    extension  = asn_constraints->get_extension();
    // the TTCN-3 subtype will be the union of the root and extension parts
    // the ETSI ES 201 873-7 V4.1.2 (2009-07) document says to "ignore any extension markers"
    // but titan now works this way :)
    if (root) copy(root);
    if (extension) union_(extension);
  } else { // no constraints on this type -> this is an alias type, just copy the subtype from the other
    if (parent_subtype) {
      root = parent_subtype->root;
      extendable = parent_subtype->extendable;
      extension = parent_subtype->extension;
      copy(parent_subtype);
    } else {
      SubtypeConstraint* asn_parent_subtype = get_asn_type_constraint(my_owner);
      if (asn_parent_subtype) copy(asn_parent_subtype);
    }
  }

  // check if subtype is valid: it must not be an empty set (is_empty==TTRUE)
  // issue warning if subtype is given but is full set (is_full==TTRUE)
  // ignore cases of TUNKNOWN when compiler can't figure out if the aggregate
  // set is empty or full
  switch (subtype) {
  case ST_INTEGER:
    if (integer_st!=NULL) {
      if (integer_st->is_empty()==TTRUE) goto empty_error;
      if (integer_st->is_full()==TTRUE) {
        print_full_warning();
        delete integer_st;
        integer_st = NULL;
      }
    }
    break;
  case ST_FLOAT:
    if (float_st!=NULL) {
      if (float_st->is_empty()==TTRUE) goto empty_error;
      if (float_st->is_full()==TTRUE) {
        print_full_warning();
        delete float_st;
        float_st = NULL;
      }
    }
    break;
  case ST_BOOLEAN:
    if (boolean_st!=NULL) {
      if (boolean_st->is_empty()==TTRUE) goto empty_error;
      if (boolean_st->is_full()==TTRUE) {
        print_full_warning();
        delete boolean_st;
        boolean_st = NULL;
      }
    }
    break;
  case ST_VERDICTTYPE:
    if (verdict_st!=NULL) {
      if (verdict_st->is_empty()==TTRUE) goto empty_error;
      if (verdict_st->is_full()==TTRUE) {
        print_full_warning();
        delete verdict_st;
        verdict_st = NULL;
      }
    }
    break;
  case ST_BITSTRING:
    if (bitstring_st!=NULL) {
      if (bitstring_st->is_empty()==TTRUE) goto empty_error;
      if (bitstring_st->is_full()==TTRUE) {
        print_full_warning();
        delete bitstring_st;
        bitstring_st = NULL;
      }
    }
    break;
  case ST_HEXSTRING:
    if (hexstring_st!=NULL) {
      if (hexstring_st->is_empty()==TTRUE) goto empty_error;
      if (hexstring_st->is_full()==TTRUE) {
        print_full_warning();
        delete hexstring_st;
        hexstring_st = NULL;
      }
    }
    break;
  case ST_OCTETSTRING:
    if (octetstring_st!=NULL) {
      if (octetstring_st->is_empty()==TTRUE) goto empty_error;
      if (octetstring_st->is_full()==TTRUE) {
        print_full_warning();
        delete octetstring_st;
        octetstring_st = NULL;
      }
    }
    break;
  case ST_CHARSTRING:
    if (charstring_st!=NULL) {
      if (charstring_st->is_empty()==TTRUE) goto empty_error;
      if (charstring_st->is_full()==TTRUE) {
        print_full_warning();
        delete charstring_st;
        charstring_st = NULL;
      }
    }
    break;
  case ST_UNIVERSAL_CHARSTRING:
    if (universal_charstring_st!=NULL) {
      if (universal_charstring_st->is_empty()==TTRUE) goto empty_error;
      if (universal_charstring_st->is_full()==TTRUE) {
        print_full_warning();
        delete universal_charstring_st;
        universal_charstring_st = NULL;
      }
    }
    break;
  case ST_OBJID:
  case ST_RECORD:
  case ST_SET:
  case ST_ENUM:
  case ST_UNION:
  case ST_FUNCTION:
  case ST_ALTSTEP:
  case ST_TESTCASE:
    if (value_st!=NULL) {
      if (value_st->is_empty()==TTRUE) goto empty_error;
      if (value_st->is_full()==TTRUE) {
        print_full_warning();
        delete value_st;
        value_st = NULL;
      }
    }
    break;
  case ST_RECORDOF:
  case ST_SETOF:
    if (recof_st!=NULL) {
      if (recof_st->is_empty()==TTRUE) goto empty_error;
      if (recof_st->is_full()==TTRUE) {
        print_full_warning();
        delete recof_st;
        recof_st = NULL;
      }
    }
    break;
  default:
    FATAL_ERROR("SubType::chk()");
  }
  if ((length_restriction!=NULL) && (length_restriction->is_full()==TTRUE)) {
    delete length_restriction;
    length_restriction = NULL;
  }
  checked = STC_YES;
  return;

empty_error:
  my_owner->error("The subtype is an empty set");
  set_to_error();
  checked = STC_YES;
  return;
}

void SubType::dump(unsigned level) const
{
  string str = to_string();
  if (str.size()>0) DEBUG(level, "restriction(s): %s", str.c_str());
}

Int SubType::get_length_restriction() const
{
  if (checked!=STC_YES) FATAL_ERROR("SubType::get_length_restriction()");
  if (parsed==NULL) return -1; // only own length restriction counts
  if (length_restriction==NULL) return -1;
  if (TTRUE == length_restriction->is_empty()) return -1;
  return ( (length_restriction->get_minimal()==length_restriction->get_maximal()) ?
           (Int)(length_restriction->get_minimal().get_size()) :
           -1 );
}

bool SubType::zero_length_allowed() const
{
  if (checked!=STC_YES) FATAL_ERROR("SubType::zero_length_allowed()");
  if (parsed==NULL) return true; // only own length restriction counts
  if (length_restriction==NULL) return true;
  return length_restriction->is_element(size_limit_t(0));
}

bool SubType::length_allowed(size_t len) const
{
  if (checked != STC_YES) {
    FATAL_ERROR("SubType::length_allowed()");
  }
  if (length_restriction == NULL) {
    return true;
  }
  return length_restriction->is_element(size_limit_t(len));
}

string SubType::to_string() const
{
  if (root) {
    string ret_val(root->to_string());
    if (extendable) ret_val += ", ...";
    if (extension) {
      ret_val += ", ";
      ret_val += extension->to_string();
    }
    return ret_val;
  }
  return SubtypeConstraint::to_string();
}

////////////////////////////////////////////////////////////////////////////////

void SubType::generate_code(output_struct &)
{
  if (checked!=STC_YES) FATAL_ERROR("SubType::generate_code()");
}

void SubType::generate_json_schema(JSON_Tokenizer& json,
                                   bool allow_special_float /* = true */)
{
  bool has_value_list = false;
  size_t nof_ranges = 0;
  for (size_t i = 0; i < parsed->size(); ++i) {
    SubTypeParse *parse = (*parsed)[i];
    switch (parse->get_selection()) {
    case SubTypeParse::STP_SINGLE:
      // single values will be added later, all at once
      has_value_list = true;
      break;
    case SubTypeParse::STP_RANGE:
      ++nof_ranges;
      break;
    case SubTypeParse::STP_LENGTH: {
      Ttcn::LengthRestriction* len_res = parse->Length();
      Value* min_val = len_res->get_is_range() ? len_res->get_lower_value() :
        len_res->get_single_value();
      Value* max_val = len_res->get_is_range() ? len_res->get_upper_value() :
        len_res->get_single_value();
      const char* json_min = NULL;
      const char* json_max = NULL;
      switch (subtype) {
      case ST_RECORDOF:
      case ST_SETOF:
        // use minItems and maxItems for record of/set of
        json_min = "minItems";
        json_max = "maxItems";
        break;
      case ST_BITSTRING:
      case ST_HEXSTRING:
      case ST_OCTETSTRING:
      case ST_CHARSTRING:
      case ST_UNIVERSAL_CHARSTRING:
        // use minLength and maxLength for string types
        json_min = "minLength";
        json_max = "maxLength";
        break;
      default:
        FATAL_ERROR("SubType::generate_json_schema - length %d", subtype);
      }
      json.put_next_token(JSON_TOKEN_NAME, json_min);
      min_val->generate_json_value(json);
      if (max_val != NULL) {
        json.put_next_token(JSON_TOKEN_NAME, json_max);
        max_val->generate_json_value(json);
      }
      break; }
    case SubTypeParse::STP_PATTERN: {
      json.put_next_token(JSON_TOKEN_NAME, "pattern");
      char* json_pattern = parse->Pattern()->convert_to_json();
      json.put_next_token(JSON_TOKEN_STRING, json_pattern);
      Free(json_pattern);
      break; }
    default:
      break;
    }
  }

  bool need_anyOf = (subtype == ST_INTEGER || subtype == ST_FLOAT) &&
    (nof_ranges + (has_value_list ? 1 : 0) > 1);
  if (need_anyOf) {
    // there are multiple value range/value list restrictions,
    // they need to be grouped in an 'anyOf' structure
    json.put_next_token(JSON_TOKEN_NAME, "anyOf");
    json.put_next_token(JSON_TOKEN_ARRAY_START);
    json.put_next_token(JSON_TOKEN_OBJECT_START);
  }
  if (has_value_list) {
    // generate the value list into an enum
    json.put_next_token(JSON_TOKEN_NAME, "enum");
    json.put_next_token(JSON_TOKEN_ARRAY_START);
    generate_json_schema_value_list(json, allow_special_float, false);
    json.put_next_token(JSON_TOKEN_ARRAY_END);
    if (my_owner->has_as_value_union()) {
      // the original value list cannot always be recreated from the generated
      // JSON value list in case of "as value" unions (because there are no field
      // names)
      // the list needs to be regenerated with field names (as if it was a regular
      // union) under a new keyword (valueList)
      json.put_next_token(JSON_TOKEN_NAME, "valueList");
      json.put_next_token(JSON_TOKEN_ARRAY_START);
      generate_json_schema_value_list(json, allow_special_float, true);
      json.put_next_token(JSON_TOKEN_ARRAY_END);
    }
  }
  if (need_anyOf && has_value_list) {
    // end of the value list and beginning of the first value range
    json.put_next_token(JSON_TOKEN_OBJECT_END);
    json.put_next_token(JSON_TOKEN_OBJECT_START);
  }
  if (nof_ranges > 0) {
    switch (subtype) {
    case ST_INTEGER:
    case ST_FLOAT:
      generate_json_schema_number_ranges(json);
      break;
    case ST_CHARSTRING:
    case ST_UNIVERSAL_CHARSTRING: {
      // merge all string range restrictions into one JSON schema pattern
      char* pattern_str = mcopystrn("\"^[", 3);
      pattern_str = generate_json_schema_string_ranges(pattern_str);
      pattern_str = mputstrn(pattern_str, "]*$\"", 4);
      json.put_next_token(JSON_TOKEN_NAME, "pattern");
      json.put_next_token(JSON_TOKEN_STRING, pattern_str);
      Free(pattern_str);
      break; }
    default:
      FATAL_ERROR("SubType::generate_json_schema - range %d", subtype);
    }
  }
  if (need_anyOf) {
    // end of the 'anyOf' structure
    json.put_next_token(JSON_TOKEN_OBJECT_END);
    json.put_next_token(JSON_TOKEN_ARRAY_END);
  }
}

void SubType::generate_json_schema_value_list(JSON_Tokenizer& json,
                                              bool allow_special_float,
                                              bool union_value_list)
{
  for (size_t i = 0; i < parsed->size(); ++i) {
    SubTypeParse *parse = (*parsed)[i];
    if (parse->get_selection() == SubTypeParse::STP_SINGLE) {
      if (parse->Single()->get_valuetype() == Value::V_REFD) {
        Common::Assignment* ass = parse->Single()->get_reference()->get_refd_assignment();
        if (ass->get_asstype() == Common::Assignment::A_TYPE) {
          // it's a reference to another subtype, insert its value list here
          ass->get_Type()->get_sub_type()->generate_json_schema_value_list(json,
            allow_special_float, union_value_list);
        }
      }
      else {
        Ttcn::JsonOmitCombination omit_combo(parse->Single());
        do {
          parse->Single()->generate_json_value(json, allow_special_float,
            union_value_list, &omit_combo);
        } // only generate the first combination for the unions' "valueList" keyword
        while (!union_value_list && omit_combo.next());
      }
    }
  }
}

bool SubType::generate_json_schema_number_ranges(JSON_Tokenizer& json, bool first /* = true */)
{
  for (size_t i = 0; i < parsed->size(); ++i) {
    SubTypeParse *parse = (*parsed)[i];
    if (parse->get_selection() == SubTypeParse::STP_SINGLE) {
      if (parse->Single()->get_valuetype() == Value::V_REFD) {
        Common::Assignment* ass = parse->Single()->get_reference()->get_refd_assignment();
        if (ass->get_asstype() == Common::Assignment::A_TYPE) {
          // it's a reference to another subtype, insert its value ranges here
          first = ass->get_Type()->get_sub_type()->generate_json_schema_number_ranges(json, first);
        }
      }
    }
    else if (parse->get_selection() == SubTypeParse::STP_RANGE) {
      if (!first) {
        // the ranges are in an 'anyOf' structure, they need to be placed in an object
        json.put_next_token(JSON_TOKEN_OBJECT_END);
        json.put_next_token(JSON_TOKEN_OBJECT_START);
      }
      else {
        first = false;
      }
      // add the minimum and/or maximum values as numbers
      if (parse->Min() != NULL) {
        json.put_next_token(JSON_TOKEN_NAME, "minimum");
        parse->Min()->generate_json_value(json);
        json.put_next_token(JSON_TOKEN_NAME, "exclusiveMinimum");
        json.put_next_token(parse->MinExclusive() ? JSON_TOKEN_LITERAL_TRUE : JSON_TOKEN_LITERAL_FALSE);
      }
      if (parse->Max() != NULL) {
        json.put_next_token(JSON_TOKEN_NAME, "maximum");
        parse->Max()->generate_json_value(json);
        json.put_next_token(JSON_TOKEN_NAME, "exclusiveMaximum");
        json.put_next_token(parse->MaxExclusive() ? JSON_TOKEN_LITERAL_TRUE : JSON_TOKEN_LITERAL_FALSE);
      }
    }
  }
  return first;
}

char* SubType::generate_json_schema_string_ranges(char* pattern_str)
{
  for (size_t i = 0; i < parsed->size(); ++i) {
    SubTypeParse *parse = (*parsed)[i];
    if (parse->get_selection() == SubTypeParse::STP_SINGLE) {
      if (parse->Single()->get_valuetype() == Value::V_REFD) {
        Common::Assignment* ass = parse->Single()->get_reference()->get_refd_assignment();
        if (ass->get_asstype() == Common::Assignment::A_TYPE) {
          // it's a reference to another subtype, insert its string ranges here
          pattern_str = ass->get_Type()->get_sub_type()->generate_json_schema_string_ranges(pattern_str);
        }
      }
    }
    else if (parse->get_selection() == SubTypeParse::STP_RANGE) {
      // insert the string range into the pattern string
      string lower_str = (subtype == ST_CHARSTRING) ? parse->Min()->get_val_str() :
        ustring_to_uft8(parse->Min()->get_val_ustr());
      string upper_str = (subtype == ST_CHARSTRING) ? parse->Max()->get_val_str() :
        ustring_to_uft8(parse->Max()->get_val_ustr());
      pattern_str = mputprintf(pattern_str, "%s-%s", lower_str.c_str(), upper_str.c_str());
    }
  }
  return pattern_str;
}

void SubType::generate_json_schema_float(JSON_Tokenizer& json)
{
  bool has_nan = float_st->is_element(make_ttcn3float(REAL_NAN));
  bool has_pos_inf = float_st->is_element(make_ttcn3float(REAL_INFINITY));
  bool has_neg_inf = float_st->is_element(make_ttcn3float(-REAL_INFINITY));
  bool has_special = has_nan || has_pos_inf || has_neg_inf;
  bool has_number = false;
  for (size_t i = 0; i < parsed->size() && !has_number; ++i) {
    // go through the restrictions and check if at least one number is allowed
    SubTypeParse *parse = (*parsed)[i];
    switch (parse->get_selection()) {
    case SubTypeParse::STP_SINGLE: {
      Real r = parse->Single()->get_val_Real();
      if (r == r && r != REAL_INFINITY && r != -REAL_INFINITY) {
        // a single value other than NaN, INF and -INF is a number
        has_number = true;
      }
      break; }
    case SubTypeParse::STP_RANGE: {
      if (parse->Min() != NULL) {
        if (parse->Min()->get_val_Real() != REAL_INFINITY) {
          // a minimum value other than INF means a number is allowed
          has_number = true;
        }
      }
      if (parse->Max() != NULL) {
        // a maximum value other than -INF means a number is allowed
        if (parse->Max()->get_val_Real() != -REAL_INFINITY) {
          has_number = true;
        }
      }
      break; }
    default:
      break;
    }
  }
  if (has_number && has_special) {
    json.put_next_token(JSON_TOKEN_NAME, "anyOf");
    json.put_next_token(JSON_TOKEN_ARRAY_START);
    json.put_next_token(JSON_TOKEN_OBJECT_START);
  }
  if (has_number) {
    json.put_next_token(JSON_TOKEN_NAME, "type");
    json.put_next_token(JSON_TOKEN_STRING, "\"number\"");
    // generate the restrictions' schema elements here
    // (the 2nd parameter makes sure that NaN, INF and -INF are ignored)
    generate_json_schema(json, false);
  }
  if (has_number && has_special) {
    json.put_next_token(JSON_TOKEN_OBJECT_END);
    json.put_next_token(JSON_TOKEN_OBJECT_START);
  }
  if (has_special) {
    json.put_next_token(JSON_TOKEN_NAME, "enum");
    json.put_next_token(JSON_TOKEN_ARRAY_START);
    if (has_nan) {
      json.put_next_token(JSON_TOKEN_STRING, "\"not_a_number\"");
    }
    if (has_pos_inf) {
      json.put_next_token(JSON_TOKEN_STRING, "\"infinity\"");
    }
    if (has_neg_inf) {
      json.put_next_token(JSON_TOKEN_STRING, "\"-infinity\"");
    }
    json.put_next_token(JSON_TOKEN_ARRAY_END);
  }
  if (has_number && has_special) {
    json.put_next_token(JSON_TOKEN_OBJECT_END);
    json.put_next_token(JSON_TOKEN_ARRAY_END);
  }
}

} // namespace Common