Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / mcs / property.cs
1 //
2 // property.cs: Property based handlers
3 //
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 //          Martin Baulig (martin@ximian.com)
6 //          Marek Safar (marek.safar@seznam.cz)
7 //
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 //
10 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2004-2008 Novell, Inc
12 //
13
14 using System;
15 using System.Collections.Generic;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Runtime.CompilerServices;
19 using System.Runtime.InteropServices;
20 using System.Security;
21 using System.Security.Permissions;
22 using System.Text;
23
24 #if NET_2_1
25 using XmlElement = System.Object;
26 #else
27 using System.Xml;
28 #endif
29
30 using Mono.CompilerServices.SymbolWriter;
31
32 namespace Mono.CSharp
33 {
34         // It is used as a base class for all property based members
35         // This includes properties, indexers, and events
36         public abstract class PropertyBasedMember : InterfaceMemberBase
37         {
38                 public PropertyBasedMember (DeclSpace parent, GenericMethod generic,
39                         FullNamedExpression type, Modifiers mod, Modifiers allowed_mod,
40                         MemberName name, Attributes attrs)
41                         : base (parent, generic, type, mod, allowed_mod, name, attrs)
42                 {
43                 }
44
45                 protected void CheckReservedNameConflict (string prefix, MethodSpec accessor)
46                 {
47                         string name;
48                         AParametersCollection parameters;
49                         if (accessor != null) {
50                                 name = accessor.Name;
51                                 parameters = accessor.Parameters;
52                         } else {
53                                 name = prefix + ShortName;
54                                 if (IsExplicitImpl)
55                                         name = MemberName.Left + "." + name;
56
57                                 if (this is Indexer) {
58                                         parameters = ((Indexer) this).ParameterInfo;
59                                         if (prefix[0] == 's') {
60                                                 var data = new IParameterData[parameters.Count + 1];
61                                                 Array.Copy (parameters.FixedParameters, data, data.Length - 1);
62                                                 data[data.Length - 1] = new ParameterData ("value", Parameter.Modifier.NONE);
63                                                 var types = new TypeSpec[data.Length];
64                                                 Array.Copy (parameters.Types, types, data.Length - 1);
65                                                 types[data.Length - 1] = member_type;
66
67                                                 parameters = new ParametersImported (data, types, false);
68                                         }
69                                 } else {
70                                         if (prefix[0] == 's')
71                                                 parameters = ParametersCompiled.CreateFullyResolved (new[] { member_type });
72                                         else
73                                                 parameters = ParametersCompiled.EmptyReadOnlyParameters;
74                                 }
75                         }
76
77                         var conflict = MemberCache.FindMember (Parent.Definition,
78                                 new MemberFilter (name, 0, MemberKind.Method, parameters, null),
79                                 BindingRestriction.DeclaredOnly | BindingRestriction.NoAccessors);
80
81                         if (conflict != null) {
82                                 Report.SymbolRelatedToPreviousError (conflict);
83                                 Report.Error (82, Location, "A member `{0}' is already reserved", conflict.GetSignatureForError ());
84                         }
85                 }
86
87                 protected override bool VerifyClsCompliance ()
88                 {
89                         if (!base.VerifyClsCompliance ())
90                                 return false;
91
92                         if (!MemberType.IsCLSCompliant ()) {
93                                 Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
94                                         GetSignatureForError ());
95                         }
96                         return true;
97                 }
98
99         }
100
101         public class PropertySpec : MemberSpec, IInterfaceMemberSpec
102         {
103                 PropertyInfo info;
104                 TypeSpec memberType;
105                 MethodSpec set, get;
106
107                 public PropertySpec (MemberKind kind, TypeSpec declaringType, IMemberDefinition definition, TypeSpec memberType, PropertyInfo info, Modifiers modifiers)
108                         : base (kind, declaringType, definition, modifiers)
109                 {
110                         this.info = info;
111                         this.memberType = memberType;
112                 }
113
114                 #region Properties
115
116                 public MethodSpec Get {
117                         get {
118                                 return get;
119                         }
120                         set {
121                                 get = value;
122                                 get.IsAccessor = true;
123                         }
124                 }
125
126                 public MethodSpec Set { 
127                         get {
128                                 return set;
129                         }
130                         set {
131                                 set = value;
132                                 set.IsAccessor = true;
133                         }
134                 }
135
136                 public bool IsNotRealProperty {
137                         get {
138                                 return (state & StateFlags.IsNotRealProperty) != 0;
139                         }
140                         set {
141                                 state |= StateFlags.IsNotRealProperty;
142                         }
143                 }
144
145                 public bool HasDifferentAccessibility {
146                         get {
147                                 return HasGet && HasSet && 
148                                         (Get.Modifiers & Modifiers.AccessibilityMask) != (Set.Modifiers & Modifiers.AccessibilityMask);
149                         }
150                 }
151
152                 public bool HasGet {
153                         get {
154                                 return Get != null;
155                         }
156                 }
157
158                 public bool HasSet {
159                         get {
160                                 return Set != null;
161                         }
162                 }
163
164                 public PropertyInfo MetaInfo {
165                         get {
166                                 if ((state & StateFlags.PendingMetaInflate) != 0)
167                                         throw new NotSupportedException ();
168
169                                 return info;
170                         }
171                 }
172
173                 public TypeSpec MemberType {
174                         get {
175                                 return memberType;
176                         }
177                 }
178
179                 #endregion
180
181                 public override MemberSpec InflateMember (TypeParameterInflator inflator)
182                 {
183                         var ps = (PropertySpec) base.InflateMember (inflator);
184                         ps.memberType = inflator.Inflate (memberType);
185                         return ps;
186                 }
187         }
188
189         //
190         // Properties and Indexers both generate PropertyBuilders, we use this to share 
191         // their common bits.
192         //
193         abstract public class PropertyBase : PropertyBasedMember {
194
195                 public class GetMethod : PropertyMethod
196                 {
197                         static string[] attribute_targets = new string [] { "method", "return" };
198
199                         internal const string Prefix = "get_";
200
201                         public GetMethod (PropertyBase method, Modifiers modifiers, Attributes attrs, Location loc)
202                                 : base (method, Prefix, modifiers, attrs, loc)
203                         {
204                         }
205
206                         public override MethodBuilder Define (DeclSpace parent)
207                         {
208                                 base.Define (parent);
209
210                                 Spec = new MethodSpec (MemberKind.Method, parent.PartialContainer.Definition, this, ReturnType, null, ParameterInfo, ModFlags);
211
212                                 method_data = new MethodData (method, ModFlags, flags, this);
213
214                                 if (!method_data.Define (parent, method.GetFullName (MemberName), Report))
215                                         return null;
216
217                                 Spec.SetMetaInfo (method_data.MethodBuilder);
218
219                                 return method_data.MethodBuilder;
220                         }
221
222                         public override TypeSpec ReturnType {
223                                 get {
224                                         return method.MemberType;
225                                 }
226                         }
227
228                         public override ParametersCompiled ParameterInfo {
229                                 get {
230                                         return ParametersCompiled.EmptyReadOnlyParameters;
231                                 }
232                         }
233
234                         public override string[] ValidAttributeTargets {
235                                 get {
236                                         return attribute_targets;
237                                 }
238                         }
239                 }
240
241                 public class SetMethod : PropertyMethod {
242
243                         static string[] attribute_targets = new string [] { "method", "param", "return" };
244
245                         internal const string Prefix = "set_";
246
247                         ImplicitParameter param_attr;
248                         protected ParametersCompiled parameters;
249
250                         public SetMethod (PropertyBase method, Modifiers modifiers, ParametersCompiled parameters, Attributes attrs, Location loc)
251                                 : base (method, Prefix, modifiers, attrs, loc)
252                         {
253                                 this.parameters = parameters;
254                         }
255
256                         protected override void ApplyToExtraTarget (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
257                         {
258                                 if (a.Target == AttributeTargets.Parameter) {
259                                         if (param_attr == null)
260                                                 param_attr = new ImplicitParameter (method_data.MethodBuilder);
261
262                                         param_attr.ApplyAttributeBuilder (a, ctor, cdata, pa);
263                                         return;
264                                 }
265
266                                 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
267                         }
268
269                         public override ParametersCompiled ParameterInfo {
270                             get {
271                                 return parameters;
272                             }
273                         }
274
275                         public override MethodBuilder Define (DeclSpace parent)
276                         {
277                                 parameters.Resolve (this);
278                                 
279                                 base.Define (parent);
280
281                                 Spec = new MethodSpec (MemberKind.Method, parent.PartialContainer.Definition, this, ReturnType, null, ParameterInfo, ModFlags);
282
283                                 method_data = new MethodData (method, ModFlags, flags, this);
284
285                                 if (!method_data.Define (parent, method.GetFullName (MemberName), Report))
286                                         return null;
287
288                                 Spec.SetMetaInfo (method_data.MethodBuilder);
289
290                                 return method_data.MethodBuilder;
291                         }
292
293                         public override TypeSpec ReturnType {
294                                 get {
295                                         return TypeManager.void_type;
296                                 }
297                         }
298
299                         public override string[] ValidAttributeTargets {
300                                 get {
301                                         return attribute_targets;
302                                 }
303                         }
304                 }
305
306                 static string[] attribute_targets = new string [] { "property" };
307
308                 public abstract class PropertyMethod : AbstractPropertyEventMethod
309                 {
310                         public const Modifiers AllowedModifiers =
311                                 Modifiers.PUBLIC |
312                                 Modifiers.PROTECTED |
313                                 Modifiers.INTERNAL |
314                                 Modifiers.PRIVATE;
315                 
316                         protected readonly PropertyBase method;
317                         protected MethodAttributes flags;
318
319                         public PropertyMethod (PropertyBase method, string prefix, Modifiers modifiers, Attributes attrs, Location loc)
320                                 : base (method, prefix, attrs, loc)
321                         {
322                                 this.method = method;
323                                 this.ModFlags = modifiers | (method.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE));
324                         }
325
326                         public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
327                         {
328                                 if (a.IsInternalMethodImplAttribute) {
329                                         method.is_external_implementation = true;
330                                 }
331
332                                 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
333                         }
334
335                         public override AttributeTargets AttributeTargets {
336                                 get {
337                                         return AttributeTargets.Method;
338                                 }
339                         }
340
341                         public override bool IsClsComplianceRequired ()
342                         {
343                                 return method.IsClsComplianceRequired ();
344                         }
345
346                         public virtual MethodBuilder Define (DeclSpace parent)
347                         {
348                                 TypeContainer container = parent.PartialContainer;
349
350                                 //
351                                 // Check for custom access modifier
352                                 //
353                                 if ((ModFlags & Modifiers.AccessibilityMask) == 0) {
354                                         ModFlags |= method.ModFlags;
355                                         flags = method.flags;
356                                 } else {
357                                         if (container.Kind == MemberKind.Interface)
358                                                 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
359                                                         GetSignatureForError ());
360
361                                         if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
362                                                 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
363                                         }
364
365                                         CheckModifiers (ModFlags);
366                                         ModFlags |= (method.ModFlags & (~Modifiers.AccessibilityMask));
367                                         ModFlags |= Modifiers.PROPERTY_CUSTOM;
368                                         flags = ModifiersExtensions.MethodAttr (ModFlags);
369                                         flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
370                                 }
371
372                                 CheckAbstractAndExtern (block != null);
373                                 CheckProtectedModifier ();
374
375                                 if (block != null && block.IsIterator)
376                                         Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
377
378                                 return null;
379                         }
380
381                         public bool HasCustomAccessModifier {
382                                 get {
383                                         return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
384                                 }
385                         }
386
387                         public PropertyBase Property {
388                                 get {
389                                         return method;
390                                 }
391                         }
392
393                         public override ObsoleteAttribute GetAttributeObsolete ()
394                         {
395                                 return method.GetAttributeObsolete ();
396                         }
397
398                         public override string GetSignatureForError()
399                         {
400                                 return method.GetSignatureForError () + "." + prefix.Substring (0, 3);
401                         }
402
403                         void CheckModifiers (Modifiers modflags)
404                         {
405                                 if (!ModifiersExtensions.IsRestrictedModifier (modflags & Modifiers.AccessibilityMask, method.ModFlags & Modifiers.AccessibilityMask)) {
406                                         Report.Error (273, Location,
407                                                 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
408                                                 GetSignatureForError (), method.GetSignatureForError ());
409                                 }
410                         }
411                 }
412
413                 PropertyMethod get, set, first;
414                 PropertyBuilder PropertyBuilder;
415
416                 public PropertyBase (DeclSpace parent, FullNamedExpression type, Modifiers mod_flags,
417                                      Modifiers allowed_mod, MemberName name, Attributes attrs)
418                         : base (parent, null, type, mod_flags, allowed_mod, name, attrs)
419                 {
420                 }
421
422                 #region Properties
423
424                 public override AttributeTargets AttributeTargets {
425                         get {
426                                 return AttributeTargets.Property;
427                         }
428                 }
429
430                 public PropertyMethod AccessorFirst {
431                         get {
432                                 return first;
433                         }
434                 }
435
436                 public PropertyMethod AccessorSecond {
437                         get {
438                                 return first == get ? set : get;
439                         }
440                 }
441
442                 public PropertyMethod Get {
443                         get {
444                                 return get;
445                         }
446                         set {
447                                 get = value;
448                                 if (first == null)
449                                         first = value;
450
451                                 Parent.AddMember (get);
452                         }
453                 }
454
455                 public PropertyMethod Set {
456                         get {
457                                 return set;
458                         }
459                         set {
460                                 set = value;
461                                 if (first == null)
462                                         first = value;
463
464                                 Parent.AddMember (set);
465                         }
466                 }
467
468                 public override string[] ValidAttributeTargets {
469                         get {
470                                 return attribute_targets;
471                         }
472                 }
473
474                 #endregion
475
476                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
477                 {
478                         if (a.HasSecurityAttribute) {
479                                 a.Error_InvalidSecurityParent ();
480                                 return;
481                         }
482
483                         if (a.Type == pa.Dynamic) {
484                                 a.Error_MisusedDynamicAttribute ();
485                                 return;
486                         }
487
488                         PropertyBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
489                 }
490
491                 void CheckMissingAccessor (MemberKind kind, ParametersCompiled parameters, bool get)
492                 {
493                         if (IsExplicitImpl) {
494                                 MemberFilter filter;
495                                 if (kind == MemberKind.Indexer)
496                                         filter = new MemberFilter (MemberCache.IndexerNameAlias, 0, kind, parameters, null);
497                                 else
498                                         filter = new MemberFilter (MemberName.Name, 0, kind, null, null);
499
500                                 var implementing = MemberCache.FindMember (InterfaceType, filter, BindingRestriction.DeclaredOnly) as PropertySpec;
501
502                                 if (implementing == null)
503                                         return;
504
505                                 var accessor = get ? implementing.Get : implementing.Set;
506                                 if (accessor != null) {
507                                         Report.SymbolRelatedToPreviousError (accessor);
508                                         Report.Error (551, Location, "Explicit interface implementation `{0}' is missing accessor `{1}'",
509                                                 GetSignatureForError (), accessor.GetSignatureForError ());
510                                 }
511                         }
512                 }
513
514                 protected override bool CheckOverrideAgainstBase (MemberSpec base_member)
515                 {
516                         var ok = base.CheckOverrideAgainstBase (base_member);
517
518                         //
519                         // Check base property accessors conflict
520                         //
521                         var base_prop = (PropertySpec) base_member;
522                         if (Get != null) {
523                                 if (!base_prop.HasGet) {
524                                         if (ok) {
525                                                 Report.SymbolRelatedToPreviousError (base_prop);
526                                                 Report.Error (545, Get.Location,
527                                                         "`{0}': cannot override because `{1}' does not have an overridable get accessor",
528                                                         Get.GetSignatureForError (), base_prop.GetSignatureForError ());
529                                                 ok = false;
530                                         }
531                                 } else if (Get.HasCustomAccessModifier || base_prop.HasDifferentAccessibility) {
532                                         if (!CheckAccessModifiers (Get, base_prop.Get)) {
533                                                 Error_CannotChangeAccessModifiers (Get, base_prop.Get);
534                                                 ok = false;
535                                         }
536                                 }
537                         }
538
539                         if (Set != null) {
540                                 if (!base_prop.HasSet) {
541                                         if (ok) {
542                                                 Report.SymbolRelatedToPreviousError (base_prop);
543                                                 Report.Error (546, Set.Location,
544                                                         "`{0}': cannot override because `{1}' does not have an overridable set accessor",
545                                                         Set.GetSignatureForError (), base_prop.GetSignatureForError ());
546                                                 ok = false;
547                                         }
548                                 } else if (Set.HasCustomAccessModifier || base_prop.HasDifferentAccessibility) {
549                                         if (!CheckAccessModifiers (Set, base_prop.Set)) {
550                                                 Error_CannotChangeAccessModifiers (Set, base_prop.Set);
551                                                 ok = false;
552                                         }
553                                 }
554                         }
555
556                         if ((Set == null || !Set.HasCustomAccessModifier) && (Get == null || !Get.HasCustomAccessModifier)) {
557                                 if (!CheckAccessModifiers (this, base_prop)) {
558                                         Error_CannotChangeAccessModifiers (this, base_prop);
559                                         ok = false;
560                                 }
561                         }
562
563                         return ok;
564                 }
565
566                 protected override void DoMemberTypeDependentChecks ()
567                 {
568                         base.DoMemberTypeDependentChecks ();
569
570                         IsTypePermitted ();
571
572                         if (MemberType.IsStatic)
573                                 Error_StaticReturnType ();
574                 }
575
576                 protected override void DoMemberTypeIndependentChecks ()
577                 {
578                         base.DoMemberTypeIndependentChecks ();
579
580                         //
581                         // Accessors modifiers check
582                         //
583                         if (AccessorSecond != null) {
584                                 if ((Get.ModFlags & Modifiers.AccessibilityMask) != 0 && (Set.ModFlags & Modifiers.AccessibilityMask) != 0) {
585                                         Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
586                                                 GetSignatureForError ());
587                                 }
588                         } else if ((ModFlags & Modifiers.OVERRIDE) == 0 && 
589                                 (Get == null && (Set.ModFlags & Modifiers.AccessibilityMask) != 0) ||
590                                 (Set == null && (Get.ModFlags & Modifiers.AccessibilityMask) != 0)) {
591                                 Report.Error (276, Location, 
592                                               "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
593                                               GetSignatureForError ());
594                         }
595                 }
596
597                 protected bool DefineAccessors ()
598                 {
599                         first.Define (Parent);
600                         if (AccessorSecond != null)
601                                 AccessorSecond.Define (Parent);
602
603                         return true;
604                 }
605
606                 protected void DefineBuilders (MemberKind kind, ParametersCompiled parameters)
607                 {
608                         PropertyBuilder = Parent.TypeBuilder.DefineProperty (
609                                 GetFullName (MemberName), PropertyAttributes.None,
610 #if !BOOTSTRAP_BASIC    // Requires trunk version mscorlib
611                                 IsStatic ? 0 : CallingConventions.HasThis,
612 #endif
613                                 MemberType.GetMetaInfo (), null, null,
614                                 parameters.GetMetaInfo (), null, null);
615
616                         PropertySpec spec;
617                         if (kind == MemberKind.Indexer)
618                                 spec = new IndexerSpec (Parent.Definition, this, MemberType, parameters, PropertyBuilder, ModFlags);
619                         else
620                                 spec = new PropertySpec (kind, Parent.Definition, this, MemberType, PropertyBuilder, ModFlags);
621
622                         if (Get != null) {
623                                 spec.Get = Get.Spec;
624
625                                 var method = Get.Spec.GetMetaInfo () as MethodBuilder;
626                                 if (method != null) {
627                                         PropertyBuilder.SetGetMethod (method);
628                                         Parent.MemberCache.AddMember (this, method.Name, Get.Spec);
629                                 }
630                         } else {
631                                 CheckMissingAccessor (kind, parameters, true);
632                         }
633
634                         if (Set != null) {
635                                 spec.Set = Set.Spec;
636
637                                 var method = Set.Spec.GetMetaInfo () as MethodBuilder;
638                                 if (method != null) {
639                                         PropertyBuilder.SetSetMethod (method);
640                                         Parent.MemberCache.AddMember (this, method.Name, Set.Spec);
641                                 }
642                         } else {
643                                 CheckMissingAccessor (kind, parameters, false);
644                         }
645
646                         Parent.MemberCache.AddMember (this, PropertyBuilder.Name, spec);
647                 }
648
649                 public override void Emit ()
650                 {
651                         CheckReservedNameConflict (GetMethod.Prefix, get == null ? null : get.Spec);
652                         CheckReservedNameConflict (SetMethod.Prefix, set == null ? null : set.Spec);
653
654                         if (OptAttributes != null)
655                                 OptAttributes.Emit ();
656
657                         if (member_type == InternalType.Dynamic) {
658                                 PredefinedAttributes.Get.Dynamic.EmitAttribute (PropertyBuilder);
659                         } else {
660                                 var trans_flags = TypeManager.HasDynamicTypeUsed (member_type);
661                                 if (trans_flags != null) {
662                                         var pa = PredefinedAttributes.Get.DynamicTransform;
663                                         if (pa.Constructor != null || pa.ResolveConstructor (Location, ArrayContainer.MakeType (TypeManager.bool_type))) {
664                                                 PropertyBuilder.SetCustomAttribute (
665                                                         new CustomAttributeBuilder (pa.Constructor, new object[] { trans_flags }));
666                                         }
667                                 }
668                         }
669
670                         first.Emit (Parent);
671                         if (AccessorSecond != null)
672                                 AccessorSecond.Emit (Parent);
673
674                         base.Emit ();
675                 }
676
677                 public override bool IsUsed {
678                         get {
679                                 if (IsExplicitImpl)
680                                         return true;
681
682                                 return Get.IsUsed | Set.IsUsed;
683                         }
684                 }
685
686                 protected override void SetMemberName (MemberName new_name)
687                 {
688                         base.SetMemberName (new_name);
689
690                         if (Get != null)
691                                 Get.UpdateName (this);
692
693                         if (Set != null)
694                                 Set.UpdateName (this);
695                 }
696
697                 //
698                 //   Represents header string for documentation comment.
699                 //
700                 public override string DocCommentHeader {
701                         get { return "P:"; }
702                 }
703         }
704                         
705         public class Property : PropertyBase
706         {
707                 public sealed class BackingField : Field
708                 {
709                         readonly Property property;
710
711                         public BackingField (Property p)
712                                 : base (p.Parent, p.type_expr,
713                                 Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (p.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
714                                 new MemberName ("<" + p.GetFullName (p.MemberName) + ">k__BackingField", p.Location), null)
715                         {
716                                 this.property = p;
717                         }
718
719                         public string OriginalName {
720                                 get {
721                                         return property.Name;
722                                 }
723                         }
724
725                         public override string GetSignatureForError ()
726                         {
727                                 return property.GetSignatureForError ();
728                         }
729                 }
730
731                 public Property (DeclSpace parent, FullNamedExpression type, Modifiers mod,
732                                  MemberName name, Attributes attrs)
733                         : base (parent, type, mod,
734                                 parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
735                                 parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct :
736                                 AllowedModifiersClass,
737                                 name, attrs)
738                 {
739                 }
740
741                 void CreateAutomaticProperty ()
742                 {
743                         // Create backing field
744                         Field field = new BackingField (this);
745                         if (!field.Define ())
746                                 return;
747
748                         Parent.PartialContainer.AddField (field);
749
750                         FieldExpr fe = new FieldExpr (field, Location);
751                         if ((field.ModFlags & Modifiers.STATIC) == 0)
752                                 fe.InstanceExpression = new CompilerGeneratedThis (fe.Type, Location);
753
754                         // Create get block
755                         Get.Block = new ToplevelBlock (Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location);
756                         Return r = new Return (fe, Location);
757                         Get.Block.AddStatement (r);
758
759                         // Create set block
760                         Set.Block = new ToplevelBlock (Compiler, Set.ParameterInfo, Location);
761                         Assign a = new SimpleAssign (fe, new SimpleName ("value", Location));
762                         Set.Block.AddStatement (new StatementExpression (a));
763                 }
764
765                 public override bool Define ()
766                 {
767                         if (!base.Define ())
768                                 return false;
769
770                         flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
771
772                         if (!IsInterface && (ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0 &&
773                                 AccessorSecond != null && Get.Block == null && Set.Block == null) {
774                                 if (RootContext.Version <= LanguageVersion.ISO_2)
775                                         Report.FeatureIsNotAvailable (Location, "automatically implemented properties");
776
777                                 Get.ModFlags |= Modifiers.COMPILER_GENERATED;
778                                 Set.ModFlags |= Modifiers.COMPILER_GENERATED;
779                                 CreateAutomaticProperty ();
780                         }
781
782                         if (!DefineAccessors ())
783                                 return false;
784
785                         if (!CheckBase ())
786                                 return false;
787
788                         DefineBuilders (MemberKind.Property, ParametersCompiled.EmptyReadOnlyParameters);
789                         return true;
790                 }
791
792                 public override void Emit ()
793                 {
794                         if ((AccessorFirst.ModFlags & (Modifiers.STATIC | Modifiers.COMPILER_GENERATED)) == Modifiers.COMPILER_GENERATED && Parent.PartialContainer.HasExplicitLayout) {
795                                 Report.Error (842, Location,
796                                         "Automatically implemented property `{0}' cannot be used inside a type with an explicit StructLayout attribute",
797                                         GetSignatureForError ());
798                         }
799
800                         base.Emit ();
801                 }
802
803                 public override string GetDocCommentName (DeclSpace ds)
804                 {
805                         return String.Concat (DocCommentHeader, ds.Name, ".", GetFullName (ShortName).Replace ('.', '#'));
806                 }
807         }
808
809         /// <summary>
810         /// For case when event is declared like property (with add and remove accessors).
811         /// </summary>
812         public class EventProperty: Event {
813                 public abstract class AEventPropertyAccessor : AEventAccessor
814                 {
815                         protected AEventPropertyAccessor (EventProperty method, string prefix, Attributes attrs, Location loc)
816                                 : base (method, prefix, attrs, loc)
817                         {
818                         }
819
820                         public override MethodBuilder Define (DeclSpace ds)
821                         {
822                                 CheckAbstractAndExtern (block != null);
823                                 return base.Define (ds);
824                         }
825                         
826                         public override string GetSignatureForError ()
827                         {
828                                 return method.GetSignatureForError () + "." + prefix.Substring (0, prefix.Length - 1);
829                         }
830                 }
831
832                 public sealed class AddDelegateMethod: AEventPropertyAccessor
833                 {
834                         public AddDelegateMethod (EventProperty method, Attributes attrs, Location loc)
835                                 : base (method, AddPrefix, attrs, loc)
836                         {
837                         }
838                 }
839
840                 public sealed class RemoveDelegateMethod: AEventPropertyAccessor
841                 {
842                         public RemoveDelegateMethod (EventProperty method, Attributes attrs, Location loc)
843                                 : base (method, RemovePrefix, attrs, loc)
844                         {
845                         }
846                 }
847
848                 static readonly string[] attribute_targets = new string [] { "event" };
849
850                 public EventProperty (DeclSpace parent, FullNamedExpression type, Modifiers mod_flags, MemberName name, Attributes attrs)
851                         : base (parent, type, mod_flags, name, attrs)
852                 {
853                 }
854
855                 public override bool Define()
856                 {
857                         if (!base.Define ())
858                                 return false;
859
860                         SetIsUsed ();
861                         return true;
862                 }
863
864                 public override string[] ValidAttributeTargets {
865                         get {
866                                 return attribute_targets;
867                         }
868                 }
869         }
870
871         /// <summary>
872         /// Event is declared like field.
873         /// </summary>
874         public class EventField : Event {
875                 abstract class EventFieldAccessor : AEventAccessor
876                 {
877                         protected EventFieldAccessor (EventField method, string prefix)
878                                 : base (method, prefix, null, Location.Null)
879                         {
880                         }
881
882                         public override void Emit (DeclSpace parent)
883                         {
884                                 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0) {
885                                         if (parent is Class) {
886                                                 MethodBuilder mb = method_data.MethodBuilder;
887                                                 mb.SetImplementationFlags (mb.GetMethodImplementationFlags () | MethodImplAttributes.Synchronized);
888                                         }
889
890                                         var field_info = ((EventField) method).backing_field;
891                                         FieldExpr f_expr = new FieldExpr (field_info, Location);
892                                         if ((method.ModFlags & Modifiers.STATIC) == 0)
893                                                 f_expr.InstanceExpression = new CompilerGeneratedThis (field_info.Spec.MemberType, Location);
894
895                                         block = new ToplevelBlock (Compiler, ParameterInfo, Location);
896                                         block.AddStatement (new StatementExpression (
897                                                 new CompoundAssign (Operation,
898                                                         f_expr,
899                                                         block.GetParameterReference (ParameterInfo[0].Name, Location),
900                                                         Location)));
901                                 }
902
903                                 base.Emit (parent);
904                         }
905
906                         protected abstract Binary.Operator Operation { get; }
907                 }
908
909                 sealed class AddDelegateMethod: EventFieldAccessor
910                 {
911                         public AddDelegateMethod (EventField method):
912                                 base (method, AddPrefix)
913                         {
914                         }
915
916                         protected override Binary.Operator Operation {
917                                 get { return Binary.Operator.Addition; }
918                         }
919                 }
920
921                 sealed class RemoveDelegateMethod: EventFieldAccessor
922                 {
923                         public RemoveDelegateMethod (EventField method):
924                                 base (method, RemovePrefix)
925                         {
926                         }
927
928                         protected override Binary.Operator Operation {
929                                 get { return Binary.Operator.Subtraction; }
930                         }
931                 }
932
933
934                 static readonly string[] attribute_targets = new string [] { "event", "field", "method" };
935                 static readonly string[] attribute_targets_interface = new string[] { "event", "method" };
936
937                 Expression initializer;
938                 Field backing_field;
939                 List<FieldDeclarator> declarators;
940
941                 public EventField (DeclSpace parent, FullNamedExpression type, Modifiers mod_flags, MemberName name, Attributes attrs)
942                         : base (parent, type, mod_flags, name, attrs)
943                 {
944                         Add = new AddDelegateMethod (this);
945                         Remove = new RemoveDelegateMethod (this);
946                 }
947
948                 #region Properties
949
950                 bool HasBackingField {
951                         get {
952                                 return !IsInterface && (ModFlags & Modifiers.ABSTRACT) == 0;
953                         }
954                 }
955
956                 public Expression Initializer {
957                         get {
958                                 return initializer;
959                         }
960                         set {
961                                 initializer = value;
962                         }
963                 }
964
965                 public override string[] ValidAttributeTargets {
966                         get {
967                                 return HasBackingField ? attribute_targets : attribute_targets_interface;
968                         }
969                 }
970
971                 #endregion
972
973                 public void AddDeclarator (FieldDeclarator declarator)
974                 {
975                         if (declarators == null)
976                                 declarators = new List<FieldDeclarator> (2);
977
978                         declarators.Add (declarator);
979
980                         // TODO: This will probably break
981                         Parent.AddMember (this, declarator.Name.Value);
982                 }
983
984                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
985                 {
986                         if (a.Target == AttributeTargets.Field) {
987                                 backing_field.ApplyAttributeBuilder (a, ctor, cdata, pa);
988                                 return;
989                         }
990
991                         if (a.Target == AttributeTargets.Method) {
992                                 int errors = Report.Errors;
993                                 Add.ApplyAttributeBuilder (a, ctor, cdata, pa);
994                                 if (errors == Report.Errors)
995                                         Remove.ApplyAttributeBuilder (a, ctor, cdata, pa);
996                                 return;
997                         }
998
999                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
1000                 }
1001
1002                 public override bool Define()
1003                 {
1004                         if (!base.Define ())
1005                                 return false;
1006
1007                         if (declarators != null) {
1008                                 var t = new TypeExpression (MemberType, TypeExpression.Location);
1009                                 int index = Parent.PartialContainer.Events.IndexOf (this);
1010                                 foreach (var d in declarators) {
1011                                         var ef = new EventField (Parent, t, ModFlags, new MemberName (d.Name.Value, d.Name.Location), OptAttributes);
1012
1013                                         if (d.Initializer != null)
1014                                                 ef.initializer = d.Initializer;
1015
1016                                         Parent.PartialContainer.Events.Insert (++index, ef);
1017                                 }
1018                         }
1019
1020                         if (!HasBackingField) {
1021                                 SetIsUsed ();
1022                                 return true;
1023                         }
1024
1025                         if (Add.IsInterfaceImplementation)
1026                                 SetIsUsed ();
1027
1028                         backing_field = new Field (Parent,
1029                                 new TypeExpression (MemberType, Location),
1030                                 Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
1031                                 MemberName, null);
1032
1033                         Parent.PartialContainer.AddField (backing_field);
1034                         backing_field.Initializer = Initializer;
1035                         backing_field.ModFlags &= ~Modifiers.COMPILER_GENERATED;
1036
1037                         // Call define because we passed fields definition
1038                         backing_field.Define ();
1039
1040                         // Set backing field for event fields
1041                         spec.BackingField = backing_field.Spec;
1042
1043                         return true;
1044                 }
1045         }
1046
1047         public abstract class Event : PropertyBasedMember {
1048                 public abstract class AEventAccessor : AbstractPropertyEventMethod
1049                 {
1050                         protected readonly Event method;
1051                         ImplicitParameter param_attr;
1052                         ParametersCompiled parameters;
1053
1054                         static readonly string[] attribute_targets = new string [] { "method", "param", "return" };
1055
1056                         public const string AddPrefix = "add_";
1057                         public const string RemovePrefix = "remove_";
1058
1059                         protected AEventAccessor (Event method, string prefix, Attributes attrs, Location loc)
1060                                 : base (method, prefix, attrs, loc)
1061                         {
1062                                 this.method = method;
1063                                 this.ModFlags = method.ModFlags;
1064                                 this.parameters = ParametersCompiled.CreateImplicitParameter (method.TypeExpression, loc);;
1065                         }
1066
1067                         public bool IsInterfaceImplementation {
1068                                 get { return method_data.implementing != null; }
1069                         }
1070
1071                         public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1072                         {
1073                                 if (a.IsInternalMethodImplAttribute) {
1074                                         method.is_external_implementation = true;
1075                                 }
1076
1077                                 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
1078                         }
1079
1080                         protected override void ApplyToExtraTarget (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1081                         {
1082                                 if (a.Target == AttributeTargets.Parameter) {
1083                                         if (param_attr == null)
1084                                                 param_attr = new ImplicitParameter (method_data.MethodBuilder);
1085
1086                                         param_attr.ApplyAttributeBuilder (a, ctor, cdata, pa);
1087                                         return;
1088                                 }
1089
1090                                 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
1091                         }
1092
1093                         public override AttributeTargets AttributeTargets {
1094                                 get {
1095                                         return AttributeTargets.Method;
1096                                 }
1097                         }
1098
1099                         public override bool IsClsComplianceRequired ()
1100                         {
1101                                 return method.IsClsComplianceRequired ();
1102                         }
1103
1104                         public virtual MethodBuilder Define (DeclSpace parent)
1105                         {
1106                                 parameters.Resolve (this);
1107
1108                                 method_data = new MethodData (method, method.ModFlags,
1109                                         method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
1110
1111                                 if (!method_data.Define (parent, method.GetFullName (MemberName), Report))
1112                                         return null;
1113
1114                                 MethodBuilder mb = method_data.MethodBuilder;
1115                                 ParameterInfo.ApplyAttributes (mb);
1116                                 Spec = new MethodSpec (MemberKind.Method, parent.PartialContainer.Definition, this, ReturnType, mb, ParameterInfo, method.ModFlags);
1117                                 Spec.IsAccessor = true;
1118
1119                                 return mb;
1120                         }
1121
1122                         public override TypeSpec ReturnType {
1123                                 get {
1124                                         return TypeManager.void_type;
1125                                 }
1126                         }
1127
1128                         public override ObsoleteAttribute GetAttributeObsolete ()
1129                         {
1130                                 return method.GetAttributeObsolete ();
1131                         }
1132
1133                         public override string[] ValidAttributeTargets {
1134                                 get {
1135                                         return attribute_targets;
1136                                 }
1137                         }
1138
1139                         public override ParametersCompiled ParameterInfo {
1140                                 get {
1141                                         return parameters;
1142                                 }
1143                         }
1144                 }
1145
1146                 AEventAccessor add, remove;
1147                 EventBuilder EventBuilder;
1148                 protected EventSpec spec;
1149
1150                 protected Event (DeclSpace parent, FullNamedExpression type, Modifiers mod_flags, MemberName name, Attributes attrs)
1151                         : base (parent, null, type, mod_flags,
1152                                 parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
1153                                 parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct :
1154                                 AllowedModifiersClass,
1155                                 name, attrs)
1156                 {
1157                 }
1158
1159                 #region Properties
1160
1161                 public override AttributeTargets AttributeTargets {
1162                         get {
1163                                 return AttributeTargets.Event;
1164                         }
1165                 }
1166
1167                 public AEventAccessor Add {
1168                         get {
1169                                 return this.add;
1170                         }
1171                         set {
1172                                 add = value;
1173                                 Parent.AddMember (value);
1174                         }
1175                 }
1176
1177                 public AEventAccessor Remove {
1178                         get {
1179                                 return this.remove;
1180                         }
1181                         set {
1182                                 remove = value;
1183                                 Parent.AddMember (value);
1184                         }
1185                 }
1186                 #endregion
1187
1188                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1189                 {
1190                         if ((a.HasSecurityAttribute)) {
1191                                 a.Error_InvalidSecurityParent ();
1192                                 return;
1193                         }
1194
1195                         EventBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
1196                 }
1197
1198                 protected override bool CheckOverrideAgainstBase (MemberSpec base_member)
1199                 {
1200                         var ok = base.CheckOverrideAgainstBase (base_member);
1201
1202                         if (!CheckAccessModifiers (this, base_member)) {
1203                                 Error_CannotChangeAccessModifiers (this, base_member);
1204                                 ok = false;
1205                         }
1206
1207                         return ok;
1208                 }
1209
1210                 public override bool Define ()
1211                 {
1212                         if (!base.Define ())
1213                                 return false;
1214
1215                         if (!TypeManager.IsDelegateType (MemberType)) {
1216                                 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
1217                         }
1218
1219                         if (!CheckBase ())
1220                                 return false;
1221
1222                         //
1223                         // Now define the accessors
1224                         //
1225                         var AddBuilder = Add.Define (Parent);
1226                         if (AddBuilder == null)
1227                                 return false;
1228
1229                         var RemoveBuilder = remove.Define (Parent);
1230                         if (RemoveBuilder == null)
1231                                 return false;
1232
1233                         EventBuilder = Parent.TypeBuilder.DefineEvent (Name, EventAttributes.None, MemberType.GetMetaInfo ());
1234                         EventBuilder.SetAddOnMethod (AddBuilder);
1235                         EventBuilder.SetRemoveOnMethod (RemoveBuilder);
1236
1237                         spec = new EventSpec (Parent.Definition, this, MemberType, ModFlags, Add.Spec, remove.Spec);
1238
1239                         Parent.MemberCache.AddMember (this, Name, spec);
1240                         Parent.MemberCache.AddMember (this, AddBuilder.Name, Add.Spec);
1241                         Parent.MemberCache.AddMember (this, RemoveBuilder.Name, remove.Spec);
1242
1243                         return true;
1244                 }
1245
1246                 public override void Emit ()
1247                 {
1248                         CheckReservedNameConflict (null, add.Spec);
1249                         CheckReservedNameConflict (null, remove.Spec);
1250
1251                         if (OptAttributes != null) {
1252                                 OptAttributes.Emit ();
1253                         }
1254
1255                         Add.Emit (Parent);
1256                         Remove.Emit (Parent);
1257
1258                         base.Emit ();
1259                 }
1260
1261                 //
1262                 //   Represents header string for documentation comment.
1263                 //
1264                 public override string DocCommentHeader {
1265                         get { return "E:"; }
1266                 }
1267         }
1268
1269         public class EventSpec : MemberSpec, IInterfaceMemberSpec
1270         {
1271                 MethodSpec add, remove;
1272                 FieldSpec backing_field;
1273
1274                 public EventSpec (TypeSpec declaringType, IMemberDefinition definition, TypeSpec eventType, Modifiers modifiers, MethodSpec add, MethodSpec remove)
1275                         : base (MemberKind.Event, declaringType, definition, modifiers)
1276                 {
1277                         this.AccessorAdd = add;
1278                         this.AccessorRemove = remove;
1279                         this.MemberType = eventType;
1280                 }
1281
1282                 #region Properties
1283
1284                 public MethodSpec AccessorAdd { 
1285                         get {
1286                                 return add;
1287                         }
1288                         set {
1289                                 add = value;
1290                         }
1291                 }
1292
1293                 public MethodSpec AccessorRemove {
1294                         get {
1295                                 return remove;
1296                         }
1297                         set {
1298                                 remove = value;
1299                         }
1300                 }
1301
1302                 public FieldSpec BackingField {
1303                         get {
1304                                 return backing_field;
1305                         }
1306                         set {
1307                                 backing_field = value;
1308                         }
1309                 }
1310
1311                 public TypeSpec MemberType { get; private set; }
1312
1313                 #endregion
1314
1315                 public override MemberSpec InflateMember (TypeParameterInflator inflator)
1316                 {
1317                         var es = (EventSpec) base.InflateMember (inflator);
1318                         es.MemberType = inflator.Inflate (MemberType);
1319                         return es;
1320                 }
1321         }
1322  
1323         public class Indexer : PropertyBase, IParametersMember
1324         {
1325                 public class GetIndexerMethod : GetMethod, IParametersMember
1326                 {
1327                         ParametersCompiled parameters;
1328
1329                         public GetIndexerMethod (PropertyBase property, Modifiers modifiers, ParametersCompiled parameters, Attributes attrs, Location loc)
1330                                 : base (property, modifiers, attrs, loc)
1331                         {
1332                                 this.parameters = parameters;
1333                         }
1334
1335                         public override MethodBuilder Define (DeclSpace parent)
1336                         {
1337                                 parameters.Resolve (this);
1338                                 return base.Define (parent);
1339                         }
1340
1341                         public override ParametersCompiled ParameterInfo {
1342                                 get {
1343                                         return parameters;
1344                                 }
1345                         }
1346
1347                         #region IParametersMember Members
1348
1349                         AParametersCollection IParametersMember.Parameters {
1350                                 get {
1351                                         return parameters;
1352                                 }
1353                         }
1354
1355                         TypeSpec IInterfaceMemberSpec.MemberType {
1356                                 get {
1357                                         return ReturnType;
1358                                 }
1359                         }
1360
1361                         #endregion
1362                 }
1363
1364                 public class SetIndexerMethod : SetMethod, IParametersMember
1365                 {
1366                         public SetIndexerMethod (PropertyBase property, Modifiers modifiers, ParametersCompiled parameters, Attributes attrs, Location loc)
1367                                 : base (property, modifiers, parameters, attrs, loc)
1368                         {
1369                         }
1370
1371                         #region IParametersMember Members
1372
1373                         AParametersCollection IParametersMember.Parameters {
1374                                 get {
1375                                         return parameters;
1376                                 }
1377                         }
1378
1379                         TypeSpec IInterfaceMemberSpec.MemberType {
1380                                 get {
1381                                         return ReturnType;
1382                                 }
1383                         }
1384
1385                         #endregion
1386                 }
1387
1388                 const Modifiers AllowedModifiers =
1389                         Modifiers.NEW |
1390                         Modifiers.PUBLIC |
1391                         Modifiers.PROTECTED |
1392                         Modifiers.INTERNAL |
1393                         Modifiers.PRIVATE |
1394                         Modifiers.VIRTUAL |
1395                         Modifiers.SEALED |
1396                         Modifiers.OVERRIDE |
1397                         Modifiers.UNSAFE |
1398                         Modifiers.EXTERN |
1399                         Modifiers.ABSTRACT;
1400
1401                 const Modifiers AllowedInterfaceModifiers =
1402                         Modifiers.NEW;
1403
1404                 readonly ParametersCompiled parameters;
1405
1406                 public Indexer (DeclSpace parent, FullNamedExpression type, MemberName name, Modifiers mod,
1407                                 ParametersCompiled parameters, Attributes attrs)
1408                         : base (parent, type, mod,
1409                                 parent.PartialContainer.Kind == MemberKind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
1410                                 name, attrs)
1411                 {
1412                         this.parameters = parameters;
1413                 }
1414
1415                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1416                 {
1417                         if (a.Type == pa.IndexerName) {
1418                                 if (IsExplicitImpl) {
1419                                         Report.Error (415, a.Location,
1420                                                 "The `{0}' attribute is valid only on an indexer that is not an explicit interface member declaration",
1421                                                 TypeManager.CSharpName (a.Type));
1422                                 }
1423
1424                                 // Attribute was copied to container
1425                                 return;
1426                         }
1427
1428                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
1429                 }
1430
1431                 protected override bool CheckForDuplications ()
1432                 {
1433                         return Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1434                 }
1435                 
1436                 public override bool Define ()
1437                 {
1438                         if (!base.Define ())
1439                                 return false;
1440
1441                         if (!DefineParameters (parameters))
1442                                 return false;
1443
1444                         if (OptAttributes != null) {
1445                                 Attribute indexer_attr = OptAttributes.Search (PredefinedAttributes.Get.IndexerName);
1446                                 if (indexer_attr != null) {
1447                                         var compiling = indexer_attr.Type.MemberDefinition as TypeContainer;
1448                                         if (compiling != null)
1449                                                 compiling.Define ();
1450
1451                                         string name = indexer_attr.GetIndexerAttributeValue ();
1452                                         if ((ModFlags & Modifiers.OVERRIDE) != 0) {
1453                                                 Report.Error (609, indexer_attr.Location,
1454                                                         "Cannot set the `IndexerName' attribute on an indexer marked override");
1455                                         }
1456
1457                                         if (!string.IsNullOrEmpty (name))
1458                                                 ShortName = name;
1459                                 }
1460                         }
1461
1462                         if (InterfaceType != null) {
1463                                 string base_IndexerName = InterfaceType.MemberDefinition.GetAttributeDefaultMember ();
1464                                 if (base_IndexerName != Name)
1465                                         ShortName = base_IndexerName;
1466                         }
1467
1468                         if (!Parent.PartialContainer.AddMember (this))
1469                                 return false;
1470
1471                         flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
1472                         
1473                         if (!DefineAccessors ())
1474                                 return false;
1475
1476                         if (!CheckBase ())
1477                                 return false;
1478
1479                         DefineBuilders (MemberKind.Indexer, parameters);
1480                         return true;
1481                 }
1482
1483                 public override bool EnableOverloadChecks (MemberCore overload)
1484                 {
1485                         if (overload is Indexer) {
1486                                 caching_flags |= Flags.MethodOverloadsExist;
1487                                 return true;
1488                         }
1489
1490                         return base.EnableOverloadChecks (overload);
1491                 }
1492
1493                 public override string GetDocCommentName (DeclSpace ds)
1494                 {
1495                         return DocUtil.GetMethodDocCommentName (this, parameters, ds);
1496                 }
1497
1498                 public override string GetSignatureForError ()
1499                 {
1500                         StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
1501                         if (MemberName.Left != null) {
1502                                 sb.Append ('.');
1503                                 sb.Append (MemberName.Left.GetSignatureForError ());
1504                         }
1505
1506                         sb.Append (".this");
1507                         sb.Append (parameters.GetSignatureForError ().Replace ('(', '[').Replace (')', ']'));
1508                         return sb.ToString ();
1509                 }
1510
1511                 public AParametersCollection Parameters {
1512                         get {
1513                                 return parameters;
1514                         }
1515                 }
1516
1517                 public ParametersCompiled ParameterInfo {
1518                         get {
1519                                 return parameters;
1520                         }
1521                 }
1522
1523                 protected override bool VerifyClsCompliance ()
1524                 {
1525                         if (!base.VerifyClsCompliance ())
1526                                 return false;
1527
1528                         parameters.VerifyClsCompliance (this);
1529                         return true;
1530                 }
1531         }
1532
1533         public class IndexerSpec : PropertySpec, IParametersMember
1534         {
1535                 AParametersCollection parameters;
1536
1537                 public IndexerSpec (TypeSpec declaringType, IMemberDefinition definition, TypeSpec memberType, AParametersCollection parameters, PropertyInfo info, Modifiers modifiers)
1538                         : base (MemberKind.Indexer, declaringType, definition, memberType, info, modifiers)
1539                 {
1540                         this.parameters = parameters;
1541                 }
1542
1543                 #region Properties
1544                 public AParametersCollection Parameters {
1545                         get {
1546                                 return parameters;
1547                         }
1548                 }
1549                 #endregion
1550
1551                 public override string GetSignatureForError ()
1552                 {
1553                         return DeclaringType.GetSignatureForError () + ".this" + parameters.GetSignatureForError ("[", "]", parameters.Count);
1554                 }
1555
1556                 public override MemberSpec InflateMember (TypeParameterInflator inflator)
1557                 {
1558                         var spec = (IndexerSpec) base.InflateMember (inflator);
1559                         spec.parameters = parameters.Inflate (inflator);
1560                         return spec;
1561                 }
1562         }
1563 }