2 // namespace.cs: Tracks namespaces
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
10 using System.Collections;
12 namespace Mono.CSharp {
15 /// Keeps track of the namespaces defined in the C# code.
17 public class Namespace : IAlias {
18 static ArrayList all_namespaces = new ArrayList ();
19 static Hashtable namespaces_map = new Hashtable ();
25 Hashtable defined_names;
28 /// Constructor Takes the current namespace and the
29 /// name. This is bootstrapped with parent == null
32 public Namespace (Namespace parent, string name)
36 string pname = parent != null ? parent.Name : "";
41 fullname = parent.Name + "." + name;
43 entries = new ArrayList ();
44 namespaces = new Hashtable ();
45 defined_names = new Hashtable ();
47 all_namespaces.Add (this);
48 if (namespaces_map.Contains (fullname))
50 namespaces_map [fullname] = true;
53 public static bool IsNamespace (string name)
55 return namespaces_map [name] != null;
58 public static Namespace Root = new Namespace (null, "");
60 public Namespace GetNamespace (string name, bool create)
62 int pos = name.IndexOf ('.');
67 first = name.Substring (0, pos);
71 ns = (Namespace) namespaces [first];
76 ns = new Namespace (this, first);
77 namespaces.Add (first, ns);
81 ns = ns.GetNamespace (name.Substring (pos + 1), create);
86 public static Namespace LookupNamespace (string name, bool create)
88 return Root.GetNamespace (name, create);
91 public IAlias Lookup (DeclSpace ds, string name, Location loc)
93 IAlias o = Lookup (name);
96 DeclSpace tdecl = o as DeclSpace;
98 t = tdecl.DefineType ();
102 if ((ds == null) || ds.CheckAccessLevel (t))
103 return new TypeExpression (t, loc);
106 Namespace ns = GetNamespace (name, false);
110 t = TypeManager.LookupType (DeclSpace.MakeFQN (fullname, name));
111 if ((t == null) || ((ds != null) && !ds.CheckAccessLevel (t)))
114 return new TypeExpression (t, loc);
117 public void AddNamespaceEntry (NamespaceEntry entry)
122 public void DefineName (string name, IAlias o)
124 defined_names.Add (name, o);
127 public IAlias Lookup (string name)
129 return (IAlias) defined_names [name];
132 static public ArrayList UserDefinedNamespaces {
134 return all_namespaces;
139 /// The qualified name of the current namespace
148 /// The parent of this namespace, used by the parser to "Pop"
149 /// the current namespace declaration
151 public Namespace Parent {
157 public static void DefineNamespaces (SymbolWriter symwriter)
159 foreach (Namespace ns in all_namespaces) {
160 foreach (NamespaceEntry entry in ns.entries)
161 entry.DefineNamespace (symwriter);
166 /// Used to validate that all the using clauses are correct
167 /// after we are finished parsing all the files.
169 public static void VerifyUsing ()
171 foreach (Namespace ns in all_namespaces) {
172 foreach (NamespaceEntry entry in ns.entries)
173 entry.VerifyUsing ();
177 public override string ToString ()
180 return "Namespace (<root>)";
182 return String.Format ("Namespace ({0})", Name);
186 get { return false; }
189 TypeExpr IAlias.ResolveAsType (EmitContext ec)
191 throw new InvalidOperationException ();
195 public class NamespaceEntry
198 NamespaceEntry parent, implicit_parent;
202 ArrayList using_clauses;
203 public bool DeclarationFound = false;
206 // This class holds the location where a using definition is
207 // done, and whether it has been used by the program or not.
209 // We use this to flag using clauses for namespaces that do not
212 public class UsingEntry {
213 public readonly string Name;
214 public readonly NamespaceEntry NamespaceEntry;
215 public readonly Location Location;
217 public UsingEntry (NamespaceEntry entry, string name, Location loc)
220 NamespaceEntry = entry;
224 Namespace resolved_ns;
226 public Namespace Resolve ()
228 if (resolved_ns != null)
231 object resolved = NamespaceEntry.LookupForUsing (Name, Location);
232 resolved_ns = resolved as Namespace;
237 public class AliasEntry {
238 public readonly string Name;
239 public readonly MemberName Alias;
240 public readonly NamespaceEntry NamespaceEntry;
241 public readonly Location Location;
243 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
247 NamespaceEntry = entry;
253 public IAlias Resolve ()
255 if (resolved != null)
259 // GENERICS: Cope with the expression and not with the string
260 // this will fail with `using A = Stack<int>'
263 string alias = Alias.GetTypeName ();
265 resolved = NamespaceEntry.LookupForUsing (alias, Location);
266 if (resolved == null)
270 resolved = new TypeAliasExpression (
271 resolved, Alias.TypeArguments, Location);
277 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
278 : this (parent, file, name, false, loc)
281 protected NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, bool is_implicit, Location loc)
283 this.parent = parent;
285 this.IsImplicit = is_implicit;
288 if (!is_implicit && (parent != null))
289 ns = parent.NS.GetNamespace (name, true);
290 else if (name != null)
291 ns = Namespace.LookupNamespace (name, true);
294 ns.AddNamespaceEntry (this);
296 if ((parent != null) && (parent.NS != ns.Parent))
297 implicit_parent = new NamespaceEntry (parent, file, ns.Parent.Name, true, loc);
299 implicit_parent = parent;
301 this.FullName = ns.Name;
304 static int next_id = 0;
305 public readonly string FullName;
306 public readonly int ID;
307 public readonly bool IsImplicit;
309 public Namespace NS {
315 public NamespaceEntry Parent {
321 public NamespaceEntry ImplicitParent {
323 return implicit_parent;
327 public void DefineName (string name, IAlias o)
329 ns.DefineName (name, o);
333 /// Records a new namespace for resolving name references
335 public void Using (string ns, Location loc)
337 if (DeclarationFound){
338 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
345 if (using_clauses == null)
346 using_clauses = new ArrayList ();
348 foreach (UsingEntry old_entry in using_clauses) {
349 if (old_entry.Name == ns) {
350 if (RootContext.WarningLevel >= 3)
351 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", ns);
356 UsingEntry ue = new UsingEntry (this, ns, loc);
357 using_clauses.Add (ue);
360 public void UsingAlias (string name, MemberName alias, Location loc)
362 if (DeclarationFound){
363 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
368 aliases = new Hashtable ();
370 if (aliases.Contains (name)){
371 Report.Error (1537, loc, "The using alias `" + name +
372 "' appeared previously in this namespace");
376 aliases [name] = new AliasEntry (this, name, alias, loc);
379 protected AliasEntry GetAliasEntry (string alias)
381 AliasEntry entry = null;
384 entry = (AliasEntry) aliases [alias];
385 if (entry == null && Parent != null)
386 entry = Parent.GetAliasEntry (alias);
391 public IAlias LookupAlias (string alias)
393 AliasEntry entry = GetAliasEntry (alias);
398 return entry.Resolve ();
402 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
403 // resolved as if the immediately containing namespace body has no using-directives.
405 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
406 // in the using-namespace-directive.
408 public IAlias LookupForUsing (string dotted_name, Location loc)
410 int pos = dotted_name.IndexOf ('.');
411 string simple_name = dotted_name;
414 simple_name = dotted_name.Substring (0, pos);
415 rest = dotted_name.Substring (pos + 1);
418 IAlias o = NS.Lookup (null, simple_name, loc);
419 if (o == null && ImplicitParent != null)
420 o = ImplicitParent.LookupNamespaceOrType (null, simple_name, loc);
422 if (o == null || rest == null)
425 Namespace ns = o as Namespace;
427 return ns.Lookup (null, rest, loc);
429 Type nested = TypeManager.LookupType (o.Name + "." + rest);
433 return new TypeExpression (nested, loc);
436 public IAlias LookupNamespaceOrType (DeclSpace ds, string name, Location loc)
438 IAlias resolved = null;
439 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
440 if ((resolved = curr_ns.Lookup (ds, name, loc)) != null)
446 private IAlias Lookup (DeclSpace ds, string name, Location loc)
452 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
454 // FIXME: Remove this block. Only simple names should come here.
455 // The bug: The loop in LookupNamespaceOrType continues if
456 // the lookup for N succeeds but the nested lookup for I fails.
457 // This is one part of #52697.
459 int pos = name.IndexOf ('.');
461 string first = name.Substring (0, pos);
462 string last = name.Substring (pos + 1);
464 o = Lookup (ds, first, loc);
470 o = ns.Lookup (ds, last, loc);
474 Type nested = TypeManager.LookupType (o.Name + "." + last);
475 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
478 return new TypeExpression (nested, loc);
482 // Check whether it's in the namespace.
484 o = NS.Lookup (ds, name, loc);
491 AliasEntry entry = GetAliasEntry (name);
493 IAlias alias = entry.Resolve ();
498 if (name.IndexOf ('.') > 0)
502 // Check using entries.
504 IAlias t = null, match = null;
505 foreach (Namespace using_ns in GetUsingTable ()) {
506 match = using_ns.Lookup (ds, name, loc);
507 if ((match != null) && match.IsType){
509 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.Name, match.Name);
520 // Our cached computation.
521 Namespace [] namespace_using_table;
522 public Namespace[] GetUsingTable ()
524 if (namespace_using_table != null)
525 return namespace_using_table;
527 if (using_clauses == null)
528 return new Namespace [0];
530 ArrayList list = new ArrayList (using_clauses.Count);
532 foreach (UsingEntry ue in using_clauses) {
533 Namespace using_ns = ue.Resolve ();
534 if (using_ns == null)
540 namespace_using_table = new Namespace [list.Count];
541 list.CopyTo (namespace_using_table, 0);
542 return namespace_using_table;
545 public void DefineNamespace (SymbolWriter symwriter)
550 parent.DefineNamespace (symwriter);
553 if (using_clauses != null) {
554 using_list = new string [using_clauses.Count];
555 for (int i = 0; i < using_clauses.Count; i++)
556 using_list [i] = ((UsingEntry) using_clauses [i]).Name;
558 using_list = new string [0];
561 int parent_id = parent != null ? parent.symfile_id : 0;
562 if (file.SourceFileEntry == null)
565 symfile_id = symwriter.DefineNamespace (
566 ns.Name, file.SourceFileEntry, using_list, parent_id);
569 public int SymbolFileID {
575 static void MsgtryRef (string s)
577 Console.WriteLine (" Try using -r:" + s);
580 static void MsgtryPkg (string s)
582 Console.WriteLine (" Try using -pkg:" + s);
585 protected void error246 (Location loc, string name)
587 Report.Error (246, loc, "The namespace `" + name +
588 "' can not be found (missing assembly reference?)");
591 case "Gtk": case "GtkSharp":
592 MsgtryPkg ("gtk-sharp");
595 case "Gdk": case "GdkSharp":
596 MsgtryPkg ("gdk-sharp");
599 case "Glade": case "GladeSharp":
600 MsgtryPkg ("glade-sharp");
603 case "System.Drawing":
604 case "System.Web.Services":
607 case "System.Windows.Forms":
614 /// Used to validate that all the using clauses are correct
615 /// after we are finished parsing all the files.
617 public void VerifyUsing ()
619 TypeContainer dummy = new RootTypes ();
620 EmitContext ec = new EmitContext (
621 dummy, Location.Null, null, null, 0, false);
623 if (using_clauses != null){
624 foreach (UsingEntry ue in using_clauses){
625 if (ue.Resolve () != null)
628 if (LookupForUsing (ue.Name, ue.Location) == null)
629 error246 (ue.Location, ue.Name);
631 Report.Error (138, ue.Location, "The using keyword only lets you specify a namespace, " +
632 "`" + ue.Name + "' is a class not a namespace.");
637 if (aliases != null){
638 foreach (DictionaryEntry de in aliases){
639 AliasEntry entry = (AliasEntry) de.Value;
641 IAlias alias = entry.Resolve ();
644 alias.ResolveAsType (ec);
649 error246 (entry.Location, entry.Alias.GetTypeName ());
654 public override string ToString ()
656 if (NS == Namespace.Root)
657 return "NamespaceEntry (<root>)";
659 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);