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);
165 else if (rt != null && t != rt && RootContext.WarningLevel >= 2) {
166 Report.SymbolRelatedToPreviousError (t);
167 Report.SymbolRelatedToPreviousError (rt);
168 Report.Warning (436, loc,
169 "Ignoring imported type `{0}' since the current assembly already has a declaration with the same name",
173 TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
174 cached_types [name] = te;
178 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
180 Namespace ns = GetNamespace (name, false);
184 TypeExpr te = LookupType (name, loc);
185 if (te == null || !ds.CheckAccessLevel (te.Type))
191 public void AddNamespaceEntry (NamespaceEntry entry)
196 public void AddDeclSpace (string name, DeclSpace ds)
198 declspaces.Add (name, ds);
201 static public ArrayList UserDefinedNamespaces {
202 get { return all_namespaces; }
206 /// The qualified name of the current namespace
209 get { return fullname; }
212 public override string FullName {
213 get { return fullname; }
217 /// The parent of this namespace, used by the parser to "Pop"
218 /// the current namespace declaration
220 public Namespace Parent {
221 get { return parent; }
224 public static void DefineNamespaces (SymbolWriter symwriter)
226 foreach (Namespace ns in all_namespaces) {
227 foreach (NamespaceEntry entry in ns.entries)
228 entry.DefineNamespace (symwriter);
233 /// Used to validate that all the using clauses are correct
234 /// after we are finished parsing all the files.
236 public static void VerifyUsing ()
238 foreach (Namespace ns in all_namespaces) {
239 foreach (NamespaceEntry entry in ns.entries)
240 entry.VerifyUsing ();
244 public override string ToString ()
247 return "Namespace (<root>)";
249 return String.Format ("Namespace ({0})", Name);
253 public class NamespaceEntry
256 NamespaceEntry parent, implicit_parent;
260 ArrayList using_clauses;
261 public bool DeclarationFound = false;
264 // This class holds the location where a using definition is
265 // done, and whether it has been used by the program or not.
267 // We use this to flag using clauses for namespaces that do not
270 public class UsingEntry {
271 public readonly MemberName Name;
272 readonly Expression Expr;
273 readonly NamespaceEntry NamespaceEntry;
274 readonly Location Location;
276 public UsingEntry (NamespaceEntry entry, MemberName name, Location loc)
279 Expr = name.GetTypeExpression (loc);
280 NamespaceEntry = entry;
284 internal Namespace resolved;
286 public Namespace Resolve ()
288 if (resolved != null)
291 DeclSpace root = RootContext.Tree.Types;
292 root.NamespaceEntry = NamespaceEntry;
293 FullNamedExpression fne = Expr.ResolveAsTypeStep (root.EmitContext, false);
294 root.NamespaceEntry = null;
297 Error_NamespaceNotFound (Location, Name.ToString ());
301 resolved = fne as Namespace;
302 if (resolved == null) {
303 Report.Error (138, Location,
304 "`{0} is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.ToString ());
310 public class AliasEntry {
311 public readonly string Name;
312 public readonly Expression Alias;
313 public readonly NamespaceEntry NamespaceEntry;
314 public readonly Location Location;
316 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
319 Alias = alias.GetTypeExpression (loc);
320 NamespaceEntry = entry;
324 FullNamedExpression resolved;
326 public FullNamedExpression Resolve ()
328 if (resolved != null)
331 DeclSpace root = RootContext.Tree.Types;
332 root.NamespaceEntry = NamespaceEntry;
333 resolved = Alias.ResolveAsTypeStep (root.EmitContext, false);
334 root.NamespaceEntry = null;
340 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
342 this.parent = parent;
344 this.IsImplicit = false;
348 ns = parent.NS.GetNamespace (name, true);
349 else if (name != null)
350 ns = Namespace.LookupNamespace (name, true);
353 ns.AddNamespaceEntry (this);
357 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
359 this.parent = parent;
361 this.IsImplicit = true;
367 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
368 // resolved as if the immediately containing namespace body has no using-directives.
370 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
371 // in the using-namespace-directive.
373 // To implement these rules, the expressions in the using directives are resolved using
374 // the "doppelganger" (ghostly bodiless duplicate).
376 NamespaceEntry doppelganger;
377 NamespaceEntry Doppelganger {
379 if (!IsImplicit && doppelganger == null)
380 doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
385 static int next_id = 0;
386 public readonly int ID;
387 public readonly bool IsImplicit;
389 public Namespace NS {
393 public NamespaceEntry Parent {
394 get { return parent; }
397 public NamespaceEntry ImplicitParent {
401 if (implicit_parent == null) {
402 implicit_parent = (parent.NS == ns.Parent)
404 : new NamespaceEntry (parent, file, ns.Parent);
406 return implicit_parent;
411 /// Records a new namespace for resolving name references
413 public void Using (MemberName name, Location loc)
415 if (DeclarationFound){
416 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
420 if (name.Equals (ns.MemberName))
423 if (using_clauses == null)
424 using_clauses = new ArrayList ();
426 foreach (UsingEntry old_entry in using_clauses) {
427 if (name.Equals (old_entry.Name)) {
428 if (RootContext.WarningLevel >= 3)
429 Report.Warning (105, loc, "The using directive for `{0}' appeared previously in this namespace", name);
435 UsingEntry ue = new UsingEntry (Doppelganger, name, loc);
436 using_clauses.Add (ue);
439 public void UsingAlias (string name, MemberName alias, Location loc)
441 if (DeclarationFound){
442 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
447 aliases = new Hashtable ();
449 if (aliases.Contains (name)) {
450 AliasEntry ae = (AliasEntry)aliases [name];
451 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
452 Report.Error (1537, loc, "The using alias `" + name +
453 "' appeared previously in this namespace");
457 aliases [name] = new AliasEntry (Doppelganger, name, alias, loc);
460 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
462 // Precondition: Only simple names (no dots) will be looked up with this function.
463 FullNamedExpression resolved = null;
464 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
465 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
471 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
473 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
474 name, t1.FullName, t2.FullName);
477 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
480 // Check whether it's in the namespace.
482 FullNamedExpression fne = NS.Lookup (ds, name, loc);
492 if (aliases != null) {
493 AliasEntry entry = aliases [name] as AliasEntry;
495 return entry.Resolve ();
499 // Check using entries.
501 FullNamedExpression match = null;
502 foreach (Namespace using_ns in GetUsingTable ()) {
503 match = using_ns.Lookup (ds, name, loc);
504 if (match == null || !(match is TypeExpr))
508 Error_AmbiguousTypeReference (loc, name, fne, match);
517 // Our cached computation.
518 readonly Namespace [] empty_namespaces = new Namespace [0];
519 Namespace [] namespace_using_table;
520 Namespace [] GetUsingTable ()
522 if (namespace_using_table != null)
523 return namespace_using_table;
525 if (using_clauses == null) {
526 namespace_using_table = empty_namespaces;
527 return namespace_using_table;
530 ArrayList list = new ArrayList (using_clauses.Count);
532 foreach (UsingEntry ue in using_clauses) {
533 Namespace using_ns = ue.Resolve ();
534 if (using_ns == null)
540 namespace_using_table = new Namespace [list.Count];
541 list.CopyTo (namespace_using_table, 0);
542 return namespace_using_table;
545 readonly string [] empty_using_list = new string [0];
547 public void DefineNamespace (SymbolWriter symwriter)
552 parent.DefineNamespace (symwriter);
554 string [] using_list = empty_using_list;
555 if (using_clauses != null) {
556 using_list = new string [using_clauses.Count];
557 for (int i = 0; i < using_clauses.Count; i++)
558 using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
561 int parent_id = parent != null ? parent.symfile_id : 0;
562 if (file.SourceFileEntry == null)
565 symfile_id = symwriter.DefineNamespace (
566 ns.Name, file.SourceFileEntry, using_list, parent_id);
569 public int SymbolFileID {
570 get { return symfile_id; }
573 static void MsgtryRef (string s)
575 Console.WriteLine (" Try using -r:" + s);
578 static void MsgtryPkg (string s)
580 Console.WriteLine (" Try using -pkg:" + s);
583 public static void Error_NamespaceNotFound (Location loc, string name)
585 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
589 case "Gtk": case "GtkSharp":
590 MsgtryPkg ("gtk-sharp");
593 case "Gdk": case "GdkSharp":
594 MsgtryPkg ("gdk-sharp");
597 case "Glade": case "GladeSharp":
598 MsgtryPkg ("glade-sharp");
601 case "System.Drawing":
602 case "System.Web.Services":
605 case "System.Windows.Forms":
612 /// Used to validate that all the using clauses are correct
613 /// after we are finished parsing all the files.
615 public void VerifyUsing ()
617 if (using_clauses != null) {
618 foreach (UsingEntry ue in using_clauses)
622 if (aliases != null) {
623 foreach (DictionaryEntry de in aliases) {
624 AliasEntry alias = (AliasEntry) de.Value;
625 if (alias.Resolve () == null)
626 Error_NamespaceNotFound (alias.Location, alias.Alias.ToString ());
631 public override string ToString ()
633 if (NS == Namespace.Root)
634 return "NamespaceEntry (<root>)";
636 return String.Format ("NamespaceEntry ({0},{1},{2})", ns.Name, IsImplicit, ID);