Merge branch 'marek'
[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@seznam.cz)
7 //
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 //
10 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2004-2008 Novell, Inc
12 //
13
14 using System;
15 using System.Collections.Generic;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Runtime.CompilerServices;
19 using System.Runtime.InteropServices;
20 using System.Security;
21 using System.Security.Permissions;
22 using System.Text;
23 using System.Linq;
24
25 #if NET_2_1
26 using XmlElement = System.Object;
27 #else
28 using System.Xml;
29 #endif
30
31 using Mono.CompilerServices.SymbolWriter;
32
33 namespace Mono.CSharp {
34
35         /// <summary>
36         ///   This is the base class for structs and classes.  
37         /// </summary>
38         public abstract class TypeContainer : DeclSpace, ITypeDefinition
39         {
40                 //
41                 // Different context is needed when resolving type container base
42                 // types. Type names come from the parent scope but type parameter
43                 // names from the container scope.
44                 //
45                 public struct BaseContext : IMemberContext
46                 {
47                         TypeContainer tc;
48
49                         public BaseContext (TypeContainer tc)
50                         {
51                                 this.tc = tc;
52                         }
53
54                         #region IMemberContext Members
55
56                         public CompilerContext Compiler {
57                                 get { return tc.Compiler; }
58                         }
59
60                         public TypeSpec CurrentType {
61                                 get { return tc.Parent.CurrentType; }
62                         }
63
64                         public TypeParameter[] CurrentTypeParameters {
65                                 get { return tc.PartialContainer.CurrentTypeParameters; }
66                         }
67
68                         public MemberCore CurrentMemberDefinition {
69                                 get { return tc; }
70                         }
71
72                         public bool HasUnresolvedConstraints {
73                                 get { return true; }
74                         }
75
76                         public bool IsObsolete {
77                                 get { return tc.IsObsolete; }
78                         }
79
80                         public bool IsUnsafe {
81                                 get { return tc.IsUnsafe; }
82                         }
83
84                         public bool IsStatic {
85                                 get { return tc.IsStatic; }
86                         }
87
88                         public string GetSignatureForError ()
89                         {
90                                 return tc.GetSignatureForError ();
91                         }
92
93                         public IList<MethodSpec> LookupExtensionMethod (TypeSpec extensionType, string name, int arity, ref NamespaceEntry scope)
94                         {
95                                 return null;
96                         }
97
98                         public FullNamedExpression LookupNamespaceAlias (string name)
99                         {
100                                 return tc.Parent.LookupNamespaceAlias (name);
101                         }
102
103                         public FullNamedExpression LookupNamespaceOrType (string name, int arity, Location loc, bool ignore_cs0104)
104                         {
105                                 if (arity == 0) {
106                                         TypeParameter[] tp = CurrentTypeParameters;
107                                         if (tp != null) {
108                                                 TypeParameter t = TypeParameter.FindTypeParameter (tp, name);
109                                                 if (t != null)
110                                                         return new TypeParameterExpr (t, loc);
111                                         }
112                                 }
113
114                                 return tc.Parent.LookupNamespaceOrType (name, arity, loc, ignore_cs0104);
115                         }
116
117                         #endregion
118                 }
119
120                 [Flags]
121                 enum CachedMethods
122                 {
123                         Equals                          = 1,
124                         GetHashCode                     = 1 << 1,
125                         HasStaticFieldInitializer       = 1 << 2
126                 }
127
128
129                 // Whether this is a struct, class or interface
130                 public readonly MemberKind Kind;
131
132                 // Holds a list of classes and structures
133                 protected List<TypeContainer> types;
134
135                 List<MemberCore> ordered_explicit_member_list;
136                 List<MemberCore> ordered_member_list;
137
138                 // Holds the list of properties
139                 List<MemberCore> properties;
140
141                 // Holds the list of constructors
142                 protected List<Constructor> instance_constructors;
143
144                 // Holds the list of fields
145                 protected List<FieldBase> fields;
146
147                 // Holds a list of fields that have initializers
148                 protected List<FieldInitializer> initialized_fields;
149
150                 // Holds a list of static fields that have initializers
151                 protected List<FieldInitializer> initialized_static_fields;
152
153                 // Holds the list of constants
154                 protected List<MemberCore> constants;
155
156                 // Holds the methods.
157                 List<MemberCore> methods;
158
159                 // Holds the events
160                 protected List<MemberCore> events;
161
162                 // Holds the indexers
163                 List<MemberCore> indexers;
164
165                 // Holds the operators
166                 List<MemberCore> operators;
167
168                 // Holds the compiler generated classes
169                 List<CompilerGeneratedClass> compiler_generated;
170
171                 Dictionary<MethodSpec, Method> hoisted_base_call_proxies;
172
173                 //
174                 // Pointers to the default constructor and the default static constructor
175                 //
176                 protected Constructor default_constructor;
177                 protected Constructor default_static_constructor;
178
179                 //
180                 // Points to the first non-static field added to the container.
181                 //
182                 // This is an arbitrary choice.  We are interested in looking at _some_ non-static field,
183                 // and the first one's as good as any.
184                 //
185                 FieldBase first_nonstatic_field = null;
186
187                 //
188                 // This one is computed after we can distinguish interfaces
189                 // from classes from the arraylist `type_bases' 
190                 //
191                 protected TypeSpec base_type;
192                 protected TypeExpr base_type_expr;
193                 protected TypeExpr[] iface_exprs;
194
195                 protected List<FullNamedExpression> type_bases;
196
197                 bool members_defined;
198                 bool members_defined_ok;
199                 bool type_defined;
200
201                 TypeContainer InTransit;
202
203                 GenericTypeParameterBuilder[] all_tp_builders;
204
205                 public const string DefaultIndexerName = "Item";
206
207                 private bool seen_normal_indexers = false;
208                 private string indexer_name = DefaultIndexerName;
209                 protected bool requires_delayed_unmanagedtype_check;
210                 bool error;
211
212                 private CachedMethods cached_method;
213
214                 protected TypeSpec spec;
215                 TypeSpec current_type;
216
217                 List<TypeContainer> partial_parts;
218
219                 /// <remarks>
220                 ///  The pending methods that need to be implemented
221                 //   (interfaces or abstract methods)
222                 /// </remarks>
223                 PendingImplementation pending;
224
225                 public TypeContainer (NamespaceEntry ns, DeclSpace parent, MemberName name,
226                                       Attributes attrs, MemberKind kind)
227                         : base (ns, parent, name, attrs)
228                 {
229                         if (parent != null && parent.NamespaceEntry != ns)
230                                 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
231
232                         this.Kind = kind;
233                         this.PartialContainer = this;
234                 }
235
236                 #region Properties
237
238                 public override TypeSpec CurrentType {
239                         get {
240                                 if (current_type == null) {
241                                         if (IsGeneric) {
242                                                 //
243                                                 // Switch to inflated version as it's used by all expressions
244                                                 //
245                                                 var targs = CurrentTypeParameters == null ? TypeSpec.EmptyTypes : CurrentTypeParameters.Select (l => l.Type).ToArray ();
246                                                 current_type = spec.MakeGenericType (targs);
247                                         } else {
248                                                 current_type = spec;
249                                         }
250                                 }
251
252                                 return current_type;
253                         }
254                 }
255
256                 public override TypeParameter[] CurrentTypeParameters {
257                         get {
258                                 return PartialContainer.type_params;
259                         }
260                 }
261
262                 int CurrentTypeParametersStartIndex {
263                         get {
264                                 int total = all_tp_builders.Length;
265                                 if (CurrentTypeParameters != null) {
266                                         return total - CurrentTypeParameters.Length;
267                                 }
268                                 return total;
269                         }
270                 }
271
272                 public TypeSpec Definition {
273                         get {
274                                 return spec;
275                         }
276                 }
277
278                 public bool HasMembersDefined {
279                         get {
280                                 return members_defined;
281                         }
282                 }
283
284                 #endregion
285
286                 public bool AddMember (MemberCore symbol)
287                 {
288                         return AddToContainer (symbol, symbol.MemberName.Basename);
289                 }
290
291                 public bool AddMember (MemberCore symbol, string name)
292                 {
293                         return AddToContainer (symbol, name);
294                 }
295
296                 protected virtual bool AddMemberType (TypeContainer ds)
297                 {
298                         return AddToContainer (ds, ds.Basename);
299                 }
300
301                 protected virtual void RemoveMemberType (DeclSpace ds)
302                 {
303                         RemoveFromContainer (ds.Basename);
304                 }
305
306                 public void AddConstant (Const constant)
307                 {
308                         if (!AddMember (constant))
309                                 return;
310
311                         if (constants == null)
312                                 constants = new List<MemberCore> ();
313                         
314                         constants.Add (constant);
315                 }
316
317                 public TypeContainer AddTypeContainer (TypeContainer tc)
318                 {
319                         if (!AddMemberType (tc))
320                                 return tc;
321
322                         if (types == null)
323                                 types = new List<TypeContainer> ();
324
325                         types.Add (tc);
326                         return tc;
327                 }
328
329                 public virtual TypeContainer AddPartial (TypeContainer next_part)
330                 {
331                         return AddPartial (next_part, next_part.Basename);
332                 }
333
334                 protected TypeContainer AddPartial (TypeContainer next_part, string name)
335                 {
336                         next_part.ModFlags |= Modifiers.PARTIAL;
337                         TypeContainer tc = GetDefinition (name) as TypeContainer;
338                         if (tc == null)
339                                 return AddTypeContainer (next_part);
340
341                         if ((tc.ModFlags & Modifiers.PARTIAL) == 0) {
342                                 Report.SymbolRelatedToPreviousError (next_part);
343                                 Error_MissingPartialModifier (tc);
344                         }
345
346                         if (tc.Kind != next_part.Kind) {
347                                 Report.SymbolRelatedToPreviousError (tc);
348                                 Report.Error (261, next_part.Location,
349                                         "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
350                                         next_part.GetSignatureForError ());
351                         }
352
353                         if ((tc.ModFlags & Modifiers.AccessibilityMask) != (next_part.ModFlags & Modifiers.AccessibilityMask) &&
354                                 ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0 &&
355                                  (next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0)) {
356                                 Report.SymbolRelatedToPreviousError (tc);
357                                 Report.Error (262, next_part.Location,
358                                         "Partial declarations of `{0}' have conflicting accessibility modifiers",
359                                         next_part.GetSignatureForError ());
360                         }
361
362                         if (tc.partial_parts == null)
363                                 tc.partial_parts = new List<TypeContainer> (1);
364
365                         if ((next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
366                                 tc.ModFlags |= next_part.ModFlags & ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.AccessibilityMask);
367                         } else if ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
368                                 tc.ModFlags &= ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.AccessibilityMask);
369                                 tc.ModFlags |= next_part.ModFlags;
370                         } else {
371                                 tc.ModFlags |= next_part.ModFlags;
372                         }
373
374                         tc.spec.Modifiers = tc.ModFlags;
375
376                         if (next_part.attributes != null) {
377                                 if (tc.attributes == null)
378                                         tc.attributes = next_part.attributes;
379                                 else
380                                         tc.attributes.AddAttributes (next_part.attributes.Attrs);
381                         }
382
383                         next_part.PartialContainer = tc;
384                         tc.partial_parts.Add (next_part);
385                         return tc;
386                 }
387
388                 public virtual void RemoveTypeContainer (TypeContainer next_part)
389                 {
390                         if (types != null)
391                                 types.Remove (next_part);
392                         RemoveMemberType (next_part);
393                 }
394                 
395                 public void AddDelegate (Delegate d)
396                 {
397                         AddTypeContainer (d);
398                 }
399
400                 private void AddMemberToList (MemberCore mc, List<MemberCore> alist, bool isexplicit)
401                 {
402                         if (ordered_explicit_member_list == null)  {
403                                 ordered_explicit_member_list = new List<MemberCore> ();
404                                 ordered_member_list = new List<MemberCore> ();
405                         }
406
407                         if (isexplicit) {
408                                 if (Kind == MemberKind.Interface) {
409                                         Report.Error (541, mc.Location,
410                                                 "`{0}': explicit interface declaration can only be declared in a class or struct",
411                                                 mc.GetSignatureForError ());
412                                 }
413
414                                 ordered_explicit_member_list.Add (mc);
415                                 alist.Insert (0, mc);
416                         } else {
417                                 ordered_member_list.Add (mc);
418                                 alist.Add (mc);
419                         }
420
421                 }
422                 
423                 public void AddMethod (MethodOrOperator method)
424                 {
425                         if (!AddToContainer (method, method.MemberName.Basename))
426                                 return;
427                         
428                         if (methods == null)
429                                 methods = new List<MemberCore> ();
430
431                         if (method.MemberName.Left != null) 
432                                 AddMemberToList (method, methods, true);
433                         else 
434                                 AddMemberToList (method, methods, false);
435                 }
436
437                 public void AddConstructor (Constructor c)
438                 {
439                         bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
440                         if (!AddToContainer (c, is_static ?
441                                 ConstructorBuilder.ConstructorName : ConstructorBuilder.TypeConstructorName))
442                                 return;
443                         
444                         if (is_static && c.ParameterInfo.IsEmpty){
445                                 if (default_static_constructor != null) {
446                                     Report.SymbolRelatedToPreviousError (default_static_constructor);
447                                         Report.Error (111, c.Location,
448                                                 "A member `{0}' is already defined. Rename this member or use different parameter types",
449                                                 c.GetSignatureForError ());
450                                     return;
451                                 }
452
453                                 default_static_constructor = c;
454                         } else {
455                                 if (c.ParameterInfo.IsEmpty)
456                                         default_constructor = c;
457                                 
458                                 if (instance_constructors == null)
459                                         instance_constructors = new List<Constructor> ();
460                                 
461                                 instance_constructors.Add (c);
462                         }
463                 }
464
465                 public bool AddField (FieldBase field)
466                 {
467                         if (!AddMember (field))
468                                 return false;
469
470                         if (fields == null)
471                                 fields = new List<FieldBase> ();
472
473                         fields.Add (field);
474
475                         if ((field.ModFlags & Modifiers.STATIC) != 0)
476                                 return true;
477
478                         if (first_nonstatic_field == null) {
479                                 first_nonstatic_field = field;
480                                 return true;
481                         }
482
483                         if (Kind == MemberKind.Struct && first_nonstatic_field.Parent != field.Parent) {
484                                 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
485                                 Report.Warning (282, 3, field.Location,
486                                         "struct instance field `{0}' found in different declaration from instance field `{1}'",
487                                         field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
488                         }
489                         return true;
490                 }
491
492                 public void AddProperty (Property prop)
493                 {
494                         if (!AddMember (prop))
495                                 return;
496
497                         if (properties == null)
498                                 properties = new List<MemberCore> ();
499
500                         if (prop.MemberName.Left != null)
501                                 AddMemberToList (prop, properties, true);
502                         else 
503                                 AddMemberToList (prop, properties, false);
504                 }
505
506                 public void AddEvent (Event e)
507                 {
508                         if (!AddMember (e))
509                                 return;
510
511                         if (events == null)
512                                 events = new List<MemberCore> ();
513
514                         events.Add (e);
515                 }
516
517                 /// <summary>
518                 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
519                 /// </summary>
520                 public void AddIndexer (Indexer i)
521                 {
522                         if (indexers == null)
523                                 indexers = new List<MemberCore> ();
524
525                         if (i.IsExplicitImpl)
526                                 AddMemberToList (i, indexers, true);
527                         else 
528                                 AddMemberToList (i, indexers, false);
529                 }
530
531                 public void AddOperator (Operator op)
532                 {
533                         if (!AddMember (op))
534                                 return;
535
536                         if (operators == null)
537                                 operators = new List<MemberCore> ();
538
539                         operators.Add (op);
540                 }
541
542                 public void AddCompilerGeneratedClass (CompilerGeneratedClass c)
543                 {
544                         Report.Debug (64, "ADD COMPILER GENERATED CLASS", this, c);
545
546                         if (compiler_generated == null)
547                                 compiler_generated = new List<CompilerGeneratedClass> ();
548
549                         compiler_generated.Add (c);
550                 }
551
552                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
553                 {
554                         if (a.Type == pa.DefaultMember) {
555                                 if (Indexers != null) {
556                                         Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
557                                         return;
558                                 }
559                         }
560
561                         if (a.Type == pa.Required) {
562                                 Report.Error (1608, a.Location, "The RequiredAttribute attribute is not permitted on C# types");
563                                 return;
564                         }
565
566                         TypeBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
567                 } 
568
569                 public override AttributeTargets AttributeTargets {
570                         get {
571                                 throw new NotSupportedException ();
572                         }
573                 }
574
575                 public IList<TypeContainer> Types {
576                         get {
577                                 return types;
578                         }
579                 }
580
581                 public IList<MemberCore> Methods {
582                         get {
583                                 return methods;
584                         }
585                 }
586
587                 public IList<MemberCore> Constants {
588                         get {
589                                 return constants;
590                         }
591                 }
592
593                 public TypeSpec BaseType {
594                         get {
595                                 return spec.BaseType;
596                         }
597                 }
598
599                 public IList<FieldBase> Fields {
600                         get {
601                                 return fields;
602                         }
603                 }
604
605                 public IList<Constructor> InstanceConstructors {
606                         get {
607                                 return instance_constructors;
608                         }
609                 }
610
611                 public IList<MemberCore> Properties {
612                         get {
613                                 return properties;
614                         }
615                 }
616
617                 public IList<MemberCore> Events {
618                         get {
619                                 return events;
620                         }
621                 }
622                 
623                 public IList<MemberCore> Indexers {
624                         get {
625                                 return indexers;
626                         }
627                 }
628
629                 public IList<MemberCore> Operators {
630                         get {
631                                 return operators;
632                         }
633                 }
634
635                 public IList<CompilerGeneratedClass> CompilerGeneratedClasses {
636                         get {
637                                 return compiler_generated;
638                         }
639                 }
640
641                 protected override TypeAttributes TypeAttr {
642                         get {
643                                 return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
644                         }
645                 }
646
647                 public int TypeParametersCount {
648                         get {
649                                 return MemberName.Arity;
650                         }
651                 }
652
653                 TypeParameterSpec[] ITypeDefinition.TypeParameters {
654                         get {
655                                 // TODO MemberCache: this is going to hurt
656                                 return PartialContainer.type_params.Select (l => l.Type).ToArray ();
657                         }
658                 }
659
660                 public string GetAttributeDefaultMember ()
661                 {
662                         return indexers == null ? DefaultIndexerName : indexer_name;
663                 }
664
665                 public bool IsComImport {
666                         get {
667                                 if (OptAttributes == null)
668                                         return false;
669
670                                 return OptAttributes.Contains (Compiler.PredefinedAttributes.ComImport);
671                         }
672                 }
673
674                 string ITypeDefinition.Namespace {
675                         get {
676                                 return NamespaceEntry.NS.MemberName.GetSignatureForError ();
677                         }
678                 }
679
680                 public virtual void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
681                 {
682                         if ((field.ModFlags & Modifiers.STATIC) != 0){
683                                 if (initialized_static_fields == null) {
684                                         PartialContainer.HasStaticFieldInitializer = true;
685                                         initialized_static_fields = new List<FieldInitializer> (4);
686                                 }
687
688                                 initialized_static_fields.Add (expression);
689                         } else {
690                                 if (initialized_fields == null)
691                                         initialized_fields = new List<FieldInitializer> (4);
692
693                                 initialized_fields.Add (expression);
694                         }
695                 }
696
697                 public void ResolveFieldInitializers (BlockContext ec)
698                 {
699                         if (partial_parts != null) {
700                                 foreach (TypeContainer part in partial_parts) {
701                                         part.DoResolveFieldInitializers (ec);
702                                 }
703                         }
704                         DoResolveFieldInitializers (ec);
705                 }
706
707                 void DoResolveFieldInitializers (BlockContext ec)
708                 {
709                         if (ec.IsStatic) {
710                                 if (initialized_static_fields == null)
711                                         return;
712
713                                 bool has_complex_initializer = !RootContext.Optimize;
714                                 int i;
715                                 ExpressionStatement [] init = new ExpressionStatement [initialized_static_fields.Count];
716                                 for (i = 0; i < initialized_static_fields.Count; ++i) {
717                                         FieldInitializer fi = initialized_static_fields [i];
718                                         ExpressionStatement s = fi.ResolveStatement (ec);
719                                         if (s == null) {
720                                                 s = EmptyExpressionStatement.Instance;
721                                         } else if (fi.IsComplexInitializer) {
722                                                 has_complex_initializer |= true;
723                                         }
724
725                                         init [i] = s;
726                                 }
727
728                                 for (i = 0; i < initialized_static_fields.Count; ++i) {
729                                         FieldInitializer fi = initialized_static_fields [i];
730                                         //
731                                         // Need special check to not optimize code like this
732                                         // static int a = b = 5;
733                                         // static int b = 0;
734                                         //
735                                         if (!has_complex_initializer && fi.IsDefaultInitializer)
736                                                 continue;
737
738                                         ec.CurrentBlock.AddScopeStatement (new StatementExpression (init [i]));
739                                 }
740
741                                 return;
742                         }
743
744                         if (initialized_fields == null)
745                                 return;
746
747                         for (int i = 0; i < initialized_fields.Count; ++i) {
748                                 FieldInitializer fi = initialized_fields [i];
749                                 ExpressionStatement s = fi.ResolveStatement (ec);
750                                 if (s == null)
751                                         continue;
752
753                                 //
754                                 // Field is re-initialized to its default value => removed
755                                 //
756                                 if (fi.IsDefaultInitializer && RootContext.Optimize)
757                                         continue;
758
759                                 ec.CurrentBlock.AddScopeStatement (new StatementExpression (s));
760                         }
761                 }
762
763                 public override string DocComment {
764                         get {
765                                 return comment;
766                         }
767                         set {
768                                 if (value == null)
769                                         return;
770
771                                 comment += value;
772                         }
773                 }
774
775                 public PendingImplementation PendingImplementations {
776                         get { return pending; }
777                 }
778
779                 public TypeSpec GetAttributeCoClass ()
780                 {
781                         if (OptAttributes == null)
782                                 return null;
783
784                         Attribute a = OptAttributes.Search (Compiler.PredefinedAttributes.CoClass);
785                         if (a == null)
786                                 return null;
787
788                         return a.GetCoClassAttributeValue ();
789                 }
790
791                 public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
792                 {
793                         Attribute a = null;
794                         if (OptAttributes != null) {
795                                 a = OptAttributes.Search (pa);
796                         }
797
798                         if (a == null)
799                                 return null;
800
801                         return a.GetAttributeUsageAttribute ();
802                 }
803
804                 public virtual void AddBasesForPart (DeclSpace part, List<FullNamedExpression> bases)
805                 {
806                         // FIXME: get rid of partial_parts and store lists of bases of each part here
807                         // assumed, not verified: 'part' is in 'partial_parts' 
808                         ((TypeContainer) part).type_bases = bases;
809                 }
810
811                 /// <summary>
812                 ///   This function computes the Base class and also the
813                 ///   list of interfaces that the class or struct @c implements.
814                 ///   
815                 ///   The return value is an array (might be null) of
816                 ///   interfaces implemented (as Types).
817                 ///   
818                 ///   The @base_class argument is set to the base object or null
819                 ///   if this is `System.Object'. 
820                 /// </summary>
821                 protected virtual TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
822                 {
823                         base_class = null;
824                         if (type_bases == null)
825                                 return null;
826
827                         int count = type_bases.Count;
828                         TypeExpr [] ifaces = null;
829                         var base_context = new BaseContext (this);
830                         for (int i = 0, j = 0; i < count; i++){
831                                 FullNamedExpression fne = type_bases [i];
832
833                                 TypeExpr fne_resolved = fne.ResolveAsTypeTerminal (base_context, false);
834                                 if (fne_resolved == null)
835                                         continue;
836
837                                 if (i == 0 && Kind == MemberKind.Class && !fne_resolved.Type.IsInterface) {
838                                         if (fne_resolved.Type == InternalType.Dynamic)
839                                                 Report.Error (1965, Location, "Class `{0}' cannot derive from the dynamic type",
840                                                         GetSignatureForError ());
841                                         else
842                                                 base_type = fne_resolved.Type;
843
844                                         base_class = fne_resolved;
845                                         continue;
846                                 }
847
848                                 if (ifaces == null)
849                                         ifaces = new TypeExpr [count - i];
850
851                                 if (fne_resolved.Type.IsInterface) {
852                                         for (int ii = 0; ii < j; ++ii) {
853                                                 if (fne_resolved.Type == ifaces [ii].Type) {
854                                                         Report.Error (528, Location, "`{0}' is already listed in interface list",
855                                                                 fne_resolved.GetSignatureForError ());
856                                                         break;
857                                                 }
858                                         }
859
860                                         if (Kind == MemberKind.Interface && !IsAccessibleAs (fne_resolved.Type)) {
861                                                 Report.Error (61, fne.Location,
862                                                         "Inconsistent accessibility: base interface `{0}' is less accessible than interface `{1}'",
863                                                         fne_resolved.GetSignatureForError (), GetSignatureForError ());
864                                         }
865                                 } else {
866                                         Report.SymbolRelatedToPreviousError (fne_resolved.Type);
867                                         if (Kind != MemberKind.Class) {
868                                                 Report.Error (527, fne.Location, "Type `{0}' in interface list is not an interface", fne_resolved.GetSignatureForError ());
869                                         } else if (base_class != null)
870                                                 Report.Error (1721, fne.Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
871                                                         GetSignatureForError (), base_class.GetSignatureForError (), fne_resolved.GetSignatureForError ());
872                                         else {
873                                                 Report.Error (1722, fne.Location, "`{0}': Base class `{1}' must be specified as first",
874                                                         GetSignatureForError (), fne_resolved.GetSignatureForError ());
875                                         }
876                                 }
877
878                                 ifaces [j++] = fne_resolved;
879                         }
880
881                         return ifaces;
882                 }
883
884                 TypeExpr[] GetNormalPartialBases ()
885                 {
886                         var ifaces = new List<TypeExpr> (0);
887                         if (iface_exprs != null)
888                                 ifaces.AddRange (iface_exprs);
889
890                         foreach (TypeContainer part in partial_parts) {
891                                 TypeExpr new_base_class;
892                                 TypeExpr[] new_ifaces = part.ResolveBaseTypes (out new_base_class);
893                                 if (new_base_class != null) {
894                                         if (base_type_expr != null && new_base_class.Type != base_type) {
895                                                 Report.SymbolRelatedToPreviousError (new_base_class.Location, "");
896                                                 Report.Error (263, part.Location,
897                                                         "Partial declarations of `{0}' must not specify different base classes",
898                                                         part.GetSignatureForError ());
899                                         } else {
900                                                 base_type_expr = new_base_class;
901                                                 base_type = base_type_expr.Type;
902                                         }
903                                 }
904
905                                 if (new_ifaces == null)
906                                         continue;
907
908                                 foreach (TypeExpr iface in new_ifaces) {
909                                         if (ifaces.Contains (iface))
910                                                 continue;
911
912                                         ifaces.Add (iface);
913                                 }
914                         }
915
916                         if (ifaces.Count == 0)
917                                 return null;
918
919                         return ifaces.ToArray ();
920                 }
921
922                 //
923                 // Checks that some operators come in pairs:
924                 //  == and !=
925                 // > and <
926                 // >= and <=
927                 // true and false
928                 //
929                 // They are matched based on the return type and the argument types
930                 //
931                 void CheckPairedOperators ()
932                 {
933                         bool has_equality_or_inequality = false;
934                         var operators = this.operators.ToArray ();
935                         bool[] has_pair = new bool[operators.Length];
936
937                         for (int i = 0; i < operators.Length; ++i) {
938                                 if (operators[i] == null)
939                                         continue;
940
941                                 Operator o_a = (Operator) operators[i];
942                                 Operator.OpType o_type = o_a.OperatorType;
943                                 if (o_type == Operator.OpType.Equality || o_type == Operator.OpType.Inequality)
944                                         has_equality_or_inequality = true;
945
946                                 Operator.OpType matching_type = o_a.GetMatchingOperator ();
947                                 if (matching_type == Operator.OpType.TOP) {
948                                         operators[i] = null;
949                                         continue;
950                                 }
951
952                                 for (int ii = 0; ii < operators.Length; ++ii) {
953                                         Operator o_b = (Operator) operators[ii];
954                                         if (o_b == null || o_b.OperatorType != matching_type)
955                                                 continue;
956
957                                         if (!TypeSpecComparer.IsEqual (o_a.ReturnType, o_b.ReturnType))
958                                                 continue;
959
960                                         if (!TypeSpecComparer.Equals (o_a.ParameterTypes, o_b.ParameterTypes))
961                                                 continue;
962
963                                         operators[i] = null;
964
965                                         //
966                                         // Used to ignore duplicate user conversions
967                                         //
968                                         has_pair[ii] = true;
969                                 }
970                         }
971
972                         for (int i = 0; i < operators.Length; ++i) {
973                                 if (operators[i] == null || has_pair[i])
974                                         continue;
975
976                                 Operator o = (Operator) operators [i];
977                                 Report.Error (216, o.Location,
978                                         "The operator `{0}' requires a matching operator `{1}' to also be defined",
979                                         o.GetSignatureForError (), Operator.GetName (o.GetMatchingOperator ()));
980                         }
981
982                         if (has_equality_or_inequality) {
983                                 if (Methods == null || !HasEquals)
984                                         Report.Warning (660, 2, Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)",
985                                                 GetSignatureForError ());
986
987                                 if (Methods == null || !HasGetHashCode)
988                                         Report.Warning (661, 2, Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()",
989                                                 GetSignatureForError ());
990                         }
991                 }
992         
993                 bool CreateTypeBuilder ()
994                 {
995                         //
996                         // Sets .size to 1 for structs with no instance fields
997                         //
998                         int type_size = Kind == MemberKind.Struct && first_nonstatic_field == null ? 1 : 0;
999
1000                         if (IsTopLevel) {
1001                                 if (GlobalRootNamespace.Instance.IsNamespace (Name)) {
1002                                         Report.Error (519, Location, "`{0}' clashes with a predefined namespace", Name);
1003                                         return false;
1004                                 }
1005
1006                                 ModuleBuilder builder = Module.Compiled.Builder;
1007                                 TypeBuilder = builder.DefineType (Name, TypeAttr, null, type_size);
1008                         } else {
1009                                 TypeBuilder builder = Parent.TypeBuilder;
1010
1011                                 TypeBuilder = builder.DefineNestedType (Basename, TypeAttr, null, type_size);
1012                         }
1013
1014                         spec.SetMetaInfo (TypeBuilder);
1015                         spec.MemberCache = new MemberCache (this);
1016                         spec.DeclaringType = Parent.CurrentType;
1017
1018                         if (!IsTopLevel)
1019                                 Parent.MemberCache.AddMember (spec);
1020
1021                         if (IsGeneric) {
1022                                 string[] param_names = new string[TypeParameters.Length];
1023                                 for (int i = 0; i < TypeParameters.Length; i++)
1024                                         param_names [i] = TypeParameters[i].Name;
1025
1026                                 all_tp_builders = TypeBuilder.DefineGenericParameters (param_names);
1027
1028                                 int offset = CurrentTypeParametersStartIndex;
1029                                 for (int i = offset; i < all_tp_builders.Length; i++) {
1030                                         CurrentTypeParameters [i - offset].Define (all_tp_builders [i], spec);
1031                                 }
1032                         }
1033
1034                         return true;
1035                 }
1036
1037                 //
1038                 // Creates a proxy base method call inside this container for hoisted base member calls
1039                 //
1040                 public MethodSpec CreateHoistedBaseCallProxy (ResolveContext rc, MethodSpec method)
1041                 {
1042                         Method proxy_method;
1043
1044                         //
1045                         // One proxy per base method is enough
1046                         //
1047                         if (hoisted_base_call_proxies == null) {
1048                                 hoisted_base_call_proxies = new Dictionary<MethodSpec, Method> ();
1049                                 proxy_method = null;
1050                         } else {
1051                                 hoisted_base_call_proxies.TryGetValue (method, out proxy_method);
1052                         }
1053
1054                         if (proxy_method == null) {
1055                                 string name = CompilerGeneratedClass.MakeName (method.Name, null, "BaseCallProxy", hoisted_base_call_proxies.Count);
1056                                 var base_parameters = method.Parameters.FixedParameters as Parameter[];
1057                                 if (base_parameters == null) {
1058                                         base_parameters = new Parameter[method.Parameters.Count];
1059                                         for (int i = 0; i < base_parameters.Length; ++i) {
1060                                                 var base_param = method.Parameters.FixedParameters[i];
1061                                                 base_parameters[i] = new Parameter (new TypeExpression (method.Parameters.Types[i], Location),
1062                                                         base_param.Name, base_param.ModFlags, null, Location);
1063                                                 base_parameters[i].Resolve (this, i);
1064                                         }
1065                                 }
1066
1067                                 var cloned_params = ParametersCompiled.CreateFullyResolved (base_parameters, method.Parameters.Types);
1068                                 if (method.Parameters.HasArglist) {
1069                                         cloned_params.FixedParameters[0] = new Parameter (null, "__arglist", Parameter.Modifier.NONE, null, Location);
1070                                         cloned_params.Types[0] = TypeManager.runtime_argument_handle_type;
1071                                 }
1072
1073                                 GenericMethod generic_method;
1074                                 MemberName member_name;
1075                                 if (method.IsGeneric) {
1076                                         //
1077                                         // Copy all base generic method type parameters info
1078                                         //
1079                                         var hoisted_tparams = method.GenericDefinition.TypeParameters;
1080                                         var targs = new TypeArguments ();
1081                                         var type_params = new TypeParameter[hoisted_tparams.Length];
1082                                         for (int i = 0; i < type_params.Length; ++i) {
1083                                                 var tp = hoisted_tparams[i];
1084                                                 targs.Add (new TypeParameterName (tp.Name, null, Location));
1085                                                 type_params[i] = new TypeParameter (tp, null, null, new MemberName (tp.Name), null);
1086                                         }
1087
1088                                         member_name = new MemberName (name, targs, Location);
1089                                         generic_method = new GenericMethod (NamespaceEntry, this, member_name, type_params,
1090                                                 new TypeExpression (method.ReturnType, Location), cloned_params);
1091                                 } else {
1092                                         member_name = new MemberName (name);
1093                                         generic_method = null;
1094                                 }
1095
1096                                 // Compiler generated proxy
1097                                 proxy_method = new Method (this, generic_method, new TypeExpression (method.ReturnType, Location),
1098                                         Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED | Modifiers.DEBUGGER_HIDDEN,
1099                                         member_name, cloned_params, null);
1100
1101                                 var block = new ToplevelBlock (Compiler, proxy_method.ParameterInfo, Location);
1102
1103                                 var mg = MethodGroupExpr.CreatePredefined (method, method.DeclaringType, Location);
1104                                 mg.InstanceExpression = new BaseThis (method.DeclaringType, Location);
1105
1106                                 // Get all the method parameters and pass them as arguments
1107                                 var real_base_call = new Invocation (mg, block.GetAllParametersArguments ());
1108                                 Statement statement;
1109                                 if (method.ReturnType == TypeManager.void_type)
1110                                         statement = new StatementExpression (real_base_call);
1111                                 else
1112                                         statement = new Return (real_base_call, Location);
1113
1114                                 block.AddStatement (statement);
1115                                 proxy_method.Block = block;
1116
1117                                 methods.Add (proxy_method);
1118                                 proxy_method.Define ();
1119
1120                                 hoisted_base_call_proxies.Add (method, proxy_method);
1121                         }
1122
1123                         return proxy_method.Spec;
1124                 }
1125
1126                 bool DefineBaseTypes ()
1127                 {
1128                         iface_exprs = ResolveBaseTypes (out base_type_expr);
1129                         if (partial_parts != null) {
1130                                 iface_exprs = GetNormalPartialBases ();
1131                         }
1132
1133                         var cycle = CheckRecursiveDefinition (this);
1134                         if (cycle != null) {
1135                                 Report.SymbolRelatedToPreviousError (cycle);
1136                                 if (this is Interface) {
1137                                         Report.Error (529, Location,
1138                                                 "Inherited interface `{0}' causes a cycle in the interface hierarchy of `{1}'",
1139                                             GetSignatureForError (), cycle.GetSignatureForError ());
1140                                 } else {
1141                                         Report.Error (146, Location,
1142                                                 "Circular base class dependency involving `{0}' and `{1}'",
1143                                                 GetSignatureForError (), cycle.GetSignatureForError ());
1144                                 }
1145
1146                                 base_type = null;
1147                         }
1148
1149                         if (iface_exprs != null) {
1150                                 foreach (TypeExpr iface in iface_exprs) {
1151                                         // Prevents a crash, the interface might not have been resolved: 442144
1152                                         if (iface == null)
1153                                                 continue;
1154                                         
1155                                         var iface_type = iface.Type;
1156
1157                                         if (!spec.AddInterface (iface_type))
1158                                                 continue;
1159
1160                                         if (iface_type.IsGeneric && spec.Interfaces != null) {
1161                                                 foreach (var prev_iface in iface_exprs) {
1162                                                         if (prev_iface == iface)
1163                                                                 break;
1164
1165                                                         if (!TypeSpecComparer.Unify.IsEqual (iface_type, prev_iface.Type))
1166                                                                 continue;
1167
1168                                                         Report.Error (695, Location,
1169                                                                 "`{0}' cannot implement both `{1}' and `{2}' because they may unify for some type parameter substitutions",
1170                                                                 GetSignatureForError (), prev_iface.GetSignatureForError (), iface_type.GetSignatureForError ());
1171                                                 }
1172                                         }
1173
1174                                         TypeBuilder.AddInterfaceImplementation (iface_type.GetMetaInfo ());
1175
1176                                         // Ensure the base is always setup
1177                                         var compiled_iface = iface_type.MemberDefinition as Interface;
1178                                         if (compiled_iface != null) {
1179                                                 // TODO: Need DefineBaseType only
1180                                                 compiled_iface.DefineType ();
1181                                         }
1182
1183                                         if (iface_type.Interfaces != null) {
1184                                                 var base_ifaces = new List<TypeSpec> (iface_type.Interfaces);
1185                                                 for (int i = 0; i < base_ifaces.Count; ++i) {
1186                                                         var ii_iface_type = base_ifaces[i];
1187                                                         if (spec.AddInterface (ii_iface_type)) {
1188                                                                 TypeBuilder.AddInterfaceImplementation (ii_iface_type.GetMetaInfo ());
1189
1190                                                                 if (ii_iface_type.Interfaces != null)
1191                                                                         base_ifaces.AddRange (ii_iface_type.Interfaces);
1192                                                         }
1193                                                 }
1194                                         }
1195                                 }
1196                         }
1197
1198                         if (Kind == MemberKind.Interface) {
1199                                 spec.BaseType = TypeManager.object_type;
1200                                 return true;
1201                         }
1202
1203                         if (base_type != null) {
1204                                 spec.BaseType = base_type;
1205
1206                                 // Set base type after type creation
1207                                 TypeBuilder.SetParent (base_type.GetMetaInfo ());
1208                         }
1209
1210                         return true;
1211                 }
1212
1213                 public virtual void DefineConstants ()
1214                 {
1215                         if (constants != null) {
1216                                 foreach (Const c in constants) {
1217                                         c.DefineValue ();
1218                                 }
1219                         }
1220
1221                         if (instance_constructors != null) {
1222                                 foreach (MethodCore m in instance_constructors) {
1223                                         var p = m.ParameterInfo;
1224                                         if (!p.IsEmpty) {
1225                                                 p.ResolveDefaultValues (m);
1226                                         }
1227                                 }
1228                         }
1229
1230                         if (methods != null) {
1231                                 foreach (MethodCore m in methods) {
1232                                         var p = m.ParameterInfo;
1233                                         if (!p.IsEmpty) {
1234                                                 p.ResolveDefaultValues (m);
1235                                         }
1236                                 }
1237                         }
1238
1239                         if (indexers != null) {
1240                                 foreach (Indexer i in indexers) {
1241                                         i.ParameterInfo.ResolveDefaultValues (i);
1242                                 }
1243                         }
1244
1245                         if (types != null) {
1246                                 foreach (var t in types)
1247                                         t.DefineConstants ();
1248                         }
1249                 }
1250
1251                 //
1252                 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1253                 //
1254                 public TypeBuilder CreateType ()
1255                 {
1256                         if (TypeBuilder != null)
1257                                 return TypeBuilder;
1258
1259                         if (error)
1260                                 return null;
1261
1262                         if (!CreateTypeBuilder ()) {
1263                                 error = true;
1264                                 return null;
1265                         }
1266
1267                         if (partial_parts != null) {
1268                                 foreach (TypeContainer part in partial_parts) {
1269                                         part.spec = spec;
1270                                         part.current_type = current_type;
1271                                         part.TypeBuilder = TypeBuilder;
1272                                 }
1273                         }
1274
1275                         if (Types != null) {
1276                                 foreach (TypeContainer tc in Types) {
1277                                         if (tc.CreateType () == null) {
1278                                                 error = true;
1279                                                 return null;
1280                                         }
1281                                 }
1282                         }
1283
1284                         return TypeBuilder;
1285                 }
1286
1287                 public override TypeBuilder DefineType ()
1288                 {
1289                         if (error)
1290                                 return null;
1291                         if (type_defined)
1292                                 return TypeBuilder;
1293
1294                         type_defined = true;
1295
1296                         if (!DefineBaseTypes ()) {
1297                                 error = true;
1298                                 return null;
1299                         }
1300
1301                         if (!DefineNestedTypes ()) {
1302                                 error = true;
1303                                 return null;
1304                         }
1305
1306                         return TypeBuilder;
1307                 }
1308
1309                 public override void SetParameterInfo (List<Constraints> constraints_list)
1310                 {
1311                         base.SetParameterInfo (constraints_list);
1312
1313                         if (PartialContainer.CurrentTypeParameters == null || PartialContainer == this)
1314                                 return;
1315
1316                         TypeParameter[] tc_names = PartialContainer.CurrentTypeParameters;
1317                         for (int i = 0; i < tc_names.Length; ++i) {
1318                                 if (tc_names [i].Name != type_params [i].Name) {
1319                                         Report.SymbolRelatedToPreviousError (PartialContainer.Location, "");
1320                                         Report.Error (264, Location, "Partial declarations of `{0}' must have the same type parameter names in the same order",
1321                                                 GetSignatureForError ());
1322                                         break;
1323                                 }
1324
1325                                 if (tc_names [i].Variance != type_params [i].Variance) {
1326                                         Report.SymbolRelatedToPreviousError (PartialContainer.Location, "");
1327                                         Report.Error (1067, Location, "Partial declarations of `{0}' must have the same type parameter variance modifiers",
1328                                                 GetSignatureForError ());
1329                                         break;
1330                                 }
1331                         }
1332                 }
1333
1334                 //
1335                 // Replaces normal spec with predefined one when compiling corlib
1336                 // and this type container defines predefined type
1337                 //
1338                 public void SetPredefinedSpec (PredefinedTypeSpec spec)
1339                 {
1340                         this.spec = spec;
1341                 }
1342
1343                 void UpdateTypeParameterConstraints (TypeContainer part)
1344                 {
1345                         TypeParameter[] current_params = type_params;
1346                         for (int i = 0; i < current_params.Length; i++) {
1347                                 if (current_params [i].AddPartialConstraints (part, part.type_params [i]))
1348                                         continue;
1349
1350                                 Report.SymbolRelatedToPreviousError (Location, "");
1351                                 Report.Error (265, part.Location,
1352                                         "Partial declarations of `{0}' have inconsistent constraints for type parameter `{1}'",
1353                                         GetSignatureForError (), current_params [i].GetSignatureForError ());
1354                         }
1355                 }
1356
1357                 public bool ResolveTypeParameters ()
1358                 {
1359                         if (!DoResolveTypeParameters ())
1360                                 return false;
1361
1362                         if (types != null) {
1363                                 foreach (var type in types)
1364                                         if (!type.ResolveTypeParameters ())
1365                                                 return false;
1366                         }
1367
1368                         if (compiler_generated != null) {
1369                                 foreach (CompilerGeneratedClass c in compiler_generated)
1370                                         if (!c.ResolveTypeParameters ())
1371                                                 return false;
1372                         }
1373
1374                         return true;
1375                 }
1376
1377                 protected virtual bool DoResolveTypeParameters ()
1378                 {
1379                         if (CurrentTypeParameters == null)
1380                                 return true;
1381
1382                         if (PartialContainer != this)
1383                                 throw new InternalErrorException ();
1384
1385                         var base_context = new BaseContext (this);
1386                         foreach (TypeParameter type_param in CurrentTypeParameters) {
1387                                 if (!type_param.ResolveConstraints (base_context)) {
1388                                         error = true;
1389                                         return false;
1390                                 }
1391                         }
1392
1393                         if (partial_parts != null) {
1394                                 foreach (TypeContainer part in partial_parts)
1395                                         UpdateTypeParameterConstraints (part);
1396                         }
1397
1398                         return true;
1399                 }
1400
1401                 protected virtual bool DefineNestedTypes ()
1402                 {
1403                         if (Types != null) {
1404                                 foreach (TypeContainer tc in Types)
1405                                         if (tc.DefineType () == null)
1406                                                 return false;
1407                         }
1408
1409                         return true;
1410                 }
1411
1412                 TypeSpec CheckRecursiveDefinition (TypeContainer tc)
1413                 {
1414                         if (InTransit != null)
1415                                 return spec;
1416
1417                         InTransit = tc;
1418
1419                         if (base_type_expr != null) {
1420                                 var ptc = base_type.MemberDefinition as TypeContainer;
1421                                 if (ptc != null && ptc.CheckRecursiveDefinition (this) != null)
1422                                         return base_type;
1423                         }
1424
1425                         if (iface_exprs != null) {
1426                                 foreach (TypeExpr iface in iface_exprs) {
1427                                         // the interface might not have been resolved, prevents a crash, see #442144
1428                                         if (iface == null)
1429                                                 continue;
1430                                         var ptc = iface.Type.MemberDefinition as Interface;
1431                                         if (ptc != null && ptc.CheckRecursiveDefinition (this) != null)
1432                                                 return iface.Type;
1433                                 }
1434                         }
1435
1436                         if (!IsTopLevel && Parent.PartialContainer.CheckRecursiveDefinition (this) != null)
1437                                 return spec;
1438
1439                         InTransit = null;
1440                         return null;
1441                 }
1442
1443                 /// <summary>
1444                 ///   Populates our TypeBuilder with fields and methods
1445                 /// </summary>
1446                 public sealed override bool Define ()
1447                 {
1448                         if (members_defined)
1449                                 return members_defined_ok;
1450
1451                         members_defined_ok = DoDefineMembers ();
1452                         members_defined = true;
1453
1454                         if (types != null) {
1455                                 foreach (var nested in types)
1456                                         nested.Define ();
1457                         }
1458
1459                         return members_defined_ok;
1460                 }
1461
1462                 protected virtual bool DoDefineMembers ()
1463                 {
1464                         if (iface_exprs != null) {
1465                                 foreach (TypeExpr iface in iface_exprs) {
1466                                         var iface_type = iface.Type;
1467
1468                                         // Ensure the base is always setup
1469                                         var compiled_iface = iface_type.MemberDefinition as Interface;
1470                                         if (compiled_iface != null)
1471                                                 compiled_iface.Define ();
1472
1473                                         if (Kind == MemberKind.Interface)
1474                                                 MemberCache.AddInterface (iface_type);
1475
1476                                         ObsoleteAttribute oa = iface_type.GetAttributeObsolete ();
1477                                         if (oa != null && !IsObsolete)
1478                                                 AttributeTester.Report_ObsoleteMessage (oa, iface.GetSignatureForError (), Location, Report);
1479
1480                                         GenericTypeExpr ct = iface as GenericTypeExpr;
1481                                         if (ct != null) {
1482                                                 // TODO: passing `this' is wrong, should be base type iface instead
1483                                                 TypeManager.CheckTypeVariance (ct.Type, Variance.Covariant, this);
1484
1485                                                 ct.CheckConstraints (this);
1486
1487                                                 if (ct.HasDynamicArguments () && !IsCompilerGenerated) {
1488                                                         Report.Error (1966, iface.Location,
1489                                                                 "`{0}': cannot implement a dynamic interface `{1}'",
1490                                                                 GetSignatureForError (), iface.GetSignatureForError ());
1491                                                         return false;
1492                                                 }
1493                                         }
1494                                 }
1495                         }
1496
1497                         if (base_type_expr != null) {
1498                                 ObsoleteAttribute obsolete_attr = base_type.GetAttributeObsolete ();
1499                                 if (obsolete_attr != null && !IsObsolete)
1500                                         AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location, Report);
1501
1502                                 var ct = base_type_expr as GenericTypeExpr;
1503                                 if (ct != null)
1504                                         ct.CheckConstraints (this);
1505                         }
1506
1507                         if (base_type != null) {
1508                                 var baseContainer = base_type.MemberDefinition as ClassOrStruct;
1509                                 if (baseContainer != null) {
1510                                         baseContainer.Define ();
1511
1512                                         //
1513                                         // It can trigger define of this type (for generic types only)
1514                                         //
1515                                         if (HasMembersDefined)
1516                                                 return true;
1517                                 }
1518                         }
1519
1520                         if (type_params != null) {
1521                                 foreach (var tp in type_params) {
1522                                         tp.CheckGenericConstraints ();
1523                                 }
1524                         }
1525
1526                         DefineContainerMembers (constants);
1527                         DefineContainerMembers (fields);
1528
1529                         if (Kind == MemberKind.Struct || Kind == MemberKind.Class) {
1530                                 pending = PendingImplementation.GetPendingImplementations (this);
1531
1532                                 if (requires_delayed_unmanagedtype_check) {
1533                                         requires_delayed_unmanagedtype_check = false;
1534                                         foreach (FieldBase f in fields) {
1535                                                 if (f.MemberType != null && f.MemberType.IsPointer)
1536                                                         TypeManager.VerifyUnmanaged (Compiler, f.MemberType, f.Location);
1537                                         }
1538                                 }
1539                         }
1540                 
1541                         //
1542                         // Constructors are not in the defined_names array
1543                         //
1544                         DefineContainerMembers (instance_constructors);
1545                 
1546                         DefineContainerMembers (events);
1547                         DefineContainerMembers (ordered_explicit_member_list);
1548                         DefineContainerMembers (ordered_member_list);
1549
1550                         if (operators != null) {
1551                                 DefineContainerMembers (operators);
1552                                 CheckPairedOperators ();
1553                         }
1554
1555                         ComputeIndexerName();
1556                         CheckEqualsAndGetHashCode();
1557
1558                         return true;
1559                 }
1560
1561                 protected virtual void DefineContainerMembers (System.Collections.IList mcal) // IList<MemberCore>
1562                 {
1563                         if (mcal != null) {
1564                                 for (int i = 0; i < mcal.Count; ++i) {
1565                                         MemberCore mc = (MemberCore) mcal[i];
1566                                         try {
1567                                                 mc.Define ();
1568                                         } catch (Exception e) {
1569                                                 throw new InternalErrorException (mc, e);
1570                                         }
1571                                 }
1572                         }
1573                 }
1574                 
1575                 protected virtual void ComputeIndexerName ()
1576                 {
1577                         if (indexers == null)
1578                                 return;
1579
1580                         string class_indexer_name = null;
1581
1582                         //
1583                         // If there's both an explicit and an implicit interface implementation, the
1584                         // explicit one actually implements the interface while the other one is just
1585                         // a normal indexer.  See bug #37714.
1586                         //
1587
1588                         // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
1589                         foreach (Indexer i in indexers) {
1590                                 if (i.InterfaceType != null) {
1591                                         if (seen_normal_indexers)
1592                                                 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
1593                                         continue;
1594                                 }
1595
1596                                 seen_normal_indexers = true;
1597
1598                                 if (class_indexer_name == null) {
1599                                         class_indexer_name = i.ShortName;
1600                                         continue;
1601                                 }
1602
1603                                 if (i.ShortName != class_indexer_name)
1604                                         Report.Error (668, i.Location, "Two indexers have different names; the IndexerName attribute must be used with the same name on every indexer within a type");
1605                         }
1606
1607                         if (class_indexer_name != null)
1608                                 indexer_name = class_indexer_name;
1609                 }
1610
1611                 void EmitIndexerName ()
1612                 {
1613                         if (!seen_normal_indexers)
1614                                 return;
1615
1616                         PredefinedAttribute pa = Compiler.PredefinedAttributes.DefaultMember;
1617                         if (pa.Constructor == null &&
1618                                 !pa.ResolveConstructor (Location, TypeManager.string_type))
1619                                 return;
1620
1621                         CustomAttributeBuilder cb = new CustomAttributeBuilder (pa.Constructor, new string [] { GetAttributeDefaultMember () });
1622                         TypeBuilder.SetCustomAttribute (cb);
1623                 }
1624
1625                 protected virtual void CheckEqualsAndGetHashCode ()
1626                 {
1627                         if (methods == null)
1628                                 return;
1629
1630                         if (HasEquals && !HasGetHashCode) {
1631                                 Report.Warning (659, 3, this.Location, "`{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", this.GetSignatureForError ());
1632                         }
1633                 }
1634
1635                 // Indicated whether container has StructLayout attribute set Explicit
1636                 public bool HasExplicitLayout {
1637                         get { return (caching_flags & Flags.HasExplicitLayout) != 0; }
1638                         set { caching_flags |= Flags.HasExplicitLayout; }
1639                 }
1640
1641                 public bool HasStructLayout {
1642                         get { return (caching_flags & Flags.HasStructLayout) != 0; }
1643                         set { caching_flags |= Flags.HasStructLayout; }
1644                 }
1645
1646                 public MemberCache MemberCache {
1647                         get {
1648                                 return spec.MemberCache;
1649                         }
1650                 }
1651
1652                 void CheckMemberUsage (List<MemberCore> al, string member_type)
1653                 {
1654                         if (al == null)
1655                                 return;
1656
1657                         foreach (MemberCore mc in al) {
1658                                 if ((mc.ModFlags & Modifiers.AccessibilityMask) != Modifiers.PRIVATE)
1659                                         continue;
1660
1661                                 if (!mc.IsUsed && (mc.caching_flags & Flags.Excluded) == 0) {
1662                                         Report.Warning (169, 3, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ());
1663                                 }
1664                         }
1665                 }
1666
1667                 public virtual void VerifyMembers ()
1668                 {
1669                         //
1670                         // Check for internal or private fields that were never assigned
1671                         //
1672                         if (Report.WarningLevel >= 3) {
1673                                 if (RootContext.EnhancedWarnings) {
1674                                         CheckMemberUsage (properties, "property");
1675                                         CheckMemberUsage (methods, "method");
1676                                         CheckMemberUsage (constants, "constant");
1677                                 }
1678
1679                                 if (fields != null){
1680                                         bool is_type_exposed = Kind == MemberKind.Struct || IsExposedFromAssembly ();
1681                                         foreach (FieldBase f in fields) {
1682                                                 if ((f.ModFlags & Modifiers.AccessibilityMask) != Modifiers.PRIVATE) {
1683                                                         if (is_type_exposed)
1684                                                                 continue;
1685
1686                                                         f.SetIsUsed ();
1687                                                 }                               
1688                                                 
1689                                                 if (!f.IsUsed){
1690                                                         if ((f.caching_flags & Flags.IsAssigned) == 0)
1691                                                                 Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ());
1692                                                         else {
1693                                                                 Report.Warning (414, 3, f.Location, "The private field `{0}' is assigned but its value is never used",
1694                                                                         f.GetSignatureForError ());
1695                                                         }
1696                                                         continue;
1697                                                 }
1698                                                 
1699                                                 //
1700                                                 // Only report 649 on level 4
1701                                                 //
1702                                                 if (Report.WarningLevel < 4)
1703                                                         continue;
1704                                                 
1705                                                 if ((f.caching_flags & Flags.IsAssigned) != 0)
1706                                                         continue;
1707
1708                                                 //
1709                                                 // Don't be pendatic over serializable attributes
1710                                                 //
1711                                                 if (f.OptAttributes != null || PartialContainer.HasStructLayout)
1712                                                         continue;
1713                                                 
1714                                                 Constant c = New.Constantify (f.MemberType);
1715                                                 Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
1716                                                         f.GetSignatureForError (), c == null ? "null" : c.AsString ());
1717                                         }
1718                                 }
1719                         }
1720                 }
1721
1722                 public override void Emit ()
1723                 {
1724                         if (!IsTopLevel) {
1725                                 MemberSpec candidate;
1726                                 var conflict_symbol = MemberCache.FindBaseMember (this, out candidate);
1727                                 if (conflict_symbol == null && candidate == null) {
1728                                         if ((ModFlags & Modifiers.NEW) != 0)
1729                                                 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required",
1730                                                         GetSignatureForError ());
1731                                 } else {
1732                                         if ((ModFlags & Modifiers.NEW) == 0) {
1733                                                 if (candidate == null)
1734                                                         candidate = conflict_symbol;
1735
1736                                                 Report.SymbolRelatedToPreviousError (candidate);
1737                                                 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
1738                                                         GetSignatureForError (), candidate.GetSignatureForError ());
1739                                         }
1740                                 }
1741                         }
1742
1743                         if (all_tp_builders != null) {
1744                                 int current_starts_index = CurrentTypeParametersStartIndex;
1745                                 for (int i = 0; i < all_tp_builders.Length; i++) {
1746                                         if (i < current_starts_index) {
1747                                                 TypeParameters[i].EmitConstraints (all_tp_builders [i]);
1748                                         } else {
1749                                                 CurrentTypeParameters [i - current_starts_index].Emit ();
1750                                         }
1751                                 }
1752                         }
1753
1754                         if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
1755                                 Compiler.PredefinedAttributes.CompilerGenerated.EmitAttribute (TypeBuilder);
1756
1757                         base.Emit ();
1758                 }
1759
1760                 // TODO: move to ClassOrStruct
1761                 void EmitConstructors ()
1762                 {
1763                         if (instance_constructors == null)
1764                                 return;
1765
1766                         if (spec.IsAttribute && IsExposedFromAssembly () && RootContext.VerifyClsCompliance && IsClsComplianceRequired ()) {
1767                                 bool has_compliant_args = false;
1768
1769                                 foreach (Constructor c in instance_constructors) {
1770                                         try {
1771                                                 c.Emit ();
1772                                         }
1773                                         catch (Exception e) {
1774                                                 throw new InternalErrorException (c, e);
1775                                         }
1776
1777                                         if (has_compliant_args)
1778                                                 continue;
1779
1780                                         has_compliant_args = c.HasCompliantArgs;
1781                                 }
1782                                 if (!has_compliant_args)
1783                                         Report.Warning (3015, 1, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
1784                         } else {
1785                                 foreach (Constructor c in instance_constructors) {
1786                                         try {
1787                                                 c.Emit ();
1788                                         }
1789                                         catch (Exception e) {
1790                                                 throw new InternalErrorException (c, e);
1791                                         }
1792                                 }
1793                         }
1794                 }
1795
1796                 /// <summary>
1797                 ///   Emits the code, this step is performed after all
1798                 ///   the types, enumerations, constructors
1799                 /// </summary>
1800                 public virtual void EmitType ()
1801                 {
1802                         if (OptAttributes != null)
1803                                 OptAttributes.Emit ();
1804
1805                         Emit ();
1806
1807                         EmitConstructors ();
1808
1809                         if (constants != null)
1810                                 foreach (Const con in constants)
1811                                         con.Emit ();
1812
1813                         if (default_static_constructor != null)
1814                                 default_static_constructor.Emit ();
1815                         
1816                         if (operators != null)
1817                                 foreach (Operator o in operators)
1818                                         o.Emit ();
1819
1820                         if (properties != null)
1821                                 foreach (Property p in properties)
1822                                         p.Emit ();
1823
1824                         if (indexers != null) {
1825                                 foreach (Indexer indx in indexers)
1826                                         indx.Emit ();
1827                                 EmitIndexerName ();
1828                         }
1829
1830                         if (events != null){
1831                                 foreach (Event e in Events)
1832                                         e.Emit ();
1833                         }
1834
1835                         if (methods != null) {
1836                                 for (int i = 0; i < methods.Count; ++i)
1837                                         ((MethodOrOperator) methods [i]).Emit ();
1838                         }
1839                         
1840                         if (fields != null)
1841                                 foreach (FieldBase f in fields)
1842                                         f.Emit ();
1843
1844                         if (types != null) {
1845                                 foreach (TypeContainer t in types)
1846                                         t.EmitType ();
1847                         }
1848
1849                         if (pending != null)
1850                                 pending.VerifyPendingMethods (Report);
1851
1852                         if (Report.Errors > 0)
1853                                 return;
1854
1855                         if (compiler_generated != null) {
1856                                 for (int i = 0; i < compiler_generated.Count; ++i)
1857                                         compiler_generated [i].EmitType ();
1858                         }
1859                 }
1860
1861                 public void CloseType ()
1862                 {
1863                         if ((caching_flags & Flags.CloseTypeCreated) != 0)
1864                                 return;
1865
1866                         // Close base type container first to avoid TypeLoadException
1867                         if (spec.BaseType != null) {
1868                                 var btype = spec.BaseType.MemberDefinition as TypeContainer;
1869                                 if (btype != null)
1870                                         btype.CloseType ();
1871                         }
1872
1873                         try {
1874                                 caching_flags |= Flags.CloseTypeCreated;
1875                                 TypeBuilder.CreateType ();
1876                         } catch (TypeLoadException){
1877                                 //
1878                                 // This is fine, the code still created the type
1879                                 //
1880 //                              Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
1881 //                              Console.WriteLine (e.Message);
1882                         } catch (Exception e) {
1883                                 throw new InternalErrorException (this, e);
1884                         }
1885                         
1886                         if (Types != null){
1887                                 foreach (TypeContainer tc in Types)
1888                                         tc.CloseType ();
1889                         }
1890
1891                         if (compiler_generated != null)
1892                                 foreach (CompilerGeneratedClass c in compiler_generated)
1893                                         c.CloseType ();
1894                         
1895                         types = null;
1896                         fields = null;
1897                         initialized_fields = null;
1898                         initialized_static_fields = null;
1899                         constants = null;
1900                         ordered_explicit_member_list = null;
1901                         ordered_member_list = null;
1902                         methods = null;
1903                         events = null;
1904                         indexers = null;
1905                         operators = null;
1906                         compiler_generated = null;
1907                         default_constructor = null;
1908                         default_static_constructor = null;
1909                         type_bases = null;
1910                         OptAttributes = null;
1911                 }
1912
1913                 //
1914                 // Performs the validation on a Method's modifiers (properties have
1915                 // the same properties).
1916                 //
1917                 // TODO: Why is it not done at parse stage ?
1918                 //
1919                 public bool MethodModifiersValid (MemberCore mc)
1920                 {
1921                         const Modifiers vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
1922                         const Modifiers nv = (Modifiers.NEW | Modifiers.VIRTUAL);
1923                         bool ok = true;
1924                         var flags = mc.ModFlags;
1925                         
1926                         //
1927                         // At most one of static, virtual or override
1928                         //
1929                         if ((flags & Modifiers.STATIC) != 0){
1930                                 if ((flags & vao) != 0){
1931                                         Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
1932                                                 mc.GetSignatureForError ());
1933                                         ok = false;
1934                                 }
1935                         }
1936
1937                         if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
1938                                 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
1939                                         mc.GetSignatureForError ());
1940                                 ok = false;
1941                         }
1942
1943                         //
1944                         // If the declaration includes the abstract modifier, then the
1945                         // declaration does not include static, virtual or extern
1946                         //
1947                         if ((flags & Modifiers.ABSTRACT) != 0){
1948                                 if ((flags & Modifiers.EXTERN) != 0){
1949                                         Report.Error (
1950                                                 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
1951                                         ok = false;
1952                                 }
1953
1954                                 if ((flags & Modifiers.SEALED) != 0) {
1955                                         Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
1956                                         ok = false;
1957                                 }
1958
1959                                 if ((flags & Modifiers.VIRTUAL) != 0){
1960                                         Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
1961                                         ok = false;
1962                                 }
1963
1964                                 if ((ModFlags & Modifiers.ABSTRACT) == 0){
1965                                         Report.SymbolRelatedToPreviousError (this);
1966                                         Report.Error (513, mc.Location, "`{0}' is abstract but it is declared in the non-abstract class `{1}'",
1967                                                 mc.GetSignatureForError (), GetSignatureForError ());
1968                                         ok = false;
1969                                 }
1970                         }
1971
1972                         if ((flags & Modifiers.PRIVATE) != 0){
1973                                 if ((flags & vao) != 0){
1974                                         Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
1975                                         ok = false;
1976                                 }
1977                         }
1978
1979                         if ((flags & Modifiers.SEALED) != 0){
1980                                 if ((flags & Modifiers.OVERRIDE) == 0){
1981                                         Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
1982                                         ok = false;
1983                                 }
1984                         }
1985
1986                         return ok;
1987                 }
1988
1989                 public Constructor DefaultStaticConstructor {
1990                         get { return default_static_constructor; }
1991                 }
1992
1993                 protected override bool VerifyClsCompliance ()
1994                 {
1995                         if (!base.VerifyClsCompliance ())
1996                                 return false;
1997
1998                         // Check this name against other containers
1999                         NamespaceEntry.NS.VerifyClsCompliance ();
2000
2001                         // Check all container names for user classes
2002                         if (Kind != MemberKind.Delegate)
2003                                 MemberCache.VerifyClsCompliance (Definition, Report);
2004
2005                         if (BaseType != null && !BaseType.IsCLSCompliant ()) {
2006                                 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant",
2007                                         GetSignatureForError (), BaseType.GetSignatureForError ());
2008                         }
2009                         return true;
2010                 }
2011
2012                 /// <summary>
2013                 ///   Performs checks for an explicit interface implementation.  First it
2014                 ///   checks whether the `interface_type' is a base inteface implementation.
2015                 ///   Then it checks whether `name' exists in the interface type.
2016                 /// </summary>
2017                 public bool VerifyImplements (InterfaceMemberBase mb)
2018                 {
2019                         var ifaces = spec.Interfaces;
2020                         if (ifaces != null) {
2021                                 foreach (TypeSpec t in ifaces){
2022                                         if (t == mb.InterfaceType)
2023                                                 return true;
2024                                 }
2025                         }
2026                         
2027                         Report.SymbolRelatedToPreviousError (mb.InterfaceType);
2028                         Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2029                                 mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
2030                         return false;
2031                 }
2032
2033                 //
2034                 // Used for visiblity checks to tests whether this definition shares
2035                 // base type baseType, it does member-definition search
2036                 //
2037                 public bool IsBaseTypeDefinition (TypeSpec baseType)
2038                 {
2039                         // RootContext check
2040                         if (TypeBuilder == null)
2041                                 return false;
2042
2043                         var type = spec;
2044                         do {
2045                                 if (type.MemberDefinition == baseType.MemberDefinition)
2046                                         return true;
2047
2048                                 type = type.BaseType;
2049                         } while (type != null);
2050
2051                         return false;
2052                 }
2053
2054                 public MemberCache LoadMembers (TypeSpec declaringType)
2055                 {
2056                         throw new NotSupportedException ("Not supported for compiled definition " + GetSignatureForError ());
2057                 }
2058
2059                 public void Mark_HasEquals ()
2060                 {
2061                         cached_method |= CachedMethods.Equals;
2062                 }
2063
2064                 public void Mark_HasGetHashCode ()
2065                 {
2066                         cached_method |= CachedMethods.GetHashCode;
2067                 }
2068
2069                 /// <summary>
2070                 /// Method container contains Equals method
2071                 /// </summary>
2072                 public bool HasEquals {
2073                         get {
2074                                 return (cached_method & CachedMethods.Equals) != 0;
2075                         }
2076                 }
2077  
2078                 /// <summary>
2079                 /// Method container contains GetHashCode method
2080                 /// </summary>
2081                 public bool HasGetHashCode {
2082                         get {
2083                                 return (cached_method & CachedMethods.GetHashCode) != 0;
2084                         }
2085                 }
2086
2087                 public bool HasStaticFieldInitializer {
2088                         get {
2089                                 return (cached_method & CachedMethods.HasStaticFieldInitializer) != 0;
2090                         }
2091                         set {
2092                                 if (value)
2093                                         cached_method |= CachedMethods.HasStaticFieldInitializer;
2094                                 else
2095                                         cached_method &= ~CachedMethods.HasStaticFieldInitializer;
2096                         }
2097                 }
2098
2099                 //
2100                 // Generates xml doc comments (if any), and if required,
2101                 // handle warning report.
2102                 //
2103                 internal override void GenerateDocComment (DeclSpace ds)
2104                 {
2105                         DocUtil.GenerateTypeDocComment (this, ds, Report);
2106                 }
2107
2108                 public override string DocCommentHeader {
2109                         get { return "T:"; }
2110                 }
2111         }
2112
2113         public abstract class ClassOrStruct : TypeContainer
2114         {
2115                 Dictionary<SecurityAction, PermissionSet> declarative_security;
2116
2117                 public ClassOrStruct (NamespaceEntry ns, DeclSpace parent,
2118                                       MemberName name, Attributes attrs, MemberKind kind)
2119                         : base (ns, parent, name, attrs, kind)
2120                 {
2121                 }
2122
2123                 protected override bool AddToContainer (MemberCore symbol, string name)
2124                 {
2125                         if (!(symbol is Constructor) && symbol.MemberName.Name == MemberName.Name) {
2126                                 if (symbol is TypeParameter) {
2127                                         Report.Error (694, symbol.Location,
2128                                                 "Type parameter `{0}' has same name as containing type, or method",
2129                                                 symbol.GetSignatureForError ());
2130                                         return false;
2131                                 }
2132                         
2133                                 InterfaceMemberBase imb = symbol as InterfaceMemberBase;
2134                                 if (imb == null || !imb.IsExplicitImpl) {
2135                                         Report.SymbolRelatedToPreviousError (this);
2136                                         Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
2137                                                 symbol.GetSignatureForError ());
2138                                         return false;
2139                                 }
2140                         }
2141
2142                         return base.AddToContainer (symbol, name);
2143                 }
2144
2145                 public override void VerifyMembers ()
2146                 {
2147                         base.VerifyMembers ();
2148
2149                         if ((events != null) && Report.WarningLevel >= 3) {
2150                                 foreach (Event e in events){
2151                                         // Note: The event can be assigned from same class only, so we can report
2152                                         // this warning for all accessibility modes
2153                                         if ((e.caching_flags & Flags.IsUsed) == 0)
2154                                                 Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
2155                                 }
2156                         }
2157                 }
2158
2159                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2160                 {
2161                         if (a.IsValidSecurityAttribute ()) {
2162                                 if (declarative_security == null)
2163                                         declarative_security = new Dictionary<SecurityAction, PermissionSet> ();
2164
2165                                 a.ExtractSecurityPermissionSet (declarative_security);
2166                                 return;
2167                         }
2168
2169                         if (a.Type == pa.StructLayout) {
2170                                 PartialContainer.HasStructLayout = true;
2171
2172                                 if (a.GetLayoutKindValue () == LayoutKind.Explicit)
2173                                         PartialContainer.HasExplicitLayout = true;
2174                         }
2175
2176                         if (a.Type == pa.Dynamic) {
2177                                 a.Error_MisusedDynamicAttribute ();
2178                                 return;
2179                         }
2180
2181                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2182                 }
2183
2184                 /// <summary>
2185                 /// Defines the default constructors 
2186                 /// </summary>
2187                 protected void DefineDefaultConstructor (bool is_static)
2188                 {
2189                         // The default instance constructor is public
2190                         // If the class is abstract, the default constructor is protected
2191                         // The default static constructor is private
2192
2193                         Modifiers mods;
2194                         if (is_static) {
2195                                 mods = Modifiers.STATIC | Modifiers.PRIVATE;
2196                         } else {
2197                                 mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
2198                         }
2199
2200                         Constructor c = new Constructor (this, MemberName.Name, mods,
2201                                 null, ParametersCompiled.EmptyReadOnlyParameters,
2202                                 new GeneratedBaseInitializer (Location),
2203                                 Location);
2204                         
2205                         AddConstructor (c);
2206                         c.Block = new ToplevelBlock (Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location);
2207                 }
2208
2209                 protected override bool DoDefineMembers ()
2210                 {
2211                         CheckProtectedModifier ();
2212
2213                         base.DoDefineMembers ();
2214
2215                         if (default_static_constructor != null)
2216                                 default_static_constructor.Define ();
2217
2218                         return true;
2219                 }
2220
2221                 public override void Emit ()
2222                 {
2223                         if (default_static_constructor == null && PartialContainer.HasStaticFieldInitializer) {
2224                                 DefineDefaultConstructor (true);
2225                                 default_static_constructor.Define ();
2226                         }
2227
2228                         base.Emit ();
2229
2230                         if (declarative_security != null) {
2231                                 foreach (var de in declarative_security) {
2232                                         TypeBuilder.AddDeclarativeSecurity (de.Key, de.Value);
2233                                 }
2234                         }
2235                 }
2236
2237                 public override IList<MethodSpec> LookupExtensionMethod (TypeSpec extensionType, string name, int arity, ref NamespaceEntry scope)
2238                 {
2239                         DeclSpace top_level = Parent;
2240                         if (top_level != null) {
2241                                 while (top_level.Parent != null)
2242                                         top_level = top_level.Parent;
2243
2244                                 var candidates = NamespaceEntry.NS.LookupExtensionMethod (extensionType, this, name, arity);
2245                                 if (candidates != null) {
2246                                         scope = NamespaceEntry;
2247                                         return candidates;
2248                                 }
2249                         }
2250
2251                         return NamespaceEntry.LookupExtensionMethod (extensionType, name, arity, ref scope);
2252                 }
2253
2254                 protected override TypeAttributes TypeAttr {
2255                         get {
2256                                 if (default_static_constructor == null)
2257                                         return base.TypeAttr | TypeAttributes.BeforeFieldInit;
2258
2259                                 return base.TypeAttr;
2260                         }
2261                 }
2262         }
2263
2264
2265         // TODO: should be sealed
2266         public class Class : ClassOrStruct {
2267                 const Modifiers AllowedModifiers =
2268                         Modifiers.NEW |
2269                         Modifiers.PUBLIC |
2270                         Modifiers.PROTECTED |
2271                         Modifiers.INTERNAL |
2272                         Modifiers.PRIVATE |
2273                         Modifiers.ABSTRACT |
2274                         Modifiers.SEALED |
2275                         Modifiers.STATIC |
2276                         Modifiers.UNSAFE;
2277
2278                 public const TypeAttributes StaticClassAttribute = TypeAttributes.Abstract | TypeAttributes.Sealed;
2279
2280                 public Class (NamespaceEntry ns, DeclSpace parent, MemberName name, Modifiers mod,
2281                               Attributes attrs)
2282                         : base (ns, parent, name, attrs, MemberKind.Class)
2283                 {
2284                         var accmods = (Parent == null || Parent.Parent == null) ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2285                         this.ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod, accmods, Location, Report);
2286                         spec = new TypeSpec (Kind, null, this, null, ModFlags);
2287
2288                         if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
2289                                 Report.FeatureIsNotAvailable (Location, "static classes");
2290                         }
2291                 }
2292
2293                 public override void AddBasesForPart (DeclSpace part, List<FullNamedExpression> bases)
2294                 {
2295                         if (part.Name == "System.Object")
2296                                 Report.Error (537, part.Location,
2297                                         "The class System.Object cannot have a base class or implement an interface.");
2298                         base.AddBasesForPart (part, bases);
2299                 }
2300
2301                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2302                 {
2303                         if (a.Type == pa.AttributeUsage) {
2304                                 if (!BaseType.IsAttribute && spec != TypeManager.attribute_type) {
2305                                         Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2306                                 }
2307                         }
2308
2309                         if (a.Type == pa.Conditional && !BaseType.IsAttribute) {
2310                                 Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2311                                 return;
2312                         }
2313
2314                         if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
2315                                 a.Error_MissingGuidAttribute ();
2316                                 return;
2317                         }
2318
2319                         if (a.Type == pa.Extension) {
2320                                 a.Error_MisusedExtensionAttribute ();
2321                                 return;
2322                         }
2323
2324                         if (a.Type.IsConditionallyExcluded (Location))
2325                                 return;
2326
2327                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2328                 }
2329
2330                 public override AttributeTargets AttributeTargets {
2331                         get {
2332                                 return AttributeTargets.Class;
2333                         }
2334                 }
2335
2336                 protected override void DefineContainerMembers (System.Collections.IList list)
2337                 {
2338                         if (list == null)
2339                                 return;
2340
2341                         if (!IsStatic) {
2342                                 base.DefineContainerMembers (list);
2343                                 return;
2344                         }
2345
2346                         foreach (MemberCore m in list) {
2347                                 if (m is Operator) {
2348                                         Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2349                                         continue;
2350                                 }
2351
2352                                 if (m is Destructor) {
2353                                         Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2354                                         continue;
2355                                 }
2356
2357                                 if (m is Indexer) {
2358                                         Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2359                                         continue;
2360                                 }
2361
2362                                 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2363                                         continue;
2364
2365                                 if (m is Constructor) {
2366                                         Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2367                                         continue;
2368                                 }
2369
2370                                 Method method = m as Method;
2371                                 if (method != null && method.ParameterInfo.HasExtensionMethodType) {
2372                                         Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static", m.GetSignatureForError ());
2373                                         continue;
2374                                 }
2375
2376                                 Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2377                         }
2378
2379                         base.DefineContainerMembers (list);
2380                 }
2381
2382                 protected override bool DoDefineMembers ()
2383                 {
2384                         if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2385                                 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2386                         }
2387
2388                         if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2389                                 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2390                         }
2391
2392                         if (InstanceConstructors == null && !IsStatic)
2393                                 DefineDefaultConstructor (false);
2394
2395                         return base.DoDefineMembers ();
2396                 }
2397
2398                 public override void Emit ()
2399                 {
2400                         base.Emit ();
2401
2402                         if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
2403                                 Compiler.PredefinedAttributes.Extension.EmitAttribute (TypeBuilder);
2404
2405                         var trans_flags = TypeManager.HasDynamicTypeUsed (base_type);
2406                         if (trans_flags != null) {
2407                                 var pa = Compiler.PredefinedAttributes.DynamicTransform;
2408                                 if (pa.Constructor != null || pa.ResolveConstructor (Location, ArrayContainer.MakeType (TypeManager.bool_type))) {
2409                                         TypeBuilder.SetCustomAttribute (new CustomAttributeBuilder (pa.Constructor, new object[] { trans_flags }));
2410                                 }
2411                         }
2412                 }
2413
2414                 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
2415                 {
2416                         TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
2417
2418                         if (base_class == null) {
2419                                 if (spec != TypeManager.object_type)
2420                                         base_type = TypeManager.object_type;
2421                         } else {
2422                                 if (base_type.IsGenericParameter){
2423                                         Report.Error (689, base_class.Location, "`{0}': Cannot derive from type parameter `{1}'",
2424                                                 GetSignatureForError (), base_type.GetSignatureForError ());
2425                                 } else if (IsGeneric && base_type.IsAttribute) {
2426                                         Report.Error (698, base_class.Location,
2427                                                 "A generic type cannot derive from `{0}' because it is an attribute class",
2428                                                 base_class.GetSignatureForError ());
2429                                 } else if (base_type.IsStatic) {
2430                                         Report.SymbolRelatedToPreviousError (base_class.Type);
2431                                         Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
2432                                                 GetSignatureForError (), base_type.GetSignatureForError ());
2433                                 } else if (base_type.IsSealed) {
2434                                         Report.SymbolRelatedToPreviousError (base_class.Type);
2435                                         Report.Error (509, Location, "`{0}': cannot derive from sealed type `{1}'",
2436                                                 GetSignatureForError (), base_type.GetSignatureForError ());
2437                                 } else if (PartialContainer.IsStatic && base_class.Type != TypeManager.object_type) {
2438                                         Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object",
2439                                                 GetSignatureForError (), base_class.GetSignatureForError ());
2440                                 }
2441
2442                                 if (base_type is PredefinedTypeSpec && !(spec is PredefinedTypeSpec) &&
2443                                         (base_type == TypeManager.enum_type || base_type == TypeManager.value_type || base_type == TypeManager.multicast_delegate_type ||
2444                                         base_type == TypeManager.delegate_type || base_type == TypeManager.array_type)) {
2445                                         Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
2446                                                 GetSignatureForError (), base_type.GetSignatureForError ());
2447
2448                                         base_type = TypeManager.object_type;
2449                                 }
2450
2451                                 if (!IsAccessibleAs (base_type)) {
2452                                         Report.SymbolRelatedToPreviousError (base_type);
2453                                         Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'",
2454                                                 base_type.GetSignatureForError (), GetSignatureForError ());
2455                                 }
2456                         }
2457
2458                         if (PartialContainer.IsStatic && ifaces != null) {
2459                                 foreach (TypeExpr t in ifaces)
2460                                         Report.SymbolRelatedToPreviousError (t.Type);
2461                                 Report.Error (714, Location, "Static class `{0}' cannot implement interfaces", GetSignatureForError ());
2462                         }
2463
2464                         return ifaces;
2465                 }
2466
2467                 /// Search for at least one defined condition in ConditionalAttribute of attribute class
2468                 /// Valid only for attribute classes.
2469                 public override string[] ConditionalConditions ()
2470                 {
2471                         if ((caching_flags & (Flags.Excluded_Undetected | Flags.Excluded)) == 0)
2472                                 return null;
2473
2474                         caching_flags &= ~Flags.Excluded_Undetected;
2475
2476                         if (OptAttributes == null)
2477                                 return null;
2478
2479                         Attribute[] attrs = OptAttributes.SearchMulti (Compiler.PredefinedAttributes.Conditional);
2480                         if (attrs == null)
2481                                 return null;
2482
2483                         string[] conditions = new string[attrs.Length];
2484                         for (int i = 0; i < conditions.Length; ++i)
2485                                 conditions[i] = attrs[i].GetConditionalAttributeValue ();
2486
2487                         caching_flags |= Flags.Excluded;
2488                         return conditions;
2489                 }
2490
2491                 //
2492                 // FIXME: How do we deal with the user specifying a different
2493                 // layout?
2494                 //
2495                 protected override TypeAttributes TypeAttr {
2496                         get {
2497                                 TypeAttributes ta = base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
2498                                 if (IsStatic)
2499                                         ta |= StaticClassAttribute;
2500                                 return ta;
2501                         }
2502                 }
2503         }
2504
2505         public sealed class Struct : ClassOrStruct {
2506
2507                 bool is_unmanaged, has_unmanaged_check_done;
2508                 bool InTransit;
2509
2510                 // <summary>
2511                 //   Modifiers allowed in a struct declaration
2512                 // </summary>
2513                 const Modifiers AllowedModifiers =
2514                         Modifiers.NEW       |
2515                         Modifiers.PUBLIC    |
2516                         Modifiers.PROTECTED |
2517                         Modifiers.INTERNAL  |
2518                         Modifiers.UNSAFE    |
2519                         Modifiers.PRIVATE;
2520
2521                 public Struct (NamespaceEntry ns, DeclSpace parent, MemberName name,
2522                                Modifiers mod, Attributes attrs)
2523                         : base (ns, parent, name, attrs, MemberKind.Struct)
2524                 {
2525                         var accmods = parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;                   
2526                         this.ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod, accmods, Location, Report) | Modifiers.SEALED ;
2527                         spec = new TypeSpec (Kind, null, this, null, ModFlags);
2528                 }
2529
2530                 public override AttributeTargets AttributeTargets {
2531                         get {
2532                                 return AttributeTargets.Struct;
2533                         }
2534                 }
2535
2536                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2537                 {
2538                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2539
2540                         //
2541                         // When struct constains fixed fixed and struct layout has explicitly
2542                         // set CharSet, its value has to be propagated to compiler generated
2543                         // fixed field types
2544                         //
2545                         if (a.Type == pa.StructLayout && Fields != null && a.HasField ("CharSet")) {
2546                                 for (int i = 0; i < Fields.Count; ++i) {
2547                                         FixedField ff = Fields [i] as FixedField;
2548                                         if (ff != null)
2549                                                 ff.SetCharSet (TypeBuilder.Attributes);
2550                                 }
2551                         }
2552                 }
2553
2554                 bool CheckStructCycles (Struct s)
2555                 {
2556                         if (s.Fields == null)
2557                                 return true;
2558
2559                         if (s.InTransit)
2560                                 return false;
2561
2562                         s.InTransit = true;
2563                         foreach (FieldBase field in s.Fields) {
2564                                 TypeSpec ftype = field.Spec.MemberType;
2565                                 if (!ftype.IsStruct)
2566                                         continue;
2567
2568                                 if (ftype is PredefinedTypeSpec)
2569                                         continue;
2570
2571                                 foreach (var targ in ftype.TypeArguments) {
2572                                         if (!CheckFieldTypeCycle (targ)) {
2573                                                 Report.Error (523, field.Location,
2574                                                         "Struct member `{0}' of type `{1}' causes a cycle in the struct layout",
2575                                                         field.GetSignatureForError (), ftype.GetSignatureForError ());
2576                                                 break;
2577                                         }
2578                                 }
2579
2580                                 if ((field.IsStatic && (!ftype.IsGeneric || ftype == CurrentType)))
2581                                         continue;
2582
2583                                 if (!CheckFieldTypeCycle (ftype)) {
2584                                         Report.Error (523, field.Location,
2585                                                 "Struct member `{0}' of type `{1}' causes a cycle in the struct layout",
2586                                                 field.GetSignatureForError (), ftype.GetSignatureForError ());
2587                                         break;
2588                                 }
2589                         }
2590
2591                         s.InTransit = false;
2592                         return true;
2593                 }
2594
2595                 bool CheckFieldTypeCycle (TypeSpec ts)
2596                 {
2597                         var fts = ts.MemberDefinition as Struct;
2598                         if (fts == null)
2599                                 return true;
2600
2601                         return CheckStructCycles (fts);
2602                 }
2603
2604                 public override void Emit ()
2605                 {
2606                         CheckStructCycles (this);
2607
2608                         base.Emit ();
2609                 }
2610
2611                 public override bool IsUnmanagedType ()
2612                 {
2613                         if (fields == null)
2614                                 return true;
2615
2616                         if (has_unmanaged_check_done)
2617                                 return is_unmanaged;
2618
2619                         if (requires_delayed_unmanagedtype_check)
2620                                 return true;
2621
2622                         requires_delayed_unmanagedtype_check = true;
2623
2624                         foreach (FieldBase f in fields) {
2625                                 if (f.IsStatic)
2626                                         continue;
2627
2628                                 // It can happen when recursive unmanaged types are defined
2629                                 // struct S { S* s; }
2630                                 TypeSpec mt = f.MemberType;
2631                                 if (mt == null) {
2632                                         return true;
2633                                 }
2634
2635                                 while (mt.IsPointer)
2636                                         mt = TypeManager.GetElementType (mt);
2637
2638                                 if (mt.MemberDefinition == this) {
2639                                         for (var p = Parent; p != null; p = p.Parent) {
2640                                                 if (p.Kind == MemberKind.Class) {
2641                                                         has_unmanaged_check_done = true;
2642                                                         return false;
2643                                                 }
2644                                         }
2645                                         continue;
2646                                 }
2647
2648                                 if (TypeManager.IsUnmanagedType (mt))
2649                                         continue;
2650
2651                                 has_unmanaged_check_done = true;
2652                                 return false;
2653                         }
2654
2655                         has_unmanaged_check_done = true;
2656                         is_unmanaged = true;
2657                         return true;
2658                 }
2659
2660                 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
2661                 {
2662                         TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
2663                         base_type = TypeManager.value_type;
2664                         return ifaces;
2665                 }
2666
2667                 protected override TypeAttributes TypeAttr {
2668                         get {
2669                                 const TypeAttributes DefaultTypeAttributes =
2670                                         TypeAttributes.SequentialLayout |
2671                                         TypeAttributes.Sealed;
2672
2673                                 return base.TypeAttr | DefaultTypeAttributes;
2674                         }
2675                 }
2676
2677                 public override void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
2678                 {
2679                         if ((field.ModFlags & Modifiers.STATIC) == 0) {
2680                                 Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
2681                                         field.GetSignatureForError ());
2682                                 return;
2683                         }
2684                         base.RegisterFieldForInitialization (field, expression);
2685                 }
2686
2687         }
2688
2689         /// <summary>
2690         ///   Interfaces
2691         /// </summary>
2692         public sealed class Interface : TypeContainer {
2693
2694                 /// <summary>
2695                 ///   Modifiers allowed in a class declaration
2696                 /// </summary>
2697                 public const Modifiers AllowedModifiers =
2698                         Modifiers.NEW       |
2699                         Modifiers.PUBLIC    |
2700                         Modifiers.PROTECTED |
2701                         Modifiers.INTERNAL  |
2702                         Modifiers.UNSAFE    |
2703                         Modifiers.PRIVATE;
2704
2705                 public Interface (NamespaceEntry ns, DeclSpace parent, MemberName name, Modifiers mod,
2706                                   Attributes attrs)
2707                         : base (ns, parent, name, attrs, MemberKind.Interface)
2708                 {
2709                         var accmods = parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2710
2711                         this.ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod, accmods, name.Location, Report);
2712                         spec = new TypeSpec (Kind, null, this, null, ModFlags);
2713                 }
2714
2715                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2716                 {
2717                         if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
2718                                 a.Error_MissingGuidAttribute ();
2719                                 return;
2720                         }
2721
2722                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2723                 }
2724
2725
2726                 public override AttributeTargets AttributeTargets {
2727                         get {
2728                                 return AttributeTargets.Interface;
2729                         }
2730                 }
2731
2732                 protected override TypeAttributes TypeAttr {
2733                         get {
2734                                 const TypeAttributes DefaultTypeAttributes =
2735                                         TypeAttributes.AutoLayout |
2736                                         TypeAttributes.Abstract |
2737                                         TypeAttributes.Interface;
2738
2739                                 return base.TypeAttr | DefaultTypeAttributes;
2740                         }
2741                 }
2742
2743                 protected override bool VerifyClsCompliance ()
2744                 {
2745                         if (!base.VerifyClsCompliance ())
2746                                 return false;
2747
2748                         if (iface_exprs != null) {
2749                                 foreach (var iface in iface_exprs) {
2750                                         if (iface.Type.IsCLSCompliant ())
2751                                                 continue;
2752
2753                                         Report.SymbolRelatedToPreviousError (iface.Type);
2754                                         Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
2755                                                 GetSignatureForError (), TypeManager.CSharpName (iface.Type));
2756                                 }
2757                         }
2758
2759                         return true;
2760                 }
2761         }
2762
2763         public abstract class InterfaceMemberBase : MemberBase
2764         {
2765                 //
2766                 // Common modifiers allowed in a class declaration
2767                 //
2768                 protected const Modifiers AllowedModifiersClass =
2769                         Modifiers.NEW |
2770                         Modifiers.PUBLIC |
2771                         Modifiers.PROTECTED |
2772                         Modifiers.INTERNAL |
2773                         Modifiers.PRIVATE |
2774                         Modifiers.STATIC |
2775                         Modifiers.VIRTUAL |
2776                         Modifiers.SEALED |
2777                         Modifiers.OVERRIDE |
2778                         Modifiers.ABSTRACT |
2779                         Modifiers.UNSAFE |
2780                         Modifiers.EXTERN;
2781
2782                 //
2783                 // Common modifiers allowed in a struct declaration
2784                 //
2785                 protected const Modifiers AllowedModifiersStruct =
2786                         Modifiers.NEW |
2787                         Modifiers.PUBLIC |
2788                         Modifiers.PROTECTED |
2789                         Modifiers.INTERNAL |
2790                         Modifiers.PRIVATE |
2791                         Modifiers.STATIC |
2792                         Modifiers.OVERRIDE |
2793                         Modifiers.UNSAFE |
2794                         Modifiers.EXTERN;
2795
2796                 //
2797                 // Common modifiers allowed in a interface declaration
2798                 //
2799                 protected const Modifiers AllowedModifiersInterface =
2800                         Modifiers.NEW |
2801                         Modifiers.UNSAFE;
2802
2803                 //
2804                 // Whether this is an interface member.
2805                 //
2806                 public bool IsInterface;
2807
2808                 //
2809                 // If true, this is an explicit interface implementation
2810                 //
2811                 public bool IsExplicitImpl;
2812
2813                 protected bool is_external_implementation;
2814
2815                 //
2816                 // The interface type we are explicitly implementing
2817                 //
2818                 public TypeSpec InterfaceType;
2819
2820                 //
2821                 // The method we're overriding if this is an override method.
2822                 //
2823                 protected MethodSpec base_method;
2824
2825                 readonly Modifiers explicit_mod_flags;
2826                 public MethodAttributes flags;
2827
2828                 public InterfaceMemberBase (DeclSpace parent, GenericMethod generic,
2829                                    FullNamedExpression type, Modifiers mod, Modifiers allowed_mod,
2830                                    MemberName name, Attributes attrs)
2831                         : base (parent, generic, type, mod, allowed_mod, Modifiers.PRIVATE,
2832                                 name, attrs)
2833                 {
2834                         IsInterface = parent.PartialContainer.Kind == MemberKind.Interface;
2835                         IsExplicitImpl = (MemberName.Left != null);
2836                         explicit_mod_flags = mod;
2837                 }
2838                 
2839                 protected override bool CheckBase ()
2840                 {
2841                         if (!base.CheckBase ())
2842                                 return false;
2843
2844                         if ((caching_flags & Flags.MethodOverloadsExist) != 0)
2845                                 CheckForDuplications ();
2846                         
2847                         if (IsExplicitImpl)
2848                                 return true;
2849
2850                         // For System.Object only
2851                         if (Parent.BaseType == null)
2852                                 return true;
2853
2854                         MemberSpec candidate;
2855                         var base_member = FindBaseMember (out candidate);
2856
2857                         if ((ModFlags & Modifiers.OVERRIDE) != 0) {
2858                                 if (base_member == null) {
2859                                         if (candidate == null) {
2860                                                 if (this is Method && ((Method)this).ParameterInfo.IsEmpty && MemberName.Name == Destructor.MetadataName && MemberName.Arity == 0) {
2861                                                         Report.Error (249, Location, "Do not override `{0}'. Use destructor syntax instead",
2862                                                                 "object.Finalize()");
2863                                                 } else {
2864                                                         Report.Error (115, Location, "`{0}' is marked as an override but no suitable {1} found to override",
2865                                                                 GetSignatureForError (), SimpleName.GetMemberType (this));
2866                                                 }
2867                                         } else {
2868                                                 Report.SymbolRelatedToPreviousError (candidate);
2869                                                 if (this is Event)
2870                                                         Report.Error (72, Location, "`{0}': cannot override because `{1}' is not an event",
2871                                                                 GetSignatureForError (), TypeManager.GetFullNameSignature (candidate));
2872                                                 else if (this is PropertyBase)
2873                                                         Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property",
2874                                                                 GetSignatureForError (), TypeManager.GetFullNameSignature (candidate));
2875                                                 else
2876                                                         Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method",
2877                                                                 GetSignatureForError (), TypeManager.GetFullNameSignature (candidate));
2878                                         }
2879
2880                                         return false;
2881                                 }
2882
2883                                 if (!CheckOverrideAgainstBase (base_member))
2884                                         return false;
2885
2886                                 ObsoleteAttribute oa = base_member.GetAttributeObsolete ();
2887                                 if (oa != null) {
2888                                         if (OptAttributes == null || !OptAttributes.Contains (Compiler.PredefinedAttributes.Obsolete)) {
2889                                                 Report.SymbolRelatedToPreviousError (base_member);
2890                                                 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
2891                                                         GetSignatureForError (), TypeManager.GetFullNameSignature (base_member));
2892                                         }
2893                                 } else {
2894                                         if (OptAttributes != null && OptAttributes.Contains (Compiler.PredefinedAttributes.Obsolete)) {
2895                                                 Report.SymbolRelatedToPreviousError (base_member);
2896                                                 Report.Warning (809, 1, Location, "Obsolete member `{0}' overrides non-obsolete member `{1}'",
2897                                                         GetSignatureForError (), TypeManager.GetFullNameSignature (base_member));
2898                                         }
2899                                 }
2900
2901                                 base_method = base_member as MethodSpec;
2902                                 return true;
2903                         }
2904
2905                         if (base_member == null && candidate != null && (!(candidate is IParametersMember) || !(this is IParametersMember)))
2906                                 base_member = candidate;
2907
2908                         if (base_member == null) {
2909                                 if ((ModFlags & Modifiers.NEW) != 0) {
2910                                         if (base_member == null) {
2911                                                 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required",
2912                                                         GetSignatureForError ());
2913                                         }
2914                                 }
2915                         } else {
2916                                 if ((ModFlags & Modifiers.NEW) == 0) {
2917                                         ModFlags |= Modifiers.NEW;
2918                                         Report.SymbolRelatedToPreviousError (base_member);
2919                                         if (!IsInterface && (base_member.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL | Modifiers.OVERRIDE)) != 0) {
2920                                                 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",
2921                                                         GetSignatureForError (), base_member.GetSignatureForError ());
2922                                         } else {
2923                                                 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
2924                                                         GetSignatureForError (), base_member.GetSignatureForError ());
2925                                         }
2926                                 }
2927
2928                                 if (!IsInterface && base_member.IsAbstract && candidate == null) {
2929                                         Report.SymbolRelatedToPreviousError (base_member);
2930                                         Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
2931                                                 GetSignatureForError (), base_member.GetSignatureForError ());
2932                                 }
2933                         }
2934
2935                         return true;
2936                 }
2937
2938                 protected virtual bool CheckForDuplications ()
2939                 {
2940                         return Parent.MemberCache.CheckExistingMembersOverloads (this, ParametersCompiled.EmptyReadOnlyParameters);
2941                 }
2942
2943                 //
2944                 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
2945                 // that have been defined.
2946                 //
2947                 protected virtual bool CheckOverrideAgainstBase (MemberSpec base_member)
2948                 {
2949                         bool ok = true;
2950
2951                         if ((base_member.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL | Modifiers.OVERRIDE)) == 0) {
2952                                 Report.SymbolRelatedToPreviousError (base_member);
2953                                 Report.Error (506, Location,
2954                                         "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
2955                                          GetSignatureForError (), TypeManager.CSharpSignature (base_member));
2956                                 ok = false;
2957                         }
2958
2959                         // Now we check that the overriden method is not final  
2960                         if ((base_member.Modifiers & Modifiers.SEALED) != 0) {
2961                                 Report.SymbolRelatedToPreviousError (base_member);
2962                                 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
2963                                                           GetSignatureForError (), TypeManager.CSharpSignature (base_member));
2964                                 ok = false;
2965                         }
2966
2967                         var base_member_type = ((IInterfaceMemberSpec) base_member).MemberType;
2968                         if (!TypeSpecComparer.Override.IsEqual (MemberType, base_member_type)) {
2969                                 Report.SymbolRelatedToPreviousError (base_member);
2970                                 if (this is PropertyBasedMember) {
2971                                         Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
2972                                                 GetSignatureForError (), TypeManager.CSharpName (base_member_type), TypeManager.CSharpSignature (base_member));
2973                                 } else {
2974                                         Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
2975                                                 GetSignatureForError (), TypeManager.CSharpName (base_member_type), TypeManager.CSharpSignature (base_member));
2976                                 }
2977                                 ok = false;
2978                         }
2979
2980                         return ok;
2981                 }
2982
2983                 protected static bool CheckAccessModifiers (MemberCore this_member, MemberSpec base_member)
2984                 {
2985                         var thisp = this_member.ModFlags & Modifiers.AccessibilityMask;
2986                         var base_classp = base_member.Modifiers & Modifiers.AccessibilityMask;
2987
2988                         if ((base_classp & (Modifiers.PROTECTED | Modifiers.INTERNAL)) == (Modifiers.PROTECTED | Modifiers.INTERNAL)) {
2989                                 //
2990                                 // when overriding protected internal, the method can be declared
2991                                 // protected internal only within the same assembly or assembly
2992                                 // which has InternalsVisibleTo
2993                                 //
2994                                 if ((thisp & (Modifiers.PROTECTED | Modifiers.INTERNAL)) == (Modifiers.PROTECTED | Modifiers.INTERNAL)) {
2995                                         return TypeManager.IsThisOrFriendAssembly (this_member.Assembly, base_member.Assembly);
2996                                 } 
2997                                 if ((thisp & Modifiers.PROTECTED) != Modifiers.PROTECTED) {
2998                                         //
2999                                         // if it's not "protected internal", it must be "protected"
3000                                         //
3001
3002                                         return false;
3003                                 }
3004                                 if (this_member.Parent.PartialContainer.Module.Assembly == base_member.Assembly) {
3005                                         //
3006                                         // protected within the same assembly - an error
3007                                         //
3008                                         return false;
3009                                 }
3010                                 if ((thisp & ~(Modifiers.PROTECTED | Modifiers.INTERNAL)) !=
3011                                            (base_classp & ~(Modifiers.PROTECTED | Modifiers.INTERNAL))) {
3012                                         //
3013                                         // protected ok, but other attributes differ - report an error
3014                                         //
3015                                         return false;
3016                                 }
3017                                 return true;
3018                         }
3019
3020                         return thisp == base_classp;
3021                 }
3022
3023                 public override bool Define ()
3024                 {
3025                         if (IsInterface) {
3026                                 ModFlags = Modifiers.PUBLIC | Modifiers.ABSTRACT |
3027                                         Modifiers.VIRTUAL | (ModFlags & (Modifiers.UNSAFE | Modifiers.NEW));
3028
3029                                 flags = MethodAttributes.Public |
3030                                         MethodAttributes.Abstract |
3031                                         MethodAttributes.HideBySig |
3032                                         MethodAttributes.NewSlot |
3033                                         MethodAttributes.Virtual;
3034                         } else {
3035                                 Parent.PartialContainer.MethodModifiersValid (this);
3036
3037                                 flags = ModifiersExtensions.MethodAttr (ModFlags);
3038                         }
3039
3040                         if (IsExplicitImpl) {
3041                                 TypeExpr iface_texpr = MemberName.Left.GetTypeExpression ().ResolveAsTypeTerminal (Parent, false);
3042                                 if (iface_texpr == null)
3043                                         return false;
3044
3045                                 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3046                                         Report.Error (754, Location, "A partial method `{0}' cannot explicitly implement an interface",
3047                                                 GetSignatureForError ());
3048                                 }
3049
3050                                 InterfaceType = iface_texpr.Type;
3051
3052                                 if (!InterfaceType.IsInterface) {
3053                                         Report.SymbolRelatedToPreviousError (InterfaceType);
3054                                         Report.Error (538, Location, "The type `{0}' in explicit interface declaration is not an interface",
3055                                                 TypeManager.CSharpName (InterfaceType));
3056                                 } else {
3057                                         Parent.PartialContainer.VerifyImplements (this);
3058                                 }
3059
3060                                 ModifiersExtensions.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location, Report);
3061                         }
3062
3063                         return base.Define ();
3064                 }
3065
3066                 protected bool DefineParameters (ParametersCompiled parameters)
3067                 {
3068                         if (!parameters.Resolve (this))
3069                                 return false;
3070
3071                         bool error = false;
3072                         for (int i = 0; i < parameters.Count; ++i) {
3073                                 Parameter p = parameters [i];
3074
3075                                 if (p.HasDefaultValue && (IsExplicitImpl || this is Operator || (this is Indexer && parameters.Count == 1)))
3076                                         p.Warning_UselessOptionalParameter (Report);
3077
3078                                 if (p.CheckAccessibility (this))
3079                                         continue;
3080
3081                                 TypeSpec t = parameters.Types [i];
3082                                 Report.SymbolRelatedToPreviousError (t);
3083                                 if (this is Indexer)
3084                                         Report.Error (55, Location,
3085                                                       "Inconsistent accessibility: parameter type `{0}' is less accessible than indexer `{1}'",
3086                                                       TypeManager.CSharpName (t), GetSignatureForError ());
3087                                 else if (this is Operator)
3088                                         Report.Error (57, Location,
3089                                                       "Inconsistent accessibility: parameter type `{0}' is less accessible than operator `{1}'",
3090                                                       TypeManager.CSharpName (t), GetSignatureForError ());
3091                                 else
3092                                         Report.Error (51, Location,
3093                                                 "Inconsistent accessibility: parameter type `{0}' is less accessible than method `{1}'",
3094                                                 TypeManager.CSharpName (t), GetSignatureForError ());
3095                                 error = true;
3096                         }
3097                         return !error;
3098                 }
3099
3100                 public override void Emit()
3101                 {
3102                         // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
3103                         // We are more strict than csc and report this as an error because SRE does not allow emit that
3104                         if ((ModFlags & Modifiers.EXTERN) != 0 && !is_external_implementation) {
3105                                 if (this is Constructor) {
3106                                         Report.Error (824, Location,
3107                                                 "Constructor `{0}' is marked `external' but has no external implementation specified", GetSignatureForError ());
3108                                 } else {
3109                                         Report.Error (626, Location,
3110                                                 "`{0}' is marked as an external but has no DllImport attribute. Consider adding a DllImport attribute to specify the external implementation",
3111                                                 GetSignatureForError ());
3112                                 }
3113                         }
3114
3115                         base.Emit ();
3116                 }
3117
3118                 public override bool EnableOverloadChecks (MemberCore overload)
3119                 {
3120                         //
3121                         // Two members can differ in their explicit interface
3122                         // type parameter only
3123                         //
3124                         InterfaceMemberBase imb = overload as InterfaceMemberBase;
3125                         if (imb != null && imb.IsExplicitImpl) {
3126                                 if (IsExplicitImpl) {
3127                                         caching_flags |= Flags.MethodOverloadsExist;
3128                                 }
3129                                 return true;
3130                         }
3131
3132                         return IsExplicitImpl;
3133                 }
3134
3135                 protected void Error_CannotChangeAccessModifiers (MemberCore member, MemberSpec base_member)
3136                 {
3137                         Report.SymbolRelatedToPreviousError (base_member);
3138                         Report.Error (507, member.Location,
3139                                 "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3140                                 member.GetSignatureForError (),
3141                                 ModifiersExtensions.AccessibilityName (base_member.Modifiers),
3142                                 base_member.GetSignatureForError ());
3143                 }
3144
3145                 protected void Error_StaticReturnType ()
3146                 {
3147                         Report.Error (722, Location,
3148                                 "`{0}': static types cannot be used as return types",
3149                                 MemberType.GetSignatureForError ());
3150                 }
3151
3152                 /// <summary>
3153                 /// Gets base method and its return type
3154                 /// </summary>
3155                 protected virtual MemberSpec FindBaseMember (out MemberSpec bestCandidate)
3156                 {
3157                         return MemberCache.FindBaseMember (this, out bestCandidate);
3158                 }
3159
3160                 //
3161                 // The "short" name of this property / indexer / event.  This is the
3162                 // name without the explicit interface.
3163                 //
3164                 public string ShortName {
3165                         get { return MemberName.Name; }
3166                         set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
3167                 }
3168                 
3169                 //
3170                 // Returns full metadata method name
3171                 //
3172                 public string GetFullName (MemberName name)
3173                 {
3174                         return GetFullName (name.Name);
3175                 }
3176
3177                 public string GetFullName (string name)
3178                 {
3179                         if (!IsExplicitImpl)
3180                                 return name;
3181
3182                         //
3183                         // When dealing with explicit members a full interface type
3184                         // name is added to member name to avoid possible name conflicts
3185                         //
3186                         // We use CSharpName which gets us full name with benefit of
3187                         // replacing predefined names which saves some space and name
3188                         // is still unique
3189                         //
3190                         return TypeManager.CSharpName (InterfaceType) + "." + name;
3191                 }
3192
3193                 protected override bool VerifyClsCompliance ()
3194                 {
3195                         if (!base.VerifyClsCompliance ()) {
3196                                 return false;
3197                         }
3198
3199                         if (GenericMethod != null)
3200                                 GenericMethod.VerifyClsCompliance ();
3201
3202                         return true;
3203                 }
3204
3205                 public override bool IsUsed 
3206                 {
3207                         get { return IsExplicitImpl || base.IsUsed; }
3208                 }
3209
3210         }
3211
3212         public abstract class MemberBase : MemberCore
3213         {
3214                 protected FullNamedExpression type_expr;
3215                 protected TypeSpec member_type;
3216
3217                 public readonly DeclSpace ds;
3218                 public readonly GenericMethod GenericMethod;
3219
3220                 protected MemberBase (DeclSpace parent, GenericMethod generic,
3221                                       FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, Modifiers def_mod,
3222                                       MemberName name, Attributes attrs)
3223                         : base (parent, name, attrs)
3224                 {
3225                         this.ds = generic != null ? generic : (DeclSpace) parent;
3226                         this.type_expr = type;
3227                         ModFlags = ModifiersExtensions.Check (allowed_mod, mod, def_mod, Location, Report);
3228                         GenericMethod = generic;
3229                         if (GenericMethod != null)
3230                                 GenericMethod.ModFlags = ModFlags;
3231                 }
3232
3233                 #region Properties
3234
3235                 public TypeSpec MemberType {
3236                         get {
3237                                 return member_type;
3238                         }
3239                 }
3240
3241                 public FullNamedExpression TypeExpression {
3242                         get {
3243                                 return type_expr;
3244                         }
3245                 }
3246
3247                 #endregion
3248
3249                 //
3250                 // Main member define entry
3251                 //
3252                 public override bool Define ()
3253                 {
3254                         DoMemberTypeIndependentChecks ();
3255
3256                         //
3257                         // Returns false only when type resolution failed
3258                         //
3259                         if (!ResolveMemberType ())
3260                                 return false;
3261
3262                         DoMemberTypeDependentChecks ();
3263                         return true;
3264                 }
3265
3266                 //
3267                 // Any type_name independent checks
3268                 //
3269                 protected virtual void DoMemberTypeIndependentChecks ()
3270                 {
3271                         if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
3272                                 (ModFlags & (Modifiers.VIRTUAL | Modifiers.ABSTRACT)) != 0) {
3273                                 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
3274                                         GetSignatureForError (), Parent.GetSignatureForError ());
3275                         }
3276                 }
3277
3278                 //
3279                 // Any type_name dependent checks
3280                 //
3281                 protected virtual void DoMemberTypeDependentChecks ()
3282                 {
3283                         // verify accessibility
3284                         if (!IsAccessibleAs (MemberType)) {
3285                                 Report.SymbolRelatedToPreviousError (MemberType);
3286                                 if (this is Property)
3287                                         Report.Error (53, Location,
3288                                                       "Inconsistent accessibility: property type `" +
3289                                                       TypeManager.CSharpName (MemberType) + "' is less " +
3290                                                       "accessible than property `" + GetSignatureForError () + "'");
3291                                 else if (this is Indexer)
3292                                         Report.Error (54, Location,
3293                                                       "Inconsistent accessibility: indexer return type `" +
3294                                                       TypeManager.CSharpName (MemberType) + "' is less " +
3295                                                       "accessible than indexer `" + GetSignatureForError () + "'");
3296                                 else if (this is MethodCore) {
3297                                         if (this is Operator)
3298                                                 Report.Error (56, Location,
3299                                                               "Inconsistent accessibility: return type `" +
3300                                                               TypeManager.CSharpName (MemberType) + "' is less " +
3301                                                               "accessible than operator `" + GetSignatureForError () + "'");
3302                                         else
3303                                                 Report.Error (50, Location,
3304                                                               "Inconsistent accessibility: return type `" +
3305                                                               TypeManager.CSharpName (MemberType) + "' is less " +
3306                                                               "accessible than method `" + GetSignatureForError () + "'");
3307                                 } else {
3308                                         Report.Error (52, Location,
3309                                                       "Inconsistent accessibility: field type `" +
3310                                                       TypeManager.CSharpName (MemberType) + "' is less " +
3311                                                       "accessible than field `" + GetSignatureForError () + "'");
3312                                 }
3313                         }
3314
3315                         Variance variance = this is Event ? Variance.Contravariant : Variance.Covariant;
3316                         TypeManager.CheckTypeVariance (MemberType, variance, this);
3317                 }
3318
3319                 protected bool IsTypePermitted ()
3320                 {
3321                         if (TypeManager.IsSpecialType (MemberType)) {
3322                                 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
3323                                 return false;
3324                         }
3325                         return true;
3326                 }
3327
3328                 protected virtual bool CheckBase ()
3329                 {
3330                         CheckProtectedModifier ();
3331
3332                         return true;
3333                 }
3334
3335                 protected virtual bool ResolveMemberType ()
3336                 {
3337                         if (member_type != null)
3338                                 throw new InternalErrorException ("Multi-resolve");
3339
3340                         TypeExpr te = type_expr.ResolveAsTypeTerminal (this, false);
3341                         if (te == null)
3342                                 return false;
3343                         
3344                         //
3345                         // Replace original type name, error reporting can use fully resolved name
3346                         //
3347                         type_expr = te;
3348
3349                         member_type = te.Type;
3350                         return true;
3351                 }
3352         }
3353 }
3354