[672969] Delay type parameter constrain import until its declaring type is ready
[mono.git] / mcs / mcs / import.cs
1 //
2 // import.cs: System.Reflection conversions
3 //
4 // Authors: Marek Safar (marek.safar@gmail.com)
5 //
6 // Dual licensed under the terms of the MIT X11 or GNU GPL
7 //
8 // Copyright 2009, 2010 Novell, Inc
9 //
10
11 using System;
12 using System.Runtime.CompilerServices;
13 using System.Linq;
14 using System.Collections.Generic;
15
16 #if STATIC
17 using MetaType = IKVM.Reflection.Type;
18 using IKVM.Reflection;
19 using IKVM.Reflection.Emit;
20 #else
21 using MetaType = System.Type;
22 using System.Reflection;
23 using System.Reflection.Emit;
24 #endif
25
26 namespace Mono.CSharp
27 {
28         public abstract class MetadataImporter
29         {
30                 //
31                 // Dynamic types reader with additional logic to reconstruct a dynamic
32                 // type using DynamicAttribute values
33                 //
34                 struct DynamicTypeReader
35                 {
36                         static readonly bool[] single_attribute = { true };
37
38                         public int Position;
39                         bool[] flags;
40
41                         // There is no common type for CustomAttributeData and we cannot
42                         // use ICustomAttributeProvider
43                         object provider;
44
45                         //
46                         // A member provider which can be used to get CustomAttributeData
47                         //
48                         public DynamicTypeReader (object provider)
49                         {
50                                 Position = 0;
51                                 flags = null;
52                                 this.provider = provider;
53                         }
54
55                         //
56                         // Returns true when object at local position has dynamic attribute flag
57                         //
58                         public bool IsDynamicObject (MetadataImporter importer)
59                         {
60                                 if (provider != null)
61                                         ReadAttribute (importer);
62
63                                 return flags != null && Position < flags.Length && flags[Position];
64                         }
65
66                         //
67                         // Returns true when DynamicAttribute exists
68                         //
69                         public bool HasDynamicAttribute (MetadataImporter importer)
70                         {
71                                 if (provider != null)
72                                         ReadAttribute (importer);
73
74                                 return flags != null;
75                         }
76
77                         void ReadAttribute (MetadataImporter importer)
78                         {
79                                 IList<CustomAttributeData> cad;
80                                 if (provider is MemberInfo) {
81                                         cad = CustomAttributeData.GetCustomAttributes ((MemberInfo) provider);
82                                 } else if (provider is ParameterInfo) {
83                                         cad = CustomAttributeData.GetCustomAttributes ((ParameterInfo) provider);
84                                 } else {
85                                         provider = null;
86                                         return;
87                                 }
88
89                                 if (cad.Count > 0) {
90                                         string ns, name;
91                                         foreach (var ca in cad) {
92                                                 importer.GetCustomAttributeTypeName (ca, out ns, out name);
93                                                 if (name != "DynamicAttribute" && ns != CompilerServicesNamespace)
94                                                         continue;
95
96                                                 if (ca.ConstructorArguments.Count == 0) {
97                                                         flags = single_attribute;
98                                                         break;
99                                                 }
100
101                                                 var arg_type = ca.ConstructorArguments[0].ArgumentType;
102
103                                                 if (arg_type.IsArray && MetaType.GetTypeCode (arg_type.GetElementType ()) == TypeCode.Boolean) {
104                                                         var carg = (IList<CustomAttributeTypedArgument>) ca.ConstructorArguments[0].Value;
105                                                         flags = new bool[carg.Count];
106                                                         for (int i = 0; i < flags.Length; ++i) {
107                                                                 if (MetaType.GetTypeCode (carg[i].ArgumentType) == TypeCode.Boolean)
108                                                                         flags[i] = (bool) carg[i].Value;
109                                                         }
110
111                                                         break;
112                                                 }
113                                         }
114                                 }
115
116                                 provider = null;
117                         }
118                 }
119
120                 protected readonly Dictionary<MetaType, TypeSpec> import_cache;
121                 protected readonly Dictionary<MetaType, TypeSpec> compiled_types;
122                 protected readonly Dictionary<Assembly, IAssemblyDefinition> assembly_2_definition;
123                 readonly ModuleContainer module;
124
125                 public static readonly string CompilerServicesNamespace = "System.Runtime.CompilerServices";
126
127                 protected MetadataImporter (ModuleContainer module)
128                 {
129                         this.module = module;
130
131                         import_cache = new Dictionary<MetaType, TypeSpec> (1024, ReferenceEquality<MetaType>.Default);
132                         compiled_types = new Dictionary<MetaType, TypeSpec> (40, ReferenceEquality<MetaType>.Default);
133                         assembly_2_definition = new Dictionary<Assembly, IAssemblyDefinition> (ReferenceEquality<Assembly>.Default);
134                         IgnorePrivateMembers = true;
135                 }
136
137                 #region Properties
138
139                 public ICollection<IAssemblyDefinition> Assemblies {
140                         get {
141                                 return assembly_2_definition.Values;
142                         }
143                 }
144
145                 public bool IgnorePrivateMembers { get; set; }
146
147                 #endregion
148
149                 public abstract void AddCompiledType (TypeBuilder builder, TypeSpec spec);
150                 protected abstract MemberKind DetermineKindFromBaseType (MetaType baseType);
151                 protected abstract bool HasVolatileModifier (MetaType[] modifiers);
152                 public abstract void GetCustomAttributeTypeName (CustomAttributeData cad, out string typeNamespace, out string typeName);
153
154                 public FieldSpec CreateField (FieldInfo fi, TypeSpec declaringType)
155                 {
156                         Modifiers mod = 0;
157                         var fa = fi.Attributes;
158                         switch (fa & FieldAttributes.FieldAccessMask) {
159                                 case FieldAttributes.Public:
160                                         mod = Modifiers.PUBLIC;
161                                         break;
162                                 case FieldAttributes.Assembly:
163                                         mod = Modifiers.INTERNAL;
164                                         break;
165                                 case FieldAttributes.Family:
166                                         mod = Modifiers.PROTECTED;
167                                         break;
168                                 case FieldAttributes.FamORAssem:
169                                         mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
170                                         break;
171                                 default:
172                                         // Ignore private fields (even for error reporting) to not require extra dependencies
173                                         if (IgnorePrivateMembers || HasAttribute (CustomAttributeData.GetCustomAttributes (fi), "CompilerGeneratedAttribute", CompilerServicesNamespace))
174                                                 return null;
175
176                                         mod = Modifiers.PRIVATE;
177                                         break;
178                         }
179
180                         TypeSpec field_type;
181
182                         try {
183                                 field_type = ImportType (fi.FieldType, new DynamicTypeReader (fi));
184                         } catch (Exception e) {
185                                 // TODO: I should construct fake TypeSpec based on TypeRef signature
186                                 // but there is no way to do it with System.Reflection
187                                 throw new InternalErrorException (e, "Cannot import field `{0}.{1}' referenced in assembly `{2}'",
188                                         declaringType.GetSignatureForError (), fi.Name, declaringType.MemberDefinition.DeclaringAssembly);
189                         }
190
191                         var definition = new ImportedMemberDefinition (fi, field_type, this);
192
193                         if ((fa & FieldAttributes.Literal) != 0) {
194                                 var c = Constant.CreateConstantFromValue (field_type, fi.GetRawConstantValue (), Location.Null);
195                                 return new ConstSpec (declaringType, definition, field_type, fi, mod, c);
196                         }
197
198                         if ((fa & FieldAttributes.InitOnly) != 0) {
199                                 if (field_type == TypeManager.decimal_type) {
200                                         var dc = ReadDecimalConstant (CustomAttributeData.GetCustomAttributes (fi));
201                                         if (dc != null)
202                                                 return new ConstSpec (declaringType, definition, field_type, fi, mod, dc);
203                                 }
204
205                                 mod |= Modifiers.READONLY;
206                         } else {
207                                 var req_mod = fi.GetRequiredCustomModifiers ();
208                                 if (req_mod.Length > 0 && HasVolatileModifier (req_mod))
209                                         mod |= Modifiers.VOLATILE;
210                         }
211
212                         if ((fa & FieldAttributes.Static) != 0) {
213                                 mod |= Modifiers.STATIC;
214                         } else {
215                                 // Fixed buffers cannot be static
216                                 if (declaringType.IsStruct && field_type.IsStruct && field_type.IsNested &&
217                                         HasAttribute (CustomAttributeData.GetCustomAttributes (fi), "FixedBufferAttribute", CompilerServicesNamespace)) {
218
219                                         // TODO: Sanity check on field_type (only few type are allowed)
220                                         var element_field = CreateField (fi.FieldType.GetField (FixedField.FixedElementName), declaringType);
221                                         return new FixedFieldSpec (declaringType, definition, fi, element_field, mod);
222                                 }
223                         }
224
225                         return new FieldSpec (declaringType, definition, field_type, fi, mod);
226                 }
227
228                 public EventSpec CreateEvent (EventInfo ei, TypeSpec declaringType, MethodSpec add, MethodSpec remove)
229                 {
230                         add.IsAccessor = true;
231                         remove.IsAccessor = true;
232
233                         if (add.Modifiers != remove.Modifiers)
234                                 throw new NotImplementedException ("Different accessor modifiers " + ei.Name);
235
236                         var event_type = ImportType (ei.EventHandlerType, new DynamicTypeReader (ei));
237                         var definition = new ImportedMemberDefinition (ei, event_type,  this);
238                         return new EventSpec (declaringType, definition, event_type, add.Modifiers, add, remove);
239                 }
240
241                 TypeParameterSpec[] CreateGenericParameters (MetaType type, TypeSpec declaringType)
242                 {
243                         var tparams = type.GetGenericArguments ();
244
245                         int parent_owned_count;
246                         if (type.IsNested) {
247                                 parent_owned_count = type.DeclaringType.GetGenericArguments ().Length;
248
249                                 //
250                                 // System.Reflection duplicates parent type parameters for each
251                                 // nested type with slightly modified properties (eg. different owner)
252                                 // This just makes things more complicated (think of cloned constraints)
253                                 // therefore we remap any nested type owned by parent using `type_cache'
254                                 // to the single TypeParameterSpec
255                                 //
256                                 if (declaringType != null && parent_owned_count > 0) {
257                                         int read_count = 0;
258                                         while (read_count != parent_owned_count) {
259                                                 var tparams_count = declaringType.Arity;
260                                                 if (tparams_count != 0) {
261                                                         var parent_tp = declaringType.MemberDefinition.TypeParameters;
262                                                         read_count += tparams_count;
263                                                         for (int i = 0; i < tparams_count; i++) {
264                                                                 import_cache.Add (tparams[parent_owned_count - read_count + i], parent_tp[i]);
265                                                         }
266                                                 }
267
268                                                 declaringType = declaringType.DeclaringType;
269                                         }
270                                 }                       
271                         } else {
272                                 parent_owned_count = 0;
273                         }
274
275                         if (tparams.Length - parent_owned_count == 0)
276                                 return null;
277
278                         return CreateGenericParameters (parent_owned_count, tparams);
279                 }
280
281                 TypeParameterSpec[] CreateGenericParameters (int first, MetaType[] tparams)
282                 {
283                         var tspec = new TypeParameterSpec[tparams.Length - first];
284                         for (int pos = first; pos < tparams.Length; ++pos) {
285                                 var type = tparams[pos];
286                                 int index = pos - first;
287
288                                 tspec[index] = (TypeParameterSpec) CreateType (type, new DynamicTypeReader (), false);
289                         }
290
291                         return tspec;
292                 }
293
294                 TypeSpec[] CreateGenericArguments (int first, MetaType[] tparams, DynamicTypeReader dtype)
295                 {
296                         ++dtype.Position;
297
298                         var tspec = new TypeSpec [tparams.Length - first];
299                         for (int pos = first; pos < tparams.Length; ++pos) {
300                                 var type = tparams[pos];
301                                 int index = pos - first;
302
303                                 TypeSpec spec;
304                                 if (type.HasElementType) {
305                                         var element = type.GetElementType ();
306                                         ++dtype.Position;
307                                         spec = ImportType (element, dtype);
308
309                                         if (!type.IsArray) {
310                                                 throw new NotImplementedException ("Unknown element type " + type.ToString ());
311                                         }
312
313                                         spec = ArrayContainer.MakeType (module, spec, type.GetArrayRank ());
314                                 } else {
315                                         spec = CreateType (type, dtype, true);
316
317                                         //
318                                         // We treat nested generic types as inflated internally where
319                                         // reflection uses type definition
320                                         //
321                                         // class A<T> {
322                                         //    IFoo<A<T>> foo;   // A<T> is definition in this case
323                                         // }
324                                         //
325                                         // TODO: Is full logic from CreateType needed here as well?
326                                         //
327                                         if (!IsMissingType (type) && type.IsGenericTypeDefinition) {
328                                                 var targs = CreateGenericArguments (0, type.GetGenericArguments (), dtype);
329                                                 spec = spec.MakeGenericType (module, targs);
330                                         }
331                                 }
332
333                                 ++dtype.Position;
334                                 tspec[index] = spec;
335                         }
336
337                         return tspec;
338                 }
339
340                 public MethodSpec CreateMethod (MethodBase mb, TypeSpec declaringType)
341                 {
342                         Modifiers mod = ReadMethodModifiers (mb, declaringType);
343                         TypeParameterSpec[] tparams;
344
345                         var parameters = CreateParameters (declaringType, mb.GetParameters (), mb);
346
347                         if (mb.IsGenericMethod) {
348                                 if (!mb.IsGenericMethodDefinition)
349                                         throw new NotSupportedException ("assert");
350
351                                 tparams = CreateGenericParameters (0, mb.GetGenericArguments ());
352                         } else {
353                                 tparams = null;
354                         }
355
356                         MemberKind kind;
357                         TypeSpec returnType;
358                         if (mb.MemberType == MemberTypes.Constructor) {
359                                 kind = MemberKind.Constructor;
360                                 returnType = TypeManager.void_type;
361                         } else {
362                                 //
363                                 // Detect operators and destructors
364                                 //
365                                 string name = mb.Name;
366                                 kind = MemberKind.Method;
367                                 if (tparams == null && !mb.DeclaringType.IsInterface && name.Length > 6) {
368                                         if ((mod & (Modifiers.STATIC | Modifiers.PUBLIC)) == (Modifiers.STATIC | Modifiers.PUBLIC)) {
369                                                 if (name[2] == '_' && name[1] == 'p' && name[0] == 'o') {
370                                                         var op_type = Operator.GetType (name);
371                                                         if (op_type.HasValue && parameters.Count > 0 && parameters.Count < 3) {
372                                                                 kind = MemberKind.Operator;
373                                                         }
374                                                 }
375                                         } else if (parameters.IsEmpty && name == Destructor.MetadataName) {
376                                                 kind = MemberKind.Destructor;
377                                                 if (declaringType == TypeManager.object_type) {
378                                                         mod &= ~Modifiers.OVERRIDE;
379                                                         mod |= Modifiers.VIRTUAL;
380                                                 }
381                                         }
382                                 }
383
384                                 var mi = (MethodInfo) mb;
385                                 returnType = ImportType (mi.ReturnType, new DynamicTypeReader (mi.ReturnParameter));
386
387                                 // Cannot set to OVERRIDE without full hierarchy checks
388                                 // this flag indicates that the method could be override
389                                 // but further validation is needed
390                                 if ((mod & Modifiers.OVERRIDE) != 0 && kind == MemberKind.Method && declaringType.BaseType != null) {
391                                         var filter = MemberFilter.Method (name, tparams != null ? tparams.Length : 0, parameters, null);
392                                         var candidate = MemberCache.FindMember (declaringType.BaseType, filter, BindingRestriction.None);
393
394                                         //
395                                         // For imported class method do additional validation to be sure that metadata
396                                         // override flag was correct
397                                         // 
398                                         // Difference between protected internal and protected is ok
399                                         //
400                                         const Modifiers conflict_mask = Modifiers.AccessibilityMask & ~Modifiers.INTERNAL;
401                                         if (candidate == null || (candidate.Modifiers & conflict_mask) != (mod & conflict_mask) || candidate.IsStatic) {
402                                                 mod &= ~Modifiers.OVERRIDE;
403                                                 mod |= Modifiers.VIRTUAL;
404                                         }
405                                 }
406                         }
407
408                         IMemberDefinition definition;
409                         if (tparams != null) {
410                                 var gmd = new ImportedGenericMethodDefinition ((MethodInfo) mb, returnType, parameters, tparams, this);
411                                 foreach (var tp in gmd.TypeParameters) {
412                                         ImportTypeParameterTypeConstraints (tp, tp.GetMetaInfo ());
413                                 }
414
415                                 definition = gmd;
416                         } else {
417                                 definition = new ImportedParameterMemberDefinition (mb, returnType, parameters, this);
418                         }
419
420                         MethodSpec ms = new MethodSpec (kind, declaringType, definition, returnType, mb, parameters, mod);
421                         if (tparams != null)
422                                 ms.IsGeneric = true;
423
424                         return ms;
425                 }
426
427                 //
428                 // Imports System.Reflection parameters
429                 //
430                 AParametersCollection CreateParameters (TypeSpec parent, ParameterInfo[] pi, MethodBase method)
431                 {
432                         int varargs = method != null && (method.CallingConvention & CallingConventions.VarArgs) != 0 ? 1 : 0;
433
434                         if (pi.Length == 0 && varargs == 0)
435                                 return ParametersCompiled.EmptyReadOnlyParameters;
436
437                         TypeSpec[] types = new TypeSpec[pi.Length + varargs];
438                         IParameterData[] par = new IParameterData[pi.Length + varargs];
439                         bool is_params = false;
440                         for (int i = 0; i < pi.Length; i++) {
441                                 ParameterInfo p = pi[i];
442                                 Parameter.Modifier mod = 0;
443                                 Expression default_value = null;
444                                 if (p.ParameterType.IsByRef) {
445                                         if ((p.Attributes & (ParameterAttributes.Out | ParameterAttributes.In)) == ParameterAttributes.Out)
446                                                 mod = Parameter.Modifier.OUT;
447                                         else
448                                                 mod = Parameter.Modifier.REF;
449
450                                         //
451                                         // Strip reference wrapping
452                                         //
453                                         var el = p.ParameterType.GetElementType ();
454                                         types[i] = ImportType (el, new DynamicTypeReader (p));  // TODO: 1-based positio to be csc compatible
455                                 } else if (i == 0 && method.IsStatic && parent.IsStatic && parent.MemberDefinition.DeclaringAssembly.HasExtensionMethod &&
456                                         HasAttribute (CustomAttributeData.GetCustomAttributes (method), "ExtensionAttribute", CompilerServicesNamespace)) {
457                                         mod = Parameter.Modifier.This;
458                                         types[i] = ImportType (p.ParameterType);
459                                 } else {
460                                         types[i] = ImportType (p.ParameterType, new DynamicTypeReader (p));
461
462                                         if (i >= pi.Length - 2 && types[i] is ArrayContainer) {
463                                                 if (HasAttribute (CustomAttributeData.GetCustomAttributes (p), "ParamArrayAttribute", "System")) {
464                                                         mod = Parameter.Modifier.PARAMS;
465                                                         is_params = true;
466                                                 }
467                                         }
468
469                                         if (!is_params && p.IsOptional) {
470                                                 object value = p.RawDefaultValue;
471                                                 var ptype = types[i];
472                                                 if ((p.Attributes & ParameterAttributes.HasDefault) != 0 && ptype.Kind != MemberKind.TypeParameter && (value != null || TypeManager.IsReferenceType (ptype))) {
473                                                         if (value == null) {
474                                                                 default_value = Constant.CreateConstant (null, ptype, null, Location.Null);
475                                                         } else {
476                                                                 default_value = ImportParameterConstant (value).Resolve (null);
477
478                                                                 if (ptype.IsEnum) {
479                                                                         default_value = new EnumConstant ((Constant) default_value, ptype).Resolve (null);
480                                                                 }
481                                                         }
482                                                 } else if (value == Missing.Value) {
483                                                         default_value = EmptyExpression.MissingValue;
484                                                 } else if (value == null) {
485                                                         default_value = new DefaultValueExpression (new TypeExpression (ptype, Location.Null), Location.Null);
486                                                 } else if (ptype == TypeManager.decimal_type) {
487                                                         default_value = ImportParameterConstant (value).Resolve (null);
488                                                 }
489                                         }
490                                 }
491
492                                 par[i] = new ParameterData (p.Name, mod, default_value);
493                         }
494
495                         if (varargs != 0) {
496                                 par[par.Length - 1] = new ArglistParameter (Location.Null);
497                                 types[types.Length - 1] = InternalType.Arglist;
498                         }
499
500                         return method != null ?
501                                 new ParametersImported (par, types, varargs != 0, is_params) :
502                                 new ParametersImported (par, types, is_params);
503                 }
504
505                 //
506                 // Returns null when the property is not valid C# property
507                 //
508                 public PropertySpec CreateProperty (PropertyInfo pi, TypeSpec declaringType, MethodSpec get, MethodSpec set)
509                 {
510                         Modifiers mod = 0;
511                         AParametersCollection param = null;
512                         TypeSpec type = null;
513                         if (get != null) {
514                                 mod = get.Modifiers;
515                                 param = get.Parameters;
516                                 type = get.ReturnType;
517                         }
518
519                         bool is_valid_property = true;
520                         if (set != null) {
521                                 if (set.ReturnType != TypeManager.void_type)
522                                         is_valid_property = false;
523
524                                 var set_param_count = set.Parameters.Count - 1;
525
526                                 if (set_param_count < 0) {
527                                         set_param_count = 0;
528                                         is_valid_property = false;
529                                 }
530
531                                 var set_type = set.Parameters.Types[set_param_count];
532
533                                 if (mod == 0) {
534                                         AParametersCollection set_based_param;
535
536                                         if (set_param_count == 0) {
537                                                 set_based_param = ParametersCompiled.EmptyReadOnlyParameters;
538                                         } else {
539                                                 //
540                                                 // Create indexer parameters based on setter method parameters (the last parameter has to be removed)
541                                                 //
542                                                 var data = new IParameterData[set_param_count];
543                                                 var types = new TypeSpec[set_param_count];
544                                                 Array.Copy (set.Parameters.FixedParameters, data, set_param_count);
545                                                 Array.Copy (set.Parameters.Types, types, set_param_count);
546                                                 set_based_param = new ParametersImported (data, types, set.Parameters.HasParams);
547                                         }
548
549                                         mod = set.Modifiers;
550                                         param = set_based_param;
551                                         type = set_type;
552                                 } else {
553                                         if (set_param_count != get.Parameters.Count)
554                                                 is_valid_property = false;
555
556                                         if (get.ReturnType != set_type)
557                                                 is_valid_property = false;
558
559                                         // Possible custom accessor modifiers
560                                         if ((mod & Modifiers.AccessibilityMask) != (set.Modifiers & Modifiers.AccessibilityMask)) {
561                                                 var get_acc = mod & Modifiers.AccessibilityMask;
562                                                 if (get_acc != Modifiers.PUBLIC) {
563                                                         var set_acc = set.Modifiers & Modifiers.AccessibilityMask;
564                                                         // If the accessor modifiers are not same, do extra restriction checks
565                                                         if (get_acc != set_acc) {
566                                                                 var get_restr = ModifiersExtensions.IsRestrictedModifier (get_acc, set_acc);
567                                                                 var set_restr = ModifiersExtensions.IsRestrictedModifier (set_acc, get_acc);
568                                                                 if (get_restr && set_restr) {
569                                                                         is_valid_property = false; // Neither is more restrictive
570                                                                 }
571
572                                                                 if (get_restr) {
573                                                                         mod &= ~Modifiers.AccessibilityMask;
574                                                                         mod |= set_acc;
575                                                                 }
576                                                         }
577                                                 }
578                                         }
579                                 }
580                         }
581
582                         PropertySpec spec = null;
583                         if (!param.IsEmpty) {
584                                 var index_name = declaringType.MemberDefinition.GetAttributeDefaultMember ();
585                                 if (index_name == null) {
586                                         is_valid_property = false;
587                                 } else {
588                                         if (get != null) {
589                                                 if (get.IsStatic)
590                                                         is_valid_property = false;
591                                                 if (get.Name.IndexOf (index_name, StringComparison.Ordinal) != 4)
592                                                         is_valid_property = false;
593                                         }
594                                         if (set != null) {
595                                                 if (set.IsStatic)
596                                                         is_valid_property = false;
597                                                 if (set.Name.IndexOf (index_name, StringComparison.Ordinal) != 4)
598                                                         is_valid_property = false;
599                                         }
600                                 }
601
602                                 if (is_valid_property)
603                                         spec = new IndexerSpec (declaringType, new ImportedParameterMemberDefinition (pi, type, param, this), type, param, pi, mod);
604                         }
605
606                         if (spec == null)
607                                 spec = new PropertySpec (MemberKind.Property, declaringType, new ImportedMemberDefinition (pi, type, this), type, pi, mod);
608
609                         if (!is_valid_property) {
610                                 spec.IsNotRealProperty = true;
611                                 return spec;
612                         }
613
614                         if (set != null)
615                                 spec.Set = set;
616                         if (get != null)
617                                 spec.Get = get;
618
619                         return spec;
620                 }
621
622                 public TypeSpec CreateType (MetaType type)
623                 {
624                         return CreateType (type, new DynamicTypeReader (), true);
625                 }
626
627                 public TypeSpec CreateNestedType (MetaType type, TypeSpec declaringType)
628                 {
629                         return CreateType (type, declaringType, new DynamicTypeReader (type), false);
630                 }
631
632                 TypeSpec CreateType (MetaType type, DynamicTypeReader dtype, bool canImportBaseType)
633                 {
634                         TypeSpec declaring_type;
635                         if (type.IsNested && !type.IsGenericParameter)
636                                 declaring_type = CreateType (type.DeclaringType, new DynamicTypeReader (type.DeclaringType), true);
637                         else
638                                 declaring_type = null;
639
640                         return CreateType (type, declaring_type, dtype, canImportBaseType);
641                 }
642
643                 TypeSpec CreateType (MetaType type, TypeSpec declaringType, DynamicTypeReader dtype, bool canImportBaseType)
644                 {
645                         TypeSpec spec;
646                         if (import_cache.TryGetValue (type, out spec)) {
647                                 if (spec == TypeManager.object_type) {
648                                         if (dtype.IsDynamicObject (this))
649                                                 return InternalType.Dynamic;
650
651                                         return spec;
652                                 }
653
654                                 if (!spec.IsGeneric || type.IsGenericTypeDefinition)
655                                         return spec;
656
657                                 if (!dtype.HasDynamicAttribute (this))
658                                         return spec;
659
660                                 // We've found same object in the cache but this one has a dynamic custom attribute
661                                 // and it's most likely dynamic version of same type IFoo<object> agains IFoo<dynamic>
662                                 // Do type resolve process again in that case
663
664                                 // TODO: Handle cases where they still unify
665                         }
666
667                         if (IsMissingType (type)) {
668                                 spec = new TypeSpec (MemberKind.MissingType, declaringType, new ImportedTypeDefinition (type, this), type, Modifiers.PUBLIC);
669                                 spec.MemberCache = MemberCache.Empty;
670                                 import_cache.Add (type, spec);
671                                 return spec;
672                         }
673
674                         if (type.IsGenericType && !type.IsGenericTypeDefinition) {
675                                 var type_def = type.GetGenericTypeDefinition ();
676                                 var targs = CreateGenericArguments (0, type.GetGenericArguments (), dtype);
677                                 if (declaringType == null) {
678                                         // Simple case, no nesting
679                                         spec = CreateType (type_def, null, new DynamicTypeReader (), canImportBaseType);
680                                         spec = spec.MakeGenericType (module, targs);
681                                 } else {
682                                         //
683                                         // Nested type case, converting .NET types like
684                                         // A`1.B`1.C`1<int, long, string> to typespec like
685                                         // A<int>.B<long>.C<string>
686                                         //
687                                         var nested_hierarchy = new List<TypeSpec> ();
688                                         while (declaringType.IsNested) {
689                                                 nested_hierarchy.Add (declaringType);
690                                                 declaringType = declaringType.DeclaringType;
691                                         }
692
693                                         int targs_pos = 0;
694                                         if (declaringType.Arity > 0) {
695                                                 spec = declaringType.MakeGenericType (module, targs.Skip (targs_pos).Take (declaringType.Arity).ToArray ());
696                                                 targs_pos = spec.Arity;
697                                         } else {
698                                                 spec = declaringType;
699                                         }
700
701                                         for (int i = nested_hierarchy.Count; i != 0; --i) {
702                                                 var t = nested_hierarchy [i - 1];
703                                                 spec = MemberCache.FindNestedType (spec, t.Name, t.Arity);
704                                                 if (t.Arity > 0) {
705                                                         spec = spec.MakeGenericType (module, targs.Skip (targs_pos).Take (spec.Arity).ToArray ());
706                                                         targs_pos += t.Arity;
707                                                 }
708                                         }
709
710                                         string name = type.Name;
711                                         int index = name.IndexOf ('`');
712                                         if (index > 0)
713                                                 name = name.Substring (0, index);
714
715                                         spec = MemberCache.FindNestedType (spec, name, targs.Length - targs_pos);
716                                         if (spec.Arity > 0) {
717                                                 spec = spec.MakeGenericType (module, targs.Skip (targs_pos).ToArray ());
718                                         }
719                                 }
720
721                                 // Don't add generic type with dynamic arguments, they can interfere with same type
722                                 // using object type arguments
723                                 if (!spec.HasDynamicElement) {
724
725                                         // Add to reading cache to speed up reading
726                                         if (!import_cache.ContainsKey (type))
727                                                 import_cache.Add (type, spec);
728                                 }
729
730                                 return spec;
731                         }
732
733                         Modifiers mod;
734                         MemberKind kind;
735
736                         var ma = type.Attributes;
737                         switch (ma & TypeAttributes.VisibilityMask) {
738                         case TypeAttributes.Public:
739                         case TypeAttributes.NestedPublic:
740                                 mod = Modifiers.PUBLIC;
741                                 break;
742                         case TypeAttributes.NestedPrivate:
743                                 mod = Modifiers.PRIVATE;
744                                 break;
745                         case TypeAttributes.NestedFamily:
746                                 mod = Modifiers.PROTECTED;
747                                 break;
748                         case TypeAttributes.NestedFamORAssem:
749                                 mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
750                                 break;
751                         default:
752                                 mod = Modifiers.INTERNAL;
753                                 break;
754                         }
755
756                         if ((ma & TypeAttributes.Interface) != 0) {
757                                 kind = MemberKind.Interface;
758                         } else if (type.IsGenericParameter) {
759                                 kind = MemberKind.TypeParameter;
760                         } else {
761                                 var base_type = type.BaseType;
762                                 if (base_type == null || (ma & TypeAttributes.Abstract) != 0) {
763                                         kind = MemberKind.Class;
764                                 } else {
765                                         kind = DetermineKindFromBaseType (base_type);
766                                         if (kind == MemberKind.Struct || kind == MemberKind.Delegate) {
767                                                 mod |= Modifiers.SEALED;
768                                         }
769                                 }
770
771                                 if (kind == MemberKind.Class) {
772                                         if ((ma & TypeAttributes.Sealed) != 0) {
773                                                 mod |= Modifiers.SEALED;
774                                                 if ((ma & TypeAttributes.Abstract) != 0)
775                                                         mod |= Modifiers.STATIC;
776                                         } else if ((ma & TypeAttributes.Abstract) != 0) {
777                                                 mod |= Modifiers.ABSTRACT;
778                                         }
779                                 }
780                         }
781
782                         var definition = new ImportedTypeDefinition (type, this);
783                         TypeSpec pt;
784
785                         if (kind == MemberKind.Enum) {
786                                 const BindingFlags underlying_member = BindingFlags.DeclaredOnly |
787                                         BindingFlags.Instance |
788                                         BindingFlags.Public | BindingFlags.NonPublic;
789
790                                 var type_members = type.GetFields (underlying_member);
791                                 foreach (var type_member in type_members) {
792                                         spec = new EnumSpec (declaringType, definition, CreateType (type_member.FieldType), type, mod);
793                                         break;
794                                 }
795
796                                 if (spec == null)
797                                         kind = MemberKind.Class;
798
799                         } else if (kind == MemberKind.TypeParameter) {
800                                 spec = CreateTypeParameter (type, declaringType);
801                         } else if (type.IsGenericTypeDefinition) {
802                                 definition.TypeParameters = CreateGenericParameters (type, declaringType);
803                         } else if (compiled_types.TryGetValue (type, out pt)) {
804                                 //
805                                 // Same type was found in inside compiled types. It's
806                                 // either build-in type or forward referenced typed
807                                 // which point into just compiled assembly.
808                                 //
809                                 spec = pt;
810                                 BuildinTypeSpec bts = pt as BuildinTypeSpec;
811                                 if (bts != null)
812                                         bts.SetDefinition (definition, type, mod);
813                         }
814
815                         if (spec == null)
816                                 spec = new TypeSpec (kind, declaringType, definition, type, mod);
817
818                         import_cache.Add (type, spec);
819
820                         //
821                         // Two stage setup as the base type can be inflated declaring type or
822                         // another nested type inside same declaring type which has not been
823                         // loaded, therefore we can import a base type of nested types once
824                         // the types have been imported
825                         //
826                         if (canImportBaseType)
827                                 ImportTypeBase (spec, type);
828
829                         return spec;
830                 }
831
832                 public IAssemblyDefinition GetAssemblyDefinition (Assembly assembly)
833                 {
834                         IAssemblyDefinition found;
835                         if (!assembly_2_definition.TryGetValue (assembly, out found)) {
836
837                                 // This can happen in dynamic context only
838                                 var def = new ImportedAssemblyDefinition (assembly, this);
839                                 assembly_2_definition.Add (assembly, def);
840                                 def.ReadAttributes ();
841                                 found = def;
842                         }
843
844                         return found;
845                 }
846
847                 public void ImportTypeBase (MetaType type)
848                 {
849                         TypeSpec spec = import_cache[type];
850                         if (spec != null)
851                                 ImportTypeBase (spec, type);
852                 }
853
854                 TypeParameterSpec CreateTypeParameter (MetaType type, TypeSpec declaringType)
855                 {
856                         Variance variance;
857                         switch (type.GenericParameterAttributes & GenericParameterAttributes.VarianceMask) {
858                         case GenericParameterAttributes.Covariant:
859                                 variance = Variance.Covariant;
860                                 break;
861                         case GenericParameterAttributes.Contravariant:
862                                 variance = Variance.Contravariant;
863                                 break;
864                         default:
865                                 variance = Variance.None;
866                                 break;
867                         }
868
869                         SpecialConstraint special = SpecialConstraint.None;
870                         var import_special = type.GenericParameterAttributes & GenericParameterAttributes.SpecialConstraintMask;
871
872                         if ((import_special & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0) {
873                                 special |= SpecialConstraint.Struct;
874                         } else if ((import_special & GenericParameterAttributes.DefaultConstructorConstraint) != 0) {
875                                 special = SpecialConstraint.Constructor;
876                         }
877
878                         if ((import_special & GenericParameterAttributes.ReferenceTypeConstraint) != 0) {
879                                 special |= SpecialConstraint.Class;
880                         }
881
882                         TypeParameterSpec spec;
883                         var def = new ImportedTypeParameterDefinition (type, this);
884                         if (type.DeclaringMethod != null) {
885                                 spec = new TypeParameterSpec (type.GenericParameterPosition, def, special, variance, type);
886                         } else {
887                                 spec = new TypeParameterSpec (declaringType, type.GenericParameterPosition, def, special, variance, type);
888                         }
889
890                         return spec;
891                 }
892
893                 //
894                 // Test for a custom attribute type match. Custom attributes are not really predefined globaly 
895                 // they can be assembly specific therefore we do check based on names only
896                 //
897                 public bool HasAttribute (IList<CustomAttributeData> attributesData, string attrName, string attrNamespace)
898                 {
899                         if (attributesData.Count == 0)
900                                 return false;
901
902                         string ns, name;
903                         foreach (var attr in attributesData) {
904                                 GetCustomAttributeTypeName (attr, out ns, out name);
905                                 if (name == attrName && ns == attrNamespace)
906                                         return true;
907                         }
908
909                         return false;
910                 }
911
912                 void ImportTypeBase (TypeSpec spec, MetaType type)
913                 {
914                         if (spec.Kind == MemberKind.Interface)
915                                 spec.BaseType = TypeManager.object_type;
916                         else if (type.BaseType != null) {
917                                 TypeSpec base_type;
918                                 if (!IsMissingType (type.BaseType) && type.BaseType.IsGenericType)
919                                         base_type = CreateType (type.BaseType, new DynamicTypeReader (type), true);
920                                 else
921                                         base_type = CreateType (type.BaseType);
922
923                                 spec.BaseType = base_type;
924                         }
925
926                         MetaType[] ifaces;
927 #if STATIC
928                         ifaces = type.__GetDeclaredInterfaces ();
929                         if (ifaces.Length != 0) {
930                                 foreach (var iface in ifaces)
931                                         spec.AddInterface (CreateType (iface));
932                         }
933
934                         if (spec.BaseType != null) {
935                                 var bifaces = spec.BaseType.Interfaces;
936                                 if (bifaces != null) {
937                                         foreach (var iface in bifaces)
938                                                 spec.AddInterface (iface);
939                                 }
940                         }
941 #else
942                         ifaces = type.GetInterfaces ();
943
944                         if (ifaces.Length > 0) {
945                                 foreach (var iface in ifaces) {
946                                         spec.AddInterface (CreateType (iface));
947                                 }
948                         }
949 #endif
950
951                         if (spec.MemberDefinition.TypeParametersCount > 0) {
952                                 foreach (var tp in spec.MemberDefinition.TypeParameters) {
953                                         ImportTypeParameterTypeConstraints (tp, tp.GetMetaInfo ());
954                                 }
955                         }
956
957                 }
958
959                 protected void ImportTypes (MetaType[] types, Namespace targetNamespace, bool hasExtensionTypes)
960                 {
961                         Namespace ns = targetNamespace;
962                         string prev_namespace = null;
963                         foreach (var t in types) {
964                                 if (t == null)
965                                         continue;
966
967                                 // Be careful not to trigger full parent type loading
968                                 if (t.MemberType == MemberTypes.NestedType)
969                                         continue;
970
971                                 if (t.Name[0] == '<')
972                                         continue;
973
974                                 var it = CreateType (t, null, new DynamicTypeReader (t), true);
975                                 if (it == null)
976                                         continue;
977
978                                 if (prev_namespace != t.Namespace) {
979                                         ns = t.Namespace == null ? targetNamespace : targetNamespace.GetNamespace (t.Namespace, true);
980                                         prev_namespace = t.Namespace;
981                                 }
982
983                                 ns.AddType (it);
984
985                                 if (it.IsStatic && hasExtensionTypes &&
986                                         HasAttribute (CustomAttributeData.GetCustomAttributes (t), "ExtensionAttribute", CompilerServicesNamespace)) {
987                                         it.SetExtensionMethodContainer ();
988                                 }
989                         }
990                 }
991
992                 void ImportTypeParameterTypeConstraints (TypeParameterSpec spec, MetaType type)
993                 {
994                         var constraints = type.GetGenericParameterConstraints ();
995                         List<TypeSpec> tparams = null;
996                         foreach (var ct in constraints) {
997                                 if (ct.IsGenericParameter) {
998                                         if (tparams == null)
999                                                 tparams = new List<TypeSpec> ();
1000
1001                                         tparams.Add (CreateType (ct));
1002                                         continue;
1003                                 }
1004
1005                                 if (!IsMissingType (ct) && ct.IsClass) {
1006                                         spec.BaseType = CreateType (ct);
1007                                         continue;
1008                                 }
1009
1010                                 spec.AddInterface (CreateType (ct));
1011                         }
1012
1013                         if (spec.BaseType == null)
1014                                 spec.BaseType = TypeManager.object_type;
1015
1016                         if (tparams != null)
1017                                 spec.TypeArguments = tparams.ToArray ();
1018                 }
1019
1020                 static Constant ImportParameterConstant (object value)
1021                 {
1022                         //
1023                         // Get type of underlying value as int constant can be used for object
1024                         // parameter type. This is not allowed in C# but other languages can do that
1025                         //
1026                         switch (System.Type.GetTypeCode (value.GetType ())) {
1027                         case TypeCode.Boolean:
1028                                 return new BoolConstant ((bool) value, Location.Null);
1029                         case TypeCode.Byte:
1030                                 return new ByteConstant ((byte) value, Location.Null);
1031                         case TypeCode.Char:
1032                                 return new CharConstant ((char) value, Location.Null);
1033                         case TypeCode.Decimal:
1034                                 return new DecimalConstant ((decimal) value, Location.Null);
1035                         case TypeCode.Double:
1036                                 return new DoubleConstant ((double) value, Location.Null);
1037                         case TypeCode.Int16:
1038                                 return new ShortConstant ((short) value, Location.Null);
1039                         case TypeCode.Int32:
1040                                 return new IntConstant ((int) value, Location.Null);
1041                         case TypeCode.Int64:
1042                                 return new LongConstant ((long) value, Location.Null);
1043                         case TypeCode.SByte:
1044                                 return new SByteConstant ((sbyte) value, Location.Null);
1045                         case TypeCode.Single:
1046                                 return new FloatConstant ((float) value, Location.Null);
1047                         case TypeCode.String:
1048                                 return new StringConstant ((string) value, Location.Null);
1049                         case TypeCode.UInt16:
1050                                 return new UShortConstant ((ushort) value, Location.Null);
1051                         case TypeCode.UInt32:
1052                                 return new UIntConstant ((uint) value, Location.Null);
1053                         case TypeCode.UInt64:
1054                                 return new ULongConstant ((ulong) value, Location.Null);
1055                         }
1056
1057                         throw new NotImplementedException (value.GetType ().ToString ());
1058                 }
1059
1060                 public TypeSpec ImportType (MetaType type)
1061                 {
1062                         return ImportType (type, new DynamicTypeReader (type));
1063                 }
1064
1065                 TypeSpec ImportType (MetaType type, DynamicTypeReader dtype)
1066                 {
1067                         if (type.HasElementType) {
1068                                 var element = type.GetElementType ();
1069                                 ++dtype.Position;
1070                                 var spec = ImportType (element, dtype);
1071
1072                                 if (type.IsArray)
1073                                         return ArrayContainer.MakeType (module, spec, type.GetArrayRank ());
1074                                 if (type.IsByRef)
1075                                         return ReferenceContainer.MakeType (spec);
1076                                 if (type.IsPointer)
1077                                         return PointerContainer.MakeType (spec);
1078
1079                                 throw new NotImplementedException ("Unknown element type " + type.ToString ());
1080                         }
1081
1082                         return CreateType (type, dtype, true);
1083                 }
1084
1085                 static bool IsMissingType (MetaType type)
1086                 {
1087 #if STATIC
1088                         return type.__IsMissing;
1089 #else
1090                         return false;
1091 #endif
1092                 }
1093
1094                 //
1095                 // Decimal constants cannot be encoded in the constant blob, and thus are marked
1096                 // as IsInitOnly ('readonly' in C# parlance).  We get its value from the 
1097                 // DecimalConstantAttribute metadata.
1098                 //
1099                 Constant ReadDecimalConstant (IList<CustomAttributeData> attrs)
1100                 {
1101                         if (attrs.Count == 0)
1102                                 return null;
1103
1104                         string name, ns;
1105                         foreach (var ca in attrs) {
1106                                 GetCustomAttributeTypeName (ca, out ns, out name);
1107                                 if (name != "DecimalConstantAttribute" || ns != CompilerServicesNamespace)
1108                                         continue;
1109
1110                                 var value = new decimal (
1111                                         (int) (uint) ca.ConstructorArguments[4].Value,
1112                                         (int) (uint) ca.ConstructorArguments[3].Value,
1113                                         (int) (uint) ca.ConstructorArguments[2].Value,
1114                                         (byte) ca.ConstructorArguments[1].Value != 0,
1115                                         (byte) ca.ConstructorArguments[0].Value);
1116
1117                                 return new DecimalConstant (value, Location.Null).Resolve (null);
1118                         }
1119
1120                         return null;
1121                 }
1122
1123                 static Modifiers ReadMethodModifiers (MethodBase mb, TypeSpec declaringType)
1124                 {
1125                         Modifiers mod;
1126                         var ma = mb.Attributes;
1127                         switch (ma & MethodAttributes.MemberAccessMask) {
1128                         case MethodAttributes.Public:
1129                                 mod = Modifiers.PUBLIC;
1130                                 break;
1131                         case MethodAttributes.Assembly:
1132                                 mod = Modifiers.INTERNAL;
1133                                 break;
1134                         case MethodAttributes.Family:
1135                                 mod = Modifiers.PROTECTED;
1136                                 break;
1137                         case MethodAttributes.FamORAssem:
1138                                 mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
1139                                 break;
1140                         default:
1141                                 mod = Modifiers.PRIVATE;
1142                                 break;
1143                         }
1144
1145                         if ((ma & MethodAttributes.Static) != 0) {
1146                                 mod |= Modifiers.STATIC;
1147                                 return mod;
1148                         }
1149                         if ((ma & MethodAttributes.Abstract) != 0 && declaringType.IsClass) {
1150                                 mod |= Modifiers.ABSTRACT;
1151                                 return mod;
1152                         }
1153
1154                         if ((ma & MethodAttributes.Final) != 0)
1155                                 mod |= Modifiers.SEALED;
1156
1157                         // It can be sealed and override
1158                         if ((ma & MethodAttributes.Virtual) != 0) {
1159                                 if ((ma & MethodAttributes.NewSlot) != 0 || !declaringType.IsClass) {
1160                                         // No private virtual or sealed virtual
1161                                         if ((mod & (Modifiers.PRIVATE | Modifiers.SEALED)) == 0)
1162                                                 mod |= Modifiers.VIRTUAL;
1163                                 } else {
1164                                         mod |= Modifiers.OVERRIDE;
1165                                 }
1166                         }
1167
1168                         return mod;
1169                 }
1170         }
1171
1172         abstract class ImportedDefinition : IMemberDefinition
1173         {
1174                 protected class AttributesBag
1175                 {
1176                         public static readonly AttributesBag Default = new AttributesBag ();
1177
1178                         public AttributeUsageAttribute AttributeUsage;
1179                         public ObsoleteAttribute Obsolete;
1180                         public string[] Conditionals;
1181                         public string DefaultIndexerName;
1182                         public bool? CLSAttributeValue;
1183                         public TypeSpec CoClass;
1184                         
1185                         public static AttributesBag Read (MemberInfo mi, MetadataImporter importer)
1186                         {
1187                                 AttributesBag bag = null;
1188                                 List<string> conditionals = null;
1189
1190                                 // It should not throw any loading exception
1191                                 IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes (mi);
1192
1193                                 string ns, name;
1194                                 foreach (var a in attrs) {
1195                                         importer.GetCustomAttributeTypeName (a, out ns, out name);
1196                                         if (name == "ObsoleteAttribute") {
1197                                                 if (ns != "System")
1198                                                         continue;
1199
1200                                                 if (bag == null)
1201                                                         bag = new AttributesBag ();
1202
1203                                                 var args = a.ConstructorArguments;
1204
1205                                                 if (args.Count == 1) {
1206                                                         bag.Obsolete = new ObsoleteAttribute ((string) args[0].Value);
1207                                                 } else if (args.Count == 2) {
1208                                                         bag.Obsolete = new ObsoleteAttribute ((string) args[0].Value, (bool) args[1].Value);
1209                                                 } else {
1210                                                         bag.Obsolete = new ObsoleteAttribute ();
1211                                                 }
1212
1213                                                 continue;
1214                                         }
1215
1216                                         if (name == "ConditionalAttribute") {
1217                                                 if (ns != "System.Diagnostics")
1218                                                         continue;
1219
1220                                                 if (bag == null)
1221                                                         bag = new AttributesBag ();
1222
1223                                                 if (conditionals == null)
1224                                                         conditionals = new List<string> (2);
1225
1226                                                 conditionals.Add ((string) a.ConstructorArguments[0].Value);
1227                                                 continue;
1228                                         }
1229
1230                                         if (name == "CLSCompliantAttribute") {
1231                                                 if (ns != "System")
1232                                                         continue;
1233
1234                                                 if (bag == null)
1235                                                         bag = new AttributesBag ();
1236
1237                                                 bag.CLSAttributeValue = (bool) a.ConstructorArguments[0].Value;
1238                                                 continue;
1239                                         }
1240
1241                                         // Type only attributes
1242                                         if (mi.MemberType == MemberTypes.TypeInfo || mi.MemberType == MemberTypes.NestedType) {
1243                                                 if (name == "DefaultMemberAttribute") {
1244                                                         if (ns != "System.Reflection")
1245                                                                 continue;
1246
1247                                                         if (bag == null)
1248                                                                 bag = new AttributesBag ();
1249
1250                                                         bag.DefaultIndexerName = (string) a.ConstructorArguments[0].Value;
1251                                                         continue;
1252                                                 }
1253
1254                                                 if (name == "AttributeUsageAttribute") {
1255                                                         if (ns != "System")
1256                                                                 continue;
1257
1258                                                         if (bag == null)
1259                                                                 bag = new AttributesBag ();
1260
1261                                                         bag.AttributeUsage = new AttributeUsageAttribute ((AttributeTargets) a.ConstructorArguments[0].Value);
1262                                                         foreach (var named in a.NamedArguments) {
1263                                                                 if (named.MemberInfo.Name == "AllowMultiple")
1264                                                                         bag.AttributeUsage.AllowMultiple = (bool) named.TypedValue.Value;
1265                                                                 else if (named.MemberInfo.Name == "Inherited")
1266                                                                         bag.AttributeUsage.Inherited = (bool) named.TypedValue.Value;
1267                                                         }
1268                                                         continue;
1269                                                 }
1270
1271                                                 // Interface only attribute
1272                                                 if (name == "CoClassAttribute") {
1273                                                         if (ns != "System.Runtime.InteropServices")
1274                                                                 continue;
1275
1276                                                         if (bag == null)
1277                                                                 bag = new AttributesBag ();
1278
1279                                                         bag.CoClass = importer.ImportType ((MetaType) a.ConstructorArguments[0].Value);
1280                                                         continue;
1281                                                 }
1282                                         }
1283                                 }
1284
1285                                 if (bag == null)
1286                                         return Default;
1287
1288                                 if (conditionals != null)
1289                                         bag.Conditionals = conditionals.ToArray ();
1290                                 
1291                                 return bag;
1292                         }
1293                 }
1294
1295                 protected readonly MemberInfo provider;
1296                 protected AttributesBag cattrs;
1297                 protected readonly MetadataImporter importer;
1298
1299                 public ImportedDefinition (MemberInfo provider, MetadataImporter importer)
1300                 {
1301                         this.provider = provider;
1302                         this.importer = importer;
1303                 }
1304
1305                 #region Properties
1306
1307                 public bool IsImported {
1308                         get {
1309                                 return true;
1310                         }
1311                 }
1312
1313                 public virtual string Name {
1314                         get {
1315                                 return provider.Name;
1316                         }
1317                 }
1318
1319                 #endregion
1320
1321                 public string[] ConditionalConditions ()
1322                 {
1323                         if (cattrs == null)
1324                                 ReadAttributes ();
1325
1326                         return cattrs.Conditionals;
1327                 }
1328
1329                 public ObsoleteAttribute GetAttributeObsolete ()
1330                 {
1331                         if (cattrs == null)
1332                                 ReadAttributes ();
1333
1334                         return cattrs.Obsolete;
1335                 }
1336
1337                 public bool? CLSAttributeValue {
1338                         get {
1339                                 if (cattrs == null)
1340                                         ReadAttributes ();
1341
1342                                 return cattrs.CLSAttributeValue;
1343                         }
1344                 }
1345
1346                 protected void ReadAttributes ()
1347                 {
1348                         cattrs = AttributesBag.Read (provider, importer);
1349                 }
1350
1351                 public void SetIsAssigned ()
1352                 {
1353                         // Unused for imported members
1354                 }
1355
1356                 public void SetIsUsed ()
1357                 {
1358                         // Unused for imported members
1359                 }
1360         }
1361
1362         public class ImportedModuleDefinition
1363         {
1364                 readonly Module module;
1365                 bool cls_compliant;
1366                 readonly MetadataImporter importer;
1367                 
1368                 public ImportedModuleDefinition (Module module, MetadataImporter importer)
1369                 {
1370                         this.module = module;
1371                         this.importer = importer;
1372                 }
1373
1374                 #region Properties
1375
1376                 public bool IsCLSCompliant {
1377                         get {
1378                                 return cls_compliant;
1379                         }
1380                 }
1381
1382                 public string Name {
1383                         get {
1384                                 return module.Name;
1385                         }
1386                 }
1387
1388                 #endregion
1389
1390                 public void ReadAttributes ()
1391                 {
1392                         IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes (module);
1393
1394                         string ns, name;
1395                         foreach (var a in attrs) {
1396                                 importer.GetCustomAttributeTypeName (a, out ns, out name);
1397                                 if (name == "CLSCompliantAttribute") {
1398                                         if (ns != "System")
1399                                                 continue;
1400
1401                                         cls_compliant = (bool) a.ConstructorArguments[0].Value;
1402                                         continue;
1403                                 }
1404                         }
1405                 }
1406
1407                 //
1408                 // Reads assembly attributes which where attached to a special type because
1409                 // module does have assembly manifest
1410                 //
1411                 public List<Attribute> ReadAssemblyAttributes ()
1412                 {
1413                         var t = module.GetType (AssemblyAttributesPlaceholder.GetGeneratedName (Name));
1414                         if (t == null)
1415                                 return null;
1416
1417                         var field = t.GetField (AssemblyAttributesPlaceholder.AssemblyFieldName, BindingFlags.NonPublic | BindingFlags.Static);
1418                         if (field == null)
1419                                 return null;
1420
1421                         // TODO: implement, the idea is to fabricate specil Attribute class and
1422                         // add it to OptAttributes before resolving the source code attributes
1423                         // Need to build module location as well for correct error reporting
1424
1425                         //var assembly_attributes = CustomAttributeData.GetCustomAttributes (field);
1426                         //var attrs = new List<Attribute> (assembly_attributes.Count);
1427                         //foreach (var a in assembly_attributes)
1428                         //{
1429                         //    var type = metaImporter.ImportType (a.Constructor.DeclaringType);
1430                         //    var ctor = metaImporter.CreateMethod (a.Constructor, type);
1431
1432                         //    foreach (var carg in a.ConstructorArguments) {
1433                         //        carg.Value
1434                         //    }
1435
1436                         //    attrs.Add (new Attribute ("assembly", ctor, null, Location.Null, true));
1437                         //}
1438
1439                         return null;
1440                 }
1441         }
1442
1443         public class ImportedAssemblyDefinition : IAssemblyDefinition
1444         {
1445                 readonly Assembly assembly;
1446                 readonly AssemblyName aname;
1447                 readonly MetadataImporter importer;
1448                 bool cls_compliant;
1449                 bool contains_extension_methods;
1450
1451                 List<AssemblyName> internals_visible_to;
1452                 Dictionary<IAssemblyDefinition, AssemblyName> internals_visible_to_cache;
1453
1454                 public ImportedAssemblyDefinition (Assembly assembly, MetadataImporter importer)
1455                 {
1456                         this.assembly = assembly;
1457                         this.aname = assembly.GetName ();
1458                         this.importer = importer;
1459                 }
1460
1461                 #region Properties
1462
1463                 public Assembly Assembly {
1464                         get {
1465                                 return assembly;
1466                         }
1467                 }
1468
1469                 public string FullName {
1470                         get {
1471                                 return aname.FullName;
1472                         }
1473                 }
1474
1475                 public bool HasExtensionMethod {
1476                         get {
1477                                 return contains_extension_methods;
1478                         }
1479                 }
1480
1481                 public bool HasStrongName {
1482                         get {
1483                                 return aname.GetPublicKey ().Length != 0;
1484                         }
1485                 }
1486
1487                 public bool IsMissing {
1488                         get {
1489 #if STATIC
1490                                 return assembly.__IsMissing;
1491 #else
1492                                 return false;
1493 #endif
1494                         }
1495                 }
1496
1497                 public bool IsCLSCompliant {
1498                         get {
1499                                 return cls_compliant;
1500                         }
1501                 }
1502
1503                 public string Location {
1504                         get {
1505                                 return assembly.Location;
1506                         }
1507                 }
1508
1509                 public string Name {
1510                         get {
1511                                 return aname.Name;
1512                         }
1513                 }
1514
1515                 #endregion
1516
1517                 public byte[] GetPublicKeyToken ()
1518                 {
1519                         return aname.GetPublicKeyToken ();
1520                 }
1521
1522                 public AssemblyName GetAssemblyVisibleToName (IAssemblyDefinition assembly)
1523                 {
1524                         return internals_visible_to_cache [assembly];
1525                 }
1526
1527                 public bool IsFriendAssemblyTo (IAssemblyDefinition assembly)
1528                 {
1529                         if (internals_visible_to == null)
1530                                 return false;
1531
1532                         AssemblyName is_visible = null;
1533                         if (internals_visible_to_cache == null) {
1534                                 internals_visible_to_cache = new Dictionary<IAssemblyDefinition, AssemblyName> ();
1535                         } else {
1536                                 if (internals_visible_to_cache.TryGetValue (assembly, out is_visible))
1537                                         return is_visible != null;
1538                         }
1539
1540                         var token = assembly.GetPublicKeyToken ();
1541                         if (token != null && token.Length == 0)
1542                                 token = null;
1543
1544                         foreach (var internals in internals_visible_to) {
1545                                 if (internals.Name != assembly.Name)
1546                                         continue;
1547
1548                                 if (token == null && assembly is AssemblyDefinition) {
1549                                         is_visible = internals;
1550                                         break;
1551                                 }
1552
1553                                 if (!ArrayComparer.IsEqual (token, internals.GetPublicKeyToken ()))
1554                                         continue;
1555
1556                                 is_visible = internals;
1557                                 break;
1558                         }
1559
1560                         internals_visible_to_cache.Add (assembly, is_visible);
1561                         return is_visible != null;
1562                 }
1563
1564                 public void ReadAttributes ()
1565                 {
1566 #if STATIC
1567                         if (assembly.__IsMissing)
1568                                 return;
1569 #endif
1570
1571                         IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes (assembly);
1572
1573                         string ns, name;
1574                         foreach (var a in attrs) {
1575                                 importer.GetCustomAttributeTypeName (a, out ns, out name);
1576
1577                                 if (name == "CLSCompliantAttribute") {
1578                                         if (ns == "System") {
1579                                                 try {
1580                                                         cls_compliant = (bool) a.ConstructorArguments[0].Value;
1581                                                 } catch {
1582                                                 }
1583                                         }
1584                                         continue;
1585                                 }
1586
1587                                 if (name == "InternalsVisibleToAttribute") {
1588                                         if (ns != MetadataImporter.CompilerServicesNamespace)
1589                                                 continue;
1590
1591                                         string s;
1592                                         try {
1593                                                 s = a.ConstructorArguments[0].Value as string;
1594                                         } catch {
1595                                                 s = null;
1596                                         }
1597
1598                                         if (s == null)
1599                                                 continue;
1600
1601                                         var an = new AssemblyName (s);
1602                                         if (internals_visible_to == null)
1603                                                 internals_visible_to = new List<AssemblyName> ();
1604
1605                                         internals_visible_to.Add (an);
1606                                         continue;
1607                                 }
1608
1609                                 if (name == "ExtensionAttribute") {
1610                                         if (ns == MetadataImporter.CompilerServicesNamespace)
1611                                                 contains_extension_methods = true;
1612
1613                                         continue;
1614                                 }
1615                         }
1616                 }
1617
1618                 public override string ToString ()
1619                 {
1620                         return FullName;
1621                 }
1622         }
1623
1624         class ImportedMemberDefinition : ImportedDefinition
1625         {
1626                 readonly TypeSpec type;
1627
1628                 public ImportedMemberDefinition (MemberInfo member, TypeSpec type, MetadataImporter importer)
1629                         : base (member, importer)
1630                 {
1631                         this.type = type;
1632                 }
1633
1634                 #region Properties
1635
1636                 public TypeSpec MemberType {
1637                         get {
1638                                 return type;
1639                         }
1640                 }
1641
1642                 #endregion
1643         }
1644
1645         class ImportedParameterMemberDefinition : ImportedMemberDefinition, IParametersMember
1646         {
1647                 readonly AParametersCollection parameters;
1648
1649                 public ImportedParameterMemberDefinition (MethodBase provider, TypeSpec type, AParametersCollection parameters, MetadataImporter importer)
1650                         : base (provider, type, importer)
1651                 {
1652                         this.parameters = parameters;
1653                 }
1654
1655                 public ImportedParameterMemberDefinition (PropertyInfo provider, TypeSpec type, AParametersCollection parameters, MetadataImporter importer)
1656                         : base (provider, type, importer)
1657                 {
1658                         this.parameters = parameters;
1659                 }
1660
1661                 #region Properties
1662
1663                 public AParametersCollection Parameters {
1664                         get {
1665                                 return parameters;
1666                         }
1667                 }
1668
1669                 #endregion
1670         }
1671
1672         class ImportedGenericMethodDefinition : ImportedParameterMemberDefinition, IGenericMethodDefinition
1673         {
1674                 readonly TypeParameterSpec[] tparams;
1675
1676                 public ImportedGenericMethodDefinition (MethodInfo provider, TypeSpec type, AParametersCollection parameters, TypeParameterSpec[] tparams, MetadataImporter importer)
1677                         : base (provider, type, parameters, importer)
1678                 {
1679                         this.tparams = tparams;
1680                 }
1681
1682                 #region Properties
1683
1684                 public TypeParameterSpec[] TypeParameters {
1685                         get {
1686                                 return tparams;
1687                         }
1688                 }
1689
1690                 public int TypeParametersCount {
1691                         get {
1692                                 return tparams.Length;
1693                         }
1694                 }
1695
1696                 #endregion
1697         }
1698
1699         class ImportedTypeDefinition : ImportedDefinition, ITypeDefinition
1700         {
1701                 TypeParameterSpec[] tparams;
1702                 string name;
1703
1704                 public ImportedTypeDefinition (MetaType type, MetadataImporter importer)
1705                         : base (type, importer)
1706                 {
1707                 }
1708
1709                 #region Properties
1710
1711                 public IAssemblyDefinition DeclaringAssembly {
1712                         get {
1713                                 return importer.GetAssemblyDefinition (provider.Module.Assembly);
1714                         }
1715                 }
1716
1717                 public override string Name {
1718                         get {
1719                                 if (name == null) {
1720                                         name = base.Name;
1721                                         if (tparams != null) {
1722                                                 int arity_start = name.IndexOf ('`');
1723                                                 if (arity_start > 0)
1724                                                         name = name.Substring (0, arity_start);
1725                                         }
1726                                 }
1727
1728                                 return name;
1729                         }
1730                 }
1731
1732                 public string Namespace {
1733                         get {
1734                                 return ((MetaType) provider).Namespace;
1735                         }
1736                 }
1737
1738                 public int TypeParametersCount {
1739                         get {
1740                                 return tparams == null ? 0 : tparams.Length;
1741                         }
1742                 }
1743
1744                 public TypeParameterSpec[] TypeParameters {
1745                         get {
1746                                 return tparams;
1747                         }
1748                         set {
1749                                 tparams = value;
1750                         }
1751                 }
1752
1753                 #endregion
1754
1755                 public static void Error_MissingDependency (IMemberContext ctx, List<TypeSpec> types, Location loc)
1756                 {
1757                         // 
1758                         // Report details about missing type and most likely cause of the problem.
1759                         // csc reports 1683, 1684 as warnings but we report them only when used
1760                         // or referenced from the user core in which case compilation error has to
1761                         // be reported because compiler cannot continue anyway
1762                         //
1763                         foreach (var t in types) {
1764                                 string name = t.GetSignatureForError ();
1765
1766                                 if (t.MemberDefinition.DeclaringAssembly == ctx.Module.DeclaringAssembly) {
1767                                         ctx.Module.Compiler.Report.Error (1683, loc,
1768                                                 "Reference to type `{0}' claims it is defined in this assembly, but it is not defined in source or any added modules",
1769                                                 name);
1770                                 } else if (t.MemberDefinition.DeclaringAssembly.IsMissing) {
1771                                         ctx.Module.Compiler.Report.Error (12, loc,
1772                                                 "The type `{0}' is defined in an assembly that is not referenced. Consider adding a reference to assembly `{1}'",
1773                                                 name, t.MemberDefinition.DeclaringAssembly.FullName);
1774                                 } else {
1775                                         ctx.Module.Compiler.Report.Error (1684, loc,
1776                                                 "Reference to type `{0}' claims it is defined assembly `{1}', but it could not be found",
1777                                                 name, t.MemberDefinition.DeclaringAssembly.FullName);
1778                                 }
1779                         }
1780                 }
1781
1782                 public TypeSpec GetAttributeCoClass ()
1783                 {
1784                         if (cattrs == null)
1785                                 ReadAttributes ();
1786
1787                         return cattrs.CoClass;
1788                 }
1789
1790                 public string GetAttributeDefaultMember ()
1791                 {
1792                         if (cattrs == null)
1793                                 ReadAttributes ();
1794
1795                         return cattrs.DefaultIndexerName;
1796                 }
1797
1798                 public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
1799                 {
1800                         if (cattrs == null)
1801                                 ReadAttributes ();
1802
1803                         return cattrs.AttributeUsage;
1804                 }
1805
1806                 bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
1807                 {
1808                         var a = importer.GetAssemblyDefinition (provider.Module.Assembly);
1809                         return a == assembly || a.IsFriendAssemblyTo (assembly);
1810                 }
1811
1812                 public void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
1813                 {
1814                         //
1815                         // Not interested in members of nested private types unless the importer needs them
1816                         //
1817                         if (declaringType.IsPrivate && importer.IgnorePrivateMembers) {
1818                                 cache = MemberCache.Empty;
1819                                 return;
1820                         }
1821
1822                         var loading_type = (MetaType) provider;
1823                         const BindingFlags all_members = BindingFlags.DeclaredOnly |
1824                                 BindingFlags.Static | BindingFlags.Instance |
1825                                 BindingFlags.Public | BindingFlags.NonPublic;
1826
1827                         const MethodAttributes explicit_impl = MethodAttributes.NewSlot |
1828                                         MethodAttributes.Virtual | MethodAttributes.HideBySig |
1829                                         MethodAttributes.Final;
1830
1831                         Dictionary<MethodBase, MethodSpec> possible_accessors = null;
1832                         List<EventSpec> imported_events = null;
1833                         EventSpec event_spec;
1834                         MemberSpec imported;
1835                         MethodInfo m;
1836                         MemberInfo[] all;
1837                         try {
1838                                 all = loading_type.GetMembers (all_members);
1839                         } catch (Exception e) {
1840                                 throw new InternalErrorException (e, "Could not import type `{0}' from `{1}'",
1841                                         declaringType.GetSignatureForError (), declaringType.MemberDefinition.DeclaringAssembly.FullName);
1842                         }
1843
1844                         if (cache == null) {
1845                                 cache = new MemberCache (all.Length);
1846
1847                                 //
1848                                 // Do the types first as they can be referenced by the members before
1849                                 // they are found or inflated
1850                                 //
1851                                 foreach (var member in all) {
1852                                         if (member.MemberType != MemberTypes.NestedType)
1853                                                 continue;
1854
1855                                         var t = (MetaType) member;
1856
1857                                         // Ignore compiler generated types, mostly lambda containers
1858                                         if ((t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate && importer.IgnorePrivateMembers)
1859                                                 continue;
1860
1861                                         imported = importer.CreateNestedType (t, declaringType);
1862                                         cache.AddMemberImported (imported);
1863                                 }
1864
1865                                 foreach (var member in all) {
1866                                         if (member.MemberType != MemberTypes.NestedType)
1867                                                 continue;
1868
1869                                         var t = (MetaType) member;
1870
1871                                         if ((t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate && importer.IgnorePrivateMembers)
1872                                                 continue;
1873
1874                                         importer.ImportTypeBase (t);
1875                                 }
1876                         }
1877
1878                         if (!onlyTypes) {
1879                                 //
1880                                 // The logic here requires methods to be returned first which seems to work for both Mono and .NET
1881                                 //
1882                                 foreach (var member in all) {
1883                                         switch (member.MemberType) {
1884                                         case MemberTypes.Constructor:
1885                                         case MemberTypes.Method:
1886                                                 MethodBase mb = (MethodBase) member;
1887                                                 var attrs = mb.Attributes;
1888
1889                                                 if ((attrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Private) {
1890                                                         if (importer.IgnorePrivateMembers)
1891                                                                 continue;
1892
1893                                                         // Ignore explicitly implemented members
1894                                                         if ((attrs & explicit_impl) == explicit_impl)
1895                                                                 continue;
1896
1897                                                         // Ignore compiler generated methods
1898                                                         if (importer.HasAttribute (CustomAttributeData.GetCustomAttributes (mb), "CompilerGeneratedAttribute", MetadataImporter.CompilerServicesNamespace))
1899                                                                 continue;
1900                                                 }
1901
1902                                                 imported = importer.CreateMethod (mb, declaringType);
1903                                                 if (imported.Kind == MemberKind.Method && !imported.IsGeneric) {
1904                                                         if (possible_accessors == null)
1905                                                                 possible_accessors = new Dictionary<MethodBase, MethodSpec> (ReferenceEquality<MethodBase>.Default);
1906
1907                                                         // There are no metadata rules for accessors, we have to consider any method as possible candidate
1908                                                         possible_accessors.Add (mb, (MethodSpec) imported);
1909                                                 }
1910
1911                                                 break;
1912                                         case MemberTypes.Property:
1913                                                 if (possible_accessors == null)
1914                                                         continue;
1915
1916                                                 var p = (PropertyInfo) member;
1917                                                 //
1918                                                 // Links possible accessors with property
1919                                                 //
1920                                                 MethodSpec get, set;
1921                                                 m = p.GetGetMethod (true);
1922                                                 if (m == null || !possible_accessors.TryGetValue (m, out get))
1923                                                         get = null;
1924
1925                                                 m = p.GetSetMethod (true);
1926                                                 if (m == null || !possible_accessors.TryGetValue (m, out set))
1927                                                         set = null;
1928
1929                                                 // No accessors registered (e.g. explicit implementation)
1930                                                 if (get == null && set == null)
1931                                                         continue;
1932
1933                                                 imported = importer.CreateProperty (p, declaringType, get, set);
1934                                                 if (imported == null)
1935                                                         continue;
1936
1937                                                 break;
1938                                         case MemberTypes.Event:
1939                                                 if (possible_accessors == null)
1940                                                         continue;
1941
1942                                                 var e = (EventInfo) member;
1943                                                 //
1944                                                 // Links accessors with event
1945                                                 //
1946                                                 MethodSpec add, remove;
1947                                                 m = e.GetAddMethod (true);
1948                                                 if (m == null || !possible_accessors.TryGetValue (m, out add))
1949                                                         add = null;
1950
1951                                                 m = e.GetRemoveMethod (true);
1952                                                 if (m == null || !possible_accessors.TryGetValue (m, out remove))
1953                                                         remove = null;
1954
1955                                                 // Both accessors are required
1956                                                 if (add == null || remove == null)
1957                                                         continue;
1958
1959                                                 event_spec = importer.CreateEvent (e, declaringType, add, remove);
1960                                                 if (!importer.IgnorePrivateMembers) {
1961                                                         if (imported_events == null)
1962                                                                 imported_events = new List<EventSpec> ();
1963
1964                                                         imported_events.Add (event_spec);
1965                                                 }
1966
1967                                                 imported = event_spec;
1968                                                 break;
1969                                         case MemberTypes.Field:
1970                                                 var fi = (FieldInfo) member;
1971
1972                                                 imported = importer.CreateField (fi, declaringType);
1973                                                 if (imported == null)
1974                                                         continue;
1975
1976                                                 //
1977                                                 // For dynamic binder event has to be fully restored to allow operations
1978                                                 // within the type container to work correctly
1979                                                 //
1980                                                 if (imported_events != null) {
1981                                                         // The backing event field should be private but it may not
1982                                                         int i;
1983                                                         for (i = 0; i < imported_events.Count; ++i) {
1984                                                                 var ev = imported_events[i];
1985                                                                 if (ev.Name == fi.Name) {
1986                                                                         ev.BackingField = (FieldSpec) imported;
1987                                                                         imported_events.RemoveAt (i);
1988                                                                         i = -1;
1989                                                                         break;
1990                                                                 }
1991                                                         }
1992
1993                                                         if (i < 0)
1994                                                                 continue;
1995                                                 }
1996
1997                                                 break;
1998                                         case MemberTypes.NestedType:
1999                                                 // Already in the cache from the first pass
2000                                                 continue;
2001                                         default:
2002                                                 throw new NotImplementedException (member.ToString ());
2003                                         }
2004
2005                                         cache.AddMemberImported (imported);
2006                                 }
2007                         }
2008
2009                         if (declaringType.IsInterface && declaringType.Interfaces != null) {
2010                                 foreach (var iface in declaringType.Interfaces) {
2011                                         cache.AddInterface (iface);
2012                                 }
2013                         }
2014                 }
2015         }
2016
2017         class ImportedTypeParameterDefinition : ImportedDefinition, ITypeDefinition
2018         {
2019                 public ImportedTypeParameterDefinition (MetaType type, MetadataImporter importer)
2020                         : base (type, importer)
2021                 {
2022                 }
2023
2024                 #region Properties
2025
2026                 public IAssemblyDefinition DeclaringAssembly {
2027                         get {
2028                                 throw new NotImplementedException ();
2029                         }
2030                 }
2031
2032                 public string Namespace {
2033                         get {
2034                                 return null;
2035                         }
2036                 }
2037
2038                 public int TypeParametersCount {
2039                         get {
2040                                 return 0;
2041                         }
2042                 }
2043
2044                 public TypeParameterSpec[] TypeParameters {
2045                         get {
2046                                 return null;
2047                         }
2048                 }
2049
2050                 #endregion
2051
2052                 public TypeSpec GetAttributeCoClass ()
2053                 {
2054                         return null;
2055                 }
2056
2057                 public string GetAttributeDefaultMember ()
2058                 {
2059                         throw new NotSupportedException ();
2060                 }
2061
2062                 public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
2063                 {
2064                         throw new NotSupportedException ();
2065                 }
2066
2067                 bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
2068                 {
2069                         throw new NotImplementedException ();
2070                 }
2071
2072                 public void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
2073                 {
2074                         throw new NotImplementedException ();
2075                 }
2076         }
2077 }