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 TypeExpr LookupType (string name)
140 if (cached_types.Contains (name))
141 return cached_types [name] as TypeExpr;
144 DeclSpace tdecl = declspaces [name] as DeclSpace;
147 // Note that this is not:
149 // t = tdecl.DefineType ()
151 // This is to make it somewhat more useful when a DefineType
152 // fails due to problems in nested types (more useful in the sense
153 // of fewer misleading error messages)
156 t = tdecl.TypeBuilder;
158 string lookup = this == Namespace.Root ? name : fullname + "." + name;
159 t = TypeManager.LookupTypeReflection (lookup);
161 TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
162 cached_types [name] = te;
166 public FullNamedExpression Lookup (DeclSpace ds, string name)
168 Namespace ns = GetNamespace (name, false);
172 TypeExpr te = LookupType (name);
173 if (te == null || !ds.CheckAccessLevel (te.Type))
179 public void AddNamespaceEntry (NamespaceEntry entry)
184 public void AddDeclSpace (string name, DeclSpace ds)
186 declspaces.Add (name, ds);
189 static public ArrayList UserDefinedNamespaces {
190 get { return all_namespaces; }
194 /// The qualified name of the current namespace
197 get { return fullname; }
200 public override string FullName {
201 get { return fullname; }
205 /// The parent of this namespace, used by the parser to "Pop"
206 /// the current namespace declaration
208 public Namespace Parent {
209 get { return parent; }
212 public static void DefineNamespaces (SymbolWriter symwriter)
214 foreach (Namespace ns in all_namespaces) {
215 foreach (NamespaceEntry entry in ns.entries)
216 entry.DefineNamespace (symwriter);
221 /// Used to validate that all the using clauses are correct
222 /// after we are finished parsing all the files.
224 public static void VerifyUsing ()
226 foreach (Namespace ns in all_namespaces) {
227 foreach (NamespaceEntry entry in ns.entries)
228 entry.VerifyUsing ();
232 public override string ToString ()
235 return "Namespace (<root>)";
237 return String.Format ("Namespace ({0})", Name);
241 public class NamespaceEntry
244 NamespaceEntry parent, implicit_parent;
248 ArrayList using_clauses;
249 public bool DeclarationFound = false;
252 // This class holds the location where a using definition is
253 // done, and whether it has been used by the program or not.
255 // We use this to flag using clauses for namespaces that do not
258 public class UsingEntry {
259 public readonly MemberName Name;
260 readonly Expression Expr;
261 readonly NamespaceEntry NamespaceEntry;
262 readonly Location Location;
264 public UsingEntry (NamespaceEntry entry, MemberName name, Location loc)
267 Expr = name.GetTypeExpression (loc);
268 NamespaceEntry = entry;
272 internal Namespace resolved;
274 public Namespace Resolve ()
276 if (resolved != null)
279 DeclSpace root = RootContext.Tree.Types;
280 root.NamespaceEntry = NamespaceEntry;
281 FullNamedExpression fne = Expr.ResolveAsTypeStep (root.EmitContext, false);
282 root.NamespaceEntry = null;
285 Error_NamespaceNotFound (Location, Name.ToString ());
289 resolved = fne as Namespace;
290 if (resolved == null) {
291 Report.Error (138, Location,
292 "`{0} is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.ToString ());
298 public class AliasEntry {
299 public readonly string Name;
300 public readonly Expression Alias;
301 public readonly NamespaceEntry NamespaceEntry;
302 public readonly Location Location;
304 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
307 Alias = alias.GetTypeExpression (loc);
308 NamespaceEntry = entry;
312 FullNamedExpression resolved;
314 public FullNamedExpression Resolve ()
316 if (resolved != null)
319 DeclSpace root = RootContext.Tree.Types;
320 root.NamespaceEntry = NamespaceEntry;
321 resolved = Alias.ResolveAsTypeStep (root.EmitContext, false);
322 root.NamespaceEntry = null;
328 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
330 this.parent = parent;
332 this.IsImplicit = false;
336 ns = parent.NS.GetNamespace (name, true);
337 else if (name != null)
338 ns = Namespace.LookupNamespace (name, true);
341 ns.AddNamespaceEntry (this);
345 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
347 this.parent = parent;
349 this.IsImplicit = true;
355 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
356 // resolved as if the immediately containing namespace body has no using-directives.
358 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
359 // in the using-namespace-directive.
361 // To implement these rules, the expressions in the using directives are resolved using
362 // the "doppelganger" (ghostly bodiless duplicate).
364 NamespaceEntry doppelganger;
365 NamespaceEntry Doppelganger {
367 if (!IsImplicit && doppelganger == null)
368 doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
373 static int next_id = 0;
374 public readonly int ID;
375 public readonly bool IsImplicit;
377 public Namespace NS {
381 public NamespaceEntry Parent {
382 get { return parent; }
385 public NamespaceEntry ImplicitParent {
389 if (implicit_parent == null) {
390 implicit_parent = (parent.NS == ns.Parent)
392 : new NamespaceEntry (parent, file, ns.Parent);
394 return implicit_parent;
399 /// Records a new namespace for resolving name references
401 public void Using (MemberName name, Location loc)
403 if (DeclarationFound){
404 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
408 if (name.Equals (ns.MemberName))
411 if (using_clauses == null)
412 using_clauses = new ArrayList ();
414 foreach (UsingEntry old_entry in using_clauses) {
415 if (name.Equals (old_entry.Name)) {
416 if (RootContext.WarningLevel >= 3)
417 Report.Warning (105, loc, "The using directive for `{0}' appeared previously in this namespace", name);
423 UsingEntry ue = new UsingEntry (Doppelganger, name, loc);
424 using_clauses.Add (ue);
427 public void UsingAlias (string name, MemberName alias, Location loc)
429 if (DeclarationFound){
430 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
435 aliases = new Hashtable ();
437 if (aliases.Contains (name)) {
438 AliasEntry ae = (AliasEntry)aliases [name];
439 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
440 Report.Error (1537, loc, "The using alias `" + name +
441 "' appeared previously in this namespace");
445 aliases [name] = new AliasEntry (Doppelganger, name, alias, loc);
448 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
450 // Precondition: Only simple names (no dots) will be looked up with this function.
451 FullNamedExpression resolved = null;
452 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
453 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
459 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
461 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
462 name, t1.FullName, t2.FullName);
465 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
468 // Check whether it's in the namespace.
470 FullNamedExpression fne = NS.Lookup (ds, name);
480 if (aliases != null) {
481 AliasEntry entry = aliases [name] as AliasEntry;
483 return entry.Resolve ();
487 // Check using entries.
489 FullNamedExpression match = null;
490 foreach (Namespace using_ns in GetUsingTable ()) {
491 match = using_ns.Lookup (ds, name);
492 if (match == null || !(match is TypeExpr))
496 Error_AmbiguousTypeReference (loc, name, fne, match);
505 // Our cached computation.
506 readonly Namespace [] empty_namespaces = new Namespace [0];
507 Namespace [] namespace_using_table;
508 Namespace [] GetUsingTable ()
510 if (namespace_using_table != null)
511 return namespace_using_table;
513 if (using_clauses == null) {
514 namespace_using_table = empty_namespaces;
515 return namespace_using_table;
518 ArrayList list = new ArrayList (using_clauses.Count);
520 foreach (UsingEntry ue in using_clauses) {
521 Namespace using_ns = ue.Resolve ();
522 if (using_ns == null)
528 namespace_using_table = new Namespace [list.Count];
529 list.CopyTo (namespace_using_table, 0);
530 return namespace_using_table;
533 readonly string [] empty_using_list = new string [0];
535 public void DefineNamespace (SymbolWriter symwriter)
540 parent.DefineNamespace (symwriter);
542 string [] using_list = empty_using_list;
543 if (using_clauses != null) {
544 using_list = new string [using_clauses.Count];
545 for (int i = 0; i < using_clauses.Count; i++)
546 using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
549 int parent_id = parent != null ? parent.symfile_id : 0;
550 if (file.SourceFileEntry == null)
553 symfile_id = symwriter.DefineNamespace (
554 ns.Name, file.SourceFileEntry, using_list, parent_id);
557 public int SymbolFileID {
558 get { return symfile_id; }
561 static void MsgtryRef (string s)
563 Console.WriteLine (" Try using -r:" + s);
566 static void MsgtryPkg (string s)
568 Console.WriteLine (" Try using -pkg:" + s);
571 public static void Error_NamespaceNotFound (Location loc, string name)
573 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
577 case "Gtk": case "GtkSharp":
578 MsgtryPkg ("gtk-sharp");
581 case "Gdk": case "GdkSharp":
582 MsgtryPkg ("gdk-sharp");
585 case "Glade": case "GladeSharp":
586 MsgtryPkg ("glade-sharp");
589 case "System.Drawing":
590 case "System.Web.Services":
593 case "System.Windows.Forms":
600 /// Used to validate that all the using clauses are correct
601 /// after we are finished parsing all the files.
603 public void VerifyUsing ()
605 if (using_clauses != null) {
606 foreach (UsingEntry ue in using_clauses)
610 if (aliases != null) {
611 foreach (DictionaryEntry de in aliases) {
612 AliasEntry alias = (AliasEntry) de.Value;
613 if (alias.Resolve () == null)
614 Error_NamespaceNotFound (alias.Location, alias.Alias.ToString ());
619 public override string ToString ()
621 if (NS == Namespace.Root)
622 return "NamespaceEntry (<root>)";
624 return String.Format ("NamespaceEntry ({0},{1},{2})", ns.Name, IsImplicit, ID);