Merge pull request #3240 from alexanderkyte/aot_compiler_leaks
[mono.git] / mcs / class / corlib / ReferenceSources / RuntimeType.cs
index 3557e77817c6b392af4e19cf509d4d6ee0219264..4981f4175a15c4b1846a98a2bf2f75e4815864e8 100644 (file)
@@ -41,12 +41,78 @@ using System.Runtime.Serialization;
 
 namespace System
 {
+       // Contains information about the type which is expensive to compute
+       [StructLayout (LayoutKind.Sequential)]
+       internal class MonoTypeInfo {
+               // this is the displayed form: special characters
+               // ,+*&*[]\ in the identifier portions of the names
+               // have been escaped with a leading backslash (\)
+               public string full_name;
+               public MonoCMethod default_ctor;
+       }
+
+       [StructLayout (LayoutKind.Sequential)]
        partial class RuntimeType
        {
-               internal virtual MonoCMethod GetDefaultConstructor ()
+               [NonSerialized]
+               MonoTypeInfo type_info;
+
+               internal Object GenericCache;
+
+               internal RuntimeType (Object obj)
                {
-                       // TODO: Requires MonoType
-                       throw new NotSupportedException ();
+                       throw new NotImplementedException ();
+               }
+
+               internal MonoCMethod GetDefaultConstructor ()
+               {
+                       MonoCMethod ctor = null;
+
+                       if (type_info == null)
+                               type_info = new MonoTypeInfo ();
+                       else
+                               ctor = type_info.default_ctor;
+
+                       if (ctor == null) {
+                               var ctors = GetConstructors (BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
+
+                               for (int i = 0; i < ctors.Length; ++i) {
+                                       if (ctors [i].GetParametersCount () == 0) {
+                                               type_info.default_ctor = ctor = (MonoCMethod) ctors [i];
+                                               break;
+                                       }
+                               }
+                       }
+
+                       return ctor;
+               }
+
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               extern MethodInfo GetCorrespondingInflatedMethod (MethodInfo generic);
+
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               extern ConstructorInfo GetCorrespondingInflatedConstructor (ConstructorInfo generic);
+
+               internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
+                {
+                       if (fromNoninstanciated == null)
+                               throw new ArgumentNullException ("fromNoninstanciated");
+                        return GetCorrespondingInflatedMethod (fromNoninstanciated);
+                }
+
+               internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
+               {
+                       if (fromNoninstanciated == null)
+                               throw new ArgumentNullException ("fromNoninstanciated");
+                        return GetCorrespondingInflatedConstructor (fromNoninstanciated);
+               }
+
+               internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
+               {
+                       /* create sensible flags from given FieldInfo */
+                       BindingFlags flags = fromNoninstanciated.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
+                       flags |= fromNoninstanciated.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic;
+                       return GetField (fromNoninstanciated.Name, flags);
                }
 
                string GetDefaultMemberName ()
@@ -358,7 +424,7 @@ namespace System
 
                public override StructLayoutAttribute StructLayoutAttribute {
                        get {
-                               return GetStructLayoutAttribute ();
+                               return StructLayoutAttribute.GetCustomAttribute (this);
                        }
                }
 
@@ -386,7 +452,8 @@ namespace System
                                throw new InvalidOperationException(Environment.GetResourceString("Arg_NotGenericParameter"));
                        Contract.EndContractBlock();
 
-                       Type[] constraints = GetGenericParameterConstraints_impl ();
+                       var paramInfo = new Mono.RuntimeGenericParamInfoHandle (RuntimeTypeHandle.GetGenericParameterInfo (this));
+                       Type[] constraints = paramInfo.Constraints;
 
                        if (constraints == null)
                                constraints = EmptyArray<Type>.Value;
@@ -394,42 +461,11 @@ namespace System
                        return constraints;
                }
 
-               public override Type MakeGenericType (params Type[] typeArguments)
+               internal static object CreateInstanceForAnotherGenericParameter (Type genericType, RuntimeType genericArgument)
                {
-                       if (IsUserType)
-                               throw new NotSupportedException ();
-                       if (!IsGenericTypeDefinition)
-                               throw new InvalidOperationException ("not a generic type definition");
-                       if (typeArguments == null)
-                               throw new ArgumentNullException ("typeArguments");
-                       if (GetGenericArguments().Length != typeArguments.Length)
-                               throw new ArgumentException (String.Format ("The type or method has {0} generic parameter(s) but {1} generic argument(s) where provided. A generic argument must be provided for each generic parameter.", GetGenericArguments ().Length, typeArguments.Length), "typeArguments");
-
-                       bool hasUserType = false;
-
-                       Type[] systemTypes = new Type[typeArguments.Length];
-                       for (int i = 0; i < typeArguments.Length; ++i) {
-                               Type t = typeArguments [i];
-                               if (t == null)
-                                       throw new ArgumentNullException ("typeArguments");
-
-                               if (!(t is MonoType))
-                                       hasUserType = true;
-                               systemTypes [i] = t;
-                       }
-
-                       if (hasUserType) {
-#if FULL_AOT_RUNTIME
-                               throw new NotSupportedException ("User types are not supported under full aot");
-#else
-                               return new MonoGenericClass (this, typeArguments);
-#endif
-                       }
-
-                       Type res = MakeGenericType (this, systemTypes);
-                       if (res == null)
-                               throw new TypeLoadException ();
-                       return res;
+                       var gt = (RuntimeType) MakeGenericType (genericType, new Type [] { genericArgument });
+                       var ctor = gt.GetDefaultConstructor ();
+                       return ctor.InternalInvoke (null, null);
                }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
@@ -484,39 +520,8 @@ namespace System
                        }
                }
 
-               StructLayoutAttribute GetStructLayoutAttribute ()
-               {
-                       LayoutKind kind;
-
-                       if (IsLayoutSequential)
-                               kind = LayoutKind.Sequential;
-                       else if (IsExplicitLayout)
-                               kind = LayoutKind.Explicit;
-                       else
-                               kind = LayoutKind.Auto;
-
-                       StructLayoutAttribute attr = new StructLayoutAttribute (kind);
-
-                       if (IsUnicodeClass)
-                               attr.CharSet = CharSet.Unicode;
-                       else if (IsAnsiClass)
-                               attr.CharSet = CharSet.Ansi;
-                       else
-                               attr.CharSet = CharSet.Auto;
-
-                       if (kind != LayoutKind.Auto) {
-                               int packing;
-                               GetPacking (out packing, out attr.Size);
-                               // 0 means no data provided, we end up with default value
-                               if (packing != 0)
-                                       attr.Pack = packing;
-                       }
-
-                       return attr;
-               }
-
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               extern void GetPacking (out int packing, out int size);
+               internal extern void GetPacking (out int packing, out int size);
 
 #if MONO_COM
                private static Dictionary<Guid, Type> clsid_types;
@@ -612,13 +617,11 @@ namespace System
                internal extern string getFullName(bool full_name, bool assembly_qualified);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern override Type [] GetGenericArguments ();
-
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               extern GenericParameterAttributes GetGenericParameterAttributes ();
+               extern Type[] GetGenericArgumentsInternal (bool runtimeArray);
 
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               extern Type[] GetGenericParameterConstraints_impl ();
+               GenericParameterAttributes GetGenericParameterAttributes () {
+                       return (new Mono.RuntimeGenericParamInfoHandle (RuntimeTypeHandle.GetGenericParameterInfo (this))).Attributes;
+               }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                extern int GetGenericParameterPosition ();
@@ -646,12 +649,6 @@ namespace System
                        get;
                }
 
-               public override string FullName {
-                       get {
-                               throw new NotImplementedException ();
-                       }
-               }
-
                public extern override string Name {
                        [MethodImplAttribute(MethodImplOptions.InternalCall)]
                        get;
@@ -662,6 +659,12 @@ namespace System
                        get;
                }
 
+#if MOBILE
+               static int get_core_clr_security_level ()
+               {
+                       return 1;
+               }
+#else
                //seclevel { transparent = 0, safe-critical = 1, critical = 2}
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern int get_core_clr_security_level ();
@@ -676,6 +679,34 @@ namespace System
 
                public override bool IsSecuritySafeCritical {
                        get { return get_core_clr_security_level () == 1; }
-               }               
+               }
+#endif
+
+               public override int GetHashCode()
+               {
+                       Type t = UnderlyingSystemType;
+                       if (t != null && t != this)
+                               return t.GetHashCode ();
+                       return (int)_impl.Value;
+               }
+
+               public override string FullName {
+                       get {
+                               string fullName;
+                               // This doesn't need locking
+                               if (type_info == null)
+                                       type_info = new MonoTypeInfo ();
+                               if ((fullName = type_info.full_name) == null)
+                                       fullName = type_info.full_name = getFullName (true, false);
+
+                               return fullName;
+                       }
+               }
+
+               internal override bool IsUserType {
+                       get {
+                               return false;
+                       }
+               }
        }
 }