From: Marek Safar Date: Fri, 21 May 2004 11:42:47 +0000 (-0000) Subject: 2004-05-21 Marek Safar X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=85a424320827c78db8ee0e11845d8a5dbe0a0b5c;p=mono.git 2004-05-21 Marek Safar * attribute.cs, cs-parser.jay: Fix errors/cs0579-7.cs. svn path=/trunk/mcs/; revision=27811 --- diff --git a/mcs/mcs/ChangeLog b/mcs/mcs/ChangeLog index 9de6afa5dd0..837be106e39 100755 --- a/mcs/mcs/ChangeLog +++ b/mcs/mcs/ChangeLog @@ -1,3 +1,7 @@ +2004-05-21 Marek Safar + + * attribute.cs, cs-parser.jay: Fix errors/cs0579-7.cs. + 2004-05-20 Martin Baulig Merged this back from gmcs to keep the differences to a minumum. diff --git a/mcs/mcs/attribute.cs b/mcs/mcs/attribute.cs index d83905e1567..dac6740faa6 100644 --- a/mcs/mcs/attribute.cs +++ b/mcs/mcs/attribute.cs @@ -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); /// - /// 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. /// 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; - } - /// /// Emit attribute for Attributable symbol /// @@ -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; diff --git a/mcs/mcs/class.cs b/mcs/mcs/class.cs index 628ec85c840..0780d1ae0f9 100755 --- a/mcs/mcs/class.cs +++ b/mcs/mcs/class.cs @@ -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, diff --git a/mcs/mcs/codegen.cs b/mcs/mcs/codegen.cs index 756ced34e39..f097b877402 100755 --- a/mcs/mcs/codegen.cs +++ b/mcs/mcs/codegen.cs @@ -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) diff --git a/mcs/mcs/cs-parser.jay b/mcs/mcs/cs-parser.jay index e296ee2f883..6efb1a28915 100755 --- a/mcs/mcs/cs-parser.jay +++ b/mcs/mcs/cs-parser.jay @@ -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);