2004-05-21 Marek Safar <marek.safar@seznam.cz>
authorMarek Safar <marek.safar@gmail.com>
Fri, 21 May 2004 11:42:47 +0000 (11:42 -0000)
committerMarek Safar <marek.safar@gmail.com>
Fri, 21 May 2004 11:42:47 +0000 (11:42 -0000)
* attribute.cs, cs-parser.jay: Fix errors/cs0579-7.cs.

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

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

index 9de6afa5dd076269b39db6e438fbcbd5e6934ac6..837be106e3928ee71354c3730e13f0ad313783e4 100755 (executable)
@@ -1,3 +1,7 @@
+2004-05-21  Marek Safar  <marek.safar@seznam.cz>
+
+       * attribute.cs, cs-parser.jay: Fix errors/cs0579-7.cs.
+
 2004-05-20  Martin Baulig  <martin@ximian.com>
 
        Merged this back from gmcs to keep the differences to a minumum.
index d83905e156748ccb459886e20bb7902d3332610e..dac6740faa65e1755f3bbb4adb8e33f71b270360 100644 (file)
@@ -44,6 +44,8 @@ namespace Mono.CSharp {
                        }
                        set {
                                attributes = value;
+                               if (attributes != null)
+                                       attributes.CheckTargets (ValidAttributeTargets);
                        }
                }
 
@@ -60,13 +62,14 @@ namespace Mono.CSharp {
                public abstract bool IsClsCompliaceRequired (DeclSpace ds);
 
                /// <summary>
-               /// Gets list of valid attribute targets for explicit target declaration
+               /// Gets list of valid attribute targets for explicit target declaration.
+               /// The first array item is default target. Don't break this rule.
                /// </summary>
                protected abstract string[] ValidAttributeTargets { get; }
        };
 
        public class Attribute {
-               public readonly string Target;
+               public string Target;
                public readonly string    Name;
                public readonly ArrayList Arguments;
 
@@ -802,11 +805,6 @@ namespace Mono.CSharp {
                        get { return ImplOptions == MethodImplOptions.InternalCall; }
                }
 
-               protected virtual bool CanIgnoreInvalidAttribute (Attributable ias)
-               {
-                       return false;
-               }
-
                /// <summary>
                /// Emit attribute for Attributable symbol
                /// </summary>
@@ -818,24 +816,17 @@ namespace Mono.CSharp {
 
                        AttributeUsageAttribute usage_attr = GetAttributeUsage ();
                        if ((usage_attr.ValidOn & ias.AttributeTargets) == 0) {
-                               // The parser applies toplevel attributes both to the assembly and
-                               // to a top-level class, if any.  So, be silent about them.
-                               if (! CanIgnoreInvalidAttribute (ias))
-                                       Error_AttributeNotValidForElement (this, Location);
+                               Error_AttributeNotValidForElement (this, Location);
                                return;
                        }
 
                        ias.ApplyAttributeBuilder (this, cb);
 
-                       // Because default target is null (we save some space). We need to transform it here
-                       // for distinction between "default" and "doesn't exist"
-                       string target = Target == null ? "default" : Target;
-                       string emitted = emitted_attr [Type] as string;
-                       if (emitted == target && !usage_attr.AllowMultiple) {
+                       if (!usage_attr.AllowMultiple && emitted_attr.Contains (Target)) {
                                Report.Error (579, Location, "Duplicate '" + Name + "' attribute");
                        }
 
-                       emitted_attr [Type] = target;
+                       emitted_attr [Type] = Target;
 
                        // Here we are testing attribute arguments for array usage (error 3016)
                        if (ias.IsClsCompliaceRequired (ec.DeclSpace)) {
@@ -1065,16 +1056,6 @@ namespace Mono.CSharp {
                                ec.DeclSpace.NamespaceEntry = ns;
                        return base.CheckAttributeType (ec, complain);
                }
-
-               protected override bool CanIgnoreInvalidAttribute (Attributable ias)
-               {
-                       // Ignore error if this attribute is shared between the Assembly
-                       // and a top-level class.  The parser couldn't figure out which entity
-                       // this attribute belongs to.  If this attribute is erroneous, it should
-                       // be caught when it is processed by the top-level class.
-
-                       return (Target == null && ias is CommonAssemblyModulClass);
-               }
        }
 
        public class Attributes {
@@ -1102,8 +1083,10 @@ namespace Mono.CSharp {
                public void CheckTargets (string[] possible_targets)
                {
                        foreach (Attribute a in Attrs) {
-                               if (a.Target == null)
+                               if (a.Target == null) {
+                                       a.Target = possible_targets [0];
                                        continue;
+                               }
 
                                if (((IList) possible_targets).Contains (a.Target))
                                        continue;
index 628ec85c84024c5928971779dabb607f07ed4eb2..0780d1ae0f9bbc171f4cac8e8f239c42a4895dea 100755 (executable)
@@ -6026,7 +6026,7 @@ namespace Mono.CSharp {
                public string MethodName;
                public Method OperatorMethod;
 
-               static string[] attribute_targets = new string [] { "param" };
+               static string[] attribute_targets = new string [] { "method", "return" };
 
                public Operator (OpType type, Expression ret_type, int mod_flags,
                                 Expression arg1type, string arg1name,
index 756ced34e395ab8fbbe12b7b8a71bbf692ce0668..f097b87740265e0742794731b51491705e095d5a 100755 (executable)
@@ -768,7 +768,6 @@ namespace Mono.CSharp {
                {
                }
 
-               // TODO: The error can be reported more than once
                public void AddAttributes (ArrayList attrs)
                {
                        if (OptAttributes == null) {
@@ -780,6 +779,12 @@ namespace Mono.CSharp {
                        OptAttributes.CheckTargets (ValidAttributeTargets);
                }
 
+               public void SetAttributes (Attributes attrs)
+               {
+                       OptAttributes = attrs;
+                       OptAttributes.CheckTargets (ValidAttributeTargets);
+               }
+
                public virtual void Emit (TypeContainer tc) 
                {
                        if (OptAttributes == null)
index e296ee2f883a6bd4072dcd23a4448397ecb3e447..6efb1a289152083a4d568cc493d2fa3793bdd3e7 100755 (executable)
@@ -283,8 +283,8 @@ namespace Mono.CSharp
 
 compilation_unit
         : outer_declarations opt_EOF
-        | outer_declarations attribute_sections opt_EOF
-        | attribute_sections opt_EOF
+        | outer_declarations global_attributes opt_EOF
+        | global_attributes opt_EOF
        | opt_EOF /* allow empty files */
         ;
        
@@ -446,6 +446,15 @@ type_declaration
 // Attributes 17.2
 //
 
+global_attributes
+       : attribute_sections
+{
+       if ($1 != null)
+               CodeGen.Assembly.SetAttributes ((Attributes)$1);
+
+       $$ = $1;
+}
+
 opt_attributes
        : /* empty */ 
          {
@@ -473,11 +482,6 @@ attribute_sections
                                CodeGen.Assembly.AddAttributes (sect);
                                $$ = null;
                        } else {
-
-                               // Because we don't know if target should be on assembly or type
-                               // we add it to both. This leads error CS0657 is reported twice
-                               // for this case but it is better than mistification.
-                               CodeGen.Assembly.AddAttributes (sect);
                                $$ = new Attributes (sect);
                        }
                } else {
@@ -568,8 +572,7 @@ attribute
          }
          opt_attribute_arguments
          {
-               if (current_attr_target == "assembly" || current_attr_target == "module"
-                   || (global_attrs_enabled && current_attr_target == null))
+               if (current_attr_target == "assembly" || current_attr_target == "module")
                        $$ = new GlobalAttribute (current_container, current_attr_target, (string) ($1).ToString (), (ArrayList) $3, (Location) $2);
                else
                        $$ = new Attribute (current_attr_target, (string) ($1).ToString (), (ArrayList) $3, (Location) $2);