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

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.wst.jsdt.core.ast.ASTVisitor;
import org.eclipse.wst.jsdt.core.ast.IASTNode;
import org.eclipse.wst.jsdt.core.ast.IAbstractFunctionDeclaration;
import org.eclipse.wst.jsdt.core.ast.IAbstractVariableDeclaration;
import org.eclipse.wst.jsdt.core.ast.IAllocationExpression;
import org.eclipse.wst.jsdt.core.ast.IArgument;
import org.eclipse.wst.jsdt.core.ast.IAssignment;
import org.eclipse.wst.jsdt.core.ast.IBinaryExpression;
import org.eclipse.wst.jsdt.core.ast.IExpression;
import org.eclipse.wst.jsdt.core.ast.IFalseLiteral;
import org.eclipse.wst.jsdt.core.ast.IFieldReference;
import org.eclipse.wst.jsdt.core.ast.IFunctionCall;
import org.eclipse.wst.jsdt.core.ast.IFunctionDeclaration;
import org.eclipse.wst.jsdt.core.ast.IFunctionExpression;
import org.eclipse.wst.jsdt.core.ast.IJsDoc;
import org.eclipse.wst.jsdt.core.ast.ILocalDeclaration;
import org.eclipse.wst.jsdt.core.ast.INumberLiteral;
import org.eclipse.wst.jsdt.core.ast.IObjectLiteral;
import org.eclipse.wst.jsdt.core.ast.IObjectLiteralField;
import org.eclipse.wst.jsdt.core.ast.IProgramElement;
import org.eclipse.wst.jsdt.core.ast.IReference;
import org.eclipse.wst.jsdt.core.ast.IReturnStatement;
import org.eclipse.wst.jsdt.core.ast.IScriptFileDeclaration;
import org.eclipse.wst.jsdt.core.ast.ISingleNameReference;
import org.eclipse.wst.jsdt.core.ast.IStringLiteral;
import org.eclipse.wst.jsdt.core.ast.IThisReference;
import org.eclipse.wst.jsdt.core.ast.ITrueLiteral;
import org.eclipse.wst.jsdt.core.compiler.CharOperation;
import org.eclipse.wst.jsdt.internal.compiler.ast.ASTNode;
import org.eclipse.wst.jsdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.wst.jsdt.internal.compiler.ast.AllocationExpression;
import org.eclipse.wst.jsdt.internal.compiler.ast.ArrayInitializer;
import org.eclipse.wst.jsdt.internal.compiler.ast.ArrayReference;
import org.eclipse.wst.jsdt.internal.compiler.ast.Assignment;
import org.eclipse.wst.jsdt.internal.compiler.ast.BinaryExpression;
import org.eclipse.wst.jsdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.wst.jsdt.internal.compiler.ast.Expression;
import org.eclipse.wst.jsdt.internal.compiler.ast.FieldReference;
import org.eclipse.wst.jsdt.internal.compiler.ast.FunctionExpression;
import org.eclipse.wst.jsdt.internal.compiler.ast.Javadoc;
import org.eclipse.wst.jsdt.internal.compiler.ast.JavadocSingleNameReference;
import org.eclipse.wst.jsdt.internal.compiler.ast.LocalDeclaration;
import org.eclipse.wst.jsdt.internal.compiler.ast.MessageSend;
import org.eclipse.wst.jsdt.internal.compiler.ast.MethodDeclaration;
import org.eclipse.wst.jsdt.internal.compiler.ast.ObjectLiteral;
import org.eclipse.wst.jsdt.internal.compiler.ast.OperatorIds;
import org.eclipse.wst.jsdt.internal.compiler.ast.Reference;
import org.eclipse.wst.jsdt.internal.compiler.ast.SingleNameReference;
import org.eclipse.wst.jsdt.internal.compiler.ast.StringLiteral;
import org.eclipse.wst.jsdt.internal.compiler.ast.ThisReference;
import org.eclipse.wst.jsdt.internal.compiler.ast.UnaryExpression;
import org.eclipse.wst.jsdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.wst.jsdt.internal.compiler.util.HashtableOfObject;
import org.eclipse.wst.jsdt.internal.compiler.util.Util;
import org.eclipse.wst.jsdt.internal.core.search.indexing.IIndexConstants;

/**
 * The default inference engine.
 * 
 * <p>
 * Clients may subclass this class but should expect some breakage by future releases.
 * </p>
 * 
 * Provisional API: This class/interface is part of an interim API that is still under development
 * and expected to
 * change significantly before reaching stability. It is being made available at this early stage to
 * solicit feedback
 * from pioneering adopters on the understanding that any code that uses this API will almost
 * certainly be broken
 * (repeatedly) as the API evolves.
 */
public class InferEngine extends ASTVisitor implements IInferEngine {

	/**
	 * <p>
	 * String type that is initialized on first use and added to the compilation unit.
	 * </p>
	 */
	private InferredType fStringType;
	
	/**
	 * <p>
	 * Number type that is initialized on first use and added to the compilation unit.
	 * </p>
	 */
	private InferredType fNumberType;
	
	/**
	 * <p>
	 * Boolean type that is initialized on first use and added to the compilation unit.
	 * </p>
	 */
	private InferredType fBooleanType;
	
	/**
	 * <p>
	 * Function type that is initialized on first use and added to the compilation unit.
	 * </p>
	 */
	private InferredType fFunctionType;
	
	/**
	 * <p>
	 * Array type that is initialized on first use and added to the compilation unit.
	 * </p>
	 */
	private InferredType fArrayType;
	
	/**
	 * <p>
	 * Void type that is initialized on first use and added to the compilation unit.
	 * </p>
	 */
	private InferredType fVoidType;
	
	/**
	 * <p>
	 * Object type that is initialized on first use and added to the compilation unit.
	 * </p>
	 */
	private InferredType fObjectType;
	
	InferOptions inferOptions;
	CompilationUnitDeclaration compUnit;
	Context[] contexts = new Context[100];
	int contextPtr = -1;
	Context currentContext = new Context();
	protected int passNumber = 1;

	boolean isTopLevelAnonymousFunction;
	int anonymousCount = 0;
	
	public static boolean DEBUG = false;

	public InferrenceProvider inferenceProvider;

	/**
	 * @deprecated use {@link #getStringType()}
	 */
	public InferredType StringType = new InferredType(TypeConstants.JAVA_LANG_STRING[0]);
	
	/**
	 * @deprecated use {@link #getNumberType()}
	 */
	public InferredType NumberType = new InferredType(TypeConstants.NUMBER[0]);
	
	/**
	 * @deprecated use {@link #getBooleanType()}
	 */
	public InferredType BooleanType = new InferredType(TypeConstants.BOOLEAN_OBJECT[0]);
	
	/**
	 * @deprecated use {@link #getFunctionType()}
	 */
	public InferredType FunctionType = new InferredType(TypeConstants.FUNCTION[0]);
	
	/**
	 * @deprecated use {@link #getArrayType()}
	 */
	public InferredType ArrayType = new InferredType(TypeConstants.ARRAY[0]);
	
	/**
	 * @deprecated use {@link #getVoidType()}
	 */
	public InferredType VoidType = new InferredType(TypeConstants.VOID);
	
	/**
	 * @deprecated use {@link #getObjectType()}
	 */
	public InferredType ObjectType = new InferredType(TypeConstants.OBJECT);

	/**
	 * @deprecated - no longer used
	 */
	public InferredType GlobalType = new InferredType(InferredType.GLOBAL_NAME);

	public static HashtableOfObject WellKnownTypes = new HashtableOfObject();
	{
		WellKnownTypes.put(TypeConstants.OBJECT, null);
		WellKnownTypes.put(TypeConstants.ARRAY[0], null);
		WellKnownTypes.put(TypeConstants.JAVA_LANG_STRING[0], null);
		WellKnownTypes.put(TypeConstants.NUMBER[0], null);
		WellKnownTypes.put(TypeConstants.BOOLEAN_OBJECT[0], null);
		WellKnownTypes.put(TypeConstants.FUNCTION[0], null);
		WellKnownTypes.put(new char[] { 'D', 'a', 't', 'e' }, null);
		WellKnownTypes.put(new char[] { 'M', 'a', 't', 'h' }, null);
		WellKnownTypes.put(new char[] { 'R', 'e', 'g', 'E', 'x', 'p' }, null);
		WellKnownTypes.put(new char[] { 'E', 'r', 'r', 'o', 'r' }, null);
	}

	protected InferredType inferredGlobal = null;

	/**
	 * @deprecated Will be removed
	 */
	static final char[] CONSTRUCTOR_ID = { 'c', 'o', 'n', 's', 't', 'r', 'u', 'c', 't', 'o', 'r' };
	
	/**
	 * <p>
	 * Use to keep track of the current context of the infer engine.
	 * </p>
	 */
	static class Context {
		InferredType currentType;
		IFunctionDeclaration currentMethod;

		/** The current assignment. */
		IAssignment currentAssignment;

		/** the current declaration */
		ILocalDeclaration currentLocalDeclaration;
		
		/** The current return */
		IReturnStatement currentReturn;
		
		boolean isJsDocClass;

		private HashtableOfObject definedMembers;

		/* Parent context to provide chaining when searching
		 * for members in scope. */
		private Context parent = null;

		/* Root context */
		Context() {
		}

		/* Nested context */
		Context(Context parent) {
			this.parent = parent;

			currentType = parent.currentType;
			currentMethod = parent.currentMethod;
			this.currentAssignment = parent.currentAssignment;
			this.currentLocalDeclaration = parent.currentLocalDeclaration;
			this.currentReturn = parent.currentReturn;
			this.isJsDocClass = parent.isJsDocClass;
		}

		public Object getMember(char[] key) {
			Object value = null;
			if(definedMembers != null) {
				value = definedMembers.get(key);
			}

			// chain lookup
			if(value == null && parent != null) {
				value = parent.getMember(key);
			}

			return value;
		}

		public void addMember(char[] key, Object member) {
			if(key == null)
				return;

			if(definedMembers == null) {
				definedMembers = new HashtableOfObject();
			}

			definedMembers.put(key, member);
		}

		public void setCurrentType(InferredType type) {
			this.currentType = type;
			Context parentContext = this.parent;

			while(parentContext != null && parentContext.currentMethod == this.currentMethod) {
				parentContext.currentType = type;
				parentContext = parentContext.parent;
			}
		}
	}

	private static boolean REPORT_INFER_TIME = false;

	/**
	 * <p>
	 * Constructor that uses default {@link InferOptions}.
	 * </p>
	 */
	public InferEngine() {
		this(new InferOptions());
	}

	/**
	 * <p>
	 * Constructor using given {@link InferOptions}.
	 * </p>
	 * 
	 * @param inferOptions
	 *            to create this infer engine with
	 */
	public InferEngine(InferOptions inferOptions) {
		this.inferOptions = inferOptions;
	}

	public void initialize() {
		this.contextPtr = -1;
		this.currentContext = new Context();
		this.passNumber = 1;
		this.isTopLevelAnonymousFunction = false;
		this.anonymousCount = 0;
		this.inferredGlobal = null;
	}

	public void setCompilationUnit(CompilationUnitDeclaration scriptFileDeclaration) {
		this.compUnit = scriptFileDeclaration;
		buildDefinedMembers(scriptFileDeclaration.getStatements(), null);
	}

	public boolean visit(IFunctionCall functionCall) {
		boolean visitChildren = handleFunctionCall(functionCall);
		if(visitChildren) {
			if(functionCall.getReceiver() instanceof FunctionExpression) {
				if (this.contextPtr == -1) {
					this.isTopLevelAnonymousFunction = true;
				}
				if (functionCall instanceof MessageSend && ((MessageSend) functionCall).getArguments() != null) {
					MethodDeclaration methodDeclaration = ((FunctionExpression) functionCall.getReceiver()).getMethodDeclaration();
					if (methodDeclaration != null && methodDeclaration.getArguments() != null) {
						IArgument[] declaredArguments = methodDeclaration.getArguments();
						IExpression[] sentArguments = ((MessageSend) functionCall).getArguments();
						for (int i = 0; i < declaredArguments.length; i++) {
							if (i >= sentArguments.length) {
								continue;
							}
							handleFunctionDeclarationArgument(declaredArguments[i], sentArguments[i]);
						}
					}
				}
			}
		}
		return visitChildren;
	}

	protected void handleFunctionDeclarationArgument(IArgument declaredArgument, IExpression sentArgument) {
		// set the declared argument's type to be the matching parameter type
		if (!declaredArgument.isType() && declaredArgument.getInferredType() == null) {
			if (sentArgument instanceof SingleNameReference) {
				InferredType inferredType = getInferredType(sentArgument);
				if (inferredType == null) {
					char[] parameterName = getName(sentArgument);
					if (parameterName!= null && isGlobal(parameterName)) {
						inferredType = createAnonymousGlobalType(parameterName);
					}	
				}
				declaredArgument.setInferredType(inferredType);
			}
			else if (sentArgument instanceof ThisReference) {
				//check if "this" refers to the global object
				if (this.isTopLevelAnonymousFunction) {
					char[] parameterName = declaredArgument.getName();
					if (parameterName != null) {
						InferredType inferredType = createAnonymousGlobalType(parameterName);
						declaredArgument.setInferredType(inferredType);
					}	
				} 
			}
		}
	}

	public boolean visit(ILocalDeclaration localDeclaration) {
		// add as a member of the current context
		currentContext.addMember(localDeclaration.getName(), localDeclaration);

		// create a new context for the local declaration
		pushContext();
		this.currentContext.currentLocalDeclaration = localDeclaration;
		
		if(this.passNumber == 1 && localDeclaration instanceof LocalDeclaration && this.currentContext.currentMethod == null) {
			((LocalDeclaration)localDeclaration).setIsLocal(false);
		}

		if(localDeclaration.getJsDoc() != null) {
			Javadoc javadoc = (Javadoc) localDeclaration.getJsDoc();
			createTypeIfNecessary(javadoc);
			InferredAttribute attribute = null;
			if(javadoc.memberOf != null) {
				InferredType type = this.addType(javadoc.memberOf.getFullTypeName(), true);
				int nameStart = localDeclaration.sourceStart();
				attribute = type.addAttribute(localDeclaration.getName(), localDeclaration, nameStart);
				handleAttributeDeclaration(attribute, localDeclaration.getInitialization());
				if(localDeclaration.getInitialization() != null) {
					attribute.initializationStart = localDeclaration.getInitialization().sourceStart();
					attribute.type = getTypeOf(localDeclaration.getInitialization());
				}
				attribute.inType = type;
			}

			if(javadoc.returnType != null) {
				InferredType type = this.addType(changePrimitiveToObject(javadoc.returnType.getFullTypeName()));
				localDeclaration.setInferredType(type);
				if(attribute != null)
					attribute.type = type;
			}
		}

		// visit the function in case it defines a type
		if(localDeclaration.getInitialization() instanceof IFunctionExpression) {
			boolean keepVisiting = handleFunctionExpressionLocalDeclaration(localDeclaration);
			if(!keepVisiting) {
				return false;
			}
		} 
		
		//if initialization set, attempt to set the inferred type
		if(localDeclaration.getInitialization() != null) {
			if(localDeclaration.getInitialization() instanceof MessageSend) {
				handleFunctionCall((IFunctionCall) localDeclaration.getInitialization(),
						(LocalDeclaration) localDeclaration);
				if(((MessageSend)localDeclaration.getInitialization()).receiver instanceof IFunctionExpression && this.passNumber == 2) {
					if(((FunctionExpression)((MessageSend)localDeclaration.getInitialization()).receiver).methodDeclaration != null) {
						localDeclaration.setInferredType(((FunctionExpression)((MessageSend)localDeclaration.getInitialization()).receiver).methodDeclaration.inferredType);
					}
				}
			} else {
				if (this.isExpressionAType(localDeclaration.getInitialization())) {
					localDeclaration.setIsType(true);
				}
				InferredType type = this.getTypeForVariableInitialization(localDeclaration.getName(), localDeclaration.getInitialization());
				if (localDeclaration.getInferredType() == null || (type != null && type.isAnonymous))
					localDeclaration.setInferredType(type);
			}
		}
		
		return true;
	}

	/**
	 * @see org.eclipse.wst.jsdt.core.ast.ASTVisitor#endVisit(org.eclipse.wst.jsdt.core.ast.ILocalDeclaration)
	 */
	public void endVisit(ILocalDeclaration localDeclaration) {
		popContext();
	}

	private void createTypeIfNecessary(Javadoc javadoc) {
		if(javadoc.memberOf != null) {
			char[][] namespace = {};
			char[][] typeName = javadoc.memberOf.getTypeName();
			if(javadoc.namespace != null) {
				namespace = javadoc.namespace.getTypeName();
			}
			char[] name =
					CharOperation.concat(CharOperation.concatWith(namespace, '.'), CharOperation.concatWith(typeName,
							'.'), '.');
			this.currentContext.currentType = addType(name);
			if(javadoc.extendsType != null) {
				char[] superName = CharOperation.concatWith(javadoc.extendsType.getTypeName(), '.');
				this.currentContext.currentType.setSuperType(addType(superName));
			}
			this.currentContext.isJsDocClass = true;

		}

	}

	public boolean visit(IAssignment assignment) {
		//if assigning to single name add assignment to context if there is not an existing var declaration
		IAbstractVariableDeclaration existingVarDecl = null;
		IAssignment existingAssignmentDecl = null;
		if(assignment.getLeftHandSide() instanceof ISingleNameReference) {
			existingVarDecl = this.getVariable(assignment.getLeftHandSide());
			if(existingVarDecl == null) {
				existingAssignmentDecl = this.getAssignment(assignment.getLeftHandSide());
				if(existingAssignmentDecl == null) {
					currentContext.addMember(this.getName(assignment.getLeftHandSide()), assignment);
				}
			}
		}
		
		pushContext();
		this.currentContext.currentAssignment = assignment;
		
		//set the function that contains this assignment
		if(this.passNumber == 1 && assignment instanceof Assignment && this.currentContext.currentMethod != null) {
			((Assignment)assignment).setContainingFunction(this.currentContext.currentMethod);
		}
		
		IExpression assignmentExpression = assignment.getExpression();
		if(handlePotentialType(assignment)) {

		} else if(assignmentExpression instanceof FunctionExpression) {
			boolean keepVisiting = handleFunctionExpressionAssignment(assignment);
			
			//set the type on the existing var declaration if it does not already have one set
			if(assignment.getInferredType() != null && existingVarDecl != null && existingVarDecl.getInferredType() == null) {
				existingVarDecl.setInferredType(assignment.getInferredType());
			}
			
			if(!keepVisiting) {
				return false;
			}
		} else if(assignmentExpression instanceof SingleNameReference && this.currentContext.currentType != null
				&& isThis(assignment.getLeftHandSide())) {
			
			ISingleNameReference snr = (ISingleNameReference) assignmentExpression;
			Object object = this.currentContext.getMember(snr.getToken());

			IFieldReference fieldReference = (IFieldReference) assignment.getLeftHandSide();
			char[] memberName = fieldReference.getToken();
			InferredMember member = null;

			int nameStart = fieldReference.sourceEnd() - memberName.length + 1;

			/* this.foo = bar //bar is a function */
			if(object instanceof MethodDeclaration) {

				MethodDeclaration method = (MethodDeclaration) object;
				member = this.currentContext.currentType.addMethod(memberName, method, nameStart);

			}
			/* this.foo = bar //assume that bar is not a function and create a new attribute in the
			 * current type */
			else {
				member = this.currentContext.currentType.addAttribute(memberName, assignment, nameStart);
				handleAttributeDeclaration((InferredAttribute) member, assignment.getExpression());
				if(((InferredAttribute) member).type == null)
					((InferredAttribute) member).type = getTypeOf(assignmentExpression);
			}

			// setting location
			if(member != null) {
				// this is a not static member because it is being set on the this
				member.isStatic = false;
			}
		}

		// foo = ??;
		else if(assignment.getLeftHandSide() instanceof ISingleNameReference) {
			char[] variableName = this.getName(assignment);
			
			/* if there is an existing variable declaration
			 * else there is not */
			InferredType existingType = null;
			
			if(existingVarDecl != null) {
				existingType = existingVarDecl.getInferredType();
			} else if(existingAssignmentDecl != null) {
				existingType = existingAssignmentDecl.getInferredType();
			}
			
			/* if existing variable declaration does not already have an inferred type
			 * 
			 * else it does and the new assignment type is different then the currently
			 * assigned type */
			InferredType type = null;
			if(existingType == null) {
				type = this.getTypeForVariableInitialization(variableName, assignmentExpression);
				
				if(this.isExpressionAType(assignmentExpression)) {
					assignment.setIsType(true);
				}
			} else {
				InferredType newAssignmentType = this.getTypeOf(assignmentExpression);
				if(newAssignmentType != null && existingType != newAssignmentType) {
					/* if the existing type is not anonymous, not global and
					 * existing var is not an argument then create a new
					 * global anonymous type to mix everything together in
					 * 
					 * else just use the existing type */
					InferredType newCombinedType = null;
					if(!existingType.isAnonymous && !existingType.isGlobal() && !(existingVarDecl instanceof IArgument)) {
						if(existingVarDecl instanceof LocalDeclaration && ((LocalDeclaration)existingVarDecl).isLocal()) {
							newCombinedType = this.createAnonymousType(existingVarDecl, null);
						} else {
							newCombinedType = this.createAnonymousGlobalType(variableName);
						}
						newCombinedType.setIsDefinition(true);
						
						/* add the original type of the variable declaration to the new combined type
						 * 
						 * if the existing type is indexed then add it later
						 * else it is not must add it now */
						if(existingType.isIndexed()) {
							newCombinedType.addMixin(existingType.getName());
						} else {
							newCombinedType.mixin(existingType);
						}
					} else {
						newCombinedType = existingType;
					}
	
					/* add the type of the new assignment to the combined type
					 * 
					 * if the existing type is indexed then add it later
					 * else it is not must add it now */
					if(newAssignmentType.isIndexed()) {
						newCombinedType.addMixin(newAssignmentType.getName());
					} else {
						newCombinedType.mixin(newAssignmentType);
					}
					
					type = newCombinedType;
				}
			}
			
			/* use the new type to set the inferred type on this assignment and
			 * any existing variable declaration or assignment */
			if(type != null) {
				assignment.setInferredType(type);
				
				if(existingVarDecl != null) {
					existingVarDecl.setInferredType(type);
				}
				
				if(existingAssignmentDecl != null) {
					existingAssignmentDecl.setInferredType(type);
				}
			}

			return true;
		}
		else if(assignmentExpression instanceof AllocationExpression
				&& ((AllocationExpression) assignmentExpression).member instanceof FunctionExpression) {

			handleFunctionExpressionAssignment(assignment);
		} else if(assignmentExpression instanceof Assignment
				&& ((Assignment) assignmentExpression).expression instanceof FunctionExpression) {

			handleFunctionExpressionAssignment(assignment);
		}
		else if(this.inferOptions.useAssignments) {
			IExpression lhs = assignment.getLeftHandSide();
			
			// if foo.bar = ? where ? is not {} and not a function
			if(lhs instanceof FieldReference || lhs instanceof ArrayReference) {

				Reference lhsRef = (Reference) lhs;
				Expression receiver = null;
				char[] attName = null;
				int nameStart = 0;
				if(lhsRef instanceof FieldReference) {
					receiver = ((FieldReference) lhsRef).receiver;
					attName = ((FieldReference) lhsRef).token;
					nameStart = (int) (((FieldReference) lhsRef).nameSourcePosition >>> 32);
				} else if(lhsRef instanceof ArrayReference) {
					if(((ArrayReference) lhsRef).position instanceof StringLiteral) {
						receiver = ((ArrayReference) lhsRef).receiver;
						attName = ((StringLiteral) ((ArrayReference) lhsRef).position).source();
						nameStart = ((StringLiteral) ((ArrayReference) lhsRef).position).sourceStart + 1;
					}
				}

				//attempt to get receiver type
				InferredType receiverType = this.getInferredType(receiver);
				
				//if not found type yet check if receiver is function
				if(receiverType == null) {
					IFunctionDeclaration function = getDefinedFunction(receiver);
					if(function != null) {
						char[] typeName = constructTypeName(receiver);
						if(typeName != null) {
							receiverType = addType(typeName);
						}
					}
				}
				
				//all else fails on pass two will possibly create a receiver type
				if(receiverType == null && this.passNumber == 2) {
					receiverType = this.getReceiverType(receiver, true);
				}

				if(receiver != null && receiverType != null && attName != null && attName.length > 0) {
					/* if the receiver type is not anonymous and is the expression is not a type then
					 * create a new anonymous sub type to do the assignment to */
					if(!receiverType.isAnonymous && !this.isExpressionAType(receiver)) {
						receiverType = this.createTypeToAssignTo(receiver, receiverType);
						this.setTypeOf(receiver, receiverType);
					}
					
					// in the case where the supertype of the reciever is a function, make sure we store the actual function
					// for later use
					if(receiverType != null && receiverType.getSuperType() != null && receiverType.getSuperType().isFunction()) {
						IAbstractVariableDeclaration varDecl = this.getVariable(receiver);
						if(varDecl != null) {
							IExpression expression = varDecl.getInitialization();
							if(expression != null && expression instanceof IFunctionExpression) {
								receiverType.setCorrespondingFunction(((IFunctionExpression)expression).getMethodDeclaration());
							}
						}
					}
					
					/* if receiver is instance of type so create new type to assign to
					 * 
					 * else if receiver is a type then will just assign directly to it,
					 * and if not anonymous then its a static assignment that type statically */
					boolean isStatic = false;
					if(!this.isExpressionAType(receiver)) {
						receiverType = this.createTypeToAssignTo(receiver, receiverType);
					} else if(!receiverType.isAnonymous && !receiver.isThis()) {
						isStatic = true;
					}

					// check if there is an attribute or function already created
					InferredMethod method = null;
					InferredAttribute attr = receiverType.findAttribute(attName);
					if(attr == null) {
						method = receiverType.findMethod(attName, null);
					} else if (this.passNumber == 2) {
						handleAttributeDeclaration(attr);
					}

					// ignore if the attribute exists and has a type
					if((method == null && attr == null) || (method == null && attr != null && attr.type == null)) {
						//if type already set on assignment, use that
						InferredType rhsType = assignment.getInferredType();
						
						// If the RHS is a type then create a type on the LHS
						boolean isType = false;
						if(rhsType != null && assignment.getInferredType() != null && assignment.isType() &&
									this.isExpressionAType(receiver) && !receiverType.isAnonymous) {
							
							isType = true;
							
							//create new type name
							char[] newTypeName = receiverType.getName();
							newTypeName = CharOperation.concat(newTypeName, attName, '.');
							
							/* if the RHS type is anonymous just rename it to use the LHS name
							 * else create new type on the LHS and add it as a synonym of the RHS type */
							if(rhsType.isAnonymous && !rhsType.isGlobal()) {
								this.convertAnonymousTypeToNamed(rhsType, newTypeName);
								rhsType.setIsDefinition(true);
								rhsType.setNameStart(assignment.sourceStart());
							}
							else if(!CharOperation.equals(newTypeName, rhsType.getName())){
								InferredType newType = this.addType(newTypeName, true);
								rhsType.addSynonym(newType);
							}
						}
						
						//if type not found type yet, check if RHS refers to a function definition
						IFunctionDeclaration definedFunction = null;
						if(rhsType == null) {
							definedFunction = this.getDefinedFunction(assignmentExpression);
						}
						
						/* if RHS is a function, add a function to the receiver type
						 * else add new attribute to receiver type */
						if(definedFunction != null) {
							method = receiverType.addMethod(attName, definedFunction, nameStart);
							receiverType.setIsDefinition(true);
							method.isStatic = isStatic;
						} else {
							//create the attribute
							int nameStart_ = nameStart;
							attr = receiverType.addAttribute(attName, assignment, nameStart_);
							receiverType.setIsDefinition(true);
							this.handleAttributeDeclaration(attr, assignmentExpression);
							
							//if still not RHS type then get one
							if(rhsType == null) {
								/* if LHS is global "this"
								 * else just an attribute on a type */
								if(receiver instanceof IThisReference &&
											this.currentContext.currentType == this.getInferredGlobal(false)) {
									
									rhsType = this.getTypeForVariableInitialization(attName, assignmentExpression);
								} else {
									rhsType = this.getTypeOf(assignmentExpression);
									if(receiverType != null && rhsType != null && receiverType.isGlobal() && rhsType.isAnonymous) {
										char[] globalAttName = createAnonymousGlobalTypeName(attName);
										convertAnonymousTypeToNamed(rhsType, globalAttName);
									}
									// if the attribute is also a function, add the function using the corresponding function stored earlier
									if(rhsType != null && rhsType.getSuperType() != null && rhsType.getSuperType().isFunction() && rhsType.getCorrespondingFunction() != null) {
										method = receiverType.addMethod(attName, rhsType.getCorrespondingFunction(), nameStart);
										method.isStatic = isStatic;
									}
								}
							}
							
							//determine if static
							char[] possibleTypeName = constructTypeName(receiver);
							attr.isStatic = isStatic || (possibleTypeName != null && compUnit.findInferredType(possibleTypeName) != null);
							
							//assign the type to the attribute
							if(attr.type == null || rhsType != null) {
								attr.type = rhsType;
								attr.setIsType(isType);
							}
							
							//if determined RHS type set it as the inferred type for the assignment 
							if(rhsType != null) {
								assignment.setInferredType(rhsType);
							}
						}
					} else if(method == null && attr != null && attr.type != null && attr.type.getSuperType() != null && attr.type.getSuperType().isFunction() && attr.type.getCorrespondingFunction() != null) {
						// if the attribute is also a function, add the function using the cooresponding function stored earlier
						method = receiverType.addMethod(attName, attr.type.getCorrespondingFunction(), nameStart);
						receiverType.setIsDefinition(true);
						method.isStatic = isStatic;
					}
				}
			}
			// if foo = ? where ? is not {} and not a function
			else {
				// no inferred type already set, use the type of the RHS expression
				if(assignment.getInferredType() == null) {
					InferredType rhsType = this.getTypeOf(assignment.getExpression());
					assignment.setInferredType(rhsType);
				}
			}
			
			//only create global type for LHS if LHS's root is global
			if(this.isRootGlobal(lhs)) {
				// construct the LHS and RHS type names
				char[] lhsName = constructTypeName(assignment.getLeftHandSide());
				char[] rhsName = constructTypeName(assignment.getExpression());
	
				//if RHS type exists then create LHS type and add it as synonym of the RHS type
				if(lhsName != null && lhsName.length > 0 && rhsName != null && rhsName.length > 0) {
					InferredType rhsType = this.findDefinedType(rhsName);
					
					if(rhsType != null) {
						InferredType lhsType = this.addType(lhsName, true);
						lhsType.setNameStart(lhs.sourceStart());
						
						lhsType.addSynonym(rhsType);
					}
				}
			}
		}
		return true; // do nothing by default, keep traversing
	}

	protected InferredType createAnonymousType(char[] possibleTypeName, InferredType currentType) {
		char[] name;
		if(this.isKnownType(possibleTypeName)) {
			name = possibleTypeName;
		} else {
			char[] cs = String.valueOf(this.anonymousCount++).toCharArray();
			name = CharOperation.concat(ANONYMOUS_PREFIX, possibleTypeName, cs);
		}
		InferredType type = addType(name, true);
		type.isAnonymous = true;
		type.setIsGlobal(false);
		if(currentType != null) {
			type.setSuperType(currentType);
		}
		
		return type;
	}

	/**
	 * <p>
	 * Creates an anonymous type for a given node with an optional parent type.
	 * </p>
	 * 
	 * @param forNode
	 *            the node to create the anonymous type for, the text range of
	 *            this node will be used to create the anonymous type name
	 * @param parrentType
	 *            optional parent type of the new anonymous type
	 * 
	 * @return a new anonymous type
	 */
	protected InferredType createAnonymousType(IASTNode forNode, InferredType parrentType) {
		char[] name = createAnonymousTypeName(forNode);
		InferredType type = addType(name, true);
		type.isAnonymous = true;
		type.setIsGlobal(false);
		if(parrentType != null) {
			type.setSuperType(parrentType);
		}
		return type;
	}

	/**
	 * @deprecated - here for compatibility
	 */
	private InferredType createAnonymousType(IAbstractVariableDeclaration var) {

		InferredType currentType = var.getInferredType();

		if (currentType==null || !currentType.isAnonymous)
		{
			InferredType type=createAnonymousType(var, currentType);
			var.setInferredType(type);
		}
		return var.getInferredType();
	}
	
	/* Creates an anonymous type based in the location in the document. This information is used
	 * to avoid creating duplicates because of the 2-pass nature of this engine. */
	private InferredType createAnonymousType(IObjectLiteral objLit) {
		InferredType anonType = objLit.getInferredType();
		if(anonType == null) {

			char[] name = createAnonymousTypeName(objLit);
			anonType = addType(name, true);
			anonType.isAnonymous = true;
			anonType.isObjectLiteral = true;
			anonType.setSuperType(this.getObjectType());
			anonType.setIsGlobal(false);

			anonType.sourceStart = objLit.sourceStart();
			anonType.sourceEnd = objLit.sourceEnd();
		}

		populateType(anonType, objLit, false);

		return anonType;
	}

	/**
	 * <p>
	 * Creates a global anonymous type.
	 * </p>
	 * 
	 * @param varName
	 *            name of the global variable to create the global anonymous type for
	 *            
	 * @return a global anonymous type created from for the given global variable name
	 */
	protected InferredType createAnonymousGlobalType(char[] varName) {
		char[] name = createAnonymousGlobalTypeName(varName);
		InferredType globalType = this.compUnit.findInferredType(name);

		if(globalType == null) {
			globalType = this.addType(name, false);
			globalType.isAnonymous = true;
			globalType.isObjectLiteral = true;
			globalType.setSuperType(this.getObjectType());
			globalType.setIsGlobal(true);
		}

		return globalType;
	}

	/**
	 * <p>
	 * Creates an anonymous type name for the given {@link IASTNode}
	 * </p>
	 * 
	 * @param node
	 *            create the anonymous type name off the location of this node
	 * @return an anonymous type name based off the given nodes location
	 */
	protected static char[] createAnonymousTypeName(IASTNode node) {
		char[] loc = (String.valueOf(node.sourceStart()) + '_' + String.valueOf(node.sourceEnd())).toCharArray();
		return CharOperation.concat(ANONYMOUS_PREFIX, ANONYMOUS_CLASS_ID, loc);
	}

	/**
	 * <p>
	 * Creates an anonymous type name from the given variable name.
	 * </p>
	 * 
	 * @param varName
	 *            to use when creating the anonymous type name
	 * @return
	 */
	public static char[] createAnonymousGlobalTypeName(char[] varName) {
		return CharOperation.concat(CharOperation.concat(ANONYMOUS_PREFIX, ANONYMOUS_CLASS_ID), varName, '_');
	}

	/**
	 * handle the inferencing for an assignment whose right hand side is a function expression
	 * 
	 * @param the
	 *            assignment AST node
	 * @return true if handled
	 */
	protected boolean handleFunctionExpressionAssignment(IAssignment assignment) {
		IFunctionExpression functionExpression = null;
		if(assignment.getExpression() instanceof IFunctionExpression) {
			functionExpression = (IFunctionExpression) assignment.getExpression();
		} else if(assignment.getExpression() instanceof IAllocationExpression) {
			functionExpression = (IFunctionExpression) ((IAllocationExpression) assignment.getExpression()).getMember();
		} else if(assignment.getExpression() instanceof IAssignment) {
			functionExpression = (FunctionExpression) ((IAssignment) assignment.getExpression()).getExpression();
		}
		
		if (functionExpression == null) {
			return false;
		}
		
		MethodDeclaration methodDeclaration = functionExpression.getMethodDeclaration();

		char[] possibleTypeName = constructTypeName(assignment.getLeftHandSide());

		InferredType type = null;
		if(possibleTypeName != null) {
			type = compUnit.findInferredType(possibleTypeName);
			if(type == null && isPossibleClassName(possibleTypeName)) {
				type = addType(possibleTypeName, true);
			}
			if(type == null && methodDeclaration.getJsDoc() != null
					&& ((Javadoc) methodDeclaration.getJsDoc()).isConstructor) {
				type = addType(possibleTypeName, true);
				handleJSDocConstructor(type, methodDeclaration, assignment.sourceStart());
			}
		}

		// isConstructor
		if(type != null) {
			if(this.inferOptions.useInitMethod) {
				this.currentContext.currentType = type;
				int nameStart = assignment.getLeftHandSide().sourceStart();
				int expressionEnd = assignment.getExpression().sourceEnd();
				handleConstructor(type, methodDeclaration, nameStart, expressionEnd);
				
				// want to be sure to set the type of the assignment and local declaration if there is one
				assignment.setInferredType(type);
				if(this.currentContext.currentLocalDeclaration != null && CharOperation.equals(this.currentContext.currentLocalDeclaration.getName(), getName(assignment))) {
					this.currentContext.currentLocalDeclaration.setInferredType(type);
				}

				// constructor is actually an anonymous function assigned to a single name
				methodDeclaration.setIsAnonymous(true);
			}
		} else {// could be method
			if(assignment.getLeftHandSide() instanceof FieldReference
					|| assignment.getLeftHandSide() instanceof ArrayReference) {
				
				Reference ref = (Reference) assignment.getLeftHandSide();
				Expression receiver = null;
				char[] methodName = null;
				int nameStart = 0;
				if(ref instanceof FieldReference) {
					receiver = ((FieldReference) ref).receiver;
					methodName = ((FieldReference) ref).token;
					nameStart = (int) (((FieldReference) ref).nameSourcePosition >>> 32);
				} else if(ref instanceof ArrayReference) {
					if(((ArrayReference) ref).position instanceof StringLiteral) {
						receiver = ((ArrayReference) ref).receiver;
						methodName = ((StringLiteral) ((ArrayReference) ref).position).source();
						nameStart = ((StringLiteral) ((ArrayReference) ref).position).sourceStart + 1;
					}
				}
				
				//if no receiver then done
				if(receiver == null) {
					return false;
				}

				InferredType receiverType = getInferredType(receiver);
				if(receiverType == null && passNumber == 2) {
					receiverType = this.getReceiverType(receiver, true);
				}

				if(receiverType != null && methodName != null) {
					if(!receiver.isThis()) {
						receiverType = this.createTypeToAssignTo(receiver, receiverType);
					}

					// check if there is a member method already created
					InferredMethod method = receiverType.findMethod(methodName, methodDeclaration);

					if(method == null) {
						// create member method if it does not exist
						method = receiverType.addMethod(methodName, methodDeclaration, nameStart);
						receiverType.setIsDefinition(true);

						/* determine if static
						 * check if the receiver is a type */
						char[] possibleInTypeName = constructTypeName(receiver);
						method.isStatic = (possibleInTypeName != null && compUnit.findInferredType(possibleInTypeName) != null);

						return true; // keep visiting to get return type
					} else if(this.passNumber == 2) {
						return false; // no need to visit again
					}

				} else if(this.passNumber == 2 && methodName != null) { // create anonymous class
					receiverType = this.getReceiverType(receiver, false);
					if(receiverType != null) {
						InferredMethod method = receiverType.addMethod(methodName, methodDeclaration, nameStart);
						method.isStatic = !receiverType.isObjectLiteral;
						receiverType.updatePositions(assignment.sourceStart(), assignment.sourceEnd());
					}
				}
			} else if(assignment.getLeftHandSide() instanceof SingleNameReference && this.passNumber == 2) {
				// set the inferred type
				assignment.setInferredType(getTypeOf(assignment.getExpression()));

				methodDeclaration.setIsAnonymous(true);
				methodDeclaration.setSelector(((SingleNameReference) assignment.getLeftHandSide()).token);
				methodDeclaration.sourceStart = (((SingleNameReference) assignment.getLeftHandSide()).sourceStart());
			}
		}
		return true;
	}

	/**
	 * <p>
	 * Handle a local declaration who's right hand side is a function.
	 * </p>
	 * <p>
	 * Use case:
	 * </p>
	 * 
	 * <pre>
	 * foo.bar.Test = function() { this.num = 42; }
	 * </pre>
	 * 
	 * @param localDeclaration
	 *            {@link ILocalDeclaration} to attempt to infer a type from
	 * @return <code>true</code> if keep visiting, <code>false</code> otherwise.
	 */
	private boolean handleFunctionExpressionLocalDeclaration(ILocalDeclaration localDeclaration) {
		boolean keepVisiting = true;
		IFunctionExpression functionExpression = null;
		IExpression expression = localDeclaration.getInitialization();
		if(expression instanceof IFunctionExpression) {
			functionExpression = (IFunctionExpression) expression;
		} else if(expression instanceof IAllocationExpression) {
			functionExpression = (IFunctionExpression) ((IAllocationExpression) expression).getMember();
		} else if(expression instanceof IAssignment) {
			functionExpression = (FunctionExpression) ((IAssignment) expression).getExpression();
		}

		if (functionExpression == null) {
			return false;
		}
		
		MethodDeclaration methodDeclaration = functionExpression.getMethodDeclaration();
		char[] possibleTypeName = localDeclaration.getName();

		InferredType type = null;
		if(possibleTypeName != null) {
			type = compUnit.findInferredType(possibleTypeName);
			if(type == null && isPossibleClassName(possibleTypeName)) {
				type = addType(possibleTypeName, true);
			}
			if(type == null && methodDeclaration.getJsDoc() != null
					&& ((Javadoc) methodDeclaration.getJsDoc()).isConstructor) {

				type = addType(possibleTypeName, true);
				handleJSDocConstructor(type, methodDeclaration, localDeclaration.sourceStart());
			}
		}

		if(type != null) { // isConstructor
			if(this.inferOptions.useInitMethod) {
				this.currentContext.currentType = type;
				type.setIsDefinition(true);
				int nameStart = localDeclaration.sourceStart();
				type.addConstructorMethod(type.name, methodDeclaration, nameStart);
				type.updatePositions(nameStart, localDeclaration.getInitialization().sourceEnd());
				
				//set the type for the local declaration to be the new type
				localDeclaration.setInferredType(type);
				localDeclaration.setIsType(true);
			}

			keepVisiting = false;
		}
		return keepVisiting;
	}

	/**
	 * @param assignment
	 * @return whether a type was not created for this assignment
	 */
	protected boolean handlePotentialType(IAssignment assignment) {

		IExpression lhs = assignment.getLeftHandSide();
		if(lhs instanceof FieldReference) {
			FieldReference fieldReference = (FieldReference) lhs;

			/* foo.prototype = ? */
			if(fieldReference.isPrototype()) {
				/* When encountering a prototype, we are going to assume that the
				 * receiver is a type.
				 * 
				 * If the type had not been inferred, it will be added at this point */
				InferredType newType = null;
				char[] possibleTypeName = constructTypeName(fieldReference.getReceiver());
				if(possibleTypeName != null)
					newType = compUnit.findInferredType(possibleTypeName);
				else
					return true; // no type created

				// create the new type if not found
				if(newType == null) {
					// if we have the function, check if it has a return type, if so, don't consider it a new type
					IAbstractFunctionDeclaration theFunction = this.getFunction(fieldReference.getReceiver());
					IAbstractVariableDeclaration theVariable = null;
					if(theFunction == null)
						theVariable = this.getVariable(fieldReference.getReceiver());
					if(theFunction != null) {
						if(theFunction.getInferredType() != null && !theFunction.getInferredType().isVoid()) {
							return false;
						}
					} else if (theVariable != null) {
						if(theVariable.getInitialization() != null && theVariable.getInitialization() instanceof IFunctionExpression) {
							if(((IFunctionExpression)theVariable.getInitialization()).getMethodDeclaration().getInferredType() != null && 
										!((IFunctionExpression)theVariable.getInitialization()).getMethodDeclaration().getInferredType().isVoid()) {
								return false;
							}
						}
					}
					newType = addType(possibleTypeName, true);
				}
				newType.setIsDefinition(true);
				newType.updatePositions(assignment.sourceStart(), assignment.sourceEnd());

				/* foo.prototype = new ... */
				if(assignment.getExpression() instanceof IAllocationExpression) {
					// setting the super type
					IAllocationExpression allocationExpression = (IAllocationExpression) assignment.getExpression();

					InferredType superType = null;
					char[] possibleSuperTypeName = constructTypeName(allocationExpression.getMember());
					if(possibleSuperTypeName != null) {
						superType = compUnit.findInferredType(possibleSuperTypeName);

						if(superType == null)
							superType = addType(possibleSuperTypeName);

						// check if it is set already because it might be set by jsdocs
						if(newType.getSuperType() == null)
							newType.setSuperType(superType);
					}

					return true;
				}
				/* foo.prototype = {...} */
				else if(assignment.getExpression() instanceof IObjectLiteral) {
					// rather than creating an anonymous type, is better just to set the members
					// directly
					// on newType
					populateType(newType, (IObjectLiteral) assignment.getExpression(), false);

					// check if it is set already because it might be set by jsdocs
					if(newType.getSuperType() == null)
						newType.setSuperType(this.getObjectType());

					return true;
				}
				/* foo.prototype = foo.someField; */
				else if(assignment.getExpression() instanceof FieldReference) {
					InferredType superType = getTypeOf(assignment.getExpression());
					if(newType.getSuperType() == null && superType != null) {
						newType.setSuperType(superType);
					}
					return true;
				}
				/* foo.prototype = somevar; */
				else if(assignment.getExpression() instanceof SingleNameReference) {
					InferredType superType = getTypeOf(assignment.getExpression());
					if (newType.getSuperType() == null && superType != null) {
						newType.setSuperType(superType);
					}
					return true;
				}
			}
			/* foo.prototype.bar = ? */
			else if(fieldReference.receiver.isPrototype() ) {

				FieldReference prototype = (FieldReference) fieldReference.receiver;

				//if prototype receiver is a type, get its type
				InferredType assignedToType = null;
				if(this.isExpressionAType(prototype.receiver)) {
					assignedToType = this.getTypeOf(prototype.receiver);
				}
				
				//if not found assigned to type and can create possible name, then create a type
				if(assignedToType == null) {
					char[] possibleTypeName = constructTypeName(prototype.receiver);

					if(possibleTypeName != null) {
						assignedToType = addType(possibleTypeName);
						assignedToType.updatePositions(assignment.sourceStart(), assignment.sourceEnd());
						assignedToType.setIsDefinition(true);
					} else {
						return true; // can not create type, keep visiting
					}
				}

				// prevent Object literal based anonymous types from being created more than once
				if(passNumber == 1 && assignment.getExpression() instanceof IObjectLiteral) {
					return false;
				}

				char[] memberName = fieldReference.token;
				int nameStart = (int) (fieldReference.nameSourcePosition >>> 32);

				InferredType typeOf =
						(assignment.getJsDoc() != null && assignment.getJsDoc() instanceof Javadoc && ((Javadoc) assignment.getJsDoc()).returnType != null) ? this.addType(changePrimitiveToObject(((Javadoc) assignment.getJsDoc()).returnType.getFullTypeName()))
								: getTypeOf(assignment.getExpression());
				IFunctionDeclaration methodDecl = null;

				if(typeOf == null || typeOf == this.getFunctionType())
					methodDecl = getDefinedFunction(assignment.getExpression());

				if(methodDecl != null) {
					assignedToType.addMethod(memberName, methodDecl, nameStart);
				} else {
					InferredAttribute attribute = assignedToType.addAttribute(memberName, assignment, nameStart);
					handleAttributeDeclaration(attribute, assignment.getExpression());
					attribute.initializationStart = assignment.getExpression().sourceStart();
					if(attribute.type == null)
						attribute.type = typeOf;
				}
				return true;
			}
			/* this.foo = ? */
			else if(fieldReference.receiver instanceof IThisReference) {
				InferredType newType = null;

				IFunctionDeclaration parentMethod = this.currentContext.currentMethod;
				IAssignment parentAssignment = this.currentContext.parent.currentAssignment;
				ILocalDeclaration parentLocalDeclaration = this.currentContext.parent.currentLocalDeclaration;
				char[] newTypeName = null;
				int typeStart = 0;
				int typeEnd = 0;
				/* if there is a current assignment and LHS is a function and that function
				 * is the current method then use the RHS as the type name
				 * 
				 * else if there is a current local declaration and the LHS is a function and
				 * that function is the current method then use the RHS as the type name
				 * 
				 * else if the parent method is not in a type and has a name use that as the type
				 * name */
				if(this.currentContext.parent != null
						&& parentAssignment != null
						&& parentAssignment.getExpression() instanceof IFunctionExpression
						&& ((IFunctionExpression) parentAssignment.getExpression()).getMethodDeclaration() == parentMethod) {
					// see if we're adding a field through the prototype
					if (parentAssignment.getLeftHandSide().getASTType() == IASTNode.FIELD_REFERENCE
						&& ((Expression) ((IFieldReference) parentAssignment.getLeftHandSide()).getReceiver()).isPrototype()) {
						newTypeName = Util.getTypeName(((IFieldReference) ((IFieldReference) parentAssignment.getLeftHandSide()).getReceiver()).getReceiver());
					}
					else {
						newTypeName = Util.getTypeName(parentAssignment.getLeftHandSide());
					}
					typeStart = parentAssignment.sourceStart();
					typeEnd = parentAssignment.sourceEnd();
				} else if(this.currentContext.parent != null
						&& parentLocalDeclaration != null
						&& parentLocalDeclaration.getInitialization() instanceof IFunctionExpression
						&& ((IFunctionExpression) parentLocalDeclaration.getInitialization()).getMethodDeclaration() == parentMethod) {

					newTypeName = parentLocalDeclaration.getName();
					typeStart = parentLocalDeclaration.sourceStart();
					typeEnd = parentLocalDeclaration.sourceEnd();
				} else if(parentMethod != null
						&& parentMethod.getName() != null
						&& (parentMethod.getInferredMethod() == null || parentMethod.getInferredMethod().inType == null)) {

					newTypeName = parentMethod.getName();
					typeStart = parentMethod.sourceStart();
					typeEnd = parentMethod.sourceEnd();
				}

				// if calculated new type name, use it to create a new type
				if(newTypeName != null) {
					newType = compUnit.findInferredType(newTypeName);
					// create the new type if not found
					if(newType == null) {
						newType = addType(newTypeName);
					}
				} else {
					return false; // no type to create
				}

				newType.setIsDefinition(true);
				newType.updatePositions(typeStart, typeEnd);
				
				//if there is a parent assignment then set the inferred type and that it is a type
				if(parentAssignment != null) {
					parentAssignment.setInferredType(newType);
					parentAssignment.setIsType(true);
				}
				
				//if there is a parent declaration then set the inferred type and that it is a type
				if(parentLocalDeclaration != null) {
					parentLocalDeclaration.setInferredType(newType);
					parentLocalDeclaration.setIsType(true);
				}

				// prevent Object literal based anonymous types from being created more than once
				if(passNumber == 1 && assignment.getExpression() instanceof IObjectLiteral) {
					return false;
				}

				char[] memberName = fieldReference.token;
				int nameStart = (int) (fieldReference.nameSourcePosition >>> 32);

				InferredType typeOf = getTypeOf(assignment.getExpression());
				IFunctionDeclaration methodDecl = null;

				if(typeOf == null || typeOf == this.getFunctionType()) {
					methodDecl = getDefinedFunction(assignment.getExpression());
				}

				if(methodDecl != null) {
					newType.addMethod(memberName, methodDecl, nameStart);
					if(methodDecl.getInferredType() == null && assignment.getJsDoc() != null
							&& ((Javadoc) assignment.getJsDoc()).returnType != null) {
						if(((Javadoc) assignment.getJsDoc()).returnType.getFullTypeName() != null)
							methodDecl.setInferredType(addType(((Javadoc) assignment.getJsDoc()).returnType.getFullTypeName()));
					}
				} else {
					InferredAttribute attribute = newType.addAttribute(memberName, assignment, nameStart);
					if(attribute.type == null && assignment.getJsDoc() != null
							&& ((Javadoc) assignment.getJsDoc()).returnType != null) {
						if(((Javadoc) assignment.getJsDoc()).returnType.getFullTypeName() != null)
							attribute.type = addType(((Javadoc) assignment.getJsDoc()).returnType.getFullTypeName());
					}
					handleAttributeDeclaration(attribute, assignment.getExpression());
					attribute.initializationStart = assignment.getExpression().sourceStart();
					if(attribute.type == null)
						attribute.type = typeOf;
				}
				return true;
			}			
		}
		return false;
	}

	/**
	 * Get the function referenced by the expression
	 * 
	 * @param expression
	 *            AST node
	 * @return the function or null
	 */
	protected IFunctionDeclaration getDefinedFunction(IExpression expression) {
		if(expression instanceof SingleNameReference) {
			Object object = this.currentContext.getMember(((SingleNameReference) expression).token);
			if(object instanceof AbstractMethodDeclaration) {
				return (MethodDeclaration) object;
			}
		} else if(expression instanceof FunctionExpression) {
			return ((FunctionExpression) expression).methodDeclaration;
		} else if(expression instanceof FieldReference) {
			FieldReference fieldReference = (FieldReference) expression;
			InferredType receiverType = getInferredType(fieldReference.receiver);
			if(receiverType == null && passNumber == 2) {
				receiverType = this.getReceiverType(fieldReference.receiver, false);
			}
			if(receiverType != null) {
				InferredMethod method = receiverType.findMethod(fieldReference.token, null);
				if(method != null) {
					return method.getFunctionDeclaration();
				}
			}

		}

		return null;

	}

	/**
	 * <p>
	 * Sets the inferred type of the given expression to the given type. Any existing inferred type
	 * is overridden. If the given expression is not supported by this method then it is a no op.
	 * </p>
	 * 
	 * <p>
	 * Currently supports:
	 * <ul>
	 * <li>{@link ISingleNameReference}</li>
	 * <li>{@link FieldReference} - if it can resolve everything it needs to</li>
	 * <li>{@link IObjectLiteral}</li>
	 * <li>{@link IThisReference}</li>
	 * </ul>
	 * </p>
	 * 
	 * @param expression
	 *            to set the inferred type for
	 * @param type
	 *            inferred type to set on the given expression
	 */
	protected void setTypeOf(IExpression expression, InferredType type) {
		if(expression instanceof ISingleNameReference) {
			//set on variable declaration if there is one
			IAbstractVariableDeclaration varDecl = getVariable(expression);
			if(varDecl != null) {
				varDecl.setInferredType(type);
			}

			//set on global field if there is one
			if(this.inferredGlobal != null) {
				InferredAttribute attribute =
						this.inferredGlobal.findAttribute(((ISingleNameReference) expression).getToken());
				if(attribute != null) {
					attribute.type = type;
				}
			}
			
			//set on assignment if there is one
			IAssignment varAssignment = this.getAssignment(expression);
			if(varAssignment != null) {
				varAssignment.setInferredType(type);
			}
		} else if(expression instanceof FieldReference) {
			FieldReference fieldReference = (FieldReference) expression;
			InferredType parentType = this.getTypeOf(fieldReference.getReceiver());
			if(parentType != null) {
				InferredAttribute attr = parentType.findAttribute(fieldReference.token);
				if(attr != null) {
					attr.type = type;
				}
			}
		} else if(expression instanceof IObjectLiteral) {
			((IObjectLiteral) expression).setInferredType(type);
		} else if(expression instanceof IThisReference) {
			this.currentContext.currentType = type;
		}
	}

	protected InferredType getTypeOf(IExpression expression) {
		if(expression instanceof IStringLiteral) {
			return this.getStringType();
		} else if(expression instanceof INumberLiteral) {
			return this.getNumberType();
		} else if(expression instanceof IAllocationExpression) {
			IAllocationExpression allocationExpression = (IAllocationExpression) expression;
			IExpression member = allocationExpression.getMember();
			InferredType type = null;
			
			//check for type with the same name as the allocation member
			char[] possibleTypeName = constructTypeName(member);
			if(possibleTypeName != null) {
				type = compUnit.findInferredType(possibleTypeName);

			}
			
			/* if no existing type with the same name as the allocation member
			 * attempt to get the type of the member */
			if(type == null) {
				type = this.getTypeOf(member);
			}
			
			/* if no type with same name as member, and member does not have a type,
			 * create a new type with the same name as the member */
			if((type == null || type == getFunctionType()) && possibleTypeName != null && possibleTypeName.length > 0) {
				type = this.addType(possibleTypeName, false);
			}
			
			return type;
		} else if(expression instanceof ISingleNameReference) {
			//check for variable declaration to get type from
			IAbstractVariableDeclaration varDecl = this.getVariable(expression);
			if(varDecl != null) {
				return varDecl.getInferredType();
			}
			
			//check global type to get type from
			if(this.inferredGlobal != null) {
				InferredAttribute attribute =
						this.inferredGlobal.findAttribute(((ISingleNameReference) expression).getToken());
				if(attribute != null) {
					return attribute.type;
				}
			}
			
			//check for an assignment to get the type from
			IAssignment assign = this.getAssignment(expression);
			if(assign != null) {
				InferredType type = assign.getInferredType();
				if(type != null) {
					return type;
				}
			}
			
			/* check if expression is a function and if there is a type with the same name,
			 * if so then the given expression is a reference to that type */
			IAbstractFunctionDeclaration funcDecl = this.getFunction(expression);
			if(funcDecl != null) {
				InferredType type = this.findDefinedType(funcDecl.getName());
				if(type != null) {
					return type;
				}
			}
			
			//search for type with that name
			char[] possibleTypeName = this.constructTypeName(expression);
			if(possibleTypeName != null) {
				InferredType type = compUnit.findInferredType(possibleTypeName);
				if(type != null) {
					return type;
				}
			}
			
			//search for anonymous global type based of SNR
			char[] possibleGlobalTypeName = createAnonymousGlobalTypeName(((SingleNameReference)expression).token);
			if(possibleGlobalTypeName != null) {
				InferredType type = compUnit.findInferredType(possibleGlobalTypeName);
				if(type != null) {
					return type;
				}
			}
			if(funcDecl != null) {
				return getFunctionType();
			}
		} else if(expression instanceof FieldReference) {
			FieldReference fieldReference = (FieldReference) expression;
			/* if this reference and in current type
			 * else not  */
			if(fieldReference.receiver.isThis() && currentContext.currentType != null) {
				InferredAttribute attribute = currentContext.currentType.findAttribute(fieldReference.getToken());
				if(attribute != null) {
					return attribute.type;
				}
			} else {
				//get the receiver type then get the type of an existing field or function
				InferredType recieverType = this.getReceiverType(((FieldReference) expression).getReceiver(), false);
				if(recieverType != null) {
					char[] fieldName = ((FieldReference) expression).getToken();
					InferredAttribute attr = recieverType.findAttribute(fieldName);
					if(attr != null) {
						return attr.type;
					} else if(recieverType.findMethod(fieldName, null) != null){
						return this.getFunctionType();
					}
				}
			}
			
			char[] typeName = constructTypeName(expression);
			if(typeName != null && typeName.length > 0) {
				InferredType type = this.findDefinedType(typeName);
				if(type != null) {
					return type;
				}
			}
		} else if(expression instanceof IReturnStatement) {
			return ((IReturnStatement) expression).getInferredType();
		} else if(expression instanceof ArrayInitializer) {
			ArrayInitializer arrayInitializer = (ArrayInitializer) expression;
			boolean typeSet = false;
			InferredType memberType = null;
			if(arrayInitializer.expressions != null)
				for(int i = 0; i < arrayInitializer.expressions.length; i++) {
					InferredType thisType = getTypeOf(arrayInitializer.expressions[i]);
					if(thisType != null) {
						if(!thisType.equals(memberType))
							if(!typeSet) {
								memberType = thisType;
							} else {
								memberType = null;
							}
						typeSet = true;

					}
				}
			if(memberType != null) {
				InferredType type = new InferredType(InferredType.ARRAY_NAME);
				type.referenceClass = memberType;
				return type;
			} else {
				return this.getArrayType();
			}
		} else if(expression instanceof ITrueLiteral || expression instanceof IFalseLiteral) {
			return this.getBooleanType();
		} else if(expression instanceof IObjectLiteral) {

			// create an anonymous type based on the ObjectLiteral
			InferredType type = createAnonymousType((IObjectLiteral) expression);

			// set the start and end
			type.sourceStart = expression.sourceStart();
			type.sourceEnd = expression.sourceEnd();

			return type;
		} else if(expression instanceof IThisReference) {
			return this.currentContext.currentType != null ? this.currentContext.currentType : getInferredGlobal(true);
		} else if(expression instanceof Assignment)
			return getTypeOf(((Assignment) expression).getExpression());
		else if(expression instanceof FunctionExpression)
			return this.getFunctionType();
		else if(expression instanceof UnaryExpression) {
			return getTypeOf(((UnaryExpression) expression).expression);
		} else if(expression instanceof BinaryExpression) {
			BinaryExpression bExpression = (BinaryExpression) expression;
			int operator = (bExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT;
			switch(operator) {
				case OperatorIds.MULTIPLY:
				case OperatorIds.DIVIDE:
				case OperatorIds.REMAINDER:
				case OperatorIds.MINUS:
				case OperatorIds.LEFT_SHIFT:
				case OperatorIds.RIGHT_SHIFT:
					return this.getNumberType();
				case OperatorIds.PLUS: {
					InferredType leftType = getTypeOf(bExpression.left);
					InferredType rightType = getTypeOf(bExpression.right);
					if(leftType != null && leftType.equals(this.getStringType())) {
						return this.getStringType();
					}
					if(rightType != null && rightType.equals(this.getStringType())) {
						return this.getStringType();
					}
					if(leftType == null || rightType == null) {
						return null;
					}
					if(leftType.equals(this.getStringType()) || rightType.equals(this.getStringType())) {
						return this.getStringType();
					} else if(leftType.equals(this.getNumberType()) && rightType.equals(this.getNumberType())) {
						return this.getNumberType();
					}
					return null;
				}
				case OperatorIds.AND_AND:
				case OperatorIds.OR_OR: {
					/* if the case:
					 * 		foo = foo || ""
					 * 		return foo || ""
					 * 		foo = foo && ""
					 * 		return foo || ""
					 * else a normal || binary case */
					if((this.currentContext.currentAssignment != null && expressionContains(this.currentContext.currentAssignment.getExpression(), bExpression)) ||
								(this.currentContext.currentLocalDeclaration != null && expressionContains(this.currentContext.currentLocalDeclaration.getInitialization(), bExpression)) ||
								(this.currentContext.currentReturn != null && expressionContains(this.currentContext.currentReturn.getExpression(), bExpression))) {

						InferredType leftType = this.getTypeOf(bExpression.left);
						InferredType rightType = this.getTypeOf(bExpression.right);

						/* if both the left and right type are the same then just return the left one
						 * else merge the two types into one type
						 */
						if(leftType == rightType) {
							return leftType;
						} else if(leftType != null && rightType != null) {
							InferredType newCombinedType  = this.createAnonymousType(expression, null);
							
							/* add the left type to the combined type
							 * 
							 * if the left type is indexed then add it later
							 * else it is not must add it now */
							if(leftType.isIndexed()) {
								newCombinedType.addMixin(leftType.getName());
							} else {
								newCombinedType.mixin(leftType);
							}
							
							/* add the right type to the combined type
							 * 
							 * if the right type is indexed then add it later
							 * else it is not must add it now */
							if(rightType.isIndexed()) {
								newCombinedType.addMixin(rightType.getName());
							} else {
								newCombinedType.mixin(rightType);
							}
							
							return newCombinedType;
						} else if(leftType != null) {
							return leftType;
						} else if(rightType != null) {
							return rightType;
						}
					}
					
					return this.getBooleanType();
				}
				case OperatorIds.EQUAL_EQUAL:
				case OperatorIds.EQUAL_EQUAL_EQUAL:
				case OperatorIds.NOT_EQUAL:
				case OperatorIds.NOT_EQUAL_EQUAL:
				case OperatorIds.GREATER:
				case OperatorIds.GREATER_EQUAL:
				case OperatorIds.LESS:
				case OperatorIds.LESS_EQUAL:
				case OperatorIds.INSTANCEOF:
				case OperatorIds.IN:
					return this.getBooleanType();
				default:
					return null;
			}
		} else if(expression instanceof MessageSend) {
			if(((MessageSend)expression).receiver instanceof IFunctionExpression) {
				if(((FunctionExpression)((MessageSend)expression).receiver).methodDeclaration != null) {
					return ((FunctionExpression)((MessageSend)expression).receiver).methodDeclaration.inferredType;
				}
			} else if(((MessageSend)expression).selector != null) {
				// if the method call has no receiver, see if we can look it up
				// in the current context. If found, the inferredType(return type) of
				// that function is the value we are looking for.
				Object method = this.currentContext.getMember(((MessageSend)expression).selector);
				if(method instanceof MethodDeclaration) {
					return ((MethodDeclaration) method).inferredType;
				}
			}
		}

		return null;
	}

	protected void populateType(InferredType type, IObjectLiteral objLit, boolean isStatic) {
		if(objLit.getInferredType() == null) {
			objLit.setInferredType(type);
			if(objLit.getFields() != null) {
				for(int i = 0; i < objLit.getFields().length; i++) {
					IObjectLiteralField field = objLit.getFields()[i];

					char[] name = null;
					int nameStart = -1;

					if(field.getFieldName() instanceof SingleNameReference) {
						SingleNameReference singleNameReference = (SingleNameReference) field.getFieldName();
						name = singleNameReference.token;
						nameStart = singleNameReference.sourceStart;
					} else if(field.getFieldName() instanceof IStringLiteral) {
						IStringLiteral stringLiteral = (IStringLiteral) field.getFieldName();
						name = stringLiteral.source();
						nameStart = stringLiteral.sourceStart();
					} else
						continue; // not supporting this case right now

					Javadoc javaDoc = (Javadoc) field.getJsDoc();
					InferredType returnType = null;
					if(javaDoc != null) {
						if(javaDoc.memberOf != null) {
							char[] typeName = javaDoc.memberOf.getFullTypeName();
							convertAnonymousTypeToNamed(type, typeName);
							type.setIsDefinition(true);
						} else if(this.currentContext.isJsDocClass && javaDoc.property != null) {
							if(type.isAnonymous) {
								InferredType previousType = this.currentContext.currentType;
								if(previousType != null) {
									copyAnonymousTypeToNamed(type, previousType);
									objLit.setInferredType(type = this.currentContext.currentType = previousType);
								}

							}
						}
						if(javaDoc.returnType != null) {
							returnType = this.addType(changePrimitiveToObject(javaDoc.returnType.getFullTypeName()));
						}
					}

					// need to build the members of the anonymous inferred type
					if(field.getInitializer() instanceof IFunctionExpression) {
						IFunctionExpression functionExpression = (IFunctionExpression) field.getInitializer();
						InferredMember method =
								type.addMethod(name, functionExpression.getMethodDeclaration(), nameStart);
						method.isStatic = isStatic;
						if(javaDoc != null) {
							functionExpression.getMethodDeclaration().modifiers = javaDoc.modifiers;
						}
						handleFunctionDeclarationArguments(functionExpression.getMethodDeclaration(), javaDoc);
						if(returnType != null && functionExpression.getMethodDeclaration().getInferredType() == null) {
							functionExpression.getMethodDeclaration().setInferredType(returnType);
						}

					}
					// attribute
					else if (field.getInitializer() != null) {
						InferredAttribute attribute = type.findAttribute(name);
						if(attribute == null) {
							attribute = type.addAttribute(name, field.getInitializer(), nameStart);
							handleAttributeDeclaration(attribute, field.getInitializer());
							attribute.isStatic = isStatic;
							// @GINO: recursion might not be the best idea
							if(returnType != null) {
								attribute.type = returnType;
								// apply (force) type onto OL initializer
								if(field.getInitializer() instanceof ObjectLiteral) {
									((ObjectLiteral) field.getInitializer()).setInferredType(returnType);
								}
							} else {
								attribute.type = getTypeOf(field.getInitializer());
							}
							
							
						}
						
						//if attribute is function then create a function as well
						if(attribute.type != null && attribute.type.isFunction()) {
							if( type.findMethod(attribute.name, null) == null) {
								IAbstractFunctionDeclaration func = this.getFunction(field.getInitializer());
								if(func instanceof IFunctionDeclaration) {
									type.addMethod(name, (IFunctionDeclaration)func, nameStart);
								}
							}
						}
					}
				}
			}
		}
	}

	public void endVisit(IAssignment assignment) {
		popContext();
	}
	
	protected boolean handleAttributeDeclaration(InferredAttribute attribute) {
		return true;
	}

	protected boolean handleAttributeDeclaration(InferredAttribute attribute, IExpression initializer) {
		return true;
	}

	protected boolean handleFunctionCall(IFunctionCall messageSend) {
		return handleFunctionCall(messageSend, null);
	}

	protected boolean handleFunctionCall(IFunctionCall messageSend, LocalDeclaration assignmentExpression) {
		return true;
	}

	public boolean visit(IReturnStatement returnStatement) {
		this.pushContext();
		this.currentContext.currentReturn = returnStatement;
		
		if(currentContext.currentMethod != null) {
			if(returnStatement.getExpression() != null) {

				InferredType type = null;
				IExpression expression = returnStatement.getExpression();
				if(expression instanceof IObjectLiteral) {
					type = createAnonymousType((ObjectLiteral) expression);

					// set the start and end
					type.sourceStart = expression.sourceStart();
					type.sourceEnd = expression.sourceEnd();
				} else {
					type = getTypeOf(expression);
				}
				if(currentContext.currentMethod.getInferredType() == this.getVoidType() && type != null) {
					currentContext.currentMethod.setInferredType(type);
				} else if(currentContext.currentMethod.getInferredType() == this.getArrayType() 
							&& currentContext.currentMethod.getInferredType().referenceClass == null
							&& type != null && CharOperation.equals(type.name, TypeConstants.ARRAY[0])){
					currentContext.currentMethod.setInferredType(type);
				} else {
					/* If the return statement inferred type is null or
					 * the existing inferred return type and the statement return type are not
					 * equal and the return type is either not well known or is well
					 * known and the return type names are the same
					 * 
					 * This logic is to cover the scenario where the return type is a known type but
					 * is from a different instance of the InferEngine */
					boolean shouldSetToAny = !((MethodDeclaration) currentContext.currentMethod).isInferredJsDocType();
					if(type != null && shouldSetToAny) {
						// get the name of the current methods inferred return type
						String currentMethodInferredType = null;
						if(this.currentContext.currentMethod.getInferredType() != null
								&& this.currentContext.currentMethod.getInferredType().name != null) {
							currentMethodInferredType =
									new String(this.currentContext.currentMethod.getInferredType().name);
						}

						boolean returnTypesEqual = type.equals(currentContext.currentMethod.getInferredType());
						boolean returnTypeNamesEqual = (new String(type.name)).equals(currentMethodInferredType);
						boolean returnTypeIsWellKnown = WellKnownTypes.containsKey(type.name);

						shouldSetToAny =
								!returnTypesEqual
										&& (!returnTypeIsWellKnown || !(returnTypeIsWellKnown && returnTypeNamesEqual));
					}

					if(shouldSetToAny) {
						currentContext.currentMethod.setInferredType(null);
					}
				}
			}
		}
		return false;
	}
	
	/**
	 * @see org.eclipse.wst.jsdt.core.ast.ASTVisitor#endVisit(org.eclipse.wst.jsdt.core.ast.IReturnStatement)
	 */
	public void endVisit(IReturnStatement returnStatement) {
		this.popContext();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.core.ast.ASTVisitor#endVisit(org.eclipse.wst.jsdt.core.ast.IFunctionCall)
	 */
	public void endVisit(IFunctionCall functionCall) {
		super.endVisit(functionCall);

		if(functionCall.getReceiver() instanceof FunctionExpression) {
			if (this.contextPtr == -1) {
				this.isTopLevelAnonymousFunction = true;
			}
			if (functionCall instanceof MessageSend && ((MessageSend) functionCall).getArguments() != null) {
				MethodDeclaration methodDeclaration = ((FunctionExpression) functionCall.getReceiver()).getMethodDeclaration();
				if (methodDeclaration != null && methodDeclaration.getArguments() != null) {
					IArgument[] declaredArguments = methodDeclaration.getArguments();
					IExpression[] sentArguments = ((MessageSend) functionCall).getArguments();
					for (int i = 0; i < declaredArguments.length; i++) {
						if (i >= sentArguments.length) {
							continue;
						}
						// if a sent argument now is a subtype of the declared argument, update the sent argument to be the type of the declared argument
						char[] parameterName = null;
						if ((parameterName = getName(sentArguments[i])) != null && passNumber == 2 && declaredArguments[i].getInferredType()!=null) {
							IASTNode node = (IASTNode) currentContext.getMember(parameterName);
							if (node instanceof IAbstractVariableDeclaration && ((IAbstractVariableDeclaration) node).getInferredType() != null) {
								if (((IAbstractVariableDeclaration) node).getInferredType().isNamed() && ((LocalDeclaration) node).getInferredType().equals(declaredArguments[i].getInferredType().getSuperType())) {
									((IAbstractVariableDeclaration) node).setInferredType(declaredArguments[i].getInferredType());
								}
							}
							else if (node instanceof IAssignment && ((IAssignment) node).getInferredType() != null) {
								if (((IAssignment) node).getInferredType().isNamed() && ((IAssignment) node).getInferredType().equals(declaredArguments[i].getInferredType().getSuperType())) {
									((IAssignment) node).setInferredType(declaredArguments[i].getInferredType());
								}
							}
						}
					}
				}
			}
		}
	}

	public void endVisit(IFunctionDeclaration methodDeclaration) {
		popContext();
	}

	public boolean visit(IFunctionDeclaration methodDeclaration) {
		pushContext();
		if(this.isTopLevelAnonymousFunction && this.currentContext.currentType == null) {
			this.currentContext.currentType = this.getInferredGlobal(true);
		} else if(!this.isTopLevelAnonymousFunction && this.currentContext.currentType != null && CharOperation.equals(this.currentContext.currentType.getName(), IIndexConstants.GLOBAL_SYMBOL)) {
			// if we are not a top level function and the current type is global, reset it to null.
			// this prevents this.anything style attributes from leaking to the global scope.
			this.currentContext.currentType = null;
		}

		this.isTopLevelAnonymousFunction = false;
		char[] methodName = methodDeclaration.getName();
		// if declaration didn't have name get name from inferred method if there is one
		if(methodName == null && methodDeclaration.getInferredMethod() != null) {
			methodName = methodDeclaration.getInferredMethod().name;
		}
		
		//set the function that contains this assignment
		if(this.passNumber == 1 && methodDeclaration instanceof AbstractMethodDeclaration && this.currentContext.currentMethod != null) {
			((AbstractMethodDeclaration)methodDeclaration).setContainingFunction(this.currentContext.currentMethod);
		}

		//always need to build arguments
		buildDefinedMembers(methodDeclaration.getStatements(), methodDeclaration.getArguments());
		
		//only on the first pass should JS doc be looked at
		if(passNumber == 1) {
			if(methodDeclaration.getJsDoc() != null) {
				InferredMethod method = null;
				Javadoc javadoc = (Javadoc) methodDeclaration.getJsDoc();
				createTypeIfNecessary(javadoc);
				if(javadoc.isConstructor) {
					InferredType type;
					if(!this.currentContext.isJsDocClass && methodName != null) {
						type = this.addType(methodName);
					} else {
						type = this.currentContext.currentType;
					}
					
					if(type != null) {
						handleJSDocConstructor(type, methodDeclaration, methodDeclaration.sourceStart());
					}
				} else if(javadoc.memberOf != null) {
					InferredType type = this.addType(javadoc.memberOf.getFullTypeName(), true);
					char[] name = methodName;
					int nameStart = methodDeclaration.sourceStart();
					if(name != null) {
						method = type.addMethod(methodName, methodDeclaration, nameStart);
					}
				} else if(javadoc.methodDef != null && this.currentContext.isJsDocClass) {
					InferredType type = this.currentContext.currentType;
					char[][] methName = javadoc.methodDef.getTypeName();
					int nameStart = ((MethodDeclaration) methodDeclaration).sourceStart;
					if(methName.length == 1) {
						method = type.addMethod(methName[0], methodDeclaration, nameStart);
					} else {
						method = type.addMethod(methName[methName.length - 1], methodDeclaration, nameStart);
						method.isStatic = true;
					}

				}

				if(javadoc.returnType != null) {
					InferredType type = this.addType(changePrimitiveToObject(javadoc.returnType.getFullTypeName()));
					methodDeclaration.setInferredType(type);
					((MethodDeclaration) methodDeclaration).bits |= ASTNode.IsInferredJsDocType;
				}

			}
			handleFunctionDeclarationArguments(methodDeclaration, methodDeclaration.getJsDoc());
		}
		
		// check if this is a constructor
		if(passNumber == 2) {
			if(methodName != null) {
				InferredType type = compUnit.findInferredType(methodName);
				InferredMethod inferMeth = methodDeclaration.getInferredMethod();
				
				//only a constructor if the method is in the found type, or is in a type with the same name
				if(type != null &&
						(inferMeth == null ||
						inferMeth.inType == null ||
						inferMeth.inType == type ||
						CharOperation.equals(inferMeth.inType.getName(), type.getName())) ) {
						 
					this.currentContext.currentType = type;
					type.setIsDefinition(true);
					
					if(inferMeth == null || !inferMeth.isConstructor) {
						int nameStart = methodDeclaration.sourceStart();
						type.addConstructorMethod(methodName, methodDeclaration, nameStart);
					}
				}
			}
		}
		this.currentContext.currentMethod = methodDeclaration;
		if(methodDeclaration.getInferredMethod() != null && methodDeclaration.getInferredMethod().inType != null) {
			this.currentContext.currentType = methodDeclaration.getInferredMethod().inType;
		}
		
		if(methodDeclaration.getInferredType() == null) {
			methodDeclaration.setInferredType(this.getVoidType());
		}
		
		return true;
	}
	
	protected void handleConstructor(InferredType type, IFunctionDeclaration methodDeclaration, int start, int end) {
		type.setIsDefinition(true);
		type.addConstructorMethod(type.name, methodDeclaration, start);
		type.updatePositions(start, end);
	}

	protected void handleJSDocConstructor(InferredType type, IFunctionDeclaration methodDeclaration, int nameStart) {
		Javadoc javadoc = (Javadoc) methodDeclaration.getJsDoc();
		type.setIsDefinition(true);
		type.addConstructorMethod(type.name, methodDeclaration, nameStart);

		if(javadoc.extendsType != null) {
			InferredType superType = this.addType(javadoc.extendsType.getFullTypeName());
			type.setSuperType(superType);
		}

	}
	
	protected void handleFunctionDeclarationArguments(IFunctionDeclaration methodDeclaration, IJsDoc jsdoc) {
		if(jsdoc == null || !(jsdoc instanceof Javadoc))
			return;
		Javadoc javadoc = (Javadoc) jsdoc;

		IArgument[] arguments = methodDeclaration.getArguments();
		if(arguments != null) {
			for(int i = 0; i < arguments.length; i++) {
				//skip if argument already has a named type
				InferredType currType = arguments[i].getInferredType();
				if(currType != null && !currType.isAnonymous) {
					//TODO: this could possibly be smarter
					continue;
				}

				InferredType paramType = null;
				JavadocSingleNameReference param = javadoc.findParam(arguments[i].getName());
				if(param != null) {
					if(param.types != null) {
						char[] name = {};
						for(int j = 0; j < param.types.length; j++) {
							// char []typeName=param.types[j].getFullTypeName();
							// make sure we are using the type version of Boolean, even if the user
							// entered boolean as the JSdoc type.
							char[] typeName = changePrimitiveToObject(param.types[j].getFullTypeName());
							if(j == 0) {
								name = typeName;
							} else {
								name = CharOperation.append(name, '|');
								name = CharOperation.concat(name, typeName);
							}
						}
						
						paramType = this.addType(name);
					}
				}
				/**
				 * http://code.google.com/p/jsdoc-toolkit/wiki/InlineDocs
				 **/
				else if(arguments[i].getJsDoc() != null) {
					if(((Javadoc) arguments[i].getJsDoc()).returnType != null) {
						paramType = this.addType(((Javadoc) arguments[i].getJsDoc()).returnType.getFullTypeName());
					}
				} else if(arguments[i].getComment() != null) {
					char[] comment = CharOperation.trim(arguments[i].getComment());
					boolean validForName = true;
					for(int j = 0; j < comment.length && validForName; j++) {
						validForName &=
								!CharOperation.isWhitespace(comment[j])
										&& (Character.isJavaIdentifierPart(comment[j]) || comment[j] == '.');
					}
					if(validForName) {
						paramType = this.addType(comment);
					}
				}
				
				if(paramType != null) {
					/* if the current type is not null (then it it is also anonymous)
					 * set the doced type as its parent */
					if(currType != null) {
						InferredType currSuperType = currType.getSuperType();
						currType.setSuperType(paramType);
						paramType.setSuperType(currSuperType);
						
						paramType = currType;
					}
					
					arguments[i].setInferredType(paramType);
				}
			}
		}
	}

	private void copyAnonymousTypeToNamed(InferredType inClass, InferredType toType) {
		if(toType == null)
			return;

		compUnit.inferredTypesHash.removeKey(inClass.name);
		if(inClass.methods != null) {
			toType.methods.addAll(inClass.methods);
		}
		if(inClass.attributes != null) {
			for(int i = 0; i < inClass.numberAttributes; i++) {
				toType.addAttribute(inClass.attributes[i]);
			}
		}

	}

	/**
	 * <p>
	 * Renames the given type to the given name. If there is a constructor on the type
	 * that is also renamed.
	 * </p>
	 * 
	 * @param type
	 *            {@link InferredType} to rename
	 * @param newTypeName
	 *            new type name for the given {@link InferredType}
	 */
	protected void renameType(InferredType type, char[] newTypeName) {
		//rename constructor on type if there is one
		InferredMethod constructor = type.findMethod(TypeConstants.INIT, null);
		if(constructor != null) {
			constructor.name = newTypeName;
		}
		
		//rename the type
		compUnit.inferredTypesHash.removeKey(type.name);
		type.name = newTypeName;
		compUnit.inferredTypesHash.put(newTypeName, type);
	}

	/**
	 * <p>
	 * Converts the given anonymous type to a named global type. If the given type is not anonymous
	 * then this is a no-op.
	 * </p>
	 * 
	 * @param type
	 *            anonymous {@link InferredType} to name
	 * @param newTypeName
	 *            new type name for the given anonymous {@link InferredType}
	 * 
	 * @see InferredType#isAnonymous
	 */
	protected void convertAnonymousTypeToNamed(InferredType type, char[] newTypeName) {
		if(type.isAnonymous) {
			this.renameType(type, newTypeName);
			type.isAnonymous = false;
			type.setIsGlobal(true);
		}
	}

	protected boolean isMatch(IExpression expr, char[][] names, int index) {
		char[] matchName = names[index];
		if(expr instanceof SingleNameReference) {
			SingleNameReference snr = (SingleNameReference) expr;
			return CharOperation.equals(snr.token, matchName);
		} else if(expr instanceof FieldReference && names.length > 1 && index > 0) {
			FieldReference fieldReference = (FieldReference) expr;
			if(CharOperation.equals(fieldReference.token, matchName)) {
				return isMatch(fieldReference.receiver, names, index - 1);
			}

		}
		return false;
	}

	/**
	 * @deprecated not used
	 */
	protected boolean isFunction(IFunctionCall messageSend, String string) {
		String[] names = string.split("\\."); //$NON-NLS-1$
		char[] functionName = names[names.length - 1].toCharArray();
		if(!CharOperation.equals(functionName, messageSend.getSelector())) {
			return false;
		}

		char[][] namesChars = new char[names.length][];
		for(int i = 0; i < namesChars.length; i++) {
			namesChars[i] = names[i].toCharArray();
		}
		if(names.length > 1) {
			return isMatch(messageSend.getReceiver(), namesChars, namesChars.length - 2);
		}
		return true;
	}

	protected boolean isFunction(IFunctionCall messageSend, char[][] names) {
		if(messageSend == null) {
			return false;
		}
		
		char[] functionName = names[names.length - 1];
		if(!CharOperation.equals(functionName, messageSend.getSelector())) {
			return false;
		}

		if(names.length > 1) {
			return isMatch(messageSend.getReceiver(), names, names.length - 2);
		}
		
		return true;
	}

	public void doInfer() {
		try {
			long time0 = 0;
			if(REPORT_INFER_TIME) {
				time0 = System.currentTimeMillis();
			}
			clearBuiltInTypes();
			
			ASTVisitor visitor = getVisitor(compUnit);
			if (visitor != null)
				compUnit.traverse(visitor);
			
			passNumber = 2;
			visitor = getVisitor(compUnit);
			if (visitor != null)
				compUnit.traverse(visitor);
			
			for(int i = 0; i < compUnit.numberInferredTypes; i++) {
				if(compUnit.inferredTypes[i].sourceStart < 0)
					compUnit.inferredTypes[i].sourceStart = 0;
			}

			if(REPORT_INFER_TIME) {
				long time = System.currentTimeMillis() - time0;
				System.err.println(getClass().getName() + " inferred " + new String(compUnit.getFileName()) + " in " //$NON-NLS-1$ //$NON-NLS-2$
						+ time + "ms"); //$NON-NLS-1$
			}
			this.compUnit = null;

		} catch(RuntimeException e) {
			org.eclipse.wst.jsdt.internal.core.util.Util.log(e, "error during type inferencing"); //$NON-NLS-1$
		}
	}

	protected InferredType addType(char[] className) {
		return addType(className, false);
	}

	/**
	 * Create a new inferred type with the given name
	 * 
	 * @param className
	 *            the name of the inferred type
	 * @param isDefinition
	 *            true if this unit defines the type
	 * @return new Inferred type
	 */
	protected InferredType addType(char[] className, boolean isDefinition) {		
		InferredType type = compUnit.addType(className, isDefinition, this.inferenceProvider.getID());

		return type;
	}

	protected final void pushContext() {
		Context newContext = new Context(currentContext);
		contexts[++contextPtr] = currentContext;
		currentContext = newContext;

	}

	protected final void popContext() {
		currentContext = contexts[contextPtr];
		contexts[contextPtr--] = null;
	}
	

	/**
	 * @deprecated not used internally, will be removed
	 */
	protected final boolean isInNamedMethod() {
		return this.currentContext.currentMethod != null && this.currentContext.currentMethod.getName() != null;
	}

	/**
	 * Finds a Var Declaration on the context from the name represented with the expression
	 */
	protected IAbstractVariableDeclaration getVariable(IExpression expression) {
		char[] name = null;

		if(expression instanceof ISingleNameReference) {
			name = ((ISingleNameReference) expression).getToken();
		} else if(expression instanceof IFieldReference) {
			IReference ref = getRoot((IFieldReference)expression);
			if(ref != null && ref instanceof SingleNameReference) {
				name = ((SingleNameReference)ref).getToken();
			}
		}
		
		if(name != null) {
			Object var = this.currentContext.getMember(name);
			if(var instanceof IAbstractVariableDeclaration) {
				return (IAbstractVariableDeclaration) var;
			}

		}
		return null;

	}

	/**
	 * Finds a assignment on the context from the name represented with the expression
	 */
	protected IAssignment getAssignment(IExpression expression) {
		char[] name = null;

		if(expression instanceof ISingleNameReference) {
			name = ((ISingleNameReference) expression).getToken();
		} else if(expression instanceof IFieldReference) {
			IReference ref = getRoot((IFieldReference)expression);
			if(ref != null && ref instanceof SingleNameReference) {
				name = ((SingleNameReference)ref).getToken();
			}
		}
		
		if(name != null) {
			Object assignment = this.currentContext.getMember(name);
			if(assignment instanceof IAssignment) {
				return (IAssignment) assignment;
			}
		}
		
		return null;
	}

	/**
	 * <p>
	 * Finds a Function Declaration on the context from the name represented
	 * with the expression
	 * </p>
	 * 
	 * <p>
	 * Supported:
	 * <ul>
	 * <li>{@link ISingleNameReference}</li>
	 * <li>{@link ILocalDeclaration}</li>
	 * </ul>
	 * </p>
	 */
	protected IAbstractFunctionDeclaration getFunction(IExpression expression) {
		IAbstractFunctionDeclaration function = null;
		
		char[] name = null;
		if(expression instanceof ISingleNameReference) {
			name = ((ISingleNameReference) expression).getToken();
			
			Object method = this.currentContext.getMember(name);
			if(method instanceof IAbstractFunctionDeclaration) {
				function = (IAbstractFunctionDeclaration) method;
			} else if(method instanceof ILocalDeclaration) {
				IExpression init = ((ILocalDeclaration) method).getInitialization();
				if(init instanceof IFunctionExpression) {
					function = ((IFunctionExpression)init).getMethodDeclaration();
				}
			}
		} else if(expression instanceof IFieldReference) {
			InferredType receiverType = this.getReceiverType(((IFieldReference) expression).getReceiver(), false);
			if(receiverType != null) {
				InferredMethod inferredMethod = receiverType.findMethod(((IFieldReference) expression).getToken(), null);
				if(inferredMethod != null) {
					function = inferredMethod.getFunctionDeclaration();
				}
			}
			name = ((IFieldReference) expression).getToken();
		} else if(expression instanceof MessageSend) {
			name = ((MessageSend)expression).selector;
			if(name != null) {
				// look up the function in the current context, we want to get
				// to its return statement eventually.
				Object method = this.currentContext.getMember(name);
				if(method instanceof IAbstractFunctionDeclaration) {
					IAbstractFunctionDeclaration calledFunction = (IAbstractFunctionDeclaration) method;
					if(calledFunction.getStatements() != null) {
						IProgramElement[] statements = calledFunction.getStatements();
						// this is a bit of a guess, but look for a return statement
						// if found and it is returning a function, then use that
						// if not we just return nothing, no harm done.
						for(int i = 0; i < statements.length; i++) {
							if(statements[i] instanceof IReturnStatement) {
								if(((IReturnStatement)statements[i]).getExpression() instanceof IFunctionExpression) {
									function = ((IFunctionExpression) ((IReturnStatement)statements[i]).getExpression()).getMethodDeclaration();
									break;
								}
							}
						}
					}
				}
			}
		}
		
		return function;
	}

	private void buildDefinedMembers(IProgramElement[] statements, IArgument[] arguments) {

		if(arguments != null) {
			for(int i = 0; i < arguments.length; i++) {
				this.currentContext.addMember(arguments[i].getName(), arguments[i]);
			}
		}
		if(statements != null) {
			for(int i = 0; i < statements.length; i++) {
				if(statements[i] instanceof ILocalDeclaration) {
					ILocalDeclaration local = (ILocalDeclaration) statements[i];
					this.currentContext.addMember(local.getName(), local);
				} else if(statements[i] instanceof IAbstractFunctionDeclaration) {
					IAbstractFunctionDeclaration method = (IAbstractFunctionDeclaration) statements[i];
					if(method.getName() != null)
						this.currentContext.addMember(method.getName(), method);
				}
			}
		}
	}

	private static boolean isThis(IExpression expression) {
		return expression instanceof ASTNode && ((ASTNode)expression).isThis();
	}

	/* This method is used to determined the inferred type of a LHS Expression.
	 * 
	 * It could return null.
	 * 
	 * a.b.c */
	private InferredType getInferredType(IExpression expression) {

		InferredType type = null;

		/* this */
		if(expression instanceof IThisReference) {
			type = this.currentContext.currentType;
		}
		/* foo (could be a Type name or a reference to a variable) */
		else if(expression instanceof SingleNameReference) {
			//check for local variable first
			IAbstractVariableDeclaration varDecl = this.getVariable(expression);
			if(varDecl != null) {
				type = varDecl.getInferredType();
			} 
			
			//if not found type yet check for assignment 
			IAssignment assignment = null;
			if(type == null) {
				assignment = getAssignment(expression);
				if(assignment != null) {
					type = assignment.getInferredType();
				}
			}
			
			/* if not found type yet and there was no declaration or assignment,
			 * or the found type is Function then check if there is a known type
			 * with the same name as the SNR. */
			if((type == null && varDecl == null && assignment == null) ||
						(type != null && type.isFunction())) {
				
				char[] possibleTypeName = constructTypeName(expression);
				if(possibleTypeName != null) {
					/* if pre-existing type, use that
					 * else check if known type name */
					InferredType existingType = compUnit.findInferredType(possibleTypeName);
					if(existingType != null) {
						type = existingType;
					} else if(WellKnownTypes.containsKey(possibleTypeName) ||
								this.isKnownType(possibleTypeName)) {
						
						type = addType(possibleTypeName, false);
					}
				}
			}
		}
		/* foo.bar.xxx... */
		else if(expression instanceof FieldReference) {
			char[] possibleTypeName = constructTypeName(expression);

			if(possibleTypeName != null) {
				// search the defined types in the context
				type = compUnit.findInferredType(possibleTypeName);
			}

			if(type == null && isPossibleClassName(possibleTypeName)) {
				type = addType(possibleTypeName, true);
			}

			/* Continue the search by trying to resolve further down the name
			 * because this token of the field reference could be a member of a
			 * type or instance of a type */
			if(type == null) {
				FieldReference fRef = (FieldReference) expression;

				// this
				InferredType parentType = getInferredType(fRef.receiver);

				if(parentType != null) {
					// check the members and return type
					InferredAttribute typeAttribute = parentType.findAttribute(fRef.token);

					if(typeAttribute != null) {
						type = typeAttribute.type;
					}
				}
			}

		}

		return type;
	}

	/**
	 * @deprecated - here for compatibility
	 */
	protected InferredType getInferredType2(IExpression fieldReceiver) {
		InferredType receiverType = null;
		IAbstractVariableDeclaration var = getVariable(fieldReceiver);
		if(var != null) {
			receiverType = createAnonymousType(var);
		} else {
			if(this.inferredGlobal != null && fieldReceiver instanceof ISingleNameReference) {
				char[] name = ((ISingleNameReference) fieldReceiver).getToken();
				InferredAttribute attr = this.inferredGlobal.findAttribute(name);
				if(attr != null) {
					receiverType = attr.type;
				}
			}

		}
		return receiverType;
	}

	protected boolean isKnownType(char[] possibleTypeName) {
		return false;
	}

	/* For SNR it returns the name
	 * For FR it construct a Qualified name separated by '.'
	 * 
	 * If at any point it hits a portion of the Field reference that is
	 * not supported (such as a function call, a prototype, or this ) */
	protected final char[] constructTypeName(IExpression expression) {
		return Util.getTypeName(expression);
	}

	public boolean visit(IObjectLiteral literal) {
		if(this.passNumber == 1 && literal.getInferredType() == null) {
			createAnonymousType(literal);
		}
		pushContext();
		this.currentContext.currentType = literal.getInferredType();
		return true;
	}

	public void endVisit(IObjectLiteral literal) {
		popContext();
	}

	/**
	 * Overridden by client who wish to update the infer options
	 * 
	 * @param options
	 */
	public void initializeOptions(InferOptions options) {
	}

	protected boolean isPossibleClassName(char[] name) {
		return false;
	}

	/**
	 * Get the Script file this inference is being done on
	 * 
	 * @return
	 */
	public IScriptFileDeclaration getScriptFileDeclaration() {
		return this.compUnit;
	}

	public InferredType findDefinedType(char[] className) {
		return compUnit.findInferredType(className);
	}
	
	protected Object findDefinedMember(char[] memberName) {
		return currentContext.getMember(memberName);
	}

	protected char[] changePrimitiveToObject(char[] name) {
		/*
		 * Changes the first character of the name of the primitive types to
		 * uppercase. This will allow future reference to the object wrapper
		 * instead of the primitive type.
		 */
		if(CharOperation.equals(name, TypeConstants.BOOLEAN, false)) {
			return this.getBooleanType().getName();
		}
		
		return name;
	}
	
	/**
	 * @return {@link InferredType} for the String type
	 */
	protected InferredType getStringType() {
		if(fStringType == null) {
			fStringType = this.addType(TypeConstants.JAVA_LANG_STRING[0]);
		}
		
		return fStringType;
	}
	
	/**
	 * @return {@link InferredType} for the Number type
	 */
	protected InferredType getNumberType() {
		if(fNumberType == null) {
			fNumberType = this.addType(TypeConstants.NUMBER[0]);
		}
		
		return fNumberType;
	}
	
	/**
	 * @return {@link InferredType} for the Boolean type
	 */
	protected InferredType getBooleanType() {
		if (fBooleanType == null) {
			fBooleanType = this.addType(TypeConstants.BOOLEAN_OBJECT[0]);
		}
		
		return fBooleanType;
	}
	
	/**
	 * @return {@link InferredType} for the Function type
	 */
	protected InferredType getFunctionType() {
		if(fFunctionType == null) {
			fFunctionType = this.addType(TypeConstants.FUNCTION[0]);
		}
		
		return fFunctionType;
	}
	
	/**
	 * @return {@link InferredType} for the Array type
	 */
	protected InferredType getArrayType() {
		if(fArrayType == null) {
			fArrayType = this.addType(TypeConstants.ARRAY[0]);
		}
		
		return fArrayType;
	}
	
	/**
	 * @return {@link InferredType} for the Void type
	 */
	protected InferredType getVoidType() {
		if(fVoidType == null) {
			fVoidType = this.addType(TypeConstants.VOID);
		}
		
		return fVoidType;
	}
	
	/**
	 * @return {@link InferredType} for the Object type
	 */
	protected InferredType getObjectType() {
		if(fObjectType == null) {
			fObjectType = this.addType(TypeConstants.OBJECT);
		}
		
		return fObjectType;
	}

	/**
	 * <p>
	 * Gets the name of the given expression.
	 * </p>
	 * 
	 * <p>
	 * Supported:
	 * <ul>
	 * <li>{@link ISingleNameReference}</li>
	 * <li>{@link IFieldReference}</li>
	 * <li>{@link IAssignment}</li>
	 * </ul>
	 * </p>
	 * 
	 * @param expression
	 *            {@link IExpression} to get the name for
	 * 
	 * @return name of the given {@link IExpression} or <code>null</code> if none
	 *         can be determined
	 */
	protected char[] getName(IExpression expression) {
		char[] name = null;

		if(expression instanceof ISingleNameReference) {
			name = ((ISingleNameReference) expression).getToken();
		} else if(expression instanceof IFieldReference) {
			name = ((IFieldReference) expression).getToken();
		} else if(expression instanceof IAssignment) {
			name = this.getName(((IAssignment) expression).getLeftHandSide());
		}

		return name;
	}

	/**
	 * <p>
	 * Given a variable name and an initialization expression determines the
	 * type to initialize the variable with the given name to.
	 * </p>
	 * 
	 * @param variableName
	 *            name of the variable to be initialized
	 * @param initialization
	 *            the initialization expression used to initialize the
	 *            variable with the given name
	 * 
	 * @return {@link InferredType} to initialize the variable with the given
	 *         name with
	 */
	private InferredType getTypeForVariableInitialization(char[] variableName, IExpression initialization) {
		InferredType type = this.getTypeOf(initialization);

		/* if the initialization type is not indexed and the variable declaration is in the global scope
		 * rename the type to a globally anonymous type and mark it as a global type
		 */
		boolean isGlobal = this.isGlobal(variableName);
		if(type != null && !type.isIndexed() && isGlobal) {
				char[] globalTypeName = createAnonymousGlobalTypeName(variableName);
				this.renameType(type, globalTypeName);
				type.isAnonymous = true;
				type.setIsGlobal(true);
		} else if(type == null && isGlobal && this.passNumber == 2) {
			type = this.createAnonymousGlobalType(variableName);
		}

		return type;
	}
	
	/**
	 * <p>
	 * Will create a type to assign to given the receiver that is being assigned to, the current
	 * type that is or will be assigned to the receiver, and the assignment expression that will be
	 * used for the assignment.
	 * </p>
	 * 
	 * <p>
	 * <b>NOTE:</b> This does not actually deal with the assignment, it just creates the type to assign
	 * to and updates the type for the given receiver.
	 * </p>
	 * 
	 * @param receiver
	 *            the receiver to update the type for
	 * @param currentReceiverType
	 *            current type that is or will be assigned to the receiver
	 * 
	 * @return {@link InferredType} that was created to do the assignment to or the given current
	 *         receiver type if no new type was created
	 */
	private InferredType createTypeToAssignTo(IExpression receiver, InferredType currentReceiverType) {
		char[] varName = this.getName(receiver);
		InferredType newType = null;
		
		/* If the current receiver type is not anonymous and is not a this statement,
		 * and the receiver is not a type (rather then instance of a type, then
		 * create a new anonymous type with current type as the parent.
		 * 
		 * else use the given current receiver type */
		if(!currentReceiverType.isAnonymous && !isThis(receiver)
				&& !CharOperation.equals(currentReceiverType.name, varName) && !this.isExpressionAType(receiver)) {
			
			/* if the variable being assigned to is in the global scope then
			 * need to create a global type
			 * 
			 * else create a local anonymous type
			 */
			boolean isGlobal = this.isGlobal(varName);
			if(isGlobal) {
				/* if current type is a function create a new type with the var name
				 * else create new anonymous global type */
				if(currentReceiverType.isFunction()) {
					newType = this.addType(varName, true);
				} else {
					newType = this.createAnonymousGlobalType(varName);
				}
			} else {
				newType = this.createAnonymousType(receiver, currentReceiverType);
			}
		}

		/* if created a new type set its super type to the original type and update the type for the receiver
		 * 
		 * else just return the original given current receiver type */
		if(newType != null) {
			newType.setSuperType(currentReceiverType);
			this.setTypeOf(receiver, newType);
		} else {
			newType = currentReceiverType;
		}
		
		return newType;
	}
	
	/**
	 * <p>
	 * Clears out all of the built in types.
	 * </p>
	 */
	private void clearBuiltInTypes() {
		fStringType = null;
		fNumberType = null;
		fBooleanType = null;
		fFunctionType = null;
		fArrayType = null;
		fVoidType = null;
		fObjectType = null;
	}
	
	/**
	 * <p>
	 * This method is intended to take a chain of field references and
	 * determine the type that the last field should be or is defined on.
	 * </p>
	 * 
	 * <p>
	 * EX: <code>foo.bar.awesome.crazy = 42;</code><br>
	 * <br>
	 * If that is the entirety of the file and the receiver of the
	 * <code>foo.bar.awesome.crazy</code> statement is given to this function,
	 * so the <code>foo.bar.awesome</code> part, then this function will
	 * create a <code>foo</code> field on the global inferred type and and
	 * then give it a type that has a <code>bar</code> field, and then give
	 * the <code>bar</code> field a type with an <code>awesome</code> field
	 * and then finally return a new type assigned to the <code>awesome</code>
	 * field such that some other code can deal with assigning the
	 * <code>crazy</code> field with whatever type is on the right hand side
	 * of the assignment.
	 * </p>
	 * 
	 * @param receiver
	 *            the receiver side of a {@link FieldReference} to get the
	 *            type for
	 * 
	 * @param defineRoot
	 *            Has two purposes. If the root of the field reference has no
	 *            type and this is <code>true</code> a type will be created.
	 *            If the root of this field reference does not have a type and
	 *            this is <code>false</code> no root type will be created an
	 *            thus no type will be returned by this method. When there is
	 *            a root type if this argument is <code>true</code> then the
	 *            type on the root will be marked as a definition, else if
	 *            <code>false</code> the root will not be marked as a
	 *            definition.
	 * 
	 * @return {@link InferredType} associated with the given receiver side of
	 *         a {@link FieldReference}
	 */
	protected InferredType getReceiverType(IExpression receiver, boolean defineRoot) {
		InferredType receiverType = null;
		IExpression current = receiver;
		List recievers = new ArrayList();
		InferredType rootType = null;
		
		/* Find the SingleNameReference that the reference chain starts with
		 * or the root type if the chain starts with a THIS statement */
		while(current != null && !(current instanceof SingleNameReference) && rootType == null) {
			if(current instanceof FieldReference) {
				recievers.add(current);
				current = ((FieldReference) current).getReceiver();
			} else if(current instanceof ThisReference) {
				rootType = this.currentContext.currentType;
				((ThisReference) current).setInferredType(rootType);
				current = null;
			} else {
				current = null;
			}
		}
		
		//if the root was a single name then determine or create its type
		IAbstractVariableDeclaration rootVarDecl = null;
		IAssignment rootAssignment = null;
		if(rootType == null && current instanceof SingleNameReference) {
			
			//first check if there is a type with the same name as the single name reference
			char[] name = ((SingleNameReference) current).getToken();
			rootType = this.findDefinedType(name);
			
			// if the single name reference is a function then create a type using the function name
			if(rootType == null) {
				IFunctionDeclaration func = this.getDefinedFunction(current);
				if(func != null) {
					rootType = this.addType(name, true);
					rootType.setNameStart(current.sourceStart());
					rootType.setSourceEnd(current.sourceEnd());
				}
			}
			
			// if single name reference is to a variable
			if(rootType == null) {
				rootVarDecl = this.getVariable(current);
				if(rootVarDecl != null) {
					rootType = rootVarDecl.getInferredType();
					
					/* if variable does not already have an inferred type create an anonymous one
					 * or if the receiver type is not anonymous and is the expression is not a type then
					 * create a new anonymous sub type to do the assignment to */
					
					if(rootType == null || (!rootType.isAnonymous && !rootVarDecl.isType())) {
						/* if global then create anonymous global type and set super type as current root type
						 * else create anonymous type using current root type as super type */
						boolean isGlobal = this.isGlobal(name);
						if(isGlobal) {
							InferredType globalType = this.createAnonymousGlobalType(name);
							globalType.setSuperType(rootType);
							rootType = globalType;
							rootType.setIsDefinition(true);
							rootVarDecl.setInferredType(rootType);
						} else if(defineRoot) {
							rootType = this.createAnonymousType(current, rootType);
							rootType.setIsDefinition(true);
							rootVarDecl.setInferredType(rootType);
						}
					}
				}
			}
			
			//if single name reference is to a field on the inferred 'global' type
			if(rootType == null && this.inferredGlobal != null) {
				InferredAttribute attr = this.getInferredGlobal(false).findAttribute(name);
				if(attr != null) {
					if(attr.type == null) {
						attr.type = this.createAnonymousGlobalType(attr.name);
					}
					rootType = attr.type;
				} else {
					InferredMethod meth = this.getInferredGlobal(false).findMethod(name, null);
					if(meth != null) {
						rootType = this.addType(name, true);
						rootType.setNameStart(current.sourceStart());
						rootType.setSourceEnd(current.sourceEnd());
					}
				}
			}
			
			// if single name reference is to an existing assignment with no declaration
			if(rootType == null) {
				rootAssignment = this.getAssignment(current);
				if(rootAssignment != null) {
					rootType = rootAssignment.getInferredType();
				}
			}
			
			/* else if define root create a new anonymous global type */
			if(rootType == null && defineRoot) {
				rootType = this.createAnonymousGlobalType(name);
			}
		}
		
		/* if determined a root type, and there is no case where we should not,
		 * make sure there is fields and types built up for the rest of the reference chain */
		if(rootType != null) {
			/* only define root if requested and root not part of long chain,
			 * or the root was already defined somewhere else in this file
			 * 
			 * IE: foo.bar.blarg = 42, foo is not defined here
			 * 	   foo.bar = 42, foo is defined here */
			if(defineRoot && (recievers.isEmpty() || rootVarDecl != null || rootAssignment != null)) {
				rootType.setIsDefinition(true);
			}
			
			InferredType currentType = rootType;
			for(int i = recievers.size()-1; i >= 0; --i) {
				FieldReference ref = (FieldReference)recievers.get(i);
				
				InferredAttribute attr = currentType.findAttribute(ref.getToken());
				if(attr != null) {
					if(attr.type != null) {
						currentType = attr.type;
					} else {
						attr.type  = this.createAnonymousType(attr, null);
						currentType = attr.type;
					}
				} else {
					InferredMethod meth = currentType.findMethod(ref.getToken(), null);
					if(meth != null) {
						char[] typeName = CharOperation.concatWith(ref.asQualifiedName(), '.');
						currentType = this.addType(typeName, true);
					} else {
						attr = currentType.addAttribute(ref.getToken(), ref, ref.sourceStart);
						
						if(currentType == this.getInferredGlobal(false)) {
							attr.type = this.createAnonymousGlobalType(attr.name);
							attr.type.setIsDefinition(true);
						} else {
							attr.type = this.createAnonymousType(attr, null);
						}
						currentType = attr.type;
					}
				}
			}
			
			//set the last type in the chain as the receiver type
			receiverType = currentType;
		}
		
		return receiverType;
	}
	
	/**
	 * @param define
	 *            <code>true</code> to define the inferred global type if one
	 *            is not yet defined, <code>false</code> otherwise
	 * 
	 * @return inferred global type, or <code>null</code> if none is yet
	 *         defined and <code>define</code> was given as <code>false</code>
	 */
	protected InferredType getInferredGlobal(boolean define) {
		if(this.inferredGlobal == null && define) {
			this.inferredGlobal = addType(IIndexConstants.GLOBAL_SYMBOL, true);
			this.inferredGlobal.isAnonymous = true;
			this.inferredGlobal.setIsGlobal(true);
		}
		
		return this.inferredGlobal;
	}
	
	/**
	 * <p>
	 * Determine if the root of the given expression is global or not.
	 * </p>
	 * 
	 * @param expr
	 *            Determine if the root of this expression is global or not
	 * 
	 * @return <code>true</code> if the root of the given expression is
	 *         global, <code>false</code> otherwise
	 */
	private boolean isRootGlobal(IExpression expr) {
		boolean isGlobal = false;
		
		IReference root = null;
		if(expr instanceof SingleNameReference) {
			root = (SingleNameReference) expr;
		} else if(expr instanceof FieldReference) {
			root = getRoot((FieldReference)expr);
		}
		
		/* if root is a single name reference then determine if it is global
		 * else if no root then assume global */
		if(root != null && root instanceof ISingleNameReference) {
			isGlobal = this.isGlobal(((ISingleNameReference)root).getToken());
		} else if(root != null && root instanceof IThisReference) {
			isGlobal = this.currentContext.currentType == this.getInferredGlobal(false);
		} else if(root == null){
			isGlobal = true;
		}
		
		return isGlobal;
	}
	
	/**
	 * <p>
	 * Determines if the given variable name is global.
	 * </p>
	 * 
	 * @param name
	 *            determine if there is a global variable with this name
	 * 
	 * @return <code>true</code> if there is a global variable with this name,
	 *         <code>false</code> otherwise
	 */
	protected boolean isGlobal(char[] name) {
		boolean isGlobal = false;
		
		if (name == null)
			return isGlobal;
		
		//check the root context
		Object globalMember;
		Object currentContextMember = this.currentContext.getMember(name);
		if(this.contexts[0] != null) {
			globalMember = this.contexts[0].getMember(name);
		} else {
			globalMember = currentContextMember;
		}
		
		/* is global if global member with same name is not null and from the current context
		 * the first member with that name is also the global member, this is to cover the case
		 * with a shadowing local variable */
		isGlobal = globalMember != null && currentContextMember == globalMember;
		
		/* if not determined to be global then check if the current context member
		 * is an assignment. If it is then that assignment is an assignment without
		 * a local declaration which means it is global */
		if(!isGlobal) {
			isGlobal = currentContextMember instanceof Assignment;
		}
		
		//if not determined to be global yet then check inferred global type
		if(!isGlobal) {
			globalMember = null;
			InferredType inferredGlobal = this.getInferredGlobal(false);
			if(inferredGlobal != null) {
				globalMember = inferredGlobal.findAttribute(name);
				
				if(globalMember == null) {
					globalMember = inferredGlobal.findMethod(name, null);
				}
			}
			
			isGlobal = globalMember != null;
		}
		
		/* if not determined to be global yet and no global member or
		 * local member found assume to be global */
		if(!isGlobal) {
			if(globalMember == null && currentContextMember == null) {
				isGlobal = true;
			}
		}
		
		return isGlobal;
	}
	
	/**
	 * <p>
	 * Given an {@link IFieldReference} finds the root
	 * {@link SingleNameReference}.
	 * </p>
	 * 
	 * @param ref
	 *            {@link IFieldReference} to find the root
	 *            {@link SingleNameReference} for
	 * 
	 * @return {@link SingleNameReference} that is the root of the given
	 *         {@link IFieldReference}, or <code>null</code> if it can not be
	 *         found.
	 */
	private static IReference getRoot(IFieldReference ref) {
		IReference root = null;
		
		IExpression current = ref;
		while(root == null && current != null) {
			if(current instanceof IFieldReference) {
				current = ((IFieldReference) current).getReceiver();
			} else if(current instanceof SingleNameReference) {
				root = (SingleNameReference)current;
			} else if(current instanceof IThisReference) {
				root = (IThisReference)current;
			} else {
				current = null;
			}
		}
		
		return root;
	}
	
	/**
	 * <p>
	 * Determines if the given parent expression contains or is the given
	 * needle expression.
	 * </p>
	 * 
	 * <p>
	 * Handles:
	 * <ul>
	 * <li>{@link IBinaryExpression}</li>
	 * </ul>
	 * </p>
	 * 
	 * @param parent
	 *            determine if the given needle is or is contained by this
	 *            parent expression
	 * @param needle
	 *            determine if the this needle is or is contained by the given
	 *            parent expression
	 * 
	 * @return <code>true</code> if the given needle is or is contained by the
	 *         given parent expression, <code>false</code> otherwise
	 */
	private static boolean expressionContains(IExpression parent, IExpression needle) {
		boolean contains = false;
		
		LinkedList expressions = new LinkedList();
		expressions.add(parent);
		
		while(expressions.size() > 0 && !contains) {
			IExpression current = (IExpression)expressions.removeFirst();
			
			contains = needle == current;
			
			if(!contains) {
				if(current instanceof IBinaryExpression) {
					expressions.add(((IBinaryExpression) current).getLeft());
					expressions.add(((IBinaryExpression) current).getRight());
				} else if(current instanceof Assignment) {
					expressions.add(((Assignment) current).getLeftHandSide());
					expressions.add(((Assignment) current).getExpression());
				}
			}
		}
		
		return contains;
	}
	
	/**
	 * <p>
	 * Determines if the given expression is a type rather then an instance of
	 * a type.
	 * </p>
	 * 
	 * @param expr
	 *            determine if this expression is a type rather then an
	 *            instance of a type
	 * 
	 * @return <code>true</code> if the given expression is a type,
	 *         <code>false</code> if the given expression is the instance of a
	 *         type or unknown
	 */
	private boolean isExpressionAType(IExpression expr) {
		boolean isType = false;
		
		if(expr instanceof IAssignment) {
			isType = ((IAssignment) expr).isType();
		} else if(expr instanceof IAbstractVariableDeclaration) {
			isType = ((IAbstractVariableDeclaration) expr).isType();
		} else if(expr instanceof IFieldReference) {
			IExpression receiver = ((IFieldReference) expr).getReceiver();
			InferredType receiverType = this.getTypeOf(receiver);
			if(receiverType != null) {
				InferredAttribute attr = receiverType.findAttribute(((IFieldReference) expr).getToken());
				isType = attr != null && attr.isType();
			}
		} else if(expr instanceof ISingleNameReference) {
			IAbstractVariableDeclaration varDecl = this.getVariable(expr);
			if(varDecl != null) {
				isType = varDecl.isType();
			}
			
			if(!isType) {
				IAssignment assign = this.getAssignment(expr);
				if(assign != null) {
					isType = assign.isType();
				}
			}
			
			if(!isType) {
				IAbstractFunctionDeclaration funcDecl = this.getFunction(expr);
				if(funcDecl != null && funcDecl.getName() != null) {
					InferredType typeDefinedByFunc = this.findDefinedType(funcDecl.getName());
					isType = typeDefinedByFunc != null;
				}
			}
			
			if(!isType) {
				InferredType existingType =this.compUnit.findInferredType(((ISingleNameReference) expr).getToken());
				isType = existingType != null;
			}
		} else if(expr instanceof IThisReference) {
			isType = true;
		}
		
		return isType;
	}

	protected InferredType createAnonymousTypeForMixin(IExpression mixInto, InferredType parentType) {
		InferredType mixIntoType;
		IAbstractVariableDeclaration localVar = getVariable(mixInto);
		char[] varName = getName(mixInto);
		if (mixInto instanceof ISingleNameReference && (localVar == null || isGlobal(varName))) {
			mixIntoType = createAnonymousGlobalType(varName);
			mixIntoType.setSuperType(parentType);
		}
		else {
			mixIntoType = createAnonymousType(mixInto, parentType);
		}
		mixIntoType.isObjectLiteral = false;
		return mixIntoType;
	}

	protected InferredType getAttributeType(char[] attName, IExpression receiver, boolean defineRoot) {
		InferredType attrType = null;
		InferredType receiverType = this.getReceiverType(receiver, defineRoot);
		if (receiverType != null) {
			InferredAttribute attr = receiverType.findAttribute(attName);
			if (attr != null && attr.type != null) {
				attrType = attr.type;
			}
		}
		return attrType;
	}
	
	/**
	 * Return a visitor to traverse the given compilation unit's AST.
	 * Subclasses may override to provide a more minimal implementations while
	 * retaining use of utility methods from the base InferEngine.
	 * 
	 * @return a visitor for use with the given compilation unit and current
	 *         engine states, or <code>null</code>
	 */
	protected ASTVisitor getVisitor(CompilationUnitDeclaration compilationUnit) {
		return this;
	}
}

Back to the top