New tests.
[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 readonly 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.Get.IsDummy || pm.Property.Set.IsDummy)) {
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 { return (caching_flags & Flags.IsUsed) != 0; }
479                 }
480
481                 protected Report Report {
482                         get {
483                                 return Compiler.Report;
484                         }
485                 }
486
487                 public void SetIsUsed ()
488                 {
489                         caching_flags |= Flags.IsUsed;
490                 }
491
492                 public void SetIsAssigned ()
493                 {
494                         caching_flags |= Flags.IsAssigned;
495                 }
496
497                 /// <summary>
498                 /// Returns instance of ObsoleteAttribute for this MemberCore
499                 /// </summary>
500                 public virtual ObsoleteAttribute GetAttributeObsolete ()
501                 {
502                         if ((caching_flags & (Flags.Obsolete_Undetected | Flags.Obsolete)) == 0)
503                                 return null;
504
505                         caching_flags &= ~Flags.Obsolete_Undetected;
506
507                         if (OptAttributes == null)
508                                 return null;
509
510                         Attribute obsolete_attr = OptAttributes.Search (PredefinedAttributes.Get.Obsolete);
511                         if (obsolete_attr == null)
512                                 return null;
513
514                         caching_flags |= Flags.Obsolete;
515
516                         ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute ();
517                         if (obsolete == null)
518                                 return null;
519
520                         return obsolete;
521                 }
522
523                 /// <summary>
524                 /// Checks for ObsoleteAttribute presence. It's used for testing of all non-types elements
525                 /// </summary>
526                 public virtual void CheckObsoleteness (Location loc)
527                 {
528                         ObsoleteAttribute oa = GetAttributeObsolete ();
529                         if (oa != null)
530                                 AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, Report);
531                 }
532
533                 //
534                 // Checks whether the type P is as accessible as this member
535                 //
536                 public bool IsAccessibleAs (TypeSpec p)
537                 {
538                         //
539                         // if M is private, its accessibility is the same as this declspace.
540                         // we already know that P is accessible to T before this method, so we
541                         // may return true.
542                         //
543                         if ((mod_flags & Modifiers.PRIVATE) != 0)
544                                 return true;
545
546                         while (TypeManager.HasElementType (p))
547                                 p = TypeManager.GetElementType (p);
548
549                         if (p.IsGenericParameter)
550                                 return true;
551
552                         for (TypeSpec p_parent; p != null; p = p_parent) {
553                                 p_parent = p.DeclaringType;
554
555                                 if (p.IsGeneric) {
556                                         foreach (TypeSpec t in p.TypeArguments) {
557                                                 if (!IsAccessibleAs (t))
558                                                         return false;
559                                         }
560                                 }
561
562                                 var pAccess = p.Modifiers & Modifiers.AccessibilityMask;
563                                 if (pAccess == Modifiers.PUBLIC)
564                                         continue;
565
566                                 bool same_access_restrictions = false;
567                                 for (MemberCore mc = this; !same_access_restrictions && mc != null && mc.Parent != null; mc = mc.Parent) {
568                                         var al = mc.ModFlags & Modifiers.AccessibilityMask;
569                                         switch (pAccess) {
570                                         case Modifiers.INTERNAL:
571                                                 if (al == Modifiers.PRIVATE || al == Modifiers.INTERNAL)
572                                                         same_access_restrictions = TypeManager.IsThisOrFriendAssembly (Parent.Module.Assembly, p.Assembly);
573                                                 
574                                                 break;
575
576                                         case Modifiers.PROTECTED:
577                                                 if (al == Modifiers.PROTECTED) {
578                                                         same_access_restrictions = mc.Parent.IsBaseTypeDefinition (p_parent);
579                                                         break;
580                                                 }
581
582                                                 if (al == Modifiers.PRIVATE) {
583                                                         //
584                                                         // When type is private and any of its parents derives from
585                                                         // protected type then the type is accessible
586                                                         //
587                                                         while (mc.Parent != null) {
588                                                                 if (mc.Parent.IsBaseTypeDefinition (p_parent))
589                                                                         same_access_restrictions = true;
590                                                                 mc = mc.Parent; 
591                                                         }
592                                                 }
593                                                 
594                                                 break;
595
596                                         case Modifiers.PROTECTED | Modifiers.INTERNAL:
597                                                 if (al == Modifiers.INTERNAL)
598                                                         same_access_restrictions = TypeManager.IsThisOrFriendAssembly (Parent.Module.Assembly, p.Assembly);
599                                                 else if (al == Modifiers.PROTECTED)
600                                                         same_access_restrictions = mc.Parent.IsBaseTypeDefinition (p_parent);
601                                                 else if (al == (Modifiers.PROTECTED | Modifiers.INTERNAL))
602                                                         same_access_restrictions = mc.Parent.IsBaseTypeDefinition (p_parent) &&
603                                                                 TypeManager.IsThisOrFriendAssembly (Parent.Module.Assembly, p.Assembly);
604                                                 break;
605
606                                         case Modifiers.PRIVATE:
607                                                 //
608                                                 // Both are private and share same parent
609                                                 //
610                                                 if (al == Modifiers.PRIVATE) {
611                                                         var decl = mc.Parent;
612                                                         do {
613                                                                 same_access_restrictions = decl.CurrentType == p_parent;
614                                                         } while (!same_access_restrictions && !decl.IsTopLevel && (decl = decl.Parent) != null);
615                                                 }
616                                                 
617                                                 break;
618                                                 
619                                         default:
620                                                 throw new InternalErrorException (al.ToString ());
621                                         }
622                                 }
623                                 
624                                 if (!same_access_restrictions)
625                                         return false;
626                         }
627
628                         return true;
629                 }
630
631                 /// <summary>
632                 /// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
633                 /// </summary>
634                 public override bool IsClsComplianceRequired ()
635                 {
636                         if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
637                                 return (caching_flags & Flags.ClsCompliant) != 0;
638
639                         caching_flags &= ~Flags.ClsCompliance_Undetected;
640
641                         if (HasClsCompliantAttribute) {
642                                 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0)
643                                         return false;
644
645                                 caching_flags |= Flags.ClsCompliant;
646                                 return true;
647                         }
648
649                         if (Parent.PartialContainer.IsClsComplianceRequired ()) {
650                                 caching_flags |= Flags.ClsCompliant;
651                                 return true;
652                         }
653
654                         return false;
655                 }
656
657                 public virtual string[] ConditionalConditions ()
658                 {
659                         return null;
660                 }
661
662                 /// <summary>
663                 /// Returns true when MemberCore is exposed from assembly.
664                 /// </summary>
665                 public bool IsExposedFromAssembly ()
666                 {
667                         if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
668                                 return false;
669                         
670                         DeclSpace parentContainer = Parent.PartialContainer;
671                         while (parentContainer != null && parentContainer.ModFlags != 0) {
672                                 if ((parentContainer.ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
673                                         return false;
674                                 parentContainer = parentContainer.Parent;
675                         }
676                         return true;
677                 }
678
679                 public virtual ExtensionMethodGroupExpr LookupExtensionMethod (TypeSpec extensionType, string name, int arity, Location loc)
680                 {
681                         return Parent.LookupExtensionMethod (extensionType, name, arity, loc);
682                 }
683
684                 public virtual FullNamedExpression LookupNamespaceAlias (string name)
685                 {
686                         return Parent.NamespaceEntry.LookupNamespaceAlias (name);
687                 }
688
689                 public virtual FullNamedExpression LookupNamespaceOrType (string name, int arity, Location loc, bool ignore_cs0104)
690                 {
691                         return Parent.LookupNamespaceOrType (name, arity, loc, ignore_cs0104);
692                 }
693
694                 /// <summary>
695                 /// Goes through class hierarchy and gets value of first found CLSCompliantAttribute.
696                 /// If no is attribute exists then assembly CLSCompliantAttribute is returned.
697                 /// </summary>
698                 public bool IsNotCLSCompliant ()
699                 {
700                         if ((caching_flags & Flags.HasCompliantAttribute_Undetected) == 0)
701                                 return (caching_flags & Flags.ClsCompliantAttributeFalse) != 0;
702
703                         caching_flags &= ~Flags.HasCompliantAttribute_Undetected;
704
705                         if (OptAttributes != null) {
706                                 Attribute cls_attribute = OptAttributes.Search (PredefinedAttributes.Get.CLSCompliant);
707                                 if (cls_attribute != null) {
708                                         caching_flags |= Flags.HasClsCompliantAttribute;
709                                         if (cls_attribute.GetClsCompliantAttributeValue ())
710                                                 return false;
711
712                                         caching_flags |= Flags.ClsCompliantAttributeFalse;
713                                         return true;
714                                 }
715                         }
716
717                         return false;
718                 }
719
720                 /// <summary>
721                 /// Returns true if MemberCore is explicitly marked with CLSCompliantAttribute
722                 /// </summary>
723                 protected bool HasClsCompliantAttribute {
724                         get {
725                                 if ((caching_flags & Flags.HasCompliantAttribute_Undetected) != 0)
726                                         IsNotCLSCompliant ();
727                                 
728                                 return (caching_flags & Flags.HasClsCompliantAttribute) != 0;
729                         }
730                 }
731
732                 /// <summary>
733                 /// Returns true when a member supports multiple overloads (methods, indexers, etc)
734                 /// </summary>
735                 public virtual bool EnableOverloadChecks (MemberCore overload)
736                 {
737                         return false;
738                 }
739
740                 /// <summary>
741                 /// The main virtual method for CLS-Compliant verifications.
742                 /// The method returns true if member is CLS-Compliant and false if member is not
743                 /// CLS-Compliant which means that CLS-Compliant tests are not necessary. A descendants override it
744                 /// and add their extra verifications.
745                 /// </summary>
746                 protected virtual bool VerifyClsCompliance ()
747                 {
748                         if (HasClsCompliantAttribute) {
749                                 if (CodeGen.Assembly.ClsCompliantAttribute == null) {
750                                         Attribute a = OptAttributes.Search (PredefinedAttributes.Get.CLSCompliant);
751                                         if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
752                                                 Report.Warning (3021, 2, a.Location,
753                                                         "`{0}' does not need a CLSCompliant attribute because the assembly is not marked as CLS-compliant",
754                                                         GetSignatureForError ());
755                                         } else {
756                                                 Report.Warning (3014, 1, a.Location,
757                                                         "`{0}' cannot be marked as CLS-compliant because the assembly is not marked as CLS-compliant",
758                                                         GetSignatureForError ());
759                                         }
760                                         return false;
761                                 }
762
763                                 if (!IsExposedFromAssembly ()) {
764                                         Attribute a = OptAttributes.Search (PredefinedAttributes.Get.CLSCompliant);
765                                         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 ());
766                                         return false;
767                                 }
768
769                                 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
770                                         if (Parent.Kind == MemberKind.Interface && Parent.IsClsComplianceRequired ()) {
771                                                 Report.Warning (3010, 1, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
772                                         } else if (Parent.Kind == MemberKind.Class && (ModFlags & Modifiers.ABSTRACT) != 0 && Parent.IsClsComplianceRequired ()) {
773                                                 Report.Warning (3011, 1, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
774                                         }
775
776                                         return false;
777                                 }
778
779                                 if (Parent.Parent != null && !Parent.IsClsComplianceRequired ()) {
780                                         Attribute a = OptAttributes.Search (PredefinedAttributes.Get.CLSCompliant);
781                                         Report.Warning (3018, 1, a.Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'",
782                                                 GetSignatureForError (), Parent.GetSignatureForError ());
783                                         return false;
784                                 }
785                         } else {
786                                 if (!IsExposedFromAssembly ())
787                                         return false;
788
789                                 if (!Parent.PartialContainer.IsClsComplianceRequired ())
790                                         return false;
791                         }
792
793                         if (member_name.Name [0] == '_') {
794                                 Report.Warning (3008, 1, Location, "Identifier `{0}' is not CLS-compliant", GetSignatureForError () );
795                         }
796
797                         return true;
798                 }
799
800                 //
801                 // Raised (and passed an XmlElement that contains the comment)
802                 // when GenerateDocComment is writing documentation expectedly.
803                 //
804                 internal virtual void OnGenerateDocComment (XmlElement intermediateNode)
805                 {
806                 }
807
808                 //
809                 // Returns a string that represents the signature for this 
810                 // member which should be used in XML documentation.
811                 //
812                 public virtual string GetDocCommentName (DeclSpace ds)
813                 {
814                         if (ds == null || this is DeclSpace)
815                                 return DocCommentHeader + Name;
816                         else
817                                 return String.Concat (DocCommentHeader, ds.Name, ".", Name);
818                 }
819
820                 //
821                 // Generates xml doc comments (if any), and if required,
822                 // handle warning report.
823                 //
824                 internal virtual void GenerateDocComment (DeclSpace ds)
825                 {
826                         try {
827                                 DocUtil.GenerateDocComment (this, ds, Report);
828                         } catch (Exception e) {
829                                 throw new InternalErrorException (this, e);
830                         }
831                 }
832
833                 #region IMemberContext Members
834
835                 public virtual CompilerContext Compiler {
836                         get { return Parent.Compiler; }
837                 }
838
839                 public virtual TypeSpec CurrentType {
840                         get { return Parent.CurrentType; }
841                 }
842
843                 public MemberCore CurrentMemberDefinition {
844                         get { return this; }
845                 }
846
847                 public virtual TypeParameter[] CurrentTypeParameters {
848                         get { return null; }
849                 }
850
851                 public virtual bool HasUnresolvedConstraints {
852                         get { return false; }
853                 }
854
855                 public bool IsObsolete {
856                         get {
857                                 if (GetAttributeObsolete () != null)
858                                         return true;
859
860                                 return Parent == null ? false : Parent.IsObsolete;
861                         }
862                 }
863
864                 public bool IsUnsafe {
865                         get {
866                                 if ((ModFlags & Modifiers.UNSAFE) != 0)
867                                         return true;
868
869                                 return Parent == null ? false : Parent.IsUnsafe;
870                         }
871                 }
872
873                 public bool IsStatic {
874                         get {
875                                 return (ModFlags & Modifiers.STATIC) != 0;
876                         }
877                 }
878
879                 #endregion
880         }
881
882         //
883         // Base member specification. A member specification contains
884         // member details which can alter in the context (e.g. generic instances)
885         //
886         public abstract class MemberSpec
887         {
888                 [Flags]
889                 protected enum StateFlags
890                 {
891                         Obsolete_Undetected = 1,        // Obsolete attribute has not been detected yet
892                         Obsolete = 1 << 1,                      // Member has obsolete attribute
893                         CLSCompliant_Undetected = 1 << 3,       // CLSCompliant attribute has not been detected yet
894                         CLSCompliant = 1 << 4,          // Member is CLS Compliant
895
896                         IsAccessor = 1 << 9,            // Method is an accessor
897                         IsGeneric = 1 << 10,            // Member contains type arguments
898
899                         PendingMetaInflate = 1 << 12,
900                         PendingMakeMethod = 1 << 13,
901                         PendingMemberCacheMembers = 1 << 14,
902                         PendingBaseTypeInflate = 1 << 15,
903                         InterfacesExpanded = 1 << 16,
904                         IsNotRealProperty = 1 << 17,
905                 }
906
907                 protected Modifiers modifiers;
908                 protected StateFlags state;
909                 protected IMemberDefinition definition;
910                 public readonly MemberKind Kind;
911                 protected TypeSpec declaringType;
912
913 #if DEBUG
914                 static int counter;
915                 public int ID = counter++;
916 #endif
917
918                 protected MemberSpec (MemberKind kind, TypeSpec declaringType, IMemberDefinition definition, Modifiers modifiers)
919                 {
920                         this.Kind = kind;
921                         this.declaringType = declaringType;
922                         this.definition = definition;
923                         this.modifiers = modifiers;
924
925                         state = StateFlags.Obsolete_Undetected | StateFlags.CLSCompliant_Undetected;
926                 }
927
928                 #region Properties
929
930                 public Assembly Assembly {
931                         get {
932                                 return definition.Assembly;
933                         }
934                 }
935
936                 public virtual int Arity {
937                         get {
938                                 return 0;
939                         }
940                 }
941
942                 public TypeSpec DeclaringType {
943                         get {
944                                 return declaringType;
945                         }
946                         set {
947                                 declaringType = value;
948                         }
949                 }
950
951                 public IMemberDefinition MemberDefinition {
952                         get {
953                                 return definition;
954                         }
955                 }
956
957                 public Modifiers Modifiers {
958                         get {
959                                 return modifiers;
960                         }
961                         set {
962                                 modifiers = value;
963                         }
964                 }
965                 
966                 public virtual string Name {
967                         get {
968                                 return definition.Name;
969                         }
970                 }
971
972                 public bool IsAbstract {
973                         get { return (modifiers & Modifiers.ABSTRACT) != 0; }
974                 }
975
976                 public bool IsAccessor {
977                         get {
978                                 return (state & StateFlags.IsAccessor) != 0;
979                         }
980                         set {
981                                 state = value ? state | StateFlags.IsAccessor : state & ~StateFlags.IsAccessor;
982                         }
983                 }
984
985                 //
986                 // Return true when this member is a generic in C# terms
987                 // therefore nested non-generic type of generic type will
988                 // return false
989                 //
990                 public bool IsGeneric {
991                         get {
992                                 return (state & StateFlags.IsGeneric) != 0;
993                         }
994                         set {
995                                 state = value ? state | StateFlags.IsGeneric : state & ~StateFlags.IsGeneric;
996                         }
997                 }
998
999                 public bool IsPrivate {
1000                         get { return (modifiers & Modifiers.PRIVATE) != 0; }
1001                 }
1002
1003                 public bool IsStatic {
1004                         get { 
1005                                 return (modifiers & Modifiers.STATIC) != 0;
1006                         }
1007                 }
1008
1009                 #endregion
1010
1011                 public virtual ObsoleteAttribute GetAttributeObsolete ()
1012                 {
1013                         if ((state & (StateFlags.Obsolete | StateFlags.Obsolete_Undetected)) == 0)
1014                                 return null;
1015
1016                         state &= ~StateFlags.Obsolete_Undetected;
1017
1018                         var oa = definition.GetAttributeObsolete ();
1019                         if (oa != null)
1020                                 state |= StateFlags.Obsolete;
1021
1022                         return oa;
1023                 }
1024
1025                 protected virtual bool IsNotCLSCompliant ()
1026                 {
1027                         return MemberDefinition.IsNotCLSCompliant ();
1028                 }
1029
1030                 public virtual string GetSignatureForError ()
1031                 {
1032                         var bf = MemberDefinition as Property.BackingField;
1033                         var name = bf == null ? Name : bf.OriginalName;
1034                         return DeclaringType.GetSignatureForError () + "." + name;
1035                 }
1036
1037                 public virtual MemberSpec InflateMember (TypeParameterInflator inflator)
1038                 {
1039                         var inflated = (MemberSpec) MemberwiseClone ();
1040                         inflated.declaringType = inflator.TypeInstance;
1041                         inflated.state |= StateFlags.PendingMetaInflate;
1042                         return inflated;
1043                 }
1044
1045                 //
1046                 // Returns member CLS compliance based on full member hierarchy
1047                 //
1048                 public bool IsCLSCompliant ()
1049                 {
1050                         if ((state & StateFlags.CLSCompliant_Undetected) != 0) {
1051                                 state &= ~StateFlags.CLSCompliant_Undetected;
1052
1053                                 if (IsNotCLSCompliant ())
1054                                         return false;
1055
1056                                 bool compliant;
1057                                 if (DeclaringType != null) {
1058                                         compliant = DeclaringType.IsCLSCompliant ();
1059                                 } else {
1060                                         // TODO: NEED AssemblySpec
1061                                         if (MemberDefinition.IsImported) {
1062                                                 var attr = MemberDefinition.Assembly.GetCustomAttributes (typeof (CLSCompliantAttribute), false);
1063                                                 compliant = attr.Length > 0 && ((CLSCompliantAttribute) attr[0]).IsCompliant;
1064                                         } else {
1065                                                 compliant = CodeGen.Assembly.IsClsCompliant;
1066                                         }
1067                                 }
1068
1069                                 if (compliant)
1070                                         state |= StateFlags.CLSCompliant;
1071                         }
1072
1073                         return (state & StateFlags.CLSCompliant) != 0;
1074                 }
1075
1076                 public bool IsConditionallyExcluded (Location loc)
1077                 {
1078                         if ((Kind & (MemberKind.Class | MemberKind.Method)) == 0)
1079                                 return false;
1080
1081                         var conditions = MemberDefinition.ConditionalConditions ();
1082                         if (conditions == null)
1083                                 return false;
1084
1085                         foreach (var condition in conditions) {
1086                                 if (loc.CompilationUnit.IsConditionalDefined (condition))
1087                                         return false;
1088                         }
1089
1090                         return true;
1091                 }
1092
1093                 public override string ToString ()
1094                 {
1095                         return GetSignatureForError ();
1096                 }
1097         }
1098
1099         //
1100         // Member details which are same between all member
1101         // specifications
1102         //
1103         public interface IMemberDefinition
1104         {
1105                 Assembly Assembly { get; }
1106                 string Name { get; }
1107                 bool IsImported { get; }
1108
1109                 string[] ConditionalConditions ();
1110                 ObsoleteAttribute GetAttributeObsolete ();
1111                 bool IsNotCLSCompliant ();
1112                 void SetIsAssigned ();
1113                 void SetIsUsed ();
1114         }
1115
1116         public interface IParametersMember : IInterfaceMemberSpec
1117         {
1118                 AParametersCollection Parameters { get; }
1119         }
1120
1121         public interface IInterfaceMemberSpec
1122         {
1123                 TypeSpec MemberType { get; }
1124         }
1125
1126         //
1127         // Base type container declaration. It exists to handle partial types
1128         // which share same definition (PartialContainer) but have different
1129         // resolve scopes
1130         //
1131         public abstract class DeclSpace : MemberCore {
1132                 /// <summary>
1133                 ///   This points to the actual definition that is being
1134                 ///   created with System.Reflection.Emit
1135                 /// </summary>
1136                 public TypeBuilder TypeBuilder;
1137
1138                 //
1139                 // This is the namespace in which this typecontainer
1140                 // was declared.  We use this to resolve names.
1141                 //
1142                 public NamespaceEntry NamespaceEntry;
1143
1144                 private Dictionary<string, FullNamedExpression> Cache = new Dictionary<string, FullNamedExpression> ();
1145                 
1146                 public readonly string Basename;
1147                 
1148                 protected Dictionary<string, MemberCore> defined_names;
1149
1150                 public TypeContainer PartialContainer;          
1151
1152                 protected readonly bool is_generic;
1153                 readonly int count_type_params;
1154                 protected TypeParameter[] type_params;
1155                 TypeParameter[] type_param_list;
1156
1157                 //
1158                 // Whether we are Generic
1159                 //
1160                 public bool IsGeneric {
1161                         get {
1162                                 if (is_generic)
1163                                         return true;
1164                                 else if (Parent != null)
1165                                         return Parent.IsGeneric;
1166                                 else
1167                                         return false;
1168                         }
1169                 }
1170
1171                 static string[] attribute_targets = new string [] { "type" };
1172
1173                 public DeclSpace (NamespaceEntry ns, DeclSpace parent, MemberName name,
1174                                   Attributes attrs)
1175                         : base (parent, name, attrs)
1176                 {
1177                         NamespaceEntry = ns;
1178                         Basename = name.Basename;
1179                         defined_names = new Dictionary<string, MemberCore> ();
1180                         PartialContainer = null;
1181                         if (name.TypeArguments != null) {
1182                                 is_generic = true;
1183                                 count_type_params = name.TypeArguments.Count;
1184                         }
1185                         if (parent != null)
1186                                 count_type_params += parent.count_type_params;
1187                 }
1188
1189                 /// <summary>
1190                 /// Adds the member to defined_names table. It tests for duplications and enclosing name conflicts
1191                 /// </summary>
1192                 protected virtual bool AddToContainer (MemberCore symbol, string name)
1193                 {
1194                         MemberCore mc;
1195                         if (!defined_names.TryGetValue (name, out mc)) {
1196                                 defined_names.Add (name, symbol);
1197                                 return true;
1198                         }
1199
1200                         if (((mc.ModFlags | symbol.ModFlags) & Modifiers.COMPILER_GENERATED) != 0)
1201                                 return true;
1202
1203                         if (symbol.EnableOverloadChecks (mc))
1204                                 return true;
1205
1206                         InterfaceMemberBase im = mc as InterfaceMemberBase;
1207                         if (im != null && im.IsExplicitImpl)
1208                                 return true;
1209
1210                         Report.SymbolRelatedToPreviousError (mc);
1211                         if ((mc.ModFlags & Modifiers.PARTIAL) != 0 && (symbol is ClassOrStruct || symbol is Interface)) {
1212                                 Error_MissingPartialModifier (symbol);
1213                                 return false;
1214                         }
1215
1216                         if (this is ModuleContainer) {
1217                                 Report.Error (101, symbol.Location, 
1218                                         "The namespace `{0}' already contains a definition for `{1}'",
1219                                         ((DeclSpace)symbol).NamespaceEntry.GetSignatureForError (), symbol.MemberName.Name);
1220                         } else if (symbol is TypeParameter) {
1221                                 Report.Error (692, symbol.Location,
1222                                         "Duplicate type parameter `{0}'", symbol.GetSignatureForError ());
1223                         } else {
1224                                 Report.Error (102, symbol.Location,
1225                                               "The type `{0}' already contains a definition for `{1}'",
1226                                               GetSignatureForError (), symbol.MemberName.Name);
1227                         }
1228
1229                         return false;
1230                 }
1231
1232                 protected void RemoveFromContainer (string name)
1233                 {
1234                         defined_names.Remove (name);
1235                 }
1236                 
1237                 /// <summary>
1238                 ///   Returns the MemberCore associated with a given name in the declaration
1239                 ///   space. It doesn't return method based symbols !!
1240                 /// </summary>
1241                 /// 
1242                 public MemberCore GetDefinition (string name)
1243                 {
1244                         MemberCore mc = null;
1245                         defined_names.TryGetValue (name, out mc);
1246                         return mc;
1247                 }
1248         
1249                 // 
1250                 // root_types contains all the types.  All TopLevel types
1251                 // hence have a parent that points to `root_types', that is
1252                 // why there is a non-obvious test down here.
1253                 //
1254                 public bool IsTopLevel {
1255                         get { return (Parent != null && Parent.Parent == null); }
1256                 }
1257
1258                 public virtual bool IsUnmanagedType ()
1259                 {
1260                         return false;
1261                 }
1262
1263                 protected virtual TypeAttributes TypeAttr {
1264                         get { return Module.DefaultCharSetType; }
1265                 }
1266
1267                 /// <remarks>
1268                 ///  Should be overriten by the appropriate declaration space
1269                 /// </remarks>
1270                 public abstract TypeBuilder DefineType ();
1271
1272                 protected void Error_MissingPartialModifier (MemberCore type)
1273                 {
1274                         Report.Error (260, type.Location,
1275                                 "Missing partial modifier on declaration of type `{0}'. Another partial declaration of this type exists",
1276                                 type.GetSignatureForError ());
1277                 }
1278
1279                 public override string GetSignatureForError ()
1280                 {
1281                         return MemberName.GetSignatureForError ();
1282                 }
1283                 
1284                 public bool CheckAccessLevel (TypeSpec check_type)
1285                 {
1286                         TypeSpec tb = PartialContainer.Definition;
1287                         check_type = check_type.GetDefinition ();
1288
1289                         var check_attr = check_type.Modifiers & Modifiers.AccessibilityMask;
1290
1291                         switch (check_attr){
1292                         case Modifiers.PUBLIC:
1293                                 return true;
1294
1295                         case Modifiers.INTERNAL:
1296                                 return TypeManager.IsThisOrFriendAssembly (Assembly, check_type.Assembly);
1297                                 
1298                         case Modifiers.PRIVATE:
1299                                 TypeSpec declaring = check_type.DeclaringType;
1300                                 return tb == declaring.GetDefinition () || TypeManager.IsNestedChildOf (tb, declaring); 
1301
1302                         case Modifiers.PROTECTED:
1303                                 //
1304                                 // Only accessible to methods in current type or any subtypes
1305                                 //
1306                                 return TypeManager.IsNestedFamilyAccessible (tb, check_type.DeclaringType);
1307
1308                         case Modifiers.PROTECTED | Modifiers.INTERNAL:
1309                                 if (TypeManager.IsThisOrFriendAssembly (Assembly, check_type.Assembly))
1310                                         return true;
1311
1312                                 goto case Modifiers.PROTECTED;
1313                         }
1314
1315                         throw new NotImplementedException (check_attr.ToString ());
1316                 }
1317
1318                 private TypeSpec LookupNestedTypeInHierarchy (string name, int arity)
1319                 {
1320                         // TODO: GenericMethod only
1321                         if (PartialContainer == null)
1322                                 return null;
1323
1324                         // Has any nested type
1325                         // Does not work, because base type can have
1326                         //if (PartialContainer.Types == null)
1327                         //      return null;
1328
1329                         var container = PartialContainer.CurrentType;
1330
1331                         // Is not Root container
1332                         if (container == null)
1333                                 return null;
1334
1335                         var     t = MemberCache.FindNestedType (container, name, arity);
1336                         if (t == null)
1337                                 return null;
1338
1339                         // FIXME: Breaks error reporting
1340                         if (!CheckAccessLevel (t))
1341                                 return null;
1342
1343                         return t;
1344                 }
1345
1346                 //
1347                 // Public function used to locate types.
1348                 //
1349                 // Set 'ignore_cs0104' to true if you want to ignore cs0104 errors.
1350                 //
1351                 // Returns: Type or null if they type can not be found.
1352                 //
1353                 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, Location loc, bool ignore_cs0104)
1354                 {
1355                         FullNamedExpression e;
1356                         if (arity == 0 && Cache.TryGetValue (name, out e))
1357                                 return e;
1358
1359                         e = null;
1360                         int errors = Report.Errors;
1361
1362                         if (arity == 0) {
1363                                 TypeParameter[] tp = CurrentTypeParameters;
1364                                 if (tp != null) {
1365                                         TypeParameter tparam = TypeParameter.FindTypeParameter (tp, name);
1366                                         if (tparam != null)
1367                                                 e = new TypeParameterExpr (tparam, Location.Null);
1368                                 }
1369                         }
1370
1371                         if (e == null) {
1372                                 TypeSpec t = LookupNestedTypeInHierarchy (name, arity);
1373
1374                                 if (t != null)
1375                                         e = new TypeExpression (t, Location.Null);
1376                                 else if (Parent != null)
1377                                         e = Parent.LookupNamespaceOrType (name, arity, loc, ignore_cs0104);
1378                                 else
1379                                         e = NamespaceEntry.LookupNamespaceOrType (name, arity, loc, ignore_cs0104);
1380                         }
1381
1382                         // TODO MemberCache: How to cache arity stuff ?
1383                         if (errors == Report.Errors && arity == 0)
1384                                 Cache [name] = e;
1385                         
1386                         return e;
1387                 }
1388
1389                 public override Assembly Assembly {
1390                         get { return Module.Assembly; }
1391                 }
1392
1393                 public virtual ModuleContainer Module {
1394                         get { return Parent.Module; }
1395                 }
1396
1397                 TypeParameter[] initialize_type_params ()
1398                 {
1399                         if (type_param_list != null)
1400                                 return type_param_list;
1401
1402                         DeclSpace the_parent = Parent;
1403                         if (this is GenericMethod)
1404                                 the_parent = null;
1405
1406                         var list = new List<TypeParameter> ();
1407                         if (the_parent != null && the_parent.IsGeneric) {
1408                                 // FIXME: move generics info out of DeclSpace
1409                                 TypeParameter[] parent_params = the_parent.TypeParameters;
1410                                 list.AddRange (parent_params);
1411                         }
1412  
1413                         int count = type_params != null ? type_params.Length : 0;
1414                         for (int i = 0; i < count; i++) {
1415                                 TypeParameter param = type_params [i];
1416                                 list.Add (param);
1417                                 if (Parent.CurrentTypeParameters != null) {
1418                                         foreach (TypeParameter tp in Parent.CurrentTypeParameters) {
1419                                                 if (tp.Name != param.Name)                              
1420                                                         continue;
1421
1422                                                 Report.SymbolRelatedToPreviousError (tp.Location, null);
1423                                                 Report.Warning (693, 3, param.Location,
1424                                                         "Type parameter `{0}' has the same name as the type parameter from outer type `{1}'",
1425                                                         param.Name, Parent.GetSignatureForError ());
1426                                         }
1427                                 }
1428                         }
1429
1430                         type_param_list = new TypeParameter [list.Count];
1431                         list.CopyTo (type_param_list, 0);
1432                         return type_param_list;
1433                 }
1434
1435                 public virtual void SetParameterInfo (List<Constraints> constraints_list)
1436                 {
1437                         if (!is_generic) {
1438                                 if (constraints_list != null) {
1439                                         Report.Error (
1440                                                 80, Location, "Constraints are not allowed " +
1441                                                 "on non-generic declarations");
1442                                 }
1443
1444                                 return;
1445                         }
1446
1447                         TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
1448                         type_params = new TypeParameter [names.Length];
1449
1450                         //
1451                         // Register all the names
1452                         //
1453                         for (int i = 0; i < type_params.Length; i++) {
1454                                 TypeParameterName name = names [i];
1455
1456                                 Constraints constraints = null;
1457                                 if (constraints_list != null) {
1458                                         int total = constraints_list.Count;
1459                                         for (int ii = 0; ii < total; ++ii) {
1460                                                 Constraints constraints_at = (Constraints)constraints_list[ii];
1461                                                 // TODO: it is used by iterators only
1462                                                 if (constraints_at == null) {
1463                                                         constraints_list.RemoveAt (ii);
1464                                                         --total;
1465                                                         continue;
1466                                                 }
1467                                                 if (constraints_at.TypeParameter.Value == name.Name) {
1468                                                         constraints = constraints_at;
1469                                                         constraints_list.RemoveAt(ii);
1470                                                         break;
1471                                                 }
1472                                         }
1473                                 }
1474
1475                                 Variance variance = name.Variance;
1476                                 if (name.Variance != Variance.None && !(this is Delegate || this is Interface)) {
1477                                         Report.Error (1960, name.Location, "Variant type parameters can only be used with interfaces and delegates");
1478                                         variance = Variance.None;
1479                                 }
1480
1481                                 type_params [i] = new TypeParameter (
1482                                         Parent, i, new MemberName (name.Name, Location), constraints, name.OptAttributes, variance);
1483
1484                                 AddToContainer (type_params [i], name.Name);
1485                         }
1486
1487                         if (constraints_list != null && constraints_list.Count > 0) {
1488                                 foreach (Constraints constraint in constraints_list) {
1489                                         Report.Error(699, constraint.Location, "`{0}': A constraint references nonexistent type parameter `{1}'", 
1490                                                 GetSignatureForError (), constraint.TypeParameter.Value);
1491                                 }
1492                         }
1493                 }
1494
1495                 protected TypeParameter[] TypeParameters {
1496                         get {
1497                                 if (!IsGeneric)
1498                                         throw new InvalidOperationException ();
1499                                 if ((PartialContainer != null) && (PartialContainer != this))
1500                                         return PartialContainer.TypeParameters;
1501                                 if (type_param_list == null)
1502                                         initialize_type_params ();
1503
1504                                 return type_param_list;
1505                         }
1506                 }
1507
1508                 public int CountTypeParameters {
1509                         get {
1510                                 return count_type_params;
1511                         }
1512                 }
1513
1514                 public override string[] ValidAttributeTargets {
1515                         get { return attribute_targets; }
1516                 }
1517
1518                 protected override bool VerifyClsCompliance ()
1519                 {
1520                         if (!base.VerifyClsCompliance ()) {
1521                                 return false;
1522                         }
1523
1524                         if (type_params != null) {
1525                                 foreach (TypeParameter tp in type_params) {
1526                                         tp.VerifyClsCompliance ();
1527                                 }
1528                         }
1529
1530                         return true;
1531                 }
1532         }
1533 }