1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
|
//
// MarkStep.cs
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// (C) 2006 Jb Evain
// (C) 2007 Novell, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
namespace Mono.Linker.Steps {
public partial class MarkStep : IStep {
protected LinkContext _context;
protected Queue<MethodDefinition> _methods;
protected List<MethodDefinition> _virtual_methods;
protected Queue<AttributeProviderPair> _assemblyLevelAttributes;
protected Queue<AttributeProviderPair> _lateMarkedAttributes;
protected List<TypeDefinition> _typesWithInterfaces;
public AnnotationStore Annotations {
get { return _context.Annotations; }
}
public Tracer Tracer {
get { return _context.Tracer; }
}
private MarkingHelpers MarkingHelpers => _context.MarkingHelpers;
public MarkStep ()
{
_methods = new Queue<MethodDefinition> ();
_virtual_methods = new List<MethodDefinition> ();
_assemblyLevelAttributes = new Queue<AttributeProviderPair> ();
_lateMarkedAttributes = new Queue<AttributeProviderPair> ();
_typesWithInterfaces = new List<TypeDefinition> ();
}
public virtual void Process (LinkContext context)
{
_context = context;
Initialize ();
Process ();
}
void Initialize ()
{
foreach (AssemblyDefinition assembly in _context.GetAssemblies ())
InitializeAssembly (assembly);
}
protected virtual void InitializeAssembly (AssemblyDefinition assembly)
{
Tracer.Push (assembly);
try {
MarkAssembly (assembly);
foreach (TypeDefinition type in assembly.MainModule.Types)
InitializeType (type);
} finally {
Tracer.Pop ();
}
}
void InitializeType (TypeDefinition type)
{
if (type.HasNestedTypes) {
foreach (var nested in type.NestedTypes)
InitializeType (nested);
}
if (!Annotations.IsMarked (type))
return;
MarkType (type);
if (type.HasFields)
InitializeFields (type);
if (type.HasMethods)
InitializeMethods (type.Methods);
}
protected bool IsFullyPreserved (TypeDefinition type)
{
if (Annotations.TryGetPreserve (type, out TypePreserve preserve) && preserve == TypePreserve.All)
return true;
switch (Annotations.GetAction (type.Module.Assembly)) {
case AssemblyAction.Copy:
case AssemblyAction.CopyUsed:
case AssemblyAction.AddBypassNGen:
case AssemblyAction.AddBypassNGenUsed:
return true;
}
return false;
}
void InitializeFields (TypeDefinition type)
{
foreach (FieldDefinition field in type.Fields)
if (Annotations.IsMarked (field))
MarkField (field);
}
void InitializeMethods (Collection<MethodDefinition> methods)
{
foreach (MethodDefinition method in methods)
if (Annotations.IsMarked (method))
EnqueueMethod (method);
}
void Process ()
{
if (QueueIsEmpty ())
throw new InvalidOperationException ("No entry methods");
while (ProcessPrimaryQueue () || ProcessLazyAttributes () || ProcessLateMarkedAttributes ())
// deal with [TypeForwardedTo] pseudo-attributes
foreach (AssemblyDefinition assembly in _context.GetAssemblies ()) {
if (!assembly.MainModule.HasExportedTypes)
continue;
foreach (var exported in assembly.MainModule.ExportedTypes) {
bool isForwarder = exported.IsForwarder;
var declaringType = exported.DeclaringType;
while (!isForwarder && (declaringType != null)) {
isForwarder = declaringType.IsForwarder;
declaringType = declaringType.DeclaringType;
}
if (!isForwarder)
continue;
TypeDefinition type = exported.Resolve ();
if (type == null)
continue;
if (!Annotations.IsMarked (type))
continue;
Tracer.Push (type);
try {
MarkingHelpers.MarkExportedType (exported, assembly.MainModule);
} finally {
Tracer.Pop ();
}
}
}
}
bool ProcessPrimaryQueue ()
{
if (QueueIsEmpty ())
return false;
while (!QueueIsEmpty ()) {
ProcessQueue ();
ProcessVirtualMethods ();
ProcessMarkedTypesWithInterfaces ();
DoAdditionalProcessing ();
}
return true;
}
void ProcessQueue ()
{
while (!QueueIsEmpty ()) {
MethodDefinition method = _methods.Dequeue ();
Tracer.Push (method);
try {
ProcessMethod (method);
} catch (Exception e) {
throw new MarkException (string.Format ("Error processing method: '{0}' in assembly: '{1}'", method.FullName, method.Module.Name), e, method);
} finally {
Tracer.Pop ();
}
}
}
bool QueueIsEmpty ()
{
return _methods.Count == 0;
}
protected virtual void EnqueueMethod (MethodDefinition method)
{
_methods.Enqueue (method);
}
void ProcessVirtualMethods ()
{
foreach (MethodDefinition method in _virtual_methods) {
Tracer.Push (method);
ProcessVirtualMethod (method);
Tracer.Pop ();
}
}
void ProcessMarkedTypesWithInterfaces ()
{
// We may mark an interface type later on. Which means we need to reprocess any time with one or more interface implementations that have not been marked
// and if an interface type is found to be marked and implementation is not marked, then we need to mark that implementation
foreach (var type in _typesWithInterfaces) {
// Exception, types that have not been flagged as instantiated yet. These types may not need their interfaces even if the
// interface type is marked
if (!Annotations.IsInstantiated (type))
continue;
MarkInterfaceImplementations (type);
}
}
void ProcessVirtualMethod (MethodDefinition method)
{
var overrides = Annotations.GetOverrides (method);
if (overrides == null)
return;
foreach (MethodDefinition @override in overrides)
ProcessOverride (@override, method);
}
void ProcessOverride (MethodDefinition method, MethodDefinition @base)
{
if (!Annotations.IsMarked (method.DeclaringType))
return;
if (Annotations.IsProcessed (method))
return;
if (Annotations.IsMarked (method))
return;
var isInstantiated = Annotations.IsInstantiated (method.DeclaringType);
// We don't need to mark overrides until it is possible that the type could be instantiated
// Note : The base type is interface check should be removed once we have base type sweeping
if (!isInstantiated && @base.DeclaringType.IsInterface)
return;
if (!isInstantiated && !@base.IsAbstract)
return;
MarkMethod (method);
ProcessVirtualMethod (method);
}
void MarkMarshalSpec (IMarshalInfoProvider spec)
{
if (!spec.HasMarshalInfo)
return;
var marshaler = spec.MarshalInfo as CustomMarshalInfo;
if (marshaler == null)
return;
MarkType (marshaler.ManagedType);
}
void MarkCustomAttributes (ICustomAttributeProvider provider)
{
if (!provider.HasCustomAttributes)
return;
Tracer.Push (provider);
try {
foreach (CustomAttribute ca in provider.CustomAttributes) {
if (IsUserDependencyMarker (ca.AttributeType) && provider is MemberReference mr) {
MarkUserDependency (mr, ca);
continue;
}
if (_context.KeepUsedAttributeTypesOnly) {
_lateMarkedAttributes.Enqueue (new AttributeProviderPair (ca, provider));
} else {
if (!ShouldMarkCustomAttribute (ca, provider))
continue;
MarkCustomAttribute (ca);
}
}
} finally {
Tracer.Pop ();
}
}
protected virtual bool IsUserDependencyMarker (TypeReference type)
{
return PreserveDependencyLookupStep.IsPreserveDependencyAttribute (type);
}
protected virtual void MarkUserDependency (MemberReference context, CustomAttribute ca)
{
if (ca.HasProperties && ca.Properties [0].Name == "Condition") {
var condition = ca.Properties [0].Argument.Value as string;
switch (condition) {
case "":
case null:
break;
case "DEBUG":
if (!_context.KeepMembersForDebugger)
return;
break;
default:
// Don't have yet a way to match the general condition so everything is excluded
return;
}
}
AssemblyDefinition assembly;
var args = ca.ConstructorArguments;
if (args.Count >= 3 && args [2].Value is string assemblyName) {
if (!_context.Resolver.AssemblyCache.TryGetValue (assemblyName, out assembly)) {
_context.Logger.LogMessage (MessageImportance.Low, $"Could not resolve '{assemblyName}' assembly dependency");
return;
}
} else {
assembly = null;
}
TypeDefinition td = null;
if (args.Count >= 2 && args [1].Value is string typeName) {
td = FindType (assembly ?? context.Module.Assembly, typeName);
if (td == null) {
_context.Logger.LogMessage (MessageImportance.Low, $"Could not resolve '{typeName}' type dependency");
return;
}
} else {
td = context.DeclaringType.Resolve ();
}
string member = null;
string[] signature = null;
if (args.Count >= 1 && args [0].Value is string memberSignature) {
memberSignature = memberSignature.Replace (" ", "");
var sign_start = memberSignature.IndexOf ('(');
var sign_end = memberSignature.LastIndexOf (')');
if (sign_start > 0 && sign_end > sign_start) {
var parameters = memberSignature.Substring (sign_start + 1, sign_end - sign_start - 1);
signature = string.IsNullOrEmpty (parameters) ? Array.Empty<string> () : parameters.Split (',');
member = memberSignature.Substring (0, sign_start);
} else {
member = memberSignature;
}
}
if (MarkDependencyMethod (td, member, signature))
return;
if (MarkDependencyField (td, member))
return;
_context.Logger.LogMessage (MessageImportance.High, $"Could not resolve dependency member '{member}' declared in type '{td.FullName}'");
}
static TypeDefinition FindType (AssemblyDefinition assembly, string fullName)
{
fullName = fullName.ToCecilName ();
var type = assembly.MainModule.GetType (fullName);
return type?.Resolve ();
}
bool MarkDependencyMethod (TypeDefinition type, string name, string[] signature)
{
bool marked = false;
int arity;
int arity_marker = name.IndexOf ('`');
if (arity_marker < 1 || !int.TryParse (name.Substring (arity_marker + 1), out arity)) {
arity = 0;
} else {
name = name.Substring (0, arity_marker);
}
foreach (var m in type.Methods) {
if (m.Name != name)
continue;
if (m.GenericParameters.Count != arity)
continue;
if (signature == null) {
MarkMethod (m);
marked = true;
continue;
}
var mp = m.Parameters;
if (mp.Count != signature.Length)
continue;
int i = 0;
for (; i < signature.Length; ++i) {
if (mp [i].ParameterType.FullName != signature [i].Trim ().ToCecilName ()) {
i = -1;
break;
}
}
if (i < 0)
continue;
MarkMethod (m);
marked = true;
}
return marked;
}
bool MarkDependencyField (TypeDefinition type, string name)
{
foreach (var f in type.Fields) {
if (f.Name == name) {
MarkField (f);
return true;
}
}
return false;
}
void LazyMarkCustomAttributes (ICustomAttributeProvider provider, AssemblyDefinition assembly)
{
if (!provider.HasCustomAttributes)
return;
foreach (CustomAttribute ca in provider.CustomAttributes)
_assemblyLevelAttributes.Enqueue (new AttributeProviderPair (ca, assembly));
}
protected virtual void MarkCustomAttribute (CustomAttribute ca)
{
Tracer.Push ((object)ca.AttributeType ?? (object)ca);
try {
Annotations.Mark (ca);
MarkMethod (ca.Constructor);
MarkCustomAttributeArguments (ca);
TypeReference constructor_type = ca.Constructor.DeclaringType;
TypeDefinition type = constructor_type.Resolve ();
if (type == null) {
HandleUnresolvedType (constructor_type);
return;
}
MarkCustomAttributeProperties (ca, type);
MarkCustomAttributeFields (ca, type);
} finally {
Tracer.Pop ();
}
}
protected virtual bool ShouldMarkCustomAttribute (CustomAttribute ca, ICustomAttributeProvider provider)
{
var attr_type = ca.AttributeType;
if (_context.KeepUsedAttributeTypesOnly) {
switch (attr_type.FullName) {
// [ThreadStatic] and [ContextStatic] are required by the runtime
case "System.ThreadStaticAttribute":
case "System.ContextStaticAttribute":
return true;
// Attributes related to `fixed` keyword used to declare fixed length arrays
case "System.Runtime.CompilerServices.FixedBufferAttribute":
return true;
case "System.Runtime.InteropServices.InterfaceTypeAttribute":
case "System.Runtime.InteropServices.GuidAttribute":
return true;
}
if (!Annotations.IsMarked (attr_type.Resolve ()))
return false;
}
return true;
}
protected virtual bool ShouldMarkTypeStaticConstructor (TypeDefinition type)
{
if (Annotations.HasPreservedStaticCtor (type))
return false;
if (type.IsBeforeFieldInit && _context.IsOptimizationEnabled (CodeOptimizations.BeforeFieldInit))
return false;
return true;
}
protected void MarkStaticConstructor (TypeDefinition type)
{
if (MarkMethodIf (type.Methods, IsNonEmptyStaticConstructor) != null)
Annotations.SetPreservedStaticCtor (type);
}
protected virtual bool ShouldMarkTopLevelCustomAttribute (AttributeProviderPair app, MethodDefinition resolvedConstructor)
{
var ca = app.Attribute;
if (!ShouldMarkCustomAttribute (app.Attribute, app.Provider))
return false;
// If an attribute's module has not been marked after processing all types in all assemblies and the attribute itself has not been marked,
// then surely nothing is using this attribute and there is no need to mark it
if (!Annotations.IsMarked (resolvedConstructor.Module) && !Annotations.IsMarked (ca.AttributeType))
return false;
if (ca.Constructor.DeclaringType.Namespace == "System.Diagnostics") {
string attributeName = ca.Constructor.DeclaringType.Name;
if (attributeName == "DebuggerDisplayAttribute" || attributeName == "DebuggerTypeProxyAttribute") {
var displayTargetType = GetDebuggerAttributeTargetType (app.Attribute, (AssemblyDefinition) app.Provider);
if (displayTargetType == null || !Annotations.IsMarked (displayTargetType))
return false;
}
}
return true;
}
protected void MarkSecurityDeclarations (ISecurityDeclarationProvider provider)
{
// most security declarations are removed (if linked) but user code might still have some
// and if the attributes references types then they need to be marked too
if ((provider == null) || !provider.HasSecurityDeclarations)
return;
foreach (var sd in provider.SecurityDeclarations)
MarkSecurityDeclaration (sd);
}
protected virtual void MarkSecurityDeclaration (SecurityDeclaration sd)
{
if (!sd.HasSecurityAttributes)
return;
foreach (var sa in sd.SecurityAttributes)
MarkSecurityAttribute (sa);
}
protected virtual void MarkSecurityAttribute (SecurityAttribute sa)
{
TypeReference security_type = sa.AttributeType;
TypeDefinition type = security_type.Resolve ();
if (type == null)
throw new ResolutionException (security_type);
MarkType (security_type);
MarkSecurityAttributeProperties (sa, type);
MarkSecurityAttributeFields (sa, type);
}
protected void MarkSecurityAttributeProperties (SecurityAttribute sa, TypeDefinition attribute)
{
if (!sa.HasProperties)
return;
foreach (var named_argument in sa.Properties)
MarkCustomAttributeProperty (named_argument, attribute);
}
protected void MarkSecurityAttributeFields (SecurityAttribute sa, TypeDefinition attribute)
{
if (!sa.HasFields)
return;
foreach (var named_argument in sa.Fields)
MarkCustomAttributeField (named_argument, attribute);
}
protected void MarkCustomAttributeProperties (CustomAttribute ca, TypeDefinition attribute)
{
if (!ca.HasProperties)
return;
foreach (var named_argument in ca.Properties)
MarkCustomAttributeProperty (named_argument, attribute);
}
protected void MarkCustomAttributeProperty (CustomAttributeNamedArgument namedArgument, TypeDefinition attribute)
{
PropertyDefinition property = GetProperty (attribute, namedArgument.Name);
Tracer.Push (property);
if (property != null)
MarkMethod (property.SetMethod);
MarkIfType (namedArgument.Argument);
Tracer.Pop ();
}
PropertyDefinition GetProperty (TypeDefinition type, string propertyname)
{
while (type != null) {
PropertyDefinition property = type.Properties.FirstOrDefault (p => p.Name == propertyname);
if (property != null)
return property;
type = type.BaseType != null ? ResolveTypeDefinition (type.BaseType) : null;
}
return null;
}
protected void MarkCustomAttributeFields (CustomAttribute ca, TypeDefinition attribute)
{
if (!ca.HasFields)
return;
foreach (var named_argument in ca.Fields)
MarkCustomAttributeField (named_argument, attribute);
}
protected void MarkCustomAttributeField (CustomAttributeNamedArgument namedArgument, TypeDefinition attribute)
{
FieldDefinition field = GetField (attribute, namedArgument.Name);
if (field != null)
MarkField (field);
MarkIfType (namedArgument.Argument);
}
FieldDefinition GetField (TypeDefinition type, string fieldname)
{
while (type != null) {
FieldDefinition field = type.Fields.FirstOrDefault (f => f.Name == fieldname);
if (field != null)
return field;
type = type.BaseType != null ? ResolveTypeDefinition (type.BaseType) : null;
}
return null;
}
MethodDefinition GetMethodWithNoParameters (TypeDefinition type, string methodname)
{
while (type != null) {
MethodDefinition method = type.Methods.FirstOrDefault (m => m.Name == methodname && !m.HasParameters);
if (method != null)
return method;
type = type.BaseType != null ? ResolveTypeDefinition (type.BaseType) : null;
}
return null;
}
void MarkCustomAttributeArguments (CustomAttribute ca)
{
if (!ca.HasConstructorArguments)
return;
foreach (var argument in ca.ConstructorArguments)
MarkIfType (argument);
}
void MarkIfType (CustomAttributeArgument argument)
{
var at = argument.Type;
if (at.IsArray) {
var et = at.GetElementType ();
if (et.Namespace != "System" || et.Name != "Type")
return;
MarkType (et);
if (argument.Value == null)
return;
foreach (var cac in (CustomAttributeArgument[]) argument.Value)
MarkWithResolvedScope ((TypeReference) cac.Value);
} else if (at.Namespace == "System" && at.Name == "Type") {
MarkType (argument.Type);
MarkWithResolvedScope ((TypeReference) argument.Value);
}
}
// custom attributes encoding means it's possible to have a scope that will point into a PCL facade
// even if we (just before saving) will resolve all type references (bug #26752)
void MarkWithResolvedScope (TypeReference type)
{
if (type == null)
return;
// a GenericInstanceType can could contains generic arguments with scope that
// needs to be updated out of the PCL facade (bug #28823)
var git = (type as GenericInstanceType);
if ((git != null) && git.HasGenericArguments) {
foreach (var ga in git.GenericArguments)
MarkWithResolvedScope (ga);
}
// we cannot set the Scope of a TypeSpecification but it's element type can be set
// e.g. System.String[] -> System.String
var ts = (type as TypeSpecification);
if (ts != null) {
MarkWithResolvedScope (ts.ElementType);
return;
}
var td = type.Resolve ();
if (td != null)
type.Scope = td.Scope;
MarkType (type);
}
protected bool CheckProcessed (IMetadataTokenProvider provider)
{
if (Annotations.IsProcessed (provider))
return true;
Annotations.Processed (provider);
return false;
}
protected void MarkAssembly (AssemblyDefinition assembly)
{
if (CheckProcessed (assembly))
return;
ProcessModule (assembly);
MarkAssemblyCustomAttributes (assembly);
MarkSecurityDeclarations (assembly);
foreach (ModuleDefinition module in assembly.Modules)
LazyMarkCustomAttributes (module, assembly);
}
void ProcessModule (AssemblyDefinition assembly)
{
// Pre-mark <Module> if there is any methods as they need to be executed
// at assembly load time
foreach (TypeDefinition type in assembly.MainModule.Types)
{
if (type.Name == "<Module>" && type.HasMethods)
{
MarkType (type);
break;
}
}
}
bool ProcessLazyAttributes ()
{
var startingQueueCount = _assemblyLevelAttributes.Count;
if (startingQueueCount == 0)
return false;
var skippedItems = new List<AttributeProviderPair> ();
var markOccurred = false;
while (_assemblyLevelAttributes.Count != 0) {
var assemblyLevelAttribute = _assemblyLevelAttributes.Dequeue ();
var customAttribute = assemblyLevelAttribute.Attribute;
var resolved = customAttribute.Constructor.Resolve ();
if (resolved == null) {
HandleUnresolvedMethod (customAttribute.Constructor);
continue;
}
if (!ShouldMarkTopLevelCustomAttribute (assemblyLevelAttribute, resolved)) {
skippedItems.Add (assemblyLevelAttribute);
continue;
}
string attributeFullName = customAttribute.Constructor.DeclaringType.FullName;
switch (attributeFullName) {
case "System.Diagnostics.DebuggerDisplayAttribute":
MarkTypeWithDebuggerDisplayAttribute (GetDebuggerAttributeTargetType (assemblyLevelAttribute.Attribute, (AssemblyDefinition) assemblyLevelAttribute.Provider), customAttribute);
break;
case "System.Diagnostics.DebuggerTypeProxyAttribute":
MarkTypeWithDebuggerTypeProxyAttribute (GetDebuggerAttributeTargetType (assemblyLevelAttribute.Attribute, (AssemblyDefinition) assemblyLevelAttribute.Provider), customAttribute);
break;
}
markOccurred = true;
MarkCustomAttribute (customAttribute);
}
// requeue the items we skipped in case we need to make another pass
foreach (var item in skippedItems)
_assemblyLevelAttributes.Enqueue (item);
return markOccurred;
}
bool ProcessLateMarkedAttributes ()
{
var startingQueueCount = _lateMarkedAttributes.Count;
if (startingQueueCount == 0)
return false;
var skippedItems = new List<AttributeProviderPair> ();
var markOccurred = false;
while (_lateMarkedAttributes.Count != 0) {
var attributeProviderPair = _lateMarkedAttributes.Dequeue ();
var customAttribute = attributeProviderPair.Attribute;
var resolved = customAttribute.Constructor.Resolve ();
if (resolved == null) {
HandleUnresolvedMethod (customAttribute.Constructor);
continue;
}
if (!ShouldMarkCustomAttribute (customAttribute, attributeProviderPair.Provider)) {
skippedItems.Add (attributeProviderPair);
continue;
}
markOccurred = true;
MarkCustomAttribute (customAttribute);
}
// requeue the items we skipped in case we need to make another pass
foreach (var item in skippedItems)
_lateMarkedAttributes.Enqueue (item);
return markOccurred;
}
protected void MarkField (FieldReference reference)
{
// if (IgnoreScope (reference.DeclaringType.Scope))
// return;
if (reference.DeclaringType is GenericInstanceType)
MarkType (reference.DeclaringType);
FieldDefinition field = ResolveFieldDefinition (reference);
if (field == null)
throw new ResolutionException (reference);
if (CheckProcessed (field))
return;
MarkType (field.DeclaringType);
MarkType (field.FieldType);
MarkCustomAttributes (field);
MarkMarshalSpec (field);
DoAdditionalFieldProcessing (field);
var parent = reference.DeclaringType.Resolve ();
if (!Annotations.HasPreservedStaticCtor (parent))
MarkStaticConstructor (parent);
Annotations.Mark (field);
}
protected virtual bool IgnoreScope (IMetadataScope scope)
{
AssemblyDefinition assembly = ResolveAssembly (scope);
return Annotations.GetAction (assembly) != AssemblyAction.Link;
}
FieldDefinition ResolveFieldDefinition (FieldReference field)
{
FieldDefinition fd = field as FieldDefinition;
if (fd == null)
fd = field.Resolve ();
return fd;
}
void MarkScope (IMetadataScope scope)
{
var provider = scope as IMetadataTokenProvider;
if (provider == null)
return;
Annotations.Mark (provider);
}
protected virtual void MarkSerializable (TypeDefinition type)
{
MarkDefaultConstructor (type);
MarkMethodsIf (type.Methods, IsSpecialSerializationConstructor);
}
protected virtual TypeDefinition MarkType (TypeReference reference)
{
if (reference == null)
return null;
reference = GetOriginalType (reference);
if (reference is FunctionPointerType)
return null;
if (reference is GenericParameter)
return null;
// if (IgnoreScope (reference.Scope))
// return null;
TypeDefinition type = ResolveTypeDefinition (reference);
if (type == null) {
HandleUnresolvedType (reference);
return null;
}
if (CheckProcessed (type))
return null;
Tracer.Push (type);
MarkScope (type.Scope);
MarkType (type.BaseType);
MarkType (type.DeclaringType);
MarkCustomAttributes (type);
MarkSecurityDeclarations (type);
if (IsMulticastDelegate (type)) {
MarkMulticastDelegate (type);
}
if (IsSerializable (type))
MarkSerializable (type);
if (!_context.IsFeatureExcluded ("etw") && BCL.EventTracingForWindows.IsEventSourceImplementation (type, _context)) {
MarkEventSourceProviders (type);
}
MarkTypeSpecialCustomAttributes (type);
MarkGenericParameterProvider (type);
// keep fields for value-types and for classes with LayoutKind.Sequential or Explicit
if (type.IsValueType || !type.IsAutoLayout)
MarkFields (type, type.IsEnum);
// There are a number of markings we can defer until later when we know it's possible a reference type could be instantiated
// For example, if no instance of a type exist, then we don't need to mark the interfaces on that type
// However, for some other types there is no benefit to deferring
if (type.IsInterface) {
// There's no benefit to deferring processing of an interface type until we know a type implementing that interface is marked
MarkRequirementsForInstantiatedTypes (type);
} else if (type.IsValueType) {
// Note : Technically interfaces could be removed from value types in some of the same cases as reference types, however, it's harder to know when
// a value type instance could exist. You'd have to track initobj and maybe locals types. Going to punt for now.
MarkRequirementsForInstantiatedTypes (type);
} else if (IsFullyPreserved (type)) {
// Here for a couple reasons:
// * Edge case to cover a scenario where a type has preserve all, implements interfaces, but does not have any instance ctors.
// Normally TypePreserve.All would cause an instance ctor to be marked and that would in turn lead to MarkInterfaceImplementations being called
// Without an instance ctor, MarkInterfaceImplementations is not called and then TypePreserve.All isn't truly respected.
// * If an assembly has the action Copy and had ResolveFromAssemblyStep ran for the assembly, then InitializeType will have led us here
// When the entire assembly is preserved, then all interfaces, base, etc will be preserved on the type, so we need to make sure
// all of these types are marked. For example, if an interface implementation is of a type in another assembly that is linked,
// and there are no other usages of that interface type, then we need to make sure the interface type is still marked because
// this type is going to retain the interface implementation
MarkRequirementsForInstantiatedTypes (type);
} else if (AlwaysMarkTypeAsInstantiated (type)) {
MarkRequirementsForInstantiatedTypes (type);
}
if (type.HasInterfaces)
_typesWithInterfaces.Add (type);
if (type.HasMethods) {
if (ShouldMarkTypeStaticConstructor (type))
MarkStaticConstructor (type);
MarkMethodsIf (type.Methods, HasSerializationAttribute);
}
DoAdditionalTypeProcessing (type);
Tracer.Pop ();
Annotations.Mark (type);
ApplyPreserveInfo (type);
return type;
}
// Allow subclassers to mark additional things in the main processing loop
protected virtual void DoAdditionalProcessing ()
{
}
// Allow subclassers to mark additional things
protected virtual void DoAdditionalTypeProcessing (TypeDefinition type)
{
}
// Allow subclassers to mark additional things
protected virtual void DoAdditionalFieldProcessing (FieldDefinition field)
{
}
// Allow subclassers to mark additional things
protected virtual void DoAdditionalPropertyProcessing (PropertyDefinition property)
{
}
// Allow subclassers to mark additional things
protected virtual void DoAdditionalEventProcessing (EventDefinition evt)
{
}
// Allow subclassers to mark additional things
protected virtual void DoAdditionalInstantiatedTypeProcessing (TypeDefinition type)
{
}
void MarkAssemblyCustomAttributes (AssemblyDefinition assembly)
{
if (!assembly.HasCustomAttributes)
return;
foreach (CustomAttribute attribute in assembly.CustomAttributes)
_assemblyLevelAttributes.Enqueue (new AttributeProviderPair (attribute, assembly));
}
TypeDefinition GetDebuggerAttributeTargetType (CustomAttribute ca, AssemblyDefinition asm)
{
TypeReference targetTypeReference = null;
foreach (var property in ca.Properties) {
if (property.Name == "Target") {
targetTypeReference = (TypeReference) property.Argument.Value;
break;
}
if (property.Name == "TargetTypeName") {
if (TypeNameParser.TryParseTypeAssemblyQualifiedName ((string) property.Argument.Value, out string typeName, out string assemblyName)) {
if (string.IsNullOrEmpty (assemblyName))
targetTypeReference = asm.MainModule.GetType (typeName);
else
targetTypeReference = _context.GetAssemblies ().FirstOrDefault (a => a.Name.Name == assemblyName)?.MainModule.GetType (typeName);
}
break;
}
}
if (targetTypeReference != null)
return ResolveTypeDefinition (targetTypeReference);
return null;
}
void MarkTypeSpecialCustomAttributes (TypeDefinition type)
{
if (!type.HasCustomAttributes)
return;
foreach (CustomAttribute attribute in type.CustomAttributes) {
switch (attribute.Constructor.DeclaringType.FullName) {
case "System.Xml.Serialization.XmlSchemaProviderAttribute":
MarkXmlSchemaProvider (type, attribute);
break;
case "System.Diagnostics.DebuggerDisplayAttribute":
MarkTypeWithDebuggerDisplayAttribute (type, attribute);
break;
case "System.Diagnostics.DebuggerTypeProxyAttribute":
MarkTypeWithDebuggerTypeProxyAttribute (type, attribute);
break;
case "System.Diagnostics.Tracing.EventDataAttribute":
MarkMethodsIf (type.Methods, IsPublicInstancePropertyMethod);
break;
}
}
}
void MarkMethodSpecialCustomAttributes (MethodDefinition method)
{
if (!method.HasCustomAttributes)
return;
foreach (CustomAttribute attribute in method.CustomAttributes) {
switch (attribute.Constructor.DeclaringType.FullName) {
case "System.Web.Services.Protocols.SoapHeaderAttribute":
MarkSoapHeader (method, attribute);
break;
}
}
}
void MarkXmlSchemaProvider (TypeDefinition type, CustomAttribute attribute)
{
string method_name;
if (!TryGetStringArgument (attribute, out method_name))
return;
MarkNamedMethod (type, method_name);
}
void MarkTypeWithDebuggerDisplayAttribute (TypeDefinition type, CustomAttribute attribute)
{
if (_context.KeepMembersForDebugger) {
string displayString = (string) attribute.ConstructorArguments[0].Value;
Regex regex = new Regex ("{[^{}]+}", RegexOptions.Compiled);
foreach (Match match in regex.Matches (displayString)) {
// Remove '{' and '}'
string realMatch = match.Value.Substring (1, match.Value.Length - 2);
// Remove ",nq" suffix if present
// (it asks the expression evaluator to remove the quotes when displaying the final value)
if (Regex.IsMatch(realMatch, @".+,\s*nq")) {
realMatch = realMatch.Substring (0, realMatch.LastIndexOf (','));
}
if (realMatch.EndsWith ("()")) {
string methodName = realMatch.Substring (0, realMatch.Length - 2);
MethodDefinition method = GetMethodWithNoParameters (type, methodName);
if (method != null) {
MarkMethod (method);
continue;
}
} else {
FieldDefinition field = GetField (type, realMatch);
if (field != null) {
MarkField (field);
continue;
}
PropertyDefinition property = GetProperty (type, realMatch);
if (property != null) {
if (property.GetMethod != null) {
MarkMethod (property.GetMethod);
}
if (property.SetMethod != null) {
MarkMethod (property.SetMethod);
}
continue;
}
}
while (type != null) {
MarkMethods (type);
MarkFields (type, includeStatic: true);
type = type.BaseType != null ? ResolveTypeDefinition (type.BaseType) : null;
}
return;
}
}
}
void MarkTypeWithDebuggerTypeProxyAttribute (TypeDefinition type, CustomAttribute attribute)
{
if (_context.KeepMembersForDebugger) {
object constructorArgument = attribute.ConstructorArguments[0].Value;
TypeReference proxyTypeReference = constructorArgument as TypeReference;
if (proxyTypeReference == null) {
string proxyTypeReferenceString = constructorArgument as string;
if (proxyTypeReferenceString != null) {
proxyTypeReference = type.Module.GetType (proxyTypeReferenceString, runtimeName: true);
}
}
if (proxyTypeReference == null) {
return;
}
MarkType (proxyTypeReference);
TypeDefinition proxyType = ResolveTypeDefinition (proxyTypeReference);
if (proxyType != null) {
MarkMethods (proxyType);
MarkFields (proxyType, includeStatic: true);
}
}
}
static bool TryGetStringArgument (CustomAttribute attribute, out string argument)
{
argument = null;
if (attribute.ConstructorArguments.Count < 1)
return false;
argument = attribute.ConstructorArguments [0].Value as string;
return argument != null;
}
protected int MarkNamedMethod (TypeDefinition type, string method_name)
{
if (!type.HasMethods)
return 0;
int count = 0;
foreach (MethodDefinition method in type.Methods) {
if (method.Name != method_name)
continue;
MarkMethod (method);
count++;
}
return count;
}
void MarkSoapHeader (MethodDefinition method, CustomAttribute attribute)
{
string member_name;
if (!TryGetStringArgument (attribute, out member_name))
return;
MarkNamedField (method.DeclaringType, member_name);
MarkNamedProperty (method.DeclaringType, member_name);
}
void MarkNamedField (TypeDefinition type, string field_name)
{
if (!type.HasFields)
return;
foreach (FieldDefinition field in type.Fields) {
if (field.Name != field_name)
continue;
MarkField (field);
}
}
void MarkNamedProperty (TypeDefinition type, string property_name)
{
if (!type.HasProperties)
return;
foreach (PropertyDefinition property in type.Properties) {
if (property.Name != property_name)
continue;
Tracer.Push (property);
MarkMethod (property.GetMethod);
MarkMethod (property.SetMethod);
Tracer.Pop ();
}
}
void MarkInterfaceImplementations (TypeDefinition type)
{
if (!type.HasInterfaces)
return;
foreach (var iface in type.Interfaces) {
// Only mark interface implementations of interface types that have been marked.
// This enables stripping of interfaces that are never used
var resolvedInterfaceType = iface.InterfaceType.Resolve ();
if (resolvedInterfaceType == null) {
HandleUnresolvedType (iface.InterfaceType);
continue;
}
if (ShouldMarkInterfaceImplementation (type, iface, resolvedInterfaceType))
MarkInterfaceImplementation (iface);
}
}
void MarkGenericParameterProvider (IGenericParameterProvider provider)
{
if (!provider.HasGenericParameters)
return;
foreach (GenericParameter parameter in provider.GenericParameters)
MarkGenericParameter (parameter);
}
void MarkGenericParameter (GenericParameter parameter)
{
MarkCustomAttributes (parameter);
foreach (TypeReference constraint in parameter.Constraints)
MarkType (constraint);
}
bool IsVirtualAndHasPreservedParent (MethodDefinition method)
{
if (!method.IsVirtual)
return false;
var base_list = Annotations.GetBaseMethods (method);
if (base_list == null)
return false;
foreach (MethodDefinition @base in base_list) {
if (IgnoreScope (@base.DeclaringType.Scope))
return true;
if (IsVirtualAndHasPreservedParent (@base))
return true;
}
return false;
}
static bool IsSpecialSerializationConstructor (MethodDefinition method)
{
if (!method.IsInstanceConstructor ())
return false;
var parameters = method.Parameters;
if (parameters.Count != 2)
return false;
return parameters [0].ParameterType.Name == "SerializationInfo" &&
parameters [1].ParameterType.Name == "StreamingContext";
}
protected void MarkMethodsIf (Collection<MethodDefinition> methods, Func<MethodDefinition, bool> predicate)
{
foreach (MethodDefinition method in methods)
if (predicate (method))
MarkMethod (method);
}
protected MethodDefinition MarkMethodIf (Collection<MethodDefinition> methods, Func<MethodDefinition, bool> predicate)
{
foreach (MethodDefinition method in methods) {
if (predicate (method)) {
return MarkMethod (method);
}
}
return null;
}
protected bool MarkDefaultConstructor (TypeDefinition type)
{
if (type?.HasMethods != true)
return false;
return MarkMethodIf (type.Methods, MethodDefinitionExtensions.IsDefaultConstructor) != null;
}
static bool IsNonEmptyStaticConstructor (MethodDefinition method)
{
if (!method.IsStaticConstructor ())
return false;
if (!method.HasBody || !method.IsIL)
return true;
if (method.Body.CodeSize != 1)
return true;
return method.Body.Instructions [0].OpCode.Code != Code.Ret;
}
static bool HasSerializationAttribute (MethodDefinition method)
{
if (!method.HasCustomAttributes)
return false;
foreach (var ca in method.CustomAttributes) {
var cat = ca.AttributeType;
if (cat.Namespace != "System.Runtime.Serialization")
continue;
switch (cat.Name) {
case "OnDeserializedAttribute":
case "OnDeserializingAttribute":
case "OnSerializedAttribute":
case "OnSerializingAttribute":
return true;
}
}
return false;
}
static bool IsSerializable (TypeDefinition td)
{
return (td.Attributes & TypeAttributes.Serializable) != 0;
}
static bool IsMulticastDelegate (TypeDefinition td)
{
return td.BaseType != null && td.BaseType.FullName == "System.MulticastDelegate";
}
protected virtual bool AlwaysMarkTypeAsInstantiated (TypeDefinition td)
{
switch (td.Name) {
// These types are created from native code which means we are unable to track when they are instantiated
// Since these are such foundational types, let's take the easy route and just always assume an instance of one of these
// could exist
case "Delegate":
case "MulticastDelegate":
return td.Namespace == "System";
}
return false;
}
void MarkEventSourceProviders (TypeDefinition td)
{
foreach (var nestedType in td.NestedTypes) {
if (BCL.EventTracingForWindows.IsProviderName (nestedType.Name))
MarkStaticFields (nestedType);
}
}
protected virtual void MarkMulticastDelegate (TypeDefinition type)
{
MarkMethodCollection (type.Methods);
}
protected static TypeDefinition ResolveTypeDefinition (TypeReference type)
{
TypeDefinition td = type as TypeDefinition;
if (td == null)
td = type.Resolve ();
return td;
}
protected TypeReference GetOriginalType (TypeReference type)
{
while (type is TypeSpecification) {
GenericInstanceType git = type as GenericInstanceType;
if (git != null)
MarkGenericArguments (git);
var mod = type as IModifierType;
if (mod != null)
MarkModifierType (mod);
var fnptr = type as FunctionPointerType;
if (fnptr != null) {
MarkParameters (fnptr);
MarkType (fnptr.ReturnType);
break; // FunctionPointerType is the original type
}
else {
type = ((TypeSpecification) type).ElementType;
}
}
return type;
}
void MarkParameters (FunctionPointerType fnptr)
{
if (!fnptr.HasParameters)
return;
for (int i = 0; i < fnptr.Parameters.Count; i++)
{
MarkType (fnptr.Parameters[i].ParameterType);
}
}
void MarkModifierType (IModifierType mod)
{
MarkType (mod.ModifierType);
}
void MarkGenericArguments (IGenericInstance instance)
{
foreach (TypeReference argument in instance.GenericArguments)
MarkType (argument);
MarkGenericArgumentConstructors (instance);
}
void MarkGenericArgumentConstructors (IGenericInstance instance)
{
var arguments = instance.GenericArguments;
var generic_element = GetGenericProviderFromInstance (instance);
if (generic_element == null)
return;
var parameters = generic_element.GenericParameters;
if (arguments.Count != parameters.Count)
return;
for (int i = 0; i < arguments.Count; i++) {
var argument = arguments [i];
var parameter = parameters [i];
if (!parameter.HasDefaultConstructorConstraint)
continue;
var argument_definition = ResolveTypeDefinition (argument);
MarkDefaultConstructor (argument_definition);
}
}
IGenericParameterProvider GetGenericProviderFromInstance (IGenericInstance instance)
{
var method = instance as GenericInstanceMethod;
if (method != null)
return ResolveMethodDefinition (method.ElementMethod);
var type = instance as GenericInstanceType;
if (type != null)
return ResolveTypeDefinition (type.ElementType);
return null;
}
void ApplyPreserveInfo (TypeDefinition type)
{
ApplyPreserveMethods (type);
if (!Annotations.TryGetPreserve (type, out TypePreserve preserve))
return;
switch (preserve) {
case TypePreserve.All:
MarkFields (type, true);
MarkMethods (type);
break;
case TypePreserve.Fields:
if (!MarkFields (type, true, true))
_context.LogMessage ($"Type {type.FullName} has no fields to preserve");
break;
case TypePreserve.Methods:
if (!MarkMethods (type))
_context.LogMessage ($"Type {type.FullName} has no methods to preserve");
break;
}
}
void ApplyPreserveMethods (TypeDefinition type)
{
var list = Annotations.GetPreservedMethods (type);
if (list == null)
return;
MarkMethodCollection (list);
}
void ApplyPreserveMethods (MethodDefinition method)
{
var list = Annotations.GetPreservedMethods (method);
if (list == null)
return;
MarkMethodCollection (list);
}
protected bool MarkFields (TypeDefinition type, bool includeStatic, bool markBackingFieldsOnlyIfPropertyMarked = false)
{
if (!type.HasFields)
return false;
foreach (FieldDefinition field in type.Fields) {
if (!includeStatic && field.IsStatic)
continue;
if (markBackingFieldsOnlyIfPropertyMarked && field.Name.EndsWith (">k__BackingField", StringComparison.Ordinal)) {
// We can't reliably construct the expected property name from the backing field name for all compilers
// because csc shortens the name of the backing field in some cases
// For example:
// Field Name = <IFoo<int>.Bar>k__BackingField
// Property Name = IFoo<System.Int32>.Bar
//
// instead we will search the properties and find the one that makes use of the current backing field
var propertyDefinition = SearchPropertiesForMatchingFieldDefinition (field);
if (propertyDefinition != null && !Annotations.IsMarked (propertyDefinition))
continue;
}
MarkField (field);
}
return true;
}
static PropertyDefinition SearchPropertiesForMatchingFieldDefinition (FieldDefinition field)
{
foreach (var property in field.DeclaringType.Properties) {
var instr = property.GetMethod?.Body?.Instructions;
if (instr == null)
continue;
foreach (var ins in instr) {
if (ins?.Operand == field)
return property;
}
}
return null;
}
protected void MarkStaticFields (TypeDefinition type)
{
if (!type.HasFields)
return;
foreach (FieldDefinition field in type.Fields) {
if (field.IsStatic)
MarkField (field);
}
}
protected virtual bool MarkMethods (TypeDefinition type)
{
if (!type.HasMethods)
return false;
MarkMethodCollection (type.Methods);
return true;
}
void MarkMethodCollection (IList<MethodDefinition> methods)
{
foreach (MethodDefinition method in methods)
MarkMethod (method);
}
protected virtual MethodDefinition MarkMethod (MethodReference reference)
{
reference = GetOriginalMethod (reference);
if (reference.DeclaringType is ArrayType)
return null;
Tracer.Push (reference);
if (reference.DeclaringType is GenericInstanceType)
MarkType (reference.DeclaringType);
// if (IgnoreScope (reference.DeclaringType.Scope))
// return;
MethodDefinition method = ResolveMethodDefinition (reference);
try {
if (method == null) {
HandleUnresolvedMethod (reference);
return null;
}
if (Annotations.GetAction (method) == MethodAction.Nothing)
Annotations.SetAction (method, MethodAction.Parse);
EnqueueMethod (method);
} finally {
Tracer.Pop ();
}
Tracer.AddDependency (method);
return method;
}
AssemblyDefinition ResolveAssembly (IMetadataScope scope)
{
AssemblyDefinition assembly = _context.Resolve (scope);
MarkAssembly (assembly);
return assembly;
}
protected MethodReference GetOriginalMethod (MethodReference method)
{
while (method is MethodSpecification) {
GenericInstanceMethod gim = method as GenericInstanceMethod;
if (gim != null)
MarkGenericArguments (gim);
method = ((MethodSpecification) method).ElementMethod;
}
return method;
}
MethodDefinition ResolveMethodDefinition (MethodReference method)
{
MethodDefinition md = method as MethodDefinition;
if (md == null)
md = method.Resolve ();
return md;
}
protected virtual void ProcessMethod (MethodDefinition method)
{
if (CheckProcessed (method))
return;
Tracer.Push (method);
MarkType (method.DeclaringType);
MarkCustomAttributes (method);
MarkSecurityDeclarations (method);
MarkGenericParameterProvider (method);
if (ShouldMarkAsInstancePossible (method))
MarkRequirementsForInstantiatedTypes (method.DeclaringType);
if (IsPropertyMethod (method))
MarkProperty (GetProperty (method));
else if (IsEventMethod (method))
MarkEvent (GetEvent (method));
if (method.HasParameters) {
foreach (ParameterDefinition pd in method.Parameters) {
MarkType (pd.ParameterType);
MarkCustomAttributes (pd);
MarkMarshalSpec (pd);
}
}
if (method.HasOverrides) {
foreach (MethodReference ov in method.Overrides) {
MarkMethod (ov);
MarkExplicitInterfaceImplementation (method, ov);
}
}
MarkMethodSpecialCustomAttributes (method);
if (method.IsVirtual)
_virtual_methods.Add (method);
MarkNewCodeDependencies (method);
MarkBaseMethods (method);
MarkType (method.ReturnType);
MarkCustomAttributes (method.MethodReturnType);
MarkMarshalSpec (method.MethodReturnType);
if (method.IsPInvokeImpl || method.IsInternalCall) {
ProcessInteropMethod (method);
}
if (ShouldParseMethodBody (method))
MarkMethodBody (method.Body);
DoAdditionalMethodProcessing (method);
Annotations.Mark (method);
ApplyPreserveMethods (method);
Tracer.Pop ();
}
// Allow subclassers to mark additional things when marking a method
protected virtual void DoAdditionalMethodProcessing (MethodDefinition method)
{
}
protected virtual bool ShouldMarkAsInstancePossible (MethodDefinition method)
{
// We don't need to mark it multiple times
if (Annotations.IsInstantiated (method.DeclaringType))
return false;
if (method.IsInstanceConstructor ())
return true;
if (method.DeclaringType.IsInterface)
return true;
return false;
}
protected virtual void MarkRequirementsForInstantiatedTypes (TypeDefinition type)
{
if (Annotations.IsInstantiated (type))
return;
Annotations.MarkInstantiated (type);
MarkInterfaceImplementations (type);
foreach (var method in type.Methods) {
if (method.IsFinalizer ())
MarkMethod (method);
else if (IsVirtualAndHasPreservedParent (method))
MarkMethod (method);
}
DoAdditionalInstantiatedTypeProcessing (type);
}
void MarkExplicitInterfaceImplementation (MethodDefinition method, MethodReference ov)
{
var resolvedOverride = ov.Resolve ();
if (resolvedOverride == null) {
HandleUnresolvedMethod (ov);
return;
}
if (resolvedOverride.DeclaringType.IsInterface) {
foreach (var ifaceImpl in method.DeclaringType.Interfaces) {
var resolvedInterfaceType = ifaceImpl.InterfaceType.Resolve ();
if (resolvedInterfaceType == null) {
HandleUnresolvedType (ifaceImpl.InterfaceType);
continue;
}
if (resolvedInterfaceType == resolvedOverride.DeclaringType) {
MarkInterfaceImplementation (ifaceImpl);
return;
}
}
}
}
void MarkNewCodeDependencies (MethodDefinition method)
{
switch (Annotations.GetAction (method)) {
case MethodAction.ConvertToStub:
if (!method.IsInstanceConstructor ())
return;
var baseType = ResolveTypeDefinition (method.DeclaringType.BaseType);
if (!MarkDefaultConstructor (baseType))
throw new NotSupportedException ($"Cannot stub constructor on '{method.DeclaringType}' when base type does not have default constructor");
break;
case MethodAction.ConvertToThrow:
if (_context.MarkedKnownMembers.NotSupportedExceptionCtorString != null)
break;
var nse = BCL.FindPredefinedType ("System", "NotSupportedException", _context);
if (nse == null)
throw new NotSupportedException ("Missing predefined 'System.NotSupportedException' type");
MarkType (nse);
var nseCtor = MarkMethodIf (nse.Methods, KnownMembers.IsNotSupportedExceptionCtorString);
if (nseCtor == null)
throw new MarkException ($"Could not find constructor on '{nse.FullName}'");
_context.MarkedKnownMembers.NotSupportedExceptionCtorString = nseCtor;
break;
}
}
void MarkBaseMethods (MethodDefinition method)
{
var base_methods = Annotations.GetBaseMethods (method);
if (base_methods == null)
return;
foreach (MethodDefinition base_method in base_methods) {
if (base_method.DeclaringType.IsInterface && !method.DeclaringType.IsInterface)
continue;
MarkMethod (base_method);
MarkBaseMethods (base_method);
}
}
void ProcessInteropMethod(MethodDefinition method)
{
TypeDefinition returnTypeDefinition = ResolveTypeDefinition (method.ReturnType);
const bool includeStaticFields = false;
if (returnTypeDefinition != null && !returnTypeDefinition.IsImport) {
MarkDefaultConstructor (returnTypeDefinition);
MarkFields (returnTypeDefinition, includeStaticFields);
}
if (method.HasThis && !method.DeclaringType.IsImport) {
MarkFields (method.DeclaringType, includeStaticFields);
}
foreach (ParameterDefinition pd in method.Parameters) {
TypeReference paramTypeReference = pd.ParameterType;
if (paramTypeReference is TypeSpecification) {
paramTypeReference = (paramTypeReference as TypeSpecification).ElementType;
}
TypeDefinition paramTypeDefinition = ResolveTypeDefinition (paramTypeReference);
if (paramTypeDefinition != null && !paramTypeDefinition.IsImport) {
MarkFields (paramTypeDefinition, includeStaticFields);
if (pd.ParameterType.IsByReference) {
MarkDefaultConstructor (paramTypeDefinition);
}
}
}
}
protected virtual bool ShouldParseMethodBody (MethodDefinition method)
{
if (!method.HasBody)
return false;
switch (Annotations.GetAction (method)) {
case MethodAction.ForceParse:
return true;
case MethodAction.Parse:
AssemblyDefinition assembly = ResolveAssembly (method.DeclaringType.Scope);
switch (Annotations.GetAction (assembly)) {
case AssemblyAction.Link:
case AssemblyAction.Copy:
case AssemblyAction.CopyUsed:
case AssemblyAction.AddBypassNGen:
case AssemblyAction.AddBypassNGenUsed:
return true;
default:
return false;
}
default:
return false;
}
}
static internal bool IsPropertyMethod (MethodDefinition md)
{
return (md.SemanticsAttributes & MethodSemanticsAttributes.Getter) != 0 ||
(md.SemanticsAttributes & MethodSemanticsAttributes.Setter) != 0;
}
static internal bool IsPublicInstancePropertyMethod (MethodDefinition md)
{
return md.IsPublic && !md.IsStatic && IsPropertyMethod (md);
}
static bool IsEventMethod (MethodDefinition md)
{
return (md.SemanticsAttributes & MethodSemanticsAttributes.AddOn) != 0 ||
(md.SemanticsAttributes & MethodSemanticsAttributes.Fire) != 0 ||
(md.SemanticsAttributes & MethodSemanticsAttributes.RemoveOn) != 0;
}
static internal PropertyDefinition GetProperty (MethodDefinition md)
{
TypeDefinition declaringType = md.DeclaringType;
foreach (PropertyDefinition prop in declaringType.Properties)
if (prop.GetMethod == md || prop.SetMethod == md)
return prop;
return null;
}
static EventDefinition GetEvent (MethodDefinition md)
{
TypeDefinition declaringType = md.DeclaringType;
foreach (EventDefinition evt in declaringType.Events)
if (evt.AddMethod == md || evt.InvokeMethod == md || evt.RemoveMethod == md)
return evt;
return null;
}
protected void MarkProperty (PropertyDefinition prop)
{
MarkCustomAttributes (prop);
DoAdditionalPropertyProcessing (prop);
}
protected virtual void MarkEvent (EventDefinition evt)
{
MarkCustomAttributes (evt);
MarkMethodIfNotNull (evt.AddMethod);
MarkMethodIfNotNull (evt.InvokeMethod);
MarkMethodIfNotNull (evt.RemoveMethod);
DoAdditionalEventProcessing (evt);
}
void MarkMethodIfNotNull (MethodReference method)
{
if (method == null)
return;
MarkMethod (method);
}
protected virtual void MarkMethodBody (MethodBody body)
{
foreach (VariableDefinition var in body.Variables)
MarkType (var.VariableType);
foreach (ExceptionHandler eh in body.ExceptionHandlers)
if (eh.HandlerType == ExceptionHandlerType.Catch)
MarkType (eh.CatchType);
foreach (Instruction instruction in body.Instructions)
MarkInstruction (instruction);
MarkThingsUsedViaReflection (body);
PostMarkMethodBody (body);
}
partial void PostMarkMethodBody (MethodBody body);
protected virtual void MarkThingsUsedViaReflection (MethodBody body)
{
MarkSomethingUsedViaReflection ("GetConstructor", MarkConstructorsUsedViaReflection, body.Instructions);
MarkSomethingUsedViaReflection ("GetMethod", MarkMethodsUsedViaReflection, body.Instructions);
MarkSomethingUsedViaReflection ("GetProperty", MarkPropertyUsedViaReflection, body.Instructions);
MarkSomethingUsedViaReflection ("GetField", MarkFieldUsedViaReflection, body.Instructions);
MarkSomethingUsedViaReflection ("GetEvent", MarkEventUsedViaReflection, body.Instructions);
MarkTypeUsedViaReflection (body.Instructions);
}
protected virtual void MarkInstruction (Instruction instruction)
{
switch (instruction.OpCode.OperandType) {
case OperandType.InlineField:
MarkField ((FieldReference) instruction.Operand);
break;
case OperandType.InlineMethod:
MarkMethod ((MethodReference) instruction.Operand);
break;
case OperandType.InlineTok:
object token = instruction.Operand;
if (token is TypeReference)
MarkType ((TypeReference) token);
else if (token is MethodReference)
MarkMethod ((MethodReference) token);
else
MarkField ((FieldReference) token);
break;
case OperandType.InlineType:
MarkType ((TypeReference) instruction.Operand);
break;
default:
break;
}
}
protected virtual void HandleUnresolvedType (TypeReference reference)
{
if (!_context.IgnoreUnresolved) {
throw new ResolutionException (reference);
}
}
protected virtual void HandleUnresolvedMethod (MethodReference reference)
{
if (!_context.IgnoreUnresolved) {
throw new ResolutionException (reference);
}
}
protected virtual bool ShouldMarkInterfaceImplementation (TypeDefinition type, InterfaceImplementation iface, TypeDefinition resolvedInterfaceType)
{
if (Annotations.IsMarked (iface))
return false;
if (Annotations.IsMarked (resolvedInterfaceType) && !Annotations.IsMarked (iface))
return true;
// It's hard to know if a com or windows runtime interface will be needed from managed code alone,
// so as a precaution we will mark these interfaces once the type is instantiated
if (resolvedInterfaceType.IsImport || resolvedInterfaceType.IsWindowsRuntime)
return true;
return IsFullyPreserved (type);
}
protected virtual void MarkInterfaceImplementation (InterfaceImplementation iface)
{
MarkCustomAttributes (iface);
MarkType (iface.InterfaceType);
Annotations.Mark (iface);
}
bool CheckReflectionMethod (Instruction instruction, string reflectionMethod)
{
if (instruction.OpCode != OpCodes.Call && instruction.OpCode != OpCodes.Callvirt)
return false;
var methodBeingCalled = instruction.Operand as MethodReference;
if (methodBeingCalled == null || methodBeingCalled.DeclaringType.Name != "Type" || methodBeingCalled.DeclaringType.Namespace != "System")
return false;
if (methodBeingCalled.Name != reflectionMethod)
return false;
return true;
}
void MarkSomethingUsedViaReflection (string reflectionMethod, Action<Collection<Instruction>, string, TypeDefinition, BindingFlags> markMethod, Collection<Instruction> instructions)
{
for (var i = 0; i < instructions.Count; i++) {
var instruction = instructions [i];
if (!CheckReflectionMethod (instruction, reflectionMethod))
continue;
var nameOfThingUsedViaReflection = OperandOfNearestInstructionBefore<string> (i, OpCodes.Ldstr, instructions);
var bindingFlags = (BindingFlags) OperandOfNearestInstructionBefore<sbyte> (i, OpCodes.Ldc_I4_S, instructions);
// There might be more than one ldtoken opcode above the call in the IL stream. Be conservative and check all of
// the types which were loaded for the method being used.
var declaringTypesOfThingInvokedViaReflection = OperandsOfInstructionsBefore (i, OpCodes.Ldtoken, instructions);
foreach (var declaringTypeOfThingInvokedViaReflection in declaringTypesOfThingInvokedViaReflection) {
var typeDefinition = declaringTypeOfThingInvokedViaReflection?.Resolve ();
if (typeDefinition != null)
markMethod (instructions, nameOfThingUsedViaReflection, typeDefinition, bindingFlags);
}
}
}
void MarkTypeUsedViaReflection (Collection<Instruction> instructions)
{
for (var i = 0; i < instructions.Count; i++) {
var instruction = instructions [i];
if (!CheckReflectionMethod (instruction, "GetType"))
continue;
var typeAssemblyQualifiedName = OperandOfNearestInstructionBefore<string> (i, OpCodes.Ldstr, instructions);
if (!TypeNameParser.TryParseTypeAssemblyQualifiedName (typeAssemblyQualifiedName, out string typeName, out string assemblyName))
continue;
TypeDefinition foundType = null;
foreach (var assemblyDefinition in _context.GetAssemblies ()) {
if (assemblyName != null && assemblyDefinition.Name.Name != assemblyName)
continue;
foundType = assemblyDefinition.MainModule.GetType (typeName);
if (foundType != null)
break;
}
if (foundType == null)
continue;
_context.Tracer.Push ($"Reflection-{foundType}");
try {
MarkType (foundType);
} finally {
_context.Tracer.Pop ();
}
}
}
void MarkConstructorsUsedViaReflection (Collection<Instruction> instructions, string unused, TypeDefinition declaringType, BindingFlags bindingFlags)
{
foreach (var method in declaringType.Methods) {
if ((bindingFlags == BindingFlags.Default || bindingFlags.IsSet(BindingFlags.Public) == method.IsPublic) && method.Name == ".ctor") {
Tracer.Push ($"Reflection-{method}");
try {
MarkMethod (method);
} finally {
Tracer.Pop ();
}
}
}
}
void MarkMethodsUsedViaReflection (Collection<Instruction> instructions, string name, TypeDefinition declaringType, BindingFlags bindingFlags)
{
if (name == null)
return;
foreach (var method in declaringType.Methods) {
if ((bindingFlags == BindingFlags.Default || bindingFlags.IsSet(BindingFlags.Public) == method.IsPublic && bindingFlags.IsSet(BindingFlags.Static) == method.IsStatic)
&& method.Name == name) {
Tracer.Push ($"Reflection-{method}");
try {
MarkMethod (method);
} finally {
Tracer.Pop ();
}
}
}
}
void MarkPropertyUsedViaReflection (Collection<Instruction> instructions, string name, TypeDefinition declaringType, BindingFlags unused)
{
if (name == null)
return;
foreach (var property in declaringType.Properties) {
if (property.Name == name) {
Tracer.Push ($"Reflection-{property}");
try {
// It is not easy to reliably detect in the IL code whether the getter or setter (or both) are used.
// Be conservative and mark everything for the property.
MarkProperty (property);
MarkMethodIfNotNull (property.GetMethod);
MarkMethodIfNotNull (property.SetMethod);
} finally {
Tracer.Pop ();
}
}
}
}
void MarkFieldUsedViaReflection (Collection<Instruction> instructions, string name, TypeDefinition declaringType, BindingFlags unused)
{
if (name == null)
return;
foreach (var field in declaringType.Fields) {
if (field.Name == name) {
Tracer.Push ($"Reflection-{field}");
try {
MarkField (field);
} finally {
Tracer.Pop ();
}
}
}
}
void MarkEventUsedViaReflection (Collection<Instruction> instructions, string name, TypeDefinition declaringType, BindingFlags unused)
{
if (name == null)
return;
foreach (var eventInfo in declaringType.Events) {
if (eventInfo.Name == name) {
Tracer.Push ($"Reflection-{eventInfo}");
try {
MarkEvent (eventInfo);
} finally {
Tracer.Pop ();
}
}
}
}
static TOperand OperandOfNearestInstructionBefore<TOperand> (int startingInstructionIndex, OpCode opCode, IList<Instruction> instructions)
{
for (var i = startingInstructionIndex; i >= 0; i--) {
if (instructions [i].OpCode == opCode)
return (TOperand) instructions [i].Operand;
}
return default (TOperand);
}
static List<TypeReference> OperandsOfInstructionsBefore (int startingInstructionIndex, OpCode opCode, IList<Instruction> instructions)
{
var operands = new List<TypeReference> ();
for (var i = startingInstructionIndex; i >= 0; i--) {
if (instructions [i].OpCode == opCode) {
var type = instructions [i].Operand as TypeReference;
if (type != null)
operands.Add (type);
}
}
return operands;
}
protected class AttributeProviderPair {
public AttributeProviderPair (CustomAttribute attribute, ICustomAttributeProvider provider)
{
Attribute = attribute;
Provider = provider;
}
public CustomAttribute Attribute { get; private set; }
public ICustomAttributeProvider Provider { get; private set; }
}
}
// Make our own copy of the BindingFlags enum, so that we don't depend on System.Reflection.
[Flags]
enum BindingFlags
{
Default = 0,
IgnoreCase = 1,
DeclaredOnly = 2,
Instance = 4,
Static = 8,
Public = 16,
NonPublic = 32,
FlattenHierarchy = 64,
InvokeMethod = 256,
CreateInstance = 512,
GetField = 1024,
SetField = 2048,
GetProperty = 4096,
SetProperty = 8192,
PutDispProperty = 16384,
PutRefDispProperty = 32768,
ExactBinding = 65536,
SuppressChangeType = 131072,
OptionalParamBinding = 262144,
IgnoreReturn = 16777216
}
static class BindingFlagsExtensions
{
public static bool IsSet(this BindingFlags flags, BindingFlags check)
{
return (flags & check) == check;
}
}
}
|