2001-10-17 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / namespace.cs
1 using System;
2 using System.Collections;
3
4 namespace CIR {
5
6         // <summary>
7         //   Keeps track of the namespaces defined in the C# code.
8         // </summary>
9         public class Namespace {
10                 Namespace parent;
11                 string name;
12                 ArrayList using_clauses;
13                 bool decl_found = false;
14                 
15                 // <summary>
16                 //   Constructor Takes the current namespace and the
17                 //   name.  This is bootstrapped with parent == null
18                 //   and name = ""
19                 // </summary>
20                 public Namespace (Namespace parent, string name)
21                 {
22                         this.name = name;
23                         this.parent = parent;
24                 }
25
26                 // <summary>
27                 //   The qualified name of the current namespace
28                 // </summary>
29                 public string Name {
30                         get {
31                                 string pname = parent != null ? parent.Name : "";
32                                 
33                                 if (pname == "")
34                                         return name;
35                                 else
36                                         return parent.Name + "." + name;
37                         }
38                 }
39
40                 // <summary>
41                 //   The parent of this namespace, used by the parser to "Pop"
42                 //   the current namespace declaration
43                 // </summary>
44                 public Namespace Parent {
45                         get {
46                                 return parent;
47                         }
48                 }
49
50                 // <summary>
51                 //   When a declaration is found in a namespace,
52                 //   we call this function, to emit an error if the
53                 //   program attempts to use a using clause afterwards
54                 // </summary>
55                 public void DeclarationFound ()
56                 {
57                         decl_found = true;
58                 }
59
60                 // <summary>
61                 //   Records a new namespace for resolving name references
62                 // </summary>
63                 public void Using (string ns)
64                 {
65                         if (decl_found){
66                                 CSharpParser.error (1529, "A using clause must precede all other namespace elements");
67                                 return;
68                         }
69                         
70                         if (using_clauses == null)
71                                 using_clauses = new ArrayList ();
72
73                         using_clauses.Add (ns);
74                 }
75
76                 public ArrayList UsingTable {
77                         get {
78                                 return using_clauses;
79                         }
80                 }
81
82                 // <summary>
83                 //   Used to validate that all the using clauses are correct
84                 //   after we are finished parsing all the files
85                 // </summary>
86                 public void VerifyUsing ()
87                 {
88                         foreach (DictionaryEntry de in using_clauses){
89                                 if (de.Value == null){
90                                         string name = (string) de.Key;
91                                         
92                                         CSharpParser.error (234, "The type or namespace `" +
93                                                             name + "' does not exist in the " +
94                                                             "class or namespace `" + name + "'");
95                                 }
96                         }
97                 }
98         }
99 }
100