Merge pull request #1230 from esdrubal/xslattribute
[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 // Copyright 2011 Xamarin Inc
13 //
14
15 using System;
16 using System.Collections.Generic;
17 using System.Text;
18 using Mono.CompilerServices.SymbolWriter;
19
20 #if NET_2_1
21 using XmlElement = System.Object;
22 #endif
23
24 #if STATIC
25 using IKVM.Reflection;
26 using IKVM.Reflection.Emit;
27 #else
28 using System.Reflection;
29 using System.Reflection.Emit;
30 #endif
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                 protected PropertyBasedMember (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name, Attributes attrs)
39                         : base (parent, type, mod, allowed_mod, name, attrs)
40                 {
41                 }
42
43                 protected void CheckReservedNameConflict (string prefix, MethodSpec accessor)
44                 {
45                         string name;
46                         AParametersCollection parameters;
47                         if (accessor != null) {
48                                 name = accessor.Name;
49                                 parameters = accessor.Parameters;
50                         } else {
51                                 name = prefix + ShortName;
52                                 if (IsExplicitImpl)
53                                         name = MemberName.Left + "." + name;
54
55                                 if (this is Indexer) {
56                                         parameters = ((Indexer) this).ParameterInfo;
57                                         if (prefix[0] == 's') {
58                                                 var data = new IParameterData[parameters.Count + 1];
59                                                 Array.Copy (parameters.FixedParameters, data, data.Length - 1);
60                                                 data[data.Length - 1] = new ParameterData ("value", Parameter.Modifier.NONE);
61                                                 var types = new TypeSpec[data.Length];
62                                                 Array.Copy (parameters.Types, types, data.Length - 1);
63                                                 types[data.Length - 1] = member_type;
64
65                                                 parameters = new ParametersImported (data, types, false);
66                                         }
67                                 } else {
68                                         if (prefix[0] == 's')
69                                                 parameters = ParametersCompiled.CreateFullyResolved (new[] { member_type });
70                                         else
71                                                 parameters = ParametersCompiled.EmptyReadOnlyParameters;
72                                 }
73                         }
74
75                         var conflict = MemberCache.FindMember (Parent.Definition,
76                                 new MemberFilter (name, 0, MemberKind.Method, parameters, null),
77                                 BindingRestriction.DeclaredOnly | BindingRestriction.NoAccessors);
78
79                         if (conflict != null) {
80                                 Report.SymbolRelatedToPreviousError (conflict);
81                                 Report.Error (82, Location, "A member `{0}' is already reserved", conflict.GetSignatureForError ());
82                         }
83                 }
84
85                 public abstract void PrepareEmit ();
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 HasDifferentAccessibility {
137                         get {
138                                 return HasGet && HasSet && 
139                                         (Get.Modifiers & Modifiers.AccessibilityMask) != (Set.Modifiers & Modifiers.AccessibilityMask);
140                         }
141                 }
142
143                 public bool HasGet {
144                         get {
145                                 return Get != null;
146                         }
147                 }
148
149                 public bool HasSet {
150                         get {
151                                 return Set != null;
152                         }
153                 }
154
155                 public PropertyInfo MetaInfo {
156                         get {
157                                 if ((state & StateFlags.PendingMetaInflate) != 0)
158                                         throw new NotSupportedException ();
159
160                                 return info;
161                         }
162                 }
163
164                 public TypeSpec MemberType {
165                         get {
166                                 return memberType;
167                         }
168                 }
169
170                 #endregion
171
172                 public override MemberSpec InflateMember (TypeParameterInflator inflator)
173                 {
174                         var ps = (PropertySpec) base.InflateMember (inflator);
175                         ps.memberType = inflator.Inflate (memberType);
176                         return ps;
177                 }
178
179                 public override List<MissingTypeSpecReference> ResolveMissingDependencies (MemberSpec caller)
180                 {
181                         return memberType.ResolveMissingDependencies (this);
182                 }
183         }
184
185         //
186         // Properties and Indexers both generate PropertyBuilders, we use this to share 
187         // their common bits.
188         //
189         abstract public class PropertyBase : PropertyBasedMember {
190
191                 public class GetMethod : PropertyMethod
192                 {
193                         static readonly string[] attribute_targets = new string [] { "method", "return" };
194
195                         internal const string Prefix = "get_";
196
197                         public GetMethod (PropertyBase method, Modifiers modifiers, Attributes attrs, Location loc)
198                                 : base (method, Prefix, modifiers, attrs, loc)
199                         {
200                         }
201
202                         public override void Define (TypeContainer parent)
203                         {
204                                 base.Define (parent);
205
206                                 Spec = new MethodSpec (MemberKind.Method, parent.PartialContainer.Definition, this, ReturnType, ParameterInfo, ModFlags);
207
208                                 method_data = new MethodData (method, ModFlags, flags, this);
209
210                                 method_data.Define (parent.PartialContainer, method.GetFullName (MemberName));
211                         }
212
213                         public override TypeSpec ReturnType {
214                                 get {
215                                         return method.MemberType;
216                                 }
217                         }
218
219                         public override ParametersCompiled ParameterInfo {
220                                 get {
221                                         return ParametersCompiled.EmptyReadOnlyParameters;
222                                 }
223                         }
224
225                         public override string[] ValidAttributeTargets {
226                                 get {
227                                         return attribute_targets;
228                                 }
229                         }
230                 }
231
232                 public class SetMethod : PropertyMethod {
233
234                         static readonly string[] attribute_targets = new string[] { "method", "param", "return" };
235
236                         internal const string Prefix = "set_";
237
238                         protected ParametersCompiled parameters;
239
240                         public SetMethod (PropertyBase method, Modifiers modifiers, ParametersCompiled parameters, Attributes attrs, Location loc)
241                                 : base (method, Prefix, modifiers, attrs, loc)
242                         {
243                                 this.parameters = parameters;
244                         }
245
246                         protected override void ApplyToExtraTarget (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
247                         {
248                                 if (a.Target == AttributeTargets.Parameter) {
249                                         parameters[0].ApplyAttributeBuilder (a, ctor, cdata, pa);
250                                         return;
251                                 }
252
253                                 base.ApplyToExtraTarget (a, ctor, cdata, pa);
254                         }
255
256                         public override ParametersCompiled ParameterInfo {
257                             get {
258                                 return parameters;
259                             }
260                         }
261
262                         public override void Define (TypeContainer parent)
263                         {
264                                 parameters.Resolve (this);
265                                 
266                                 base.Define (parent);
267
268                                 Spec = new MethodSpec (MemberKind.Method, parent.PartialContainer.Definition, this, ReturnType, ParameterInfo, ModFlags);
269
270                                 method_data = new MethodData (method, ModFlags, flags, this);
271
272                                 method_data.Define (parent.PartialContainer, method.GetFullName (MemberName));
273                         }
274
275                         public override TypeSpec ReturnType {
276                                 get {
277                                         return Parent.Compiler.BuiltinTypes.Void;
278                                 }
279                         }
280
281                         public override string[] ValidAttributeTargets {
282                                 get {
283                                         return attribute_targets;
284                                 }
285                         }
286                 }
287
288                 static readonly string[] attribute_targets = new string[] { "property" };
289
290                 public abstract class PropertyMethod : AbstractPropertyEventMethod
291                 {
292                         const Modifiers AllowedModifiers =
293                                 Modifiers.PUBLIC |
294                                 Modifiers.PROTECTED |
295                                 Modifiers.INTERNAL |
296                                 Modifiers.PRIVATE;
297                 
298                         protected readonly PropertyBase method;
299                         protected MethodAttributes flags;
300
301                         public PropertyMethod (PropertyBase method, string prefix, Modifiers modifiers, Attributes attrs, Location loc)
302                                 : base (method, prefix, attrs, loc)
303                         {
304                                 this.method = method;
305                                 this.ModFlags = ModifiersExtensions.Check (AllowedModifiers, modifiers, 0, loc, Report);
306                                 this.ModFlags |= (method.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE));
307                         }
308
309                         public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
310                         {
311                                 if (a.Type == pa.MethodImpl) {
312                                         method.is_external_implementation = a.IsInternalCall ();
313                                 }
314
315                                 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
316                         }
317
318                         public override AttributeTargets AttributeTargets {
319                                 get {
320                                         return AttributeTargets.Method;
321                                 }
322                         }
323
324                         public override bool IsClsComplianceRequired ()
325                         {
326                                 return method.IsClsComplianceRequired ();
327                         }
328
329                         public virtual void Define (TypeContainer parent)
330                         {
331                                 var container = parent.PartialContainer;
332
333                                 //
334                                 // Check for custom access modifier
335                                 //
336                                 if ((ModFlags & Modifiers.AccessibilityMask) == 0) {
337                                         ModFlags |= method.ModFlags;
338                                         flags = method.flags;
339                                 } else {
340                                         if (container.Kind == MemberKind.Interface)
341                                                 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
342                                                         GetSignatureForError ());
343                                         else if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
344                                                 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
345                                         }
346
347                                         CheckModifiers (ModFlags);
348                                         ModFlags |= (method.ModFlags & (~Modifiers.AccessibilityMask));
349                                         ModFlags |= Modifiers.PROPERTY_CUSTOM;
350                                         flags = ModifiersExtensions.MethodAttr (ModFlags);
351                                         flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
352                                 }
353
354                                 CheckAbstractAndExtern (block != null);
355                                 CheckProtectedModifier ();
356
357                                 if (block != null) {
358                                         if (block.IsIterator)
359                                                 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
360
361                                         if (Compiler.Settings.WriteMetadataOnly)
362                                                 block = null;
363                                 }
364                         }
365
366                         public bool HasCustomAccessModifier {
367                                 get {
368                                         return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
369                                 }
370                         }
371
372                         public PropertyBase Property {
373                                 get {
374                                         return method;
375                                 }
376                         }
377
378                         public override ObsoleteAttribute GetAttributeObsolete ()
379                         {
380                                 return method.GetAttributeObsolete ();
381                         }
382
383                         public override string GetSignatureForError()
384                         {
385                                 return method.GetSignatureForError () + "." + prefix.Substring (0, 3);
386                         }
387
388                         void CheckModifiers (Modifiers modflags)
389                         {
390                                 if (!ModifiersExtensions.IsRestrictedModifier (modflags & Modifiers.AccessibilityMask, method.ModFlags & Modifiers.AccessibilityMask)) {
391                                         Report.Error (273, Location,
392                                                 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
393                                                 GetSignatureForError (), method.GetSignatureForError ());
394                                 }
395                         }
396                 }
397
398                 PropertyMethod get, set, first;
399                 PropertyBuilder PropertyBuilder;
400
401                 protected PropertyBase (TypeDefinition parent, FullNamedExpression type, Modifiers mod_flags, Modifiers allowed_mod, MemberName name, Attributes attrs)
402                         : base (parent, type, mod_flags, allowed_mod, name, attrs)
403                 {
404                 }
405
406                 #region Properties
407
408                 public override AttributeTargets AttributeTargets {
409                         get {
410                                 return AttributeTargets.Property;
411                         }
412                 }
413
414                 public PropertyMethod AccessorFirst {
415                         get {
416                                 return first;
417                         }
418                 }
419
420                 public PropertyMethod AccessorSecond {
421                         get {
422                                 return first == get ? set : get;
423                         }
424                 }
425
426                 public override Variance ExpectedMemberTypeVariance {
427                         get {
428                                 return (get != null && set != null) ?
429                                         Variance.None : set == null ?
430                                         Variance.Covariant :
431                                         Variance.Contravariant;
432                         }
433                 }
434
435                 public PropertyMethod Get {
436                         get {
437                                 return get;
438                         }
439                         set {
440                                 get = value;
441                                 if (first == null)
442                                         first = value;
443
444                                 Parent.AddNameToContainer (get, get.MemberName.Basename);
445                         }
446                 }
447
448                 public PropertyMethod Set {
449                         get {
450                                 return set;
451                         }
452                         set {
453                                 set = value;
454                                 if (first == null)
455                                         first = value;
456
457                                 Parent.AddNameToContainer (set, set.MemberName.Basename);
458                         }
459                 }
460
461                 public override string[] ValidAttributeTargets {
462                         get {
463                                 return attribute_targets;
464                         }
465                 }
466
467                 #endregion
468
469                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
470                 {
471                         if (a.HasSecurityAttribute) {
472                                 a.Error_InvalidSecurityParent ();
473                                 return;
474                         }
475
476                         if (a.Type == pa.Dynamic) {
477                                 a.Error_MisusedDynamicAttribute ();
478                                 return;
479                         }
480
481                         PropertyBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
482                 }
483
484                 void CheckMissingAccessor (MemberKind kind, ParametersCompiled parameters, bool get)
485                 {
486                         if (IsExplicitImpl) {
487                                 MemberFilter filter;
488                                 if (kind == MemberKind.Indexer)
489                                         filter = new MemberFilter (MemberCache.IndexerNameAlias, 0, kind, parameters, null);
490                                 else
491                                         filter = new MemberFilter (MemberName.Name, 0, kind, null, null);
492
493                                 var implementing = MemberCache.FindMember (InterfaceType, filter, BindingRestriction.DeclaredOnly) as PropertySpec;
494
495                                 if (implementing == null)
496                                         return;
497
498                                 var accessor = get ? implementing.Get : implementing.Set;
499                                 if (accessor != null) {
500                                         Report.SymbolRelatedToPreviousError (accessor);
501                                         Report.Error (551, Location, "Explicit interface implementation `{0}' is missing accessor `{1}'",
502                                                 GetSignatureForError (), accessor.GetSignatureForError ());
503                                 }
504                         }
505                 }
506
507                 protected override bool CheckOverrideAgainstBase (MemberSpec base_member)
508                 {
509                         var ok = base.CheckOverrideAgainstBase (base_member);
510
511                         //
512                         // Check base property accessors conflict
513                         //
514                         var base_prop = (PropertySpec) base_member;
515                         if (Get == null) {
516                                 if ((ModFlags & Modifiers.SEALED) != 0 && base_prop.HasGet && !base_prop.Get.IsAccessible (this)) {
517                                         // TODO: Should be different error code but csc uses for some reason same
518                                         Report.SymbolRelatedToPreviousError (base_prop);
519                                         Report.Error (545, Location,
520                                                 "`{0}': cannot override because `{1}' does not have accessible get accessor",
521                                                 GetSignatureForError (), base_prop.GetSignatureForError ());
522                                         ok = false;
523                                 }
524                         } else {
525                                 if (!base_prop.HasGet) {
526                                         if (ok) {
527                                                 Report.SymbolRelatedToPreviousError (base_prop);
528                                                 Report.Error (545, Get.Location,
529                                                         "`{0}': cannot override because `{1}' does not have an overridable get accessor",
530                                                         Get.GetSignatureForError (), base_prop.GetSignatureForError ());
531                                                 ok = false;
532                                         }
533                                 } else if (Get.HasCustomAccessModifier || base_prop.HasDifferentAccessibility) {
534                                         if (!CheckAccessModifiers (Get, base_prop.Get)) {
535                                                 Error_CannotChangeAccessModifiers (Get, base_prop.Get);
536                                                 ok = false;
537                                         }
538                                 }
539                         }
540
541                         if (Set == null) {
542                                 if ((ModFlags & Modifiers.SEALED) != 0 && base_prop.HasSet && !base_prop.Set.IsAccessible (this)) {
543                                         // TODO: Should be different error code but csc uses for some reason same
544                                         Report.SymbolRelatedToPreviousError (base_prop);
545                                         Report.Error (546, Location,
546                                                 "`{0}': cannot override because `{1}' does not have accessible set accessor",
547                                                 GetSignatureForError (), base_prop.GetSignatureForError ());
548                                         ok = false;
549                                 }
550                         } else {
551                                 if (!base_prop.HasSet) {
552                                         if (ok) {
553                                                 Report.SymbolRelatedToPreviousError (base_prop);
554                                                 Report.Error (546, Set.Location,
555                                                         "`{0}': cannot override because `{1}' does not have an overridable set accessor",
556                                                         Set.GetSignatureForError (), base_prop.GetSignatureForError ());
557                                                 ok = false;
558                                         }
559                                 } else if (Set.HasCustomAccessModifier || base_prop.HasDifferentAccessibility) {
560                                         if (!CheckAccessModifiers (Set, base_prop.Set)) {
561                                                 Error_CannotChangeAccessModifiers (Set, base_prop.Set);
562                                                 ok = false;
563                                         }
564                                 }
565                         }
566
567                         if ((Set == null || !Set.HasCustomAccessModifier) && (Get == null || !Get.HasCustomAccessModifier)) {
568                                 if (!CheckAccessModifiers (this, base_prop)) {
569                                         Error_CannotChangeAccessModifiers (this, base_prop);
570                                         ok = false;
571                                 }
572                         }
573
574                         return ok;
575                 }
576
577                 protected override void DoMemberTypeDependentChecks ()
578                 {
579                         base.DoMemberTypeDependentChecks ();
580
581                         IsTypePermitted ();
582
583                         if (MemberType.IsStatic)
584                                 Error_StaticReturnType ();
585                 }
586
587                 protected override void DoMemberTypeIndependentChecks ()
588                 {
589                         base.DoMemberTypeIndependentChecks ();
590
591                         //
592                         // Accessors modifiers check
593                         //
594                         if (AccessorSecond != null) {
595                                 if ((Get.ModFlags & Modifiers.AccessibilityMask) != 0 && (Set.ModFlags & Modifiers.AccessibilityMask) != 0) {
596                                         Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
597                                                 GetSignatureForError ());
598                                 }
599                         } else if ((ModFlags & Modifiers.OVERRIDE) == 0 && 
600                                 (Get == null && (Set.ModFlags & Modifiers.AccessibilityMask) != 0) ||
601                                 (Set == null && (Get.ModFlags & Modifiers.AccessibilityMask) != 0)) {
602                                 Report.Error (276, Location, 
603                                               "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
604                                               GetSignatureForError ());
605                         }
606                 }
607
608                 protected bool DefineAccessors ()
609                 {
610                         first.Define (Parent);
611                         if (AccessorSecond != null)
612                                 AccessorSecond.Define (Parent);
613
614                         return true;
615                 }
616
617                 protected void DefineBuilders (MemberKind kind, ParametersCompiled parameters)
618                 {
619                         PropertyBuilder = Parent.TypeBuilder.DefineProperty (
620                                 GetFullName (MemberName), PropertyAttributes.None,
621 #if !BOOTSTRAP_BASIC    // Requires trunk version mscorlib
622                                 IsStatic ? 0 : CallingConventions.HasThis,
623 #endif
624                                 MemberType.GetMetaInfo (), null, null,
625                                 parameters.GetMetaInfo (), null, null);
626
627                         PropertySpec spec;
628                         if (kind == MemberKind.Indexer)
629                                 spec = new IndexerSpec (Parent.Definition, this, MemberType, parameters, PropertyBuilder, ModFlags);
630                         else
631                                 spec = new PropertySpec (kind, Parent.Definition, this, MemberType, PropertyBuilder, ModFlags);
632
633                         if (Get != null) {
634                                 spec.Get = Get.Spec;
635                                 Parent.MemberCache.AddMember (this, Get.Spec.Name, Get.Spec);
636                         } else {
637                                 CheckMissingAccessor (kind, parameters, true);
638                         }
639
640                         if (Set != null) {
641                                 spec.Set = Set.Spec;
642                                 Parent.MemberCache.AddMember (this, Set.Spec.Name, Set.Spec);
643                         } else {
644                                 CheckMissingAccessor (kind, parameters, false);
645                         }
646
647                         Parent.MemberCache.AddMember (this, PropertyBuilder.Name, spec);
648                 }
649
650                 public override void Emit ()
651                 {
652                         CheckReservedNameConflict (GetMethod.Prefix, get == null ? null : get.Spec);
653                         CheckReservedNameConflict (SetMethod.Prefix, set == null ? null : set.Spec);
654
655                         if (OptAttributes != null)
656                                 OptAttributes.Emit ();
657
658                         if (member_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
659                                 Module.PredefinedAttributes.Dynamic.EmitAttribute (PropertyBuilder);
660                         } else if (member_type.HasDynamicElement) {
661                                 Module.PredefinedAttributes.Dynamic.EmitAttribute (PropertyBuilder, member_type, Location);
662                         }
663
664                         ConstraintChecker.Check (this, member_type, type_expr.Location);
665
666                         first.Emit (Parent);
667                         if (AccessorSecond != null)
668                                 AccessorSecond.Emit (Parent);
669
670                         base.Emit ();
671                 }
672
673                 public override bool IsUsed {
674                         get {
675                                 if (IsExplicitImpl)
676                                         return true;
677
678                                 return Get.IsUsed | Set.IsUsed;
679                         }
680                 }
681
682                 public override void PrepareEmit ()
683                 {
684                         AccessorFirst.PrepareEmit ();
685                         if (AccessorSecond != null)
686                                 AccessorSecond.PrepareEmit ();
687
688                         if (get != null) {
689                                 var method = Get.Spec.GetMetaInfo () as MethodBuilder;
690                                 if (method != null)
691                                         PropertyBuilder.SetGetMethod (method);
692                         }
693
694                         if (set != null) {
695                                 var method = Set.Spec.GetMetaInfo () as MethodBuilder;
696                                 if (method != null)
697                                         PropertyBuilder.SetSetMethod (method);
698                         }
699                 }
700
701                 protected override void SetMemberName (MemberName new_name)
702                 {
703                         base.SetMemberName (new_name);
704
705                         if (Get != null)
706                                 Get.UpdateName (this);
707
708                         if (Set != null)
709                                 Set.UpdateName (this);
710                 }
711
712                 public override void WriteDebugSymbol (MonoSymbolFile file)
713                 {
714                         if (get != null)
715                                 get.WriteDebugSymbol (file);
716
717                         if (set != null)
718                                 set.WriteDebugSymbol (file);
719                 }
720
721                 //
722                 //   Represents header string for documentation comment.
723                 //
724                 public override string DocCommentHeader {
725                         get { return "P:"; }
726                 }
727         }
728                         
729         public class Property : PropertyBase
730         {
731                 public sealed class BackingField : Field
732                 {
733                         readonly Property property;
734                         const Modifiers DefaultModifiers = Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | Modifiers.DEBUGGER_HIDDEN;
735
736                         public BackingField (Property p, bool readOnly)
737                                 : base (p.Parent, p.type_expr, DefaultModifiers | (p.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
738                                 new MemberName ("<" + p.GetFullName (p.MemberName) + ">k__BackingField", p.Location), null)
739                         {
740                                 this.property = p;
741                                 if (readOnly)
742                                         ModFlags |= Modifiers.READONLY;
743                         }
744
745                         public Property OriginalProperty {
746                                 get {
747                                         return property;
748                                 }
749                         }
750
751                         public override string GetSignatureForError ()
752                         {
753                                 return property.GetSignatureForError ();
754                         }
755                 }
756
757                 static readonly string[] attribute_target_auto = new string[] { "property", "field" };
758
759                 Field backing_field;
760
761                 public Property (TypeDefinition parent, FullNamedExpression type, Modifiers mod,
762                                  MemberName name, Attributes attrs)
763                         : base (parent, type, mod,
764                                 parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
765                                 parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct :
766                                 AllowedModifiersClass,
767                                 name, attrs)
768                 {
769                 }
770
771                 public Expression Initializer { get; set; }
772
773                 public override void Accept (StructuralVisitor visitor)
774                 {
775                         visitor.Visit (this);
776                 }
777
778                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
779                 {
780                         if (a.Target == AttributeTargets.Field) {
781                                 backing_field.ApplyAttributeBuilder (a, ctor, cdata, pa);
782                                 return;
783                         }
784
785                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
786                 }
787
788                 void CreateAutomaticProperty ()
789                 {
790                         // Create backing field
791                         backing_field = new BackingField (this, Initializer != null && Set == null);
792                         if (!backing_field.Define ())
793                                 return;
794
795                         if (Initializer != null) {
796                                 backing_field.Initializer = Initializer;
797                                 Parent.RegisterFieldForInitialization (backing_field, new FieldInitializer (backing_field, Initializer, Location));
798                                 backing_field.ModFlags |= Modifiers.READONLY;
799                         }
800
801                         Parent.PartialContainer.Members.Add (backing_field);
802
803                         FieldExpr fe = new FieldExpr (backing_field, Location);
804                         if ((backing_field.ModFlags & Modifiers.STATIC) == 0)
805                                 fe.InstanceExpression = new CompilerGeneratedThis (Parent.CurrentType, Location);
806
807                         //
808                         // Create get block but we careful with location to
809                         // emit only single sequence point per accessor. This allow
810                         // to set a breakpoint on it even with no user code
811                         //
812                         Get.Block = new ToplevelBlock (Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location.Null);
813                         Return r = new Return (fe, Get.Location);
814                         Get.Block.AddStatement (r);
815                         Get.ModFlags |= Modifiers.COMPILER_GENERATED;
816
817                         // Create set block
818                         if (Set != null) {
819                                 Set.Block = new ToplevelBlock (Compiler, Set.ParameterInfo, Location.Null);
820                                 Assign a = new SimpleAssign (fe, new SimpleName ("value", Location.Null), Location.Null);
821                                 Set.Block.AddStatement (new StatementExpression (a, Set.Location));
822                                 Set.ModFlags |= Modifiers.COMPILER_GENERATED;
823                         }
824                 }
825
826                 public override bool Define ()
827                 {
828                         if (!base.Define ())
829                                 return false;
830
831                         flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
832
833                         bool auto = AccessorFirst.Block == null && (AccessorSecond == null || AccessorSecond.Block == null) &&
834                                 (ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0;
835
836                         if (Initializer != null) {
837                                 if (!auto)
838                                         Report.Error (8050, Location, "`{0}': Only auto-implemented properties can have initializers",
839                                                 GetSignatureForError ());
840
841                                 if (IsInterface)
842                                         Report.Error (8053, Location, "`{0}': Properties inside interfaces cannot have initializers",
843                                                 GetSignatureForError ());
844
845                                 if (Compiler.Settings.Version < LanguageVersion.V_6)
846                                         Report.FeatureIsNotAvailable (Compiler, Location, "auto-implemented property initializer");
847                         }
848
849                         if (auto) {
850                                 if (Get == null) {
851                                         Report.Error (8052, Location, "Auto-implemented property `{0}' must have get accessor",
852                                                 GetSignatureForError ());
853                                         return false;
854                                 }
855
856                                 if (Initializer == null && AccessorSecond == null) {
857                                         Report.Error (8051, Location, "Auto-implemented property `{0}' must have set accessor or initializer",
858                                                 GetSignatureForError ());
859                                 }
860
861                                 if (Compiler.Settings.Version < LanguageVersion.V_3 && Initializer == null)
862                                         Report.FeatureIsNotAvailable (Compiler, Location, "auto-implemented properties");
863
864                                 CreateAutomaticProperty ();
865                         }
866
867                         if (!DefineAccessors ())
868                                 return false;
869
870                         if (AccessorSecond == null) {
871                                 PropertyMethod pm;
872                                 if (AccessorFirst is GetMethod)
873                                         pm = new SetMethod (this, 0, ParametersCompiled.EmptyReadOnlyParameters, null, Location);
874                                 else
875                                         pm = new GetMethod (this, 0, null, Location);
876
877                                 Parent.AddNameToContainer (pm, pm.MemberName.Basename);
878                         }
879
880                         if (!CheckBase ())
881                                 return false;
882
883                         DefineBuilders (MemberKind.Property, ParametersCompiled.EmptyReadOnlyParameters);
884                         return true;
885                 }
886
887                 public override void Emit ()
888                 {
889                         if ((AccessorFirst.ModFlags & (Modifiers.STATIC | Modifiers.COMPILER_GENERATED)) == Modifiers.COMPILER_GENERATED && Parent.PartialContainer.HasExplicitLayout) {
890                                 Report.Error (842, Location,
891                                         "Automatically implemented property `{0}' cannot be used inside a type with an explicit StructLayout attribute",
892                                         GetSignatureForError ());
893                         }
894
895                         base.Emit ();
896                 }
897
898                 public override string[] ValidAttributeTargets {
899                         get {
900                                 return Get != null && ((Get.ModFlags & Modifiers.COMPILER_GENERATED) != 0) ?
901                                         attribute_target_auto : base.ValidAttributeTargets;
902                         }
903                 }
904         }
905
906         /// <summary>
907         /// For case when event is declared like property (with add and remove accessors).
908         /// </summary>
909         public class EventProperty: Event {
910                 public abstract class AEventPropertyAccessor : AEventAccessor
911                 {
912                         protected AEventPropertyAccessor (EventProperty method, string prefix, Attributes attrs, Location loc)
913                                 : base (method, prefix, attrs, loc)
914                         {
915                         }
916
917                         public override void Define (TypeContainer ds)
918                         {
919                                 CheckAbstractAndExtern (block != null);
920                                 base.Define (ds);
921                         }
922                         
923                         public override string GetSignatureForError ()
924                         {
925                                 return method.GetSignatureForError () + "." + prefix.Substring (0, prefix.Length - 1);
926                         }
927                 }
928
929                 public sealed class AddDelegateMethod: AEventPropertyAccessor
930                 {
931                         public AddDelegateMethod (EventProperty method, Attributes attrs, Location loc)
932                                 : base (method, AddPrefix, attrs, loc)
933                         {
934                         }
935                 }
936
937                 public sealed class RemoveDelegateMethod: AEventPropertyAccessor
938                 {
939                         public RemoveDelegateMethod (EventProperty method, Attributes attrs, Location loc)
940                                 : base (method, RemovePrefix, attrs, loc)
941                         {
942                         }
943                 }
944
945                 static readonly string[] attribute_targets = new string [] { "event" };
946
947                 public EventProperty (TypeDefinition parent, FullNamedExpression type, Modifiers mod_flags, MemberName name, Attributes attrs)
948                         : base (parent, type, mod_flags, name, attrs)
949                 {
950                 }
951
952                 public override void Accept (StructuralVisitor visitor)
953                 {
954                         visitor.Visit (this);
955                 }
956                 
957                 public override bool Define()
958                 {
959                         if (!base.Define ())
960                                 return false;
961
962                         SetIsUsed ();
963                         return true;
964                 }
965
966                 public override string[] ValidAttributeTargets {
967                         get {
968                                 return attribute_targets;
969                         }
970                 }
971         }
972
973         /// <summary>
974         /// Event is declared like field.
975         /// </summary>
976         public class EventField : Event
977         {
978                 abstract class EventFieldAccessor : AEventAccessor
979                 {
980                         protected EventFieldAccessor (EventField method, string prefix)
981                                 : base (method, prefix, null, method.Location)
982                         {
983                         }
984
985                         protected abstract MethodSpec GetOperation (Location loc);
986
987                         public override void Emit (TypeDefinition parent)
988                         {
989                                 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0 && !Compiler.Settings.WriteMetadataOnly) {
990                                         block = new ToplevelBlock (Compiler, ParameterInfo, Location) {
991                                                 IsCompilerGenerated = true
992                                         };
993                                         FabricateBodyStatement ();
994                                 }
995
996                                 base.Emit (parent);
997                         }
998
999                         void FabricateBodyStatement ()
1000                         {
1001                                 //
1002                                 // Delegate obj1 = backing_field
1003                                 // do {
1004                                 //   Delegate obj2 = obj1;
1005                                 //   obj1 = Interlocked.CompareExchange (ref backing_field, Delegate.Combine|Remove(obj2, value), obj1);
1006                                 // } while ((object)obj1 != (object)obj2)
1007                                 //
1008
1009                                 var field_info = ((EventField) method).backing_field;
1010                                 FieldExpr f_expr = new FieldExpr (field_info, Location);
1011                                 if (!IsStatic)
1012                                         f_expr.InstanceExpression = new CompilerGeneratedThis (Parent.CurrentType, Location);
1013
1014                                 var obj1 = LocalVariable.CreateCompilerGenerated (field_info.MemberType, block, Location);
1015                                 var obj2 = LocalVariable.CreateCompilerGenerated (field_info.MemberType, block, Location);
1016
1017                                 block.AddStatement (new StatementExpression (new SimpleAssign (new LocalVariableReference (obj1, Location), f_expr)));
1018
1019                                 var cond = new BooleanExpression (new Binary (Binary.Operator.Inequality,
1020                                         new Cast (new TypeExpression (Module.Compiler.BuiltinTypes.Object, Location), new LocalVariableReference (obj1, Location), Location),
1021                                         new Cast (new TypeExpression (Module.Compiler.BuiltinTypes.Object, Location), new LocalVariableReference (obj2, Location), Location)));
1022
1023                                 var body = new ExplicitBlock (block, Location, Location);
1024                                 block.AddStatement (new Do (body, cond, Location, Location));
1025
1026                                 body.AddStatement (new StatementExpression (
1027                                         new SimpleAssign (new LocalVariableReference (obj2, Location), new LocalVariableReference (obj1, Location))));
1028
1029                                 var args_oper = new Arguments (2);
1030                                 args_oper.Add (new Argument (new LocalVariableReference (obj2, Location)));
1031                                 args_oper.Add (new Argument (block.GetParameterReference (0, Location)));
1032
1033                                 var op_method = GetOperation (Location);
1034
1035                                 var args = new Arguments (3);
1036                                 args.Add (new Argument (f_expr, Argument.AType.Ref));
1037                                 args.Add (new Argument (new Cast (
1038                                         new TypeExpression (field_info.MemberType, Location),
1039                                         new Invocation (MethodGroupExpr.CreatePredefined (op_method, op_method.DeclaringType, Location), args_oper),
1040                                         Location)));
1041                                 args.Add (new Argument (new LocalVariableReference (obj1, Location)));
1042
1043                                 var cas = Module.PredefinedMembers.InterlockedCompareExchange_T.Get ();
1044                                 if (cas == null) {
1045                                         // Workaround for cripled (e.g. microframework) mscorlib without CompareExchange
1046                                         body.AddStatement (new Lock (
1047                                                 block.GetParameterReference (0, Location),
1048                                                 new StatementExpression (new SimpleAssign (
1049                                                         f_expr, args [1].Expr, Location), Location), Location));
1050                                 } else {
1051                                         body.AddStatement (new StatementExpression (new SimpleAssign (
1052                                                 new LocalVariableReference (obj1, Location),
1053                                                 new Invocation (MethodGroupExpr.CreatePredefined (cas, cas.DeclaringType, Location), args))));
1054                                 }
1055                         }
1056                 }
1057
1058                 sealed class AddDelegateMethod: EventFieldAccessor
1059                 {
1060                         public AddDelegateMethod (EventField method):
1061                                 base (method, AddPrefix)
1062                         {
1063                         }
1064
1065                         protected override MethodSpec GetOperation (Location loc)
1066                         {
1067                                 return Module.PredefinedMembers.DelegateCombine.Resolve (loc);
1068                         }
1069                 }
1070
1071                 sealed class RemoveDelegateMethod: EventFieldAccessor
1072                 {
1073                         public RemoveDelegateMethod (EventField method):
1074                                 base (method, RemovePrefix)
1075                         {
1076                         }
1077
1078                         protected override MethodSpec GetOperation (Location loc)
1079                         {
1080                                 return Module.PredefinedMembers.DelegateRemove.Resolve (loc);
1081                         }
1082                 }
1083
1084
1085                 static readonly string[] attribute_targets = new string [] { "event", "field", "method" };
1086                 static readonly string[] attribute_targets_interface = new string[] { "event", "method" };
1087
1088                 Expression initializer;
1089                 Field backing_field;
1090                 List<FieldDeclarator> declarators;
1091
1092                 public EventField (TypeDefinition parent, FullNamedExpression type, Modifiers mod_flags, MemberName name, Attributes attrs)
1093                         : base (parent, type, mod_flags, name, attrs)
1094                 {
1095                         Add = new AddDelegateMethod (this);
1096                         Remove = new RemoveDelegateMethod (this);
1097                 }
1098
1099                 #region Properties
1100
1101                 public List<FieldDeclarator> Declarators {
1102                         get {
1103                                 return this.declarators;
1104                         }
1105                 }
1106
1107                 bool HasBackingField {
1108                         get {
1109                                 return !IsInterface && (ModFlags & Modifiers.ABSTRACT) == 0;
1110                         }
1111                 }
1112
1113                 public Expression Initializer {
1114                         get {
1115                                 return initializer;
1116                         }
1117                         set {
1118                                 initializer = value;
1119                         }
1120                 }
1121
1122                 public override string[] ValidAttributeTargets {
1123                         get {
1124                                 return HasBackingField ? attribute_targets : attribute_targets_interface;
1125                         }
1126                 }
1127
1128                 #endregion
1129
1130                 
1131                 public override void Accept (StructuralVisitor visitor)
1132                 {
1133                         visitor.Visit (this);
1134                 }
1135
1136                 public void AddDeclarator (FieldDeclarator declarator)
1137                 {
1138                         if (declarators == null)
1139                                 declarators = new List<FieldDeclarator> (2);
1140
1141                         declarators.Add (declarator);
1142
1143                         Parent.AddNameToContainer (this, declarator.Name.Value);
1144                 }
1145
1146                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1147                 {
1148                         if (a.Target == AttributeTargets.Field) {
1149                                 backing_field.ApplyAttributeBuilder (a, ctor, cdata, pa);
1150                                 return;
1151                         }
1152
1153                         if (a.Target == AttributeTargets.Method) {
1154                                 int errors = Report.Errors;
1155                                 Add.ApplyAttributeBuilder (a, ctor, cdata, pa);
1156                                 if (errors == Report.Errors)
1157                                         Remove.ApplyAttributeBuilder (a, ctor, cdata, pa);
1158                                 return;
1159                         }
1160
1161                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
1162                 }
1163
1164                 public override bool Define()
1165                 {
1166                         var mod_flags_src = ModFlags;
1167
1168                         if (!base.Define ())
1169                                 return false;
1170
1171                         if (declarators != null) {
1172                                 if ((mod_flags_src & Modifiers.DEFAULT_ACCESS_MODIFIER) != 0)
1173                                         mod_flags_src &= ~(Modifiers.AccessibilityMask | Modifiers.DEFAULT_ACCESS_MODIFIER);
1174
1175                                 var t = new TypeExpression (MemberType, TypeExpression.Location);
1176                                 foreach (var d in declarators) {
1177                                         var ef = new EventField (Parent, t, mod_flags_src, new MemberName (d.Name.Value, d.Name.Location), OptAttributes);
1178
1179                                         if (d.Initializer != null)
1180                                                 ef.initializer = d.Initializer;
1181
1182                                         ef.Define ();
1183                                         Parent.PartialContainer.Members.Add (ef);
1184                                 }
1185                         }
1186
1187                         if (!HasBackingField) {
1188                                 SetIsUsed ();
1189                                 return true;
1190                         }
1191
1192                         backing_field = new Field (Parent,
1193                                 new TypeExpression (MemberType, Location),
1194                                 Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
1195                                 MemberName, null);
1196
1197                         Parent.PartialContainer.Members.Add (backing_field);
1198                         backing_field.Initializer = Initializer;
1199                         backing_field.ModFlags &= ~Modifiers.COMPILER_GENERATED;
1200
1201                         // Call define because we passed fields definition
1202                         backing_field.Define ();
1203
1204                         // Set backing field for event fields
1205                         spec.BackingField = backing_field.Spec;
1206
1207                         return true;
1208                 }
1209         }
1210
1211         public abstract class Event : PropertyBasedMember
1212         {
1213                 public abstract class AEventAccessor : AbstractPropertyEventMethod
1214                 {
1215                         protected readonly Event method;
1216                         readonly ParametersCompiled parameters;
1217
1218                         static readonly string[] attribute_targets = new string [] { "method", "param", "return" };
1219
1220                         public const string AddPrefix = "add_";
1221                         public const string RemovePrefix = "remove_";
1222
1223                         protected AEventAccessor (Event method, string prefix, Attributes attrs, Location loc)
1224                                 : base (method, prefix, attrs, loc)
1225                         {
1226                                 this.method = method;
1227                                 this.ModFlags = method.ModFlags;
1228                                 this.parameters = ParametersCompiled.CreateImplicitParameter (method.TypeExpression, loc);
1229                         }
1230
1231                         public bool IsInterfaceImplementation {
1232                                 get { return method_data.implementing != null; }
1233                         }
1234
1235                         public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1236                         {
1237                                 if (a.Type == pa.MethodImpl) {
1238                                         method.is_external_implementation = a.IsInternalCall ();
1239                                 }
1240
1241                                 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
1242                         }
1243
1244                         protected override void ApplyToExtraTarget (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1245                         {
1246                                 if (a.Target == AttributeTargets.Parameter) {
1247                                         parameters[0].ApplyAttributeBuilder (a, ctor, cdata, pa);
1248                                         return;
1249                                 }
1250
1251                                 base.ApplyToExtraTarget (a, ctor, cdata, pa);
1252                         }
1253
1254                         public override AttributeTargets AttributeTargets {
1255                                 get {
1256                                         return AttributeTargets.Method;
1257                                 }
1258                         }
1259
1260                         public override bool IsClsComplianceRequired ()
1261                         {
1262                                 return method.IsClsComplianceRequired ();
1263                         }
1264
1265                         public virtual void Define (TypeContainer parent)
1266                         {
1267                                 // Fill in already resolved event type to speed things up and
1268                                 // avoid confusing duplicate errors
1269                                 ((Parameter) parameters.FixedParameters[0]).Type = method.member_type;
1270                                 parameters.Types = new TypeSpec[] { method.member_type };
1271
1272                                 method_data = new MethodData (method, method.ModFlags,
1273                                         method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
1274
1275                                 if (!method_data.Define (parent.PartialContainer, method.GetFullName (MemberName)))
1276                                         return;
1277
1278                                 if (Compiler.Settings.WriteMetadataOnly)
1279                                         block = null;
1280
1281                                 Spec = new MethodSpec (MemberKind.Method, parent.PartialContainer.Definition, this, ReturnType, ParameterInfo, method.ModFlags);
1282                                 Spec.IsAccessor = true;
1283                         }
1284
1285                         public override TypeSpec ReturnType {
1286                                 get {
1287                                         return Parent.Compiler.BuiltinTypes.Void;
1288                                 }
1289                         }
1290
1291                         public override ObsoleteAttribute GetAttributeObsolete ()
1292                         {
1293                                 return method.GetAttributeObsolete ();
1294                         }
1295
1296                         public MethodData MethodData {
1297                                 get {
1298                                         return method_data;
1299                                 }
1300                         }
1301
1302                         public override string[] ValidAttributeTargets {
1303                                 get {
1304                                         return attribute_targets;
1305                                 }
1306                         }
1307
1308                         public override ParametersCompiled ParameterInfo {
1309                                 get {
1310                                         return parameters;
1311                                 }
1312                         }
1313                 }
1314
1315                 AEventAccessor add, remove;
1316                 EventBuilder EventBuilder;
1317                 protected EventSpec spec;
1318
1319                 protected Event (TypeDefinition parent, FullNamedExpression type, Modifiers mod_flags, MemberName name, Attributes attrs)
1320                         : base (parent, type, mod_flags,
1321                                 parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
1322                                 parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct :
1323                                 AllowedModifiersClass,
1324                                 name, attrs)
1325                 {
1326                 }
1327
1328                 #region Properties
1329
1330                 public override AttributeTargets AttributeTargets {
1331                         get {
1332                                 return AttributeTargets.Event;
1333                         }
1334                 }
1335
1336                 public AEventAccessor Add {
1337                         get {
1338                                 return this.add;
1339                         }
1340                         set {
1341                                 add = value;
1342                                 Parent.AddNameToContainer (value, value.MemberName.Basename);
1343                         }
1344                 }
1345
1346                 public override Variance ExpectedMemberTypeVariance {
1347                         get {
1348                                 return Variance.Contravariant;
1349                         }
1350                 }
1351
1352                 public AEventAccessor Remove {
1353                         get {
1354                                 return this.remove;
1355                         }
1356                         set {
1357                                 remove = value;
1358                                 Parent.AddNameToContainer (value, value.MemberName.Basename);
1359                         }
1360                 }
1361                 #endregion
1362
1363                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1364                 {
1365                         if ((a.HasSecurityAttribute)) {
1366                                 a.Error_InvalidSecurityParent ();
1367                                 return;
1368                         }
1369
1370                         EventBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
1371                 }
1372
1373                 protected override bool CheckOverrideAgainstBase (MemberSpec base_member)
1374                 {
1375                         var ok = base.CheckOverrideAgainstBase (base_member);
1376
1377                         if (!CheckAccessModifiers (this, base_member)) {
1378                                 Error_CannotChangeAccessModifiers (this, base_member);
1379                                 ok = false;
1380                         }
1381
1382                         return ok;
1383                 }
1384
1385                 public override bool Define ()
1386                 {
1387                         if (!base.Define ())
1388                                 return false;
1389
1390                         if (!MemberType.IsDelegate) {
1391                                 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
1392                         }
1393
1394                         if (!CheckBase ())
1395                                 return false;
1396
1397                         //
1398                         // Now define the accessors
1399                         //
1400                         add.Define (Parent);
1401                         remove.Define (Parent);
1402
1403                         EventBuilder = Parent.TypeBuilder.DefineEvent (GetFullName (MemberName), EventAttributes.None, MemberType.GetMetaInfo ());
1404
1405                         spec = new EventSpec (Parent.Definition, this, MemberType, ModFlags, Add.Spec, remove.Spec);
1406
1407                         Parent.MemberCache.AddMember (this, GetFullName (MemberName), spec);
1408                         Parent.MemberCache.AddMember (this, Add.Spec.Name, Add.Spec);
1409                         Parent.MemberCache.AddMember (this, Remove.Spec.Name, remove.Spec);
1410
1411                         return true;
1412                 }
1413
1414                 public override void Emit ()
1415                 {
1416                         CheckReservedNameConflict (null, add.Spec);
1417                         CheckReservedNameConflict (null, remove.Spec);
1418
1419                         if (OptAttributes != null) {
1420                                 OptAttributes.Emit ();
1421                         }
1422
1423                         ConstraintChecker.Check (this, member_type, type_expr.Location);
1424
1425                         Add.Emit (Parent);
1426                         Remove.Emit (Parent);
1427
1428                         base.Emit ();
1429                 }
1430
1431                 public override void PrepareEmit ()
1432                 {
1433                         add.PrepareEmit ();
1434                         remove.PrepareEmit ();
1435
1436                         EventBuilder.SetAddOnMethod (add.MethodData.MethodBuilder);
1437                         EventBuilder.SetRemoveOnMethod (remove.MethodData.MethodBuilder);
1438                 }
1439
1440                 public override void WriteDebugSymbol (MonoSymbolFile file)
1441                 {
1442                         add.WriteDebugSymbol (file);
1443                         remove.WriteDebugSymbol (file);
1444                 }
1445
1446                 //
1447                 //   Represents header string for documentation comment.
1448                 //
1449                 public override string DocCommentHeader {
1450                         get { return "E:"; }
1451                 }
1452         }
1453
1454         public class EventSpec : MemberSpec, IInterfaceMemberSpec
1455         {
1456                 MethodSpec add, remove;
1457                 FieldSpec backing_field;
1458
1459                 public EventSpec (TypeSpec declaringType, IMemberDefinition definition, TypeSpec eventType, Modifiers modifiers, MethodSpec add, MethodSpec remove)
1460                         : base (MemberKind.Event, declaringType, definition, modifiers)
1461                 {
1462                         this.AccessorAdd = add;
1463                         this.AccessorRemove = remove;
1464                         this.MemberType = eventType;
1465                 }
1466
1467                 #region Properties
1468
1469                 public MethodSpec AccessorAdd { 
1470                         get {
1471                                 return add;
1472                         }
1473                         set {
1474                                 add = value;
1475                         }
1476                 }
1477
1478                 public MethodSpec AccessorRemove {
1479                         get {
1480                                 return remove;
1481                         }
1482                         set {
1483                                 remove = value;
1484                         }
1485                 }
1486
1487                 public FieldSpec BackingField {
1488                         get {
1489                                 return backing_field;
1490                         }
1491                         set {
1492                                 backing_field = value;
1493                         }
1494                 }
1495
1496                 public TypeSpec MemberType { get; private set; }
1497
1498                 #endregion
1499
1500                 public override MemberSpec InflateMember (TypeParameterInflator inflator)
1501                 {
1502                         var es = (EventSpec) base.InflateMember (inflator);
1503                         es.MemberType = inflator.Inflate (MemberType);
1504
1505                         if (backing_field != null)
1506                                 es.backing_field = (FieldSpec) backing_field.InflateMember (inflator);
1507
1508                         return es;
1509                 }
1510
1511                 public override List<MissingTypeSpecReference> ResolveMissingDependencies (MemberSpec caller)
1512                 {
1513                         return MemberType.ResolveMissingDependencies (this);
1514                 }
1515         }
1516  
1517         public class Indexer : PropertyBase, IParametersMember
1518         {
1519                 public class GetIndexerMethod : GetMethod, IParametersMember
1520                 {
1521                         ParametersCompiled parameters;
1522
1523                         public GetIndexerMethod (PropertyBase property, Modifiers modifiers, ParametersCompiled parameters, Attributes attrs, Location loc)
1524                                 : base (property, modifiers, attrs, loc)
1525                         {
1526                                 this.parameters = parameters;
1527                         }
1528
1529                         public override void Define (TypeContainer parent)
1530                         {
1531                                 // Disable reporting, parameters are resolved twice
1532                                 Report.DisableReporting ();
1533                                 try {
1534                                         parameters.Resolve (this);
1535                                 } finally {
1536                                         Report.EnableReporting ();
1537                                 }
1538
1539                                 base.Define (parent);
1540                         }
1541
1542                         public override ParametersCompiled ParameterInfo {
1543                                 get {
1544                                         return parameters;
1545                                 }
1546                         }
1547
1548                         #region IParametersMember Members
1549
1550                         AParametersCollection IParametersMember.Parameters {
1551                                 get {
1552                                         return parameters;
1553                                 }
1554                         }
1555
1556                         TypeSpec IInterfaceMemberSpec.MemberType {
1557                                 get {
1558                                         return ReturnType;
1559                                 }
1560                         }
1561
1562                         #endregion
1563                 }
1564
1565                 public class SetIndexerMethod : SetMethod, IParametersMember
1566                 {
1567                         public SetIndexerMethod (PropertyBase property, Modifiers modifiers, ParametersCompiled parameters, Attributes attrs, Location loc)
1568                                 : base (property, modifiers, parameters, attrs, loc)
1569                         {
1570                         }
1571
1572                         #region IParametersMember Members
1573
1574                         AParametersCollection IParametersMember.Parameters {
1575                                 get {
1576                                         return parameters;
1577                                 }
1578                         }
1579
1580                         TypeSpec IInterfaceMemberSpec.MemberType {
1581                                 get {
1582                                         return ReturnType;
1583                                 }
1584                         }
1585
1586                         #endregion
1587                 }
1588
1589                 const Modifiers AllowedModifiers =
1590                         Modifiers.NEW |
1591                         Modifiers.PUBLIC |
1592                         Modifiers.PROTECTED |
1593                         Modifiers.INTERNAL |
1594                         Modifiers.PRIVATE |
1595                         Modifiers.VIRTUAL |
1596                         Modifiers.SEALED |
1597                         Modifiers.OVERRIDE |
1598                         Modifiers.UNSAFE |
1599                         Modifiers.EXTERN |
1600                         Modifiers.ABSTRACT;
1601
1602                 const Modifiers AllowedInterfaceModifiers =
1603                         Modifiers.NEW;
1604
1605                 readonly ParametersCompiled parameters;
1606
1607                 public Indexer (TypeDefinition parent, FullNamedExpression type, MemberName name, Modifiers mod, ParametersCompiled parameters, Attributes attrs)
1608                         : base (parent, type, mod,
1609                                 parent.PartialContainer.Kind == MemberKind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
1610                                 name, attrs)
1611                 {
1612                         this.parameters = parameters;
1613                 }
1614
1615                 #region Properties
1616
1617                 AParametersCollection IParametersMember.Parameters {
1618                         get {
1619                                 return parameters;
1620                         }
1621                 }
1622
1623                 public ParametersCompiled ParameterInfo {
1624                         get {
1625                                 return parameters;
1626                         }
1627                 }
1628
1629                 #endregion
1630
1631                 
1632                 public override void Accept (StructuralVisitor visitor)
1633                 {
1634                         visitor.Visit (this);
1635                 }
1636
1637                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1638                 {
1639                         if (a.Type == pa.IndexerName) {
1640                                 // Attribute was copied to container
1641                                 return;
1642                         }
1643
1644                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
1645                 }
1646
1647                 protected override bool CheckForDuplications ()
1648                 {
1649                         return Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1650                 }
1651                 
1652                 public override bool Define ()
1653                 {
1654                         if (!base.Define ())
1655                                 return false;
1656
1657                         if (!DefineParameters (parameters))
1658                                 return false;
1659
1660                         if (OptAttributes != null) {
1661                                 Attribute indexer_attr = OptAttributes.Search (Module.PredefinedAttributes.IndexerName);
1662                                 if (indexer_attr != null) {
1663                                         var compiling = indexer_attr.Type.MemberDefinition as TypeContainer;
1664                                         if (compiling != null)
1665                                                 compiling.Define ();
1666
1667                                         if (IsExplicitImpl) {
1668                                                 Report.Error (415, indexer_attr.Location,
1669                                                         "The `{0}' attribute is valid only on an indexer that is not an explicit interface member declaration",
1670                                                         indexer_attr.Type.GetSignatureForError ());
1671                                         } else if ((ModFlags & Modifiers.OVERRIDE) != 0) {
1672                                                 Report.Error (609, indexer_attr.Location,
1673                                                         "Cannot set the `IndexerName' attribute on an indexer marked override");
1674                                         } else {
1675                                                 string name = indexer_attr.GetIndexerAttributeValue ();
1676
1677                                                 if (!string.IsNullOrEmpty (name)) {
1678                                                         SetMemberName (new MemberName (MemberName.Left, name, Location));
1679                                                 }
1680                                         }
1681                                 }
1682                         }
1683
1684                         if (InterfaceType != null) {
1685                                 string base_IndexerName = InterfaceType.MemberDefinition.GetAttributeDefaultMember ();
1686                                 if (base_IndexerName != ShortName) {
1687                                         SetMemberName (new MemberName (MemberName.Left, base_IndexerName, new TypeExpression (InterfaceType, Location), Location));
1688                                 }
1689                         }
1690
1691                         Parent.AddNameToContainer (this, MemberName.Basename);
1692
1693                         flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
1694                         
1695                         if (!DefineAccessors ())
1696                                 return false;
1697
1698                         if (!CheckBase ())
1699                                 return false;
1700
1701                         DefineBuilders (MemberKind.Indexer, parameters);
1702                         return true;
1703                 }
1704
1705                 public override bool EnableOverloadChecks (MemberCore overload)
1706                 {
1707                         if (overload is Indexer) {
1708                                 caching_flags |= Flags.MethodOverloadsExist;
1709                                 return true;
1710                         }
1711
1712                         return base.EnableOverloadChecks (overload);
1713                 }
1714
1715                 public override void Emit ()
1716                 {
1717                         parameters.CheckConstraints (this);
1718
1719                         base.Emit ();
1720                 }
1721
1722                 public override string GetSignatureForError ()
1723                 {
1724                         StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
1725                         if (MemberName.ExplicitInterface != null) {
1726                                 sb.Append (".");
1727                                 sb.Append (MemberName.ExplicitInterface.GetSignatureForError ());
1728                         }
1729
1730                         sb.Append (".this");
1731                         sb.Append (parameters.GetSignatureForError ("[", "]", parameters.Count));
1732                         return sb.ToString ();
1733                 }
1734
1735                 public override string GetSignatureForDocumentation ()
1736                 {
1737                         return base.GetSignatureForDocumentation () + parameters.GetSignatureForDocumentation ();
1738                 }
1739
1740                 protected override bool VerifyClsCompliance ()
1741                 {
1742                         if (!base.VerifyClsCompliance ())
1743                                 return false;
1744
1745                         parameters.VerifyClsCompliance (this);
1746                         return true;
1747                 }
1748         }
1749
1750         public class IndexerSpec : PropertySpec, IParametersMember
1751         {
1752                 AParametersCollection parameters;
1753
1754                 public IndexerSpec (TypeSpec declaringType, IMemberDefinition definition, TypeSpec memberType, AParametersCollection parameters, PropertyInfo info, Modifiers modifiers)
1755                         : base (MemberKind.Indexer, declaringType, definition, memberType, info, modifiers)
1756                 {
1757                         this.parameters = parameters;
1758                 }
1759
1760                 #region Properties
1761                 public AParametersCollection Parameters {
1762                         get {
1763                                 return parameters;
1764                         }
1765                 }
1766                 #endregion
1767
1768                 public override string GetSignatureForDocumentation ()
1769                 {
1770                         return base.GetSignatureForDocumentation () + parameters.GetSignatureForDocumentation ();
1771                 }
1772
1773                 public override string GetSignatureForError ()
1774                 {
1775                         return DeclaringType.GetSignatureForError () + ".this" + parameters.GetSignatureForError ("[", "]", parameters.Count);
1776                 }
1777
1778                 public override MemberSpec InflateMember (TypeParameterInflator inflator)
1779                 {
1780                         var spec = (IndexerSpec) base.InflateMember (inflator);
1781                         spec.parameters = parameters.Inflate (inflator);
1782                         return spec;
1783                 }
1784
1785                 public override List<MissingTypeSpecReference> ResolveMissingDependencies (MemberSpec caller)
1786                 {
1787                         var missing = base.ResolveMissingDependencies (caller);
1788
1789                         foreach (var pt in parameters.Types) {
1790                                 var m = pt.GetMissingDependencies (caller);
1791                                 if (m == null)
1792                                         continue;
1793
1794                                 if (missing == null)
1795                                         missing = new List<MissingTypeSpecReference> ();
1796
1797                                 missing.AddRange (m);
1798                         }
1799
1800                         return missing;
1801                 }
1802         }
1803 }