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

Import.cs « Mono.Cecil - github.com/mono/cecil.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5686f6b5a68143cb7bde2a9b5063010f52abb7c6 (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
//
// Import.cs
//
// Author:
//   Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2010 Jb Evain
//
// 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 SR = System.Reflection;

using Mono.Cecil.Metadata;

namespace Mono.Cecil {

	class MetadataImporter {

		readonly ModuleDefinition module;

		static readonly Dictionary<Type, ElementType> type_etype_mapping = new Dictionary<Type, ElementType> {
			{ typeof (void), ElementType.Void },
			{ typeof (bool), ElementType.Boolean },
			{ typeof (char), ElementType.Char },
			{ typeof (sbyte), ElementType.I1 },
			{ typeof (byte), ElementType.U1 },
			{ typeof (short), ElementType.I2 },
			{ typeof (ushort), ElementType.U2 },
			{ typeof (int), ElementType.I4 },
			{ typeof (uint), ElementType.U4 },
			{ typeof (long), ElementType.I8 },
			{ typeof (ulong), ElementType.U8 },
			{ typeof (float), ElementType.R4 },
			{ typeof (double), ElementType.R8 },
			{ typeof (string), ElementType.String },
			{ typeof (TypedReference), ElementType.TypedByRef },
			{ typeof (IntPtr), ElementType.I },
			{ typeof (UIntPtr), ElementType.U },
			{ typeof (object), ElementType.Object },
		};

		public MetadataImporter (ModuleDefinition module)
		{
			this.module = module;
		}

		public TypeReference ImportType (Type type, IGenericContext context)
		{
			if (IsTypeSpecification (type))
				return ImportTypeSpecification (type, context);

			var reference = new TypeReference (
				string.Empty,
				type.Name,
				ImportScope (type.Assembly),
				type.IsValueType);

			reference.etype = ImportElementType (type);
			reference.module = module;

			if (IsNestedType (type))
				reference.DeclaringType = ImportType (type.DeclaringType, context);
			else
				reference.Namespace = type.Namespace;

			if (type.IsGenericType)
				ImportGenericParameters (reference, type.GetGenericArguments ());

			return reference;
		}

		static bool IsNestedType (Type type)
		{
#if !SILVERLIGHT
			return type.IsNested;
#else
			return type.IsNestedPublic
				|| type.IsNestedPrivate
				|| type.IsNestedFamORAssem
				|| type.IsNestedFamily
				|| type.IsNestedFamANDAssem
				|| type.IsNestedAssembly;
#endif
		}

		TypeReference ImportTypeSpecification (Type type, IGenericContext context)
		{
			if (type.IsByRef)
				return new ByReferenceType (ImportType (type.GetElementType (), context));

			if (type.IsPointer)
				return new PointerType (ImportType (type.GetElementType (), context));

			if (type.IsArray) {
				var array_type = new ArrayType (ImportType (type.GetElementType (), context));
				for (int i = 1; i < type.GetArrayRank (); i++)
					array_type.Dimensions.Add (new ArrayDimension ());

				return array_type;
			}

			if (IsGenericInstance (type)) {
				var element_type = ImportType (type.GetGenericTypeDefinition (), context);
				var instance = new GenericInstanceType (element_type);
				var arguments = type.GetGenericArguments ();

				for (int i = 0; i < arguments.Length; i++)
					instance.GenericArguments.Add (ImportType (arguments [i], element_type));

				return instance;
			}

			if (type.IsGenericParameter) {
				if (context == null)
					throw new InvalidOperationException ();

				var owner = type.DeclaringMethod != null
					? context.Method
					: context.Type;

				return owner.GenericParameters [type.GenericParameterPosition];
			}

			throw new NotSupportedException (type.FullName);
		}

		static bool IsTypeSpecification (Type type)
		{
			return type.HasElementType
				|| IsGenericInstance (type)
				|| type.IsGenericParameter;
		}

		static bool IsGenericInstance (Type type)
		{
			return type.IsGenericType && !type.IsGenericTypeDefinition;
		}

		static ElementType ImportElementType (Type type)
		{
			ElementType etype;
			if (!type_etype_mapping.TryGetValue (type, out etype))
				return ElementType.None;

			return etype;
		}

		AssemblyNameReference ImportScope (SR.Assembly assembly)
		{
			AssemblyNameReference scope;
			var name = assembly.GetName ();

			if (TryGetAssemblyNameReference (name, out scope))
				return scope;

			scope = new AssemblyNameReference (name.Name, name.Version) {
				Culture = name.CultureInfo.Name,
				PublicKeyToken = name.GetPublicKeyToken (),
				HashAlgorithm = (AssemblyHashAlgorithm) name.HashAlgorithm,
			};

			module.AssemblyReferences.Add (scope);

			return scope;
		}

		bool TryGetAssemblyNameReference (SR.AssemblyName name, out AssemblyNameReference assembly_reference)
		{
			var references = module.AssemblyReferences;

			for (int i = 0; i < references.Count; i++) {
				var reference = references [i];
				if (name.FullName != reference.FullName) // TODO compare field by field
					continue;

				assembly_reference = reference;
				return true;
			}

			assembly_reference = null;
			return false;
		}

		public FieldReference ImportField (SR.FieldInfo field)
		{
			var declaring_type = ImportType (field.DeclaringType, null);

			if (IsGenericInstance (field.DeclaringType))
				field = ResolveFieldDefinition (field);

			return new FieldReference {
				Name = field.Name,
				DeclaringType = declaring_type,
				FieldType = ImportType (field.FieldType, declaring_type),
			};
		}

		static SR.FieldInfo ResolveFieldDefinition (SR.FieldInfo field)
		{
#if !SILVERLIGHT
			return field.Module.ResolveField (field.MetadataToken);
#else
			return field.DeclaringType.GetGenericTypeDefinition ().GetField (field.Name,
				SR.BindingFlags.Public
				| SR.BindingFlags.NonPublic
				| (field.IsStatic ? SR.BindingFlags.Static : SR.BindingFlags.Instance));
#endif
		}

		public MethodReference ImportMethod (SR.MethodBase method)
		{
			if (IsMethodSpecification (method))
				return ImportMethodSpecification (method);

			var declaring_type = ImportType (method.DeclaringType, null);

			if (IsGenericInstance (method.DeclaringType))
				method = method.Module.ResolveMethod (method.MetadataToken);

			var reference = new MethodReference {
				Name = method.Name,
				HasThis = HasCallingConvention (method, SR.CallingConventions.HasThis),
				ExplicitThis = HasCallingConvention (method, SR.CallingConventions.ExplicitThis),
				DeclaringType = ImportType (method.DeclaringType, null),
			};

			if (HasCallingConvention (method, SR.CallingConventions.VarArgs))
				reference.CallingConvention &= MethodCallingConvention.VarArg;

			if (method.IsGenericMethod)
				ImportGenericParameters (reference, method.GetGenericArguments ());

			var method_info = method as SR.MethodInfo;
			reference.ReturnType = method_info != null
				? ImportType (method_info.ReturnType, reference)
				: ImportType (typeof (void), null);

			var parameters = method.GetParameters ();
			for (int i = 0; i < parameters.Length; i++)
				reference.Parameters.Add (
					new ParameterDefinition (ImportType (parameters [i].ParameterType, reference)));

			reference.DeclaringType = declaring_type;

			return reference;
		}

		static void ImportGenericParameters (IGenericParameterProvider provider, Type [] arguments)
		{
			for (int i = 0; i < arguments.Length; i++)
				provider.GenericParameters.Add (new GenericParameter (arguments [i].Name, provider));
		}

		static bool IsMethodSpecification (SR.MethodBase method)
		{
			return method.IsGenericMethod && !method.IsGenericMethodDefinition;
		}

		MethodReference ImportMethodSpecification (SR.MethodBase method)
		{
			var method_info = method as SR.MethodInfo;
			if (method_info == null)
				throw new InvalidOperationException ();

			var element_method = ImportMethod (method_info.GetGenericMethodDefinition ());
			var instance = new GenericInstanceMethod (element_method);
			var arguments = method.GetGenericArguments ();

			for (int i = 0; i < arguments.Length; i++)
				instance.GenericArguments.Add (ImportType (arguments [i], element_method));

			return instance;
		}

		static bool HasCallingConvention (SR.MethodBase method, SR.CallingConventions conventions)
		{
			return (method.CallingConvention & conventions) != 0;
		}

		static readonly Dictionary<string, ElementType> string_etype_mapping = new Dictionary<string, ElementType> {
			{ "Void", ElementType.Void },
			{ "Boolean", ElementType.Boolean },
			{ "Char", ElementType.Char },
			{ "SByte", ElementType.I1 },
			{ "Byte", ElementType.U1 },
			{ "Int16", ElementType.I2 },
			{ "UInt16", ElementType.U2 },
			{ "Int32", ElementType.I4 },
			{ "UInt32", ElementType.U4 },
			{ "Int64", ElementType.I8 },
			{ "UInt64", ElementType.U8 },
			{ "Single", ElementType.R4 },
			{ "Double", ElementType.R8 },
			{ "String", ElementType.String },
			{ "TypedReference", ElementType.TypedByRef },
			{ "IntPtr", ElementType.I },
			{ "UIntPtr", ElementType.U },
			{ "Object", ElementType.Object },
		};

		public TypeReference ImportType (TypeReference type, IGenericContext context)
		{
			if (type.IsTypeSpecification ())
				return ImportTypeSpecification (type, context);

			var reference = new TypeReference (
				type.Namespace,
				type.Name,
				ImportScope (type.Scope),
				type.IsValueType);

			reference.etype = ImportElementType (type);
			reference.module = module;

			if (type.IsNested)
				reference.DeclaringType = ImportType (type.DeclaringType, context);

			if (type.HasGenericParameters)
				ImportGenericParameters (reference, type);

			return reference;
		}

		static ElementType ImportElementType (TypeReference type)
		{
			if (type.etype != ElementType.None)
				return type.etype;

			ElementType etype;
			if (IsCorlibSystemType (type) && string_etype_mapping.TryGetValue (type.Name, out etype))
				return etype;

			return ElementType.None;
		}

		static bool IsCorlibSystemType (TypeReference type)
		{
			if (type.Namespace != "System")
				return false;

			var assembly = type.Module.Assembly;
			return assembly != null && assembly.Name.Name == "mscorlib";
		}

		IMetadataScope ImportScope (IMetadataScope scope)
		{
			switch (scope.MetadataScopeType) {
			case MetadataScopeType.AssemblyNameReference:
				return ImportAssemblyName ((AssemblyNameReference) scope);
			case MetadataScopeType.ModuleDefinition:
				return ImportAssemblyName (((ModuleDefinition) scope).Assembly.Name);
			case MetadataScopeType.ModuleReference:
				throw new NotImplementedException ();
			}

			throw new NotSupportedException ();
		}

		AssemblyNameReference ImportAssemblyName (AssemblyNameReference name)
		{
			AssemblyNameReference reference;
			if (TryGetAssemblyNameReference (name, out reference))
				return reference;

			reference = new AssemblyNameReference (name.Name, name.Version) {
				Culture = name.Culture,
				HashAlgorithm = name.HashAlgorithm,
			};

			var pk_token = new byte [name.PublicKeyToken.Length];
			if (pk_token.Length > 0)
				Buffer.BlockCopy (name.PublicKeyToken, 0, pk_token, 0, pk_token.Length);

			reference.PublicKeyToken = pk_token;

			module.AssemblyReferences.Add (reference);

			return reference;
		}

		bool TryGetAssemblyNameReference (AssemblyNameReference name_reference, out AssemblyNameReference assembly_reference)
		{
			var references = module.AssemblyReferences;

			for (int i = 0; i < references.Count; i++) {
				var reference = references [i];
				if (name_reference.FullName != reference.FullName) // TODO compare field by field
					continue;

				assembly_reference = reference;
				return true;
			}

			assembly_reference = null;
			return false;
		}

		static void ImportGenericParameters (IGenericParameterProvider imported, IGenericParameterProvider original)
		{
			var parameters = original.GenericParameters;

			for (int i = 0; i < parameters.Count; i++)
				imported.GenericParameters.Add (new GenericParameter (parameters [i].Name, imported));
		}

		TypeReference ImportTypeSpecification (TypeReference type, IGenericContext context)
		{
			switch (type.etype) {
			case ElementType.SzArray:
				var vector = (ArrayType) type;
				return new ArrayType (ImportType (vector.ElementType, context));
			case ElementType.Ptr:
				var pointer = (PointerType) type;
				return new PointerType (ImportType (pointer.ElementType, context));
			case ElementType.ByRef:
				var byref = (ByReferenceType) type;
				return new PointerType (ImportType (byref.ElementType, context));
			case ElementType.Pinned:
				var pinned = (PinnedType) type;
				return new PinnedType (ImportType (pinned.ElementType, context));
			case ElementType.Sentinel:
				var sentinel = (SentinelType) type;
				return new SentinelType (ImportType (sentinel.ElementType, context));
			case ElementType.CModOpt:
				var modopt = (OptionalModifierType) type;
				return new OptionalModifierType (
					ImportType (modopt.ModifierType, context),
					ImportType (modopt.ElementType, context));
			case ElementType.CModReqD:
				var modreq = (RequiredModifierType) type;
				return new RequiredModifierType (
					ImportType (modreq.ModifierType, context),
					ImportType (modreq.ElementType, context));
			case ElementType.Array:
				var array = (ArrayType) type;
				var imported_array = new ArrayType (ImportType (array.ElementType, context));

				var dimensions = array.Dimensions;

				imported_array.Dimensions.Clear ();

				for (int i = 0; i < dimensions.Count; i++) {
					var dimension = dimensions [i];
					imported_array.Dimensions.Add (new ArrayDimension (dimension.LowerBound, dimension.UpperBound));
				}

				return imported_array;
			case ElementType.GenericInst:
				var instance = (GenericInstanceType) type;
				var imported_instance = new GenericInstanceType (ImportType (instance.ElementType, context));

				var arguments = instance.GenericArguments;

				for (int i = 0; i < arguments.Count; i++)
					imported_instance.GenericArguments.Add (ImportType (arguments [i], context));

				return imported_instance;
			case ElementType.Var:
				if (context == null || context.Type == null)
					throw new InvalidOperationException ();

				return ((TypeReference) context.Type).GetElementType ().GenericParameters [((GenericParameter) type).Position];
			case ElementType.MVar:
				if (context == null || context.Method == null)
					throw new InvalidOperationException ();

				return context.Method.GenericParameters [((GenericParameter) type).Position];
			}

			throw new NotSupportedException (type.etype.ToString ());
		}

		public FieldReference ImportField (FieldReference field)
		{
			var declaring_type = ImportType (field.DeclaringType, null);

			return new FieldReference {
				Name = field.Name,
				DeclaringType = declaring_type,
				FieldType = ImportType (field.FieldType, declaring_type),
			};
		}

		public MethodReference ImportMethod (MethodReference method)
		{
			if (method.IsGenericInstance)
				return ImportMethodSpecification (method);

			var declaring_type = ImportType (method.DeclaringType, null);

			var reference = new MethodReference {
				Name = method.Name,
				HasThis = method.HasThis,
				ExplicitThis = method.ExplicitThis,
				DeclaringType = declaring_type,
			};

			if (method.IsVarArg ())
				reference.CallingConvention &= MethodCallingConvention.VarArg;

			if (method.HasGenericParameters)
				ImportGenericParameters (reference, method);

			reference.ReturnType = ImportType (method.ReturnType, reference);

			if (!method.HasParameters)
				return reference;

			var parameters = method.Parameters;
			for (int i = 0; i < parameters.Count; i++)
				reference.Parameters.Add (
					new ParameterDefinition (ImportType (parameters [i].ParameterType, reference)));

			return reference;
		}

		MethodSpecification ImportMethodSpecification (MethodReference method)
		{
			if (!method.IsGenericInstance)
				throw new NotSupportedException ();

			var instance = (GenericInstanceMethod) method;
			var element_method = ImportMethod (instance.ElementMethod);
			var imported_instance = new GenericInstanceMethod (element_method);

			var arguments = instance.GenericArguments;

			for (int i = 0; i < arguments.Count; i++)
				imported_instance.GenericArguments.Add (ImportType (arguments [i], element_method));

			return imported_instance;
		}
	}
}