Update helper errors to use the -pkg syntax when needed, patch from iain@mccoy.id...
[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                                 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 MemberName Alias;
235                         public readonly NamespaceEntry NamespaceEntry;
236                         public readonly Location Location;
237                         
238                         public AliasEntry (NamespaceEntry entry, string name, MemberName 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                                 string alias = Alias.GetPartialName ();
254
255                                 // According to section 16.3.1, the namespace-or-type-name is resolved
256                                 // as if the immediately containing namespace body has no using-directives.
257                                 resolved = NamespaceEntry.Lookup (null, alias, true, Location);
258
259                                 NamespaceEntry curr_ns = NamespaceEntry.Parent;
260
261                                 while ((curr_ns != null) && (resolved == null)) {
262                                         resolved = curr_ns.Lookup (null, alias, false, 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                                         if (RootContext.WarningLevel >= 3)
346                                                 Report.Warning (105, loc, "The using directive for '{0}' appeared previously in this namespace", ns);
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 name, MemberName alias, Location loc)
356                 {
357                         if (DeclarationFound){
358                                 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
359                                 return;
360                         }
361
362                         if (aliases == null)
363                                 aliases = new Hashtable ();
364                         
365                         if (aliases.Contains (name)){
366                                 Report.Error (1537, loc, "The using alias `" + name +
367                                               "' appeared previously in this namespace");
368                                 return;
369                         }
370
371                         aliases [name] = new AliasEntry (this, name, alias, loc);
372                 }
373
374                 protected AliasEntry GetAliasEntry (string alias)
375                 {
376                         AliasEntry entry = null;
377
378                         if (aliases != null)
379                                 entry = (AliasEntry) aliases [alias];
380                         if (entry == null && Parent != null)
381                                 entry = Parent.GetAliasEntry (alias);
382
383                         return entry;
384                 }
385
386                 public string LookupAlias (string alias)
387                 {
388                         AliasEntry entry = GetAliasEntry (alias);
389
390                         if (entry == null)
391                                 return null;
392
393                         object resolved = entry.Resolve ();
394                         if (resolved == null)
395                                 return null;
396                         else if (resolved is Namespace)
397                                 return ((Namespace) resolved).Name;
398                         else
399                                 return ((Type) resolved).FullName;
400                 }
401
402                 public object Lookup (DeclSpace ds, string name, bool ignore_using, Location loc)
403                 {
404                         object o;
405                         Namespace ns;
406
407                         //
408                         // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
409                         //
410                         int pos = name.IndexOf ('.');
411                         if (pos >= 0) {
412                                 string first = name.Substring (0, pos);
413                                 string last = name.Substring (pos + 1);
414
415                                 o = Lookup (ds, first, ignore_using, loc);
416                                 if (o == null)
417                                         return null;
418
419                                 ns = o as Namespace;
420                                 if (ns != null)
421                                         return ns.Lookup (ds, last);
422
423                                 Type nested = TypeManager.LookupType ((((Type) o).Name + "." + last));
424                                 if ((nested == null) || ((ds != null) && !ds.CheckAccessLevel (nested)))
425                                         return null;
426
427                                 return nested;
428                         }
429
430                         //
431                         // Check whether it's a namespace.
432                         //
433                         o = NS.Lookup (ds, name);
434                         if (o != null)
435                                 return o;
436
437                         if (ignore_using)
438                                 return null;
439
440                         //
441                         // Check aliases.
442                         //
443                         AliasEntry entry = GetAliasEntry (name);
444                         if (entry != null) {
445                                 o = entry.Resolve ();
446                                 if (o != null)
447                                         return o;
448                         }
449
450                         if (name.IndexOf ('.') > 0)
451                                 return null;
452
453                         //
454                         // Check using entries.
455                         //
456                         Type t = null, match = null;
457                         foreach (Namespace using_ns in GetUsingTable ()) {
458                                 match = using_ns.Lookup (ds, name) as Type;
459                                 if (match != null){
460                                         if (t != null) {
461                                                 DeclSpace.Error_AmbiguousTypeReference (loc, name, t, match);
462                                                 return null;
463                                         } else {
464                                                 t = match;
465                                         }
466                                 }
467                         }
468
469                         return t;
470                 }
471
472                 // Our cached computation.
473                 Namespace [] namespace_using_table;
474                 public Namespace[] GetUsingTable ()
475                 {
476                         if (namespace_using_table != null)
477                                 return namespace_using_table;
478                         
479                         if (using_clauses == null)
480                                 return new Namespace [0];
481
482                         ArrayList list = new ArrayList (using_clauses.Count);
483
484                         foreach (UsingEntry ue in using_clauses) {
485                                 Namespace using_ns = ue.Resolve ();
486                                 if (using_ns == null)
487                                         continue;
488
489                                 list.Add (using_ns);
490                         }
491
492                         namespace_using_table = new Namespace [list.Count];
493                         list.CopyTo (namespace_using_table, 0);
494                         return namespace_using_table;
495                 }
496
497                 public void DefineNamespace (SymbolWriter symwriter)
498                 {
499                         if (symfile_id != 0)
500                                 return;
501                         if (parent != null)
502                                 parent.DefineNamespace (symwriter);
503
504                         string[] using_list;
505                         if (using_clauses != null) {
506                                 using_list = new string [using_clauses.Count];
507                                 for (int i = 0; i < using_clauses.Count; i++)
508                                         using_list [i] = ((UsingEntry) using_clauses [i]).Name;
509                         } else {
510                                 using_list = new string [0];
511                         }
512
513                         int parent_id = parent != null ? parent.symfile_id : 0;
514                         if (file.SourceFileEntry == null)
515                                 return;
516
517                         symfile_id = symwriter.DefineNamespace (
518                                 ns.Name, file.SourceFileEntry, using_list, parent_id);
519                 }
520
521                 public int SymbolFileID {
522                         get {
523                                 return symfile_id;
524                         }
525                 }
526
527                 static void MsgtryRef (string s)
528                 {
529                         Console.WriteLine ("    Try using -r:" + s);
530                 }
531                 
532                 static void MsgtryPkg (string s)
533                 {
534                         Console.WriteLine ("    Try using -pkg:" + s);
535                 }
536
537                 protected void error246 (Location loc, string name)
538                 {
539                         if (TypeManager.LookupType (name) != null)
540                                 Report.Error (138, loc, "The using keyword only lets you specify a namespace, " +
541                                               "`" + name + "' is a class not a namespace.");
542                         else {
543                                 Report.Error (246, loc, "The namespace `" + name +
544                                               "' can not be found (missing assembly reference?)");
545
546                                 switch (name){
547                                 case "Gtk": case "GtkSharp":
548                                         MsgtryPkg ("gtk-sharp");
549                                         break;
550
551                                 case "Gdk": case "GdkSharp":
552                                         MsgtryPkg ("gdk-sharp");
553                                         break;
554
555                                 case "Glade": case "GladeSharp":
556                                         MsgtryPkg ("glade-sharp");
557                                         break;
558                                                         
559                                 case "System.Drawing":
560                                         MsgtryRef ("System.Drawing");
561                                         break;
562                                                         
563                                 case "System.Web.Services":
564                                         MsgtryRef ("System.Web.Services");
565                                         break;
566
567                                 case "System.Web":
568                                         MsgtryRef ("System.Web");
569                                         break;
570                                                         
571                                 case "System.Data":
572                                         MsgtryRef ("System.Data");
573                                         break;
574
575                                 case "System.Windows.Forms":
576                                         MsgtryRef ("System.Windows.Forms");
577                                         break;
578                                 }
579                         }
580                 }
581
582                 /// <summary>
583                 ///   Used to validate that all the using clauses are correct
584                 ///   after we are finished parsing all the files.  
585                 /// </summary>
586                 public void VerifyUsing ()
587                 {
588                         if (using_clauses != null){
589                                 foreach (UsingEntry ue in using_clauses){
590                                         if (ue.Resolve () != null)
591                                                 continue;
592
593                                         error246 (ue.Location, ue.Name);
594                                 }
595                         }
596
597                         if (aliases != null){
598                                 foreach (DictionaryEntry de in aliases){
599                                         AliasEntry alias = (AliasEntry) de.Value;
600
601                                         if (alias.Resolve () != null)
602                                                 continue;
603
604                                         error246 (alias.Location, alias.Alias.GetPartialName ());
605                                 }
606                         }
607                 }
608
609                 public override string ToString ()
610                 {
611                         if (NS == Namespace.Root)
612                                 return "NamespaceEntry (<root>)";
613                         else
614                                 return String.Format ("NamespaceEntry ({0},{1},{2})", FullName, IsImplicit, ID);
615                 }
616         }
617 }