New test.
[mono.git] / mcs / class / corlib / System.Reflection.Emit / AssemblyBuilder.cs
index f1827a2d76cd939c10af3f915c1d01a0ff939eba..2d5b6ea4817ec68903ebc9b0fbdf8a0812195386 100644 (file)
@@ -90,12 +90,122 @@ namespace System.Reflection.Emit
                }
        }
 
-#if NET_2_0
+       internal class GenericInstanceKey {
+               Type gtd;
+               internal Type[] args;
+
+               internal GenericInstanceKey (Type gtd, Type[] args)
+               {
+                       this.gtd = gtd;
+                       this.args = args;
+               }
+
+               static bool IsBoundedVector (Type type) {
+                       ArrayType at = type as ArrayType;
+                       if (at != null)
+                               return at.GetEffectiveRank () == 1;
+                       return type.ToString ().EndsWith ("[*]", StringComparison.Ordinal); /*Super uggly hack, SR doesn't allow one to query for it */
+               }
+
+               static bool TypeEquals (Type a, Type b) {
+                       if (a == b)
+                               return true;
+
+                       if (a.HasElementType) {
+                               if (!b.HasElementType)
+                                       return false;
+                               if (!TypeEquals (a.GetElementType (), b.GetElementType ()))
+                                       return false;
+                               if (a.IsArray) {
+                                       if (!b.IsArray)
+                                               return false;
+                                       int rank = a.GetArrayRank ();
+                                       if (rank != b.GetArrayRank ())
+                                               return false;
+                                       if (rank == 1 && IsBoundedVector (a) != IsBoundedVector (b))
+                                               return false;
+                               } else if (a.IsByRef) {
+                                       if (!b.IsByRef)
+                                               return false;
+                               } else if (a.IsPointer) {
+                                       if (!b.IsPointer)
+                                               return false;
+                               }
+                               return true;
+                       }
+
+                       if (a.IsGenericType) {
+                               if (!b.IsGenericType)
+                                       return false;
+                               if (a.IsGenericParameter)
+                                       return a == b;
+                               if (a.IsGenericParameter) //previous test should have caught it
+                                       return false;
+
+                               if (a.IsGenericTypeDefinition) {
+                                       if (!b.IsGenericTypeDefinition)
+                                               return false;
+                               } else {
+                                       if (b.IsGenericTypeDefinition)
+                                               return false;
+                                       if (!TypeEquals (a.GetGenericTypeDefinition (), b.GetGenericTypeDefinition ()))
+                                               return false;
+
+                                       Type[] argsA = a.GetGenericArguments ();
+                                       Type[] argsB = b.GetGenericArguments ();
+                                       for (int i = 0; i < argsA.Length; ++i) {
+                                               if (!TypeEquals (argsA [i], argsB [i]))
+                                                       return false;
+                                       }
+                               }
+                       }
+
+                       /*
+                       Now only non-generic, non compound types are left. To properly deal with user
+                       types we would have to call UnderlyingSystemType, but we let them have their
+                       own instantiation as this is MS behavior and mcs (pre C# 4.0, at least) doesn't
+                       depend on proper UT canonicalization.
+                       */
+                       return a == b;
+               }
+
+               public override bool Equals (object obj)
+               {
+                       GenericInstanceKey other = obj as GenericInstanceKey;
+                       if (other == null)
+                               return false;
+                       if (gtd != other.gtd)
+                               return false;
+                       for (int i = 0; i < args.Length; ++i) {
+                               Type a = args [i];
+                               Type b = other.args [i];
+                               /*
+                               We must cannonicalize as much as we can. Using equals means that some resulting types
+                               won't have the exact same types as the argument ones. 
+                               For example, flyweight types used array, pointer and byref will should this behavior.
+                               MCS seens to be resilient to this problem so hopefully this won't show up.   
+                               */
+                               if (a != b && !a.Equals (b))
+                                       return false;
+                       }
+                       return true;
+               }
+
+               public override int GetHashCode ()
+               {
+                       int hash = gtd.GetHashCode ();
+                       for (int i = 0; i < args.Length; ++i)
+                               hash ^= args [i].GetHashCode ();
+                       return hash;
+               }
+       }
+
+
        [ComVisible (true)]
        [ComDefaultInterface (typeof (_AssemblyBuilder))]
-#endif
        [ClassInterface (ClassInterfaceType.None)]
        public sealed class AssemblyBuilder : Assembly, _AssemblyBuilder {
+#pragma warning disable 169, 414
                #region Sync with object-internals.h
                private UIntPtr dynamic_assembly; /* GC-tracked */
                private MethodInfo entry_point;
@@ -121,7 +231,10 @@ namespace System.Reflection.Emit
                ImageFileMachine machine;
                bool corlib_internal;
                Type[] type_forwarders;
+               byte[] pktoken;
                #endregion
+#pragma warning restore 169, 414
+               
                internal Type corlib_object_type = typeof (System.Object);
                internal Type corlib_value_type = typeof (System.ValueType);
                internal Type corlib_enum_type = typeof (System.Enum);
@@ -134,6 +247,7 @@ namespace System.Reflection.Emit
                NativeResourceType native_resource;
                readonly bool is_compiler_context;
                string versioninfo_culture;
+               Hashtable generic_instances = new Hashtable ();
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private static extern void basic_init (AssemblyBuilder ab);
@@ -143,20 +257,26 @@ namespace System.Reflection.Emit
 
                internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access, bool corlib_internal)
                {
-#if BOOTSTRAP_WITH_OLDLIB
-                       is_compiler_context = true;
-#else
                        is_compiler_context = (access & COMPILER_ACCESS) != 0;
-#endif
 
                        // remove Mono specific flag to allow enum check to pass
                        access &= ~COMPILER_ACCESS;
 
-#if NET_2_0
+#if MOONLIGHT
+                       // only "Run" is supported by Silverlight
+                       // however SMCS requires more than this but runs outside the CoreCLR sandbox
+                       if (SecurityManager.SecurityEnabled && (access != AssemblyBuilderAccess.Run))
+                               throw new ArgumentException ("access");
+#endif
+
                        if (!Enum.IsDefined (typeof (AssemblyBuilderAccess), access))
                                throw new ArgumentException (string.Format (CultureInfo.InvariantCulture,
                                        "Argument value {0} is not valid.", (int) access),
                                        "access");
+
+#if NET_4_0
+                       if ((access & AssemblyBuilderAccess.RunAndCollect) == AssemblyBuilderAccess.RunAndCollect)
+                               throw new NotSupportedException ("RunAndCollect not yet supported.");
 #endif
 
                        name = n.Name;
@@ -183,8 +303,7 @@ namespace System.Reflection.Emit
                        if (n.KeyPair != null) {
                                // full keypair is available (for signing)
                                sn = n.KeyPair.StrongName ();
-                       }
-                       else {
+                       } else {
                                // public key is available (for delay-signing)
                                byte[] pk = n.GetPublicKey ();
                                if ((pk != null) && (pk.Length > 0)) {
@@ -192,7 +311,19 @@ namespace System.Reflection.Emit
                                }
                        }
 
+                       if (sn != null)
+                               flags |= (uint) AssemblyNameFlags.PublicKey;
+
                        this.corlib_internal = corlib_internal;
+                       if (sn != null) {
+                               this.pktoken = new byte[sn.PublicKeyToken.Length * 2];
+                               int pkti = 0;
+                               foreach (byte pkb in sn.PublicKeyToken) {
+                                       string part = pkb.ToString("x2");
+                                       this.pktoken[pkti++] = (byte)part[0];
+                                       this.pktoken[pkti++] = (byte)part[1];
+                               }
+                       }
 
                        basic_init (this);
                }
@@ -215,21 +346,17 @@ namespace System.Reflection.Emit
                        }
                }
 
-#if NET_1_1
                /* This is to keep signature compatibility with MS.NET */
                public override string ImageRuntimeVersion {
                        get {
                                return base.ImageRuntimeVersion;
                        }
                }
-#endif
 
-#if NET_2_0
                [MonoTODO]
                public override bool ReflectionOnly {
                        get { return base.ReflectionOnly; }
                }
-#endif
 
                public void AddResourceFile (string name, string fileName)
                {
@@ -268,6 +395,7 @@ namespace System.Reflection.Emit
                /// </summary>
                internal void AddPermissionRequests (PermissionSet required, PermissionSet optional, PermissionSet refused)
                {
+#if !NET_2_1
                        if (created)
                                throw new InvalidOperationException ("Assembly was already saved.");
 
@@ -294,6 +422,7 @@ namespace System.Reflection.Emit
                                permissions_refused [0] = new RefEmitPermissionSet (
                                        SecurityAction.RequestRefuse, refused.ToXml ().ToString ());
                        }
+#endif
                }
 
                internal void EmbedResourceFile (string name, string fileName)
@@ -339,20 +468,21 @@ namespace System.Reflection.Emit
                        resources [p].data = blob;
                }
 
-#if NET_2_0
                internal void AddTypeForwarder (Type t) {
                        if (t == null)
                                throw new ArgumentNullException ("t");
+                       if (t.IsNested)
+                               throw new ArgumentException ();
 
                        if (type_forwarders == null) {
                                type_forwarders = new Type [1] { t };
                        } else {
                                Type[] arr = new Type [type_forwarders.Length + 1];
                                Array.Copy (type_forwarders, arr, type_forwarders.Length);
+                               arr [type_forwarders.Length] = t;
                                type_forwarders = arr;
                        }
                }
-#endif
 
                public ModuleBuilder DefineDynamicModule (string name)
                {
@@ -726,6 +856,17 @@ namespace System.Reflection.Emit
                        }
                }
 
+               internal bool IsRun {
+                       get {
+                               return access == (uint)AssemblyBuilderAccess.Run || access == (uint)AssemblyBuilderAccess.RunAndSave
+#if NET_4_0
+                                        || access == (uint)AssemblyBuilderAccess.RunAndCollect
+#endif
+                               ;
+
+                       }
+               }
+
                internal string AssemblyDir {
                        get {
                                return dir;
@@ -746,7 +887,6 @@ namespace System.Reflection.Emit
                        }
                }
 
-#if NET_2_0 || BOOTSTRAP_NET_2_0
                ModuleBuilder manifest_module;
 
                //
@@ -758,18 +898,19 @@ namespace System.Reflection.Emit
                                manifest_module = DefineDynamicModule ("Default Dynamic Module");
                        return manifest_module;
                }
-#endif
 
-#if NET_2_0
+               [MonoLimitation ("No support for PE32+ assemblies for AMD64 and IA64")]
                public 
-#else
-               internal
-#endif
                void Save (string assemblyFileName, PortableExecutableKinds portableExecutableKind, ImageFileMachine imageFileMachine)
                {
                        this.peKind = portableExecutableKind;
                        this.machine = imageFileMachine;
 
+                       if ((peKind & PortableExecutableKinds.PE32Plus) != 0 || (peKind & PortableExecutableKinds.Unmanaged32Bit) != 0)
+                               throw new NotImplementedException (peKind.ToString ());
+                       if (machine == ImageFileMachine.IA64 || machine == ImageFileMachine.AMD64)
+                               throw new NotImplementedException (machine.ToString ());
+
                        if (resource_writers != null) {
                                foreach (IResourceWriter writer in resource_writers) {
                                        writer.Generate ();
@@ -881,10 +1022,14 @@ namespace System.Reflection.Emit
                                } else if (attrname == "System.Reflection.AssemblyFlagsAttribute") {
                                        data = customBuilder.Data;
                                        pos = 2;
-                                       flags = (uint) data [pos];
+                                       flags |= (uint) data [pos];
                                        flags |= ((uint) data [pos + 1]) << 8;
                                        flags |= ((uint) data [pos + 2]) << 16;
                                        flags |= ((uint) data [pos + 3]) << 24;
+
+                                       // ignore PublicKey flag if assembly is not strongnamed
+                                       if (sn == null)
+                                               flags &= ~(uint) AssemblyNameFlags.PublicKey;
                                }
                        }
 
@@ -899,9 +1044,7 @@ namespace System.Reflection.Emit
                        }
                }
 
-#if NET_2_0
                [ComVisible (true)]
-#endif
                public void SetCustomAttribute ( ConstructorInfo con, byte[] binaryAttribute) {
                        if (con == null)
                                throw new ArgumentNullException ("con");
@@ -1020,6 +1163,21 @@ namespace System.Reflection.Emit
                        return an;
                }
 
+               /*Warning, @typeArguments must be a mscorlib internal array. So make a copy before passing it in*/
+               internal Type MakeGenericType (Type gtd, Type[] typeArguments)
+               {
+                       if (!IsCompilerContext)
+                               return new MonoGenericClass (gtd, typeArguments);
+
+                       GenericInstanceKey key = new GenericInstanceKey (gtd, typeArguments);
+                       MonoGenericClass res = (MonoGenericClass)generic_instances [key];
+                       if (res == null) {
+                               res = new MonoGenericClass (gtd, typeArguments);
+                               generic_instances [key] = res;
+                       }
+                       return res;
+               }
+
                void _AssemblyBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
                {
                        throw new NotImplementedException ();
@@ -1039,5 +1197,55 @@ namespace System.Reflection.Emit
                {
                        throw new NotImplementedException ();
                }
+
+#if NET_4_0
+               public override Type GetType (string name, bool throwOnError, bool ignoreCase)
+               {
+                       if (name == null)
+                               throw new ArgumentNullException (name);
+                       if (name.Length == 0)
+                       throw new ArgumentException ("name", "Name cannot be empty");
+
+                       var res = InternalGetType (null, name, throwOnError, ignoreCase);
+                       if (res is TypeBuilder) {
+                               if (throwOnError)
+                                       throw new TypeLoadException (string.Format ("Could not load type '{0}' from assembly '{1}'", name, this.name));
+                               return null;
+                       }
+                       return res;
+               }
+
+               public override Module GetModule (String name)
+               {
+                       if (name == null)
+                               throw new ArgumentNullException ("name");
+                       if (name.Length == 0)
+                               throw new ArgumentException ("Name can't be empty");
+
+                       if (modules == null)
+                               return null;
+
+                       foreach (Module module in modules) {
+                               if (module.ScopeName == name)
+                                       return module;
+                       }
+
+                       return null;
+               }
+
+               public override Module[] GetModules (bool getResourceModules)
+               {
+                       Module[] modules = GetModulesInternal ();
+
+                       if (!getResourceModules) {
+                               ArrayList result = new ArrayList (modules.Length);
+                               foreach (Module m in modules)
+                                       if (!m.IsResource ())
+                                               result.Add (m);
+                               return (Module[])result.ToArray (typeof (Module));
+                       }
+                       return modules;
+               }
+#endif
        }
 }