File: ResolveTypes.cpp

package info (click to toggle)
fauhdlc 20180504-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,956 kB
  • sloc: cpp: 23,188; ansic: 6,077; yacc: 3,764; lex: 763; makefile: 605; sh: 494; xml: 403
file content (2326 lines) | stat: -rw-r--r-- 55,645 bytes parent folder | download | duplicates (3)
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
/* $Id$
 * ResolveTypes: perform type analysis.
 *
 * Copyright (C) 2007-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#include <cassert>
#include <algorithm>
#include "frontend/visitor/ResolveTypes.hpp"

#include "frontend/ast/ConstInteger.hpp"
#include "frontend/ast/ConstReal.hpp"
#include "frontend/ast/Entity.hpp"
#include "frontend/ast/SymbolDeclaration.hpp"
#include "frontend/ast/ValDeclaration.hpp"
#include "frontend/ast/SignalDeclaration.hpp"
#include "frontend/ast/ConstantDeclaration.hpp"
#include "frontend/ast/Expression.hpp"
#include "frontend/ast/IfStat.hpp"
#include "frontend/ast/NullStat.hpp"
#include "frontend/ast/ForLoopStat.hpp"
#include "frontend/ast/WhileLoopStat.hpp"
#include "frontend/ast/NextStat.hpp"
#include "frontend/ast/VarAssignStat.hpp"
#include "frontend/ast/WaitStat.hpp"
#include "frontend/ast/ExitStat.hpp"
#include "frontend/ast/SigAssignStat.hpp"
#include "frontend/ast/WaveFormElem.hpp"
#include "frontend/ast/ReturnStat.hpp"
#include "frontend/ast/ProcCallStat.hpp"
#include "frontend/ast/AssertStat.hpp"
#include "frontend/ast/VarDeclaration.hpp"
#include "frontend/ast/DiscreteRange.hpp"
#include "frontend/ast/CaseStat.hpp"
#include "frontend/ast/CaseAlternative.hpp"
#include "frontend/ast/Others.hpp"
#include "frontend/ast/Architecture.hpp"
#include "frontend/ast/AssociationElement.hpp"
#include "frontend/ast/FunctionDeclaration.hpp"
#include "frontend/ast/ProcedureDeclaration.hpp"
#include "frontend/ast/CompInstStat.hpp"
#include "frontend/ast/Package.hpp"
#include "frontend/ast/PackageBody.hpp"
#include "frontend/ast/Process.hpp"
#include "frontend/ast/SubprogBody.hpp"
#include "frontend/ast/CondalSigAssign.hpp"
#include "frontend/ast/EnumerationType.hpp"
#include "frontend/ast/PhysicalType.hpp"
#include "frontend/ast/PhysicalTypeUnit.hpp"
#include "frontend/ast/RangeConstraintType.hpp"
#include "frontend/ast/UnconstrainedArrayType.hpp"
#include "frontend/ast/RecordType.hpp"
#include "frontend/ast/Aggregate.hpp"
#include "frontend/ast/Subscript.hpp"
#include "frontend/ast/TypeConversion.hpp"
#include "frontend/ast/ConstantDeclaration.hpp"
#include "frontend/reporting/ErrorRegistry.hpp"
#include "frontend/reporting/CompileError.hpp"
#include "util/MiscUtil.hpp"
#include "frontend/ast/SimpleName.hpp"
#include "frontend/ast/SelectedName.hpp"
#include "frontend/ast/AttributeName.hpp"
#include "frontend/ast/FunctionCall.hpp"
#include "frontend/ast/Slice.hpp"
#include "frontend/ast/TemporaryName.hpp"
#include "frontend/ast/ReturnStat.hpp"
#include "frontend/ast/AttributeSpecification.hpp"
#include "frontend/visitor/LookupTypes.hpp"
#include "frontend/visitor/UnconstraintBounds.hpp"

#define SLICE_DEBUG 0
#define SELECTION_DEBUG 0
#define FUNCCALLS_DEBUG 0

namespace ast {

void
ResolveTypes::visit(CompInstStat &node)
{
	if (node.genericMap != NULL) {
		for (std::list<AssociationElement*>::const_iterator i = 
			node.genericMap->begin(); i != node.genericMap->end();
			i++) {
	
			//FIXME this may fail for positional association.
			//      (because there, the formal is missing, but
			//      the wanted type is the projected argument's
			//      type)
			this->typeCandidates.clear();
			(*i)->accept(*this);
			if (! this->needUniqueType(**i)) {
				return;
			}
			// pin down type
			(*i)->accept(*this);
			assert(this->typeCandidates.size() == 1);
		}
	}

	if (node.portMap != NULL) {
		for (std::list<AssociationElement*>::const_iterator i = 
			node.portMap->begin(); i != node.portMap->end();
			i++) {
	
			//FIXME same as above.
			this->typeCandidates.clear();
			(*i)->accept(*this);

			bool ret = this->needUniqueType(**i);
			if (! ret) {
				// error already reported, skip.
				continue;
			}

			// pin down type
			(*i)->accept(*this);
			assert(this->typeCandidates.size() == 1);
		}
	}
	this->typeCandidates.clear();
}

void
ResolveTypes::visit(AssociationElement &node)
{
	bool mustSingle = (this->typeCandidates.size() == 1);

	if (node.formal != NULL) {
		node.formal->accept(*this);
	}
	
	if (node.actual != NULL) {
		node.actual->accept(*this);
	}

	if (mustSingle) {
		this->needUniqueType(node);
	}
}

void
ResolveTypes::visit(UnconstrainedArrayType &node)
{
	assert(node.indexTypes != NULL);
	node.baseType = BASE_TYPE_ARRAY;
	assert(node.elementType != NULL);
	node.elementType->accept(*this);
}

void
ResolveTypes::visit(DiscreteRange &node)
{
	if (node.rangeName != NULL) {
		this->processDRByName(node);
		return;
	}

	assert(node.from != NULL);
	assert(node.to != NULL);

	// gather types of from.
	typeSetT backup = this->typeCandidates;
	node.from->accept(*this);
	typeSetT fromT = this->typeCandidates;
	this->typeCandidates = backup;
	node.to->accept(*this);

	//propagate a RangeConstraintType based on the left bound 
	//(usually the index type) with an additional constraint
	if ((fromT.size() == 1) && (this->typeCandidates.size() == 1)) {
		const TypeDeclaration *left = *fromT.begin();
		const TypeDeclaration *right = *this->typeCandidates.begin();

		enum BaseType resolved = (left->baseType && right->baseType);
		node.baseType = ResolveTypes::transformBaseType(resolved, 
								node.location);

		if (node.baseType != BASE_TYPE_RANGE_INT) {
			CompileError *ce = new CompileError(node, 
				"Not a discrete type in discrete range.");
			ErrorRegistry::addError(ce);
		}

		// if backup is empty, we'll need to deliver all possible
		// types -> keep types from right hand side.
		if (! backup.empty()) {
			this->typeCandidates = backup;
		}

		// generate range type
		SubtypeIndication *rangeType 
				= new SubtypeIndication(
						left, 
						Location("temporary"));
		rangeType->constraint = &node;
		node.type = rangeType;

		return;
	} else {
		// can this happen? at what place?
		std::cerr << "BOOM: The unexpected happened at " 
			<< node.location << std::endl;

		std::cerr << "left:" << std::endl;
		this->debugPrintTypes(fromT);
		std::cerr << "right: " << std::endl;
		this->debugPrintTypes(this->typeCandidates);
		std::cerr << "wanted: " << std::endl;
		this->debugPrintTypes(backup);
		assert(false);
	}
}

void
ResolveTypes::processDRByName(DiscreteRange &node)
{
	assert(node.rangeName != NULL);
	bool mustSingle = this->typeCandidates.size() == 1;
	node.rangeName->accept(*this);

	if (mustSingle) {
		this->needUniqueType(node);
	}
}

void 
ResolveTypes::visit(VarAssignStat &node)
{
	assert(node.target);
	assert(node.source);

	// traverse to target
	this->typeCandidates.clear();
	node.target->accept(*this);
	if (! this->needUniqueType(node)) {
		return;
	}

	// pin down type of target
	node.target->accept(*this);
	assert(this->typeCandidates.size() == 1);

	//traverse to source
	node.source->accept(*this);
	if (! this->needUniqueType(node)) {
		return;
	}
}

void
ResolveTypes::visit(SigAssignStat &node)
{
	assert(node.target);
	assert(node.waveForm);

	// traverse to target
	this->typeCandidates.clear();
	node.target->accept(*this);

	if (! this->needUniqueType(node)) {
		return;
	}

	// pin down type of target
	node.target->accept(*this);
	assert(this->typeCandidates.size() == 1);

	typeSetT backup = this->typeCandidates;
	//traverse to every element of the waveform.
	for (std::list<WaveFormElem*>::const_iterator i = 
		node.waveForm->begin();
		i != node.waveForm->end(); i++) {

		this->typeCandidates = backup;
		(*i)->accept(*this);

		if (! this->needUniqueType(node)) {
			return;
		}
	}
}

void
ResolveTypes::visit(WaveFormElem &node)
{
	assert(node.value != NULL);
	node.value->accept(*this);
	
	if (node.delay != NULL) {
		typeSetT backup = this->typeCandidates;
		this->typeCandidates.clear();

		const TypeDeclaration *t = 
			this->symbolTable.getStdStandardType("time");
		this->typeCandidates.insert(t);
		node.delay->accept(*this);
		this->needUniqueType(*node.delay);

		this->typeCandidates = backup;
	}
}

void
ResolveTypes::visit(SimpleName &node)
{
	bool mustSingle = (this->typeCandidates.size() == 1);

	SymbolFilter tf = SymbolFilter(node.candidates, this->typeCandidates);
	tf.apply();

	if (mustSingle) {
		this->needUniqueType(node);
	} else {
		this->needNotEmpty(node.location);
	}
}

void
ResolveTypes::visit(SelectedName &node)
{
	/** nested class to transform a record element symbol to the 
	 *  parent record type.
	 */
	class SelectionFilter : public SymbolFilter {
	public:
		/** c'tor
		 *  @param cands candidate symbols
		 *  @param wantTypes list of wanted types
		 */
		SelectionFilter(std::list<Symbol*> &cands,
				typeSetT &wantTypes
				) : SymbolFilter (cands, wantTypes) {}
	private:
		/** operation to transform an element to a parent record type.
		 *  @param element RecordTypeElement symbol
		 *  @return parent RecordType.
		 */
		virtual const TypeDeclaration*
		operator()(Symbol *element) const {

			assert(element);
			if (element->type != SYMBOL_ELEMENT) {
				return NULL;
			}

			RecordTypeElement *rel = 
				dynamic_cast<RecordTypeElement*>(
						&(element->declaration));
			assert(rel);
			assert(rel->parent);
			return rel->parent;
		}

	};

	assert(node.prefix);
	assert(node.name);

#if SELECTION_DEBUG
	std::cerr << "Selection enter (prefix at " << node.prefix->location
		<< "=" 
		<< node.prefix << ")" 
		<< std::endl;
	ResolveTypes::debugPrintTypes(this->typeCandidates);
#endif

	bool mustSingle = (this->typeCandidates.size() == 1);

	// 1. filter on current types.
	SymbolFilter tf = SymbolFilter(node.candidates, this->typeCandidates);
	tf.apply();

#if SELECTION_DEBUG
	std::cerr << "Selection after SymbolFilter" << std::endl;
	ResolveTypes::debugPrintTypes(this->typeCandidates);
#endif

	size_t numSyms = node.candidates.size();

	typeSetT backup = this->typeCandidates;
	this->typeCandidates.clear();

	// build list of possible record types of prefix
	SelectionFilter sf = SelectionFilter(node.candidates, 
						this->typeCandidates);
	sf.apply();

#if SELECTION_DEBUG
	std::cerr << "prefix candidates (before traversal)" << std::endl;
	ResolveTypes::debugPrintTypes(this->typeCandidates);
#endif

	// traverse to prefix
	node.prefix->accept(*this);

#if SELECTION_DEBUG
	std::cerr << "prefix candidates (after traversal)" << std::endl;
	ResolveTypes::debugPrintTypes(this->typeCandidates);
#endif

	// prefix types were filtered, filter out candidate symbols which
	// still match.
	sf.apply();

	this->needNotEmpty(node.prefix->location);

	// restore old candidates 
	this->typeCandidates = backup;
	if (node.candidates.size() != numSyms) {
#if SELECTION_DEBUG
		std::cerr << "Selection: again" << std::endl;
#endif
		// candidates got reduced by the SelectionFilter. Filter by
		// wanted types again.
		tf.apply();

		// apply types to prefix
		backup = this->typeCandidates;
		this->typeCandidates.clear();
		// regenerate the only possible type the prefix may take
		sf.apply();
		// it *must* be one
		assert(this->typeCandidates.size() == 1);
		// apply it to the prefix
		node.prefix->accept(*this);

		this->typeCandidates = backup;
	}

	if (mustSingle) {
		this->needUniqueType(node);
	} else {
		this->needNotEmpty(node.location);
	}
}

void
ResolveTypes::visit(AttributeName &node)
{
	typedef void (ResolveTypes::*rtANMethT)(AttributeName &);

	// FIXME isn't that crack? cf. comments in BuiltinSymbolTable
	std::map<std::string, rtANMethT> attrCals;
	attrCals["range"] = &ResolveTypes::processRangeAttr;
	attrCals["left"] = &ResolveTypes::processLeftAttr;
	attrCals["right"] = &ResolveTypes::processRightAttr;
	attrCals["event"] = &ResolveTypes::processEventAttr;
	// TODO
	//
	// FIXME actually processEventAttr looks good to be 
	//       used as a more general way to resolve attributes.

	std::map<std::string, rtANMethT>::iterator i = 
		attrCals.find(*node.name);

	if (i == attrCals.end()) {
		std::cerr << "cannot find attribute <" << *node.name
			<< ">." << std::endl;
		assert(false);
	}
	
	(this->*(i->second))(node);
}

void
ResolveTypes::processRangeAttr(AttributeName &node)
{
	assert(node.prefix != NULL);

	bool mustSingle = this->typeCandidates.size() == 1;
	typeSetT backup = this->typeCandidates;

	this->typeCandidates.clear();
	node.prefix->accept(*this);
	IndexTypeFilter itf = 
		IndexTypeFilter(this->typeCandidates, backup, 1);
	itf.apply();

	if (mustSingle) {
		bool res = this->needUniqueType(node);
		if (res) {
			switch ((*backup.begin())->baseType) {
			case BASE_TYPE_INTEGER:
				node.baseType = BASE_TYPE_RANGE_INT;
				break;

			case BASE_TYPE_REAL:
				node.baseType = BASE_TYPE_RANGE_REAL;
				break;

			default:
				assert(false);
			}

			// pin down type...
			node.prefix->accept(*this);
		}
	}
	this->typeCandidates = backup;
}

void
ResolveTypes::processEventAttr(AttributeName &node)
{
	assert(node.prefix != NULL);

	// first traverse to prefix. Type must be clear from the
	// context.
	typeSetT backup = this->typeCandidates;
	this->typeCandidates.clear();
	node.prefix->accept(*this);

	// there may be only one type for the prefix!
	bool res = this->needUniqueType(node);
	if (res) {
		node.prefix->accept(*this);
	}

	this->typeCandidates = backup;
	// filter on symbols
	SymbolFilter tf = SymbolFilter(node.candidates, this->typeCandidates);
	tf.apply();
}

void
ResolveTypes::processLeftAttr(AttributeName &node)
{
	assert(false);
}

void
ResolveTypes::processRightAttr(AttributeName &node)
{
	assert(false);
}

void
ResolveTypes::visit(FunctionCall &node)
{
	assert(node.subprog);
	assert(node.arguments);

	bool mustSingle = (this->typeCandidates.size() == 1);

	SymbolFilter rf = SymbolFilter(node.subprog->candidates,
						this->typeCandidates);
	rf.apply();

	// minor check that no positional arguments are present after named
	// arguments (TODO should go somewhere else)
	// FIXME assert's, that no named areguments are present at all,
	// since these are currently not supported.
	bool positional = true;
	for (std::list<AssociationElement*>::const_iterator i = 
		node.arguments->begin(); i != node.arguments->end(); i++) {

		if ((*i)->formal) {
			positional = false;
			assert(false); //named arguments not yet supported.
		} else {
			if (! positional) {
				CompileError *ce = new CompileError(
						(*i)->formal->location,
						"Positional argument after "
						"named argument");
				ErrorRegistry::addError(ce);
				return;
			}
		}
	}

	typeSetT backup = this->typeCandidates;
	this->typeCandidates.clear();
	
	this->processSubprogCall(node, mustSingle);
	// filter again, to reduce return types.
	this->typeCandidates = backup;
	rf.apply();

	if (mustSingle && node.subprog->candidates.size() == 1) {
		node.definition = 
			dynamic_cast<FunctionDeclaration*>(
				&node.subprog->candidates.front()->declaration
				);
		assert(node.definition != NULL);
	}

	if (mustSingle) {
		this->needUniqueType(node);
	}
}

void
ResolveTypes::visit(ProcCallStat &node)
{
	assert(node.subprog);
	assert(node.arguments);

	this->typeCandidates.clear();
	this->processSubprogCall(node, true);
	if (node.subprog->candidates.size() != 1) {
		// error reported from AssociationElement list already
		return;
	}

	Symbol *sym = node.subprog->candidates.front();
	node.definition = 
		dynamic_cast<ProcedureDeclaration*>(&sym->declaration);
}

void
ResolveTypes::visit(SubtypeIndication &node)
{
	this->typeCandidates.clear();
	// ignore typeName, that's already handled in c'tor
	
	// FIXME that's too easy... constraint must be of a
	//       specific type!
	if (node.constraint != NULL) {
		node.constraint->accept(*this);
		this->typeCandidates.clear();
	}

	if (node.resolutionFunction != NULL) {
		SimpleName *sn =
			dynamic_cast<SimpleName*>(node.resolutionFunction);
		if (! sn) {
			// FIXME better error message. Imho this can
			// only happen for selected names. However
			// resolution functions should only be expanded names,
			// which in turn would result in SimpleName.
			CompileError *ce = 
				new CompileError(
					*node.resolutionFunction,
					"There is something wrong");
			ErrorRegistry::addError(ce);
			return;
		}
		this->processResolutionFunction(node, *sn);
	}

	if (node.indexConstraint == NULL) {
		return;
	}

	/* base must be unconstraint array */
	const UnconstrainedArrayType *base = 
		dynamic_cast<const UnconstrainedArrayType*>(
				this->findBaseType(node.declaration));
	assert(base != NULL);
	assert(base->indexTypes != NULL);

	if (node.indexConstraint->size() != base->indexTypes->size()) {
		CompileError *ce = new CompileError(node, 
			"wrong number of indices for index constraint.");
		ErrorRegistry::addError(ce);
		return;
	}

	std::list<TypeDeclaration*>::iterator j = base->indexTypes->begin();
	for (std::list<DiscreteRange*>::iterator i = 
		node.indexConstraint->begin();
		i != node.indexConstraint->end(); i++, j++) {

		this->typeCandidates.insert(*j);
		(*i)->accept(*this);
		this->typeCandidates.clear();
	}
}

void
ResolveTypes::processResolutionFunction(
	SubtypeIndication &type,
	SimpleName &resolver
)
{
	assert(type.resolutionFunction != NULL);
	
	// 1. filter by return type.
	this->typeCandidates.insert(&type);
	SymbolFilter sf = 
		SymbolFilter(resolver.candidates, this->typeCandidates);
	sf.apply();
	this->typeCandidates.clear();

	// 2. filter by parameter type (one dimensional array, element type
	//    must match this type.
	
	for (std::list<Symbol *>::iterator i = resolver.candidates.begin();
		i != resolver.candidates.end(); 
		/* nothing */) {

		FunctionDeclaration *fd = 
			dynamic_cast<FunctionDeclaration*>(
				&(*i)->declaration);
		if (fd == NULL) {
			i = resolver.candidates.erase(i);
			continue;
		}

		if ((fd->arguments == NULL) || (fd->arguments->size() != 1)) {
			i = resolver.candidates.erase(i);
			continue;
		}

		const ValDeclaration *arg = fd->arguments->front();
		if (! arg->isUnconstraint()) {
			i = resolver.candidates.erase(i);
			continue;
		}

		if (arg->storageClass != ValDeclaration::OBJ_CLASS_CONSTANT) {
			i = resolver.candidates.erase(i);
			continue;
		}

		const TypeDeclaration *elementT = 
			ResolveTypes::subscribedType(*arg->subtypeIndic,
							1,
							resolver.location);
		if (elementT == NULL) {
			i = resolver.candidates.erase(i);
			continue;
		}

		if (! ResolveTypes::baseTypeEqual(*elementT, type)) {
			i = resolver.candidates.erase(i);
			continue;
		}

		i++;
						
	}
	
	if (resolver.candidates.size() != 1) {
		CompileError *ce = 
			new CompileError(resolver,
					"Type Error for resolution function");
		ErrorRegistry::addError(ce);
	}
}


template <typename T>
void
ResolveTypes::processSubprogCall(T &node, bool mustSingle)
{
	assert(node.arguments);
	assert(node.subprog);

	/* FIXME this is not quite right yet.
	 * Example: 
	 * 	x : integer;
	 *
	 *	if 2 = x then
	 *	...
	 *
	 * Currently, 2 as first operand will resolve = uniquely to
	 * "="(anon, anon: universal_integer);
	 *
	 * Hence it will result in a type error, because x is of a different
	 * type (and as non-universal type not convertible).
	 */

	//! filter out symbols by number of arguments.
	/** FIXME currently this filter relies on no formal parts being 
	 *        present, because these could indicate individual 
	 *        association. 
	 *        For individual association, one approach would be to 
	 *        flatten every type (which has the caveat, that conversion
	 *        functions cannot be handled that way, since these need to
	 *        be done on the complete composite type, if no individual
	 *        association is used) and check again.
	 *        Another possible approach, would be to count the different
	 *        formal parts with different names.
	 *        This filter also doesn't take into account yet, that 
	 *        formal parts are a means of reducing the candidates as 
	 *        well (e.g. if a formal part with given name is not present
	 *        for a candidate).
	 */    
	class ArgcFilter {
	public:
		/** c'tor
		 *  @param cands candidate symbols of callable's
		 *  @param args current argument list
		 */
		ArgcFilter(std::list<Symbol*> &cands,
				const std::list<AssociationElement*> &args
				) : sc(cands), a(args) {}

		/** apply the filter */
		void apply(void) {
			for (std::list<Symbol*>::iterator i = 
				this->sc.begin(); i != this->sc.end();
				/* nothing */) {

				//FIXME see long comment above

				Callable *c = 
					dynamic_cast<Callable*>(
							&(*i)->declaration);

				if (c == NULL) {
					i = this->sc.erase(i);
					continue;
				}

				if (   (c->arguments == NULL) 
				    && this->a.empty()) {

				    	i++;
					continue;
				} 

				if (c->arguments == NULL) {
					// args not empty
					i = this->sc.erase(i);
					continue;
				}

				if (c->arguments->size() != this->a.size()) {
					i = this->sc.erase(i);
					continue;
				}
				i++;
			}
		}
	private:
		//!candidates
		std::list<Symbol*> &sc;
		//!arguments
		const std::list<AssociationElement*> &a;
	};

	// filter on number of arguments
	ArgcFilter argcf = ArgcFilter(
			node.subprog->candidates,
			*node.arguments);
	argcf.apply();

	ProjectPositionalArg paf = ProjectPositionalArg(
					node.subprog->candidates,
					this->typeCandidates,
					0);
	size_t firstSinglePos = node.arguments->size();

	for (std::list<AssociationElement*>::const_iterator i = 
		node.arguments->begin(); i != node.arguments->end(); 
		i++, paf.position++) {

		//TODO: named arguments.
		assert((*i)->formal == NULL);
		assert((*i)->actual); /* FIXME open associations */
		
		this->typeCandidates.clear();
		// fetch *all* possible types into typeCandidates.
		paf.apply();

		// if there's only a single type candidate, recall 
		// the position so that we can avoid duplicate traversal
		// for type pinning
		if ((this->typeCandidates.size() == 1)
		   && (paf.position < firstSinglePos)) {
		   	firstSinglePos = paf.position;
		}

#if FUNCCALLS_DEBUG
		std::cerr << (*i)->location << ": " << *i 
			<< " all possible types " << std::endl;
		this->debugPrintTypes(this->typeCandidates);
#endif /* FUNCCALLS_DEBUG */

		// traverse to actual part
		(*i)->accept(*this);

#if FUNCCALLS_DEBUG
		std::cerr << (*i)->location << ": " << *i 
			<< " reduced set" << std::endl;
		this->debugPrintTypes(this->typeCandidates);
#endif /* FUNCCALLS_DEBUG */

		// now typeCandidates contains the set of possbile types, that
		// can be matched by the argument. filter the possible types
		// again (actually possible types are reduced not by 
		// actual types, but rather by a reduced set of possible types).
		paf.apply();
	}

	this->typeCandidates.clear();

	if (! mustSingle) {
		return;
	}

	// check if unique
	if (1 < node.subprog->candidates.size()) {
		std::string msg = "Ambiguous subprogram call of <";
		msg += *node.subprog->name + ">. ";

#if FUNCCALLS_DEBUG
		std::cerr << "TYPE ERROR: " << node << std::endl;
#endif /* FUNCCALLS_DEBUG */
		CompileError *ce = new CompileError(node, msg);
		ErrorRegistry::addError(ce);
		return;
	}

	if (node.subprog->candidates.empty()) {
		return;
	}

	// now each argument *must* result in a single type!
	// pin these down.
	paf.position = 0;
	for (std::list<AssociationElement*>::const_iterator i = 
		node.arguments->begin(); i != node.arguments->end(); 
		i++, paf.position++) {

		// break early if other arguments already have been
		// pinned down.
		if (paf.position == firstSinglePos) {
			break;
		}

		//TODO: named arguments.
		assert((*i)->formal == NULL);
		assert((*i)->actual); /* FIXME open associations */
		
		this->typeCandidates.clear();
		// fetch *all* possible types into typeCandidates
		// (can be only one)
		paf.apply();
		assert(this->typeCandidates.size() == 1);

		// traverse to actual part to pin it down
		(*i)->accept(*this);
	}
	this->typeCandidates.clear();
}

void
ResolveTypes::visit(Subscript &node)
{
	// FIXME 6.4 (and 10.5) make it not explicitely clear, if the 
	//       index constraints may be used to eliminate possibile
	//       interpretions in the first place.
	//
	//       Is this valid?
	//
	//	 type foo is range 1 to 10;
	//	 type bar is range 1 to 20;
	//       type x is array(foo) of integer;
	//       type y is array(bar) of integer;
	//
	//       function f() return x;
	//       function f() return y;
	//
	//       var a : foo;
	//       var b : bar;
	//       var i : integer;
	//
	//       a := f(a);
	//       a := f(b);
	//
	// IMHO the only rule here would be that the prefix must be an array
	//      type, so wether it has the correct number of indices
	//      and wether the index types are set correct shouldn't matter.
	//
	// The current implementation eliminates candidates of f via 
	// index types though.
	assert(node.source != NULL);
	assert(node.indices != NULL);

	typeSetT wantTypes = this->typeCandidates;
	bool mustSingle = wantTypes.size() == 1;
	this->typeCandidates.clear();

	node.source->accept(*this);

	SubscriptFilter sf = SubscriptFilter(this->typeCandidates, wantTypes);
	sf.apply();

	typeSetT backup = this->typeCandidates;
	if (! mustSingle) {
		// fetch index types
		unsigned int nIdx = 1;
		for (std::list<Expression*>::iterator i = node.indices->begin();
			i != node.indices->end(); i++) {
			
			this->typeCandidates.clear();

			IndexTypeFilter itf = 
				IndexTypeFilter(backup, 
						this->typeCandidates, nIdx);
			itf.apply();
			(*i)->accept(*this);

			nIdx++;
		}
	}
	this->typeCandidates = backup;

	if (mustSingle) {
		// pin down source
		node.source->accept(*this);

		// and pin down index types
		unsigned int nIdx = 1;
		for (std::list<Expression*>::iterator i = node.indices->begin();
			i != node.indices->end(); i++) {

			this->typeCandidates.clear();
			IndexTypeFilter itf = 
				IndexTypeFilter(backup, 
						this->typeCandidates, nIdx);
			itf.apply();
			(*i)->accept(*this);

			if (! this->needUniqueType(**i)) {
				this->typeCandidates = backup;
				return;
			}
			nIdx++;
		}
	}

	this->typeCandidates = wantTypes;
	if (mustSingle) {
		bool ret = this->needUniqueType(node);
		if (! ret) {
			return;
		}

		// check number of indices
		const TypeDeclaration *d = 
			ResolveTypes::findBaseType(node.source->type);
		const UnconstrainedArrayType *ua = 
			dynamic_cast<const UnconstrainedArrayType*>(d);
		assert(ua != NULL);
		if (node.indices->size() < ua->indexTypes->size()) {
			CompileError *ce = 
				new CompileError(node, "Too few indices");
			ErrorRegistry::addError(ce);
		}

		if (node.indices->size() > ua->indexTypes->size()) {
			CompileError *ce = 
				new CompileError(node, "Too many indices");
			ErrorRegistry::addError(ce);
		}

	} else {
		this->needNotEmpty(node.location);
	}
}

void
ResolveTypes::visit(ConstInteger &node)
{
	if (node.physUnit != NULL) {
		// unit is a physical type. Let SimpleName handle this.
		node.physUnit->accept(*this);
		node.type = node.physUnit->type;
		return;
	}

	const TypeDeclaration *uInt = 
		this->symbolTable.getStdStandardType("__universal_integer__");
	assert(uInt);
	this->processUniversal(node, uInt, BASE_TYPE_INTEGER);
}

void
ResolveTypes::visit(ConstReal &node)
{
	const TypeDeclaration *uReal = 
		this->symbolTable.getStdStandardType("__universal_real__");
	assert(uReal);
	this->processUniversal(node, uReal, BASE_TYPE_REAL);
}

void
ResolveTypes::visit(Aggregate &node)
{
	//FIXME handles only array aggregates atm.
	assert(node.associations != NULL);
	bool isArray = false;

	for (typeSetT::iterator i = this->typeCandidates.begin(); 
		i != this->typeCandidates.end(); /* nothing */) {

		switch((*i)->baseType) {
		case BASE_TYPE_ARRAY:
			isArray = true;
			i++;
			continue;
		case BASE_TYPE_RECORD:
			isArray = false;
			i++;
			continue;
		default:
			/* fall through */
			break;
		}

		// not a composite type, remove.
		typeSetT::iterator j = i;
		i++;
		this->typeCandidates.erase(j);
	}

	if (! this->needUniqueType(node)) {
		return;
	}

	// typeCandidates contain exactly one type
	if (isArray) {
		this->processArrayAgg(node);
		node.baseType = BASE_TYPE_ARRAY;
		return;
	} 

	node.baseType = BASE_TYPE_RECORD;
	// TODO record Aggregates
	return;
}

void
ResolveTypes::processArrayAgg(Aggregate &node)
{
	// must have been filtered by caller.
	assert(this->typeCandidates.size() == 1);

	// determine subscripted type
	const TypeDeclaration *wanted = *(this->typeCandidates.begin());

	for (std::list<ElementAssociation*>::const_iterator i = 
		node.associations->begin(); i != node.associations->end();
		i++) {

		this->typeCandidates.clear();
		this->typeCandidates.insert(wanted);
		// don't dispatch, call directly instead.
		this->processArrayAssoc(**i);
	}

	// restore candidates
	this->typeCandidates.clear();
	this->typeCandidates.insert(wanted);

	if (ResolveTypes::isConstraintArray(node.type)) {
		return;
	}

	// unconstraint array as base type. Try to calculate bounds.
	UnconstraintBounds ub = UnconstraintBounds();
	node.accept(ub);
	assert(ub.bounds != NULL);

	// generate a new SubtypeIndication with the range constraint.
	SubtypeIndication *si = 
		new SubtypeIndication(node.type, node.location);
	si->indexConstraint = new std::list<DiscreteRange*>();
	si->indexConstraint->push_back(ub.bounds);
	node.type = si;
}

void
ResolveTypes::visit(ElementAssociation &node)
{
	assert(false); // use direct calls instead of dispatching.
}

void
ResolveTypes::visit(ReturnStat &node)
{
	if (node.result == NULL) {
		// no return expression, break early.
		return;
	}

	// determine return type
	// FIXME catch error that result is present, but 
	//       node refers to a Procedure
	LookupTypes lat = LookupTypes(false, false);

	assert(node.enclosingSubprog != NULL);
	node.enclosingSubprog->accept(lat);
	assert(lat.declaration != NULL);

	this->typeCandidates.clear();
	this->typeCandidates.insert(lat.declaration);
	
	// traverse to result expression (will report errors, since
	// there is only a single type in wantTypes)
	node.result->accept(*this);
	this->typeCandidates.clear();
}

void
ResolveTypes::visit(Process &node)
{
	if (node.sensitivityList != NULL) {
		for (std::list<Name*>::iterator i = 
			node.sensitivityList->begin();
			i != node.sensitivityList->end(); i++) {

			this->typeCandidates.clear();
			(*i)->accept(*this);
			if (! this->needUniqueType(**i)) {
				continue;
			}
			
			(*i)->accept(*this);
			this->needUniqueType(**i);
		}
	}

	if (node.declarations != NULL) {
		this->listTraverse(*node.declarations);
	}
	this->typeCandidates.clear();

	if (node.seqStats != NULL) {
		this->listTraverse(*node.seqStats);
	}
}

void
ResolveTypes::processArrayAssoc(ElementAssociation &node)
{
	// need exactly one candidate. If that's not true, Aggregate should
	// have reported an error and not called this method.
	assert(this->typeCandidates.size() == 1); 

	if (node.choices != NULL) {
		// determine possible index types.
		typeSetT backup = this->typeCandidates;
		this->typeCandidates.clear();
		IndexTypeFilter itf = IndexTypeFilter(backup, 
							this->typeCandidates,
							1);
		itf.apply();
		typeSetT wantedIndexTypes = this->typeCandidates;

		for (std::list<Expression*>::const_iterator i = 
			node.choices->begin();
			i != node.choices->end(); i++) {

			this->typeCandidates = wantedIndexTypes;
			(*i)->accept(*this);
			// check if index types match.
			itf.apply();
			if (! this->needUniqueType(node)) {
				// type error, bail out immediately
				return;
			}

			assert((*i)->type != NULL);
		}
		this->typeCandidates = backup;
	}

	assert(node.actual != NULL);
	// getting tricky: 
	// * if it's a multidimensional array, remove the first index and 
	//   traverse to actual.
	// * otherwise determine the element type and traverse to actual.
	//
	// Examples: (0 => (0 => '1', 1 => '0'), 1 to 3 => (others => '1'))
	//           is valid for
	//           array(0 to 3, 0 to 1) of character;
	//           *and*
	//           at : array(0 to 1) of character;
	//           array(0 to 3) of at;
	const TypeDeclaration *wanted = *this->typeCandidates.begin();
	const TypeDeclaration *needed = 
		ResolveTypes::subscribedType(*wanted, 1, node.location);

	// traverse to actual
	this->typeCandidates.clear();
	this->typeCandidates.insert(needed);
	node.actual->accept(*this);
	this->typeCandidates.clear();
	this->typeCandidates.insert(wanted);
}

const TypeDeclaration *
ResolveTypes::subscribedType(
	const TypeDeclaration &array, 
	unsigned int nIdx,
	Location loc
)
{
	assert(array.baseType == BASE_TYPE_ARRAY);
	std::list<DiscreteRange*> idxConstraint = std::list<DiscreteRange*>();
	const UnconstrainedArrayType *uArr = 
		ResolveTypes::pickupIndexConstraint(&array, idxConstraint);

	assert(uArr->indexTypes != NULL);

	if (uArr->indexTypes->size() < nIdx) {
		// FIXME: issue error here.
		assert(false);
	}

	if (uArr->indexTypes->size() == nIdx) {
		// subscription to base type.
		return uArr->elementType;
	}

	// partial subscription. strip off first nIdx indices.
	std::list<TypeDeclaration*>::iterator i = uArr->indexTypes->begin();
	for (unsigned int c = nIdx; c > 0; c--) {
		i++;
	}

	std::list<TypeDeclaration*> *shiftIndices = 
					new std::list<TypeDeclaration*>();

	shiftIndices->insert(shiftIndices->end(), i, uArr->indexTypes->end());

	// that's the new anonymous base type.
	UnconstrainedArrayType *ua = new UnconstrainedArrayType(
			new std::string("__anonymous__"),
			shiftIndices,
			uArr->elementType,
			loc);

	if (idxConstraint.empty()) {
		// must have been an Unconstraint array already.
		return ua;
	}

	// FIXME must be checked somewhere (hello parser?)
	assert(idxConstraint.size() == uArr->indexTypes->size());
	// strip off nIdx indices from indexConstraint as well.
	for (std::list<DiscreteRange*>::iterator j = idxConstraint.begin();
		nIdx > 0; nIdx--) {

		assert(j != idxConstraint.end());
		j = idxConstraint.erase(j);
	}

	SubtypeIndication *si = new SubtypeIndication(ua, loc);
	si->indexConstraint = new std::list<DiscreteRange*>(idxConstraint);

	return si;
}


void
ResolveTypes::visit(Others &node)
{
	if (this->typeCandidates.size() == 1) {
		this->needUniqueType(node);
	}

	// otherwise others does not affect type candidates.
}

void
ResolveTypes::visit(Slice &node)
{
	assert(node.source);
#if SLICE_DEBUG
	std::cerr << "Slice before source" << std::endl;
	ResolveTypes::debugPrintTypes(this->typeCandidates);
#endif
	node.source->accept(*this);

#if SLICE_DEBUG
	std::cerr << "Slice after source" << std::endl;
	ResolveTypes::debugPrintTypes(this->typeCandidates);
#endif

	typeSetT backup = this->typeCandidates;
	this->typeCandidates.clear();

	// determine index types and store these in typeCandidates
	IndexTypeFilter itf = 
		IndexTypeFilter(backup, this->typeCandidates, 1);
	itf.apply();
#if SLICE_DEBUG
	std::cerr << "#indexTypes for " << *node.source << "=" 
		<< this->typeCandidates.size() << std::endl;
	std::cerr << "#source types=" << backup.size() << std::endl;
#endif

	// traverse to range and filter out types
	assert(node.range);
	node.range->accept(*this);
	itf.apply();
	this->typeCandidates = backup;

	this->needUniqueType(node);
}

void
ResolveTypes::visit(TemporaryName &node)
{
	// only traverse to prefix, do nothing else
	assert(node.prefix != NULL);
	node.prefix->accept(*this);

	// set the type in case it's known
	if (this->typeCandidates.size() == 1) {
		this->needUniqueType(node);
	}
}

void
ResolveTypes::visit(AttributeSpecification &node)
{
	this->typeCandidates.clear();
	assert(node.declaration != NULL);
	assert(node.declaration->type != NULL);

	this->typeCandidates.insert(node.declaration->type);

	assert(node.init != NULL);
	node.init->accept(*this);

	this->needUniqueType(node);
	this->typeCandidates.clear();
}

void
ResolveTypes::processUniversal(
	Expression &node,
	const TypeDeclaration *directMatch,
	enum BaseType icCompatible
)
{
	assert(directMatch != NULL);

	// no type candidates -> put directMatch (universal_int, U.real)
	// in there.
	// (implicit conversions are only allowed, if the wanted type
	// is known, LRM 7.3.5).
	if (this->typeCandidates.empty()) {
		this->typeCandidates.insert(directMatch);
		this->needUniqueType(node);
		return;
	}

	bool mustSingle = (this->typeCandidates.size() == 1);

	// check if there is a direct match for __universal_integer__
	typeSetT backup = this->typeCandidates;
	for (typeSetT::iterator i = backup.begin(); i != backup.end(); 
		/* nothing */) {

		if (! ResolveTypes::baseTypeEqual(**i, *directMatch)) {
			typeSetT::iterator j = i;
			i++;
			backup.erase(j);
			continue;
		}

		i++;
	}

	if (! backup.empty()) {
		// direct match against universal_integer found. return this.
		this->typeCandidates = backup;
		this->needUniqueType(node);
		return;
	}

	// node is of type (convertible) universal_integer. Filter out all 
	// types that are not integer based.
	for (typeSetT::iterator i = this->typeCandidates.begin(); 
		i != this->typeCandidates.end(); /* nothing */) {

		if ((*i)->baseType != icCompatible) {
			typeSetT::iterator j = i;
			i++;
			this->typeCandidates.erase(j);
			continue;
		}
		i++;
	}

	if (mustSingle) {
		this->needUniqueType(node);
	}
}

void
ResolveTypes::visit(RangeConstraintType &node)
{
	this->processConstraintType(node);
}

void
ResolveTypes::visit(PhysicalType &node)
{
	this->processConstraintType(node);
	if (node.baseType != BASE_TYPE_INTEGER) {
		node.baseType = BASE_TYPE_INTEGER;
		CompileError *err = new CompileError(node, 
				"Constraint must be an integral type.");
		ErrorRegistry::addError(err);
	}
}

void
ResolveTypes::visit(RecordType &node)
{
	// nothing to do, *don't* traverse to elements!
}

void
ResolveTypes::visit(FunctionDeclaration &node)
{
	// do *not* traverse to returnType
	this->process(node);
}

void
ResolveTypes::visit(ForLoopStat &node)
{
	// set type declaration of loop parameter specification from
	// discrete range.
	this->typeCandidates.clear();
	assert(node.range != NULL);
	node.range->accept(*this);

	bool ret = this->needUniqueType(node);
	if (! ret) {
		return;
	}

	const TypeDeclaration *t = *this->typeCandidates.begin();
	if (t->isUniversal) {
		t = this->symbolTable.getStdStandardType("integer");
		this->typeCandidates.clear();
		this->typeCandidates.insert(t);
	}

	// actually pin down type
	node.range->accept(*this);

	assert(node.loopVariable != NULL);
	assert(! this->typeCandidates.empty());
	t = *this->typeCandidates.begin();

	// check if it is a discrete type.
	switch (t->baseType) {
	case BASE_TYPE_INTEGER:
		break;
	
	default: {
		CompileError *ce = 
			new CompileError(*node.range,
				"For loop parameter must be discrete.");
		ErrorRegistry::addError(ce);
		return;
	    }
	}

	node.loopVariable->subtypeIndic = 
		new SubtypeIndication(t, node.loopVariable->location);
	this->typeCandidates.clear();

	// handle seqstats via process.
	this->process(node);
}

void
ResolveTypes::visit(WhileLoopStat &node)
{
	this->typeCandidates.clear();
	// condition must be of type boolean
	const TypeDeclaration *boolean = 
		this->symbolTable.getStdStandardType("boolean");

	assert(boolean != NULL);
	assert(node.condition != NULL);

	this->typeCandidates.insert(boolean);
	node.condition->accept(*this);
	this->typeCandidates.clear();

	if (node.loopStats != NULL) {
		this->listTraverse(*node.loopStats);
	}
}

void
ResolveTypes::visit(AssertStat &node)
{
	this->typeCandidates.clear();

	// condition must be of type boolean
	const TypeDeclaration *boolean = 
		this->symbolTable.getStdStandardType("boolean");

	assert(boolean != NULL);
	assert(node.condition != NULL);

	this->typeCandidates.insert(boolean);
	node.condition->accept(*this);
	this->typeCandidates.clear();

	if (node.report != NULL) {
		// report must be of type string (i.e. compatible to)
		const TypeDeclaration *strT = 
			this->symbolTable.getStdStandardType("string");
		assert(strT != NULL);
		this->typeCandidates.insert(strT);
		node.report->accept(*this);
		this->typeCandidates.clear();
	}

	if (node.severity != NULL) {
		// report must be of type SEVERITY_LEVEL
		const TypeDeclaration *sevLvl = 
			this->symbolTable.getStdStandardType(
							"severity_level");
		assert(sevLvl != NULL);
		this->typeCandidates.insert(sevLvl);
		node.severity->accept(*this);
		this->typeCandidates.clear();
	}
}

void
ResolveTypes::visit(WaitStat &node)
{
	this->typeCandidates.clear();
	// handle condition
	this->process(node);

	if (node.timeout != NULL) {
		// timeout must be of type "time"
		const TypeDeclaration *time = 
			this->symbolTable.getStdStandardType("time");
		this->typeCandidates.insert(time);

		node.timeout->accept(*this);
		this->needUniqueType(*node.timeout);
		this->typeCandidates.clear();
	}

	if (node.sensitivities == NULL) {
		return;
	}

	// node.sensitivities != NULL
	for (std::list<Name*>::iterator i = node.sensitivities->begin();
		i != node.sensitivities->end(); i++) {
		
		// TODO (probably in a separate step): the sensitivity list
		//      must consist of solely static names.
		(*i)->accept(*this);
		if (! this->needUniqueType(**i)) {
			this->typeCandidates.clear();
			continue;
		}

		(*i)->accept(*this);
		this->needUniqueType(**i);
		this->typeCandidates.clear();
	}
}

void
ResolveTypes::processAlternative(CaseAlternative &node)
{
	assert(node.isVals != NULL);
	assert(node.thenStats != NULL);
	typeSetT backup = this->typeCandidates;
	assert(backup.size() == 1);

	for (std::list<Expression*>::iterator i = node.isVals->begin();
		i != node.isVals->end(); i++) {

		this->typeCandidates = backup;
		(*i)->accept(*this);
		this->needUniqueType(**i);
	}

	this->typeCandidates.clear();
	this->listTraverse(*node.thenStats);
	this->typeCandidates = backup;
}

void
ResolveTypes::visit(CaseStat &node)
{
	assert(node.select != NULL);
	assert(node.alternatives != NULL);

	this->typeCandidates.clear();
	node.select->accept(*this);
	// TODO lrm, 8.8: base type must be discrete, or a one dimensional 
	//                array of a character type.
	bool r = this->needUniqueType(*node.select);
	if (! r) {
		// don't even bother alternatives on type error
		this->typeCandidates.clear();
		return;
	}
	// pin down type
	node.select->accept(*this);
	if (this->typeCandidates.size() != 1) {
		return;
	}

	for (std::list<CaseAlternative*>::iterator i = 
		node.alternatives->begin();
		i != node.alternatives->end(); i++) {

		this->processAlternative(**i);
	}

	this->typeCandidates.clear();
}

template <typename T>
void
ResolveTypes::processConstraintType(T &node)
{
	this->typeCandidates.clear();
	assert(node.constraint != NULL);

	if (node.constraint->rangeName != NULL) {
		// handle these via discrete range.
		node.constraint->accept(*this);
		if (! this->needUniqueType(node)) {
			return;
		}
		const TypeDeclaration *t = *this->typeCandidates.begin();
		switch(t->baseType) {
		case BASE_TYPE_RANGE_INT:
			node.baseType = BASE_TYPE_INTEGER;
			break;
		case BASE_TYPE_RANGE_REAL:
			node.baseType = BASE_TYPE_RANGE_REAL;
			break;
		default: {
			node.baseType = BASE_TYPE_UNSET;
			CompileError *ce = new CompileError(node.location,
				"invalid type for constraint.");
			ErrorRegistry::addError(ce);
		    }
			
		}
		return;
	}


	assert(node.constraint->from != NULL);
	assert(node.constraint->to != NULL);
	node.constraint->from->accept(*this);
	if (! this->needUniqueType(*node.constraint->from)) {
		this->typeCandidates.clear();
		node.baseType = BASE_TYPE_UNSET;
		return;
	}

	// traverse again, to pin down from type
	node.constraint->from->accept(*this);
	// FIXME can this happen?
	assert(this->typeCandidates.size() == 1);

	const TypeDeclaration *t = *this->typeCandidates.begin();
	enum BaseType left = t->baseType;


	this->typeCandidates.clear();
	node.constraint->to->accept(*this);
	if (! this->needUniqueType(*node.constraint->to)) {
		this->typeCandidates.clear();
		node.baseType = BASE_TYPE_UNSET;
		return;
	}

	// traverse again, to pin to from type
	node.constraint->to->accept(*this);
	assert(this->typeCandidates.size() == 1);

	// set base type
	t = *this->typeCandidates.begin();
	this->typeCandidates.clear();
	left = left && t->baseType;

	switch(left) {
	case BASE_TYPE_INTEGER:
	case BASE_TYPE_REAL:
		node.baseType = left;
		break;
	default: {
		node.baseType = BASE_TYPE_UNSET;
		CompileError *ce = new CompileError(node.location,
			"invalid type for constraint.");
		ErrorRegistry::addError(ce);
	    }
	}
}

bool
ResolveTypes::baseTypeEqual(
	const TypeDeclaration &t1,
	const TypeDeclaration &t2
)
{
	// determine base types.
	const TypeDeclaration *b1 = ResolveTypes::findBaseType(&t1);
	const TypeDeclaration *b2 = ResolveTypes::findBaseType(&t2); 

	// compare pointer!
	return b1 == b2;
}

const TypeDeclaration*
ResolveTypes::findBaseType(const TypeDeclaration* t)
{
	if (t == NULL) {
		return NULL;
	}

	const SubtypeIndication *s = 
			dynamic_cast<const SubtypeIndication*>(t);
	if (s == NULL) {
		// not a subtype
		return t;
	}

	// recurse
	assert(s->declaration);
	return ResolveTypes::findBaseType(s->declaration);
}

const UnconstrainedArrayType*
ResolveTypes::pickupIndexConstraint(
	const TypeDeclaration *constrainedArray,
	std::list<DiscreteRange*> &indexConstraint
)
{
	assert(constrainedArray != NULL);
	assert(constrainedArray->baseType == BASE_TYPE_ARRAY);

	const SubtypeIndication *sub = 
		dynamic_cast<const SubtypeIndication*>(constrainedArray);

	if ((sub != NULL) && (sub->indexConstraint != NULL)) {
		indexConstraint.insert(
			indexConstraint.end(), 
			sub->indexConstraint->begin(),
			sub->indexConstraint->end());
	}

	if (sub != NULL) {
		assert(sub->declaration != NULL);
		return ResolveTypes::pickupIndexConstraint(
						sub->declaration,
						indexConstraint);
	}

	const UnconstrainedArrayType *ua = 
		dynamic_cast<const UnconstrainedArrayType*>(constrainedArray);
	assert(ua != NULL);
	return ua;
}

bool
ResolveTypes::isConstraintArray(const TypeDeclaration *type)
{
	switch (type->baseType) {
	case BASE_TYPE_ARRAY:
		break;

	default:
		return false;
	}

	std::list<DiscreteRange *> dr;

	ResolveTypes::pickupIndexConstraint(type, dr);
	return ! dr.empty();
}

void
ResolveTypes::process(ValDeclaration& node)
{
	assert(node.subtypeIndic != NULL);
	this->typeCandidates.clear();
	node.subtypeIndic->accept(*this);

	this->typeCandidates.clear();
	if (node.init != NULL) {
		this->typeCandidates.insert(node.subtypeIndic);
		node.init->accept(*this);

		// for arrays, the node's subtype indication may
		// refer to an unconstraint array, and the bounds
		// are defined by the initializer. replace the
		// subtype indication then.
		if (this->needUniqueType(node)) {
			if (node.init->baseType != BASE_TYPE_ARRAY) {
				return;
			}

			// only constants may get the type inferred from the 
			// initializer.
			if (node.storageClass 
				!= ValDeclaration::OBJ_CLASS_CONSTANT) {
				return;
			}

			// and for constants, *only* those that are not
			// interface elements.
			const ConstantDeclaration *c = 
				dynamic_cast<const ConstantDeclaration*>(
								&node);

			assert(c != NULL);
			if (! c->fixedValue) {
				return;
			}


			SubtypeIndication *si = 
				dynamic_cast<SubtypeIndication*>(
					node.init->type);
			assert(si != NULL);
			util::MiscUtil::terminate(node.subtypeIndic);
			node.subtypeIndic = si;
#if 0
			std::cerr << "replaced subtype with " << si 
				<< std::endl;
			std::cerr << "initializer is" << node.init << std::endl;
#endif
		}
	}
}

void
ResolveTypes::process(LibUnit &node)
{
	//don't consider use- or library clauses
	//no need to call process here as well.

	//traverse to declarations.
	if (node.declarations != NULL) {
		this->listTraverse(*node.declarations);
	}
}

void
ResolveTypes::process(ConditionedStat &node)
{
	this->process(static_cast<SeqStat&>(node));
	if (node.condition == NULL) {
		return;
	}
	
	// condition must be of type boolean
	const TypeDeclaration *boolean = 
		this->symbolTable.getStdStandardType("boolean");
	
	assert(boolean != NULL);
	this->typeCandidates.insert(boolean);

	node.condition->accept(*this);
	this->typeCandidates.clear();
}

void
ResolveTypes::process(SeqStat &node)
{
	this->typeCandidates.clear();
}

bool
ResolveTypes::needUniqueType(AstNode &node) const
{
	if (this->typeCandidates.size() == 1) {
		return true;
	}

	if (this->typeCandidates.empty()) {
		std::string msg = "Type error for <";
		msg += util::MiscUtil::toString(node);
		msg += ">.";
		CompileError *ce = new CompileError(node, msg);
		ErrorRegistry::addError(ce);
		return false;
	}

	std::string msg = "Ambiguous Types for <";
		msg += util::MiscUtil::toString(node);
		msg += ">.";

	CompileError *ce = new CompileError(node, msg);
	ErrorRegistry::addError(ce);
	return false;
}

bool
ResolveTypes::needUniqueType(Expression &node) const
{
	bool ret = this->needUniqueType(static_cast<AstNode&>(node));
	if (ret) {
		const TypeDeclaration *t = *this->typeCandidates.begin();
		node.type = const_cast<TypeDeclaration*>(t);
		node.baseType = t->baseType;
	}

	return ret;
}

void
ResolveTypes::needNotEmpty(Location loc) 
{
	if (this->typeCandidates.empty()) {
		//FIXME: better error messages
		CompileError *ce = new CompileError(
					loc, 
					"Type error.");
		ErrorRegistry::addError(ce);
	}
}

enum BaseType
ResolveTypes::transformBaseType(enum BaseType rangeType, Location loc)
{
	switch(rangeType) {
	case BASE_TYPE_INTEGER:
		return BASE_TYPE_RANGE_INT;
	case BASE_TYPE_REAL:
		return BASE_TYPE_RANGE_REAL;
		break;
	default: {
		CompileError *ce = new CompileError(
			loc,
			"Wrong base type for discrete range.");
		ErrorRegistry::addError(ce);
		}
	}

	return BASE_TYPE_UNSET;
}

void
ResolveTypes::debugPrintTypes(const typeSetT &t)
{
	for (typeSetT::const_iterator i = t.begin(); i != t.end(); i++) {
		std::cerr << "\tcand=" << *i << std::endl;
	}
}

DiscreteRange *
ResolveTypes::determineIndexRangeAgg(
	const UnconstrainedArrayType *at,
	const std::list<ElementAssociation *> &assocs
)
{
	universal_integer lowerBound;
	universal_integer upperBound;

	assert(at->indexTypes != NULL);
	// TODO currently only 1-dimensional arrays supported.
	assert(at->indexTypes->size() == 1);

	DiscreteRange *maxBounds = 
		ResolveTypes::findRange(at->indexTypes->front());
	assert(maxBounds != NULL);
	// TODO direction
	assert(maxBounds->direction == DiscreteRange::DIRECTION_UP);

	// start with a NULL range...
	lowerBound = maxBounds->getUpperBound();
	upperBound = maxBounds->getLowerBound();

	if ((! assocs.empty()) && (assocs.front()->choices == NULL)) {
		// positional aggregate. lower bound is indextype'left
		lowerBound = upperBound;
		upperBound = lowerBound + assocs.size() - 1;

		ConstInteger *lb = new ConstInteger(lowerBound,
						Location("context"));
		ConstInteger *ub = new ConstInteger(upperBound,
						Location("context"));
		return new DiscreteRange(lb, ub, 
					DiscreteRange::DIRECTION_UP, 
					Location("context"));
	}

	// aggregate with choices.
	for (std::list<ElementAssociation *>::const_iterator i = 
		assocs.begin(); 
		i != assocs.end(); i++) {

		if ((*i)->choices != NULL) {
			for (std::list<Expression*>::const_iterator j =
				(*i)->choices->begin(); 
				j != (*i)->choices->end();
				j++) {

				const ConstInteger *c = 
					dynamic_cast<const ConstInteger*>(*j);
				assert(c != NULL);

				if (c->value < lowerBound) {
					lowerBound = c->value;
				}

				if (c->value > upperBound) {
					upperBound = c->value;
				}
			}
		} else {
			upperBound++;
		}
	}

	ConstInteger *lb = new ConstInteger(lowerBound, Location("context"));
	ConstInteger *ub = new ConstInteger(upperBound, Location("context"));
	return new DiscreteRange(lb, ub, DiscreteRange::DIRECTION_UP, 
				Location("context"));
}

DiscreteRange *
ResolveTypes::findRange(const TypeDeclaration *rangeType)
{
	const SubtypeIndication *sub;

	sub = dynamic_cast<const SubtypeIndication *>(rangeType);
	if (sub != NULL) {
		if (sub->constraint != NULL) {
			return sub->constraint;
		}
		return ResolveTypes::findRange(sub->declaration);
	}

	const RangeConstraintType *base;
	base = dynamic_cast<const RangeConstraintType *>(rangeType);
	assert(base != NULL);
	assert(base->constraint != NULL);

	return base->constraint;
}


template <typename T>
ResolveTypes::TypeFilter<T>::TypeFilter
(
	T &cands,
	typeSetT &wantTypes
) : 		candidates(cands),
		wantedTypes(wantTypes)
{
}

const TypeDeclaration*
ResolveTypes::SymbolFilter::operator()(Symbol *element) const
{
	LookupTypes lat = LookupTypes(false, false);
	element->declaration.accept(lat);

	return lat.declaration;
}

template <typename T>
void
ResolveTypes::TypeFilter<T>::apply(void)
{
	bool getAllPossibleTypes = this->wantedTypes.empty();
	typeSetT possibleTypes = typeSetT();

	for (typename T::iterator i = this->candidates.begin();
		i != this->candidates.end(); /* nothing */) {
		
		//determine type declaration via () operator.
		const TypeDeclaration *t = (*this)(*i);

		if (t == NULL) {
			//operation turned out that candidate not plausible
			typename T::iterator j = i;
			i++;
			this->candidates.erase(j);
			continue;
		}

		if (getAllPossibleTypes) {
			// don't perform type checking.
			possibleTypes.insert(t);
			i++;
			continue;
		}

		if (this->checkType(t)) {
			possibleTypes.insert(t);
			i++;
			continue;
		}

		//resulting type not in wanted types.
		typename T::iterator j = i;
		i++;
		this->candidates.erase(j);
	}

	this->wantedTypes = possibleTypes;
}

template <typename T>
bool
ResolveTypes::TypeFilter<T>::checkType(const TypeDeclaration *t) const
{
	assert(t);

	// must have wanted types, otherwise no type checking should be
	// performed but all possible types should get reported.
	assert(! this->wantedTypes.empty());
	
	for (typeSetT::const_iterator i = this->wantedTypes.begin();
		i != this->wantedTypes.end(); i++) {
		
		if (ResolveTypes::baseTypeEqual(**i, *t)) {
			return true;
		}
	}
	return false;
}

const TypeDeclaration*
ResolveTypes::ProjectPositionalArg::operator()(Symbol* element) const
{
	switch(element->type) {
	case SYMBOL_FUNCTION:
	case SYMBOL_PROCEDURE:
		break;
	default:
		/* only useful for functions/procedures */
		//FIXME: port map/generic map aspects!
		assert(false);
	}

	Callable *c = dynamic_cast<Callable*>(&element->declaration);
	assert(c);
	assert(c->arguments);

	if (c->arguments->size() <= this->position) {
		return NULL;
	}

	std::list<ValDeclaration*>::const_iterator i = c->arguments->begin();
	std::advance(i, this->position);

	const SubtypeIndication *s = (*i)->subtypeIndic;
	return s->declaration;
}

/** resolve the type of the source to a subscribed
 *  type.
 *  @param element source type 
 *  @return subscribed type.
 */
const TypeDeclaration*
ResolveTypes::SubscriptFilter::operator()(
	typeSetT::value_type element
) const 
{

	// not an array?
	if (element->baseType != BASE_TYPE_ARRAY) {
		return NULL;
	}

	const SubtypeIndication *si = 
		dynamic_cast<const SubtypeIndication*>(
						element);
	assert(si);
	const UnconstrainedArrayType *ua = 
		dynamic_cast<const UnconstrainedArrayType*>(
			ResolveTypes::findBaseType(si));

	assert(ua);
	assert(ua->elementType);
	
	return ua->elementType;
}

const TypeDeclaration*
ResolveTypes::IndexTypeFilter::operator()(typeSetT::value_type element) const
{
	if (element->baseType != BASE_TYPE_ARRAY) {
		return NULL;
	}

	const UnconstrainedArrayType *array = 
		dynamic_cast<const UnconstrainedArrayType*>(
			ResolveTypes::findBaseType(element));
	assert(array != NULL);
	assert(array->indexTypes != NULL);
	if (array->indexTypes->size() < this->nIdx) {
		return NULL;
	}
	
	
	bool found = false;
	std::list<TypeDeclaration*>::const_iterator i = 
					array->indexTypes->begin();

	for (unsigned int j = 1; i != array->indexTypes->end(); i++, j++) {
		if (this->nIdx <= j) {
			found = true;
			break;
		}
	}

	if (found) {
		return *i;
	}
	return NULL;
}

}; /* namespace ast */