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