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 String.Concat (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");
97 if (using_clauses == null)
98 using_clauses = new ArrayList ();
100 foreach (UsingEntry old_entry in using_clauses){
101 if (old_entry.Name == ns){
102 Report.Warning (105, loc, "The using directive for '" + ns +
103 "' appeared previously in this namespace");
108 UsingEntry ue = new UsingEntry (ns, loc);
109 using_clauses.Add (ue);
112 public ArrayList UsingTable {
114 return using_clauses;
118 public void UsingAlias (string alias, string namespace_or_type, Location loc)
121 aliases = new Hashtable ();
123 if (aliases.Contains (alias)){
124 Report.Error (1537, loc, "The using alias `" + alias +
125 "' appeared previously in this namespace");
129 aliases [alias] = namespace_or_type;
132 public string LookupAlias (string alias)
136 // System.Console.WriteLine ("Lookup " + alias + " in " + name);
139 value = (string) (aliases [alias]);
140 if (value == null && Parent != null)
141 value = Parent.LookupAlias (alias);
147 /// Used to validate that all the using clauses are correct
148 /// after we are finished parsing all the files.
150 public static bool VerifyUsing ()
152 ArrayList unused = new ArrayList ();
155 foreach (Namespace ns in all_namespaces){
156 ArrayList uses = ns.UsingTable;
160 foreach (UsingEntry ue in uses){
168 // If we have unused using aliases, load all namespaces and check
169 // whether it is unused, or it was missing
171 if (unused.Count > 0){
172 Hashtable namespaces = TypeManager.GetNamespaces ();
174 foreach (UsingEntry ue in unused){
175 if (namespaces.Contains (ue.Name)){
176 Report.Warning (6024, ue.Location, "Unused namespace in `using' declaration");
181 Report.Error (246, ue.Location, "The namespace `" + ue.Name +
182 "' can not be found (missing assembly reference?)");