* Version.cs (CompareTo, Equals): Make sure the versions for
[mono-project.git] / mcs / gmcs / namespace.cs
blobec0a8347be7f965ea04df864df213b262a8c2377
1 //
2 // namespace.cs: Tracks namespaces
3 //
4 // Author:
5 // Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 using System;
10 using System.Collections;
12 namespace Mono.CSharp {
14 /// <summary>
15 /// Keeps track of the namespaces defined in the C# code.
16 ///
17 /// This is an Expression to allow it to be referenced in the
18 /// compiler parse/intermediate tree during name resolution.
19 /// </summary>
20 public class Namespace : FullNamedExpression, IAlias {
21 static ArrayList all_namespaces = new ArrayList ();
22 static Hashtable namespaces_map = new Hashtable ();
24 Namespace parent;
25 string fullname;
26 ArrayList entries;
27 Hashtable namespaces;
28 Hashtable defined_names;
30 /// <summary>
31 /// Constructor Takes the current namespace and the
32 /// name. This is bootstrapped with parent == null
33 /// and name = ""
34 /// </summary>
35 public Namespace (Namespace parent, string name)
37 // Expression members.
38 this.eclass = ExprClass.Namespace;
39 this.Type = null;
40 this.loc = Location.Null;
42 this.parent = parent;
44 string pname = parent != null ? parent.Name : "";
46 if (pname == "")
47 fullname = name;
48 else
49 fullname = parent.Name + "." + name;
51 entries = new ArrayList ();
52 namespaces = new Hashtable ();
53 defined_names = new Hashtable ();
55 all_namespaces.Add (this);
56 if (namespaces_map.Contains (fullname))
57 return;
58 namespaces_map [fullname] = true;
61 public override Expression DoResolve (EmitContext ec)
63 return this;
66 public override void Emit (EmitContext ec)
68 throw new InternalErrorException ("Expression tree referenced namespace " + fullname + " during Emit ()");
71 public static bool IsNamespace (string name)
73 return namespaces_map [name] != null;
76 public static Namespace Root = new Namespace (null, "");
78 public Namespace GetNamespace (string name, bool create)
80 int pos = name.IndexOf ('.');
82 Namespace ns;
83 string first;
84 if (pos >= 0)
85 first = name.Substring (0, pos);
86 else
87 first = name;
89 ns = (Namespace) namespaces [first];
90 if (ns == null) {
91 if (!create)
92 return null;
94 ns = new Namespace (this, first);
95 namespaces.Add (first, ns);
98 if (pos >= 0)
99 ns = ns.GetNamespace (name.Substring (pos + 1), create);
101 return ns;
104 public static Namespace LookupNamespace (string name, bool create)
106 return Root.GetNamespace (name, create);
109 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
111 IAlias o = (IAlias) defined_names [name];
113 Type t;
114 DeclSpace tdecl = o as DeclSpace;
115 if (tdecl != null) {
116 t = tdecl.DefineType ();
117 if (t == null)
118 return null;
120 if ((ds == null) || ds.CheckAccessLevel (t))
121 return new TypeExpression (t, Location.Null);
124 Namespace ns = GetNamespace (name, false);
125 if (ns != null)
126 return ns;
128 t = TypeManager.LookupType (DeclSpace.MakeFQN (fullname, name));
129 if ((t == null) || ((ds != null) && !ds.CheckAccessLevel (t)))
130 return null;
132 return new TypeExpression (t, Location.Null);
135 public void AddNamespaceEntry (NamespaceEntry entry)
137 entries.Add (entry);
140 public void DefineName (string name, IAlias o)
142 defined_names.Add (name, o);
145 static public ArrayList UserDefinedNamespaces {
146 get {
147 return all_namespaces;
151 /// <summary>
152 /// The qualified name of the current namespace
153 /// </summary>
154 public string Name {
155 get {
156 return fullname;
160 public override string FullName {
161 get {
162 return fullname;
166 /// <summary>
167 /// The parent of this namespace, used by the parser to "Pop"
168 /// the current namespace declaration
169 /// </summary>
170 public Namespace Parent {
171 get {
172 return parent;
176 public static void DefineNamespaces (SymbolWriter symwriter)
178 foreach (Namespace ns in all_namespaces) {
179 foreach (NamespaceEntry entry in ns.entries)
180 entry.DefineNamespace (symwriter);
184 /// <summary>
185 /// Used to validate that all the using clauses are correct
186 /// after we are finished parsing all the files.
187 /// </summary>
188 public static void VerifyUsing ()
190 foreach (Namespace ns in all_namespaces) {
191 foreach (NamespaceEntry entry in ns.entries)
192 entry.VerifyUsing ();
196 public override string ToString ()
198 if (this == Root)
199 return "Namespace (<root>)";
200 else
201 return String.Format ("Namespace ({0})", Name);
204 bool IAlias.IsType {
205 get { return false; }
208 TypeExpr IAlias.ResolveAsType (EmitContext ec)
210 throw new InvalidOperationException ();
214 public class NamespaceEntry
216 Namespace ns;
217 NamespaceEntry parent, implicit_parent;
218 SourceFile file;
219 int symfile_id;
220 Hashtable aliases;
221 ArrayList using_clauses;
222 public bool DeclarationFound = false;
225 // This class holds the location where a using definition is
226 // done, and whether it has been used by the program or not.
228 // We use this to flag using clauses for namespaces that do not
229 // exist.
231 public class UsingEntry {
232 public readonly string Name;
233 public readonly NamespaceEntry NamespaceEntry;
234 public readonly Location Location;
236 public UsingEntry (NamespaceEntry entry, string name, Location loc)
238 Name = name;
239 NamespaceEntry = entry;
240 Location = loc;
243 Namespace resolved_ns;
245 public Namespace Resolve ()
247 if (resolved_ns != null)
248 return resolved_ns;
250 FullNamedExpression resolved = NamespaceEntry.LookupForUsing (Name, Location);
251 resolved_ns = resolved as Namespace;
252 return resolved_ns;
256 public class AliasEntry {
257 public readonly string Name;
258 public readonly MemberName Alias;
259 public readonly NamespaceEntry NamespaceEntry;
260 public readonly Location Location;
262 public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
264 Name = name;
265 Alias = alias;
266 NamespaceEntry = entry;
267 Location = loc;
270 FullNamedExpression resolved;
272 public FullNamedExpression Resolve ()
274 if (resolved != null)
275 return resolved;
278 // GENERICS: Cope with the expression and not with the string
279 // this will fail with `using A = Stack<int>'
282 string alias = Alias.GetTypeName ();
284 resolved = NamespaceEntry.LookupForUsing (alias, Location);
285 if (resolved == null)
286 return null;
288 if (Alias.TypeArguments == null)
289 return resolved;
291 EmitContext ec = RootContext.Tree.Types.EmitContext;
292 resolved = new TypeAliasExpression (resolved, Alias.TypeArguments, Location);
293 resolved = resolved.ResolveAsTypeStep (ec);
295 return resolved;
299 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
300 : this (parent, file, name, false, loc)
303 protected NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, bool is_implicit, Location loc)
305 this.parent = parent;
306 this.file = file;
307 this.IsImplicit = is_implicit;
308 this.ID = ++next_id;
310 if (!is_implicit && (parent != null))
311 ns = parent.NS.GetNamespace (name, true);
312 else if (name != null)
313 ns = Namespace.LookupNamespace (name, true);
314 else
315 ns = Namespace.Root;
316 ns.AddNamespaceEntry (this);
318 if ((parent != null) && (parent.NS != ns.Parent))
319 implicit_parent = new NamespaceEntry (parent, file, ns.Parent.Name, true, loc);
320 else
321 implicit_parent = parent;
323 this.FullName = ns.Name;
326 static int next_id = 0;
327 public readonly string FullName;
328 public readonly int ID;
329 public readonly bool IsImplicit;
331 public Namespace NS {
332 get {
333 return ns;
337 public NamespaceEntry Parent {
338 get {
339 return parent;
343 public NamespaceEntry ImplicitParent {
344 get {
345 return implicit_parent;
349 public void DefineName (string name, IAlias o)
351 ns.DefineName (name, o);
354 /// <summary>
355 /// Records a new namespace for resolving name references
356 /// </summary>
357 public void Using (string ns, Location loc)
359 if (DeclarationFound){
360 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
361 return;
364 if (ns == FullName)
365 return;
367 if (using_clauses == null)
368 using_clauses = new ArrayList ();
370 foreach (UsingEntry old_entry in using_clauses) {
371 if (old_entry.Name == ns) {
372 if (RootContext.WarningLevel >= 3)
373 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", ns);
374 return;
378 UsingEntry ue = new UsingEntry (this, ns, loc);
379 using_clauses.Add (ue);
382 public void UsingAlias (string name, MemberName alias, Location loc)
384 if (DeclarationFound){
385 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
386 return;
389 if (aliases == null)
390 aliases = new Hashtable ();
392 if (aliases.Contains (name)){
393 Report.Error (1537, loc, "The using alias `" + name +
394 "' appeared previously in this namespace");
395 return;
398 aliases [name] = new AliasEntry (this, name, alias, loc);
401 public FullNamedExpression LookupAlias (string alias)
403 AliasEntry entry = null;
404 if (aliases != null)
405 entry = (AliasEntry) aliases [alias];
407 return entry == null ? null : entry.Resolve ();
411 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
412 // resolved as if the immediately containing namespace body has no using-directives.
414 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
415 // in the using-namespace-directive.
417 public FullNamedExpression LookupForUsing (string dotted_name, Location loc)
419 int pos = dotted_name.IndexOf ('.');
420 string simple_name = dotted_name;
421 string rest = null;
422 if (pos >= 0) {
423 simple_name = dotted_name.Substring (0, pos);
424 rest = dotted_name.Substring (pos + 1);
427 FullNamedExpression o = NS.Lookup (null, simple_name, loc);
428 if (o == null && ImplicitParent != null)
429 o = ImplicitParent.LookupNamespaceOrType (null, simple_name, loc);
431 if (o == null || rest == null)
432 return o;
434 Namespace ns = o as Namespace;
435 if (ns != null)
436 return ns.Lookup (null, rest, loc);
438 Type nested = TypeManager.LookupType (o.FullName + "." + rest);
439 if (nested == null)
440 return null;
442 return new TypeExpression (nested, Location.Null);
445 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc)
447 FullNamedExpression resolved = null;
448 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
449 if ((resolved = curr_ns.Lookup (ds, name, loc)) != null)
450 break;
452 return resolved;
455 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
457 FullNamedExpression o;
458 Namespace ns;
461 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
463 // FIXME: Remove this block. Only simple names should come here.
464 // The bug: The loop in LookupNamespaceOrType continues if
465 // the lookup for N succeeds but the nested lookup for I fails.
466 // This is one part of #52697.
468 int pos = name.IndexOf ('.');
469 if (pos >= 0) {
470 string first = name.Substring (0, pos);
471 string last = name.Substring (pos + 1);
473 o = Lookup (ds, first, loc);
474 if (o == null)
475 return null;
477 ns = o as Namespace;
478 if (ns != null) {
479 o = ns.Lookup (ds, last, loc);
480 return o;
483 Type nested = TypeManager.LookupType (o.FullName + "." + last);
484 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
485 return null;
487 return new TypeExpression (nested, Location.Null);
491 // Check whether it's in the namespace.
493 o = NS.Lookup (ds, name, loc);
494 if (o != null)
495 return o;
498 // Check aliases.
500 o = LookupAlias (name);
501 if (o != null)
502 return o;
504 if (name.IndexOf ('.') > 0)
505 return null;
508 // Check using entries.
510 FullNamedExpression t = null, match = null;
511 foreach (Namespace using_ns in GetUsingTable ()) {
512 match = using_ns.Lookup (ds, name, loc);
513 if ((match != null) && (match is TypeExpr)) {
514 if (t != null) {
515 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.FullName, match.FullName);
516 return null;
517 } else {
518 t = match;
523 return t;
526 // Our cached computation.
527 Namespace [] namespace_using_table;
528 public Namespace[] GetUsingTable ()
530 if (namespace_using_table != null)
531 return namespace_using_table;
533 if (using_clauses == null) {
534 namespace_using_table = new Namespace [0];
535 return namespace_using_table;
538 ArrayList list = new ArrayList (using_clauses.Count);
540 foreach (UsingEntry ue in using_clauses) {
541 Namespace using_ns = ue.Resolve ();
542 if (using_ns == null)
543 continue;
545 list.Add (using_ns);
548 namespace_using_table = new Namespace [list.Count];
549 list.CopyTo (namespace_using_table, 0);
550 return namespace_using_table;
553 public void DefineNamespace (SymbolWriter symwriter)
555 if (symfile_id != 0)
556 return;
557 if (parent != null)
558 parent.DefineNamespace (symwriter);
560 string[] using_list;
561 if (using_clauses != null) {
562 using_list = new string [using_clauses.Count];
563 for (int i = 0; i < using_clauses.Count; i++)
564 using_list [i] = ((UsingEntry) using_clauses [i]).Name;
565 } else {
566 using_list = new string [0];
569 int parent_id = parent != null ? parent.symfile_id : 0;
570 if (file.SourceFileEntry == null)
571 return;
573 symfile_id = symwriter.DefineNamespace (
574 ns.Name, file.SourceFileEntry, using_list, parent_id);
577 public int SymbolFileID {
578 get {
579 return symfile_id;
583 static void MsgtryRef (string s)
585 Console.WriteLine (" Try using -r:" + s);
588 static void MsgtryPkg (string s)
590 Console.WriteLine (" Try using -pkg:" + s);
593 protected void error246 (Location loc, string name)
595 Report.Error (246, loc, "The namespace `" + name +
596 "' can not be found (missing assembly reference?)");
598 switch (name) {
599 case "Gtk": case "GtkSharp":
600 MsgtryPkg ("gtk-sharp");
601 break;
603 case "Gdk": case "GdkSharp":
604 MsgtryPkg ("gdk-sharp");
605 break;
607 case "Glade": case "GladeSharp":
608 MsgtryPkg ("glade-sharp");
609 break;
611 case "System.Drawing":
612 case "System.Web.Services":
613 case "System.Web":
614 case "System.Data":
615 case "System.Windows.Forms":
616 MsgtryRef (name);
617 break;
621 /// <summary>
622 /// Used to validate that all the using clauses are correct
623 /// after we are finished parsing all the files.
624 /// </summary>
625 public void VerifyUsing ()
627 if (using_clauses != null){
628 foreach (UsingEntry ue in using_clauses){
629 if (ue.Resolve () != null)
630 continue;
632 if (LookupForUsing (ue.Name, ue.Location) == null)
633 error246 (ue.Location, ue.Name);
634 else
635 Report.Error (138, ue.Location, "The using keyword only lets you specify a namespace, " +
636 "`" + ue.Name + "' is a class not a namespace.");
641 if (aliases != null){
642 foreach (DictionaryEntry de in aliases){
643 AliasEntry alias = (AliasEntry) de.Value;
645 if (alias.Resolve () != null)
646 continue;
648 error246 (alias.Location, alias.Alias.GetTypeName ());
653 public override string ToString ()
655 if (NS == Namespace.Root)
656 return "NamespaceEntry (<root>)";
657 else
658 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);