TARGET_JVM: add browscap.ini to System.Web
[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                 public readonly DeclSpace SlaveDeclSpace;
433
434                 ListDictionary extern_aliases;
435
436                 static ArrayList entries = new ArrayList ();
437
438                 public static void Reset ()
439                 {
440                         entries = new ArrayList ();
441                 }
442
443                 //
444                 // This class holds the location where a using definition is
445                 // done, and whether it has been used by the program or not.
446                 //
447                 // We use this to flag using clauses for namespaces that do not
448                 // exist.
449                 //
450                 public class UsingEntry : IResolveContext {
451                         public readonly MemberName Name;
452                         readonly Expression Expr;
453                         readonly NamespaceEntry NamespaceEntry;
454                         readonly Location Location;
455                         
456                         public UsingEntry (NamespaceEntry entry, MemberName name, Location loc)
457                         {
458                                 Name = name;
459                                 Expr = name.GetTypeExpression ();
460                                 NamespaceEntry = entry;
461                                 Location = loc;
462                         }
463
464                         internal Namespace resolved;
465
466                         public Namespace Resolve ()
467                         {
468                                 if (resolved != null)
469                                         return resolved;
470
471                                 FullNamedExpression fne = Expr.ResolveAsTypeStep (this, false);
472                                 if (fne == null) {
473                                         Error_NamespaceNotFound (Location, Name.ToString ());
474                                         return null;
475                                 }
476
477                                 resolved = fne as Namespace;
478                                 if (resolved == null) {
479                                         Report.Error (138, Location,
480                                                 "`{0} is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.ToString ());
481                                 }
482                                 return resolved;
483                         }
484
485                         DeclSpace IResolveContext.DeclContainer {
486                                 get { return NamespaceEntry.SlaveDeclSpace; }
487                         }
488                         bool IResolveContext.IsInObsoleteScope {
489                                 get { return false; }
490                         }
491                         bool IResolveContext.IsInUnsafeScope {
492                                 get { return false; }
493                         }
494                 }
495
496                 public abstract class AliasEntry {
497                         public readonly string Name;
498                         public readonly NamespaceEntry NamespaceEntry;
499                         public readonly Location Location;
500                         
501                         protected AliasEntry (NamespaceEntry entry, string name, Location loc)
502                         {
503                                 Name = name;
504                                 NamespaceEntry = entry;
505                                 Location = loc;
506                         }
507                         
508                         protected FullNamedExpression resolved;
509                         bool error;
510
511                         public FullNamedExpression Resolve ()
512                         {
513                                 if (resolved != null || error)
514                                         return resolved;
515                                 resolved = DoResolve ();
516                                 if (resolved == null)
517                                         error = true;
518                                 return resolved;
519                         }
520
521                         protected abstract FullNamedExpression DoResolve ();
522                 }
523
524                 public class LocalAliasEntry : AliasEntry, IResolveContext {
525                         public readonly Expression Alias;
526                         
527                         public LocalAliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc) :
528                                 base (entry, name, loc)
529                         {
530                                 Alias = alias.GetTypeExpression ();
531                         }
532
533                         protected override FullNamedExpression DoResolve ()
534                         {
535                                 resolved = Alias.ResolveAsTypeStep (this, false);
536                                 if (resolved == null)
537                                         return null;
538
539                                 if (resolved.Type != null) {
540                                         TypeAttributes attr = resolved.Type.Attributes & TypeAttributes.VisibilityMask;
541                                         if (attr == TypeAttributes.NestedPrivate || attr == TypeAttributes.NestedFamily ||
542                                                 ((attr == TypeAttributes.NestedFamORAssem || attr == TypeAttributes.NestedAssembly) && 
543                                                 TypeManager.LookupDeclSpace (resolved.Type) == null)) {
544                                                 Expression.ErrorIsInaccesible (Alias.Location, Alias.ToString ());
545                                                 return null;
546                                         }
547                                 }
548
549                                 return resolved;
550                         }
551
552                         DeclSpace IResolveContext.DeclContainer {
553                                 get { return NamespaceEntry.SlaveDeclSpace; }
554                         }
555                         bool IResolveContext.IsInObsoleteScope {
556                                 get { return false; }
557                         }
558                         bool IResolveContext.IsInUnsafeScope {
559                                 get { return false; }
560                         }
561                 }
562
563                 public class ExternAliasEntry : AliasEntry {
564                         public ExternAliasEntry (NamespaceEntry entry, string name, Location loc) :
565                                 base (entry, name, loc)
566                         {
567                         }
568
569                         protected override FullNamedExpression DoResolve ()
570                         {
571                                 resolved = RootNamespace.GetRootNamespace (Name);
572                                 if (resolved == null)
573                                         Report.Error (430, Location, "The extern alias '" + Name +
574                                                                         "' was not specified in a /reference option");
575
576                                 return resolved;
577                         }
578                 }
579
580                 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name)
581                 {
582                         this.parent = parent;
583                         this.file = file;
584                         entries.Add (this);
585                         this.ID = entries.Count;
586
587                         if (parent != null)
588                                 ns = parent.NS.GetNamespace (name, true);
589                         else if (name != null)
590                                 ns = RootNamespace.Global.GetNamespace (name, true);
591                         else
592                                 ns = RootNamespace.Global;
593                         SlaveDeclSpace = new RootDeclSpace (this);
594                 }
595
596                 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns, bool slave)
597                 {
598                         this.parent = parent;
599                         this.file = file;
600                         // no need to add self to 'entries', since we don't have any aliases or using entries.
601                         this.ID = -1;
602                         this.IsImplicit = true;
603                         this.ns = ns;
604                         this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
605                 }
606
607                 //
608                 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
609                 // resolved as if the immediately containing namespace body has no using-directives.
610                 //
611                 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
612                 // in the using-namespace-directive.
613                 //
614                 // To implement these rules, the expressions in the using directives are resolved using 
615                 // the "doppelganger" (ghostly bodiless duplicate).
616                 //
617                 NamespaceEntry doppelganger;
618                 NamespaceEntry Doppelganger {
619                         get {
620                                 if (!IsImplicit && doppelganger == null)
621                                         doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
622                                 return doppelganger;
623                         }
624                 }
625
626                 public readonly int ID;
627                 public readonly bool IsImplicit;
628
629                 public Namespace NS {
630                         get { return ns; }
631                 }
632
633                 public NamespaceEntry Parent {
634                         get { return parent; }
635                 }
636
637                 public NamespaceEntry ImplicitParent {
638                         get {
639                                 if (parent == null)
640                                         return null;
641                                 if (implicit_parent == null) {
642                                         implicit_parent = (parent.NS == ns.Parent)
643                                                 ? parent
644                                                 : new NamespaceEntry (parent, file, ns.Parent, false);
645                                 }
646                                 return implicit_parent;
647                         }
648                 }
649
650                 /// <summary>
651                 ///   Records a new namespace for resolving name references
652                 /// </summary>
653                 public void Using (MemberName name, Location loc)
654                 {
655                         if (DeclarationFound){
656                                 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
657                                 return;
658                         }
659
660                         UsingFound = true;
661
662                         if (name.Equals (ns.MemberName))
663                                 return;
664                         
665                         if (using_clauses == null)
666                                 using_clauses = new ArrayList ();
667
668                         foreach (UsingEntry old_entry in using_clauses) {
669                                 if (name.Equals (old_entry.Name)) {
670                                         Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetName ());
671                                         return;
672                                 }
673                         }
674
675                         UsingEntry ue = new UsingEntry (Doppelganger, name, loc);
676                         using_clauses.Add (ue);
677                 }
678
679                 public void UsingAlias (string name, MemberName alias, Location loc)
680                 {
681                         if (DeclarationFound){
682                                 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
683                                 return;
684                         }
685
686                         UsingFound = true;
687
688                         if (aliases == null)
689                                 aliases = new Hashtable ();
690
691                         if (aliases.Contains (name)) {
692                                 AliasEntry ae = (AliasEntry) aliases [name];
693                                 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
694                                 Report.Error (1537, loc, "The using alias `{0}' appeared previously in this namespace", name);
695                                 return;
696                         }
697
698                         if (RootContext.Version == LanguageVersion.Default &&
699                             name == "global" && RootContext.WarningLevel >= 2)
700                                 Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
701                                         " the global namespace will be used instead");
702
703                         // FIXME: get correct error number.  See if the above check can be merged
704                         if (extern_aliases != null && extern_aliases.Contains (name)) {
705                                 AliasEntry ae = (AliasEntry) extern_aliases [name];
706                                 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
707                                 Report.Error (1537, loc, "The using alias `{0}' appeared previously in this namespace", name);
708                                 return;
709                         }
710
711                         aliases [name] = new LocalAliasEntry (Doppelganger, name, alias, loc);
712                 }
713
714                 public void UsingExternalAlias (string name, Location loc)
715                 {
716                         if (UsingFound || DeclarationFound) {
717                                 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
718                                 return;
719                         }
720                         
721                         // Share the extern_aliases field with the Doppelganger
722                         if (extern_aliases == null) {
723                                 extern_aliases = new ListDictionary ();
724                                 Doppelganger.extern_aliases = extern_aliases;
725                         }
726
727                         if (extern_aliases.Contains (name)) {
728                                 AliasEntry ae = (AliasEntry) extern_aliases [name];
729                                 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
730                                 Report.Error (1537, loc, "The using alias `{0}' appeared previously in this namespace", name);
731                                 return;
732                         }
733
734                         if (name == "global") {
735                                 Report.Error (1681, loc, "You cannot redefine the global extern alias");
736                                 return;
737                         }
738
739                         // Register the alias in aliases and extern_aliases, since we need both of them
740                         // to keep things simple (different resolution scenarios)
741                         ExternAliasEntry alias = new ExternAliasEntry (Doppelganger, name, loc);
742                         extern_aliases [name] = alias;
743                 }
744
745                 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
746                 {
747                         // Precondition: Only simple names (no dots) will be looked up with this function.
748                         FullNamedExpression resolved = null;
749                         for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
750                                 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
751                                         break;
752                         }
753                         return resolved;
754                 }
755
756                 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
757                 {
758                         Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
759                                 name, t1.FullName, t2.FullName);
760                 }
761
762                 // Looks-up a alias named @name in this and surrounding namespace declarations
763                 public FullNamedExpression LookupAlias (string name)
764                 {
765                         AliasEntry entry = null;
766                         for (NamespaceEntry n = this; n != null; n = n.ImplicitParent) {
767                                 if (n.extern_aliases != null && (entry = n.extern_aliases [name] as AliasEntry) != null)
768                                         break;
769                                 if (n.aliases != null && (entry = n.aliases [name] as AliasEntry) != null)
770                                         break;
771                         }
772                         return entry == null ? null : entry.Resolve ();
773                 }
774
775                 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
776                 {
777                         //
778                         // Check whether it's in the namespace.
779                         //
780                         FullNamedExpression fne = NS.Lookup (ds, name, loc);
781                         if (fne != null)
782                                 return fne;
783
784                         if (extern_aliases != null) {
785                                 AliasEntry entry = extern_aliases [name] as AliasEntry;
786                                 if (entry != null)
787                                         return entry.Resolve ();
788                         }
789                         
790                         if (IsImplicit)
791                                 return null;
792                         
793                         //
794                         // Check aliases. 
795                         //
796                         if (aliases != null) {
797                                 AliasEntry entry = aliases [name] as AliasEntry;
798                                 if (entry != null)
799                                         return entry.Resolve ();
800                         }
801
802                         //
803                         // Check using entries.
804                         //
805                         FullNamedExpression match = null;
806                         foreach (Namespace using_ns in GetUsingTable ()) {
807                                 match = using_ns.Lookup (ds, name, loc);
808                                 if (match == null || !(match is TypeExpr))
809                                         continue;
810                                 if (fne != null) {
811                                         if (!ignore_cs0104)
812                                                 Error_AmbiguousTypeReference (loc, name, fne, match);
813                                         return null;
814                                 }
815                                 fne = match;
816                         }
817
818                         return fne;
819                 }
820
821                 // Our cached computation.
822                 readonly Namespace [] empty_namespaces = new Namespace [0];
823                 Namespace [] namespace_using_table;
824                 Namespace [] GetUsingTable ()
825                 {
826                         if (namespace_using_table != null)
827                                 return namespace_using_table;
828
829                         if (using_clauses == null) {
830                                 namespace_using_table = empty_namespaces;
831                                 return namespace_using_table;
832                         }
833
834                         ArrayList list = new ArrayList (using_clauses.Count);
835
836                         foreach (UsingEntry ue in using_clauses) {
837                                 Namespace using_ns = ue.Resolve ();
838                                 if (using_ns == null)
839                                         continue;
840
841                                 list.Add (using_ns);
842                         }
843
844                         namespace_using_table = new Namespace [list.Count];
845                         list.CopyTo (namespace_using_table, 0);
846                         return namespace_using_table;
847                 }
848
849                 readonly string [] empty_using_list = new string [0];
850
851                 public int SymbolFileID {
852                         get {
853                                 if (symfile_id == 0 && file.SourceFileEntry != null) {
854                                         int parent_id = parent == null ? 0 : parent.SymbolFileID;
855
856                                         string [] using_list = empty_using_list;
857                                         if (using_clauses != null) {
858                                                 using_list = new string [using_clauses.Count];
859                                                 for (int i = 0; i < using_clauses.Count; i++)
860                                                         using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
861                                         }
862
863                                         symfile_id = CodeGen.SymbolWriter.DefineNamespace (ns.Name, file.SourceFileEntry, using_list, parent_id);
864                                 }
865                                 return symfile_id;
866                         }
867                 }
868
869                 static void MsgtryRef (string s)
870                 {
871                         Console.WriteLine ("    Try using -r:" + s);
872                 }
873
874                 static void MsgtryPkg (string s)
875                 {
876                         Console.WriteLine ("    Try using -pkg:" + s);
877                 }
878
879                 public static void Error_NamespaceNotFound (Location loc, string name)
880                 {
881                         Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
882                                 name);
883
884                         switch (name) {
885                         case "Gtk": case "GtkSharp":
886                                 MsgtryPkg ("gtk-sharp");
887                                 break;
888
889                         case "Gdk": case "GdkSharp":
890                                 MsgtryPkg ("gdk-sharp");
891                                 break;
892
893                         case "Glade": case "GladeSharp":
894                                 MsgtryPkg ("glade-sharp");
895                                 break;
896
897                         case "System.Drawing":
898                         case "System.Web.Services":
899                         case "System.Web":
900                         case "System.Data":
901                         case "System.Windows.Forms":
902                                 MsgtryRef (name);
903                                 break;
904                         }
905                 }
906
907                 /// <summary>
908                 ///   Used to validate that all the using clauses are correct
909                 ///   after we are finished parsing all the files.  
910                 /// </summary>
911                 void VerifyUsing ()
912                 {
913                         if (extern_aliases != null) {
914                                 foreach (DictionaryEntry de in extern_aliases)
915                                         ((AliasEntry) de.Value).Resolve ();
916                         }               
917
918                         if (using_clauses != null) {
919                                 foreach (UsingEntry ue in using_clauses)
920                                         ue.Resolve ();
921                         }
922
923                         if (aliases != null) {
924                                 foreach (DictionaryEntry de in aliases)
925                                         ((AliasEntry) de.Value).Resolve ();
926                         }
927                 }
928
929                 /// <summary>
930                 ///   Used to validate that all the using clauses are correct
931                 ///   after we are finished parsing all the files.  
932                 /// </summary>
933                 static public void VerifyAllUsing ()
934                 {
935                         foreach (NamespaceEntry entry in entries)
936                                 entry.VerifyUsing ();
937                 }
938
939                 public string GetSignatureForError ()
940                 {
941                         return ns.GetSignatureForError ();
942                 }
943
944                 public override string ToString ()
945                 {
946                         return ns.ToString ();
947                 }
948         }
949 }