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 Namespace curr_ns = NamespaceEntry.NS;
221 while ((curr_ns != null) && (resolved_ns == null)) {
222 resolved_ns = curr_ns.GetNamespace (Name, false);
224 if (resolved_ns == null)
225 curr_ns = curr_ns.Parent;
232 public class AliasEntry {
233 public readonly string Name;
234 public readonly Expression Alias;
235 public readonly NamespaceEntry NamespaceEntry;
236 public readonly Location Location;
238 public AliasEntry (NamespaceEntry entry, string name, Expression alias, Location loc)
242 NamespaceEntry = entry;
248 public object Resolve ()
250 if (resolved != null)
253 NamespaceEntry curr_ns = NamespaceEntry;
256 // GENERICS: Cope with the expression and not with the string
257 // this will fail with `using A = Stack<int>'
260 string alias = Alias.ToString ();
261 while ((curr_ns != null) && (resolved == null)) {
262 resolved = curr_ns.Lookup (null, alias, Location);
264 if (resolved == null)
265 curr_ns = curr_ns.Parent;
272 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
273 : this (parent, file, name, false, loc)
276 protected NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, bool is_implicit, Location loc)
278 this.parent = parent;
280 this.IsImplicit = is_implicit;
283 if (!is_implicit && (parent != null))
284 ns = parent.NS.GetNamespace (name, true);
285 else if (name != null)
286 ns = Namespace.LookupNamespace (name, true);
289 ns.AddNamespaceEntry (this);
291 if ((parent != null) && (parent.NS != ns.Parent))
292 implicit_parent = new NamespaceEntry (parent, file, ns.Parent.Name, true, loc);
294 implicit_parent = parent;
296 this.FullName = ns.Name;
299 static int next_id = 0;
300 public readonly string FullName;
301 public readonly int ID;
302 public readonly bool IsImplicit;
304 public Namespace NS {
310 public NamespaceEntry Parent {
316 public NamespaceEntry ImplicitParent {
318 return implicit_parent;
322 public void DefineName (string name, object o)
324 ns.DefineName (name, o);
328 /// Records a new namespace for resolving name references
330 public void Using (string ns, Location loc)
332 if (DeclarationFound){
333 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
340 if (using_clauses == null)
341 using_clauses = new ArrayList ();
343 foreach (UsingEntry old_entry in using_clauses){
344 if (old_entry.Name == ns){
345 Report.Warning (105, loc, "The using directive for '" + ns +
346 "' appeared previously in this namespace");
351 UsingEntry ue = new UsingEntry (this, ns, loc);
352 using_clauses.Add (ue);
355 public void UsingAlias (string alias, Expression namespace_or_type, Location loc)
358 aliases = new Hashtable ();
360 if (aliases.Contains (alias)){
361 Report.Error (1537, loc, "The using alias `" + alias +
362 "' appeared previously in this namespace");
366 aliases [alias] = new AliasEntry (this, alias, namespace_or_type, loc);
369 protected AliasEntry GetAliasEntry (string alias)
371 AliasEntry entry = null;
374 entry = (AliasEntry) aliases [alias];
375 if (entry == null && Parent != null)
376 entry = Parent.GetAliasEntry (alias);
381 public string LookupAlias (string alias)
383 AliasEntry entry = GetAliasEntry (alias);
388 object resolved = entry.Resolve ();
389 if (resolved == null)
391 else if (resolved is Namespace)
392 return ((Namespace) resolved).Name;
394 return ((Type) resolved).FullName;
397 public object Lookup (DeclSpace ds, string name, Location loc)
403 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
405 int pos = name.IndexOf ('.');
407 string first = name.Substring (0, pos);
408 string last = name.Substring (pos + 1);
410 o = Lookup (ds, first, loc);
416 return ns.Lookup (ds, last);
418 Type nested = TypeManager.LookupType ((((Type) o).Name + "." + last));
419 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
426 // Check whether it's a namespace.
428 o = NS.Lookup (ds, name);
435 AliasEntry entry = GetAliasEntry (name);
437 o = entry.Resolve ();
443 // Check using entries.
445 Type t = null, match = null;
446 foreach (Namespace using_ns in GetUsingTable ()) {
447 match = using_ns.Lookup (ds, name) as Type;
450 DeclSpace.Error_AmbiguousTypeReference (loc, name, t, match);
461 // Our cached computation.
462 Namespace [] namespace_using_table;
463 public Namespace[] GetUsingTable ()
465 if (namespace_using_table != null)
466 return namespace_using_table;
468 if (using_clauses == null)
469 return new Namespace [0];
471 ArrayList list = new ArrayList (using_clauses.Count);
473 foreach (UsingEntry ue in using_clauses) {
474 Namespace using_ns = ue.Resolve ();
475 if (using_ns == null)
481 namespace_using_table = new Namespace [list.Count];
482 list.CopyTo (namespace_using_table, 0);
483 return namespace_using_table;
486 public void DefineNamespace (SymbolWriter symwriter)
491 parent.DefineNamespace (symwriter);
494 if (using_clauses != null) {
495 using_list = new string [using_clauses.Count];
496 for (int i = 0; i < using_clauses.Count; i++)
497 using_list [i] = ((UsingEntry) using_clauses [i]).Name;
499 using_list = new string [0];
502 int parent_id = parent != null ? parent.symfile_id : 0;
503 symfile_id = symwriter.DefineNamespace (ns.Name, file, using_list, parent_id);
506 public int SymbolFileID {
512 static void Msgtry (string s)
514 Console.WriteLine (" Try using -r:" + s);
517 protected void error246 (Location loc, string name)
519 if (TypeManager.LookupType (name) != null)
520 Report.Error (138, loc, "The using keyword only lets you specify a namespace, " +
521 "`" + name + "' is a class not a namespace.");
523 Report.Error (246, loc, "The namespace `" + name +
524 "' can not be found (missing assembly reference?)");
527 case "Gtk": case "GtkSharp":
528 Msgtry ("gtk-sharp");
531 case "Gdk": case "GdkSharp":
532 Msgtry ("gdk-sharp");
535 case "Glade": case "GladeSharp":
536 Msgtry ("glade-sharp");
539 case "System.Drawing":
540 Msgtry ("System.Drawing");
543 case "System.Web.Services":
544 Msgtry ("System.Web.Services");
548 Msgtry ("System.Web");
552 Msgtry ("System.Data");
555 case "System.Windows.Forms":
556 Msgtry ("System.Windows.Forms");
563 /// Used to validate that all the using clauses are correct
564 /// after we are finished parsing all the files.
566 public void VerifyUsing ()
568 if (using_clauses != null){
569 foreach (UsingEntry ue in using_clauses){
570 if (ue.Resolve () != null)
573 error246 (ue.Location, ue.Name);
577 if (aliases != null){
578 foreach (DictionaryEntry de in aliases){
579 AliasEntry alias = (AliasEntry) de.Value;
581 if (alias.Resolve () != null)
584 error246 (alias.Location, alias.Alias.ToString ());
589 public override string ToString ()
591 if (NS == Namespace.Root)
592 return "NamespaceEntry (<root>)";
594 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);