2 // import.cs: System.Reflection conversions
4 // Authors: Marek Safar (marek.safar@gmail.com)
6 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 // Copyright 2009, 2010 Novell, Inc
12 using System.Runtime.CompilerServices;
14 using System.Collections.Generic;
17 using MetaType = IKVM.Reflection.Type;
18 using IKVM.Reflection;
20 using MetaType = System.Type;
21 using System.Reflection;
26 public abstract class MetadataImporter
29 // Dynamic types reader with additional logic to reconstruct a dynamic
30 // type using DynamicAttribute values
32 struct DynamicTypeReader
34 static readonly bool[] single_attribute = { true };
39 // There is no common type for CustomAttributeData and we cannot
40 // use ICustomAttributeProvider
44 // A member provider which can be used to get CustomAttributeData
46 public DynamicTypeReader (object provider)
50 this.provider = provider;
54 // Returns true when object at local position has dynamic attribute flag
56 public bool IsDynamicObject (MetadataImporter importer)
59 ReadAttribute (importer);
61 return flags != null && Position < flags.Length && flags[Position];
65 // Returns true when DynamicAttribute exists
67 public bool HasDynamicAttribute (MetadataImporter importer)
70 ReadAttribute (importer);
75 void ReadAttribute (MetadataImporter importer)
77 IList<CustomAttributeData> cad;
78 if (provider is MemberInfo) {
79 cad = CustomAttributeData.GetCustomAttributes ((MemberInfo) provider);
80 } else if (provider is ParameterInfo) {
81 cad = CustomAttributeData.GetCustomAttributes ((ParameterInfo) provider);
89 foreach (var ca in cad) {
90 importer.GetCustomAttributeTypeName (ca, out ns, out name);
91 if (name != "DynamicAttribute" && ns != CompilerServicesNamespace)
94 if (ca.ConstructorArguments.Count == 0) {
95 flags = single_attribute;
99 var arg_type = ca.ConstructorArguments[0].ArgumentType;
101 if (arg_type.IsArray && MetaType.GetTypeCode (arg_type.GetElementType ()) == TypeCode.Boolean) {
102 var carg = (IList<CustomAttributeTypedArgument>) ca.ConstructorArguments[0].Value;
103 flags = new bool[carg.Count];
104 for (int i = 0; i < flags.Length; ++i) {
105 if (MetaType.GetTypeCode (carg[i].ArgumentType) == TypeCode.Boolean)
106 flags[i] = (bool) carg[i].Value;
118 protected readonly Dictionary<MetaType, TypeSpec> import_cache;
119 protected readonly Dictionary<MetaType, BuildinTypeSpec> buildin_types;
120 readonly Dictionary<Assembly, ImportedAssemblyDefinition> assembly_2_definition;
122 public static readonly string CompilerServicesNamespace = "System.Runtime.CompilerServices";
124 protected MetadataImporter ()
126 import_cache = new Dictionary<MetaType, TypeSpec> (1024, ReferenceEquality<MetaType>.Default);
127 buildin_types = new Dictionary<MetaType, BuildinTypeSpec> (40, ReferenceEquality<MetaType>.Default);
128 assembly_2_definition = new Dictionary<Assembly, ImportedAssemblyDefinition> (ReferenceEquality<Assembly>.Default);
129 IgnorePrivateMembers = true;
134 public ICollection<ImportedAssemblyDefinition> Assemblies {
136 return assembly_2_definition.Values;
140 public bool IgnorePrivateMembers { get; set; }
144 protected abstract MemberKind DetermineKindFromBaseType (MetaType baseType);
145 protected abstract bool HasVolatileModifier (MetaType[] modifiers);
146 public abstract void GetCustomAttributeTypeName (CustomAttributeData cad, out string typeNamespace, out string typeName);
148 public FieldSpec CreateField (FieldInfo fi, TypeSpec declaringType)
151 var fa = fi.Attributes;
152 switch (fa & FieldAttributes.FieldAccessMask) {
153 case FieldAttributes.Public:
154 mod = Modifiers.PUBLIC;
156 case FieldAttributes.Assembly:
157 mod = Modifiers.INTERNAL;
159 case FieldAttributes.Family:
160 mod = Modifiers.PROTECTED;
162 case FieldAttributes.FamORAssem:
163 mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
166 // Ignore private fields (even for error reporting) to not require extra dependencies
167 if (IgnorePrivateMembers || HasAttribute (CustomAttributeData.GetCustomAttributes (fi), "CompilerGeneratedAttribute", CompilerServicesNamespace))
170 mod = Modifiers.PRIVATE;
177 field_type = ImportType (fi.FieldType, new DynamicTypeReader (fi));
178 } catch (Exception e) {
179 // TODO: I should construct fake TypeSpec based on TypeRef signature
180 // but there is no way to do it with System.Reflection
181 throw new InternalErrorException (e, "Cannot import field `{0}.{1}' referenced in assembly `{2}'",
182 declaringType.GetSignatureForError (), fi.Name, declaringType.MemberDefinition.DeclaringAssembly);
185 var definition = new ImportedMemberDefinition (fi, field_type, this);
187 if ((fa & FieldAttributes.Literal) != 0) {
188 var c = Constant.CreateConstantFromValue (field_type, fi.GetRawConstantValue (), Location.Null);
189 return new ConstSpec (declaringType, definition, field_type, fi, mod, c);
192 if ((fa & FieldAttributes.InitOnly) != 0) {
193 if (field_type == TypeManager.decimal_type) {
194 var dc = ReadDecimalConstant (CustomAttributeData.GetCustomAttributes (fi));
196 return new ConstSpec (declaringType, definition, field_type, fi, mod, dc);
199 mod |= Modifiers.READONLY;
201 var req_mod = fi.GetRequiredCustomModifiers ();
202 if (req_mod.Length > 0 && HasVolatileModifier (req_mod))
203 mod |= Modifiers.VOLATILE;
206 if ((fa & FieldAttributes.Static) != 0) {
207 mod |= Modifiers.STATIC;
209 // Fixed buffers cannot be static
210 if (declaringType.IsStruct && field_type.IsStruct && field_type.IsNested &&
211 HasAttribute (CustomAttributeData.GetCustomAttributes (fi), "FixedBufferAttribute", CompilerServicesNamespace)) {
213 // TODO: Sanity check on field_type (only few type are allowed)
214 var element_field = CreateField (fi.FieldType.GetField (FixedField.FixedElementName), declaringType);
215 return new FixedFieldSpec (declaringType, definition, fi, element_field, mod);
219 return new FieldSpec (declaringType, definition, field_type, fi, mod);
222 public EventSpec CreateEvent (EventInfo ei, TypeSpec declaringType, MethodSpec add, MethodSpec remove)
224 add.IsAccessor = true;
225 remove.IsAccessor = true;
227 if (add.Modifiers != remove.Modifiers)
228 throw new NotImplementedException ("Different accessor modifiers " + ei.Name);
230 var event_type = ImportType (ei.EventHandlerType, new DynamicTypeReader (ei));
231 var definition = new ImportedMemberDefinition (ei, event_type, this);
232 return new EventSpec (declaringType, definition, event_type, add.Modifiers, add, remove);
235 TypeParameterSpec[] CreateGenericParameters (MetaType type, TypeSpec declaringType)
237 var tparams = type.GetGenericArguments ();
239 int parent_owned_count;
241 parent_owned_count = type.DeclaringType.GetGenericArguments ().Length;
244 // System.Reflection duplicates parent type parameters for each
245 // nested type with slightly modified properties (eg. different owner)
246 // This just makes things more complicated (think of cloned constraints)
247 // therefore we remap any nested type owned by parent using `type_cache'
248 // to the single TypeParameterSpec
250 if (declaringType != null && parent_owned_count > 0) {
252 while (read_count != parent_owned_count) {
253 var tparams_count = declaringType.Arity;
254 if (tparams_count != 0) {
255 var parent_tp = declaringType.MemberDefinition.TypeParameters;
256 read_count += tparams_count;
257 for (int i = 0; i < tparams_count; i++) {
258 import_cache.Add (tparams[parent_owned_count - read_count + i], parent_tp[i]);
262 declaringType = declaringType.DeclaringType;
266 parent_owned_count = 0;
269 if (tparams.Length - parent_owned_count == 0)
272 return CreateGenericParameters (parent_owned_count, tparams);
275 TypeParameterSpec[] CreateGenericParameters (int first, MetaType[] tparams)
277 var tspec = new TypeParameterSpec[tparams.Length - first];
278 for (int pos = first; pos < tparams.Length; ++pos) {
279 var type = tparams[pos];
280 int index = pos - first;
282 tspec [index] = (TypeParameterSpec) CreateType (type, new DynamicTypeReader (), false);
288 TypeSpec[] CreateGenericArguments (int first, MetaType[] tparams, DynamicTypeReader dtype)
292 var tspec = new TypeSpec [tparams.Length - first];
293 for (int pos = first; pos < tparams.Length; ++pos) {
294 var type = tparams[pos];
295 int index = pos - first;
298 if (type.HasElementType) {
299 var element = type.GetElementType ();
301 spec = ImportType (element, dtype);
304 throw new NotImplementedException ("Unknown element type " + type.ToString ());
307 spec = ArrayContainer.MakeType (spec, type.GetArrayRank ());
309 spec = CreateType (type, dtype, true);
312 // We treat nested generic types as inflated internally where
313 // reflection uses type definition
316 // IFoo<A<T>> foo; // A<T> is definition in this case
319 // TODO: Is full logic from CreateType needed here as well?
321 if (type.IsGenericTypeDefinition) {
322 var targs = CreateGenericArguments (0, type.GetGenericArguments (), dtype);
323 spec = spec.MakeGenericType (targs);
334 public MethodSpec CreateMethod (MethodBase mb, TypeSpec declaringType)
336 Modifiers mod = ReadMethodModifiers (mb, declaringType);
337 TypeParameterSpec[] tparams;
339 var parameters = CreateParameters (declaringType, mb.GetParameters (), mb);
341 if (mb.IsGenericMethod) {
342 if (!mb.IsGenericMethodDefinition)
343 throw new NotSupportedException ("assert");
345 tparams = CreateGenericParameters (0, mb.GetGenericArguments ());
352 if (mb.MemberType == MemberTypes.Constructor) {
353 kind = MemberKind.Constructor;
354 returnType = TypeManager.void_type;
357 // Detect operators and destructors
359 string name = mb.Name;
360 kind = MemberKind.Method;
361 if (tparams == null && !mb.DeclaringType.IsInterface && name.Length > 6) {
362 if ((mod & (Modifiers.STATIC | Modifiers.PUBLIC)) == (Modifiers.STATIC | Modifiers.PUBLIC)) {
363 if (name[2] == '_' && name[1] == 'p' && name[0] == 'o') {
364 var op_type = Operator.GetType (name);
365 if (op_type.HasValue && parameters.Count > 0 && parameters.Count < 3) {
366 kind = MemberKind.Operator;
369 } else if (parameters.IsEmpty && name == Destructor.MetadataName) {
370 kind = MemberKind.Destructor;
371 if (declaringType == TypeManager.object_type) {
372 mod &= ~Modifiers.OVERRIDE;
373 mod |= Modifiers.VIRTUAL;
378 var mi = (MethodInfo) mb;
379 returnType = ImportType (mi.ReturnType, new DynamicTypeReader (mi.ReturnParameter));
381 // Cannot set to OVERRIDE without full hierarchy checks
382 // this flag indicates that the method could be override
383 // but further validation is needed
384 if ((mod & Modifiers.OVERRIDE) != 0 && kind == MemberKind.Method && declaringType.BaseType != null) {
385 var filter = MemberFilter.Method (name, tparams != null ? tparams.Length : 0, parameters, null);
386 var candidate = MemberCache.FindMember (declaringType.BaseType, filter, BindingRestriction.None);
389 // For imported class method do additional validation to be sure that metadata
390 // override flag was correct
392 // Difference between protected internal and protected is ok
394 const Modifiers conflict_mask = Modifiers.AccessibilityMask & ~Modifiers.INTERNAL;
395 if (candidate == null || (candidate.Modifiers & conflict_mask) != (mod & conflict_mask) || candidate.IsStatic) {
396 mod &= ~Modifiers.OVERRIDE;
401 IMemberDefinition definition;
402 if (tparams != null) {
403 definition = new ImportedGenericMethodDefinition ((MethodInfo) mb, returnType, parameters, tparams, this);
405 definition = new ImportedParameterMemberDefinition (mb, returnType, parameters, this);
408 MethodSpec ms = new MethodSpec (kind, declaringType, definition, returnType, mb, parameters, mod);
416 // Imports System.Reflection parameters
418 AParametersCollection CreateParameters (TypeSpec parent, ParameterInfo[] pi, MethodBase method)
420 int varargs = method != null && (method.CallingConvention & CallingConventions.VarArgs) != 0 ? 1 : 0;
422 if (pi.Length == 0 && varargs == 0)
423 return ParametersCompiled.EmptyReadOnlyParameters;
425 TypeSpec[] types = new TypeSpec[pi.Length + varargs];
426 IParameterData[] par = new IParameterData[pi.Length + varargs];
427 bool is_params = false;
428 for (int i = 0; i < pi.Length; i++) {
429 ParameterInfo p = pi[i];
430 Parameter.Modifier mod = 0;
431 Expression default_value = null;
432 if (p.ParameterType.IsByRef) {
433 if ((p.Attributes & (ParameterAttributes.Out | ParameterAttributes.In)) == ParameterAttributes.Out)
434 mod = Parameter.Modifier.OUT;
436 mod = Parameter.Modifier.REF;
439 // Strip reference wrapping
441 var el = p.ParameterType.GetElementType ();
442 types[i] = ImportType (el, new DynamicTypeReader (p)); // TODO: 1-based positio to be csc compatible
443 } else if (i == 0 && method.IsStatic && parent.IsStatic && parent.MemberDefinition.DeclaringAssembly.HasExtensionMethod &&
444 HasAttribute (CustomAttributeData.GetCustomAttributes (method), "ExtensionAttribute", CompilerServicesNamespace)) {
445 mod = Parameter.Modifier.This;
446 types[i] = ImportType (p.ParameterType);
448 types[i] = ImportType (p.ParameterType, new DynamicTypeReader (p));
450 if (i >= pi.Length - 2 && types[i] is ArrayContainer) {
451 if (HasAttribute (CustomAttributeData.GetCustomAttributes (p), "ParamArrayAttribute", "System")) {
452 mod = Parameter.Modifier.PARAMS;
457 if (!is_params && p.IsOptional) {
458 object value = p.RawDefaultValue;
459 var ptype = types[i];
460 if ((p.Attributes & ParameterAttributes.HasDefault) != 0 && ptype.Kind != MemberKind.TypeParameter && (value != null || TypeManager.IsReferenceType (ptype))) {
462 default_value = Constant.CreateConstant (null, ptype, null, Location.Null);
464 default_value = ImportParameterConstant (value).Resolve (null);
467 default_value = new EnumConstant ((Constant) default_value, ptype).Resolve (null);
470 } else if (value == Missing.Value) {
471 default_value = EmptyExpression.MissingValue;
472 } else if (value == null) {
473 default_value = new DefaultValueExpression (new TypeExpression (ptype, Location.Null), Location.Null);
474 } else if (ptype == TypeManager.decimal_type) {
475 default_value = ImportParameterConstant (value).Resolve (null);
480 par[i] = new ParameterData (p.Name, mod, default_value);
484 par[par.Length - 1] = new ArglistParameter (Location.Null);
485 types[types.Length - 1] = InternalType.Arglist;
488 return method != null ?
489 new ParametersImported (par, types, varargs != 0, is_params) :
490 new ParametersImported (par, types, is_params);
494 // Returns null when the property is not valid C# property
496 public PropertySpec CreateProperty (PropertyInfo pi, TypeSpec declaringType, MethodSpec get, MethodSpec set)
499 AParametersCollection param = null;
500 TypeSpec type = null;
503 param = get.Parameters;
504 type = get.ReturnType;
507 bool is_valid_property = true;
509 if (set.ReturnType != TypeManager.void_type)
510 is_valid_property = false;
512 var set_param_count = set.Parameters.Count - 1;
514 if (set_param_count < 0) {
516 is_valid_property = false;
519 var set_type = set.Parameters.Types[set_param_count];
522 AParametersCollection set_based_param;
524 if (set_param_count == 0) {
525 set_based_param = ParametersCompiled.EmptyReadOnlyParameters;
528 // Create indexer parameters based on setter method parameters (the last parameter has to be removed)
530 var data = new IParameterData[set_param_count];
531 var types = new TypeSpec[set_param_count];
532 Array.Copy (set.Parameters.FixedParameters, data, set_param_count);
533 Array.Copy (set.Parameters.Types, types, set_param_count);
534 set_based_param = new ParametersImported (data, types, set.Parameters.HasParams);
538 param = set_based_param;
541 if (set_param_count != get.Parameters.Count)
542 is_valid_property = false;
544 if (get.ReturnType != set_type)
545 is_valid_property = false;
547 // Possible custom accessor modifiers
548 if ((mod & Modifiers.AccessibilityMask) != (set.Modifiers & Modifiers.AccessibilityMask)) {
549 var get_acc = mod & Modifiers.AccessibilityMask;
550 if (get_acc != Modifiers.PUBLIC) {
551 var set_acc = set.Modifiers & Modifiers.AccessibilityMask;
552 // If the accessor modifiers are not same, do extra restriction checks
553 if (get_acc != set_acc) {
554 var get_restr = ModifiersExtensions.IsRestrictedModifier (get_acc, set_acc);
555 var set_restr = ModifiersExtensions.IsRestrictedModifier (set_acc, get_acc);
556 if (get_restr && set_restr) {
557 is_valid_property = false; // Neither is more restrictive
561 mod &= ~Modifiers.AccessibilityMask;
570 PropertySpec spec = null;
571 if (!param.IsEmpty) {
572 var index_name = declaringType.MemberDefinition.GetAttributeDefaultMember ();
573 if (index_name == null) {
574 is_valid_property = false;
578 is_valid_property = false;
579 if (get.Name.IndexOf (index_name, StringComparison.Ordinal) != 4)
580 is_valid_property = false;
584 is_valid_property = false;
585 if (set.Name.IndexOf (index_name, StringComparison.Ordinal) != 4)
586 is_valid_property = false;
590 if (is_valid_property)
591 spec = new IndexerSpec (declaringType, new ImportedParameterMemberDefinition (pi, type, param, this), type, param, pi, mod);
595 spec = new PropertySpec (MemberKind.Property, declaringType, new ImportedMemberDefinition (pi, type, this), type, pi, mod);
597 if (!is_valid_property) {
598 spec.IsNotRealProperty = true;
610 public TypeSpec CreateType (MetaType type)
612 return CreateType (type, new DynamicTypeReader (), true);
615 public TypeSpec CreateNestedType (MetaType type, TypeSpec declaringType)
617 return CreateType (type, declaringType, new DynamicTypeReader (type), false);
620 TypeSpec CreateType (MetaType type, DynamicTypeReader dtype, bool canImportBaseType)
622 TypeSpec declaring_type;
623 if (type.IsNested && !type.IsGenericParameter)
624 declaring_type = CreateType (type.DeclaringType, new DynamicTypeReader (type.DeclaringType), true);
626 declaring_type = null;
628 return CreateType (type, declaring_type, dtype, canImportBaseType);
631 TypeSpec CreateType (MetaType type, TypeSpec declaringType, DynamicTypeReader dtype, bool canImportBaseType)
634 if (import_cache.TryGetValue (type, out spec)) {
635 if (spec == TypeManager.object_type) {
636 if (dtype.IsDynamicObject (this))
637 return InternalType.Dynamic;
642 if (!spec.IsGeneric || type.IsGenericTypeDefinition)
645 if (!dtype.HasDynamicAttribute (this))
648 // We've found same object in the cache but this one has a dynamic custom attribute
649 // and it's most likely dynamic version of same type IFoo<object> agains IFoo<dynamic>
650 // Do type resolve process again in that case
652 // TODO: Handle cases where they still unify
655 if (type.IsGenericType && !type.IsGenericTypeDefinition) {
656 var type_def = type.GetGenericTypeDefinition ();
657 var targs = CreateGenericArguments (0, type.GetGenericArguments (), dtype);
658 if (declaringType == null) {
659 // Simple case, no nesting
660 spec = CreateType (type_def, null, new DynamicTypeReader (), canImportBaseType);
661 spec = spec.MakeGenericType (targs);
664 // Nested type case, converting .NET types like
665 // A`1.B`1.C`1<int, long, string> to typespec like
666 // A<int>.B<long>.C<string>
668 var nested_hierarchy = new List<TypeSpec> ();
669 while (declaringType.IsNested) {
670 nested_hierarchy.Add (declaringType);
671 declaringType = declaringType.DeclaringType;
675 if (declaringType.Arity > 0) {
676 spec = declaringType.MakeGenericType (targs.Skip (targs_pos).Take (declaringType.Arity).ToArray ());
677 targs_pos = spec.Arity;
679 spec = declaringType;
682 for (int i = nested_hierarchy.Count; i != 0; --i) {
683 var t = nested_hierarchy [i - 1];
684 spec = MemberCache.FindNestedType (spec, t.Name, t.Arity);
686 spec = spec.MakeGenericType (targs.Skip (targs_pos).Take (spec.Arity).ToArray ());
687 targs_pos += t.Arity;
691 string name = type.Name;
692 int index = name.IndexOf ('`');
694 name = name.Substring (0, index);
696 spec = MemberCache.FindNestedType (spec, name, targs.Length - targs_pos);
697 if (spec.Arity > 0) {
698 spec = spec.MakeGenericType (targs.Skip (targs_pos).ToArray ());
702 // Don't add generic type with dynamic arguments, they can interfere with same type
703 // using object type arguments
704 if (!spec.HasDynamicElement) {
706 // Add to reading cache to speed up reading
707 if (!import_cache.ContainsKey (type))
708 import_cache.Add (type, spec);
717 var ma = type.Attributes;
718 switch (ma & TypeAttributes.VisibilityMask) {
719 case TypeAttributes.Public:
720 case TypeAttributes.NestedPublic:
721 mod = Modifiers.PUBLIC;
723 case TypeAttributes.NestedPrivate:
724 mod = Modifiers.PRIVATE;
726 case TypeAttributes.NestedFamily:
727 mod = Modifiers.PROTECTED;
729 case TypeAttributes.NestedFamORAssem:
730 mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
733 mod = Modifiers.INTERNAL;
737 if ((ma & TypeAttributes.Interface) != 0) {
738 kind = MemberKind.Interface;
739 } else if (type.IsGenericParameter) {
740 kind = MemberKind.TypeParameter;
742 var base_type = type.BaseType;
743 if (base_type == null || (ma & TypeAttributes.Abstract) != 0) {
744 kind = MemberKind.Class;
746 kind = DetermineKindFromBaseType (base_type);
747 if (kind == MemberKind.Struct || kind == MemberKind.Delegate) {
748 mod |= Modifiers.SEALED;
752 if (kind == MemberKind.Class) {
753 if ((ma & TypeAttributes.Sealed) != 0) {
754 mod |= Modifiers.SEALED;
755 if ((ma & TypeAttributes.Abstract) != 0)
756 mod |= Modifiers.STATIC;
757 } else if ((ma & TypeAttributes.Abstract) != 0) {
758 mod |= Modifiers.ABSTRACT;
763 var definition = new ImportedTypeDefinition (type, this);
766 if (kind == MemberKind.Enum) {
767 const BindingFlags underlying_member = BindingFlags.DeclaredOnly |
768 BindingFlags.Instance |
769 BindingFlags.Public | BindingFlags.NonPublic;
771 var type_members = type.GetFields (underlying_member);
772 foreach (var type_member in type_members) {
773 spec = new EnumSpec (declaringType, definition, CreateType (type_member.FieldType), type, mod);
778 kind = MemberKind.Class;
780 } else if (kind == MemberKind.TypeParameter) {
781 // Return as type_cache was updated
782 return CreateTypeParameter (type, declaringType);
783 } else if (type.IsGenericTypeDefinition) {
784 definition.TypeParameters = CreateGenericParameters (type, declaringType);
786 // Constraints are not loaded on demand and can reference this type
787 if (import_cache.TryGetValue (type, out spec))
790 } else if (buildin_types.TryGetValue (type, out pt)) {
792 pt.SetDefinition (definition, type, mod);
796 spec = new TypeSpec (kind, declaringType, definition, type, mod);
798 import_cache.Add (type, spec);
801 // Two stage setup as the base type can be inflated declaring type or
802 // another nested type inside same declaring type which has not been
803 // loaded, therefore we can import a base type of nested types once
804 // the types have been imported
806 if (canImportBaseType)
807 ImportTypeBase (spec, type);
812 public ImportedAssemblyDefinition GetAssemblyDefinition (Assembly assembly)
814 ImportedAssemblyDefinition def;
815 if (!assembly_2_definition.TryGetValue (assembly, out def)) {
817 // This can happen in dynamic context only
818 def = new ImportedAssemblyDefinition (assembly, this);
819 assembly_2_definition.Add (assembly, def);
820 def.ReadAttributes ();
826 public void ImportTypeBase (MetaType type)
828 TypeSpec spec = import_cache[type];
830 ImportTypeBase (spec, type);
833 void ImportTypeBase (TypeSpec spec, MetaType type)
835 if (spec.Kind == MemberKind.Interface)
836 spec.BaseType = TypeManager.object_type;
837 else if (type.BaseType != null) {
839 if (type.BaseType.IsGenericType)
840 base_type = CreateType (type.BaseType, new DynamicTypeReader (type), true);
842 base_type = CreateType (type.BaseType);
844 spec.BaseType = base_type;
847 var ifaces = type.GetInterfaces ();
848 if (ifaces.Length > 0) {
849 foreach (var iface in ifaces) {
850 spec.AddInterface (CreateType (iface));
855 TypeParameterSpec CreateTypeParameter (MetaType type, TypeSpec declaringType)
858 switch (type.GenericParameterAttributes & GenericParameterAttributes.VarianceMask) {
859 case GenericParameterAttributes.Covariant:
860 variance = Variance.Covariant;
862 case GenericParameterAttributes.Contravariant:
863 variance = Variance.Contravariant;
866 variance = Variance.None;
870 SpecialConstraint special = SpecialConstraint.None;
871 var import_special = type.GenericParameterAttributes & GenericParameterAttributes.SpecialConstraintMask;
873 if ((import_special & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0) {
874 special |= SpecialConstraint.Struct;
875 } else if ((import_special & GenericParameterAttributes.DefaultConstructorConstraint) != 0) {
876 special = SpecialConstraint.Constructor;
879 if ((import_special & GenericParameterAttributes.ReferenceTypeConstraint) != 0) {
880 special |= SpecialConstraint.Class;
883 TypeParameterSpec spec;
884 var def = new ImportedTypeParameterDefinition (type, this);
885 if (type.DeclaringMethod != null)
886 spec = new TypeParameterSpec (type.GenericParameterPosition, def, special, variance, type);
888 spec = new TypeParameterSpec (declaringType, type.GenericParameterPosition, def, special, variance, type);
890 // Add it now, so any constraint can reference it and get same instance
891 import_cache.Add (type, spec);
893 var constraints = type.GetGenericParameterConstraints ();
894 List<TypeSpec> tparams = null;
895 foreach (var ct in constraints) {
896 if (ct.IsGenericParameter) {
898 tparams = new List<TypeSpec> ();
900 tparams.Add (CreateType (ct));
905 spec.BaseType = CreateType (ct);
909 spec.AddInterface (CreateType (ct));
912 if (spec.BaseType == null)
913 spec.BaseType = TypeManager.object_type;
916 spec.TypeArguments = tparams.ToArray ();
922 // Test for a custom attribute type match. Custom attributes are not really predefined globaly
923 // they can be assembly specific therefore we do check based on names only
925 public bool HasAttribute (IList<CustomAttributeData> attributesData, string attrName, string attrNamespace)
927 if (attributesData.Count == 0)
931 foreach (var attr in attributesData) {
932 GetCustomAttributeTypeName (attr, out ns, out name);
933 if (name == attrName && ns == attrNamespace)
940 protected void ImportTypes (MetaType[] types, Namespace targetNamespace, bool hasExtensionTypes)
942 Namespace ns = targetNamespace;
943 string prev_namespace = null;
944 foreach (var t in types) {
948 // Be careful not to trigger full parent type loading
949 if (t.MemberType == MemberTypes.NestedType)
952 if (t.Name[0] == '<')
955 var it = CreateType (t, null, new DynamicTypeReader (t), true);
959 if (prev_namespace != t.Namespace) {
960 ns = t.Namespace == null ? targetNamespace : targetNamespace.GetNamespace (t.Namespace, true);
961 prev_namespace = t.Namespace;
966 if (it.IsStatic && hasExtensionTypes &&
967 HasAttribute (CustomAttributeData.GetCustomAttributes (t), "ExtensionAttribute", CompilerServicesNamespace)) {
968 it.SetExtensionMethodContainer ();
973 static Constant ImportParameterConstant (object value)
976 // Get type of underlying value as int constant can be used for object
977 // parameter type. This is not allowed in C# but other languages can do that
979 switch (System.Type.GetTypeCode (value.GetType ())) {
980 case TypeCode.Boolean:
981 return new BoolConstant ((bool) value, Location.Null);
983 return new ByteConstant ((byte) value, Location.Null);
985 return new CharConstant ((char) value, Location.Null);
986 case TypeCode.Decimal:
987 return new DecimalConstant ((decimal) value, Location.Null);
988 case TypeCode.Double:
989 return new DoubleConstant ((double) value, Location.Null);
991 return new ShortConstant ((short) value, Location.Null);
993 return new IntConstant ((int) value, Location.Null);
995 return new LongConstant ((long) value, Location.Null);
997 return new SByteConstant ((sbyte) value, Location.Null);
998 case TypeCode.Single:
999 return new FloatConstant ((float) value, Location.Null);
1000 case TypeCode.String:
1001 return new StringConstant ((string) value, Location.Null);
1002 case TypeCode.UInt16:
1003 return new UShortConstant ((ushort) value, Location.Null);
1004 case TypeCode.UInt32:
1005 return new UIntConstant ((uint) value, Location.Null);
1006 case TypeCode.UInt64:
1007 return new ULongConstant ((ulong) value, Location.Null);
1010 throw new NotImplementedException (value.GetType ().ToString ());
1013 public TypeSpec ImportType (MetaType type)
1015 return ImportType (type, new DynamicTypeReader (type));
1018 TypeSpec ImportType (MetaType type, DynamicTypeReader dtype)
1020 if (type.HasElementType) {
1021 var element = type.GetElementType ();
1023 var spec = ImportType (element, dtype);
1026 return ArrayContainer.MakeType (spec, type.GetArrayRank ());
1028 return ReferenceContainer.MakeType (spec);
1030 return PointerContainer.MakeType (spec);
1032 throw new NotImplementedException ("Unknown element type " + type.ToString ());
1035 return CreateType (type, dtype, true);
1039 // Decimal constants cannot be encoded in the constant blob, and thus are marked
1040 // as IsInitOnly ('readonly' in C# parlance). We get its value from the
1041 // DecimalConstantAttribute metadata.
1043 Constant ReadDecimalConstant (IList<CustomAttributeData> attrs)
1045 if (attrs.Count == 0)
1049 foreach (var ca in attrs) {
1050 GetCustomAttributeTypeName (ca, out ns, out name);
1051 if (name != "DecimalConstantAttribute" || ns != CompilerServicesNamespace)
1054 var value = new decimal (
1055 (int) (uint) ca.ConstructorArguments[4].Value,
1056 (int) (uint) ca.ConstructorArguments[3].Value,
1057 (int) (uint) ca.ConstructorArguments[2].Value,
1058 (byte) ca.ConstructorArguments[1].Value != 0,
1059 (byte) ca.ConstructorArguments[0].Value);
1061 return new DecimalConstant (value, Location.Null).Resolve (null);
1067 static Modifiers ReadMethodModifiers (MethodBase mb, TypeSpec declaringType)
1070 var ma = mb.Attributes;
1071 switch (ma & MethodAttributes.MemberAccessMask) {
1072 case MethodAttributes.Public:
1073 mod = Modifiers.PUBLIC;
1075 case MethodAttributes.Assembly:
1076 mod = Modifiers.INTERNAL;
1078 case MethodAttributes.Family:
1079 mod = Modifiers.PROTECTED;
1081 case MethodAttributes.FamORAssem:
1082 mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
1085 mod = Modifiers.PRIVATE;
1089 if ((ma & MethodAttributes.Static) != 0) {
1090 mod |= Modifiers.STATIC;
1093 if ((ma & MethodAttributes.Abstract) != 0 && declaringType.IsClass) {
1094 mod |= Modifiers.ABSTRACT;
1098 if ((ma & MethodAttributes.Final) != 0)
1099 mod |= Modifiers.SEALED;
1101 // It can be sealed and override
1102 if ((ma & MethodAttributes.Virtual) != 0) {
1103 if ((ma & MethodAttributes.NewSlot) != 0 || !declaringType.IsClass) {
1104 // No private virtual or sealed virtual
1105 if ((mod & (Modifiers.PRIVATE | Modifiers.SEALED)) == 0)
1106 mod |= Modifiers.VIRTUAL;
1108 mod |= Modifiers.OVERRIDE;
1116 abstract class ImportedDefinition : IMemberDefinition
1118 protected class AttributesBag
1120 public static readonly AttributesBag Default = new AttributesBag ();
1122 public AttributeUsageAttribute AttributeUsage;
1123 public ObsoleteAttribute Obsolete;
1124 public string[] Conditionals;
1125 public string DefaultIndexerName;
1126 public bool IsNotCLSCompliant;
1127 public TypeSpec CoClass;
1129 public static AttributesBag Read (MemberInfo mi, MetadataImporter importer)
1131 AttributesBag bag = null;
1132 List<string> conditionals = null;
1134 // It should not throw any loading exception
1135 IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes (mi);
1138 foreach (var a in attrs) {
1139 importer.GetCustomAttributeTypeName (a, out ns, out name);
1140 if (name == "ObsoleteAttribute") {
1145 bag = new AttributesBag ();
1147 var args = a.ConstructorArguments;
1149 if (args.Count == 1) {
1150 bag.Obsolete = new ObsoleteAttribute ((string) args[0].Value);
1151 } else if (args.Count == 2) {
1152 bag.Obsolete = new ObsoleteAttribute ((string) args[0].Value, (bool) args[1].Value);
1154 bag.Obsolete = new ObsoleteAttribute ();
1160 if (name == "ConditionalAttribute") {
1161 if (ns != "System.Diagnostics")
1165 bag = new AttributesBag ();
1167 if (conditionals == null)
1168 conditionals = new List<string> (2);
1170 conditionals.Add ((string) a.ConstructorArguments[0].Value);
1174 if (name == "CLSCompliantAttribute") {
1179 bag = new AttributesBag ();
1181 bag.IsNotCLSCompliant = !(bool) a.ConstructorArguments[0].Value;
1185 // Type only attributes
1186 if (mi.MemberType == MemberTypes.TypeInfo || mi.MemberType == MemberTypes.NestedType) {
1187 if (name == "DefaultMemberAttribute") {
1188 if (ns != "System.Reflection")
1192 bag = new AttributesBag ();
1194 bag.DefaultIndexerName = (string) a.ConstructorArguments[0].Value;
1198 if (name == "AttributeUsageAttribute") {
1203 bag = new AttributesBag ();
1205 bag.AttributeUsage = new AttributeUsageAttribute ((AttributeTargets) a.ConstructorArguments[0].Value);
1206 foreach (var named in a.NamedArguments) {
1207 if (named.MemberInfo.Name == "AllowMultiple")
1208 bag.AttributeUsage.AllowMultiple = (bool) named.TypedValue.Value;
1209 else if (named.MemberInfo.Name == "Inherited")
1210 bag.AttributeUsage.Inherited = (bool) named.TypedValue.Value;
1215 // Interface only attribute
1216 if (name == "CoClassAttribute") {
1217 if (ns != "System.Runtime.InteropServices")
1221 bag = new AttributesBag ();
1223 bag.CoClass = importer.ImportType ((MetaType) a.ConstructorArguments[0].Value);
1232 if (conditionals != null)
1233 bag.Conditionals = conditionals.ToArray ();
1239 protected readonly MemberInfo provider;
1240 protected AttributesBag cattrs;
1241 protected readonly MetadataImporter importer;
1243 public ImportedDefinition (MemberInfo provider, MetadataImporter importer)
1245 this.provider = provider;
1246 this.importer = importer;
1251 public bool IsImported {
1257 public virtual string Name {
1259 return provider.Name;
1265 public abstract List<MissingType> ResolveMissingDependencies ();
1267 public string[] ConditionalConditions ()
1272 return cattrs.Conditionals;
1275 public ObsoleteAttribute GetAttributeObsolete ()
1280 return cattrs.Obsolete;
1283 public bool IsNotCLSCompliant ()
1288 return cattrs.IsNotCLSCompliant;
1291 protected void ReadAttributes ()
1293 cattrs = AttributesBag.Read (provider, importer);
1296 public void SetIsAssigned ()
1298 // Unused for imported members
1301 public void SetIsUsed ()
1303 // Unused for imported members
1307 public class ImportedModuleDefinition
1309 readonly Module module;
1311 readonly MetadataImporter importer;
1313 public ImportedModuleDefinition (Module module, MetadataImporter importer)
1315 this.module = module;
1316 this.importer = importer;
1321 public bool IsCLSCompliant {
1323 return cls_compliant;
1327 public string Name {
1335 public void ReadAttributes ()
1337 IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes (module);
1340 foreach (var a in attrs) {
1341 importer.GetCustomAttributeTypeName (a, out ns, out name);
1342 if (name == "CLSCompliantAttribute") {
1346 cls_compliant = (bool) a.ConstructorArguments[0].Value;
1353 // Reads assembly attributes which where attached to a special type because
1354 // module does have assembly manifest
1356 public List<Attribute> ReadAssemblyAttributes ()
1358 var t = module.GetType (AssemblyAttributesPlaceholder.GetGeneratedName (Name));
1362 var field = t.GetField (AssemblyAttributesPlaceholder.AssemblyFieldName, BindingFlags.NonPublic | BindingFlags.Static);
1366 // TODO: implement, the idea is to fabricate specil Attribute class and
1367 // add it to OptAttributes before resolving the source code attributes
1368 // Need to build module location as well for correct error reporting
1370 //var assembly_attributes = CustomAttributeData.GetCustomAttributes (field);
1371 //var attrs = new List<Attribute> (assembly_attributes.Count);
1372 //foreach (var a in assembly_attributes)
1374 // var type = metaImporter.ImportType (a.Constructor.DeclaringType);
1375 // var ctor = metaImporter.CreateMethod (a.Constructor, type);
1377 // foreach (var carg in a.ConstructorArguments) {
1381 // attrs.Add (new Attribute ("assembly", ctor, null, Location.Null, true));
1388 public class ImportedAssemblyDefinition : IAssemblyDefinition
1390 readonly Assembly assembly;
1391 readonly AssemblyName aname;
1392 readonly MetadataImporter importer;
1394 bool contains_extension_methods;
1396 List<AssemblyName> internals_visible_to;
1397 Dictionary<IAssemblyDefinition, AssemblyName> internals_visible_to_cache;
1399 public ImportedAssemblyDefinition (Assembly assembly, MetadataImporter importer)
1401 this.assembly = assembly;
1402 this.aname = assembly.GetName ();
1403 this.importer = importer;
1408 public Assembly Assembly {
1414 public string FullName {
1416 return aname.FullName;
1420 public bool HasExtensionMethod {
1422 return contains_extension_methods;
1426 public bool HasStrongName {
1428 return aname.GetPublicKey ().Length != 0;
1432 public bool IsCLSCompliant {
1434 return cls_compliant;
1438 public string Location {
1440 return assembly.Location;
1444 public string Name {
1452 public byte[] GetPublicKeyToken ()
1454 return aname.GetPublicKeyToken ();
1457 public AssemblyName GetAssemblyVisibleToName (IAssemblyDefinition assembly)
1459 return internals_visible_to_cache [assembly];
1462 public bool IsFriendAssemblyTo (IAssemblyDefinition assembly)
1464 if (internals_visible_to == null)
1467 AssemblyName is_visible = null;
1468 if (internals_visible_to_cache == null) {
1469 internals_visible_to_cache = new Dictionary<IAssemblyDefinition, AssemblyName> ();
1471 if (internals_visible_to_cache.TryGetValue (assembly, out is_visible))
1472 return is_visible != null;
1475 var token = assembly.GetPublicKeyToken ();
1476 if (token != null && token.Length == 0)
1479 foreach (var internals in internals_visible_to) {
1480 if (internals.Name != assembly.Name)
1483 if (token == null && assembly is AssemblyDefinition) {
1484 is_visible = internals;
1488 if (!ArrayComparer.IsEqual (token, internals.GetPublicKeyToken ()))
1491 is_visible = internals;
1495 internals_visible_to_cache.Add (assembly, is_visible);
1496 return is_visible != null;
1499 public void ReadAttributes ()
1501 IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes (assembly);
1504 foreach (var a in attrs) {
1505 importer.GetCustomAttributeTypeName (a, out ns, out name);
1507 if (name == "CLSCompliantAttribute") {
1509 cls_compliant = (bool) a.ConstructorArguments[0].Value;
1513 if (name == "InternalsVisibleToAttribute") {
1514 if (ns != MetadataImporter.CompilerServicesNamespace)
1517 string s = a.ConstructorArguments[0].Value as string;
1521 var an = new AssemblyName (s);
1522 if (internals_visible_to == null)
1523 internals_visible_to = new List<AssemblyName> ();
1525 internals_visible_to.Add (an);
1529 if (name == "ExtensionAttribute") {
1530 if (ns == MetadataImporter.CompilerServicesNamespace)
1531 contains_extension_methods = true;
1538 public override string ToString ()
1544 class ImportedMemberDefinition : ImportedDefinition
1546 readonly TypeSpec type;
1548 public ImportedMemberDefinition (MemberInfo member, TypeSpec type, MetadataImporter importer)
1549 : base (member, importer)
1556 public TypeSpec MemberType {
1564 public override List<MissingType> ResolveMissingDependencies ()
1566 return type.GetMissingDependencies ();
1570 class ImportedParameterMemberDefinition : ImportedMemberDefinition, IParametersMember
1572 readonly AParametersCollection parameters;
1574 public ImportedParameterMemberDefinition (MethodBase provider, TypeSpec type, AParametersCollection parameters, MetadataImporter importer)
1575 : base (provider, type, importer)
1577 this.parameters = parameters;
1580 public ImportedParameterMemberDefinition (PropertyInfo provider, TypeSpec type, AParametersCollection parameters, MetadataImporter importer)
1581 : base (provider, type, importer)
1583 this.parameters = parameters;
1588 public AParametersCollection Parameters {
1596 public override List<MissingType> ResolveMissingDependencies ()
1598 var missing = base.ResolveMissingDependencies ();
1599 foreach (var pt in parameters.Types) {
1600 var m = pt.GetMissingDependencies ();
1604 if (missing == null)
1605 missing = new List<MissingType> ();
1607 missing.AddRange (m);
1614 class ImportedGenericMethodDefinition : ImportedParameterMemberDefinition, IGenericMethodDefinition
1616 readonly TypeParameterSpec[] tparams;
1618 public ImportedGenericMethodDefinition (MethodInfo provider, TypeSpec type, AParametersCollection parameters, TypeParameterSpec[] tparams, MetadataImporter importer)
1619 : base (provider, type, parameters, importer)
1621 this.tparams = tparams;
1626 public TypeParameterSpec[] TypeParameters {
1632 public int TypeParametersCount {
1634 return tparams.Length;
1641 class ImportedTypeDefinition : ImportedDefinition, ITypeDefinition
1643 TypeParameterSpec[] tparams;
1646 public ImportedTypeDefinition (MetaType type, MetadataImporter importer)
1647 : base (type, importer)
1653 public IAssemblyDefinition DeclaringAssembly {
1655 return importer.GetAssemblyDefinition (provider.Module.Assembly);
1659 public override string Name {
1663 if (tparams != null) {
1664 int arity_start = name.IndexOf ('`');
1665 if (arity_start > 0)
1666 name = name.Substring (0, arity_start);
1674 public string Namespace {
1676 return ((MetaType) provider).Namespace;
1680 public int TypeParametersCount {
1682 return tparams == null ? 0 : tparams.Length;
1686 public TypeParameterSpec[] TypeParameters {
1697 public static void Error_MissingDependency (IMemberContext ctx, List<MissingType> types, Location loc)
1699 foreach (var t in types) {
1700 string name = t.Name;
1701 if (t.Namespace != null)
1702 name = t.Namespace + "." + name;
1704 if (t.Module.Assembly.GetName ().Name == ctx.Module.DeclaringAssembly.Name) {
1705 ctx.Compiler.Report.Warning (1683, 1, loc,
1706 "Reference to type `{0}' claims it is defined in this assembly, but it is not defined in source or any added modules",
1710 ctx.Compiler.Report.Error (12, loc,
1711 "The type `{0}' is defined in an assembly that is not referenced. Consider adding a reference to assembly `{1}'",
1712 name, t.Module.Assembly.FullName);
1716 public TypeSpec GetAttributeCoClass ()
1721 return cattrs.CoClass;
1724 public string GetAttributeDefaultMember ()
1729 return cattrs.DefaultIndexerName;
1732 public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
1737 return cattrs.AttributeUsage;
1740 bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
1742 var a = importer.GetAssemblyDefinition (provider.Module.Assembly);
1743 return a == assembly || a.IsFriendAssemblyTo (assembly);
1746 public void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
1749 // Not interested in members of nested private types unless the importer needs them
1751 if (declaringType.IsPrivate && importer.IgnorePrivateMembers) {
1752 cache = MemberCache.Empty;
1756 var loading_type = (MetaType) provider;
1757 const BindingFlags all_members = BindingFlags.DeclaredOnly |
1758 BindingFlags.Static | BindingFlags.Instance |
1759 BindingFlags.Public | BindingFlags.NonPublic;
1761 const MethodAttributes explicit_impl = MethodAttributes.NewSlot |
1762 MethodAttributes.Virtual | MethodAttributes.HideBySig |
1763 MethodAttributes.Final;
1765 Dictionary<MethodBase, MethodSpec> possible_accessors = null;
1766 List<EventSpec> imported_events = null;
1767 EventSpec event_spec;
1768 MemberSpec imported;
1772 all = loading_type.GetMembers (all_members);
1773 } catch (Exception e) {
1774 throw new InternalErrorException (e, "Could not import type `{0}' from `{1}'",
1775 declaringType.GetSignatureForError (), declaringType.MemberDefinition.DeclaringAssembly.FullName);
1778 if (cache == null) {
1779 cache = new MemberCache (all.Length);
1782 // Do the types first as they can be referenced by the members before
1783 // they are found or inflated
1785 foreach (var member in all) {
1786 if (member.MemberType != MemberTypes.NestedType)
1789 var t = (MetaType) member;
1791 // Ignore compiler generated types, mostly lambda containers
1792 if ((t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate)
1795 imported = importer.CreateNestedType (t, declaringType);
1796 cache.AddMember (imported);
1799 foreach (var member in all) {
1800 if (member.MemberType != MemberTypes.NestedType)
1803 var t = (MetaType) member;
1805 if ((t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate)
1808 importer.ImportTypeBase (t);
1814 // The logic here requires methods to be returned first which seems to work for both Mono and .NET
1816 foreach (var member in all) {
1817 switch (member.MemberType) {
1818 case MemberTypes.Constructor:
1819 case MemberTypes.Method:
1820 MethodBase mb = (MethodBase) member;
1821 var attrs = mb.Attributes;
1823 if ((attrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Private) {
1824 if (importer.IgnorePrivateMembers)
1827 // Ignore explicitly implemented members
1828 if ((attrs & explicit_impl) == explicit_impl)
1831 // Ignore compiler generated methods
1832 if (importer.HasAttribute (CustomAttributeData.GetCustomAttributes (mb), "CompilerGeneratedAttribute", MetadataImporter.CompilerServicesNamespace))
1836 imported = importer.CreateMethod (mb, declaringType);
1837 if (imported.Kind == MemberKind.Method && !imported.IsGeneric) {
1838 if (possible_accessors == null)
1839 possible_accessors = new Dictionary<MethodBase, MethodSpec> (ReferenceEquality<MethodBase>.Default);
1841 // There are no metadata rules for accessors, we have to consider any method as possible candidate
1842 possible_accessors.Add (mb, (MethodSpec) imported);
1846 case MemberTypes.Property:
1847 if (possible_accessors == null)
1850 var p = (PropertyInfo) member;
1852 // Links possible accessors with property
1854 MethodSpec get, set;
1855 m = p.GetGetMethod (true);
1856 if (m == null || !possible_accessors.TryGetValue (m, out get))
1859 m = p.GetSetMethod (true);
1860 if (m == null || !possible_accessors.TryGetValue (m, out set))
1863 // No accessors registered (e.g. explicit implementation)
1864 if (get == null && set == null)
1867 imported = importer.CreateProperty (p, declaringType, get, set);
1868 if (imported == null)
1872 case MemberTypes.Event:
1873 if (possible_accessors == null)
1876 var e = (EventInfo) member;
1878 // Links accessors with event
1880 MethodSpec add, remove;
1881 m = e.GetAddMethod (true);
1882 if (m == null || !possible_accessors.TryGetValue (m, out add))
1885 m = e.GetRemoveMethod (true);
1886 if (m == null || !possible_accessors.TryGetValue (m, out remove))
1889 // Both accessors are required
1890 if (add == null || remove == null)
1893 event_spec = importer.CreateEvent (e, declaringType, add, remove);
1894 if (!importer.IgnorePrivateMembers) {
1895 if (imported_events == null)
1896 imported_events = new List<EventSpec> ();
1898 imported_events.Add (event_spec);
1901 imported = event_spec;
1903 case MemberTypes.Field:
1904 var fi = (FieldInfo) member;
1906 imported = importer.CreateField (fi, declaringType);
1907 if (imported == null)
1911 // For dynamic binder event has to be fully restored to allow operations
1912 // within the type container to work correctly
1914 if (imported_events != null) {
1915 // The backing event field should be private but it may not
1916 int index = imported_events.FindIndex (l => l.Name == fi.Name);
1918 event_spec = imported_events[index];
1919 event_spec.BackingField = (FieldSpec) imported;
1920 imported_events.RemoveAt (index);
1926 case MemberTypes.NestedType:
1927 // Already in the cache from the first pass
1930 throw new NotImplementedException (member.ToString ());
1933 cache.AddMember (imported);
1937 if (declaringType.IsInterface && declaringType.Interfaces != null) {
1938 foreach (var iface in declaringType.Interfaces) {
1939 cache.AddInterface (iface);
1944 public override List<MissingType> ResolveMissingDependencies ()
1947 List<MissingType> missing = null;
1949 MetaType mt = (MetaType) provider;
1951 if (mt is MissingType) {
1952 missing = new List<MissingType> ();
1953 missing.Add ((MissingType) mt);
1956 foreach (var iface in mt.GetInterfaces ()) {
1957 if (iface is MissingType) {
1958 if (missing == null)
1959 missing = new List<MissingType> ();
1961 missing.Add ((MissingType) iface);
1965 if (missing != null)
1969 } while (mt != null);
1975 class ImportedTypeParameterDefinition : ImportedDefinition, ITypeDefinition
1977 public ImportedTypeParameterDefinition (MetaType type, MetadataImporter importer)
1978 : base (type, importer)
1984 public IAssemblyDefinition DeclaringAssembly {
1986 throw new NotImplementedException ();
1990 public string Namespace {
1996 public int TypeParametersCount {
2002 public TypeParameterSpec[] TypeParameters {
2010 public TypeSpec GetAttributeCoClass ()
2015 public string GetAttributeDefaultMember ()
2017 throw new NotSupportedException ();
2020 public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
2022 throw new NotSupportedException ();
2025 bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
2027 throw new NotImplementedException ();
2030 public void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
2032 throw new NotImplementedException ();
2035 public override List<MissingType> ResolveMissingDependencies ()