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 ();
22 string name, fullname;
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)
37 string pname = parent != null ? parent.Name : "";
42 fullname = parent.Name + "." + name;
44 entries = new ArrayList ();
45 namespaces = new Hashtable ();
46 defined_names = new Hashtable ();
48 all_namespaces.Add (this);
49 if (namespaces_map.Contains (fullname))
51 namespaces_map [fullname] = true;
54 public static bool IsNamespace (string name)
56 return namespaces_map [name] != null;
59 public static Namespace Root = new Namespace (null, "");
61 public Namespace GetNamespace (string name, bool create)
63 int pos = name.IndexOf ('.');
68 first = name.Substring (0, pos);
72 ns = (Namespace) namespaces [first];
77 ns = new Namespace (this, first);
78 namespaces.Add (first, ns);
82 ns = ns.GetNamespace (name.Substring (pos + 1), create);
87 public static Namespace LookupNamespace (string name, bool create)
89 return Root.GetNamespace (name, create);
92 public object Lookup (DeclSpace ds, string name)
94 object o = Lookup (name);
97 DeclSpace tdecl = o as DeclSpace;
99 t = tdecl.DefineType ();
101 if ((ds == null) || ds.CheckAccessLevel (t))
105 Namespace ns = GetNamespace (name, false);
109 t = TypeManager.LookupType (DeclSpace.MakeFQN (fullname, name));
110 if ((t == null) || ((ds != null) && !ds.CheckAccessLevel (t)))
116 public void AddNamespaceEntry (NamespaceEntry entry)
121 public void DefineName (string name, object o)
123 defined_names.Add (name, o);
126 public object Lookup (string name)
128 return defined_names [name];
131 static public ArrayList UserDefinedNamespaces {
133 return all_namespaces;
138 /// The qualified name of the current namespace
147 /// The parent of this namespace, used by the parser to "Pop"
148 /// the current namespace declaration
150 public Namespace Parent {
156 public static void DefineNamespaces (SymbolWriter symwriter)
158 foreach (Namespace ns in all_namespaces) {
159 foreach (NamespaceEntry entry in ns.entries)
160 entry.DefineNamespace (symwriter);
165 /// Used to validate that all the using clauses are correct
166 /// after we are finished parsing all the files.
168 public static void VerifyUsing ()
170 foreach (Namespace ns in all_namespaces) {
171 foreach (NamespaceEntry entry in ns.entries)
172 entry.VerifyUsing ();
176 public override string ToString ()
179 return "Namespace (<root>)";
181 return String.Format ("Namespace ({0})", Name);
185 public class NamespaceEntry
188 NamespaceEntry parent, implicit_parent;
192 ArrayList using_clauses;
193 public bool DeclarationFound = false;
196 // This class holds the location where a using definition is
197 // done, and whether it has been used by the program or not.
199 // We use this to flag using clauses for namespaces that do not
202 public class UsingEntry {
203 public readonly string Name;
204 public readonly NamespaceEntry NamespaceEntry;
205 public readonly Location Location;
207 public UsingEntry (NamespaceEntry entry, string name, Location loc)
210 NamespaceEntry = entry;
214 Namespace resolved_ns;
216 public Namespace Resolve ()
218 if (resolved_ns != null)
221 Namespace curr_ns = NamespaceEntry.NS;
222 while ((curr_ns != null) && (resolved_ns == null)) {
223 resolved_ns = curr_ns.GetNamespace (Name, false);
225 if (resolved_ns == null)
226 curr_ns = curr_ns.Parent;
233 public class AliasEntry {
234 public readonly string Name;
235 public readonly Expression Alias;
236 public readonly NamespaceEntry NamespaceEntry;
237 public readonly Location Location;
239 public AliasEntry (NamespaceEntry entry, string name, Expression alias, Location loc)
243 NamespaceEntry = entry;
249 public object Resolve ()
251 if (resolved != null)
254 NamespaceEntry curr_ns = NamespaceEntry;
257 // GENERICS: Cope with the expression and not with the string
258 // this will fail with `using A = Stack<int>'
261 string alias = Alias.ToString ();
262 while ((curr_ns != null) && (resolved == null)) {
263 resolved = curr_ns.Lookup (null, alias, Location);
265 if (resolved == null)
266 curr_ns = curr_ns.Parent;
273 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
274 : this (parent, file, name, false, loc)
277 protected NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, bool is_implicit, Location loc)
279 this.parent = parent;
281 this.IsImplicit = is_implicit;
284 if (!is_implicit && (parent != null))
285 ns = parent.NS.GetNamespace (name, true);
286 else if (name != null)
287 ns = Namespace.LookupNamespace (name, true);
290 ns.AddNamespaceEntry (this);
292 if ((parent != null) && (parent.NS != ns.Parent))
293 implicit_parent = new NamespaceEntry (parent, file, ns.Parent.Name, true, loc);
295 implicit_parent = parent;
297 this.FullName = ns.Name;
300 static int next_id = 0;
301 public readonly string FullName;
302 public readonly int ID;
303 public readonly bool IsImplicit;
305 public Namespace NS {
311 public NamespaceEntry Parent {
317 public NamespaceEntry ImplicitParent {
319 return implicit_parent;
323 public void DefineName (string name, object o)
325 ns.DefineName (name, o);
329 /// Records a new namespace for resolving name references
331 public void Using (string ns, Location loc)
333 if (DeclarationFound){
334 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
341 if (using_clauses == null)
342 using_clauses = new ArrayList ();
344 foreach (UsingEntry old_entry in using_clauses){
345 if (old_entry.Name == ns){
346 Report.Warning (105, loc, "The using directive for '" + ns +
347 "' appeared previously in this namespace");
352 UsingEntry ue = new UsingEntry (this, ns, loc);
353 using_clauses.Add (ue);
356 public void UsingAlias (string alias, Expression namespace_or_type, Location loc)
359 aliases = new Hashtable ();
361 if (aliases.Contains (alias)){
362 Report.Error (1537, loc, "The using alias `" + alias +
363 "' appeared previously in this namespace");
367 aliases [alias] = new AliasEntry (this, alias, namespace_or_type, loc);
370 protected AliasEntry GetAliasEntry (string alias)
372 AliasEntry entry = null;
375 entry = (AliasEntry) aliases [alias];
376 if (entry == null && Parent != null)
377 entry = Parent.GetAliasEntry (alias);
382 public string LookupAlias (string alias)
384 AliasEntry entry = GetAliasEntry (alias);
389 object resolved = entry.Resolve ();
390 if (resolved == null)
392 else if (resolved is Namespace)
393 return ((Namespace) resolved).Name;
395 return ((Type) resolved).FullName;
398 public object Lookup (DeclSpace ds, string name, Location loc)
404 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
406 int pos = name.IndexOf ('.');
408 string first = name.Substring (0, pos);
409 string last = name.Substring (pos + 1);
411 o = Lookup (ds, first, loc);
417 return ns.Lookup (ds, last);
419 Type nested = TypeManager.LookupType ((((Type) o).Name + "." + last));
420 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
427 // Check whether it's a namespace.
429 o = NS.Lookup (ds, name);
436 AliasEntry entry = GetAliasEntry (name);
438 o = entry.Resolve ();
444 // Check using entries.
446 Type t = null, match = null;
447 foreach (Namespace using_ns in GetUsingTable ()) {
448 match = using_ns.Lookup (ds, name) as Type;
451 DeclSpace.Error_AmbiguousTypeReference (loc, name, t, match);
462 // Our cached computation.
463 Namespace [] namespace_using_table;
464 public Namespace[] GetUsingTable ()
466 if (namespace_using_table != null)
467 return namespace_using_table;
469 if (using_clauses == null)
470 return new Namespace [0];
472 ArrayList list = new ArrayList (using_clauses.Count);
474 foreach (UsingEntry ue in using_clauses) {
475 Namespace using_ns = ue.Resolve ();
476 if (using_ns == null)
482 namespace_using_table = new Namespace [list.Count];
483 list.CopyTo (namespace_using_table, 0);
484 return namespace_using_table;
487 public void DefineNamespace (SymbolWriter symwriter)
492 parent.DefineNamespace (symwriter);
495 if (using_clauses != null) {
496 using_list = new string [using_clauses.Count];
497 for (int i = 0; i < using_clauses.Count; i++)
498 using_list [i] = ((UsingEntry) using_clauses [i]).Name;
500 using_list = new string [0];
503 int parent_id = parent != null ? parent.symfile_id : 0;
504 symfile_id = symwriter.DefineNamespace (ns.Name, file, using_list, parent_id);
507 public int SymbolFileID {
513 static void Msgtry (string s)
515 Console.WriteLine (" Try using -r:" + s);
518 protected void error246 (Location loc, string name)
520 if (TypeManager.LookupType (name) != null)
521 Report.Error (138, loc, "The using keyword only lets you specify a namespace, " +
522 "`" + name + "' is a class not a namespace.");
524 Report.Error (246, loc, "The namespace `" + name +
525 "' can not be found (missing assembly reference?)");
528 case "Gtk": case "GtkSharp":
529 Msgtry ("gtk-sharp");
532 case "Gdk": case "GdkSharp":
533 Msgtry ("gdk-sharp");
536 case "Glade": case "GladeSharp":
537 Msgtry ("glade-sharp");
540 case "System.Drawing":
541 Msgtry ("System.Drawing");
544 case "System.Web.Services":
545 Msgtry ("System.Web.Services");
549 Msgtry ("System.Web");
553 Msgtry ("System.Data");
556 case "System.Windows.Forms":
557 Msgtry ("System.Windows.Forms");
564 /// Used to validate that all the using clauses are correct
565 /// after we are finished parsing all the files.
567 public void VerifyUsing ()
569 if (using_clauses != null){
570 foreach (UsingEntry ue in using_clauses){
571 if (ue.Resolve () != null)
574 error246 (ue.Location, ue.Name);
578 if (aliases != null){
579 foreach (DictionaryEntry de in aliases){
580 AliasEntry alias = (AliasEntry) de.Value;
582 if (alias.Resolve () != null)
585 error246 (alias.Location, alias.Alias.ToString ());
590 public override string ToString ()
592 if (NS == Namespace.Root)
593 return "NamespaceEntry (<root>)";
595 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);