2004-02-14 Francisco Figueiredo Jr. <fxjrlists@yahoo.com.br>
[mono.git] / mcs / gmcs / namespace.cs
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;
11
12 namespace Mono.CSharp {
13
14         /// <summary>
15         ///   Keeps track of the namespaces defined in the C# code.
16         /// </summary>
17         public class Namespace {
18                 static ArrayList all_namespaces = new ArrayList ();
19                 static Hashtable namespaces_map = new Hashtable ();
20                 
21                 Namespace parent;
22                 string fullname;
23                 ArrayList entries;
24                 Hashtable namespaces;
25                 Hashtable defined_names;
26
27                 /// <summary>
28                 ///   Constructor Takes the current namespace and the
29                 ///   name.  This is bootstrapped with parent == null
30                 ///   and name = ""
31                 /// </summary>
32                 public Namespace (Namespace parent, string name)
33                 {
34                         this.parent = parent;
35
36                         string pname = parent != null ? parent.Name : "";
37                                 
38                         if (pname == "")
39                                 fullname = name;
40                         else
41                                 fullname = parent.Name + "." + name;
42
43                         entries = new ArrayList ();
44                         namespaces = new Hashtable ();
45                         defined_names = new Hashtable ();
46
47                         all_namespaces.Add (this);
48                         if (namespaces_map.Contains (fullname))
49                                 return;
50                         namespaces_map [fullname] = true;
51                 }
52
53                 public static bool IsNamespace (string name)
54                 {
55                         return namespaces_map [name] != null;
56                 }
57                 
58                 public static Namespace Root = new Namespace (null, "");
59
60                 public Namespace GetNamespace (string name, bool create)
61                 {
62                         int pos = name.IndexOf ('.');
63
64                         Namespace ns;
65                         string first;
66                         if (pos >= 0)
67                                 first = name.Substring (0, pos);
68                         else
69                                 first = name;
70
71                         ns = (Namespace) namespaces [first];
72                         if (ns == null) {
73                                 if (!create)
74                                         return null;
75
76                                 ns = new Namespace (this, first);
77                                 namespaces.Add (first, ns);
78                         }
79
80                         if (pos >= 0)
81                                 ns = ns.GetNamespace (name.Substring (pos + 1), create);
82
83                         return ns;
84                 }
85
86                 public static Namespace LookupNamespace (string name, bool create)
87                 {
88                         return Root.GetNamespace (name, create);
89                 }
90
91                 public object Lookup (DeclSpace ds, string name)
92                 {
93                         object o = Lookup (name);
94
95                         Type t;
96                         DeclSpace tdecl = o as DeclSpace;
97                         if (tdecl != null) {
98                                 t = tdecl.DefineType ();
99
100                                 if ((ds == null) || ds.CheckAccessLevel (t))
101                                         return t;
102                         }
103
104                         Namespace ns = GetNamespace (name, false);
105                         if (ns != null)
106                                 return ns;
107
108                         t = TypeManager.LookupType (DeclSpace.MakeFQN (fullname, name));
109                         if ((t == null) || ((ds != null) && !ds.CheckAccessLevel (t)))
110                                 return null;
111
112                         return t;
113                 }
114
115                 public void AddNamespaceEntry (NamespaceEntry entry)
116                 {
117                         entries.Add (entry);
118                 }
119
120                 public void DefineName (string name, object o)
121                 {
122                         defined_names.Add (name, o);
123                 }
124
125                 public object Lookup (string name)
126                 {
127                         return defined_names [name];
128                 }
129
130                 static public ArrayList UserDefinedNamespaces {
131                         get {
132                                 return all_namespaces;
133                         }
134                 }
135
136                 /// <summary>
137                 ///   The qualified name of the current namespace
138                 /// </summary>
139                 public string Name {
140                         get {
141                                 return fullname;
142                         }
143                 }
144
145                 /// <summary>
146                 ///   The parent of this namespace, used by the parser to "Pop"
147                 ///   the current namespace declaration
148                 /// </summary>
149                 public Namespace Parent {
150                         get {
151                                 return parent;
152                         }
153                 }
154
155                 public static void DefineNamespaces (SymbolWriter symwriter)
156                 {
157                         foreach (Namespace ns in all_namespaces) {
158                                 foreach (NamespaceEntry entry in ns.entries)
159                                         entry.DefineNamespace (symwriter);
160                         }
161                 }
162
163                 /// <summary>
164                 ///   Used to validate that all the using clauses are correct
165                 ///   after we are finished parsing all the files.  
166                 /// </summary>
167                 public static void VerifyUsing ()
168                 {
169                         foreach (Namespace ns in all_namespaces) {
170                                 foreach (NamespaceEntry entry in ns.entries)
171                                         entry.VerifyUsing ();
172                         }
173                 }
174
175                 public override string ToString ()
176                 {
177                         if (this == Root)
178                                 return "Namespace (<root>)";
179                         else
180                                 return String.Format ("Namespace ({0})", Name);
181                 }
182         }
183
184         public class NamespaceEntry
185         {
186                 Namespace ns;
187                 NamespaceEntry parent, implicit_parent;
188                 SourceFile file;
189                 int symfile_id;
190                 Hashtable aliases;
191                 ArrayList using_clauses;
192                 public bool DeclarationFound = false;
193
194                 //
195                 // This class holds the location where a using definition is
196                 // done, and whether it has been used by the program or not.
197                 //
198                 // We use this to flag using clauses for namespaces that do not
199                 // exist.
200                 //
201                 public class UsingEntry {
202                         public readonly string Name;
203                         public readonly NamespaceEntry NamespaceEntry;
204                         public readonly Location Location;
205                         
206                         public UsingEntry (NamespaceEntry entry, string name, Location loc)
207                         {
208                                 Name = name;
209                                 NamespaceEntry = entry;
210                                 Location = loc;
211                         }
212
213                         Namespace resolved_ns;
214
215                         public Namespace Resolve ()
216                         {
217                                 if (resolved_ns != null)
218                                         return resolved_ns;
219
220                                 Namespace curr_ns = NamespaceEntry.NS;
221                                 while ((curr_ns != null) && (resolved_ns == null)) {
222                                         resolved_ns = curr_ns.GetNamespace (Name, false);
223
224                                         if (resolved_ns == null)
225                                                 curr_ns = curr_ns.Parent;
226                                 }
227
228                                 return resolved_ns;
229                         }
230                 }
231
232                 public class AliasEntry {
233                         public readonly string Name;
234                         public readonly Expression Alias;
235                         public readonly NamespaceEntry NamespaceEntry;
236                         public readonly Location Location;
237                         
238                         public AliasEntry (NamespaceEntry entry, string name, Expression alias, Location loc)
239                         {
240                                 Name = name;
241                                 Alias = alias;
242                                 NamespaceEntry = entry;
243                                 Location = loc;
244                         }
245
246                         object resolved;
247
248                         public object Resolve ()
249                         {
250                                 if (resolved != null)
251                                         return resolved;
252
253                                 NamespaceEntry curr_ns = NamespaceEntry;
254
255                                 //
256                                 // GENERICS: Cope with the expression and not with the string
257                                 // this will fail with `using A = Stack<int>'
258                                 //
259                                 
260                                 string alias = Alias.ToString ();
261                                 while ((curr_ns != null) && (resolved == null)) {
262                                         resolved = curr_ns.Lookup (null, alias, Location);
263
264                                         if (resolved == null)
265                                                 curr_ns = curr_ns.Parent;
266                                 }
267
268                                 return resolved;
269                         }
270                 }
271
272                 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
273                         : this (parent, file, name, false, loc)
274                 { }
275
276                 protected NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, bool is_implicit, Location loc)
277                 {
278                         this.parent = parent;
279                         this.file = file;
280                         this.IsImplicit = is_implicit;
281                         this.ID = ++next_id;
282
283                         if (!is_implicit && (parent != null))
284                                 ns = parent.NS.GetNamespace (name, true);
285                         else if (name != null)
286                                 ns = Namespace.LookupNamespace (name, true);
287                         else
288                                 ns = Namespace.Root;
289                         ns.AddNamespaceEntry (this);
290
291                         if ((parent != null) && (parent.NS != ns.Parent))
292                                 implicit_parent = new NamespaceEntry (parent, file, ns.Parent.Name, true, loc);
293                         else
294                                 implicit_parent = parent;
295
296                         this.FullName = ns.Name;
297                 }
298
299                 static int next_id = 0;
300                 public readonly string FullName;
301                 public readonly int ID;
302                 public readonly bool IsImplicit;
303
304                 public Namespace NS {
305                         get {
306                                 return ns;
307                         }
308                 }
309
310                 public NamespaceEntry Parent {
311                         get {
312                                 return parent;
313                         }
314                 }
315
316                 public NamespaceEntry ImplicitParent {
317                         get {
318                                 return implicit_parent;
319                         }
320                 }
321
322                 public void DefineName (string name, object o)
323                 {
324                         ns.DefineName (name, o);
325                 }
326
327                 /// <summary>
328                 ///   Records a new namespace for resolving name references
329                 /// </summary>
330                 public void Using (string ns, Location loc)
331                 {
332                         if (DeclarationFound){
333                                 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
334                                 return;
335                         }
336
337                         if (ns == FullName)
338                                 return;
339                         
340                         if (using_clauses == null)
341                                 using_clauses = new ArrayList ();
342
343                         foreach (UsingEntry old_entry in using_clauses){
344                                 if (old_entry.Name == ns){
345                                         Report.Warning (105, loc, "The using directive for '" + ns +
346                                                         "' appeared previously in this namespace");
347                                         return;
348                                 }
349                         }
350                         
351                         UsingEntry ue = new UsingEntry (this, ns, loc);
352                         using_clauses.Add (ue);
353                 }
354
355                 public void UsingAlias (string alias, Expression namespace_or_type, Location loc)
356                 {
357                         if (aliases == null)
358                                 aliases = new Hashtable ();
359                         
360                         if (aliases.Contains (alias)){
361                                 Report.Error (1537, loc, "The using alias `" + alias +
362                                               "' appeared previously in this namespace");
363                                 return;
364                         }
365
366                         aliases [alias] = new AliasEntry (this, alias, namespace_or_type, loc);
367                 }
368
369                 protected AliasEntry GetAliasEntry (string alias)
370                 {
371                         AliasEntry entry = null;
372
373                         if (aliases != null)
374                                 entry = (AliasEntry) aliases [alias];
375                         if (entry == null && Parent != null)
376                                 entry = Parent.GetAliasEntry (alias);
377
378                         return entry;
379                 }
380
381                 public string LookupAlias (string alias)
382                 {
383                         AliasEntry entry = GetAliasEntry (alias);
384
385                         if (entry == null)
386                                 return null;
387
388                         object resolved = entry.Resolve ();
389                         if (resolved == null)
390                                 return null;
391                         else if (resolved is Namespace)
392                                 return ((Namespace) resolved).Name;
393                         else
394                                 return ((Type) resolved).FullName;
395                 }
396
397                 public object Lookup (DeclSpace ds, string name, Location loc)
398                 {
399                         object o;
400                         Namespace ns;
401
402                         //
403                         // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
404                         //
405                         int pos = name.IndexOf ('.');
406                         if (pos >= 0) {
407                                 string first = name.Substring (0, pos);
408                                 string last = name.Substring (pos + 1);
409
410                                 o = Lookup (ds, first, loc);
411                                 if (o == null)
412                                         return null;
413
414                                 ns = o as Namespace;
415                                 if (ns != null)
416                                         return ns.Lookup (ds, last);
417
418                                 Type nested = TypeManager.LookupType ((((Type) o).Name + "." + last));
419                                 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
420                                         return null;
421
422                                 return nested;
423                         }
424
425                         //
426                         // Check whether it's a namespace.
427                         //
428                         o = NS.Lookup (ds, name);
429                         if (o != null)
430                                 return o;
431
432                         //
433                         // Check aliases.
434                         //
435                         AliasEntry entry = GetAliasEntry (name);
436                         if (entry != null) {
437                                 o = entry.Resolve ();
438                                 if (o != null)
439                                         return o;
440                         }
441
442                         //
443                         // Check using entries.
444                         //
445                         Type t = null, match = null;
446                         foreach (Namespace using_ns in GetUsingTable ()) {
447                                 match = using_ns.Lookup (ds, name) as Type;
448                                 if (match != null){
449                                         if (t != null) {
450                                                 DeclSpace.Error_AmbiguousTypeReference (loc, name, t, match);
451                                                 return null;
452                                         } else {
453                                                 t = match;
454                                         }
455                                 }
456                         }
457
458                         return t;
459                 }
460
461                 // Our cached computation.
462                 Namespace [] namespace_using_table;
463                 public Namespace[] GetUsingTable ()
464                 {
465                         if (namespace_using_table != null)
466                                 return namespace_using_table;
467                         
468                         if (using_clauses == null)
469                                 return new Namespace [0];
470
471                         ArrayList list = new ArrayList (using_clauses.Count);
472
473                         foreach (UsingEntry ue in using_clauses) {
474                                 Namespace using_ns = ue.Resolve ();
475                                 if (using_ns == null)
476                                         continue;
477
478                                 list.Add (using_ns);
479                         }
480
481                         namespace_using_table = new Namespace [list.Count];
482                         list.CopyTo (namespace_using_table, 0);
483                         return namespace_using_table;
484                 }
485
486                 public void DefineNamespace (SymbolWriter symwriter)
487                 {
488                         if (symfile_id != 0)
489                                 return;
490                         if (parent != null)
491                                 parent.DefineNamespace (symwriter);
492
493                         string[] using_list;
494                         if (using_clauses != null) {
495                                 using_list = new string [using_clauses.Count];
496                                 for (int i = 0; i < using_clauses.Count; i++)
497                                         using_list [i] = ((UsingEntry) using_clauses [i]).Name;
498                         } else {
499                                 using_list = new string [0];
500                         }
501
502                         int parent_id = parent != null ? parent.symfile_id : 0;
503                         symfile_id = symwriter.DefineNamespace (ns.Name, file, using_list, parent_id);
504                 }
505
506                 public int SymbolFileID {
507                         get {
508                                 return symfile_id;
509                         }
510                 }
511
512                 static void Msgtry (string s)
513                 {
514                         Console.WriteLine ("    Try using -r:" + s);
515                 }
516
517                 protected void error246 (Location loc, string name)
518                 {
519                         if (TypeManager.LookupType (name) != null)
520                                 Report.Error (138, loc, "The using keyword only lets you specify a namespace, " +
521                                               "`" + name + "' is a class not a namespace.");
522                         else {
523                                 Report.Error (246, loc, "The namespace `" + name +
524                                               "' can not be found (missing assembly reference?)");
525
526                                 switch (name){
527                                 case "Gtk": case "GtkSharp":
528                                         Msgtry ("gtk-sharp");
529                                         break;
530
531                                 case "Gdk": case "GdkSharp":
532                                         Msgtry ("gdk-sharp");
533                                         break;
534
535                                 case "Glade": case "GladeSharp":
536                                         Msgtry ("glade-sharp");
537                                         break;
538                                                         
539                                 case "System.Drawing":
540                                         Msgtry ("System.Drawing");
541                                         break;
542                                                         
543                                 case "System.Web.Services":
544                                         Msgtry ("System.Web.Services");
545                                         break;
546
547                                 case "System.Web":
548                                         Msgtry ("System.Web");
549                                         break;
550                                                         
551                                 case "System.Data":
552                                         Msgtry ("System.Data");
553                                         break;
554
555                                 case "System.Windows.Forms":
556                                         Msgtry ("System.Windows.Forms");
557                                         break;
558                                 }
559                         }
560                 }
561
562                 /// <summary>
563                 ///   Used to validate that all the using clauses are correct
564                 ///   after we are finished parsing all the files.  
565                 /// </summary>
566                 public void VerifyUsing ()
567                 {
568                         if (using_clauses != null){
569                                 foreach (UsingEntry ue in using_clauses){
570                                         if (ue.Resolve () != null)
571                                                 continue;
572
573                                         error246 (ue.Location, ue.Name);
574                                 }
575                         }
576
577                         if (aliases != null){
578                                 foreach (DictionaryEntry de in aliases){
579                                         AliasEntry alias = (AliasEntry) de.Value;
580
581                                         if (alias.Resolve () != null)
582                                                 continue;
583
584                                         error246 (alias.Location, alias.Alias.ToString ());
585                                 }
586                         }
587                 }
588
589                 public override string ToString ()
590                 {
591                         if (NS == Namespace.Root)
592                                 return "NamespaceEntry (<root>)";
593                         else
594                                 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);
595                 }
596         }
597 }