* typemanager.cs: Removed TypeManager.builder_to_attr which was
authorJambunathan K <jambunathan@mono-cvs.ximian.com>
Sat, 6 Nov 2004 18:25:25 +0000 (18:25 -0000)
committerJambunathan K <jambunathan@mono-cvs.ximian.com>
Sat, 6 Nov 2004 18:25:25 +0000 (18:25 -0000)
redundant and replaced LookupAttr with LookupClass.

* rootcontext.cs:
* module.cs: Miscellaneous changes

* mb-parser.jay: Upadted actions of the attributes grammar

* codegen.cs: 1) Introduced new classes CommonAssemblyModulClass,
AssemblyClass, ModuleClass 2) Introduced following methods
CodeGen.AddGlobalAttributes, CodeGen.EmitGlobalAttributes

* class.cs: Factored out TypeContainer.AttributeUsage

* attribute.cs: 1) Factored out Attribute.UsageAttribute and
Attribute.GetAttributeUsage 2) Introducing the new base class
Attributable from which all Attributable types will be derived in
the future 3) Added new accessors Attribute.IsAssemblyAttribute
and Attribute.IsModuleAttribute

svn path=/trunk/mcs/; revision=35771

mcs/mbas/ChangeLog
mcs/mbas/attribute.cs
mcs/mbas/class.cs
mcs/mbas/codegen.cs
mcs/mbas/mb-parser.jay
mcs/mbas/module.cs
mcs/mbas/rootcontext.cs
mcs/mbas/typemanager.cs

index 1523c9969c3cd31728da2ff1b4cdfbcf0dc083e5..71294df5827f4d557895e5cf203929b7ed19f298 100644 (file)
@@ -1,3 +1,24 @@
+2004-11-07  Jambunathan K  <kjambunathan@novell.com>
+       * typemanager.cs: Removed TypeManager.builder_to_attr which was
+       redundant and replaced LookupAttr with LookupClass.
+
+       * rootcontext.cs: 
+       * module.cs: Miscellaneous changes
+
+       * mb-parser.jay: Upadted actions of the attributes grammar
+
+       * codegen.cs: 1) Introduced new classes CommonAssemblyModulClass,
+       AssemblyClass, ModuleClass 2) Introduced following methods
+       CodeGen.AddGlobalAttributes, CodeGen.EmitGlobalAttributes
+
+       * class.cs: Factored out TypeContainer.AttributeUsage
+
+       * attribute.cs: 1) Factored out Attribute.UsageAttribute and
+       Attribute.GetAttributeUsage 2) Introducing the new base class
+       Attributable from which all Attributable types will be derived in
+       the future 3) Added new accessors Attribute.IsAssemblyAttribute
+       and Attribute.IsModuleAttribute
+
 2004-11-06  Jambunathan K  <kjambunathan@novell.com>
        * rootcontext.cs: Removed the static field -
        RootContext:global_attribute.
index 7a472bf4d9cf19dcc44059aff3db0db8b328165e..5a21e2c3c106aec26987a6c7936887bc21e9e3a3 100644 (file)
@@ -20,6 +20,30 @@ using System.Text;
 
 namespace Mono.MonoBASIC {
 
+       /// <summary>
+       ///   Base class for objects that can have Attributes applied to them.
+       /// </summary>
+       public abstract class Attributable {
+               /// <summary>
+               ///   Attributes for this type
+               /// </summary>
+               Attributes attributes;
+
+               public Attributable(Attributes attrs)
+               {
+                       attributes = attrs;
+               }
+
+               public Attributes OptAttributes 
+               {
+                       get {
+                               return attributes;
+                       }
+                       set {
+                               attributes = value;
+                       }
+               }
+       };
        public class Attribute {
                public readonly string ExplicitTarget;
                public readonly string    Name;
@@ -28,16 +52,17 @@ namespace Mono.MonoBASIC {
                Location Location;
 
                public Type Type;
-               
-               //
-               // The following are only meaningful when the attribute
-               // being emitted is one of the builtin ones
-               //
-               AttributeTargets Targets;
-               bool AllowMultiple;
-               bool Inherited;
 
-               bool UsageAttr = false;
+               // Is non-null if type is AttributeUsageAttribute
+               AttributeUsageAttribute usage_attribute;
+
+               public AttributeUsageAttribute UsageAttribute {
+                       get {
+                               return usage_attribute;
+                       }
+               }
+               
+               bool usage_attr = false;
                
                MethodImplOptions ImplOptions;
                UnmanagedType     UnmanagedType;
@@ -105,6 +130,7 @@ namespace Mono.MonoBASIC {
                                " missing a using directive or an assembly reference ?)");
                        return null;
                }
+               
 
                public Type ResolveType (EmitContext ec)
                {
@@ -123,10 +149,10 @@ namespace Mono.MonoBASIC {
                        bool MethodImplAttr = false;
                        bool MarshalAsAttr = false;
 
-                       UsageAttr = false;
+                       usage_attr = false;
 
                        if (Type == TypeManager.attribute_usage_type)
-                               UsageAttr = true;
+                               usage_attr = true;
                        if (Type == TypeManager.methodimpl_attr_type)
                                MethodImplAttr = true;
                        if (Type == TypeManager.marshal_as_attr_type)
@@ -171,8 +197,8 @@ namespace Mono.MonoBASIC {
                                        return null;
                                }
                                
-                               if (UsageAttr)
-                                       this.Targets = (AttributeTargets) pos_values [0];
+                               if (usage_attr)
+                                       usage_attribute = new AttributeUsageAttribute ((AttributeTargets) pos_values [0]);
                                
                                if (MethodImplAttr)
                                        this.ImplOptions = (MethodImplOptions) pos_values [0];
@@ -225,11 +251,11 @@ namespace Mono.MonoBASIC {
                                                object o = ((Constant) e).GetValue ();
                                                prop_values.Add (o);
                                                
-                                               if (UsageAttr) {
+                                               if (usage_attr) {
                                                        if (member_name == "AllowMultiple")
-                                                               this.AllowMultiple = (bool) o;
+                                                               usage_attribute.AllowMultiple = (bool) o;
                                                        if (member_name == "Inherited")
-                                                               this.Inherited = (bool) o;
+                                                               usage_attribute.Inherited = (bool) o;
                                                }
                                                
                                        } else if (e is TypeOf) {
@@ -342,32 +368,7 @@ namespace Mono.MonoBASIC {
                static string GetValidPlaces (Attribute attr)
                {
                        StringBuilder sb = new StringBuilder ();
-                       AttributeTargets targets = 0;
-                       
-                       TypeContainer a = TypeManager.LookupAttr (attr.Type);
-
-                       if (a == null) {
-                               
-                               System.Attribute [] attrs = null;
-                               
-                               try {
-                                       attrs = System.Attribute.GetCustomAttributes (attr.Type);
-                                       
-                               } catch {
-                                       Report.Error (-20, attr.Location, "Cannot find attribute type " + attr.Name +
-                                                     " (maybe you forgot to set the usage using the" +
-                                                     " AttributeUsage attribute ?).");
-                                       return null;
-                               }
-                                       
-                               foreach (System.Attribute tmp in attrs)
-                                       if (tmp is AttributeUsageAttribute) {
-                                               targets = ((AttributeUsageAttribute) tmp).ValidOn;
-                                               break;
-                                       }
-                       } else
-                               targets = a.Targets;
-
+                       AttributeTargets targets = attr.GetAttributeUsage ().ValidOn;
                        
                        if ((targets & AttributeTargets.Assembly) != 0)
                                sb.Append ("'assembly' ");
@@ -425,29 +426,8 @@ namespace Mono.MonoBASIC {
 
                public static bool CheckAttribute (Attribute a, object element)
                {
-                       TypeContainer attr = TypeManager.LookupAttr (a.Type);
-                       AttributeTargets targets = 0;
-
-                       
-                       if (attr == null) {
-
-                               System.Attribute [] attrs = null;
-                               
-                               try {
-                                       attrs = System.Attribute.GetCustomAttributes (a.Type);
-
-                               } catch {
-                                       Report.Error (-20, a.Location, "Cannot find attribute type " + a.Name +
-                                                     " (maybe you forgot to set the usage using the" +
-                                                     " AttributeUsage attribute ?).");
-                                       return false;
-                               }
-                                       
-                               foreach (System.Attribute tmp in attrs)
-                                       if (tmp is AttributeUsageAttribute) 
-                                               targets = ((AttributeUsageAttribute) tmp).ValidOn;
-                       } else
-                               targets = attr.Targets;
+                       TypeContainer attr = TypeManager.LookupClass (a.Type);
+                       AttributeTargets targets = a.GetAttributeUsage ().ValidOn;
 
                        if (element is Class) {
                                if ((targets & AttributeTargets.Class) != 0)
@@ -511,11 +491,31 @@ namespace Mono.MonoBASIC {
                                        return true;
                                else
                                        return false;
-                       }
+                       } else if (element is ModuleBuilder){
+                                       if ((targets & AttributeTargets.Module) != 0)
+                                               return true;
+                                       else
+                                               return false;
+                               }  
 
                        return false;
                }
 
+               /// <summary>
+               /// Returns AttributeUsage attribute for this type
+               /// </summary>
+               public AttributeUsageAttribute GetAttributeUsage ()
+               {
+                       Class attr_class = (Class) TypeManager.LookupClass (Type);
+
+                       if (attr_class == null) {
+                               object[] usage_attr = Type.GetCustomAttributes (TypeManager.attribute_usage_type, true);
+                               return (AttributeUsageAttribute)usage_attr [0];
+                       }
+               
+                       return attr_class.AttributeUsage;
+               }
+
                //
                // This method should be invoked to pull the IndexerName attribute from an
                // Indexer if it exists.
@@ -678,11 +678,8 @@ namespace Mono.MonoBASIC {
                                } else if (kind is TypeContainer) {
                                        TypeContainer tc = (TypeContainer) kind;
                                                
-                                       if (a.UsageAttr) {
-                                               tc.Targets = a.Targets;
-                                               tc.AllowMultiple = a.AllowMultiple;
-                                               tc.Inherited = a.Inherited;
-                                                       
+                                       if (a.UsageAttribute != null) {
+                                               tc.AttributeUsage = a.UsageAttribute;
                                        } else if (a.Type == TypeManager.default_member_type) {
                                                if (tc.Indexers != null) {
                                                        Report.Error (646, loc, "Cannot specify the DefaultMember attribute on" + " a type containing an indexer");
@@ -831,7 +828,19 @@ namespace Mono.MonoBASIC {
                                mb.SetImplementationFlags (MethodImplAttributes.PreserveSig);
                        
                        return mb;
-               }                       
+               }
+
+               public bool IsAssemblyAttribute {
+                       get {
+                               return ExplicitTarget == "assembly";
+                       }
+               }
+
+               public bool IsModuleAttribute {
+                       get {
+                               return ExplicitTarget == "module";
+                       }
+               }
        }
        
        public class Attributes {
@@ -849,18 +858,22 @@ namespace Mono.MonoBASIC {
                        Attrs = attrs;
                }
 
-               public void AddAttributes (ArrayList attrs)
+               public void Add (Attribute attr)
                {
-                       Attrs.AddRange (attrs);
+                       Attrs.Add (attr);
                }
 
-               public bool Contains (Type t)
+               public void Add (ArrayList attrs)
                {
-                       foreach (Attribute a in Attrs){
-                                       if (a.Type == t)
-                                               return true;
-                               }
-                       return false;
-               }                       
+                       Attrs.AddRange (attrs);
+               }
+
+//             public void Emit (EmitContext ec, Attributable ias)
+//             {
+//                     ListDictionary ld = new ListDictionary ();
+       
+//                     foreach (Attribute a in Attrs)
+//                             a.Emit (ec, ias, ld);
+//             }
        }
 }
index f8d23d353dace48411c57373daeda1edba40db8f..90ebe3fe21ccbb654d34ce8c93d1dfaa54970213 100644 (file)
@@ -121,9 +121,17 @@ namespace Mono.MonoBASIC {
 
                // Information in the case we are an attribute type
 
-               public AttributeTargets Targets = AttributeTargets.All;
-               public bool AllowMultiple = false;
-               public bool Inherited;
+               AttributeUsageAttribute attribute_usage = new AttributeUsageAttribute (AttributeTargets.All);
+
+               public AttributeUsageAttribute AttributeUsage {
+                       get {
+                               return attribute_usage;
+                       }
+
+                       set {
+                               attribute_usage = value;
+                       }                               
+               }
 
                // The interfaces we implement.
                Type [] ifaces;
@@ -875,7 +883,6 @@ namespace Mono.MonoBASIC {
                            (parent == TypeManager.attribute_type ||
                             parent.IsSubclassOf (TypeManager.attribute_type))) {
                                RootContext.RegisterAttribute (this);
-                               TypeManager.RegisterAttrType (TypeBuilder, this);
                        } else
                                RootContext.RegisterOrder (this); 
                                
index 0ece01942c1f1f45d48a6dce6968b80eedc79609..19a63a4db6f2ca0e998c8a169c823e3bc4ebcb82 100644 (file)
@@ -21,11 +21,38 @@ namespace Mono.MonoBASIC {
        /// </summary>
        public class CodeGen {
                static AppDomain current_domain;
-               public static AssemblyBuilder AssemblyBuilder;
-               public static ModuleBuilder   ModuleBuilder;
+
+               public static AssemblyBuilder AssemblyBuilder {
+                       get { 
+                               return Assembly.Builder;
+                       }                               
+
+                       set {
+                               Assembly.Builder = value;
+                       }                               
+               }
+
+               public static ModuleBuilder ModuleBuilder {
+                       get { 
+                               return Module.Builder;
+                       }                               
+
+                       set {
+                               Module.Builder = value;
+                       }                               
+               }
+
+               public static AssemblyClass Assembly;
+               public static ModuleClass Module;
 
                static public ISymbolWriter SymbolWriter;
 
+               static CodeGen ()
+               {
+                       Assembly = new AssemblyClass ();
+                       Module = new ModuleClass ();
+               }
+
                public static string Basename (string name)
                {
                        int pos = name.LastIndexOf ("/");
@@ -203,6 +230,23 @@ namespace Mono.MonoBASIC {
                                SymbolWriter.Close ();
                        }
                }
+
+               public static void AddGlobalAttributes (ArrayList attrs)
+               {
+                       foreach (Attribute attr in attrs) {
+                               if (attr.IsAssemblyAttribute)
+                                       Assembly.AddAttribute (attr);
+                               else if (attr.IsModuleAttribute)
+                                       Module.AddAttribute (attr);
+                       }
+               }
+
+               public static void EmitGlobalAttributes ()
+               {
+                       //Assembly.Emit (Tree.Types);
+                       //Module.Emit (Tree.Types);
+
+               }
        }
 
        /// <summary>
@@ -689,4 +733,73 @@ namespace Mono.MonoBASIC {
                        }
                }
        }
+
+       
+       public abstract class CommonAssemblyModulClass: Attributable {
+               protected CommonAssemblyModulClass ():
+                       base (null)
+               {
+               }
+
+               public void AddAttribute (Attribute attr)
+               {
+                       if (OptAttributes == null) {
+                               OptAttributes = new Attributes (attr);
+                       } else {
+                               OptAttributes.Add (attr);
+                       }
+               }
+
+//             public virtual void Emit (TypeContainer tc) 
+//             {
+//                     if (OptAttributes == null)
+//                             return;
+//                     EmitContext ec = new EmitContext (tc, Location.Null, null, null, 0, false);
+//                     OptAttributes.Emit (ec, this);
+//             }
+                
+//             protected Attribute GetClsCompliantAttribute ()
+//             {
+//                     if (OptAttributes == null)
+//                             return null;
+
+//                     EmitContext temp_ec = new EmitContext (new RootTypes (), Mono.CSharp.Location.Null, null, null, 0, false);
+//                     Attribute a = OptAttributes.Search (TypeManager.cls_compliant_attribute_type, temp_ec);
+//                     if (a != null) {
+//                             a.Resolve (temp_ec);
+//                     }
+//                     return a;
+//             }
+       }
+
+       public class AssemblyClass: CommonAssemblyModulClass 
+       {
+               public AssemblyBuilder Builder;
+
+               public AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Assembly;
+                       }
+               }
+               
+//             public override void Emit (TypeContainer tc)
+//             {
+//                     base.Emit (tc);
+//             }
+       }
+       
+       public class ModuleClass: CommonAssemblyModulClass 
+       {
+               public ModuleBuilder Builder;
+
+               public ModuleClass ()
+               {
+               }
+       
+               public AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Module;
+                       }
+               }
+       }
 }
index 5121a5a2d2c8a7c8332285dddbc86481898562b7..8669751edcf09f4b7d6db632d2cde26d2a9ba732 100644 (file)
@@ -848,7 +848,12 @@ attribute_sections
                if (expecting_local_attribs) {
                        local_attrib_section_added = true;
                        allow_global_attribs = false;
-               }                       
+
+                       $$ = new Attributes ((ArrayList) $1);
+               }       
+
+               if (expecting_global_attribs)
+                       CodeGen.AddGlobalAttributes ((ArrayList) $1);
 
                expecting_local_attribs = false;
                expecting_global_attribs = false;
@@ -859,8 +864,9 @@ attribute_sections
           }
           attribute_section    
          {
-               $$ = null;
+               $$ = $1;
                if ($3 != null) {
+                       ArrayList attrs = (ArrayList) $3;
 
                        if (expecting_local_attribs) {
                                if (local_attrib_section_added) {
@@ -870,16 +876,21 @@ attribute_sections
                                        break;
                                }
 
+                               if ($1 == null)
+                                       $$ = new Attributes (attrs);
+                               else 
+                                       ((Attributes) $1).Add (attrs);
+
                                local_attrib_section_added = true;
                                allow_global_attribs = false;
                        }
+
+                       if (expecting_global_attribs)
+                               CodeGen.AddGlobalAttributes ((ArrayList) $3);
                }       
 
                expecting_local_attribs = false;
                expecting_global_attribs = false;
-               
-               $$ = $1; 
-
          }
        ;
 
@@ -900,7 +911,7 @@ attribute_section
                                }
                        }
 
-                       $$ = new Attributes ((ArrayList) $2);
+                       $$ = $2;
                }
          }
        ; 
index bb284b5d516e6f4486c90d6e73ad12e5fe122367..4506a1f7b83b1e551f0694f89f310edb3db4f5f1 100644 (file)
@@ -27,7 +27,7 @@ namespace Mono.MonoBASIC
                        if (attrs == null)
                                attrs = new Attributes(al);
                        else
-                               attrs.AddAttributes(al);
+                               attrs.Add(al);
                }
        }
        
index 4e4bd4ac61247568d27b46ec1f7a6bfc5dda010e..63bd527b07cf5a1939ad548e8772ad9247fdddbc 100644 (file)
@@ -754,6 +754,8 @@ namespace Mono.MonoBASIC {
                        if (attribute_types != null)
                                foreach (TypeContainer tc in attribute_types)
                                        tc.Emit ();
+
+                       CodeGen.EmitGlobalAttributes ();
                        
                        if (type_container_resolve_order != null) {
                                foreach (TypeContainer tc in type_container_resolve_order)
index 50c3348f2132bbf9ba438451f74a5f366331f5cd..1b44fdf9534d63ae9505b3780deb74201f14188c 100644 (file)
@@ -212,12 +212,6 @@ public class TypeManager {
        // <remarks>
        static Hashtable method_internal_params;
 
-       // <remarks>
-       //  Keeps track of attribute types
-       // </remarks>
-
-       static Hashtable builder_to_attr;
-
        // <remarks>
        //  Keeps track of methods
        // </remarks>
@@ -310,7 +304,6 @@ public class TypeManager {
                typecontainers = new CaseInsensitiveHashtable ();
                
                builder_to_declspace = new PtrHashtable ();
-               builder_to_attr = new PtrHashtable ();
                builder_to_method = new PtrHashtable ();
                method_arguments = new PtrHashtable ();
                method_internal_params = new PtrHashtable ();
@@ -394,11 +387,6 @@ public class TypeManager {
                builder_to_method.Add (builder, method);
        }
 
-       public static void RegisterAttrType (Type t, TypeContainer tc)
-       {
-               builder_to_attr.Add (t, tc);
-       }
-
        /// <summary>
        ///   Returns the TypeContainer whose Type is 't' or null if there is no
        ///   TypeContainer for 't' (ie, the Type comes from a library)
@@ -433,10 +421,10 @@ public class TypeManager {
        {
                return builder_to_declspace [t] as Enum;
        }
-       
-       public static TypeContainer LookupAttr (Type t)
+
+       public static TypeContainer LookupClass (Type t)
        {
-               return (TypeContainer) builder_to_attr [t];
+               return builder_to_declspace [t] as TypeContainer;
        }
        
        /// <summary>