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 {
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 object Lookup (DeclSpace ds, string name)
93 object o = Lookup (name);
96 DeclSpace tdecl = o as DeclSpace;
98 t = tdecl.DefineType ();
100 if ((ds == null) || ds.CheckAccessLevel (t))
104 Namespace ns = GetNamespace (name, false);
108 t = TypeManager.LookupType (DeclSpace.MakeFQN (fullname, name));
109 if ((t == null) || ((ds != null) && !ds.CheckAccessLevel (t)))
115 public void AddNamespaceEntry (NamespaceEntry entry)
120 public void DefineName (string name, object o)
122 defined_names.Add (name, o);
125 public object Lookup (string name)
127 return defined_names [name];
130 static public ArrayList UserDefinedNamespaces {
132 return all_namespaces;
137 /// The qualified name of the current namespace
146 /// The parent of this namespace, used by the parser to "Pop"
147 /// the current namespace declaration
149 public Namespace Parent {
155 public static void DefineNamespaces (SymbolWriter symwriter)
157 foreach (Namespace ns in all_namespaces) {
158 foreach (NamespaceEntry entry in ns.entries)
159 entry.DefineNamespace (symwriter);
164 /// Used to validate that all the using clauses are correct
165 /// after we are finished parsing all the files.
167 public static void VerifyUsing ()
169 foreach (Namespace ns in all_namespaces) {
170 foreach (NamespaceEntry entry in ns.entries)
171 entry.VerifyUsing ();
175 public override string ToString ()
178 return "Namespace (<root>)";
180 return String.Format ("Namespace ({0})", Name);
184 public class NamespaceEntry
187 NamespaceEntry parent, implicit_parent;
191 ArrayList using_clauses;
192 public bool DeclarationFound = false;
195 // This class holds the location where a using definition is
196 // done, and whether it has been used by the program or not.
198 // We use this to flag using clauses for namespaces that do not
201 public class UsingEntry {
202 public readonly string Name;
203 public readonly NamespaceEntry NamespaceEntry;
204 public readonly Location Location;
206 public UsingEntry (NamespaceEntry entry, string name, Location loc)
209 NamespaceEntry = entry;
213 Namespace resolved_ns;
215 public Namespace Resolve ()
217 if (resolved_ns != null)
220 object resolved = NamespaceEntry.LookupForUsing (Name, Location);
221 resolved_ns = resolved as Namespace;
226 public class AliasEntry {
227 public readonly string Name;
228 public readonly MemberName Alias;
229 public readonly NamespaceEntry NamespaceEntry;
230 public readonly Location Location;
232 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
236 NamespaceEntry = entry;
242 public object Resolve ()
244 if (resolved != null)
247 string alias = Alias.GetPartialName ();
249 resolved = NamespaceEntry.LookupForUsing (alias, Location);
254 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
255 : this (parent, file, name, false, loc)
258 protected NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, bool is_implicit, Location loc)
260 this.parent = parent;
262 this.IsImplicit = is_implicit;
265 if (!is_implicit && (parent != null))
266 ns = parent.NS.GetNamespace (name, true);
267 else if (name != null)
268 ns = Namespace.LookupNamespace (name, true);
271 ns.AddNamespaceEntry (this);
273 if ((parent != null) && (parent.NS != ns.Parent))
274 implicit_parent = new NamespaceEntry (parent, file, ns.Parent.Name, true, loc);
276 implicit_parent = parent;
278 this.FullName = ns.Name;
281 static int next_id = 0;
282 public readonly string FullName;
283 public readonly int ID;
284 public readonly bool IsImplicit;
286 public Namespace NS {
292 public NamespaceEntry Parent {
298 public NamespaceEntry ImplicitParent {
300 return implicit_parent;
304 public void DefineName (string name, object o)
306 ns.DefineName (name, o);
310 /// Records a new namespace for resolving name references
312 public void Using (string ns, Location loc)
314 if (DeclarationFound){
315 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
322 if (using_clauses == null)
323 using_clauses = new ArrayList ();
325 foreach (UsingEntry old_entry in using_clauses) {
326 if (old_entry.Name == ns) {
327 if (RootContext.WarningLevel >= 3)
328 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", ns);
333 UsingEntry ue = new UsingEntry (this, ns, loc);
334 using_clauses.Add (ue);
337 public void UsingAlias (string name, MemberName alias, Location loc)
339 if (DeclarationFound){
340 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
345 aliases = new Hashtable ();
347 if (aliases.Contains (name)){
348 Report.Error (1537, loc, "The using alias `" + name +
349 "' appeared previously in this namespace");
353 aliases [name] = new AliasEntry (this, name, alias, loc);
356 protected AliasEntry GetAliasEntry (string alias)
358 AliasEntry entry = null;
361 entry = (AliasEntry) aliases [alias];
362 if (entry == null && Parent != null)
363 entry = Parent.GetAliasEntry (alias);
368 public string LookupAlias (string alias)
370 AliasEntry entry = GetAliasEntry (alias);
375 object resolved = entry.Resolve ();
376 if (resolved == null)
378 else if (resolved is Namespace)
379 return ((Namespace) resolved).Name;
381 return ((Type) resolved).FullName;
385 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
386 // resolved as if the immediately containing namespace body has no using-directives.
388 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
389 // in the using-namespace-directive.
391 public object LookupForUsing (string dotted_name, Location loc)
393 int pos = dotted_name.IndexOf ('.');
394 string simple_name = dotted_name;
397 simple_name = dotted_name.Substring (0, pos);
398 rest = dotted_name.Substring (pos + 1);
401 object o = NS.Lookup (null, simple_name);
402 if (o == null && ImplicitParent != null)
403 o = ImplicitParent.LookupNamespaceOrType (null, simple_name, loc);
405 if (o == null || rest == null)
408 Namespace ns = o as Namespace;
410 return ns.Lookup (null, rest);
412 Type nested = TypeManager.LookupType ((((Type) o).Name + "." + rest));
416 public object LookupNamespaceOrType (DeclSpace ds, string name, Location loc)
418 object resolved = null;
419 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
420 if ((resolved = curr_ns.Lookup (ds, name, loc)) != null)
426 private object Lookup (DeclSpace ds, string name, Location loc)
432 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
434 // FIXME: Remove this block. Only simple names should come here.
435 // The bug: The loop in LookupNamespaceOrType continues if
436 // the lookup for N succeeds but the nested lookup for I fails.
437 // This is one part of #52697.
439 int pos = name.IndexOf ('.');
441 string first = name.Substring (0, pos);
442 string last = name.Substring (pos + 1);
444 o = Lookup (ds, first, loc);
450 return ns.Lookup (ds, last);
452 Type nested = TypeManager.LookupType ((((Type) o).Name + "." + last));
453 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
460 // Check whether it's in the namespace.
462 o = NS.Lookup (ds, name);
469 AliasEntry entry = GetAliasEntry (name);
471 o = entry.Resolve ();
476 if (name.IndexOf ('.') > 0)
480 // Check using entries.
482 Type t = null, match = null;
483 foreach (Namespace using_ns in GetUsingTable ()) {
484 match = using_ns.Lookup (ds, name) as Type;
487 DeclSpace.Error_AmbiguousTypeReference (loc, name, t, match);
498 // Our cached computation.
499 Namespace [] namespace_using_table;
500 public Namespace[] GetUsingTable ()
502 if (namespace_using_table != null)
503 return namespace_using_table;
505 if (using_clauses == null)
506 return new Namespace [0];
508 ArrayList list = new ArrayList (using_clauses.Count);
510 foreach (UsingEntry ue in using_clauses) {
511 Namespace using_ns = ue.Resolve ();
512 if (using_ns == null)
518 namespace_using_table = new Namespace [list.Count];
519 list.CopyTo (namespace_using_table, 0);
520 return namespace_using_table;
523 public void DefineNamespace (SymbolWriter symwriter)
528 parent.DefineNamespace (symwriter);
531 if (using_clauses != null) {
532 using_list = new string [using_clauses.Count];
533 for (int i = 0; i < using_clauses.Count; i++)
534 using_list [i] = ((UsingEntry) using_clauses [i]).Name;
536 using_list = new string [0];
539 int parent_id = parent != null ? parent.symfile_id : 0;
540 if (file.SourceFileEntry == null)
543 symfile_id = symwriter.DefineNamespace (
544 ns.Name, file.SourceFileEntry, using_list, parent_id);
547 public int SymbolFileID {
553 static void MsgtryRef (string s)
555 Console.WriteLine (" Try using -r:" + s);
558 static void MsgtryPkg (string s)
560 Console.WriteLine (" Try using -pkg:" + s);
563 protected void error246 (Location loc, string name)
565 Report.Error (246, loc, "The namespace `" + name +
566 "' can not be found (missing assembly reference?)");
569 case "Gtk": case "GtkSharp":
570 MsgtryPkg ("gtk-sharp");
573 case "Gdk": case "GdkSharp":
574 MsgtryPkg ("gdk-sharp");
577 case "Glade": case "GladeSharp":
578 MsgtryPkg ("glade-sharp");
581 case "System.Drawing":
582 case "System.Web.Services":
585 case "System.Windows.Forms":
592 /// Used to validate that all the using clauses are correct
593 /// after we are finished parsing all the files.
595 public void VerifyUsing ()
597 if (using_clauses != null){
598 foreach (UsingEntry ue in using_clauses){
599 if (ue.Resolve () != null)
602 if (LookupForUsing (ue.Name, ue.Location) == null)
603 error246 (ue.Location, ue.Name);
605 Report.Error (138, ue.Location, "The using keyword only lets you specify a namespace, " +
606 "`" + ue.Name + "' is a class not a namespace.");
611 if (aliases != null){
612 foreach (DictionaryEntry de in aliases){
613 AliasEntry alias = (AliasEntry) de.Value;
615 if (alias.Resolve () != null)
618 error246 (alias.Location, alias.Alias.GetPartialName ());
623 public override string ToString ()
625 if (NS == Namespace.Root)
626 return "NamespaceEntry (<root>)";
628 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);