merge -r 53370:58178
[mono.git] / mcs / gmcs / namespace.cs
1 //
2 // namespace.cs: Tracks namespaces
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 using System;
10 using System.Collections;
11 using System.Collections.Specialized;
12 using System.Reflection;
13
14 namespace Mono.CSharp {
15
16         public class RootNamespace : Namespace {
17                 static MethodInfo get_namespaces_method;
18
19                 string alias_name;
20                 Assembly referenced_assembly;
21
22                 Hashtable all_namespaces;
23
24                 static Hashtable root_namespaces;
25                 public static GlobalRootNamespace Global;
26                 
27                 static RootNamespace ()
28                 {
29                         get_namespaces_method = typeof (Assembly).GetMethod ("GetNamespaces", BindingFlags.Instance | BindingFlags.NonPublic);
30
31                         Reset ();
32                 }
33
34                 public static void Reset ()
35                 {
36                         root_namespaces = new Hashtable ();
37                         Global = new GlobalRootNamespace ();
38                         root_namespaces ["global"] = Global;
39                 }
40
41                 protected RootNamespace (string alias_name, Assembly assembly)
42                         : base (null, String.Empty)
43                 {
44                         this.alias_name = alias_name;
45                         referenced_assembly = assembly;
46
47                         all_namespaces = new Hashtable ();
48                         all_namespaces.Add ("", this);
49
50                         if (referenced_assembly != null)
51                                 ComputeNamespaces (this.referenced_assembly);
52                 }
53
54                 public static void DefineRootNamespace (string name, Assembly assembly)
55                 {
56                         if (name == "global") {
57                                 // FIXME: Add proper error number
58                                 Report.Error (-42, "Cannot define an external alias named `global'");
59                                 return;
60                         }
61                         RootNamespace retval = GetRootNamespace (name);
62                         if (retval == null || retval.referenced_assembly != assembly)
63                                 root_namespaces [name] = new RootNamespace (name, assembly);
64                 }
65
66                 public static RootNamespace GetRootNamespace (string name)
67                 {
68                         return (RootNamespace) root_namespaces [name];
69                 }
70
71                 public virtual Type LookupTypeReflection (string name, Location loc)
72                 {
73                         return GetTypeInAssembly (referenced_assembly, name);
74                 }
75
76                 public void RegisterNamespace (Namespace child)
77                 {
78                         if (child != this)
79                                 all_namespaces.Add (child.Name, child);
80                 }
81
82                 public bool IsNamespace (string name)
83                 {
84                         return all_namespaces.Contains (name);
85                 }
86
87                 protected void EnsureNamespace (string dotted_name)
88                 {
89                         if (dotted_name != null && dotted_name != "" && ! IsNamespace (dotted_name))
90                                 GetNamespace (dotted_name, true);
91                 }
92
93                 protected void ComputeNamespaces (Assembly assembly)
94                 {
95                         if (get_namespaces_method != null) {
96                                 string [] namespaces = (string []) get_namespaces_method.Invoke (assembly, null);
97                                 foreach (string ns in namespaces)
98                                         EnsureNamespace (ns);
99                                 return;
100                         }
101
102                         foreach (Type t in assembly.GetExportedTypes ())
103                                 EnsureNamespace (t.Namespace);
104                 }
105                 
106                 protected static Type GetTypeInAssembly (Assembly assembly, string name)
107                 {
108                         Type t = assembly.GetType (name);
109                         if (t == null)
110                                 return null;
111
112                         if (t.IsPointer)
113                                 throw new InternalErrorException ("Use GetPointerType() to get a pointer");
114                         
115                         
116                         TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
117                         if (ta == TypeAttributes.NestedPrivate)
118                                 return null;
119                         
120                         if (ta == TypeAttributes.NotPublic ||
121                                         ta == TypeAttributes.NestedAssembly ||
122                                         ta == TypeAttributes.NestedFamANDAssem)
123                                 if (!TypeManager.IsFriendAssembly (t.Assembly))
124                                         return null;
125
126                         return t;
127                 }
128
129                 public override string ToString ()
130                 {
131                         return String.Format ("RootNamespace ({0}::)", alias_name);
132                 }
133
134                 public override string GetSignatureForError ()
135                 {
136                         return alias_name + "::";
137                 }
138         }
139
140         public class GlobalRootNamespace : RootNamespace {
141                 Assembly [] assemblies;
142                 Module [] modules;
143
144                 public GlobalRootNamespace ()
145                         : base ("global", null)
146                 {
147                         assemblies = new Assembly [0];
148                 }
149
150                 public Assembly [] Assemblies {
151                         get { return assemblies; }
152                 }
153
154                 public Module [] Modules {
155                         get { return modules; }
156                 }
157
158                 public void AddAssemblyReference (Assembly a)
159                 {
160                         foreach (Assembly assembly in assemblies) {
161                                 if (a == assembly)
162                                         return;
163                         }
164
165                         int top = assemblies.Length;
166                         Assembly [] n = new Assembly [top + 1];
167                         assemblies.CopyTo (n, 0);
168                         n [top] = a;
169                         assemblies = n;
170
171                         ComputeNamespaces (a);
172                 }
173
174                 public void AddModuleReference (Module m)
175                 {
176                         int top = modules != null ? modules.Length : 0;
177                         Module [] n = new Module [top + 1];
178                         if (modules != null)
179                                 modules.CopyTo (n, 0);
180                         n [top] = m;
181                         modules = n;
182
183                         if (m == CodeGen.Module.Builder)
184                                 return;
185
186                         foreach (Type t in m.GetTypes ())
187                                 EnsureNamespace (t.Namespace);
188                 }
189
190                 public override Type LookupTypeReflection (string name, Location loc)
191                 {
192                         Type found_type = null;
193                 
194                         foreach (Assembly a in assemblies) {
195                                 Type t = GetTypeInAssembly (a, name);
196                                 if (t == null)
197                                         continue;
198                                         
199                                 if (found_type == null) {
200                                         found_type = t;
201                                         continue;
202                                 }
203
204                                 Report.SymbolRelatedToPreviousError (found_type);
205                                 Report.SymbolRelatedToPreviousError (t);
206                                 Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
207                                         
208                                 return found_type;
209                         }
210
211                         if (modules != null) {
212                                 foreach (Module module in modules) {
213                                         Type t = module.GetType (name);
214                                         if (t == null)
215                                                 continue;
216
217                                         if (found_type == null) {
218                                                 found_type = t;
219                                                 continue;
220                                         }
221                                         
222                                         Report.SymbolRelatedToPreviousError (t);
223                                         Report.SymbolRelatedToPreviousError (found_type);
224                                         Report.Warning (436, 2, loc, "Ignoring imported type `{0}' since the current assembly already has a declaration with the same name",
225                                                 TypeManager.CSharpName (t));
226                                         return t;
227                                 }
228                         }
229
230                         return found_type;
231                 }
232         }
233
234         /// <summary>
235         ///   Keeps track of the namespaces defined in the C# code.
236         ///
237         ///   This is an Expression to allow it to be referenced in the
238         ///   compiler parse/intermediate tree during name resolution.
239         /// </summary>
240         public class Namespace : FullNamedExpression {
241                 
242                 Namespace parent;
243                 string fullname;
244                 Hashtable namespaces;
245                 IDictionary declspaces;
246                 Hashtable cached_types;
247                 RootNamespace root;
248
249                 public readonly MemberName MemberName;
250
251                 /// <summary>
252                 ///   Constructor Takes the current namespace and the
253                 ///   name.  This is bootstrapped with parent == null
254                 ///   and name = ""
255                 /// </summary>
256                 public Namespace (Namespace parent, string name)
257                 {
258                         // Expression members.
259                         this.eclass = ExprClass.Namespace;
260                         this.Type = null;
261                         this.loc = Location.Null;
262
263                         this.parent = parent;
264
265                         if (parent != null)
266                                 this.root = parent.root;
267                         else
268                                 this.root = this as RootNamespace;
269
270                         if (this.root == null)
271                                 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
272                         
273                         string pname = parent != null ? parent.Name : "";
274                                 
275                         if (pname == "")
276                                 fullname = name;
277                         else
278                                 fullname = parent.Name + "." + name;
279
280                         if (fullname == null)
281                                 throw new InternalErrorException ("Namespace has a null fullname");
282
283                         if (parent != null && parent.MemberName != MemberName.Null)
284                                 MemberName = new MemberName (
285                                         parent.MemberName, name, parent.MemberName.Location);
286                         else if (name == "")
287                                 MemberName = MemberName.Null;
288                         else
289                                 MemberName = new MemberName (name, Location.Null);
290
291                         namespaces = new Hashtable ();
292                         cached_types = new Hashtable ();
293
294                         root.RegisterNamespace (this);
295                 }
296
297                 public override Expression DoResolve (EmitContext ec)
298                 {
299                         return this;
300                 }
301
302                 public override void Emit (EmitContext ec)
303                 {
304                         throw new InternalErrorException ("Expression tree referenced namespace " + fullname + " during Emit ()");
305                 }
306
307                 public override string GetSignatureForError ()
308                 {
309                         return Name;
310                 }
311                 
312                 public Namespace GetNamespace (string name, bool create)
313                 {
314                         int pos = name.IndexOf ('.');
315
316                         Namespace ns;
317                         string first;
318                         if (pos >= 0)
319                                 first = name.Substring (0, pos);
320                         else
321                                 first = name;
322
323                         ns = (Namespace) namespaces [first];
324                         if (ns == null) {
325                                 if (!create)
326                                         return null;
327
328                                 ns = new Namespace (this, first);
329                                 namespaces.Add (first, ns);
330                         }
331
332                         if (pos >= 0)
333                                 ns = ns.GetNamespace (name.Substring (pos + 1), create);
334
335                         return ns;
336                 }
337
338                 TypeExpr LookupType (string name, Location loc)
339                 {
340                         if (cached_types.Contains (name))
341                                 return cached_types [name] as TypeExpr;
342
343                         Type t = null;
344                         if (declspaces != null) {
345                                 DeclSpace tdecl = declspaces [name] as DeclSpace;
346                                 if (tdecl != null) {
347                                         //
348                                         // Note that this is not:
349                                         //
350                                         //   t = tdecl.DefineType ()
351                                         //
352                                         // This is to make it somewhat more useful when a DefineType
353                                         // fails due to problems in nested types (more useful in the sense
354                                         // of fewer misleading error messages)
355                                         //
356                                         tdecl.DefineType ();
357                                         t = tdecl.TypeBuilder;
358                                 }
359                         }
360                         string lookup = t != null ? t.FullName : (fullname == "" ? name : fullname + "." + name);
361                         Type rt = root.LookupTypeReflection (lookup, loc);
362                         if (t == null)
363                                 t = rt;
364
365                         TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
366                         cached_types [name] = te;
367                         return te;
368                 }
369
370                 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
371                 {
372                         if (namespaces.Contains (name))
373                                 return (Namespace) namespaces [name];
374
375                         TypeExpr te = LookupType (name, loc);
376                         if (te == null || !ds.CheckAccessLevel (te.Type))
377                                 return null;
378
379                         return te;
380                 }
381
382                 public void AddDeclSpace (string name, DeclSpace ds)
383                 {
384                         if (declspaces == null)
385                                 declspaces = new HybridDictionary ();
386                         declspaces.Add (name, ds);
387                 }
388
389                 /// <summary>
390                 ///   The qualified name of the current namespace
391                 /// </summary>
392                 public string Name {
393                         get { return fullname; }
394                 }
395
396                 public override string FullName {
397                         get { return fullname; }
398                 }
399
400                 /// <summary>
401                 ///   The parent of this namespace, used by the parser to "Pop"
402                 ///   the current namespace declaration
403                 /// </summary>
404                 public Namespace Parent {
405                         get { return parent; }
406                 }
407
408                 public override string ToString ()
409                 {
410                         return String.Format ("Namespace ({0})", Name);
411                 }
412         }
413
414         public class NamespaceEntry {
415                 Namespace ns;
416                 NamespaceEntry parent, implicit_parent;
417                 SourceFile file;
418                 int symfile_id;
419                 Hashtable aliases;
420                 ArrayList using_clauses;
421                 public bool DeclarationFound = false;
422                 public bool UsingFound = false;
423
424                 static ArrayList entries = new ArrayList ();
425
426                 public static void Reset ()
427                 {
428                         entries = new ArrayList ();
429                 }
430
431                 //
432                 // This class holds the location where a using definition is
433                 // done, and whether it has been used by the program or not.
434                 //
435                 // We use this to flag using clauses for namespaces that do not
436                 // exist.
437                 //
438                 public class UsingEntry {
439                         public readonly MemberName Name;
440                         readonly Expression Expr;
441                         readonly NamespaceEntry NamespaceEntry;
442                         readonly Location Location;
443                         
444                         public UsingEntry (NamespaceEntry entry, MemberName name, Location loc)
445                         {
446                                 Name = name;
447                                 Expr = name.GetTypeExpression ();
448                                 NamespaceEntry = entry;
449                                 Location = loc;
450                         }
451
452                         internal Namespace resolved;
453
454                         public Namespace Resolve ()
455                         {
456                                 if (resolved != null)
457                                         return resolved;
458
459                                 DeclSpace root = RootContext.Tree.Types;
460                                 root.NamespaceEntry = NamespaceEntry;
461                                 FullNamedExpression fne = Expr.ResolveAsTypeStep (root.EmitContext, false);
462                                 root.NamespaceEntry = null;
463
464                                 if (fne == null) {
465                                         Error_NamespaceNotFound (Location, Name.ToString ());
466                                         return null;
467                                 }
468
469                                 resolved = fne as Namespace;
470                                 if (resolved == null) {
471                                         Report.Error (138, Location,
472                                                 "`{0} is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.ToString ());
473                                 }
474                                 return resolved;
475                         }
476                 }
477
478                 public abstract class AliasEntry {
479                         public readonly string Name;
480                         public readonly NamespaceEntry NamespaceEntry;
481                         public readonly Location Location;
482                         
483                         protected AliasEntry (NamespaceEntry entry, string name, Location loc)
484                         {
485                                 Name = name;
486                                 NamespaceEntry = entry;
487                                 Location = loc;
488                         }
489                         
490                         protected FullNamedExpression resolved;
491                         bool error;
492
493                         public FullNamedExpression Resolve ()
494                         {
495                                 if (resolved != null || error)
496                                         return resolved;
497                                 resolved = DoResolve ();
498                                 if (resolved == null)
499                                         error = true;
500                                 return resolved;
501                         }
502
503                         protected abstract FullNamedExpression DoResolve ();
504                 }
505
506                 public class LocalAliasEntry : AliasEntry
507                 {
508                         public readonly Expression Alias;
509                         
510                         public LocalAliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc) :
511                                 base (entry, name, loc)
512                         {
513                                 Alias = alias.GetTypeExpression ();
514                         }
515
516                         protected override FullNamedExpression DoResolve ()
517                         {
518                                 DeclSpace root = RootContext.Tree.Types;
519                                 root.NamespaceEntry = NamespaceEntry;
520                                 resolved = Alias.ResolveAsTypeStep (root.EmitContext, false);
521                                 root.NamespaceEntry = null;
522
523                                 if (resolved == null)
524                                         Error_NamespaceNotFound (Location, Alias.ToString ());
525                                 return resolved;
526                         }
527                 }
528
529                 public class ExternAliasEntry : AliasEntry 
530                 {
531                         public ExternAliasEntry (NamespaceEntry entry, string name, Location loc) :
532                                 base (entry, name, loc)
533                         {
534                         }
535
536                         protected override FullNamedExpression DoResolve ()
537                         {
538                                 resolved = RootNamespace.GetRootNamespace (Name);
539                                 if (resolved == null)
540                                         Report.Error (430, Location, "The extern alias '" + Name +
541                                                                         "' was not specified in a /reference option");
542
543                                 return resolved;
544                         }
545                 }
546
547                 public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
548                 {
549                         this.parent = parent;
550                         this.file = file;
551                         this.IsImplicit = false;
552                         entries.Add (this);
553                         this.ID = entries.Count;
554
555                         if (parent != null)
556                                 ns = parent.NS.GetNamespace (name, true);
557                         else if (name != null)
558                                 ns = RootNamespace.Global.GetNamespace (name, true);
559                         else
560                                 ns = RootNamespace.Global;
561                 }
562
563                 private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
564                 {
565                         this.parent = parent;
566                         this.file = file;
567                         // no need to add self to 'entries', since we don't have any aliases or using entries.
568                         this.ID = -1;
569                         this.IsImplicit = true;
570                         this.ns = ns;
571                 }
572
573                 //
574                 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
575                 // resolved as if the immediately containing namespace body has no using-directives.
576                 //
577                 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
578                 // in the using-namespace-directive.
579                 //
580                 // To implement these rules, the expressions in the using directives are resolved using 
581                 // the "doppelganger" (ghostly bodiless duplicate).
582                 //
583                 NamespaceEntry doppelganger;
584                 NamespaceEntry Doppelganger {
585                         get {
586                                 if (!IsImplicit && doppelganger == null)
587                                         doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
588                                 return doppelganger;
589                         }
590                 }
591
592                 public readonly int ID;
593                 public readonly bool IsImplicit;
594
595                 public Namespace NS {
596                         get { return ns; }
597                 }
598
599                 public NamespaceEntry Parent {
600                         get { return parent; }
601                 }
602
603                 public NamespaceEntry ImplicitParent {
604                         get {
605                                 if (parent == null)
606                                         return null;
607                                 if (implicit_parent == null) {
608                                         implicit_parent = (parent.NS == ns.Parent)
609                                                 ? parent
610                                                 : new NamespaceEntry (parent, file, ns.Parent);
611                                 }
612                                 return implicit_parent;
613                         }
614                 }
615
616                 /// <summary>
617                 ///   Records a new namespace for resolving name references
618                 /// </summary>
619                 public void Using (MemberName name, Location loc)
620                 {
621                         if (DeclarationFound){
622                                 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
623                                 return;
624                         }
625
626                         if (name.Equals (ns.MemberName))
627                                 return;
628                         
629                         if (using_clauses == null)
630                                 using_clauses = new ArrayList ();
631
632                         foreach (UsingEntry old_entry in using_clauses) {
633                                 if (name.Equals (old_entry.Name)) {
634                                         Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetName ());
635                                                 return;
636                                         }
637                                 }
638
639                         UsingEntry ue = new UsingEntry (Doppelganger, name, loc);
640                         using_clauses.Add (ue);
641                 }
642
643                 public void UsingAlias (string name, MemberName alias, Location loc)
644                 {
645                         if (DeclarationFound){
646                                 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
647                                 return;
648                         }
649
650                         if (aliases == null)
651                                 aliases = new Hashtable ();
652
653                         if (aliases.Contains (name)) {
654                                 AliasEntry ae = (AliasEntry)aliases [name];
655                                 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
656                                 Report.Error (1537, loc, "The using alias `" + name +
657                                               "' appeared previously in this namespace");
658                                 return;
659                         }
660
661                         if (RootContext.Version == LanguageVersion.Default &&
662                             name == "global" && RootContext.WarningLevel >= 2)
663                                 Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
664                                         " the global namespace will be used instead");
665
666                         aliases [name] = new LocalAliasEntry (Doppelganger, name, alias, loc);
667                 }
668
669                 public void UsingExternalAlias (string name, Location loc)
670                 {
671                         if (UsingFound || DeclarationFound) {
672                                 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
673                                 return;
674                         }
675                         
676                         if (aliases == null)
677                                 aliases = new Hashtable ();
678                         
679                         if (aliases.Contains (name)) {
680                                 AliasEntry ae = (AliasEntry) aliases [name];
681                                 Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
682                                 Report.Error (1537, loc, "The using alias `" + name +
683                                               "' appeared previously in this namespace");
684                                 return;
685                         }
686
687                         if (name == "global") {
688                                 Report.Error (1681, loc, "You cannot redefine the global extern alias");
689                                 return;
690                         }
691
692                         aliases [name] = new ExternAliasEntry (Doppelganger, name, loc);
693                 }
694
695                 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
696                 {
697                         // Precondition: Only simple names (no dots) will be looked up with this function.
698                         FullNamedExpression resolved = null;
699                         for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
700                                 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
701                                         break;
702                         }
703                         return resolved;
704                 }
705
706                 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
707                 {
708                         Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
709                                 name, t1.FullName, t2.FullName);
710                 }
711
712                 // Looks-up a alias named @name in this and surrounding namespace declarations
713                 public FullNamedExpression LookupAlias (string name)
714                 {
715                         AliasEntry entry = null;
716                         // We use Parent rather than ImplicitParent since we know implicit namespace declarations
717                         // cannot have using entries.
718                         for (NamespaceEntry n = this; n != null; n = n.Parent) {
719                                 if (n.aliases == null)
720                                         continue;
721                                 entry = n.aliases [name] as AliasEntry;
722                                 if (entry != null)
723                                         return entry.Resolve ();
724                         }
725                         return null;
726                 }
727
728                 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
729                 {
730                         //
731                         // Check whether it's in the namespace.
732                         //
733                         FullNamedExpression fne = NS.Lookup (ds, name, loc);
734                         if (fne != null)
735                                 return fne;
736
737                         if (IsImplicit)
738                                 return null;
739
740                         //
741                         // Check aliases.
742                         //
743                         if (aliases != null) {
744                                 AliasEntry entry = aliases [name] as AliasEntry;
745                                 if (entry != null)
746                                         return entry.Resolve ();
747                         }
748
749                         //
750                         // Check using entries.
751                         //
752                         FullNamedExpression match = null;
753                         foreach (Namespace using_ns in GetUsingTable ()) {
754                                 match = using_ns.Lookup (ds, name, loc);
755                                 if (match == null || !(match is TypeExpr))
756                                         continue;
757                                 if (fne != null) {
758                                         if (!ignore_cs0104)
759                                                 Error_AmbiguousTypeReference (loc, name, fne, match);
760                                         return null;
761                                 }
762                                 fne = match;
763                         }
764
765                         return fne;
766                 }
767
768                 // Our cached computation.
769                 readonly Namespace [] empty_namespaces = new Namespace [0];
770                 Namespace [] namespace_using_table;
771                 Namespace [] GetUsingTable ()
772                 {
773                         if (namespace_using_table != null)
774                                 return namespace_using_table;
775
776                         if (using_clauses == null) {
777                                 namespace_using_table = empty_namespaces;
778                                 return namespace_using_table;
779                         }
780
781                         ArrayList list = new ArrayList (using_clauses.Count);
782
783                         foreach (UsingEntry ue in using_clauses) {
784                                 Namespace using_ns = ue.Resolve ();
785                                 if (using_ns == null)
786                                         continue;
787
788                                 list.Add (using_ns);
789                         }
790
791                         namespace_using_table = new Namespace [list.Count];
792                         list.CopyTo (namespace_using_table, 0);
793                         return namespace_using_table;
794                 }
795
796                 readonly string [] empty_using_list = new string [0];
797
798                 public int SymbolFileID {
799                         get {
800                                 if (symfile_id == 0 && file.SourceFileEntry != null) {
801                                         int parent_id = parent == null ? 0 : parent.SymbolFileID;
802
803                                         string [] using_list = empty_using_list;
804                                         if (using_clauses != null) {
805                                                 using_list = new string [using_clauses.Count];
806                                                 for (int i = 0; i < using_clauses.Count; i++)
807                                                         using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString ();
808                                         }
809
810                                         symfile_id = CodeGen.SymbolWriter.DefineNamespace (ns.Name, file.SourceFileEntry, using_list, parent_id);
811                                 }
812                                 return symfile_id;
813                         }
814                 }
815
816                 static void MsgtryRef (string s)
817                 {
818                         Console.WriteLine ("    Try using -r:" + s);
819                 }
820
821                 static void MsgtryPkg (string s)
822                 {
823                         Console.WriteLine ("    Try using -pkg:" + s);
824                 }
825
826                 public static void Error_NamespaceNotFound (Location loc, string name)
827                 {
828                         Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
829                                 name);
830
831                         switch (name) {
832                         case "Gtk": case "GtkSharp":
833                                 MsgtryPkg ("gtk-sharp");
834                                 break;
835
836                         case "Gdk": case "GdkSharp":
837                                 MsgtryPkg ("gdk-sharp");
838                                 break;
839
840                         case "Glade": case "GladeSharp":
841                                 MsgtryPkg ("glade-sharp");
842                                 break;
843
844                         case "System.Drawing":
845                         case "System.Web.Services":
846                         case "System.Web":
847                         case "System.Data":
848                         case "System.Windows.Forms":
849                                 MsgtryRef (name);
850                                 break;
851                         }
852                 }
853
854                 /// <summary>
855                 ///   Used to validate that all the using clauses are correct
856                 ///   after we are finished parsing all the files.  
857                 /// </summary>
858                 void VerifyUsing ()
859                 {
860                         if (using_clauses != null) {
861                                 foreach (UsingEntry ue in using_clauses)
862                                         ue.Resolve ();
863                         }
864
865                         if (aliases != null) {
866                                 foreach (DictionaryEntry de in aliases)
867                                         ((AliasEntry) de.Value).Resolve ();
868                         }
869                 }
870
871                 /// <summary>
872                 ///   Used to validate that all the using clauses are correct
873                 ///   after we are finished parsing all the files.  
874                 /// </summary>
875                 static public void VerifyAllUsing ()
876                 {
877                         foreach (NamespaceEntry entry in entries)
878                                 entry.VerifyUsing ();
879                 }
880
881                 public string GetSignatureForError ()
882                 {
883                         return ns.GetSignatureForError ();
884                 }
885
886                 public override string ToString ()
887                 {
888                         return ns.ToString ();
889                 }
890         }
891 }