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