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