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;
117 // Note that this is not:
119 // t = tdecl.DefineType ()
121 // This is to make it somewhat more useful when a DefineType
122 // fails due to problems in nested types (more useful in the sense
123 // of fewer misleading error messages)
126 t = tdecl.TypeBuilder;
131 if ((ds == null) || ds.CheckAccessLevel (t))
132 return new TypeExpression (t, Location.Null);
135 Namespace ns = GetNamespace (name, false);
139 // We are sure that 'name' is a simple name (no dots).
140 t = TypeManager.LookupTypeDirect (DeclSpace.MakeFQN (fullname, name));
141 if ((t == null) || ((ds != null) && !ds.CheckAccessLevel (t)))
144 return new TypeExpression (t, Location.Null);
147 public void AddNamespaceEntry (NamespaceEntry entry)
152 public void DefineName (string name, IAlias o)
154 defined_names.Add (name, o);
157 static public ArrayList UserDefinedNamespaces {
159 return all_namespaces;
164 /// The qualified name of the current namespace
172 public override string FullName {
179 /// The parent of this namespace, used by the parser to "Pop"
180 /// the current namespace declaration
182 public Namespace Parent {
188 public static void DefineNamespaces (SymbolWriter symwriter)
190 foreach (Namespace ns in all_namespaces) {
191 foreach (NamespaceEntry entry in ns.entries)
192 entry.DefineNamespace (symwriter);
197 /// Used to validate that all the using clauses are correct
198 /// after we are finished parsing all the files.
200 public static void VerifyUsing ()
202 foreach (Namespace ns in all_namespaces) {
203 foreach (NamespaceEntry entry in ns.entries)
204 entry.VerifyUsing ();
208 public override string ToString ()
211 return "Namespace (<root>)";
213 return String.Format ("Namespace ({0})", Name);
217 get { return false; }
220 TypeExpr IAlias.ResolveAsType (EmitContext ec)
222 throw new InvalidOperationException ();
226 public class NamespaceEntry
229 NamespaceEntry parent, implicit_parent;
233 ArrayList using_clauses;
234 public bool DeclarationFound = false;
237 // This class holds the location where a using definition is
238 // done, and whether it has been used by the program or not.
240 // We use this to flag using clauses for namespaces that do not
243 public class UsingEntry {
244 public Expression Name;
245 public readonly NamespaceEntry NamespaceEntry;
246 public readonly Location Location;
248 public UsingEntry (NamespaceEntry entry, Expression name, Location loc)
251 NamespaceEntry = entry;
255 internal FullNamedExpression resolved;
257 public Namespace Resolve ()
259 if (resolved != null)
260 return resolved as Namespace;
262 DeclSpace root = RootContext.Tree.Types;
263 root.NamespaceEntry = NamespaceEntry;
264 resolved = Name.ResolveAsTypeStep (root.EmitContext);
265 root.NamespaceEntry = null;
267 return resolved as Namespace;
271 public class AliasEntry {
272 public readonly string Name;
273 public readonly Expression Alias;
274 public readonly NamespaceEntry NamespaceEntry;
275 public readonly Location Location;
277 public AliasEntry (NamespaceEntry entry, string name, Expression alias, Location loc)
281 NamespaceEntry = entry;
285 FullNamedExpression resolved;
287 public FullNamedExpression Resolve ()
289 if (resolved != null)
292 DeclSpace root = RootContext.Tree.Types;
293 root.NamespaceEntry = NamespaceEntry;
294 resolved = Alias.ResolveAsTypeStep (root.EmitContext);
295 root.NamespaceEntry = null;
301 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
303 this.parent = parent;
305 this.IsImplicit = false;
309 ns = parent.NS.GetNamespace (name, true);
310 else if (name != null)
311 ns = Namespace.LookupNamespace (name, true);
314 ns.AddNamespaceEntry (this);
315 this.FullName = ns.Name;
319 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
321 this.parent = parent;
323 this.IsImplicit = true;
326 this.FullName = ns.Name;
330 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
331 // resolved as if the immediately containing namespace body has no using-directives.
333 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
334 // in the using-namespace-directive.
336 // To implement these rules, the expressions in the using directives are resolved using
337 // the "doppelganger" (ghostly bodiless duplicate).
339 NamespaceEntry doppelganger;
340 NamespaceEntry Doppelganger {
342 if (!IsImplicit && doppelganger == null)
343 doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
348 static int next_id = 0;
349 public readonly string FullName;
350 public readonly int ID;
351 public readonly bool IsImplicit;
353 public Namespace NS {
359 public NamespaceEntry Parent {
365 public NamespaceEntry ImplicitParent {
369 if (implicit_parent == null) {
370 implicit_parent = (parent.NS == ns.Parent)
372 : new NamespaceEntry (parent, file, ns.Parent);
374 return implicit_parent;
378 public void DefineName (string name, IAlias o)
380 ns.DefineName (name, o);
384 /// Records a new namespace for resolving name references
386 public void Using (Expression ns, Location loc)
388 string name = ns.ToString ();
389 if (DeclarationFound){
390 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
394 if (name == FullName)
397 if (using_clauses == null)
398 using_clauses = new ArrayList ();
400 foreach (UsingEntry old_entry in using_clauses) {
401 if (old_entry.Name.ToString () == name) {
402 if (RootContext.WarningLevel >= 3)
403 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", name);
409 UsingEntry ue = new UsingEntry (Doppelganger, ns, loc);
410 using_clauses.Add (ue);
413 public void UsingAlias (string name, Expression alias, Location loc)
415 if (DeclarationFound){
416 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
421 aliases = new Hashtable ();
423 if (aliases.Contains (name)){
424 AliasEntry ae = (AliasEntry)aliases [name];
425 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
426 Report.Error (1537, loc, "The using alias `" + name +
427 "' appeared previously in this namespace");
431 aliases [name] = new AliasEntry (Doppelganger, name, alias, loc);
434 public FullNamedExpression LookupAlias (string alias)
436 AliasEntry entry = null;
438 entry = (AliasEntry) aliases [alias];
440 return entry == null ? null : entry.Resolve ();
443 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
445 FullNamedExpression resolved = null;
448 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
449 int pos = name.IndexOf ('.');
451 rest = name.Substring (pos + 1);
452 name = name.Substring (0, pos);
455 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
456 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
460 if (resolved == null || rest == null)
463 Namespace ns = resolved as Namespace;
465 return ns.Lookup (ds, rest, loc);
467 Type nested = TypeManager.LookupType (resolved.FullName + "." + rest);
468 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
471 return new TypeExpression (nested, Location.Null);
474 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
476 // Precondition: Only simple names (no dots) will be looked up with this function.
479 // Check whether it's in the namespace.
481 FullNamedExpression o = NS.Lookup (ds, name, loc);
491 o = LookupAlias (name);
496 // Check using entries.
498 FullNamedExpression t = null, match = null;
499 foreach (Namespace using_ns in GetUsingTable ()) {
500 match = using_ns.Lookup (ds, name, loc);
501 if ((match != null) && (match is TypeExpr)) {
504 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.FullName, match.FullName);
516 // Our cached computation.
517 Namespace [] namespace_using_table;
518 public Namespace[] GetUsingTable ()
520 if (namespace_using_table != null)
521 return namespace_using_table;
523 if (using_clauses == null) {
524 namespace_using_table = new Namespace [0];
525 return namespace_using_table;
528 ArrayList list = new ArrayList (using_clauses.Count);
530 foreach (UsingEntry ue in using_clauses) {
531 Namespace using_ns = ue.Resolve ();
532 if (using_ns == null)
538 namespace_using_table = new Namespace [list.Count];
539 list.CopyTo (namespace_using_table, 0);
540 return namespace_using_table;
543 public void DefineNamespace (SymbolWriter symwriter)
548 parent.DefineNamespace (symwriter);
551 if (using_clauses != null) {
552 using_list = new string [using_clauses.Count];
553 for (int i = 0; i < using_clauses.Count; i++)
554 using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
556 using_list = new string [0];
559 int parent_id = parent != null ? parent.symfile_id : 0;
560 if (file.SourceFileEntry == null)
563 symfile_id = symwriter.DefineNamespace (
564 ns.Name, file.SourceFileEntry, using_list, parent_id);
567 public int SymbolFileID {
573 static void MsgtryRef (string s)
575 Console.WriteLine (" Try using -r:" + s);
578 static void MsgtryPkg (string s)
580 Console.WriteLine (" Try using -pkg:" + s);
583 protected void error246 (Location loc, string name)
585 Report.Error (246, loc, "The namespace `" + name +
586 "' can not be found (missing assembly reference?)");
589 case "Gtk": case "GtkSharp":
590 MsgtryPkg ("gtk-sharp");
593 case "Gdk": case "GdkSharp":
594 MsgtryPkg ("gdk-sharp");
597 case "Glade": case "GladeSharp":
598 MsgtryPkg ("glade-sharp");
601 case "System.Drawing":
602 case "System.Web.Services":
605 case "System.Windows.Forms":
612 /// Used to validate that all the using clauses are correct
613 /// after we are finished parsing all the files.
615 public void VerifyUsing ()
617 if (using_clauses != null){
618 foreach (UsingEntry ue in using_clauses){
619 if (ue.Resolve () != null)
622 if (ue.resolved == null)
623 error246 (ue.Location, ue.Name.ToString ());
625 Report.Error (138, ue.Location, "The using keyword only lets you specify a namespace, " +
626 "`" + ue.Name + "' is a class not a namespace.");
631 if (aliases != null){
632 foreach (DictionaryEntry de in aliases){
633 AliasEntry alias = (AliasEntry) de.Value;
635 if (alias.Resolve () != null)
638 error246 (alias.Location, alias.Alias.ToString ());
643 public override string ToString ()
645 if (NS == Namespace.Root)
646 return "NamespaceEntry (<root>)";
648 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);