2003-09-07 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / mcs / tree.cs
1 //
2 // tree.cs: keeps a tree representation of the generated code
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //
6 // Licensed under the terms of the GNU GPL
7 //
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
9 //
10 //
11
12 using System;
13 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.IO;
17
18 namespace Mono.CSharp
19 {
20
21         public interface ITreeDump {
22                 int  Dump (Tree tree, StreamWriter output);
23                 void ParseOptions (string options);
24         }
25
26         // <summary>
27         //   
28         //   We store here all the toplevel types that we have parsed,
29         //   this is the root of all information we have parsed.
30         // 
31         // </summary>
32         
33         public class Tree {
34                 TypeContainer root_types;
35
36                 // <summary>
37                 //  Keeps track of namespaces defined in the source code
38                 // </summary>
39                 Hashtable namespaces;
40
41                 // <summary>
42                 //   Keeps track of all the types definied (classes, structs, ifaces, enums)
43                 // </summary>
44                 Hashtable decls;
45                 
46                 public Tree ()
47                 {
48                         root_types = new TypeContainer (null, null, "", new Location (-1));
49
50                         decls = new Hashtable ();
51                         namespaces = new Hashtable ();
52                 }
53
54                 DoubleHash decl_ns_name = new DoubleHash ();
55                 
56                 public void RecordDecl (string name, DeclSpace ds)
57                 {
58                         if (decls.Contains (name)){
59                                 Report.Error (
60                                         101, ds.Location,
61                                         "There is already a definition for `" + name + "'");
62                                 DeclSpace other = (DeclSpace) decls [name];
63                                 Report.Error (0,
64                                         other.Location, "(Location of symbol related to previous error)");
65                                 return;
66                         }
67
68                         ds.RecordDecl ();
69
70                         int p = name.LastIndexOf ('.');
71                         if (p == -1)
72                                 decl_ns_name.Insert ("", name, ds);
73                         else {
74                                 decl_ns_name.Insert (name.Substring (0, p), name.Substring (p+1), ds);
75                         }
76
77                         decls.Add (name, ds);
78                 }
79
80                 public DeclSpace LookupByNamespace (string ns, string name)
81                 {
82                         object res;
83                         
84                         decl_ns_name.Lookup (ns, name, out res);
85                         return (DeclSpace) res;
86                 }
87                 
88                 public NamespaceEntry RecordNamespace (NamespaceEntry parent, SourceFile file, string name, Location loc)
89                 {
90                         NamespaceEntry ns = new NamespaceEntry (parent, file, name, loc);
91
92                         if (namespaces.Contains (file)){
93                                 Hashtable ns_ns = (Hashtable) namespaces [file];
94
95                                 if (ns_ns.Contains (ns.FullName))
96                                         return (NamespaceEntry) ns_ns [ns.FullName];
97                                 ns_ns.Add (ns.FullName, ns);
98                         } else {
99                                 Hashtable new_table = new Hashtable ();
100                                 namespaces [file] = new_table;
101
102                                 new_table.Add (ns.FullName, ns);
103                         }
104
105                         return ns;
106                 }
107
108                 //
109                 // FIXME: Why are we using Types?
110                 //
111                 public TypeContainer Types {
112                         get {
113                                 return root_types;
114                         }
115                 }
116
117                 public Hashtable Decls {
118                         get {
119                                 return decls;
120                         }
121                 }
122
123                 public Hashtable Namespaces {
124                         get {
125                                 return namespaces;
126                         }
127                 }
128         }
129 }