**** Merged r36954 from MCS ****
[mono.git] / mcs / mcs / 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                                 object resolved = NamespaceEntry.LookupForUsing (Name, Location);
221                                 resolved_ns = resolved as Namespace;
222                                 return resolved_ns;
223                         }
224                 }
225
226                 public class AliasEntry {
227                         public readonly string Name;
228                         public readonly MemberName Alias;
229                         public readonly NamespaceEntry NamespaceEntry;
230                         public readonly Location Location;
231                         
232                         public AliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc)
233                         {
234                                 Name = name;
235                                 Alias = alias;
236                                 NamespaceEntry = entry;
237                                 Location = loc;
238                         }
239
240                         object resolved;
241
242                         public object Resolve ()
243                         {
244                                 if (resolved != null)
245                                         return resolved;
246
247                                 string alias = Alias.GetPartialName ();
248
249                                 resolved = NamespaceEntry.LookupForUsing (alias, Location);
250                                 return resolved;
251                         }
252                 }
253
254                 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
255                         : this (parent, file, name, false, loc)
256                 { }
257
258                 protected NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, bool is_implicit, Location loc)
259                 {
260                         this.parent = parent;
261                         this.file = file;
262                         this.IsImplicit = is_implicit;
263                         this.ID = ++next_id;
264
265                         if (!is_implicit && (parent != null))
266                                 ns = parent.NS.GetNamespace (name, true);
267                         else if (name != null)
268                                 ns = Namespace.LookupNamespace (name, true);
269                         else
270                                 ns = Namespace.Root;
271                         ns.AddNamespaceEntry (this);
272
273                         if ((parent != null) && (parent.NS != ns.Parent))
274                                 implicit_parent = new NamespaceEntry (parent, file, ns.Parent.Name, true, loc);
275                         else
276                                 implicit_parent = parent;
277
278                         this.FullName = ns.Name;
279                 }
280
281                 static int next_id = 0;
282                 public readonly string FullName;
283                 public readonly int ID;
284                 public readonly bool IsImplicit;
285
286                 public Namespace NS {
287                         get {
288                                 return ns;
289                         }
290                 }
291
292                 public NamespaceEntry Parent {
293                         get {
294                                 return parent;
295                         }
296                 }
297
298                 public NamespaceEntry ImplicitParent {
299                         get {
300                                 return implicit_parent;
301                         }
302                 }
303
304                 public void DefineName (string name, object o)
305                 {
306                         ns.DefineName (name, o);
307                 }
308
309                 /// <summary>
310                 ///   Records a new namespace for resolving name references
311                 /// </summary>
312                 public void Using (string ns, Location loc)
313                 {
314                         if (DeclarationFound){
315                                 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
316                                 return;
317                         }
318
319                         if (ns == FullName)
320                                 return;
321                         
322                         if (using_clauses == null)
323                                 using_clauses = new ArrayList ();
324
325                         foreach (UsingEntry old_entry in using_clauses) {
326                                 if (old_entry.Name == ns) {
327                                         if (RootContext.WarningLevel >= 3)
328                                                 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", ns);
329                                         return;
330                                 }
331                         }
332                         
333                         UsingEntry ue = new UsingEntry (this, ns, loc);
334                         using_clauses.Add (ue);
335                 }
336
337                 public void UsingAlias (string name, MemberName alias, Location loc)
338                 {
339                         if (DeclarationFound){
340                                 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
341                                 return;
342                         }
343
344                         if (aliases == null)
345                                 aliases = new Hashtable ();
346                         
347                         if (aliases.Contains (name)){
348                                 Report.Error (1537, loc, "The using alias `" + name +
349                                               "' appeared previously in this namespace");
350                                 return;
351                         }
352
353                         aliases [name] = new AliasEntry (this, name, alias, loc);
354                 }
355
356                 protected AliasEntry GetAliasEntry (string alias)
357                 {
358                         AliasEntry entry = null;
359
360                         if (aliases != null)
361                                 entry = (AliasEntry) aliases [alias];
362                         if (entry == null && Parent != null)
363                                 entry = Parent.GetAliasEntry (alias);
364
365                         return entry;
366                 }
367
368                 public string LookupAlias (string alias)
369                 {
370                         AliasEntry entry = GetAliasEntry (alias);
371
372                         if (entry == null)
373                                 return null;
374
375                         object resolved = entry.Resolve ();
376                         if (resolved == null)
377                                 return null;
378                         else if (resolved is Namespace)
379                                 return ((Namespace) resolved).Name;
380                         else
381                                 return ((Type) resolved).FullName;
382                 }
383
384                 //
385                 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is 
386                 // resolved as if the immediately containing namespace body has no using-directives.
387                 //
388                 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
389                 // in the using-namespace-directive.
390                 //
391                 public object LookupForUsing (string dotted_name, Location loc)
392                 {
393                         int pos = dotted_name.IndexOf ('.');
394                         string simple_name = dotted_name;
395                         string rest = null;
396                         if (pos >= 0) {
397                                 simple_name = dotted_name.Substring (0, pos);
398                                 rest = dotted_name.Substring (pos + 1);
399                         }
400
401                         object o = NS.Lookup (null, simple_name);
402                         if (o == null && ImplicitParent != null)
403                                 o = ImplicitParent.LookupNamespaceOrType (null, simple_name, loc);
404
405                         if (o == null || rest == null)
406                                 return o;
407
408                         Namespace ns = o as Namespace;
409                         if (ns != null)
410                                 return ns.Lookup (null, rest);
411                         
412                         Type nested = TypeManager.LookupType ((((Type) o).Name + "." + rest));
413                         return nested;
414                 }
415
416                 public object LookupNamespaceOrType (DeclSpace ds, string name, Location loc)
417                 {
418                         object resolved = null;
419                         for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
420                                 if ((resolved = curr_ns.Lookup (ds, name, loc)) != null)
421                                         break;
422                         }
423                         return resolved;
424                 }
425
426                 private object Lookup (DeclSpace ds, string name, Location loc)
427                 {
428                         object o;
429                         Namespace ns;
430
431                         //
432                         // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
433                         //
434                         // FIXME: Remove this block.  Only simple names should come here.
435                         //        The bug: The loop in LookupNamespaceOrType continues if 
436                         //        the lookup for N succeeds but the nested lookup for I fails.
437                         //        This is one part of #52697.
438                         //
439                         int pos = name.IndexOf ('.');
440                         if (pos >= 0) {
441                                 string first = name.Substring (0, pos);
442                                 string last = name.Substring (pos + 1);
443
444                                 o = Lookup (ds, first, loc);
445                                 if (o == null)
446                                         return null;
447
448                                 ns = o as Namespace;
449                                 if (ns != null)
450                                         return ns.Lookup (ds, last);
451
452                                 Type nested = TypeManager.LookupType ((((Type) o).Name + "." + last));
453                                 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
454                                         return null;
455
456                                 return nested;
457                         }
458
459                         //
460                         // Check whether it's in the namespace.
461                         //
462                         o = NS.Lookup (ds, name);
463                         if (o != null)
464                                 return o;
465
466                         //
467                         // Check aliases.
468                         //
469                         AliasEntry entry = GetAliasEntry (name);
470                         if (entry != null) {
471                                 o = entry.Resolve ();
472                                 if (o != null)
473                                         return o;
474                         }
475
476                         if (name.IndexOf ('.') > 0)
477                                 return null;
478
479                         //
480                         // Check using entries.
481                         //
482                         Type t = null, match = null;
483                         foreach (Namespace using_ns in GetUsingTable ()) {
484                                 match = using_ns.Lookup (ds, name) as Type;
485                                 if (match != null){
486                                         if (t != null) {
487                                                 DeclSpace.Error_AmbiguousTypeReference (loc, name, t, match);
488                                                 return null;
489                                         } else {
490                                                 t = match;
491                                         }
492                                 }
493                         }
494
495                         return t;
496                 }
497
498                 // Our cached computation.
499                 Namespace [] namespace_using_table;
500                 public Namespace[] GetUsingTable ()
501                 {
502                         if (namespace_using_table != null)
503                                 return namespace_using_table;
504                         
505                         if (using_clauses == null)
506                                 return new Namespace [0];
507
508                         ArrayList list = new ArrayList (using_clauses.Count);
509
510                         foreach (UsingEntry ue in using_clauses) {
511                                 Namespace using_ns = ue.Resolve ();
512                                 if (using_ns == null)
513                                         continue;
514
515                                 list.Add (using_ns);
516                         }
517
518                         namespace_using_table = new Namespace [list.Count];
519                         list.CopyTo (namespace_using_table, 0);
520                         return namespace_using_table;
521                 }
522
523                 public void DefineNamespace (SymbolWriter symwriter)
524                 {
525                         if (symfile_id != 0)
526                                 return;
527                         if (parent != null)
528                                 parent.DefineNamespace (symwriter);
529
530                         string[] using_list;
531                         if (using_clauses != null) {
532                                 using_list = new string [using_clauses.Count];
533                                 for (int i = 0; i < using_clauses.Count; i++)
534                                         using_list [i] = ((UsingEntry) using_clauses [i]).Name;
535                         } else {
536                                 using_list = new string [0];
537                         }
538
539                         int parent_id = parent != null ? parent.symfile_id : 0;
540                         if (file.SourceFileEntry == null)
541                                 return;
542
543                         symfile_id = symwriter.DefineNamespace (
544                                 ns.Name, file.SourceFileEntry, using_list, parent_id);
545                 }
546
547                 public int SymbolFileID {
548                         get {
549                                 return symfile_id;
550                         }
551                 }
552
553                 static void MsgtryRef (string s)
554                 {
555                         Console.WriteLine ("    Try using -r:" + s);
556                 }
557                 
558                 static void MsgtryPkg (string s)
559                 {
560                         Console.WriteLine ("    Try using -pkg:" + s);
561                 }
562
563                 protected void error246 (Location loc, string name)
564                 {
565                         Report.Error (246, loc, "The namespace `" + name +
566                                       "' can not be found (missing assembly reference?)");
567
568                         switch (name) {
569                         case "Gtk": case "GtkSharp":
570                                 MsgtryPkg ("gtk-sharp");
571                                 break;
572
573                         case "Gdk": case "GdkSharp":
574                                 MsgtryPkg ("gdk-sharp");
575                                 break;
576
577                         case "Glade": case "GladeSharp":
578                                 MsgtryPkg ("glade-sharp");
579                                 break;
580
581                         case "System.Drawing":
582                         case "System.Web.Services":
583                         case "System.Web":
584                         case "System.Data":
585                         case "System.Windows.Forms":
586                                 MsgtryRef (name);
587                                 break;
588                         }
589                 }
590
591                 /// <summary>
592                 ///   Used to validate that all the using clauses are correct
593                 ///   after we are finished parsing all the files.  
594                 /// </summary>
595                 public void VerifyUsing ()
596                 {
597                         if (using_clauses != null){
598                                 foreach (UsingEntry ue in using_clauses){
599                                         if (ue.Resolve () != null)
600                                                 continue;
601
602                                         if (LookupForUsing (ue.Name, ue.Location) == null)
603                                                 error246 (ue.Location, ue.Name);
604                                         else
605                                                 Report.Error (138, ue.Location, "The using keyword only lets you specify a namespace, " +
606                                                               "`" + ue.Name + "' is a class not a namespace.");
607
608                                 }
609                         }
610
611                         if (aliases != null){
612                                 foreach (DictionaryEntry de in aliases){
613                                         AliasEntry alias = (AliasEntry) de.Value;
614
615                                         if (alias.Resolve () != null)
616                                                 continue;
617
618                                         error246 (alias.Location, alias.Alias.GetPartialName ());
619                                 }
620                         }
621                 }
622
623                 public override string ToString ()
624                 {
625                         if (NS == Namespace.Root)
626                                 return "NamespaceEntry (<root>)";
627                         else
628                                 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);
629                 }
630         }
631 }