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 MemberName Name;
257 public Expression Expr;
258 public readonly NamespaceEntry NamespaceEntry;
259 public readonly Location Location;
261 public UsingEntry (NamespaceEntry entry, MemberName name, Location loc)
264 Expr = name.GetTypeExpression (loc);
265 NamespaceEntry = entry;
269 internal FullNamedExpression resolved;
271 public Namespace Resolve ()
273 if (resolved != null)
274 return resolved as Namespace;
276 DeclSpace root = RootContext.Tree.Types;
277 root.NamespaceEntry = NamespaceEntry;
278 resolved = Expr.ResolveAsTypeStep (root.EmitContext);
279 root.NamespaceEntry = null;
281 return resolved as Namespace;
285 public class AliasEntry {
286 public readonly string Name;
287 public readonly Expression Alias;
288 public readonly NamespaceEntry NamespaceEntry;
289 public readonly Location Location;
291 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
294 Alias = alias.GetTypeExpression (loc);
295 NamespaceEntry = entry;
299 FullNamedExpression resolved;
301 public FullNamedExpression Resolve ()
303 if (resolved != null)
306 DeclSpace root = RootContext.Tree.Types;
307 root.NamespaceEntry = NamespaceEntry;
308 resolved = Alias.ResolveAsTypeStep (root.EmitContext);
309 root.NamespaceEntry = null;
315 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
317 this.parent = parent;
319 this.IsImplicit = false;
323 ns = parent.NS.GetNamespace (name, true);
324 else if (name != null)
325 ns = Namespace.LookupNamespace (name, true);
328 ns.AddNamespaceEntry (this);
332 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
334 this.parent = parent;
336 this.IsImplicit = true;
342 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
343 // resolved as if the immediately containing namespace body has no using-directives.
345 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
346 // in the using-namespace-directive.
348 // To implement these rules, the expressions in the using directives are resolved using
349 // the "doppelganger" (ghostly bodiless duplicate).
351 NamespaceEntry doppelganger;
352 NamespaceEntry Doppelganger {
354 if (!IsImplicit && doppelganger == null)
355 doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
360 static int next_id = 0;
361 public readonly int ID;
362 public readonly bool IsImplicit;
364 public Namespace NS {
370 public NamespaceEntry Parent {
376 public NamespaceEntry ImplicitParent {
380 if (implicit_parent == null) {
381 implicit_parent = (parent.NS == ns.Parent)
383 : new NamespaceEntry (parent, file, ns.Parent);
385 return implicit_parent;
390 /// Records a new namespace for resolving name references
392 public void Using (MemberName name, Location loc)
394 if (DeclarationFound){
395 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
399 if (name.Equals (ns.MemberName))
402 if (using_clauses == null)
403 using_clauses = new ArrayList ();
405 foreach (UsingEntry old_entry in using_clauses) {
406 if (name.Equals (old_entry.Name)) {
407 if (RootContext.WarningLevel >= 3)
408 Report.Warning (105, loc, "The using directive for `{0}' appeared previously in this namespace", name);
414 UsingEntry ue = new UsingEntry (Doppelganger, name, loc);
415 using_clauses.Add (ue);
418 public void UsingAlias (string name, MemberName alias, Location loc)
420 if (DeclarationFound){
421 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
426 aliases = new Hashtable ();
428 if (aliases.Contains (name)){
429 AliasEntry ae = (AliasEntry)aliases [name];
430 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
431 Report.Error (1537, loc, "The using alias `" + name +
432 "' appeared previously in this namespace");
436 aliases [name] = new AliasEntry (Doppelganger, name, alias, loc);
439 public FullNamedExpression LookupAlias (string alias)
441 AliasEntry entry = null;
443 entry = (AliasEntry) aliases [alias];
445 return entry == null ? null : entry.Resolve ();
448 static readonly char [] dot_array = { '.' };
450 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
452 FullNamedExpression resolved = null;
455 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
456 int pos = name.IndexOf ('.');
458 rest = name.Substring (pos + 1);
459 name = name.Substring (0, pos);
462 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
463 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
467 if (resolved == null || rest == null)
470 // Now handle the rest of the the name.
471 string [] elements = rest.Split (dot_array);
472 int count = elements.Length;
474 while (i < count && resolved != null && resolved is Namespace) {
475 Namespace ns = resolved as Namespace;
476 resolved = ns.Lookup (ds, elements [i++], loc);
479 if (resolved == null || resolved is Namespace)
482 Type t = ((TypeExpr) resolved).Type;
485 if (ds != null && !ds.CheckAccessLevel (t))
488 return new TypeExpression (t, Location.Null);
489 t = TypeManager.GetNestedType (t, elements [i++]);
495 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
497 // Precondition: Only simple names (no dots) will be looked up with this function.
500 // Check whether it's in the namespace.
502 FullNamedExpression o = NS.Lookup (ds, name, loc);
512 o = LookupAlias (name);
517 // Check using entries.
519 FullNamedExpression t = null, match = null;
520 foreach (Namespace using_ns in GetUsingTable ()) {
521 match = using_ns.Lookup (ds, name, loc);
522 if ((match != null) && (match is TypeExpr)) {
525 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.FullName, match.FullName);
537 // Our cached computation.
538 Namespace [] namespace_using_table;
539 public Namespace[] GetUsingTable ()
541 if (namespace_using_table != null)
542 return namespace_using_table;
544 if (using_clauses == null) {
545 namespace_using_table = new Namespace [0];
546 return namespace_using_table;
549 ArrayList list = new ArrayList (using_clauses.Count);
551 foreach (UsingEntry ue in using_clauses) {
552 Namespace using_ns = ue.Resolve ();
553 if (using_ns == null)
559 namespace_using_table = new Namespace [list.Count];
560 list.CopyTo (namespace_using_table, 0);
561 return namespace_using_table;
564 public void DefineNamespace (SymbolWriter symwriter)
569 parent.DefineNamespace (symwriter);
572 if (using_clauses != null) {
573 using_list = new string [using_clauses.Count];
574 for (int i = 0; i < using_clauses.Count; i++)
575 using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
577 using_list = new string [0];
580 int parent_id = parent != null ? parent.symfile_id : 0;
581 if (file.SourceFileEntry == null)
584 symfile_id = symwriter.DefineNamespace (
585 ns.Name, file.SourceFileEntry, using_list, parent_id);
588 public int SymbolFileID {
594 static void MsgtryRef (string s)
596 Console.WriteLine (" Try using -r:" + s);
599 static void MsgtryPkg (string s)
601 Console.WriteLine (" Try using -pkg:" + s);
604 public static void Error_NamespaceNotFound (Location loc, string name)
606 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
610 case "Gtk": case "GtkSharp":
611 MsgtryPkg ("gtk-sharp");
614 case "Gdk": case "GdkSharp":
615 MsgtryPkg ("gdk-sharp");
618 case "Glade": case "GladeSharp":
619 MsgtryPkg ("glade-sharp");
622 case "System.Drawing":
623 case "System.Web.Services":
626 case "System.Windows.Forms":
633 /// Used to validate that all the using clauses are correct
634 /// after we are finished parsing all the files.
636 public void VerifyUsing ()
638 if (using_clauses != null){
639 foreach (UsingEntry ue in using_clauses){
640 if (ue.Resolve () != null)
643 if (ue.resolved == null)
644 Error_NamespaceNotFound (ue.Location, ue.Name.ToString ());
646 Report.Error (138, ue.Location,
647 "`{0} is a type not a namespace. A using namespace directive can only be applied to namespaces", ue.Name);
652 if (aliases != null){
653 foreach (DictionaryEntry de in aliases){
654 AliasEntry alias = (AliasEntry) de.Value;
656 if (alias.Resolve () != null)
659 Error_NamespaceNotFound (alias.Location, alias.Alias.ToString ());
664 public override string ToString ()
666 if (NS == Namespace.Root)
667 return "NamespaceEntry (<root>)";
669 return String.Format ("NamespaceEntry ({0},{1},{2})", ns.Name, IsImplicit, ID);