**** Merged from MCS ****
authorMartin Baulig <martin@novell.com>
Thu, 20 May 2004 12:24:59 +0000 (12:24 -0000)
committerMartin Baulig <martin@novell.com>
Thu, 20 May 2004 12:24:59 +0000 (12:24 -0000)
svn path=/trunk/mcs/; revision=27743

mcs/gmcs/ChangeLog
mcs/gmcs/attribute.cs
mcs/gmcs/class.cs
mcs/gmcs/codegen.cs
mcs/gmcs/cs-parser.jay
mcs/gmcs/parameter.cs

index 2d66e08cc31b278d0fc02eba48130990107a3888..f76e079a27ad4cff279b9da7ac66a9585df725c7 100755 (executable)
@@ -1,3 +1,33 @@
+2004-05-19  Raja R Harinath  <rharinath@novell.com>
+
+       * cs-parser.jay (attribute): Create a GlobalAttribute for the case
+       of a top-level attribute without an attribute target.
+       * attribute.cs (Attribute.Error_AttributeConstructorMismatch): 
+       Make non-static.
+       (Attribute.Conditional_GetConditionName), 
+       (Attribute.Obsolete_GetObsoleteMessage): Update.
+       (Attribute.IndexerName_GetIndexerName): New.  Attribute-specific
+       part of ScanForIndexerName.
+       (Attribute.CanIgnoreInvalidAttribute): New function.
+       (Attribute.ScanForIndexerName): Move to ...
+       (Attributes.ScanForIndexerName): ... here.
+       (Attributes.Attrs): Rename from now-misnamed AttributeSections.
+       (Attributes.Search): New internal variant that can choose not to
+       complain if types aren't resolved.  The original signature now
+       complains.
+       (Attributes.GetClsCompliantAttribute): Use internal variant, with
+       complaints suppressed.
+       (GlobalAttribute.CheckAttributeType): Overwrite ds.NamespaceEntry
+       only if it not useful.
+       (CanIgnoreInvalidAttribute): Ignore assembly attribute errors at
+       top-level for attributes that are shared between the assembly
+       and a top-level class.
+       * parameter.cs (ImplicitParameter): Rename from ParameterAtribute.
+       * class.cs: Update to reflect changes.
+       (DefineIndexers): Fuse loops.
+       * codegen.cs (GetAssemblyName): Update to reflect changes.  Accept
+       a couple more variants of attribute names.
+
 2004-05-18  Marek Safar  <marek.safar@seznam.cz>
 
        Fix bug #52585 (Implemented explicit attribute declaration)
index a69963be6a0edfc303d603460bba41b0828c554b..cb2067cbb6b7eb5f8485e1ea1eac3352842b243b 100644 (file)
@@ -120,9 +120,9 @@ namespace Mono.CSharp {
                                      "expression or array creation expression");\r
                }\r
 \r
-               static void Error_AttributeConstructorMismatch (Location loc)\r
+               void Error_AttributeConstructorMismatch ()\r
                {\r
-                       Report.Error (-6, loc,\r
+                       Report.Error (-6, Location,\r
                                       "Could not find a constructor for this argument list.");\r
                }\r
 \r
@@ -447,7 +447,7 @@ namespace Mono.CSharp {
                                 Location);\r
 \r
                        if (mg == null) {\r
-                               Error_AttributeConstructorMismatch (Location);\r
+                               Error_AttributeConstructorMismatch ();\r
                                return null;\r
                        }\r
 \r
@@ -455,7 +455,7 @@ namespace Mono.CSharp {
                                ec, (MethodGroupExpr) mg, pos_args, false, Location);\r
 \r
                        if (constructor == null) {\r
-                               Error_AttributeConstructorMismatch (Location);\r
+                               Error_AttributeConstructorMismatch ();\r
                                return null;\r
                        }\r
 \r
@@ -636,59 +636,6 @@ namespace Mono.CSharp {
                        return attr_class.AttributeUsage;\r
                }\r
 \r
-               //\r
-               // This method should be invoked to pull the IndexerName attribute from an\r
-               // Indexer if it exists.\r
-               //\r
-               public static string ScanForIndexerName (EmitContext ec, Attributes opt_attrs)\r
-               {\r
-                       if (opt_attrs == null)\r
-                               return null;\r
-                       if (opt_attrs.AttributeSections == null)\r
-                               return null;\r
-\r
-                       foreach (Attribute a in opt_attrs.AttributeSections) {\r
-                               if (a.ResolveType (ec, true) == null)\r
-                                       return null;\r
-\r
-                               if (a.Type != TypeManager.indexer_name_type)\r
-                                       continue;\r
-\r
-                               //\r
-                               // So we have found an IndexerName, pull the data out.\r
-                               //\r
-                               if (a.Arguments == null || a.Arguments [0] == null){\r
-                                       Error_AttributeConstructorMismatch (a.Location);\r
-                                       return null;\r
-                               }\r
-                               ArrayList pos_args = (ArrayList) a.Arguments [0];\r
-                               if (pos_args.Count == 0){\r
-                                       Error_AttributeConstructorMismatch (a.Location);\r
-                                       return null;\r
-                               }\r
-                                       \r
-                               Argument arg = (Argument) pos_args [0];\r
-                               if (!arg.Resolve (ec, a.Location))\r
-                                       return null;\r
-                                       \r
-                               Expression e = arg.Expr;\r
-                               if (!(e is StringConstant)){\r
-                                       Error_AttributeConstructorMismatch (a.Location);\r
-                                       return null;\r
-                               }\r
-\r
-                               //\r
-                               // Remove the attribute from the list\r
-                               //\r
-\r
-                               //TODO: It is very close to hack and it can crash here\r
-                               opt_attrs.AttributeSections.Remove (a);\r
-\r
-                               return (((StringConstant) e).Value);\r
-                       }\r
-                       return null;\r
-               }\r
-\r
                //\r
                // This pulls the condition name out of a Conditional attribute\r
                //\r
@@ -698,19 +645,19 @@ namespace Mono.CSharp {
                        // So we have a Conditional, pull the data out.\r
                        //\r
                        if (Arguments == null || Arguments [0] == null){\r
-                               Error_AttributeConstructorMismatch (Location);\r
+                               Error_AttributeConstructorMismatch ();\r
                                return null;\r
                        }\r
 \r
                        ArrayList pos_args = (ArrayList) Arguments [0];\r
                        if (pos_args.Count != 1){\r
-                               Error_AttributeConstructorMismatch (Location);\r
+                               Error_AttributeConstructorMismatch ();\r
                                return null;\r
                        }\r
 \r
                        Argument arg = (Argument) pos_args [0]; \r
                        if (!(arg.Expr is StringConstant)){\r
-                               Error_AttributeConstructorMismatch (Location);\r
+                               Error_AttributeConstructorMismatch ();\r
                                return null;\r
                        }\r
 \r
@@ -733,20 +680,20 @@ namespace Mono.CSharp {
                        if (pos_args.Count == 0)\r
                                return "";\r
                        else if (pos_args.Count > 2){\r
-                               Error_AttributeConstructorMismatch (Location);\r
+                               Error_AttributeConstructorMismatch ();\r
                                return null;\r
                        }\r
 \r
                        Argument arg = (Argument) pos_args [0]; \r
                        if (!(arg.Expr is StringConstant)){\r
-                               Error_AttributeConstructorMismatch (Location);\r
+                               Error_AttributeConstructorMismatch ();\r
                                return null;\r
                        }\r
 \r
                        if (pos_args.Count == 2){\r
                                Argument arg2 = (Argument) pos_args [1];\r
                                if (!(arg2.Expr is BoolConstant)){\r
-                                       Error_AttributeConstructorMismatch (Location);\r
+                                       Error_AttributeConstructorMismatch ();\r
                                        return null;\r
                                }\r
                                is_error = ((BoolConstant) arg2.Expr).Value;\r
@@ -755,6 +702,31 @@ namespace Mono.CSharp {
                        return ((StringConstant) arg.Expr).Value;\r
                }\r
 \r
+               public string IndexerName_GetIndexerName (EmitContext ec)\r
+               {\r
+                       if (Arguments == null || Arguments [0] == null){\r
+                               Error_AttributeConstructorMismatch ();\r
+                               return null;\r
+                       }\r
+                       ArrayList pos_args = (ArrayList) Arguments [0];\r
+                       if (pos_args.Count != 1) {\r
+                               Error_AttributeConstructorMismatch ();\r
+                               return null;\r
+                       }\r
+\r
+                       Argument arg = (Argument) pos_args [0];\r
+                       if (!arg.Resolve (ec, Location))\r
+                               return null;\r
+\r
+                       StringConstant sc = arg.Expr as StringConstant;\r
+                       if (sc == null){\r
+                               Error_AttributeConstructorMismatch ();\r
+                               return null;\r
+                       }\r
+\r
+                       return sc.Value;\r
+               }\r
+\r
                /// <summary>\r
                /// Returns value of CLSCompliantAttribute contructor parameter but because the method can be called\r
                /// before ApplyAttribute. We need to resolve the arguments.\r
@@ -837,6 +809,11 @@ namespace Mono.CSharp {
                        get { return ImplOptions == MethodImplOptions.InternalCall; }\r
                }\r
 \r
+               protected virtual bool CanIgnoreInvalidAttribute (Attributable ias)\r
+               {\r
+                       return false;\r
+               }\r
+\r
                /// <summary>\r
                /// Emit attribute for Attributable symbol\r
                /// </summary>\r
@@ -848,7 +825,10 @@ namespace Mono.CSharp {
 \r
                        AttributeUsageAttribute usage_attr = GetAttributeUsage ();\r
                        if ((usage_attr.ValidOn & ias.AttributeTargets) == 0) {\r
-                               Error_AttributeNotValidForElement (this, Location);\r
+                               // The parser applies toplevel attributes both to the assembly and\r
+                               // to a top-level class, if any.  So, be silent about them.\r
+                               if (! CanIgnoreInvalidAttribute (ias))\r
+                                       Error_AttributeNotValidForElement (this, Location);\r
                                return;\r
                        }\r
 \r
@@ -1084,28 +1064,40 @@ namespace Mono.CSharp {
 \r
                protected override Type CheckAttributeType (EmitContext ec, bool complain)\r
                {\r
-                       ec.DeclSpace.NamespaceEntry = ns;\r
+                       NamespaceEntry old = ec.DeclSpace.NamespaceEntry;\r
+                       if (old == null || old.NS == null || old.NS == Namespace.Root) \r
+                               ec.DeclSpace.NamespaceEntry = ns;\r
                        return base.CheckAttributeType (ec, complain);\r
                }\r
+\r
+               protected override bool CanIgnoreInvalidAttribute (Attributable ias)\r
+               {\r
+                       // Ignore error if this attribute is shared between the Assembly\r
+                       // and a top-level class.  The parser couldn't figure out which entity\r
+                       // this attribute belongs to.  If this attribute is erroneous, it should\r
+                       // be caught when it is processed by the top-level class.\r
+\r
+                       return (Target == null && ias is CommonAssemblyModulClass);\r
+               }\r
        }\r
 \r
        public class Attributes {\r
-               public ArrayList AttributeSections;\r
+               public ArrayList Attrs;\r
 \r
                public Attributes (Attribute a)\r
                {\r
-                       AttributeSections = new ArrayList ();\r
-                       AttributeSections.Add (a);\r
+                       Attrs = new ArrayList ();\r
+                       Attrs.Add (a);\r
                }\r
 \r
                public Attributes (ArrayList attrs)\r
                {\r
-                       AttributeSections = attrs;\r
+                       Attrs = attrs;\r
                }\r
 \r
                public void AddAttributes (ArrayList attrs)\r
                {\r
-                       AttributeSections.AddRange (attrs);\r
+                       Attrs.AddRange (attrs);\r
                }\r
 \r
                /// <summary>\r
@@ -1113,19 +1105,11 @@ namespace Mono.CSharp {
                /// </summary>\r
                public void CheckTargets (string[] possible_targets)\r
                {\r
-                       foreach (Attribute a in AttributeSections) {\r
+                       foreach (Attribute a in Attrs) {\r
                                if (a.Target == null)\r
                                        continue;\r
 \r
-                               bool valid_target = false;\r
-                               for (int i = 0; i < possible_targets.Length; ++i) {\r
-                                       if (a.Target == possible_targets [i]) {\r
-                                               valid_target = true;\r
-                                               break;\r
-                                       }\r
-                               }\r
-\r
-                               if (valid_target)\r
+                               if (((IList) possible_targets).Contains (a.Target))\r
                                        continue;\r
 \r
                                StringBuilder sb = new StringBuilder ();\r
@@ -1138,20 +1122,25 @@ namespace Mono.CSharp {
                        }\r
                }\r
 \r
-               public Attribute Search (Type t, EmitContext ec)\r
+               private Attribute Search (Type t, EmitContext ec, bool complain)\r
                {\r
-                       foreach (Attribute attr_section in AttributeSections) {\r
-                               if (attr_section.ResolveType (ec, false) == t)\r
-                                       return attr_section;\r
+                       foreach (Attribute a in Attrs) {\r
+                               if (a.ResolveType (ec, false) == t)\r
+                                       return a;\r
                        }\r
                        return null;\r
                }\r
 \r
+               public Attribute Search (Type t, EmitContext ec)\r
+               {\r
+                       return Search (t, ec, true);\r
+               }\r
+\r
                public void Emit (EmitContext ec, Attributable ias)\r
                {\r
                        ListDictionary ld = new ListDictionary ();\r
 \r
-                       foreach (Attribute a in AttributeSections) {\r
+                       foreach (Attribute a in Attrs) {\r
                                a.Emit (ec, ias, ld);\r
                        }\r
                }\r
@@ -1163,7 +1152,23 @@ namespace Mono.CSharp {
 \r
                public Attribute GetClsCompliantAttribute (EmitContext ec)\r
                {\r
-                       return Search (TypeManager.cls_compliant_attribute_type, ec);\r
+                       return Search (TypeManager.cls_compliant_attribute_type, ec, false);\r
+               }\r
+\r
+               /// <summary>\r
+               /// Pulls the IndexerName attribute from an Indexer if it exists.\r
+               /// </summary>\r
+               public string ScanForIndexerName (EmitContext ec)\r
+               {\r
+                       Attribute a = Search (TypeManager.indexer_name_type, ec);\r
+                       if (a == null)\r
+                               return null;\r
+\r
+                       // Remove the attribute from the list\r
+                       //TODO: It is very close to hack and it can crash here\r
+                       Attrs.Remove (a);\r
+\r
+                       return a.IndexerName_GetIndexerName (ec);\r
                }\r
        }\r
 \r
index 198e547b317127d3b1d70f10ade8038913641ba9..d2deb71d21d69315baf98c5c032caa4e811c1cd8 100755 (executable)
@@ -430,7 +430,7 @@ namespace Mono.CSharp {
                        }
                        
                        base.ApplyAttributeBuilder (a, cb);
-                       
+               } 
 
                public override AttributeTargets AttributeTargets {
                        get {
@@ -1107,32 +1107,25 @@ namespace Mono.CSharp {
                        // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
                        bool seen_normal_indexers = false;
                        foreach (Indexer i in Indexers) {
-                               if (i.MemberName.TypeName != null)
-                                       seen_normal_indexers = true;
-                               else if (seen_normal_indexers)
-                                       throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
-                       }
-                       foreach (Indexer i in Indexers){
                                string name;
 
                                i.Define (this);
 
                                name = i.IndexerName;
 
-                               if (i.InterfaceType != null)
+                               if (i.InterfaceType != null) {
+                                       if (seen_normal_indexers)
+                                               throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
                                        continue;
+                               }
+
+                               seen_normal_indexers = true;
 
-                               if (class_indexer_name == null){
+                               if (class_indexer_name == null)
                                        class_indexer_name = name;
-                                       continue;
-                               }
-                               
-                               if (name == class_indexer_name)
-                                       continue;
-                               
-                               Report.Error (
-                                       668, "Two indexers have different names, " +
-                                       " you should use the same name for all your indexers");
+                               else if (name != class_indexer_name)
+                                       Report.Error (668, "Two indexers have different names, " +
+                                                     " you should use the same name for all your indexers");
                        }
 
                        if (seen_normal_indexers && class_indexer_name == null)
@@ -3757,10 +3750,10 @@ namespace Mono.CSharp {
 
                public virtual bool ApplyAttributes (Attributes opt_attrs, bool is_method, DeclSpace ds)
                {
-                       if ((opt_attrs == null) || (opt_attrs.AttributeSections == null))
+                       if ((opt_attrs == null) || (opt_attrs.Attrs == null))
                                return true;
 
-                       foreach (Attribute a in opt_attrs.AttributeSections) {
+                       foreach (Attribute a in opt_attrs.Attrs) {
                                Type attr_type = a.ResolveType (ec, true);
                                if (attr_type == TypeManager.conditional_attribute_type) {
                                        if (!ApplyConditionalAttribute (a))
@@ -4967,7 +4960,7 @@ namespace Mono.CSharp {
                public class SetMethod: PropertyMethod {
 
                        static string[] attribute_targets = new string [] { "method", "param", "return" };
-                       ParameterAtribute param_attr;
+                       ImplicitParameter param_attr;
 
                        public SetMethod (MethodCore method, Accessor accessor):
                                base (method, accessor)
@@ -4978,7 +4971,7 @@ namespace Mono.CSharp {
                        {
                                if (a.Target == "param") {
                                        if (param_attr == null)
-                                               param_attr = new ParameterAtribute (method_data.MethodBuilder);
+                                               param_attr = new ImplicitParameter (method_data.MethodBuilder);
 
                                        param_attr.ApplyAttributeBuilder (a, cb);
                                        return;
@@ -5712,7 +5705,7 @@ namespace Mono.CSharp {
                        protected MethodData method_data;
                        Block block;
                        ReturnParameter return_attributes;
-                       ParameterAtribute param_attr;
+                       ImplicitParameter param_attr;
 
                        static string[] attribute_targets = new string [] { "method", "param", "return" };
 
@@ -5741,7 +5734,7 @@ namespace Mono.CSharp {
 
                                if (a.Target == "param") {
                                        if (param_attr == null)
-                                               param_attr = new ParameterAtribute (method_data.MethodBuilder);
+                                               param_attr = new ImplicitParameter (method_data.MethodBuilder);
 
                                        param_attr.ApplyAttributeBuilder (a, cb);
                                        return;
@@ -6113,7 +6106,9 @@ namespace Mono.CSharp {
                        if (!DoDefine (container, container))
                                return false;
 
-                       IndexerName = Attribute.ScanForIndexerName (ec, OptAttributes);
+                       if (OptAttributes != null)
+                               IndexerName = OptAttributes.ScanForIndexerName (ec);
+
                        if (IndexerName == null)
                                IndexerName = "Item";
                        else {
@@ -6124,9 +6119,7 @@ namespace Mono.CSharp {
                                
                                if (IsExplicitImpl) {
                                Report.Error (592, Location,
-                                                     "Attribute 'IndexerName' is not valid on explicit " +
-                                                     "implementations.");
-                                       
+                                                     "Attribute 'IndexerName' is not valid on explicit implementations.");
                                        return false;
                                }
                        }
index 5c913992c4c3e452fbf4853a9568056fcc61f930..5e1113b035e41e9a509be6e60b11070a6feb251e 100755 (executable)
@@ -850,17 +850,19 @@ namespace Mono.CSharp {
                        is_cls_compliant = a.GetClsCompliantAttributeValue (null);
                }
 
-               //TODO: this code is buggy because compare Attribute name without resolve is wrong
                public AssemblyName GetAssemblyName (string name, string output) 
                {
                        if (OptAttributes != null) {
-                               foreach (Attribute a in OptAttributes.AttributeSections) {
+                               foreach (Attribute a in OptAttributes.Attrs) {
                                        if (a.Target != "assembly")
                                                continue;
-                                       // strongname attributes don't support AllowMultiple
-                                       //Attribute a = (Attribute) asect.Attributes [0];
+                                       // TODO: This code is buggy: comparing Attribute name without resolving it is wrong.
+                                       //       However, this is invoked by CodeGen.Init, at which time none of the namespaces
+                                       //       are loaded yet.
                                        switch (a.Name) {
                                                case "AssemblyKeyFile":
+                                               case "AssemblyKeyFileAttribute":
+                                               case "System.Reflection.AssemblyKeyFileAttribute":
                                                        if (RootContext.StrongNameKeyFile != null) {
                                                                Report.Warning (1616, "Compiler option -keyfile overrides " +
                                                                        "AssemblyKeyFileAttribute");
@@ -872,6 +874,8 @@ namespace Mono.CSharp {
                                                        }
                                                        break;
                                                case "AssemblyKeyName":
+                                               case "AssemblyKeyNameAttribute":
+                                               case "System.Reflection.AssemblyKeyNameAttribute":
                                                        if (RootContext.StrongNameKeyContainer != null) {
                                                                Report.Warning (1616, "Compiler option -keycontainer overrides " +
                                                                        "AssemblyKeyNameAttribute");
@@ -883,6 +887,8 @@ namespace Mono.CSharp {
                                                        }
                                                        break;
                                                case "AssemblyDelaySign":
+                                               case "AssemblyDelaySignAttribute":
+                                               case "System.Reflection.AssemblyDelaySignAttribute":
                                                        RootContext.StrongNameDelaySign = a.GetBoolean ();
                                                        break;
                                        }
index e554eb0cb7a17f0aa5c576ec6698977dd256cd8b..a36b0110cc33bcf883bde33de9abef91555d7823 100755 (executable)
@@ -589,7 +589,8 @@ attribute
                //
                // Attributes need a string, not an expression: generic types will fail here.
                //
-               if (current_attr_target == "assembly" || current_attr_target == "module")
+               if (current_attr_target == "assembly" || current_attr_target == "module"
+                       || (global_attrs_enabled && current_attr_target == null))
                        $$ = new GlobalAttribute (current_container, current_attr_target,
                                                  ((Expression) $1).ToString (),
                                                  (ArrayList) $3, (Location) $2);
index c0900b3608df39240933ba355ee680408010d4ff..7bfc3d51560cc304e6c6df9e766c51712e71a309 100755 (executable)
@@ -90,11 +90,11 @@ namespace Mono.CSharp {
        }
 
        /// <summary>
-       /// Class for applying custom attributes on the parameter type.
-       /// ! Now it works only with 1st parameter (for properties and events)
+       /// Class for applying custom attributes on the implicit parameter type
+       /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
        /// </summary>
-       public class ParameterAtribute: ParameterBase {
-               public ParameterAtribute (MethodBuilder mb):
+       public class ImplicitParameter: ParameterBase {
+               public ImplicitParameter (MethodBuilder mb):
                        base (null)
                {
                        builder = mb.DefineParameter (1, ParameterAttributes.None, "");