Prevent race conditions for this cached variable, by using a temporary variable,...
[mono.git] / mcs / class / IKVM.Reflection / Universe.cs
index 4ad9e4651d3acd74f7bed778d9c5a536eda41b2b..eaecae9c939fe7bda1bcc71570c3c45857c99657 100644 (file)
@@ -77,15 +77,23 @@ namespace IKVM.Reflection
 
        public delegate Assembly ResolveEventHandler(object sender, ResolveEventArgs args);
 
+       [Flags]
+       public enum UniverseOptions
+       {
+               None = 0,
+               EnableFunctionPointers = 1,
+       }
+
        public sealed class Universe : IDisposable
        {
-               internal readonly Dictionary<Type, Type> canonicalizedTypes = new Dictionary<Type, Type>();
+               private readonly Dictionary<Type, Type> canonicalizedTypes = new Dictionary<Type, Type>();
                private readonly List<Assembly> assemblies = new List<Assembly>();
                private readonly List<AssemblyBuilder> dynamicAssemblies = new List<AssemblyBuilder>();
                private readonly Dictionary<string, Assembly> assembliesByName = new Dictionary<string, Assembly>();
                private readonly Dictionary<System.Type, Type> importedTypes = new Dictionary<System.Type, Type>();
                private Dictionary<ScopedTypeName, Type> missingTypes;
                private bool resolveMissingMembers;
+               private readonly bool enableFunctionPointers;
                private Type typeof_System_Object;
                private Type typeof_System_ValueType;
                private Type typeof_System_Enum;
@@ -114,7 +122,6 @@ namespace IKVM.Reflection
                private Type typeof_System_NonSerializedAttribute;
                private Type typeof_System_SerializableAttribute;
                private Type typeof_System_AttributeUsageAttribute;
-               private Type typeof_System_Reflection_AssemblyCultureAttribute;
                private Type typeof_System_Runtime_InteropServices_DllImportAttribute;
                private Type typeof_System_Runtime_InteropServices_FieldOffsetAttribute;
                private Type typeof_System_Runtime_InteropServices_InAttribute;
@@ -146,6 +153,16 @@ namespace IKVM.Reflection
                private Type typeof_System_Security_Permissions_SecurityAction;
                private List<ResolveEventHandler> resolvers = new List<ResolveEventHandler>();
 
+               public Universe()
+                       : this(UniverseOptions.None)
+               {
+               }
+
+               public Universe(UniverseOptions options)
+               {
+                       enableFunctionPointers = (options & UniverseOptions.EnableFunctionPointers) != 0;
+               }
+
                internal Assembly Mscorlib
                {
                        get { return Load("mscorlib"); }
@@ -318,11 +335,6 @@ namespace IKVM.Reflection
                        get { return typeof_System_AttributeUsageAttribute ?? (typeof_System_AttributeUsageAttribute = ImportMscorlibType(typeof(System.AttributeUsageAttribute))); }
                }
 
-               internal Type System_Reflection_AssemblyCultureAttribute
-               {
-                       get { return typeof_System_Reflection_AssemblyCultureAttribute ?? (typeof_System_Reflection_AssemblyCultureAttribute = ImportMscorlibType(typeof(System.Reflection.AssemblyCultureAttribute))); }
-               }
-
                internal Type System_Runtime_InteropServices_DllImportAttribute
                {
                        get { return typeof_System_Runtime_InteropServices_DllImportAttribute ?? (typeof_System_Runtime_InteropServices_DllImportAttribute = ImportMscorlibType(typeof(System.Runtime.InteropServices.DllImportAttribute))); }
@@ -856,6 +868,11 @@ namespace IKVM.Reflection
                        get { return resolveMissingMembers; }
                }
 
+               internal bool EnableFunctionPointers
+               {
+                       get { return enableFunctionPointers; }
+               }
+
                private struct ScopedTypeName : IEquatable<ScopedTypeName>
                {
                        private readonly object scope;
@@ -942,5 +959,33 @@ namespace IKVM.Reflection
                        }
                        throw new System.MissingMemberException(declaringType.ToString(), name);
                }
+
+               internal Type CanonicalizeType(Type type)
+               {
+                       Type canon;
+                       if (!canonicalizedTypes.TryGetValue(type, out canon))
+                       {
+                               canon = type;
+                               canonicalizedTypes.Add(canon, canon);
+                       }
+                       return canon;
+               }
+
+               public Type MakeFunctionPointer(__StandAloneMethodSig sig)
+               {
+                       return FunctionPointerType.Make(this, sig);
+               }
+
+               public __StandAloneMethodSig MakeStandAloneMethodSig(System.Runtime.InteropServices.CallingConvention callingConvention, Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
+               {
+                       return new __StandAloneMethodSig(true, callingConvention, 0, returnType ?? this.System_Void, Util.Copy(parameterTypes), Type.EmptyTypes,
+                               PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes)));
+               }
+
+               public __StandAloneMethodSig MakeStandAloneMethodSig(CallingConventions callingConvention, Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, Type[] optionalParameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
+               {
+                       return new __StandAloneMethodSig(false, 0, callingConvention, returnType ?? this.System_Void, Util.Copy(parameterTypes), Util.Copy(optionalParameterTypes),
+                               PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes) + Util.NullSafeLength(optionalParameterTypes)));
+               }
        }
 }