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