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