summaryrefslogtreecommitdiff
path: root/vala/valasemanticanalyzer.vala
blob: ff2068a84010033e394b6efed336443486c08647 (plain)
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
/* valasemanticanalyzer.vala
 *
 * Copyright (C) 2006-2008  Jürg Billeter, Raffaele Sandrini
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Jürg Billeter <j@bitron.ch>
 *	Raffaele Sandrini <raffaele@sandrini.ch>
 */

using GLib;
using Gee;

/**
 * Code visitor analyzing and checking code.
 */
public class Vala.SemanticAnalyzer : CodeVisitor {
	private CodeContext context;

	Symbol root_symbol;
	Symbol current_symbol;
	SourceFile current_source_file;
	DataType current_return_type;
	Class current_class;
	Struct current_struct;

	Gee.List<NamespaceReference> current_using_directives;

	DataType bool_type;
	DataType string_type;
	DataType int_type;
	DataType uint_type;
	DataType ulong_type;
	DataType size_t_type;
	DataType unichar_type;
	DataType type_type;
	Class object_type;
	TypeSymbol initially_unowned_type;
	DataType glist_type;
	DataType gslist_type;
	Class gerror_type;
	DataType iterable_type;
	Interface iterator_type;
	Interface list_type;
	Interface map_type;

	private int next_lambda_id = 0;

	// keep replaced alive to make sure they remain valid
	// for the whole execution of CodeNode.accept
	Gee.List<CodeNode> replaced_nodes = new ArrayList<CodeNode> ();

	public SemanticAnalyzer () {
	}

	/**
	 * Analyze and check code in the specified context.
	 *
	 * @param context a code context
	 */
	public void analyze (CodeContext context) {
		this.context = context;

		root_symbol = context.root;

		bool_type = new ValueType ((TypeSymbol) root_symbol.scope.lookup ("bool"));
		string_type = new ObjectType ((Class) root_symbol.scope.lookup ("string"));

		int_type = new ValueType ((TypeSymbol) root_symbol.scope.lookup ("int"));
		uint_type = new ValueType ((TypeSymbol) root_symbol.scope.lookup ("uint"));
		ulong_type = new ValueType ((TypeSymbol) root_symbol.scope.lookup ("ulong"));
		size_t_type = new ValueType ((TypeSymbol) root_symbol.scope.lookup ("size_t"));
		unichar_type = new ValueType ((TypeSymbol) root_symbol.scope.lookup ("unichar"));

		// TODO: don't require GLib namespace in semantic analyzer
		var glib_ns = root_symbol.scope.lookup ("GLib");
		if (glib_ns != null) {
			object_type = (Class) glib_ns.scope.lookup ("Object");
			initially_unowned_type = (TypeSymbol) glib_ns.scope.lookup ("InitiallyUnowned");

			type_type = new ValueType ((TypeSymbol) glib_ns.scope.lookup ("Type"));

			glist_type = new ObjectType ((Class) glib_ns.scope.lookup ("List"));
			gslist_type = new ObjectType ((Class) glib_ns.scope.lookup ("SList"));

			gerror_type = (Class) glib_ns.scope.lookup ("Error");
		}

		var gee_ns = root_symbol.scope.lookup ("Gee");
		if (gee_ns != null) {
			iterable_type = new ObjectType ((Interface) gee_ns.scope.lookup ("Iterable"));
			iterator_type = (Interface) gee_ns.scope.lookup ("Iterator");
			list_type = (Interface) gee_ns.scope.lookup ("List");
			map_type = (Interface) gee_ns.scope.lookup ("Map");
		}

		current_symbol = root_symbol;
		context.accept (this);
	}

	public override void visit_source_file (SourceFile file) {
		current_source_file = file;
		current_using_directives = file.get_using_directives ();

		next_lambda_id = 0;

		file.accept_children (this);

		current_using_directives = null;
	}

	public override void visit_class (Class cl) {
		current_symbol = cl;
		current_class = cl;

		foreach (DataType base_type_reference in cl.get_base_types ()) {
			// check whether base type is at least as accessible as the class
			if (!is_type_accessible (cl, base_type_reference)) {
				cl.error = true;
				Report.error (cl.source_reference, "base type `%s` is less accessible than class `%s`".printf (base_type_reference.to_string (), cl.get_full_name ()));
				return;
			}

			current_source_file.add_type_dependency (base_type_reference, SourceFileDependencyType.HEADER_FULL);
		}

		cl.accept_children (this);

		/* compact classes cannot implement interfaces */
		if (cl.is_compact) {
			foreach (DataType base_type in cl.get_base_types ()) {
				if (base_type.data_type is Interface) {
					cl.error = true;
					Report.error (cl.source_reference, "compact classes `%s` may not implement interfaces".printf (cl.get_full_name ()));
				}
			}
		}

		/* gather all prerequisites */
		Gee.List<TypeSymbol> prerequisites = new ArrayList<TypeSymbol> ();
		foreach (DataType base_type in cl.get_base_types ()) {
			if (base_type.data_type is Interface) {
				get_all_prerequisites ((Interface) base_type.data_type, prerequisites);
			}
		}
		/* check whether all prerequisites are met */
		Gee.List<string> missing_prereqs = new ArrayList<string> ();
		foreach (TypeSymbol prereq in prerequisites) {
			if (!class_is_a (cl, prereq)) {
				missing_prereqs.insert (0, prereq.get_full_name ());
			}
		}
		/* report any missing prerequisites */
		if (missing_prereqs.size > 0) {
			cl.error = true;

			string error_string = "%s: some prerequisites (".printf (cl.get_full_name ());
			bool first = true;
			foreach (string s in missing_prereqs) {
				if (first) {
					error_string = "%s`%s'".printf (error_string, s);
					first = false;
				} else {
					error_string = "%s, `%s'".printf (error_string, s);
				}
			}
			error_string += ") are not met";
			Report.error (cl.source_reference, error_string);
		}

		/* VAPI classes don't have to specify overridden methods */
		if (!cl.external_package) {
			/* all abstract symbols defined in base types have to be at least defined (or implemented) also in this type */
			foreach (DataType base_type in cl.get_base_types ()) {
				if (base_type.data_type is Interface) {
					Interface iface = (Interface) base_type.data_type;

					if (cl.base_class != null && cl.base_class.is_subtype_of (iface)) {
						// reimplementation of interface, class is not required to reimplement all methods
						break;
					}

					/* We do not need to do expensive equality checking here since this is done
					 * already. We only need to guarantee the symbols are present.
					 */

					/* check methods */
					foreach (Method m in iface.get_methods ()) {
						if (m.is_abstract) {
							var sym = cl.scope.lookup (m.name);
							if (!(sym is Method)) {
								cl.error = true;
								Report.error (cl.source_reference, "`%s' does not implement interface method `%s'".printf (cl.get_full_name (), m.get_full_name ()));
							}
						}
					}
				}
			}

			/* all abstract symbols defined in base classes have to be implemented in non-abstract classes */
			if (!cl.is_abstract) {
				var base_class = cl.base_class;
				while (base_class != null && base_class.is_abstract) {
					foreach (Method base_method in base_class.get_methods ()) {
						if (base_method.is_abstract) {
							var override_method = symbol_lookup_inherited (cl, base_method.name) as Method;
							if (override_method == null || !override_method.overrides) {
								cl.error = true;
								Report.error (cl.source_reference, "`%s' does not implement abstract method `%s'".printf (cl.get_full_name (), base_method.get_full_name ()));
							}
						}
					}
					base_class = base_class.base_class;
				}
			}
		}

		current_symbol = current_symbol.parent_symbol;
		current_class = null;
	}

	private void get_all_prerequisites (Interface iface, Gee.List<TypeSymbol> list) {
		foreach (DataType prereq in iface.get_prerequisites ()) {
			TypeSymbol type = prereq.data_type;
			/* skip on previous errors */
			if (type == null) {
				continue;
			}

			list.add (type);
			if (type is Interface) {
				get_all_prerequisites ((Interface) type, list);

			}
		}
	}

	private bool class_is_a (Class cl, TypeSymbol t) {
		if (cl == t) {
			return true;
		}

		foreach (DataType base_type in cl.get_base_types ()) {
			if (base_type.data_type is Class) {
				if (class_is_a ((Class) base_type.data_type, t)) {
					return true;
				}
			} else if (base_type.data_type == t) {
				return true;
			}
		}

		return false;
	}

	public override void visit_struct (Struct st) {
		current_symbol = st;
		current_struct = st;

		st.accept_children (this);

		current_symbol = current_symbol.parent_symbol;
		current_struct = null;
	}

	public override void visit_interface (Interface iface) {
		current_symbol = iface;

		foreach (DataType prerequisite_reference in iface.get_prerequisites ()) {
			// check whether prerequisite is at least as accessible as the interface
			if (!is_type_accessible (iface, prerequisite_reference)) {
				iface.error = true;
				Report.error (iface.source_reference, "prerequisite `%s` is less accessible than interface `%s`".printf (prerequisite_reference.to_string (), iface.get_full_name ()));
				return;
			}

			current_source_file.add_type_dependency (prerequisite_reference, SourceFileDependencyType.HEADER_FULL);
		}

		/* check prerequisites */
		Class prereq_class;
		foreach (DataType prereq in iface.get_prerequisites ()) {
			TypeSymbol class_or_interface = prereq.data_type;
			/* skip on previous errors */
			if (class_or_interface == null) {
				iface.error = true;
				continue;
			}
			/* interfaces are not allowed to have multiple instantiable prerequisites */
			if (class_or_interface is Class) {
				if (prereq_class != null) {
					iface.error = true;
					Report.error (iface.source_reference, "%s: Interfaces cannot have multiple instantiable prerequisites (`%s' and `%s')".printf (iface.get_full_name (), class_or_interface.get_full_name (), prereq_class.get_full_name ()));
					return;
				}

				prereq_class = (Class) class_or_interface;
			}
		}

		iface.accept_children (this);

		current_symbol = current_symbol.parent_symbol;
	}

	public override void visit_enum (Enum en) {
		en.accept_children (this);
	}

	public override void visit_enum_value (EnumValue ev) {
		ev.accept_children (this);
	}

	public override void visit_error_domain (ErrorDomain ed) {
		ed.accept_children (this);
	}

	public override void visit_delegate (Delegate d) {
		d.accept_children (this);
	}

	public override void visit_constant (Constant c) {
		c.type_reference.accept (this);

		if (!c.external_package) {
			if (c.initializer == null) {
				c.error = true;
				Report.error (c.source_reference, "A const field requires a initializer to be provided");
			} else {
				c.initializer.target_type = c.type_reference;

				c.initializer.accept (this);
			}
		}
	}

	public override void visit_field (Field f) {
		if (f.initializer != null) {
			f.initializer.target_type = f.field_type;
		}

		f.accept_children (this);

		if (f.binding == MemberBinding.INSTANCE && f.parent_symbol is Interface) {
			f.error = true;
			Report.error (f.source_reference, "Interfaces may not have instance fields");
			return;
		}

		if (!f.is_internal_symbol ()) {
			current_source_file.add_type_dependency (f.field_type, SourceFileDependencyType.HEADER_SHALLOW);
		} else {
			if (f.parent_symbol is Namespace) {
				f.error = true;
				Report.error (f.source_reference, "Namespaces may not have private members");
				return;
			}

			current_source_file.add_type_dependency (f.field_type, SourceFileDependencyType.SOURCE);
		}
	}

	public override void visit_method (Method m) {
		if (m.is_abstract) {
			if (m.parent_symbol is Class) {
				var cl = (Class) m.parent_symbol;
				if (!cl.is_abstract) {
					m.error = true;
					Report.error (m.source_reference, "Abstract methods may not be declared in non-abstract classes");
					return;
				}
			} else if (!(m.parent_symbol is Interface)) {
				m.error = true;
				Report.error (m.source_reference, "Abstract methods may not be declared outside of classes and interfaces");
				return;
			}
		} else if (m.is_virtual) {
			if (!(m.parent_symbol is Class) && !(m.parent_symbol is Interface)) {
				m.error = true;
				Report.error (m.source_reference, "Virtual methods may not be declared outside of classes and interfaces");
				return;
			}

			if (m.parent_symbol is Class) {
				var cl = (Class) m.parent_symbol;
				if (cl.is_compact) {
					Report.error (m.source_reference, "Virtual methods may not be declared in compact classes");
					return;
				}
			}
		} else if (m.overrides) {
			if (!(m.parent_symbol is Class)) {
				m.error = true;
				Report.error (m.source_reference, "Methods may not be overridden outside of classes");
				return;
			}
		}

		var old_symbol = current_symbol;
		var old_return_type = current_return_type;
		current_symbol = m;
		current_return_type = m.return_type;

		var init_attr = m.get_attribute ("ModuleInit");
		if (init_attr != null) {
			m.source_reference.file.context.module_init_method = m;
		}

		if (!m.is_internal_symbol ()) {
			current_source_file.add_type_dependency (m.return_type, SourceFileDependencyType.HEADER_SHALLOW);
		}
		current_source_file.add_type_dependency (m.return_type, SourceFileDependencyType.SOURCE);

		m.accept_children (this);

		current_symbol = old_symbol;
		current_return_type = old_return_type;

		if (current_symbol.parent_symbol is Method) {
			/* lambda expressions produce nested methods */
			var up_method = (Method) current_symbol.parent_symbol;
			current_return_type = up_method.return_type;
		}

		if (current_symbol is Struct) {
			if (m.is_abstract || m.is_virtual || m.overrides) {
				Report.error (m.source_reference, "A struct member `%s' cannot be marked as override, virtual, or abstract".printf (m.get_full_name ()));
				return;
			}
		} else if (m.overrides && m.base_method == null) {
			Report.error (m.source_reference, "%s: no suitable method found to override".printf (m.get_full_name ()));
		}

		// check whether return type is at least as accessible as the method
		if (!is_type_accessible (m, m.return_type)) {
			m.error = true;
			Report.error (m.source_reference, "return type `%s` is less accessible than method `%s`".printf (m.return_type.to_string (), m.get_full_name ()));
			return;
		}

		foreach (Expression precondition in m.get_preconditions ()) {
			if (precondition.error) {
				// if there was an error in the precondition, skip this check
				m.error = true;
				return;
			}

			if (!precondition.value_type.compatible (bool_type)) {
				m.error = true;
				Report.error (precondition.source_reference, "Precondition must be boolean");
				return;
			}
		}

		foreach (Expression postcondition in m.get_postconditions ()) {
			if (postcondition.error) {
				// if there was an error in the postcondition, skip this check
				m.error = true;
				return;
			}

			if (!postcondition.value_type.compatible (bool_type)) {
				m.error = true;
				Report.error (postcondition.source_reference, "Postcondition must be boolean");
				return;
			}
		}

		// check that all errors that can be thrown in the method body are declared
		if (m.body != null) { 
			foreach (DataType body_error_type in m.body.get_error_types ()) {
				bool can_propagate_error = false;
				foreach (DataType method_error_type in m.get_error_types ()) {
					if (body_error_type.compatible (method_error_type)) {
						can_propagate_error = true;
					}
				}
				if (!can_propagate_error) {
					Report.warning (body_error_type.source_reference, "unhandled error `%s'".printf (body_error_type.to_string()));
				}
			}
		}
	}

	public override void visit_creation_method (CreationMethod m) {
		if (m.type_name != null && m.type_name != current_symbol.name) {
			// type_name is null for constructors generated by GIdlParser
			Report.error (m.source_reference, "missing return type in method `%s.%s´".printf (current_symbol.get_full_name (), m.type_name));
			m.error = true;
			return;
		}

		current_symbol = m;
		current_return_type = m.return_type;

		m.accept_children (this);

		current_symbol = current_symbol.parent_symbol;
		current_return_type = null;

		if (current_symbol.parent_symbol is Method) {
			/* lambda expressions produce nested methods */
			var up_method = (Method) current_symbol.parent_symbol;
			current_return_type = up_method.return_type;
		}

		if (m.is_abstract || m.is_virtual || m.overrides) {
			Report.error (m.source_reference, "The creation method `%s' cannot be marked as override, virtual, or abstract".printf (m.get_full_name ()));
			return;
		}
	}

	public override void visit_formal_parameter (FormalParameter p) {
		p.accept_children (this);

		if (context.non_null && p.default_expression != null) {
			if (p.default_expression is NullLiteral
			    && !p.parameter_type.nullable
			    && p.direction != ParameterDirection.OUT) {
				Report.warning (p.source_reference, "`null' incompatible with parameter type `%s`".printf (p.parameter_type.to_string ()));
			}
		}

		if (!p.ellipsis) {
			if (!p.is_internal_symbol ()) {
				current_source_file.add_type_dependency (p.parameter_type, SourceFileDependencyType.HEADER_SHALLOW);
			}
			current_source_file.add_type_dependency (p.parameter_type, SourceFileDependencyType.SOURCE);

			// check whether parameter type is at least as accessible as the method
			if (!is_type_accessible (p, p.parameter_type)) {
				p.error = true;
				Report.error (p.source_reference, "parameter type `%s` is less accessible than method `%s`".printf (p.parameter_type.to_string (), p.parent_symbol.get_full_name ()));
				return;
			}
		}

		/* special treatment for construct formal parameters used in creation methods */
		if (p.construct_parameter) {
			if (!(p.parent_symbol is CreationMethod)) {
				p.error = true;
				Report.error (p.source_reference, "construct parameters are only allowed in type creation methods");
				return;
			}

			var method_body = ((CreationMethod) p.parent_symbol).body;
			var left = new MemberAccess (new MemberAccess.simple ("this"), p.name);
			var right = new MemberAccess.simple (p.name);

			method_body.add_statement (new ExpressionStatement (new Assignment (left, right), p.source_reference));
		}
	}

	// check whether type is at least as accessible as the specified symbol
	private bool is_type_accessible (Symbol sym, DataType type) {
		foreach (Symbol type_symbol in type.get_symbols ()) {
			Scope method_scope = sym.get_top_accessible_scope ();
			Scope type_scope = type_symbol.get_top_accessible_scope ();
			if ((method_scope == null && type_scope != null)
			    || (method_scope != null && !method_scope.is_subscope_of (type_scope))) {
				return false;
			}
		}

		return true;
	}

	public override void visit_property (Property prop) {
		current_symbol = prop;

		if (prop.binding != MemberBinding.INSTANCE) {
			Report.error (prop.source_reference, "static properties are not yet supported");
			prop.error = true;
			return;
		}

		prop.accept_children (this);

		// check whether property type is at least as accessible as the property
		if (!is_type_accessible (prop, prop.property_type)) {
			prop.error = true;
			Report.error (prop.source_reference, "property type `%s` is less accessible than property `%s`".printf (prop.property_type.to_string (), prop.get_full_name ()));
			return;
		}

		/* abstract/virtual properties using reference types without
		 * reference counting need to transfer ownership of their
		 * return values because of limitations in the GObject property
		 * system (g_object_get always returns strong references).
		 * Reference counting types can simulate to return a weak
		 * reference */
		if ((prop.is_abstract || prop.is_virtual) &&
		    prop.property_type.data_type != null &&
		    prop.property_type.data_type.is_reference_type () &&
		    !prop.property_type.data_type.is_reference_counting () &&
		    !prop.property_type.value_owned)
		{
			Report.error (prop.source_reference, "%s: abstract or virtual properties using reference types not supporting reference counting, like `%s', have to mark their return value to transfer ownership.".printf (prop.get_full_name (), prop.property_type.data_type.get_full_name ()));
			prop.error = true;
		}

		current_symbol = current_symbol.parent_symbol;

		if (!prop.is_internal_symbol ()) {
			current_source_file.add_type_dependency (prop.property_type, SourceFileDependencyType.HEADER_SHALLOW);
		}
		current_source_file.add_type_dependency (prop.property_type, SourceFileDependencyType.SOURCE);

		if (prop.overrides && prop.base_property == null) {
			Report.error (prop.source_reference, "%s: no suitable property found to override".printf (prop.get_full_name ()));
		}

		/* construct properties must be public */
		if (prop.set_accessor != null && prop.set_accessor.construction) {
			if (prop.access != SymbolAccessibility.PUBLIC) {
				prop.error = true;
				Report.error (prop.source_reference, "%s: construct properties must be public".printf (prop.get_full_name ()));
			}
		}
	}

	public override void visit_property_accessor (PropertyAccessor acc) {
		acc.prop = (Property) current_symbol;

		if (acc.readable) {
			current_return_type = acc.prop.property_type;
		} else {
			// void
			current_return_type = new VoidType ();
		}

		if (!acc.prop.external_package) {
			if (acc.body == null && !acc.prop.interface_only && !acc.prop.is_abstract) {
				/* no accessor body specified, insert default body */

				if (acc.prop.parent_symbol is Interface) {
					acc.error = true;
					Report.error (acc.source_reference, "Automatic properties can't be used in interfaces");
					return;
				}
				acc.automatic_body = true;
				acc.body = new Block ();
				var ma = new MemberAccess.simple ("_%s".printf (acc.prop.name), acc.source_reference);
				if (acc.readable) {
					acc.body.add_statement (new ReturnStatement (ma, acc.source_reference));
				} else {
					var assignment = new Assignment (ma, new MemberAccess.simple ("value", acc.source_reference), AssignmentOperator.SIMPLE, acc.source_reference);
					acc.body.add_statement (new ExpressionStatement (assignment));
				}
			}

			if (acc.body != null && (acc.writable || acc.construction)) {
				var value_type = acc.prop.property_type.copy ();
				acc.value_parameter = new FormalParameter ("value", value_type, acc.source_reference);
				acc.body.scope.add (acc.value_parameter.name, acc.value_parameter);
			}
		}

		acc.accept_children (this);

		current_return_type = null;
	}

	public override void visit_signal (Signal sig) {
		sig.accept_children (this);
	}

	public override void visit_constructor (Constructor c) {
		c.this_parameter = new FormalParameter ("this", new ObjectType (current_class));
		c.scope.add (c.this_parameter.name, c.this_parameter);

		c.owner = current_symbol.scope;
		current_symbol = c;

		c.accept_children (this);

		current_symbol = current_symbol.parent_symbol;
	}

	public override void visit_destructor (Destructor d) {
		d.owner = current_symbol.scope;
		current_symbol = d;

		d.accept_children (this);

		current_symbol = current_symbol.parent_symbol;
	}

	public override void visit_named_argument (NamedArgument n) {
	}

	public override void visit_block (Block b) {
		b.owner = current_symbol.scope;
		current_symbol = b;

		b.accept_children (this);

		foreach (LocalVariable local in b.get_local_variables ()) {
			local.active = false;
		}

		foreach (Statement stmt in b.get_statements()) {
			b.add_error_types (stmt.get_error_types ());
		}

		current_symbol = current_symbol.parent_symbol;
	}

	public override void visit_declaration_statement (DeclarationStatement stmt) {
		var local = stmt.declaration as LocalVariable;
		if (local != null && local.initializer != null) {
			foreach (DataType error_type in local.initializer.get_error_types ()) {
				// ensure we can trace back which expression may throw errors of this type
				var initializer_error_type = error_type.copy ();
				initializer_error_type.source_reference = local.initializer.source_reference;

				stmt.add_error_type (initializer_error_type);
			}
		}
	}

	public override void visit_local_variable (LocalVariable local) {
		if (local.initializer != null) {
			local.initializer.target_type = local.variable_type;
		}

		local.accept_children (this);

		if (local.variable_type == null) {
			/* var type */

			if (local.initializer == null) {
				local.error = true;
				Report.error (local.source_reference, "var declaration not allowed without initializer");
				return;
			}
			if (local.initializer.value_type == null) {
				local.error = true;
				Report.error (local.source_reference, "var declaration not allowed with non-typed initializer");
				return;
			}

			local.variable_type = local.initializer.value_type.copy ();
			local.variable_type.value_owned = true;
			local.variable_type.floating_reference = false;

			local.initializer.target_type = local.variable_type;
		}

		if (local.initializer != null) {
			if (local.initializer.value_type == null) {
				if (!(local.initializer is MemberAccess) && !(local.initializer is LambdaExpression)) {
					local.error = true;
					Report.error (local.source_reference, "expression type not allowed as initializer");
					return;
				}

				if (local.initializer.symbol_reference is Method &&
				    local.variable_type is DelegateType) {
					var m = (Method) local.initializer.symbol_reference;
					var dt = (DelegateType) local.variable_type;
					var cb = dt.delegate_symbol;

					/* check whether method matches callback type */
					if (!cb.matches_method (m)) {
						local.error = true;
						Report.error (local.source_reference, "declaration of method `%s' doesn't match declaration of callback `%s'".printf (m.get_full_name (), cb.get_full_name ()));
						return;
					}

					local.initializer.value_type = local.variable_type;
				} else {
					local.error = true;
					Report.error (local.source_reference, "expression type not allowed as initializer");
					return;
				}
			}

			if (!local.initializer.value_type.compatible (local.variable_type)) {
				local.error = true;
				Report.error (local.source_reference, "Assignment: Cannot convert from `%s' to `%s'".printf (local.initializer.value_type.to_string (), local.variable_type.to_string ()));
				return;
			}

			if (local.initializer.value_type.is_disposable ()) {
				/* rhs transfers ownership of the expression */
				if (!(local.variable_type is PointerType) && !local.variable_type.value_owned) {
					/* lhs doesn't own the value */
					local.error = true;
					Report.error (local.source_reference, "Invalid assignment from owned expression to unowned variable");
					return;
				}
			}
		}

		current_source_file.add_type_dependency (local.variable_type, SourceFileDependencyType.SOURCE);

		current_symbol.scope.add (local.name, local);

		var block = (Block) current_symbol;
		block.add_local_variable (local);

		local.active = true;
	}

	/**
	 * Visit operation called for initializer lists
	 *
	 * @param list an initializer list
	 */
	public override void visit_initializer_list (InitializerList list) {
		if (list.target_type is ArrayType) {
			/* initializer is used as array initializer */
			var array_type = (ArrayType) list.target_type;

			foreach (Expression e in list.get_initializers ()) {
				e.target_type = array_type.element_type.copy ();
			}
		} else if (list.target_type != null && list.target_type.data_type is Struct) {
			/* initializer is used as struct initializer */
			var st = (Struct) list.target_type.data_type;

			var field_it = st.get_fields ().iterator ();
			foreach (Expression e in list.get_initializers ()) {
				Field field = null;
				while (field == null) {
					if (!field_it.next ()) {
						list.error = true;
						Report.error (e.source_reference, "too many expressions in initializer list for `%s'".printf (list.target_type.to_string ()));
						return;
					}
					field = field_it.get ();
					if (field.binding != MemberBinding.INSTANCE) {
						// we only initialize instance fields
						field = null;
					}
				}

				e.target_type = field.field_type.copy ();
			}
		} else if (list.target_type == null) {
			list.error = true;
			Report.error (list.source_reference, "initializer list used for unknown type");
			return;
		} else {
			list.error = true;
			Report.error (list.source_reference, "initializer list used for `%s', which is neither array nor struct".printf (list.target_type.to_string ()));
			return;
		}

		list.accept_children (this);

		bool error = false;
		foreach (Expression e in list.get_initializers ()) {
			if (e.value_type == null) {
				error = true;
				continue;
			}

			var unary = e as UnaryExpression;
			if (unary != null && (unary.operator == UnaryOperator.REF || unary.operator == UnaryOperator.OUT)) {
				// TODO check type for ref and out expressions
			} else if (!e.value_type.compatible (e.target_type)) {
				error = true;
				e.error = true;
				Report.error (e.source_reference, "Expected initializer of type `%s' but got `%s'".printf (e.target_type.to_string (), e.value_type.to_string ()));
			}
		}

		if (!error) {
			/* everything seems to be correct */
			list.value_type = list.target_type;
		}
	}

	public override void visit_expression_statement (ExpressionStatement stmt) {
		if (stmt.expression.error) {
			// ignore inner error
			stmt.error = true;
			return;
		}

		stmt.add_error_types (stmt.expression.get_error_types ());
	}

	public override void visit_if_statement (IfStatement stmt) {
		stmt.accept_children (this);

		if (stmt.condition.error) {
			/* if there was an error in the condition, skip this check */
			stmt.error = true;
			return;
		}

		if (!stmt.condition.value_type.compatible (bool_type)) {
			stmt.error = true;
			Report.error (stmt.condition.source_reference, "Condition must be boolean");
			return;
		}

		stmt.add_error_types (stmt.condition.get_error_types ());
		stmt.add_error_types (stmt.true_statement.get_error_types ());

		if (stmt.false_statement != null) {
			stmt.add_error_types (stmt.false_statement.get_error_types ());
		}
	}

	public override void visit_switch_section (SwitchSection section) {
		foreach (SwitchLabel label in section.get_labels ()) {
			label.accept (this);
		}

		section.owner = current_symbol.scope;
		current_symbol = section;

		foreach (Statement st in section.get_statements ()) {
			st.accept (this);
		}

		foreach (LocalVariable local in section.get_local_variables ()) {
			local.active = false;
		}

		current_symbol = current_symbol.parent_symbol;
	}

	public override void visit_while_statement (WhileStatement stmt) {
		stmt.accept_children (this);

		if (stmt.condition.error) {
			/* if there was an error in the condition, skip this check */
			stmt.error = true;
			return;
		}

		if (!stmt.condition.value_type.compatible (bool_type)) {
			stmt.error = true;
			Report.error (stmt.condition.source_reference, "Condition must be boolean");
			return;
		}

		stmt.add_error_types (stmt.condition.get_error_types ());
		stmt.add_error_types (stmt.body.get_error_types ());
	}

	public override void visit_do_statement (DoStatement stmt) {
		stmt.accept_children (this);

		if (stmt.condition.error) {
			/* if there was an error in the condition, skip this check */
			stmt.error = true;
			return;
		}

		if (!stmt.condition.value_type.compatible (bool_type)) {
			stmt.error = true;
			Report.error (stmt.condition.source_reference, "Condition must be boolean");
			return;
		}

		stmt.add_error_types (stmt.condition.get_error_types ());
		stmt.add_error_types (stmt.body.get_error_types ());
	}

	public override void visit_for_statement (ForStatement stmt) {
		stmt.accept_children (this);

		if (stmt.condition != null && stmt.condition.error) {
			/* if there was an error in the condition, skip this check */
			stmt.error = true;
			return;
		}

		if (stmt.condition != null && !stmt.condition.value_type.compatible (bool_type)) {
			stmt.error = true;
			Report.error (stmt.condition.source_reference, "Condition must be boolean");
			return;
		}

		if (stmt.condition != null) {
			stmt.add_error_types (stmt.condition.get_error_types ());
		}

		stmt.add_error_types (stmt.body.get_error_types ());
		foreach (Expression exp in stmt.get_initializer ()) {
			stmt.add_error_types (exp.get_error_types ());
		}
		foreach (Expression exp in stmt.get_iterator ()) {
			stmt.add_error_types (exp.get_error_types ());
		}
	}

	public override void visit_foreach_statement (ForeachStatement stmt) {
		// analyze collection expression first, used for type inference
		stmt.collection.accept (this);

		if (stmt.collection.error) {
			// ignore inner error
			stmt.error = true;
			return;
		} else if (stmt.collection.value_type == null) {
			Report.error (stmt.collection.source_reference, "invalid collection expression");
			stmt.error = true;
			return;
		}

		var collection_type = stmt.collection.value_type.copy ();
		stmt.collection.target_type = collection_type.copy ();
		
		DataType element_data_type = null;
	
		if (collection_type.is_array ()) {
			var array_type = (ArrayType) collection_type;
			element_data_type = array_type.element_type;
		} else if (collection_type.compatible (glist_type) || collection_type.compatible (gslist_type)) {
			if (collection_type.get_type_arguments ().size > 0) {
				element_data_type = (DataType) collection_type.get_type_arguments ().get (0);
			}
		} else if (iterable_type != null && collection_type.compatible (iterable_type)) {
			if (list_type == null || !collection_type.compatible (new ObjectType (list_type))) {
				// don't use iterator objects for lists for performance reasons
				var foreach_iterator_type = new ObjectType (iterator_type);
				foreach_iterator_type.value_owned = true;
				foreach_iterator_type.add_type_argument (stmt.type_reference);
				stmt.iterator_variable = new LocalVariable (foreach_iterator_type, "%s_it".printf (stmt.variable_name));

				stmt.add_local_variable (stmt.iterator_variable);
				stmt.iterator_variable.active = true;
			}

			var it_method = (Method) iterable_type.data_type.scope.lookup ("iterator");
			if (it_method.return_type.get_type_arguments ().size > 0) {
				var type_arg = it_method.return_type.get_type_arguments ().get (0);
				if (type_arg.type_parameter != null) {
					element_data_type = SemanticAnalyzer.get_actual_type (collection_type, it_method, type_arg, stmt);
				} else {
					element_data_type = type_arg;
				}
			}
		} else {
			stmt.error = true;
			Report.error (stmt.source_reference, "Gee.List not iterable");
			return;
		}

		if (element_data_type == null) {
			stmt.error = true;
			Report.error (stmt.collection.source_reference, "missing type argument for collection");
			return;
		}

		// analyze element type
		if (stmt.type_reference == null) {
			// var type
			stmt.type_reference = element_data_type.copy ();
		} else if (!element_data_type.compatible (stmt.type_reference)) {
			stmt.error = true;
			Report.error (stmt.source_reference, "Foreach: Cannot convert from `%s' to `%s'".printf (element_data_type.to_string (), stmt.type_reference.to_string ()));
			return;
		}
		
		current_source_file.add_type_dependency (stmt.type_reference, SourceFileDependencyType.SOURCE);

		stmt.element_variable = new LocalVariable (stmt.type_reference, stmt.variable_name);

		stmt.body.scope.add (stmt.variable_name, stmt.element_variable);

		stmt.body.add_local_variable (stmt.element_variable);
		stmt.element_variable.active = true;

		// analyze body
		stmt.owner = current_symbol.scope;
		current_symbol = stmt;

		stmt.body.accept (this);

		foreach (LocalVariable local in stmt.get_local_variables ()) {
			local.active = false;
		}

		current_symbol = current_symbol.parent_symbol;

		stmt.collection_variable = new LocalVariable (collection_type, "%s_collection".printf (stmt.variable_name));

		stmt.add_local_variable (stmt.collection_variable);
		stmt.collection_variable.active = true;


		stmt.add_error_types (stmt.collection.get_error_types ());
		stmt.add_error_types (stmt.body.get_error_types ());
	}

	public override void visit_return_statement (ReturnStatement stmt) {
		if (stmt.return_expression != null) {
			stmt.return_expression.target_type = current_return_type;
		}

		stmt.accept_children (this);

		if (stmt.return_expression != null && stmt.return_expression.error) {
			// ignore inner error
			stmt.error = true;
			return;
		}

		if (current_return_type == null) {
			stmt.error = true;
			Report.error (stmt.source_reference, "Return not allowed in this context");
			return;
		}

		if (stmt.return_expression == null && !(current_return_type is VoidType)) {
			stmt.error = true;
			Report.error (stmt.source_reference, "Return without value in non-void function");
			return;
		}

		if (stmt.return_expression != null && current_return_type is VoidType) {
			Report.error (stmt.source_reference, "Return with value in void function");
			return;
		}

		if (stmt.return_expression != null &&
		     !stmt.return_expression.value_type.compatible (current_return_type)) {
			Report.error (stmt.source_reference, "Return: Cannot convert from `%s' to `%s'".printf (stmt.return_expression.value_type.to_string (), current_return_type.to_string ()));
			return;
		}

		if (stmt.return_expression != null &&
		    stmt.return_expression.value_type.is_disposable () &&
		    !current_return_type.value_owned) {
			stmt.error = true;
			Report.error (stmt.source_reference, "Return value transfers ownership but method return type hasn't been declared to transfer ownership");
			return;
		}

		if (stmt.return_expression != null &&
		    stmt.return_expression.symbol_reference is LocalVariable &&
		    stmt.return_expression.value_type.is_disposable () &&
		    !current_return_type.value_owned) {
			Report.warning (stmt.source_reference, "Local variable with strong reference used as return value and method return type hasn't been declared to transfer ownership");
		}

		if (context.non_null && stmt.return_expression is NullLiteral
		    && !current_return_type.nullable) {
			Report.warning (stmt.source_reference, "`null' incompatible with return type `%s`".printf (current_return_type.to_string ()));
		}

		if (stmt.return_expression != null) {
			stmt.add_error_types (stmt.return_expression.get_error_types ());
		}
	}

	public override void visit_throw_statement (ThrowStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_try_statement (TryStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_catch_clause (CatchClause clause) {
		if (clause.error_type != null) {
			current_source_file.add_type_dependency (clause.error_type, SourceFileDependencyType.SOURCE);

			clause.error_variable = new LocalVariable (clause.error_type.copy (), clause.variable_name);

			clause.body.scope.add (clause.variable_name, clause.error_variable);
		} else {
			clause.error_type = new ErrorType (null, clause.source_reference);
		}

		clause.accept_children (this);
	}

	public override void visit_lock_statement (LockStatement stmt) {
		/* resource must be a member access and denote a Lockable */
		if (!(stmt.resource is MemberAccess && stmt.resource.symbol_reference is Lockable)) {
		    	stmt.error = true;
			stmt.resource.error = true;
			Report.error (stmt.resource.source_reference, "Expression is either not a member access or does not denote a lockable member");
			return;
		}

		/* parent symbol must be the current class */
		if (stmt.resource.symbol_reference.parent_symbol != current_class) {
		    	stmt.error = true;
			stmt.resource.error = true;
			Report.error (stmt.resource.source_reference, "Only members of the current class are lockable");
		}

		((Lockable) stmt.resource.symbol_reference).set_lock_used (true);
	}

	public override void visit_delete_statement (DeleteStatement stmt) {
		stmt.accept_children (this);

		if (stmt.expression.error) {
			// if there was an error in the inner expression, skip this check
			return;
		}

		if (!(stmt.expression.value_type is PointerType)) {
			stmt.error = true;
			Report.error (stmt.source_reference, "delete operator not supported for `%s'".printf (stmt.expression.value_type.to_string ()));
		}
	}

	private int create_sizes_from_initializer_list (InitializerList il, int rank, Gee.List<Literal> sl) {
		var init = new IntegerLiteral (il.size.to_string (), il.source_reference);
		init.accept (this);
		sl.add (init);

		int subsize = -1;
		foreach (Expression e in il.get_initializers ()) {
			if (e is InitializerList) {
				if (rank == 1) {
					il.error = true;
					e.error = true;
					Report.error (e.source_reference, "Expected array element, got array initializer list");
					return -1;
				}
				int size = create_sizes_from_initializer_list ((InitializerList)e, rank - 1, sl);
				if (size == -1) {
					return -1;
				}
				if (subsize >= 0 && subsize != size) {
					il.error = true;
					Report.error (il.source_reference, "Expected initializer list of size %d, got size %d".printf (subsize, size));
					return -1;
				} else {
					subsize = size;
				}
			} else {
				if (rank != 1) {
					il.error = true;
					e.error = true;
					Report.error (e.source_reference, "Expected array initializer list, got array element");
					return -1;
				}
			}
		}
		return il.size;
	}

	/**
	 * Visit operations called for array creation expresions.
	 *
	 * @param expr an array creation expression
	 */
	public override void visit_array_creation_expression (ArrayCreationExpression expr) {
		Gee.List<Expression> size = expr.get_sizes ();
		var initlist = expr.initializer_list;

		if (expr.element_type != null) {
			expr.element_type.accept (this);
		}

		foreach (Expression e in size) {
			e.accept (this);
		}

		var calc_sizes = new ArrayList<Literal> ();
		if (initlist != null) {
			initlist.target_type = new ArrayType (expr.element_type, expr.rank, expr.source_reference);

			initlist.accept (this);

			var ret = create_sizes_from_initializer_list (initlist, expr.rank, calc_sizes);
			if (ret == -1) {
				expr.error = true;
			}
		}

		if (size.size > 0) {
			/* check for errors in the size list */
			foreach (Expression e in size) {
				if (e.value_type == null) {
					/* return on previous error */
					return;
				} else if (!(e.value_type.data_type is Struct) || !((Struct) e.value_type.data_type).is_integer_type ()) {
					expr.error = true;
					Report.error (e.source_reference, "Expression of integer type expected");
				}
			}
		} else {
			if (initlist == null) {
				expr.error = true;
				/* this is an internal error because it is already handeld by the parser */
				Report.error (expr.source_reference, "internal error: initializer list expected");
			} else {
				foreach (Expression size in calc_sizes) {
					expr.append_size (size);
				}
			}
		}

		if (expr.error) {
			return;
		}

		/* check for wrong elements inside the initializer */
		if (expr.initializer_list != null && expr.initializer_list.value_type == null) {
			return;
		}

		/* try to construct the type of the array */
		if (expr.element_type == null) {
			expr.error = true;
			Report.error (expr.source_reference, "Cannot determine the element type of the created array");
			return;
		}

		expr.element_type.value_owned = true;

		expr.value_type = new ArrayType (expr.element_type, expr.rank, expr.source_reference);
		expr.value_type.value_owned = true;
	}

	public override void visit_boolean_literal (BooleanLiteral expr) {
		expr.value_type = bool_type;
	}

	public override void visit_character_literal (CharacterLiteral expr) {
		expr.value_type = new ValueType ((TypeSymbol) root_symbol.scope.lookup ("char"));
	}

	public override void visit_integer_literal (IntegerLiteral expr) {
		var int_type = new IntegerType ((TypeSymbol) root_symbol.scope.lookup (expr.get_type_name ()));
		int_type.literal = expr;
		expr.value_type = int_type;
	}

	public override void visit_real_literal (RealLiteral expr) {
		expr.value_type = new ValueType ((TypeSymbol) root_symbol.scope.lookup (expr.get_type_name ()));
	}

	public override void visit_string_literal (StringLiteral expr) {
		expr.value_type = string_type.copy ();
	}

	public override void visit_null_literal (NullLiteral expr) {
		expr.value_type = new NullType (expr.source_reference);
	}

	private DataType? get_value_type_for_symbol (Symbol sym, bool lvalue) {
		if (sym is Field) {
			var f = (Field) sym;
			var type = f.field_type.copy ();
			if (!lvalue) {
				type.value_owned = false;
			}
			return type;
		} else if (sym is Constant) {
			var c = (Constant) sym;
			return c.type_reference;
		} else if (sym is Property) {
			var prop = (Property) sym;
			if (prop.property_type != null) {
				var type = prop.property_type.copy ();
				type.value_owned = false;
				return type;
			}
		} else if (sym is FormalParameter) {
			var p = (FormalParameter) sym;
			var type = p.parameter_type.copy ();
			if (!lvalue) {
				type.value_owned = false;
			}
			return type;
		} else if (sym is LocalVariable) {
			var local = (LocalVariable) sym;
			var type = local.variable_type.copy ();
			if (!lvalue) {
				type.value_owned = false;
			}
			return type;
		} else if (sym is EnumValue) {
			return new ValueType ((TypeSymbol) sym.parent_symbol);
		} else if (sym is Method) {
			return new MethodType ((Method) sym);
		} else if (sym is Signal) {
			return new SignalType ((Signal) sym);
		}
		return null;
	}

	public static Symbol? symbol_lookup_inherited (Symbol sym, string name) {
		var result = sym.scope.lookup (name);
		if (result != null) {
			return result;
		}

		if (sym is Class) {
			var cl = (Class) sym;
			// first check interfaces without prerequisites
			// (prerequisites can be assumed to be met already)
			foreach (DataType base_type in cl.get_base_types ()) {
				if (base_type.data_type is Interface) {
					result = base_type.data_type.scope.lookup (name);
					if (result != null) {
						return result;
					}
				}
			}
			// then check base class recursively
			if (cl.base_class != null) {
				return symbol_lookup_inherited (cl.base_class, name);
			}
		} else if (sym is Struct) {
			var st = (Struct) sym;
			foreach (DataType base_type in st.get_base_types ()) {
				result = symbol_lookup_inherited (base_type.data_type, name);
				if (result != null) {
					return result;
				}
			}
		} else if (sym is Interface) {
			var iface = (Interface) sym;
			// first check interface prerequisites recursively
			foreach (DataType prerequisite in iface.get_prerequisites ()) {
				if (prerequisite.data_type is Interface) {
					result = symbol_lookup_inherited (prerequisite.data_type, name);
					if (result != null) {
						return result;
					}
				}
			}
			// then check class prerequisite recursively
			foreach (DataType prerequisite in iface.get_prerequisites ()) {
				if (prerequisite.data_type is Class) {
					result = symbol_lookup_inherited (prerequisite.data_type, name);
					if (result != null) {
						return result;
					}
				}
			}
		}

		return null;
	}

	public override void visit_parenthesized_expression (ParenthesizedExpression expr) {
		expr.inner.target_type = expr.target_type;

		expr.accept_children (this);

		if (expr.inner.error) {
			// ignore inner error
			expr.error = true;
			return;
		}

		if (expr.inner.value_type == null) {
			// static type may be null for method references
			expr.error = true;
			Report.error (expr.inner.source_reference, "Invalid expression type");
			return;
		}

		expr.value_type = expr.inner.value_type.copy ();
		// don't call g_object_ref_sink on inner and outer expression
		expr.value_type.floating_reference = false;

		// don't transform expression twice
		expr.inner.target_type = expr.inner.value_type.copy ();
	}
	
	public override void visit_member_access (MemberAccess expr) {
		Symbol base_symbol = null;
		FormalParameter this_parameter = null;
		bool may_access_instance_members = false;

		expr.symbol_reference = null;

		if (expr.qualified) {
			base_symbol = root_symbol;
			expr.symbol_reference = root_symbol.scope.lookup (expr.member_name);
		} else if (expr.inner == null) {
			if (expr.member_name == "this") {
				if (!is_in_instance_method ()) {
					expr.error = true;
					Report.error (expr.source_reference, "This access invalid outside of instance methods");
					return;
				}
			}

			base_symbol = current_symbol;

			var sym = current_symbol;
			while (sym != null && expr.symbol_reference == null) {
				if (this_parameter == null) {
					if (sym is CreationMethod) {
						var cm = (CreationMethod) sym;
						this_parameter = cm.this_parameter;
						may_access_instance_members = true;
					} else if (sym is Property) {
						var prop = (Property) sym;
						this_parameter = prop.this_parameter;
						may_access_instance_members = true;
					} else if (sym is Constructor) {
						var c = (Constructor) sym;
						this_parameter = c.this_parameter;
						may_access_instance_members = true;
					} else if (sym is Destructor) {
						var d = (Destructor) sym;
						this_parameter = d.this_parameter;
						may_access_instance_members = true;
					} else if (sym is Method) {
						var m = (Method) sym;
						this_parameter = m.this_parameter;
						may_access_instance_members = (m.binding == MemberBinding.INSTANCE);
					}
				}

				expr.symbol_reference = symbol_lookup_inherited (sym, expr.member_name);
				sym = sym.parent_symbol;
			}

			if (expr.symbol_reference == null) {
				foreach (NamespaceReference ns in current_using_directives) {
					var local_sym = ns.namespace_symbol.scope.lookup (expr.member_name);
					if (local_sym != null) {
						if (expr.symbol_reference != null) {
							expr.error = true;
							Report.error (expr.source_reference, "`%s' is an ambiguous reference between `%s' and `%s'".printf (expr.member_name, expr.symbol_reference.get_full_name (), local_sym.get_full_name ()));
							return;
						}
						expr.symbol_reference = local_sym;
					}
				}
			}
		} else {
			if (expr.inner.error) {
				/* if there was an error in the inner expression, skip this check */
				expr.error = true;
				return;
			}

			if (expr.inner is MemberAccess) {
				var ma = (MemberAccess) expr.inner;
				if (ma.prototype_access) {
					expr.error = true;
					Report.error (expr.source_reference, "Access to instance member `%s' denied".printf (expr.inner.symbol_reference.get_full_name ()));
					return;
				}
			}

			if (expr.inner is MemberAccess || expr.inner is BaseAccess) {
				base_symbol = expr.inner.symbol_reference;

				if (expr.creation_member && base_symbol is TypeSymbol) {
					// check for named creation method
					expr.symbol_reference = base_symbol.scope.lookup (".new." + expr.member_name);
				}

				if (expr.symbol_reference == null && (base_symbol is Namespace || base_symbol is TypeSymbol)) {
					expr.symbol_reference = base_symbol.scope.lookup (expr.member_name);
					if (expr.inner is BaseAccess) {
						// inner expression is base access
						// access to instance members of the base type possible
						may_access_instance_members = true;
					}
				}
			}

			if (expr.symbol_reference == null && expr.inner.value_type != null) {
				if (expr.pointer_member_access) {
					expr.symbol_reference = expr.inner.value_type.get_pointer_member (expr.member_name);
				} else {
					if (expr.inner.value_type.data_type != null) {
						base_symbol = expr.inner.value_type.data_type;
					}
					expr.symbol_reference = expr.inner.value_type.get_member (expr.member_name);
				}
				if (expr.symbol_reference != null) {
					// inner expression is variable, field, or parameter
					// access to instance members of the corresponding type possible
					may_access_instance_members = true;
				}
			}

			if (expr.symbol_reference == null && expr.inner is MemberAccess && base_symbol is Struct) {
				// check for named struct creation method
				expr.symbol_reference = base_symbol.scope.lookup (".new." + expr.member_name);
			}

			if (expr.symbol_reference == null && expr.inner.value_type != null && expr.inner.value_type.is_dynamic) {
				// allow late bound members for dynamic types
				var dynamic_object_type = (ObjectType) expr.inner.value_type;
				if (expr.parent_node is InvocationExpression) {
					var invoc = (InvocationExpression) expr.parent_node;
					if (invoc.call == expr) {
						// dynamic method
						DataType ret_type;
						if (invoc.target_type != null) {
							ret_type = invoc.target_type.copy ();
							ret_type.value_owned = true;
						} else if (invoc.parent_node is ExpressionStatement) {
							ret_type = new VoidType ();
						} else {
							// expect dynamic object of the same type
							ret_type = expr.inner.value_type.copy ();
						}
						var m = new DynamicMethod (expr.inner.value_type, expr.member_name, ret_type, expr.source_reference);
						m.invocation = invoc;
						m.add_error_type (new ErrorType (null));
						m.access = SymbolAccessibility.PUBLIC;
						m.add_parameter (new FormalParameter.with_ellipsis ());
						dynamic_object_type.type_symbol.scope.add (null, m);
						expr.symbol_reference = m;
					}
				} else if (expr.parent_node is Assignment) {
					var a = (Assignment) expr.parent_node;
					if (a.left == expr
					    && (a.operator == AssignmentOperator.ADD
					        || a.operator == AssignmentOperator.SUB)) {
						// dynamic signal
						var s = new DynamicSignal (expr.inner.value_type, expr.member_name, new VoidType (), expr.source_reference);
						s.handler = a.right;
						s.access = SymbolAccessibility.PUBLIC;
						dynamic_object_type.type_symbol.scope.add (null, s);
						expr.symbol_reference = s;
					} else if (a.left == expr) {
						// dynamic property assignment
						var prop = new DynamicProperty (expr.inner.value_type, expr.member_name, expr.source_reference);
						prop.access = SymbolAccessibility.PUBLIC;
						prop.set_accessor = new PropertyAccessor (false, true, false, null, null);
						prop.set_accessor.access = SymbolAccessibility.PUBLIC;
						prop.owner = expr.inner.value_type.data_type.scope;
						dynamic_object_type.type_symbol.scope.add (null, prop);
						expr.symbol_reference = prop;
					}
				}
				if (expr.symbol_reference == null) {
					// dynamic property read access
					var prop = new DynamicProperty (expr.inner.value_type, expr.member_name, expr.source_reference);
					if (expr.target_type != null) {
						prop.property_type = expr.target_type;
					} else {
						// expect dynamic object of the same type
						prop.property_type = expr.inner.value_type.copy ();
					}
					prop.access = SymbolAccessibility.PUBLIC;
					prop.get_accessor = new PropertyAccessor (true, false, false, null, null);
					prop.get_accessor.access = SymbolAccessibility.PUBLIC;
					prop.owner = expr.inner.value_type.data_type.scope;
					dynamic_object_type.type_symbol.scope.add (null, prop);
					expr.symbol_reference = prop;
				}
				if (expr.symbol_reference != null) {
					may_access_instance_members = true;
				}
			}
		}

		if (expr.symbol_reference == null) {
			expr.error = true;

			string base_type_name = "(null)";
			if (expr.inner != null && expr.inner.value_type != null) {
				base_type_name = expr.inner.value_type.to_string ();
			} else if (base_symbol != null) {
				base_type_name = base_symbol.get_full_name ();
			}

			Report.error (expr.source_reference, "The name `%s' does not exist in the context of `%s'".printf (expr.member_name, base_type_name));
			return;
		}

		var member = expr.symbol_reference;
		var access = SymbolAccessibility.PUBLIC;
		bool instance = false;
		if (member is Field) {
			var f = (Field) member;
			access = f.access;
			instance = (f.binding == MemberBinding.INSTANCE);
		} else if (member is Method) {
			var m = (Method) member;
			access = m.access;
			instance = (m.binding == MemberBinding.INSTANCE);
		} else if (member is Property) {
			var prop = (Property) member;
			access = prop.access;
			if (expr.lvalue) {
				if (prop.set_accessor == null) {
					expr.error = true;
					Report.error (expr.source_reference, "Property `%s' is read-only".printf (prop.get_full_name ()));
					return;
				}
				if (prop.access == SymbolAccessibility.PUBLIC) {
					access = prop.set_accessor.access;
				} else if (prop.access == SymbolAccessibility.PROTECTED
				           && prop.set_accessor.access != SymbolAccessibility.PUBLIC) {
					access = prop.set_accessor.access;
				}
			} else {
				if (prop.get_accessor == null) {
					expr.error = true;
					Report.error (expr.source_reference, "Property `%s' is write-only".printf (prop.get_full_name ()));
					return;
				}
				if (prop.access == SymbolAccessibility.PUBLIC) {
					access = prop.get_accessor.access;
				} else if (prop.access == SymbolAccessibility.PROTECTED
				           && prop.get_accessor.access != SymbolAccessibility.PUBLIC) {
					access = prop.get_accessor.access;
				}
			}
			instance = (prop.binding == MemberBinding.INSTANCE);
		} else if (member is Signal) {
			instance = true;
		}

		if (access == SymbolAccessibility.PRIVATE) {
			var target_type = member.parent_symbol;

			bool in_target_type = false;
			for (Symbol this_symbol = current_symbol; this_symbol != null; this_symbol = this_symbol.parent_symbol) {
				if (target_type == this_symbol) {
					in_target_type = true;
					break;
				}
			}

			if (!in_target_type) {
				expr.error = true;
				Report.error (expr.source_reference, "Access to private member `%s' denied".printf (member.get_full_name ()));
				return;
			}
		}
		if (instance && !may_access_instance_members) {
			expr.prototype_access = true;

			if (expr.symbol_reference is Method) {
				// also set static type for prototype access
				// required when using instance methods as delegates in constants
				// TODO replace by MethodPrototype
				expr.value_type = get_value_type_for_symbol (expr.symbol_reference, expr.lvalue);
			} else if (expr.symbol_reference is Field) {
				expr.value_type = new FieldPrototype ((Field) expr.symbol_reference);
			}
		} else {
			// implicit this access
			if (instance && expr.inner == null) {
				expr.inner = new MemberAccess (null, "this", expr.source_reference);
				expr.inner.value_type = this_parameter.parameter_type.copy ();
				expr.inner.symbol_reference = this_parameter;
			}

			expr.value_type = get_value_type_for_symbol (expr.symbol_reference, expr.lvalue);

			// resolve generic return values
			if (expr.value_type != null && expr.value_type.type_parameter != null) {
				if (expr.inner != null) {
					expr.value_type = get_actual_type (expr.inner.value_type, expr.symbol_reference, expr.value_type, expr);
					if (expr.value_type == null) {
						return;
					}
				}
			}

			if (expr.symbol_reference is Method) {
				var m = (Method) expr.symbol_reference;

				Method base_method;
				if (m.base_method != null) {
					base_method = m.base_method;
				} else if (m.base_interface_method != null) {
					base_method = m.base_interface_method;
				} else {
					base_method = m;
				}

				if (instance && base_method.parent_symbol != null) {
					expr.inner.target_type = get_data_type_for_symbol ((TypeSymbol) base_method.parent_symbol);
				}
			} else if (expr.symbol_reference is Property) {
				var prop = (Property) expr.symbol_reference;

				Property base_property;
				if (prop.base_property != null) {
					base_property = prop.base_property;
				} else if (prop.base_interface_property != null) {
					base_property = prop.base_interface_property;
				} else {
					base_property = prop;
				}

				if (instance && base_property.parent_symbol != null) {
					expr.inner.target_type = get_data_type_for_symbol ((TypeSymbol) base_property.parent_symbol);
				}
			} else if ((expr.symbol_reference is Field
			            || expr.symbol_reference is Signal)
			           && instance && expr.symbol_reference.parent_symbol != null) {
				expr.inner.target_type = get_data_type_for_symbol ((TypeSymbol) expr.symbol_reference.parent_symbol);
			}
		}

		current_source_file.add_symbol_dependency (expr.symbol_reference, SourceFileDependencyType.SOURCE);
	}

	public static DataType get_data_type_for_symbol (TypeSymbol sym) {
		DataType type = null;

		if (sym is ObjectTypeSymbol) {
			type = new ObjectType ((ObjectTypeSymbol) sym);
		} else if (sym is Struct) {
			type = new ValueType ((Struct) sym);
		} else if (sym is Enum) {
			type = new ValueType ((Enum) sym);
		} else if (sym is ErrorDomain) {
			type = new ErrorType ((ErrorDomain) sym);
		} else {
			Report.error (null, "internal error: `%s' is not a supported type".printf (sym.get_full_name ()));
			return new InvalidType ();
		}

		return type;
	}

	public override void visit_invocation_expression (InvocationExpression expr) {
		expr.call.accept (this);

		if (expr.call.error) {
			/* if method resolving didn't succeed, skip this check */
			expr.error = true;
			return;
		}

		if (expr.call is MemberAccess) {
			var ma = (MemberAccess) expr.call;
			if (ma.prototype_access) {
				expr.error = true;
				Report.error (expr.source_reference, "Access to instance member `%s' denied".printf (expr.call.symbol_reference.get_full_name ()));
				return;
			}
		}

		var mtype = expr.call.value_type;

		// check for struct construction
		if (expr.call is MemberAccess &&
		    (expr.call.symbol_reference is CreationMethod
		     || expr.call.symbol_reference is Struct)) {
			var struct_creation_expression = new ObjectCreationExpression ((MemberAccess) expr.call, expr.source_reference);
			struct_creation_expression.struct_creation = true;
			foreach (Expression arg in expr.get_argument_list ()) {
				struct_creation_expression.add_argument (arg);
			}
			struct_creation_expression.target_type = expr.target_type;
			replaced_nodes.add (expr);
			expr.parent_node.replace_expression (expr, struct_creation_expression);
			struct_creation_expression.accept (this);
			return;
		}

		Gee.List<FormalParameter> params;

		if (mtype != null && mtype.is_invokable ()) {
			params = mtype.get_parameters ();
		} else {
			expr.error = true;
			Report.error (expr.source_reference, "invocation not supported in this context");
			return;
		}

		var args = expr.get_argument_list ();
		Iterator<Expression> arg_it = args.iterator ();
		foreach (FormalParameter param in params) {
			if (param.ellipsis) {
				break;
			}

			if (arg_it.next ()) {
				Expression arg = arg_it.get ();

				/* store expected type for callback parameters */
				arg.target_type = param.parameter_type;

				// resolve generic type parameters
				var ma = expr.call as MemberAccess;
				if (arg.target_type.type_parameter != null) {
					if (ma != null && ma.inner != null) {
						arg.target_type = get_actual_type (ma.inner.value_type, ma.symbol_reference, arg.target_type, arg);
						assert (arg.target_type != null);
					}
				}
			}
		}

		foreach (Expression arg in expr.get_argument_list ()) {
			arg.accept (this);
		}

		DataType ret_type;

		ret_type = mtype.get_return_type ();
		params = mtype.get_parameters ();

		if (ret_type is VoidType) {
			// void return type
			if (!(expr.parent_node is ExpressionStatement)
			    && !(expr.parent_node is ForStatement)) {
				// A void method invocation can be in the initializer or
				// iterator of a for statement
				expr.error = true;
				Report.error (expr.source_reference, "invocation of void method not allowed as expression");
				return;
			}
		}

		// resolve generic return values
		var ma = expr.call as MemberAccess;
		if (ret_type.type_parameter != null) {
			if (ma != null && ma.inner != null) {
				ret_type = get_actual_type (ma.inner.value_type, ma.symbol_reference, ret_type, expr);
				if (ret_type == null) {
					return;
				}
			}
		}
		Gee.List<DataType> resolved_type_args = new ArrayList<DataType> ();
		foreach (DataType type_arg in ret_type.get_type_arguments ()) {
			if (type_arg.type_parameter != null && ma != null && ma.inner != null) {
				resolved_type_args.add (get_actual_type (ma.inner.value_type, ma.symbol_reference, type_arg, expr));
			} else {
				resolved_type_args.add (type_arg);
			}
		}
		ret_type = ret_type.copy ();
		ret_type.remove_all_type_arguments ();
		foreach (DataType resolved_type_arg in resolved_type_args) {
			ret_type.add_type_argument (resolved_type_arg);
		}

		if (mtype is MethodType) {
			var m = ((MethodType) mtype).method_symbol;
			foreach (DataType error_type in m.get_error_types ()) {
				// ensure we can trace back which expression may throw errors of this type
				var call_error_type = error_type.copy ();
				call_error_type.source_reference = expr.source_reference;

				expr.add_error_type (call_error_type);
			}
		}

		expr.value_type = ret_type;

		check_arguments (expr, mtype, params, expr.get_argument_list ());
	}

	private bool check_arguments (Expression expr, DataType mtype, Gee.List<FormalParameter> params, Gee.List<Expression> args) {
		Expression prev_arg = null;
		Iterator<Expression> arg_it = args.iterator ();

		bool diag = (mtype is MethodType && ((MethodType) mtype).method_symbol.get_attribute ("Diagnostics") != null);

		bool ellipsis = false;
		int i = 0;
		foreach (FormalParameter param in params) {
			if (param.ellipsis) {
				ellipsis = true;
				break;
			}

			/* header file necessary if we need to cast argument */
			current_source_file.add_type_dependency (param.parameter_type, SourceFileDependencyType.SOURCE);

			if (!arg_it.next ()) {
				if (param.default_expression == null) {
					expr.error = true;
					Report.error (expr.source_reference, "Too few arguments, method `%s' does not take %d arguments".printf (mtype.to_string (), args.size));
					return false;
				}
			} else {
				var arg = arg_it.get ();
				if (arg.error) {
					// ignore inner error
					expr.error = true;
					return false;
				} else if (arg.value_type == null) {
					// disallow untyped arguments except for type inference of callbacks
					if (!(param.parameter_type is DelegateType) || !(arg.symbol_reference is Method)) {
						expr.error = true;
						Report.error (arg.source_reference, "Invalid type for argument %d".printf (i + 1));
						return false;
					}
				} else if (arg.target_type != null && !arg.value_type.compatible (arg.target_type)
				           && !(arg is NullLiteral && param.direction == ParameterDirection.OUT)) {
					expr.error = true;
					Report.error (arg.source_reference, "Argument %d: Cannot convert from `%s' to `%s'".printf (i + 1, arg.value_type.to_string (), arg.target_type.to_string ()));
					return false;
				} else {
					// 0 => null, 1 => in, 2 => ref, 3 => out
					int arg_type = 1;
					if (arg.value_type is NullType) {
						arg_type = 0;
					} else if (arg is UnaryExpression) {
						var unary = (UnaryExpression) arg;
						if (unary.operator == UnaryOperator.REF) {
							arg_type = 2;
						} else if (unary.operator == UnaryOperator.OUT) {
							arg_type = 3;
						}
					}

					if (arg_type == 0) {
						if (param.direction == ParameterDirection.REF) {
							expr.error = true;
							Report.error (arg.source_reference, "Argument %d: Cannot pass null to reference parameter".printf (i + 1));
							return false;
						} else if (context.non_null && param.direction != ParameterDirection.OUT && !param.parameter_type.nullable) {
							Report.warning (arg.source_reference, "Argument %d: Cannot pass null to non-null parameter type".printf (i + 1));
						}
					} else if (arg_type == 1) {
						if (param.direction != ParameterDirection.IN) {
							expr.error = true;
							Report.error (arg.source_reference, "Argument %d: Cannot pass value to reference or output parameter".printf (i + 1));
							return false;
						}
					} else if (arg_type == 2) {
						if (param.direction != ParameterDirection.REF) {
							expr.error = true;
							Report.error (arg.source_reference, "Argument %d: Cannot pass ref argument to non-reference parameter".printf (i + 1));
							return false;
						}
					} else if (arg_type == 3) {
						if (param.direction != ParameterDirection.OUT) {
							expr.error = true;
							Report.error (arg.source_reference, "Argument %d: Cannot pass out argument to non-output parameter".printf (i + 1));
							return false;
						}
					}
				}

				prev_arg = arg;

				i++;
			}
		}

		if (ellipsis) {
			while (arg_it.next ()) {
				var arg = arg_it.get ();
				if (arg.error) {
					// ignore inner error
					expr.error = true;
					return false;
				} else if (arg.value_type == null) {
					// disallow untyped arguments except for type inference of callbacks
					if (!(arg.symbol_reference is Method)) {
						expr.error = true;
						Report.error (expr.source_reference, "Invalid type for argument %d".printf (i + 1));
						return false;
					}
				}

				i++;
			}
		} else if (!ellipsis && arg_it.next ()) {
			expr.error = true;
			Report.error (expr.source_reference, "Too many arguments, method `%s' does not take %d arguments".printf (mtype.to_string (), args.size));
			return false;
		}

		if (diag && prev_arg != null) {
			var format_arg = prev_arg as StringLiteral;
			if (format_arg != null) {
				format_arg.value = "\"%s:%d: %s".printf (Path.get_basename (expr.source_reference.file.filename), expr.source_reference.first_line, format_arg.value.offset (1));
			}
		}

		return true;
	}

	private static DataType? get_instance_base_type (DataType instance_type, DataType base_type, CodeNode node_reference) {
		// construct a new type reference for the base type with correctly linked type arguments
		ReferenceType instance_base_type;
		if (base_type.data_type is Class) {
			instance_base_type = new ObjectType ((Class) base_type.data_type);
		} else {
			instance_base_type = new ObjectType ((Interface) base_type.data_type);
		}
		foreach (DataType type_arg in base_type.get_type_arguments ()) {
			if (type_arg.type_parameter != null) {
				// link to type argument of derived type
				int param_index = instance_type.data_type.get_type_parameter_index (type_arg.type_parameter.name);
				if (param_index == -1) {
					Report.error (node_reference.source_reference, "internal error: unknown type parameter %s".printf (type_arg.type_parameter.name));
					node_reference.error = true;
					return null;
				}
				type_arg = instance_type.get_type_arguments ().get (param_index);
			}
			instance_base_type.add_type_argument (type_arg);
		}
		return instance_base_type;
	}

	public static DataType? get_actual_type (DataType derived_instance_type, Symbol generic_member, DataType generic_type, CodeNode node_reference) {
		DataType instance_type = derived_instance_type;

		while (instance_type is PointerType) {
			var instance_pointer_type = (PointerType) instance_type;
			instance_type = instance_pointer_type.base_type;
		}

		// trace type arguments back to the datatype where the method has been declared
		while (instance_type.data_type != generic_member.parent_symbol) {
			DataType instance_base_type = null;

			// use same algorithm as symbol_lookup_inherited
			if (instance_type.data_type is Class) {
				var cl = (Class) instance_type.data_type;
				// first check interfaces without prerequisites
				// (prerequisites can be assumed to be met already)
				foreach (DataType base_type in cl.get_base_types ()) {
					if (base_type.data_type is Interface) {
						if (base_type.data_type.scope.lookup (generic_member.name) == generic_member) {
							instance_base_type = get_instance_base_type (instance_type, base_type, node_reference);
							break;
						}
					}
				}
				// then check base class recursively
				if (instance_base_type == null) {
					foreach (DataType base_type in cl.get_base_types ()) {
						if (base_type.data_type is Class) {
							if (symbol_lookup_inherited (cl.base_class, generic_member.name) == generic_member) {
								instance_base_type = get_instance_base_type (instance_type, base_type, node_reference);
								break;
							}
						}
					}
				}
			} else if (instance_type.data_type is Struct) {
				var st = (Struct) instance_type.data_type;
				foreach (DataType base_type in st.get_base_types ()) {
					if (base_type.data_type.scope.lookup (generic_member.name) == generic_member) {
						instance_base_type = get_instance_base_type (instance_type, base_type, node_reference);
						break;
					}
				}
			} else if (instance_type.data_type is Interface) {
				var iface = (Interface) instance_type.data_type;
				// first check interface prerequisites recursively
				foreach (DataType prerequisite in iface.get_prerequisites ()) {
					if (prerequisite.data_type is Interface) {
						if (symbol_lookup_inherited (prerequisite.data_type, generic_member.name) == generic_member) {
							instance_base_type = get_instance_base_type (instance_type, prerequisite, node_reference);
							break;
						}
					}
				}
				if (instance_base_type == null) {
					// then check class prerequisite recursively
					foreach (DataType prerequisite in iface.get_prerequisites ()) {
						if (prerequisite.data_type is Class) {
							if (symbol_lookup_inherited (prerequisite.data_type, generic_member.name) == generic_member) {
								instance_base_type = get_instance_base_type (instance_type, prerequisite, node_reference);
								break;
							}
						}
					}
				}
			} else {
				Report.error (node_reference.source_reference, "internal error: unsupported generic type");
				node_reference.error = true;
				return null;
			}

			if (instance_base_type == null) {
				Report.error (node_reference.source_reference, "internal error: unable to find generic member `%s'".printf (generic_member.name));
				node_reference.error = true;
				return null;
			}
			instance_type = instance_base_type;

			while (instance_type is PointerType) {
				var instance_pointer_type = (PointerType) instance_type;
				instance_type = instance_pointer_type.base_type;
			}
		}
		if (instance_type.data_type != generic_member.parent_symbol) {
			Report.error (node_reference.source_reference, "internal error: generic type parameter tracing not supported yet");
			node_reference.error = true;
			return null;
		}
		int param_index = instance_type.data_type.get_type_parameter_index (generic_type.type_parameter.name);
		if (param_index == -1) {
			Report.error (node_reference.source_reference, "internal error: unknown type parameter %s".printf (generic_type.type_parameter.name));
			node_reference.error = true;
			return null;
		}

		DataType actual_type = null;
		if (param_index < instance_type.get_type_arguments ().size) {
			actual_type = (DataType) instance_type.get_type_arguments ().get (param_index);
		}
		if (actual_type == null) {
			// no actual type available
			return generic_type;
		}
		actual_type = actual_type.copy ();
		actual_type.is_type_argument = true;
		actual_type.value_owned = actual_type.value_owned && generic_type.value_owned;
		return actual_type;
	}

	public override void visit_element_access (ElementAccess expr) {
		if (expr.container.value_type == null) {
			/* don't proceed if a child expression failed */
			expr.error = true;
			return;
		}

		var container_type = expr.container.value_type.data_type;

		bool index_int_type_check = true;

		var pointer_type = expr.container.value_type as PointerType;

		/* assign a value_type when possible */
		if (expr.container.value_type is ArrayType) {
			var array_type = (ArrayType) expr.container.value_type;
			expr.value_type = array_type.element_type.copy ();
			if (!expr.lvalue) {
				expr.value_type.value_owned = false;
			}
		} else if (pointer_type != null && !pointer_type.base_type.is_reference_type_or_type_parameter ()) {
			expr.value_type = pointer_type.base_type.copy ();
		} else if (container_type == string_type.data_type) {
			if (expr.get_indices ().size != 1) {
				expr.error = true;
				Report.error (expr.source_reference, "Element access with more than one dimension is not supported for strings");
				return;
			}

			expr.value_type = unichar_type;
		} else if (container_type != null && list_type != null && map_type != null &&
		           (container_type.is_subtype_of (list_type) || container_type.is_subtype_of (map_type))) {
			Gee.List<Expression> indices = expr.get_indices ();
			if (indices.size != 1) {
				expr.error = true;
				Report.error (expr.source_reference, "Element access with more than one dimension is not supported for the specified type");
				return;
			}
			Iterator<Expression> indices_it = indices.iterator ();
			indices_it.next ();
			var index = indices_it.get ();
			index_int_type_check = false;

			// lookup symbol in interface instead of class as implemented interface methods are not in VAPI files
			Symbol get_sym = null;
			if (container_type.is_subtype_of (list_type)) {
				get_sym = list_type.scope.lookup ("get");
			} else if (container_type.is_subtype_of (map_type)) {
				get_sym = map_type.scope.lookup ("get");
			}
			var get_method = (Method) get_sym;
			Gee.List<FormalParameter> get_params = get_method.get_parameters ();
			Iterator<FormalParameter> get_params_it = get_params.iterator ();
			get_params_it.next ();
			var get_param = get_params_it.get ();

			var index_type = get_param.parameter_type;
			if (index_type.type_parameter != null) {
				index_type = get_actual_type (expr.container.value_type, get_method, get_param.parameter_type, expr);
			}

			if (!index.value_type.compatible (index_type)) {
				expr.error = true;
				Report.error (expr.source_reference, "index expression: Cannot convert from `%s' to `%s'".printf (index.value_type.to_string (), index_type.to_string ()));
				return;
			}

			expr.value_type = get_actual_type (expr.container.value_type, get_method, get_method.return_type, expr).copy ();
			if (expr.lvalue) {
				// get () returns owned value, set () accepts unowned value
				expr.value_type.value_owned = false;
			}
		} else {
			expr.error = true;
			Report.error (expr.source_reference, "The expression `%s' does not denote an Array".printf (expr.container.value_type.to_string ()));
		}

		if (index_int_type_check) {
			/* check if the index is of type integer */
			foreach (Expression e in expr.get_indices ()) {
				/* don't proceed if a child expression failed */
				if (e.value_type == null) {
					return;
				}

				/* check if the index is of type integer */
				if (!(e.value_type.data_type is Struct) || !((Struct) e.value_type.data_type).is_integer_type ()) {
					expr.error = true;
					Report.error (e.source_reference, "Expression of integer type expected");
				}
			}
		}
	}

	private bool is_in_instance_method () {
		var sym = current_symbol;
		while (sym != null) {
			if (sym is CreationMethod) {
				return true;
			} else if (sym is Method) {
				var m = (Method) sym;
				return m.binding == MemberBinding.INSTANCE;
			} else if (sym is Constructor) {
				var c = (Constructor) sym;
				return c.binding == MemberBinding.INSTANCE;
			} else if (sym is Destructor) {
				return true;
			} else if (sym is Property) {
				return true;
			}
			sym = sym.parent_symbol;
		}

		return false;
	}

	public override void visit_base_access (BaseAccess expr) {
		if (!is_in_instance_method ()) {
			expr.error = true;
			Report.error (expr.source_reference, "Base access invalid outside of instance methods");
			return;
		}

		if (current_class == null) {
			if (current_struct == null) {
				expr.error = true;
				Report.error (expr.source_reference, "Base access invalid outside of class and struct");
				return;
			} else if (current_struct.get_base_types ().size != 1) {
				expr.error = true;
				Report.error (expr.source_reference, "Base access invalid without base type %d".printf (current_struct.get_base_types ().size));
				return;
			}
			Iterator<DataType> base_type_it = current_struct.get_base_types ().iterator ();
			base_type_it.next ();
			expr.value_type = base_type_it.get ();
		} else if (current_class.base_class == null) {
			expr.error = true;
			Report.error (expr.source_reference, "Base access invalid without base class");
			return;
		} else {
			expr.value_type = new ObjectType (current_class.base_class);
		}

		expr.symbol_reference = expr.value_type.data_type;
	}

	public override void visit_postfix_expression (PostfixExpression expr) {
		expr.value_type = expr.inner.value_type;
	}

	public override void visit_object_creation_expression (ObjectCreationExpression expr) {
		if (expr.member_name != null) {
			expr.member_name.accept (this);
		}

		TypeSymbol type = null;

		if (expr.type_reference == null) {
			if (expr.member_name == null) {
				expr.error = true;
				Report.error (expr.source_reference, "Incomplete object creation expression");
				return;
			}

			if (expr.member_name.symbol_reference == null) {
				expr.error = true;
				return;
			}

			var constructor_sym = expr.member_name.symbol_reference;
			var type_sym = expr.member_name.symbol_reference;

			var type_args = expr.member_name.get_type_arguments ();

			if (constructor_sym is Method) {
				type_sym = constructor_sym.parent_symbol;

				var constructor = (Method) constructor_sym;
				if (!(constructor_sym is CreationMethod)) {
					expr.error = true;
					Report.error (expr.source_reference, "`%s' is not a creation method".printf (constructor.get_full_name ()));
					return;
				}

				expr.symbol_reference = constructor;

				type_args = ((MemberAccess) expr.member_name.inner).get_type_arguments ();
			} else if (constructor_sym is ErrorCode) {
				type_sym = constructor_sym.parent_symbol;

				expr.symbol_reference = constructor_sym;
			}

			if (type_sym is Class) {
				type = (TypeSymbol) type_sym;
				expr.type_reference = new ObjectType ((Class) type);
			} else if (type_sym is Struct) {
				type = (TypeSymbol) type_sym;
				expr.type_reference = new ValueType (type);
			} else if (type_sym is ErrorDomain) {
				expr.type_reference = new ErrorType ((ErrorDomain) type_sym, expr.source_reference);
			} else {
				expr.error = true;
				Report.error (expr.source_reference, "`%s' is not a class, struct, or error domain".printf (type_sym.get_full_name ()));
				return;
			}

			foreach (DataType type_arg in type_args) {
				expr.type_reference.add_type_argument (type_arg);

				current_source_file.add_type_dependency (type_arg, SourceFileDependencyType.SOURCE);
			}
		} else {
			type = expr.type_reference.data_type;
		}

		current_source_file.add_symbol_dependency (type, SourceFileDependencyType.SOURCE);

		expr.value_type = expr.type_reference.copy ();
		expr.value_type.value_owned = true;

		int given_num_type_args = expr.type_reference.get_type_arguments ().size;
		int expected_num_type_args = 0;

		if (type is Class) {
			var cl = (Class) type;

			expected_num_type_args = cl.get_type_parameters ().size;

			if (expr.struct_creation) {
				expr.error = true;
				Report.error (expr.source_reference, "syntax error, use `new' to create new objects");
				return;
			}

			if (cl.is_abstract) {
				expr.value_type = null;
				expr.error = true;
				Report.error (expr.source_reference, "Can't create instance of abstract class `%s'".printf (cl.get_full_name ()));
				return;
			}

			if (cl.is_static) {
				expr.value_type = null;
				expr.error = true;
				Report.error (expr.source_reference, "Can't create instance of static class `%s'".printf (cl.get_full_name ()));
				return;
			}

			if (expr.symbol_reference == null) {
				expr.symbol_reference = cl.default_construction_method;
			}

			while (cl != null) {
				if (cl == initially_unowned_type) {
					expr.value_type.floating_reference = true;
					break;
				}

				cl = cl.base_class;
			}
		} else if (type is Struct) {
			var st = (Struct) type;

			expected_num_type_args = st.get_type_parameters ().size;

			if (!expr.struct_creation) {
				Report.warning (expr.source_reference, "deprecated syntax, don't use `new' to initialize structs");
			}

			if (expr.symbol_reference == null) {
				expr.symbol_reference = st.default_construction_method;
			}
		}

		if (expected_num_type_args > given_num_type_args) {
			expr.error = true;
			Report.error (expr.source_reference, "too few type arguments");
			return;
		} else if (expected_num_type_args < given_num_type_args) {
			expr.error = true;
			Report.error (expr.source_reference, "too many type arguments");
			return;
		}

		if (expr.symbol_reference == null && expr.get_argument_list ().size != 0) {
			expr.value_type = null;
			expr.error = true;
			Report.error (expr.source_reference, "No arguments allowed when constructing type `%s'".printf (type.get_full_name ()));
			return;
		}

		if (expr.symbol_reference is Method) {
			var m = (Method) expr.symbol_reference;

			var args = expr.get_argument_list ();
			Iterator<Expression> arg_it = args.iterator ();
			foreach (FormalParameter param in m.get_parameters ()) {
				if (param.ellipsis) {
					break;
				}

				if (arg_it.next ()) {
					Expression arg = arg_it.get ();

					/* store expected type for callback parameters */
					arg.target_type = param.parameter_type;
				}
			}

			foreach (Expression arg in args) {
				arg.accept (this);
			}

			check_arguments (expr, new MethodType (m), m.get_parameters (), args);

			foreach (DataType error_type in m.get_error_types ()) {
				// ensure we can trace back which expression may throw errors of this type
				var call_error_type = error_type.copy ();
				call_error_type.source_reference = expr.source_reference;

				expr.add_error_type (call_error_type);
			}
		} else if (expr.type_reference is ErrorType) {
			expr.accept_children (this);

			if (expr.get_argument_list ().size == 0) {
				expr.error = true;
				Report.error (expr.source_reference, "Too few arguments, errors need at least 1 argument");
			} else {
				Iterator<Expression> arg_it = expr.get_argument_list ().iterator ();
				arg_it.next ();
				var ex = arg_it.get ();
				if (ex.value_type == null || !ex.value_type.compatible (string_type)) {
					expr.error = true;
					Report.error (expr.source_reference, "Invalid type for argument 1");
				}
			}
		}

		foreach (MemberInitializer init in expr.get_object_initializer ()) {
			visit_member_initializer (init, expr.type_reference);
		}
	}

	void visit_member_initializer (MemberInitializer init, DataType type) {
		init.accept (this);

		init.symbol_reference = symbol_lookup_inherited (type.data_type, init.name);
		if (!(init.symbol_reference is Field || init.symbol_reference is Property)) {
			init.error = true;
			Report.error (init.source_reference, "Invalid member `%s' in `%s'".printf (init.name, type.data_type.get_full_name ()));
			return;
		}
		if (init.symbol_reference.access != SymbolAccessibility.PUBLIC) {
			init.error = true;
			Report.error (init.source_reference, "Access to private member `%s' denied".printf (init.symbol_reference.get_full_name ()));
			return;
		}
		DataType member_type;
		if (init.symbol_reference is Field) {
			var f = (Field) init.symbol_reference;
			member_type = f.field_type;
		} else if (init.symbol_reference is Property) {
			var prop = (Property) init.symbol_reference;
			member_type = prop.property_type;
			if (prop.set_accessor == null || !prop.set_accessor.writable) {
				init.error = true;
				Report.error (init.source_reference, "Property `%s' is read-only".printf (prop.get_full_name ()));
				return;
			}
		}
		if (init.initializer.value_type == null || !init.initializer.value_type.compatible (member_type)) {
			init.error = true;
			Report.error (init.source_reference, "Invalid type for member `%s'".printf (init.name));
			return;
		}
	}

	public override void visit_sizeof_expression (SizeofExpression expr) {
		expr.value_type = ulong_type;
	}

	public override void visit_typeof_expression (TypeofExpression expr) {
		expr.value_type = type_type;
	}

	private bool is_numeric_type (DataType type) {
		if (!(type.data_type is Struct)) {
			return false;
		}

		var st = (Struct) type.data_type;
		return st.is_integer_type () || st.is_floating_type ();
	}

	private bool is_integer_type (DataType type) {
		if (!(type.data_type is Struct)) {
			return false;
		}

		var st = (Struct) type.data_type;
		return st.is_integer_type ();
	}

	public override void visit_unary_expression (UnaryExpression expr) {
		if (expr.inner.error) {
			/* if there was an error in the inner expression, skip type check */
			expr.error = true;
			return;
		}

		if (expr.operator == UnaryOperator.PLUS || expr.operator == UnaryOperator.MINUS) {
			// integer or floating point type
			if (!is_numeric_type (expr.inner.value_type)) {
				expr.error = true;
				Report.error (expr.source_reference, "Operator not supported for `%s'".printf (expr.inner.value_type.to_string ()));
				return;
			}

			expr.value_type = expr.inner.value_type;
		} else if (expr.operator == UnaryOperator.LOGICAL_NEGATION) {
			// boolean type
			if (!expr.inner.value_type.compatible (bool_type)) {
				expr.error = true;
				Report.error (expr.source_reference, "Operator not supported for `%s'".printf (expr.inner.value_type.to_string ()));
				return;
			}

			expr.value_type = expr.inner.value_type;
		} else if (expr.operator == UnaryOperator.BITWISE_COMPLEMENT) {
			// integer type
			if (!is_integer_type (expr.inner.value_type)) {
				expr.error = true;
				Report.error (expr.source_reference, "Operator not supported for `%s'".printf (expr.inner.value_type.to_string ()));
				return;
			}

			expr.value_type = expr.inner.value_type;
		} else if (expr.operator == UnaryOperator.INCREMENT ||
		           expr.operator == UnaryOperator.DECREMENT) {
			// integer type
			if (!is_integer_type (expr.inner.value_type)) {
				expr.error = true;
				Report.error (expr.source_reference, "Operator not supported for `%s'".printf (expr.inner.value_type.to_string ()));
				return;
			}

			var ma = find_member_access (expr.inner);
			if (ma == null) {
				expr.error = true;
				Report.error (expr.source_reference, "Prefix operators not supported for this expression");
				return;
			}

			var old_value = new MemberAccess (ma.inner, ma.member_name, expr.inner.source_reference);
			var bin = new BinaryExpression (expr.operator == UnaryOperator.INCREMENT ? BinaryOperator.PLUS : BinaryOperator.MINUS, old_value, new IntegerLiteral ("1"), expr.source_reference);

			var assignment = new Assignment (ma, bin, AssignmentOperator.SIMPLE, expr.source_reference);
			var parenthexp = new ParenthesizedExpression (assignment, expr.source_reference);
			parenthexp.target_type = expr.target_type;
			replaced_nodes.add (expr);
			expr.parent_node.replace_expression (expr, parenthexp);
			parenthexp.accept (this);
			return;
		} else if (expr.operator == UnaryOperator.REF || expr.operator == UnaryOperator.OUT) {
			if (expr.inner.symbol_reference is Field || expr.inner.symbol_reference is FormalParameter || expr.inner.symbol_reference is LocalVariable) {
				// ref and out can only be used with fields, parameters, and local variables
				expr.value_type = expr.inner.value_type;
			} else {
				expr.error = true;
				Report.error (expr.source_reference, "ref and out method arguments can only be used with fields, parameters, and local variables");
				return;
			}
		} else {
			expr.error = true;
			Report.error (expr.source_reference, "internal error: unsupported unary operator");
			return;
		}
	}

	private MemberAccess? find_member_access (Expression expr) {
		if (expr is ParenthesizedExpression) {
			var pe = (ParenthesizedExpression) expr;
			return find_member_access (pe.inner);
		}

		if (expr is MemberAccess) {
			return (MemberAccess) expr;
		}

		return null;
	}

	public override void visit_cast_expression (CastExpression expr) {
		if (expr.inner.error) {
			expr.error = true;
			return;
		}

		// FIXME: check whether cast is allowed

		current_source_file.add_type_dependency (expr.type_reference, SourceFileDependencyType.SOURCE);

		expr.value_type = expr.type_reference;
		expr.value_type.value_owned = expr.inner.value_type.value_owned;

		expr.inner.target_type = expr.inner.value_type.copy ();
	}

	public override void visit_pointer_indirection (PointerIndirection expr) {
		if (expr.inner.error) {
			return;
		}
		if (expr.inner.value_type == null) {
			expr.error = true;
			Report.error (expr.source_reference, "internal error: unknown type of inner expression");
			return;
		}
		if (expr.inner.value_type is PointerType) {
			var pointer_type = (PointerType) expr.inner.value_type;
			if (pointer_type.base_type is ReferenceType) {
				expr.error = true;
				Report.error (expr.source_reference, "Pointer indirection not supported for this expression");
				return;
			}
			expr.value_type = pointer_type.base_type;
		} else {
			expr.error = true;
			Report.error (expr.source_reference, "Pointer indirection not supported for this expression");
			return;
		}
	}

	public override void visit_addressof_expression (AddressofExpression expr) {
		if (expr.inner.error) {
			return;
		}
		if (!(expr.inner.value_type is ValueType
		      || expr.inner.value_type is ObjectType
		      || expr.inner.value_type is PointerType)) {
			expr.error = true;
			Report.error (expr.source_reference, "Address-of operator not supported for this expression");
			return;
		}

		if (expr.inner.value_type.is_reference_type_or_type_parameter ()) {
			expr.value_type = new PointerType (new PointerType (expr.inner.value_type));
		} else {
			expr.value_type = new PointerType (expr.inner.value_type);
		}
	}

	public override void visit_reference_transfer_expression (ReferenceTransferExpression expr) {
		expr.inner.lvalue = true;

		expr.accept_children (this);

		if (expr.inner.error) {
			/* if there was an error in the inner expression, skip type check */
			expr.error = true;
			return;
		}

		if (!(expr.inner is MemberAccess || expr.inner is ElementAccess)) {
			expr.error = true;
			Report.error (expr.source_reference, "Reference transfer not supported for this expression");
			return;
		}

		if (!expr.inner.value_type.is_disposable ()
		    && !(expr.inner.value_type is PointerType)) {
			expr.error = true;
			Report.error (expr.source_reference, "No reference to be transferred");
			return;
		}

		expr.value_type = expr.inner.value_type.copy ();
		expr.value_type.value_owned = true;
	}

	private DataType? get_arithmetic_result_type (DataType left_type, DataType right_type) {
		 if (!(left_type.data_type is Struct) || !(right_type.data_type is Struct)) {
			// at least one operand not struct
		 	return null;
		 }

		 var left = (Struct) left_type.data_type;
		 var right = (Struct) right_type.data_type;

		 if ((!left.is_floating_type () && !left.is_integer_type ()) ||
		     (!right.is_floating_type () && !right.is_integer_type ())) {
			// at least one operand not numeric
		 	return null;
		 }

		 if (left.is_floating_type () == right.is_floating_type ()) {
			// both operands integer or floating type
		 	if (left.get_rank () >= right.get_rank ()) {
		 		return left_type;
		 	} else {
		 		return right_type;
		 	}
		 } else {
			// one integer and one floating type operand
		 	if (left.is_floating_type ()) {
		 		return left_type;
		 	} else {
		 		return right_type;
		 	}
		 }
	}

	public override void visit_binary_expression (BinaryExpression expr) {
		if (expr.left.error || expr.right.error) {
			/* if there were any errors in inner expressions, skip type check */
			expr.error = true;
			return;
		}

		if (expr.left.value_type == null) {
			Report.error (expr.left.source_reference, "invalid left operand");
			expr.error = true;
			return;
		}

		if (expr.operator != BinaryOperator.IN && expr.right.value_type == null) {
			Report.error (expr.right.source_reference, "invalid right operand");
			expr.error = true;
			return;
		}

		if (expr.left.value_type.data_type == string_type.data_type
		    && expr.operator == BinaryOperator.PLUS) {
			if (expr.right.value_type == null || expr.right.value_type.data_type != string_type.data_type) {
				expr.error = true;
				Report.error (expr.source_reference, "Operands must be strings");
				return;
			}

			/* string concatenation: convert to a.concat (b) */

			var concat_call = new InvocationExpression (new MemberAccess (expr.left, "concat"));
			concat_call.add_argument (expr.right);
			concat_call.target_type = expr.target_type;

			replaced_nodes.add (expr);
			expr.parent_node.replace_expression (expr, concat_call);

			concat_call.accept (this);
		} else if (expr.operator == BinaryOperator.PLUS
			   || expr.operator == BinaryOperator.MINUS
			   || expr.operator == BinaryOperator.MUL
			   || expr.operator == BinaryOperator.DIV) {
			// check for pointer arithmetic
			if (expr.left.value_type is PointerType) {
				var offset_type = expr.right.value_type.data_type as Struct;
				if (offset_type != null && offset_type.is_integer_type ()) {
					if (expr.operator == BinaryOperator.PLUS
					    || expr.operator == BinaryOperator.MINUS) {
						// pointer arithmetic: pointer +/- offset
						expr.value_type = expr.left.value_type.copy ();
					}
				} else if (expr.right.value_type is PointerType) {
					// pointer arithmetic: pointer - pointer
					expr.value_type = size_t_type;
				}
			}

			if (expr.value_type == null) {
				expr.value_type = get_arithmetic_result_type (expr.left.value_type, expr.right.value_type);
			}

			if (expr.value_type == null) {
				expr.error = true;
				Report.error (expr.source_reference, "Arithmetic operation not supported for types `%s' and `%s'".printf (expr.left.value_type.to_string (), expr.right.value_type.to_string ()));
				return;
			}
		} else if (expr.operator == BinaryOperator.MOD
			   || expr.operator == BinaryOperator.SHIFT_LEFT
			   || expr.operator == BinaryOperator.SHIFT_RIGHT
			   || expr.operator == BinaryOperator.BITWISE_XOR) {
			expr.value_type = get_arithmetic_result_type (expr.left.value_type, expr.right.value_type);

			if (expr.value_type == null) {
				expr.error = true;
				Report.error (expr.source_reference, "Arithmetic operation not supported for types `%s' and `%s'".printf (expr.left.value_type.to_string (), expr.right.value_type.to_string ()));
				return;
			}
		} else if (expr.operator == BinaryOperator.LESS_THAN
			   || expr.operator == BinaryOperator.GREATER_THAN
			   || expr.operator == BinaryOperator.LESS_THAN_OR_EQUAL
			   || expr.operator == BinaryOperator.GREATER_THAN_OR_EQUAL) {
			if (expr.left.value_type.compatible (string_type)
			    && expr.right.value_type.compatible (string_type)) {
				// string comparison
				} else if (expr.left.value_type is PointerType && expr.right.value_type is PointerType) {
					// pointer arithmetic
			} else {
				var resulting_type = get_arithmetic_result_type (expr.left.value_type, expr.right.value_type);

				if (resulting_type == null) {
					expr.error = true;
					Report.error (expr.source_reference, "Relational operation not supported for types `%s' and `%s'".printf (expr.left.value_type.to_string (), expr.right.value_type.to_string ()));
					return;
				}
			}

			expr.value_type = bool_type;
		} else if (expr.operator == BinaryOperator.EQUALITY
			   || expr.operator == BinaryOperator.INEQUALITY) {
			/* relational operation */

			if (!expr.right.value_type.compatible (expr.left.value_type)
			    && !expr.left.value_type.compatible (expr.right.value_type)) {
				Report.error (expr.source_reference, "Equality operation: `%s' and `%s' are incompatible".printf (expr.right.value_type.to_string (), expr.left.value_type.to_string ()));
				expr.error = true;
				return;
			}

			if (expr.left.value_type.compatible (string_type)
			    && expr.right.value_type.compatible (string_type)) {
				// string comparison
			}

			expr.value_type = bool_type;
		} else if (expr.operator == BinaryOperator.BITWISE_AND
			   || expr.operator == BinaryOperator.BITWISE_OR) {
			// integer type or flags type

			expr.value_type = expr.left.value_type;
		} else if (expr.operator == BinaryOperator.AND
			   || expr.operator == BinaryOperator.OR) {
			if (!expr.left.value_type.compatible (bool_type) || !expr.right.value_type.compatible (bool_type)) {
				expr.error = true;
				Report.error (expr.source_reference, "Operands must be boolean");
			}

			expr.value_type = bool_type;
		} else if (expr.operator == BinaryOperator.IN) {
			// integer type or flags type

			expr.value_type = bool_type;
		} else {
			assert_not_reached ();
		}
	}

	public override void visit_type_check (TypeCheck expr) {
		if (expr.type_reference.data_type == null) {
			/* if type resolving didn't succeed, skip this check */
			expr.error = true;
			return;
		}

		current_source_file.add_type_dependency (expr.type_reference, SourceFileDependencyType.SOURCE);

		expr.value_type = bool_type;
	}

	public override void visit_conditional_expression (ConditionalExpression expr) {
		if (!expr.condition.value_type.compatible (bool_type)) {
			expr.error = true;
			Report.error (expr.condition.source_reference, "Condition must be boolean");
			return;
		}

		/* FIXME: support memory management */
		if (expr.false_expression.value_type.compatible (expr.true_expression.value_type)) {
			expr.value_type = expr.true_expression.value_type.copy ();
		} else if (expr.true_expression.value_type.compatible (expr.false_expression.value_type)) {
			expr.value_type = expr.false_expression.value_type.copy ();
		} else {
			expr.error = true;
			Report.error (expr.condition.source_reference, "Incompatible expressions");
			return;
		}
	}

	private string get_lambda_name () {
		var result = "__lambda%d".printf (next_lambda_id);

		next_lambda_id++;

		return result;
	}

	private Method? find_current_method () {
		var sym = current_symbol;
		while (sym != null) {
			if (sym is Method) {
				return (Method) sym;
			}
			sym = sym.parent_symbol;
		}
		return null;
	}

	private bool is_in_constructor () {
		var sym = current_symbol;
		while (sym != null) {
			if (sym is Constructor) {
				return true;
			}
			sym = sym.parent_symbol;
		}
		return false;
	}

	public override void visit_lambda_expression (LambdaExpression l) {
		if (!(l.target_type is DelegateType)) {
			l.error = true;
			Report.error (l.source_reference, "lambda expression not allowed in this context");
			return;
		}

		bool in_instance_method = false;
		var current_method = find_current_method ();
		if (current_method != null) {
			in_instance_method = (current_method.binding == MemberBinding.INSTANCE);
		} else {
			in_instance_method = is_in_constructor ();
		}

		var cb = (Delegate) ((DelegateType) l.target_type).delegate_symbol;
		l.method = new Method (get_lambda_name (), cb.return_type);
		if (!cb.has_target || !in_instance_method) {
			l.method.binding = MemberBinding.STATIC;
		}
		l.method.owner = current_symbol.scope;

		var lambda_params = l.get_parameters ();
		Iterator<string> lambda_param_it = lambda_params.iterator ();
		foreach (FormalParameter cb_param in cb.get_parameters ()) {
			if (!lambda_param_it.next ()) {
				/* lambda expressions are allowed to have less parameters */
				break;
			}

			string lambda_param = lambda_param_it.get ();

			var param = new FormalParameter (lambda_param, cb_param.parameter_type);

			l.method.add_parameter (param);
		}

		if (lambda_param_it.next ()) {
			/* lambda expressions may not expect more parameters */
			l.error = true;
			Report.error (l.source_reference, "lambda expression: too many parameters");
			return;
		}

		if (l.expression_body != null) {
			var block = new Block ();
			block.scope.parent_scope = l.method.scope;

			if (l.method.return_type.data_type != null) {
				block.add_statement (new ReturnStatement (l.expression_body, l.source_reference));
			} else {
				block.add_statement (new ExpressionStatement (l.expression_body, l.source_reference));
			}

			l.method.body = block;
		} else {
			l.method.body = l.statement_body;
		}
		l.method.body.owner = l.method.scope;

		/* lambda expressions should be usable like MemberAccess of a method */
		l.symbol_reference = l.method;

		l.accept_children (this);
	}

	public override void visit_assignment (Assignment a) {
		a.left.lvalue = true;

		a.left.accept (this);

		if (a.left.error) {
			// skip on error in inner expression
			a.error = true;
			return;
		}

		if (a.left is MemberAccess) {
			var ma = (MemberAccess) a.left;

			if (!(ma.symbol_reference is Signal || ma.symbol_reference is DynamicProperty) && ma.value_type == null) {
				a.error = true;
				Report.error (a.source_reference, "unsupported lvalue in assignment");
				return;
			}
			if (ma.prototype_access) {
				a.error = true;
				Report.error (a.source_reference, "Access to instance member `%s' denied".printf (ma.symbol_reference.get_full_name ()));
				return;
			}

			if (ma.error || ma.symbol_reference == null) {
				a.error = true;
				/* if no symbol found, skip this check */
				return;
			}

			if (ma.symbol_reference is DynamicSignal) {
				// target_type not available for dynamic signals
			} else if (ma.symbol_reference is Signal) {
				var sig = (Signal) ma.symbol_reference;
				a.right.target_type = new DelegateType (sig.get_delegate ());
			} else {
				a.right.target_type = ma.value_type;
			}
		} else if (a.left is ElementAccess) {
			a.right.target_type = a.left.value_type;
		} else if (a.left is PointerIndirection) {
			a.right.target_type = a.left.value_type;
		} else {
			a.error = true;
			Report.error (a.source_reference, "unsupported lvalue in assignment");
			return;
		}

		a.right.accept (this);

		if (a.right.error) {
			// skip on error in inner expression
			a.error = true;
			return;
		}

		if (a.operator != AssignmentOperator.SIMPLE && a.left is MemberAccess) {
			// transform into simple assignment
			// FIXME: only do this if the backend doesn't support
			// the assignment natively

			var ma = (MemberAccess) a.left;

			if (!(ma.symbol_reference is Signal)) {
				var old_value = new MemberAccess (ma.inner, ma.member_name);

				var bin = new BinaryExpression (BinaryOperator.PLUS, old_value, new ParenthesizedExpression (a.right, a.right.source_reference));
				bin.target_type = a.right.target_type;
				a.right.target_type = a.right.target_type.copy ();
				a.right.target_type.value_owned = false;

				if (a.operator == AssignmentOperator.BITWISE_OR) {
					bin.operator = BinaryOperator.BITWISE_OR;
				} else if (a.operator == AssignmentOperator.BITWISE_AND) {
					bin.operator = BinaryOperator.BITWISE_AND;
				} else if (a.operator == AssignmentOperator.BITWISE_XOR) {
					bin.operator = BinaryOperator.BITWISE_XOR;
				} else if (a.operator == AssignmentOperator.ADD) {
					bin.operator = BinaryOperator.PLUS;
				} else if (a.operator == AssignmentOperator.SUB) {
					bin.operator = BinaryOperator.MINUS;
				} else if (a.operator == AssignmentOperator.MUL) {
					bin.operator = BinaryOperator.MUL;
				} else if (a.operator == AssignmentOperator.DIV) {
					bin.operator = BinaryOperator.DIV;
				} else if (a.operator == AssignmentOperator.PERCENT) {
					bin.operator = BinaryOperator.MOD;
				} else if (a.operator == AssignmentOperator.SHIFT_LEFT) {
					bin.operator = BinaryOperator.SHIFT_LEFT;
				} else if (a.operator == AssignmentOperator.SHIFT_RIGHT) {
					bin.operator = BinaryOperator.SHIFT_RIGHT;
				}

				a.right = bin;
				a.right.accept (this);

				a.operator = AssignmentOperator.SIMPLE;
			}
		}

		if (a.left is MemberAccess) {
			var ma = (MemberAccess) a.left;

			if (ma.symbol_reference is Signal) {
				var sig = (Signal) ma.symbol_reference;

				if (a.right.symbol_reference == null) {
					a.error = true;
					Report.error (a.right.source_reference, "unsupported expression for signal handler");
					return;
				}

				var dynamic_sig = sig as DynamicSignal;
				if (dynamic_sig != null) {
					bool first = true;
					foreach (FormalParameter param in dynamic_sig.handler.value_type.get_parameters ()) {
						if (first) {
							// skip sender parameter
							first = false;
						} else {
							dynamic_sig.add_parameter (param);
						}
					}
					a.right.target_type = new DelegateType (sig.get_delegate ());
				}
			} else if (ma.symbol_reference is Property) {
				var prop = (Property) ma.symbol_reference;

				var dynamic_prop = prop as DynamicProperty;
				if (dynamic_prop != null) {
					dynamic_prop.property_type = a.right.value_type.copy ();
					a.left.value_type = dynamic_prop.property_type.copy ();
				}

				if (prop.set_accessor == null
				    || (!prop.set_accessor.writable && !(find_current_method () is CreationMethod || is_in_constructor ()))) {
					ma.error = true;
					Report.error (ma.source_reference, "Property `%s' is read-only".printf (prop.get_full_name ()));
					return;
				}
			} else if (ma.symbol_reference is LocalVariable && a.right.value_type == null) {
				var local = (LocalVariable) ma.symbol_reference;

				if (a.right.symbol_reference is Method &&
				    local.variable_type is DelegateType) {
					var m = (Method) a.right.symbol_reference;
					var dt = (DelegateType) local.variable_type;
					var cb = dt.delegate_symbol;

					/* check whether method matches callback type */
					if (!cb.matches_method (m)) {
						a.error = true;
						Report.error (a.source_reference, "declaration of method `%s' doesn't match declaration of callback `%s'".printf (m.get_full_name (), cb.get_full_name ()));
						return;
					}

					a.right.value_type = local.variable_type;
				} else {
					a.error = true;
					Report.error (a.source_reference, "Assignment: Invalid callback assignment attempt");
					return;
				}
			} else if (ma.symbol_reference is Field && a.right.value_type == null) {
				var f = (Field) ma.symbol_reference;

				if (a.right.symbol_reference is Method &&
				    f.field_type is DelegateType) {
					var m = (Method) a.right.symbol_reference;
					var dt = (DelegateType) f.field_type;
					var cb = dt.delegate_symbol;

					/* check whether method matches callback type */
					if (!cb.matches_method (m)) {
						f.error = true;
						Report.error (a.source_reference, "declaration of method `%s' doesn't match declaration of callback `%s'".printf (m.get_full_name (), cb.get_full_name ()));
						return;
					}

					a.right.value_type = f.field_type;
				} else {
					a.error = true;
					Report.error (a.source_reference, "Assignment: Invalid callback assignment attempt");
					return;
				}
			} else if (a.left.value_type != null && a.right.value_type != null) {
				/* if there was an error on either side,
				 * i.e. a.{left|right}.value_type == null, skip type check */

				if (!a.right.value_type.compatible (a.left.value_type)) {
					a.error = true;
					Report.error (a.source_reference, "Assignment: Cannot convert from `%s' to `%s'".printf (a.right.value_type.to_string (), a.left.value_type.to_string ()));
					return;
				}

				if (a.right.value_type.is_disposable ()) {
					/* rhs transfers ownership of the expression */
					if (!(a.left.value_type is PointerType) && !a.left.value_type.value_owned) {
						/* lhs doesn't own the value */
						a.error = true;
						Report.error (a.source_reference, "Invalid assignment from owned expression to unowned variable");
					}
				} else if (a.left.value_type.value_owned) {
					/* lhs wants to own the value
					 * rhs doesn't transfer the ownership
					 * code generator needs to add reference
					 * increment calls */
				}
			}
		} else if (a.left is ElementAccess) {
			var ea = (ElementAccess) a.left;

			if (!a.right.value_type.compatible (a.left.value_type)) {
				a.error = true;
				Report.error (a.source_reference, "Assignment: Cannot convert from `%s' to `%s'".printf (a.right.value_type.to_string (), a.left.value_type.to_string ()));
				return;
			}

			if (a.right.value_type.is_disposable ()) {
				/* rhs transfers ownership of the expression */

				DataType element_type;

				if (ea.container.value_type is ArrayType) {
					var array_type = (ArrayType) ea.container.value_type;
					element_type = array_type.element_type;
				} else {
					var args = ea.container.value_type.get_type_arguments ();
					assert (args.size == 1);
					element_type = args.get (0);
				}

				if (!(element_type is PointerType) && !element_type.value_owned) {
					/* lhs doesn't own the value */
					a.error = true;
					Report.error (a.source_reference, "Invalid assignment from owned expression to unowned variable");
					return;
				}
			} else if (a.left.value_type.value_owned) {
				/* lhs wants to own the value
				 * rhs doesn't transfer the ownership
				 * code generator needs to add reference
				 * increment calls */
			}
		} else {
			return;
		}

		if (a.left.value_type != null) {
			a.value_type = a.left.value_type.copy ();
			a.value_type.value_owned = false;
		} else {
			a.value_type = null;
		}

		a.add_error_types (a.left.get_error_types ());
		a.add_error_types (a.right.get_error_types ());
	}
}