2004-08-11 Bernie Solomon <bernard@ugsolutions.com>
[mono.git] / mcs / mbas / sourcebeingcompiled.cs
1 //
2 // sourcebeingcompiled.cs: Tracks once-per-source things 
3 //
4 // Author:
5 //   Rafael Teixeira (rafaelteixeirabr@hotmail.com)
6 //
7 // (C) 2004 Rafael Teixeira.
8 //
9
10 using System;
11 using System.Collections;
12 using Mono.Languages;
13
14 namespace Mono.MonoBASIC {
15
16         /// <summary>
17         ///   Keeps track of the once-per-source things in the VB.NET code.
18         /// </summary>
19         public class SourceBeingCompiled {
20
21                 Hashtable imports_clauses;
22                 Hashtable aliases;
23                 
24                 //
25                 // This class holds the location where a using definition is
26                 // done, and whether it has been used by the program or not.
27                 //
28                 // We use this to flag using clauses for namespaces that do not
29                 // exist.
30                 //
31                 public class ImportsEntry {
32                         public string Name;
33                         public bool Used;
34                         public Location Location;
35                         
36                         public ImportsEntry (string name, Location loc)
37                         {
38                                 Name = name;
39                                 Location = loc;
40                                 Used = false;
41                         }
42                 }
43                 
44                 public SourceBeingCompiled () { } 
45
46                 /// <summary>
47                 ///   Initializes the list of preimported namespaces
48                 /// </summary>
49                 public void InitializeImports (ArrayList ImportsList)
50                 {
51                         foreach(string preImportedNamespace in ImportsList)
52                                 this.Imports(preImportedNamespace, Location.Null);
53                 }
54
55                 /// <summary>
56                 ///   Records a new namespace for resolving name references
57                 /// </summary>
58                 public void Imports (string ns, Location loc)
59                 {
60                         if (imports_clauses == null)
61                                 imports_clauses = new CaseInsensitiveHashtable ();
62
63                         ImportsEntry ue = new ImportsEntry (ns, loc);
64                         imports_clauses [ns] = ue;
65                 }
66
67                 public ICollection ImportsTable {
68                         get {
69                                 return imports_clauses.Values;
70                         }
71                 }
72                 
73                 public string[] GetNamespacesInScope(string currentNamespace)
74                 {
75                         ArrayList list = new ArrayList();
76                         foreach(ImportsEntry ie in ImportsTable)
77                                 list.Add(ie.Name);
78                         list.Add(currentNamespace);
79                         return (string[])list.ToArray(typeof(string));
80                         
81                 }
82
83                 public void ImportsWithAlias (string alias, string namespace_or_type, Location loc)
84                 {
85                         if (aliases == null)
86                                 aliases = new CaseInsensitiveHashtable ();
87                         
88                         if (aliases.Contains (alias)){
89                                 Report.Error (1537, loc, "The Imports clause with alias '" + alias +
90                                               "' appeared previously in this namespace");
91                                 return;
92                         }
93                                         
94                         aliases [alias] = namespace_or_type;
95                 }
96
97                 public string LookupAlias (string alias)
98                 {
99                         string value = null;
100
101                         if (aliases != null)
102                                 value = (string) (aliases [alias]);
103
104                         return value;
105                 }
106
107                 /// <summary>
108                 ///   Used to validate that all the using clauses are correct
109                 ///   after we are finished parsing all the files.  
110                 /// </summary>
111                 public void VerifyImports ()
112                 {
113                         ArrayList unused = new ArrayList ();
114                         
115                         foreach (ImportsEntry ue in ImportsTable) {
116                                 if (ue.Used)
117                                         continue;
118                                 unused.Add (ue);
119                         }
120
121                         //
122                         // If we have unused imports aliases, load all namespaces and check
123                         // whether it is unused, or it was missing
124                         //
125 /* FIXME: why is happening a ghostly NullReferenceException inside TypeManager.GetNamespaces ()?
126
127                         if (unused.Count > 0) {
128                                 CaseInsensitiveHashtable namespaces = TypeManager.GetNamespaces ();
129
130                                 foreach (ImportsEntry ue in unused) {
131                                         if (namespaces.Contains (ue.Name)){
132                                                 Report.Warning (6024, ue.Location, "Unused namespace in 'Imports' declaration");
133                                                 continue;
134                                         }
135
136                                         Report.Error (246, ue.Location, "The namespace '" + ue.Name +
137                                                       "' can not be found (missing assembly reference?)");
138                                 }
139                         }
140 */
141                 }
142
143         }
144 }