Merge pull request #3059 from lateralusX/jlorenss/win-x64-dyncall-gsharedvt-support
[mono.git] / mcs / class / corlib / ReferenceSources / RuntimeType.cs
index e0f48d36255c5437b7707e20716623c247846731..f577efcd5ca871464b7ed8ca65b028a35086fd98 100644 (file)
@@ -37,15 +37,82 @@ using System.Reflection.Emit;
 #endif
 using System.Diagnostics.Contracts;
 using System.Security;
+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)
+               {
+                       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)
                {
-                       // TODO: Requires MonoType
-                       throw new NotSupportedException ();
+                       /* 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 ()
@@ -54,6 +121,23 @@ namespace System
                        return att.Length != 0 ? ((DefaultMemberAttribute) att [0]).MemberName : null;
                }
 
+               RuntimeConstructorInfo m_serializationCtor;
+               internal RuntimeConstructorInfo GetSerializationCtor()
+               {
+                       if (m_serializationCtor == null) {
+                               var s_SICtorParamTypes = new Type[] { typeof(SerializationInfo), typeof(StreamingContext) };
+
+                               m_serializationCtor = GetConstructor(
+                                       BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
+                                       null,
+                                       CallingConventions.Any,
+                                       s_SICtorParamTypes,
+                                       null) as RuntimeConstructorInfo;
+                       }
+
+                       return m_serializationCtor;
+               }
+
                internal Object CreateInstanceSlow(bool publicOnly, bool skipCheckThis, bool fillCache, ref StackCrawlMark stackMark)
                {
                        bool bNeedSecurityCheck = true;
@@ -295,6 +379,16 @@ namespace System
                        return null;
                }
 
+               string GetCachedName (TypeNameKind kind)
+               {
+                       switch (kind) {
+                       case TypeNameKind.SerializationName:
+                               return ToString ();
+                       default:
+                               throw new NotImplementedException ();
+                       }
+               }
+
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                extern Type make_array_type (int rank);
 
@@ -330,7 +424,7 @@ namespace System
 
                public override StructLayoutAttribute StructLayoutAttribute {
                        get {
-                               return GetStructLayoutAttribute ();
+                               return StructLayoutAttribute.GetCustomAttribute (this);
                        }
                }
 
@@ -366,42 +460,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)]
@@ -456,39 +519,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;
@@ -584,7 +616,7 @@ namespace System
                internal extern string getFullName(bool full_name, bool assembly_qualified);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern override Type [] GetGenericArguments ();
+               extern Type[] GetGenericArgumentsInternal (bool runtimeArray);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                extern GenericParameterAttributes GetGenericParameterAttributes ();
@@ -618,12 +650,6 @@ namespace System
                        get;
                }
 
-               public override string FullName {
-                       get {
-                               throw new NotImplementedException ();
-                       }
-               }
-
                public extern override string Name {
                        [MethodImplAttribute(MethodImplOptions.InternalCall)]
                        get;
@@ -634,6 +660,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 ();
@@ -648,6 +680,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;
+                       }
+               }
        }
-}
\ No newline at end of file
+}