2 // namespace.cs: Tracks namespaces
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
10 using System.Collections;
13 namespace Mono.MonoBASIC {
16 /// Keeps track of the namespaces defined in the C# code.
18 public class Namespace {
19 static ArrayList all_namespaces = new ArrayList ();
23 Hashtable using_clauses;
26 public bool DeclarationFound = false;
29 // This class holds the location where a using definition is
30 // done, and whether it has been used by the program or not.
32 // We use this to flag using clauses for namespaces that do not
35 public class UsingEntry {
38 public Location Location;
40 public UsingEntry (string name, Location loc)
49 /// Constructor Takes the current namespace and the
50 /// name. This is bootstrapped with parent == null
53 public Namespace (Namespace parent, string name)
58 all_namespaces.Add (this);
62 /// The qualified name of the current namespace
66 string pname = parent != null ? parent.Name : "";
71 return parent.Name + "." + name;
76 /// The parent of this namespace, used by the parser to "Pop"
77 /// the current namespace declaration
79 public Namespace Parent {
86 /// Records a new namespace for resolving name references
88 public void Using (string ns, Location loc)
90 if (DeclarationFound){
91 Report.Error (1529, loc, "A using clause must precede all other namespace elements");
95 if (using_clauses == null)
96 using_clauses = new CaseInsensitiveHashtable ();
98 UsingEntry ue = new UsingEntry (ns, loc);
99 using_clauses [ns] = ue;
102 public ICollection UsingTable {
104 return using_clauses.Values;
108 public void UsingAlias (string alias, string namespace_or_type, Location loc)
111 aliases = new CaseInsensitiveHashtable ();
113 if (aliases.Contains (alias)){
114 Report.Error (1537, loc, "The using alias `" + alias +
115 "' appeared previously in this namespace");
119 aliases [alias] = namespace_or_type;
122 public string LookupAlias (string alias)
126 // System.Console.WriteLine ("Lookup " + alias + " in " + name);
129 value = (string) (aliases [alias]);
130 if (value == null && Parent != null)
131 value = Parent.LookupAlias (alias);
137 /// Used to validate that all the using clauses are correct
138 /// after we are finished parsing all the files.
140 public static bool VerifyUsing ()
142 ArrayList unused = new ArrayList ();
145 foreach (Namespace ns in all_namespaces){
146 foreach (UsingEntry ue in ns.UsingTable){
154 // If we have unused using aliases, load all namespaces and check
155 // whether it is unused, or it was missing
157 if (unused.Count > 0){
158 CaseInsensitiveHashtable namespaces = TypeManager.GetNamespaces ();
160 foreach (UsingEntry ue in unused){
161 if (namespaces.Contains (ue.Name)){
162 Report.Warning (6024, ue.Location, "Unused namespace in `using' declaration");
167 Report.Error (246, ue.Location, "The namespace `" + ue.Name +
168 "' can not be found (missing assembly reference?)");