In ilasm/tests:
[mcs.git] / bmcs / namespace.cs
blobd803b3906b47382eb28f9dfe058a2440ac37e14c
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 Expression Name;
233 public readonly NamespaceEntry NamespaceEntry;
234 public readonly Location Location;
236 public UsingEntry (NamespaceEntry entry, Expression name, Location loc)
238 Name = name;
239 NamespaceEntry = entry;
240 Location = loc;
243 internal FullNamedExpression resolved;
245 public Namespace Resolve ()
247 if (resolved != null)
248 return resolved as Namespace;
250 DeclSpace root = RootContext.Tree.Types;
251 root.NamespaceEntry = NamespaceEntry;
252 resolved = Name.ResolveAsTypeStep (root.EmitContext);
253 root.NamespaceEntry = null;
255 return resolved as Namespace;
259 public class AliasEntry {
260 public readonly string Name;
261 public readonly Expression Alias;
262 public readonly NamespaceEntry NamespaceEntry;
263 public readonly Location Location;
265 public AliasEntry (NamespaceEntry entry, string name, Expression alias, Location loc)
267 Name = name;
268 Alias = alias;
269 NamespaceEntry = entry;
270 Location = loc;
273 FullNamedExpression resolved;
275 public FullNamedExpression Resolve ()
277 if (resolved != null)
278 return resolved;
280 DeclSpace root = RootContext.Tree.Types;
281 root.NamespaceEntry = NamespaceEntry;
282 resolved = Alias.ResolveAsTypeStep (root.EmitContext);
283 root.NamespaceEntry = null;
285 return resolved;
289 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
291 this.parent = parent;
292 this.file = file;
293 this.IsImplicit = false;
294 this.ID = ++next_id;
296 if (parent != null)
297 ns = parent.NS.GetNamespace (name, true);
298 else if (name != null)
299 ns = Namespace.LookupNamespace (name, true);
300 else
301 ns = Namespace.Root;
302 ns.AddNamespaceEntry (this);
303 this.FullName = ns.Name;
307 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
309 this.parent = parent;
310 this.file = file;
311 this.IsImplicit = true;
312 this.ID = ++next_id;
313 this.ns = ns;
314 this.FullName = ns.Name;
318 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
319 // resolved as if the immediately containing namespace body has no using-directives.
321 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
322 // in the using-namespace-directive.
324 // To implement these rules, the expressions in the using directives are resolved using
325 // the "doppelganger" (ghostly bodiless duplicate).
327 NamespaceEntry doppelganger;
328 NamespaceEntry Doppelganger {
329 get {
330 if (!IsImplicit && doppelganger == null)
331 doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
332 return doppelganger;
336 static int next_id = 0;
337 public readonly string FullName;
338 public readonly int ID;
339 public readonly bool IsImplicit;
341 public Namespace NS {
342 get {
343 return ns;
347 public NamespaceEntry Parent {
348 get {
349 return parent;
353 public NamespaceEntry ImplicitParent {
354 get {
355 if (parent == null)
356 return null;
357 if (implicit_parent == null) {
358 implicit_parent = (parent.NS == ns.Parent)
359 ? parent
360 : new NamespaceEntry (parent, file, ns.Parent);
362 return implicit_parent;
366 public void DefineName (string name, IAlias o)
368 ns.DefineName (name, o);
371 /// <summary>
372 /// Records a new namespace for resolving name references
373 /// </summary>
374 public void Using (Expression ns, Location loc)
376 string name = ns.ToString ();
377 if (DeclarationFound){
378 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
379 return;
382 if (name == FullName)
383 return;
385 if (using_clauses == null)
386 using_clauses = new ArrayList ();
388 foreach (UsingEntry old_entry in using_clauses) {
389 if (old_entry.Name.ToString () == name) {
390 if (RootContext.WarningLevel >= 3)
391 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", name);
392 return;
397 UsingEntry ue = new UsingEntry (Doppelganger, ns, loc);
398 using_clauses.Add (ue);
401 public void UsingAlias (string name, Expression alias, Location loc)
403 if (DeclarationFound){
404 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
405 return;
408 if (aliases == null)
409 aliases = new Hashtable ();
411 if (aliases.Contains (name)){
412 Report.Error (1537, loc, "The using alias `" + name +
413 "' appeared previously in this namespace");
414 return;
417 aliases [name] = new AliasEntry (Doppelganger, name, alias, loc);
420 public FullNamedExpression LookupAlias (string alias)
422 AliasEntry entry = null;
423 if (aliases != null)
424 entry = (AliasEntry) aliases [alias];
426 return entry == null ? null : entry.Resolve ();
429 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc)
431 FullNamedExpression resolved = null;
432 string rest = null;
434 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
435 int pos = name.IndexOf ('.');
436 if (pos >= 0) {
437 rest = name.Substring (pos + 1);
438 name = name.Substring (0, pos);
441 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
442 if ((resolved = curr_ns.Lookup (ds, name, loc)) != null)
443 break;
446 if (resolved == null || rest == null)
447 return resolved;
449 Namespace ns = resolved as Namespace;
450 if (ns != null)
451 return ns.Lookup (ds, rest, loc);
453 Type nested = TypeManager.LookupType (resolved.FullName + "." + rest);
454 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
455 return null;
457 return new TypeExpression (nested, Location.Null);
460 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
462 // Precondition: Only simple names (no dots) will be looked up with this function.
465 // Check whether it's in the namespace.
467 FullNamedExpression o = NS.Lookup (ds, name, loc);
468 if (o != null)
469 return o;
471 if (IsImplicit)
472 return null;
475 // Check aliases.
477 o = LookupAlias (name);
478 if (o != null)
479 return o;
482 // Check using entries.
484 FullNamedExpression t = null, match = null;
485 foreach (Namespace using_ns in GetUsingTable ()) {
486 match = using_ns.Lookup (ds, name, loc);
487 if ((match != null) && (match is TypeExpr)) {
488 if (t != null) {
489 DeclSpace.Error_AmbiguousTypeReference (loc, name, t.FullName, match.FullName);
490 return null;
491 } else {
492 t = match;
497 return t;
500 // Our cached computation.
501 Namespace [] namespace_using_table;
502 public Namespace[] GetUsingTable ()
504 if (namespace_using_table != null)
505 return namespace_using_table;
507 if (using_clauses == null) {
508 namespace_using_table = new Namespace [0];
509 return namespace_using_table;
512 ArrayList list = new ArrayList (using_clauses.Count);
514 foreach (UsingEntry ue in using_clauses) {
515 Namespace using_ns = ue.Resolve ();
516 if (using_ns == null)
517 continue;
519 list.Add (using_ns);
522 namespace_using_table = new Namespace [list.Count];
523 list.CopyTo (namespace_using_table, 0);
524 return namespace_using_table;
527 public void DefineNamespace (SymbolWriter symwriter)
529 if (symfile_id != 0)
530 return;
531 if (parent != null)
532 parent.DefineNamespace (symwriter);
534 string[] using_list;
535 if (using_clauses != null) {
536 using_list = new string [using_clauses.Count];
537 for (int i = 0; i < using_clauses.Count; i++)
538 using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
539 } else {
540 using_list = new string [0];
543 int parent_id = parent != null ? parent.symfile_id : 0;
544 if (file.SourceFileEntry == null)
545 return;
547 symfile_id = symwriter.DefineNamespace (
548 ns.Name, file.SourceFileEntry, using_list, parent_id);
551 public int SymbolFileID {
552 get {
553 return symfile_id;
557 static void MsgtryRef (string s)
559 Console.WriteLine (" Try using -r:" + s);
562 static void MsgtryPkg (string s)
564 Console.WriteLine (" Try using -pkg:" + s);
567 protected void error246 (Location loc, string name)
569 Report.Error (246, loc, "The namespace `" + name +
570 "' can not be found (missing assembly reference?)");
572 switch (name) {
573 case "Gtk": case "GtkSharp":
574 MsgtryPkg ("gtk-sharp");
575 break;
577 case "Gdk": case "GdkSharp":
578 MsgtryPkg ("gdk-sharp");
579 break;
581 case "Glade": case "GladeSharp":
582 MsgtryPkg ("glade-sharp");
583 break;
585 case "System.Drawing":
586 case "System.Web.Services":
587 case "System.Web":
588 case "System.Data":
589 case "System.Windows.Forms":
590 MsgtryRef (name);
591 break;
595 /// <summary>
596 /// Used to validate that all the using clauses are correct
597 /// after we are finished parsing all the files.
598 /// </summary>
599 public void VerifyUsing ()
601 if (using_clauses != null){
602 foreach (UsingEntry ue in using_clauses){
603 if (ue.Resolve () != null)
604 continue;
606 if (ue.resolved == null)
607 error246 (ue.Location, ue.Name.ToString ());
608 else
609 Report.Error (138, ue.Location, "The using keyword only lets you specify a namespace, " +
610 "`" + ue.Name + "' is a class not a namespace.");
615 if (aliases != null){
616 foreach (DictionaryEntry de in aliases){
617 AliasEntry alias = (AliasEntry) de.Value;
619 if (alias.Resolve () != null)
620 continue;
622 error246 (alias.Location, alias.Alias.ToString ());
627 public override string ToString ()
629 if (NS == Namespace.Root)
630 return "NamespaceEntry (<root>)";
631 else
632 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);