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 Expression Name;
231 public readonly NamespaceEntry NamespaceEntry;
232 public readonly Location Location;
234 public UsingEntry (NamespaceEntry entry, Expression name, Location loc)
237 NamespaceEntry = entry;
241 internal FullNamedExpression resolved;
243 public Namespace Resolve ()
245 if (resolved != null)
246 return resolved as Namespace;
248 DeclSpace root = RootContext.Tree.Types;
249 root.NamespaceEntry = NamespaceEntry;
250 resolved = Name.ResolveAsTypeStep (root.EmitContext);
251 root.NamespaceEntry = null;
253 return resolved as Namespace;
257 public class AliasEntry {
258 public readonly string Name;
259 public readonly Expression Alias;
260 public readonly NamespaceEntry NamespaceEntry;
261 public readonly Location Location;
263 public AliasEntry (NamespaceEntry entry, string name, Expression alias, Location loc)
267 NamespaceEntry = entry;
271 FullNamedExpression resolved;
273 public FullNamedExpression Resolve ()
275 if (resolved != null)
278 DeclSpace root = RootContext.Tree.Types;
279 root.NamespaceEntry = NamespaceEntry;
280 resolved = Alias.ResolveAsTypeStep (root.EmitContext);
281 root.NamespaceEntry = null;
287 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
289 this.parent = parent;
291 this.IsImplicit = false;
295 ns = parent.NS.GetNamespace (name, true);
296 else if (name != null)
297 ns = Namespace.LookupNamespace (name, true);
300 ns.AddNamespaceEntry (this);
301 this.FullName = ns.Name;
305 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
307 this.parent = parent;
309 this.IsImplicit = true;
312 this.FullName = ns.Name;
316 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
317 // resolved as if the immediately containing namespace body has no using-directives.
319 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
320 // in the using-namespace-directive.
322 // To implement these rules, the expressions in the using directives are resolved using
323 // the "doppelganger" (ghostly bodiless duplicate).
325 NamespaceEntry doppelganger;
326 NamespaceEntry Doppelganger {
328 if (!IsImplicit && doppelganger == null)
329 doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
334 static int next_id = 0;
335 public readonly string FullName;
336 public readonly int ID;
337 public readonly bool IsImplicit;
339 public Namespace NS {
345 public NamespaceEntry Parent {
351 public NamespaceEntry ImplicitParent {
355 if (implicit_parent == null) {
356 implicit_parent = (parent.NS == ns.Parent)
358 : new NamespaceEntry (parent, file, ns.Parent);
360 return implicit_parent;
364 public void DefineName (string name, IAlias o)
366 ns.DefineName (name, o);
370 /// Records a new namespace for resolving name references
372 public void Using (Expression ns, Location loc)
374 string name = ns.ToString ();
375 if (DeclarationFound){
376 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
380 if (name == FullName)
383 if (using_clauses == null)
384 using_clauses = new ArrayList ();
386 foreach (UsingEntry old_entry in using_clauses) {
387 if (old_entry.Name.ToString () == name) {
388 if (RootContext.WarningLevel >= 3)
389 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", name);
395 UsingEntry ue = new UsingEntry (Doppelganger, ns, loc);
396 using_clauses.Add (ue);
399 public void UsingAlias (string name, Expression alias, Location loc)
401 if (DeclarationFound){
402 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
407 aliases = new Hashtable ();
409 if (aliases.Contains (name)){
410 Report.Error (1537, loc, "The using alias `" + name +
411 "' appeared previously in this namespace");
415 aliases [name] = new AliasEntry (Doppelganger, name, alias, loc);
418 public FullNamedExpression LookupAlias (string alias)
420 AliasEntry entry = null;
422 entry = (AliasEntry) aliases [alias];
424 return entry == null ? null : entry.Resolve ();
427 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc)
429 FullNamedExpression resolved = null;
432 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
433 int pos = name.IndexOf ('.');
435 rest = name.Substring (pos + 1);
436 name = name.Substring (0, pos);
439 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
440 if ((resolved = curr_ns.Lookup (ds, name, loc)) != null)
444 if (resolved == null || rest == null)
447 Namespace ns = resolved as Namespace;
449 return ns.Lookup (ds, rest, loc);
451 Type nested = TypeManager.LookupType (resolved.FullName + "." + rest);
452 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
455 return new TypeExpression (nested, Location.Null);
458 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
460 // Precondition: Only simple names (no dots) will be looked up with this function.
463 // Check whether it's in the namespace.
465 FullNamedExpression o = NS.Lookup (ds, name, loc);
475 o = LookupAlias (name);
480 // Check using entries.
482 FullNamedExpression t = null, match = null;
483 foreach (Namespace using_ns in GetUsingTable ()) {
484 match = using_ns.Lookup (ds, name, loc);
485 if ((match != null) && (match is TypeExpr)) {
487 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.FullName, match.FullName);
498 // Our cached computation.
499 Namespace [] namespace_using_table;
500 public Namespace[] GetUsingTable ()
502 if (namespace_using_table != null)
503 return namespace_using_table;
505 if (using_clauses == null) {
506 namespace_using_table = new Namespace [0];
507 return namespace_using_table;
510 ArrayList list = new ArrayList (using_clauses.Count);
512 foreach (UsingEntry ue in using_clauses) {
513 Namespace using_ns = ue.Resolve ();
514 if (using_ns == null)
520 namespace_using_table = new Namespace [list.Count];
521 list.CopyTo (namespace_using_table, 0);
522 return namespace_using_table;
525 public void DefineNamespace (SymbolWriter symwriter)
530 parent.DefineNamespace (symwriter);
533 if (using_clauses != null) {
534 using_list = new string [using_clauses.Count];
535 for (int i = 0; i < using_clauses.Count; i++)
536 using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
538 using_list = new string [0];
541 int parent_id = parent != null ? parent.symfile_id : 0;
542 if (file.SourceFileEntry == null)
545 symfile_id = symwriter.DefineNamespace (
546 ns.Name, file.SourceFileEntry, using_list, parent_id);
549 public int SymbolFileID {
555 static void MsgtryRef (string s)
557 Console.WriteLine (" Try using -r:" + s);
560 static void MsgtryPkg (string s)
562 Console.WriteLine (" Try using -pkg:" + s);
565 protected void error246 (Location loc, string name)
567 Report.Error (246, loc, "The namespace `" + name +
568 "' can not be found (missing assembly reference?)");
571 case "Gtk": case "GtkSharp":
572 MsgtryPkg ("gtk-sharp");
575 case "Gdk": case "GdkSharp":
576 MsgtryPkg ("gdk-sharp");
579 case "Glade": case "GladeSharp":
580 MsgtryPkg ("glade-sharp");
583 case "System.Drawing":
584 case "System.Web.Services":
587 case "System.Windows.Forms":
594 /// Used to validate that all the using clauses are correct
595 /// after we are finished parsing all the files.
597 public void VerifyUsing ()
599 if (using_clauses != null){
600 foreach (UsingEntry ue in using_clauses){
601 if (ue.Resolve () != null)
604 if (ue.resolved == null)
605 error246 (ue.Location, ue.Name.ToString ());
607 Report.Error (138, ue.Location, "The using keyword only lets you specify a namespace, " +
608 "`" + ue.Name + "' is a class not a namespace.");
613 if (aliases != null){
614 foreach (DictionaryEntry de in aliases){
615 AliasEntry alias = (AliasEntry) de.Value;
617 if (alias.Resolve () != null)
620 error246 (alias.Location, alias.Alias.ToString ());
625 public override string ToString ()
627 if (NS == Namespace.Root)
628 return "NamespaceEntry (<root>)";
630 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);