summaryrefslogtreecommitdiff
path: root/src/ToolBox/SOS/Strike/ExpressionNode.cpp
blob: 6516823aaa3f34153c28098f2dfdff82c7b22fe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include "ExpressionNode.h"


#ifndef IfFailRet
#define IfFailRet(EXPR) do { Status = (EXPR); if(FAILED(Status)) { return (Status); } } while (0)
#endif

// Returns the complete expression being evaluated to get the value for this node
// The returned pointer is a string interior to this object - once you release
// all references to this object the string is invalid.
WCHAR* ExpressionNode::GetAbsoluteExpression() { return pAbsoluteExpression; }

// Returns the sub expression that logically indicates how the parent expression
// was built upon to reach this node. This relative value has no purpose other
// than an identifier and to convey UI meaning to the user. At present typical values
// are the name of type, a local, a parameter, a field, an array index, or '<basetype>'
// for a baseclass casting operation
// The returned pointer is a string interior to this object - once you release
// all references to this object the string is invalid.
WCHAR* ExpressionNode::GetRelativeExpression() { return pRelativeExpression; }

// Returns a text representation of the type of value that this node refers to
// It is possible this node doesn't evaluate to anything and therefore has no
// type
// The returned pointer is a string interior to this object - once you release
// all references to this object the string is invalid.
WCHAR* ExpressionNode::GetTypeName() { PopulateType(); return pTypeName; }

// Returns a text representation of the value for this node. It is possible that
// this node doesn't evaluate to anything and therefore has no value text.
// The returned pointer is a string interior to this object - once you release
// all references to this object the string is invalid.
WCHAR* ExpressionNode::GetTextValue() { PopulateTextValue(); return pTextValue; }

// If there is any error during the evaluation of this node's expression, it is
// returned here.
// The returned pointer is a string interior to this object - once you release
// all references to this object the string is invalid.
WCHAR* ExpressionNode::GetErrorMessage() { return pErrorMessage; }

// Factory function for creating the expression node at the root of a tree
HRESULT ExpressionNode::CreateExpressionNode(__in_z WCHAR* pExpression, ExpressionNode** ppExpressionNode)
{
    *ppExpressionNode = NULL;
    HRESULT Status = CreateExpressionNodeHelper(pExpression,
        pExpression,
        0,
        NULL,
        NULL,
        NULL,
        0,
        NULL,
        ppExpressionNode);
    if(FAILED(Status) && *ppExpressionNode == NULL)
    {

        WCHAR pErrorMessage[MAX_ERROR];
        _snwprintf_s(pErrorMessage, MAX_ERROR, _TRUNCATE, L"Error 0x%x while parsing expression", Status);
        *ppExpressionNode = new ExpressionNode(pExpression, pErrorMessage);
        Status = S_OK;
        if(*ppExpressionNode == NULL)
            Status = E_OUTOFMEMORY;
    }
    return Status;
}

// Performs recursive expansion within the tree for nodes that are along the path to varToExpand.
// Expansion involves calulating a set of child expressions from the current expression via
// field dereferencing, array index dereferencing, or casting to a base type.
// For example if a tree was rooted with expression 'foo.bar' and varToExpand is '(Baz)foo.bar[9]'
// then 'foo.bar', 'foo.bar[9]', and '(Baz)foo.bar[9]' nodes would all be expanded.
HRESULT ExpressionNode::Expand(__in_z WCHAR* varToExpand)
{
    if(!ShouldExpandVariable(varToExpand))
        return S_FALSE;
    if(pValue == NULL && pTypeCast == NULL)
        return S_OK;

    // if the node evaluates to a type, then the children are static fields of the type
    if(pValue == NULL)
        return ExpandFields(NULL, varToExpand);

    // If the value is a null reference there is nothing to expand
    HRESULT Status = S_OK;
    BOOL isNull = TRUE;
    ToRelease<ICorDebugValue> pInnerValue;
    IfFailRet(DereferenceAndUnboxValue(pValue, &pInnerValue, &isNull));
    if(isNull)
    {
        return S_OK;
    }


    CorElementType corElemType;
    IfFailRet(pValue->GetType(&corElemType));
    if (corElemType == ELEMENT_TYPE_SZARRAY)
    {
        //If its an array, add children representing the indexed elements
        return ExpandSzArray(pInnerValue, varToExpand);
    }
    else if(corElemType == ELEMENT_TYPE_CLASS || corElemType == ELEMENT_TYPE_VALUETYPE)
    {
        // If its a class or struct (not counting string, array, or object) then add children representing
        // the fields.
        return ExpandFields(pInnerValue, varToExpand);
    }
    else
    {
        // nothing else expands
        return S_OK;
    }
}

// Standard depth first search tree traversal pattern with a callback
VOID ExpressionNode::DFSVisit(ExpressionNodeVisitorCallback pFunc, VOID* pUserData, int depth)
{
    pFunc(this, depth, pUserData);
    ExpressionNode* pCurChild = pChild;
    while(pCurChild != NULL)
    {
        pCurChild->DFSVisit(pFunc, pUserData, depth+1);
        pCurChild = pCurChild->pNextSibling;
    }
}


// Creates a new expression with a given debuggee value and frame
ExpressionNode::ExpressionNode(__in_z WCHAR* pExpression, ICorDebugValue* pValue, ICorDebugILFrame* pFrame)
{
    Init(pValue, NULL, pFrame);
    _snwprintf_s(pAbsoluteExpression, MAX_EXPRESSION, _TRUNCATE, L"%s", pExpression);
    _snwprintf_s(pRelativeExpression, MAX_EXPRESSION, _TRUNCATE, L"%s", pExpression);
}

// Creates a new expression that has an error and no value
ExpressionNode::ExpressionNode(__in_z WCHAR* pExpression, __in_z WCHAR* pErrorMessage)
{
    Init(NULL, NULL, NULL);
    _snwprintf_s(pAbsoluteExpression, MAX_EXPRESSION, _TRUNCATE, L"%s", pExpression);
    _snwprintf_s(pRelativeExpression, MAX_EXPRESSION, _TRUNCATE, L"%s", pExpression);
    _snwprintf_s(this->pErrorMessage, MAX_ERROR, _TRUNCATE, L"%s", pErrorMessage);
}

// Creates a new child expression
ExpressionNode::ExpressionNode(__in_z WCHAR* pParentExpression, ChildKind ck, __in_z WCHAR* pRelativeExpression, ICorDebugValue* pValue, ICorDebugType* pType, ICorDebugILFrame* pFrame, UVCP_CONSTANT pDefaultValue, ULONG cchDefaultValue)
{
    Init(pValue, pType, pFrame);
    if(ck == ChildKind_BaseClass)
    {
        _snwprintf_s(pAbsoluteExpression, MAX_EXPRESSION, _TRUNCATE, L"%s", pParentExpression);
    }
    else
    {
        _snwprintf_s(pAbsoluteExpression, MAX_EXPRESSION, _TRUNCATE, ck == ChildKind_Field ? L"%s.%s" : L"%s[%s]", pParentExpression, pRelativeExpression);
    }
    _snwprintf_s(this->pRelativeExpression, MAX_EXPRESSION, _TRUNCATE, ck == ChildKind_Index ? L"[%s]" : L"%s", pRelativeExpression);
    this->pDefaultValue = pDefaultValue;
    this->cchDefaultValue = cchDefaultValue;
}

// Common member initialization for the constructors
VOID ExpressionNode::Init(ICorDebugValue* pValue, ICorDebugType* pTypeCast, ICorDebugILFrame* pFrame)
{
    this->pValue = pValue;
    this->pTypeCast = pTypeCast;
    this->pILFrame = pFrame;
    pChild = NULL;
    pNextSibling = NULL;
    pTextValue[0] = 0;
    pErrorMessage[0] = 0;
    pAbsoluteExpression[0] = 0;
    pRelativeExpression[0] = 0;
    pTypeName[0] = 0;

    pDefaultValue = NULL;
    cchDefaultValue = 0;

    // The ToRelease holders don't automatically AddRef
    if(pILFrame != NULL)
        pILFrame->AddRef();
    if(pTypeCast != NULL)
        pTypeCast->AddRef();
    if(pValue != NULL)
    {   
        pValue->AddRef();
        PopulateMetaDataImport();
    }
}

// Retreves the correct IMetaDataImport for the type represented in this node and stores it
// in pMD.
HRESULT ExpressionNode::PopulateMetaDataImport()
{
    if(pMD != NULL)
        return S_OK;

    HRESULT Status = S_OK;
    ToRelease<ICorDebugType> pType;
    if(pTypeCast != NULL)
    {
        pType = pTypeCast;
        pType->AddRef();
    }
    else
    {
        BOOL isNull;
        ToRelease<ICorDebugValue> pInnerValue;
        IfFailRet(DereferenceAndUnboxValue(pValue, &pInnerValue, &isNull));
        ToRelease<ICorDebugValue2> pValue2;
        IfFailRet(pInnerValue->QueryInterface(IID_ICorDebugValue2, (LPVOID *) &pValue2));

        IfFailRet(pValue2->GetExactType(&pType));

        // for array, pointer, and byref types we can't directly get a class, we must unwrap first
        CorElementType et;
        IfFailRet(pType->GetType(&et));
        while(et == ELEMENT_TYPE_ARRAY || et == ELEMENT_TYPE_SZARRAY || et == ELEMENT_TYPE_BYREF || et == ELEMENT_TYPE_PTR)
        {
            pType->GetFirstTypeParameter(&pType);
            IfFailRet(pType->GetType(&et));
        }
    }
    ToRelease<ICorDebugClass> pClass;
    IfFailRet(pType->GetClass(&pClass));
    ToRelease<ICorDebugModule> pModule;
    IfFailRet(pClass->GetModule(&pModule));
    ToRelease<IUnknown> pMDUnknown;
    IfFailRet(pModule->GetMetaDataInterface(IID_IMetaDataImport, &pMDUnknown));
    IfFailRet(pMDUnknown->QueryInterface(IID_IMetaDataImport, (LPVOID*) &pMD));
    return Status;
}

// Determines the string representation of pType and stores it in typeName
HRESULT ExpressionNode::CalculateTypeName(ICorDebugType * pType, __inout_ecount(typeNameLen) WCHAR* typeName, DWORD typeNameLen)
{
    HRESULT Status = S_OK;

    CorElementType corElemType;
    IfFailRet(pType->GetType(&corElemType));

    switch (corElemType)
    {
        //List of unsupported CorElementTypes:
        //ELEMENT_TYPE_END            = 0x0,
        //ELEMENT_TYPE_VAR            = 0x13,     // a class type variable VAR <U1>
        //ELEMENT_TYPE_GENERICINST    = 0x15,     // GENERICINST <generic type> <argCnt> <arg1> ... <argn>
        //ELEMENT_TYPE_TYPEDBYREF     = 0x16,     // TYPEDREF  (it takes no args) a typed referece to some other type
        //ELEMENT_TYPE_MVAR           = 0x1e,     // a method type variable MVAR <U1>
        //ELEMENT_TYPE_CMOD_REQD      = 0x1F,     // required C modifier : E_T_CMOD_REQD <mdTypeRef/mdTypeDef>
        //ELEMENT_TYPE_CMOD_OPT       = 0x20,     // optional C modifier : E_T_CMOD_OPT <mdTypeRef/mdTypeDef>
        //ELEMENT_TYPE_INTERNAL       = 0x21,     // INTERNAL <typehandle>
        //ELEMENT_TYPE_MAX            = 0x22,     // first invalid element type
        //ELEMENT_TYPE_MODIFIER       = 0x40,
        //ELEMENT_TYPE_SENTINEL       = 0x01 | ELEMENT_TYPE_MODIFIER, // sentinel for varargs
        //ELEMENT_TYPE_PINNED         = 0x05 | ELEMENT_TYPE_MODIFIER,
    default:
        swprintf_s(typeName, typeNameLen, L"(Unhandled CorElementType: 0x%x)\0", corElemType);
        break;

    case ELEMENT_TYPE_VALUETYPE:
    case ELEMENT_TYPE_CLASS:
        {
            //Defaults in case we fail...
            if(corElemType == ELEMENT_TYPE_VALUETYPE) swprintf_s(typeName, typeNameLen, L"struct\0");
            else swprintf_s(typeName, typeNameLen, L"class\0");

            mdTypeDef typeDef;
            ToRelease<ICorDebugClass> pClass;
            if(SUCCEEDED(pType->GetClass(&pClass)) && SUCCEEDED(pClass->GetToken(&typeDef)))
            {
                ToRelease<ICorDebugModule> pModule;
                IfFailRet(pClass->GetModule(&pModule));

                ToRelease<IUnknown> pMDUnknown;
                ToRelease<IMetaDataImport> pMD;
                IfFailRet(pModule->GetMetaDataInterface(IID_IMetaDataImport, &pMDUnknown));
                IfFailRet(pMDUnknown->QueryInterface(IID_IMetaDataImport, (LPVOID*) &pMD));

                if(SUCCEEDED(NameForToken_s(TokenFromRid(typeDef, mdtTypeDef), pMD, g_mdName, mdNameLen, false)))
                    swprintf_s(typeName, typeNameLen, L"%s\0", g_mdName);
            }
            AddGenericArgs(pType, typeName, typeNameLen);
        }
        break;
    case ELEMENT_TYPE_VOID:
        swprintf_s(typeName, typeNameLen, L"void\0");
        break;
    case ELEMENT_TYPE_BOOLEAN:
        swprintf_s(typeName, typeNameLen, L"bool\0");
        break;
    case ELEMENT_TYPE_CHAR:
        swprintf_s(typeName, typeNameLen, L"char\0");
        break;
    case ELEMENT_TYPE_I1:
        swprintf_s(typeName, typeNameLen, L"signed byte\0");
        break;
    case ELEMENT_TYPE_U1:
        swprintf_s(typeName, typeNameLen, L"byte\0");
        break;
    case ELEMENT_TYPE_I2:
        swprintf_s(typeName, typeNameLen, L"short\0");
        break;
    case ELEMENT_TYPE_U2:
        swprintf_s(typeName, typeNameLen, L"unsigned short\0");
        break;    
    case ELEMENT_TYPE_I4:
        swprintf_s(typeName, typeNameLen, L"int\0");
        break;
    case ELEMENT_TYPE_U4:
        swprintf_s(typeName, typeNameLen, L"unsigned int\0");
        break;
    case ELEMENT_TYPE_I8:
        swprintf_s(typeName, typeNameLen, L"long\0");
        break;
    case ELEMENT_TYPE_U8:
        swprintf_s(typeName, typeNameLen, L"unsigned long\0");
        break;
    case ELEMENT_TYPE_R4:
        swprintf_s(typeName, typeNameLen, L"float\0");
        break;
    case ELEMENT_TYPE_R8:
        swprintf_s(typeName, typeNameLen, L"double\0");
        break;
    case ELEMENT_TYPE_OBJECT:
        swprintf_s(typeName, typeNameLen, L"object\0");
        break;
    case ELEMENT_TYPE_STRING:
        swprintf_s(typeName, typeNameLen, L"string\0");
        break;
    case ELEMENT_TYPE_I:
        swprintf_s(typeName, typeNameLen, L"IntPtr\0");
        break;
    case ELEMENT_TYPE_U:
        swprintf_s(typeName, typeNameLen, L"UIntPtr\0");
        break;
    case ELEMENT_TYPE_SZARRAY:
    case ELEMENT_TYPE_ARRAY:
    case ELEMENT_TYPE_BYREF:
    case ELEMENT_TYPE_PTR:
        {
            // get a name for the type we are building from
            ToRelease<ICorDebugType> pFirstParameter;
            if(SUCCEEDED(pType->GetFirstTypeParameter(&pFirstParameter)))
                CalculateTypeName(pFirstParameter, typeName, typeNameLen);
            else
                swprintf_s(typeName, typeNameLen, L"<unknown>\0");

            // append the appropriate [], *, &
            switch(corElemType)
            {
            case ELEMENT_TYPE_SZARRAY: 
                wcsncat_s(typeName, typeNameLen, L"[]", typeNameLen);
                return S_OK;
            case ELEMENT_TYPE_ARRAY:
                {
                    ULONG32 rank = 0;
                    pType->GetRank(&rank);
                    wcsncat_s(typeName, typeNameLen, L"[", typeNameLen);
                    for(ULONG32 i = 0; i < rank - 1; i++)
                    {
                        // todo- could we print out exact boundaries?
                        wcsncat_s(typeName, typeNameLen, L",", typeNameLen);
                    }
                    wcsncat_s(typeName, typeNameLen, L"]", typeNameLen);
                }
                return S_OK;
            case ELEMENT_TYPE_BYREF:   
                wcsncat_s(typeName, typeNameLen, L"&", typeNameLen);
                return S_OK;
            case ELEMENT_TYPE_PTR:     
                wcsncat_s(typeName, typeNameLen, L"*", typeNameLen);
                return S_OK;
            }
        }
        break;
    case ELEMENT_TYPE_FNPTR:
        swprintf_s(typeName, typeNameLen, L"*(...)");
        break;
    case ELEMENT_TYPE_TYPEDBYREF:
        swprintf_s(typeName, typeNameLen, L"typedbyref");
        break;
    }
    return S_OK;
}


// Appends angle brackets and the generic argument list to a type name
HRESULT ExpressionNode::AddGenericArgs(ICorDebugType * pType, __inout_ecount(typeNameLen) WCHAR* typeName, DWORD typeNameLen)
{
    bool isFirst = true;
    ToRelease<ICorDebugTypeEnum> pTypeEnum;
    if(SUCCEEDED(pType->EnumerateTypeParameters(&pTypeEnum)))
    {
        ULONG numTypes = 0;
        ToRelease<ICorDebugType> pCurrentTypeParam;

        while(SUCCEEDED(pTypeEnum->Next(1, &pCurrentTypeParam, &numTypes)))
        {
            if(numTypes == 0) break;

            if(isFirst)
            {
                isFirst = false;
                wcsncat_s(typeName, typeNameLen, L"<", typeNameLen);
            }
            else wcsncat_s(typeName, typeNameLen, L",", typeNameLen);

            WCHAR typeParamName[mdNameLen];
            typeParamName[0] = L'\0';
            CalculateTypeName(pCurrentTypeParam, typeParamName, mdNameLen);
            wcsncat_s(typeName, typeNameLen, typeParamName, typeNameLen);
        }
        if(!isFirst)
            wcsncat_s(typeName, typeNameLen, L">", typeNameLen);
    }

    return S_OK;
}

// Determines the text name for the type of this node and caches it
HRESULT ExpressionNode::PopulateType()
{
    HRESULT Status = S_OK;
    if(pTypeName[0] != 0)
        return S_OK;

    //default value
    swprintf_s(pTypeName, MAX_EXPRESSION, L"<unknown>");

    // if we are displaying this type as a specific sub-type, use that
    if(pTypeCast != NULL)
        return CalculateTypeName(pTypeCast, pTypeName, MAX_EXPRESSION);

    // if there is no value then either we succesfully already determined the type
    // name, or this node has no value or type and thus no type name.
    if(pValue == NULL)
        return S_OK;

    // get the type from the value and then calculate a name based on that
    ToRelease<ICorDebugType> pType;
    ToRelease<ICorDebugValue2> pValue2;
    if(SUCCEEDED(pValue->QueryInterface(IID_ICorDebugValue2, (void**) &pValue2)) && SUCCEEDED(pValue2->GetExactType(&pType)))
        return CalculateTypeName(pType, pTypeName, MAX_EXPRESSION);

    return S_OK;
}

// Node expansion helpers

// Inserts a new child at the end of the linked list of children
// PERF: This has O(N) insert time but these lists should never be large
VOID ExpressionNode::AddChild(ExpressionNode* pNewChild)
{
    if(pChild == NULL)
        pChild = pNewChild;
    else
    {
        ExpressionNode* pCursor = pChild;
        while(pCursor->pNextSibling != NULL)
            pCursor = pCursor->pNextSibling;
        pCursor->pNextSibling = pNewChild;
    }
}

// Helper that determines if the current node is on the path of nodes represented by
// expression varToExpand
BOOL ExpressionNode::ShouldExpandVariable(__in_z WCHAR* varToExpand)
{
    if(pAbsoluteExpression == NULL || varToExpand == NULL) return FALSE;

    // if there is a cast operation, move past it
    WCHAR* pEndCast = _wcschr(varToExpand, L')');
    varToExpand = (pEndCast == NULL) ? varToExpand : pEndCast+1; 

    size_t varToExpandLen = _wcslen(varToExpand);
    size_t currentExpansionLen = _wcslen(pAbsoluteExpression);
    if(currentExpansionLen > varToExpandLen) return FALSE;
    if(currentExpansionLen < varToExpandLen && 
        varToExpand[currentExpansionLen] != L'.' &&
        varToExpand[currentExpansionLen] != L'[')
        return FALSE;
    if(_wcsncmp(pAbsoluteExpression, varToExpand, currentExpansionLen) != 0) return FALSE;

    return TRUE;
}

// Expands this array node by creating child nodes with expressions refering to individual array elements
HRESULT ExpressionNode::ExpandSzArray(ICorDebugValue* pInnerValue, __in_z WCHAR* varToExpand)
{
    HRESULT Status = S_OK;
    ToRelease<ICorDebugArrayValue> pArrayValue;
    IfFailRet(pInnerValue->QueryInterface(IID_ICorDebugArrayValue, (LPVOID*) &pArrayValue));

    ULONG32 nRank;
    IfFailRet(pArrayValue->GetRank(&nRank));
    if (nRank != 1)
    {
        _snwprintf_s(pErrorMessage, MAX_ERROR, _TRUNCATE, L"Multi-dimensional arrays NYI");
        return E_UNEXPECTED;
    }

    ULONG32 cElements;
    IfFailRet(pArrayValue->GetCount(&cElements));

    //TODO: do we really want all the elements? This could be huge!
    for (ULONG32 i=0; i < cElements; i++)
    {
        WCHAR index[20];
        swprintf_s(index, 20, L"%d", i);

        ToRelease<ICorDebugValue> pElementValue;
        IfFailRet(pArrayValue->GetElementAtPosition(i, &pElementValue));
        ExpressionNode* pExpr = new ExpressionNode(pAbsoluteExpression, ChildKind_Index, index, pElementValue, NULL, pILFrame);
        AddChild(pExpr);
        pExpr->Expand(varToExpand);
    }
    return S_OK;
}

// Expands this struct/class node by creating child nodes with expressions refering to individual field values
// and one node for the basetype value
HRESULT ExpressionNode::ExpandFields(ICorDebugValue* pInnerValue, __in_z WCHAR* varToExpand)
{
    HRESULT Status = S_OK;

    mdTypeDef currentTypeDef;
    ToRelease<ICorDebugClass> pClass;
    ToRelease<ICorDebugType> pType;
    ToRelease<ICorDebugModule> pModule;
    if(pTypeCast == NULL)
    {
        ToRelease<ICorDebugValue2> pValue2;
        IfFailRet(pInnerValue->QueryInterface(IID_ICorDebugValue2, (LPVOID *) &pValue2));
        IfFailRet(pValue2->GetExactType(&pType));
    }
    else
    {
        pType = pTypeCast;
        pType->AddRef();
    }
    IfFailRet(pType->GetClass(&pClass));
    IfFailRet(pClass->GetModule(&pModule));
    IfFailRet(pClass->GetToken(&currentTypeDef));

    ToRelease<IUnknown> pMDUnknown;
    ToRelease<IMetaDataImport> pMD;
    IfFailRet(pModule->GetMetaDataInterface(IID_IMetaDataImport, &pMDUnknown));
    IfFailRet(pMDUnknown->QueryInterface(IID_IMetaDataImport, (LPVOID*) &pMD));

    // If the current type has a base type that isn't object, enum, or ValueType then add a node for the base type
    WCHAR baseTypeName[mdNameLen] = L"\0";
    ToRelease<ICorDebugType> pBaseType;
    ExpressionNode* pBaseTypeNode = NULL;
    if(SUCCEEDED(pType->GetBase(&pBaseType)) && pBaseType != NULL && SUCCEEDED(CalculateTypeName(pBaseType, baseTypeName, mdNameLen)))
    {
        if(_wcsncmp(baseTypeName, L"System.Enum", 11) == 0)
            return S_OK;
        else if(_wcsncmp(baseTypeName, L"System.Object", 13) != 0 && _wcsncmp(baseTypeName, L"System.ValueType", 16) != 0)
        {
            pBaseTypeNode = new ExpressionNode(pAbsoluteExpression, ChildKind_BaseClass, L"<baseclass>", pInnerValue, pBaseType, pILFrame);
            AddChild(pBaseTypeNode);
        }
    }

    // add nodes for all the fields in this object
    ULONG numFields = 0;
    HCORENUM fEnum = NULL;
    mdFieldDef fieldDef;
    BOOL fieldExpanded = FALSE;
    while(SUCCEEDED(pMD->EnumFields(&fEnum, currentTypeDef, &fieldDef, 1, &numFields)) && numFields != 0)
    {
        mdTypeDef         classDef = 0;
        ULONG             nameLen = 0;
        DWORD             fieldAttr = 0;
        WCHAR             mdName[mdNameLen];
        WCHAR             typeName[mdNameLen];
        CorElementType    fieldDefaultValueEt;
        UVCP_CONSTANT     pDefaultValue;
        ULONG             cchDefaultValue;
        if(SUCCEEDED(pMD->GetFieldProps(fieldDef, &classDef, mdName, mdNameLen, &nameLen, &fieldAttr, NULL, NULL, (DWORD*)&fieldDefaultValueEt, &pDefaultValue, &cchDefaultValue)))
        {
            ToRelease<ICorDebugType> pFieldType;
            ToRelease<ICorDebugValue> pFieldVal;

            // static fields (of any kind - AppDomain, thread, context, RVA)
            if (fieldAttr & fdStatic)
            {
                pType->GetStaticFieldValue(fieldDef, pILFrame, &pFieldVal);
            } 
            // non-static fields on an object instance
            else if(pInnerValue != NULL)
            {
                ToRelease<ICorDebugObjectValue> pObjValue;
                if (SUCCEEDED(pInnerValue->QueryInterface(IID_ICorDebugObjectValue, (LPVOID*) &pObjValue)))
                    pObjValue->GetFieldValue(pClass, fieldDef, &pFieldVal);
            }
            // skip over non-static fields on static types
            else
            {
                continue; 
            }

            // we didn't get a value yet and there is default value available
            // need to calculate the type because there won't be a ICorDebugValue to derive it from
            if(pFieldVal == NULL && pDefaultValue != NULL)
            {
                FindTypeFromElementType(fieldDefaultValueEt, &pFieldType);
            }

            ExpressionNode* pNewChildNode = new ExpressionNode(pAbsoluteExpression, ChildKind_Field, mdName, pFieldVal, pFieldType, pILFrame, pDefaultValue, cchDefaultValue);
            AddChild(pNewChildNode);
            if(pNewChildNode->Expand(varToExpand) != S_FALSE)
                fieldExpanded = TRUE;
        }
    }
    pMD->CloseEnum(fEnum);

    // Only recurse to expand the base type if all of these hold:
    // 1) base type exists
    // 2) no field was expanded
    // 3) the non-casting portion of the varToExpand doesn't match the current expression
    //    OR the cast exists and doesn't match

    if(pBaseTypeNode == NULL) return Status;
    if(fieldExpanded) return Status;

    WCHAR* pEndCast = _wcschr(varToExpand, L')');
    WCHAR* pNonCast = (pEndCast == NULL) ? varToExpand : pEndCast+1;
    if(_wcscmp(pNonCast, pAbsoluteExpression) != 0)
    {
        pBaseTypeNode->Expand(varToExpand);
        return Status;
    }

    if(varToExpand[0] == L'(' && pEndCast != NULL)
    {
        int cchCastTypeName = ((int)(pEndCast-1)-(int)varToExpand)/2;
        PopulateType();
        if(_wcslen(pTypeName) != (cchCastTypeName) ||
            _wcsncmp(varToExpand+1, pTypeName, cchCastTypeName) != 0)
        {
            pBaseTypeNode->Expand(varToExpand);
            return Status;
        }
    }

    return Status;
}


// Value Population functions

//Helper for unwrapping values
HRESULT ExpressionNode::DereferenceAndUnboxValue(ICorDebugValue * pInputValue, ICorDebugValue** ppOutputValue, BOOL * pIsNull)
{
    HRESULT Status = S_OK;
    *ppOutputValue = NULL;
    if(pIsNull != NULL) *pIsNull = FALSE;

    ToRelease<ICorDebugReferenceValue> pReferenceValue;
    Status = pInputValue->QueryInterface(IID_ICorDebugReferenceValue, (LPVOID*) &pReferenceValue);
    if (SUCCEEDED(Status))
    {
        BOOL isNull = FALSE;
        IfFailRet(pReferenceValue->IsNull(&isNull));
        if(!isNull)
        {
            ToRelease<ICorDebugValue> pDereferencedValue;
            IfFailRet(pReferenceValue->Dereference(&pDereferencedValue));
            return DereferenceAndUnboxValue(pDereferencedValue, ppOutputValue);
        }
        else
        {
            if(pIsNull != NULL) *pIsNull = TRUE;
            *ppOutputValue = pInputValue;
            (*ppOutputValue)->AddRef();
            return S_OK;
        }
    }

    ToRelease<ICorDebugBoxValue> pBoxedValue;
    Status = pInputValue->QueryInterface(IID_ICorDebugBoxValue, (LPVOID*) &pBoxedValue);
    if (SUCCEEDED(Status))
    {
        ToRelease<ICorDebugObjectValue> pUnboxedValue;
        IfFailRet(pBoxedValue->GetObject(&pUnboxedValue));
        return DereferenceAndUnboxValue(pUnboxedValue, ppOutputValue);
    }
    *ppOutputValue = pInputValue;
    (*ppOutputValue)->AddRef();
    return S_OK;
}

// Returns TRUE if the value derives from System.Enum
BOOL ExpressionNode::IsEnum(ICorDebugValue * pInputValue)
{
    ToRelease<ICorDebugValue> pValue;
    if(FAILED(DereferenceAndUnboxValue(pInputValue, &pValue, NULL))) return FALSE;

    WCHAR baseTypeName[mdNameLen];
    ToRelease<ICorDebugValue2> pValue2;
    ToRelease<ICorDebugType> pType;
    ToRelease<ICorDebugType> pBaseType;

    if(FAILED(pValue->QueryInterface(IID_ICorDebugValue2, (LPVOID *) &pValue2))) return FALSE;
    if(FAILED(pValue2->GetExactType(&pType))) return FALSE;
    if(FAILED(pType->GetBase(&pBaseType)) || pBaseType == NULL) return FALSE;
    if(FAILED(CalculateTypeName(pBaseType, baseTypeName, mdNameLen))) return  FALSE;

    return (_wcsncmp(baseTypeName, L"System.Enum", 11) == 0);
}

// Calculates the value text for nodes that have enum values
HRESULT ExpressionNode::PopulateEnumValue(ICorDebugValue* pEnumValue, BYTE* enumValue)
{
    HRESULT Status = S_OK;

    mdTypeDef currentTypeDef;
    ToRelease<ICorDebugClass> pClass;
    ToRelease<ICorDebugValue2> pValue2;
    ToRelease<ICorDebugType> pType;
    ToRelease<ICorDebugModule> pModule;
    IfFailRet(pEnumValue->QueryInterface(IID_ICorDebugValue2, (LPVOID *) &pValue2));
    IfFailRet(pValue2->GetExactType(&pType));
    IfFailRet(pType->GetClass(&pClass));
    IfFailRet(pClass->GetModule(&pModule));
    IfFailRet(pClass->GetToken(&currentTypeDef));

    ToRelease<IUnknown> pMDUnknown;
    ToRelease<IMetaDataImport> pMD;
    IfFailRet(pModule->GetMetaDataInterface(IID_IMetaDataImport, &pMDUnknown));
    IfFailRet(pMDUnknown->QueryInterface(IID_IMetaDataImport, (LPVOID*) &pMD));


    //First, we need to figure out the underlying enum type so that we can correctly type cast the raw values of each enum constant
    //We get that from the non-static field of the enum variable (I think the field is called __value or something similar)
    ULONG numFields = 0;
    HCORENUM fEnum = NULL;
    mdFieldDef fieldDef;
    CorElementType enumUnderlyingType = ELEMENT_TYPE_END;
    while(SUCCEEDED(pMD->EnumFields(&fEnum, currentTypeDef, &fieldDef, 1, &numFields)) && numFields != 0)
    {
        DWORD             fieldAttr = 0;
        PCCOR_SIGNATURE   pSignatureBlob = NULL;
        ULONG             sigBlobLength = 0;
        if(SUCCEEDED(pMD->GetFieldProps(fieldDef, NULL, NULL, 0, NULL, &fieldAttr, &pSignatureBlob, &sigBlobLength, NULL, NULL, NULL)))
        {
            if((fieldAttr & fdStatic) == 0)
            {
                CorSigUncompressCallingConv(pSignatureBlob);
                enumUnderlyingType = CorSigUncompressElementType(pSignatureBlob);
                break;
            }
        }
    }
    pMD->CloseEnum(fEnum);


    //Now that we know the underlying enum type, let's decode the enum variable into OR-ed, human readable enum contants
    fEnum = NULL;
    bool isFirst = true;
    ULONG64 remainingValue = *((ULONG64*)enumValue);
    WCHAR* pTextValueCursor = pTextValue;
    DWORD cchTextValueCursor = MAX_EXPRESSION;
    while(SUCCEEDED(pMD->EnumFields(&fEnum, currentTypeDef, &fieldDef, 1, &numFields)) && numFields != 0)
    {
        ULONG             nameLen = 0;
        DWORD             fieldAttr = 0;
        WCHAR             mdName[mdNameLen];
        WCHAR             typeName[mdNameLen];
        UVCP_CONSTANT     pRawValue = NULL;
        ULONG             rawValueLength = 0;
        if(SUCCEEDED(pMD->GetFieldProps(fieldDef, NULL, mdName, mdNameLen, &nameLen, &fieldAttr, NULL, NULL, NULL, &pRawValue, &rawValueLength)))
        {
            DWORD enumValueRequiredAttributes = fdPublic | fdStatic | fdLiteral | fdHasDefault;
            if((fieldAttr & enumValueRequiredAttributes) != enumValueRequiredAttributes)
                continue;

            ULONG64 currentConstValue = 0;
            switch (enumUnderlyingType)
            {
            case ELEMENT_TYPE_CHAR:
            case ELEMENT_TYPE_I1:
                currentConstValue = (ULONG64)(*((CHAR*)pRawValue));
                break;
            case ELEMENT_TYPE_U1:
                currentConstValue = (ULONG64)(*((BYTE*)pRawValue));
                break;
            case ELEMENT_TYPE_I2:
                currentConstValue = (ULONG64)(*((SHORT*)pRawValue));
                break;
            case ELEMENT_TYPE_U2:
                currentConstValue = (ULONG64)(*((USHORT*)pRawValue));
                break;
            case ELEMENT_TYPE_I4:
                currentConstValue = (ULONG64)(*((INT32*)pRawValue));
                break;
            case ELEMENT_TYPE_U4:
                currentConstValue = (ULONG64)(*((UINT32*)pRawValue));
                break;
            case ELEMENT_TYPE_I8:
                currentConstValue = (ULONG64)(*((LONG*)pRawValue));
                break;
            case ELEMENT_TYPE_U8:
                currentConstValue = (ULONG64)(*((ULONG*)pRawValue));
                break;
            case ELEMENT_TYPE_I:
                currentConstValue = (ULONG64)(*((int*)pRawValue));
                break;
            case ELEMENT_TYPE_U:
            case ELEMENT_TYPE_R4:
            case ELEMENT_TYPE_R8:
                // Technically U and the floating-point ones are options in the CLI, but not in the CLS or C#, so these are NYI
            default:
                currentConstValue = 0;
            }

            if((currentConstValue == remainingValue) || ((currentConstValue != 0) && ((currentConstValue & remainingValue) == currentConstValue)))
            {
                remainingValue &= ~currentConstValue;
                DWORD charsCopied = 0;
                if(isFirst)
                {
                    charsCopied = _snwprintf_s(pTextValueCursor, cchTextValueCursor, _TRUNCATE, L"= %s", mdName);
                    isFirst = false;
                }
                else 
                    charsCopied = _snwprintf_s(pTextValueCursor, cchTextValueCursor, _TRUNCATE, L" | %s", mdName);

                // if an error or truncation occurred, stop copying
                if(charsCopied == -1)
                {
                    cchTextValueCursor = 0;
                    pTextValueCursor = NULL;
                }
                else
                {
                    // charsCopied is the number of characters copied, not counting the terminating null
                    // this advances the cursor to point right at the terminating null so that future copies
                    // will concatenate the string
                    pTextValueCursor += charsCopied;
                    cchTextValueCursor -= charsCopied;
                }
            }
        }
    }
    pMD->CloseEnum(fEnum);

    return Status;
}

// Helper that caches the textual value for nodes that evaluate to a string object
HRESULT ExpressionNode::GetDebuggeeStringValue(ICorDebugValue* pInputValue, __inout_ecount(cchBuffer) WCHAR* wszBuffer, DWORD cchBuffer)
{
    HRESULT Status;

    ToRelease<ICorDebugStringValue> pStringValue;
    IfFailRet(pInputValue->QueryInterface(IID_ICorDebugStringValue, (LPVOID*) &pStringValue));

    ULONG32 cchValueReturned;
    IfFailRet(pStringValue->GetString(cchBuffer, &cchValueReturned, wszBuffer));

    return S_OK;
}

// Retrieves the string value for a constant
HRESULT ExpressionNode::GetConstantStringValue(__inout_ecount(cchBuffer) WCHAR* wszBuffer, DWORD cchBuffer)
{
    // The string encoded in metadata isn't null-terminated
    // so we need to copy it to a null terminated buffer
    DWORD copyLen = cchDefaultValue;
    if(copyLen > cchBuffer-1)
        copyLen = cchDefaultValue;

    wcsncpy_s(wszBuffer, cchBuffer, (WCHAR*)pDefaultValue, copyLen);
    return S_OK;
}

// Helper that caches the textual value for nodes that evaluate to array objects
HRESULT ExpressionNode::PopulateSzArrayValue(ICorDebugValue* pInputValue)
{
    HRESULT Status = S_OK;

    ToRelease<ICorDebugArrayValue> pArrayValue;
    IfFailRet(pInputValue->QueryInterface(IID_ICorDebugArrayValue, (LPVOID*) &pArrayValue));

    ULONG32 nRank;
    IfFailRet(pArrayValue->GetRank(&nRank));
    if (nRank != 1)
    {
        _snwprintf_s(pErrorMessage, MAX_EXPRESSION, _TRUNCATE, L"Multi-dimensional arrays NYI");
        return E_UNEXPECTED;
    }

    ULONG32 cElements;
    IfFailRet(pArrayValue->GetCount(&cElements));

    if (cElements == 0)
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"(empty)");
    else if (cElements == 1) 
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"(1 element)");
    else  
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"(%d elements)", cElements);

    return S_OK;
}

// Helper that caches the textual value for nodes of any type
HRESULT ExpressionNode::PopulateTextValueHelper()
{
    HRESULT Status = S_OK;

    BOOL isNull = TRUE;
    ToRelease<ICorDebugValue> pInnerValue;
    CorElementType corElemType;
    ULONG32 cbSize = 0;
    if(pValue != NULL)
    {
        IfFailRet(DereferenceAndUnboxValue(pValue, &pInnerValue, &isNull));

        if(isNull)
        {
            _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= null");
            return S_OK;
        }
        IfFailRet(pInnerValue->GetSize(&cbSize));
        IfFailRet(pInnerValue->GetType(&corElemType));
    }
    else if(pDefaultValue != NULL)
    {
        if(pTypeCast == NULL)
        {
            // this shouldn't happen, but just print nothing if it does
            return S_OK;
        }
        // This works around an irritating issue in ICorDebug. For default values
        // we have to construct the ICorDebugType ourselves, however ICorDebug
        // doesn't allow type construction using the correct element types. The
        // caller must past CLASS or VALUETYPE even when a more specific short
        // form element type is applicable. That means that later, here, we get
        // back the wrong answer. To work around this we format the type as a
        // string, and check it against all the known types. That allows us determine
        // everything except VALUETYPE/CLASS. Thankfully that distinction is the
        // one piece of data ICorDebugType will tell us if needed.
        if(FAILED(GetCanonicalElementTypeForTypeName(GetTypeName(), &corElemType)))
        {
            pTypeCast->GetType(&corElemType);
        }

        switch(corElemType)
        {
        case ELEMENT_TYPE_BOOLEAN:
        case ELEMENT_TYPE_I1:
        case ELEMENT_TYPE_U1:
            cbSize = 1;
            break;

        case ELEMENT_TYPE_CHAR:
        case ELEMENT_TYPE_I2:
        case ELEMENT_TYPE_U2:
            cbSize = 2;
            break;

        case ELEMENT_TYPE_I:
        case ELEMENT_TYPE_U:
        case ELEMENT_TYPE_I4:
        case ELEMENT_TYPE_U4:
        case ELEMENT_TYPE_R4:
            cbSize = 4;
            break;

        case ELEMENT_TYPE_I8:
        case ELEMENT_TYPE_U8:
        case ELEMENT_TYPE_R8:
            cbSize = 8;
            break;
        }
    }

    if (corElemType == ELEMENT_TYPE_STRING)
    {
        WCHAR buffer[MAX_EXPRESSION];
        buffer[0] = L'\0';
        if(pInnerValue != NULL)
            GetDebuggeeStringValue(pInnerValue, buffer, MAX_EXPRESSION);
        else
            GetConstantStringValue(buffer, MAX_EXPRESSION);
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= \"%s\"", buffer);
    }
    else if (corElemType == ELEMENT_TYPE_SZARRAY)
    {
        return PopulateSzArrayValue(pInnerValue);
    }


    ArrayHolder<BYTE> rgbValue = new BYTE[cbSize];
    memset(rgbValue.GetPtr(), 0, cbSize * sizeof(BYTE));
    if(pInnerValue != NULL)
    {
        ToRelease<ICorDebugGenericValue> pGenericValue;
        IfFailRet(pInnerValue->QueryInterface(IID_ICorDebugGenericValue, (LPVOID*) &pGenericValue));
        IfFailRet(pGenericValue->GetValue((LPVOID) &(rgbValue[0])));
    }
    else
    {
        memcpy((LPVOID) &(rgbValue[0]), pDefaultValue, cbSize);
    }

    //TODO: this should really be calculated from the type
    if(pInnerValue != NULL && IsEnum(pInnerValue))
    {
        Status = PopulateEnumValue(pInnerValue, rgbValue);
        return Status;
    }

    switch (corElemType)
    {
    default:
        _snwprintf_s(pErrorMessage, MAX_ERROR, _TRUNCATE, L"Unhandled CorElementType: 0x%x", corElemType);
        Status = E_FAIL;
        break;

    case ELEMENT_TYPE_PTR:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"<pointer>");
        break;

    case ELEMENT_TYPE_FNPTR:
        {
            CORDB_ADDRESS addr = 0;
            ToRelease<ICorDebugReferenceValue> pReferenceValue = NULL;
            if(SUCCEEDED(pInnerValue->QueryInterface(IID_ICorDebugReferenceValue, (LPVOID*) &pReferenceValue)))
                pReferenceValue->GetValue(&addr);
            _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"<function pointer 0x%x>", addr);
        }
        break;

    case ELEMENT_TYPE_VALUETYPE:
    case ELEMENT_TYPE_CLASS:
    case ELEMENT_TYPE_OBJECT:
        ULONG64 pointer;
        if(pInnerValue != NULL && SUCCEEDED(pInnerValue->GetAddress(&pointer)))
            _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"@ 0x%p", (void *) pointer);
        break;

    case ELEMENT_TYPE_BOOLEAN:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= %s", rgbValue[0] == 0 ? L"false" : L"true");
        break;

    case ELEMENT_TYPE_CHAR:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= '%C'", *(WCHAR *) &(rgbValue[0]));
        break;

    case ELEMENT_TYPE_I1:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= %d", *(char*) &(rgbValue[0]));
        break;

    case ELEMENT_TYPE_U1:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= %d", *(unsigned char*) &(rgbValue[0]));
        break;

    case ELEMENT_TYPE_I2:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= %hd", *(short*) &(rgbValue[0]));
        break;

    case ELEMENT_TYPE_U2:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= %hu", *(unsigned short*) &(rgbValue[0]));
        break;

    case ELEMENT_TYPE_I:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= %d", *(int*) &(rgbValue[0]));
        break;

    case ELEMENT_TYPE_U:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= %u", *(unsigned int*) &(rgbValue[0]));
        break;

    case ELEMENT_TYPE_I4:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= %d", *(int*) &(rgbValue[0]));
        break;

    case ELEMENT_TYPE_U4:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= %u", *(unsigned int*) &(rgbValue[0]));
        break;

    case ELEMENT_TYPE_I8:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= %I64d", *(__int64*) &(rgbValue[0]));
        break;

    case ELEMENT_TYPE_U8:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= %I64u", *(unsigned __int64*) &(rgbValue[0]));
        break;

    case ELEMENT_TYPE_R4:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"= %f", (double) *(float*) &(rgbValue[0]));
        break;

    case ELEMENT_TYPE_R8:
        _snwprintf_s(pTextValue, MAX_EXPRESSION, _TRUNCATE, L"%f", *(double*) &(rgbValue[0]));
        break;

        // TODO: The following corElementTypes are not yet implemented here.  Array
        // might be interesting to add, though the others may be of rather limited use:
        // ELEMENT_TYPE_ARRAY          = 0x14,     // MDARRAY <type> <rank> <bcount> <bound1> ... <lbcount> <lb1> ...
        // 
        // ELEMENT_TYPE_GENERICINST    = 0x15,     // GENERICINST <generic type> <argCnt> <arg1> ... <argn>
    }

    return Status;
}

// Caches the textual value of this node
HRESULT ExpressionNode::PopulateTextValue()
{
    if(pErrorMessage[0] != 0)
        return E_UNEXPECTED;
    if(pValue == NULL && pDefaultValue == NULL)
        return S_OK;
    HRESULT Status = PopulateTextValueHelper();
    if(FAILED(Status) && pErrorMessage[0] == 0)
    {
        _snwprintf_s(pErrorMessage, MAX_ERROR, _TRUNCATE, L"Error in PopulateTextValueHelper: 0x%x", Status);
    }
    return Status;
}


// Expression parsing and search

//Callback that searches a frame to determine if it contains a local variable or parameter of a given name
VOID ExpressionNode::EvaluateExpressionFrameScanCallback(ICorDebugFrame* pFrame, VOID* pUserData)
{
    EvaluateExpressionFrameScanData* pData = (EvaluateExpressionFrameScanData*)pUserData;

    // we already found what we were looking for, just continue
    if(pData->pFoundValue != NULL)
        return;

    // if any of these fail we just continue on
    // querying for ILFrame will frequently fail because many frames aren't IL
    ToRelease<ICorDebugILFrame> pILFrame;
    HRESULT Status = pFrame->QueryInterface(IID_ICorDebugILFrame, (LPVOID*) &pILFrame);
    if (FAILED(Status))
    {
        return;
    }
    // we need to save off the first frame we find regardless of whether we find the
    // local or not. We might need this frame later for static field lookup.
    if(pData->pFirstFrame == NULL)
    {
        pData->pFirstFrame = pILFrame;
        pData->pFirstFrame->AddRef();
    }
    // not all IL frames map to an assembly (ex. LCG)
    ToRelease<ICorDebugFunction> pFunction;
    Status = pFrame->GetFunction(&pFunction);
    if (FAILED(Status))
    {
        return;
    }
    // from here down shouldn't generally fail, but just in case
    mdMethodDef methodDef;
    Status = pFunction->GetToken(&methodDef);
    if (FAILED(Status))
    {
        return;
    }
    ToRelease<ICorDebugModule> pModule;
    Status = pFunction->GetModule(&pModule);
    if (FAILED(Status))
    {
        return;
    }
    ToRelease<IUnknown> pMDUnknown;
    Status = pModule->GetMetaDataInterface(IID_IMetaDataImport, &pMDUnknown);
    if (FAILED(Status))
    {
        return;
    }
    ToRelease<IMetaDataImport> pMD;
    Status = pMDUnknown->QueryInterface(IID_IMetaDataImport, (LPVOID*) &pMD);
    if (FAILED(Status))
    {
        return;
    }

    pData->pFoundFrame = pILFrame;
    pData->pFoundFrame->AddRef();
    // Enumerate all the parameters
    EnumerateParameters(pMD, methodDef, pILFrame, EvaluateExpressionVariableScanCallback, pUserData);
    // Enumerate all the locals
    EnumerateLocals(pMD, methodDef, pILFrame, EvaluateExpressionVariableScanCallback, pUserData);

    // if we didn't find it in this frame then clear the frame back out
    if(pData->pFoundValue == NULL)
    {
        pData->pFoundFrame = NULL;
    }

    return;
}

//Callback checks to see if a given local/parameter has name pName
VOID ExpressionNode::EvaluateExpressionVariableScanCallback(ICorDebugValue* pValue, __in_z WCHAR* pName, __out_z WCHAR* pErrorMessage, VOID* pUserData)
{
    EvaluateExpressionFrameScanData* pData = (EvaluateExpressionFrameScanData*)pUserData;
    if(_wcscmp(pName, pData->pIdentifier) == 0)
    {
        // found it
        pData->pFoundValue = pValue;
        pValue->AddRef();
    }
    return;
}

//Factory method that recursively parses pExpression and create an ExpressionNode
//  pExpression -          the entire expression being parsed
//  pExpressionRemainder - the portion of the expression that remains to be parsed in this
//                         recursive invocation
//  charactersParsed     - the number of characters that have been parsed from pExpression
//                         so far (todo: this is basically the difference between remainder and
//                         full expression, do we need it?)
//  pParsedValue         - A debuggee value that should be used as the context for interpreting
//                         pExpressionRemainder
//  pParsedType          - A debuggee type that should be used as the context for interpreting
//                         pExpressionRemainder. 
//  pParsedDefaultValue  - A fixed value from metadata that should be used as context for
//                         interpretting pExpressionRemainder
//  cchParsedDefaultValue- Size of pParsedDefaultValue
//  pFrame               - A debuggee IL frame that disambiguates the thread and context needed
//                         to evaluate a thread-static or context-static value
//  ppExpressionNode     - OUT - the resulting expression node
//
//
//  Valid combinations of state comming into this method:
//      The expression up to charactersParsed isn't recognized yet:
//           pParsedValue = pParsedType = pParsedDefaultValue = NULL
//           cchParsedDefaultValue = 0
//      The expression up to charactersParsed is a recognized type:
//           pParsedType = <parsed type>
//           pParsedValue = pParsedDefaultValue = NULL
//           cchParsedDefaultValue = 0
//      The expression up to charactersParsed is a recognized value in the debuggee:
//           pParsedValue = <parsed value>
//           pParsedType = pParsedDefaultValue = NULL
//           cchParsedDefaultValue = 0
//      The expression up to charactersParsed is a recognized default value stored in metadata:
//           pParsedValue = NULL
//           pParsedType = <type calculated from metadata>
//           pParsedDefaultValue = <value from metadata>
//           cchParsedDefaultValue = <size of metadata value>
//
//
// REFACTORING NOTE: This method is very similar (but not identical) to the expansion logic
//                   in ExpressionNode. The primary difference is that the nodes expand all
//                   fields/indices whereas this function only expands along a precise route.
//                   If the ExpressionNode code where enhanced to support expanding precisely
//                   large portions of this function could be disposed of. As soon as the function
//                   matched the initial name it could create an ExpressionNode and then use the
//                   ExpressionNode expansion functions to drill down to the actual node required.
//                   Also need to make sure the nodes manage lifetime ok when a parent is destroyed
//                   but a child node is still referenced.
HRESULT ExpressionNode::CreateExpressionNodeHelper(__in_z WCHAR* pExpression,
                                                          __in_z WCHAR* pExpressionParseRemainder,
                                                          DWORD charactersParsed,
                                                          ICorDebugValue* pParsedValue,
                                                          ICorDebugType* pParsedType,
                                                          UVCP_CONSTANT pParsedDefaultValue,
                                                          ULONG cchParsedDefaultValue,
                                                          ICorDebugILFrame* pFrame,
                                                          ExpressionNode** ppExpressionNode)
{
    HRESULT Status = S_OK;
    WCHAR* pExpressionCursor = pExpressionParseRemainder;
    DWORD currentCharsParsed = charactersParsed;
    WCHAR pIdentifier[mdNameLen];
    pIdentifier[0] = 0;
    BOOL isArray = FALSE;
    WCHAR pResultBuffer[MAX_EXPRESSION];

    // Get the next name from the expression string
    if(FAILED(Status = ParseNextIdentifier(&pExpressionCursor, pIdentifier, mdNameLen, pResultBuffer, MAX_EXPRESSION, &currentCharsParsed, &isArray)))
    {
        *ppExpressionNode = new ExpressionNode(pExpression, pResultBuffer);
        if(*ppExpressionNode == NULL)
            return E_OUTOFMEMORY;
        else
            return S_OK;
    }

    // we've gone as far as we need, nothing left to parse
    if(Status == S_FALSE)
    {
        ToRelease<ICorDebugValue> pValue;
        *ppExpressionNode = new ExpressionNode(pExpression, ChildKind_BaseClass, pExpression, pParsedValue, pParsedType, pFrame, pParsedDefaultValue, cchParsedDefaultValue);
        if(*ppExpressionNode == NULL)
            return E_OUTOFMEMORY;
        else
            return S_OK;
    }
    // if we are just starting and have no context then we need to search locals/parameters/type names
    else if(pParsedValue == NULL && pParsedType == NULL)
    {
        // the first identifier must be a name, not an indexing expression
        if(isArray)
        {
            *ppExpressionNode = new ExpressionNode(pExpression, L"Expression must begin with a local variable, parameter, or fully qualified type name");
            return S_OK;
        }

        // scan for root on stack
        EvaluateExpressionFrameScanData data;
        data.pIdentifier = pIdentifier;
        data.pFoundValue = NULL;
        data.pFoundFrame = NULL;
        data.pFirstFrame = NULL;
        data.pErrorMessage = pResultBuffer;
        data.cchErrorMessage = MAX_EXPRESSION;
        EnumerateFrames(EvaluateExpressionFrameScanCallback, (VOID*) &data);

        if(data.pFoundValue != NULL)
        {
            // found the root, now recurse along the expression
            return CreateExpressionNodeHelper(pExpression, pExpressionCursor, currentCharsParsed, data.pFoundValue, NULL, NULL, 0, data.pFoundFrame, ppExpressionNode);
        }

        // didn't find it - search the type table for a matching name
        WCHAR pName[MAX_EXPRESSION];
        while(true)
        {
            wcsncpy_s(pName, MAX_EXPRESSION, pExpression, currentCharsParsed);
            ToRelease<ICorDebugType> pType;
            if(SUCCEEDED(FindTypeByName(pName, &pType)))
                return CreateExpressionNodeHelper(pExpression, pExpressionCursor, currentCharsParsed, NULL, pType, NULL, 0, data.pFirstFrame, ppExpressionNode);

            if(FAILED(Status = ParseNextIdentifier(&pExpressionCursor, pIdentifier, mdNameLen, pResultBuffer, MAX_EXPRESSION, &currentCharsParsed, &isArray)))
            {
                *ppExpressionNode = new ExpressionNode(pExpression, pResultBuffer);
                return S_OK;
            }
            else if(Status == S_FALSE)
            {
                break;
            }
        }

        WCHAR errorMessage[MAX_ERROR];
        swprintf_s(errorMessage, MAX_ERROR, L"No expression prefix could not be matched to an existing type, parameter, or local");
        *ppExpressionNode = new ExpressionNode(pExpression, errorMessage);
        return S_OK;
    }

    // we've got some context from an earlier portion of the search, now just need to continue
    // by dereferencing and indexing until we reach the end of the expression

    // Figure out the type, module, and metadata from our context information
    ToRelease<ICorDebugType> pType;
    BOOL isNull = TRUE;
    ToRelease<ICorDebugValue> pInnerValue = NULL;
    if(pParsedValue != NULL)
    {
        IfFailRet(DereferenceAndUnboxValue(pParsedValue, &pInnerValue, &isNull));

        if(isNull)
        {
            WCHAR parsedExpression[MAX_EXPRESSION];
            wcsncpy_s(parsedExpression, MAX_EXPRESSION, pExpression, charactersParsed);
            WCHAR errorMessage[MAX_ERROR];
            swprintf_s(errorMessage, MAX_ERROR, L"Dereferencing \'%s\' throws NullReferenceException", parsedExpression);
            *ppExpressionNode = new ExpressionNode(pExpression, errorMessage);
            return S_OK;
        }

        ToRelease<ICorDebugValue2> pValue2;
        IfFailRet(pInnerValue->QueryInterface(IID_ICorDebugValue2, (LPVOID *) &pValue2));
        IfFailRet(pValue2->GetExactType(&pType));
        CorElementType et;
        IfFailRet(pType->GetType(&et));
        while(et == ELEMENT_TYPE_ARRAY || et == ELEMENT_TYPE_SZARRAY || et == ELEMENT_TYPE_BYREF || et == ELEMENT_TYPE_PTR)
        {
            pType->GetFirstTypeParameter(&pType);
            IfFailRet(pType->GetType(&et));
        }
    }
    else
    {
        pType = pParsedType;
        pType->AddRef();
    }
    ToRelease<ICorDebugClass> pClass;
    IfFailRet(pType->GetClass(&pClass));
    ToRelease<ICorDebugModule> pModule;
    IfFailRet(pClass->GetModule(&pModule));
    mdTypeDef currentTypeDef;
    IfFailRet(pClass->GetToken(&currentTypeDef));

    ToRelease<IUnknown> pMDUnknown;
    ToRelease<IMetaDataImport> pMD;
    IfFailRet(pModule->GetMetaDataInterface(IID_IMetaDataImport, &pMDUnknown));
    IfFailRet(pMDUnknown->QueryInterface(IID_IMetaDataImport, (LPVOID*) &pMD));


    // if we are searching along and this is an array index dereference
    if(isArray)
    {
        ToRelease<ICorDebugArrayValue> pArrayValue;
        if(pInnerValue == NULL || FAILED(Status = pInnerValue->QueryInterface(IID_ICorDebugArrayValue, (LPVOID*) &pArrayValue)))
        {
            WCHAR errorMessage[MAX_ERROR];
            swprintf_s(errorMessage, MAX_ERROR, L"Index notation only supported for instances of an array type");
            *ppExpressionNode = new ExpressionNode(pExpression, errorMessage);
            return S_OK;
        }

        ULONG32 nRank;
        IfFailRet(pArrayValue->GetRank(&nRank));
        if (nRank != 1)
        {
            WCHAR errorMessage[MAX_ERROR];
            swprintf_s(errorMessage, MAX_ERROR, L"Multi-dimensional arrays NYI");
            *ppExpressionNode = new ExpressionNode(pExpression, errorMessage);
            return S_OK;
        }

        int index = -1;
        if(swscanf_s(pIdentifier, L"%d", &index) != 1)
        {
            WCHAR errorMessage[MAX_ERROR];
            swprintf_s(errorMessage, MAX_ERROR, L"Failed to parse expression, missing or invalid index expression at character %d", charactersParsed+1);
            *ppExpressionNode = new ExpressionNode(pExpression, errorMessage);
            return S_OK;
        }

        ULONG32 cElements;
        IfFailRet(pArrayValue->GetCount(&cElements));
        if(index < 0 || (ULONG32)index >= cElements)
        {
            WCHAR errorMessage[MAX_ERROR];
            swprintf_s(errorMessage, MAX_ERROR, L"Index is out of range for this array");
            *ppExpressionNode = new ExpressionNode(pExpression, errorMessage);
            return S_OK;
        }

        ToRelease<ICorDebugValue> pElementValue;
        IfFailRet(pArrayValue->GetElementAtPosition(index, &pElementValue));
        return CreateExpressionNodeHelper(pExpression, pExpressionCursor, currentCharsParsed, pElementValue, NULL, NULL, 0, pFrame, ppExpressionNode);
    }
    // if we are searching along and this is field dereference
    else
    {
        ToRelease<ICorDebugType> pBaseType = pType;
        pBaseType->AddRef();

        while(pBaseType != NULL)
        {
            // get the current base type class/token/MD
            ToRelease<ICorDebugClass> pBaseClass;
            IfFailRet(pBaseType->GetClass(&pBaseClass));
            ToRelease<ICorDebugModule> pBaseTypeModule;
            IfFailRet(pBaseClass->GetModule(&pBaseTypeModule));
            mdTypeDef baseTypeDef;
            IfFailRet(pBaseClass->GetToken(&baseTypeDef));
            ToRelease<IUnknown> pBaseTypeMDUnknown;
            ToRelease<IMetaDataImport> pBaseTypeMD;
            IfFailRet(pBaseTypeModule->GetMetaDataInterface(IID_IMetaDataImport, &pBaseTypeMDUnknown));
            IfFailRet(pBaseTypeMDUnknown->QueryInterface(IID_IMetaDataImport, (LPVOID*) &pBaseTypeMD));


            // iterate through all fields at this level of the class hierarchy
            ULONG numFields = 0;
            HCORENUM fEnum = NULL;
            mdFieldDef fieldDef;
            while(SUCCEEDED(pMD->EnumFields(&fEnum, baseTypeDef, &fieldDef, 1, &numFields)) && numFields != 0)
            {
                ULONG             nameLen = 0;
                DWORD             fieldAttr = 0;
                WCHAR             mdName[mdNameLen];
                WCHAR             typeName[mdNameLen];
                CorElementType    fieldDefaultValueEt;
                UVCP_CONSTANT     pDefaultValue;
                ULONG             cchDefaultValue;
                if(SUCCEEDED(pBaseTypeMD->GetFieldProps(fieldDef, NULL, mdName, mdNameLen, &nameLen, &fieldAttr, NULL, NULL, (DWORD*)&fieldDefaultValueEt, &pDefaultValue, &cchDefaultValue)) &&
                    _wcscmp(mdName, pIdentifier) == 0)
                {
                    ToRelease<ICorDebugType> pFieldValType = NULL;
                    ToRelease<ICorDebugValue> pFieldVal;
                    if (fieldAttr & fdStatic)
                        pBaseType->GetStaticFieldValue(fieldDef, pFrame, &pFieldVal);
                    else if(pInnerValue != NULL)
                    {
                        ToRelease<ICorDebugObjectValue> pObjValue;
                        if (SUCCEEDED(pInnerValue->QueryInterface(IID_ICorDebugObjectValue, (LPVOID*) &pObjValue)))
                            pObjValue->GetFieldValue(pBaseClass, fieldDef, &pFieldVal);
                    }

                    // we didn't get a value yet and there is default value available
                    // need to calculate the type because there won't be a ICorDebugValue to derive it from
                    if(pFieldVal == NULL && pDefaultValue != NULL)
                    {
                        FindTypeFromElementType(fieldDefaultValueEt, &pFieldValType);
                    }
                    else
                    {
                        // if we aren't using default value, make sure it is cleared out
                        pDefaultValue = NULL;
                        cchDefaultValue = 0;
                    }

                    // if we still don't have a value, check if we are trying to get an instance field from a static type
                    if(pInnerValue == NULL && pFieldVal == NULL && pDefaultValue == NULL)
                    {
                        WCHAR pObjectTypeName[MAX_EXPRESSION];
                        CalculateTypeName(pBaseType, pObjectTypeName, MAX_EXPRESSION);
                        WCHAR errorMessage[MAX_ERROR];
                        swprintf_s(errorMessage, MAX_ERROR, L"Can not evaluate instance field \'%s\' from static type \'%s\'", pIdentifier, pObjectTypeName);
                        *ppExpressionNode = new ExpressionNode(pExpression, errorMessage);
                        return S_OK;
                    }
                    return CreateExpressionNodeHelper(pExpression, pExpressionCursor, currentCharsParsed, pFieldVal, pFieldValType, pDefaultValue, cchDefaultValue, pFrame, ppExpressionNode);
                }
            }

            //advance to next base type
            ICorDebugType* pTemp = NULL;
            pBaseType->GetBase(&pTemp);
            pBaseType = pTemp;
        }

        WCHAR pObjectTypeName[MAX_EXPRESSION];
        CalculateTypeName(pType, pObjectTypeName, MAX_EXPRESSION);
        WCHAR errorMessage[MAX_ERROR];
        swprintf_s(errorMessage, MAX_ERROR, L"Field \'%s\' does not exist in type \'%s\'", pIdentifier, pObjectTypeName);
        *ppExpressionNode = new ExpressionNode(pExpression, errorMessage);
        return S_OK;
    }

    return Status;
}

// Splits apart a C#-like expression and determines the first identifier in the string and updates expression to point
// at the remaining unparsed portion
HRESULT ExpressionNode::ParseNextIdentifier(__in_z WCHAR** expression, __inout_ecount(cchIdentifierName) WCHAR* identifierName, DWORD cchIdentifierName, __inout_ecount(cchErrorMessage) WCHAR* errorMessage, DWORD cchErrorMessage, DWORD* charactersParsed, BOOL* isArrayIndex)
{

    // This algorithm is best understood as a two stage process. The first stage splits
    // the expression into two chunks an identifier and a remaining expression. The second stage
    // normalizes the identifier. The splitting algorithm doesn't care if identifiers are well-formed
    // at all, we do some error checking in the 2nd stage though. For the splitting stage, an identifier is
    // any first character, followed by as many characters as possible that aren't a '.' or a '['.
    // In the 2nd stage any '.' character is removed from the front (the only place it could be)
    // and enclosing braces are removed. An error is recorded if the identifier ends 0 length or the
    // opening bracket isn't matched. Here is an example showing how we would parse an expression
    // which is deliberately not very well formed. Each line is the result of calling this function once... it
    // takes many calls to break the entire expression down.
    //
    // expression              1st stage identifier   2nd stage identifier
    // foo.bar[18..f[1.][2][[ 
    // .bar[18..f[1.][2][[     foo                    foo
    // [18..f[1.][2][[         .bar                   bar
    // ..f[1.][2][[            [18                    error no ]
    // .f[1.][2][[             .                      error 0-length 
    // [1.][2][[               .f                     f
    // .][2][[                 [1                     error no ]
    // [2][[                   .]                     ]  (we don't error check legal CLI identifier name characters)
    // [[                      [2]                    2
    // [                       [                      error no ]
    //                         [                      error no ]

    // not an error, just the end of the expression
    if(*expression == NULL || **expression == 0)
        return S_FALSE;

    WCHAR* expressionStart = *expression;
    DWORD currentCharsParsed = *charactersParsed;
    DWORD identifierLen = (DWORD) _wcscspn(expressionStart, L".[");
    // if the first character was a . or [ skip over it. Note that we don't
    // do this always in case the first WCHAR was part of a surrogate pair
    if(identifierLen == 0)
    {
        identifierLen = (DWORD) _wcscspn(expressionStart+1, L".[") + 1;
    }

    *expression += identifierLen;
    *charactersParsed += identifierLen;

    // done with the first stage splitting, on to 2nd stage

    // a . should be followed by field name
    if(*expressionStart == L'.')
    {
        if(identifierLen == 1) // 0-length after .
        {
            swprintf_s(errorMessage, cchErrorMessage, L"Failed to parse expression, missing name after character %d", currentCharsParsed+1);
            return E_FAIL;
        }
        if(identifierLen-1 >= cchIdentifierName)
        {
            swprintf_s(errorMessage, cchErrorMessage, L"Failed to parse expression, name at character %d is too long", currentCharsParsed+2);
            return E_FAIL;
        }
        *isArrayIndex = FALSE;
        wcsncpy_s(identifierName, cchIdentifierName, expressionStart+1, identifierLen-1);
        return S_OK;
    }
    // an open bracket should be followed by a decimal value and then a closing bracket
    else if(*expressionStart == L'[')
    {
        if(*(expressionStart+identifierLen-1) != L']')
        {
            swprintf_s(errorMessage, cchErrorMessage, L"Failed to parse expression, missing or invalid index expression at character %d", currentCharsParsed+1);
            return E_FAIL;
        }
        if(identifierLen <= 2) // 0-length between []
        {
            swprintf_s(errorMessage, cchErrorMessage, L"Failed to parse expression, missing index after character %d", currentCharsParsed+1);
            return E_FAIL;
        }
        if(identifierLen-2 >= cchIdentifierName)
        {
            swprintf_s(errorMessage, cchErrorMessage, L"Failed to parse expression, index at character %d is too large", currentCharsParsed+2);
            return E_FAIL;
        }
        *isArrayIndex = TRUE;
        wcsncpy_s(identifierName, cchIdentifierName, expressionStart+1, identifierLen-2);
        return S_OK;
    }
    else // no '.' or '[', this is an initial name
    {
        if(identifierLen == 0) // 0-length
        {
            swprintf_s(errorMessage, cchErrorMessage, L"Failed to parse expression, missing name after character %d", currentCharsParsed+1);
            return E_FAIL;
        }
        if(identifierLen >= cchIdentifierName)
        {
            swprintf_s(errorMessage, cchErrorMessage, L"Failed to parse expression, name at character %d is too long", currentCharsParsed+1);
            return E_FAIL;
        }
        *isArrayIndex = FALSE;
        wcsncpy_s(identifierName, cchIdentifierName, expressionStart, identifierLen);
        return S_OK;
    }
}


// Iterate through all parameters in the ILFrame calling the callback function for each of them
HRESULT ExpressionNode::EnumerateParameters(IMetaDataImport * pMD,
                                                   mdMethodDef methodDef,
                                                   ICorDebugILFrame * pILFrame,
                                                   VariableEnumCallback pCallback,
                                                   VOID* pUserData)
{
    HRESULT Status = S_OK;

    ULONG cParams = 0;
    ToRelease<ICorDebugValueEnum> pParamEnum;
    IfFailRet(pILFrame->EnumerateArguments(&pParamEnum));
    IfFailRet(pParamEnum->GetCount(&cParams));
    DWORD methAttr = 0;
    IfFailRet(pMD->GetMethodProps(methodDef, NULL, NULL, 0, NULL, &methAttr, NULL, NULL, NULL, NULL));
    for (ULONG i=0; i < cParams; i++)
    {
        ULONG paramNameLen = 0;
        mdParamDef paramDef;
        WCHAR paramName[mdNameLen] = L"\0";

        if(i == 0 && (methAttr & mdStatic) == 0)
            swprintf_s(paramName, mdNameLen, L"this\0");
        else 
        {
            int idx = ((methAttr & mdStatic) == 0)? i : (i + 1);
            if(SUCCEEDED(pMD->GetParamForMethodIndex(methodDef, idx, &paramDef)))
                pMD->GetParamProps(paramDef, NULL, NULL, paramName, mdNameLen, &paramNameLen, NULL, NULL, NULL, NULL);
        }
        if(_wcslen(paramName) == 0)
            swprintf_s(paramName, mdNameLen, L"param_%d\0", i);

        ToRelease<ICorDebugValue> pValue;
        ULONG cArgsFetched;
        WCHAR pErrorMessage[MAX_ERROR] = L"\0";
        HRESULT hr = pParamEnum->Next(1, &pValue, &cArgsFetched);
        if (FAILED(hr))
        {
            swprintf_s(pErrorMessage, MAX_ERROR, L"  + (Error 0x%x retrieving parameter '%S')\n", hr, paramName);
        }
        if (hr == S_FALSE)
        {
            break;
        }
        pCallback(pValue, paramName, pErrorMessage, pUserData);
    }

    return Status;
}

// Enumerate all locals in the given ILFrame, calling the callback method for each of them
HRESULT ExpressionNode::EnumerateLocals(IMetaDataImport * pMD,
                                               mdMethodDef methodDef,
                                               ICorDebugILFrame * pILFrame,
                                               VariableEnumCallback pCallback,
                                               VOID* pUserData)
{
    HRESULT Status = S_OK;
    ULONG cLocals = 0;
    ToRelease<ICorDebugFunction> pFunction;
    ToRelease<ICorDebugModule> pModule;
    if(SUCCEEDED(pILFrame->GetFunction(&pFunction)))
    {
        IfFailRet(pFunction->GetModule(&pModule));
    }
    ToRelease<ICorDebugValueEnum> pLocalsEnum;
    IfFailRet(pILFrame->EnumerateLocalVariables(&pLocalsEnum));
    IfFailRet(pLocalsEnum->GetCount(&cLocals));
    if (cLocals > 0)
    {
        SymbolReader symReader;
        bool symbolsAvailable = false;
        if(pModule != NULL && SUCCEEDED(symReader.LoadSymbols(pMD, pModule)))
            symbolsAvailable = true;

        for (ULONG i=0; i < cLocals; i++)
        {
            ULONG paramNameLen = 0;
            WCHAR paramName[mdNameLen] = L"\0";
            WCHAR pErrorMessage[MAX_ERROR] = L"\0";
            ToRelease<ICorDebugValue> pValue;
            HRESULT hr = S_OK;
            if(symbolsAvailable)
                hr = symReader.GetNamedLocalVariable(pILFrame, i, paramName, mdNameLen, &pValue);
            else
            {
                ULONG cArgsFetched;
                hr = pLocalsEnum->Next(1, &pValue, &cArgsFetched);
            }
            if(_wcslen(paramName) == 0)
                swprintf_s(paramName, mdNameLen, L"local_%d\0", i);

            if (FAILED(hr))
            {
                swprintf_s(pErrorMessage, MAX_ERROR, L"  + (Error 0x%x retrieving local variable '%S')\n", hr, paramName);
            }
            else if (hr == S_FALSE)
            {
                break;
            }
            pCallback(pValue, paramName, pErrorMessage, pUserData);
        }
    }

    return Status;
}

// Iterates over all frames on the current thread's stack, calling the callback function for each of them
HRESULT ExpressionNode::EnumerateFrames(FrameEnumCallback pCallback, VOID* pUserData)
{
    HRESULT Status = S_OK;
    ToRelease<ICorDebugThread> pThread;
    ToRelease<ICorDebugThread3> pThread3;
    ToRelease<ICorDebugStackWalk> pStackWalk;
    ULONG ulThreadID = 0;
    g_ExtSystem->GetCurrentThreadSystemId(&ulThreadID);

    IfFailRet(g_pCorDebugProcess->GetThread(ulThreadID, &pThread));
    IfFailRet(pThread->QueryInterface(IID_ICorDebugThread3, (LPVOID *) &pThread3));
    IfFailRet(pThread3->CreateStackWalk(&pStackWalk));

    InternalFrameManager internalFrameManager;
    IfFailRet(internalFrameManager.Init(pThread3));

    int currentFrame = -1;

    for (Status = S_OK; ; Status = pStackWalk->Next())
    {
        currentFrame++;

        if (Status == CORDBG_S_AT_END_OF_STACK)
        {
            break;
        }
        IfFailRet(Status);

        if (IsInterrupt())
        {
            ExtOut("<interrupted>\n");
            break;
        }

        CROSS_PLATFORM_CONTEXT context;
        ULONG32 cbContextActual;
        if ((Status=pStackWalk->GetContext(
            DT_CONTEXT_FULL, 
            sizeof(context),
            &cbContextActual,
            (BYTE *)&context))!=S_OK)
        {
            ExtOut("GetFrameContext failed: %lx\n",Status);
            break;
        }

        ToRelease<ICorDebugFrame> pFrame;
        IfFailRet(pStackWalk->GetFrame(&pFrame));
        if (Status == S_FALSE)
        {
            Status = S_OK;
            continue;
        }

        pCallback(pFrame, pUserData);
    }

    return Status;
}

// Determines the corresponding ICorDebugType for a given primitive type
HRESULT ExpressionNode::FindTypeFromElementType(CorElementType et, ICorDebugType** ppType)
{
    HRESULT Status;
    switch (et)
    {
    default:
        Status = E_FAIL;
        break;

    case ELEMENT_TYPE_BOOLEAN:
        Status = FindTypeByName(L"System.Boolean", ppType);
        break;

    case ELEMENT_TYPE_CHAR:
        Status = FindTypeByName(L"System.Char", ppType);
        break;

    case ELEMENT_TYPE_I1:
        Status = FindTypeByName(L"System.SByte", ppType);
        break;

    case ELEMENT_TYPE_U1:
        Status = FindTypeByName(L"System.Byte", ppType);
        break;

    case ELEMENT_TYPE_I2:
        Status = FindTypeByName(L"System.Short", ppType);
        break;

    case ELEMENT_TYPE_U2:
        Status = FindTypeByName(L"System.UShort", ppType);
        break;

    case ELEMENT_TYPE_I:
        Status = FindTypeByName(L"System.Int32", ppType);
        break;

    case ELEMENT_TYPE_U:
        Status = FindTypeByName(L"System.UInt32", ppType);
        break;

    case ELEMENT_TYPE_I4:
        Status = FindTypeByName(L"System.Int32", ppType);
        break;

    case ELEMENT_TYPE_U4:
        Status = FindTypeByName(L"System.UInt32", ppType);
        break;

    case ELEMENT_TYPE_I8:
        Status = FindTypeByName(L"System.Int64", ppType);
        break;

    case ELEMENT_TYPE_U8:
        Status = FindTypeByName(L"System.UInt64", ppType);
        break;

    case ELEMENT_TYPE_R4:
        Status = FindTypeByName(L"System.Single", ppType);
        break;

    case ELEMENT_TYPE_R8:
        Status = FindTypeByName(L"System.Double", ppType);
        break;

    case ELEMENT_TYPE_OBJECT:
        Status = FindTypeByName(L"System.Object", ppType);
        break;

    case ELEMENT_TYPE_STRING:
        Status = FindTypeByName(L"System.String", ppType);
        break;
    }
    return Status;
}

// Gets the appropriate element type encoding for well-known fully qualified type names
// This doesn't work for arbitrary types, just types that have CorElementType short forms.
HRESULT ExpressionNode::GetCanonicalElementTypeForTypeName(__in_z WCHAR* pTypeName, CorElementType *et)
{
    //Sadly ICorDebug deliberately prevents creating ICorDebugType instances
    //that use canonical short form element types... seems like an issue to me.

    if(_wcscmp(pTypeName, L"System.String")==0)
    {
        *et = ELEMENT_TYPE_STRING;
    }
    else if(_wcscmp(pTypeName, L"System.Object")==0)
    {
        *et = ELEMENT_TYPE_OBJECT;
    }
    else if(_wcscmp(pTypeName, L"System.Void")==0)
    {
        *et = ELEMENT_TYPE_VOID;
    }
    else if(_wcscmp(pTypeName, L"System.Boolean")==0)
    {
        *et = ELEMENT_TYPE_BOOLEAN;
    }
    else if(_wcscmp(pTypeName, L"System.Char")==0)
    {
        *et = ELEMENT_TYPE_CHAR;
    }
    else if(_wcscmp(pTypeName, L"System.Byte")==0)
    {
        *et = ELEMENT_TYPE_U1;
    }
    else if(_wcscmp(pTypeName, L"System.Sbyte")==0)
    {
        *et = ELEMENT_TYPE_I1;
    }
    else if(_wcscmp(pTypeName, L"System.Int16")==0)
    {
        *et = ELEMENT_TYPE_I2;
    }
    else if(_wcscmp(pTypeName, L"System.UInt16")==0)
    {
        *et = ELEMENT_TYPE_U2;
    }
    else if(_wcscmp(pTypeName, L"System.UInt32")==0)
    {
        *et = ELEMENT_TYPE_U4;
    }
    else if(_wcscmp(pTypeName, L"System.Int32")==0)
    {
        *et = ELEMENT_TYPE_I4;
    }
    else if(_wcscmp(pTypeName, L"System.UInt64")==0)
    {
        *et = ELEMENT_TYPE_U8;
    }
    else if(_wcscmp(pTypeName, L"System.Int64")==0)
    {
        *et = ELEMENT_TYPE_I8;
    }
    else if(_wcscmp(pTypeName, L"System.Single")==0)
    {
        *et = ELEMENT_TYPE_R4;
    }
    else if(_wcscmp(pTypeName, L"System.Double")==0)
    {
        *et = ELEMENT_TYPE_R8;
    }
    else if(_wcscmp(pTypeName, L"System.IntPtr")==0)
    {
        *et = ELEMENT_TYPE_U;
    }
    else if(_wcscmp(pTypeName, L"System.UIntPtr")==0)
    {
        *et = ELEMENT_TYPE_I;
    }
    else if(_wcscmp(pTypeName, L"System.TypedReference")==0)
    {
        *et = ELEMENT_TYPE_TYPEDBYREF;
    }
    else 
    {
        return E_FAIL; // can't tell from a name whether it should be valuetype or class
    }
    return S_OK;
}

// Searches the debuggee for any ICorDebugType that matches the given fully qualified name
// This will search across all AppDomains and Assemblies
HRESULT ExpressionNode::FindTypeByName(__in_z  WCHAR* pTypeName, ICorDebugType** ppType)
{
    HRESULT Status = S_OK;
    ToRelease<ICorDebugAppDomainEnum> pAppDomainEnum;
    IfFailRet(g_pCorDebugProcess->EnumerateAppDomains(&pAppDomainEnum));
    DWORD count;
    IfFailRet(pAppDomainEnum->GetCount(&count));
    for(DWORD i = 0; i < count; i++)
    {
        ToRelease<ICorDebugAppDomain> pAppDomain;
        DWORD countFetched = 0;
        IfFailRet(pAppDomainEnum->Next(1, &pAppDomain, &countFetched));
        Status = FindTypeByName(pAppDomain, pTypeName, ppType);
        if(SUCCEEDED(Status))
            break;
    }

    return Status;
}

// Searches the debuggee for any ICorDebugType that matches the given fully qualified name
// This will search across all Assemblies in the given AppDomain
HRESULT ExpressionNode::FindTypeByName(ICorDebugAppDomain* pAppDomain, __in_z WCHAR* pTypeName, ICorDebugType** ppType)
{
    HRESULT Status = S_OK;
    ToRelease<ICorDebugAssemblyEnum> pAssemblyEnum;
    IfFailRet(pAppDomain->EnumerateAssemblies(&pAssemblyEnum));
    DWORD count;
    IfFailRet(pAssemblyEnum->GetCount(&count));
    for(DWORD i = 0; i < count; i++)
    {
        ToRelease<ICorDebugAssembly> pAssembly;
        DWORD countFetched = 0;
        IfFailRet(pAssemblyEnum->Next(1, &pAssembly, &countFetched));
        Status = FindTypeByName(pAssembly, pTypeName, ppType);
        if(SUCCEEDED(Status))
            break;
    }

    return Status;
}

// Searches the assembly for any ICorDebugType that matches the given fully qualified name
HRESULT ExpressionNode::FindTypeByName(ICorDebugAssembly* pAssembly, __in_z WCHAR* pTypeName, ICorDebugType** ppType)
{
    HRESULT Status = S_OK;
    ToRelease<ICorDebugModuleEnum> pModuleEnum;
    IfFailRet(pAssembly->EnumerateModules(&pModuleEnum));
    DWORD count;
    IfFailRet(pModuleEnum->GetCount(&count));
    for(DWORD i = 0; i < count; i++)
    {
        ToRelease<ICorDebugModule> pModule;
        DWORD countFetched = 0;
        IfFailRet(pModuleEnum->Next(1, &pModule, &countFetched));
        Status = FindTypeByName(pModule, pTypeName, ppType);
        if(SUCCEEDED(Status))
            break;
    }

    return Status;
}

// Searches a given module for any ICorDebugType that matches the given fully qualified type name
HRESULT ExpressionNode::FindTypeByName(ICorDebugModule* pModule, __in_z WCHAR* pTypeName, ICorDebugType** ppType)
{
    HRESULT Status = S_OK;
    ToRelease<IUnknown> pMDUnknown;
    ToRelease<IMetaDataImport> pMD;
    IfFailRet(pModule->GetMetaDataInterface(IID_IMetaDataImport, &pMDUnknown));
    IfFailRet(pMDUnknown->QueryInterface(IID_IMetaDataImport, (LPVOID*) &pMD));

    // If the name contains a generic argument list, extract the type name from
    // before the list
    WCHAR rootName[mdNameLen];
    WCHAR* pRootName = NULL;
    int typeNameLen = (int) _wcslen(pTypeName);
    int genericParamListStart = (int) _wcscspn(pTypeName, L"<");
    if(genericParamListStart != typeNameLen)
    {
        if(pTypeName[typeNameLen-1] != L'>' || genericParamListStart > mdNameLen)
        {
            return E_FAIL; // mal-formed type name
        }
        else
        {
            wcsncpy_s(rootName, mdNameLen, pTypeName, genericParamListStart);
            pRootName = rootName;
        }
    }
    else
    {
        pRootName = pTypeName;
    }

    // Convert from name to token to ICorDebugClass
    mdTypeDef typeDef;
    IfFailRet(pMD->FindTypeDefByName(pRootName, NULL, &typeDef));
    DWORD flags;
    ULONG nameLen;
    mdToken tkExtends;
    IfFailRet(pMD->GetTypeDefProps(typeDef, NULL, 0, &nameLen, &flags, &tkExtends));
    BOOL isValueType;
    IfFailRet(IsTokenValueTypeOrEnum(tkExtends, pMD, &isValueType));
    CorElementType et = isValueType ? ELEMENT_TYPE_VALUETYPE : ELEMENT_TYPE_CLASS;
    ToRelease<ICorDebugClass> pClass;
    IfFailRet(pModule->GetClassFromToken(typeDef, &pClass));
    ToRelease<ICorDebugClass2> pClass2;
    IfFailRet(pClass->QueryInterface(__uuidof(ICorDebugClass2), (void**)&pClass2));

    // Convert from class to type - if generic then recursively resolve the generic
    // parameter list
    ArrayHolder<ToRelease<ICorDebugType>> typeParams = NULL;
    int countTypeParams = 0;
    if(genericParamListStart != typeNameLen)
    {
        ToRelease<ICorDebugAssembly> pAssembly;
        IfFailRet(pModule->GetAssembly(&pAssembly));
        ToRelease<ICorDebugAppDomain> pDomain;
        IfFailRet(pAssembly->GetAppDomain(&pDomain));

        countTypeParams = 1;
        for(int i = genericParamListStart+1; i < typeNameLen; i++)
        {
            if(pTypeName[i] == L',') countTypeParams++;
        }
        typeParams = new ToRelease<ICorDebugType>[countTypeParams];

        WCHAR* pCurName = pTypeName + genericParamListStart+1;
        for(int i = 0; i < countTypeParams; i++)
        {
            WCHAR typeParamName[mdNameLen];
            WCHAR* pNextComma = _wcschr(pCurName, L',');
            int len = (pNextComma != NULL) ? (int)(pNextComma - pCurName) : (int)_wcslen(pCurName)-1;
            if(len > mdNameLen)
                return E_FAIL;
            wcsncpy_s(typeParamName, mdNameLen, pCurName, len);
            FindTypeByName(pDomain, typeParamName, &(typeParams[i]));
            pCurName = pNextComma+1;
        }
    }
    IfFailRet(pClass2->GetParameterizedType(et, countTypeParams, &(typeParams[0]), ppType));

    return Status;
}

// Checks whether the given token is or refers to type System.ValueType or System.Enum
HRESULT ExpressionNode::IsTokenValueTypeOrEnum(mdToken token, IMetaDataImport* pMetadata, BOOL* pResult)
{
    // This isn't a 100% correct check because we aren't verifying the module portion of the
    // type identity. Arbitrary assemblies could define a type named System.ValueType or System.Enum.
    // If that happens this code will get the answer wrong... we just assume that happens so rarely
    // that it isn't worth doing all the overhead of assembly resolution to deal with

    HRESULT Status = S_OK;
    CorTokenType type = (CorTokenType)(token & 0xFF000000);

    // only need enough space to hold either System.ValueType or System.Enum
    //System.ValueType -> 16 characters
    //System.Enum -> 11 characters
    WCHAR nameBuffer[17];
    nameBuffer[0] = L'\0';

    if(type == mdtTypeRef)
    {
        ULONG chTypeDef;
        pMetadata->GetTypeRefProps(token, NULL, NULL, 0, &chTypeDef);
        if(chTypeDef > _countof(nameBuffer))
        {
            *pResult = FALSE;
            return Status;
        }
        IfFailRet(pMetadata->GetTypeRefProps(token, NULL, nameBuffer, _countof(nameBuffer), &chTypeDef));
    }
    else if(type == mdtTypeDef)
    {
        ULONG chTypeDef;
        pMetadata->GetTypeDefProps(token, NULL, 0, &chTypeDef, NULL, NULL);
        if(chTypeDef > _countof(nameBuffer))
        {
            *pResult = FALSE;
            return Status;
        }
        IfFailRet(pMetadata->GetTypeDefProps(token, nameBuffer, _countof(nameBuffer), &chTypeDef, NULL, NULL));
    }

    if(_wcscmp(nameBuffer, L"System.ValueType") == 0 ||
        _wcscmp(nameBuffer, L"System.Enum") == 0)
    {
        *pResult = TRUE;
    }
    else
    {
        *pResult = FALSE;
    }
    return Status;
}