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 Expression Name;
233 public readonly NamespaceEntry NamespaceEntry;
234 public readonly Location Location;
236 public UsingEntry (NamespaceEntry entry, Expression name, Location loc)
239 NamespaceEntry = entry;
243 internal FullNamedExpression resolved;
245 public Namespace Resolve ()
247 if (resolved != null)
248 return resolved as Namespace;
250 DeclSpace root = RootContext.Tree.Types;
251 NamespaceEntry orig_ns = root.NamespaceEntry;
252 root.NamespaceEntry = NamespaceEntry;
253 resolved = Name.ResolveAsTypeStep (root.EmitContext);
254 root.NamespaceEntry = orig_ns;
256 return resolved as Namespace;
260 public class AliasEntry {
261 public readonly string Name;
262 public readonly Expression Alias;
263 public readonly NamespaceEntry NamespaceEntry;
264 public readonly Location Location;
266 public AliasEntry (NamespaceEntry entry, string name, Expression alias, Location loc)
270 NamespaceEntry = entry;
274 FullNamedExpression resolved;
276 public FullNamedExpression Resolve ()
278 if (resolved != null)
281 DeclSpace root = RootContext.Tree.Types;
282 root.NamespaceEntry = NamespaceEntry;
283 resolved = Alias.ResolveAsTypeStep (root.EmitContext);
284 root.NamespaceEntry = null;
290 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
292 this.parent = parent;
294 this.IsImplicit = false;
298 ns = parent.NS.GetNamespace (name, true);
299 else if (name != null)
300 ns = Namespace.LookupNamespace (name, true);
303 ns.AddNamespaceEntry (this);
304 this.FullName = ns.Name;
308 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
310 this.parent = parent;
312 this.IsImplicit = true;
315 this.FullName = ns.Name;
319 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
320 // resolved as if the immediately containing namespace body has no using-directives.
322 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
323 // in the using-namespace-directive.
325 // To implement these rules, the expressions in the using directives are resolved using
326 // the "doppelganger" (ghostly bodiless duplicate).
328 NamespaceEntry doppelganger;
329 NamespaceEntry Doppelganger {
331 if (!IsImplicit && doppelganger == null)
332 doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
337 static int next_id = 0;
338 public readonly string FullName;
339 public readonly int ID;
340 public readonly bool IsImplicit;
342 public Namespace NS {
348 public NamespaceEntry Parent {
354 public NamespaceEntry ImplicitParent {
358 if (implicit_parent == null) {
359 implicit_parent = (parent.NS == ns.Parent)
361 : new NamespaceEntry (parent, file, ns.Parent);
363 return implicit_parent;
367 public void DefineName (string name, IAlias o)
369 ns.DefineName (name, o);
373 /// Records a new namespace for resolving name references
375 public void Using (Expression ns, Location loc)
377 string name = ns.ToString ();
378 if (DeclarationFound){
379 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
383 if (name == FullName)
386 if (using_clauses == null)
387 using_clauses = new ArrayList ();
389 foreach (UsingEntry old_entry in using_clauses) {
390 if (old_entry.Name.ToString () == name) {
391 if (RootContext.WarningLevel >= 3)
392 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", name);
398 UsingEntry ue = new UsingEntry (Doppelganger, ns, loc);
399 using_clauses.Add (ue);
402 public void UsingAlias (string name, Expression alias, Location loc)
404 if (DeclarationFound){
405 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
410 aliases = new Hashtable ();
412 if (aliases.Contains (name)){
413 Report.Error (1537, loc, "The using alias `" + name +
414 "' appeared previously in this namespace");
418 aliases [name] = new AliasEntry (Doppelganger, name, alias, loc);
421 public FullNamedExpression LookupAlias (string alias)
423 AliasEntry entry = null;
425 entry = (AliasEntry) aliases [alias];
427 return entry == null ? null : entry.Resolve ();
430 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
432 FullNamedExpression resolved = null;
435 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
436 int pos = name.IndexOf ('.');
438 rest = name.Substring (pos + 1);
439 name = name.Substring (0, pos);
442 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
443 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
447 if (resolved == null || rest == null)
450 Namespace ns = resolved as Namespace;
452 return ns.Lookup (ds, rest, loc);
454 Type nested = TypeManager.LookupType (resolved.FullName + "." + rest);
455 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
458 return new TypeExpression (nested, Location.Null);
461 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
463 // Precondition: Only simple names (no dots) will be looked up with this function.
466 // Check whether it's in the namespace.
468 FullNamedExpression o = NS.Lookup (ds, name, loc);
478 o = LookupAlias (name);
483 // Check using entries.
485 FullNamedExpression t = null, match = null;
486 foreach (Namespace using_ns in GetUsingTable ()) {
487 match = using_ns.Lookup (ds, name, loc);
488 if ((match != null) && (match is TypeExpr)) {
491 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.FullName, match.FullName);
503 // Our cached computation.
504 Namespace [] namespace_using_table;
505 public Namespace[] GetUsingTable ()
507 if (namespace_using_table != null)
508 return namespace_using_table;
510 if (using_clauses == null) {
511 namespace_using_table = new Namespace [0];
512 return namespace_using_table;
515 ArrayList list = new ArrayList (using_clauses.Count);
517 foreach (UsingEntry ue in using_clauses) {
518 Namespace using_ns = ue.Resolve ();
519 if (using_ns == null)
525 namespace_using_table = new Namespace [list.Count];
526 list.CopyTo (namespace_using_table, 0);
527 return namespace_using_table;
530 public void DefineNamespace (SymbolWriter symwriter)
535 parent.DefineNamespace (symwriter);
538 if (using_clauses != null) {
539 using_list = new string [using_clauses.Count];
540 for (int i = 0; i < using_clauses.Count; i++)
541 using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
543 using_list = new string [0];
546 int parent_id = parent != null ? parent.symfile_id : 0;
547 if (file.SourceFileEntry == null)
550 symfile_id = symwriter.DefineNamespace (
551 ns.Name, file.SourceFileEntry, using_list, parent_id);
554 public int SymbolFileID {
560 static void MsgtryRef (string s)
562 Console.WriteLine (" Try using -r:" + s);
565 static void MsgtryPkg (string s)
567 Console.WriteLine (" Try using -pkg:" + s);
570 protected void error246 (Location loc, string name)
572 Report.Error (246, loc, "The namespace `" + name +
573 "' can not be found (missing assembly reference?)");
576 case "Gtk": case "GtkSharp":
577 MsgtryPkg ("gtk-sharp");
580 case "Gdk": case "GdkSharp":
581 MsgtryPkg ("gdk-sharp");
584 case "Glade": case "GladeSharp":
585 MsgtryPkg ("glade-sharp");
588 case "System.Drawing":
589 case "System.Web.Services":
592 case "System.Windows.Forms":
599 /// Used to validate that all the using clauses are correct
600 /// after we are finished parsing all the files.
602 public void VerifyUsing ()
604 if (using_clauses != null){
605 foreach (UsingEntry ue in using_clauses){
606 if (ue.Resolve () != null)
609 if (ue.resolved == null)
610 error246 (ue.Location, ue.Name.ToString ());
612 Report.Error (138, ue.Location, "The using keyword only lets you specify a namespace, " +
613 "`" + ue.Name + "' is a class not a namespace.");
618 if (aliases != null){
619 foreach (DictionaryEntry de in aliases){
620 AliasEntry alias = (AliasEntry) de.Value;
622 if (alias.Resolve () != null)
625 error246 (alias.Location, alias.Alias.ToString ());
630 public override string ToString ()
632 if (NS == Namespace.Root)
633 return "NamespaceEntry (<root>)";
635 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);