This should fix #76928. This fix incorporates ideas from a patch
[mono.git] / mcs / mcs / namespace.cs
1 //
2 // namespace.cs: Tracks namespaces
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 using System;
10 using System.Collections;
11 using System.Collections.Specialized;
12 using System.Reflection;
13
14 namespace Mono.CSharp {
15
16         public class RootNamespace : Namespace {
17                 static MethodInfo get_namespaces_method;
18
19                 string alias_name;
20                 Assembly referenced_assembly;
21
22                 Hashtable all_namespaces;
23
24                 static Hashtable root_namespaces;
25                 public static GlobalRootNamespace Global;
26                 
27                 static RootNamespace ()
28                 {
29                         get_namespaces_method = typeof (Assembly).GetMethod ("GetNamespaces", BindingFlags.Instance | BindingFlags.NonPublic);
30
31                         Reset ();
32                 }
33
34                 public static void Reset ()
35                 {
36                         root_namespaces = new Hashtable ();
37                         Global = new GlobalRootNamespace ();
38                         root_namespaces ["global"] = Global;
39                 }
40
41                 protected RootNamespace (string alias_name, Assembly assembly)
42                         : base (null, String.Empty)
43                 {
44                         this.alias_name = alias_name;
45                         referenced_assembly = assembly;
46
47                         all_namespaces = new Hashtable ();
48                         all_namespaces.Add ("", this);
49
50                         if (referenced_assembly != null)
51                                 ComputeNamespaces (this.referenced_assembly);
52                 }
53
54                 public static void DefineRootNamespace (string name, Assembly assembly)
55                 {
56                         if (name == "global") {
57                                 // FIXME: Add proper error number
58                                 Report.Error (-42, "Cannot define an external alias named `global'");
59                                 return;
60                         }
61                         RootNamespace retval = GetRootNamespace (name);
62                         if (retval == null || retval.referenced_assembly != assembly)
63                                 root_namespaces [name] = new RootNamespace (name, assembly);
64                 }
65
66                 public static RootNamespace GetRootNamespace (string name)
67                 {
68                         return (RootNamespace) root_namespaces [name];
69                 }
70
71                 public virtual Type LookupTypeReflection (string name, Location loc)
72                 {
73                         return GetTypeInAssembly (referenced_assembly, name);
74                 }
75
76                 public void RegisterNamespace (Namespace child)
77                 {
78                         if (child != this)
79                                 all_namespaces.Add (child.Name, child);
80                 }
81
82                 public bool IsNamespace (string name)
83                 {
84                         return all_namespaces.Contains (name);
85                 }
86
87                 protected void EnsureNamespace (string dotted_name)
88                 {
89                         if (dotted_name != null && dotted_name != "" && ! IsNamespace (dotted_name))
90                                 GetNamespace (dotted_name, true);
91                 }
92
93                 protected void ComputeNamespaces (Assembly assembly)
94                 {
95                         if (get_namespaces_method != null) {
96                                 string [] namespaces = (string []) get_namespaces_method.Invoke (assembly, null);
97                                 foreach (string ns in namespaces)
98                                         EnsureNamespace (ns);
99                                 return;
100                         }
101
102                         foreach (Type t in assembly.GetExportedTypes ())
103                                 EnsureNamespace (t.Namespace);
104                 }
105                 
106                 protected static Type GetTypeInAssembly (Assembly assembly, string name)
107                 {
108                         Type t = assembly.GetType (name);
109                         if (t == null)
110                                 return null;
111
112                         if (t.IsPointer)
113                                 throw new InternalErrorException ("Use GetPointerType() to get a pointer");
114                         
115                         TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
116                         if (ta == TypeAttributes.NotPublic ||
117                             ta == TypeAttributes.NestedPrivate ||
118                             ta == TypeAttributes.NestedAssembly ||
119                             ta == TypeAttributes.NestedFamANDAssem)
120                                 return null;
121
122                         return t;
123                 }
124
125                 public override string ToString ()
126                 {
127                         return String.Format ("RootNamespace ({0}::)", alias_name);
128                 }
129
130                 public override string GetSignatureForError ()
131                 {
132                         return alias_name + "::";
133                 }
134         }
135
136         public class GlobalRootNamespace : RootNamespace {
137                 Assembly [] assemblies;
138                 Module [] modules;
139
140                 public GlobalRootNamespace ()
141                         : base ("global", null)
142                 {
143                         assemblies = new Assembly [0];
144                 }
145
146                 public Assembly [] Assemblies {
147                         get { return assemblies; }
148                 }
149
150                 public Module [] Modules {
151                         get { return modules; }
152                 }
153
154                 public void AddAssemblyReference (Assembly a)
155                 {
156                         foreach (Assembly assembly in assemblies) {
157                                 if (a == assembly)
158                                         return;
159                         }
160
161                         int top = assemblies.Length;
162                         Assembly [] n = new Assembly [top + 1];
163                         assemblies.CopyTo (n, 0);
164                         n [top] = a;
165                         assemblies = n;
166
167                         ComputeNamespaces (a);
168                 }
169
170                 public void AddModuleReference (Module m)
171                 {
172                         int top = modules != null ? modules.Length : 0;
173                         Module [] n = new Module [top + 1];
174                         if (modules != null)
175                                 modules.CopyTo (n, 0);
176                         n [top] = m;
177                         modules = n;
178
179                         if (m == CodeGen.Module.Builder)
180                                 return;
181
182                         foreach (Type t in m.GetTypes ())
183                                 EnsureNamespace (t.Namespace);
184                 }
185
186                 public override Type LookupTypeReflection (string name, Location loc)
187                 {
188                         Type found_type = null;
189                 
190                         foreach (Assembly a in assemblies) {
191                                 Type t = GetTypeInAssembly (a, name);
192                                 if (t == null)
193                                         continue;
194                                         
195                                 if (found_type == null) {
196                                         found_type = t;
197                                         continue;
198                                 }
199
200                                 Report.SymbolRelatedToPreviousError (found_type);
201                                 Report.SymbolRelatedToPreviousError (t);
202                                 Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
203                                         
204                                 return found_type;
205                         }
206
207                         if (modules != null) {
208                                 foreach (Module module in modules) {
209                                         Type t = module.GetType (name);
210                                         if (t == null)
211                                                 continue;
212
213                                         if (found_type == null) {
214                                                 found_type = t;
215                                                 continue;
216                                         }
217                                         
218                                         Report.SymbolRelatedToPreviousError (t);
219                                         Report.SymbolRelatedToPreviousError (found_type);
220                                         Report.Warning (436, 2, loc, "Ignoring imported type `{0}' since the current assembly already has a declaration with the same name",
221                                                 TypeManager.CSharpName (t));
222                                         return t;
223                                 }
224                         }
225
226                         return found_type;
227                 }
228         }
229
230         /// <summary>
231         ///   Keeps track of the namespaces defined in the C# code.
232         ///
233         ///   This is an Expression to allow it to be referenced in the
234         ///   compiler parse/intermediate tree during name resolution.
235         /// </summary>
236         public class Namespace : FullNamedExpression {
237                 
238                 Namespace parent;
239                 string fullname;
240                 Hashtable namespaces;
241                 IDictionary declspaces;
242                 Hashtable cached_types;
243                 RootNamespace root;
244
245                 public readonly MemberName MemberName;
246
247                 /// <summary>
248                 ///   Constructor Takes the current namespace and the
249                 ///   name.  This is bootstrapped with parent == null
250                 ///   and name = ""
251                 /// </summary>
252                 public Namespace (Namespace parent, string name)
253                 {
254                         // Expression members.
255                         this.eclass = ExprClass.Namespace;
256                         this.Type = null;
257                         this.loc = Location.Null;
258
259                         this.parent = parent;
260
261                         if (parent != null)
262                                 this.root = parent.root;
263                         else
264                                 this.root = this as RootNamespace;
265
266                         if (this.root == null)
267                                 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
268                         
269                         string pname = parent != null ? parent.Name : "";
270                                 
271                         if (pname == "")
272                                 fullname = name;
273                         else
274                                 fullname = parent.Name + "." + name;
275
276                         if (fullname == null)
277                                 throw new InternalErrorException ("Namespace has a null fullname");
278
279                         if (parent != null && parent.MemberName != MemberName.Null)
280                                 MemberName = new MemberName (parent.MemberName, name);
281                         else if (name == "")
282                                 MemberName = MemberName.Null;
283                         else
284                                 MemberName = new MemberName (name);
285
286                         namespaces = new Hashtable ();
287                         cached_types = new Hashtable ();
288
289                         root.RegisterNamespace (this);
290                 }
291
292                 public override Expression DoResolve (EmitContext ec)
293                 {
294                         return this;
295                 }
296
297                 public override void Emit (EmitContext ec)
298                 {
299                         throw new InternalErrorException ("Expression tree referenced namespace " + fullname + " during Emit ()");
300                 }
301
302                 public override string GetSignatureForError ()
303                 {
304                         return Name;
305                 }
306                 
307                 public Namespace GetNamespace (string name, bool create)
308                 {
309                         int pos = name.IndexOf ('.');
310
311                         Namespace ns;
312                         string first;
313                         if (pos >= 0)
314                                 first = name.Substring (0, pos);
315                         else
316                                 first = name;
317
318                         ns = (Namespace) namespaces [first];
319                         if (ns == null) {
320                                 if (!create)
321                                         return null;
322
323                                 ns = new Namespace (this, first);
324                                 namespaces.Add (first, ns);
325                         }
326
327                         if (pos >= 0)
328                                 ns = ns.GetNamespace (name.Substring (pos + 1), create);
329
330                         return ns;
331                 }
332
333                 TypeExpr LookupType (string name, Location loc)
334                 {
335                         if (cached_types.Contains (name))
336                                 return cached_types [name] as TypeExpr;
337
338                         Type t = null;
339                         if (declspaces != null) {
340                                 DeclSpace tdecl = declspaces [name] as DeclSpace;
341                                 if (tdecl != null) {
342                                         //
343                                         // Note that this is not:
344                                         //
345                                         //   t = tdecl.DefineType ()
346                                         //
347                                         // This is to make it somewhat more useful when a DefineType
348                                         // fails due to problems in nested types (more useful in the sense
349                                         // of fewer misleading error messages)
350                                         //
351                                         tdecl.DefineType ();
352                                         t = tdecl.TypeBuilder;
353                                 }
354                         }
355                         string lookup = t != null ? t.FullName : (fullname == "" ? name : fullname + "." + name);
356                         Type rt = root.LookupTypeReflection (lookup, loc);
357                         if (t == null)
358                                 t = rt;
359
360                         TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
361                         cached_types [name] = te;
362                         return te;
363                 }
364
365                 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
366                 {
367                         if (namespaces.Contains (name))
368                                 return (Namespace) namespaces [name];
369
370                         TypeExpr te = LookupType (name, loc);
371                         if (te == null || !ds.CheckAccessLevel (te.Type))
372                                 return null;
373
374                         return te;
375                 }
376
377                 public void AddDeclSpace (string name, DeclSpace ds)
378                 {
379                         if (declspaces == null)
380                                 declspaces = new HybridDictionary ();
381                         declspaces.Add (name, ds);
382                 }
383
384                 /// <summary>
385                 ///   The qualified name of the current namespace
386                 /// </summary>
387                 public string Name {
388                         get { return fullname; }
389                 }
390
391                 public override string FullName {
392                         get { return fullname; }
393                 }
394
395                 /// <summary>
396                 ///   The parent of this namespace, used by the parser to "Pop"
397                 ///   the current namespace declaration
398                 /// </summary>
399                 public Namespace Parent {
400                         get { return parent; }
401                 }
402
403                 public override string ToString ()
404                 {
405                         return String.Format ("Namespace ({0})", Name);
406                 }
407         }
408
409         public class NamespaceEntry {
410                 Namespace ns;
411                 NamespaceEntry parent, implicit_parent;
412                 SourceFile file;
413                 int symfile_id;
414                 Hashtable aliases;
415                 ArrayList using_clauses;
416                 public bool DeclarationFound = false;
417                 bool UsingFound;
418
419                 ListDictionary extern_aliases;
420
421                 static ArrayList entries = new ArrayList ();
422
423                 public static void Reset ()
424                 {
425                         entries = new ArrayList ();
426                 }
427
428                 //
429                 // This class holds the location where a using definition is
430                 // done, and whether it has been used by the program or not.
431                 //
432                 // We use this to flag using clauses for namespaces that do not
433                 // exist.
434                 //
435                 public class UsingEntry {
436                         public readonly MemberName Name;
437                         readonly Expression Expr;
438                         readonly NamespaceEntry NamespaceEntry;
439                         readonly Location Location;
440                         
441                         public UsingEntry (NamespaceEntry entry, MemberName name, Location loc)
442                         {
443                                 Name = name;
444                                 Expr = name.GetTypeExpression ();
445                                 NamespaceEntry = entry;
446                                 Location = loc;
447                         }
448
449                         internal Namespace resolved;
450
451                         public Namespace Resolve ()
452                         {
453                                 if (resolved != null)
454                                         return resolved;
455
456                                 DeclSpace root = RootContext.Tree.Types;
457                                 root.NamespaceEntry = NamespaceEntry;
458                                 FullNamedExpression fne = Expr.ResolveAsTypeStep (root.EmitContext, false);
459                                 root.NamespaceEntry = null;
460
461                                 if (fne == null) {
462                                         Error_NamespaceNotFound (Location, Name.ToString ());
463                                         return null;
464                                 }
465
466                                 resolved = fne as Namespace;
467                                 if (resolved == null) {
468                                         Report.Error (138, Location,
469                                                 "`{0} is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.ToString ());
470                                 }
471                                 return resolved;
472                         }
473                 }
474
475                 public abstract class AliasEntry {
476                         public readonly string Name;
477                         public readonly NamespaceEntry NamespaceEntry;
478                         public readonly Location Location;
479                         
480                         protected AliasEntry (NamespaceEntry entry, string name, Location loc)
481                         {
482                                 Name = name;
483                                 NamespaceEntry = entry;
484                                 Location = loc;
485                         }
486                         
487                         protected FullNamedExpression resolved;
488                         bool error;
489
490                         public FullNamedExpression Resolve ()
491                         {
492                                 if (resolved != null || error)
493                                         return resolved;
494                                 resolved = DoResolve ();
495                                 if (resolved == null)
496                                         error = true;
497                                 return resolved;
498                         }
499
500                         protected abstract FullNamedExpression DoResolve ();
501                 }
502
503                 public class LocalAliasEntry : AliasEntry
504                 {
505                         public readonly Expression Alias;
506                         
507                         public LocalAliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc) :
508                                 base (entry, name, loc)
509                         {
510                                 Alias = alias.GetTypeExpression ();
511                         }
512
513                         protected override FullNamedExpression DoResolve ()
514                         {
515                                 DeclSpace root = RootContext.Tree.Types;
516                                 root.NamespaceEntry = NamespaceEntry;
517                                 resolved = Alias.ResolveAsTypeStep (root.EmitContext, false);
518                                 root.NamespaceEntry = null;
519
520                                 if (resolved == null)
521                                         Error_NamespaceNotFound (Location, Alias.ToString ());
522                                 return resolved;
523                         }
524                 }
525
526                 public class ExternAliasEntry : AliasEntry 
527                 {
528                         public ExternAliasEntry (NamespaceEntry entry, string name, Location loc) :
529                                 base (entry, name, loc)
530                         {
531                         }
532
533                         protected override FullNamedExpression DoResolve ()
534                         {
535                                 resolved = RootNamespace.GetRootNamespace (Name);
536                                 if (resolved == null)
537                                         Report.Error (430, Location, "The extern alias '" + Name +
538                                                                         "' was not specified in a /reference option");
539
540                                 return resolved;
541                         }
542                 }
543
544                 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
545                 {
546                         this.parent = parent;
547                         this.file = file;
548                         entries.Add (this);
549                         this.ID = entries.Count;
550
551                         if (parent != null)
552                                 ns = parent.NS.GetNamespace (name, true);
553                         else if (name != null)
554                                 ns = RootNamespace.Global.GetNamespace (name, true);
555                         else
556                                 ns = RootNamespace.Global;
557                 }
558
559                 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
560                 {
561                         this.parent = parent;
562                         this.file = file;
563                         // no need to add self to 'entries', since we don't have any aliases or using entries.
564                         this.ID = -1;
565                         this.IsImplicit = true;
566                         this.ns = ns;
567                 }
568
569                 //
570                 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
571                 // resolved as if the immediately containing namespace body has no using-directives.
572                 //
573                 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
574                 // in the using-namespace-directive.
575                 //
576                 // To implement these rules, the expressions in the using directives are resolved using 
577                 // the "doppelganger" (ghostly bodiless duplicate).
578                 //
579                 NamespaceEntry doppelganger;
580                 NamespaceEntry Doppelganger {
581                         get {
582                                 if (!IsImplicit && doppelganger == null)
583                                         doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
584                                 return doppelganger;
585                         }
586                 }
587
588                 public readonly int ID;
589                 public readonly bool IsImplicit;
590
591                 public Namespace NS {
592                         get { return ns; }
593                 }
594
595                 public NamespaceEntry Parent {
596                         get { return parent; }
597                 }
598
599                 public NamespaceEntry ImplicitParent {
600                         get {
601                                 if (parent == null)
602                                         return null;
603                                 if (implicit_parent == null) {
604                                         implicit_parent = (parent.NS == ns.Parent)
605                                                 ? parent
606                                                 : new NamespaceEntry (parent, file, ns.Parent);
607                                 }
608                                 return implicit_parent;
609                         }
610                 }
611
612                 /// <summary>
613                 ///   Records a new namespace for resolving name references
614                 /// </summary>
615                 public void Using (MemberName name, Location loc)
616                 {
617                         if (DeclarationFound){
618                                 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
619                                 return;
620                         }
621
622                         UsingFound = true;
623
624                         if (name.Equals (ns.MemberName))
625                                 return;
626                         
627                         if (using_clauses == null)
628                                 using_clauses = new ArrayList ();
629
630                         foreach (UsingEntry old_entry in using_clauses) {
631                                 if (name.Equals (old_entry.Name)) {
632                                         Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetName ());
633                                         return;
634                                 }
635                         }
636
637                         UsingEntry ue = new UsingEntry (Doppelganger, name, loc);
638                         using_clauses.Add (ue);
639                 }
640
641                 public void UsingAlias (string name, MemberName alias, Location loc)
642                 {
643                         if (DeclarationFound){
644                                 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
645                                 return;
646                         }
647
648                         UsingFound = true;
649
650                         if (aliases == null)
651                                 aliases = new Hashtable ();
652
653                         if (aliases.Contains (name)) {
654                                 AliasEntry ae = (AliasEntry)aliases [name];
655                                 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
656                                 Report.Error (1537, loc, "The using alias `" + name +
657                                               "' appeared previously in this namespace");
658                                 return;
659                         }
660
661                         if (RootContext.Version == LanguageVersion.Default &&
662                             name == "global" && RootContext.WarningLevel >= 2)
663                                 Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
664                                         " the global namespace will be used instead");
665
666                         aliases [name] = new LocalAliasEntry (Doppelganger, name, alias, loc);
667                 }
668
669                 public void UsingExternalAlias (string name, Location loc)
670                 {
671                         if (UsingFound || DeclarationFound) {
672                                 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
673                                 return;
674                         }
675                         
676                         if (aliases == null)
677                                 aliases = new Hashtable ();
678                         
679                         // Share the extern_aliases field with the Doppelganger
680                         if (extern_aliases == null) {
681                                 extern_aliases = new ListDictionary ();
682                                 Doppelganger.extern_aliases = extern_aliases;
683                         }
684                         
685                         if (aliases.Contains (name)) {
686                                 AliasEntry ae = (AliasEntry) aliases [name];
687                                 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
688                                 Report.Error (1537, loc, "The using alias `" + name +
689                                               "' appeared previously in this namespace");
690                                 return;
691                         }
692
693                         if (name == "global") {
694                                 Report.Error (1681, loc, "You cannot redefine the global extern alias");
695                                 return;
696                         }
697
698                         // Register the alias in aliases and extern_aliases, since we need both of them
699                         // to keep things simple (different resolution scenarios)
700                         ExternAliasEntry alias = new ExternAliasEntry (Doppelganger, name, loc);
701                         aliases [name] = alias;
702                         extern_aliases [name] = alias;
703                 }
704
705                 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
706                 {
707                         // Precondition: Only simple names (no dots) will be looked up with this function.
708                         FullNamedExpression resolved = null;
709                         for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
710                                 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
711                                         break;
712                         }
713                         return resolved;
714                 }
715
716                 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
717                 {
718                         Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
719                                 name, t1.FullName, t2.FullName);
720                 }
721
722                 // Looks-up a alias named @name in this and surrounding namespace declarations
723                 public FullNamedExpression LookupAlias (string name)
724                 {
725                         AliasEntry entry = null;
726                         // We use Parent rather than ImplicitParent since we know implicit namespace declarations
727                         // cannot have using entries.
728                         for (NamespaceEntry n = this; n != null; n = n.Parent) {
729                                 if (n.aliases == null)
730                                         continue;
731                                 entry = n.aliases [name] as AliasEntry;
732                                 if (entry != null)
733                                         return entry.Resolve ();
734                         }
735                         return null;
736                 }
737
738                 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
739                 {
740                         //
741                         // Check whether it's in the namespace.
742                         //
743                         FullNamedExpression fne = NS.Lookup (ds, name, loc);
744                         if (fne != null)
745                                 return fne;
746
747                         if (extern_aliases != null) {
748                                 AliasEntry entry = extern_aliases [name] as AliasEntry;
749                                 if (entry != null)
750                                         return entry.Resolve ();
751                         }
752                         
753                         if (IsImplicit)
754                                 return null;
755                         
756                         //
757                         // Check aliases. 
758                         //
759                         if (aliases != null) {
760                                 AliasEntry entry = aliases [name] as AliasEntry;
761                                 if (entry != null)
762                                         return entry.Resolve ();
763                         }
764
765                         //
766                         // Check using entries.
767                         //
768                         FullNamedExpression match = null;
769                         foreach (Namespace using_ns in GetUsingTable ()) {
770                                 match = using_ns.Lookup (ds, name, loc);
771                                 if (match == null || !(match is TypeExpr))
772                                         continue;
773                                 if (fne != null) {
774                                         if (!ignore_cs0104)
775                                                 Error_AmbiguousTypeReference (loc, name, fne, match);
776                                         return null;
777                                 }
778                                 fne = match;
779                         }
780
781                         return fne;
782                 }
783
784                 // Our cached computation.
785                 readonly Namespace [] empty_namespaces = new Namespace [0];
786                 Namespace [] namespace_using_table;
787                 Namespace [] GetUsingTable ()
788                 {
789                         if (namespace_using_table != null)
790                                 return namespace_using_table;
791
792                         if (using_clauses == null) {
793                                 namespace_using_table = empty_namespaces;
794                                 return namespace_using_table;
795                         }
796
797                         ArrayList list = new ArrayList (using_clauses.Count);
798
799                         foreach (UsingEntry ue in using_clauses) {
800                                 Namespace using_ns = ue.Resolve ();
801                                 if (using_ns == null)
802                                         continue;
803
804                                 list.Add (using_ns);
805                         }
806
807                         namespace_using_table = new Namespace [list.Count];
808                         list.CopyTo (namespace_using_table, 0);
809                         return namespace_using_table;
810                 }
811
812                 readonly string [] empty_using_list = new string [0];
813
814                 public int SymbolFileID {
815                         get {
816                                 if (symfile_id == 0 && file.SourceFileEntry != null) {
817                                         int parent_id = parent == null ? 0 : parent.SymbolFileID;
818
819                                         string [] using_list = empty_using_list;
820                                         if (using_clauses != null) {
821                                                 using_list = new string [using_clauses.Count];
822                                                 for (int i = 0; i < using_clauses.Count; i++)
823                                                         using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
824                                         }
825
826                                         symfile_id = CodeGen.SymbolWriter.DefineNamespace (ns.Name, file.SourceFileEntry, using_list, parent_id);
827                                 }
828                                 return symfile_id;
829                         }
830                 }
831
832                 static void MsgtryRef (string s)
833                 {
834                         Console.WriteLine ("    Try using -r:" + s);
835                 }
836
837                 static void MsgtryPkg (string s)
838                 {
839                         Console.WriteLine ("    Try using -pkg:" + s);
840                 }
841
842                 public static void Error_NamespaceNotFound (Location loc, string name)
843                 {
844                         Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
845                                 name);
846
847                         switch (name) {
848                         case "Gtk": case "GtkSharp":
849                                 MsgtryPkg ("gtk-sharp");
850                                 break;
851
852                         case "Gdk": case "GdkSharp":
853                                 MsgtryPkg ("gdk-sharp");
854                                 break;
855
856                         case "Glade": case "GladeSharp":
857                                 MsgtryPkg ("glade-sharp");
858                                 break;
859
860                         case "System.Drawing":
861                         case "System.Web.Services":
862                         case "System.Web":
863                         case "System.Data":
864                         case "System.Windows.Forms":
865                                 MsgtryRef (name);
866                                 break;
867                         }
868                 }
869
870                 /// <summary>
871                 ///   Used to validate that all the using clauses are correct
872                 ///   after we are finished parsing all the files.  
873                 /// </summary>
874                 void VerifyUsing ()
875                 {
876                         if (using_clauses != null) {
877                                 foreach (UsingEntry ue in using_clauses)
878                                         ue.Resolve ();
879                         }
880
881                         if (aliases != null) {
882                                 foreach (DictionaryEntry de in aliases)
883                                         ((AliasEntry) de.Value).Resolve ();
884                         }
885                 }
886
887                 /// <summary>
888                 ///   Used to validate that all the using clauses are correct
889                 ///   after we are finished parsing all the files.  
890                 /// </summary>
891                 static public void VerifyAllUsing ()
892                 {
893                         foreach (NamespaceEntry entry in entries)
894                                 entry.VerifyUsing ();
895                 }
896
897                 public string GetSignatureForError ()
898                 {
899                         return ns.GetSignatureForError ();
900                 }
901
902                 public override string ToString ()
903                 {
904                         return ns.ToString ();
905                 }
906         }
907 }