In mcs and gmcs:
[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 {
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                                 DeclSpace root = RootContext.ToplevelTypes;
472                                 root.NamespaceEntry = NamespaceEntry;
473                                 FullNamedExpression fne = Expr.ResolveAsTypeStep (root, false);
474                                 root.NamespaceEntry = null;
475
476                                 if (fne == null) {
477                                         Error_NamespaceNotFound (Location, Name.ToString ());
478                                         return null;
479                                 }
480
481                                 resolved = fne as Namespace;
482                                 if (resolved == null) {
483                                         Report.Error (138, Location,
484                                                 "`{0} is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.ToString ());
485                                 }
486                                 return resolved;
487                         }
488                 }
489
490                 public abstract class AliasEntry {
491                         public readonly string Name;
492                         public readonly NamespaceEntry NamespaceEntry;
493                         public readonly Location Location;
494                         
495                         protected AliasEntry (NamespaceEntry entry, string name, Location loc)
496                         {
497                                 Name = name;
498                                 NamespaceEntry = entry;
499                                 Location = loc;
500                         }
501                         
502                         protected FullNamedExpression resolved;
503                         bool error;
504
505                         public FullNamedExpression Resolve ()
506                         {
507                                 if (resolved != null || error)
508                                         return resolved;
509                                 resolved = DoResolve ();
510                                 if (resolved == null)
511                                         error = true;
512                                 return resolved;
513                         }
514
515                         protected abstract FullNamedExpression DoResolve ();
516                 }
517
518                 public class LocalAliasEntry : AliasEntry
519                 {
520                         public readonly Expression Alias;
521                         
522                         public LocalAliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc) :
523                                 base (entry, name, loc)
524                         {
525                                 Alias = alias.GetTypeExpression ();
526                         }
527
528                         protected override FullNamedExpression DoResolve ()
529                         {
530                                 DeclSpace root = RootContext.ToplevelTypes;
531                                 root.NamespaceEntry = NamespaceEntry;
532                                 resolved = Alias.ResolveAsTypeStep (root, false);
533                                 root.NamespaceEntry = null;
534
535                                 if (resolved == null)
536                                         return null;
537
538                                 if (resolved.Type != null) {
539                                         TypeAttributes attr = resolved.Type.Attributes & TypeAttributes.VisibilityMask;
540                                         if (attr == TypeAttributes.NestedPrivate || attr == TypeAttributes.NestedFamily ||
541                                                 ((attr == TypeAttributes.NestedFamORAssem || attr == TypeAttributes.NestedAssembly) && 
542                                                 TypeManager.LookupDeclSpace (resolved.Type) == null)) {
543                                                 Expression.ErrorIsInaccesible (Alias.Location, Alias.ToString ());
544                                                 return null;
545                                         }
546                                 }
547
548                                 return resolved;
549                         }
550                 }
551
552                 public class ExternAliasEntry : AliasEntry 
553                 {
554                         public ExternAliasEntry (NamespaceEntry entry, string name, Location loc) :
555                                 base (entry, name, loc)
556                         {
557                         }
558
559                         protected override FullNamedExpression DoResolve ()
560                         {
561                                 resolved = RootNamespace.GetRootNamespace (Name);
562                                 if (resolved == null)
563                                         Report.Error (430, Location, "The extern alias '" + Name +
564                                                                         "' was not specified in a /reference option");
565
566                                 return resolved;
567                         }
568                 }
569
570                 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name)
571                 {
572                         this.parent = parent;
573                         this.file = file;
574                         entries.Add (this);
575                         this.ID = entries.Count;
576
577                         if (parent != null)
578                                 ns = parent.NS.GetNamespace (name, true);
579                         else if (name != null)
580                                 ns = RootNamespace.Global.GetNamespace (name, true);
581                         else
582                                 ns = RootNamespace.Global;
583                         SlaveDeclSpace = new RootDeclSpace (this);
584                 }
585
586                 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
587                 {
588                         this.parent = parent;
589                         this.file = file;
590                         // no need to add self to 'entries', since we don't have any aliases or using entries.
591                         this.ID = -1;
592                         this.IsImplicit = true;
593                         this.ns = ns;
594                         this.SlaveDeclSpace = null;
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 }