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 ();
118 if ((ds == null) || ds.CheckAccessLevel (t))
119 return new TypeExpression (t, Location.Null);
122 Namespace ns = GetNamespace (name, false);
126 t = TypeManager.LookupType (DeclSpace.MakeFQN (fullname, name));
127 if ((t == null) || ((ds != null) && !ds.CheckAccessLevel (t)))
130 return new TypeExpression (t, Location.Null);
133 public void AddNamespaceEntry (NamespaceEntry entry)
138 public void DefineName (string name, IAlias o)
140 defined_names.Add (name, o);
143 static public ArrayList UserDefinedNamespaces {
145 return all_namespaces;
150 /// The qualified name of the current namespace
158 public override string FullName {
165 /// The parent of this namespace, used by the parser to "Pop"
166 /// the current namespace declaration
168 public Namespace Parent {
174 public static void DefineNamespaces (SymbolWriter symwriter)
176 foreach (Namespace ns in all_namespaces) {
177 foreach (NamespaceEntry entry in ns.entries)
178 entry.DefineNamespace (symwriter);
183 /// Used to validate that all the using clauses are correct
184 /// after we are finished parsing all the files.
186 public static void VerifyUsing ()
188 foreach (Namespace ns in all_namespaces) {
189 foreach (NamespaceEntry entry in ns.entries)
190 entry.VerifyUsing ();
194 public override string ToString ()
197 return "Namespace (<root>)";
199 return String.Format ("Namespace ({0})", Name);
203 get { return false; }
206 TypeExpr IAlias.ResolveAsType (EmitContext ec)
208 throw new InvalidOperationException ();
212 public class NamespaceEntry
215 NamespaceEntry parent, implicit_parent;
219 ArrayList using_clauses;
220 public bool DeclarationFound = false;
223 // This class holds the location where a using definition is
224 // done, and whether it has been used by the program or not.
226 // We use this to flag using clauses for namespaces that do not
229 public class UsingEntry {
230 public readonly string Name;
231 public readonly NamespaceEntry NamespaceEntry;
232 public readonly Location Location;
234 public UsingEntry (NamespaceEntry entry, string name, Location loc)
237 NamespaceEntry = entry;
241 Namespace resolved_ns;
243 public Namespace Resolve ()
245 if (resolved_ns != null)
248 FullNamedExpression resolved = NamespaceEntry.LookupForUsing (Name, Location);
249 resolved_ns = resolved as Namespace;
254 public class AliasEntry {
255 public readonly string Name;
256 public readonly MemberName Alias;
257 public readonly NamespaceEntry NamespaceEntry;
258 public readonly Location Location;
260 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
264 NamespaceEntry = entry;
268 FullNamedExpression resolved;
270 public FullNamedExpression Resolve ()
272 if (resolved != null)
275 string alias = Alias.GetPartialName ();
277 resolved = NamespaceEntry.LookupForUsing (alias, Location);
282 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
283 : this (parent, file, name, false, loc)
286 protected NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, bool is_implicit, Location loc)
288 this.parent = parent;
290 this.IsImplicit = is_implicit;
293 if (!is_implicit && (parent != null))
294 ns = parent.NS.GetNamespace (name, true);
295 else if (name != null)
296 ns = Namespace.LookupNamespace (name, true);
299 ns.AddNamespaceEntry (this);
301 if ((parent != null) && (parent.NS != ns.Parent))
302 implicit_parent = new NamespaceEntry (parent, file, ns.Parent.Name, true, loc);
304 implicit_parent = parent;
306 this.FullName = ns.Name;
309 static int next_id = 0;
310 public readonly string FullName;
311 public readonly int ID;
312 public readonly bool IsImplicit;
314 public Namespace NS {
320 public NamespaceEntry Parent {
326 public NamespaceEntry ImplicitParent {
328 return implicit_parent;
332 public void DefineName (string name, IAlias o)
334 ns.DefineName (name, o);
338 /// Records a new namespace for resolving name references
340 public void Using (string ns, Location loc)
342 if (DeclarationFound){
343 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
350 if (using_clauses == null)
351 using_clauses = new ArrayList ();
353 foreach (UsingEntry old_entry in using_clauses) {
354 if (old_entry.Name == ns) {
355 if (RootContext.WarningLevel >= 3)
356 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", ns);
361 UsingEntry ue = new UsingEntry (this, ns, loc);
362 using_clauses.Add (ue);
365 public void UsingAlias (string name, MemberName alias, Location loc)
367 if (DeclarationFound){
368 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
373 aliases = new Hashtable ();
375 if (aliases.Contains (name)){
376 Report.Error (1537, loc, "The using alias `" + name +
377 "' appeared previously in this namespace");
381 aliases [name] = new AliasEntry (this, name, alias, loc);
384 public FullNamedExpression LookupAlias (string alias)
386 AliasEntry entry = null;
388 entry = (AliasEntry) aliases [alias];
390 return entry == null ? null : entry.Resolve ();
394 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
395 // resolved as if the immediately containing namespace body has no using-directives.
397 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
398 // in the using-namespace-directive.
400 public FullNamedExpression LookupForUsing (string dotted_name, Location loc)
402 int pos = dotted_name.IndexOf ('.');
403 string simple_name = dotted_name;
406 simple_name = dotted_name.Substring (0, pos);
407 rest = dotted_name.Substring (pos + 1);
410 FullNamedExpression o = NS.Lookup (null, simple_name, loc);
411 if (o == null && ImplicitParent != null)
412 o = ImplicitParent.LookupNamespaceOrType (null, simple_name, loc);
414 if (o == null || rest == null)
417 Namespace ns = o as Namespace;
419 return ns.Lookup (null, rest, loc);
421 Type nested = TypeManager.LookupType (o.FullName + "." + rest);
425 return new TypeExpression (nested, Location.Null);
428 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc)
430 FullNamedExpression resolved = null;
431 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
432 if ((resolved = curr_ns.Lookup (ds, name, loc)) != null)
438 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
440 FullNamedExpression o;
444 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
446 // FIXME: Remove this block. Only simple names should come here.
447 // The bug: The loop in LookupNamespaceOrType continues if
448 // the lookup for N succeeds but the nested lookup for I fails.
449 // This is one part of #52697.
451 int pos = name.IndexOf ('.');
453 string first = name.Substring (0, pos);
454 string last = name.Substring (pos + 1);
456 o = Lookup (ds, first, loc);
462 return ns.Lookup (ds, last, loc);
464 Type nested = TypeManager.LookupType (o.FullName + "." + last);
465 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
468 return new TypeExpression (nested, Location.Null);
472 // Check whether it's in the namespace.
474 o = NS.Lookup (ds, name, loc);
481 o = LookupAlias (name);
485 if (name.IndexOf ('.') > 0)
489 // Check using entries.
491 FullNamedExpression t = null, match = null;
492 foreach (Namespace using_ns in GetUsingTable ()) {
493 match = using_ns.Lookup (ds, name, loc);
494 if ((match != null) && (match is TypeExpr)) {
496 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.FullName, match.FullName);
507 // Our cached computation.
508 Namespace [] namespace_using_table;
509 public Namespace[] GetUsingTable ()
511 if (namespace_using_table != null)
512 return namespace_using_table;
514 if (using_clauses == null) {
515 namespace_using_table = new Namespace [0];
516 return namespace_using_table;
519 ArrayList list = new ArrayList (using_clauses.Count);
521 foreach (UsingEntry ue in using_clauses) {
522 Namespace using_ns = ue.Resolve ();
523 if (using_ns == null)
529 namespace_using_table = new Namespace [list.Count];
530 list.CopyTo (namespace_using_table, 0);
531 return namespace_using_table;
534 public void DefineNamespace (SymbolWriter symwriter)
539 parent.DefineNamespace (symwriter);
542 if (using_clauses != null) {
543 using_list = new string [using_clauses.Count];
544 for (int i = 0; i < using_clauses.Count; i++)
545 using_list [i] = ((UsingEntry) using_clauses [i]).Name;
547 using_list = new string [0];
550 int parent_id = parent != null ? parent.symfile_id : 0;
551 if (file.SourceFileEntry == null)
554 symfile_id = symwriter.DefineNamespace (
555 ns.Name, file.SourceFileEntry, using_list, parent_id);
558 public int SymbolFileID {
564 static void MsgtryRef (string s)
566 Console.WriteLine (" Try using -r:" + s);
569 static void MsgtryPkg (string s)
571 Console.WriteLine (" Try using -pkg:" + s);
574 protected void error246 (Location loc, string name)
576 Report.Error (246, loc, "The namespace `" + name +
577 "' can not be found (missing assembly reference?)");
580 case "Gtk": case "GtkSharp":
581 MsgtryPkg ("gtk-sharp");
584 case "Gdk": case "GdkSharp":
585 MsgtryPkg ("gdk-sharp");
588 case "Glade": case "GladeSharp":
589 MsgtryPkg ("glade-sharp");
592 case "System.Drawing":
593 case "System.Web.Services":
596 case "System.Windows.Forms":
603 /// Used to validate that all the using clauses are correct
604 /// after we are finished parsing all the files.
606 public void VerifyUsing ()
608 if (using_clauses != null){
609 foreach (UsingEntry ue in using_clauses){
610 if (ue.Resolve () != null)
613 if (LookupForUsing (ue.Name, ue.Location) == null)
614 error246 (ue.Location, ue.Name);
616 Report.Error (138, ue.Location, "The using keyword only lets you specify a namespace, " +
617 "`" + ue.Name + "' is a class not a namespace.");
622 if (aliases != null){
623 foreach (DictionaryEntry de in aliases){
624 AliasEntry alias = (AliasEntry) de.Value;
626 if (alias.Resolve () != null)
629 error246 (alias.Location, alias.Alias.GetPartialName ());
634 public override string ToString ()
636 if (NS == Namespace.Root)
637 return "NamespaceEntry (<root>)";
639 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);