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