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 (parent.MemberName, name);
76 MemberName = MemberName.Null;
78 MemberName = new MemberName (name);
80 entries = new ArrayList ();
81 namespaces = 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 if (declspaces != null) {
148 DeclSpace tdecl = declspaces [name] as DeclSpace;
151 // Note that this is not:
153 // t = tdecl.DefineType ()
155 // This is to make it somewhat more useful when a DefineType
156 // fails due to problems in nested types (more useful in the sense
157 // of fewer misleading error messages)
160 t = tdecl.TypeBuilder;
163 string lookup = t != null ? t.FullName : (fullname == "" ? name : fullname + "." + name);
164 Type rt = TypeManager.LookupTypeReflection (lookup, loc);
168 TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
169 cached_types [name] = te;
173 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
175 Namespace ns = GetNamespace (name, false);
179 TypeExpr te = LookupType (name, loc);
180 if (te == null || !ds.CheckAccessLevel (te.Type))
186 public void AddNamespaceEntry (NamespaceEntry entry)
191 public void AddDeclSpace (string name, DeclSpace ds)
193 if (declspaces == null)
194 declspaces = new HybridDictionary ();
195 declspaces.Add (name, ds);
198 static public ArrayList UserDefinedNamespaces {
199 get { return all_namespaces; }
203 /// The qualified name of the current namespace
206 get { return fullname; }
209 public override string FullName {
210 get { return fullname; }
214 /// The parent of this namespace, used by the parser to "Pop"
215 /// the current namespace declaration
217 public Namespace Parent {
218 get { return parent; }
221 public static void DefineNamespaces (SymbolWriter symwriter)
223 foreach (Namespace ns in all_namespaces) {
224 foreach (NamespaceEntry entry in ns.entries)
225 entry.DefineNamespace (symwriter);
230 /// Used to validate that all the using clauses are correct
231 /// after we are finished parsing all the files.
233 public static void VerifyUsing ()
235 foreach (Namespace ns in all_namespaces) {
236 foreach (NamespaceEntry entry in ns.entries)
237 entry.VerifyUsing ();
241 public override string ToString ()
244 return "Namespace (<root>)";
246 return String.Format ("Namespace ({0})", Name);
250 public class NamespaceEntry
253 NamespaceEntry parent, implicit_parent;
257 ArrayList using_clauses;
258 public bool DeclarationFound = false;
261 // This class holds the location where a using definition is
262 // done, and whether it has been used by the program or not.
264 // We use this to flag using clauses for namespaces that do not
267 public class UsingEntry {
268 public readonly MemberName Name;
269 readonly Expression Expr;
270 readonly NamespaceEntry NamespaceEntry;
271 readonly Location Location;
273 public UsingEntry (NamespaceEntry entry, MemberName name, Location loc)
276 Expr = name.GetTypeExpression (loc);
277 NamespaceEntry = entry;
281 internal Namespace resolved;
283 public Namespace Resolve ()
285 if (resolved != null)
288 DeclSpace root = RootContext.Tree.Types;
289 root.NamespaceEntry = NamespaceEntry;
290 FullNamedExpression fne = Expr.ResolveAsTypeStep (root.EmitContext, false);
291 root.NamespaceEntry = null;
294 Error_NamespaceNotFound (Location, Name.ToString ());
298 resolved = fne as Namespace;
299 if (resolved == null) {
300 Report.Error (138, Location,
301 "`{0} is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.ToString ());
307 public class AliasEntry {
308 public readonly string Name;
309 public readonly Expression Alias;
310 public readonly NamespaceEntry NamespaceEntry;
311 public readonly Location Location;
313 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
316 Alias = alias.GetTypeExpression (loc);
317 NamespaceEntry = entry;
321 FullNamedExpression resolved;
323 public FullNamedExpression Resolve ()
325 if (resolved != null)
328 DeclSpace root = RootContext.Tree.Types;
329 root.NamespaceEntry = NamespaceEntry;
330 resolved = Alias.ResolveAsTypeStep (root.EmitContext, false);
331 root.NamespaceEntry = null;
337 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
339 this.parent = parent;
341 this.IsImplicit = false;
345 ns = parent.NS.GetNamespace (name, true);
346 else if (name != null)
347 ns = Namespace.LookupNamespace (name, true);
350 ns.AddNamespaceEntry (this);
354 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
356 this.parent = parent;
358 this.IsImplicit = true;
364 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
365 // resolved as if the immediately containing namespace body has no using-directives.
367 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
368 // in the using-namespace-directive.
370 // To implement these rules, the expressions in the using directives are resolved using
371 // the "doppelganger" (ghostly bodiless duplicate).
373 NamespaceEntry doppelganger;
374 NamespaceEntry Doppelganger {
376 if (!IsImplicit && doppelganger == null)
377 doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
382 static int next_id = 0;
383 public readonly int ID;
384 public readonly bool IsImplicit;
386 public Namespace NS {
390 public NamespaceEntry Parent {
391 get { return parent; }
394 public NamespaceEntry ImplicitParent {
398 if (implicit_parent == null) {
399 implicit_parent = (parent.NS == ns.Parent)
401 : new NamespaceEntry (parent, file, ns.Parent);
403 return implicit_parent;
408 /// Records a new namespace for resolving name references
410 public void Using (MemberName name, Location loc)
412 if (DeclarationFound){
413 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
417 if (name.Equals (ns.MemberName))
420 if (using_clauses == null)
421 using_clauses = new ArrayList ();
423 foreach (UsingEntry old_entry in using_clauses) {
424 if (name.Equals (old_entry.Name)) {
425 if (RootContext.WarningLevel >= 3)
426 Report.Warning (105, loc, "The using directive for `{0}' appeared previously in this namespace", name);
432 UsingEntry ue = new UsingEntry (Doppelganger, name, loc);
433 using_clauses.Add (ue);
436 public void UsingAlias (string name, MemberName alias, Location loc)
438 if (DeclarationFound){
439 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
444 aliases = new Hashtable ();
446 if (aliases.Contains (name)) {
447 AliasEntry ae = (AliasEntry)aliases [name];
448 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
449 Report.Error (1537, loc, "The using alias `" + name +
450 "' appeared previously in this namespace");
454 aliases [name] = new AliasEntry (Doppelganger, name, alias, loc);
457 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
459 // Precondition: Only simple names (no dots) will be looked up with this function.
460 FullNamedExpression resolved = null;
461 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
462 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
468 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
470 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
471 name, t1.FullName, t2.FullName);
474 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
477 // Check whether it's in the namespace.
479 FullNamedExpression fne = NS.Lookup (ds, name, loc);
489 if (aliases != null) {
490 AliasEntry entry = aliases [name] as AliasEntry;
492 return entry.Resolve ();
496 // Check using entries.
498 FullNamedExpression match = null;
499 foreach (Namespace using_ns in GetUsingTable ()) {
500 match = using_ns.Lookup (ds, name, loc);
501 if (match == null || !(match is TypeExpr))
505 Error_AmbiguousTypeReference (loc, name, fne, match);
514 // Our cached computation.
515 readonly Namespace [] empty_namespaces = new Namespace [0];
516 Namespace [] namespace_using_table;
517 Namespace [] GetUsingTable ()
519 if (namespace_using_table != null)
520 return namespace_using_table;
522 if (using_clauses == null) {
523 namespace_using_table = empty_namespaces;
524 return namespace_using_table;
527 ArrayList list = new ArrayList (using_clauses.Count);
529 foreach (UsingEntry ue in using_clauses) {
530 Namespace using_ns = ue.Resolve ();
531 if (using_ns == null)
537 namespace_using_table = new Namespace [list.Count];
538 list.CopyTo (namespace_using_table, 0);
539 return namespace_using_table;
542 readonly string [] empty_using_list = new string [0];
544 public void DefineNamespace (SymbolWriter symwriter)
549 parent.DefineNamespace (symwriter);
551 string [] using_list = empty_using_list;
552 if (using_clauses != null) {
553 using_list = new string [using_clauses.Count];
554 for (int i = 0; i < using_clauses.Count; i++)
555 using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
558 int parent_id = parent != null ? parent.symfile_id : 0;
559 if (file.SourceFileEntry == null)
562 symfile_id = symwriter.DefineNamespace (
563 ns.Name, file.SourceFileEntry, using_list, parent_id);
566 public int SymbolFileID {
567 get { return symfile_id; }
570 static void MsgtryRef (string s)
572 Console.WriteLine (" Try using -r:" + s);
575 static void MsgtryPkg (string s)
577 Console.WriteLine (" Try using -pkg:" + s);
580 public static void Error_NamespaceNotFound (Location loc, string name)
582 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
586 case "Gtk": case "GtkSharp":
587 MsgtryPkg ("gtk-sharp");
590 case "Gdk": case "GdkSharp":
591 MsgtryPkg ("gdk-sharp");
594 case "Glade": case "GladeSharp":
595 MsgtryPkg ("glade-sharp");
598 case "System.Drawing":
599 case "System.Web.Services":
602 case "System.Windows.Forms":
609 /// Used to validate that all the using clauses are correct
610 /// after we are finished parsing all the files.
612 public void VerifyUsing ()
614 if (using_clauses != null) {
615 foreach (UsingEntry ue in using_clauses)
619 if (aliases != null) {
620 foreach (DictionaryEntry de in aliases) {
621 AliasEntry alias = (AliasEntry) de.Value;
622 if (alias.Resolve () == null)
623 Error_NamespaceNotFound (alias.Location, alias.Alias.ToString ());
628 public string GetSignatureForError ()
630 if (NS == Namespace.Root)
636 public override string ToString ()
638 if (NS == Namespace.Root)
639 return "NamespaceEntry (<root>)";
641 return String.Format ("NamespaceEntry ({0},{1},{2})", ns.Name, IsImplicit, ID);