2 // namespace.cs: Tracks namespaces
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
10 using System
.Collections
;
12 namespace Mono
.CSharp
{
15 /// Keeps track of the namespaces defined in the C# code.
17 /// This is an Expression to allow it to be referenced in the
18 /// compiler parse/intermediate tree during name resolution.
20 public class Namespace
: FullNamedExpression
, IAlias
{
21 static ArrayList all_namespaces
= new ArrayList ();
22 static Hashtable namespaces_map
= new Hashtable ();
28 Hashtable defined_names
;
31 /// Constructor Takes the current namespace and the
32 /// name. This is bootstrapped with parent == null
35 public Namespace (Namespace parent
, string name
)
37 // Expression members.
38 this.eclass
= ExprClass
.Namespace
;
40 this.loc
= Location
.Null
;
44 string pname
= parent
!= null ? parent
.Name
: "";
49 fullname
= parent
.Name
+ "." + name
;
51 entries
= new ArrayList ();
52 namespaces
= new Hashtable ();
53 defined_names
= new Hashtable ();
55 all_namespaces
.Add (this);
56 if (namespaces_map
.Contains (fullname
))
58 namespaces_map
[fullname
] = true;
61 public override Expression
DoResolve (EmitContext ec
)
66 public override void Emit (EmitContext ec
)
68 throw new InternalErrorException ("Expression tree referenced namespace " + fullname
+ " during Emit ()");
71 public static bool IsNamespace (string name
)
73 return namespaces_map
[name
] != null;
76 public static Namespace Root
= new Namespace (null, "");
78 public Namespace
GetNamespace (string name
, bool create
)
80 int pos
= name
.IndexOf ('.');
85 first
= name
.Substring (0, pos
);
89 ns
= (Namespace
) namespaces
[first
];
94 ns
= new Namespace (this, first
);
95 namespaces
.Add (first
, ns
);
99 ns
= ns
.GetNamespace (name
.Substring (pos
+ 1), create
);
104 public static Namespace
LookupNamespace (string name
, bool create
)
106 return Root
.GetNamespace (name
, create
);
109 public FullNamedExpression
Lookup (DeclSpace ds
, string name
, Location loc
)
111 IAlias o
= (IAlias
) defined_names
[name
];
114 DeclSpace tdecl
= o
as DeclSpace
;
116 t
= tdecl
.DefineType ();
120 if ((ds
== null) || ds
.CheckAccessLevel (t
))
121 return new TypeExpression (t
, Location
.Null
);
124 Namespace ns
= GetNamespace (name
, false);
128 t
= TypeManager
.LookupType (DeclSpace
.MakeFQN (fullname
, name
));
129 if ((t
== null) || ((ds
!= null) && !ds
.CheckAccessLevel (t
)))
132 return new TypeExpression (t
, Location
.Null
);
135 public void AddNamespaceEntry (NamespaceEntry entry
)
140 public void DefineName (string name
, IAlias o
)
142 defined_names
.Add (name
, o
);
145 static public ArrayList UserDefinedNamespaces
{
147 return all_namespaces
;
152 /// The qualified name of the current namespace
160 public override string FullName
{
167 /// The parent of this namespace, used by the parser to "Pop"
168 /// the current namespace declaration
170 public Namespace Parent
{
176 public static void DefineNamespaces (SymbolWriter symwriter
)
178 foreach (Namespace ns
in all_namespaces
) {
179 foreach (NamespaceEntry entry
in ns
.entries
)
180 entry
.DefineNamespace (symwriter
);
185 /// Used to validate that all the using clauses are correct
186 /// after we are finished parsing all the files.
188 public static void VerifyUsing ()
190 foreach (Namespace ns
in all_namespaces
) {
191 foreach (NamespaceEntry entry
in ns
.entries
)
192 entry
.VerifyUsing ();
196 public override string ToString ()
199 return "Namespace (<root>)";
201 return String
.Format ("Namespace ({0})", Name
);
205 get { return false; }
208 TypeExpr IAlias
.ResolveAsType (EmitContext ec
)
210 throw new InvalidOperationException ();
214 public class NamespaceEntry
217 NamespaceEntry parent
, implicit_parent
;
221 ArrayList using_clauses
;
222 public bool DeclarationFound
= false;
225 // This class holds the location where a using definition is
226 // done, and whether it has been used by the program or not.
228 // We use this to flag using clauses for namespaces that do not
231 public class UsingEntry
{
232 public Expression Name
;
233 public readonly NamespaceEntry NamespaceEntry
;
234 public readonly Location Location
;
236 public UsingEntry (NamespaceEntry entry
, Expression name
, Location loc
)
239 NamespaceEntry
= entry
;
243 internal FullNamedExpression resolved
;
245 public Namespace
Resolve ()
247 if (resolved
!= null)
248 return resolved
as Namespace
;
250 DeclSpace root
= RootContext
.Tree
.Types
;
251 root
.NamespaceEntry
= NamespaceEntry
;
252 resolved
= Name
.ResolveAsTypeStep (root
.EmitContext
);
253 root
.NamespaceEntry
= null;
255 return resolved
as Namespace
;
259 public class AliasEntry
{
260 public readonly string Name
;
261 public readonly Expression Alias
;
262 public readonly NamespaceEntry NamespaceEntry
;
263 public readonly Location Location
;
265 public AliasEntry (NamespaceEntry entry
, string name
, Expression
alias, Location loc
)
269 NamespaceEntry
= entry
;
273 FullNamedExpression resolved
;
275 public FullNamedExpression
Resolve ()
277 if (resolved
!= null)
280 DeclSpace root
= RootContext
.Tree
.Types
;
281 root
.NamespaceEntry
= NamespaceEntry
;
282 resolved
= Alias
.ResolveAsTypeStep (root
.EmitContext
);
283 root
.NamespaceEntry
= null;
289 public NamespaceEntry (NamespaceEntry parent
, SourceFile file
, string name
, Location loc
)
291 this.parent
= parent
;
293 this.IsImplicit
= false;
297 ns
= parent
.NS
.GetNamespace (name
, true);
298 else if (name
!= null)
299 ns
= Namespace
.LookupNamespace (name
, true);
302 ns
.AddNamespaceEntry (this);
303 this.FullName
= ns
.Name
;
307 private NamespaceEntry (NamespaceEntry parent
, SourceFile file
, Namespace ns
)
309 this.parent
= parent
;
311 this.IsImplicit
= true;
314 this.FullName
= ns
.Name
;
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.
321 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
322 // in the using-namespace-directive.
324 // To implement these rules, the expressions in the using directives are resolved using
325 // the "doppelganger" (ghostly bodiless duplicate).
327 NamespaceEntry doppelganger
;
328 NamespaceEntry Doppelganger
{
330 if (!IsImplicit
&& doppelganger
== null)
331 doppelganger
= new NamespaceEntry (ImplicitParent
, file
, ns
);
336 static int next_id
= 0;
337 public readonly string FullName
;
338 public readonly int ID
;
339 public readonly bool IsImplicit
;
341 public Namespace NS
{
347 public NamespaceEntry Parent
{
353 public NamespaceEntry ImplicitParent
{
357 if (implicit_parent
== null) {
358 implicit_parent
= (parent
.NS
== ns
.Parent
)
360 : new NamespaceEntry (parent
, file
, ns
.Parent
);
362 return implicit_parent
;
366 public void DefineName (string name
, IAlias o
)
368 ns
.DefineName (name
, o
);
372 /// Records a new namespace for resolving name references
374 public void Using (Expression ns
, Location loc
)
376 string name
= ns
.ToString ();
377 if (DeclarationFound
){
378 Report
.Error (1529, loc
, "A using clause must precede all other namespace elements");
382 if (name
== FullName
)
385 if (using_clauses
== null)
386 using_clauses
= new ArrayList ();
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
);
397 UsingEntry ue
= new UsingEntry (Doppelganger
, ns
, loc
);
398 using_clauses
.Add (ue
);
401 public void UsingAlias (string name
, Expression
alias, Location loc
)
403 if (DeclarationFound
){
404 Report
.Error (1529, loc
, "A using clause must precede all other namespace elements");
409 aliases
= new Hashtable ();
411 if (aliases
.Contains (name
)){
412 Report
.Error (1537, loc
, "The using alias `" + name
+
413 "' appeared previously in this namespace");
417 aliases
[name
] = new AliasEntry (Doppelganger
, name
, alias, loc
);
420 public FullNamedExpression
LookupAlias (string alias)
422 AliasEntry entry
= null;
424 entry
= (AliasEntry
) aliases
[alias];
426 return entry
== null ? null : entry
.Resolve ();
429 public FullNamedExpression
LookupNamespaceOrType (DeclSpace ds
, string name
, Location loc
)
431 FullNamedExpression resolved
= null;
434 // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
435 int pos
= name
.IndexOf ('.');
437 rest
= name
.Substring (pos
+ 1);
438 name
= name
.Substring (0, pos
);
441 for (NamespaceEntry curr_ns
= this; curr_ns
!= null; curr_ns
= curr_ns
.ImplicitParent
) {
442 if ((resolved
= curr_ns
.Lookup (ds
, name
, loc
)) != null)
446 if (resolved
== null || rest
== null)
449 Namespace ns
= resolved
as Namespace
;
451 return ns
.Lookup (ds
, rest
, loc
);
453 Type nested
= TypeManager
.LookupType (resolved
.FullName
+ "." + rest
);
454 if ((nested
== null) || ((ds
!= null) && !ds
.CheckAccessLevel (nested
)))
457 return new TypeExpression (nested
, Location
.Null
);
460 private FullNamedExpression
Lookup (DeclSpace ds
, string name
, Location loc
)
462 // Precondition: Only simple names (no dots) will be looked up with this function.
465 // Check whether it's in the namespace.
467 FullNamedExpression o
= NS
.Lookup (ds
, name
, loc
);
477 o
= LookupAlias (name
);
482 // Check using entries.
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
)) {
489 DeclSpace
.Error_AmbiguousTypeReference (loc
, name
, t
.FullName
, match
.FullName
);
500 // Our cached computation.
501 Namespace
[] namespace_using_table
;
502 public Namespace
[] GetUsingTable ()
504 if (namespace_using_table
!= null)
505 return namespace_using_table
;
507 if (using_clauses
== null) {
508 namespace_using_table
= new Namespace
[0];
509 return namespace_using_table
;
512 ArrayList list
= new ArrayList (using_clauses
.Count
);
514 foreach (UsingEntry ue
in using_clauses
) {
515 Namespace using_ns
= ue
.Resolve ();
516 if (using_ns
== null)
522 namespace_using_table
= new Namespace
[list
.Count
];
523 list
.CopyTo (namespace_using_table
, 0);
524 return namespace_using_table
;
527 public void DefineNamespace (SymbolWriter symwriter
)
532 parent
.DefineNamespace (symwriter
);
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 ();
540 using_list
= new string [0];
543 int parent_id
= parent
!= null ? parent
.symfile_id
: 0;
544 if (file
.SourceFileEntry
== null)
547 symfile_id
= symwriter
.DefineNamespace (
548 ns
.Name
, file
.SourceFileEntry
, using_list
, parent_id
);
551 public int SymbolFileID
{
557 static void MsgtryRef (string s
)
559 Console
.WriteLine (" Try using -r:" + s
);
562 static void MsgtryPkg (string s
)
564 Console
.WriteLine (" Try using -pkg:" + s
);
567 protected void error246 (Location loc
, string name
)
569 Report
.Error (246, loc
, "The namespace `" + name
+
570 "' can not be found (missing assembly reference?)");
573 case "Gtk": case "GtkSharp":
574 MsgtryPkg ("gtk-sharp");
577 case "Gdk": case "GdkSharp":
578 MsgtryPkg ("gdk-sharp");
581 case "Glade": case "GladeSharp":
582 MsgtryPkg ("glade-sharp");
585 case "System.Drawing":
586 case "System.Web.Services":
589 case "System.Windows.Forms":
596 /// Used to validate that all the using clauses are correct
597 /// after we are finished parsing all the files.
599 public void VerifyUsing ()
601 if (using_clauses
!= null){
602 foreach (UsingEntry ue
in using_clauses
){
603 if (ue
.Resolve () != null)
606 if (ue
.resolved
== null)
607 error246 (ue
.Location
, ue
.Name
.ToString ());
609 Report
.Error (138, ue
.Location
, "The using keyword only lets you specify a namespace, " +
610 "`" + ue
.Name
+ "' is a class not a namespace.");
615 if (aliases
!= null){
616 foreach (DictionaryEntry de
in aliases
){
617 AliasEntry
alias = (AliasEntry
) de
.Value
;
619 if (alias.Resolve () != null)
622 error246 (alias.Location
, alias.Alias
.ToString ());
627 public override string ToString ()
629 if (NS
== Namespace
.Root
)
630 return "NamespaceEntry (<root>)";
632 return String
.Format ("NamespaceEntry ({0},{1},{2})", FullName
, IsImplicit
, ID
);