Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TypeReferenceWalker.cs « Linker « linker « src - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 700e2c7168a3ccafacdb0db2dd93141a42699b90 (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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;

namespace Mono.Linker
{
	abstract class TypeReferenceWalker
	{
		protected readonly AssemblyDefinition assembly;

		protected HashSet<TypeReference> Visited { get; } = new HashSet<TypeReference> ();

		public TypeReferenceWalker (AssemblyDefinition assembly)
		{
			this.assembly = assembly;
		}

		// Traverse the assembly and mark the scopes of discovered type references (but not exported types).
		// This includes scopes referenced by Cecil TypeReference objects that don't represent rows in the typeref table,
		// such as references to built-in types, or attribute arguments which encode type references as strings.
		protected virtual void Process ()
		{
			if (Visited.Count > 0)
				throw new InvalidOperationException ();

			WalkCustomAttributesTypesScopes (assembly);
			WalkSecurityAttributesTypesScopes (assembly);

			foreach (var module in assembly.Modules)
				WalkCustomAttributesTypesScopes (module);

			var mmodule = assembly.MainModule;
			if (mmodule.HasTypes) {
				foreach (var type in mmodule.Types) {
					WalkScopes (type);
				}
			}

			if (mmodule.HasExportedTypes)
				WalkTypeScope (mmodule.ExportedTypes);

			ProcessExtra ();
		}

		protected virtual void ProcessExtra () { }

		void WalkScopes (TypeDefinition typeDefinition)
		{
			WalkCustomAttributesTypesScopes (typeDefinition);
			WalkSecurityAttributesTypesScopes (typeDefinition);

			if (typeDefinition.BaseType != null)
				WalkScopeOfTypeReference (typeDefinition.BaseType);

			if (typeDefinition.HasInterfaces) {
				foreach (var iface in typeDefinition.Interfaces) {
					WalkCustomAttributesTypesScopes (iface);
					WalkScopeOfTypeReference (iface.InterfaceType);
				}
			}

			if (typeDefinition.HasGenericParameters)
				WalkTypeScope (typeDefinition.GenericParameters);

			if (typeDefinition.HasEvents) {
				foreach (var e in typeDefinition.Events) {
					WalkCustomAttributesTypesScopes (e);
					// e.EventType is not saved
				}
			}

			if (typeDefinition.HasFields) {
				foreach (var f in typeDefinition.Fields) {
					WalkCustomAttributesTypesScopes (f);
					WalkScopeOfTypeReference (f.FieldType);
					WalkMarshalInfoTypeScope (f);
				}
			}

			if (typeDefinition.HasMethods) {
				foreach (var m in typeDefinition.Methods) {
					WalkCustomAttributesTypesScopes (m);
					WalkSecurityAttributesTypesScopes (m);
					if (m.HasGenericParameters)
						WalkTypeScope (m.GenericParameters);

					WalkCustomAttributesTypesScopes (m.MethodReturnType);
					WalkScopeOfTypeReference (m.MethodReturnType.ReturnType);
					WalkMarshalInfoTypeScope (m.MethodReturnType);
					if (m.HasOverrides) {
						foreach (var mo in m.Overrides)
							WalkMethodReference (mo);
					}
#pragma warning disable RS0030 // MethodReference.Parameters is banned - It's best to leave this as is
					if (m.HasMetadataParameters ())
						WalkTypeScope (m.Parameters);
#pragma warning restore RS0030

					if (m.HasBody)
						WalkTypeScope (m.Body);
				}
			}

			if (typeDefinition.HasProperties) {
				foreach (var p in typeDefinition.Properties) {
					WalkCustomAttributesTypesScopes (p);
					// p.PropertyType is not saved
				}
			}

			if (typeDefinition.HasNestedTypes) {
				foreach (var nestedType in typeDefinition.NestedTypes) {
					WalkScopes (nestedType);
				}
			}
		}

		void WalkTypeScope (Collection<GenericParameter> genericParameters)
		{
			foreach (var gp in genericParameters) {
				WalkCustomAttributesTypesScopes (gp);
				if (gp.HasConstraints)
					WalkTypeScope (gp.Constraints);
			}
		}

		void WalkTypeScope (Collection<GenericParameterConstraint> constraints)
		{
			foreach (var gc in constraints) {
				WalkCustomAttributesTypesScopes (gc);
				WalkScopeOfTypeReference (gc.ConstraintType);
			}
		}

		void WalkTypeScope (Collection<ParameterDefinition> parameters)
		{
			foreach (var p in parameters) {
				WalkCustomAttributesTypesScopes (p);
				WalkScopeOfTypeReference (p.ParameterType);
				WalkMarshalInfoTypeScope (p);
			}
		}

		void WalkTypeScope (Collection<ExportedType> forwarders)
		{
			foreach (var f in forwarders)
				ProcessExportedType (f);
		}

		void WalkTypeScope (MethodBody body)
		{
			if (body.HasVariables) {
				foreach (var v in body.Variables) {
					WalkScopeOfTypeReference (v.VariableType);
				}
			}

			if (body.HasExceptionHandlers) {
				foreach (var eh in body.ExceptionHandlers) {
					if (eh.CatchType != null)
						WalkScopeOfTypeReference (eh.CatchType);
				}
			}

			foreach (var instr in body.Instructions) {
				switch (instr.OpCode.OperandType) {

				case OperandType.InlineMethod: {
						var mr = (MethodReference) instr.Operand;
						WalkMethodReference (mr);
						break;
					}

				case OperandType.InlineField: {
						var fr = (FieldReference) instr.Operand;
						WalkFieldReference (fr);
						break;
					}

				case OperandType.InlineTok: {
						switch (instr.Operand) {
						case TypeReference tr:
							WalkScopeOfTypeReference (tr);
							break;
						case FieldReference fr:
							WalkFieldReference (fr);
							break;
						case MethodReference mr:
							WalkMethodReference (mr);
							break;
						}

						break;
					}

				case OperandType.InlineType: {
						var tr = (TypeReference) instr.Operand;
						WalkScopeOfTypeReference (tr);
						break;
					}
				}
			}
		}

		void WalkMethodReference (MethodReference mr)
		{
			WalkScopeOfTypeReference (mr.ReturnType);
			WalkScopeOfTypeReference (mr.DeclaringType);

			if (mr is GenericInstanceMethod gim) {
				foreach (var tr in gim.GenericArguments)
					WalkScopeOfTypeReference (tr);
			}

			if (mr.HasMetadataParameters ()) {
#pragma warning disable RS0030 // MethedReference.Parameters is banned. Best to leave working code as is.
				WalkTypeScope (mr.Parameters);
#pragma warning restore RS0030 // Do not used banned APIs
			}
		}

		void WalkFieldReference (FieldReference fr)
		{
			WalkScopeOfTypeReference (fr.FieldType);
			WalkScopeOfTypeReference (fr.DeclaringType);
		}

		void WalkMarshalInfoTypeScope (IMarshalInfoProvider provider)
		{
			if (!provider.HasMarshalInfo)
				return;

			if (provider.MarshalInfo is CustomMarshalInfo cmi)
				WalkScopeOfTypeReference (cmi.ManagedType);
		}

		void WalkCustomAttributesTypesScopes (ICustomAttributeProvider customAttributeProvider)
		{
			if (!customAttributeProvider.HasCustomAttributes)
				return;

			foreach (var ca in customAttributeProvider.CustomAttributes)
				WalkForwardedTypesScope (ca);
		}

		void WalkSecurityAttributesTypesScopes (ISecurityDeclarationProvider securityAttributeProvider)
		{
			if (!securityAttributeProvider.HasSecurityDeclarations)
				return;

			foreach (var ca in securityAttributeProvider.SecurityDeclarations) {
				if (!ca.HasSecurityAttributes)
					continue;

				foreach (var securityAttribute in ca.SecurityAttributes)
					WalkForwardedTypesScope (securityAttribute);
			}
		}

		void WalkForwardedTypesScope (CustomAttribute attribute)
		{
			WalkMethodReference (attribute.Constructor);

			if (attribute.HasConstructorArguments) {
				foreach (var ca in attribute.ConstructorArguments)
					WalkForwardedTypesScope (ca);
			}

			if (attribute.HasFields) {
				foreach (var field in attribute.Fields)
					WalkForwardedTypesScope (field.Argument);
			}

			if (attribute.HasProperties) {
				foreach (var property in attribute.Properties)
					WalkForwardedTypesScope (property.Argument);
			}
		}

		void WalkForwardedTypesScope (SecurityAttribute attribute)
		{
			if (attribute.HasFields) {
				foreach (var field in attribute.Fields)
					WalkForwardedTypesScope (field.Argument);
			}

			if (attribute.HasProperties) {
				foreach (var property in attribute.Properties)
					WalkForwardedTypesScope (property.Argument);
			}
		}

		void WalkForwardedTypesScope (CustomAttributeArgument attributeArgument)
		{
			WalkScopeOfTypeReference (attributeArgument.Type);

			switch (attributeArgument.Value) {
			case TypeReference tr:
				WalkScopeOfTypeReference (tr);
				break;
			case CustomAttributeArgument caa:
				WalkForwardedTypesScope (caa);
				break;
			case CustomAttributeArgument[] array:
				foreach (var item in array)
					WalkForwardedTypesScope (item);
				break;
			}
		}

		void WalkScopeOfTypeReference (TypeReference type)
		{
			if (type == null)
				return;

			if (!Visited.Add (type))
				return;

			// Don't walk the scope of windows runtime projections
			if (type.IsWindowsRuntimeProjection)
				return;

			switch (type) {
			case GenericInstanceType git:
				WalkScopeOfTypeReference (git.ElementType);
				foreach (var ga in git.GenericArguments)
					WalkScopeOfTypeReference (ga);
				return;
			case FunctionPointerType fpt:
				WalkScopeOfTypeReference (fpt.ReturnType);
				if (fpt.HasParameters)
					WalkTypeScope (fpt.Parameters);
				return;
			case IModifierType imt:
				WalkScopeOfTypeReference (imt.ModifierType);
				WalkScopeOfTypeReference (imt.ElementType);
				return;
			case TypeSpecification ts:
				WalkScopeOfTypeReference (ts.ElementType);
				return;
			case TypeDefinition:
			case GenericParameter:
				// Nothing to walk
				return;
			}

			ProcessTypeReference (type);
		}

		protected abstract void ProcessTypeReference (TypeReference type);

		protected abstract void ProcessExportedType (ExportedType exportedType);
	}

}