Merged from MCS Wed Aug 27 09:42:30 CEST 2003.
[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 (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                         int p = name.LastIndexOf ('.');
69                         if (p == -1)
70                                 decl_ns_name.Insert ("", name, ds);
71                         else {
72                                 decl_ns_name.Insert (name.Substring (0, p), name.Substring (p+1), ds);
73                         }
74
75                         decls.Add (name, ds);
76                 }
77
78                 public DeclSpace LookupByNamespace (string ns, string name)
79                 {
80                         object res;
81                         
82                         decl_ns_name.Lookup (ns, name, out res);
83                         return (DeclSpace) res;
84                 }
85                 
86                 public NamespaceEntry RecordNamespace (NamespaceEntry parent, SourceFile file, string name, Location loc)
87                 {
88                         NamespaceEntry ns = new NamespaceEntry (parent, file, name, loc);
89
90                         if (namespaces.Contains (file)){
91                                 Hashtable ns_ns = (Hashtable) namespaces [file];
92
93                                 if (ns_ns.Contains (ns.FullName))
94                                         return (NamespaceEntry) ns_ns [ns.FullName];
95                                 ns_ns.Add (ns.FullName, ns);
96                         } else {
97                                 Hashtable new_table = new Hashtable ();
98                                 namespaces [file] = new_table;
99
100                                 new_table.Add (ns.FullName, ns);
101                         }
102
103                         return ns;
104                 }
105
106                 //
107                 // FIXME: Why are we using Types?
108                 //
109                 public TypeContainer Types {
110                         get {
111                                 return root_types;
112                         }
113                 }
114
115                 public Hashtable Decls {
116                         get {
117                                 return decls;
118                         }
119                 }
120
121                 public Hashtable Namespaces {
122                         get {
123                                 return namespaces;
124                         }
125                 }
126         }
127 }