2 // namespace.cs: Tracks namespaces
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
10 using System.Collections;
13 namespace Mono.CSharp {
16 /// Keeps track of the namespaces defined in the C# code.
18 public class Namespace {
19 static ArrayList all_namespaces = new ArrayList ();
23 ArrayList using_clauses;
25 public bool DeclarationFound = false;
28 // This class holds the location where a using definition is
29 // done, and whether it has been used by the program or not.
31 // We use this to flag using clauses for namespaces that do not
34 public class UsingEntry {
37 public Location Location;
39 public UsingEntry (string name, Location loc)
48 /// Constructor Takes the current namespace and the
49 /// name. This is bootstrapped with parent == null
52 public Namespace (Namespace parent, string name)
57 all_namespaces.Add (this);
61 /// The qualified name of the current namespace
65 string pname = parent != null ? parent.Name : "";
70 return parent.Name + "." + name;
75 /// The parent of this namespace, used by the parser to "Pop"
76 /// the current namespace declaration
78 public Namespace Parent {
85 /// Records a new namespace for resolving name references
87 public void Using (string ns, Location loc)
89 if (DeclarationFound){
90 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
94 if (using_clauses == null)
95 using_clauses = new ArrayList ();
97 UsingEntry ue = new UsingEntry (ns, loc);
98 using_clauses.Add (ue);
101 public ArrayList UsingTable {
103 return using_clauses;
107 public void UsingAlias (string alias, string namespace_or_type, Location loc)
110 aliases = new CaseInsensitiveHashtable ();
112 if (aliases.Contains (alias)){
113 Report.Error (1537, loc, "The using alias `" + alias +
114 "' appeared previously in this namespace");
118 aliases [alias] = namespace_or_type;
121 public string LookupAlias (string alias)
125 // System.Console.WriteLine ("Lookup " + alias + " in " + name);
128 value = (string) (aliases [alias]);
129 if (value == null && Parent != null)
130 value = Parent.LookupAlias (alias);
136 /// Used to validate that all the using clauses are correct
137 /// after we are finished parsing all the files.
139 public static bool VerifyUsing ()
141 ArrayList unused = new ArrayList ();
144 foreach (Namespace ns in all_namespaces){
145 ArrayList uses = ns.UsingTable;
149 foreach (UsingEntry ue in uses){
157 // If we have unused using aliases, load all namespaces and check
158 // whether it is unused, or it was missing
160 if (unused.Count > 0){
161 CaseInsensitiveHashtable namespaces = TypeManager.GetNamespaces ();
163 foreach (UsingEntry ue in unused){
164 if (namespaces.Contains (ue.Name)){
165 Report.Warning (6024, ue.Location, "Unused namespace in `using' declaration");
170 Report.Error (246, ue.Location, "The namespace `" + ue.Name +
171 "' can not be found (missing assembly reference?)");