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 MemberName Alias;
245 public readonly NamespaceEntry NamespaceEntry;
246 public readonly Location Location;
248 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
252 NamespaceEntry = entry;
258 public IAlias Resolve ()
260 if (resolved != null)
264 // GENERICS: Cope with the expression and not with the string
265 // this will fail with `using A = Stack<int>'
268 string alias = Alias.GetTypeName ();
270 // According to section 16.3.1, the namespace-or-type-name is resolved
271 // as if the immediately containing namespace body has no using-directives.
272 resolved = NamespaceEntry.Lookup (
273 null, alias, Alias.CountTypeArguments, true, false, Location);
275 NamespaceEntry curr_ns = NamespaceEntry.Parent;
277 while ((curr_ns != null) && (resolved == null)) {
278 resolved = curr_ns.Lookup (
279 null, alias, Alias.CountTypeArguments,
280 false, false, Location);
282 if (resolved == null)
283 curr_ns = curr_ns.Parent;
286 if (resolved == null)
290 resolved = new TypeAliasExpression (
291 resolved.Type, Alias.TypeArguments, Location);
297 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
298 : this (parent, file, name, false, loc)
301 protected NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, bool is_implicit, Location loc)
303 this.parent = parent;
305 this.IsImplicit = is_implicit;
308 if (!is_implicit && (parent != null))
309 ns = parent.NS.GetNamespace (name, true);
310 else if (name != null)
311 ns = Namespace.LookupNamespace (name, true);
314 ns.AddNamespaceEntry (this);
316 if ((parent != null) && (parent.NS != ns.Parent))
317 implicit_parent = new NamespaceEntry (parent, file, ns.Parent.Name, true, loc);
319 implicit_parent = parent;
321 this.FullName = ns.Name;
324 static int next_id = 0;
325 public readonly string FullName;
326 public readonly int ID;
327 public readonly bool IsImplicit;
329 public Namespace NS {
335 public NamespaceEntry Parent {
341 public NamespaceEntry ImplicitParent {
343 return implicit_parent;
347 public void DefineName (string name, IAlias o)
349 ns.DefineName (name, o);
353 /// Records a new namespace for resolving name references
355 public void Using (string ns, Location loc)
357 if (DeclarationFound){
358 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
365 if (using_clauses == null)
366 using_clauses = new ArrayList ();
368 if (RootContext.WarningLevel >= 3) {
369 foreach (UsingEntry old_entry in using_clauses){
370 if (old_entry.Name == ns){
371 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", ns);
377 UsingEntry ue = new UsingEntry (this, ns, loc);
378 using_clauses.Add (ue);
381 public void UsingAlias (string name, MemberName alias, Location loc)
383 if (DeclarationFound){
384 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
389 aliases = new Hashtable ();
391 if (aliases.Contains (name)){
392 Report.Error (1537, loc, "The using alias `" + name +
393 "' appeared previously in this namespace");
397 aliases [name] = new AliasEntry (this, name, alias, loc);
400 protected AliasEntry GetAliasEntry (string alias)
402 AliasEntry entry = null;
405 entry = (AliasEntry) aliases [alias];
406 if (entry == null && Parent != null)
407 entry = Parent.GetAliasEntry (alias);
412 public IAlias LookupAlias (string alias)
414 AliasEntry entry = GetAliasEntry (alias);
419 return entry.Resolve ();
422 public IAlias Lookup (DeclSpace ds, string name, int num_type_params,
423 bool ignore_using, bool silent, Location loc)
429 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
431 int pos = name.IndexOf ('.');
433 string first = name.Substring (0, pos);
434 string last = name.Substring (pos + 1);
436 o = Lookup (ds, first, 0, ignore_using, silent, loc);
442 o = ns.Lookup (ds, last, loc);
446 Type nested = TypeManager.LookupType (o.Name + "." + last);
447 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
450 return new TypeExpression (nested, loc);
454 // Check whether it's a namespace.
456 o = NS.Lookup (ds, name, loc);
466 AliasEntry entry = GetAliasEntry (name);
468 IAlias alias = entry.Resolve ();
473 if (name.IndexOf ('.') > 0)
477 // Check using entries.
479 IAlias t = null, match = null;
480 foreach (Namespace using_ns in GetUsingTable ()) {
481 match = using_ns.Lookup (ds, name, loc);
482 if ((match != null) && match.IsType){
485 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.Name, match.Name);
496 // Our cached computation.
497 Namespace [] namespace_using_table;
498 public Namespace[] GetUsingTable ()
500 if (namespace_using_table != null)
501 return namespace_using_table;
503 if (using_clauses == null)
504 return new Namespace [0];
506 ArrayList list = new ArrayList (using_clauses.Count);
508 foreach (UsingEntry ue in using_clauses) {
509 Namespace using_ns = ue.Resolve ();
510 if (using_ns == null)
516 namespace_using_table = new Namespace [list.Count];
517 list.CopyTo (namespace_using_table, 0);
518 return namespace_using_table;
521 public void DefineNamespace (SymbolWriter symwriter)
526 parent.DefineNamespace (symwriter);
529 if (using_clauses != null) {
530 using_list = new string [using_clauses.Count];
531 for (int i = 0; i < using_clauses.Count; i++)
532 using_list [i] = ((UsingEntry) using_clauses [i]).Name;
534 using_list = new string [0];
537 int parent_id = parent != null ? parent.symfile_id : 0;
538 if (file.SourceFileEntry == null)
541 symfile_id = symwriter.DefineNamespace (
542 ns.Name, file.SourceFileEntry, using_list, parent_id);
545 public int SymbolFileID {
551 static void Msgtry (string s)
553 Console.WriteLine (" Try using -r:" + s);
556 protected void error246 (Location loc, string name)
558 if (TypeManager.LookupType (name) != null)
559 Report.Error (138, loc, "The using keyword only lets you specify a namespace, " +
560 "`" + name + "' is a class not a namespace.");
562 Report.Error (246, loc, "The namespace `" + name +
563 "' can not be found (missing assembly reference?)");
566 case "Gtk": case "GtkSharp":
567 Msgtry ("gtk-sharp");
570 case "Gdk": case "GdkSharp":
571 Msgtry ("gdk-sharp");
574 case "Glade": case "GladeSharp":
575 Msgtry ("glade-sharp");
578 case "System.Drawing":
579 Msgtry ("System.Drawing");
582 case "System.Web.Services":
583 Msgtry ("System.Web.Services");
587 Msgtry ("System.Web");
591 Msgtry ("System.Data");
594 case "System.Windows.Forms":
595 Msgtry ("System.Windows.Forms");
602 /// Used to validate that all the using clauses are correct
603 /// after we are finished parsing all the files.
605 public void VerifyUsing ()
607 TypeContainer dummy = new RootTypes ();
608 EmitContext ec = new EmitContext (
609 dummy, Location.Null, null, null, 0, false);
611 if (using_clauses != null){
612 foreach (UsingEntry ue in using_clauses){
613 if (ue.Resolve () != null)
616 error246 (ue.Location, ue.Name);
620 if (aliases != null){
621 foreach (DictionaryEntry de in aliases){
622 AliasEntry entry = (AliasEntry) de.Value;
624 IAlias alias = entry.Resolve ();
627 alias.Type.ResolveType (ec);
632 error246 (entry.Location, entry.Alias.ToString ());
637 public override string ToString ()
639 if (NS == Namespace.Root)
640 return "NamespaceEntry (<root>)";
642 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);