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