2002-07-09 Rafael Teixeira <rafaelteixeirabr@hotmail.com>
[mono.git] / mcs / mbas / namespace.cs
1 //
2 // namespace.cs: Tracks namespaces
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 using System;
10 using System.Collections;
11 using Mono.Languages;
12
13 namespace Mono.CSharp {
14
15         /// <summary>
16         ///   Keeps track of the namespaces defined in the C# code.
17         /// </summary>
18         public class Namespace {
19                 Namespace parent;
20                 string name;
21                 ArrayList using_clauses;
22                 Hashtable aliases;
23                 public bool DeclarationFound = false;
24                 
25                 /// <summary>
26                 ///   Constructor Takes the current namespace and the
27                 ///   name.  This is bootstrapped with parent == null
28                 ///   and name = ""
29                 /// </summary>
30                 public Namespace (Namespace parent, string name)
31                 {
32                         this.name = name;
33                         this.parent = parent;
34                 }
35
36                 /// <summary>
37                 ///   The qualified name of the current namespace
38                 /// </summary>
39                 public string Name {
40                         get {
41                                 string pname = parent != null ? parent.Name : "";
42                                 
43                                 if (pname == "")
44                                         return name;
45                                 else
46                                         return parent.Name + "." + name;
47                         }
48                 }
49
50                 /// <summary>
51                 ///   The parent of this namespace, used by the parser to "Pop"
52                 ///   the current namespace declaration
53                 /// </summary>
54                 public Namespace Parent {
55                         get {
56                                 return parent;
57                         }
58                 }
59
60                 /// <summary>
61                 ///   Records a new namespace for resolving name references
62                 /// </summary>
63                 public void Using (string ns, Location loc)
64                 {
65                         if (DeclarationFound){
66                                 Report.Error (1529, loc, "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                 public void UsingAlias (string alias, string namespace_or_type, Location loc)
83                 {
84                         if (aliases == null)
85                                 aliases = new Hashtable ();
86                         
87                         if (aliases.Contains (alias)){
88                                 Report.Error (1537, loc, "The using alias `" + alias +
89                                               "' appeared previously in this namespace");
90                                 return;
91                         }
92                                         
93                         aliases [alias] = namespace_or_type;
94                 }
95
96                 public string LookupAlias (string alias)
97                 {
98                         string value = null;
99
100                         // System.Console.WriteLine ("Lookup " + alias + " in " + name);
101
102                         if (aliases != null)
103                                 value = (string) (aliases [alias]);
104                         if (value == null && Parent != null)
105                                 value = Parent.LookupAlias (alias);
106
107                         return value;
108                 }
109
110                 /// <summary>
111                 ///   Used to validate that all the using clauses are correct
112                 ///   after we are finished parsing all the files
113                 /// </summary>
114                 public void VerifyUsing ()
115                 {
116                         foreach (DictionaryEntry de in using_clauses){
117                                 if (de.Value == null){
118                                         string name = (string) de.Key;
119                                         
120                                         Report.Error (234, "The type or namespace `" +
121                                                             name + "' does not exist in the " +
122                                                             "class or namespace `" + name + "'");
123                                 }
124                         }
125                 }
126
127         }
128 }
129