* Makefile: Build the make-map.exe in Mono.Unix.Native; add /nowarn:0618 to
[mono.git] / mcs / ilasm / codegen / ExternTable.cs
index 503f752fe811fce3f204cd09aa3e4461c1261540..9339658a5731385741d5b3617e4d73cd3074d59a 100644 (file)
 using System;
 using System.Collections;
 using System.Reflection;
+using System.Security;
 
 namespace Mono.ILASM {
 
-        public abstract class ExternRef {
+        public interface IScope {
+                ExternTypeRef GetTypeRef (string full_name, bool is_valuetype, ExternTable table);
+                PEAPI.ClassRef GetType (string full_name, bool is_valuetype);
+        }
+       
+        public abstract class ExternRef : ICustomAttrTarget, IScope {
 
                 protected string name;
                 protected Hashtable class_table;
                 protected Hashtable typeref_table;
+                protected ArrayList customattr_list;
+                protected bool is_resolved;
 
                 public abstract void Resolve (CodeGen codegen);
                 public abstract PEAPI.IExternRef GetExternRef ();
@@ -29,50 +37,60 @@ namespace Mono.ILASM {
                         class_table = new Hashtable ();
                 }
 
-                public ExternTypeRef GetTypeRef (string full_name, bool is_valuetype, ExternTable table)
+               public string Name {
+                       get { return name; }
+               }
+
+                public void AddCustomAttribute (CustomAttr customattr)
                 {
-                        ExternTypeRef type_ref = typeref_table [full_name] as ExternTypeRef;
-                        
-                        if (type_ref != null)
-                                return type_ref;
-                        
-                        type_ref = new ExternTypeRef (this, full_name, is_valuetype, table);
-                        typeref_table [full_name] = type_ref;
-                        
-                        return type_ref;
+                        if (customattr_list == null)
+                                customattr_list = new ArrayList ();
+
+                        customattr_list.Add (customattr);
                 }
 
-                public PEAPI.ClassRef GetType (string full_name)
+                public ExternTypeRef GetTypeRef (string full_name, bool is_valuetype, ExternTable table)
                 {
-                        PEAPI.ClassRef klass = class_table[full_name] as PEAPI.ClassRef;
+                        string first= full_name;
+                        string rest = "";
+                        int slash = full_name.IndexOf ('/');
+                        if (slash > 0) {
+                                first = full_name.Substring (0, slash);
+                                rest = full_name.Substring (slash + 1);
+                        }
+                               
+                        ExternTypeRef type_ref = typeref_table [first] as ExternTypeRef;
                         
-                        if (klass != null)
-                                return klass;
-
-                        string name_space, name;
-                        ExternTable.GetNameAndNamespace (full_name, out name_space, out name);
-
-                        klass = (PEAPI.ClassRef) GetExternRef ().AddClass (name_space, name);
-                        class_table[full_name] = klass;
+                        if (type_ref != null) {
+                                if (is_valuetype && rest == "")
+                                        type_ref.MakeValueClass ();
+                        } else {
+                                type_ref = new ExternTypeRef (this, first, is_valuetype, table);
+                                typeref_table [first] = type_ref;
+                        }
 
-                        return klass;
+                        return (rest == "" ? type_ref : type_ref.GetTypeRef (rest, is_valuetype, table));
                 }
 
-                public PEAPI.ClassRef GetValueType (string full_name)
+                public PEAPI.ClassRef GetType (string full_name, bool is_valuetype)
                 {
                         PEAPI.ClassRef klass = class_table[full_name] as PEAPI.ClassRef;
-
-                        if (klass != null) 
+                        
+                        if (klass != null)
                                 return klass;
 
                         string name_space, name;
                         ExternTable.GetNameAndNamespace (full_name, out name_space, out name);
 
-                        klass = (PEAPI.ClassRef) GetExternRef ().AddValueClass (name_space, name);
-                        class_table[full_name] = klass;
+                        if (is_valuetype)
+                                klass = (PEAPI.ClassRef) GetExternRef ().AddValueClass (name_space, name);
+                        else        
+                                klass = (PEAPI.ClassRef) GetExternRef ().AddClass (name_space, name);
 
+                        class_table [full_name] = klass;
                         return klass;
                 }
+
         }
 
         public class ExternModule : ExternRef {
@@ -85,7 +103,15 @@ namespace Mono.ILASM {
 
                 public override void Resolve (CodeGen codegen)
                 {
+                        if (is_resolved)
+                                return;
+
                         ModuleRef = codegen.PEFile.AddExternModule (name);
+                        if (customattr_list != null)
+                                foreach (CustomAttr customattr in customattr_list)
+                                        customattr.AddTo (codegen, ModuleRef);
+
+                        is_resolved = true;
                 }
 
                 
@@ -95,7 +121,7 @@ namespace Mono.ILASM {
                 }
         }
 
-        public class ExternAssembly : ExternRef {
+        public class ExternAssembly : ExternRef, IDeclSecurityTarget {
                         
                 public PEAPI.AssemblyRef AssemblyRef;
 
@@ -104,6 +130,7 @@ namespace Mono.ILASM {
                 private byte [] public_key_token;
                 private string locale;
                 private byte [] hash;
+                private DeclSecurity decl_sec;
 
                 public ExternAssembly (string name, AssemblyName asmb_name) : base (name)
                 {
@@ -113,6 +140,9 @@ namespace Mono.ILASM {
 
                 public override void Resolve (CodeGen code_gen)
                 {
+                        if (is_resolved)
+                                return;
+
                         AssemblyRef = code_gen.PEFile.AddExternAssembly (name);
                         if (major != -1)
                                 AssemblyRef.AddVersionInfo (major, minor, build, revision);
@@ -124,13 +154,39 @@ namespace Mono.ILASM {
                                 AssemblyRef.AddCulture (locale);
                         if (hash != null)
                                 AssemblyRef.AddHash (hash);
+
+                        if (customattr_list != null)
+                                foreach (CustomAttr customattr in customattr_list)
+                                        customattr.AddTo (code_gen, AssemblyRef);
+                                        
+                        if (decl_sec != null)
+                                decl_sec.AddTo (code_gen, AssemblyRef);
+
                         class_table = new Hashtable ();
+
+                        is_resolved = true;
                 }
 
                 public override PEAPI.IExternRef GetExternRef ()
                 {
                         return AssemblyRef;
                 }
+                
+                public void AddPermissionSet (PEAPI.SecurityAction sec_action, PermissionSet ps)
+                {
+                        if (decl_sec == null)
+                                decl_sec = new DeclSecurity ();
+
+                        decl_sec.AddPermissionSet (sec_action, ps);        
+                }
+                
+                public void AddPermission (PEAPI.SecurityAction sec_action, IPermission iper)
+                {
+                        if (decl_sec == null)
+                                decl_sec = new DeclSecurity ();
+
+                        decl_sec.AddPermission (sec_action, iper);
+                }
 
                 public void SetVersion (int major, int minor, int build, int revision)
                 {
@@ -159,6 +215,7 @@ namespace Mono.ILASM {
                 {
                         this.hash = hash;
                 }
+
         }
 
         
@@ -166,8 +223,9 @@ namespace Mono.ILASM {
 
                 Hashtable assembly_table;
                 Hashtable module_table;
+                bool is_resolved;
                 
-                public ExternTable ()
+                public void AddCorlib ()
                 {
                         // Add mscorlib
                         string mscorlib_name = "mscorlib";
@@ -220,19 +278,29 @@ namespace Mono.ILASM {
 
                 public void Resolve (CodeGen code_gen)
                 {
-                        // Assembly table is never null because we add mscorlib
-                        foreach (ExternAssembly ext in assembly_table.Values)
-                                ext.Resolve (code_gen);
+                        if (is_resolved)
+                                return;
+
+                        if (assembly_table != null)
+                                foreach (ExternAssembly ext in assembly_table.Values)
+                                        ext.Resolve (code_gen);
                         if (module_table == null)
                                 return;
                         foreach (ExternModule ext in module_table.Values)
                                 ext.Resolve (code_gen);
+
+                       is_resolved = true;     
                 }
 
                 public ExternTypeRef GetTypeRef (string asmb_name, string full_name, bool is_valuetype)
                 {
-                        ExternAssembly ext_asmb;
-                        ext_asmb = assembly_table[asmb_name] as ExternAssembly;
+                        ExternAssembly ext_asmb = null;
+                        if (assembly_table == null && (asmb_name == "mscorlib" || asmb_name == "corlib"))
+                                /* AddCorlib if mscorlib is being referenced but
+                                   we haven't encountered a ".assembly 'name'" as yet. */
+                                AddCorlib ();
+                        if (assembly_table != null)
+                                ext_asmb = assembly_table[asmb_name] as ExternAssembly;
 
                         if (ext_asmb == null)
                                 throw new Exception (String.Format ("Assembly {0} not defined.", asmb_name));