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