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;
172 return Kind == MemberKind.Enum;
176 // TODO: Should probably do
177 // IsGenericType -- recursive
178 // HasTypeParameter -- non-recursive
179 public bool IsGenericOrParentIsGeneric {
185 ts = ts.declaringType;
186 } while (ts != null);
192 public bool IsGenericParameter {
193 get { return Kind == MemberKind.TypeParameter; }
196 public bool IsNested {
197 get { return declaringType != null && Kind != MemberKind.TypeParameter; }
200 public bool IsPointer {
202 return Kind == MemberKind.PointerType;
206 public bool IsSealed {
207 get { return (Modifiers & Modifiers.SEALED) != 0; }
210 public bool IsSpecialRuntimeType {
212 return (state & StateFlags.SpecialRuntimeType) != 0;
215 state = value ? state | StateFlags.SpecialRuntimeType : state & ~StateFlags.SpecialRuntimeType;
219 public bool IsStruct {
221 return Kind == MemberKind.Struct;
225 public bool IsTypeBuilder {
230 var meta = GetMetaInfo().GetType ();
231 return meta == TypeBuilder || meta == GenericTypeBuilder;
236 public MemberCache MemberCache {
238 if (cache == null || (state & StateFlags.PendingMemberCacheMembers) != 0)
239 InitializeMemberCache (false);
245 throw new InternalErrorException ("Membercache reset");
251 public MemberCache MemberCacheTypes {
254 InitializeMemberCache (true);
260 public new ITypeDefinition MemberDefinition {
262 return (ITypeDefinition) definition;
266 // TODO: Wouldn't be better to rely on cast to InflatedTypeSpec and
267 // remove the property, YES IT WOULD !!!
268 public virtual TypeSpec[] TypeArguments {
269 get { return TypeSpec.EmptyTypes; }
274 public bool AddInterface (TypeSpec iface)
276 if ((state & StateFlags.InterfacesExpanded) != 0)
277 throw new InternalErrorException ("Modifying expanded interface list");
279 if (ifaces == null) {
280 ifaces = new List<TypeSpec> () { iface };
284 if (!ifaces.Contains (iface)) {
292 public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
294 if (Kind != MemberKind.Class)
295 throw new InternalErrorException ();
298 return Attribute.DefaultUsageAttribute;
300 AttributeUsageAttribute aua = null;
302 while (type != null) {
303 aua = type.MemberDefinition.GetAttributeUsage (pa);
307 type = type.BaseType;
313 public virtual MetaType GetMetaInfo ()
318 public virtual TypeSpec GetDefinition ()
323 public override string GetSignatureForError ()
328 s = DeclaringType.GetSignatureForError ();
329 } else if (MemberDefinition is AnonymousTypeClass) {
330 return ((AnonymousTypeClass) MemberDefinition).GetSignatureForError ();
332 s = MemberDefinition.Namespace;
335 if (!string.IsNullOrEmpty (s))
338 return s + Name + GetTypeNameSignature ();
341 protected virtual string GetTypeNameSignature ()
346 return "<" + TypeManager.CSharpName (MemberDefinition.TypeParameters) + ">";
349 public bool ImplementsInterface (TypeSpec iface, bool variantly)
353 if (t.Interfaces != null) {
354 foreach (TypeSpec i in t.Interfaces) {
355 if (i == iface || TypeSpecComparer.IsEqual (i, iface))
358 if (variantly && TypeSpecComparer.Variant.IsEqual (i, iface))
363 // TODO: Why is it needed when we do it during import
370 protected virtual void InitializeMemberCache (bool onlyTypes)
373 MemberDefinition.LoadMembers (this, onlyTypes, ref cache);
374 } catch (Exception e) {
375 throw new InternalErrorException (e, "Unexpected error when loading type `{0}'", GetSignatureForError ());
379 state |= StateFlags.PendingMemberCacheMembers;
381 state &= ~StateFlags.PendingMemberCacheMembers;
385 // Is @baseClass base implementation of @type. With enabled @dynamicIsEqual the slower
386 // comparison is used to hide differences between `object' and `dynamic' for generic
387 // types. Should not be used for comparisons where G<object> != G<dynamic>
389 public static bool IsBaseClass (TypeSpec type, TypeSpec baseClass, bool dynamicIsObject)
391 if (dynamicIsObject && baseClass.IsGeneric) {
393 // Returns true for a hierarchies like this when passing baseClass of A<dynamic>
395 // class B : A<object> {}
397 type = type.BaseType;
398 while (type != null) {
399 if (TypeSpecComparer.IsEqual (type, baseClass))
402 type = type.BaseType;
408 while (type != null) {
409 type = type.BaseType;
410 if (type == baseClass)
417 public override MemberSpec InflateMember (TypeParameterInflator inflator)
419 var targs = IsGeneric ? MemberDefinition.TypeParameters : TypeSpec.EmptyTypes;
422 // When inflating nested type from inside the type instance will be same
423 // because type parameters are same for all nested types
425 if (DeclaringType == inflator.TypeInstance) {
426 return MakeGenericType (inflator.Context, targs);
429 return new InflatedTypeSpec (inflator.Context, this, inflator.TypeInstance, targs);
432 public InflatedTypeSpec MakeGenericType (IModuleContext context, TypeSpec[] targs)
434 if (targs.Length == 0 && !IsNested)
435 throw new ArgumentException ("Empty type arguments for type " + GetSignatureForError ());
437 InflatedTypeSpec instance;
439 if (inflated_instances == null) {
440 inflated_instances = new Dictionary<TypeSpec[], InflatedTypeSpec> (TypeSpecComparer.Default);
443 instance = this as InflatedTypeSpec;
444 if (instance != null) {
446 // Nested types could be inflated on already inflated instances
447 // Caching this type ensured we are using same instance for
448 // inside/outside inflation using local type parameters
450 inflated_instances.Add (TypeArguments, instance);
455 if (!inflated_instances.TryGetValue (targs, out instance)) {
456 if (GetDefinition () != this && !IsNested)
457 throw new InternalErrorException ("`{0}' must be type definition or nested non-inflated type to MakeGenericType",
458 GetSignatureForError ());
460 instance = new InflatedTypeSpec (context, this, declaringType, targs);
461 inflated_instances.Add (targs, instance);
467 public virtual TypeSpec Mutate (TypeParameterMutator mutator)
472 public override List<TypeSpec> ResolveMissingDependencies ()
474 List<TypeSpec> missing = null;
476 if (Kind == MemberKind.MissingType) {
477 missing = new List<TypeSpec> ();
482 foreach (var targ in TypeArguments) {
483 if (targ.Kind == MemberKind.MissingType) {
485 missing = new List<TypeSpec> ();
491 if (Interfaces != null) {
492 foreach (var iface in Interfaces) {
493 if (iface.Kind == MemberKind.MissingType) {
495 missing = new List<TypeSpec> ();
502 if (missing != null || BaseType == null)
505 return BaseType.ResolveMissingDependencies ();
508 public void SetMetaInfo (MetaType info)
510 if (this.info != null)
511 throw new InternalErrorException ("MetaInfo reset");
516 public void SetExtensionMethodContainer ()
518 modifiers |= Modifiers.METHOD_EXTENSION;
522 public sealed class BuildinTypeSpec : TypeSpec
528 // Ordered carefully for fast compares
556 MulticastDelegate = 23,
571 readonly string name;
573 public BuildinTypeSpec (MemberKind kind, string ns, string name, Type buildinKind)
574 : base (kind, null, null, null, Modifiers.PUBLIC)
576 this.type = buildinKind;
581 public BuildinTypeSpec (string name, Type buildinKind)
582 : this (MemberKind.InternalCompilerType, "", name, buildinKind)
584 // Make all internal types CLS-compliant, non-obsolete, compact
585 state = (state & ~(StateFlags.CLSCompliant_Undetected | StateFlags.Obsolete_Undetected | StateFlags.MissingDependency_Undetected)) | StateFlags.CLSCompliant;
590 public override int Arity {
596 public override BuildinTypeSpec.Type BuildinType {
602 public string FullName {
604 return ns + '.' + name;
608 public override string Name {
614 public string Namespace {
622 public static bool IsPrimitiveType (TypeSpec type)
624 return type.BuildinType >= Type.FirstPrimitive && type.BuildinType <= Type.LastPrimitive;
627 public static bool IsPrimitiveTypeOrDecimal (TypeSpec type)
629 return type.BuildinType >= Type.FirstPrimitive && type.BuildinType <= Type.Decimal;
632 public override string GetSignatureForError ()
635 case "Int32": return "int";
636 case "Int64": return "long";
637 case "String": return "string";
638 case "Boolean": return "bool";
639 case "Void": return "void";
640 case "Object": return "object";
641 case "UInt32": return "uint";
642 case "Int16": return "short";
643 case "UInt16": return "ushort";
644 case "UInt64": return "ulong";
645 case "Single": return "float";
646 case "Double": return "double";
647 case "Decimal": return "decimal";
648 case "Char": return "char";
649 case "Byte": return "byte";
650 case "SByte": return "sbyte";
660 // Returns the size of type if known, otherwise, 0
662 public static int GetSize (TypeSpec type)
664 switch (type.BuildinType) {
688 public void SetDefinition (ITypeDefinition td, MetaType type, Modifiers mod)
690 this.definition = td;
692 this.modifiers |= (mod & ~Modifiers.AccessibilityMask);
695 public void SetDefinition (TypeSpec ts)
697 this.definition = ts.MemberDefinition;
698 this.info = ts.GetMetaInfo ();
699 this.BaseType = ts.BaseType;
700 this.Interfaces = ts.Interfaces;
701 this.modifiers = ts.Modifiers;
705 static class TypeSpecComparer
708 // Does strict reference comparion only
710 public static readonly DefaultImpl Default = new DefaultImpl ();
712 public class DefaultImpl : IEqualityComparer<TypeSpec[]>
714 #region IEqualityComparer<TypeSpec[]> Members
716 bool IEqualityComparer<TypeSpec[]>.Equals (TypeSpec[] x, TypeSpec[] y)
721 if (x.Length != y.Length)
724 for (int i = 0; i < x.Length; ++i)
731 int IEqualityComparer<TypeSpec[]>.GetHashCode (TypeSpec[] obj)
734 for (int i = 0; i < obj.Length; ++i)
735 hash = (hash << 5) - hash + obj[i].GetHashCode ();
744 // When comparing type signature of overrides or overloads
745 // this version tolerates different MVARs at same position
747 public static class Override
749 public static bool IsEqual (TypeSpec a, TypeSpec b)
755 // Consider the following example:
757 // public abstract class A
759 // public abstract T Foo<T>();
762 // public class B : A
764 // public override U Foo<T>() { return default (U); }
767 // Here, `T' and `U' are method type parameters from different methods
768 // (A.Foo and B.Foo), so both `==' and Equals() will fail.
770 // However, since we're determining whether B.Foo() overrides A.Foo(),
771 // we need to do a signature based comparision and consider them equal.
774 var tp_a = a as TypeParameterSpec;
776 var tp_b = b as TypeParameterSpec;
777 return tp_b != null && tp_a.IsMethodOwned == tp_b.IsMethodOwned && tp_a.DeclaredPosition == tp_b.DeclaredPosition;
780 var ac_a = a as ArrayContainer;
782 var ac_b = b as ArrayContainer;
783 return ac_b != null && ac_a.Rank == ac_b.Rank && IsEqual (ac_a.Element, ac_b.Element);
786 if (a.BuildinType == BuildinTypeSpec.Type.Dynamic || b.BuildinType == BuildinTypeSpec.Type.Dynamic)
787 return b.BuildinType == BuildinTypeSpec.Type.Object || a.BuildinType == BuildinTypeSpec.Type.Object;
789 if (a.MemberDefinition != b.MemberDefinition)
793 for (int i = 0; i < a.TypeArguments.Length; ++i) {
794 if (!IsEqual (a.TypeArguments[i], b.TypeArguments[i]))
806 // Compares unordered arrays
808 public static bool IsSame (TypeSpec[] a, TypeSpec[] b)
813 if (a == null || b == null || a.Length != b.Length)
816 for (int ai = 0; ai < a.Length; ++ai) {
818 for (int bi = 0; bi < b.Length; ++bi) {
819 if (IsEqual (a[ai], b[bi])) {
832 public static bool IsEqual (AParametersCollection a, AParametersCollection b)
837 if (a.Count != b.Count)
840 for (int i = 0; i < a.Count; ++i) {
841 if (!IsEqual (a.Types[i], b.Types[i]))
844 const Parameter.Modifier ref_out = Parameter.Modifier.REF | Parameter.Modifier.OUT;
845 if ((a.FixedParameters[i].ModFlags & ref_out) != (b.FixedParameters[i].ModFlags & ref_out))
854 // Type variance equality comparison
856 public static class Variant
858 public static bool IsEqual (TypeSpec type1, TypeSpec type2)
860 if (!type1.IsGeneric || !type2.IsGeneric)
863 var target_type_def = type2.MemberDefinition;
864 if (type1.MemberDefinition != target_type_def)
867 var t1_targs = type1.TypeArguments;
868 var t2_targs = type2.TypeArguments;
869 var targs_definition = target_type_def.TypeParameters;
871 if (!type1.IsInterface && !type1.IsDelegate) {
875 for (int i = 0; i < targs_definition.Length; ++i) {
876 Variance v = targs_definition[i].Variance;
877 if (v == Variance.None) {
878 if (t1_targs[i] == t2_targs[i])
883 if (v == Variance.Covariant) {
884 if (!Convert.ImplicitReferenceConversionExists (new EmptyExpression (t1_targs[i]), t2_targs[i]))
886 } else if (!Convert.ImplicitReferenceConversionExists (new EmptyExpression (t2_targs[i]), t1_targs[i])) {
896 // Checks whether two generic instances may become equal for some
897 // particular instantiation (26.3.1).
899 public static class Unify
902 // Either @a or @b must be generic type
904 public static bool IsEqual (TypeSpec a, TypeSpec b)
906 if (a.MemberDefinition != b.MemberDefinition)
909 var ta = a.TypeArguments;
910 var tb = b.TypeArguments;
911 for (int i = 0; i < ta.Length; i++) {
912 if (!MayBecomeEqualGenericTypes (ta[i], tb[i]))
919 static bool ContainsTypeParameter (TypeSpec tparam, TypeSpec type)
921 TypeSpec[] targs = type.TypeArguments;
922 for (int i = 0; i < targs.Length; i++) {
923 if (tparam == targs[i])
926 if (ContainsTypeParameter (tparam, targs[i]))
934 /// Check whether `a' and `b' may become equal generic types.
935 /// The algorithm to do that is a little bit complicated.
937 static bool MayBecomeEqualGenericTypes (TypeSpec a, TypeSpec b)
939 if (a.IsGenericParameter) {
941 // If a is an array of a's type, they may never
948 // If b is a generic parameter or an actual type,
949 // they may become equal:
951 // class X<T,U> : I<T>, I<U>
952 // class X<T> : I<T>, I<float>
954 if (b.IsGenericParameter)
955 return a.DeclaringType == b.DeclaringType;
958 // We're now comparing a type parameter with a
959 // generic instance. They may become equal unless
960 // the type parameter appears anywhere in the
963 // class X<T,U> : I<T>, I<X<U>>
964 // -> error because you could instanciate it as
967 // class X<T> : I<T>, I<X<T>> -> ok
970 return !ContainsTypeParameter (a, b);
973 if (b.IsGenericParameter)
974 return MayBecomeEqualGenericTypes (b, a);
977 // At this point, neither a nor b are a type parameter.
979 // If one of them is a generic instance, compare them (if the
980 // other one is not a generic instance, they can never
983 if (TypeManager.IsGenericType (a) || TypeManager.IsGenericType (b))
984 return IsEqual (a, b);
987 // If both of them are arrays.
989 var a_ac = a as ArrayContainer;
991 var b_ac = b as ArrayContainer;
992 if (b_ac == null || a_ac.Rank != b_ac.Rank)
995 return MayBecomeEqualGenericTypes (a_ac.Element, b_ac.Element);
999 // Ok, two ordinary types.
1005 public static bool Equals (TypeSpec[] x, TypeSpec[] y)
1010 if (x.Length != y.Length)
1013 for (int i = 0; i < x.Length; ++i)
1014 if (!IsEqual (x[i], y[i]))
1021 // Identity type conversion
1023 // Default reference comparison, it has to be used when comparing
1024 // two possible dynamic/internal types
1026 public static bool IsEqual (TypeSpec a, TypeSpec b)
1029 // This also rejects dynamic == dynamic
1030 return a.Kind != MemberKind.InternalCompilerType || a.BuildinType == BuildinTypeSpec.Type.Dynamic;
1033 if (a == null || b == null)
1037 var a_a = (ArrayContainer) a;
1038 var b_a = b as ArrayContainer;
1042 return IsEqual (a_a.Element, b_a.Element) && a_a.Rank == b_a.Rank;
1045 if (!a.IsGeneric || !b.IsGeneric) {
1047 // object and dynamic are considered equivalent there is an identity conversion
1048 // between object and dynamic, and between constructed types that are the same
1049 // when replacing all occurences of dynamic with object.
1051 if (a.BuildinType == BuildinTypeSpec.Type.Dynamic || b.BuildinType == BuildinTypeSpec.Type.Dynamic)
1052 return b.BuildinType == BuildinTypeSpec.Type.Object || a.BuildinType == BuildinTypeSpec.Type.Object;
1057 if (a.MemberDefinition != b.MemberDefinition)
1061 if (!Equals (a.TypeArguments, b.TypeArguments))
1064 a = a.DeclaringType;
1065 b = b.DeclaringType;
1066 } while (a != null);
1072 public interface ITypeDefinition : IMemberDefinition
1074 IAssemblyDefinition DeclaringAssembly { get; }
1075 string Namespace { get; }
1076 int TypeParametersCount { get; }
1077 TypeParameterSpec[] TypeParameters { get; }
1079 TypeSpec GetAttributeCoClass ();
1080 string GetAttributeDefaultMember ();
1081 AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa);
1082 bool IsInternalAsPublic (IAssemblyDefinition assembly);
1083 void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache);
1086 class InternalType : TypeSpec, ITypeDefinition
1088 public static readonly InternalType AnonymousMethod = new InternalType ("anonymous method");
1089 public static readonly InternalType Arglist = new InternalType ("__arglist");
1090 public static readonly InternalType MethodGroup = new InternalType ("method group");
1091 public static readonly InternalType NullLiteral = new InternalType ("null");
1092 public static readonly InternalType FakeInternalType = new InternalType ("<fake$type>");
1094 readonly string name;
1096 InternalType (string name, MemberCache cache)
1102 InternalType (string name)
1103 : base (MemberKind.InternalCompilerType, null, null, null, Modifiers.PUBLIC)
1106 this.definition = this;
1107 cache = MemberCache.Empty;
1109 // Make all internal types CLS-compliant, non-obsolete
1110 state = (state & ~(StateFlags.CLSCompliant_Undetected | StateFlags.Obsolete_Undetected)) | StateFlags.CLSCompliant;
1115 public override int Arity {
1121 IAssemblyDefinition ITypeDefinition.DeclaringAssembly {
1123 throw new NotImplementedException ();
1127 bool IMemberDefinition.IsImported {
1133 public override string Name {
1139 string ITypeDefinition.Namespace {
1145 int ITypeDefinition.TypeParametersCount {
1151 TypeParameterSpec[] ITypeDefinition.TypeParameters {
1159 public override string GetSignatureForError ()
1164 #region ITypeDefinition Members
1166 TypeSpec ITypeDefinition.GetAttributeCoClass ()
1171 string ITypeDefinition.GetAttributeDefaultMember ()
1176 AttributeUsageAttribute ITypeDefinition.GetAttributeUsage (PredefinedAttribute pa)
1181 bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
1183 throw new NotImplementedException ();
1186 void ITypeDefinition.LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
1188 throw new NotImplementedException ();
1191 string[] IMemberDefinition.ConditionalConditions ()
1196 ObsoleteAttribute IMemberDefinition.GetAttributeObsolete ()
1201 bool? IMemberDefinition.CLSAttributeValue {
1207 void IMemberDefinition.SetIsAssigned ()
1211 void IMemberDefinition.SetIsUsed ()
1218 public abstract class ElementTypeSpec : TypeSpec, ITypeDefinition
1220 protected ElementTypeSpec (MemberKind kind, TypeSpec element, MetaType info)
1221 : base (kind, element.DeclaringType, null, info, element.Modifiers)
1223 this.Element = element;
1225 // Some flags can be copied directly from the element
1226 const StateFlags shared_flags = StateFlags.CLSCompliant | StateFlags.CLSCompliant_Undetected
1227 | StateFlags.Obsolete | StateFlags.Obsolete_Undetected | StateFlags.HasDynamicElement;
1228 state &= ~shared_flags;
1229 state |= (element.state & shared_flags);
1231 if (element.BuildinType == BuildinTypeSpec.Type.Dynamic)
1232 state |= StateFlags.HasDynamicElement;
1234 // Has to use its own type definition instead of just element definition to
1235 // correctly identify itself for cases like x.MemberDefininition == predefined.MemberDefinition
1236 this.definition = this;
1238 cache = MemberCache.Empty;
1243 public TypeSpec Element { get; private set; }
1245 public override string Name {
1247 throw new NotSupportedException ();
1253 public override ObsoleteAttribute GetAttributeObsolete ()
1255 return Element.GetAttributeObsolete ();
1258 protected virtual string GetPostfixSignature ()
1263 public override string GetSignatureForError ()
1265 return Element.GetSignatureForError () + GetPostfixSignature ();
1268 public override TypeSpec Mutate (TypeParameterMutator mutator)
1270 var me = Element.Mutate (mutator);
1274 var mutated = (ElementTypeSpec) MemberwiseClone ();
1275 mutated.Element = me;
1276 mutated.info = null;
1280 #region ITypeDefinition Members
1282 IAssemblyDefinition ITypeDefinition.DeclaringAssembly {
1284 return Element.MemberDefinition.DeclaringAssembly;
1288 bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
1290 return Element.MemberDefinition.IsInternalAsPublic (assembly);
1293 public string Namespace {
1294 get { throw new NotImplementedException (); }
1297 public int TypeParametersCount {
1303 public TypeParameterSpec[] TypeParameters {
1305 throw new NotSupportedException ();
1309 public TypeSpec GetAttributeCoClass ()
1311 return Element.MemberDefinition.GetAttributeCoClass ();
1314 public string GetAttributeDefaultMember ()
1316 return Element.MemberDefinition.GetAttributeDefaultMember ();
1319 public void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
1321 Element.MemberDefinition.LoadMembers (declaringType, onlyTypes, ref cache);
1324 public bool IsImported {
1326 return Element.MemberDefinition.IsImported;
1330 public string[] ConditionalConditions ()
1332 return Element.MemberDefinition.ConditionalConditions ();
1335 bool? IMemberDefinition.CLSAttributeValue {
1337 return Element.MemberDefinition.CLSAttributeValue;
1341 public void SetIsAssigned ()
1343 Element.MemberDefinition.SetIsAssigned ();
1346 public void SetIsUsed ()
1348 Element.MemberDefinition.SetIsUsed ();
1354 public class ArrayContainer : ElementTypeSpec
1356 public struct TypeRankPair : IEquatable<TypeRankPair>
1361 public TypeRankPair (TypeSpec ts, int rank)
1367 public override int GetHashCode ()
1369 return ts.GetHashCode () ^ rank.GetHashCode ();
1372 #region IEquatable<Tuple<T1,T2>> Members
1374 public bool Equals (TypeRankPair other)
1376 return other.ts == ts && other.rank == rank;
1383 readonly ModuleContainer module;
1385 private ArrayContainer (ModuleContainer module, TypeSpec element, int rank)
1386 : base (MemberKind.ArrayType, element, null)
1388 this.module = module;
1398 public MethodInfo GetConstructor ()
1400 var mb = module.Builder;
1402 var arg_types = new MetaType[rank];
1403 for (int i = 0; i < rank; i++)
1404 arg_types[i] = module.Compiler.BuildinTypes.Int.GetMetaInfo ();
1406 var ctor = mb.GetArrayMethod (
1407 GetMetaInfo (), Constructor.ConstructorName,
1408 CallingConventions.HasThis,
1414 public MethodInfo GetAddressMethod ()
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 address = mb.GetArrayMethod (
1423 GetMetaInfo (), "Address",
1424 CallingConventions.HasThis | CallingConventions.Standard,
1425 ReferenceContainer.MakeType (Element).GetMetaInfo (), arg_types);
1430 public MethodInfo GetGetMethod ()
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 get = mb.GetArrayMethod (
1439 GetMetaInfo (), "Get",
1440 CallingConventions.HasThis | CallingConventions.Standard,
1441 Element.GetMetaInfo (), arg_types);
1446 public MethodInfo GetSetMethod ()
1448 var mb = module.Builder;
1450 var arg_types = new MetaType[rank + 1];
1451 for (int i = 0; i < rank; i++)
1452 arg_types[i] = module.Compiler.BuildinTypes.Int.GetMetaInfo ();
1454 arg_types[rank] = Element.GetMetaInfo ();
1456 var set = mb.GetArrayMethod (
1457 GetMetaInfo (), "Set",
1458 CallingConventions.HasThis | CallingConventions.Standard,
1459 module.Compiler.BuildinTypes.Void.GetMetaInfo (), arg_types);
1464 public override MetaType GetMetaInfo ()
1468 info = Element.GetMetaInfo ().MakeArrayType ();
1470 info = Element.GetMetaInfo ().MakeArrayType (rank);
1476 protected override string GetPostfixSignature()
1478 return GetPostfixSignature (rank);
1481 public static string GetPostfixSignature (int rank)
1483 StringBuilder sb = new StringBuilder ();
1485 for (int i = 1; i < rank; i++) {
1490 return sb.ToString ();
1493 public static ArrayContainer MakeType (ModuleContainer module, TypeSpec element)
1495 return MakeType (module, element, 1);
1498 public static ArrayContainer MakeType (ModuleContainer module, TypeSpec element, int rank)
1501 var key = new TypeRankPair (element, rank);
1502 if (!module.ArraysCache.TryGetValue (key, out ac)) {
1503 ac = new ArrayContainer (module, element, rank) {
1504 BaseType = module.Compiler.BuildinTypes.Array
1507 module.ArraysCache.Add (key, ac);
1514 class ReferenceContainer : ElementTypeSpec
1516 static Dictionary<TypeSpec, ReferenceContainer> instances = new Dictionary<TypeSpec, ReferenceContainer> ();
1518 private ReferenceContainer (TypeSpec element)
1519 : base (MemberKind.Class, element, null) // TODO: Kind.Class is most likely wrong
1523 public override MetaType GetMetaInfo ()
1526 info = Element.GetMetaInfo ().MakeByRefType ();
1532 public static ReferenceContainer MakeType (TypeSpec element)
1534 ReferenceContainer pc;
1535 if (!instances.TryGetValue (element, out pc)) {
1536 pc = new ReferenceContainer (element);
1537 instances.Add (element, pc);
1543 public static void Reset ()
1545 instances = new Dictionary<TypeSpec, ReferenceContainer> ();
1549 class PointerContainer : ElementTypeSpec
1551 static Dictionary<TypeSpec, PointerContainer> instances = new Dictionary<TypeSpec, PointerContainer> ();
1553 private PointerContainer (TypeSpec element)
1554 : base (MemberKind.PointerType, element, null)
1556 // It's never CLS-Compliant
1557 state &= ~StateFlags.CLSCompliant_Undetected;
1560 public override MetaType GetMetaInfo ()
1563 info = Element.GetMetaInfo ().MakePointerType ();
1569 protected override string GetPostfixSignature()
1574 public static PointerContainer MakeType (TypeSpec element)
1576 PointerContainer pc;
1577 if (!instances.TryGetValue (element, out pc)) {
1578 pc = new PointerContainer (element);
1579 instances.Add (element, pc);
1585 public static void Reset ()
1587 instances = new Dictionary<TypeSpec, PointerContainer> ();