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