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 ();
100 if ((ds == null) || ds.CheckAccessLevel (t))
101 return new TypeExpression (t, loc);
104 Namespace ns = GetNamespace (name, false);
108 t = TypeManager.LookupType (DeclSpace.MakeFQN (fullname, name));
109 if ((t == null) || ((ds != null) && !ds.CheckAccessLevel (t)))
112 return new TypeExpression (t, loc);
115 public void AddNamespaceEntry (NamespaceEntry entry)
120 public void DefineName (string name, IAlias o)
122 defined_names.Add (name, o);
125 public IAlias Lookup (string name)
127 return (IAlias) 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 get { return false; }
187 TypeExpr IAlias.Type {
189 throw new InvalidOperationException ();
194 public class NamespaceEntry
197 NamespaceEntry parent, implicit_parent;
201 ArrayList using_clauses;
202 public bool DeclarationFound = false;
205 // This class holds the location where a using definition is
206 // done, and whether it has been used by the program or not.
208 // We use this to flag using clauses for namespaces that do not
211 public class UsingEntry {
212 public readonly string Name;
213 public readonly NamespaceEntry NamespaceEntry;
214 public readonly Location Location;
216 public UsingEntry (NamespaceEntry entry, string name, Location loc)
219 NamespaceEntry = entry;
223 Namespace resolved_ns;
225 public Namespace Resolve ()
227 if (resolved_ns != null)
230 Namespace curr_ns = NamespaceEntry.NS;
231 while ((curr_ns != null) && (resolved_ns == null)) {
232 resolved_ns = curr_ns.GetNamespace (Name, false);
234 if (resolved_ns == null)
235 curr_ns = curr_ns.Parent;
242 public class AliasEntry {
243 public readonly string Name;
244 public readonly TypeName Alias;
245 public readonly NamespaceEntry NamespaceEntry;
246 public readonly Location Location;
248 public AliasEntry (NamespaceEntry entry, string name, TypeName alias, Location loc)
252 NamespaceEntry = entry;
258 public IAlias Resolve ()
260 if (resolved != null)
263 NamespaceEntry curr_ns = NamespaceEntry;
266 // GENERICS: Cope with the expression and not with the string
267 // this will fail with `using A = Stack<int>'
270 string alias = Alias.GetTypeName (true);
271 while ((curr_ns != null) && (resolved == null)) {
272 resolved = curr_ns.Lookup (
273 null, alias, Alias.CountTypeArguments,
276 if (resolved == null)
277 curr_ns = curr_ns.Parent;
280 if (resolved == null)
284 resolved = new TypeAliasExpression (
285 resolved.Type, Alias.TypeArguments, Location);
291 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
292 : this (parent, file, name, false, loc)
295 protected NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, bool is_implicit, Location loc)
297 this.parent = parent;
299 this.IsImplicit = is_implicit;
302 if (!is_implicit && (parent != null))
303 ns = parent.NS.GetNamespace (name, true);
304 else if (name != null)
305 ns = Namespace.LookupNamespace (name, true);
308 ns.AddNamespaceEntry (this);
310 if ((parent != null) && (parent.NS != ns.Parent))
311 implicit_parent = new NamespaceEntry (parent, file, ns.Parent.Name, true, loc);
313 implicit_parent = parent;
315 this.FullName = ns.Name;
318 static int next_id = 0;
319 public readonly string FullName;
320 public readonly int ID;
321 public readonly bool IsImplicit;
323 public Namespace NS {
329 public NamespaceEntry Parent {
335 public NamespaceEntry ImplicitParent {
337 return implicit_parent;
341 public void DefineName (string name, IAlias o)
343 ns.DefineName (name, o);
347 /// Records a new namespace for resolving name references
349 public void Using (string ns, Location loc)
351 if (DeclarationFound){
352 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
359 if (using_clauses == null)
360 using_clauses = new ArrayList ();
362 foreach (UsingEntry old_entry in using_clauses){
363 if (old_entry.Name == ns){
364 Report.Warning (105, loc, "The using directive for '" + ns +
365 "' appeared previously in this namespace");
370 UsingEntry ue = new UsingEntry (this, ns, loc);
371 using_clauses.Add (ue);
374 public void UsingAlias (string name, TypeName alias, Location loc)
377 aliases = new Hashtable ();
379 if (aliases.Contains (name)){
380 Report.Error (1537, loc, "The using alias `" + name +
381 "' appeared previously in this namespace");
385 aliases [name] = new AliasEntry (this, name, alias, loc);
388 protected AliasEntry GetAliasEntry (string alias)
390 AliasEntry entry = null;
393 entry = (AliasEntry) aliases [alias];
394 if (entry == null && Parent != null)
395 entry = Parent.GetAliasEntry (alias);
400 public IAlias LookupAlias (string alias)
402 AliasEntry entry = GetAliasEntry (alias);
407 return entry.Resolve ();
410 public IAlias Lookup (DeclSpace ds, string name, int num_type_params,
411 bool silent, Location loc)
417 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
419 int pos = name.IndexOf ('.');
421 string first = name.Substring (0, pos);
422 string last = name.Substring (pos + 1);
424 o = Lookup (ds, first, 0, silent, loc);
430 o = ns.Lookup (ds, last, loc);
434 Type nested = TypeManager.LookupType (o.Name + "." + last);
435 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
438 return new TypeExpression (nested, loc);
442 // Check whether it's a namespace.
444 o = NS.Lookup (ds, name, loc);
451 AliasEntry entry = GetAliasEntry (name);
453 IAlias alias = entry.Resolve ();
458 if (name.IndexOf ('.') > 0)
462 // Check using entries.
464 IAlias t = null, match = null;
465 foreach (Namespace using_ns in GetUsingTable ()) {
466 match = using_ns.Lookup (ds, name, loc);
467 if ((match != null) && match.IsType){
470 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.Name, match.Name);
481 // Our cached computation.
482 Namespace [] namespace_using_table;
483 public Namespace[] GetUsingTable ()
485 if (namespace_using_table != null)
486 return namespace_using_table;
488 if (using_clauses == null)
489 return new Namespace [0];
491 ArrayList list = new ArrayList (using_clauses.Count);
493 foreach (UsingEntry ue in using_clauses) {
494 Namespace using_ns = ue.Resolve ();
495 if (using_ns == null)
501 namespace_using_table = new Namespace [list.Count];
502 list.CopyTo (namespace_using_table, 0);
503 return namespace_using_table;
506 public void DefineNamespace (SymbolWriter symwriter)
511 parent.DefineNamespace (symwriter);
514 if (using_clauses != null) {
515 using_list = new string [using_clauses.Count];
516 for (int i = 0; i < using_clauses.Count; i++)
517 using_list [i] = ((UsingEntry) using_clauses [i]).Name;
519 using_list = new string [0];
522 int parent_id = parent != null ? parent.symfile_id : 0;
523 symfile_id = symwriter.DefineNamespace (ns.Name, file, using_list, parent_id);
526 public int SymbolFileID {
532 static void Msgtry (string s)
534 Console.WriteLine (" Try using -r:" + s);
537 protected void error246 (Location loc, string name)
539 if (TypeManager.LookupType (name) != null)
540 Report.Error (138, loc, "The using keyword only lets you specify a namespace, " +
541 "`" + name + "' is a class not a namespace.");
543 Report.Error (246, loc, "The namespace `" + name +
544 "' can not be found (missing assembly reference?)");
547 case "Gtk": case "GtkSharp":
548 Msgtry ("gtk-sharp");
551 case "Gdk": case "GdkSharp":
552 Msgtry ("gdk-sharp");
555 case "Glade": case "GladeSharp":
556 Msgtry ("glade-sharp");
559 case "System.Drawing":
560 Msgtry ("System.Drawing");
563 case "System.Web.Services":
564 Msgtry ("System.Web.Services");
568 Msgtry ("System.Web");
572 Msgtry ("System.Data");
575 case "System.Windows.Forms":
576 Msgtry ("System.Windows.Forms");
583 /// Used to validate that all the using clauses are correct
584 /// after we are finished parsing all the files.
586 public void VerifyUsing ()
588 TypeContainer dummy = new TypeContainer ();
589 EmitContext ec = new EmitContext (
590 dummy, Location.Null, null, null, 0, false);
592 if (using_clauses != null){
593 foreach (UsingEntry ue in using_clauses){
594 if (ue.Resolve () != null)
597 error246 (ue.Location, ue.Name);
601 if (aliases != null){
602 foreach (DictionaryEntry de in aliases){
603 AliasEntry entry = (AliasEntry) de.Value;
605 IAlias alias = entry.Resolve ();
608 alias.Type.ResolveType (ec);
613 error246 (entry.Location, entry.Alias.ToString ());
618 public override string ToString ()
620 if (NS == Namespace.Root)
621 return "NamespaceEntry (<root>)";
623 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);