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;
70 throw new InternalErrorException ("Namespace has a null fullname");
72 if (parent != null && parent.MemberName != MemberName.Null)
73 MemberName = new MemberName (parent.MemberName, name);
75 MemberName = MemberName.Null;
77 MemberName = new MemberName (name);
79 entries = new ArrayList ();
80 namespaces = new Hashtable ();
81 declspaces = new Hashtable ();
82 cached_types = new Hashtable ();
84 all_namespaces.Add (this);
85 if (namespaces_map.Contains (fullname))
87 namespaces_map [fullname] = true;
90 public override Expression DoResolve (EmitContext ec)
95 public override void Emit (EmitContext ec)
97 throw new InternalErrorException ("Expression tree referenced namespace " + fullname + " during Emit ()");
100 public static bool IsNamespace (string name)
102 return namespaces_map [name] != null;
105 public override string GetSignatureForError ()
107 return Name.Length == 0 ? "::global" : Name;
110 public Namespace GetNamespace (string name, bool create)
112 int pos = name.IndexOf ('.');
117 first = name.Substring (0, pos);
121 ns = (Namespace) namespaces [first];
126 ns = new Namespace (this, first);
127 namespaces.Add (first, ns);
131 ns = ns.GetNamespace (name.Substring (pos + 1), create);
136 public static Namespace LookupNamespace (string name, bool create)
138 return Root.GetNamespace (name, create);
141 TypeExpr LookupType (string name, Location loc)
143 if (cached_types.Contains (name))
144 return cached_types [name] as TypeExpr;
147 DeclSpace tdecl = declspaces [name] as DeclSpace;
150 // Note that this is not:
152 // t = tdecl.DefineType ()
154 // This is to make it somewhat more useful when a DefineType
155 // fails due to problems in nested types (more useful in the sense
156 // of fewer misleading error messages)
159 t = tdecl.TypeBuilder;
161 string lookup = t != null ? t.FullName : (fullname == "" ? name : fullname + "." + name);
162 Type rt = TypeManager.LookupTypeReflection (lookup, loc);
166 TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
167 cached_types [name] = te;
171 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
173 Namespace ns = GetNamespace (name, false);
177 TypeExpr te = LookupType (name, loc);
178 if (te == null || !ds.CheckAccessLevel (te.Type))
184 public void AddNamespaceEntry (NamespaceEntry entry)
189 public void AddDeclSpace (string name, DeclSpace ds)
191 declspaces.Add (name, ds);
194 static public ArrayList UserDefinedNamespaces {
195 get { return all_namespaces; }
199 /// The qualified name of the current namespace
202 get { return fullname; }
205 public override string FullName {
206 get { return fullname; }
210 /// The parent of this namespace, used by the parser to "Pop"
211 /// the current namespace declaration
213 public Namespace Parent {
214 get { return parent; }
217 public static void DefineNamespaces (SymbolWriter symwriter)
219 foreach (Namespace ns in all_namespaces) {
220 foreach (NamespaceEntry entry in ns.entries)
221 entry.DefineNamespace (symwriter);
226 /// Used to validate that all the using clauses are correct
227 /// after we are finished parsing all the files.
229 public static void VerifyUsing ()
231 foreach (Namespace ns in all_namespaces) {
232 foreach (NamespaceEntry entry in ns.entries)
233 entry.VerifyUsing ();
237 public override string ToString ()
240 return "Namespace (<root>)";
242 return String.Format ("Namespace ({0})", Name);
246 public class NamespaceEntry
249 NamespaceEntry parent, implicit_parent;
253 ArrayList using_clauses;
254 public bool DeclarationFound = false;
257 // This class holds the location where a using definition is
258 // done, and whether it has been used by the program or not.
260 // We use this to flag using clauses for namespaces that do not
263 public class UsingEntry {
264 public readonly MemberName Name;
265 readonly Expression Expr;
266 readonly NamespaceEntry NamespaceEntry;
267 readonly Location Location;
269 public UsingEntry (NamespaceEntry entry, MemberName name, Location loc)
272 Expr = name.GetTypeExpression (loc);
273 NamespaceEntry = entry;
277 internal Namespace resolved;
279 public Namespace Resolve ()
281 if (resolved != null)
284 DeclSpace root = RootContext.Tree.Types;
285 root.NamespaceEntry = NamespaceEntry;
286 FullNamedExpression fne = Expr.ResolveAsTypeStep (root.EmitContext, false);
287 root.NamespaceEntry = null;
290 Error_NamespaceNotFound (Location, Name.ToString ());
294 resolved = fne as Namespace;
295 if (resolved == null) {
296 Report.Error (138, Location,
297 "`{0} is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.ToString ());
303 public class AliasEntry {
304 public readonly string Name;
305 public readonly Expression Alias;
306 public readonly NamespaceEntry NamespaceEntry;
307 public readonly Location Location;
309 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
312 Alias = alias.GetTypeExpression (loc);
313 NamespaceEntry = entry;
317 FullNamedExpression resolved;
319 public FullNamedExpression Resolve ()
321 if (resolved != null)
324 DeclSpace root = RootContext.Tree.Types;
325 root.NamespaceEntry = NamespaceEntry;
326 resolved = Alias.ResolveAsTypeStep (root.EmitContext, false);
327 root.NamespaceEntry = null;
333 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
335 this.parent = parent;
337 this.IsImplicit = false;
341 ns = parent.NS.GetNamespace (name, true);
342 else if (name != null)
343 ns = Namespace.LookupNamespace (name, true);
346 ns.AddNamespaceEntry (this);
350 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
352 this.parent = parent;
354 this.IsImplicit = true;
360 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
361 // resolved as if the immediately containing namespace body has no using-directives.
363 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
364 // in the using-namespace-directive.
366 // To implement these rules, the expressions in the using directives are resolved using
367 // the "doppelganger" (ghostly bodiless duplicate).
369 NamespaceEntry doppelganger;
370 NamespaceEntry Doppelganger {
372 if (!IsImplicit && doppelganger == null)
373 doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
378 static int next_id = 0;
379 public readonly int ID;
380 public readonly bool IsImplicit;
382 public Namespace NS {
386 public NamespaceEntry Parent {
387 get { return parent; }
390 public NamespaceEntry ImplicitParent {
394 if (implicit_parent == null) {
395 implicit_parent = (parent.NS == ns.Parent)
397 : new NamespaceEntry (parent, file, ns.Parent);
399 return implicit_parent;
404 /// Records a new namespace for resolving name references
406 public void Using (MemberName name, Location loc)
408 if (DeclarationFound){
409 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
413 if (name.Equals (ns.MemberName))
416 if (using_clauses == null)
417 using_clauses = new ArrayList ();
419 foreach (UsingEntry old_entry in using_clauses) {
420 if (name.Equals (old_entry.Name)) {
421 if (RootContext.WarningLevel >= 3)
422 Report.Warning (105, loc, "The using directive for `{0}' appeared previously in this namespace", name);
428 UsingEntry ue = new UsingEntry (Doppelganger, name, loc);
429 using_clauses.Add (ue);
432 public void UsingAlias (string name, MemberName alias, Location loc)
434 if (DeclarationFound){
435 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
440 aliases = new Hashtable ();
442 if (aliases.Contains (name)) {
443 AliasEntry ae = (AliasEntry)aliases [name];
444 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
445 Report.Error (1537, loc, "The using alias `" + name +
446 "' appeared previously in this namespace");
450 aliases [name] = new AliasEntry (Doppelganger, name, alias, loc);
453 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
455 // Precondition: Only simple names (no dots) will be looked up with this function.
456 FullNamedExpression resolved = null;
457 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
458 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
464 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
466 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
467 name, t1.FullName, t2.FullName);
470 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
473 // Check whether it's in the namespace.
475 FullNamedExpression fne = NS.Lookup (ds, name, loc);
485 if (aliases != null) {
486 AliasEntry entry = aliases [name] as AliasEntry;
488 return entry.Resolve ();
492 // Check using entries.
494 FullNamedExpression match = null;
495 foreach (Namespace using_ns in GetUsingTable ()) {
496 match = using_ns.Lookup (ds, name, loc);
497 if (match == null || !(match is TypeExpr))
501 Error_AmbiguousTypeReference (loc, name, fne, match);
510 // Our cached computation.
511 readonly Namespace [] empty_namespaces = new Namespace [0];
512 Namespace [] namespace_using_table;
513 Namespace [] GetUsingTable ()
515 if (namespace_using_table != null)
516 return namespace_using_table;
518 if (using_clauses == null) {
519 namespace_using_table = empty_namespaces;
520 return namespace_using_table;
523 ArrayList list = new ArrayList (using_clauses.Count);
525 foreach (UsingEntry ue in using_clauses) {
526 Namespace using_ns = ue.Resolve ();
527 if (using_ns == null)
533 namespace_using_table = new Namespace [list.Count];
534 list.CopyTo (namespace_using_table, 0);
535 return namespace_using_table;
538 readonly string [] empty_using_list = new string [0];
540 public void DefineNamespace (SymbolWriter symwriter)
545 parent.DefineNamespace (symwriter);
547 string [] using_list = empty_using_list;
548 if (using_clauses != null) {
549 using_list = new string [using_clauses.Count];
550 for (int i = 0; i < using_clauses.Count; i++)
551 using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
554 int parent_id = parent != null ? parent.symfile_id : 0;
555 if (file.SourceFileEntry == null)
558 symfile_id = symwriter.DefineNamespace (
559 ns.Name, file.SourceFileEntry, using_list, parent_id);
562 public int SymbolFileID {
563 get { return symfile_id; }
566 static void MsgtryRef (string s)
568 Console.WriteLine (" Try using -r:" + s);
571 static void MsgtryPkg (string s)
573 Console.WriteLine (" Try using -pkg:" + s);
576 public static void Error_NamespaceNotFound (Location loc, string name)
578 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
582 case "Gtk": case "GtkSharp":
583 MsgtryPkg ("gtk-sharp");
586 case "Gdk": case "GdkSharp":
587 MsgtryPkg ("gdk-sharp");
590 case "Glade": case "GladeSharp":
591 MsgtryPkg ("glade-sharp");
594 case "System.Drawing":
595 case "System.Web.Services":
598 case "System.Windows.Forms":
605 /// Used to validate that all the using clauses are correct
606 /// after we are finished parsing all the files.
608 public void VerifyUsing ()
610 if (using_clauses != null) {
611 foreach (UsingEntry ue in using_clauses)
615 if (aliases != null) {
616 foreach (DictionaryEntry de in aliases) {
617 AliasEntry alias = (AliasEntry) de.Value;
618 if (alias.Resolve () == null)
619 Error_NamespaceNotFound (alias.Location, alias.Alias.ToString ());
624 public override string ToString ()
626 if (NS == Namespace.Root)
627 return "NamespaceEntry (<root>)";
629 return String.Format ("NamespaceEntry ({0},{1},{2})", ns.Name, IsImplicit, ID);