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
9 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
13 using System.Collections.Generic;
17 using MetaType = IKVM.Reflection.Type;
18 using IKVM.Reflection;
20 using MetaType = System.Type;
21 using System.Reflection;
26 public class TypeSpec : MemberSpec
28 protected MetaType info;
29 protected MemberCache cache;
30 protected IList<TypeSpec> ifaces;
33 Dictionary<TypeSpec[], InflatedTypeSpec> inflated_instances;
35 public static readonly TypeSpec[] EmptyTypes = new TypeSpec[0];
38 // Reflection Emit hacking
39 static readonly Type TypeBuilder;
40 static readonly Type GenericTypeBuilder;
44 var assembly = typeof (object).Assembly;
45 TypeBuilder = assembly.GetType ("System.Reflection.Emit.TypeBuilder");
46 GenericTypeBuilder = assembly.GetType ("System.Reflection.MonoGenericClass");
47 if (GenericTypeBuilder == null)
48 GenericTypeBuilder = assembly.GetType ("System.Reflection.Emit.TypeBuilderInstantiation");
52 public TypeSpec (MemberKind kind, TypeSpec declaringType, ITypeDefinition definition, MetaType info, Modifiers modifiers)
53 : base (kind, declaringType, definition, modifiers)
55 this.declaringType = declaringType;
58 if (definition != null && definition.TypeParametersCount > 0)
59 state |= StateFlags.IsGeneric;
64 public override int Arity {
66 return MemberDefinition.TypeParametersCount;
70 public virtual TypeSpec BaseType {
79 public virtual BuiltinTypeSpec.Type BuiltinType {
81 return BuiltinTypeSpec.Type.None;
85 public bool HasDynamicElement {
87 return (state & StateFlags.HasDynamicElement) != 0;
91 public virtual IList<TypeSpec> Interfaces {
100 public bool IsArray {
102 return Kind == MemberKind.ArrayType;
106 public bool IsAttribute {
113 if (type.BuiltinType == BuiltinTypeSpec.Type.Attribute)
119 type = type.base_type;
120 } while (type != null);
126 public bool IsInterface {
128 return Kind == MemberKind.Interface;
132 public bool IsClass {
134 return Kind == MemberKind.Class;
138 public bool IsConstantCompatible {
140 if ((Kind & (MemberKind.Enum | MemberKind.Class | MemberKind.Interface | MemberKind.Delegate | MemberKind.ArrayType)) != 0)
143 switch (BuiltinType) {
144 case BuiltinTypeSpec.Type.Int:
145 case BuiltinTypeSpec.Type.UInt:
146 case BuiltinTypeSpec.Type.Long:
147 case BuiltinTypeSpec.Type.ULong:
148 case BuiltinTypeSpec.Type.Float:
149 case BuiltinTypeSpec.Type.Double:
150 case BuiltinTypeSpec.Type.Char:
151 case BuiltinTypeSpec.Type.Short:
152 case BuiltinTypeSpec.Type.Decimal:
153 case BuiltinTypeSpec.Type.Bool:
154 case BuiltinTypeSpec.Type.SByte:
155 case BuiltinTypeSpec.Type.Byte:
156 case BuiltinTypeSpec.Type.UShort:
157 case BuiltinTypeSpec.Type.Dynamic:
165 public bool IsDelegate {
167 return Kind == MemberKind.Delegate;
172 // Returns true for instances of Expression<T>
174 public virtual bool IsExpressionTreeType {
179 state = value ? state | StateFlags.InflatedExpressionType : state & ~StateFlags.InflatedExpressionType;
185 return Kind == MemberKind.Enum;
190 // Returns true for instances of IList<T>, IEnumerable<T>, ICollection<T>
192 public virtual bool IsGenericIterateInterface {
197 state = value ? state | StateFlags.GenericIterateInterface : state & ~StateFlags.GenericIterateInterface;
202 // Returns true for instances of System.Threading.Tasks.Task<T>
204 public virtual bool IsGenericTask {
209 state = value ? state | StateFlags.GenericTask : state & ~StateFlags.GenericTask;
213 // TODO: Should probably do
214 // IsGenericType -- recursive
215 // HasTypeParameter -- non-recursive
216 public bool IsGenericOrParentIsGeneric {
222 ts = ts.declaringType;
223 } while (ts != null);
229 public bool IsGenericParameter {
231 return Kind == MemberKind.TypeParameter;
236 // Returns true for instances of Nullable<T>
238 public virtual bool IsNullableType {
243 state = value ? state | StateFlags.InflatedNullableType : state & ~StateFlags.InflatedNullableType;
247 public bool IsNested {
248 get { return declaringType != null && Kind != MemberKind.TypeParameter; }
251 public bool IsPointer {
253 return Kind == MemberKind.PointerType;
257 public bool IsSealed {
258 get { return (Modifiers & Modifiers.SEALED) != 0; }
261 public bool IsSpecialRuntimeType {
263 return (state & StateFlags.SpecialRuntimeType) != 0;
266 state = value ? state | StateFlags.SpecialRuntimeType : state & ~StateFlags.SpecialRuntimeType;
270 public bool IsStruct {
272 return Kind == MemberKind.Struct;
276 public bool IsTypeBuilder {
281 var meta = GetMetaInfo().GetType ();
282 return meta == TypeBuilder || meta == GenericTypeBuilder;
287 public MemberCache MemberCache {
289 if (cache == null || (state & StateFlags.PendingMemberCacheMembers) != 0)
290 InitializeMemberCache (false);
296 throw new InternalErrorException ("Membercache reset");
302 public MemberCache MemberCacheTypes {
305 InitializeMemberCache (true);
311 public new ITypeDefinition MemberDefinition {
313 return (ITypeDefinition) definition;
317 // TODO: Wouldn't be better to rely on cast to InflatedTypeSpec and
318 // remove the property, YES IT WOULD !!!
319 public virtual TypeSpec[] TypeArguments {
320 get { return TypeSpec.EmptyTypes; }
325 public bool AddInterface (TypeSpec iface)
327 if ((state & StateFlags.InterfacesExpanded) != 0)
328 throw new InternalErrorException ("Modifying expanded interface list");
330 if (ifaces == null) {
331 ifaces = new List<TypeSpec> () { iface };
335 if (!ifaces.Contains (iface)) {
343 public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
345 if (Kind != MemberKind.Class)
346 throw new InternalErrorException ();
349 return Attribute.DefaultUsageAttribute;
351 AttributeUsageAttribute aua = null;
353 while (type != null) {
354 aua = type.MemberDefinition.GetAttributeUsage (pa);
358 type = type.BaseType;
364 public virtual MetaType GetMetaInfo ()
369 public virtual TypeSpec GetDefinition ()
374 public override string GetSignatureForDocumentation ()
376 StringBuilder sb = new StringBuilder ();
378 sb.Append (DeclaringType.GetSignatureForDocumentation ());
380 sb.Append (MemberDefinition.Namespace);
388 if (this is InflatedTypeSpec) {
390 for (int i = 0; i < Arity; ++i) {
394 sb.Append (TypeArguments[i].GetSignatureForDocumentation ());
399 sb.Append (Arity.ToString ());
403 return sb.ToString ();
406 public string GetExplicitNameSignatureForDocumentation ()
408 StringBuilder sb = new StringBuilder ();
410 sb.Append (DeclaringType.GetExplicitNameSignatureForDocumentation ());
411 } else if (MemberDefinition.Namespace != null) {
412 sb.Append (MemberDefinition.Namespace.Replace ('.', '#'));
421 for (int i = 0; i < Arity; ++i) {
425 sb.Append (TypeArguments[i].GetExplicitNameSignatureForDocumentation ());
430 return sb.ToString ();
433 public override string GetSignatureForError ()
438 s = DeclaringType.GetSignatureForError ();
439 } else if (MemberDefinition is AnonymousTypeClass) {
440 return ((AnonymousTypeClass) MemberDefinition).GetSignatureForError ();
442 s = MemberDefinition.Namespace;
445 if (!string.IsNullOrEmpty (s))
448 return s + Name + GetTypeNameSignature ();
451 public string GetSignatureForErrorIncludingAssemblyName ()
453 return string.Format ("{0} [{1}]", GetSignatureForError (), MemberDefinition.DeclaringAssembly.FullName);
456 protected virtual string GetTypeNameSignature ()
461 return "<" + TypeManager.CSharpName (MemberDefinition.TypeParameters) + ">";
464 public bool ImplementsInterface (TypeSpec iface, bool variantly)
468 var ifaces = t.Interfaces;
469 if (ifaces != null) {
470 for (int i = 0; i < ifaces.Count; ++i) {
471 if (TypeSpecComparer.IsEqual (ifaces[i], iface))
474 if (variantly && TypeSpecComparer.Variant.IsEqual (ifaces[i], iface))
479 // TODO: Why is it needed when we do it during import
486 protected virtual void InitializeMemberCache (bool onlyTypes)
489 MemberDefinition.LoadMembers (this, onlyTypes, ref cache);
490 } catch (Exception e) {
491 throw new InternalErrorException (e, "Unexpected error when loading type `{0}'", GetSignatureForError ());
495 state |= StateFlags.PendingMemberCacheMembers;
497 state &= ~StateFlags.PendingMemberCacheMembers;
501 // Is @baseClass base implementation of @type. With enabled @dynamicIsEqual the slower
502 // comparison is used to hide differences between `object' and `dynamic' for generic
503 // types. Should not be used for comparisons where G<object> != G<dynamic>
505 public static bool IsBaseClass (TypeSpec type, TypeSpec baseClass, bool dynamicIsObject)
507 if (dynamicIsObject && baseClass.IsGeneric) {
509 // Returns true for a hierarchies like this when passing baseClass of A<dynamic>
511 // class B : A<object> {}
513 type = type.BaseType;
514 while (type != null) {
515 if (TypeSpecComparer.IsEqual (type, baseClass))
518 type = type.BaseType;
524 while (type != null) {
525 type = type.BaseType;
526 if (type == baseClass)
533 public static bool IsReferenceType (TypeSpec t)
536 case MemberKind.TypeParameter:
537 return ((TypeParameterSpec) t).IsReferenceType;
538 case MemberKind.Struct:
539 case MemberKind.Enum:
541 case MemberKind.InternalCompilerType:
543 // Null is considered to be a reference type
545 return t == InternalType.NullLiteral || t.BuiltinType == BuiltinTypeSpec.Type.Dynamic;
551 public static bool IsValueType (TypeSpec t)
554 case MemberKind.TypeParameter:
555 return ((TypeParameterSpec) t).IsValueType;
556 case MemberKind.Struct:
557 case MemberKind.Enum:
564 public override MemberSpec InflateMember (TypeParameterInflator inflator)
566 var targs = IsGeneric ? MemberDefinition.TypeParameters : TypeSpec.EmptyTypes;
569 // When inflating nested type from inside the type instance will be same
570 // because type parameters are same for all nested types
572 if (DeclaringType == inflator.TypeInstance) {
573 return MakeGenericType (inflator.Context, targs);
576 return new InflatedTypeSpec (inflator.Context, this, inflator.TypeInstance, targs);
579 public InflatedTypeSpec MakeGenericType (IModuleContext context, TypeSpec[] targs)
581 if (targs.Length == 0 && !IsNested)
582 throw new ArgumentException ("Empty type arguments for type " + GetSignatureForError ());
584 InflatedTypeSpec instance;
586 if (inflated_instances == null) {
587 inflated_instances = new Dictionary<TypeSpec[], InflatedTypeSpec> (TypeSpecComparer.Default);
590 instance = this as InflatedTypeSpec;
591 if (instance != null) {
593 // Nested types could be inflated on already inflated instances
594 // Caching this type ensured we are using same instance for
595 // inside/outside inflation using local type parameters
597 inflated_instances.Add (TypeArguments, instance);
602 if (!inflated_instances.TryGetValue (targs, out instance)) {
603 if (GetDefinition () != this && !IsNested)
604 throw new InternalErrorException ("`{0}' must be type definition or nested non-inflated type to MakeGenericType",
605 GetSignatureForError ());
607 instance = new InflatedTypeSpec (context, this, declaringType, targs);
608 inflated_instances.Add (targs, instance);
614 public virtual TypeSpec Mutate (TypeParameterMutator mutator)
619 public override List<TypeSpec> ResolveMissingDependencies ()
621 List<TypeSpec> missing = null;
623 if (Kind == MemberKind.MissingType) {
624 missing = new List<TypeSpec> ();
629 foreach (var targ in TypeArguments) {
630 if (targ.Kind == MemberKind.MissingType) {
632 missing = new List<TypeSpec> ();
638 if (Interfaces != null) {
639 foreach (var iface in Interfaces) {
640 if (iface.Kind == MemberKind.MissingType) {
642 missing = new List<TypeSpec> ();
649 if (missing != null || BaseType == null)
652 return BaseType.ResolveMissingDependencies ();
655 public void SetMetaInfo (MetaType info)
657 if (this.info != null)
658 throw new InternalErrorException ("MetaInfo reset");
663 public void SetExtensionMethodContainer ()
665 modifiers |= Modifiers.METHOD_EXTENSION;
669 public sealed class BuiltinTypeSpec : TypeSpec
675 // Ordered carefully for fast compares
703 MulticastDelegate = 23,
716 readonly string name;
718 public BuiltinTypeSpec (MemberKind kind, string ns, string name, Type builtinKind)
719 : base (kind, null, null, null, Modifiers.PUBLIC)
721 this.type = builtinKind;
726 public BuiltinTypeSpec (string name, Type builtinKind)
727 : this (MemberKind.InternalCompilerType, "", name, builtinKind)
729 // Make all internal types CLS-compliant, non-obsolete, compact
730 state = (state & ~(StateFlags.CLSCompliant_Undetected | StateFlags.Obsolete_Undetected | StateFlags.MissingDependency_Undetected)) | StateFlags.CLSCompliant;
735 public override int Arity {
741 public override BuiltinTypeSpec.Type BuiltinType {
747 public string FullName {
749 return ns + '.' + name;
753 public override string Name {
759 public string Namespace {
767 public static bool IsPrimitiveType (TypeSpec type)
769 return type.BuiltinType >= Type.FirstPrimitive && type.BuiltinType <= Type.LastPrimitive;
772 public static bool IsPrimitiveTypeOrDecimal (TypeSpec type)
774 return type.BuiltinType >= Type.FirstPrimitive && type.BuiltinType <= Type.Decimal;
777 public override string GetSignatureForError ()
780 case "Int32": return "int";
781 case "Int64": return "long";
782 case "String": return "string";
783 case "Boolean": return "bool";
784 case "Void": return "void";
785 case "Object": return "object";
786 case "UInt32": return "uint";
787 case "Int16": return "short";
788 case "UInt16": return "ushort";
789 case "UInt64": return "ulong";
790 case "Single": return "float";
791 case "Double": return "double";
792 case "Decimal": return "decimal";
793 case "Char": return "char";
794 case "Byte": return "byte";
795 case "SByte": return "sbyte";
805 // Returns the size of type if known, otherwise, 0
807 public static int GetSize (TypeSpec type)
809 switch (type.BuiltinType) {
833 public void SetDefinition (ITypeDefinition td, MetaType type, Modifiers mod)
835 this.definition = td;
837 this.modifiers |= (mod & ~Modifiers.AccessibilityMask);
840 public void SetDefinition (TypeSpec ts)
842 this.definition = ts.MemberDefinition;
843 this.info = ts.GetMetaInfo ();
844 this.BaseType = ts.BaseType;
845 this.Interfaces = ts.Interfaces;
846 this.modifiers = ts.Modifiers;
850 static class TypeSpecComparer
853 // Does strict reference comparion only
855 public static readonly DefaultImpl Default = new DefaultImpl ();
857 public class DefaultImpl : IEqualityComparer<TypeSpec[]>
859 #region IEqualityComparer<TypeSpec[]> Members
861 bool IEqualityComparer<TypeSpec[]>.Equals (TypeSpec[] x, TypeSpec[] y)
866 if (x.Length != y.Length)
869 for (int i = 0; i < x.Length; ++i)
876 int IEqualityComparer<TypeSpec[]>.GetHashCode (TypeSpec[] obj)
879 for (int i = 0; i < obj.Length; ++i)
880 hash = (hash << 5) - hash + obj[i].GetHashCode ();
889 // When comparing type signature of overrides or overloads
890 // this version tolerates different MVARs at same position
892 public static class Override
894 public static bool IsEqual (TypeSpec a, TypeSpec b)
900 // Consider the following example:
902 // public abstract class A
904 // public abstract T Foo<T>();
907 // public class B : A
909 // public override U Foo<T>() { return default (U); }
912 // Here, `T' and `U' are method type parameters from different methods
913 // (A.Foo and B.Foo), so both `==' and Equals() will fail.
915 // However, since we're determining whether B.Foo() overrides A.Foo(),
916 // we need to do a signature based comparision and consider them equal.
919 var tp_a = a as TypeParameterSpec;
921 var tp_b = b as TypeParameterSpec;
922 return tp_b != null && tp_a.IsMethodOwned == tp_b.IsMethodOwned && tp_a.DeclaredPosition == tp_b.DeclaredPosition;
925 var ac_a = a as ArrayContainer;
927 var ac_b = b as ArrayContainer;
928 return ac_b != null && ac_a.Rank == ac_b.Rank && IsEqual (ac_a.Element, ac_b.Element);
931 if (a.BuiltinType == BuiltinTypeSpec.Type.Dynamic || b.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
932 return b.BuiltinType == BuiltinTypeSpec.Type.Object || a.BuiltinType == BuiltinTypeSpec.Type.Object;
934 if (a.MemberDefinition != b.MemberDefinition)
938 for (int i = 0; i < a.TypeArguments.Length; ++i) {
939 if (!IsEqual (a.TypeArguments[i], b.TypeArguments[i]))
951 // Compares unordered arrays
953 public static bool IsSame (TypeSpec[] a, TypeSpec[] b)
958 if (a == null || b == null || a.Length != b.Length)
961 for (int ai = 0; ai < a.Length; ++ai) {
963 for (int bi = 0; bi < b.Length; ++bi) {
964 if (IsEqual (a[ai], b[bi])) {
977 public static bool IsEqual (AParametersCollection a, AParametersCollection b)
982 if (a.Count != b.Count)
985 for (int i = 0; i < a.Count; ++i) {
986 if (!IsEqual (a.Types[i], b.Types[i]))
989 const Parameter.Modifier ref_out = Parameter.Modifier.REF | Parameter.Modifier.OUT;
990 if ((a.FixedParameters[i].ModFlags & ref_out) != (b.FixedParameters[i].ModFlags & ref_out))
999 // Type variance equality comparison
1001 public static class Variant
1003 public static bool IsEqual (TypeSpec type1, TypeSpec type2)
1005 if (!type1.IsGeneric || !type2.IsGeneric)
1008 var target_type_def = type2.MemberDefinition;
1009 if (type1.MemberDefinition != target_type_def)
1012 var t1_targs = type1.TypeArguments;
1013 var t2_targs = type2.TypeArguments;
1014 var targs_definition = target_type_def.TypeParameters;
1016 if (!type1.IsInterface && !type1.IsDelegate) {
1020 for (int i = 0; i < targs_definition.Length; ++i) {
1021 if (TypeSpecComparer.IsEqual (t1_targs[i], t2_targs[i]))
1024 Variance v = targs_definition[i].Variance;
1025 if (v == Variance.None) {
1029 if (v == Variance.Covariant) {
1030 if (!Convert.ImplicitReferenceConversionExists (t1_targs[i], t2_targs[i]))
1032 } else if (!Convert.ImplicitReferenceConversionExists (t2_targs[i], t1_targs[i])) {
1042 // Checks whether two generic instances may become equal for some
1043 // particular instantiation (26.3.1).
1045 public static class Unify
1048 // Either @a or @b must be generic type
1050 public static bool IsEqual (TypeSpec a, TypeSpec b)
1052 if (a.MemberDefinition != b.MemberDefinition) {
1053 var base_ifaces = a.Interfaces;
1054 if (base_ifaces != null) {
1055 foreach (var base_iface in base_ifaces) {
1056 if (base_iface.Arity > 0 && IsEqual (base_iface, b))
1064 var ta = a.TypeArguments;
1065 var tb = b.TypeArguments;
1066 for (int i = 0; i < ta.Length; i++) {
1067 if (!MayBecomeEqualGenericTypes (ta[i], tb[i]))
1074 static bool ContainsTypeParameter (TypeSpec tparam, TypeSpec type)
1076 TypeSpec[] targs = type.TypeArguments;
1077 for (int i = 0; i < targs.Length; i++) {
1078 if (tparam == targs[i])
1081 if (ContainsTypeParameter (tparam, targs[i]))
1089 /// Check whether `a' and `b' may become equal generic types.
1090 /// The algorithm to do that is a little bit complicated.
1092 static bool MayBecomeEqualGenericTypes (TypeSpec a, TypeSpec b)
1094 if (a.IsGenericParameter) {
1096 // If a is an array of a's type, they may never
1103 // If b is a generic parameter or an actual type,
1104 // they may become equal:
1106 // class X<T,U> : I<T>, I<U>
1107 // class X<T> : I<T>, I<float>
1109 if (b.IsGenericParameter)
1110 return a != b && a.DeclaringType == b.DeclaringType;
1113 // We're now comparing a type parameter with a
1114 // generic instance. They may become equal unless
1115 // the type parameter appears anywhere in the
1116 // generic instance:
1118 // class X<T,U> : I<T>, I<X<U>>
1119 // -> error because you could instanciate it as
1122 // class X<T> : I<T>, I<X<T>> -> ok
1125 return !ContainsTypeParameter (a, b);
1128 if (b.IsGenericParameter)
1129 return MayBecomeEqualGenericTypes (b, a);
1132 // At this point, neither a nor b are a type parameter.
1134 // If one of them is a generic instance, compare them (if the
1135 // other one is not a generic instance, they can never
1138 if (TypeManager.IsGenericType (a) || TypeManager.IsGenericType (b))
1139 return IsEqual (a, b);
1142 // If both of them are arrays.
1144 var a_ac = a as ArrayContainer;
1146 var b_ac = b as ArrayContainer;
1147 if (b_ac == null || a_ac.Rank != b_ac.Rank)
1150 return MayBecomeEqualGenericTypes (a_ac.Element, b_ac.Element);
1154 // Ok, two ordinary types.
1160 public static bool Equals (TypeSpec[] x, TypeSpec[] y)
1165 if (x.Length != y.Length)
1168 for (int i = 0; i < x.Length; ++i)
1169 if (!IsEqual (x[i], y[i]))
1176 // Identity type conversion
1178 // Default reference comparison, it has to be used when comparing
1179 // two possible dynamic/internal types
1181 public static bool IsEqual (TypeSpec a, TypeSpec b)
1184 // This also rejects dynamic == dynamic
1185 return a.Kind != MemberKind.InternalCompilerType || a.BuiltinType == BuiltinTypeSpec.Type.Dynamic;
1188 if (a == null || b == null)
1192 var a_a = (ArrayContainer) a;
1193 var b_a = b as ArrayContainer;
1197 return a_a.Rank == b_a.Rank && IsEqual (a_a.Element, b_a.Element);
1200 if (!a.IsGeneric || !b.IsGeneric) {
1202 // object and dynamic are considered equivalent there is an identity conversion
1203 // between object and dynamic, and between constructed types that are the same
1204 // when replacing all occurences of dynamic with object.
1206 if (a.BuiltinType == BuiltinTypeSpec.Type.Dynamic || b.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
1207 return b.BuiltinType == BuiltinTypeSpec.Type.Object || a.BuiltinType == BuiltinTypeSpec.Type.Object;
1212 if (a.MemberDefinition != b.MemberDefinition)
1216 if (!Equals (a.TypeArguments, b.TypeArguments))
1219 a = a.DeclaringType;
1220 b = b.DeclaringType;
1221 } while (a != null);
1227 public interface ITypeDefinition : IMemberDefinition
1229 IAssemblyDefinition DeclaringAssembly { get; }
1230 string Namespace { get; }
1231 int TypeParametersCount { get; }
1232 TypeParameterSpec[] TypeParameters { get; }
1234 TypeSpec GetAttributeCoClass ();
1235 string GetAttributeDefaultMember ();
1236 AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa);
1237 bool IsInternalAsPublic (IAssemblyDefinition assembly);
1238 void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache);
1241 class InternalType : TypeSpec, ITypeDefinition
1243 public static readonly InternalType AnonymousMethod = new InternalType ("anonymous method");
1244 public static readonly InternalType Arglist = new InternalType ("__arglist");
1245 public static readonly InternalType MethodGroup = new InternalType ("method group");
1246 public static readonly InternalType NullLiteral = new InternalType ("null");
1247 public static readonly InternalType FakeInternalType = new InternalType ("<fake$type>");
1248 public static readonly InternalType Namespace = new InternalType ("<namespace>");
1250 readonly string name;
1252 InternalType (string name, MemberCache cache)
1258 InternalType (string name)
1259 : base (MemberKind.InternalCompilerType, null, null, null, Modifiers.PUBLIC)
1262 this.definition = this;
1263 cache = MemberCache.Empty;
1265 // Make all internal types CLS-compliant, non-obsolete
1266 state = (state & ~(StateFlags.CLSCompliant_Undetected | StateFlags.Obsolete_Undetected)) | StateFlags.CLSCompliant;
1271 public override int Arity {
1277 IAssemblyDefinition ITypeDefinition.DeclaringAssembly {
1279 throw new NotImplementedException ();
1283 bool IMemberDefinition.IsImported {
1289 public override string Name {
1295 string ITypeDefinition.Namespace {
1301 int ITypeDefinition.TypeParametersCount {
1307 TypeParameterSpec[] ITypeDefinition.TypeParameters {
1315 public override string GetSignatureForError ()
1320 #region ITypeDefinition Members
1322 TypeSpec ITypeDefinition.GetAttributeCoClass ()
1327 string ITypeDefinition.GetAttributeDefaultMember ()
1332 AttributeUsageAttribute ITypeDefinition.GetAttributeUsage (PredefinedAttribute pa)
1337 bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
1339 throw new NotImplementedException ();
1342 void ITypeDefinition.LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
1344 throw new NotImplementedException ();
1347 string[] IMemberDefinition.ConditionalConditions ()
1352 ObsoleteAttribute IMemberDefinition.GetAttributeObsolete ()
1357 bool? IMemberDefinition.CLSAttributeValue {
1363 void IMemberDefinition.SetIsAssigned ()
1367 void IMemberDefinition.SetIsUsed ()
1374 public abstract class ElementTypeSpec : TypeSpec, ITypeDefinition
1376 protected ElementTypeSpec (MemberKind kind, TypeSpec element, MetaType info)
1377 : base (kind, element.DeclaringType, null, info, element.Modifiers)
1379 this.Element = element;
1381 // Some flags can be copied directly from the element
1382 const StateFlags shared_flags = StateFlags.CLSCompliant | StateFlags.CLSCompliant_Undetected
1383 | StateFlags.Obsolete | StateFlags.Obsolete_Undetected | StateFlags.HasDynamicElement;
1384 state &= ~shared_flags;
1385 state |= (element.state & shared_flags);
1387 if (element.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
1388 state |= StateFlags.HasDynamicElement;
1390 // Has to use its own type definition instead of just element definition to
1391 // correctly identify itself for cases like x.MemberDefininition == predefined.MemberDefinition
1392 this.definition = this;
1394 cache = MemberCache.Empty;
1399 public TypeSpec Element { get; private set; }
1401 public override string Name {
1403 throw new NotSupportedException ();
1409 public override ObsoleteAttribute GetAttributeObsolete ()
1411 return Element.GetAttributeObsolete ();
1414 protected virtual string GetPostfixSignature ()
1419 public override string GetSignatureForDocumentation ()
1421 return Element.GetSignatureForDocumentation () + GetPostfixSignature ();
1424 public override string GetSignatureForError ()
1426 return Element.GetSignatureForError () + GetPostfixSignature ();
1429 public override TypeSpec Mutate (TypeParameterMutator mutator)
1431 var me = Element.Mutate (mutator);
1435 var mutated = (ElementTypeSpec) MemberwiseClone ();
1436 mutated.Element = me;
1437 mutated.info = null;
1441 #region ITypeDefinition Members
1443 IAssemblyDefinition ITypeDefinition.DeclaringAssembly {
1445 return Element.MemberDefinition.DeclaringAssembly;
1449 bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
1451 return Element.MemberDefinition.IsInternalAsPublic (assembly);
1454 public string Namespace {
1455 get { throw new NotImplementedException (); }
1458 public int TypeParametersCount {
1464 public TypeParameterSpec[] TypeParameters {
1466 throw new NotSupportedException ();
1470 public TypeSpec GetAttributeCoClass ()
1472 return Element.MemberDefinition.GetAttributeCoClass ();
1475 public string GetAttributeDefaultMember ()
1477 return Element.MemberDefinition.GetAttributeDefaultMember ();
1480 public void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
1482 Element.MemberDefinition.LoadMembers (declaringType, onlyTypes, ref cache);
1485 public bool IsImported {
1487 return Element.MemberDefinition.IsImported;
1491 public string[] ConditionalConditions ()
1493 return Element.MemberDefinition.ConditionalConditions ();
1496 bool? IMemberDefinition.CLSAttributeValue {
1498 return Element.MemberDefinition.CLSAttributeValue;
1502 public void SetIsAssigned ()
1504 Element.MemberDefinition.SetIsAssigned ();
1507 public void SetIsUsed ()
1509 Element.MemberDefinition.SetIsUsed ();
1515 public class ArrayContainer : ElementTypeSpec
1517 public struct TypeRankPair : IEquatable<TypeRankPair>
1522 public TypeRankPair (TypeSpec ts, int rank)
1528 public override int GetHashCode ()
1530 return ts.GetHashCode () ^ rank.GetHashCode ();
1533 #region IEquatable<Tuple<T1,T2>> Members
1535 public bool Equals (TypeRankPair other)
1537 return other.ts == ts && other.rank == rank;
1544 readonly ModuleContainer module;
1546 private ArrayContainer (ModuleContainer module, TypeSpec element, int rank)
1547 : base (MemberKind.ArrayType, element, null)
1549 this.module = module;
1559 public MethodInfo GetConstructor ()
1561 var mb = module.Builder;
1563 var arg_types = new MetaType[rank];
1564 for (int i = 0; i < rank; i++)
1565 arg_types[i] = module.Compiler.BuiltinTypes.Int.GetMetaInfo ();
1567 var ctor = mb.GetArrayMethod (
1568 GetMetaInfo (), Constructor.ConstructorName,
1569 CallingConventions.HasThis,
1575 public MethodInfo GetAddressMethod ()
1577 var mb = module.Builder;
1579 var arg_types = new MetaType[rank];
1580 for (int i = 0; i < rank; i++)
1581 arg_types[i] = module.Compiler.BuiltinTypes.Int.GetMetaInfo ();
1583 var address = mb.GetArrayMethod (
1584 GetMetaInfo (), "Address",
1585 CallingConventions.HasThis | CallingConventions.Standard,
1586 ReferenceContainer.MakeType (module, Element).GetMetaInfo (), arg_types);
1591 public MethodInfo GetGetMethod ()
1593 var mb = module.Builder;
1595 var arg_types = new MetaType[rank];
1596 for (int i = 0; i < rank; i++)
1597 arg_types[i] = module.Compiler.BuiltinTypes.Int.GetMetaInfo ();
1599 var get = mb.GetArrayMethod (
1600 GetMetaInfo (), "Get",
1601 CallingConventions.HasThis | CallingConventions.Standard,
1602 Element.GetMetaInfo (), arg_types);
1607 public MethodInfo GetSetMethod ()
1609 var mb = module.Builder;
1611 var arg_types = new MetaType[rank + 1];
1612 for (int i = 0; i < rank; i++)
1613 arg_types[i] = module.Compiler.BuiltinTypes.Int.GetMetaInfo ();
1615 arg_types[rank] = Element.GetMetaInfo ();
1617 var set = mb.GetArrayMethod (
1618 GetMetaInfo (), "Set",
1619 CallingConventions.HasThis | CallingConventions.Standard,
1620 module.Compiler.BuiltinTypes.Void.GetMetaInfo (), arg_types);
1625 public override MetaType GetMetaInfo ()
1629 info = Element.GetMetaInfo ().MakeArrayType ();
1631 info = Element.GetMetaInfo ().MakeArrayType (rank);
1637 protected override string GetPostfixSignature()
1639 return GetPostfixSignature (rank);
1642 public static string GetPostfixSignature (int rank)
1644 StringBuilder sb = new StringBuilder ();
1646 for (int i = 1; i < rank; i++) {
1651 return sb.ToString ();
1654 public override string GetSignatureForDocumentation ()
1656 StringBuilder sb = new StringBuilder ();
1657 GetElementSignatureForDocumentation (sb);
1658 return sb.ToString ();
1661 void GetElementSignatureForDocumentation (StringBuilder sb)
1663 var ac = Element as ArrayContainer;
1665 sb.Append (Element.GetSignatureForDocumentation ());
1667 ac.GetElementSignatureForDocumentation (sb);
1670 for (int i = 1; i < rank; i++) {
1679 public static ArrayContainer MakeType (ModuleContainer module, TypeSpec element)
1681 return MakeType (module, element, 1);
1684 public static ArrayContainer MakeType (ModuleContainer module, TypeSpec element, int rank)
1687 var key = new TypeRankPair (element, rank);
1688 if (!module.ArrayTypesCache.TryGetValue (key, out ac)) {
1689 ac = new ArrayContainer (module, element, rank) {
1690 BaseType = module.Compiler.BuiltinTypes.Array
1693 module.ArrayTypesCache.Add (key, ac);
1700 class ReferenceContainer : ElementTypeSpec
1702 private ReferenceContainer (TypeSpec element)
1703 : base (MemberKind.Class, element, null) // TODO: Kind.Class is most likely wrong
1707 public override MetaType GetMetaInfo ()
1710 info = Element.GetMetaInfo ().MakeByRefType ();
1716 public static ReferenceContainer MakeType (ModuleContainer module, TypeSpec element)
1718 ReferenceContainer pc;
1719 if (!module.ReferenceTypesCache.TryGetValue (element, out pc)) {
1720 pc = new ReferenceContainer (element);
1721 module.ReferenceTypesCache.Add (element, pc);
1728 class PointerContainer : ElementTypeSpec
1730 private PointerContainer (TypeSpec element)
1731 : base (MemberKind.PointerType, element, null)
1733 // It's never CLS-Compliant
1734 state &= ~StateFlags.CLSCompliant_Undetected;
1737 public override MetaType GetMetaInfo ()
1740 info = Element.GetMetaInfo ().MakePointerType ();
1746 protected override string GetPostfixSignature()
1751 public static PointerContainer MakeType (ModuleContainer module, TypeSpec element)
1753 PointerContainer pc;
1754 if (!module.PointerTypesCache.TryGetValue (element, out pc)) {
1755 pc = new PointerContainer (element);
1756 module.PointerTypesCache.Add (element, pc);