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.ResolveAsType (EmitContext ec)
189 throw new InvalidOperationException ();
193 public class NamespaceEntry
196 NamespaceEntry parent, implicit_parent;
200 ArrayList using_clauses;
201 public bool DeclarationFound = false;
204 // This class holds the location where a using definition is
205 // done, and whether it has been used by the program or not.
207 // We use this to flag using clauses for namespaces that do not
210 public class UsingEntry {
211 public readonly string Name;
212 public readonly NamespaceEntry NamespaceEntry;
213 public readonly Location Location;
215 public UsingEntry (NamespaceEntry entry, string name, Location loc)
218 NamespaceEntry = entry;
222 Namespace resolved_ns;
224 public Namespace Resolve ()
226 if (resolved_ns != null)
229 Namespace curr_ns = NamespaceEntry.NS;
230 while ((curr_ns != null) && (resolved_ns == null)) {
231 resolved_ns = curr_ns.GetNamespace (Name, false);
233 if (resolved_ns == null)
234 curr_ns = curr_ns.Parent;
241 public class AliasEntry {
242 public readonly string Name;
243 public readonly MemberName Alias;
244 public readonly NamespaceEntry NamespaceEntry;
245 public readonly Location Location;
247 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
251 NamespaceEntry = entry;
257 public IAlias Resolve ()
259 if (resolved != null)
263 // GENERICS: Cope with the expression and not with the string
264 // this will fail with `using A = Stack<int>'
267 string alias = Alias.GetTypeName ();
269 // According to section 16.3.1, the namespace-or-type-name is resolved
270 // as if the immediately containing namespace body has no using-directives.
271 resolved = NamespaceEntry.Lookup (
272 null, alias, Alias.CountTypeArguments, true, Location);
274 NamespaceEntry curr_ns = NamespaceEntry.Parent;
276 while ((curr_ns != null) && (resolved == null)) {
277 resolved = curr_ns.Lookup (
278 null, alias, Alias.CountTypeArguments,
281 if (resolved == null)
282 curr_ns = curr_ns.Parent;
285 if (resolved == null)
289 resolved = new TypeAliasExpression (
290 resolved, Alias.TypeArguments, Location);
296 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
297 : this (parent, file, name, false, loc)
300 protected NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, bool is_implicit, Location loc)
302 this.parent = parent;
304 this.IsImplicit = is_implicit;
307 if (!is_implicit && (parent != null))
308 ns = parent.NS.GetNamespace (name, true);
309 else if (name != null)
310 ns = Namespace.LookupNamespace (name, true);
313 ns.AddNamespaceEntry (this);
315 if ((parent != null) && (parent.NS != ns.Parent))
316 implicit_parent = new NamespaceEntry (parent, file, ns.Parent.Name, true, loc);
318 implicit_parent = parent;
320 this.FullName = ns.Name;
323 static int next_id = 0;
324 public readonly string FullName;
325 public readonly int ID;
326 public readonly bool IsImplicit;
328 public Namespace NS {
334 public NamespaceEntry Parent {
340 public NamespaceEntry ImplicitParent {
342 return implicit_parent;
346 public void DefineName (string name, IAlias o)
348 ns.DefineName (name, o);
352 /// Records a new namespace for resolving name references
354 public void Using (string ns, Location loc)
356 if (DeclarationFound){
357 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
364 if (using_clauses == null)
365 using_clauses = new ArrayList ();
367 foreach (UsingEntry old_entry in using_clauses) {
368 if (old_entry.Name == ns) {
369 if (RootContext.WarningLevel >= 3)
370 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", ns);
375 UsingEntry ue = new UsingEntry (this, ns, loc);
376 using_clauses.Add (ue);
379 public void UsingAlias (string name, MemberName alias, Location loc)
381 if (DeclarationFound){
382 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
387 aliases = new Hashtable ();
389 if (aliases.Contains (name)){
390 Report.Error (1537, loc, "The using alias `" + name +
391 "' appeared previously in this namespace");
395 aliases [name] = new AliasEntry (this, name, alias, loc);
398 protected AliasEntry GetAliasEntry (string alias)
400 AliasEntry entry = null;
403 entry = (AliasEntry) aliases [alias];
404 if (entry == null && Parent != null)
405 entry = Parent.GetAliasEntry (alias);
410 public IAlias LookupAlias (string alias)
412 AliasEntry entry = GetAliasEntry (alias);
417 return entry.Resolve ();
420 public IAlias Lookup (DeclSpace ds, string name, int num_type_params,
421 bool ignore_using, Location loc)
427 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
429 int pos = name.IndexOf ('.');
431 string first = name.Substring (0, pos);
432 string last = name.Substring (pos + 1);
434 o = Lookup (ds, first, 0, ignore_using, loc);
440 o = ns.Lookup (ds, last, loc);
444 Type nested = TypeManager.LookupType (o.Name + "." + last);
445 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
448 return new TypeExpression (nested, loc);
452 // Check whether it's a namespace.
454 o = NS.Lookup (ds, name, loc);
464 AliasEntry entry = GetAliasEntry (name);
466 IAlias alias = entry.Resolve ();
471 if (name.IndexOf ('.') > 0)
475 // Check using entries.
477 IAlias t = null, match = null;
478 foreach (Namespace using_ns in GetUsingTable ()) {
479 match = using_ns.Lookup (ds, name, loc);
480 if ((match != null) && match.IsType){
482 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.Name, match.Name);
493 // Our cached computation.
494 Namespace [] namespace_using_table;
495 public Namespace[] GetUsingTable ()
497 if (namespace_using_table != null)
498 return namespace_using_table;
500 if (using_clauses == null)
501 return new Namespace [0];
503 ArrayList list = new ArrayList (using_clauses.Count);
505 foreach (UsingEntry ue in using_clauses) {
506 Namespace using_ns = ue.Resolve ();
507 if (using_ns == null)
513 namespace_using_table = new Namespace [list.Count];
514 list.CopyTo (namespace_using_table, 0);
515 return namespace_using_table;
518 public void DefineNamespace (SymbolWriter symwriter)
523 parent.DefineNamespace (symwriter);
526 if (using_clauses != null) {
527 using_list = new string [using_clauses.Count];
528 for (int i = 0; i < using_clauses.Count; i++)
529 using_list [i] = ((UsingEntry) using_clauses [i]).Name;
531 using_list = new string [0];
534 int parent_id = parent != null ? parent.symfile_id : 0;
535 if (file.SourceFileEntry == null)
538 symfile_id = symwriter.DefineNamespace (
539 ns.Name, file.SourceFileEntry, using_list, parent_id);
542 public int SymbolFileID {
548 static void MsgtryRef (string s)
550 Console.WriteLine (" Try using -r:" + s);
553 static void MsgtryPkg (string s)
555 Console.WriteLine (" Try using -pkg:" + s);
558 protected void error246 (Location loc, string name)
560 if (TypeManager.LookupType (name) != null)
561 Report.Error (138, loc, "The using keyword only lets you specify a namespace, " +
562 "`" + name + "' is a class not a namespace.");
564 Report.Error (246, loc, "The namespace `" + name +
565 "' can not be found (missing assembly reference?)");
568 case "Gtk": case "GtkSharp":
569 MsgtryPkg ("gtk-sharp");
572 case "Gdk": case "GdkSharp":
573 MsgtryPkg ("gdk-sharp");
576 case "Glade": case "GladeSharp":
577 MsgtryPkg ("glade-sharp");
580 case "System.Drawing":
581 MsgtryRef ("System.Drawing");
584 case "System.Web.Services":
585 MsgtryRef ("System.Web.Services");
589 MsgtryRef ("System.Web");
593 MsgtryRef ("System.Data");
596 case "System.Windows.Forms":
597 MsgtryRef ("System.Windows.Forms");
604 /// Used to validate that all the using clauses are correct
605 /// after we are finished parsing all the files.
607 public void VerifyUsing ()
609 TypeContainer dummy = new RootTypes ();
610 EmitContext ec = new EmitContext (
611 dummy, Location.Null, null, null, 0, false);
613 if (using_clauses != null){
614 foreach (UsingEntry ue in using_clauses){
615 if (ue.Resolve () != null)
618 error246 (ue.Location, ue.Name);
622 if (aliases != null){
623 foreach (DictionaryEntry de in aliases){
624 AliasEntry entry = (AliasEntry) de.Value;
626 IAlias alias = entry.Resolve ();
629 alias.ResolveAsType (ec);
634 error246 (entry.Location, entry.Alias.GetTypeName ());
639 public override string ToString ()
641 if (NS == Namespace.Root)
642 return "NamespaceEntry (<root>)";
644 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);