Allow imported members cache to be setup in 2 phases as compiled one is to deal with...
[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                 public int DynamicSitesCounter;
220
221                 /// <remarks>
222                 ///  The pending methods that need to be implemented
223                 //   (interfaces or abstract methods)
224                 /// </remarks>
225                 PendingImplementation pending;
226
227                 public TypeContainer (NamespaceEntry ns, DeclSpace parent, MemberName name,
228                                       Attributes attrs, MemberKind kind)
229                         : base (ns, parent, name, attrs)
230                 {
231                         if (parent != null && parent.NamespaceEntry != ns)
232                                 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
233
234                         this.Kind = kind;
235                         this.PartialContainer = this;
236                 }
237
238                 #region Properties
239
240                 public override TypeSpec CurrentType {
241                         get {
242                                 if (current_type == null) {
243                                         if (IsGeneric) {
244                                                 //
245                                                 // Switch to inflated version as it's used by all expressions
246                                                 //
247                                                 var targs = CurrentTypeParameters == null ? TypeSpec.EmptyTypes : CurrentTypeParameters.Select (l => l.Type).ToArray ();
248                                                 current_type = spec.MakeGenericType (targs);
249                                         } else {
250                                                 current_type = spec;
251                                         }
252                                 }
253
254                                 return current_type;
255                         }
256                 }
257
258                 public override TypeParameter[] CurrentTypeParameters {
259                         get {
260                                 return PartialContainer.type_params;
261                         }
262                 }
263
264                 int CurrentTypeParametersStartIndex {
265                         get {
266                                 int total = all_tp_builders.Length;
267                                 if (CurrentTypeParameters != null) {
268                                         return total - CurrentTypeParameters.Length;
269                                 }
270                                 return total;
271                         }
272                 }
273
274                 public TypeSpec Definition {
275                         get {
276                                 return spec;
277                         }
278                 }
279
280                 public bool HasMembersDefined {
281                         get {
282                                 return members_defined;
283                         }
284                 }
285
286                 #endregion
287
288                 public bool AddMember (MemberCore symbol)
289                 {
290                         return AddToContainer (symbol, symbol.MemberName.Basename);
291                 }
292
293                 public bool AddMember (MemberCore symbol, string name)
294                 {
295                         return AddToContainer (symbol, name);
296                 }
297
298                 protected virtual bool AddMemberType (TypeContainer ds)
299                 {
300                         return AddToContainer (ds, ds.Basename);
301                 }
302
303                 protected virtual void RemoveMemberType (DeclSpace ds)
304                 {
305                         RemoveFromContainer (ds.Basename);
306                 }
307
308                 public void AddConstant (Const constant)
309                 {
310                         if (!AddMember (constant))
311                                 return;
312
313                         if (constants == null)
314                                 constants = new List<MemberCore> ();
315                         
316                         constants.Add (constant);
317                 }
318
319                 public TypeContainer AddTypeContainer (TypeContainer tc)
320                 {
321                         if (!AddMemberType (tc))
322                                 return tc;
323
324                         if (types == null)
325                                 types = new List<TypeContainer> ();
326
327                         types.Add (tc);
328                         return tc;
329                 }
330
331                 public virtual TypeContainer AddPartial (TypeContainer next_part)
332                 {
333                         return AddPartial (next_part, next_part.Basename);
334                 }
335
336                 protected TypeContainer AddPartial (TypeContainer next_part, string name)
337                 {
338                         next_part.ModFlags |= Modifiers.PARTIAL;
339                         TypeContainer tc = GetDefinition (name) as TypeContainer;
340                         if (tc == null)
341                                 return AddTypeContainer (next_part);
342
343                         if ((tc.ModFlags & Modifiers.PARTIAL) == 0) {
344                                 Report.SymbolRelatedToPreviousError (next_part);
345                                 Error_MissingPartialModifier (tc);
346                         }
347
348                         if (tc.Kind != next_part.Kind) {
349                                 Report.SymbolRelatedToPreviousError (tc);
350                                 Report.Error (261, next_part.Location,
351                                         "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
352                                         next_part.GetSignatureForError ());
353                         }
354
355                         if ((tc.ModFlags & Modifiers.AccessibilityMask) != (next_part.ModFlags & Modifiers.AccessibilityMask) &&
356                                 ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0 &&
357                                  (next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0)) {
358                                 Report.SymbolRelatedToPreviousError (tc);
359                                 Report.Error (262, next_part.Location,
360                                         "Partial declarations of `{0}' have conflicting accessibility modifiers",
361                                         next_part.GetSignatureForError ());
362                         }
363
364                         if (tc.partial_parts == null)
365                                 tc.partial_parts = new List<TypeContainer> (1);
366
367                         if ((next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
368                                 tc.ModFlags |= next_part.ModFlags & ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.AccessibilityMask);
369                         } else if ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
370                                 tc.ModFlags &= ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.AccessibilityMask);
371                                 tc.ModFlags |= next_part.ModFlags;
372                         } else {
373                                 tc.ModFlags |= next_part.ModFlags;
374                         }
375
376                         tc.spec.Modifiers = tc.ModFlags;
377
378                         if (next_part.attributes != null) {
379                                 if (tc.attributes == null)
380                                         tc.attributes = next_part.attributes;
381                                 else
382                                         tc.attributes.AddAttributes (next_part.attributes.Attrs);
383                         }
384
385                         next_part.PartialContainer = tc;
386                         tc.partial_parts.Add (next_part);
387                         return tc;
388                 }
389
390                 public virtual void RemoveTypeContainer (TypeContainer next_part)
391                 {
392                         if (types != null)
393                                 types.Remove (next_part);
394                         RemoveMemberType (next_part);
395                 }
396                 
397                 public virtual TypeSpec AddDelegate (Delegate d)
398                 {
399                         AddTypeContainer (d);
400                         return null;
401                 }
402
403                 private void AddMemberToList (MemberCore mc, List<MemberCore> alist, bool isexplicit)
404                 {
405                         if (ordered_explicit_member_list == null)  {
406                                 ordered_explicit_member_list = new List<MemberCore> ();
407                                 ordered_member_list = new List<MemberCore> ();
408                         }
409
410                         if (isexplicit) {
411                                 if (Kind == MemberKind.Interface) {
412                                         Report.Error (541, mc.Location,
413                                                 "`{0}': explicit interface declaration can only be declared in a class or struct",
414                                                 mc.GetSignatureForError ());
415                                 }
416
417                                 ordered_explicit_member_list.Add (mc);
418                                 alist.Insert (0, mc);
419                         } else {
420                                 ordered_member_list.Add (mc);
421                                 alist.Add (mc);
422                         }
423
424                 }
425                 
426                 public void AddMethod (MethodOrOperator method)
427                 {
428                         if (!AddToContainer (method, method.MemberName.Basename))
429                                 return;
430                         
431                         if (methods == null)
432                                 methods = new List<MemberCore> ();
433
434                         if (method.MemberName.Left != null) 
435                                 AddMemberToList (method, methods, true);
436                         else 
437                                 AddMemberToList (method, methods, false);
438                 }
439
440                 public void AddConstructor (Constructor c)
441                 {
442                         bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
443                         if (!AddToContainer (c, is_static ?
444                                 ConstructorBuilder.ConstructorName : ConstructorBuilder.TypeConstructorName))
445                                 return;
446                         
447                         if (is_static && c.ParameterInfo.IsEmpty){
448                                 if (default_static_constructor != null) {
449                                     Report.SymbolRelatedToPreviousError (default_static_constructor);
450                                         Report.Error (111, c.Location,
451                                                 "A member `{0}' is already defined. Rename this member or use different parameter types",
452                                                 c.GetSignatureForError ());
453                                     return;
454                                 }
455
456                                 default_static_constructor = c;
457                         } else {
458                                 if (c.ParameterInfo.IsEmpty)
459                                         default_constructor = c;
460                                 
461                                 if (instance_constructors == null)
462                                         instance_constructors = new List<Constructor> ();
463                                 
464                                 instance_constructors.Add (c);
465                         }
466                 }
467
468                 public bool AddField (FieldBase field)
469                 {
470                         if (!AddMember (field))
471                                 return false;
472
473                         if (fields == null)
474                                 fields = new List<FieldBase> ();
475
476                         fields.Add (field);
477
478                         if ((field.ModFlags & Modifiers.STATIC) != 0)
479                                 return true;
480
481                         if (first_nonstatic_field == null) {
482                                 first_nonstatic_field = field;
483                                 return true;
484                         }
485
486                         if (Kind == MemberKind.Struct && first_nonstatic_field.Parent != field.Parent) {
487                                 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
488                                 Report.Warning (282, 3, field.Location,
489                                         "struct instance field `{0}' found in different declaration from instance field `{1}'",
490                                         field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
491                         }
492                         return true;
493                 }
494
495                 public void AddProperty (Property prop)
496                 {
497                         if (!AddMember (prop))
498                                 return;
499
500                         if (properties == null)
501                                 properties = new List<MemberCore> ();
502
503                         if (prop.MemberName.Left != null)
504                                 AddMemberToList (prop, properties, true);
505                         else 
506                                 AddMemberToList (prop, properties, false);
507                 }
508
509                 public void AddEvent (Event e)
510                 {
511                         if (!AddMember (e))
512                                 return;
513
514                         if (events == null)
515                                 events = new List<MemberCore> ();
516
517                         events.Add (e);
518                 }
519
520                 /// <summary>
521                 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
522                 /// </summary>
523                 public void AddIndexer (Indexer i)
524                 {
525                         if (indexers == null)
526                                 indexers = new List<MemberCore> ();
527
528                         if (i.IsExplicitImpl)
529                                 AddMemberToList (i, indexers, true);
530                         else 
531                                 AddMemberToList (i, indexers, false);
532                 }
533
534                 public void AddOperator (Operator op)
535                 {
536                         if (!AddMember (op))
537                                 return;
538
539                         if (operators == null)
540                                 operators = new List<MemberCore> ();
541
542                         operators.Add (op);
543                 }
544
545                 public void AddCompilerGeneratedClass (CompilerGeneratedClass c)
546                 {
547                         Report.Debug (64, "ADD COMPILER GENERATED CLASS", this, c);
548
549                         if (compiler_generated == null)
550                                 compiler_generated = new List<CompilerGeneratedClass> ();
551
552                         compiler_generated.Add (c);
553                 }
554
555                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
556                 {
557                         if (a.Type == pa.DefaultMember) {
558                                 if (Indexers != null) {
559                                         Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
560                                         return;
561                                 }
562                         }
563
564                         if (a.Type == pa.Required) {
565                                 Report.Error (1608, a.Location, "The RequiredAttribute attribute is not permitted on C# types");
566                                 return;
567                         }
568
569                         TypeBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
570                 } 
571
572                 public override AttributeTargets AttributeTargets {
573                         get {
574                                 throw new NotSupportedException ();
575                         }
576                 }
577
578                 public IList<TypeContainer> Types {
579                         get {
580                                 return types;
581                         }
582                 }
583
584                 public IList<MemberCore> Methods {
585                         get {
586                                 return methods;
587                         }
588                 }
589
590                 public IList<MemberCore> Constants {
591                         get {
592                                 return constants;
593                         }
594                 }
595
596                 public TypeSpec BaseType {
597                         get {
598                                 return spec.BaseType;
599                         }
600                 }
601
602                 public IList<FieldBase> Fields {
603                         get {
604                                 return fields;
605                         }
606                 }
607
608                 public IList<Constructor> InstanceConstructors {
609                         get {
610                                 return instance_constructors;
611                         }
612                 }
613
614                 public IList<MemberCore> Properties {
615                         get {
616                                 return properties;
617                         }
618                 }
619
620                 public IList<MemberCore> Events {
621                         get {
622                                 return events;
623                         }
624                 }
625                 
626                 public IList<MemberCore> Indexers {
627                         get {
628                                 return indexers;
629                         }
630                 }
631
632                 public IList<MemberCore> Operators {
633                         get {
634                                 return operators;
635                         }
636                 }
637
638                 public IList<CompilerGeneratedClass> CompilerGeneratedClasses {
639                         get {
640                                 return compiler_generated;
641                         }
642                 }
643
644                 protected override TypeAttributes TypeAttr {
645                         get {
646                                 return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
647                         }
648                 }
649
650                 public int TypeParametersCount {
651                         get {
652                                 return MemberName.Arity;
653                         }
654                 }
655
656                 TypeParameterSpec[] ITypeDefinition.TypeParameters {
657                         get {
658                                 // TODO MemberCache: this is going to hurt
659                                 return PartialContainer.type_params.Select (l => l.Type).ToArray ();
660                         }
661                 }
662
663                 public string GetAttributeDefaultMember ()
664                 {
665                         return indexers == null ? DefaultIndexerName : indexer_name;
666                 }
667
668                 public bool IsComImport {
669                         get {
670                                 if (OptAttributes == null)
671                                         return false;
672
673                                 return OptAttributes.Contains (Compiler.PredefinedAttributes.ComImport);
674                         }
675                 }
676
677                 string ITypeDefinition.Namespace {
678                         get {
679                                 return NamespaceEntry.NS.MemberName.GetSignatureForError ();
680                         }
681                 }
682
683                 public virtual void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
684                 {
685                         if ((field.ModFlags & Modifiers.STATIC) != 0){
686                                 if (initialized_static_fields == null) {
687                                         PartialContainer.HasStaticFieldInitializer = true;
688                                         initialized_static_fields = new List<FieldInitializer> (4);
689                                 }
690
691                                 initialized_static_fields.Add (expression);
692                         } else {
693                                 if (initialized_fields == null)
694                                         initialized_fields = new List<FieldInitializer> (4);
695
696                                 initialized_fields.Add (expression);
697                         }
698                 }
699
700                 public void ResolveFieldInitializers (BlockContext ec)
701                 {
702                         if (partial_parts != null) {
703                                 foreach (TypeContainer part in partial_parts) {
704                                         part.DoResolveFieldInitializers (ec);
705                                 }
706                         }
707                         DoResolveFieldInitializers (ec);
708                 }
709
710                 void DoResolveFieldInitializers (BlockContext ec)
711                 {
712                         if (ec.IsStatic) {
713                                 if (initialized_static_fields == null)
714                                         return;
715
716                                 bool has_complex_initializer = !RootContext.Optimize;
717                                 int i;
718                                 ExpressionStatement [] init = new ExpressionStatement [initialized_static_fields.Count];
719                                 for (i = 0; i < initialized_static_fields.Count; ++i) {
720                                         FieldInitializer fi = initialized_static_fields [i];
721                                         ExpressionStatement s = fi.ResolveStatement (ec);
722                                         if (s == null) {
723                                                 s = EmptyExpressionStatement.Instance;
724                                         } else if (fi.IsComplexInitializer) {
725                                                 has_complex_initializer |= true;
726                                         }
727
728                                         init [i] = s;
729                                 }
730
731                                 for (i = 0; i < initialized_static_fields.Count; ++i) {
732                                         FieldInitializer fi = initialized_static_fields [i];
733                                         //
734                                         // Need special check to not optimize code like this
735                                         // static int a = b = 5;
736                                         // static int b = 0;
737                                         //
738                                         if (!has_complex_initializer && fi.IsDefaultInitializer)
739                                                 continue;
740
741                                         ec.CurrentBlock.AddScopeStatement (new StatementExpression (init [i]));
742                                 }
743
744                                 return;
745                         }
746
747                         if (initialized_fields == null)
748                                 return;
749
750                         for (int i = 0; i < initialized_fields.Count; ++i) {
751                                 FieldInitializer fi = initialized_fields [i];
752                                 ExpressionStatement s = fi.ResolveStatement (ec);
753                                 if (s == null)
754                                         continue;
755
756                                 //
757                                 // Field is re-initialized to its default value => removed
758                                 //
759                                 if (fi.IsDefaultInitializer && RootContext.Optimize)
760                                         continue;
761
762                                 ec.CurrentBlock.AddScopeStatement (new StatementExpression (s));
763                         }
764                 }
765
766                 public override string DocComment {
767                         get {
768                                 return comment;
769                         }
770                         set {
771                                 if (value == null)
772                                         return;
773
774                                 comment += value;
775                         }
776                 }
777
778                 public PendingImplementation PendingImplementations {
779                         get { return pending; }
780                 }
781
782                 public TypeSpec GetAttributeCoClass ()
783                 {
784                         if (OptAttributes == null)
785                                 return null;
786
787                         Attribute a = OptAttributes.Search (Compiler.PredefinedAttributes.CoClass);
788                         if (a == null)
789                                 return null;
790
791                         return a.GetCoClassAttributeValue ();
792                 }
793
794                 public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
795                 {
796                         Attribute a = null;
797                         if (OptAttributes != null) {
798                                 a = OptAttributes.Search (pa);
799                         }
800
801                         if (a == null)
802                                 return null;
803
804                         return a.GetAttributeUsageAttribute ();
805                 }
806
807                 public virtual void AddBasesForPart (DeclSpace part, List<FullNamedExpression> bases)
808                 {
809                         // FIXME: get rid of partial_parts and store lists of bases of each part here
810                         // assumed, not verified: 'part' is in 'partial_parts' 
811                         ((TypeContainer) part).type_bases = bases;
812                 }
813
814                 /// <summary>
815                 ///   This function computes the Base class and also the
816                 ///   list of interfaces that the class or struct @c implements.
817                 ///   
818                 ///   The return value is an array (might be null) of
819                 ///   interfaces implemented (as Types).
820                 ///   
821                 ///   The @base_class argument is set to the base object or null
822                 ///   if this is `System.Object'. 
823                 /// </summary>
824                 protected virtual TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
825                 {
826                         base_class = null;
827                         if (type_bases == null)
828                                 return null;
829
830                         int count = type_bases.Count;
831                         TypeExpr [] ifaces = null;
832                         var base_context = new BaseContext (this);
833                         for (int i = 0, j = 0; i < count; i++){
834                                 FullNamedExpression fne = type_bases [i];
835
836                                 TypeExpr fne_resolved = fne.ResolveAsTypeTerminal (base_context, false);
837                                 if (fne_resolved == null)
838                                         continue;
839
840                                 if (i == 0 && Kind == MemberKind.Class && !fne_resolved.Type.IsInterface) {
841                                         if (fne_resolved.Type == InternalType.Dynamic)
842                                                 Report.Error (1965, Location, "Class `{0}' cannot derive from the dynamic type",
843                                                         GetSignatureForError ());
844                                         else
845                                                 base_type = fne_resolved.Type;
846
847                                         base_class = fne_resolved;
848                                         continue;
849                                 }
850
851                                 if (ifaces == null)
852                                         ifaces = new TypeExpr [count - i];
853
854                                 if (fne_resolved.Type.IsInterface) {
855                                         for (int ii = 0; ii < j; ++ii) {
856                                                 if (fne_resolved.Type == ifaces [ii].Type) {
857                                                         Report.Error (528, Location, "`{0}' is already listed in interface list",
858                                                                 fne_resolved.GetSignatureForError ());
859                                                         break;
860                                                 }
861                                         }
862
863                                         if (Kind == MemberKind.Interface && !IsAccessibleAs (fne_resolved.Type)) {
864                                                 Report.Error (61, fne.Location,
865                                                         "Inconsistent accessibility: base interface `{0}' is less accessible than interface `{1}'",
866                                                         fne_resolved.GetSignatureForError (), GetSignatureForError ());
867                                         }
868                                 } else {
869                                         Report.SymbolRelatedToPreviousError (fne_resolved.Type);
870                                         if (Kind != MemberKind.Class) {
871                                                 Report.Error (527, fne.Location, "Type `{0}' in interface list is not an interface", fne_resolved.GetSignatureForError ());
872                                         } else if (base_class != null)
873                                                 Report.Error (1721, fne.Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
874                                                         GetSignatureForError (), base_class.GetSignatureForError (), fne_resolved.GetSignatureForError ());
875                                         else {
876                                                 Report.Error (1722, fne.Location, "`{0}': Base class `{1}' must be specified as first",
877                                                         GetSignatureForError (), fne_resolved.GetSignatureForError ());
878                                         }
879                                 }
880
881                                 ifaces [j++] = fne_resolved;
882                         }
883
884                         return ifaces;
885                 }
886
887                 TypeExpr[] GetNormalPartialBases ()
888                 {
889                         var ifaces = new List<TypeExpr> (0);
890                         if (iface_exprs != null)
891                                 ifaces.AddRange (iface_exprs);
892
893                         foreach (TypeContainer part in partial_parts) {
894                                 TypeExpr new_base_class;
895                                 TypeExpr[] new_ifaces = part.ResolveBaseTypes (out new_base_class);
896                                 if (new_base_class != null) {
897                                         if (base_type_expr != null && new_base_class.Type != base_type) {
898                                                 Report.SymbolRelatedToPreviousError (new_base_class.Location, "");
899                                                 Report.Error (263, part.Location,
900                                                         "Partial declarations of `{0}' must not specify different base classes",
901                                                         part.GetSignatureForError ());
902                                         } else {
903                                                 base_type_expr = new_base_class;
904                                                 base_type = base_type_expr.Type;
905                                         }
906                                 }
907
908                                 if (new_ifaces == null)
909                                         continue;
910
911                                 foreach (TypeExpr iface in new_ifaces) {
912                                         if (ifaces.Contains (iface))
913                                                 continue;
914
915                                         ifaces.Add (iface);
916                                 }
917                         }
918
919                         if (ifaces.Count == 0)
920                                 return null;
921
922                         return ifaces.ToArray ();
923                 }
924
925                 //
926                 // Checks that some operators come in pairs:
927                 //  == and !=
928                 // > and <
929                 // >= and <=
930                 // true and false
931                 //
932                 // They are matched based on the return type and the argument types
933                 //
934                 void CheckPairedOperators ()
935                 {
936                         bool has_equality_or_inequality = false;
937                         var operators = this.operators.ToArray ();
938                         bool[] has_pair = new bool[operators.Length];
939
940                         for (int i = 0; i < operators.Length; ++i) {
941                                 if (operators[i] == null)
942                                         continue;
943
944                                 Operator o_a = (Operator) operators[i];
945                                 Operator.OpType o_type = o_a.OperatorType;
946                                 if (o_type == Operator.OpType.Equality || o_type == Operator.OpType.Inequality)
947                                         has_equality_or_inequality = true;
948
949                                 Operator.OpType matching_type = o_a.GetMatchingOperator ();
950                                 if (matching_type == Operator.OpType.TOP) {
951                                         operators[i] = null;
952                                         continue;
953                                 }
954
955                                 for (int ii = 0; ii < operators.Length; ++ii) {
956                                         Operator o_b = (Operator) operators[ii];
957                                         if (o_b == null || o_b.OperatorType != matching_type)
958                                                 continue;
959
960                                         if (!TypeSpecComparer.IsEqual (o_a.ReturnType, o_b.ReturnType))
961                                                 continue;
962
963                                         if (!TypeSpecComparer.Equals (o_a.ParameterTypes, o_b.ParameterTypes))
964                                                 continue;
965
966                                         operators[i] = null;
967
968                                         //
969                                         // Used to ignore duplicate user conversions
970                                         //
971                                         has_pair[ii] = true;
972                                 }
973                         }
974
975                         for (int i = 0; i < operators.Length; ++i) {
976                                 if (operators[i] == null || has_pair[i])
977                                         continue;
978
979                                 Operator o = (Operator) operators [i];
980                                 Report.Error (216, o.Location,
981                                         "The operator `{0}' requires a matching operator `{1}' to also be defined",
982                                         o.GetSignatureForError (), Operator.GetName (o.GetMatchingOperator ()));
983                         }
984
985                         if (has_equality_or_inequality) {
986                                 if (Methods == null || !HasEquals)
987                                         Report.Warning (660, 2, Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)",
988                                                 GetSignatureForError ());
989
990                                 if (Methods == null || !HasGetHashCode)
991                                         Report.Warning (661, 2, Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()",
992                                                 GetSignatureForError ());
993                         }
994                 }
995         
996                 bool CreateTypeBuilder ()
997                 {
998                         //
999                         // Sets .size to 1 for structs with no instance fields
1000                         //
1001                         int type_size = Kind == MemberKind.Struct && first_nonstatic_field == null ? 1 : 0;
1002
1003                         if (IsTopLevel) {
1004                                 if (Compiler.GlobalRootNamespace.IsNamespace (Name)) {
1005                                         Report.Error (519, Location, "`{0}' clashes with a predefined namespace", Name);
1006                                         return false;
1007                                 }
1008
1009                                 ModuleBuilder builder = Module.Compiled.Builder;
1010                                 TypeBuilder = builder.DefineType (Name, TypeAttr, null, type_size);
1011                         } else {
1012                                 TypeBuilder builder = Parent.TypeBuilder;
1013
1014                                 TypeBuilder = builder.DefineNestedType (Basename, TypeAttr, null, type_size);
1015                         }
1016
1017                         spec.SetMetaInfo (TypeBuilder);
1018                         spec.MemberCache = new MemberCache (this);
1019                         spec.DeclaringType = Parent.CurrentType;
1020
1021                         if (!IsTopLevel)
1022                                 Parent.MemberCache.AddMember (spec);
1023
1024                         if (IsGeneric) {
1025                                 string[] param_names = new string[TypeParameters.Length];
1026                                 for (int i = 0; i < TypeParameters.Length; i++)
1027                                         param_names [i] = TypeParameters[i].Name;
1028
1029                                 all_tp_builders = TypeBuilder.DefineGenericParameters (param_names);
1030
1031                                 int offset = CurrentTypeParametersStartIndex;
1032                                 for (int i = offset; i < all_tp_builders.Length; i++) {
1033                                         CurrentTypeParameters [i - offset].Define (all_tp_builders [i], spec);
1034                                 }
1035                         }
1036
1037                         return true;
1038                 }
1039
1040                 //
1041                 // Creates a proxy base method call inside this container for hoisted base member calls
1042                 //
1043                 public MethodSpec CreateHoistedBaseCallProxy (ResolveContext rc, MethodSpec method)
1044                 {
1045                         Method proxy_method;
1046
1047                         //
1048                         // One proxy per base method is enough
1049                         //
1050                         if (hoisted_base_call_proxies == null) {
1051                                 hoisted_base_call_proxies = new Dictionary<MethodSpec, Method> ();
1052                                 proxy_method = null;
1053                         } else {
1054                                 hoisted_base_call_proxies.TryGetValue (method, out proxy_method);
1055                         }
1056
1057                         if (proxy_method == null) {
1058                                 string name = CompilerGeneratedClass.MakeName (method.Name, null, "BaseCallProxy", hoisted_base_call_proxies.Count);
1059                                 var base_parameters = new Parameter[method.Parameters.Count];
1060                                 for (int i = 0; i < base_parameters.Length; ++i) {
1061                                         var base_param = method.Parameters.FixedParameters[i];
1062                                         base_parameters[i] = new Parameter (new TypeExpression (method.Parameters.Types[i], Location),
1063                                                 base_param.Name, base_param.ModFlags, null, Location);
1064                                         base_parameters[i].Resolve (this, i);
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, f.Location);
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 void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
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.IsGenericOrParentIsGeneric || mt.IsGenericParameter) {
2639                                         has_unmanaged_check_done = true;
2640                                         return false;
2641                                 }
2642
2643                                 if (TypeManager.IsUnmanagedType (mt))
2644                                         continue;
2645
2646                                 has_unmanaged_check_done = true;
2647                                 return false;
2648                         }
2649
2650                         has_unmanaged_check_done = true;
2651                         is_unmanaged = true;
2652                         return true;
2653                 }
2654
2655                 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
2656                 {
2657                         TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
2658                         base_type = TypeManager.value_type;
2659                         return ifaces;
2660                 }
2661
2662                 protected override TypeAttributes TypeAttr {
2663                         get {
2664                                 const TypeAttributes DefaultTypeAttributes =
2665                                         TypeAttributes.SequentialLayout |
2666                                         TypeAttributes.Sealed;
2667
2668                                 return base.TypeAttr | DefaultTypeAttributes;
2669                         }
2670                 }
2671
2672                 public override void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
2673                 {
2674                         if ((field.ModFlags & Modifiers.STATIC) == 0) {
2675                                 Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
2676                                         field.GetSignatureForError ());
2677                                 return;
2678                         }
2679                         base.RegisterFieldForInitialization (field, expression);
2680                 }
2681
2682         }
2683
2684         /// <summary>
2685         ///   Interfaces
2686         /// </summary>
2687         public sealed class Interface : TypeContainer {
2688
2689                 /// <summary>
2690                 ///   Modifiers allowed in a class declaration
2691                 /// </summary>
2692                 public const Modifiers AllowedModifiers =
2693                         Modifiers.NEW       |
2694                         Modifiers.PUBLIC    |
2695                         Modifiers.PROTECTED |
2696                         Modifiers.INTERNAL  |
2697                         Modifiers.UNSAFE    |
2698                         Modifiers.PRIVATE;
2699
2700                 public Interface (NamespaceEntry ns, DeclSpace parent, MemberName name, Modifiers mod,
2701                                   Attributes attrs)
2702                         : base (ns, parent, name, attrs, MemberKind.Interface)
2703                 {
2704                         var accmods = parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2705
2706                         this.ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod, accmods, name.Location, Report);
2707                         spec = new TypeSpec (Kind, null, this, null, ModFlags);
2708                 }
2709
2710                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2711                 {
2712                         if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
2713                                 a.Error_MissingGuidAttribute ();
2714                                 return;
2715                         }
2716
2717                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2718                 }
2719
2720
2721                 public override AttributeTargets AttributeTargets {
2722                         get {
2723                                 return AttributeTargets.Interface;
2724                         }
2725                 }
2726
2727                 protected override TypeAttributes TypeAttr {
2728                         get {
2729                                 const TypeAttributes DefaultTypeAttributes =
2730                                         TypeAttributes.AutoLayout |
2731                                         TypeAttributes.Abstract |
2732                                         TypeAttributes.Interface;
2733
2734                                 return base.TypeAttr | DefaultTypeAttributes;
2735                         }
2736                 }
2737
2738                 protected override bool VerifyClsCompliance ()
2739                 {
2740                         if (!base.VerifyClsCompliance ())
2741                                 return false;
2742
2743                         if (iface_exprs != null) {
2744                                 foreach (var iface in iface_exprs) {
2745                                         if (iface.Type.IsCLSCompliant ())
2746                                                 continue;
2747
2748                                         Report.SymbolRelatedToPreviousError (iface.Type);
2749                                         Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
2750                                                 GetSignatureForError (), TypeManager.CSharpName (iface.Type));
2751                                 }
2752                         }
2753
2754                         return true;
2755                 }
2756         }
2757
2758         public abstract class InterfaceMemberBase : MemberBase
2759         {
2760                 //
2761                 // Common modifiers allowed in a class declaration
2762                 //
2763                 protected const Modifiers AllowedModifiersClass =
2764                         Modifiers.NEW |
2765                         Modifiers.PUBLIC |
2766                         Modifiers.PROTECTED |
2767                         Modifiers.INTERNAL |
2768                         Modifiers.PRIVATE |
2769                         Modifiers.STATIC |
2770                         Modifiers.VIRTUAL |
2771                         Modifiers.SEALED |
2772                         Modifiers.OVERRIDE |
2773                         Modifiers.ABSTRACT |
2774                         Modifiers.UNSAFE |
2775                         Modifiers.EXTERN;
2776
2777                 //
2778                 // Common modifiers allowed in a struct declaration
2779                 //
2780                 protected const Modifiers AllowedModifiersStruct =
2781                         Modifiers.NEW |
2782                         Modifiers.PUBLIC |
2783                         Modifiers.PROTECTED |
2784                         Modifiers.INTERNAL |
2785                         Modifiers.PRIVATE |
2786                         Modifiers.STATIC |
2787                         Modifiers.OVERRIDE |
2788                         Modifiers.UNSAFE |
2789                         Modifiers.EXTERN;
2790
2791                 //
2792                 // Common modifiers allowed in a interface declaration
2793                 //
2794                 protected const Modifiers AllowedModifiersInterface =
2795                         Modifiers.NEW |
2796                         Modifiers.UNSAFE;
2797
2798                 //
2799                 // Whether this is an interface member.
2800                 //
2801                 public bool IsInterface;
2802
2803                 //
2804                 // If true, this is an explicit interface implementation
2805                 //
2806                 public bool IsExplicitImpl;
2807
2808                 protected bool is_external_implementation;
2809
2810                 //
2811                 // The interface type we are explicitly implementing
2812                 //
2813                 public TypeSpec InterfaceType;
2814
2815                 //
2816                 // The method we're overriding if this is an override method.
2817                 //
2818                 protected MethodSpec base_method;
2819
2820                 readonly Modifiers explicit_mod_flags;
2821                 public MethodAttributes flags;
2822
2823                 public InterfaceMemberBase (DeclSpace parent, GenericMethod generic,
2824                                    FullNamedExpression type, Modifiers mod, Modifiers allowed_mod,
2825                                    MemberName name, Attributes attrs)
2826                         : base (parent, generic, type, mod, allowed_mod, Modifiers.PRIVATE,
2827                                 name, attrs)
2828                 {
2829                         IsInterface = parent.PartialContainer.Kind == MemberKind.Interface;
2830                         IsExplicitImpl = (MemberName.Left != null);
2831                         explicit_mod_flags = mod;
2832                 }
2833                 
2834                 protected override bool CheckBase ()
2835                 {
2836                         if (!base.CheckBase ())
2837                                 return false;
2838
2839                         if ((caching_flags & Flags.MethodOverloadsExist) != 0)
2840                                 CheckForDuplications ();
2841                         
2842                         if (IsExplicitImpl)
2843                                 return true;
2844
2845                         // For System.Object only
2846                         if (Parent.BaseType == null)
2847                                 return true;
2848
2849                         MemberSpec candidate;
2850                         var base_member = FindBaseMember (out candidate);
2851
2852                         if ((ModFlags & Modifiers.OVERRIDE) != 0) {
2853                                 if (base_member == null) {
2854                                         if (candidate == null) {
2855                                                 if (this is Method && ((Method)this).ParameterInfo.IsEmpty && MemberName.Name == Destructor.MetadataName && MemberName.Arity == 0) {
2856                                                         Report.Error (249, Location, "Do not override `{0}'. Use destructor syntax instead",
2857                                                                 "object.Finalize()");
2858                                                 } else {
2859                                                         Report.Error (115, Location, "`{0}' is marked as an override but no suitable {1} found to override",
2860                                                                 GetSignatureForError (), SimpleName.GetMemberType (this));
2861                                                 }
2862                                         } else {
2863                                                 Report.SymbolRelatedToPreviousError (candidate);
2864                                                 if (this is Event)
2865                                                         Report.Error (72, Location, "`{0}': cannot override because `{1}' is not an event",
2866                                                                 GetSignatureForError (), TypeManager.GetFullNameSignature (candidate));
2867                                                 else if (this is PropertyBase)
2868                                                         Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property",
2869                                                                 GetSignatureForError (), TypeManager.GetFullNameSignature (candidate));
2870                                                 else
2871                                                         Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method",
2872                                                                 GetSignatureForError (), TypeManager.GetFullNameSignature (candidate));
2873                                         }
2874
2875                                         return false;
2876                                 }
2877
2878                                 if (!CheckOverrideAgainstBase (base_member))
2879                                         return false;
2880
2881                                 ObsoleteAttribute oa = base_member.GetAttributeObsolete ();
2882                                 if (oa != null) {
2883                                         if (OptAttributes == null || !OptAttributes.Contains (Compiler.PredefinedAttributes.Obsolete)) {
2884                                                 Report.SymbolRelatedToPreviousError (base_member);
2885                                                 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
2886                                                         GetSignatureForError (), TypeManager.GetFullNameSignature (base_member));
2887                                         }
2888                                 } else {
2889                                         if (OptAttributes != null && OptAttributes.Contains (Compiler.PredefinedAttributes.Obsolete)) {
2890                                                 Report.SymbolRelatedToPreviousError (base_member);
2891                                                 Report.Warning (809, 1, Location, "Obsolete member `{0}' overrides non-obsolete member `{1}'",
2892                                                         GetSignatureForError (), TypeManager.GetFullNameSignature (base_member));
2893                                         }
2894                                 }
2895
2896                                 base_method = base_member as MethodSpec;
2897                                 return true;
2898                         }
2899
2900                         if (base_member == null && candidate != null && (!(candidate is IParametersMember) || !(this is IParametersMember)))
2901                                 base_member = candidate;
2902
2903                         if (base_member == null) {
2904                                 if ((ModFlags & Modifiers.NEW) != 0) {
2905                                         if (base_member == null) {
2906                                                 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required",
2907                                                         GetSignatureForError ());
2908                                         }
2909                                 }
2910                         } else {
2911                                 if ((ModFlags & Modifiers.NEW) == 0) {
2912                                         ModFlags |= Modifiers.NEW;
2913                                         Report.SymbolRelatedToPreviousError (base_member);
2914                                         if (!IsInterface && (base_member.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL | Modifiers.OVERRIDE)) != 0) {
2915                                                 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",
2916                                                         GetSignatureForError (), base_member.GetSignatureForError ());
2917                                         } else {
2918                                                 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
2919                                                         GetSignatureForError (), base_member.GetSignatureForError ());
2920                                         }
2921                                 }
2922
2923                                 if (!IsInterface && base_member.IsAbstract && candidate == null) {
2924                                         Report.SymbolRelatedToPreviousError (base_member);
2925                                         Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
2926                                                 GetSignatureForError (), base_member.GetSignatureForError ());
2927                                 }
2928                         }
2929
2930                         return true;
2931                 }
2932
2933                 protected virtual bool CheckForDuplications ()
2934                 {
2935                         return Parent.MemberCache.CheckExistingMembersOverloads (this, ParametersCompiled.EmptyReadOnlyParameters);
2936                 }
2937
2938                 //
2939                 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
2940                 // that have been defined.
2941                 //
2942                 protected virtual bool CheckOverrideAgainstBase (MemberSpec base_member)
2943                 {
2944                         bool ok = true;
2945
2946                         if ((base_member.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL | Modifiers.OVERRIDE)) == 0) {
2947                                 Report.SymbolRelatedToPreviousError (base_member);
2948                                 Report.Error (506, Location,
2949                                         "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
2950                                          GetSignatureForError (), TypeManager.CSharpSignature (base_member));
2951                                 ok = false;
2952                         }
2953
2954                         // Now we check that the overriden method is not final  
2955                         if ((base_member.Modifiers & Modifiers.SEALED) != 0) {
2956                                 Report.SymbolRelatedToPreviousError (base_member);
2957                                 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
2958                                                           GetSignatureForError (), TypeManager.CSharpSignature (base_member));
2959                                 ok = false;
2960                         }
2961
2962                         var base_member_type = ((IInterfaceMemberSpec) base_member).MemberType;
2963                         if (!TypeSpecComparer.Override.IsEqual (MemberType, base_member_type)) {
2964                                 Report.SymbolRelatedToPreviousError (base_member);
2965                                 if (this is PropertyBasedMember) {
2966                                         Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
2967                                                 GetSignatureForError (), TypeManager.CSharpName (base_member_type), TypeManager.CSharpSignature (base_member));
2968                                 } else {
2969                                         Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
2970                                                 GetSignatureForError (), TypeManager.CSharpName (base_member_type), TypeManager.CSharpSignature (base_member));
2971                                 }
2972                                 ok = false;
2973                         }
2974
2975                         return ok;
2976                 }
2977
2978                 protected static bool CheckAccessModifiers (MemberCore this_member, MemberSpec base_member)
2979                 {
2980                         var thisp = this_member.ModFlags & Modifiers.AccessibilityMask;
2981                         var base_classp = base_member.Modifiers & Modifiers.AccessibilityMask;
2982
2983                         if ((base_classp & (Modifiers.PROTECTED | Modifiers.INTERNAL)) == (Modifiers.PROTECTED | Modifiers.INTERNAL)) {
2984                                 //
2985                                 // when overriding protected internal, the method can be declared
2986                                 // protected internal only within the same assembly or assembly
2987                                 // which has InternalsVisibleTo
2988                                 //
2989                                 if ((thisp & (Modifiers.PROTECTED | Modifiers.INTERNAL)) == (Modifiers.PROTECTED | Modifiers.INTERNAL)) {
2990                                         return TypeManager.IsThisOrFriendAssembly (this_member.Assembly, base_member.Assembly);
2991                                 } 
2992                                 if ((thisp & Modifiers.PROTECTED) != Modifiers.PROTECTED) {
2993                                         //
2994                                         // if it's not "protected internal", it must be "protected"
2995                                         //
2996
2997                                         return false;
2998                                 }
2999                                 if (this_member.Parent.PartialContainer.Module.Assembly == base_member.Assembly) {
3000                                         //
3001                                         // protected within the same assembly - an error
3002                                         //
3003                                         return false;
3004                                 }
3005                                 if ((thisp & ~(Modifiers.PROTECTED | Modifiers.INTERNAL)) !=
3006                                            (base_classp & ~(Modifiers.PROTECTED | Modifiers.INTERNAL))) {
3007                                         //
3008                                         // protected ok, but other attributes differ - report an error
3009                                         //
3010                                         return false;
3011                                 }
3012                                 return true;
3013                         }
3014
3015                         return thisp == base_classp;
3016                 }
3017
3018                 public override bool Define ()
3019                 {
3020                         if (IsInterface) {
3021                                 ModFlags = Modifiers.PUBLIC | Modifiers.ABSTRACT |
3022                                         Modifiers.VIRTUAL | (ModFlags & (Modifiers.UNSAFE | Modifiers.NEW));
3023
3024                                 flags = MethodAttributes.Public |
3025                                         MethodAttributes.Abstract |
3026                                         MethodAttributes.HideBySig |
3027                                         MethodAttributes.NewSlot |
3028                                         MethodAttributes.Virtual;
3029                         } else {
3030                                 Parent.PartialContainer.MethodModifiersValid (this);
3031
3032                                 flags = ModifiersExtensions.MethodAttr (ModFlags);
3033                         }
3034
3035                         if (IsExplicitImpl) {
3036                                 TypeExpr iface_texpr = MemberName.Left.GetTypeExpression ().ResolveAsTypeTerminal (Parent, false);
3037                                 if (iface_texpr == null)
3038                                         return false;
3039
3040                                 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3041                                         Report.Error (754, Location, "A partial method `{0}' cannot explicitly implement an interface",
3042                                                 GetSignatureForError ());
3043                                 }
3044
3045                                 InterfaceType = iface_texpr.Type;
3046
3047                                 if (!InterfaceType.IsInterface) {
3048                                         Report.SymbolRelatedToPreviousError (InterfaceType);
3049                                         Report.Error (538, Location, "The type `{0}' in explicit interface declaration is not an interface",
3050                                                 TypeManager.CSharpName (InterfaceType));
3051                                 } else {
3052                                         Parent.PartialContainer.VerifyImplements (this);
3053                                 }
3054
3055                                 ModifiersExtensions.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location, Report);
3056                         }
3057
3058                         return base.Define ();
3059                 }
3060
3061                 protected bool DefineParameters (ParametersCompiled parameters)
3062                 {
3063                         if (!parameters.Resolve (this))
3064                                 return false;
3065
3066                         bool error = false;
3067                         for (int i = 0; i < parameters.Count; ++i) {
3068                                 Parameter p = parameters [i];
3069
3070                                 if (p.HasDefaultValue && (IsExplicitImpl || this is Operator || (this is Indexer && parameters.Count == 1)))
3071                                         p.Warning_UselessOptionalParameter (Report);
3072
3073                                 if (p.CheckAccessibility (this))
3074                                         continue;
3075
3076                                 TypeSpec t = parameters.Types [i];
3077                                 Report.SymbolRelatedToPreviousError (t);
3078                                 if (this is Indexer)
3079                                         Report.Error (55, Location,
3080                                                       "Inconsistent accessibility: parameter type `{0}' is less accessible than indexer `{1}'",
3081                                                       TypeManager.CSharpName (t), GetSignatureForError ());
3082                                 else if (this is Operator)
3083                                         Report.Error (57, Location,
3084                                                       "Inconsistent accessibility: parameter type `{0}' is less accessible than operator `{1}'",
3085                                                       TypeManager.CSharpName (t), GetSignatureForError ());
3086                                 else
3087                                         Report.Error (51, Location,
3088                                                 "Inconsistent accessibility: parameter type `{0}' is less accessible than method `{1}'",
3089                                                 TypeManager.CSharpName (t), GetSignatureForError ());
3090                                 error = true;
3091                         }
3092                         return !error;
3093                 }
3094
3095                 public override void Emit()
3096                 {
3097                         // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
3098                         // We are more strict than csc and report this as an error because SRE does not allow emit that
3099                         if ((ModFlags & Modifiers.EXTERN) != 0 && !is_external_implementation) {
3100                                 if (this is Constructor) {
3101                                         Report.Error (824, Location,
3102                                                 "Constructor `{0}' is marked `external' but has no external implementation specified", GetSignatureForError ());
3103                                 } else {
3104                                         Report.Error (626, Location,
3105                                                 "`{0}' is marked as an external but has no DllImport attribute. Consider adding a DllImport attribute to specify the external implementation",
3106                                                 GetSignatureForError ());
3107                                 }
3108                         }
3109
3110                         base.Emit ();
3111                 }
3112
3113                 public override bool EnableOverloadChecks (MemberCore overload)
3114                 {
3115                         //
3116                         // Two members can differ in their explicit interface
3117                         // type parameter only
3118                         //
3119                         InterfaceMemberBase imb = overload as InterfaceMemberBase;
3120                         if (imb != null && imb.IsExplicitImpl) {
3121                                 if (IsExplicitImpl) {
3122                                         caching_flags |= Flags.MethodOverloadsExist;
3123                                 }
3124                                 return true;
3125                         }
3126
3127                         return IsExplicitImpl;
3128                 }
3129
3130                 protected void Error_CannotChangeAccessModifiers (MemberCore member, MemberSpec base_member)
3131                 {
3132                         Report.SymbolRelatedToPreviousError (base_member);
3133                         Report.Error (507, member.Location,
3134                                 "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3135                                 member.GetSignatureForError (),
3136                                 ModifiersExtensions.AccessibilityName (base_member.Modifiers),
3137                                 base_member.GetSignatureForError ());
3138                 }
3139
3140                 protected void Error_StaticReturnType ()
3141                 {
3142                         Report.Error (722, Location,
3143                                 "`{0}': static types cannot be used as return types",
3144                                 MemberType.GetSignatureForError ());
3145                 }
3146
3147                 /// <summary>
3148                 /// Gets base method and its return type
3149                 /// </summary>
3150                 protected virtual MemberSpec FindBaseMember (out MemberSpec bestCandidate)
3151                 {
3152                         return MemberCache.FindBaseMember (this, out bestCandidate);
3153                 }
3154
3155                 //
3156                 // The "short" name of this property / indexer / event.  This is the
3157                 // name without the explicit interface.
3158                 //
3159                 public string ShortName {
3160                         get { return MemberName.Name; }
3161                         set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
3162                 }
3163                 
3164                 //
3165                 // Returns full metadata method name
3166                 //
3167                 public string GetFullName (MemberName name)
3168                 {
3169                         return GetFullName (name.Name);
3170                 }
3171
3172                 public string GetFullName (string name)
3173                 {
3174                         if (!IsExplicitImpl)
3175                                 return name;
3176
3177                         //
3178                         // When dealing with explicit members a full interface type
3179                         // name is added to member name to avoid possible name conflicts
3180                         //
3181                         // We use CSharpName which gets us full name with benefit of
3182                         // replacing predefined names which saves some space and name
3183                         // is still unique
3184                         //
3185                         return TypeManager.CSharpName (InterfaceType) + "." + name;
3186                 }
3187
3188                 protected override bool VerifyClsCompliance ()
3189                 {
3190                         if (!base.VerifyClsCompliance ()) {
3191                                 return false;
3192                         }
3193
3194                         if (GenericMethod != null)
3195                                 GenericMethod.VerifyClsCompliance ();
3196
3197                         return true;
3198                 }
3199
3200                 public override bool IsUsed 
3201                 {
3202                         get { return IsExplicitImpl || base.IsUsed; }
3203                 }
3204
3205         }
3206
3207         public abstract class MemberBase : MemberCore
3208         {
3209                 protected FullNamedExpression type_expr;
3210                 protected TypeSpec member_type;
3211
3212                 public readonly DeclSpace ds;
3213                 public readonly GenericMethod GenericMethod;
3214
3215                 protected MemberBase (DeclSpace parent, GenericMethod generic,
3216                                       FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, Modifiers def_mod,
3217                                       MemberName name, Attributes attrs)
3218                         : base (parent, name, attrs)
3219                 {
3220                         this.ds = generic != null ? generic : (DeclSpace) parent;
3221                         this.type_expr = type;
3222                         ModFlags = ModifiersExtensions.Check (allowed_mod, mod, def_mod, Location, Report);
3223                         GenericMethod = generic;
3224                         if (GenericMethod != null)
3225                                 GenericMethod.ModFlags = ModFlags;
3226                 }
3227
3228                 #region Properties
3229
3230                 public TypeSpec MemberType {
3231                         get {
3232                                 return member_type;
3233                         }
3234                 }
3235
3236                 public FullNamedExpression TypeExpression {
3237                         get {
3238                                 return type_expr;
3239                         }
3240                 }
3241
3242                 #endregion
3243
3244                 //
3245                 // Main member define entry
3246                 //
3247                 public override bool Define ()
3248                 {
3249                         DoMemberTypeIndependentChecks ();
3250
3251                         //
3252                         // Returns false only when type resolution failed
3253                         //
3254                         if (!ResolveMemberType ())
3255                                 return false;
3256
3257                         DoMemberTypeDependentChecks ();
3258                         return true;
3259                 }
3260
3261                 //
3262                 // Any type_name independent checks
3263                 //
3264                 protected virtual void DoMemberTypeIndependentChecks ()
3265                 {
3266                         if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
3267                                 (ModFlags & (Modifiers.VIRTUAL | Modifiers.ABSTRACT)) != 0) {
3268                                 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
3269                                         GetSignatureForError (), Parent.GetSignatureForError ());
3270                         }
3271                 }
3272
3273                 //
3274                 // Any type_name dependent checks
3275                 //
3276                 protected virtual void DoMemberTypeDependentChecks ()
3277                 {
3278                         // verify accessibility
3279                         if (!IsAccessibleAs (MemberType)) {
3280                                 Report.SymbolRelatedToPreviousError (MemberType);
3281                                 if (this is Property)
3282                                         Report.Error (53, Location,
3283                                                       "Inconsistent accessibility: property type `" +
3284                                                       TypeManager.CSharpName (MemberType) + "' is less " +
3285                                                       "accessible than property `" + GetSignatureForError () + "'");
3286                                 else if (this is Indexer)
3287                                         Report.Error (54, Location,
3288                                                       "Inconsistent accessibility: indexer return type `" +
3289                                                       TypeManager.CSharpName (MemberType) + "' is less " +
3290                                                       "accessible than indexer `" + GetSignatureForError () + "'");
3291                                 else if (this is MethodCore) {
3292                                         if (this is Operator)
3293                                                 Report.Error (56, Location,
3294                                                               "Inconsistent accessibility: return type `" +
3295                                                               TypeManager.CSharpName (MemberType) + "' is less " +
3296                                                               "accessible than operator `" + GetSignatureForError () + "'");
3297                                         else
3298                                                 Report.Error (50, Location,
3299                                                               "Inconsistent accessibility: return type `" +
3300                                                               TypeManager.CSharpName (MemberType) + "' is less " +
3301                                                               "accessible than method `" + GetSignatureForError () + "'");
3302                                 } else {
3303                                         Report.Error (52, Location,
3304                                                       "Inconsistent accessibility: field type `" +
3305                                                       TypeManager.CSharpName (MemberType) + "' is less " +
3306                                                       "accessible than field `" + GetSignatureForError () + "'");
3307                                 }
3308                         }
3309
3310                         Variance variance = this is Event ? Variance.Contravariant : Variance.Covariant;
3311                         TypeManager.CheckTypeVariance (MemberType, variance, this);
3312                 }
3313
3314                 protected bool IsTypePermitted ()
3315                 {
3316                         if (TypeManager.IsSpecialType (MemberType)) {
3317                                 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
3318                                 return false;
3319                         }
3320                         return true;
3321                 }
3322
3323                 protected virtual bool CheckBase ()
3324                 {
3325                         CheckProtectedModifier ();
3326
3327                         return true;
3328                 }
3329
3330                 protected virtual bool ResolveMemberType ()
3331                 {
3332                         if (member_type != null)
3333                                 throw new InternalErrorException ("Multi-resolve");
3334
3335                         TypeExpr te = type_expr.ResolveAsTypeTerminal (this, false);
3336                         if (te == null)
3337                                 return false;
3338                         
3339                         //
3340                         // Replace original type name, error reporting can use fully resolved name
3341                         //
3342                         type_expr = te;
3343
3344                         member_type = te.Type;
3345                         return true;
3346                 }
3347         }
3348 }
3349