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, Location);
275 NamespaceEntry curr_ns = NamespaceEntry.Parent;
277 while ((curr_ns != null) && (resolved == null)) {
278 resolved = curr_ns.Lookup (
279 null, alias, Alias.CountTypeArguments,
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 foreach (UsingEntry old_entry in using_clauses) {
369 if (old_entry.Name == ns) {
370 if (RootContext.WarningLevel >= 3)
371 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", ns);
376 UsingEntry ue = new UsingEntry (this, ns, loc);
377 using_clauses.Add (ue);
380 public void UsingAlias (string name, MemberName alias, Location loc)
382 if (DeclarationFound){
383 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
388 aliases = new Hashtable ();
390 if (aliases.Contains (name)){
391 Report.Error (1537, loc, "The using alias `" + name +
392 "' appeared previously in this namespace");
396 aliases [name] = new AliasEntry (this, name, alias, loc);
399 protected AliasEntry GetAliasEntry (string alias)
401 AliasEntry entry = null;
404 entry = (AliasEntry) aliases [alias];
405 if (entry == null && Parent != null)
406 entry = Parent.GetAliasEntry (alias);
411 public IAlias LookupAlias (string alias)
413 AliasEntry entry = GetAliasEntry (alias);
418 return entry.Resolve ();
421 public IAlias Lookup (DeclSpace ds, string name, int num_type_params,
422 bool ignore_using, Location loc)
428 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
430 int pos = name.IndexOf ('.');
432 string first = name.Substring (0, pos);
433 string last = name.Substring (pos + 1);
435 o = Lookup (ds, first, 0, ignore_using, loc);
441 o = ns.Lookup (ds, last, loc);
445 Type nested = TypeManager.LookupType (o.Name + "." + last);
446 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
449 return new TypeExpression (nested, loc);
453 // Check whether it's a namespace.
455 o = NS.Lookup (ds, name, loc);
465 AliasEntry entry = GetAliasEntry (name);
467 IAlias alias = entry.Resolve ();
472 if (name.IndexOf ('.') > 0)
476 // Check using entries.
478 IAlias t = null, match = null;
479 foreach (Namespace using_ns in GetUsingTable ()) {
480 match = using_ns.Lookup (ds, name, loc);
481 if ((match != null) && match.IsType){
483 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.Name, match.Name);
494 // Our cached computation.
495 Namespace [] namespace_using_table;
496 public Namespace[] GetUsingTable ()
498 if (namespace_using_table != null)
499 return namespace_using_table;
501 if (using_clauses == null)
502 return new Namespace [0];
504 ArrayList list = new ArrayList (using_clauses.Count);
506 foreach (UsingEntry ue in using_clauses) {
507 Namespace using_ns = ue.Resolve ();
508 if (using_ns == null)
514 namespace_using_table = new Namespace [list.Count];
515 list.CopyTo (namespace_using_table, 0);
516 return namespace_using_table;
519 public void DefineNamespace (SymbolWriter symwriter)
524 parent.DefineNamespace (symwriter);
527 if (using_clauses != null) {
528 using_list = new string [using_clauses.Count];
529 for (int i = 0; i < using_clauses.Count; i++)
530 using_list [i] = ((UsingEntry) using_clauses [i]).Name;
532 using_list = new string [0];
535 int parent_id = parent != null ? parent.symfile_id : 0;
536 if (file.SourceFileEntry == null)
539 symfile_id = symwriter.DefineNamespace (
540 ns.Name, file.SourceFileEntry, using_list, parent_id);
543 public int SymbolFileID {
549 static void Msgtry (string s)
551 Console.WriteLine (" Try using -r:" + s);
554 protected void error246 (Location loc, string name)
556 if (TypeManager.LookupType (name) != null)
557 Report.Error (138, loc, "The using keyword only lets you specify a namespace, " +
558 "`" + name + "' is a class not a namespace.");
560 Report.Error (246, loc, "The namespace `" + name +
561 "' can not be found (missing assembly reference?)");
564 case "Gtk": case "GtkSharp":
565 Msgtry ("gtk-sharp");
568 case "Gdk": case "GdkSharp":
569 Msgtry ("gdk-sharp");
572 case "Glade": case "GladeSharp":
573 Msgtry ("glade-sharp");
576 case "System.Drawing":
577 Msgtry ("System.Drawing");
580 case "System.Web.Services":
581 Msgtry ("System.Web.Services");
585 Msgtry ("System.Web");
589 Msgtry ("System.Data");
592 case "System.Windows.Forms":
593 Msgtry ("System.Windows.Forms");
600 /// Used to validate that all the using clauses are correct
601 /// after we are finished parsing all the files.
603 public void VerifyUsing ()
605 TypeContainer dummy = new RootTypes ();
606 EmitContext ec = new EmitContext (
607 dummy, Location.Null, null, null, 0, false);
609 if (using_clauses != null){
610 foreach (UsingEntry ue in using_clauses){
611 if (ue.Resolve () != null)
614 error246 (ue.Location, ue.Name);
618 if (aliases != null){
619 foreach (DictionaryEntry de in aliases){
620 AliasEntry entry = (AliasEntry) de.Value;
622 IAlias alias = entry.Resolve ();
625 alias.Type.ResolveType (ec);
630 error246 (entry.Location, entry.Alias.GetTypeName ());
635 public override string ToString ()
637 if (NS == Namespace.Root)
638 return "NamespaceEntry (<root>)";
640 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);