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