**** Merged from MCS ****
[mono.git] / mcs / gmcs / class.cs
index 2942c0b7286850da9c3573a5e6403bbd28247deb..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)
@@ -2662,6 +2655,8 @@ namespace Mono.CSharp {
                // Whether this is an operator method.
                public bool IsOperator;
 
+               static string[] attribute_targets = new string [] { "method", "return" };
+
                public MethodCore (DeclSpace ds, Expression type, int mod, int allowed_mod,
                                   bool is_interface, MemberName name, Attributes attrs,
                                   Parameters parameters, Location loc)
@@ -2782,6 +2777,12 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               protected override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+
                protected override bool VerifyClsCompliance (DeclSpace ds)
                {
                        if (!base.VerifyClsCompliance (ds)) {
@@ -2894,6 +2895,7 @@ namespace Mono.CSharp {
        public class Method : MethodCore, IIteratorContainer, IMethodData {
                public MethodBuilder MethodBuilder;
                public MethodData MethodData;
+               ReturnParameter return_attributes;
 
                /// <summary>
                ///   Modifiers allowed in a class declaration
@@ -2990,6 +2992,14 @@ namespace Mono.CSharp {
 
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
+                       if (a.Target == "return") {
+                               if (return_attributes == null)
+                                       return_attributes = new ReturnParameter (MethodBuilder, Location);
+
+                               return_attributes.ApplyAttributeBuilder (a, cb);
+                               return;
+                       }
+
                        if (a.Type == TypeManager.methodimpl_attr_type && a.IsInternalCall) {
                                MethodBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
                        }
@@ -3525,8 +3535,7 @@ namespace Mono.CSharp {
                                ec.IsStatic = false;
                        }
 
-                       Parameters.LabelParameters (ec, ConstructorBuilder,
-                                                    OptAttributes, Location);
+                       Parameters.LabelParameters (ec, ConstructorBuilder, Location);
                        
                        SymbolWriter sw = CodeGen.SymbolWriter;
                        bool generate_debugging = false;
@@ -3741,29 +3750,24 @@ 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 (AttributeSection asec in opt_attrs.AttributeSections) {
-                               if (asec.Attributes == null)
-                                       continue;
-                                       
-                               foreach (Attribute a in asec.Attributes) {
-                                       Type attr_type = a.ResolveType (ec, true);
-                                       if (attr_type == TypeManager.conditional_attribute_type) {
-                                               if (!ApplyConditionalAttribute (a))
-                                                       return false;
-                                       } else if (attr_type == TypeManager.obsolete_attribute_type) {
-                                               if (!ApplyObsoleteAttribute (a))
-                                                       return false;
-                                       } else if (attr_type == TypeManager.dllimport_type) {
-                                               if (!is_method) {
-                                                       Attribute.Error_AttributeNotValidForElement (a, method.Location);
-                                                       return false;
-                                               }
-                                               if (!ApplyDllImportAttribute (a))
-                                                       return false;
+                       foreach (Attribute a in opt_attrs.Attrs) {
+                               Type attr_type = a.ResolveType (ec, true);
+                               if (attr_type == TypeManager.conditional_attribute_type) {
+                                       if (!ApplyConditionalAttribute (a))
+                                               return false;
+                               } else if (attr_type == TypeManager.obsolete_attribute_type) {
+                                       if (!ApplyObsoleteAttribute (a))
+                                               return false;
+                               } else if (attr_type == TypeManager.dllimport_type) {
+                                       if (!is_method) {
+                                               Attribute.Error_AttributeNotValidForElement (a, method.Location);
+                                               return false;
                                        }
+                                       if (!ApplyDllImportAttribute (a))
+                                               return false;
                                }
                        }
 
@@ -4113,7 +4117,7 @@ namespace Mono.CSharp {
                                OptAttributes.Emit (ec, kind);
 
                        if (member is MethodCore)
-                               ((MethodCore) member).Parameters.LabelParameters (ec, MethodBuilder,OptAttributes, loc);
+                               ((MethodCore) member).Parameters.LabelParameters (ec, MethodBuilder, loc);
                         
                        SymbolWriter sw = CodeGen.SymbolWriter;
                        Block block = method.Block;
@@ -4641,6 +4645,8 @@ namespace Mono.CSharp {
                [Flags]
                public enum Status : byte { ASSIGNED = 1, USED = 2 }
 
+               static string[] attribute_targets = new string [] { "field" };
+
                //
                // The constructor is only exposed to our children
                //
@@ -4735,6 +4741,12 @@ namespace Mono.CSharp {
                        return TypeManager.GetFullNameSignature (FieldBuilder);
                }
 
+               protected override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+
                protected override bool VerifyClsCompliance (DeclSpace ds)
                {
                        if (!base.VerifyClsCompliance (ds))
@@ -4909,6 +4921,8 @@ namespace Mono.CSharp {
 
                public class GetMethod: PropertyMethod
                {
+                       static string[] attribute_targets = new string [] { "method", "return" };
+
                        public GetMethod (MethodCore method, Accessor accessor):
                                base (method, accessor)
                        {
@@ -4935,14 +4949,37 @@ namespace Mono.CSharp {
                                        return method.MemberType;
                                }
                        }
+
+                       protected override string[] ValidAttributeTargets {
+                               get {
+                                       return attribute_targets;
+                               }
+                       }
                }
 
                public class SetMethod: PropertyMethod {
+
+                       static string[] attribute_targets = new string [] { "method", "param", "return" };
+                       ImplicitParameter param_attr;
+
                        public SetMethod (MethodCore method, Accessor accessor):
                                base (method, accessor)
                        {
                        }
 
+                       public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+                       {
+                               if (a.Target == "param") {
+                                       if (param_attr == null)
+                                               param_attr = new ImplicitParameter (method_data.MethodBuilder);
+
+                                       param_attr.ApplyAttributeBuilder (a, cb);
+                                       return;
+                               }
+
+                               base.ApplyAttributeBuilder (a, cb);
+                       }
+
                        protected virtual InternalParameters GetParameterInfo (TypeContainer container)
                        {
                                Parameter [] parms = new Parameter [1];
@@ -4978,13 +5015,23 @@ namespace Mono.CSharp {
                                        return TypeManager.void_type;
                                }
                        }
+
+                       protected override string[] ValidAttributeTargets {
+                               get {
+                                       return attribute_targets;
+                               }
+                       }
                }
 
+               static string[] attribute_targets = new string [] { "property" };
+
                public abstract class PropertyMethod: Attributable, IMethodData {
                        protected readonly MethodCore method;
                        protected MethodData method_data;
                        Block block;
 
+                       ReturnParameter return_attributes;
+
                        public PropertyMethod (MethodCore method, Accessor accessor):
                                base (accessor.Attributes)
                        {
@@ -5035,6 +5082,14 @@ namespace Mono.CSharp {
 
                        public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
                        {
+                               if (a.Target == "return") {
+                                       if (return_attributes == null)
+                                               return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
+
+                                       return_attributes.ApplyAttributeBuilder (a, cb);
+                                       return;
+                               }
+
                                method_data.MethodBuilder.SetCustomAttribute (cb);
                        }
 
@@ -5265,6 +5320,12 @@ namespace Mono.CSharp {
 
                        base.Emit (tc);
                }
+
+               protected override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
        }
                        
        public class Property : PropertyBase, IIteratorContainer {
@@ -5522,9 +5583,71 @@ namespace Mono.CSharp {
                }
        }
        
-       public class Event : FieldBase {
+       /// <summary>
+       /// For case when event is declared like property (with add and remove accessors).
+       /// </summary>
+       public class EventProperty: Event {
+
+               static string[] attribute_targets = new string [] { "event", "property" };
+
+               public EventProperty (DeclSpace ds, Expression type, int mod_flags,
+                                     bool is_iface, MemberName name, Object init,
+                                     Attributes attrs, Accessor add, Accessor remove,
+                                     Location loc)
+                       : base (ds, type, mod_flags, is_iface, name, init, attrs, loc)
+               {
+                       Add = new AddDelegateMethod (this, add);
+                       Remove = new RemoveDelegateMethod (this, remove);
+               }
+
+               protected override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+       }
+
+       /// <summary>
+       /// Event is declared like field.
+       /// </summary>
+       public class EventField: Event {
+
+               static string[] attribute_targets = new string [] { "method", "field", "event" };
 
-               sealed class AddDelegateMethod: DelegateMethod
+               public EventField (DeclSpace ds, Expression type, int mod_flags, bool is_iface,
+                                  MemberName name, Object init, Attributes attrs, Location loc)
+                       : base (ds, type, mod_flags, is_iface, name, init, attrs, loc)
+               {
+                       Add = new AddDelegateMethod (this);
+                       Remove = new RemoveDelegateMethod (this);
+               }
+
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               {
+                       if (a.Target == "field") {
+                               FieldBuilder.SetCustomAttribute (cb);
+                               return;
+                       }
+
+                       if (a.Target == "method") {
+                               AddBuilder.SetCustomAttribute (cb);
+                               RemoveBuilder.SetCustomAttribute (cb);
+                               return;
+                       }
+
+                       base.ApplyAttributeBuilder (a, cb);
+               }
+
+               protected override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+       }
+
+       public abstract class Event : FieldBase {
+
+               protected sealed class AddDelegateMethod: DelegateMethod
                {
                        public AddDelegateMethod (Event method):
                                base (method)
@@ -5550,7 +5673,7 @@ namespace Mono.CSharp {
 
                }
 
-               sealed class RemoveDelegateMethod: DelegateMethod
+               protected sealed class RemoveDelegateMethod: DelegateMethod
                {
                        public RemoveDelegateMethod (Event method):
                                base (method)
@@ -5581,6 +5704,10 @@ namespace Mono.CSharp {
                        protected readonly Event method;
                        protected MethodData method_data;
                        Block block;
+                       ReturnParameter return_attributes;
+                       ImplicitParameter param_attr;
+
+                       static string[] attribute_targets = new string [] { "method", "param", "return" };
 
                        public DelegateMethod (Event method):
                                base (null)
@@ -5597,6 +5724,22 @@ namespace Mono.CSharp {
 
                        public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
                        {
+                               if (a.Target == "return") {
+                                       if (return_attributes == null)
+                                               return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
+
+                                       return_attributes.ApplyAttributeBuilder (a, cb);
+                                       return;
+                               }
+
+                               if (a.Target == "param") {
+                                       if (param_attr == null)
+                                               param_attr = new ImplicitParameter (method_data.MethodBuilder);
+
+                                       param_attr.ApplyAttributeBuilder (a, cb);
+                                       return;
+                               }
+
                                method_data.MethodBuilder.SetCustomAttribute (cb);
                        }
 
@@ -5690,13 +5833,18 @@ namespace Mono.CSharp {
 
                        public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
                        {
-                               return new EmitContext (tc, method.ds, method.Location, ig, ReturnType, method.ModFlags, false);
+                               return new EmitContext (tc, method.ds, Location, ig, ReturnType, method.ModFlags, false);
                        }
 
                        public abstract string MethodName { get; }
 
                        #endregion
 
+                       protected override string[] ValidAttributeTargets {
+                               get {
+                                       return attribute_targets;
+                               }
+                       }
                }
 
 
@@ -5716,27 +5864,13 @@ namespace Mono.CSharp {
                const int AllowedInterfaceModifiers =
                        Modifiers.NEW;
 
-               readonly DelegateMethod Add, Remove;
+               protected DelegateMethod Add, Remove;
                public MyEventBuilder     EventBuilder;
                public MethodBuilder AddBuilder, RemoveBuilder;
                public DeclSpace ds;
 
                MethodData AddData, RemoveData;
                
-               public Event (DeclSpace ds, Expression type, int mod_flags, bool is_iface,
-                             MemberName name, Object init, Attributes attrs, Accessor add,
-                             Accessor remove, Location loc)
-                       : base (type, mod_flags,
-                               is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
-                               name, init, attrs, loc)
-               {
-                       IsInterface = is_iface;
-                       this.ds = ds;
-
-                       Add = new AddDelegateMethod (this, add);
-                       Remove = new RemoveDelegateMethod (this, remove);
-               }
-
                public Event (DeclSpace ds, Expression type, int mod_flags, bool is_iface,
                              MemberName name, Object init, Attributes attrs, Location loc)
                        : base (type, mod_flags,
@@ -5745,9 +5879,6 @@ namespace Mono.CSharp {
                {
                        IsInterface = is_iface;
                        this.ds = ds;
-
-                       Add = new AddDelegateMethod (this);
-                       Remove = new RemoveDelegateMethod (this);
                }
 
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
@@ -5848,7 +5979,6 @@ namespace Mono.CSharp {
 
                        base.Emit (tc);
                }
-               
        }
 
        //
@@ -5976,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 {
@@ -5987,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;
                                }
                        }
@@ -6186,6 +6316,8 @@ namespace Mono.CSharp {
                public string MethodName;
                public Method OperatorMethod;
 
+               static string[] attribute_targets = new string [] { "param" };
+
                public Operator (OpType type, Expression ret_type, int mod_flags,
                                 Expression arg1type, string arg1name,
                                 Expression arg2type, string arg2name,
@@ -6453,6 +6585,12 @@ namespace Mono.CSharp {
                                        param_types [0], param_types [1]);
                }
 
+               protected override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+
                public void SetYields ()
                {
                        ModFlags |= Modifiers.METHOD_YIELDS;