2004-04-03 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / gmcs / 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 ();
49
50                         decls = new Hashtable ();
51                         namespaces = new Hashtable ();
52                 }
53
54                 DoubleHash decl_ns_name = new DoubleHash ();
55                 
56                 public void RecordDecl (MemberName member_name, DeclSpace ds)
57                 {
58                         string name = (string) member_name;
59
60                         if (decls.Contains (name)){
61                                 Report.Error (
62                                         101, ds.Location,
63                                         "There is already a definition for `" + name + "'");
64                                 DeclSpace other = (DeclSpace) decls [name];
65                                 Report.Error (0,
66                                         other.Location, "(Location of symbol related to previous error)");
67                                 return;
68                         }
69
70                         ds.RecordDecl ();
71
72                         int p = name.LastIndexOf ('.');
73                         if (p == -1)
74                                 decl_ns_name.Insert ("", name, ds);
75                         else {
76                                 decl_ns_name.Insert (name.Substring (0, p), name.Substring (p+1), ds);
77                         }
78
79                         decls.Add (name, ds);
80                 }
81
82                 public DeclSpace LookupByNamespace (string ns, string name)
83                 {
84                         object res;
85                         
86                         decl_ns_name.Lookup (ns, name, out res);
87                         return (DeclSpace) res;
88                 }
89                 
90                 public NamespaceEntry RecordNamespace (NamespaceEntry parent, SourceFile file, string name, Location loc)
91                 {
92                         NamespaceEntry ns = new NamespaceEntry (parent, file, name, loc);
93
94                         if (namespaces.Contains (file)){
95                                 Hashtable ns_ns = (Hashtable) namespaces [file];
96
97                                 if (ns_ns.Contains (ns.FullName))
98                                         return (NamespaceEntry) ns_ns [ns.FullName];
99                                 ns_ns.Add (ns.FullName, ns);
100                         } else {
101                                 Hashtable new_table = new Hashtable ();
102                                 namespaces [file] = new_table;
103
104                                 new_table.Add (ns.FullName, ns);
105                         }
106
107                         return ns;
108                 }
109
110                 //
111                 // FIXME: Why are we using Types?
112                 //
113                 public TypeContainer Types {
114                         get {
115                                 return root_types;
116                         }
117                 }
118
119                 public Hashtable Decls {
120                         get {
121                                 return decls;
122                         }
123                 }
124
125                 public Hashtable Namespaces {
126                         get {
127                                 return namespaces;
128                         }
129                 }
130         }
131 }