2f2fe4580b400dfc74c80bd59366ceeeada97f71
[mono.git] / mcs / mcs / class.cs
1 //
2 // class.cs: Class and Struct handlers
3 //
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 //          Martin Baulig (martin@ximian.com)
6 //          Marek Safar (marek.safar@gmail.com)
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-2011 Novell, Inc
12 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
13 //
14
15 using System;
16 using System.Linq;
17 using System.Collections.Generic;
18 using System.Runtime.InteropServices;
19 using System.Security;
20 using System.Security.Permissions;
21 using System.Text;
22 using System.Diagnostics;
23 using Mono.CompilerServices.SymbolWriter;
24
25 #if MOBILE
26 using XmlElement = System.Object;
27 #endif
28
29 #if STATIC
30 using SecurityType = System.Collections.Generic.List<IKVM.Reflection.Emit.CustomAttributeBuilder>;
31 using IKVM.Reflection;
32 using IKVM.Reflection.Emit;
33 #else
34 using SecurityType = System.Collections.Generic.Dictionary<System.Security.Permissions.SecurityAction, System.Security.PermissionSet>;
35 using System.Reflection;
36 using System.Reflection.Emit;
37 #endif
38
39 namespace Mono.CSharp
40 {
41         //
42         // General types container, used as a base class for all constructs which can hold types
43         //
44         public abstract class TypeContainer : MemberCore
45         {
46                 public readonly MemberKind Kind;
47
48                 protected List<TypeContainer> containers;
49
50                 TypeDefinition main_container;
51
52                 protected Dictionary<string, MemberCore> defined_names;
53
54                 protected bool is_defined;
55
56                 public int CounterAnonymousMethods { get; set; }
57                 public int CounterAnonymousContainers { get; set; }
58                 public int CounterSwitchTypes { get; set; }
59
60                 protected TypeContainer (TypeContainer parent, MemberName name, Attributes attrs, MemberKind kind)
61                         : base (parent, name, attrs)
62                 {
63                         this.Kind = kind;
64                         defined_names = new Dictionary<string, MemberCore> ();
65                 }
66
67                 public override TypeSpec CurrentType {
68                         get {
69                                 return null;
70                         }
71                 }
72
73                 public Dictionary<string, MemberCore> DefinedNames {
74                         get {
75                                 return defined_names;
76                         }
77                 }
78
79                 public TypeDefinition PartialContainer {
80                         get {
81                                 return main_container;
82                         }
83                         protected set {
84                                 main_container = value;
85                         }
86                 }
87
88                 public IList<TypeContainer> Containers {
89                         get {
90                                 return containers;
91                         }
92                 }
93
94                 //
95                 // Any unattached attributes during parsing get added here. Used
96                 // by FULL_AST mode
97                 //
98                 public Attributes UnattachedAttributes {
99                         get; set;
100                 }
101
102                 public void AddCompilerGeneratedClass (CompilerGeneratedContainer c)
103                 {
104                         AddTypeContainerMember (c);
105                 }
106
107                 public virtual void AddPartial (TypeDefinition next_part)
108                 {
109                         MemberCore mc;
110                         (PartialContainer ?? this).defined_names.TryGetValue (next_part.MemberName.Basename, out mc);
111
112                         AddPartial (next_part, mc as TypeDefinition);
113                 }
114
115                 protected void AddPartial (TypeDefinition next_part, TypeDefinition existing)
116                 {
117                         next_part.ModFlags |= Modifiers.PARTIAL;
118
119                         if (existing == null) {
120                                 AddTypeContainer (next_part);
121                                 return;
122                         }
123
124                         if ((existing.ModFlags & Modifiers.PARTIAL) == 0) {
125                                 if (existing.Kind != next_part.Kind) {
126                                         AddTypeContainer (next_part);
127                                 } else {
128                                         Report.SymbolRelatedToPreviousError (next_part);
129                                         Error_MissingPartialModifier (existing);
130                                 }
131
132                                 return;
133                         }
134
135                         if (existing.Kind != next_part.Kind) {
136                                 Report.SymbolRelatedToPreviousError (existing);
137                                 Report.Error (261, next_part.Location,
138                                         "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
139                                         next_part.GetSignatureForError ());
140                         }
141
142                         if ((existing.ModFlags & Modifiers.AccessibilityMask) != (next_part.ModFlags & Modifiers.AccessibilityMask) &&
143                                 ((existing.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFIER) == 0 &&
144                                  (next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFIER) == 0)) {
145                                          Report.SymbolRelatedToPreviousError (existing);
146                                 Report.Error (262, next_part.Location,
147                                         "Partial declarations of `{0}' have conflicting accessibility modifiers",
148                                         next_part.GetSignatureForError ());
149                         }
150
151                         var tc_names = existing.CurrentTypeParameters;
152                         if (tc_names != null) {
153                                 for (int i = 0; i < tc_names.Count; ++i) {
154                                         var tp = next_part.MemberName.TypeParameters[i];
155                                         if (tc_names[i].MemberName.Name != tp.MemberName.Name) {
156                                                 Report.SymbolRelatedToPreviousError (existing.Location, "");
157                                                 Report.Error (264, next_part.Location, "Partial declarations of `{0}' must have the same type parameter names in the same order",
158                                                         next_part.GetSignatureForError ());
159                                                 break;
160                                         }
161
162                                         if (tc_names[i].Variance != tp.Variance) {
163                                                 Report.SymbolRelatedToPreviousError (existing.Location, "");
164                                                 Report.Error (1067, next_part.Location, "Partial declarations of `{0}' must have the same type parameter variance modifiers",
165                                                         next_part.GetSignatureForError ());
166                                                 break;
167                                         }
168                                 }
169                         }
170
171                         if ((next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFIER) != 0) {
172                                 existing.ModFlags |= next_part.ModFlags & ~(Modifiers.DEFAULT_ACCESS_MODIFIER | Modifiers.AccessibilityMask);
173                         } else if ((existing.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFIER) != 0) {
174                                 existing.ModFlags &= ~(Modifiers.DEFAULT_ACCESS_MODIFIER | Modifiers.AccessibilityMask);
175                                 existing.ModFlags |= next_part.ModFlags;
176                         } else {
177                                 existing.ModFlags |= next_part.ModFlags;
178                         }
179
180                         existing.Definition.Modifiers = existing.ModFlags;
181
182                         if (next_part.attributes != null) {
183                                 if (existing.attributes == null)
184                                         existing.attributes = next_part.attributes;
185                                 else
186                                         existing.attributes.AddAttributes (next_part.attributes.Attrs);
187                         }
188
189                         next_part.PartialContainer = existing;
190
191                         existing.AddPartialPart (next_part);
192
193                         AddTypeContainerMember (next_part);
194                 }
195
196                 public virtual void AddTypeContainer (TypeContainer tc)
197                 {
198                         AddTypeContainerMember (tc);
199
200                         var tparams = tc.MemberName.TypeParameters;
201                         if (tparams != null && tc.PartialContainer != null) {
202                                 var td = (TypeDefinition) tc;
203                                 for (int i = 0; i < tparams.Count; ++i) {
204                                         var tp = tparams[i];
205                                         if (tp.MemberName == null)
206                                                 continue;
207
208                                         td.AddNameToContainer (tp, tp.Name);
209                                 }
210                         }
211                 }
212
213                 protected virtual void AddTypeContainerMember (TypeContainer tc)
214                 {
215                         containers.Add (tc);
216                 }
217
218                 public virtual void CloseContainer ()
219                 {
220                         if (containers != null) {
221                                 foreach (TypeContainer tc in containers) {
222                                         tc.CloseContainer ();
223                                 }
224                         }
225                 }
226
227                 public virtual void CreateMetadataName (StringBuilder sb)
228                 {
229                         if (Parent != null && Parent.MemberName != null)
230                                 Parent.CreateMetadataName (sb);
231
232                         MemberName.CreateMetadataName (sb);
233                 }
234
235                 public virtual bool CreateContainer ()
236                 {
237                         if (containers != null) {
238                                 foreach (TypeContainer tc in containers) {
239                                         tc.CreateContainer ();
240                                 }
241                         }
242
243                         return true;
244                 }
245
246                 public override bool Define ()
247                 {
248                         if (containers != null) {
249                                 foreach (TypeContainer tc in containers) {
250                                         tc.Define ();
251                                 }
252                         }
253
254                         // Release cache used by parser only
255                         if (Module.Evaluator == null) {
256                                 defined_names = null;
257                         } else {
258                                 defined_names.Clear ();
259                         }
260
261                         return true;
262                 }
263
264                 public virtual void PrepareEmit ()
265                 {
266                         if (containers != null) {
267                                 foreach (var t in containers) {
268                                         try {
269                                                 t.PrepareEmit ();
270                                         } catch (Exception e) {
271                                                 if (MemberName == MemberName.Null)
272                                                         throw;
273
274                                                 throw new InternalErrorException (t, e);
275                                         }
276                                 }
277                         }
278                 }
279
280                 public virtual bool DefineContainer ()
281                 {
282                         if (is_defined)
283                                 return true;
284
285                         is_defined = true;
286
287                         DoDefineContainer ();
288
289                         if (containers != null) {
290                                 foreach (TypeContainer tc in containers) {
291                                         try {
292                                                 tc.DefineContainer ();
293                                         } catch (Exception e) {
294                                                 if (MemberName == MemberName.Null)
295                                                         throw;
296
297                                                 throw new InternalErrorException (tc, e);
298                                         }
299                                 }
300
301                                 if (PartialContainer != null && PartialContainer != this) {
302                                         containers = null;
303                                 }
304                         }
305
306                         return true;
307                 }
308
309                 public virtual void ExpandBaseInterfaces ()
310                 {
311                         if (containers != null) {
312                                 foreach (TypeContainer tc in containers) {
313                                         tc.ExpandBaseInterfaces ();
314                                 }
315                         }
316                 }
317
318                 protected virtual void DefineNamespace ()
319                 {
320                         if (containers != null) {
321                                 foreach (var tc in containers) {
322                                         try {
323                                                 tc.DefineNamespace ();
324                                         } catch (Exception e) {
325                                                 throw new InternalErrorException (tc, e);
326                                         }
327                                 }
328                         }
329                 }
330
331                 protected virtual void DoDefineContainer ()
332                 {
333                 }
334
335                 public virtual void EmitContainer ()
336                 {
337                         if (containers != null) {
338                                 for (int i = 0; i < containers.Count; ++i)
339                                         containers[i].EmitContainer ();
340                         }
341                 }
342
343                 protected void Error_MissingPartialModifier (MemberCore type)
344                 {
345                         Report.Error (260, type.Location,
346                                 "Missing partial modifier on declaration of type `{0}'. Another partial declaration of this type exists",
347                                 type.GetSignatureForError ());
348                 }
349
350                 public override string GetSignatureForDocumentation ()
351                 {
352                         if (Parent != null && Parent.MemberName != null)
353                                 return Parent.GetSignatureForDocumentation () + "." + MemberName.GetSignatureForDocumentation ();
354
355                         return MemberName.GetSignatureForDocumentation ();
356                 }
357
358                 public override string GetSignatureForError ()
359                 {
360                         if (Parent != null && Parent.MemberName != null) 
361                                 return Parent.GetSignatureForError () + "." + MemberName.GetSignatureForError ();
362
363                         return MemberName.GetSignatureForError ();
364                 }
365
366                 public virtual string GetSignatureForMetadata ()
367                 {
368                         var sb = new StringBuilder ();
369                         CreateMetadataName (sb);
370                         return sb.ToString ();
371                 }
372
373                 public virtual void RemoveContainer (TypeContainer cont)
374                 {
375                         if (containers != null)
376                                 containers.Remove (cont);
377
378                         var tc = Parent == Module ? Module : this;
379                         tc.defined_names.Remove (cont.MemberName.Basename);
380                 }
381
382                 public virtual void VerifyMembers ()
383                 {
384                         if (containers != null) {
385                                 foreach (TypeContainer tc in containers)
386                                         tc.VerifyMembers ();
387                         }
388                 }
389
390                 public override void WriteDebugSymbol (MonoSymbolFile file)
391                 {
392                         if (containers != null) {
393                                 foreach (TypeContainer tc in containers) {
394                                         tc.WriteDebugSymbol (file);
395                                 }
396                         }
397                 }
398         }
399
400         public abstract class TypeDefinition : TypeContainer, ITypeDefinition
401         {
402                 //
403                 // Different context is needed when resolving type container base
404                 // types. Type names come from the parent scope but type parameter
405                 // names from the container scope.
406                 //
407                 public struct BaseContext : IMemberContext
408                 {
409                         TypeContainer tc;
410
411                         public BaseContext (TypeContainer tc)
412                         {
413                                 this.tc = tc;
414                         }
415
416                         #region IMemberContext Members
417
418                         public CompilerContext Compiler {
419                                 get { return tc.Compiler; }
420                         }
421
422                         public TypeSpec CurrentType {
423                                 get { return tc.PartialContainer.CurrentType; }
424                         }
425
426                         public TypeParameters CurrentTypeParameters {
427                                 get { return tc.PartialContainer.CurrentTypeParameters; }
428                         }
429
430                         public MemberCore CurrentMemberDefinition {
431                                 get { return tc; }
432                         }
433
434                         public bool IsObsolete {
435                                 get { return tc.IsObsolete; }
436                         }
437
438                         public bool IsUnsafe {
439                                 get { return tc.IsUnsafe; }
440                         }
441
442                         public bool IsStatic {
443                                 get { return tc.IsStatic; }
444                         }
445
446                         public ModuleContainer Module {
447                                 get { return tc.Module; }
448                         }
449
450                         public string GetSignatureForError ()
451                         {
452                                 return tc.GetSignatureForError ();
453                         }
454
455                         public ExtensionMethodCandidates LookupExtensionMethod (string name, int arity)
456                         {
457                                 return null;
458                         }
459
460                         public FullNamedExpression LookupNamespaceAlias (string name)
461                         {
462                                 return tc.Parent.LookupNamespaceAlias (name);
463                         }
464
465                         public FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
466                         {
467                                 if (arity == 0) {
468                                         var tp = CurrentTypeParameters;
469                                         if (tp != null) {
470                                                 TypeParameter t = tp.Find (name);
471                                                 if (t != null)
472                                                         return new TypeParameterExpr (t, loc);
473                                         }
474                                 }
475
476                                 return tc.Parent.LookupNamespaceOrType (name, arity, mode, loc);
477                         }
478
479                         #endregion
480                 }
481
482                 [Flags]
483                 enum CachedMethods
484                 {
485                         Equals                          = 1,
486                         GetHashCode                     = 1 << 1,
487                         HasStaticFieldInitializer       = 1 << 2
488                 }
489
490                 readonly List<MemberCore> members;
491
492                 // Holds a list of fields that have initializers
493                 protected List<FieldInitializer> initialized_fields;
494
495                 // Holds a list of static fields that have initializers
496                 protected List<FieldInitializer> initialized_static_fields;
497
498                 Dictionary<MethodSpec, Method> hoisted_base_call_proxies;
499
500                 Dictionary<string, FullNamedExpression> Cache = new Dictionary<string, FullNamedExpression> ();
501
502                 //
503                 // Points to the first non-static field added to the container.
504                 //
505                 // This is an arbitrary choice.  We are interested in looking at _some_ non-static field,
506                 // and the first one's as good as any.
507                 //
508                 protected FieldBase first_nonstatic_field;
509
510                 //
511                 // This one is computed after we can distinguish interfaces
512                 // from classes from the arraylist `type_bases' 
513                 //
514                 protected TypeSpec base_type;
515                 FullNamedExpression base_type_expr;     // TODO: It's temporary variable
516                 protected TypeSpec[] iface_exprs;
517
518                 protected List<FullNamedExpression> type_bases;
519
520                 // Partial parts for classes only
521                 List<TypeDefinition> class_partial_parts;
522
523                 TypeDefinition InTransit;
524
525                 public TypeBuilder TypeBuilder;
526                 GenericTypeParameterBuilder[] all_tp_builders;
527                 //
528                 // All recursive type parameters put together sharing same
529                 // TypeParameter instances
530                 //
531                 TypeParameters all_type_parameters;
532
533                 public const string DefaultIndexerName = "Item";
534
535                 bool has_normal_indexers;
536                 string indexer_name;
537                 bool error;
538                 bool members_defined;
539                 bool members_defined_ok;
540                 protected bool has_static_constructor;
541
542                 private CachedMethods cached_method;
543
544                 protected TypeSpec spec;
545                 TypeSpec current_type;
546
547                 public int DynamicSitesCounter;
548                 public int AnonymousMethodsCounter;
549                 public int MethodGroupsCounter;
550
551                 static readonly string[] attribute_targets = new [] { "type" };
552                 static readonly string[] attribute_targets_primary = new [] { "type", "method" };
553
554                 /// <remarks>
555                 ///  The pending methods that need to be implemented
556                 //   (interfaces or abstract methods)
557                 /// </remarks>
558                 PendingImplementation pending;
559
560                 protected TypeDefinition (TypeContainer parent, MemberName name, Attributes attrs, MemberKind kind)
561                         : base (parent, name, attrs, kind)
562                 {
563                         PartialContainer = this;
564                         members = new List<MemberCore> ();
565                 }
566
567                 #region Properties
568
569                 public List<FullNamedExpression> BaseTypeExpressions {
570                         get {
571                                 return type_bases;
572                         }
573                 }
574
575                 public override TypeSpec CurrentType {
576                         get {
577                                 if (current_type == null) {
578                                         if (IsGenericOrParentIsGeneric) {
579                                                 //
580                                                 // Switch to inflated version as it's used by all expressions
581                                                 //
582                                                 var targs = CurrentTypeParameters == null ? TypeSpec.EmptyTypes : CurrentTypeParameters.Types;
583                                                 current_type = spec.MakeGenericType (this, targs);
584                                         } else {
585                                                 current_type = spec;
586                                         }
587                                 }
588
589                                 return current_type;
590                         }
591                 }
592
593                 public override TypeParameters CurrentTypeParameters {
594                         get {
595                                 return PartialContainer.MemberName.TypeParameters;
596                         }
597                 }
598
599                 int CurrentTypeParametersStartIndex {
600                         get {
601                                 int total = all_tp_builders.Length;
602                                 if (CurrentTypeParameters != null) {
603                                         return total - CurrentTypeParameters.Count;
604                                 }
605                                 return total;
606                         }
607                 }
608
609                 public virtual AssemblyDefinition DeclaringAssembly {
610                         get {
611                                 return Module.DeclaringAssembly;
612                         }
613                 }
614
615                 IAssemblyDefinition ITypeDefinition.DeclaringAssembly {
616                         get {
617                                 return Module.DeclaringAssembly;
618                         }
619                 }
620
621                 public TypeSpec Definition {
622                         get {
623                                 return spec;
624                         }
625                 }
626
627                 public bool HasMembersDefined {
628                         get {
629                                 return members_defined;
630                         }
631                 }
632
633                 public bool HasInstanceConstructor {
634                         get {
635                                 return (caching_flags & Flags.HasInstanceConstructor) != 0;
636                         }
637                         set {
638                                 caching_flags |= Flags.HasInstanceConstructor;
639                         }
640                 }
641
642                 // Indicated whether container has StructLayout attribute set Explicit
643                 public bool HasExplicitLayout {
644                         get { return (caching_flags & Flags.HasExplicitLayout) != 0; }
645                         set { caching_flags |= Flags.HasExplicitLayout; }
646                 }
647
648                 public bool HasOperators {
649                         get {
650                                 return (caching_flags & Flags.HasUserOperators) != 0;
651                         }
652                         set {
653                                 caching_flags |= Flags.HasUserOperators;
654                         }
655                 }
656
657                 public bool HasStructLayout {
658                         get { return (caching_flags & Flags.HasStructLayout) != 0; }
659                         set { caching_flags |= Flags.HasStructLayout; }
660                 }
661
662                 public TypeSpec[] Interfaces {
663                         get {
664                                 return iface_exprs;
665                         }
666                 }
667
668                 public bool IsGenericOrParentIsGeneric {
669                         get {
670                                 return all_type_parameters != null;
671                         }
672                 }
673
674                 public bool IsTopLevel {
675                         get {
676                                 return !(Parent is TypeDefinition);
677                         }
678                 }
679
680                 public bool IsPartial {
681                         get {
682                                 return (ModFlags & Modifiers.PARTIAL) != 0;
683                         }
684                 }
685
686                 bool ITypeDefinition.IsTypeForwarder {
687                         get {
688                                 return false;
689                         }
690                 }
691
692                 bool ITypeDefinition.IsCyclicTypeForwarder {
693                         get {
694                                 return false;
695                         }
696                 }
697
698                 //
699                 // Returns true for secondary partial containers
700                 //
701                 bool IsPartialPart {
702                         get {
703                                 return PartialContainer != this;
704                         }
705                 }
706
707                 public MemberCache MemberCache {
708                         get {
709                                 return spec.MemberCache;
710                         }
711                 }
712
713                 public List<MemberCore> Members {
714                         get {
715                                 return members;
716                         }
717                 }
718
719                 string ITypeDefinition.Namespace {
720                         get {
721                                 var p = Parent;
722                                 while (p.Kind != MemberKind.Namespace)
723                                         p = p.Parent;
724
725                                 return p.MemberName == null ? null : p.GetSignatureForError ();
726                         }
727                 }
728
729                 public ParametersCompiled PrimaryConstructorParameters { get; set; }
730
731                 public Arguments PrimaryConstructorBaseArguments { get; set; }
732
733                 public Location PrimaryConstructorBaseArgumentsStart { get; set; }
734
735                 public TypeParameters TypeParametersAll {
736                         get {
737                                 return all_type_parameters;
738                         }
739                 }
740
741                 public override string[] ValidAttributeTargets {
742                         get {
743                                 return PrimaryConstructorParameters != null ? attribute_targets_primary : attribute_targets;
744                         }
745                 }
746
747                 #endregion
748
749                 public override void Accept (StructuralVisitor visitor)
750                 {
751                         visitor.Visit (this);
752                 }
753
754                 public void AddMember (MemberCore symbol)
755                 {
756                         if (symbol.MemberName.ExplicitInterface != null) {
757                                 if (!(Kind == MemberKind.Class || Kind == MemberKind.Struct)) {
758                                         Report.Error (541, symbol.Location,
759                                                 "`{0}': explicit interface declaration can only be declared in a class or struct",
760                                                 symbol.GetSignatureForError ());
761                                 }
762                         }
763
764                         AddNameToContainer (symbol, symbol.MemberName.Name);
765                         members.Add (symbol);
766                 }
767
768                 public override void AddTypeContainer (TypeContainer tc)
769                 {
770                         AddNameToContainer (tc, tc.MemberName.Basename);
771
772                         base.AddTypeContainer (tc);
773                 }
774
775                 protected override void AddTypeContainerMember (TypeContainer tc)
776                 {
777                         members.Add (tc);
778
779                         if (containers == null)
780                                 containers = new List<TypeContainer> ();
781
782                         base.AddTypeContainerMember (tc);
783                 }
784
785                 //
786                 // Adds the member to defined_names table. It tests for duplications and enclosing name conflicts
787                 //
788                 public virtual void AddNameToContainer (MemberCore symbol, string name)
789                 {
790                         if (((ModFlags | symbol.ModFlags) & Modifiers.COMPILER_GENERATED) != 0)
791                                 return;
792
793                         MemberCore mc;
794                         if (!PartialContainer.defined_names.TryGetValue (name, out mc)) {
795                                 PartialContainer.defined_names.Add (name, symbol);
796                                 return;
797                         }
798
799                         if (symbol.EnableOverloadChecks (mc))
800                                 return;
801
802                         InterfaceMemberBase im = mc as InterfaceMemberBase;
803                         if (im != null && im.IsExplicitImpl)
804                                 return;
805
806                         Report.SymbolRelatedToPreviousError (mc);
807                         if ((mc.ModFlags & Modifiers.PARTIAL) != 0 && (symbol is ClassOrStruct || symbol is Interface)) {
808                                 Error_MissingPartialModifier (symbol);
809                                 return;
810                         }
811
812                         if (symbol is TypeParameter) {
813                                 Report.Error (692, symbol.Location,
814                                         "Duplicate type parameter `{0}'", symbol.GetSignatureForError ());
815                         } else {
816                                 Report.Error (102, symbol.Location,
817                                         "The type `{0}' already contains a definition for `{1}'",
818                                         GetSignatureForError (), name);
819                         }
820
821                         return;
822                 }
823         
824                 public void AddConstructor (Constructor c)
825                 {
826                         AddConstructor (c, false);
827                 }
828
829                 public void AddConstructor (Constructor c, bool isDefault)
830                 {
831                         bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
832                         if (!isDefault)
833                                 AddNameToContainer (c, is_static ? Constructor.TypeConstructorName : Constructor.ConstructorName);
834
835                         if (is_static && c.ParameterInfo.IsEmpty) {
836                                 PartialContainer.has_static_constructor = true;
837                         } else {
838                                 PartialContainer.HasInstanceConstructor = true;
839                         }
840
841                         members.Add (c);
842                 }
843
844                 public bool AddField (FieldBase field)
845                 {
846                         AddMember (field);
847
848                         if ((field.ModFlags & Modifiers.STATIC) != 0)
849                                 return true;
850
851                         var first_field = PartialContainer.first_nonstatic_field;
852                         if (first_field == null) {
853                                 PartialContainer.first_nonstatic_field = field;
854                                 return true;
855                         }
856
857                         if (Kind == MemberKind.Struct && first_field.Parent != field.Parent) {
858                                 Report.SymbolRelatedToPreviousError (first_field.Parent);
859                                 Report.Warning (282, 3, field.Location,
860                                         "struct instance field `{0}' found in different declaration from instance field `{1}'",
861                                         field.GetSignatureForError (), first_field.GetSignatureForError ());
862                         }
863                         return true;
864                 }
865
866                 /// <summary>
867                 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
868                 /// </summary>
869                 public void AddIndexer (Indexer i)
870                 {
871                         members.Add (i);
872                 }
873
874                 public void AddOperator (Operator op)
875                 {
876                         PartialContainer.HasOperators = true;
877                         AddMember (op);
878                 }
879
880                 public void AddPartialPart (TypeDefinition part)
881                 {
882                         if (Kind != MemberKind.Class)
883                                 return;
884
885                         if (class_partial_parts == null)
886                                 class_partial_parts = new List<TypeDefinition> ();
887
888                         class_partial_parts.Add (part);
889                 }
890
891                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
892                 {
893                         if (a.Target == AttributeTargets.Method) {
894                                 foreach (var m in members) {
895                                         var c = m as Constructor;
896                                         if (c == null)
897                                                 continue;
898
899                                         if (c.IsPrimaryConstructor) {
900                                                 c.ApplyAttributeBuilder (a, ctor, cdata, pa);
901                                                 return;
902                                         }
903                                 }
904
905                                 throw new InternalErrorException ();
906                         }
907
908                         if (has_normal_indexers && a.Type == pa.DefaultMember) {
909                                 Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
910                                 return;
911                         }
912
913                         if (a.Type == pa.Required) {
914                                 Report.Error (1608, a.Location, "The RequiredAttribute attribute is not permitted on C# types");
915                                 return;
916                         }
917
918                         TypeBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
919                 } 
920
921                 public override AttributeTargets AttributeTargets {
922                         get {
923                                 throw new NotSupportedException ();
924                         }
925                 }
926
927                 public TypeSpec BaseType {
928                         get {
929                                 return spec.BaseType;
930                         }
931                 }
932
933                 protected virtual TypeAttributes TypeAttr {
934                         get {
935                                 return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel);
936                         }
937                 }
938
939                 public int TypeParametersCount {
940                         get {
941                                 return MemberName.Arity;
942                         }
943                 }
944
945                 TypeParameterSpec[] ITypeDefinition.TypeParameters {
946                         get {
947                                 var ctp = PartialContainer.CurrentTypeParameters;
948                                 return ctp == null ? TypeParameterSpec.EmptyTypes : ctp.Types;
949                         }
950                 }
951
952                 public string GetAttributeDefaultMember ()
953                 {
954                         return indexer_name ?? DefaultIndexerName;
955                 }
956
957                 public bool IsComImport {
958                         get {
959                                 if (OptAttributes == null)
960                                         return false;
961
962                                 return OptAttributes.Contains (Module.PredefinedAttributes.ComImport);
963                         }
964                 }
965
966                 public void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
967                 {
968                         if (IsPartialPart)
969                                 PartialContainer.RegisterFieldForInitialization (field, expression);
970
971                         if ((field.ModFlags & Modifiers.STATIC) != 0){
972                                 if (initialized_static_fields == null) {
973                                         HasStaticFieldInitializer = true;
974                                         initialized_static_fields = new List<FieldInitializer> (4);
975                                 }
976
977                                 initialized_static_fields.Add (expression);
978                         } else {
979                                 if (Kind == MemberKind.Struct) {
980                                         if (Compiler.Settings.Version != LanguageVersion.Experimental) {
981                                                 Report.Error (573, expression.Location, "'{0}': Structs cannot have instance property or field initializers",
982                                                         GetSignatureForError ());
983                                         }
984                                 }
985
986                                 if (initialized_fields == null)
987                                         initialized_fields = new List<FieldInitializer> (4);
988
989                                 initialized_fields.Add (expression);
990                         }
991                 }
992
993                 public void ResolveFieldInitializers (BlockContext ec)
994                 {
995                         Debug.Assert (!IsPartialPart);
996
997                         if (ec.IsStatic) {
998                                 if (initialized_static_fields == null)
999                                         return;
1000
1001                                 bool has_complex_initializer = !ec.Module.Compiler.Settings.Optimize;
1002                                 int i;
1003                                 ExpressionStatement [] init = new ExpressionStatement [initialized_static_fields.Count];
1004                                 for (i = 0; i < initialized_static_fields.Count; ++i) {
1005                                         FieldInitializer fi = initialized_static_fields [i];
1006                                         ExpressionStatement s = fi.ResolveStatement (ec);
1007                                         if (s == null) {
1008                                                 s = EmptyExpressionStatement.Instance;
1009                                         } else if (!fi.IsSideEffectFree) {
1010                                                 has_complex_initializer = true;
1011                                         }
1012
1013                                         init [i] = s;
1014                                 }
1015
1016                                 for (i = 0; i < initialized_static_fields.Count; ++i) {
1017                                         FieldInitializer fi = initialized_static_fields [i];
1018                                         //
1019                                         // Need special check to not optimize code like this
1020                                         // static int a = b = 5;
1021                                         // static int b = 0;
1022                                         //
1023                                         if (!has_complex_initializer && fi.IsDefaultInitializer)
1024                                                 continue;
1025
1026                                         ec.AssignmentInfoOffset += fi.AssignmentOffset;
1027                                         ec.CurrentBlock.AddScopeStatement (new StatementExpression (init [i]));
1028                                 }
1029
1030                                 return;
1031                         }
1032
1033                         if (initialized_fields == null)
1034                                 return;
1035
1036                         for (int i = 0; i < initialized_fields.Count; ++i) {
1037                                 FieldInitializer fi = initialized_fields [i];
1038
1039                                 //
1040                                 // Clone before resolving otherwise when field initializer is needed
1041                                 // in more than 1 constructor any resolve after the initial one would
1042                                 // only took the resolved expression which is problem for expressions
1043                                 // that generate extra expressions or code during Resolve phase
1044                                 //
1045                                 var cloned = fi.Clone (new CloneContext ());
1046
1047                                 ExpressionStatement s = fi.ResolveStatement (ec);
1048                                 if (s == null) {
1049                                         initialized_fields [i] = new FieldInitializer (fi.Field, ErrorExpression.Instance, Location.Null);
1050                                         continue;
1051                                 }
1052
1053                                 //
1054                                 // Field is re-initialized to its default value => removed
1055                                 //
1056                                 if (fi.IsDefaultInitializer && Kind != MemberKind.Struct && ec.Module.Compiler.Settings.Optimize)
1057                                         continue;
1058
1059                                 ec.AssignmentInfoOffset += fi.AssignmentOffset;
1060                                 ec.CurrentBlock.AddScopeStatement (new StatementExpression (s));
1061                                 initialized_fields [i] = (FieldInitializer) cloned;
1062                         }
1063                 }
1064
1065                 public override string DocComment {
1066                         get {
1067                                 return comment;
1068                         }
1069                         set {
1070                                 if (value == null)
1071                                         return;
1072
1073                                 comment += value;
1074                         }
1075                 }
1076
1077                 public PendingImplementation PendingImplementations {
1078                         get { return pending; }
1079                 }
1080
1081                 internal override void GenerateDocComment (DocumentationBuilder builder)
1082                 {
1083                         if (IsPartialPart)
1084                                 return;
1085
1086                         base.GenerateDocComment (builder);
1087
1088                         foreach (var member in members)
1089                                 member.GenerateDocComment (builder);
1090                 }
1091
1092                 public TypeSpec GetAttributeCoClass ()
1093                 {
1094                         if (OptAttributes == null)
1095                                 return null;
1096
1097                         Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CoClass);
1098                         if (a == null)
1099                                 return null;
1100
1101                         return a.GetCoClassAttributeValue ();
1102                 }
1103
1104                 public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
1105                 {
1106                         Attribute a = null;
1107                         if (OptAttributes != null) {
1108                                 a = OptAttributes.Search (pa);
1109                         }
1110
1111                         if (a == null)
1112                                 return null;
1113
1114                         return a.GetAttributeUsageAttribute ();
1115                 }
1116
1117                 public virtual CompilationSourceFile GetCompilationSourceFile ()
1118                 {
1119                         TypeContainer ns = Parent;
1120                         while (true) {
1121                                 var sf = ns as CompilationSourceFile;
1122                                 if (sf != null)
1123                                         return sf;
1124
1125                                 ns = ns.Parent;
1126                         }
1127                 }
1128
1129                 public override string GetSignatureForMetadata ()
1130                 {
1131                         if (Parent is TypeDefinition) {
1132                                 return Parent.GetSignatureForMetadata () + "+" + TypeNameParser.Escape (FilterNestedName (MemberName.Basename));
1133                         }
1134
1135                         return base.GetSignatureForMetadata ();
1136                 }
1137
1138                 public virtual void SetBaseTypes (List<FullNamedExpression> baseTypes)
1139                 {
1140                         type_bases = baseTypes;
1141                 }
1142
1143                 /// <summary>
1144                 ///   This function computes the Base class and also the
1145                 ///   list of interfaces that the class or struct @c implements.
1146                 ///   
1147                 ///   The return value is an array (might be null) of
1148                 ///   interfaces implemented (as Types).
1149                 ///   
1150                 ///   The @base_class argument is set to the base object or null
1151                 ///   if this is `System.Object'. 
1152                 /// </summary>
1153                 protected virtual TypeSpec[] ResolveBaseTypes (out FullNamedExpression base_class)
1154                 {
1155                         base_class = null;
1156                         if (type_bases == null)
1157                                 return null;
1158
1159                         int count = type_bases.Count;
1160                         TypeSpec[] ifaces = null;
1161                         var base_context = new BaseContext (this);
1162                         for (int i = 0, j = 0; i < count; i++){
1163                                 FullNamedExpression fne = type_bases [i];
1164
1165                                 var fne_resolved = fne.ResolveAsType (base_context);
1166                                 if (fne_resolved == null)
1167                                         continue;
1168
1169                                 if (i == 0 && Kind == MemberKind.Class && !fne_resolved.IsInterface) {
1170                                         if (fne_resolved.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
1171                                                 Report.Error (1965, Location, "Class `{0}' cannot derive from the dynamic type",
1172                                                         GetSignatureForError ());
1173
1174                                                 continue;
1175                                         }
1176                                         
1177                                         base_type = fne_resolved;
1178                                         base_class = fne;
1179                                         continue;
1180                                 }
1181
1182                                 if (ifaces == null)
1183                                         ifaces = new TypeSpec [count - i];
1184
1185                                 if (fne_resolved.IsInterface) {
1186                                         for (int ii = 0; ii < j; ++ii) {
1187                                                 if (fne_resolved == ifaces [ii]) {
1188                                                         Report.Error (528, Location, "`{0}' is already listed in interface list",
1189                                                                 fne_resolved.GetSignatureForError ());
1190                                                         break;
1191                                                 }
1192                                         }
1193
1194                                         if (Kind == MemberKind.Interface && !IsAccessibleAs (fne_resolved)) {
1195                                                 Report.Error (61, fne.Location,
1196                                                         "Inconsistent accessibility: base interface `{0}' is less accessible than interface `{1}'",
1197                                                         fne_resolved.GetSignatureForError (), GetSignatureForError ());
1198                                         }
1199                                 } else {
1200                                         Report.SymbolRelatedToPreviousError (fne_resolved);
1201                                         if (Kind != MemberKind.Class) {
1202                                                 Report.Error (527, fne.Location, "Type `{0}' in interface list is not an interface", fne_resolved.GetSignatureForError ());
1203                                         } else if (base_class != null)
1204                                                 Report.Error (1721, fne.Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
1205                                                         GetSignatureForError (), base_class.GetSignatureForError (), fne_resolved.GetSignatureForError ());
1206                                         else {
1207                                                 Report.Error (1722, fne.Location, "`{0}': Base class `{1}' must be specified as first",
1208                                                         GetSignatureForError (), fne_resolved.GetSignatureForError ());
1209                                         }
1210                                 }
1211
1212                                 ifaces [j++] = fne_resolved;
1213                         }
1214
1215                         return ifaces;
1216                 }
1217
1218                 //
1219                 // Checks that some operators come in pairs:
1220                 //  == and !=
1221                 // > and <
1222                 // >= and <=
1223                 // true and false
1224                 //
1225                 // They are matched based on the return type and the argument types
1226                 //
1227                 void CheckPairedOperators ()
1228                 {
1229                         bool has_equality_or_inequality = false;
1230                         List<Operator.OpType> found_matched = new List<Operator.OpType> ();
1231
1232                         for (int i = 0; i < members.Count; ++i) {
1233                                 var o_a = members[i] as Operator;
1234                                 if (o_a == null)
1235                                         continue;
1236
1237                                 var o_type = o_a.OperatorType;
1238                                 if (o_type == Operator.OpType.Equality || o_type == Operator.OpType.Inequality)
1239                                         has_equality_or_inequality = true;
1240
1241                                 if (found_matched.Contains (o_type))
1242                                         continue;
1243
1244                                 var matching_type = o_a.GetMatchingOperator ();
1245                                 if (matching_type == Operator.OpType.TOP) {
1246                                         continue;
1247                                 }
1248
1249                                 bool pair_found = false;
1250                                 for (int ii = 0; ii < members.Count; ++ii) {
1251                                         var o_b = members[ii] as Operator;
1252                                         if (o_b == null || o_b.OperatorType != matching_type)
1253                                                 continue;
1254
1255                                         if (!TypeSpecComparer.IsEqual (o_a.ReturnType, o_b.ReturnType))
1256                                                 continue;
1257
1258                                         if (!TypeSpecComparer.Equals (o_a.ParameterTypes, o_b.ParameterTypes))
1259                                                 continue;
1260
1261                                         found_matched.Add (matching_type);
1262                                         pair_found = true;
1263                                         break;
1264                                 }
1265
1266                                 if (!pair_found) {
1267                                         Report.Error (216, o_a.Location,
1268                                                 "The operator `{0}' requires a matching operator `{1}' to also be defined",
1269                                                 o_a.GetSignatureForError (), Operator.GetName (matching_type));
1270                                 }
1271                         }
1272
1273                         if (has_equality_or_inequality) {
1274                                 if (!HasEquals)
1275                                         Report.Warning (660, 2, Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)",
1276                                                 GetSignatureForError ());
1277
1278                                 if (!HasGetHashCode)
1279                                         Report.Warning (661, 2, Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()",
1280                                                 GetSignatureForError ());
1281                         }
1282                 }
1283
1284                 public override void CreateMetadataName (StringBuilder sb)
1285                 {
1286                         if (Parent.MemberName != null) {
1287                                 Parent.CreateMetadataName (sb);
1288
1289                                 if (sb.Length != 0) {
1290                                         sb.Append (".");
1291                                 }
1292                         }
1293
1294                         sb.Append (MemberName.Basename);
1295                 }
1296         
1297                 bool CreateTypeBuilder ()
1298                 {
1299                         //
1300                         // Sets .size to 1 for structs with no instance fields
1301                         //
1302                         int type_size = Kind == MemberKind.Struct && first_nonstatic_field == null && !(this is StateMachine) ? 1 : 0;
1303
1304                         var parent_def = Parent as TypeDefinition;
1305                         if (parent_def == null) {
1306                                 var sb = new StringBuilder ();
1307                                 CreateMetadataName (sb);
1308                                 TypeBuilder = Module.CreateBuilder (sb.ToString (), TypeAttr, type_size);
1309                         } else {
1310                                 TypeBuilder = parent_def.TypeBuilder.DefineNestedType (FilterNestedName (MemberName.Basename), TypeAttr, null, type_size);
1311                         }
1312
1313                         if (DeclaringAssembly.Importer != null)
1314                                 DeclaringAssembly.Importer.AddCompiledType (TypeBuilder, spec);
1315
1316                         spec.SetMetaInfo (TypeBuilder);
1317                         spec.MemberCache = new MemberCache (this);
1318
1319                         TypeParameters parentAllTypeParameters = null;
1320                         if (parent_def != null) {
1321                                 spec.DeclaringType = Parent.CurrentType;
1322                                 parent_def.MemberCache.AddMember (spec);
1323                                 parentAllTypeParameters = parent_def.all_type_parameters;
1324                         }
1325
1326                         if (MemberName.TypeParameters != null || parentAllTypeParameters != null) {
1327                                 var tparam_names = CreateTypeParameters (parentAllTypeParameters);
1328
1329                                 all_tp_builders = TypeBuilder.DefineGenericParameters (tparam_names);
1330
1331                                 if (CurrentTypeParameters != null) {
1332                                         CurrentTypeParameters.Create (spec, CurrentTypeParametersStartIndex, this);
1333                                         CurrentTypeParameters.Define (all_tp_builders);
1334                                 }
1335                         }
1336
1337                         return true;
1338                 }
1339
1340                 public static string FilterNestedName (string name)
1341                 {
1342                         //
1343                         // SRE API does not handle namespaces and types separately but
1344                         // determine that from '.' in name. That's problematic because 
1345                         // dot is valid character for type name. By replacing any '.'
1346                         // in name we avoid any ambiguities and never emit metadata
1347                         // namespace for nested types
1348                         //
1349                         return name.Replace ('.', '_');
1350                 }
1351
1352                 string[] CreateTypeParameters (TypeParameters parentAllTypeParameters)
1353                 {
1354                         string[] names;
1355                         int parent_offset = 0;
1356                         if (parentAllTypeParameters != null) {
1357                                 if (CurrentTypeParameters == null) {
1358                                         all_type_parameters = parentAllTypeParameters;
1359                                         return parentAllTypeParameters.GetAllNames ();
1360                                 }
1361
1362                                 names = new string[parentAllTypeParameters.Count + CurrentTypeParameters.Count];
1363                                 all_type_parameters = new TypeParameters (names.Length);
1364                                 all_type_parameters.Add (parentAllTypeParameters);
1365
1366                                 parent_offset = all_type_parameters.Count;
1367                                 for (int i = 0; i < parent_offset; ++i)
1368                                         names[i] = all_type_parameters[i].MemberName.Name;
1369
1370                         } else {
1371                                 names = new string[CurrentTypeParameters.Count];
1372                         }
1373
1374                         for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
1375                                 if (all_type_parameters != null)
1376                                         all_type_parameters.Add (MemberName.TypeParameters[i]);
1377
1378                                 var name = CurrentTypeParameters[i].MemberName.Name;
1379                                 names[parent_offset + i] = name;
1380                                 for (int ii = 0; ii < parent_offset + i; ++ii) {
1381                                         if (names[ii] != name)
1382                                                 continue;
1383
1384                                         var tp = CurrentTypeParameters[i];
1385                                         var conflict = all_type_parameters[ii];
1386
1387                                         tp.WarningParentNameConflict (conflict);
1388                                 }
1389                         }
1390
1391                         if (all_type_parameters == null)
1392                                 all_type_parameters = CurrentTypeParameters;
1393
1394                         return names;
1395                 }
1396
1397
1398                 public SourceMethodBuilder CreateMethodSymbolEntry ()
1399                 {
1400                         if (Module.DeclaringAssembly.SymbolWriter == null || (ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
1401                                 return null;
1402
1403                         var source_file = GetCompilationSourceFile ();
1404                         if (source_file == null)
1405                                 return null;
1406
1407                         return new SourceMethodBuilder (source_file.SymbolUnitEntry);
1408                 }
1409
1410                 //
1411                 // Creates a proxy base method call inside this container for hoisted base member calls
1412                 //
1413                 public MethodSpec CreateHoistedBaseCallProxy (ResolveContext rc, MethodSpec method)
1414                 {
1415                         Method proxy_method;
1416
1417                         //
1418                         // One proxy per base method is enough
1419                         //
1420                         if (hoisted_base_call_proxies == null) {
1421                                 hoisted_base_call_proxies = new Dictionary<MethodSpec, Method> ();
1422                                 proxy_method = null;
1423                         } else {
1424                                 hoisted_base_call_proxies.TryGetValue (method, out proxy_method);
1425                         }
1426
1427                         if (proxy_method == null) {
1428                                 string name = CompilerGeneratedContainer.MakeName (method.Name, null, "BaseCallProxy", hoisted_base_call_proxies.Count);
1429
1430                                 MemberName member_name;
1431                                 TypeArguments targs = null;
1432                                 TypeSpec return_type = method.ReturnType;
1433                                 var local_param_types = method.Parameters.Types;
1434
1435                                 if (method.IsGeneric) {
1436                                         //
1437                                         // Copy all base generic method type parameters info
1438                                         //
1439                                         var hoisted_tparams = method.GenericDefinition.TypeParameters;
1440                                         var tparams = new TypeParameters ();
1441
1442                                         targs = new TypeArguments ();
1443                                         targs.Arguments = new TypeSpec[hoisted_tparams.Length];
1444                                         for (int i = 0; i < hoisted_tparams.Length; ++i) {
1445                                                 var tp = hoisted_tparams[i];
1446                                                 var tp_name = tp.Name;
1447 #if DEBUG
1448                                                 tp_name += "_Proxy";
1449 #endif
1450                                                 var local_tp = new TypeParameter (tp, null, new MemberName (tp_name, Location), null);
1451                                                 tparams.Add (local_tp);
1452
1453                                                 targs.Add (new SimpleName (tp.Name, Location));
1454                                                 targs.Arguments[i] = local_tp.Type;
1455                                         }
1456
1457                                         member_name = new MemberName (name, tparams, Location);
1458
1459                                         //
1460                                         // Mutate any method type parameters from original
1461                                         // to newly created hoisted version
1462                                         //
1463                                         var mutator = new TypeParameterMutator (hoisted_tparams, tparams);
1464                                         return_type = mutator.Mutate (return_type);
1465                                         local_param_types = mutator.Mutate (local_param_types);
1466
1467                                         var inflator = new TypeParameterInflator (this, null, hoisted_tparams, targs.Arguments);
1468                                         for (int i = 0; i < hoisted_tparams.Length; ++i) {
1469                                                 var tp_spec = (TypeParameterSpec) targs.Arguments [i];
1470                                                 tp_spec.InflateConstraints (inflator, tp_spec);
1471                                         }
1472                                 } else {
1473                                         member_name = new MemberName (name);
1474                                 }
1475
1476                                 var base_parameters = new Parameter[method.Parameters.Count];
1477                                 for (int i = 0; i < base_parameters.Length; ++i) {
1478                                         var base_param = method.Parameters.FixedParameters[i];
1479                                         base_parameters[i] = new Parameter (new TypeExpression (local_param_types [i], Location),
1480                                                 base_param.Name, base_param.ModFlags, null, Location);
1481                                         base_parameters[i].Resolve (this, i);
1482                                 }
1483
1484                                 var cloned_params = ParametersCompiled.CreateFullyResolved (base_parameters, local_param_types);
1485                                 if (method.Parameters.HasArglist) {
1486                                         cloned_params.FixedParameters[0] = new Parameter (null, "__arglist", Parameter.Modifier.NONE, null, Location);
1487                                         cloned_params.Types[0] = Module.PredefinedTypes.RuntimeArgumentHandle.Resolve ();
1488                                 }
1489
1490                                 // Compiler generated proxy
1491                                 proxy_method = new Method (this, new TypeExpression (return_type, Location),
1492                                         Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED | Modifiers.DEBUGGER_HIDDEN,
1493                                         member_name, cloned_params, null);
1494
1495                                 var block = new ToplevelBlock (Compiler, proxy_method.ParameterInfo, Location) {
1496                                         IsCompilerGenerated = true
1497                                 };
1498
1499                                 var mg = MethodGroupExpr.CreatePredefined (method, method.DeclaringType, Location);
1500                                 mg.InstanceExpression = new BaseThis (method.DeclaringType, Location);
1501                                 if (targs != null)
1502                                         mg.SetTypeArguments (rc, targs);
1503
1504                                 // Get all the method parameters and pass them as arguments
1505                                 var real_base_call = new Invocation (mg, block.GetAllParametersArguments ());
1506                                 Statement statement;
1507                                 if (method.ReturnType.Kind == MemberKind.Void)
1508                                         statement = new StatementExpression (real_base_call);
1509                                 else
1510                                         statement = new Return (real_base_call, Location);
1511
1512                                 block.AddStatement (statement);
1513                                 proxy_method.Block = block;
1514
1515                                 members.Add (proxy_method);
1516                                 proxy_method.Define ();
1517                                 proxy_method.PrepareEmit ();
1518
1519                                 hoisted_base_call_proxies.Add (method, proxy_method);
1520                         }
1521
1522                         return proxy_method.Spec;
1523                 }
1524
1525                 protected bool DefineBaseTypes ()
1526                 {
1527                         if (IsPartialPart && Kind == MemberKind.Class)
1528                                 return true;
1529
1530                         return DoDefineBaseType ();
1531                 }
1532
1533                 bool DoDefineBaseType ()
1534                 {
1535                         iface_exprs = ResolveBaseTypes (out base_type_expr);
1536                         bool set_base_type;
1537
1538                         if (IsPartialPart) {
1539                                 set_base_type = false;
1540
1541                                 if (base_type_expr != null) {
1542                                         if (PartialContainer.base_type_expr != null && PartialContainer.base_type != base_type) {
1543                                                 Report.SymbolRelatedToPreviousError (base_type_expr.Location, "");
1544                                                 Report.Error (263, Location,
1545                                                         "Partial declarations of `{0}' must not specify different base classes",
1546                                                         GetSignatureForError ());
1547                                         } else {
1548                                                 PartialContainer.base_type_expr = base_type_expr;
1549                                                 PartialContainer.base_type = base_type;
1550                                                 set_base_type = true;
1551                                         }
1552                                 }
1553
1554                                 if (iface_exprs != null) {
1555                                         if (PartialContainer.iface_exprs == null)
1556                                                 PartialContainer.iface_exprs = iface_exprs;
1557                                         else {
1558                                                 var ifaces = new List<TypeSpec> (PartialContainer.iface_exprs);
1559                                                 foreach (var iface_partial in iface_exprs) {
1560                                                         if (ifaces.Contains (iface_partial))
1561                                                                 continue;
1562
1563                                                         ifaces.Add (iface_partial);
1564                                                 }
1565
1566                                                 PartialContainer.iface_exprs = ifaces.ToArray ();
1567                                         }
1568                                 }
1569
1570                                 PartialContainer.members.AddRange (members);
1571                                 if (containers != null) {
1572                                         if (PartialContainer.containers == null)
1573                                                 PartialContainer.containers = new List<TypeContainer> ();
1574
1575                                         PartialContainer.containers.AddRange (containers);
1576                                 }
1577
1578                                 if (PrimaryConstructorParameters != null) {
1579                                         if (PartialContainer.PrimaryConstructorParameters != null) {
1580                                                 Report.Error (8036, Location, "Only one part of a partial type can declare primary constructor parameters");
1581                                         } else {
1582                                                 PartialContainer.PrimaryConstructorParameters = PrimaryConstructorParameters;
1583                                         }
1584                                 }
1585
1586                                 members_defined = members_defined_ok = true;
1587                                 caching_flags |= Flags.CloseTypeCreated;
1588                         } else {
1589                                 set_base_type = true;
1590                         }
1591
1592                         var cycle = CheckRecursiveDefinition (this);
1593                         if (cycle != null) {
1594                                 Report.SymbolRelatedToPreviousError (cycle);
1595                                 if (this is Interface) {
1596                                         Report.Error (529, Location,
1597                                                 "Inherited interface `{0}' causes a cycle in the interface hierarchy of `{1}'",
1598                                             GetSignatureForError (), cycle.GetSignatureForError ());
1599
1600                                         iface_exprs = null;
1601                                         PartialContainer.iface_exprs = null;
1602                                 } else {
1603                                         Report.Error (146, Location,
1604                                                 "Circular base class dependency involving `{0}' and `{1}'",
1605                                                 GetSignatureForError (), cycle.GetSignatureForError ());
1606
1607                                         base_type = null;
1608                                         PartialContainer.base_type = null;
1609                                 }
1610                         }
1611
1612                         if (iface_exprs != null) {
1613                                 if (!PrimaryConstructorBaseArgumentsStart.IsNull) {
1614                                         Report.Error (8049, PrimaryConstructorBaseArgumentsStart, "Implemented interfaces cannot have arguments");
1615                                 }
1616
1617                                 foreach (var iface_type in iface_exprs) {
1618                                         // Prevents a crash, the interface might not have been resolved: 442144
1619                                         if (iface_type == null)
1620                                                 continue;
1621                                         
1622                                         if (!spec.AddInterfaceDefined (iface_type))
1623                                                 continue;
1624
1625                                         TypeBuilder.AddInterfaceImplementation (iface_type.GetMetaInfo ());
1626                                 }
1627                         }
1628
1629                         if (Kind == MemberKind.Interface) {
1630                                 spec.BaseType = Compiler.BuiltinTypes.Object;
1631                                 return true;
1632                         }
1633
1634                         if (set_base_type) {
1635                                 SetBaseType ();
1636                         }
1637
1638                         //
1639                         // Base type of partial container has to be resolved before we
1640                         // resolve any nested types of the container. We need to know
1641                         // partial parts because the base type can be specified in file
1642                         // defined after current container
1643                         //
1644                         if (class_partial_parts != null) {
1645                                 foreach (var pp in class_partial_parts) {
1646                                         if (pp.PrimaryConstructorBaseArguments != null)
1647                                                 PrimaryConstructorBaseArguments = pp.PrimaryConstructorBaseArguments;
1648
1649                                         pp.DoDefineBaseType ();
1650                                 }
1651
1652                         }
1653
1654                         return true;
1655                 }
1656
1657                 void SetBaseType ()
1658                 {
1659                         if (base_type == null) {
1660                                 TypeBuilder.SetParent (null);
1661                                 return;
1662                         }
1663
1664                         if (spec.BaseType == base_type)
1665                                 return;
1666
1667                         spec.BaseType = base_type;
1668
1669                         if (IsPartialPart)
1670                                 spec.UpdateInflatedInstancesBaseType ();
1671
1672                         // Set base type after type creation
1673                         TypeBuilder.SetParent (base_type.GetMetaInfo ());
1674                 }
1675
1676                 public override void ExpandBaseInterfaces ()
1677                 {
1678                         DoResolveTypeParameters ();
1679
1680                         if (!IsPartialPart)
1681                                 DoExpandBaseInterfaces ();
1682
1683                         base.ExpandBaseInterfaces ();
1684                 }
1685
1686                 public void DoExpandBaseInterfaces ()
1687                 {
1688                         if ((caching_flags & Flags.InterfacesExpanded) != 0)
1689                                 return;
1690
1691                         caching_flags |= Flags.InterfacesExpanded;
1692
1693                         //
1694                         // Expand base interfaces. It cannot be done earlier because all partial
1695                         // interface parts need to be defined before the type they are used from
1696                         //
1697                         if (iface_exprs != null) {
1698                                 foreach (var iface in iface_exprs) {
1699                                         if (iface == null)
1700                                                 continue;
1701
1702                                         var td = iface.MemberDefinition as TypeDefinition;
1703                                         if (td != null)
1704                                                 td.DoExpandBaseInterfaces ();
1705
1706                                         if (iface.Interfaces == null)
1707                                                 continue;
1708
1709                                         foreach (var biface in iface.Interfaces) {
1710                                                 if (spec.AddInterfaceDefined (biface)) {
1711                                                         TypeBuilder.AddInterfaceImplementation (biface.GetMetaInfo ());
1712                                                 }
1713                                         }
1714                                 }
1715                         }
1716
1717                         //
1718                         // Include all base type interfaces too, see ImportTypeBase for details
1719                         //
1720                         if (base_type != null) {
1721                                 var td = base_type.MemberDefinition as TypeDefinition;
1722                                 if (td != null)
1723                                         td.DoExpandBaseInterfaces ();
1724
1725                                 //
1726                                 // Simply use base interfaces only, they are all expanded which makes
1727                                 // it easy to handle generic type argument propagation with single
1728                                 // inflator only.
1729                                 //
1730                                 // interface IA<T> : IB<T>
1731                                 // interface IB<U> : IC<U>
1732                                 // interface IC<V>
1733                                 //
1734                                 if (base_type.Interfaces != null) {
1735                                         foreach (var iface in base_type.Interfaces) {
1736                                                 spec.AddInterfaceDefined (iface);
1737                                         }
1738                                 }
1739                         }
1740                 }
1741
1742                 public override void PrepareEmit ()
1743                 {
1744                         if ((caching_flags & Flags.CloseTypeCreated) != 0)
1745                                 return;
1746
1747                         foreach (var member in members) {
1748                                 var pbm = member as MemberBase;
1749                                 if (pbm != null)
1750                                         pbm.PrepareEmit ();
1751                         }
1752
1753                         base.PrepareEmit ();
1754                 }
1755
1756                 //
1757                 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1758                 //
1759                 public override bool CreateContainer ()
1760                 {
1761                         if (TypeBuilder != null)
1762                                 return !error;
1763
1764                         if (error)
1765                                 return false;
1766
1767                         if (IsPartialPart) {
1768                                 spec = PartialContainer.spec;
1769                                 TypeBuilder = PartialContainer.TypeBuilder;
1770                                 all_tp_builders = PartialContainer.all_tp_builders;
1771                                 all_type_parameters = PartialContainer.all_type_parameters;
1772                         } else {
1773                                 if (!CreateTypeBuilder ()) {
1774                                         error = true;
1775                                         return false;
1776                                 }
1777                         }
1778
1779                         return base.CreateContainer ();
1780                 }
1781
1782                 protected override void DoDefineContainer ()
1783                 {
1784                         DefineBaseTypes ();
1785                 }
1786
1787                 //
1788                 // Replaces normal spec with predefined one when compiling corlib
1789                 // and this type container defines predefined type
1790                 //
1791                 public void SetPredefinedSpec (BuiltinTypeSpec spec)
1792                 {
1793                         // When compiling build-in types we start with two
1794                         // version of same type. One is of BuiltinTypeSpec and
1795                         // second one is ordinary TypeSpec. The unification
1796                         // happens at later stage when we know which type
1797                         // really matches the builtin type signature. However
1798                         // that means TypeSpec create during CreateType of this
1799                         // type has to be replaced with builtin one
1800                         // 
1801                         spec.SetMetaInfo (TypeBuilder);
1802                         spec.MemberCache = this.spec.MemberCache;
1803                         spec.DeclaringType = this.spec.DeclaringType;
1804
1805                         this.spec = spec;
1806                         current_type = null;
1807                         if (class_partial_parts != null) {
1808                                 foreach (var part in class_partial_parts) {
1809                                         part.spec = spec;
1810                                         part.current_type = null;
1811                                 }
1812                         }
1813                 }
1814
1815                 public override void RemoveContainer (TypeContainer cont)
1816                 {
1817                         base.RemoveContainer (cont);
1818                         Members.Remove (cont);
1819                         Cache.Remove (cont.MemberName.Basename);
1820                 }
1821
1822                 protected virtual bool DoResolveTypeParameters ()
1823                 {
1824                         var tparams = MemberName.TypeParameters;
1825                         if (tparams == null)
1826                                 return true;
1827
1828                         var base_context = new BaseContext (this);
1829                         for (int i = 0; i < tparams.Count; ++i) {
1830                                 var tp = tparams[i];
1831
1832                                 if (!tp.ResolveConstraints (base_context)) {
1833                                         error = true;
1834                                         return false;
1835                                 }
1836
1837                                 if (IsPartialPart) {
1838                                         var pc_tp = PartialContainer.CurrentTypeParameters [i];
1839
1840                                         tp.Create (spec, this);
1841                                         tp.Define (pc_tp);
1842
1843                                         if (tp.OptAttributes != null) {
1844                                                 if (pc_tp.OptAttributes == null)
1845                                                         pc_tp.OptAttributes = tp.OptAttributes;
1846                                                 else
1847                                                         pc_tp.OptAttributes.Attrs.AddRange (tp.OptAttributes.Attrs);
1848                                         }
1849                                 }
1850                         }
1851
1852                         if (IsPartialPart) {
1853                                 PartialContainer.CurrentTypeParameters.UpdateConstraints (this);
1854                         }
1855
1856                         return true;
1857                 }
1858
1859                 TypeSpec CheckRecursiveDefinition (TypeDefinition tc)
1860                 {
1861                         if (InTransit != null)
1862                                 return spec;
1863
1864                         InTransit = tc;
1865
1866                         if (base_type != null) {
1867                                 var ptc = base_type.MemberDefinition as TypeDefinition;
1868                                 if (ptc != null && ptc.CheckRecursiveDefinition (this) != null)
1869                                         return base_type;
1870                         }
1871
1872                         if (iface_exprs != null && this is Interface) {
1873                                 foreach (var iface in iface_exprs) {
1874                                         // the interface might not have been resolved, prevents a crash, see #442144
1875                                         if (iface == null)
1876                                                 continue;
1877                                         var ptc = iface.MemberDefinition as Interface;
1878                                         if (ptc != null && ptc.CheckRecursiveDefinition (this) != null)
1879                                                 return iface;
1880                                 }
1881                         }
1882
1883                         if (!IsTopLevel && Parent.PartialContainer.CheckRecursiveDefinition (this) != null)
1884                                 return spec;
1885
1886                         InTransit = null;
1887                         return null;
1888                 }
1889
1890                 /// <summary>
1891                 ///   Populates our TypeBuilder with fields and methods
1892                 /// </summary>
1893                 public sealed override bool Define ()
1894                 {
1895                         if (members_defined)
1896                                 return members_defined_ok;
1897
1898                         members_defined_ok = DoDefineMembers ();
1899                         members_defined = true;
1900
1901                         base.Define ();
1902
1903                         return members_defined_ok;
1904                 }
1905
1906                 protected virtual bool DoDefineMembers ()
1907                 {
1908                         Debug.Assert (!IsPartialPart);
1909
1910                         if (iface_exprs != null) {
1911                                 foreach (var iface_type in iface_exprs) {
1912                                         if (iface_type == null)
1913                                                 continue;
1914
1915                                         // Ensure the base is always setup
1916                                         var compiled_iface = iface_type.MemberDefinition as Interface;
1917                                         if (compiled_iface != null)
1918                                                 compiled_iface.Define ();
1919
1920                                         iface_type.CheckObsoleteness (this, Location);
1921
1922                                         if (iface_type.Arity > 0) {
1923                                                 // TODO: passing `this' is wrong, should be base type iface instead
1924                                                 VarianceDecl.CheckTypeVariance (iface_type, Variance.Covariant, this);
1925
1926                                                 if (((InflatedTypeSpec) iface_type).HasDynamicArgument () && !IsCompilerGenerated) {
1927                                                         Report.Error (1966, Location,
1928                                                                 "`{0}': cannot implement a dynamic interface `{1}'",
1929                                                                 GetSignatureForError (), iface_type.GetSignatureForError ());
1930                                                         return false;
1931                                                 }
1932                                         }
1933
1934                                         if (iface_type.IsGenericOrParentIsGeneric) {
1935                                                 foreach (var prev_iface in iface_exprs) {
1936                                                         if (prev_iface == iface_type || prev_iface == null)
1937                                                                 break;
1938
1939                                                         if (!TypeSpecComparer.Unify.IsEqual (iface_type, prev_iface))
1940                                                                 continue;
1941
1942                                                         Report.Error (695, Location,
1943                                                                 "`{0}' cannot implement both `{1}' and `{2}' because they may unify for some type parameter substitutions",
1944                                                                 GetSignatureForError (), prev_iface.GetSignatureForError (), iface_type.GetSignatureForError ());
1945                                                 }
1946                                         }
1947                                 }
1948
1949                                 if (Kind == MemberKind.Interface) {
1950                                         foreach (var iface in spec.Interfaces) {
1951                                                 MemberCache.AddInterface (iface);
1952                                         }
1953                                 }
1954                         }
1955
1956                         if (base_type != null) {
1957                                 //
1958                                 // Run checks skipped during DefineType (e.g FullNamedExpression::ResolveAsType)
1959                                 //
1960                                 if (base_type_expr != null) {
1961                                         base_type.CheckObsoleteness (this, base_type_expr.Location);
1962
1963                                         if (IsGenericOrParentIsGeneric && base_type.IsAttribute) {
1964                                                 Report.Error (698, base_type_expr.Location,
1965                                                         "A generic type cannot derive from `{0}' because it is an attribute class",
1966                                                         base_type.GetSignatureForError ());
1967                                         }
1968                                 }
1969
1970                                 var baseContainer = base_type.MemberDefinition as ClassOrStruct;
1971                                 if (baseContainer != null) {
1972                                         baseContainer.Define ();
1973
1974                                         //
1975                                         // It can trigger define of this type (for generic types only)
1976                                         //
1977                                         if (HasMembersDefined)
1978                                                 return true;
1979                                 }
1980                         }
1981
1982                         if (Kind == MemberKind.Struct || Kind == MemberKind.Class) {
1983                                 pending = PendingImplementation.GetPendingImplementations (this);
1984                         }
1985
1986                         var count = members.Count;              
1987                         for (int i = 0; i < count; ++i) {
1988                                 var mc = members[i] as InterfaceMemberBase;
1989                                 if (mc == null || !mc.IsExplicitImpl)
1990                                         continue;
1991
1992                                 try {
1993                                         mc.Define ();
1994                                 } catch (Exception e) {
1995                                         throw new InternalErrorException (mc, e);
1996                                 }
1997                         }
1998
1999                         for (int i = 0; i < count; ++i) {
2000                                 var mc = members[i] as InterfaceMemberBase;
2001                                 if (mc != null && mc.IsExplicitImpl)
2002                                         continue;
2003
2004                                 if (members[i] is TypeContainer)
2005                                         continue;
2006
2007                                 try {
2008                                         members[i].Define ();
2009                                 } catch (Exception e) {
2010                                         throw new InternalErrorException (members[i], e);
2011                                 }
2012                         }
2013
2014                         if (HasOperators) {
2015                                 CheckPairedOperators ();
2016                         }
2017
2018                         ComputeIndexerName();
2019
2020                         if (HasEquals && !HasGetHashCode) {
2021                                 Report.Warning (659, 3, Location,
2022                                         "`{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", GetSignatureForError ());
2023                         }
2024
2025                         if (Kind == MemberKind.Interface && iface_exprs != null) {
2026                                 MemberCache.RemoveHiddenMembers (spec);
2027                         }
2028
2029                         return true;
2030                 }
2031
2032                 void ComputeIndexerName ()
2033                 {
2034                         var indexers = MemberCache.FindMembers (spec, MemberCache.IndexerNameAlias, true);
2035                         if (indexers == null)
2036                                 return;
2037
2038                         string class_indexer_name = null;
2039
2040                         //
2041                         // Check normal indexers for consistent name, explicit interface implementation
2042                         // indexers are ignored
2043                         //
2044                         foreach (var indexer in indexers) {
2045                                 //
2046                                 // FindMembers can return unfiltered full hierarchy names
2047                                 //
2048                                 if (indexer.DeclaringType != spec)
2049                                         continue;
2050
2051                                 has_normal_indexers = true;
2052
2053                                 if (class_indexer_name == null) {
2054                                         indexer_name = class_indexer_name = indexer.Name;
2055                                         continue;
2056                                 }
2057
2058                                 if (indexer.Name != class_indexer_name)
2059                                         Report.Error (668, ((Indexer)indexer.MemberDefinition).Location,
2060                                                 "Two indexers have different names; the IndexerName attribute must be used with the same name on every indexer within a type");
2061                         }
2062                 }
2063
2064                 void EmitIndexerName ()
2065                 {
2066                         if (!has_normal_indexers)
2067                                 return;
2068
2069                         var ctor = Module.PredefinedMembers.DefaultMemberAttributeCtor.Get ();
2070                         if (ctor == null)
2071                                 return;
2072
2073                         var encoder = new AttributeEncoder ();
2074                         encoder.Encode (GetAttributeDefaultMember ());
2075                         encoder.EncodeEmptyNamedArguments ();
2076
2077                         TypeBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());
2078                 }
2079
2080                 public override void VerifyMembers ()
2081                 {
2082                         //
2083                         // Check for internal or private fields that were never assigned
2084                         //
2085                         if (!IsCompilerGenerated && Compiler.Settings.WarningLevel >= 3 && this == PartialContainer) {
2086                                 bool is_type_exposed = Kind == MemberKind.Struct || IsExposedFromAssembly ();
2087                                 foreach (var member in members) {
2088                                         if (member is Event) {
2089                                                 //
2090                                                 // An event can be assigned from same class only, report
2091                                                 // this warning for all accessibility modes
2092                                                 //
2093                                                 if (!member.IsUsed && !PartialContainer.HasStructLayout)
2094                                                         Report.Warning (67, 3, member.Location, "The event `{0}' is never used", member.GetSignatureForError ());
2095
2096                                                 continue;
2097                                         }
2098
2099                                         if ((member.ModFlags & Modifiers.AccessibilityMask) != Modifiers.PRIVATE) {
2100                                                 if (is_type_exposed)
2101                                                         continue;
2102
2103                                                 member.SetIsUsed ();
2104                                         }
2105
2106                                         var f = member as Field;
2107                                         if (f == null)
2108                                                 continue;
2109
2110                                         if (!member.IsUsed) {
2111                                                 if (!PartialContainer.HasStructLayout) {
2112                                                         if ((member.caching_flags & Flags.IsAssigned) == 0) {
2113                                                                 Report.Warning (169, 3, member.Location, "The private field `{0}' is never used", member.GetSignatureForError ());
2114                                                         } else {
2115                                                                 Report.Warning (414, 3, member.Location, "The private field `{0}' is assigned but its value is never used",
2116                                                                         member.GetSignatureForError ());
2117                                                         }
2118                                                 }
2119
2120                                                 continue;
2121                                         }
2122
2123                                         if ((f.caching_flags & Flags.IsAssigned) != 0)
2124                                                 continue;
2125
2126                                         //
2127                                         // Only report 649 on level 4
2128                                         //
2129                                         if (Compiler.Settings.WarningLevel < 4)
2130                                                 continue;
2131
2132                                         //
2133                                         // Don't be pedantic when type requires specific layout
2134                                         //
2135                                         if (f.OptAttributes != null || PartialContainer.HasStructLayout)
2136                                                 continue;
2137
2138                                         Constant c = New.Constantify (f.MemberType, f.Location);
2139                                         string value;
2140                                         if (c != null) {
2141                                                 value = c.GetValueAsLiteral ();
2142                                         } else if (TypeSpec.IsReferenceType (f.MemberType)) {
2143                                                 value = "null";
2144                                         } else {
2145                                                 value = null;
2146                                         }
2147
2148                                         if (value != null)
2149                                                 value = " `" + value + "'";
2150
2151                                         Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value{1}",
2152                                                 f.GetSignatureForError (), value);
2153                                 }
2154                         }
2155
2156                         base.VerifyMembers ();
2157                 }
2158
2159                 public override void Emit ()
2160                 {
2161                         if (OptAttributes != null)
2162                                 OptAttributes.Emit ();
2163
2164                         if (!IsCompilerGenerated) {
2165                                 if (!IsTopLevel) {
2166                                         MemberSpec candidate;
2167                                         bool overrides = false;
2168                                         var conflict_symbol = MemberCache.FindBaseMember (this, out candidate, ref overrides);
2169                                         if (conflict_symbol == null && candidate == null) {
2170                                                 if ((ModFlags & Modifiers.NEW) != 0)
2171                                                         Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required",
2172                                                                 GetSignatureForError ());
2173                                         } else {
2174                                                 if ((ModFlags & Modifiers.NEW) == 0) {
2175                                                         if (candidate == null)
2176                                                                 candidate = conflict_symbol;
2177
2178                                                         Report.SymbolRelatedToPreviousError (candidate);
2179                                                         Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
2180                                                                 GetSignatureForError (), candidate.GetSignatureForError ());
2181                                                 }
2182                                         }
2183                                 }
2184
2185                                 // Run constraints check on all possible generic types
2186                                 if (base_type != null && base_type_expr != null) {
2187                                         ConstraintChecker.Check (this, base_type, base_type_expr.Location);
2188                                 }
2189
2190                                 if (iface_exprs != null) {
2191                                         foreach (var iface_type in iface_exprs) {
2192                                                 if (iface_type == null)
2193                                                         continue;
2194
2195                                                 ConstraintChecker.Check (this, iface_type, Location);   // TODO: Location is wrong
2196                                         }
2197                                 }
2198                         }
2199
2200                         if (all_tp_builders != null) {
2201                                 int current_starts_index = CurrentTypeParametersStartIndex;
2202                                 for (int i = 0; i < all_tp_builders.Length; i++) {
2203                                         if (i < current_starts_index) {
2204                                                 all_type_parameters[i].EmitConstraints (all_tp_builders [i]);
2205                                         } else {
2206                                                 var tp = CurrentTypeParameters [i - current_starts_index];
2207                                                 tp.CheckGenericConstraints (!IsObsolete);
2208                                                 tp.Emit ();
2209                                         }
2210                                 }
2211                         }
2212
2213                         if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
2214                                 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (TypeBuilder);
2215
2216 #if STATIC
2217                         if ((TypeBuilder.Attributes & TypeAttributes.StringFormatMask) == 0 && Module.HasDefaultCharSet)
2218                                 TypeBuilder.__SetAttributes (TypeBuilder.Attributes | Module.DefaultCharSetType);
2219 #endif
2220
2221                         base.Emit ();
2222
2223                         for (int i = 0; i < members.Count; i++) {
2224                                 var m = members[i];
2225                                 if ((m.caching_flags & Flags.CloseTypeCreated) != 0)
2226                                         continue;
2227
2228                                 m.Emit ();
2229                         }
2230
2231                         EmitIndexerName ();
2232                         CheckAttributeClsCompliance ();
2233
2234                         if (pending != null)
2235                                 pending.VerifyPendingMethods ();
2236                 }
2237
2238
2239                 void CheckAttributeClsCompliance ()
2240                 {
2241                         if (!spec.IsAttribute || !IsExposedFromAssembly () || !Compiler.Settings.VerifyClsCompliance || !IsClsComplianceRequired ())
2242                                 return;
2243
2244                         foreach (var m in members) {
2245                                 var c = m as Constructor;
2246                                 if (c == null)
2247                                         continue;
2248
2249                                 if (c.HasCompliantArgs)
2250                                         return;
2251                         }
2252
2253                         Report.Warning (3015, 1, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2254                 }
2255
2256                 public sealed override void EmitContainer ()
2257                 {
2258                         if ((caching_flags & Flags.CloseTypeCreated) != 0)
2259                                 return;
2260
2261                         Emit ();
2262                 }
2263
2264                 public override void CloseContainer ()
2265                 {
2266                         if ((caching_flags & Flags.CloseTypeCreated) != 0)
2267                                 return;
2268
2269                         // Close base type container first to avoid TypeLoadException
2270                         if (spec.BaseType != null) {
2271                                 var btype = spec.BaseType.MemberDefinition as TypeContainer;
2272                                 if (btype != null) {
2273                                         btype.CloseContainer ();
2274
2275                                         if ((caching_flags & Flags.CloseTypeCreated) != 0)
2276                                                 return;
2277                                 }
2278                         }
2279
2280                         try {
2281                                 caching_flags |= Flags.CloseTypeCreated;
2282                                 TypeBuilder.CreateType ();
2283                         } catch (TypeLoadException) {
2284                                 //
2285                                 // This is fine, the code still created the type
2286                                 //
2287                         } catch (Exception e) {
2288                                 throw new InternalErrorException (this, e);
2289                         }
2290
2291                         base.CloseContainer ();
2292                         
2293                         containers = null;
2294                         initialized_fields = null;
2295                         initialized_static_fields = null;
2296                         type_bases = null;
2297                         OptAttributes = null;
2298                 }
2299
2300                 //
2301                 // Performs the validation on a Method's modifiers (properties have
2302                 // the same properties).
2303                 //
2304                 // TODO: Why is it not done at parse stage, move to Modifiers::Check
2305                 //
2306                 public bool MethodModifiersValid (MemberCore mc)
2307                 {
2308                         const Modifiers vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2309                         const Modifiers nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2310                         bool ok = true;
2311                         var flags = mc.ModFlags;
2312                         
2313                         //
2314                         // At most one of static, virtual or override
2315                         //
2316                         if ((flags & Modifiers.STATIC) != 0){
2317                                 if ((flags & vao) != 0){
2318                                         Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
2319                                                 mc.GetSignatureForError ());
2320                                         ok = false;
2321                                 }
2322                         }
2323
2324                         if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2325                                 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
2326                                         mc.GetSignatureForError ());
2327                                 ok = false;
2328                         }
2329
2330                         //
2331                         // If the declaration includes the abstract modifier, then the
2332                         // declaration does not include static, virtual or extern
2333                         //
2334                         if ((flags & Modifiers.ABSTRACT) != 0){
2335                                 if ((flags & Modifiers.EXTERN) != 0){
2336                                         Report.Error (
2337                                                 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
2338                                         ok = false;
2339                                 }
2340
2341                                 if ((flags & Modifiers.SEALED) != 0) {
2342                                         Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2343                                         ok = false;
2344                                 }
2345
2346                                 if ((flags & Modifiers.VIRTUAL) != 0){
2347                                         Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
2348                                         ok = false;
2349                                 }
2350
2351                                 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2352                                         Report.SymbolRelatedToPreviousError (this);
2353                                         Report.Error (513, mc.Location, "`{0}' is abstract but it is declared in the non-abstract class `{1}'",
2354                                                 mc.GetSignatureForError (), GetSignatureForError ());
2355                                         ok = false;
2356                                 }
2357                         }
2358
2359                         if ((flags & Modifiers.PRIVATE) != 0){
2360                                 if ((flags & vao) != 0){
2361                                         Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
2362                                         ok = false;
2363                                 }
2364                         }
2365
2366                         if ((flags & Modifiers.SEALED) != 0){
2367                                 if ((flags & Modifiers.OVERRIDE) == 0){
2368                                         Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2369                                         ok = false;
2370                                 }
2371                         }
2372
2373                         return ok;
2374                 }
2375
2376                 protected override bool VerifyClsCompliance ()
2377                 {
2378                         if (!base.VerifyClsCompliance ())
2379                                 return false;
2380
2381                         // Check all container names for user classes
2382                         if (Kind != MemberKind.Delegate)
2383                                 MemberCache.VerifyClsCompliance (Definition, Report);
2384
2385                         if (BaseType != null && !BaseType.IsCLSCompliant ()) {
2386                                 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant",
2387                                         GetSignatureForError (), BaseType.GetSignatureForError ());
2388                         }
2389                         return true;
2390                 }
2391
2392                 /// <summary>
2393                 ///   Performs checks for an explicit interface implementation.  First it
2394                 ///   checks whether the `interface_type' is a base inteface implementation.
2395                 ///   Then it checks whether `name' exists in the interface type.
2396                 /// </summary>
2397                 public bool VerifyImplements (InterfaceMemberBase mb)
2398                 {
2399                         var ifaces = PartialContainer.Interfaces;
2400                         if (ifaces != null) {
2401                                 foreach (TypeSpec t in ifaces){
2402                                         if (t == mb.InterfaceType || t == null)
2403                                                 return true;
2404
2405                                         var expanded_base = t.Interfaces;
2406                                         if (expanded_base == null)
2407                                                 continue;
2408
2409                                         foreach (var bt in expanded_base) {
2410                                                 if (bt == mb.InterfaceType)
2411                                                         return true;
2412                                         }
2413                                 }
2414                         }
2415                         
2416                         Report.SymbolRelatedToPreviousError (mb.InterfaceType);
2417                         Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2418                                 mb.GetSignatureForError (), mb.InterfaceType.GetSignatureForError ());
2419                         return false;
2420                 }
2421
2422                 //
2423                 // Used for visiblity checks to tests whether this definition shares
2424                 // base type baseType, it does member-definition search
2425                 //
2426                 public bool IsBaseTypeDefinition (TypeSpec baseType)
2427                 {
2428                         // RootContext check
2429                         if (TypeBuilder == null)
2430                                 return false;
2431
2432                         var type = spec;
2433                         do {
2434                                 if (type.MemberDefinition == baseType.MemberDefinition)
2435                                         return true;
2436
2437                                 type = type.BaseType;
2438                         } while (type != null);
2439
2440                         return false;
2441                 }
2442
2443                 public override bool IsClsComplianceRequired ()
2444                 {
2445                         if (IsPartialPart)
2446                                 return PartialContainer.IsClsComplianceRequired ();
2447
2448                         return base.IsClsComplianceRequired ();
2449                 }
2450
2451                 bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
2452                 {
2453                         return Module.DeclaringAssembly == assembly;
2454                 }
2455
2456                 public virtual bool IsUnmanagedType ()
2457                 {
2458                         return false;
2459                 }
2460
2461                 public void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
2462                 {
2463                         throw new NotSupportedException ("Not supported for compiled definition " + GetSignatureForError ());
2464                 }
2465
2466                 //
2467                 // Public function used to locate types.
2468                 //
2469                 // Returns: Type or null if they type can not be found.
2470                 //
2471                 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
2472                 {
2473                         FullNamedExpression e;
2474                         if (arity == 0 && Cache.TryGetValue (name, out e) && mode != LookupMode.IgnoreAccessibility)
2475                                 return e;
2476
2477                         e = null;
2478
2479                         if (arity == 0) {
2480                                 var tp = CurrentTypeParameters;
2481                                 if (tp != null) {
2482                                         TypeParameter tparam = tp.Find (name);
2483                                         if (tparam != null)
2484                                                 e = new TypeParameterExpr (tparam, Location.Null);
2485                                 }
2486                         }
2487
2488                         if (e == null) {
2489                                 TypeSpec t = LookupNestedTypeInHierarchy (name, arity);
2490
2491                                 if (t != null && (t.IsAccessible (this) || mode == LookupMode.IgnoreAccessibility))
2492                                         e = new TypeExpression (t, Location.Null);
2493                                 else {
2494                                         var errors = Compiler.Report.Errors;
2495                                         e = Parent.LookupNamespaceOrType (name, arity, mode, loc);
2496
2497                                         // TODO: LookupNamespaceOrType does more than just lookup. The result
2498                                         // cannot be cached or the error reporting won't happen
2499                                         if (errors != Compiler.Report.Errors)
2500                                                 return e;
2501                                 }
2502                         }
2503
2504                         // TODO MemberCache: How to cache arity stuff ?
2505                         if (arity == 0 && mode == LookupMode.Normal)
2506                                 Cache[name] = e;
2507
2508                         return e;
2509                 }
2510
2511                 TypeSpec LookupNestedTypeInHierarchy (string name, int arity)
2512                 {
2513                         // Has any nested type
2514                         // Does not work, because base type can have
2515                         //if (PartialContainer.Types == null)
2516                         //      return null;
2517
2518                         var container = PartialContainer.CurrentType;
2519                         return MemberCache.FindNestedType (container, name, arity, false);
2520                 }
2521
2522                 public void Mark_HasEquals ()
2523                 {
2524                         cached_method |= CachedMethods.Equals;
2525                 }
2526
2527                 public void Mark_HasGetHashCode ()
2528                 {
2529                         cached_method |= CachedMethods.GetHashCode;
2530                 }
2531
2532                 public override void WriteDebugSymbol (MonoSymbolFile file)
2533                 {
2534                         if (IsPartialPart)
2535                                 return;
2536
2537                         foreach (var m in members) {
2538                                 m.WriteDebugSymbol (file);
2539                         }
2540                 }
2541
2542                 /// <summary>
2543                 /// Method container contains Equals method
2544                 /// </summary>
2545                 public bool HasEquals {
2546                         get {
2547                                 return (cached_method & CachedMethods.Equals) != 0;
2548                         }
2549                 }
2550  
2551                 /// <summary>
2552                 /// Method container contains GetHashCode method
2553                 /// </summary>
2554                 public bool HasGetHashCode {
2555                         get {
2556                                 return (cached_method & CachedMethods.GetHashCode) != 0;
2557                         }
2558                 }
2559
2560                 public bool HasStaticFieldInitializer {
2561                         get {
2562                                 return (cached_method & CachedMethods.HasStaticFieldInitializer) != 0;
2563                         }
2564                         set {
2565                                 if (value)
2566                                         cached_method |= CachedMethods.HasStaticFieldInitializer;
2567                                 else
2568                                         cached_method &= ~CachedMethods.HasStaticFieldInitializer;
2569                         }
2570                 }
2571
2572                 public override string DocCommentHeader {
2573                         get { return "T:"; }
2574                 }
2575         }
2576
2577         public abstract class ClassOrStruct : TypeDefinition
2578         {
2579                 public const TypeAttributes StaticClassAttribute = TypeAttributes.Abstract | TypeAttributes.Sealed;
2580
2581                 SecurityType declarative_security;
2582                 protected Constructor generated_primary_constructor;
2583
2584                 protected ClassOrStruct (TypeContainer parent, MemberName name, Attributes attrs, MemberKind kind)
2585                         : base (parent, name, attrs, kind)
2586                 {
2587                 }
2588
2589                 public ToplevelBlock PrimaryConstructorBlock { get; set; }
2590
2591                 protected override TypeAttributes TypeAttr {
2592                         get {
2593                                 TypeAttributes ta = base.TypeAttr;
2594                                 if (!has_static_constructor)
2595                                         ta |= TypeAttributes.BeforeFieldInit;
2596
2597                                 if (Kind == MemberKind.Class) {
2598                                         ta |= TypeAttributes.AutoLayout | TypeAttributes.Class;
2599                                         if (IsStatic)
2600                                                 ta |= StaticClassAttribute;
2601                                 } else {
2602                                         ta |= TypeAttributes.SequentialLayout;
2603                                 }
2604
2605                                 return ta;
2606                         }
2607                 }
2608
2609                 public override void AddNameToContainer (MemberCore symbol, string name)
2610                 {
2611                         if (!(symbol is Constructor) && symbol.MemberName.Name == MemberName.Name) {
2612                                 if (symbol is TypeParameter) {
2613                                         Report.Error (694, symbol.Location,
2614                                                 "Type parameter `{0}' has same name as containing type, or method",
2615                                                 symbol.GetSignatureForError ());
2616                                         return;
2617                                 }
2618
2619                                 InterfaceMemberBase imb = symbol as InterfaceMemberBase;
2620                                 if (imb == null || !imb.IsExplicitImpl) {
2621                                         Report.SymbolRelatedToPreviousError (this);
2622                                         Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
2623                                                 symbol.GetSignatureForError ());
2624                                         return;
2625                                 }
2626                         }
2627
2628                         base.AddNameToContainer (symbol, name);
2629                 }
2630
2631                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2632                 {
2633                         if (a.IsValidSecurityAttribute ()) {
2634                                 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
2635                                 return;
2636                         }
2637
2638                         if (a.Type == pa.StructLayout) {
2639                                 PartialContainer.HasStructLayout = true;
2640                                 if (a.IsExplicitLayoutKind ())
2641                                         PartialContainer.HasExplicitLayout = true;
2642                         }
2643
2644                         if (a.Type == pa.Dynamic) {
2645                                 a.Error_MisusedDynamicAttribute ();
2646                                 return;
2647                         }
2648
2649                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2650                 }
2651
2652                 /// <summary>
2653                 /// Defines the default constructors 
2654                 /// </summary>
2655                 protected virtual Constructor DefineDefaultConstructor (bool is_static)
2656                 {
2657                         // The default instance constructor is public
2658                         // If the class is abstract, the default constructor is protected
2659                         // The default static constructor is private
2660
2661                         Modifiers mods;
2662                         ParametersCompiled parameters = null;
2663                         if (is_static) {
2664                                 mods = Modifiers.STATIC | Modifiers.PRIVATE;
2665                                 parameters = ParametersCompiled.EmptyReadOnlyParameters;
2666                         } else {
2667                                 mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
2668                                 parameters = PrimaryConstructorParameters ?? ParametersCompiled.EmptyReadOnlyParameters;
2669                         }
2670
2671                         var c = new Constructor (this, MemberName.Name, mods, null, parameters, Location);
2672                         if (Kind == MemberKind.Class)
2673                                 c.Initializer = new GeneratedBaseInitializer (Location, PrimaryConstructorBaseArguments);
2674
2675                         if (PrimaryConstructorParameters != null && !is_static) {
2676                                 c.IsPrimaryConstructor = true;
2677                                 c.caching_flags |= Flags.MethodOverloadsExist;
2678                         }
2679                         
2680                         AddConstructor (c, true);
2681                         if (PrimaryConstructorBlock == null) {
2682                                 c.Block = new ToplevelBlock (Compiler, parameters, Location) {
2683                                         IsCompilerGenerated = true
2684                                 };
2685                         } else {
2686                                 c.Block = PrimaryConstructorBlock;
2687                         }
2688
2689                         return c;
2690                 }
2691
2692                 protected override bool DoDefineMembers ()
2693                 {
2694                         CheckProtectedModifier ();
2695
2696                         if (PrimaryConstructorParameters != null) {
2697
2698                                 foreach (Parameter p in PrimaryConstructorParameters.FixedParameters) {
2699                                         if (p.Name == MemberName.Name) {
2700                                                 Report.Error (8039, p.Location, "Primary constructor of type `{0}' has parameter of same name as containing type",
2701                                                         GetSignatureForError ());
2702                                         }
2703
2704                                         if (CurrentTypeParameters != null) {
2705                                                 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
2706                                                         var tp = CurrentTypeParameters [i];
2707                                                         if (p.Name == tp.Name) {
2708                                                                 Report.Error (8038, p.Location, "Primary constructor of type `{0}' has parameter of same name as type parameter `{1}'",
2709                                                                         GetSignatureForError (), p.GetSignatureForError ());
2710                                                         }
2711                                                 }
2712                                         }
2713                                 }
2714                         }
2715
2716                         base.DoDefineMembers ();
2717
2718                         return true;
2719                 }
2720
2721                 public override void PrepareEmit ()
2722                 {
2723                         var s = this as Struct;
2724                         if (s == null || !s.HasUnmanagedCheckDone) {
2725                                 for (int i = 0; i < Members.Count; ++i) {
2726                                         var f = Members [i] as Field;
2727                                         if (f == null || f.MemberType == null || !f.MemberType.IsPointer)
2728                                                 continue;
2729
2730                                         TypeManager.VerifyUnmanaged (Module, f.MemberType, f.Location);
2731                                 }
2732                         }
2733
2734                         base.PrepareEmit ();
2735                 }
2736
2737                 public override void Emit ()
2738                 {
2739                         if (!has_static_constructor && HasStaticFieldInitializer) {
2740                                 var c = DefineDefaultConstructor (true);
2741                                 c.Define ();
2742                         }
2743
2744                         base.Emit ();
2745
2746                         if (declarative_security != null) {
2747                                 foreach (var de in declarative_security) {
2748 #if STATIC
2749                                         TypeBuilder.__AddDeclarativeSecurity (de);
2750 #else
2751                                         TypeBuilder.AddDeclarativeSecurity (de.Key, de.Value);
2752 #endif
2753                                 }
2754                         }
2755                 }
2756         }
2757
2758
2759         public sealed class Class : ClassOrStruct
2760         {
2761                 const Modifiers AllowedModifiers =
2762                         Modifiers.NEW |
2763                         Modifiers.PUBLIC |
2764                         Modifiers.PROTECTED |
2765                         Modifiers.INTERNAL |
2766                         Modifiers.PRIVATE |
2767                         Modifiers.ABSTRACT |
2768                         Modifiers.SEALED |
2769                         Modifiers.STATIC |
2770                         Modifiers.UNSAFE;
2771                         
2772                 public Class (TypeContainer parent, MemberName name, Modifiers mod, Attributes attrs)
2773                         : base (parent, name, attrs, MemberKind.Class)
2774                 {
2775                         var accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2776                         this.ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod, accmods, Location, Report);
2777                         spec = new TypeSpec (Kind, null, this, null, ModFlags);
2778                 }
2779
2780                 public override void Accept (StructuralVisitor visitor)
2781                 {
2782                         visitor.Visit (this);
2783                 }
2784
2785                 public override void SetBaseTypes (List<FullNamedExpression> baseTypes)
2786                 {
2787                         var pmn = MemberName;
2788                         if (pmn.Name == "Object" && !pmn.IsGeneric && Parent.MemberName.Name == "System" && Parent.MemberName.Left == null)
2789                                 Report.Error (537, Location,
2790                                         "The class System.Object cannot have a base class or implement an interface.");
2791
2792                         base.SetBaseTypes (baseTypes);
2793                 }
2794
2795                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2796                 {
2797                         if (a.Type == pa.AttributeUsage) {
2798                                 if (!BaseType.IsAttribute && spec.BuiltinType != BuiltinTypeSpec.Type.Attribute) {
2799                                         Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2800                                 }
2801                         }
2802
2803                         if (a.Type == pa.Conditional && !BaseType.IsAttribute) {
2804                                 Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2805                                 return;
2806                         }
2807
2808                         if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
2809                                 a.Error_MissingGuidAttribute ();
2810                                 return;
2811                         }
2812
2813                         if (a.Type == pa.Extension) {
2814                                 a.Error_MisusedExtensionAttribute ();
2815                                 return;
2816                         }
2817
2818                         if (a.Type.IsConditionallyExcluded (this))
2819                                 return;
2820
2821                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2822                 }
2823
2824                 public override AttributeTargets AttributeTargets {
2825                         get {
2826                                 return AttributeTargets.Class;
2827                         }
2828                 }
2829
2830                 protected override bool DoDefineMembers ()
2831                 {
2832                         if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2833                                 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2834                         }
2835
2836                         if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2837                                 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2838                         }
2839
2840                         if (IsStatic) {
2841                                 if (PrimaryConstructorParameters != null) {
2842                                         Report.Error (-800, Location, "`{0}': Static classes cannot have primary constructor", GetSignatureForError ());
2843                                         PrimaryConstructorParameters = null;
2844                                 }
2845
2846                                 foreach (var m in Members) {
2847                                         if (m is Operator) {
2848                                                 Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2849                                                 continue;
2850                                         }
2851
2852                                         if (m is Destructor) {
2853                                                 Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2854                                                 continue;
2855                                         }
2856
2857                                         if (m is Indexer) {
2858                                                 Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2859                                                 continue;
2860                                         }
2861
2862                                         if ((m.ModFlags & Modifiers.STATIC) != 0 || m is TypeContainer)
2863                                                 continue;
2864
2865                                         if (m is Constructor) {
2866                                                 Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2867                                                 continue;
2868                                         }
2869
2870                                         Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2871                                 }
2872                         } else {
2873                                 if (!PartialContainer.HasInstanceConstructor || PrimaryConstructorParameters != null)
2874                                         generated_primary_constructor = DefineDefaultConstructor (false);
2875                         }
2876
2877                         return base.DoDefineMembers ();
2878                 }
2879
2880                 public override void Emit ()
2881                 {
2882                         base.Emit ();
2883
2884                         if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
2885                                 Module.PredefinedAttributes.Extension.EmitAttribute (TypeBuilder);
2886
2887                         if (base_type != null && base_type.HasDynamicElement) {
2888                                 Module.PredefinedAttributes.Dynamic.EmitAttribute (TypeBuilder, base_type, Location);
2889                         }
2890                 }
2891
2892                 public override void GetCompletionStartingWith (string prefix, List<string> results)
2893                 {
2894                         base.GetCompletionStartingWith (prefix, results);
2895
2896                         var bt = base_type;
2897                         while (bt != null) {
2898                                 results.AddRange (MemberCache.GetCompletitionMembers (this, bt, prefix).Where (l => l.IsStatic).Select (l => l.Name));
2899                                 bt = bt.BaseType;
2900                         }
2901                 }
2902
2903                 protected override TypeSpec[] ResolveBaseTypes (out FullNamedExpression base_class)
2904                 {
2905                         var ifaces = base.ResolveBaseTypes (out base_class);
2906
2907                         if (base_class == null) {
2908                                 if (spec.BuiltinType != BuiltinTypeSpec.Type.Object)
2909                                         base_type = Compiler.BuiltinTypes.Object;
2910                         } else {
2911                                 if (base_type.IsGenericParameter){
2912                                         Report.Error (689, base_class.Location, "`{0}': Cannot derive from type parameter `{1}'",
2913                                                 GetSignatureForError (), base_type.GetSignatureForError ());
2914                                 } else if (base_type.IsStatic) {
2915                                         Report.SymbolRelatedToPreviousError (base_type);
2916                                         Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
2917                                                 GetSignatureForError (), base_type.GetSignatureForError ());
2918                                 } else if (base_type.IsSealed) {
2919                                         Report.SymbolRelatedToPreviousError (base_type);
2920                                         Report.Error (509, Location, "`{0}': cannot derive from sealed type `{1}'",
2921                                                 GetSignatureForError (), base_type.GetSignatureForError ());
2922                                 } else if (PartialContainer.IsStatic && base_type.BuiltinType != BuiltinTypeSpec.Type.Object) {
2923                                         Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object",
2924                                                 GetSignatureForError (), base_type.GetSignatureForError ());
2925                                 }
2926
2927                                 switch (base_type.BuiltinType) {
2928                                 case BuiltinTypeSpec.Type.Enum:
2929                                 case BuiltinTypeSpec.Type.ValueType:
2930                                 case BuiltinTypeSpec.Type.MulticastDelegate:
2931                                 case BuiltinTypeSpec.Type.Delegate:
2932                                 case BuiltinTypeSpec.Type.Array:
2933                                         if (!(spec is BuiltinTypeSpec)) {
2934                                                 Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
2935                                                         GetSignatureForError (), base_type.GetSignatureForError ());
2936
2937                                                 base_type = Compiler.BuiltinTypes.Object;
2938                                         }
2939                                         break;
2940                                 }
2941
2942                                 if (!IsAccessibleAs (base_type)) {
2943                                         Report.SymbolRelatedToPreviousError (base_type);
2944                                         Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'",
2945                                                 base_type.GetSignatureForError (), GetSignatureForError ());
2946                                 }
2947                         }
2948
2949                         if (PartialContainer.IsStatic && ifaces != null) {
2950                                 foreach (var t in ifaces)
2951                                         Report.SymbolRelatedToPreviousError (t);
2952                                 Report.Error (714, Location, "Static class `{0}' cannot implement interfaces", GetSignatureForError ());
2953                         }
2954
2955                         return ifaces;
2956                 }
2957
2958                 /// Search for at least one defined condition in ConditionalAttribute of attribute class
2959                 /// Valid only for attribute classes.
2960                 public override string[] ConditionalConditions ()
2961                 {
2962                         if ((caching_flags & (Flags.Excluded_Undetected | Flags.Excluded)) == 0)
2963                                 return null;
2964
2965                         caching_flags &= ~Flags.Excluded_Undetected;
2966
2967                         if (OptAttributes == null)
2968                                 return null;
2969
2970                         Attribute[] attrs = OptAttributes.SearchMulti (Module.PredefinedAttributes.Conditional);
2971                         if (attrs == null)
2972                                 return null;
2973
2974                         string[] conditions = new string[attrs.Length];
2975                         for (int i = 0; i < conditions.Length; ++i)
2976                                 conditions[i] = attrs[i].GetConditionalAttributeValue ();
2977
2978                         caching_flags |= Flags.Excluded;
2979                         return conditions;
2980                 }
2981         }
2982
2983         public sealed class Struct : ClassOrStruct
2984         {
2985                 bool is_unmanaged, has_unmanaged_check_done, requires_delayed_unmanagedtype_check;
2986                 bool InTransit;
2987
2988                 // <summary>
2989                 //   Modifiers allowed in a struct declaration
2990                 // </summary>
2991                 const Modifiers AllowedModifiers =
2992                         Modifiers.NEW       |
2993                         Modifiers.PUBLIC    |
2994                         Modifiers.PROTECTED |
2995                         Modifiers.INTERNAL  |
2996                         Modifiers.UNSAFE    |
2997                         Modifiers.PRIVATE;
2998
2999                 public Struct (TypeContainer parent, MemberName name, Modifiers mod, Attributes attrs)
3000                         : base (parent, name, attrs, MemberKind.Struct)
3001                 {
3002                         var accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;                      
3003                         this.ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod, accmods, Location, Report) | Modifiers.SEALED ;
3004                         spec = new TypeSpec (Kind, null, this, null, ModFlags);
3005                 }
3006
3007                 public override AttributeTargets AttributeTargets {
3008                         get {
3009                                 return AttributeTargets.Struct;
3010                         }
3011                 }
3012
3013                 public override void Accept (StructuralVisitor visitor)
3014                 {
3015                         visitor.Visit (this);
3016                 }
3017
3018                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
3019                 {
3020                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
3021
3022                         //
3023                         // When struct constains fixed fixed and struct layout has explicitly
3024                         // set CharSet, its value has to be propagated to compiler generated
3025                         // fixed types
3026                         //
3027                         if (a.Type == pa.StructLayout) {
3028                                 var value = a.GetNamedValue ("CharSet");
3029                                 if (value == null)
3030                                         return;
3031
3032                                 for (int i = 0; i < Members.Count; ++i) {
3033                                         FixedField ff = Members [i] as FixedField;
3034                                         if (ff == null)
3035                                                 continue;
3036
3037                                         ff.CharSetValue = (CharSet) System.Enum.Parse (typeof (CharSet), value.GetValue ().ToString ());
3038                                 }
3039                         }
3040                 }
3041
3042                 bool CheckStructCycles ()
3043                 {
3044                         if (InTransit)
3045                                 return false;
3046
3047                         InTransit = true;
3048                         foreach (var member in Members) {
3049                                 var field = member as Field;
3050                                 if (field == null)
3051                                         continue;
3052
3053                                 TypeSpec ftype = field.Spec.MemberType;
3054                                 if (!ftype.IsStruct)
3055                                         continue;
3056
3057                                 if (ftype is BuiltinTypeSpec)
3058                                         continue;
3059
3060                                 foreach (var targ in ftype.TypeArguments) {
3061                                         if (!CheckFieldTypeCycle (targ)) {
3062                                                 Report.Error (523, field.Location,
3063                                                         "Struct member `{0}' of type `{1}' causes a cycle in the struct layout",
3064                                                         field.GetSignatureForError (), ftype.GetSignatureForError ());
3065                                                 break;
3066                                         }
3067                                 }
3068
3069                                 //
3070                                 // Static fields of exactly same type are allowed
3071                                 //
3072                                 if (field.IsStatic && ftype == CurrentType)
3073                                         continue;
3074
3075                                 if (!CheckFieldTypeCycle (ftype)) {
3076                                         Report.Error (523, field.Location,
3077                                                 "Struct member `{0}' of type `{1}' causes a cycle in the struct layout",
3078                                                 field.GetSignatureForError (), ftype.GetSignatureForError ());
3079                                         break;
3080                                 }
3081                         }
3082
3083                         InTransit = false;
3084                         return true;
3085                 }
3086
3087                 static bool CheckFieldTypeCycle (TypeSpec ts)
3088                 {
3089                         var fts = ts.MemberDefinition as Struct;
3090                         if (fts == null)
3091                                 return true;
3092
3093                         return fts.CheckStructCycles ();
3094                 }
3095
3096                 protected override bool DoDefineMembers ()
3097                 {
3098                         var res = base.DoDefineMembers ();
3099
3100                         if (PrimaryConstructorParameters != null || (initialized_fields != null && !HasUserDefaultConstructor ())) {
3101                                 generated_primary_constructor = DefineDefaultConstructor (false);
3102                                 generated_primary_constructor.Define ();
3103                         }
3104
3105                         return res;
3106                 }
3107
3108                 public override void Emit ()
3109                 {
3110                         CheckStructCycles ();
3111
3112                         base.Emit ();
3113                 }
3114
3115                 public bool HasUnmanagedCheckDone {
3116                         get {
3117                                 return has_unmanaged_check_done;
3118                         }
3119                 }
3120
3121                 bool HasUserDefaultConstructor ()
3122                 {
3123                         foreach (var m in PartialContainer.Members) {
3124                                 var c = m as Constructor;
3125                                 if (c == null)
3126                                         continue;
3127
3128                                 if (!c.IsStatic && c.ParameterInfo.IsEmpty)
3129                                         return true;
3130                         }
3131
3132                         return false;
3133                 }
3134
3135                 public override bool IsUnmanagedType ()
3136                 {
3137                         if (has_unmanaged_check_done)
3138                                 return is_unmanaged;
3139
3140                         if (requires_delayed_unmanagedtype_check)
3141                                 return true;
3142
3143                         var parent_def = Parent.PartialContainer;
3144                         if (parent_def != null && parent_def.IsGenericOrParentIsGeneric) {
3145                                 has_unmanaged_check_done = true;
3146                                 return false;
3147                         }
3148
3149                         if (first_nonstatic_field != null) {
3150                                 requires_delayed_unmanagedtype_check = true;
3151
3152                                 foreach (var member in Members) {
3153                                         var f = member as Field;
3154                                         if (f == null)
3155                                                 continue;
3156
3157                                         if (f.IsStatic)
3158                                                 continue;
3159
3160                                         // It can happen when recursive unmanaged types are defined
3161                                         // struct S { S* s; }
3162                                         TypeSpec mt = f.MemberType;
3163                                         if (mt == null) {
3164                                                 return true;
3165                                         }
3166
3167                                         if (mt.IsUnmanaged)
3168                                                 continue;
3169
3170                                         has_unmanaged_check_done = true;
3171                                         return false;
3172                                 }
3173
3174                                 has_unmanaged_check_done = true;
3175                         }
3176
3177                         is_unmanaged = true;
3178                         return true;
3179                 }
3180
3181                 protected override TypeSpec[] ResolveBaseTypes (out FullNamedExpression base_class)
3182                 {
3183                         var ifaces = base.ResolveBaseTypes (out base_class);
3184                         base_type = Compiler.BuiltinTypes.ValueType;
3185                         return ifaces;
3186                 }
3187         }
3188
3189         /// <summary>
3190         ///   Interfaces
3191         /// </summary>
3192         public sealed class Interface : TypeDefinition {
3193
3194                 /// <summary>
3195                 ///   Modifiers allowed in a class declaration
3196                 /// </summary>
3197                 const Modifiers AllowedModifiers =
3198                         Modifiers.NEW       |
3199                         Modifiers.PUBLIC    |
3200                         Modifiers.PROTECTED |
3201                         Modifiers.INTERNAL  |
3202                         Modifiers.UNSAFE    |
3203                         Modifiers.PRIVATE;
3204
3205                 public Interface (TypeContainer parent, MemberName name, Modifiers mod, Attributes attrs)
3206                         : base (parent, name, attrs, MemberKind.Interface)
3207                 {
3208                         var accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;
3209
3210                         this.ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod, accmods, name.Location, Report);
3211                         spec = new TypeSpec (Kind, null, this, null, ModFlags);
3212                 }
3213
3214                 #region Properties
3215
3216                 public override AttributeTargets AttributeTargets {
3217                         get {
3218                                 return AttributeTargets.Interface;
3219                         }
3220                 }
3221
3222                 protected override TypeAttributes TypeAttr {
3223                         get {
3224                                 const TypeAttributes DefaultTypeAttributes =
3225                                         TypeAttributes.AutoLayout |
3226                                         TypeAttributes.Abstract |
3227                                         TypeAttributes.Interface;
3228
3229                                 return base.TypeAttr | DefaultTypeAttributes;
3230                         }
3231                 }
3232
3233                 #endregion
3234
3235                 public override void Accept (StructuralVisitor visitor)
3236                 {
3237                         visitor.Visit (this);
3238                 }
3239
3240                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
3241                 {
3242                         if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
3243                                 a.Error_MissingGuidAttribute ();
3244                                 return;
3245                         }
3246
3247                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
3248                 }
3249
3250                 protected override bool VerifyClsCompliance ()
3251                 {
3252                         if (!base.VerifyClsCompliance ())
3253                                 return false;
3254
3255                         if (iface_exprs != null) {
3256                                 foreach (var iface in iface_exprs) {
3257                                         if (iface.IsCLSCompliant ())
3258                                                 continue;
3259
3260                                         Report.SymbolRelatedToPreviousError (iface);
3261                                         Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
3262                                                 GetSignatureForError (), iface.GetSignatureForError ());
3263                                 }
3264                         }
3265
3266                         return true;
3267                 }
3268         }
3269
3270         public abstract class InterfaceMemberBase : MemberBase
3271         {
3272                 //
3273                 // Common modifiers allowed in a class declaration
3274                 //
3275                 protected const Modifiers AllowedModifiersClass =
3276                         Modifiers.NEW |
3277                         Modifiers.PUBLIC |
3278                         Modifiers.PROTECTED |
3279                         Modifiers.INTERNAL |
3280                         Modifiers.PRIVATE |
3281                         Modifiers.STATIC |
3282                         Modifiers.VIRTUAL |
3283                         Modifiers.SEALED |
3284                         Modifiers.OVERRIDE |
3285                         Modifiers.ABSTRACT |
3286                         Modifiers.UNSAFE |
3287                         Modifiers.EXTERN;
3288
3289                 //
3290                 // Common modifiers allowed in a struct declaration
3291                 //
3292                 protected const Modifiers AllowedModifiersStruct =
3293                         Modifiers.NEW |
3294                         Modifiers.PUBLIC |
3295                         Modifiers.PROTECTED |
3296                         Modifiers.INTERNAL |
3297                         Modifiers.PRIVATE |
3298                         Modifiers.STATIC |
3299                         Modifiers.OVERRIDE |
3300                         Modifiers.UNSAFE |
3301                         Modifiers.EXTERN;
3302
3303                 //
3304                 // Common modifiers allowed in a interface declaration
3305                 //
3306                 protected const Modifiers AllowedModifiersInterface =
3307                         Modifiers.NEW |
3308                         Modifiers.UNSAFE;
3309
3310                 //
3311                 // Whether this is an interface member.
3312                 //
3313                 public bool IsInterface;
3314
3315                 //
3316                 // If true, this is an explicit interface implementation
3317                 //
3318                 public readonly bool IsExplicitImpl;
3319
3320                 protected bool is_external_implementation;
3321
3322                 //
3323                 // The interface type we are explicitly implementing
3324                 //
3325                 public TypeSpec InterfaceType;
3326
3327                 //
3328                 // The method we're overriding if this is an override method.
3329                 //
3330                 protected MethodSpec base_method;
3331
3332                 readonly Modifiers explicit_mod_flags;
3333                 public MethodAttributes flags;
3334
3335                 protected InterfaceMemberBase (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name, Attributes attrs)
3336                         : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE, name, attrs)
3337                 {
3338                         IsInterface = parent.Kind == MemberKind.Interface;
3339                         IsExplicitImpl = (MemberName.ExplicitInterface != null);
3340                         explicit_mod_flags = mod;
3341                 }
3342
3343                 public abstract Variance ExpectedMemberTypeVariance { get; }
3344                 
3345                 protected override bool CheckBase ()
3346                 {
3347                         if (!base.CheckBase ())
3348                                 return false;
3349
3350                         if ((caching_flags & Flags.MethodOverloadsExist) != 0)
3351                                 CheckForDuplications ();
3352                         
3353                         if (IsExplicitImpl)
3354                                 return true;
3355
3356                         // For System.Object only
3357                         if (Parent.BaseType == null)
3358                                 return true;
3359
3360                         MemberSpec candidate;
3361                         bool overrides = false;
3362                         var base_member = FindBaseMember (out candidate, ref overrides);
3363
3364                         if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3365                                 if (base_member == null) {
3366                                         if (candidate == null) {
3367                                                 if (this is Method && ((Method)this).ParameterInfo.IsEmpty && MemberName.Name == Destructor.MetadataName && MemberName.Arity == 0) {
3368                                                         Report.Error (249, Location, "Do not override `{0}'. Use destructor syntax instead",
3369                                                                 "object.Finalize()");
3370                                                 } else {
3371                                                         Report.Error (115, Location, "`{0}' is marked as an override but no suitable {1} found to override",
3372                                                                 GetSignatureForError (), SimpleName.GetMemberType (this));
3373                                                 }
3374                                         } else {
3375                                                 Report.SymbolRelatedToPreviousError (candidate);
3376                                                 if (this is Event)
3377                                                         Report.Error (72, Location, "`{0}': cannot override because `{1}' is not an event",
3378                                                                 GetSignatureForError (), TypeManager.GetFullNameSignature (candidate));
3379                                                 else if (this is PropertyBase)
3380                                                         Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property",
3381                                                                 GetSignatureForError (), TypeManager.GetFullNameSignature (candidate));
3382                                                 else
3383                                                         Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method",
3384                                                                 GetSignatureForError (), TypeManager.GetFullNameSignature (candidate));
3385                                         }
3386
3387                                         return false;
3388                                 }
3389
3390                                 //
3391                                 // Handles ambiguous overrides
3392                                 //
3393                                 if (candidate != null) {
3394                                         Report.SymbolRelatedToPreviousError (candidate);
3395                                         Report.SymbolRelatedToPreviousError (base_member);
3396
3397                                         // Get member definition for error reporting
3398                                         var m1 = MemberCache.GetMember (base_member.DeclaringType.GetDefinition (), base_member);
3399                                         var m2 = MemberCache.GetMember (candidate.DeclaringType.GetDefinition (), candidate);
3400
3401                                         Report.Error (462, Location,
3402                                                 "`{0}' cannot override inherited members `{1}' and `{2}' because they have the same signature when used in type `{3}'",
3403                                                 GetSignatureForError (), m1.GetSignatureForError (), m2.GetSignatureForError (), Parent.GetSignatureForError ());
3404                                 }
3405
3406                                 if (!CheckOverrideAgainstBase (base_member))
3407                                         return false;
3408
3409                                 ObsoleteAttribute oa = base_member.GetAttributeObsolete ();
3410                                 if (oa != null) {
3411                                         if (OptAttributes == null || !OptAttributes.Contains (Module.PredefinedAttributes.Obsolete)) {
3412                                                 Report.SymbolRelatedToPreviousError (base_member);
3413                                                 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
3414                                                         GetSignatureForError (), base_member.GetSignatureForError ());
3415                                         }
3416                                 } else {
3417                                         if (OptAttributes != null && OptAttributes.Contains (Module.PredefinedAttributes.Obsolete)) {
3418                                                 Report.SymbolRelatedToPreviousError (base_member);
3419                                                 Report.Warning (809, 1, Location, "Obsolete member `{0}' overrides non-obsolete member `{1}'",
3420                                                         GetSignatureForError (), base_member.GetSignatureForError ());
3421                                         }
3422                                 }
3423
3424                                 base_method = base_member as MethodSpec;
3425                                 return true;
3426                         }
3427
3428                         if (base_member == null && candidate != null && (!(candidate is IParametersMember) || !(this is IParametersMember)))
3429                                 base_member = candidate;
3430
3431                         if (base_member == null) {
3432                                 if ((ModFlags & Modifiers.NEW) != 0) {
3433                                         if (base_member == null) {
3434                                                 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required",
3435                                                         GetSignatureForError ());
3436                                         }
3437                                 }
3438                         } else {
3439                                 if ((ModFlags & Modifiers.NEW) == 0) {
3440                                         ModFlags |= Modifiers.NEW;
3441                                         if (!IsCompilerGenerated) {
3442                                                 Report.SymbolRelatedToPreviousError (base_member);
3443                                                 if ((base_member.Kind & MemberKind.NestedMask) == 0 && !IsInterface && (base_member.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL | Modifiers.OVERRIDE)) != 0) {
3444                                                         Report.Warning (114, 2, Location, "`{0}' hides inherited member `{1}'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword",
3445                                                                 GetSignatureForError (), base_member.GetSignatureForError ());
3446                                                 } else {
3447                                                         Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3448                                                                 GetSignatureForError (), base_member.GetSignatureForError ());
3449                                                 }
3450                                         }
3451                                 }
3452
3453                                 if (!IsInterface && base_member.IsAbstract && !overrides && !IsStatic) {
3454                                         switch (base_member.Kind) {
3455                                         case MemberKind.Event:
3456                                         case MemberKind.Indexer:
3457                                         case MemberKind.Method:
3458                                         case MemberKind.Property:
3459                                                 Report.SymbolRelatedToPreviousError (base_member);
3460                                                 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3461                                                         GetSignatureForError (), base_member.GetSignatureForError ());
3462                                                 break;
3463                                         }
3464                                 }
3465                         }
3466
3467                         return true;
3468                 }
3469
3470                 protected virtual bool CheckForDuplications ()
3471                 {
3472                         return Parent.MemberCache.CheckExistingMembersOverloads (this, ParametersCompiled.EmptyReadOnlyParameters);
3473                 }
3474
3475                 //
3476                 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3477                 // that have been defined.
3478                 //
3479                 protected virtual bool CheckOverrideAgainstBase (MemberSpec base_member)
3480                 {
3481                         bool ok = true;
3482
3483                         if ((base_member.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL | Modifiers.OVERRIDE)) == 0) {
3484                                 Report.SymbolRelatedToPreviousError (base_member);
3485                                 Report.Error (506, Location,
3486                                         "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
3487                                          GetSignatureForError (), TypeManager.CSharpSignature (base_member));
3488                                 ok = false;
3489                         }
3490
3491                         // Now we check that the overriden method is not final  
3492                         if ((base_member.Modifiers & Modifiers.SEALED) != 0) {
3493                                 Report.SymbolRelatedToPreviousError (base_member);
3494                                 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
3495                                                           GetSignatureForError (), TypeManager.CSharpSignature (base_member));
3496                                 ok = false;
3497                         }
3498
3499                         var base_member_type = ((IInterfaceMemberSpec) base_member).MemberType;
3500                         if (!TypeSpecComparer.Override.IsEqual (MemberType, base_member_type)) {
3501                                 Report.SymbolRelatedToPreviousError (base_member);
3502                                 if (this is PropertyBasedMember) {
3503                                         Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
3504                                                 GetSignatureForError (), base_member_type.GetSignatureForError (), base_member.GetSignatureForError ());
3505                                 } else {
3506                                         Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
3507                                                 GetSignatureForError (), base_member_type.GetSignatureForError (), base_member.GetSignatureForError ());
3508                                 }
3509                                 ok = false;
3510                         }
3511
3512                         return ok;
3513                 }
3514
3515                 protected static bool CheckAccessModifiers (MemberCore this_member, MemberSpec base_member)
3516                 {
3517                         var thisp = this_member.ModFlags & Modifiers.AccessibilityMask;
3518                         var base_classp = base_member.Modifiers & Modifiers.AccessibilityMask;
3519
3520                         if ((base_classp & (Modifiers.PROTECTED | Modifiers.INTERNAL)) == (Modifiers.PROTECTED | Modifiers.INTERNAL)) {
3521                                 //
3522                                 // It must be at least "protected"
3523                                 //
3524                                 if ((thisp & Modifiers.PROTECTED) == 0) {
3525                                         return false;
3526                                 }
3527
3528                                 //
3529                                 // when overriding protected internal, the method can be declared
3530                                 // protected internal only within the same assembly or assembly
3531                                 // which has InternalsVisibleTo
3532                                 //
3533                                 if ((thisp & Modifiers.INTERNAL) != 0) {
3534                                         return base_member.DeclaringType.MemberDefinition.IsInternalAsPublic (this_member.Module.DeclaringAssembly);
3535                                 }
3536
3537                                 //
3538                                 // protected overriding protected internal inside same assembly
3539                                 // requires internal modifier as well
3540                                 //
3541                                 if (base_member.DeclaringType.MemberDefinition.IsInternalAsPublic (this_member.Module.DeclaringAssembly)) {
3542                                         return false;
3543                                 }
3544
3545                                 return true;
3546                         }
3547
3548                         return thisp == base_classp;
3549                 }
3550
3551                 public override bool Define ()
3552                 {
3553                         if (IsInterface) {
3554                                 ModFlags = Modifiers.PUBLIC | Modifiers.ABSTRACT |
3555                                         Modifiers.VIRTUAL | (ModFlags & (Modifiers.UNSAFE | Modifiers.NEW));
3556
3557                                 flags = MethodAttributes.Public |
3558                                         MethodAttributes.Abstract |
3559                                         MethodAttributes.HideBySig |
3560                                         MethodAttributes.NewSlot |
3561                                         MethodAttributes.Virtual;
3562                         } else {
3563                                 Parent.PartialContainer.MethodModifiersValid (this);
3564
3565                                 flags = ModifiersExtensions.MethodAttr (ModFlags);
3566                         }
3567
3568                         if (IsExplicitImpl) {
3569                                 InterfaceType = MemberName.ExplicitInterface.ResolveAsType (Parent);
3570                                 if (InterfaceType == null)
3571                                         return false;
3572
3573                                 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3574                                         Report.Error (754, Location, "A partial method `{0}' cannot explicitly implement an interface",
3575                                                 GetSignatureForError ());
3576                                 }
3577
3578                                 if (!InterfaceType.IsInterface) {
3579                                         Report.SymbolRelatedToPreviousError (InterfaceType);
3580                                         Report.Error (538, Location, "The type `{0}' in explicit interface declaration is not an interface",
3581                                                 InterfaceType.GetSignatureForError ());
3582                                 } else {
3583                                         Parent.PartialContainer.VerifyImplements (this);
3584                                 }
3585
3586                                 Modifiers allowed_explicit = Modifiers.AllowedExplicitImplFlags;
3587                                 if (this is Method)
3588                                         allowed_explicit |= Modifiers.ASYNC;
3589
3590                                 ModifiersExtensions.Check (allowed_explicit, explicit_mod_flags, 0, Location, Report);
3591                         }
3592
3593                         return base.Define ();
3594                 }
3595
3596                 protected bool DefineParameters (ParametersCompiled parameters)
3597                 {
3598                         if (!parameters.Resolve (this))
3599                                 return false;
3600
3601                         bool error = false;
3602                         for (int i = 0; i < parameters.Count; ++i) {
3603                                 Parameter p = parameters [i];
3604
3605                                 if (p.HasDefaultValue && (IsExplicitImpl || this is Operator || (this is Indexer && parameters.Count == 1)))
3606                                         p.Warning_UselessOptionalParameter (Report);
3607
3608                                 if (p.CheckAccessibility (this))
3609                                         continue;
3610
3611                                 TypeSpec t = parameters.Types [i];
3612                                 Report.SymbolRelatedToPreviousError (t);
3613                                 if (this is Indexer)
3614                                         Report.Error (55, Location,
3615                                                       "Inconsistent accessibility: parameter type `{0}' is less accessible than indexer `{1}'",
3616                                                       t.GetSignatureForError (), GetSignatureForError ());
3617                                 else if (this is Operator)
3618                                         Report.Error (57, Location,
3619                                                       "Inconsistent accessibility: parameter type `{0}' is less accessible than operator `{1}'",
3620                                                       t.GetSignatureForError (), GetSignatureForError ());
3621                                 else
3622                                         Report.Error (51, Location,
3623                                                 "Inconsistent accessibility: parameter type `{0}' is less accessible than method `{1}'",
3624                                                 t.GetSignatureForError (), GetSignatureForError ());
3625                                 error = true;
3626                         }
3627                         return !error;
3628                 }
3629
3630                 protected override void DoMemberTypeDependentChecks ()
3631                 {
3632                         base.DoMemberTypeDependentChecks ();
3633
3634                         VarianceDecl.CheckTypeVariance (MemberType, ExpectedMemberTypeVariance, this);
3635                 }
3636
3637                 public override void Emit()
3638                 {
3639                         // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
3640                         // We are more strict than csc and report this as an error because SRE does not allow emit that
3641                         if ((ModFlags & Modifiers.EXTERN) != 0 && !is_external_implementation && (OptAttributes == null || !OptAttributes.HasResolveError ())) {
3642                                 if (this is Constructor) {
3643                                         Report.Warning (824, 1, Location,
3644                                                 "Constructor `{0}' is marked `external' but has no external implementation specified", GetSignatureForError ());
3645                                 } else {
3646                                         Report.Warning (626, 1, Location,
3647                                                 "`{0}' is marked as an external but has no DllImport attribute. Consider adding a DllImport attribute to specify the external implementation",
3648                                                 GetSignatureForError ());
3649                                 }
3650                         }
3651
3652                         base.Emit ();
3653                 }
3654
3655                 public override bool EnableOverloadChecks (MemberCore overload)
3656                 {
3657                         //
3658                         // Two members can differ in their explicit interface
3659                         // type parameter only
3660                         //
3661                         InterfaceMemberBase imb = overload as InterfaceMemberBase;
3662                         if (imb != null && imb.IsExplicitImpl) {
3663                                 if (IsExplicitImpl) {
3664                                         caching_flags |= Flags.MethodOverloadsExist;
3665                                 }
3666                                 return true;
3667                         }
3668
3669                         return IsExplicitImpl;
3670                 }
3671
3672                 protected void Error_CannotChangeAccessModifiers (MemberCore member, MemberSpec base_member)
3673                 {
3674                         var base_modifiers = base_member.Modifiers;
3675
3676                         // Remove internal modifier from types which are not internally accessible
3677                         if ((base_modifiers & Modifiers.AccessibilityMask) == (Modifiers.PROTECTED | Modifiers.INTERNAL) &&
3678                                 !base_member.DeclaringType.MemberDefinition.IsInternalAsPublic (member.Module.DeclaringAssembly))
3679                                 base_modifiers = Modifiers.PROTECTED;
3680
3681                         Report.SymbolRelatedToPreviousError (base_member);
3682                         Report.Error (507, member.Location,
3683                                 "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3684                                 member.GetSignatureForError (),
3685                                 ModifiersExtensions.AccessibilityName (base_modifiers),
3686                                 base_member.GetSignatureForError ());
3687                 }
3688
3689                 protected void Error_StaticReturnType ()
3690                 {
3691                         Report.Error (722, Location,
3692                                 "`{0}': static types cannot be used as return types",
3693                                 MemberType.GetSignatureForError ());
3694                 }
3695
3696                 /// <summary>
3697                 /// Gets base method and its return type
3698                 /// </summary>
3699                 protected virtual MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
3700                 {
3701                         return MemberCache.FindBaseMember (this, out bestCandidate, ref overrides);
3702                 }
3703
3704                 //
3705                 // The "short" name of this property / indexer / event.  This is the
3706                 // name without the explicit interface.
3707                 //
3708                 public string ShortName {
3709                         get { return MemberName.Name; }
3710                 }
3711                 
3712                 //
3713                 // Returns full metadata method name
3714                 //
3715                 public string GetFullName (MemberName name)
3716                 {
3717                         return GetFullName (name.Name);
3718                 }
3719
3720                 public string GetFullName (string name)
3721                 {
3722                         if (!IsExplicitImpl)
3723                                 return name;
3724
3725                         //
3726                         // When dealing with explicit members a full interface type
3727                         // name is added to member name to avoid possible name conflicts
3728                         //
3729                         // We use CSharpName which gets us full name with benefit of
3730                         // replacing predefined names which saves some space and name
3731                         // is still unique
3732                         //
3733                         return InterfaceType.GetSignatureForError () + "." + name;
3734                 }
3735
3736                 public override string GetSignatureForDocumentation ()
3737                 {
3738                         if (IsExplicitImpl)
3739                                 return Parent.GetSignatureForDocumentation () + "." + InterfaceType.GetSignatureForDocumentation (true) + "#" + ShortName;
3740
3741                         return Parent.GetSignatureForDocumentation () + "." + ShortName;
3742                 }
3743
3744                 public override bool IsUsed 
3745                 {
3746                         get { return IsExplicitImpl || base.IsUsed; }
3747                 }
3748
3749                 public override void SetConstraints (List<Constraints> constraints_list)
3750                 {
3751                         if (((ModFlags & Modifiers.OVERRIDE) != 0 || IsExplicitImpl)) {
3752                                 Report.Error (460, Location,
3753                                         "`{0}': Cannot specify constraints for overrides and explicit interface implementation methods",
3754                                         GetSignatureForError ());
3755                         }
3756
3757                         base.SetConstraints (constraints_list);
3758                 }
3759         }
3760
3761         public abstract class MemberBase : MemberCore
3762         {
3763                 protected FullNamedExpression type_expr;
3764                 protected TypeSpec member_type;
3765                 public new TypeDefinition Parent;
3766
3767                 protected MemberBase (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, Modifiers def_mod, MemberName name, Attributes attrs)
3768                         : base (parent, name, attrs)
3769                 {
3770                         this.Parent = parent;
3771                         this.type_expr = type;
3772
3773                         if (name != MemberName.Null)
3774                                 ModFlags = ModifiersExtensions.Check (allowed_mod, mod, def_mod, Location, Report);
3775                 }
3776
3777                 #region Properties
3778
3779                 public TypeSpec MemberType {
3780                         get {
3781                                 return member_type;
3782                         }
3783                 }
3784
3785                 public FullNamedExpression TypeExpression {
3786                         get {
3787                                 return type_expr;
3788                         }
3789                         set {
3790                                 type_expr = value;
3791                         }
3792                 }
3793
3794                 #endregion
3795
3796                 //
3797                 // Main member define entry
3798                 //
3799                 public override bool Define ()
3800                 {
3801                         DoMemberTypeIndependentChecks ();
3802
3803                         //
3804                         // Returns false only when type resolution failed
3805                         //
3806                         if (!ResolveMemberType ())
3807                                 return false;
3808
3809                         DoMemberTypeDependentChecks ();
3810                         return true;
3811                 }
3812
3813                 //
3814                 // Any type_name independent checks
3815                 //
3816                 protected virtual void DoMemberTypeIndependentChecks ()
3817                 {
3818                         if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
3819                                 (ModFlags & (Modifiers.VIRTUAL | Modifiers.ABSTRACT)) != 0) {
3820                                 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
3821                                         GetSignatureForError (), Parent.GetSignatureForError ());
3822                         }
3823                 }
3824
3825                 //
3826                 // Any type_name dependent checks
3827                 //
3828                 protected virtual void DoMemberTypeDependentChecks ()
3829                 {
3830                         // verify accessibility
3831                         if (!IsAccessibleAs (MemberType)) {
3832                                 Report.SymbolRelatedToPreviousError (MemberType);
3833                                 if (this is Property)
3834                                         Report.Error (53, Location,
3835                                                 "Inconsistent accessibility: property type `" +
3836                                                 MemberType.GetSignatureForError () + "' is less " +
3837                                                 "accessible than property `" + GetSignatureForError () + "'");
3838                                 else if (this is Indexer)
3839                                         Report.Error (54, Location,
3840                                                 "Inconsistent accessibility: indexer return type `" +
3841                                                 MemberType.GetSignatureForError () + "' is less " +
3842                                                 "accessible than indexer `" + GetSignatureForError () + "'");
3843                                 else if (this is MethodCore) {
3844                                         if (this is Operator)
3845                                                 Report.Error (56, Location,
3846                                                         "Inconsistent accessibility: return type `" +
3847                                                         MemberType.GetSignatureForError () + "' is less " +
3848                                                         "accessible than operator `" + GetSignatureForError () + "'");
3849                                         else
3850                                                 Report.Error (50, Location,
3851                                                         "Inconsistent accessibility: return type `" +
3852                                                         MemberType.GetSignatureForError () + "' is less " +
3853                                                         "accessible than method `" + GetSignatureForError () + "'");
3854                                 } else if (this is Event) {
3855                                         Report.Error (7025, Location,
3856                                                 "Inconsistent accessibility: event type `{0}' is less accessible than event `{1}'",
3857                                                 MemberType.GetSignatureForError (), GetSignatureForError ());
3858                                 } else {
3859                                         Report.Error (52, Location,
3860                                                       "Inconsistent accessibility: field type `" +
3861                                                       MemberType.GetSignatureForError () + "' is less " +
3862                                                       "accessible than field `" + GetSignatureForError () + "'");
3863                                 }
3864                         }
3865                 }
3866
3867                 protected void IsTypePermitted ()
3868                 {
3869                         if (MemberType.IsSpecialRuntimeType) {
3870                                 if (Parent is StateMachine) {
3871                                         Report.Error (4012, Location,
3872                                                 "Parameters or local variables of type `{0}' cannot be declared in async methods or iterators",
3873                                                 MemberType.GetSignatureForError ());
3874                                 } else if (Parent is HoistedStoreyClass) {
3875                                         Report.Error (4013, Location,
3876                                                 "Local variables of type `{0}' cannot be used inside anonymous methods, lambda expressions or query expressions",
3877                                                 MemberType.GetSignatureForError ());
3878                                 } else {
3879                                         Report.Error (610, Location, 
3880                                                 "Field or property cannot be of type `{0}'", MemberType.GetSignatureForError ());
3881                                 }
3882                         }
3883                 }
3884
3885                 protected virtual bool CheckBase ()
3886                 {
3887                         CheckProtectedModifier ();
3888
3889                         return true;
3890                 }
3891
3892                 public override string GetSignatureForDocumentation ()
3893                 {
3894                         return Parent.GetSignatureForDocumentation () + "." + MemberName.Basename;
3895                 }
3896
3897                 public virtual void PrepareEmit ()
3898                 {
3899                         if (member_type != null && type_expr != null)
3900                                 member_type.CheckObsoleteness (this, type_expr.Location);
3901                 }
3902
3903                 protected virtual bool ResolveMemberType ()
3904                 {
3905                         if (member_type != null)
3906                                 throw new InternalErrorException ("Multi-resolve");
3907
3908                         member_type = type_expr.ResolveAsType (this);
3909                         return member_type != null;
3910                 }
3911         }
3912 }
3913