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 /// This is an Expression to allow it to be referenced in the
18 /// compiler parse/intermediate tree during name resolution.
20 public class Namespace : FullNamedExpression, IAlias {
21 static ArrayList all_namespaces = new ArrayList ();
22 static Hashtable namespaces_map = new Hashtable ();
28 Hashtable defined_names;
31 /// Constructor Takes the current namespace and the
32 /// name. This is bootstrapped with parent == null
35 public Namespace (Namespace parent, string name)
37 // Expression members.
38 this.eclass = ExprClass.Namespace;
40 this.loc = Location.Null;
44 string pname = parent != null ? parent.Name : "";
49 fullname = parent.Name + "." + name;
51 entries = new ArrayList ();
52 namespaces = new Hashtable ();
53 defined_names = new Hashtable ();
55 all_namespaces.Add (this);
56 if (namespaces_map.Contains (fullname))
58 namespaces_map [fullname] = true;
61 public override Expression DoResolve (EmitContext ec)
66 public override void Emit (EmitContext ec)
68 throw new InternalErrorException ("Expression tree referenced namespace " + fullname + " during Emit ()");
71 public static bool IsNamespace (string name)
73 return namespaces_map [name] != null;
76 public static Namespace Root = new Namespace (null, "");
78 public Namespace GetNamespace (string name, bool create)
80 int pos = name.IndexOf ('.');
85 first = name.Substring (0, pos);
89 ns = (Namespace) namespaces [first];
94 ns = new Namespace (this, first);
95 namespaces.Add (first, ns);
99 ns = ns.GetNamespace (name.Substring (pos + 1), create);
104 public static Namespace LookupNamespace (string name, bool create)
106 return Root.GetNamespace (name, create);
109 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
111 IAlias o = (IAlias) defined_names [name];
114 DeclSpace tdecl = o as DeclSpace;
116 t = tdecl.DefineType ();
120 if ((ds == null) || ds.CheckAccessLevel (t))
121 return new TypeExpression (t, Location.Null);
124 Namespace ns = GetNamespace (name, false);
128 t = TypeManager.LookupType (DeclSpace.MakeFQN (fullname, name));
129 if ((t == null) || ((ds != null) && !ds.CheckAccessLevel (t)))
132 return new TypeExpression (t, Location.Null);
135 public void AddNamespaceEntry (NamespaceEntry entry)
140 public void DefineName (string name, IAlias o)
142 defined_names.Add (name, o);
145 static public ArrayList UserDefinedNamespaces {
147 return all_namespaces;
152 /// The qualified name of the current namespace
160 public override string FullName {
167 /// The parent of this namespace, used by the parser to "Pop"
168 /// the current namespace declaration
170 public Namespace Parent {
176 public static void DefineNamespaces (SymbolWriter symwriter)
178 foreach (Namespace ns in all_namespaces) {
179 foreach (NamespaceEntry entry in ns.entries)
180 entry.DefineNamespace (symwriter);
185 /// Used to validate that all the using clauses are correct
186 /// after we are finished parsing all the files.
188 public static void VerifyUsing ()
190 foreach (Namespace ns in all_namespaces) {
191 foreach (NamespaceEntry entry in ns.entries)
192 entry.VerifyUsing ();
196 public override string ToString ()
199 return "Namespace (<root>)";
201 return String.Format ("Namespace ({0})", Name);
205 get { return false; }
208 TypeExpr IAlias.ResolveAsType (EmitContext ec)
210 throw new InvalidOperationException ();
214 public class NamespaceEntry
217 NamespaceEntry parent, implicit_parent;
221 ArrayList using_clauses;
222 public bool DeclarationFound = false;
225 // This class holds the location where a using definition is
226 // done, and whether it has been used by the program or not.
228 // We use this to flag using clauses for namespaces that do not
231 public class UsingEntry {
232 public readonly string Name;
233 public readonly NamespaceEntry NamespaceEntry;
234 public readonly Location Location;
236 public UsingEntry (NamespaceEntry entry, string name, Location loc)
239 NamespaceEntry = entry;
243 Namespace resolved_ns;
245 public Namespace Resolve ()
247 if (resolved_ns != null)
250 FullNamedExpression resolved = NamespaceEntry.LookupForUsing (Name, Location);
251 resolved_ns = resolved as Namespace;
256 public class AliasEntry {
257 public readonly string Name;
258 public readonly MemberName Alias;
259 public readonly NamespaceEntry NamespaceEntry;
260 public readonly Location Location;
262 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
266 NamespaceEntry = entry;
270 FullNamedExpression resolved;
272 public FullNamedExpression Resolve ()
274 if (resolved != null)
278 // GENERICS: Cope with the expression and not with the string
279 // this will fail with `using A = Stack<int>'
282 string alias = Alias.GetTypeName ();
284 resolved = NamespaceEntry.LookupForUsing (alias, Location);
285 if (resolved == null)
288 if (Alias.TypeArguments == null)
291 EmitContext ec = RootContext.Tree.Types.EmitContext;
292 resolved = new TypeAliasExpression (resolved, Alias.TypeArguments, Location);
293 resolved = resolved.ResolveAsTypeStep (ec);
299 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
300 : this (parent, file, name, false, loc)
303 protected NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, bool is_implicit, Location loc)
305 this.parent = parent;
307 this.IsImplicit = is_implicit;
310 if (!is_implicit && (parent != null))
311 ns = parent.NS.GetNamespace (name, true);
312 else if (name != null)
313 ns = Namespace.LookupNamespace (name, true);
316 ns.AddNamespaceEntry (this);
318 if ((parent != null) && (parent.NS != ns.Parent))
319 implicit_parent = new NamespaceEntry (parent, file, ns.Parent.Name, true, loc);
321 implicit_parent = parent;
323 this.FullName = ns.Name;
326 static int next_id = 0;
327 public readonly string FullName;
328 public readonly int ID;
329 public readonly bool IsImplicit;
331 public Namespace NS {
337 public NamespaceEntry Parent {
343 public NamespaceEntry ImplicitParent {
345 return implicit_parent;
349 public void DefineName (string name, IAlias o)
351 ns.DefineName (name, o);
355 /// Records a new namespace for resolving name references
357 public void Using (string ns, Location loc)
359 if (DeclarationFound){
360 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
367 if (using_clauses == null)
368 using_clauses = new ArrayList ();
370 foreach (UsingEntry old_entry in using_clauses) {
371 if (old_entry.Name == ns) {
372 if (RootContext.WarningLevel >= 3)
373 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", ns);
378 UsingEntry ue = new UsingEntry (this, ns, loc);
379 using_clauses.Add (ue);
382 public void UsingAlias (string name, MemberName alias, Location loc)
384 if (DeclarationFound){
385 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
390 aliases = new Hashtable ();
392 if (aliases.Contains (name)){
393 Report.Error (1537, loc, "The using alias `" + name +
394 "' appeared previously in this namespace");
398 aliases [name] = new AliasEntry (this, name, alias, loc);
401 public FullNamedExpression LookupAlias (string alias)
403 AliasEntry entry = null;
405 entry = (AliasEntry) aliases [alias];
407 return entry == null ? null : entry.Resolve ();
411 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
412 // resolved as if the immediately containing namespace body has no using-directives.
414 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
415 // in the using-namespace-directive.
417 public FullNamedExpression LookupForUsing (string dotted_name, Location loc)
419 int pos = dotted_name.IndexOf ('.');
420 string simple_name = dotted_name;
423 simple_name = dotted_name.Substring (0, pos);
424 rest = dotted_name.Substring (pos + 1);
427 FullNamedExpression o = NS.Lookup (null, simple_name, loc);
428 if (o == null && ImplicitParent != null)
429 o = ImplicitParent.LookupNamespaceOrType (null, simple_name, loc);
431 if (o == null || rest == null)
434 Namespace ns = o as Namespace;
436 return ns.Lookup (null, rest, loc);
438 Type nested = TypeManager.LookupType (o.FullName + "." + rest);
442 return new TypeExpression (nested, Location.Null);
445 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc)
447 FullNamedExpression resolved = null;
448 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
449 if ((resolved = curr_ns.Lookup (ds, name, loc)) != null)
455 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
457 FullNamedExpression o;
461 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
463 // FIXME: Remove this block. Only simple names should come here.
464 // The bug: The loop in LookupNamespaceOrType continues if
465 // the lookup for N succeeds but the nested lookup for I fails.
466 // This is one part of #52697.
468 int pos = name.IndexOf ('.');
470 string first = name.Substring (0, pos);
471 string last = name.Substring (pos + 1);
473 o = Lookup (ds, first, loc);
479 o = ns.Lookup (ds, last, loc);
483 Type nested = TypeManager.LookupType (o.FullName + "." + last);
484 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
487 return new TypeExpression (nested, Location.Null);
491 // Check whether it's in the namespace.
493 o = NS.Lookup (ds, name, loc);
500 o = LookupAlias (name);
504 if (name.IndexOf ('.') > 0)
508 // Check using entries.
510 FullNamedExpression t = null, match = null;
511 foreach (Namespace using_ns in GetUsingTable ()) {
512 match = using_ns.Lookup (ds, name, loc);
513 if ((match != null) && (match is TypeExpr)) {
515 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.FullName, match.FullName);
526 // Our cached computation.
527 Namespace [] namespace_using_table;
528 public Namespace[] GetUsingTable ()
530 if (namespace_using_table != null)
531 return namespace_using_table;
533 if (using_clauses == null) {
534 namespace_using_table = new Namespace [0];
535 return namespace_using_table;
538 ArrayList list = new ArrayList (using_clauses.Count);
540 foreach (UsingEntry ue in using_clauses) {
541 Namespace using_ns = ue.Resolve ();
542 if (using_ns == null)
548 namespace_using_table = new Namespace [list.Count];
549 list.CopyTo (namespace_using_table, 0);
550 return namespace_using_table;
553 public void DefineNamespace (SymbolWriter symwriter)
558 parent.DefineNamespace (symwriter);
561 if (using_clauses != null) {
562 using_list = new string [using_clauses.Count];
563 for (int i = 0; i < using_clauses.Count; i++)
564 using_list [i] = ((UsingEntry) using_clauses [i]).Name;
566 using_list = new string [0];
569 int parent_id = parent != null ? parent.symfile_id : 0;
570 if (file.SourceFileEntry == null)
573 symfile_id = symwriter.DefineNamespace (
574 ns.Name, file.SourceFileEntry, using_list, parent_id);
577 public int SymbolFileID {
583 static void MsgtryRef (string s)
585 Console.WriteLine (" Try using -r:" + s);
588 static void MsgtryPkg (string s)
590 Console.WriteLine (" Try using -pkg:" + s);
593 protected void error246 (Location loc, string name)
595 Report.Error (246, loc, "The namespace `" + name +
596 "' can not be found (missing assembly reference?)");
599 case "Gtk": case "GtkSharp":
600 MsgtryPkg ("gtk-sharp");
603 case "Gdk": case "GdkSharp":
604 MsgtryPkg ("gdk-sharp");
607 case "Glade": case "GladeSharp":
608 MsgtryPkg ("glade-sharp");
611 case "System.Drawing":
612 case "System.Web.Services":
615 case "System.Windows.Forms":
622 /// Used to validate that all the using clauses are correct
623 /// after we are finished parsing all the files.
625 public void VerifyUsing ()
627 if (using_clauses != null){
628 foreach (UsingEntry ue in using_clauses){
629 if (ue.Resolve () != null)
632 if (LookupForUsing (ue.Name, ue.Location) == null)
633 error246 (ue.Location, ue.Name);
635 Report.Error (138, ue.Location, "The using keyword only lets you specify a namespace, " +
636 "`" + ue.Name + "' is a class not a namespace.");
641 if (aliases != null){
642 foreach (DictionaryEntry de in aliases){
643 AliasEntry alias = (AliasEntry) de.Value;
645 if (alias.Resolve () != null)
648 error246 (alias.Location, alias.Alias.GetTypeName ());
653 public override string ToString ()
655 if (NS == Namespace.Root)
656 return "NamespaceEntry (<root>)";
658 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);