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 {
21 static ArrayList all_namespaces;
22 static Hashtable namespaces_map;
29 Hashtable cached_types;
31 public readonly MemberName MemberName;
33 public static Namespace Root;
40 public static void Reset ()
42 all_namespaces = new ArrayList ();
43 namespaces_map = new Hashtable ();
45 Root = new Namespace (null, "");
49 /// Constructor Takes the current namespace and the
50 /// name. This is bootstrapped with parent == null
53 public Namespace (Namespace parent, string name)
55 // Expression members.
56 this.eclass = ExprClass.Namespace;
58 this.loc = Location.Null;
62 string pname = parent != null ? parent.Name : "";
67 fullname = parent.Name + "." + name;
69 if (parent != null && parent.MemberName != MemberName.Null)
70 MemberName = new MemberName (parent.MemberName, name);
72 MemberName = MemberName.Null;
74 MemberName = new MemberName (name);
76 entries = new ArrayList ();
77 namespaces = new Hashtable ();
78 declspaces = new Hashtable ();
79 cached_types = new Hashtable ();
81 all_namespaces.Add (this);
82 if (namespaces_map.Contains (fullname))
84 namespaces_map [fullname] = true;
87 public override Expression DoResolve (EmitContext ec)
92 public override void Emit (EmitContext ec)
94 throw new InternalErrorException ("Expression tree referenced namespace " + fullname + " during Emit ()");
97 public static bool IsNamespace (string name)
99 return namespaces_map [name] != null;
102 public override string GetSignatureForError ()
104 return Name.Length == 0 ? "::global" : Name;
107 public Namespace GetNamespace (string name, bool create)
109 int pos = name.IndexOf ('.');
114 first = name.Substring (0, pos);
118 ns = (Namespace) namespaces [first];
123 ns = new Namespace (this, first);
124 namespaces.Add (first, ns);
128 ns = ns.GetNamespace (name.Substring (pos + 1), create);
133 public static Namespace LookupNamespace (string name, bool create)
135 return Root.GetNamespace (name, create);
138 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
140 Namespace ns = GetNamespace (name, false);
145 if (cached_types.Contains (name)) {
146 te = (TypeExpr) cached_types [name];
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;
163 string lookup = this == Namespace.Root ? name : fullname + "." + name;
164 t = TypeManager.LookupTypeReflection (lookup);
166 te = t == null ? null : new TypeExpression (t, Location.Null);
167 cached_types [name] = te;
170 if (te != null && ds != null && !ds.CheckAccessLevel (te.Type))
176 public void AddNamespaceEntry (NamespaceEntry entry)
181 public void AddDeclSpace (string name, DeclSpace ds)
183 declspaces.Add (name, ds);
186 static public ArrayList UserDefinedNamespaces {
187 get { return all_namespaces; }
191 /// The qualified name of the current namespace
194 get { return fullname; }
197 public override string FullName {
198 get { return fullname; }
202 /// The parent of this namespace, used by the parser to "Pop"
203 /// the current namespace declaration
205 public Namespace Parent {
206 get { return parent; }
209 public static void DefineNamespaces (SymbolWriter symwriter)
211 foreach (Namespace ns in all_namespaces) {
212 foreach (NamespaceEntry entry in ns.entries)
213 entry.DefineNamespace (symwriter);
218 /// Used to validate that all the using clauses are correct
219 /// after we are finished parsing all the files.
221 public static void VerifyUsing ()
223 foreach (Namespace ns in all_namespaces) {
224 foreach (NamespaceEntry entry in ns.entries)
225 entry.VerifyUsing ();
229 public override string ToString ()
232 return "Namespace (<root>)";
234 return String.Format ("Namespace ({0})", Name);
238 public class NamespaceEntry
241 NamespaceEntry parent, implicit_parent;
245 ArrayList using_clauses;
246 public bool DeclarationFound = false;
249 // This class holds the location where a using definition is
250 // done, and whether it has been used by the program or not.
252 // We use this to flag using clauses for namespaces that do not
255 public class UsingEntry {
256 public readonly MemberName Name;
257 readonly Expression Expr;
258 readonly NamespaceEntry NamespaceEntry;
259 readonly Location Location;
261 public UsingEntry (NamespaceEntry entry, MemberName name, Location loc)
264 Expr = name.GetTypeExpression (loc);
265 NamespaceEntry = entry;
269 internal Namespace resolved;
271 public Namespace Resolve ()
273 if (resolved != null)
276 DeclSpace root = RootContext.Tree.Types;
277 root.NamespaceEntry = NamespaceEntry;
278 FullNamedExpression fne = Expr.ResolveAsTypeStep (root.EmitContext, false);
279 root.NamespaceEntry = null;
282 Error_NamespaceNotFound (Location, Name.ToString ());
286 resolved = fne as Namespace;
287 if (resolved == null) {
288 Report.Error (138, Location,
289 "`{0} is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.ToString ());
295 public class AliasEntry {
296 public readonly string Name;
297 public readonly Expression Alias;
298 public readonly NamespaceEntry NamespaceEntry;
299 public readonly Location Location;
301 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
304 Alias = alias.GetTypeExpression (loc);
305 NamespaceEntry = entry;
309 FullNamedExpression resolved;
311 public FullNamedExpression Resolve ()
313 if (resolved != null)
316 DeclSpace root = RootContext.Tree.Types;
317 root.NamespaceEntry = NamespaceEntry;
318 resolved = Alias.ResolveAsTypeStep (root.EmitContext, false);
319 root.NamespaceEntry = null;
325 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
327 this.parent = parent;
329 this.IsImplicit = false;
333 ns = parent.NS.GetNamespace (name, true);
334 else if (name != null)
335 ns = Namespace.LookupNamespace (name, true);
338 ns.AddNamespaceEntry (this);
342 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
344 this.parent = parent;
346 this.IsImplicit = true;
352 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
353 // resolved as if the immediately containing namespace body has no using-directives.
355 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
356 // in the using-namespace-directive.
358 // To implement these rules, the expressions in the using directives are resolved using
359 // the "doppelganger" (ghostly bodiless duplicate).
361 NamespaceEntry doppelganger;
362 NamespaceEntry Doppelganger {
364 if (!IsImplicit && doppelganger == null)
365 doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
370 static int next_id = 0;
371 public readonly int ID;
372 public readonly bool IsImplicit;
374 public Namespace NS {
380 public NamespaceEntry Parent {
386 public NamespaceEntry ImplicitParent {
390 if (implicit_parent == null) {
391 implicit_parent = (parent.NS == ns.Parent)
393 : new NamespaceEntry (parent, file, ns.Parent);
395 return implicit_parent;
400 /// Records a new namespace for resolving name references
402 public void Using (MemberName name, Location loc)
404 if (DeclarationFound){
405 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
409 if (name.Equals (ns.MemberName))
412 if (using_clauses == null)
413 using_clauses = new ArrayList ();
415 foreach (UsingEntry old_entry in using_clauses) {
416 if (name.Equals (old_entry.Name)) {
417 if (RootContext.WarningLevel >= 3)
418 Report.Warning (105, loc, "The using directive for `{0}' appeared previously in this namespace", name);
424 UsingEntry ue = new UsingEntry (Doppelganger, name, loc);
425 using_clauses.Add (ue);
428 public void UsingAlias (string name, MemberName alias, Location loc)
430 if (DeclarationFound){
431 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
436 aliases = new Hashtable ();
438 if (aliases.Contains (name)){
439 AliasEntry ae = (AliasEntry)aliases [name];
440 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
441 Report.Error (1537, loc, "The using alias `" + name +
442 "' appeared previously in this namespace");
446 aliases [name] = new AliasEntry (Doppelganger, name, alias, loc);
449 public FullNamedExpression LookupAlias (string alias)
451 AliasEntry entry = null;
453 entry = (AliasEntry) aliases [alias];
455 return entry == null ? null : entry.Resolve ();
458 static readonly char [] dot_array = { '.' };
460 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
462 FullNamedExpression resolved = null;
465 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
466 int pos = name.IndexOf ('.');
468 rest = name.Substring (pos + 1);
469 name = name.Substring (0, pos);
472 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
473 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
477 if (resolved == null || rest == null)
480 // Now handle the rest of the the name.
481 string [] elements = rest.Split (dot_array);
482 int count = elements.Length;
484 while (i < count && resolved != null && resolved is Namespace) {
485 Namespace ns = resolved as Namespace;
486 resolved = ns.Lookup (ds, elements [i++], loc);
489 if (resolved == null || resolved is Namespace)
492 Type t = ((TypeExpr) resolved).Type;
495 if (ds != null && !ds.CheckAccessLevel (t))
498 return new TypeExpression (t, Location.Null);
499 t = TypeManager.GetNestedType (t, elements [i++]);
505 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
507 // Precondition: Only simple names (no dots) will be looked up with this function.
510 // Check whether it's in the namespace.
512 FullNamedExpression o = NS.Lookup (ds, name, loc);
522 o = LookupAlias (name);
527 // Check using entries.
529 FullNamedExpression t = null, match = null;
530 foreach (Namespace using_ns in GetUsingTable ()) {
531 match = using_ns.Lookup (ds, name, loc);
532 if ((match != null) && (match is TypeExpr)) {
535 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.FullName, match.FullName);
547 // Our cached computation.
548 Namespace [] namespace_using_table;
549 Namespace[] GetUsingTable ()
551 if (namespace_using_table != null)
552 return namespace_using_table;
554 if (using_clauses == null) {
555 namespace_using_table = new Namespace [0];
556 return namespace_using_table;
559 ArrayList list = new ArrayList (using_clauses.Count);
561 foreach (UsingEntry ue in using_clauses) {
562 Namespace using_ns = ue.Resolve ();
563 if (using_ns == null)
569 namespace_using_table = new Namespace [list.Count];
570 list.CopyTo (namespace_using_table, 0);
571 return namespace_using_table;
574 public void DefineNamespace (SymbolWriter symwriter)
579 parent.DefineNamespace (symwriter);
582 if (using_clauses != null) {
583 using_list = new string [using_clauses.Count];
584 for (int i = 0; i < using_clauses.Count; i++)
585 using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
587 using_list = new string [0];
590 int parent_id = parent != null ? parent.symfile_id : 0;
591 if (file.SourceFileEntry == null)
594 symfile_id = symwriter.DefineNamespace (
595 ns.Name, file.SourceFileEntry, using_list, parent_id);
598 public int SymbolFileID {
604 static void MsgtryRef (string s)
606 Console.WriteLine (" Try using -r:" + s);
609 static void MsgtryPkg (string s)
611 Console.WriteLine (" Try using -pkg:" + s);
614 public static void Error_NamespaceNotFound (Location loc, string name)
616 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
620 case "Gtk": case "GtkSharp":
621 MsgtryPkg ("gtk-sharp");
624 case "Gdk": case "GdkSharp":
625 MsgtryPkg ("gdk-sharp");
628 case "Glade": case "GladeSharp":
629 MsgtryPkg ("glade-sharp");
632 case "System.Drawing":
633 case "System.Web.Services":
636 case "System.Windows.Forms":
643 /// Used to validate that all the using clauses are correct
644 /// after we are finished parsing all the files.
646 public void VerifyUsing ()
648 if (using_clauses != null){
649 foreach (UsingEntry ue in using_clauses){
654 if (aliases != null){
655 foreach (DictionaryEntry de in aliases){
656 AliasEntry alias = (AliasEntry) de.Value;
658 if (alias.Resolve () != null)
661 Error_NamespaceNotFound (alias.Location, alias.Alias.ToString ());
666 public override string ToString ()
668 if (NS == Namespace.Root)
669 return "NamespaceEntry (<root>)";
671 return String.Format ("NamespaceEntry ({0},{1},{2})", ns.Name, IsImplicit, ID);