2 // namespace.cs: Tracks namespaces
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
10 using System.Collections;
11 using System.Collections.Specialized;
13 namespace Mono.CSharp {
16 /// Keeps track of the namespaces defined in the C# code.
18 /// This is an Expression to allow it to be referenced in the
19 /// compiler parse/intermediate tree during name resolution.
21 public class Namespace : FullNamedExpression {
22 static ArrayList all_namespaces;
23 static Hashtable namespaces_map;
29 IDictionary declspaces;
30 Hashtable cached_types;
32 public readonly MemberName MemberName;
34 public static Namespace Root;
41 public static void Reset ()
43 all_namespaces = new ArrayList ();
44 namespaces_map = new Hashtable ();
46 Root = new Namespace (null, "");
50 /// Constructor Takes the current namespace and the
51 /// name. This is bootstrapped with parent == null
54 public Namespace (Namespace parent, string name)
56 // Expression members.
57 this.eclass = ExprClass.Namespace;
59 this.loc = Location.Null;
63 string pname = parent != null ? parent.Name : "";
68 fullname = parent.Name + "." + name;
71 throw new InternalErrorException ("Namespace has a null fullname");
73 if (parent != null && parent.MemberName != MemberName.Null)
74 MemberName = new MemberName (
75 parent.MemberName, name, parent.MemberName.Location);
77 MemberName = MemberName.Null;
79 MemberName = new MemberName (name, Location.Null);
81 entries = new ArrayList ();
82 namespaces = new Hashtable ();
83 cached_types = new Hashtable ();
85 all_namespaces.Add (this);
86 if (namespaces_map.Contains (fullname))
88 namespaces_map [fullname] = true;
91 public override Expression DoResolve (EmitContext ec)
96 public override void Emit (EmitContext ec)
98 throw new InternalErrorException ("Expression tree referenced namespace " + fullname + " during Emit ()");
101 public static bool IsNamespace (string name)
103 return namespaces_map [name] != null;
106 public override string GetSignatureForError ()
108 return Name.Length == 0 ? "::global" : Name;
111 public Namespace GetNamespace (string name, bool create)
113 int pos = name.IndexOf ('.');
118 first = name.Substring (0, pos);
122 ns = (Namespace) namespaces [first];
127 ns = new Namespace (this, first);
128 namespaces.Add (first, ns);
132 ns = ns.GetNamespace (name.Substring (pos + 1), create);
137 public static Namespace LookupNamespace (string name, bool create)
139 return Root.GetNamespace (name, create);
142 TypeExpr LookupType (string name, Location loc)
144 if (cached_types.Contains (name))
145 return cached_types [name] as TypeExpr;
148 if (declspaces != null) {
149 DeclSpace tdecl = declspaces [name] as DeclSpace;
152 // Note that this is not:
154 // t = tdecl.DefineType ()
156 // This is to make it somewhat more useful when a DefineType
157 // fails due to problems in nested types (more useful in the sense
158 // of fewer misleading error messages)
161 t = tdecl.TypeBuilder;
164 string lookup = t != null ? t.FullName : (fullname == "" ? name : fullname + "." + name);
165 Type rt = TypeManager.LookupTypeReflection (lookup, loc);
169 TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
170 cached_types [name] = te;
174 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
176 Namespace ns = GetNamespace (name, false);
180 TypeExpr te = LookupType (name, loc);
181 if (te == null || !ds.CheckAccessLevel (te.Type))
187 public void AddNamespaceEntry (NamespaceEntry entry)
192 public void AddDeclSpace (string name, DeclSpace ds)
194 if (declspaces == null)
195 declspaces = new HybridDictionary ();
196 declspaces.Add (name, ds);
199 static public ArrayList UserDefinedNamespaces {
200 get { return all_namespaces; }
204 /// The qualified name of the current namespace
207 get { return fullname; }
210 public override string FullName {
211 get { return fullname; }
215 /// The parent of this namespace, used by the parser to "Pop"
216 /// the current namespace declaration
218 public Namespace Parent {
219 get { return parent; }
222 public static void DefineNamespaces (SymbolWriter symwriter)
224 foreach (Namespace ns in all_namespaces) {
225 foreach (NamespaceEntry entry in ns.entries)
226 entry.DefineNamespace (symwriter);
231 /// Used to validate that all the using clauses are correct
232 /// after we are finished parsing all the files.
234 public static void VerifyUsing ()
236 foreach (Namespace ns in all_namespaces) {
237 foreach (NamespaceEntry entry in ns.entries)
238 entry.VerifyUsing ();
242 public override string ToString ()
245 return "Namespace (<root>)";
247 return String.Format ("Namespace ({0})", Name);
251 public class NamespaceEntry
254 NamespaceEntry parent, implicit_parent;
258 ArrayList using_clauses;
259 public bool DeclarationFound = false;
262 // This class holds the location where a using definition is
263 // done, and whether it has been used by the program or not.
265 // We use this to flag using clauses for namespaces that do not
268 public class UsingEntry {
269 public readonly MemberName Name;
270 readonly Expression Expr;
271 readonly NamespaceEntry NamespaceEntry;
272 readonly Location Location;
274 public UsingEntry (NamespaceEntry entry, MemberName name, Location loc)
277 Expr = name.GetTypeExpression ();
278 NamespaceEntry = entry;
282 internal Namespace resolved;
284 public Namespace Resolve ()
286 if (resolved != null)
289 DeclSpace root = RootContext.Tree.Types;
290 root.NamespaceEntry = NamespaceEntry;
291 FullNamedExpression fne = Expr.ResolveAsTypeStep (root.EmitContext, false);
292 root.NamespaceEntry = null;
295 Error_NamespaceNotFound (Location, Name.ToString ());
299 resolved = fne as Namespace;
300 if (resolved == null) {
301 Report.Error (138, Location,
302 "`{0} is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.ToString ());
308 public class AliasEntry {
309 public readonly string Name;
310 public readonly Expression Alias;
311 public readonly NamespaceEntry NamespaceEntry;
312 public readonly Location Location;
314 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
317 Alias = alias.GetTypeExpression ();
318 NamespaceEntry = entry;
322 FullNamedExpression resolved;
324 public FullNamedExpression Resolve ()
326 if (resolved != null)
329 DeclSpace root = RootContext.Tree.Types;
330 root.NamespaceEntry = NamespaceEntry;
331 resolved = Alias.ResolveAsTypeStep (root.EmitContext, false);
332 root.NamespaceEntry = null;
338 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
340 this.parent = parent;
342 this.IsImplicit = false;
346 ns = parent.NS.GetNamespace (name, true);
347 else if (name != null)
348 ns = Namespace.LookupNamespace (name, true);
351 ns.AddNamespaceEntry (this);
355 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
357 this.parent = parent;
359 this.IsImplicit = true;
365 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
366 // resolved as if the immediately containing namespace body has no using-directives.
368 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
369 // in the using-namespace-directive.
371 // To implement these rules, the expressions in the using directives are resolved using
372 // the "doppelganger" (ghostly bodiless duplicate).
374 NamespaceEntry doppelganger;
375 NamespaceEntry Doppelganger {
377 if (!IsImplicit && doppelganger == null)
378 doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
383 static int next_id = 0;
384 public readonly int ID;
385 public readonly bool IsImplicit;
387 public Namespace NS {
391 public NamespaceEntry Parent {
392 get { return parent; }
395 public NamespaceEntry ImplicitParent {
399 if (implicit_parent == null) {
400 implicit_parent = (parent.NS == ns.Parent)
402 : new NamespaceEntry (parent, file, ns.Parent);
404 return implicit_parent;
409 /// Records a new namespace for resolving name references
411 public void Using (MemberName name, Location loc)
413 if (DeclarationFound){
414 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
418 if (name.Equals (ns.MemberName))
421 if (using_clauses == null)
422 using_clauses = new ArrayList ();
424 foreach (UsingEntry old_entry in using_clauses) {
425 if (name.Equals (old_entry.Name)) {
426 if (RootContext.WarningLevel >= 3)
427 Report.Warning (105, loc, "The using directive for `{0}' appeared previously in this namespace", name);
433 UsingEntry ue = new UsingEntry (Doppelganger, name, loc);
434 using_clauses.Add (ue);
437 public void UsingAlias (string name, MemberName alias, Location loc)
439 if (DeclarationFound){
440 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
445 aliases = new Hashtable ();
447 if (aliases.Contains (name)) {
448 AliasEntry ae = (AliasEntry)aliases [name];
449 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
450 Report.Error (1537, loc, "The using alias `" + name +
451 "' appeared previously in this namespace");
455 if (RootContext.Version == LanguageVersion.Default &&
456 name == "global" && RootContext.WarningLevel >= 2)
457 Report.Warning (440, loc, "An alias named `global' will not be used when resolving 'global::';" +
458 " the global namespace will be used instead");
460 aliases [name] = new AliasEntry (Doppelganger, name, alias, loc);
463 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
465 // Precondition: Only simple names (no dots) will be looked up with this function.
466 FullNamedExpression resolved = null;
467 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
468 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
474 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
476 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
477 name, t1.FullName, t2.FullName);
480 // Looks-up a alias named @name in this and surrounding namespace declarations
481 public FullNamedExpression LookupAlias (string name)
483 AliasEntry entry = null;
484 // We use Parent rather than ImplicitParent since we know implicit namespace declarations
485 // cannot have using entries.
486 for (NamespaceEntry n = this; n != null; n = n.Parent) {
487 if (n.aliases == null)
489 entry = n.aliases [name] as AliasEntry;
491 return entry.Resolve ();
496 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
499 // Check whether it's in the namespace.
501 FullNamedExpression fne = NS.Lookup (ds, name, loc);
511 if (aliases != null) {
512 AliasEntry entry = aliases [name] as AliasEntry;
514 return entry.Resolve ();
518 // Check using entries.
520 FullNamedExpression match = null;
521 foreach (Namespace using_ns in GetUsingTable ()) {
522 match = using_ns.Lookup (ds, name, loc);
523 if (match == null || !(match is TypeExpr))
527 Error_AmbiguousTypeReference (loc, name, fne, match);
536 // Our cached computation.
537 readonly Namespace [] empty_namespaces = new Namespace [0];
538 Namespace [] namespace_using_table;
539 Namespace [] GetUsingTable ()
541 if (namespace_using_table != null)
542 return namespace_using_table;
544 if (using_clauses == null) {
545 namespace_using_table = empty_namespaces;
546 return namespace_using_table;
549 ArrayList list = new ArrayList (using_clauses.Count);
551 foreach (UsingEntry ue in using_clauses) {
552 Namespace using_ns = ue.Resolve ();
553 if (using_ns == null)
559 namespace_using_table = new Namespace [list.Count];
560 list.CopyTo (namespace_using_table, 0);
561 return namespace_using_table;
564 readonly string [] empty_using_list = new string [0];
566 public void DefineNamespace (SymbolWriter symwriter)
571 parent.DefineNamespace (symwriter);
573 string [] using_list = empty_using_list;
574 if (using_clauses != null) {
575 using_list = new string [using_clauses.Count];
576 for (int i = 0; i < using_clauses.Count; i++)
577 using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
580 int parent_id = parent != null ? parent.symfile_id : 0;
581 if (file.SourceFileEntry == null)
584 symfile_id = symwriter.DefineNamespace (
585 ns.Name, file.SourceFileEntry, using_list, parent_id);
588 public int SymbolFileID {
589 get { return symfile_id; }
592 static void MsgtryRef (string s)
594 Console.WriteLine (" Try using -r:" + s);
597 static void MsgtryPkg (string s)
599 Console.WriteLine (" Try using -pkg:" + s);
602 public static void Error_NamespaceNotFound (Location loc, string name)
604 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
608 case "Gtk": case "GtkSharp":
609 MsgtryPkg ("gtk-sharp");
612 case "Gdk": case "GdkSharp":
613 MsgtryPkg ("gdk-sharp");
616 case "Glade": case "GladeSharp":
617 MsgtryPkg ("glade-sharp");
620 case "System.Drawing":
621 case "System.Web.Services":
624 case "System.Windows.Forms":
631 /// Used to validate that all the using clauses are correct
632 /// after we are finished parsing all the files.
634 public void VerifyUsing ()
636 if (using_clauses != null) {
637 foreach (UsingEntry ue in using_clauses)
641 if (aliases != null) {
642 foreach (DictionaryEntry de in aliases) {
643 AliasEntry alias = (AliasEntry) de.Value;
644 if (alias.Resolve () == null)
645 Error_NamespaceNotFound (alias.Location, alias.Alias.ToString ());
650 public string GetSignatureForError ()
652 if (NS == Namespace.Root)
658 public override string ToString ()
660 if (NS == Namespace.Root)
661 return "NamespaceEntry (<root>)";
663 return String.Format ("NamespaceEntry ({0},{1},{2})", ns.Name, IsImplicit, ID);