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