2 // typespec.cs: Type specification
4 // Authors: Marek Safar (marek.safar@gmail.com)
6 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 // Copyright 2010 Novell, Inc
12 using System.Collections.Generic;
16 using MetaType = IKVM.Reflection.Type;
17 using IKVM.Reflection;
19 using MetaType = System.Type;
20 using System.Reflection;
25 public class TypeSpec : MemberSpec
27 protected MetaType info;
28 protected MemberCache cache;
29 protected IList<TypeSpec> ifaces;
32 Dictionary<TypeSpec[], InflatedTypeSpec> inflated_instances;
34 public static readonly TypeSpec[] EmptyTypes = new TypeSpec[0];
37 // Reflection Emit hacking
38 static readonly Type TypeBuilder;
39 static readonly Type GenericTypeBuilder;
43 var assembly = typeof (object).Assembly;
44 TypeBuilder = assembly.GetType ("System.Reflection.Emit.TypeBuilder");
45 GenericTypeBuilder = assembly.GetType ("System.Reflection.MonoGenericClass");
46 if (GenericTypeBuilder == null)
47 GenericTypeBuilder = assembly.GetType ("System.Reflection.Emit.TypeBuilderInstantiation");
51 public TypeSpec (MemberKind kind, TypeSpec declaringType, ITypeDefinition definition, MetaType info, Modifiers modifiers)
52 : base (kind, declaringType, definition, modifiers)
54 this.declaringType = declaringType;
57 if (definition != null && definition.TypeParametersCount > 0)
58 state |= StateFlags.IsGeneric;
63 public override int Arity {
65 return MemberDefinition.TypeParametersCount;
69 public virtual TypeSpec BaseType {
78 public virtual BuildinTypeSpec.Type BuildinType {
80 return BuildinTypeSpec.Type.None;
84 public bool HasDynamicElement {
86 return (state & StateFlags.HasDynamicElement) != 0;
90 public virtual IList<TypeSpec> Interfaces {
101 return Kind == MemberKind.ArrayType;
105 public bool IsAttribute {
115 if (type.BuildinType == BuildinTypeSpec.Type.Attribute)
118 type = type.base_type;
119 } while (type != null);
125 public bool IsInterface {
127 return Kind == MemberKind.Interface;
131 public bool IsClass {
133 return Kind == MemberKind.Class;
137 public bool IsConstantCompatible {
139 if ((Kind & (MemberKind.Enum | MemberKind.Class | MemberKind.Interface | MemberKind.Delegate | MemberKind.ArrayType)) != 0)
142 switch (BuildinType) {
143 case BuildinTypeSpec.Type.Int:
144 case BuildinTypeSpec.Type.UInt:
145 case BuildinTypeSpec.Type.Long:
146 case BuildinTypeSpec.Type.ULong:
147 case BuildinTypeSpec.Type.Float:
148 case BuildinTypeSpec.Type.Double:
149 case BuildinTypeSpec.Type.Char:
150 case BuildinTypeSpec.Type.Short:
151 case BuildinTypeSpec.Type.Decimal:
152 case BuildinTypeSpec.Type.Bool:
153 case BuildinTypeSpec.Type.SByte:
154 case BuildinTypeSpec.Type.Byte:
155 case BuildinTypeSpec.Type.UShort:
156 case BuildinTypeSpec.Type.Dynamic:
164 public bool IsDelegate {
166 return Kind == MemberKind.Delegate;
171 get { return Kind == MemberKind.Enum; }
174 // TODO: Should probably do
175 // IsGenericType -- recursive
176 // HasTypeParameter -- non-recursive
177 public bool IsGenericOrParentIsGeneric {
183 ts = ts.declaringType;
184 } while (ts != null);
190 public bool IsGenericParameter {
191 get { return Kind == MemberKind.TypeParameter; }
194 public bool IsNested {
195 get { return declaringType != null && Kind != MemberKind.TypeParameter; }
198 public bool IsPointer {
200 return Kind == MemberKind.PointerType;
204 public bool IsSealed {
205 get { return (Modifiers & Modifiers.SEALED) != 0; }
208 public bool IsStruct {
210 return Kind == MemberKind.Struct;
214 public bool IsTypeBuilder {
219 var meta = GetMetaInfo().GetType ();
220 return meta == TypeBuilder || meta == GenericTypeBuilder;
225 public MemberCache MemberCache {
227 if (cache == null || (state & StateFlags.PendingMemberCacheMembers) != 0)
228 InitializeMemberCache (false);
234 throw new InternalErrorException ("Membercache reset");
240 public MemberCache MemberCacheTypes {
243 InitializeMemberCache (true);
249 public new ITypeDefinition MemberDefinition {
251 return (ITypeDefinition) definition;
255 // TODO: Wouldn't be better to rely on cast to InflatedTypeSpec and
256 // remove the property, YES IT WOULD !!!
257 public virtual TypeSpec[] TypeArguments {
258 get { return TypeSpec.EmptyTypes; }
263 public bool AddInterface (TypeSpec iface)
265 if ((state & StateFlags.InterfacesExpanded) != 0)
266 throw new InternalErrorException ("Modifying expanded interface list");
268 if (ifaces == null) {
269 ifaces = new List<TypeSpec> () { iface };
273 if (!ifaces.Contains (iface)) {
281 public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
283 if (Kind != MemberKind.Class)
284 throw new InternalErrorException ();
287 return Attribute.DefaultUsageAttribute;
289 AttributeUsageAttribute aua = null;
291 while (type != null) {
292 aua = type.MemberDefinition.GetAttributeUsage (pa);
296 type = type.BaseType;
302 public virtual MetaType GetMetaInfo ()
307 public virtual TypeSpec GetDefinition ()
312 public override string GetSignatureForError ()
317 s = DeclaringType.GetSignatureForError ();
318 } else if (MemberDefinition is AnonymousTypeClass) {
319 return ((AnonymousTypeClass) MemberDefinition).GetSignatureForError ();
321 s = MemberDefinition.Namespace;
324 if (!string.IsNullOrEmpty (s))
327 return s + Name + GetTypeNameSignature ();
330 protected virtual string GetTypeNameSignature ()
335 return "<" + TypeManager.CSharpName (MemberDefinition.TypeParameters) + ">";
338 public bool ImplementsInterface (TypeSpec iface, bool variantly)
342 if (t.Interfaces != null) { // TODO: Try t.iface
343 foreach (TypeSpec i in t.Interfaces) {
344 if (i == iface || TypeSpecComparer.IsEqual (i, iface))
347 if (variantly && TypeSpecComparer.Variant.IsEqual (i, iface))
358 protected virtual void InitializeMemberCache (bool onlyTypes)
361 MemberDefinition.LoadMembers (this, onlyTypes, ref cache);
362 } catch (Exception e) {
363 throw new InternalErrorException (e, "Unexpected error when loading type `{0}'", GetSignatureForError ());
367 state |= StateFlags.PendingMemberCacheMembers;
369 state &= ~StateFlags.PendingMemberCacheMembers;
373 // Is @baseClass base implementation of @type. With enabled @dynamicIsEqual the slower
374 // comparison is used to hide differences between `object' and `dynamic' for generic
375 // types. Should not be used for comparisons where G<object> != G<dynamic>
377 public static bool IsBaseClass (TypeSpec type, TypeSpec baseClass, bool dynamicIsObject)
379 if (dynamicIsObject && baseClass.IsGeneric) {
381 // Returns true for a hierarchies like this when passing baseClass of A<dynamic>
383 // class B : A<object> {}
385 while (type != null) {
386 type = type.BaseType;
387 if (TypeSpecComparer.IsEqual (type, baseClass))
394 while (type != null) {
395 type = type.BaseType;
396 if (type == baseClass)
403 public override MemberSpec InflateMember (TypeParameterInflator inflator)
405 var targs = IsGeneric ? MemberDefinition.TypeParameters : TypeSpec.EmptyTypes;
408 // When inflating nested type from inside the type instance will be same
409 // because type parameters are same for all nested types
411 if (DeclaringType == inflator.TypeInstance) {
412 return MakeGenericType (inflator.Context, targs);
415 return new InflatedTypeSpec (inflator.Context, this, inflator.TypeInstance, targs);
418 public InflatedTypeSpec MakeGenericType (IModuleContext context, TypeSpec[] targs)
420 if (targs.Length == 0 && !IsNested)
421 throw new ArgumentException ("Empty type arguments for type " + GetSignatureForError ());
423 InflatedTypeSpec instance;
425 if (inflated_instances == null) {
426 inflated_instances = new Dictionary<TypeSpec[], InflatedTypeSpec> (TypeSpecComparer.Default);
429 instance = this as InflatedTypeSpec;
430 if (instance != null) {
432 // Nested types could be inflated on already inflated instances
433 // Caching this type ensured we are using same instance for
434 // inside/outside inflation using local type parameters
436 inflated_instances.Add (TypeArguments, instance);
441 if (!inflated_instances.TryGetValue (targs, out instance)) {
442 if (GetDefinition () != this && !IsNested)
443 throw new InternalErrorException ("`{0}' must be type definition or nested non-inflated type to MakeGenericType",
444 GetSignatureForError ());
446 instance = new InflatedTypeSpec (context, this, declaringType, targs);
447 inflated_instances.Add (targs, instance);
453 public virtual TypeSpec Mutate (TypeParameterMutator mutator)
458 public override List<TypeSpec> ResolveMissingDependencies ()
460 List<TypeSpec> missing = null;
462 if (Kind == MemberKind.MissingType) {
463 missing = new List<TypeSpec> ();
468 foreach (var targ in TypeArguments) {
469 if (targ.Kind == MemberKind.MissingType) {
471 missing = new List<TypeSpec> ();
477 if (Interfaces != null) {
478 foreach (var iface in Interfaces) {
479 if (iface.Kind == MemberKind.MissingType) {
481 missing = new List<TypeSpec> ();
488 if (missing != null || BaseType == null)
491 return BaseType.ResolveMissingDependencies ();
494 public void SetMetaInfo (MetaType info)
496 if (this.info != null)
497 throw new InternalErrorException ("MetaInfo reset");
502 public void SetExtensionMethodContainer ()
504 modifiers |= Modifiers.METHOD_EXTENSION;
508 public sealed class BuildinTypeSpec : TypeSpec
514 // Ordered carefully for fast compares
540 MulticastDelegate = 23,
555 readonly string name;
557 public BuildinTypeSpec (MemberKind kind, string ns, string name, Type buildinKind)
558 : base (kind, null, null, null, Modifiers.PUBLIC)
560 this.type = buildinKind;
565 public BuildinTypeSpec (string name, Type buildinKind)
566 : this (MemberKind.InternalCompilerType, "", name, buildinKind)
568 // Make all internal types CLS-compliant, non-obsolete, compact
569 state = (state & ~(StateFlags.CLSCompliant_Undetected | StateFlags.Obsolete_Undetected | StateFlags.MissingDependency_Undetected)) | StateFlags.CLSCompliant;
574 public override int Arity {
580 public override BuildinTypeSpec.Type BuildinType {
586 public string FullName {
588 return ns + '.' + name;
592 public override string Name {
598 public string Namespace {
606 public static bool IsPrimitiveType (TypeSpec type)
608 switch (type.BuildinType) {
626 public static bool IsPrimitiveTypeOrDecimal (TypeSpec type)
628 switch (type.BuildinType) {
648 public override string GetSignatureForError ()
651 case "Int32": return "int";
652 case "Int64": return "long";
653 case "String": return "string";
654 case "Boolean": return "bool";
655 case "Void": return "void";
656 case "Object": return "object";
657 case "UInt32": return "uint";
658 case "Int16": return "short";
659 case "UInt16": return "ushort";
660 case "UInt64": return "ulong";
661 case "Single": return "float";
662 case "Double": return "double";
663 case "Decimal": return "decimal";
664 case "Char": return "char";
665 case "Byte": return "byte";
666 case "SByte": return "sbyte";
676 // Returns the size of type if known, otherwise, 0
678 public static int GetSize (TypeSpec type)
680 switch (type.BuildinType) {
704 public void SetDefinition (ITypeDefinition td, MetaType type, Modifiers mod)
706 this.definition = td;
708 this.modifiers |= (mod & ~Modifiers.AccessibilityMask);
711 public void SetDefinition (TypeSpec ts)
713 this.definition = ts.MemberDefinition;
714 this.info = ts.GetMetaInfo ();
715 this.BaseType = ts.BaseType;
716 this.Interfaces = ts.Interfaces;
717 this.modifiers = ts.Modifiers;
721 static class TypeSpecComparer
724 // Does strict reference comparion only
726 public static readonly DefaultImpl Default = new DefaultImpl ();
728 public class DefaultImpl : IEqualityComparer<TypeSpec[]>
730 #region IEqualityComparer<TypeSpec[]> Members
732 bool IEqualityComparer<TypeSpec[]>.Equals (TypeSpec[] x, TypeSpec[] y)
737 if (x.Length != y.Length)
740 for (int i = 0; i < x.Length; ++i)
747 int IEqualityComparer<TypeSpec[]>.GetHashCode (TypeSpec[] obj)
750 for (int i = 0; i < obj.Length; ++i)
751 hash = (hash << 5) - hash + obj[i].GetHashCode ();
760 // When comparing type signature of overrides or overloads
761 // this version tolerates different MVARs at same position
763 public static class Override
765 public static bool IsEqual (TypeSpec a, TypeSpec b)
771 // Consider the following example:
773 // public abstract class A
775 // public abstract T Foo<T>();
778 // public class B : A
780 // public override U Foo<T>() { return default (U); }
783 // Here, `T' and `U' are method type parameters from different methods
784 // (A.Foo and B.Foo), so both `==' and Equals() will fail.
786 // However, since we're determining whether B.Foo() overrides A.Foo(),
787 // we need to do a signature based comparision and consider them equal.
790 var tp_a = a as TypeParameterSpec;
792 var tp_b = b as TypeParameterSpec;
793 return tp_b != null && tp_a.IsMethodOwned == tp_b.IsMethodOwned && tp_a.DeclaredPosition == tp_b.DeclaredPosition;
796 var ac_a = a as ArrayContainer;
798 var ac_b = b as ArrayContainer;
799 return ac_b != null && ac_a.Rank == ac_b.Rank && IsEqual (ac_a.Element, ac_b.Element);
802 if (a == InternalType.Dynamic || b == InternalType.Dynamic)
803 return b.BuildinType == BuildinTypeSpec.Type.Object || a.BuildinType == BuildinTypeSpec.Type.Object;
805 if (a.MemberDefinition != b.MemberDefinition)
809 for (int i = 0; i < a.TypeArguments.Length; ++i) {
810 if (!IsEqual (a.TypeArguments[i], b.TypeArguments[i]))
822 // Compares unordered arrays
824 public static bool IsSame (TypeSpec[] a, TypeSpec[] b)
829 if (a == null || b == null || a.Length != b.Length)
832 for (int ai = 0; ai < a.Length; ++ai) {
834 for (int bi = 0; bi < b.Length; ++bi) {
835 if (IsEqual (a[ai], b[bi])) {
848 public static bool IsEqual (AParametersCollection a, AParametersCollection b)
853 if (a.Count != b.Count)
856 for (int i = 0; i < a.Count; ++i) {
857 if (!IsEqual (a.Types[i], b.Types[i]))
860 const Parameter.Modifier ref_out = Parameter.Modifier.REF | Parameter.Modifier.OUT;
861 if ((a.FixedParameters[i].ModFlags & ref_out) != (b.FixedParameters[i].ModFlags & ref_out))
870 // Type variance equality comparison
872 public static class Variant
874 public static bool IsEqual (TypeSpec type1, TypeSpec type2)
876 if (!type1.IsGeneric || !type2.IsGeneric)
879 var target_type_def = type2.MemberDefinition;
880 if (type1.MemberDefinition != target_type_def)
883 var t1_targs = type1.TypeArguments;
884 var t2_targs = type2.TypeArguments;
885 var targs_definition = target_type_def.TypeParameters;
887 if (!type1.IsInterface && !type1.IsDelegate) {
891 for (int i = 0; i < targs_definition.Length; ++i) {
892 Variance v = targs_definition[i].Variance;
893 if (v == Variance.None) {
894 if (t1_targs[i] == t2_targs[i])
899 if (v == Variance.Covariant) {
900 if (!Convert.ImplicitReferenceConversionExists (new EmptyExpression (t1_targs[i]), t2_targs[i]))
902 } else if (!Convert.ImplicitReferenceConversionExists (new EmptyExpression (t2_targs[i]), t1_targs[i])) {
912 // Checks whether two generic instances may become equal for some
913 // particular instantiation (26.3.1).
915 public static class Unify
918 // Either @a or @b must be generic type
920 public static bool IsEqual (TypeSpec a, TypeSpec b)
922 if (a.MemberDefinition != b.MemberDefinition)
925 var ta = a.TypeArguments;
926 var tb = b.TypeArguments;
927 for (int i = 0; i < ta.Length; i++) {
928 if (!MayBecomeEqualGenericTypes (ta[i], tb[i]))
935 static bool ContainsTypeParameter (TypeSpec tparam, TypeSpec type)
937 TypeSpec[] targs = type.TypeArguments;
938 for (int i = 0; i < targs.Length; i++) {
939 if (tparam == targs[i])
942 if (ContainsTypeParameter (tparam, targs[i]))
950 /// Check whether `a' and `b' may become equal generic types.
951 /// The algorithm to do that is a little bit complicated.
953 static bool MayBecomeEqualGenericTypes (TypeSpec a, TypeSpec b)
955 if (a.IsGenericParameter) {
957 // If a is an array of a's type, they may never
964 // If b is a generic parameter or an actual type,
965 // they may become equal:
967 // class X<T,U> : I<T>, I<U>
968 // class X<T> : I<T>, I<float>
970 if (b.IsGenericParameter)
971 return a.DeclaringType == b.DeclaringType;
974 // We're now comparing a type parameter with a
975 // generic instance. They may become equal unless
976 // the type parameter appears anywhere in the
979 // class X<T,U> : I<T>, I<X<U>>
980 // -> error because you could instanciate it as
983 // class X<T> : I<T>, I<X<T>> -> ok
986 return !ContainsTypeParameter (a, b);
989 if (b.IsGenericParameter)
990 return MayBecomeEqualGenericTypes (b, a);
993 // At this point, neither a nor b are a type parameter.
995 // If one of them is a generic instance, compare them (if the
996 // other one is not a generic instance, they can never
999 if (TypeManager.IsGenericType (a) || TypeManager.IsGenericType (b))
1000 return IsEqual (a, b);
1003 // If both of them are arrays.
1005 var a_ac = a as ArrayContainer;
1007 var b_ac = b as ArrayContainer;
1008 if (b_ac == null || a_ac.Rank != b_ac.Rank)
1011 return MayBecomeEqualGenericTypes (a_ac.Element, b_ac.Element);
1015 // Ok, two ordinary types.
1021 public static bool Equals (TypeSpec[] x, TypeSpec[] y)
1026 if (x.Length != y.Length)
1029 for (int i = 0; i < x.Length; ++i)
1030 if (!IsEqual (x[i], y[i]))
1037 // Identity type conversion
1039 // Default reference comparison, it has to be used when comparing
1040 // two possible dynamic/internal types
1042 public static bool IsEqual (TypeSpec a, TypeSpec b)
1045 // This also rejects dynamic == dynamic
1046 return a.Kind != MemberKind.InternalCompilerType || a == InternalType.Dynamic;
1050 // object and dynamic are considered equivalent there is an identity conversion
1051 // between object and dynamic, and between constructed types that are the same
1052 // when replacing all occurences of dynamic with object.
1054 if (a == InternalType.Dynamic || b == InternalType.Dynamic)
1055 return b.BuildinType == BuildinTypeSpec.Type.Object || a.BuildinType == BuildinTypeSpec.Type.Object;
1061 var a_a = (ArrayContainer) a;
1062 var b_a = b as ArrayContainer;
1066 return IsEqual (a_a.Element, b_a.Element) && a_a.Rank == b_a.Rank;
1069 if (!a.IsGeneric || b == null || !b.IsGeneric)
1072 if (a.MemberDefinition != b.MemberDefinition)
1076 if (!Equals (a.TypeArguments, b.TypeArguments))
1079 a = a.DeclaringType;
1080 b = b.DeclaringType;
1081 } while (a != null);
1087 public interface ITypeDefinition : IMemberDefinition
1089 IAssemblyDefinition DeclaringAssembly { get; }
1090 string Namespace { get; }
1091 int TypeParametersCount { get; }
1092 TypeParameterSpec[] TypeParameters { get; }
1094 TypeSpec GetAttributeCoClass ();
1095 string GetAttributeDefaultMember ();
1096 AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa);
1097 bool IsInternalAsPublic (IAssemblyDefinition assembly);
1098 void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache);
1101 class InternalType : TypeSpec, ITypeDefinition
1103 public static readonly InternalType AnonymousMethod = new InternalType ("anonymous method");
1104 public static readonly InternalType Arglist = new InternalType ("__arglist");
1105 public static BuildinTypeSpec Dynamic;
1106 public static readonly InternalType MethodGroup = new InternalType ("method group");
1107 public static BuildinTypeSpec Null;
1108 public static readonly InternalType FakeInternalType = new InternalType ("<fake$type>");
1110 readonly string name;
1112 InternalType (string name, MemberCache cache)
1118 InternalType (string name)
1119 : base (MemberKind.InternalCompilerType, null, null, null, Modifiers.PUBLIC)
1122 this.definition = this;
1123 cache = MemberCache.Empty;
1125 // Make all internal types CLS-compliant, non-obsolete
1126 state = (state & ~(StateFlags.CLSCompliant_Undetected | StateFlags.Obsolete_Undetected)) | StateFlags.CLSCompliant;
1131 public override int Arity {
1137 IAssemblyDefinition ITypeDefinition.DeclaringAssembly {
1139 throw new NotImplementedException ();
1143 bool IMemberDefinition.IsImported {
1149 public override string Name {
1155 string ITypeDefinition.Namespace {
1161 int ITypeDefinition.TypeParametersCount {
1167 TypeParameterSpec[] ITypeDefinition.TypeParameters {
1175 public override string GetSignatureForError ()
1180 #region ITypeDefinition Members
1182 TypeSpec ITypeDefinition.GetAttributeCoClass ()
1187 string ITypeDefinition.GetAttributeDefaultMember ()
1192 AttributeUsageAttribute ITypeDefinition.GetAttributeUsage (PredefinedAttribute pa)
1197 bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
1199 throw new NotImplementedException ();
1202 void ITypeDefinition.LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
1204 throw new NotImplementedException ();
1207 string[] IMemberDefinition.ConditionalConditions ()
1212 ObsoleteAttribute IMemberDefinition.GetAttributeObsolete ()
1217 bool? IMemberDefinition.CLSAttributeValue {
1223 void IMemberDefinition.SetIsAssigned ()
1227 void IMemberDefinition.SetIsUsed ()
1234 public abstract class ElementTypeSpec : TypeSpec, ITypeDefinition
1236 protected ElementTypeSpec (MemberKind kind, TypeSpec element, MetaType info)
1237 : base (kind, element.DeclaringType, null, info, element.Modifiers)
1239 this.Element = element;
1241 // Some flags can be copied directly from the element
1242 const StateFlags shared_flags = StateFlags.CLSCompliant | StateFlags.CLSCompliant_Undetected
1243 | StateFlags.Obsolete | StateFlags.Obsolete_Undetected | StateFlags.HasDynamicElement;
1244 state &= ~shared_flags;
1245 state |= (element.state & shared_flags);
1247 if (element == InternalType.Dynamic)
1248 state |= StateFlags.HasDynamicElement;
1250 // Has to use its own type definition instead of just element definition to
1251 // correctly identify itself for cases like x.MemberDefininition == predefined.MemberDefinition
1252 this.definition = this;
1254 cache = MemberCache.Empty;
1259 public TypeSpec Element { get; private set; }
1261 public override string Name {
1263 throw new NotSupportedException ();
1269 public override ObsoleteAttribute GetAttributeObsolete ()
1271 return Element.GetAttributeObsolete ();
1274 protected virtual string GetPostfixSignature ()
1279 public override string GetSignatureForError ()
1281 return Element.GetSignatureForError () + GetPostfixSignature ();
1284 public override TypeSpec Mutate (TypeParameterMutator mutator)
1286 var me = Element.Mutate (mutator);
1290 var mutated = (ElementTypeSpec) MemberwiseClone ();
1291 mutated.Element = me;
1292 mutated.info = null;
1296 #region ITypeDefinition Members
1298 IAssemblyDefinition ITypeDefinition.DeclaringAssembly {
1300 return Element.MemberDefinition.DeclaringAssembly;
1304 bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
1306 return Element.MemberDefinition.IsInternalAsPublic (assembly);
1309 public string Namespace {
1310 get { throw new NotImplementedException (); }
1313 public int TypeParametersCount {
1319 public TypeParameterSpec[] TypeParameters {
1321 throw new NotSupportedException ();
1325 public TypeSpec GetAttributeCoClass ()
1327 return Element.MemberDefinition.GetAttributeCoClass ();
1330 public string GetAttributeDefaultMember ()
1332 return Element.MemberDefinition.GetAttributeDefaultMember ();
1335 public void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
1337 Element.MemberDefinition.LoadMembers (declaringType, onlyTypes, ref cache);
1340 public bool IsImported {
1342 return Element.MemberDefinition.IsImported;
1346 public string[] ConditionalConditions ()
1348 return Element.MemberDefinition.ConditionalConditions ();
1351 bool? IMemberDefinition.CLSAttributeValue {
1353 return Element.MemberDefinition.CLSAttributeValue;
1357 public void SetIsAssigned ()
1359 Element.MemberDefinition.SetIsAssigned ();
1362 public void SetIsUsed ()
1364 Element.MemberDefinition.SetIsUsed ();
1370 public class ArrayContainer : ElementTypeSpec
1372 public struct TypeRankPair : IEquatable<TypeRankPair>
1377 public TypeRankPair (TypeSpec ts, int rank)
1383 public override int GetHashCode ()
1385 return ts.GetHashCode () ^ rank.GetHashCode ();
1388 #region IEquatable<Tuple<T1,T2>> Members
1390 public bool Equals (TypeRankPair other)
1392 return other.ts == ts && other.rank == rank;
1399 readonly ModuleContainer module;
1401 private ArrayContainer (ModuleContainer module, TypeSpec element, int rank)
1402 : base (MemberKind.ArrayType, element, null)
1404 this.module = module;
1414 public MethodInfo GetConstructor ()
1416 var mb = module.Builder;
1418 var arg_types = new MetaType[rank];
1419 for (int i = 0; i < rank; i++)
1420 arg_types[i] = module.Compiler.BuildinTypes.Int.GetMetaInfo ();
1422 var ctor = mb.GetArrayMethod (
1423 GetMetaInfo (), Constructor.ConstructorName,
1424 CallingConventions.HasThis,
1430 public MethodInfo GetAddressMethod ()
1432 var mb = module.Builder;
1434 var arg_types = new MetaType[rank];
1435 for (int i = 0; i < rank; i++)
1436 arg_types[i] = module.Compiler.BuildinTypes.Int.GetMetaInfo ();
1438 var address = mb.GetArrayMethod (
1439 GetMetaInfo (), "Address",
1440 CallingConventions.HasThis | CallingConventions.Standard,
1441 ReferenceContainer.MakeType (Element).GetMetaInfo (), arg_types);
1446 public MethodInfo GetGetMethod ()
1448 var mb = module.Builder;
1450 var arg_types = new MetaType[rank];
1451 for (int i = 0; i < rank; i++)
1452 arg_types[i] = module.Compiler.BuildinTypes.Int.GetMetaInfo ();
1454 var get = mb.GetArrayMethod (
1455 GetMetaInfo (), "Get",
1456 CallingConventions.HasThis | CallingConventions.Standard,
1457 Element.GetMetaInfo (), arg_types);
1462 public MethodInfo GetSetMethod ()
1464 var mb = module.Builder;
1466 var arg_types = new MetaType[rank + 1];
1467 for (int i = 0; i < rank; i++)
1468 arg_types[i] = module.Compiler.BuildinTypes.Int.GetMetaInfo ();
1470 arg_types[rank] = Element.GetMetaInfo ();
1472 var set = mb.GetArrayMethod (
1473 GetMetaInfo (), "Set",
1474 CallingConventions.HasThis | CallingConventions.Standard,
1475 module.Compiler.BuildinTypes.Void.GetMetaInfo (), arg_types);
1480 public override MetaType GetMetaInfo ()
1484 info = Element.GetMetaInfo ().MakeArrayType ();
1486 info = Element.GetMetaInfo ().MakeArrayType (rank);
1492 protected override string GetPostfixSignature()
1494 return GetPostfixSignature (rank);
1497 public static string GetPostfixSignature (int rank)
1499 StringBuilder sb = new StringBuilder ();
1501 for (int i = 1; i < rank; i++) {
1506 return sb.ToString ();
1509 public static ArrayContainer MakeType (ModuleContainer module, TypeSpec element)
1511 return MakeType (module, element, 1);
1514 public static ArrayContainer MakeType (ModuleContainer module, TypeSpec element, int rank)
1517 var key = new TypeRankPair (element, rank);
1518 if (!module.ArraysCache.TryGetValue (key, out ac)) {
1519 ac = new ArrayContainer (module, element, rank) {
1520 BaseType = module.Compiler.BuildinTypes.Array
1523 module.ArraysCache.Add (key, ac);
1530 class ReferenceContainer : ElementTypeSpec
1532 static Dictionary<TypeSpec, ReferenceContainer> instances = new Dictionary<TypeSpec, ReferenceContainer> ();
1534 private ReferenceContainer (TypeSpec element)
1535 : base (MemberKind.Class, element, null) // TODO: Kind.Class is most likely wrong
1539 public override MetaType GetMetaInfo ()
1542 info = Element.GetMetaInfo ().MakeByRefType ();
1548 public static ReferenceContainer MakeType (TypeSpec element)
1550 ReferenceContainer pc;
1551 if (!instances.TryGetValue (element, out pc)) {
1552 pc = new ReferenceContainer (element);
1553 instances.Add (element, pc);
1559 public static void Reset ()
1561 instances = new Dictionary<TypeSpec, ReferenceContainer> ();
1565 class PointerContainer : ElementTypeSpec
1567 static Dictionary<TypeSpec, PointerContainer> instances = new Dictionary<TypeSpec, PointerContainer> ();
1569 private PointerContainer (TypeSpec element)
1570 : base (MemberKind.PointerType, element, null)
1572 // It's never CLS-Compliant
1573 state &= ~StateFlags.CLSCompliant_Undetected;
1576 public override MetaType GetMetaInfo ()
1579 info = Element.GetMetaInfo ().MakePointerType ();
1585 protected override string GetPostfixSignature()
1590 public static PointerContainer MakeType (TypeSpec element)
1592 PointerContainer pc;
1593 if (!instances.TryGetValue (element, out pc)) {
1594 pc = new PointerContainer (element);
1595 instances.Add (element, pc);
1601 public static void Reset ()
1603 instances = new Dictionary<TypeSpec, PointerContainer> ();