Major name lookup fixes
[mono.git] / mcs / mcs / decl.cs
1 //
2 // decl.cs: Declaration base class for structs, classes, enums and interfaces.
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //         Marek Safar (marek.safar@seznam.cz)
6 //
7 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 //
9 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2004-2008 Novell, Inc
11 //
12 //
13
14 using System;
15 using System.Text;
16 using System.Collections.Generic;
17 using System.Globalization;
18 using System.Reflection.Emit;
19 using System.Reflection;
20
21 #if NET_2_1
22 using XmlElement = System.Object;
23 #else
24 using System.Xml;
25 #endif
26
27 namespace Mono.CSharp {
28
29         //
30         // Better name would be DottenName
31         //
32         public class MemberName {
33                 public readonly string Name;
34                 public TypeArguments TypeArguments;
35
36                 public readonly MemberName Left;
37                 public readonly Location Location;
38
39                 public static readonly MemberName Null = new MemberName ("");
40
41                 bool is_double_colon;
42
43                 private MemberName (MemberName left, string name, bool is_double_colon,
44                                     Location loc)
45                 {
46                         this.Name = name;
47                         this.Location = loc;
48                         this.is_double_colon = is_double_colon;
49                         this.Left = left;
50                 }
51
52                 private MemberName (MemberName left, string name, bool is_double_colon,
53                                     TypeArguments args, Location loc)
54                         : this (left, name, is_double_colon, loc)
55                 {
56                         if (args != null && args.Count > 0)
57                                 this.TypeArguments = args;
58                 }
59
60                 public MemberName (string name)
61                         : this (name, Location.Null)
62                 { }
63
64                 public MemberName (string name, Location loc)
65                         : this (null, name, false, loc)
66                 { }
67
68                 public MemberName (string name, TypeArguments args, Location loc)
69                         : this (null, name, false, args, loc)
70                 { }
71
72                 public MemberName (MemberName left, string name)
73                         : this (left, name, left != null ? left.Location : Location.Null)
74                 { }
75
76                 public MemberName (MemberName left, string name, Location loc)
77                         : this (left, name, false, loc)
78                 { }
79
80                 public MemberName (MemberName left, string name, TypeArguments args, Location loc)
81                         : this (left, name, false, args, loc)
82                 { }
83
84                 public MemberName (string alias, string name, TypeArguments args, Location loc)
85                         : this (new MemberName (alias, loc), name, true, args, loc)
86                 { }
87
88                 public MemberName (MemberName left, MemberName right)
89                         : this (left, right, right.Location)
90                 { }
91
92                 public MemberName (MemberName left, MemberName right, Location loc)
93                         : this (null, right.Name, false, right.TypeArguments, loc)
94                 {
95                         if (right.is_double_colon)
96                                 throw new InternalErrorException ("Cannot append double_colon member name");
97                         this.Left = (right.Left == null) ? left : new MemberName (left, right.Left);
98                 }
99
100                 // TODO: Remove
101                 public string GetName ()
102                 {
103                         return GetName (false);
104                 }
105
106                 public int Arity {
107                         get {
108                                 return TypeArguments == null ? 0 : TypeArguments.Count;
109                         }
110                 }
111
112                 public bool IsGeneric {
113                         get {
114                                 if (TypeArguments != null)
115                                         return true;
116                                 else if (Left != null)
117                                         return Left.IsGeneric;
118                                 else
119                                         return false;
120                         }
121                 }
122
123                 public string GetName (bool is_generic)
124                 {
125                         string name = is_generic ? Basename : Name;
126                         if (Left != null)
127                                 return Left.GetName (is_generic) + (is_double_colon ? "::" : ".") + name;
128
129                         return name;
130                 }
131
132                 public ATypeNameExpression GetTypeExpression ()
133                 {
134                         if (Left == null) {
135                                 if (TypeArguments != null)
136                                         return new SimpleName (Name, TypeArguments, Location);
137                                 
138                                 return new SimpleName (Name, Location);
139                         }
140
141                         if (is_double_colon) {
142                                 if (Left.Left != null)
143                                         throw new InternalErrorException ("The left side of a :: should be an identifier");
144                                 return new QualifiedAliasMember (Left.Name, Name, TypeArguments, Location);
145                         }
146
147                         Expression lexpr = Left.GetTypeExpression ();
148                         return new MemberAccess (lexpr, Name, TypeArguments, Location);
149                 }
150
151                 public MemberName Clone ()
152                 {
153                         MemberName left_clone = Left == null ? null : Left.Clone ();
154                         return new MemberName (left_clone, Name, is_double_colon, TypeArguments, Location);
155                 }
156
157                 public string Basename {
158                         get {
159                                 if (TypeArguments != null)
160                                         return MakeName (Name, TypeArguments);
161                                 return Name;
162                         }
163                 }
164
165                 public string GetSignatureForError ()
166                 {
167                         string append = TypeArguments == null ? "" : "<" + TypeArguments.GetSignatureForError () + ">";
168                         if (Left == null)
169                                 return Name + append;
170                         string connect = is_double_colon ? "::" : ".";
171                         return Left.GetSignatureForError () + connect + Name + append;
172                 }
173
174                 public override bool Equals (object other)
175                 {
176                         return Equals (other as MemberName);
177                 }
178
179                 public bool Equals (MemberName other)
180                 {
181                         if (this == other)
182                                 return true;
183                         if (other == null || Name != other.Name)
184                                 return false;
185                         if (is_double_colon != other.is_double_colon)
186                                 return false;
187
188                         if ((TypeArguments != null) &&
189                             (other.TypeArguments == null || TypeArguments.Count != other.TypeArguments.Count))
190                                 return false;
191
192                         if ((TypeArguments == null) && (other.TypeArguments != null))
193                                 return false;
194
195                         if (Left == null)
196                                 return other.Left == null;
197
198                         return Left.Equals (other.Left);
199                 }
200
201                 public override int GetHashCode ()
202                 {
203                         int hash = Name.GetHashCode ();
204                         for (MemberName n = Left; n != null; n = n.Left)
205                                 hash ^= n.Name.GetHashCode ();
206                         if (is_double_colon)
207                                 hash ^= 0xbadc01d;
208
209                         if (TypeArguments != null)
210                                 hash ^= TypeArguments.Count << 5;
211
212                         return hash & 0x7FFFFFFF;
213                 }
214
215                 public int CountTypeArguments {
216                         get {
217                                 if (TypeArguments != null)
218                                         return TypeArguments.Count;
219                                 else if (Left != null)
220                                         return Left.CountTypeArguments; 
221                                 else
222                                         return 0;
223                         }
224                 }
225
226                 public static string MakeName (string name, TypeArguments args)
227                 {
228                         if (args == null)
229                                 return name;
230
231                         return name + "`" + args.Count;
232                 }
233
234                 public static string MakeName (string name, int count)
235                 {
236                         return name + "`" + count;
237                 }
238         }
239
240         public class SimpleMemberName
241         {
242                 public string Value;
243                 public Location Location;
244
245                 public SimpleMemberName (string name, Location loc)
246                 {
247                         this.Value = name;
248                         this.Location = loc;
249                 }
250         }
251
252         /// <summary>
253         ///   Base representation for members.  This is used to keep track
254         ///   of Name, Location and Modifier flags, and handling Attributes.
255         /// </summary>
256         [System.Diagnostics.DebuggerDisplay ("{GetSignatureForError()}")]
257         public abstract class MemberCore : Attributable, IMemberContext, IMemberDefinition
258         {
259                 /// <summary>
260                 ///   Public name
261                 /// </summary>
262
263                 protected string cached_name;
264                 // TODO: Remove in favor of MemberName
265                 public string Name {
266                         get {
267                                 if (cached_name == null)
268                                         cached_name = MemberName.GetName (!(this is GenericMethod) && !(this is Method));
269                                 return cached_name;
270                         }
271                 }
272
273                 string IMemberDefinition.Name {
274                         get {
275                                 return member_name.Name;
276                         }
277                 }
278
279                 // Is not readonly because of IndexerName attribute
280                 private MemberName member_name;
281                 public MemberName MemberName {
282                         get { return member_name; }
283                 }
284
285                 /// <summary>
286                 ///   Modifier flags that the user specified in the source code
287                 /// </summary>
288                 private Modifiers mod_flags;
289                 public Modifiers ModFlags {
290                         set {
291                                 mod_flags = value;
292                                 if ((value & Modifiers.COMPILER_GENERATED) != 0)
293                                         caching_flags = Flags.IsUsed | Flags.IsAssigned;
294                         }
295                         get {
296                                 return mod_flags;
297                         }
298                 }
299
300                 public /*readonly*/ TypeContainer Parent;
301
302                 /// <summary>
303                 ///   Location where this declaration happens
304                 /// </summary>
305                 public Location Location {
306                         get { return member_name.Location; }
307                 }
308
309                 /// <summary>
310                 ///   XML documentation comment
311                 /// </summary>
312                 protected string comment;
313
314                 /// <summary>
315                 ///   Represents header string for documentation comment 
316                 ///   for each member types.
317                 /// </summary>
318                 public abstract string DocCommentHeader { get; }
319
320                 [Flags]
321                 public enum Flags {
322                         Obsolete_Undetected = 1,                // Obsolete attribute has not been detected yet
323                         Obsolete = 1 << 1,                      // Type has obsolete attribute
324                         ClsCompliance_Undetected = 1 << 2,      // CLS Compliance has not been detected yet
325                         ClsCompliant = 1 << 3,                  // Type is CLS Compliant
326                         CloseTypeCreated = 1 << 4,              // Tracks whether we have Closed the type
327                         HasCompliantAttribute_Undetected = 1 << 5,      // Presence of CLSCompliantAttribute has not been detected
328                         HasClsCompliantAttribute = 1 << 6,                      // Type has CLSCompliantAttribute
329                         ClsCompliantAttributeFalse = 1 << 7,                    // Member has CLSCompliant(false)
330                         Excluded_Undetected = 1 << 8,           // Conditional attribute has not been detected yet
331                         Excluded = 1 << 9,                                      // Method is conditional
332                         MethodOverloadsExist = 1 << 10,         // Test for duplication must be performed
333                         IsUsed = 1 << 11,
334                         IsAssigned = 1 << 12,                           // Field is assigned
335                         HasExplicitLayout       = 1 << 13,
336                         PartialDefinitionExists = 1 << 14,      // Set when corresponding partial method definition exists
337                         HasStructLayout         = 1 << 15                       // Has StructLayoutAttribute
338                 }
339
340                 /// <summary>
341                 ///   MemberCore flags at first detected then cached
342                 /// </summary>
343                 internal Flags caching_flags;
344
345                 public MemberCore (DeclSpace parent, MemberName name, Attributes attrs)
346                 {
347                         this.Parent = parent as TypeContainer;
348                         member_name = name;
349                         caching_flags = Flags.Obsolete_Undetected | Flags.ClsCompliance_Undetected | Flags.HasCompliantAttribute_Undetected | Flags.Excluded_Undetected;
350                         AddAttributes (attrs, this);
351                 }
352
353                 public virtual Assembly Assembly {
354                         get { return Parent.Module.Assembly; }
355                 }
356
357                 protected virtual void SetMemberName (MemberName new_name)
358                 {
359                         member_name = new_name;
360                         cached_name = null;
361                 }
362
363                 protected bool CheckAbstractAndExtern (bool has_block)
364                 {
365                         if (Parent.PartialContainer.Kind == MemberKind.Interface)
366                                 return true;
367
368                         if (has_block) {
369                                 if ((ModFlags & Modifiers.EXTERN) != 0) {
370                                         Report.Error (179, Location, "`{0}' cannot declare a body because it is marked extern",
371                                                 GetSignatureForError ());
372                                         return false;
373                                 }
374
375                                 if ((ModFlags & Modifiers.ABSTRACT) != 0) {
376                                         Report.Error (500, Location, "`{0}' cannot declare a body because it is marked abstract",
377                                                 GetSignatureForError ());
378                                         return false;
379                                 }
380                         } else {
381                                 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN | Modifiers.PARTIAL)) == 0 && !(Parent is Delegate)) {
382                                         if (RootContext.Version >= LanguageVersion.V_3) {
383                                                 Property.PropertyMethod pm = this as Property.PropertyMethod;
384                                                 if (pm is Indexer.GetIndexerMethod || pm is Indexer.SetIndexerMethod)
385                                                         pm = null;
386
387                                                 if (pm != null && pm.Property.AccessorSecond == null) {
388                                                         Report.Error (840, Location,
389                                                                 "`{0}' must have a body because it is not marked abstract or extern. The property can be automatically implemented when you define both accessors",
390                                                                 GetSignatureForError ());
391                                                         return false;
392                                                 }
393                                         }
394
395                                         Report.Error (501, Location, "`{0}' must have a body because it is not marked abstract, extern, or partial",
396                                                       GetSignatureForError ());
397                                         return false;
398                                 }
399                         }
400
401                         return true;
402                 }
403
404                 protected void CheckProtectedModifier ()
405                 {
406                         if ((ModFlags & Modifiers.PROTECTED) == 0)
407                                 return;
408
409                         if (Parent.PartialContainer.Kind == MemberKind.Struct) {
410                                 Report.Error (666, Location, "`{0}': Structs cannot contain protected members",
411                                         GetSignatureForError ());
412                                 return;
413                         }
414
415                         if ((Parent.ModFlags & Modifiers.STATIC) != 0) {
416                                 Report.Error (1057, Location, "`{0}': Static classes cannot contain protected members",
417                                         GetSignatureForError ());
418                                 return;
419                         }
420
421                         if ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.OVERRIDE) == 0 &&
422                                 !(this is Destructor)) {
423                                 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class",
424                                         GetSignatureForError ());
425                                 return;
426                         }
427                 }
428
429                 public abstract bool Define ();
430
431                 public virtual string DocComment {
432                         get {
433                                 return comment;
434                         }
435                         set {
436                                 comment = value;
437                         }
438                 }
439
440                 // 
441                 // Returns full member name for error message
442                 //
443                 public virtual string GetSignatureForError ()
444                 {
445                         if (Parent == null || Parent.Parent == null)
446                                 return member_name.GetSignatureForError ();
447
448                         return Parent.GetSignatureForError () + "." + member_name.GetSignatureForError ();
449                 }
450
451                 /// <summary>
452                 /// Base Emit method. This is also entry point for CLS-Compliant verification.
453                 /// </summary>
454                 public virtual void Emit ()
455                 {
456                         if (!RootContext.VerifyClsCompliance)
457                                 return;
458
459                         VerifyClsCompliance ();
460                 }
461
462                 public bool IsCompilerGenerated {
463                         get     {
464                                 if ((mod_flags & Modifiers.COMPILER_GENERATED) != 0)
465                                         return true;
466
467                                 return Parent == null ? false : Parent.IsCompilerGenerated;
468                         }
469                 }
470
471                 public bool IsImported {
472                         get {
473                                 return false;
474                         }
475                 }
476
477                 public virtual bool IsUsed {
478                         get {
479                                 return (caching_flags & Flags.IsUsed) != 0;
480                         }
481                 }
482
483                 protected Report Report {
484                         get {
485                                 return Compiler.Report;
486                         }
487                 }
488
489                 public void SetIsUsed ()
490                 {
491                         caching_flags |= Flags.IsUsed;
492                 }
493
494                 public void SetIsAssigned ()
495                 {
496                         caching_flags |= Flags.IsAssigned;
497                 }
498
499                 /// <summary>
500                 /// Returns instance of ObsoleteAttribute for this MemberCore
501                 /// </summary>
502                 public virtual ObsoleteAttribute GetAttributeObsolete ()
503                 {
504                         if ((caching_flags & (Flags.Obsolete_Undetected | Flags.Obsolete)) == 0)
505                                 return null;
506
507                         caching_flags &= ~Flags.Obsolete_Undetected;
508
509                         if (OptAttributes == null)
510                                 return null;
511
512                         Attribute obsolete_attr = OptAttributes.Search (PredefinedAttributes.Get.Obsolete);
513                         if (obsolete_attr == null)
514                                 return null;
515
516                         caching_flags |= Flags.Obsolete;
517
518                         ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute ();
519                         if (obsolete == null)
520                                 return null;
521
522                         return obsolete;
523                 }
524
525                 /// <summary>
526                 /// Checks for ObsoleteAttribute presence. It's used for testing of all non-types elements
527                 /// </summary>
528                 public virtual void CheckObsoleteness (Location loc)
529                 {
530                         ObsoleteAttribute oa = GetAttributeObsolete ();
531                         if (oa != null)
532                                 AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, Report);
533                 }
534
535                 //
536                 // Checks whether the type P is as accessible as this member
537                 //
538                 public bool IsAccessibleAs (TypeSpec p)
539                 {
540                         //
541                         // if M is private, its accessibility is the same as this declspace.
542                         // we already know that P is accessible to T before this method, so we
543                         // may return true.
544                         //
545                         if ((mod_flags & Modifiers.PRIVATE) != 0)
546                                 return true;
547
548                         while (TypeManager.HasElementType (p))
549                                 p = TypeManager.GetElementType (p);
550
551                         if (p.IsGenericParameter)
552                                 return true;
553
554                         for (TypeSpec p_parent; p != null; p = p_parent) {
555                                 p_parent = p.DeclaringType;
556
557                                 if (p.IsGeneric) {
558                                         foreach (TypeSpec t in p.TypeArguments) {
559                                                 if (!IsAccessibleAs (t))
560                                                         return false;
561                                         }
562                                 }
563
564                                 var pAccess = p.Modifiers & Modifiers.AccessibilityMask;
565                                 if (pAccess == Modifiers.PUBLIC)
566                                         continue;
567
568                                 bool same_access_restrictions = false;
569                                 for (MemberCore mc = this; !same_access_restrictions && mc != null && mc.Parent != null; mc = mc.Parent) {
570                                         var al = mc.ModFlags & Modifiers.AccessibilityMask;
571                                         switch (pAccess) {
572                                         case Modifiers.INTERNAL:
573                                                 if (al == Modifiers.PRIVATE || al == Modifiers.INTERNAL)
574                                                         same_access_restrictions = TypeManager.IsThisOrFriendAssembly (Parent.Module.Assembly, p.Assembly);
575                                                 
576                                                 break;
577
578                                         case Modifiers.PROTECTED:
579                                                 if (al == Modifiers.PROTECTED) {
580                                                         same_access_restrictions = mc.Parent.IsBaseTypeDefinition (p_parent);
581                                                         break;
582                                                 }
583
584                                                 if (al == Modifiers.PRIVATE) {
585                                                         //
586                                                         // When type is private and any of its parents derives from
587                                                         // protected type then the type is accessible
588                                                         //
589                                                         while (mc.Parent != null) {
590                                                                 if (mc.Parent.IsBaseTypeDefinition (p_parent))
591                                                                         same_access_restrictions = true;
592                                                                 mc = mc.Parent; 
593                                                         }
594                                                 }
595                                                 
596                                                 break;
597
598                                         case Modifiers.PROTECTED | Modifiers.INTERNAL:
599                                                 if (al == Modifiers.INTERNAL)
600                                                         same_access_restrictions = TypeManager.IsThisOrFriendAssembly (Parent.Module.Assembly, p.Assembly);
601                                                 else if (al == Modifiers.PROTECTED)
602                                                         same_access_restrictions = mc.Parent.IsBaseTypeDefinition (p_parent);
603                                                 else if (al == (Modifiers.PROTECTED | Modifiers.INTERNAL))
604                                                         same_access_restrictions = mc.Parent.IsBaseTypeDefinition (p_parent) &&
605                                                                 TypeManager.IsThisOrFriendAssembly (Parent.Module.Assembly, p.Assembly);
606                                                 break;
607
608                                         case Modifiers.PRIVATE:
609                                                 //
610                                                 // Both are private and share same parent
611                                                 //
612                                                 if (al == Modifiers.PRIVATE) {
613                                                         var decl = mc.Parent;
614                                                         do {
615                                                                 same_access_restrictions = decl.CurrentType == p_parent;
616                                                         } while (!same_access_restrictions && !decl.IsTopLevel && (decl = decl.Parent) != null);
617                                                 }
618                                                 
619                                                 break;
620                                                 
621                                         default:
622                                                 throw new InternalErrorException (al.ToString ());
623                                         }
624                                 }
625                                 
626                                 if (!same_access_restrictions)
627                                         return false;
628                         }
629
630                         return true;
631                 }
632
633                 /// <summary>
634                 /// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
635                 /// </summary>
636                 public override bool IsClsComplianceRequired ()
637                 {
638                         if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
639                                 return (caching_flags & Flags.ClsCompliant) != 0;
640
641                         caching_flags &= ~Flags.ClsCompliance_Undetected;
642
643                         if (HasClsCompliantAttribute) {
644                                 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0)
645                                         return false;
646
647                                 caching_flags |= Flags.ClsCompliant;
648                                 return true;
649                         }
650
651                         if (Parent.PartialContainer.IsClsComplianceRequired ()) {
652                                 caching_flags |= Flags.ClsCompliant;
653                                 return true;
654                         }
655
656                         return false;
657                 }
658
659                 public virtual string[] ConditionalConditions ()
660                 {
661                         return null;
662                 }
663
664                 /// <summary>
665                 /// Returns true when MemberCore is exposed from assembly.
666                 /// </summary>
667                 public bool IsExposedFromAssembly ()
668                 {
669                         if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
670                                 return false;
671                         
672                         DeclSpace parentContainer = Parent.PartialContainer;
673                         while (parentContainer != null && parentContainer.ModFlags != 0) {
674                                 if ((parentContainer.ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
675                                         return false;
676                                 parentContainer = parentContainer.Parent;
677                         }
678                         return true;
679                 }
680
681                 public virtual ExtensionMethodGroupExpr LookupExtensionMethod (TypeSpec extensionType, string name, int arity, Location loc)
682                 {
683                         return Parent.LookupExtensionMethod (extensionType, name, arity, loc);
684                 }
685
686                 public virtual FullNamedExpression LookupNamespaceAlias (string name)
687                 {
688                         return Parent.NamespaceEntry.LookupNamespaceAlias (name);
689                 }
690
691                 public virtual FullNamedExpression LookupNamespaceOrType (string name, int arity, Location loc, bool ignore_cs0104)
692                 {
693                         return Parent.LookupNamespaceOrType (name, arity, loc, ignore_cs0104);
694                 }
695
696                 /// <summary>
697                 /// Goes through class hierarchy and gets value of first found CLSCompliantAttribute.
698                 /// If no is attribute exists then assembly CLSCompliantAttribute is returned.
699                 /// </summary>
700                 public bool IsNotCLSCompliant ()
701                 {
702                         if ((caching_flags & Flags.HasCompliantAttribute_Undetected) == 0)
703                                 return (caching_flags & Flags.ClsCompliantAttributeFalse) != 0;
704
705                         caching_flags &= ~Flags.HasCompliantAttribute_Undetected;
706
707                         if (OptAttributes != null) {
708                                 Attribute cls_attribute = OptAttributes.Search (PredefinedAttributes.Get.CLSCompliant);
709                                 if (cls_attribute != null) {
710                                         caching_flags |= Flags.HasClsCompliantAttribute;
711                                         if (cls_attribute.GetClsCompliantAttributeValue ())
712                                                 return false;
713
714                                         caching_flags |= Flags.ClsCompliantAttributeFalse;
715                                         return true;
716                                 }
717                         }
718
719                         return false;
720                 }
721
722                 /// <summary>
723                 /// Returns true if MemberCore is explicitly marked with CLSCompliantAttribute
724                 /// </summary>
725                 protected bool HasClsCompliantAttribute {
726                         get {
727                                 if ((caching_flags & Flags.HasCompliantAttribute_Undetected) != 0)
728                                         IsNotCLSCompliant ();
729                                 
730                                 return (caching_flags & Flags.HasClsCompliantAttribute) != 0;
731                         }
732                 }
733
734                 /// <summary>
735                 /// Returns true when a member supports multiple overloads (methods, indexers, etc)
736                 /// </summary>
737                 public virtual bool EnableOverloadChecks (MemberCore overload)
738                 {
739                         return false;
740                 }
741
742                 /// <summary>
743                 /// The main virtual method for CLS-Compliant verifications.
744                 /// The method returns true if member is CLS-Compliant and false if member is not
745                 /// CLS-Compliant which means that CLS-Compliant tests are not necessary. A descendants override it
746                 /// and add their extra verifications.
747                 /// </summary>
748                 protected virtual bool VerifyClsCompliance ()
749                 {
750                         if (HasClsCompliantAttribute) {
751                                 if (CodeGen.Assembly.ClsCompliantAttribute == null) {
752                                         Attribute a = OptAttributes.Search (PredefinedAttributes.Get.CLSCompliant);
753                                         if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
754                                                 Report.Warning (3021, 2, a.Location,
755                                                         "`{0}' does not need a CLSCompliant attribute because the assembly is not marked as CLS-compliant",
756                                                         GetSignatureForError ());
757                                         } else {
758                                                 Report.Warning (3014, 1, a.Location,
759                                                         "`{0}' cannot be marked as CLS-compliant because the assembly is not marked as CLS-compliant",
760                                                         GetSignatureForError ());
761                                         }
762                                         return false;
763                                 }
764
765                                 if (!IsExposedFromAssembly ()) {
766                                         Attribute a = OptAttributes.Search (PredefinedAttributes.Get.CLSCompliant);
767                                         Report.Warning (3019, 2, a.Location, "CLS compliance checking will not be performed on `{0}' because it is not visible from outside this assembly", GetSignatureForError ());
768                                         return false;
769                                 }
770
771                                 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
772                                         if (Parent.Kind == MemberKind.Interface && Parent.IsClsComplianceRequired ()) {
773                                                 Report.Warning (3010, 1, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
774                                         } else if (Parent.Kind == MemberKind.Class && (ModFlags & Modifiers.ABSTRACT) != 0 && Parent.IsClsComplianceRequired ()) {
775                                                 Report.Warning (3011, 1, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
776                                         }
777
778                                         return false;
779                                 }
780
781                                 if (Parent.Parent != null && !Parent.IsClsComplianceRequired ()) {
782                                         Attribute a = OptAttributes.Search (PredefinedAttributes.Get.CLSCompliant);
783                                         Report.Warning (3018, 1, a.Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'",
784                                                 GetSignatureForError (), Parent.GetSignatureForError ());
785                                         return false;
786                                 }
787                         } else {
788                                 if (!IsExposedFromAssembly ())
789                                         return false;
790
791                                 if (!Parent.PartialContainer.IsClsComplianceRequired ())
792                                         return false;
793                         }
794
795                         if (member_name.Name [0] == '_') {
796                                 Report.Warning (3008, 1, Location, "Identifier `{0}' is not CLS-compliant", GetSignatureForError () );
797                         }
798
799                         return true;
800                 }
801
802                 //
803                 // Raised (and passed an XmlElement that contains the comment)
804                 // when GenerateDocComment is writing documentation expectedly.
805                 //
806                 internal virtual void OnGenerateDocComment (XmlElement intermediateNode)
807                 {
808                 }
809
810                 //
811                 // Returns a string that represents the signature for this 
812                 // member which should be used in XML documentation.
813                 //
814                 public virtual string GetDocCommentName (DeclSpace ds)
815                 {
816                         if (ds == null || this is DeclSpace)
817                                 return DocCommentHeader + Name;
818                         else
819                                 return String.Concat (DocCommentHeader, ds.Name, ".", Name);
820                 }
821
822                 //
823                 // Generates xml doc comments (if any), and if required,
824                 // handle warning report.
825                 //
826                 internal virtual void GenerateDocComment (DeclSpace ds)
827                 {
828                         try {
829                                 DocUtil.GenerateDocComment (this, ds, Report);
830                         } catch (Exception e) {
831                                 throw new InternalErrorException (this, e);
832                         }
833                 }
834
835                 #region IMemberContext Members
836
837                 public virtual CompilerContext Compiler {
838                         get { return Parent.Compiler; }
839                 }
840
841                 public virtual TypeSpec CurrentType {
842                         get { return Parent.CurrentType; }
843                 }
844
845                 public MemberCore CurrentMemberDefinition {
846                         get { return this; }
847                 }
848
849                 public virtual TypeParameter[] CurrentTypeParameters {
850                         get { return null; }
851                 }
852
853                 public virtual bool HasUnresolvedConstraints {
854                         get { return false; }
855                 }
856
857                 public bool IsObsolete {
858                         get {
859                                 if (GetAttributeObsolete () != null)
860                                         return true;
861
862                                 return Parent == null ? false : Parent.IsObsolete;
863                         }
864                 }
865
866                 public bool IsUnsafe {
867                         get {
868                                 if ((ModFlags & Modifiers.UNSAFE) != 0)
869                                         return true;
870
871                                 return Parent == null ? false : Parent.IsUnsafe;
872                         }
873                 }
874
875                 public bool IsStatic {
876                         get {
877                                 return (ModFlags & Modifiers.STATIC) != 0;
878                         }
879                 }
880
881                 #endregion
882         }
883
884         //
885         // Base member specification. A member specification contains
886         // member details which can alter in the context (e.g. generic instances)
887         //
888         public abstract class MemberSpec
889         {
890                 [Flags]
891                 protected enum StateFlags
892                 {
893                         Obsolete_Undetected = 1,        // Obsolete attribute has not been detected yet
894                         Obsolete = 1 << 1,                      // Member has obsolete attribute
895                         CLSCompliant_Undetected = 1 << 3,       // CLSCompliant attribute has not been detected yet
896                         CLSCompliant = 1 << 4,          // Member is CLS Compliant
897
898                         IsAccessor = 1 << 9,            // Method is an accessor
899                         IsGeneric = 1 << 10,            // Member contains type arguments
900
901                         PendingMetaInflate = 1 << 12,
902                         PendingMakeMethod = 1 << 13,
903                         PendingMemberCacheMembers = 1 << 14,
904                         PendingBaseTypeInflate = 1 << 15,
905                         InterfacesExpanded = 1 << 16,
906                         IsNotRealProperty = 1 << 17,
907                 }
908
909                 protected Modifiers modifiers;
910                 protected StateFlags state;
911                 protected IMemberDefinition definition;
912                 public readonly MemberKind Kind;
913                 protected TypeSpec declaringType;
914
915 #if DEBUG
916                 static int counter;
917                 public int ID = counter++;
918 #endif
919
920                 protected MemberSpec (MemberKind kind, TypeSpec declaringType, IMemberDefinition definition, Modifiers modifiers)
921                 {
922                         this.Kind = kind;
923                         this.declaringType = declaringType;
924                         this.definition = definition;
925                         this.modifiers = modifiers;
926
927                         state = StateFlags.Obsolete_Undetected | StateFlags.CLSCompliant_Undetected;
928                 }
929
930                 #region Properties
931
932                 public Assembly Assembly {
933                         get {
934                                 return definition.Assembly;
935                         }
936                 }
937
938                 public virtual int Arity {
939                         get {
940                                 return 0;
941                         }
942                 }
943
944                 public TypeSpec DeclaringType {
945                         get {
946                                 return declaringType;
947                         }
948                         set {
949                                 declaringType = value;
950                         }
951                 }
952
953                 public IMemberDefinition MemberDefinition {
954                         get {
955                                 return definition;
956                         }
957                 }
958
959                 public Modifiers Modifiers {
960                         get {
961                                 return modifiers;
962                         }
963                         set {
964                                 modifiers = value;
965                         }
966                 }
967                 
968                 public virtual string Name {
969                         get {
970                                 return definition.Name;
971                         }
972                 }
973
974                 public bool IsAbstract {
975                         get { return (modifiers & Modifiers.ABSTRACT) != 0; }
976                 }
977
978                 public bool IsAccessor {
979                         get {
980                                 return (state & StateFlags.IsAccessor) != 0;
981                         }
982                         set {
983                                 state = value ? state | StateFlags.IsAccessor : state & ~StateFlags.IsAccessor;
984                         }
985                 }
986
987                 //
988                 // Return true when this member is a generic in C# terms
989                 // A nested non-generic type of generic type will return false
990                 //
991                 public bool IsGeneric {
992                         get {
993                                 return (state & StateFlags.IsGeneric) != 0;
994                         }
995                         set {
996                                 state = value ? state | StateFlags.IsGeneric : state & ~StateFlags.IsGeneric;
997                         }
998                 }
999
1000                 public bool IsPrivate {
1001                         get { return (modifiers & Modifiers.PRIVATE) != 0; }
1002                 }
1003
1004                 public bool IsPublic {
1005                         get { return (modifiers & Modifiers.PUBLIC) != 0; }
1006                 }
1007
1008                 public bool IsStatic {
1009                         get { 
1010                                 return (modifiers & Modifiers.STATIC) != 0;
1011                         }
1012                 }
1013
1014                 #endregion
1015
1016                 public virtual ObsoleteAttribute GetAttributeObsolete ()
1017                 {
1018                         if ((state & (StateFlags.Obsolete | StateFlags.Obsolete_Undetected)) == 0)
1019                                 return null;
1020
1021                         state &= ~StateFlags.Obsolete_Undetected;
1022
1023                         var oa = definition.GetAttributeObsolete ();
1024                         if (oa != null)
1025                                 state |= StateFlags.Obsolete;
1026
1027                         return oa;
1028                 }
1029
1030                 protected virtual bool IsNotCLSCompliant ()
1031                 {
1032                         return MemberDefinition.IsNotCLSCompliant ();
1033                 }
1034
1035                 public virtual string GetSignatureForError ()
1036                 {
1037                         var bf = MemberDefinition as Property.BackingField;
1038                         var name = bf == null ? Name : bf.OriginalName;
1039                         return DeclaringType.GetSignatureForError () + "." + name;
1040                 }
1041
1042                 public virtual MemberSpec InflateMember (TypeParameterInflator inflator)
1043                 {
1044                         var inflated = (MemberSpec) MemberwiseClone ();
1045                         inflated.declaringType = inflator.TypeInstance;
1046                         inflated.state |= StateFlags.PendingMetaInflate;
1047 #if DEBUG
1048                         if (inflated.ID > 0)
1049                                 inflated.ID = -inflated.ID;
1050 #endif
1051                         return inflated;
1052                 }
1053
1054                 //
1055                 // Is this member accessible from invocationType
1056                 //
1057                 public bool IsAccessible (TypeSpec invocationType)
1058                 {
1059                         var ma = Modifiers & Modifiers.AccessibilityMask;
1060                         if (ma == Modifiers.PUBLIC)
1061                                 return true;
1062
1063                         var parentType = /* this as TypeSpec ?? */ DeclaringType;
1064                 
1065                         //
1066                         // If only accessible to the current class or children
1067                         //
1068                         if (ma == Modifiers.PRIVATE)
1069                                 return invocationType.MemberDefinition == parentType.MemberDefinition ||
1070                                         TypeManager.IsNestedChildOf (invocationType, parentType);
1071
1072                         if ((ma & Modifiers.INTERNAL) != 0) {
1073                                 var b = TypeManager.IsThisOrFriendAssembly (invocationType == InternalType.FakeInternalType ?
1074                                          CodeGen.Assembly.Builder : invocationType.Assembly, parentType.Assembly);
1075                                 if (b || ma == Modifiers.INTERNAL)
1076                                         return b;
1077                         }
1078
1079                         // PROTECTED
1080                         if (!TypeManager.IsNestedFamilyAccessible (invocationType, parentType))
1081                                 return false;
1082
1083                         return true;
1084                 }
1085
1086                 //
1087                 // Returns member CLS compliance based on full member hierarchy
1088                 //
1089                 public bool IsCLSCompliant ()
1090                 {
1091                         if ((state & StateFlags.CLSCompliant_Undetected) != 0) {
1092                                 state &= ~StateFlags.CLSCompliant_Undetected;
1093
1094                                 if (IsNotCLSCompliant ())
1095                                         return false;
1096
1097                                 bool compliant;
1098                                 if (DeclaringType != null) {
1099                                         compliant = DeclaringType.IsCLSCompliant ();
1100                                 } else {
1101                                         // TODO: NEED AssemblySpec
1102                                         if (MemberDefinition.IsImported) {
1103                                                 var attr = MemberDefinition.Assembly.GetCustomAttributes (typeof (CLSCompliantAttribute), false);
1104                                                 compliant = attr.Length > 0 && ((CLSCompliantAttribute) attr[0]).IsCompliant;
1105                                         } else {
1106                                                 compliant = CodeGen.Assembly.IsClsCompliant;
1107                                         }
1108                                 }
1109
1110                                 if (compliant)
1111                                         state |= StateFlags.CLSCompliant;
1112                         }
1113
1114                         return (state & StateFlags.CLSCompliant) != 0;
1115                 }
1116
1117                 public bool IsConditionallyExcluded (Location loc)
1118                 {
1119                         if ((Kind & (MemberKind.Class | MemberKind.Method)) == 0)
1120                                 return false;
1121
1122                         var conditions = MemberDefinition.ConditionalConditions ();
1123                         if (conditions == null)
1124                                 return false;
1125
1126                         foreach (var condition in conditions) {
1127                                 if (loc.CompilationUnit.IsConditionalDefined (condition))
1128                                         return false;
1129                         }
1130
1131                         return true;
1132                 }
1133
1134                 public override string ToString ()
1135                 {
1136                         return GetSignatureForError ();
1137                 }
1138         }
1139
1140         //
1141         // Member details which are same between all member
1142         // specifications
1143         //
1144         public interface IMemberDefinition
1145         {
1146                 Assembly Assembly { get; }
1147                 string Name { get; }
1148                 bool IsImported { get; }
1149
1150                 string[] ConditionalConditions ();
1151                 ObsoleteAttribute GetAttributeObsolete ();
1152                 bool IsNotCLSCompliant ();
1153                 void SetIsAssigned ();
1154                 void SetIsUsed ();
1155         }
1156
1157         public interface IParametersMember : IInterfaceMemberSpec
1158         {
1159                 AParametersCollection Parameters { get; }
1160         }
1161
1162         public interface IInterfaceMemberSpec
1163         {
1164                 TypeSpec MemberType { get; }
1165         }
1166
1167         //
1168         // Base type container declaration. It exists to handle partial types
1169         // which share same definition (PartialContainer) but have different
1170         // resolve scopes
1171         //
1172         public abstract class DeclSpace : MemberCore {
1173                 /// <summary>
1174                 ///   This points to the actual definition that is being
1175                 ///   created with System.Reflection.Emit
1176                 /// </summary>
1177                 public TypeBuilder TypeBuilder;
1178
1179                 //
1180                 // This is the namespace in which this typecontainer
1181                 // was declared.  We use this to resolve names.
1182                 //
1183                 public NamespaceEntry NamespaceEntry;
1184
1185                 private Dictionary<string, FullNamedExpression> Cache = new Dictionary<string, FullNamedExpression> ();
1186                 
1187                 public readonly string Basename;
1188                 
1189                 protected Dictionary<string, MemberCore> defined_names;
1190
1191                 public TypeContainer PartialContainer;          
1192
1193                 protected readonly bool is_generic;
1194                 readonly int count_type_params;
1195                 protected TypeParameter[] type_params;
1196                 TypeParameter[] type_param_list;
1197
1198                 //
1199                 // Whether we are Generic
1200                 //
1201                 public bool IsGeneric {
1202                         get {
1203                                 if (is_generic)
1204                                         return true;
1205                                 else if (Parent != null)
1206                                         return Parent.IsGeneric;
1207                                 else
1208                                         return false;
1209                         }
1210                 }
1211
1212                 static string[] attribute_targets = new string [] { "type" };
1213
1214                 public DeclSpace (NamespaceEntry ns, DeclSpace parent, MemberName name,
1215                                   Attributes attrs)
1216                         : base (parent, name, attrs)
1217                 {
1218                         NamespaceEntry = ns;
1219                         Basename = name.Basename;
1220                         defined_names = new Dictionary<string, MemberCore> ();
1221                         PartialContainer = null;
1222                         if (name.TypeArguments != null) {
1223                                 is_generic = true;
1224                                 count_type_params = name.TypeArguments.Count;
1225                         }
1226                         if (parent != null)
1227                                 count_type_params += parent.count_type_params;
1228                 }
1229
1230                 /// <summary>
1231                 /// Adds the member to defined_names table. It tests for duplications and enclosing name conflicts
1232                 /// </summary>
1233                 protected virtual bool AddToContainer (MemberCore symbol, string name)
1234                 {
1235                         MemberCore mc;
1236                         if (!defined_names.TryGetValue (name, out mc)) {
1237                                 defined_names.Add (name, symbol);
1238                                 return true;
1239                         }
1240
1241                         if (((mc.ModFlags | symbol.ModFlags) & Modifiers.COMPILER_GENERATED) != 0)
1242                                 return true;
1243
1244                         if (symbol.EnableOverloadChecks (mc))
1245                                 return true;
1246
1247                         InterfaceMemberBase im = mc as InterfaceMemberBase;
1248                         if (im != null && im.IsExplicitImpl)
1249                                 return true;
1250
1251                         Report.SymbolRelatedToPreviousError (mc);
1252                         if ((mc.ModFlags & Modifiers.PARTIAL) != 0 && (symbol is ClassOrStruct || symbol is Interface)) {
1253                                 Error_MissingPartialModifier (symbol);
1254                                 return false;
1255                         }
1256
1257                         if (this is ModuleContainer) {
1258                                 Report.Error (101, symbol.Location, 
1259                                         "The namespace `{0}' already contains a definition for `{1}'",
1260                                         ((DeclSpace)symbol).NamespaceEntry.GetSignatureForError (), symbol.MemberName.Name);
1261                         } else if (symbol is TypeParameter) {
1262                                 Report.Error (692, symbol.Location,
1263                                         "Duplicate type parameter `{0}'", symbol.GetSignatureForError ());
1264                         } else {
1265                                 Report.Error (102, symbol.Location,
1266                                               "The type `{0}' already contains a definition for `{1}'",
1267                                               GetSignatureForError (), symbol.MemberName.Name);
1268                         }
1269
1270                         return false;
1271                 }
1272
1273                 protected void RemoveFromContainer (string name)
1274                 {
1275                         defined_names.Remove (name);
1276                 }
1277                 
1278                 /// <summary>
1279                 ///   Returns the MemberCore associated with a given name in the declaration
1280                 ///   space. It doesn't return method based symbols !!
1281                 /// </summary>
1282                 /// 
1283                 public MemberCore GetDefinition (string name)
1284                 {
1285                         MemberCore mc = null;
1286                         defined_names.TryGetValue (name, out mc);
1287                         return mc;
1288                 }
1289         
1290                 // 
1291                 // root_types contains all the types.  All TopLevel types
1292                 // hence have a parent that points to `root_types', that is
1293                 // why there is a non-obvious test down here.
1294                 //
1295                 public bool IsTopLevel {
1296                         get { return (Parent != null && Parent.Parent == null); }
1297                 }
1298
1299                 public virtual bool IsUnmanagedType ()
1300                 {
1301                         return false;
1302                 }
1303
1304                 protected virtual TypeAttributes TypeAttr {
1305                         get { return Module.DefaultCharSetType; }
1306                 }
1307
1308                 /// <remarks>
1309                 ///  Should be overriten by the appropriate declaration space
1310                 /// </remarks>
1311                 public abstract TypeBuilder DefineType ();
1312
1313                 protected void Error_MissingPartialModifier (MemberCore type)
1314                 {
1315                         Report.Error (260, type.Location,
1316                                 "Missing partial modifier on declaration of type `{0}'. Another partial declaration of this type exists",
1317                                 type.GetSignatureForError ());
1318                 }
1319
1320                 public override string GetSignatureForError ()
1321                 {
1322                         return MemberName.GetSignatureForError ();
1323                 }
1324                 
1325                 public bool CheckAccessLevel (TypeSpec check_type)
1326                 {
1327 // TODO: Use this instead
1328 //                      return PartialContainer.Definition.IsAccessible (check_type);
1329
1330                         TypeSpec tb = PartialContainer.Definition;
1331                         check_type = check_type.GetDefinition ();
1332
1333                         var check_attr = check_type.Modifiers & Modifiers.AccessibilityMask;
1334
1335                         switch (check_attr){
1336                         case Modifiers.PUBLIC:
1337                                 return true;
1338
1339                         case Modifiers.INTERNAL:
1340                                 return TypeManager.IsThisOrFriendAssembly (Assembly, check_type.Assembly);
1341                                 
1342                         case Modifiers.PRIVATE:
1343                                 TypeSpec declaring = check_type.DeclaringType;
1344                                 return tb == declaring.GetDefinition () || TypeManager.IsNestedChildOf (tb, declaring); 
1345
1346                         case Modifiers.PROTECTED:
1347                                 //
1348                                 // Only accessible to methods in current type or any subtypes
1349                                 //
1350                                 return TypeManager.IsNestedFamilyAccessible (tb, check_type.DeclaringType);
1351
1352                         case Modifiers.PROTECTED | Modifiers.INTERNAL:
1353                                 if (TypeManager.IsThisOrFriendAssembly (Assembly, check_type.Assembly))
1354                                         return true;
1355
1356                                 goto case Modifiers.PROTECTED;
1357                         }
1358
1359                         throw new NotImplementedException (check_attr.ToString ());
1360                 }
1361
1362                 private TypeSpec LookupNestedTypeInHierarchy (string name, int arity)
1363                 {
1364                         // TODO: GenericMethod only
1365                         if (PartialContainer == null)
1366                                 return null;
1367
1368                         // Has any nested type
1369                         // Does not work, because base type can have
1370                         //if (PartialContainer.Types == null)
1371                         //      return null;
1372
1373                         var container = PartialContainer.CurrentType;
1374
1375                         // Is not Root container
1376                         if (container == null)
1377                                 return null;
1378
1379                         var     t = MemberCache.FindNestedType (container, name, arity);
1380                         if (t == null)
1381                                 return null;
1382
1383                         // FIXME: Breaks error reporting
1384                         if (!t.IsAccessible (CurrentType))
1385                                 return null;
1386
1387                         return t;
1388                 }
1389
1390                 //
1391                 // Public function used to locate types.
1392                 //
1393                 // Set 'ignore_cs0104' to true if you want to ignore cs0104 errors.
1394                 //
1395                 // Returns: Type or null if they type can not be found.
1396                 //
1397                 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, Location loc, bool ignore_cs0104)
1398                 {
1399                         FullNamedExpression e;
1400                         if (arity == 0 && Cache.TryGetValue (name, out e))
1401                                 return e;
1402
1403                         e = null;
1404                         int errors = Report.Errors;
1405
1406                         if (arity == 0) {
1407                                 TypeParameter[] tp = CurrentTypeParameters;
1408                                 if (tp != null) {
1409                                         TypeParameter tparam = TypeParameter.FindTypeParameter (tp, name);
1410                                         if (tparam != null)
1411                                                 e = new TypeParameterExpr (tparam, Location.Null);
1412                                 }
1413                         }
1414
1415                         if (e == null) {
1416                                 TypeSpec t = LookupNestedTypeInHierarchy (name, arity);
1417
1418                                 if (t != null)
1419                                         e = new TypeExpression (t, Location.Null);
1420                                 else if (Parent != null)
1421                                         e = Parent.LookupNamespaceOrType (name, arity, loc, ignore_cs0104);
1422                                 else
1423                                         e = NamespaceEntry.LookupNamespaceOrType (name, arity, loc, ignore_cs0104);
1424                         }
1425
1426                         // TODO MemberCache: How to cache arity stuff ?
1427                         if (errors == Report.Errors && arity == 0)
1428                                 Cache [name] = e;
1429                         
1430                         return e;
1431                 }
1432
1433                 public override Assembly Assembly {
1434                         get { return Module.Assembly; }
1435                 }
1436
1437                 public virtual ModuleContainer Module {
1438                         get { return Parent.Module; }
1439                 }
1440
1441                 TypeParameter[] initialize_type_params ()
1442                 {
1443                         if (type_param_list != null)
1444                                 return type_param_list;
1445
1446                         DeclSpace the_parent = Parent;
1447                         if (this is GenericMethod)
1448                                 the_parent = null;
1449
1450                         var list = new List<TypeParameter> ();
1451                         if (the_parent != null && the_parent.IsGeneric) {
1452                                 // FIXME: move generics info out of DeclSpace
1453                                 TypeParameter[] parent_params = the_parent.TypeParameters;
1454                                 list.AddRange (parent_params);
1455                         }
1456  
1457                         int count = type_params != null ? type_params.Length : 0;
1458                         for (int i = 0; i < count; i++) {
1459                                 TypeParameter param = type_params [i];
1460                                 list.Add (param);
1461                                 if (Parent.CurrentTypeParameters != null) {
1462                                         foreach (TypeParameter tp in Parent.CurrentTypeParameters) {
1463                                                 if (tp.Name != param.Name)                              
1464                                                         continue;
1465
1466                                                 Report.SymbolRelatedToPreviousError (tp.Location, null);
1467                                                 Report.Warning (693, 3, param.Location,
1468                                                         "Type parameter `{0}' has the same name as the type parameter from outer type `{1}'",
1469                                                         param.Name, Parent.GetSignatureForError ());
1470                                         }
1471                                 }
1472                         }
1473
1474                         type_param_list = new TypeParameter [list.Count];
1475                         list.CopyTo (type_param_list, 0);
1476                         return type_param_list;
1477                 }
1478
1479                 public virtual void SetParameterInfo (List<Constraints> constraints_list)
1480                 {
1481                         if (!is_generic) {
1482                                 if (constraints_list != null) {
1483                                         Report.Error (
1484                                                 80, Location, "Constraints are not allowed " +
1485                                                 "on non-generic declarations");
1486                                 }
1487
1488                                 return;
1489                         }
1490
1491                         TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
1492                         type_params = new TypeParameter [names.Length];
1493
1494                         //
1495                         // Register all the names
1496                         //
1497                         for (int i = 0; i < type_params.Length; i++) {
1498                                 TypeParameterName name = names [i];
1499
1500                                 Constraints constraints = null;
1501                                 if (constraints_list != null) {
1502                                         int total = constraints_list.Count;
1503                                         for (int ii = 0; ii < total; ++ii) {
1504                                                 Constraints constraints_at = (Constraints)constraints_list[ii];
1505                                                 // TODO: it is used by iterators only
1506                                                 if (constraints_at == null) {
1507                                                         constraints_list.RemoveAt (ii);
1508                                                         --total;
1509                                                         continue;
1510                                                 }
1511                                                 if (constraints_at.TypeParameter.Value == name.Name) {
1512                                                         constraints = constraints_at;
1513                                                         constraints_list.RemoveAt(ii);
1514                                                         break;
1515                                                 }
1516                                         }
1517                                 }
1518
1519                                 Variance variance = name.Variance;
1520                                 if (name.Variance != Variance.None && !(this is Delegate || this is Interface)) {
1521                                         Report.Error (1960, name.Location, "Variant type parameters can only be used with interfaces and delegates");
1522                                         variance = Variance.None;
1523                                 }
1524
1525                                 type_params [i] = new TypeParameter (
1526                                         Parent, i, new MemberName (name.Name, Location), constraints, name.OptAttributes, variance);
1527
1528                                 AddToContainer (type_params [i], name.Name);
1529                         }
1530
1531                         if (constraints_list != null && constraints_list.Count > 0) {
1532                                 foreach (Constraints constraint in constraints_list) {
1533                                         Report.Error(699, constraint.Location, "`{0}': A constraint references nonexistent type parameter `{1}'", 
1534                                                 GetSignatureForError (), constraint.TypeParameter.Value);
1535                                 }
1536                         }
1537                 }
1538
1539                 protected TypeParameter[] TypeParameters {
1540                         get {
1541                                 if (!IsGeneric)
1542                                         throw new InvalidOperationException ();
1543                                 if ((PartialContainer != null) && (PartialContainer != this))
1544                                         return PartialContainer.TypeParameters;
1545                                 if (type_param_list == null)
1546                                         initialize_type_params ();
1547
1548                                 return type_param_list;
1549                         }
1550                 }
1551
1552                 public int CountTypeParameters {
1553                         get {
1554                                 return count_type_params;
1555                         }
1556                 }
1557
1558                 public override string[] ValidAttributeTargets {
1559                         get { return attribute_targets; }
1560                 }
1561
1562                 protected override bool VerifyClsCompliance ()
1563                 {
1564                         if (!base.VerifyClsCompliance ()) {
1565                                 return false;
1566                         }
1567
1568                         if (type_params != null) {
1569                                 foreach (TypeParameter tp in type_params) {
1570                                         tp.VerifyClsCompliance ();
1571                                 }
1572                         }
1573
1574                         return true;
1575                 }
1576         }
1577 }