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