2 // Mono.ILASM.ClassTable.cs
5 // Jackson Harper (Jackson@LatitudeGeo.com)
7 // (C) 2003 Jackson Harper, All rights reserved
12 using System.Collections;
14 namespace Mono.ILASM {
16 public class ClassTable {
18 private class ClassTableItem {
20 private static readonly int DefinedFlag = 2;
24 public ArrayList LocationList;
26 public MethodTable method_table;
27 public FieldTable field_table;
29 public ClassTableItem (ClassDef klass, Location location)
33 LocationList = new ArrayList ();
34 LocationList.Add (location);
35 method_table = new MethodTable (klass);
36 field_table = new FieldTable (klass);
40 get { return ((flags & DefinedFlag) != 0); }
49 public MethodTable MethodTable {
50 get { return method_table; }
53 public FieldTable FieldTable {
54 get { return field_table; }
59 protected readonly TypeAttr DefaultAttr;
60 protected Hashtable table;
61 protected PEFile pefile;
63 public ClassTable (PEFile pefile)
65 DefaultAttr = TypeAttr.Public;
67 table = new Hashtable ();
70 public Class Get (string full_name)
72 ClassTableItem item = table[full_name] as ClassTableItem;
80 public Class GetReference (string full_name, Location location)
82 ClassTableItem item = table[full_name] as ClassTableItem;
85 item.LocationList.Add (location);
89 string name_space, name;
90 GetNameAndNamespace (full_name, out name_space, out name);
91 ClassDef klass = pefile.AddClass (DefaultAttr, name_space, name);
92 AddReference (full_name, klass, location);
97 public MethodTable GetMethodTable (string full_name, Location location)
99 ClassTableItem item = table[full_name] as ClassTableItem;
102 GetReference (full_name, location);
103 return GetMethodTable (full_name, location);
106 return item.MethodTable;
109 public FieldTable GetFieldTable (string full_name, Location location)
111 ClassTableItem item = table[full_name] as ClassTableItem;
114 GetReference (full_name, location);
115 return GetFieldTable (full_name, location);
118 return item.FieldTable;
121 public ClassDef AddDefinition (string name_space, string name,
122 TypeAttr attr, Location location)
124 string full_name = String.Format ("{0}.{1}", name_space, name);
126 CheckExists (full_name);
128 ClassDef klass = pefile.AddClass (attr, name_space, name);
129 AddDefined (full_name, klass, location);
134 public ClassDef AddDefinition (string name_space, string name,
135 TypeAttr attr, Class parent, Location location)
137 string full_name = String.Format ("{0}.{1}", name_space, name);
139 CheckExists (full_name);
141 ClassDef klass = pefile.AddClass (attr, name_space, name, parent);
142 AddDefined (full_name, klass, location);
148 /// When there is no code left to compile, check to make sure referenced types where defined
149 /// TODO: Proper error reporting
151 public void CheckForUndefined ()
153 foreach (DictionaryEntry dic_entry in table) {
154 ClassTableItem table_item = (ClassTableItem) dic_entry.Value;
155 if (table_item.Defined)
157 throw new Exception (String.Format ("Type: {0} is not defined.", dic_entry.Key));
162 /// If a type is allready defined throw an Error
164 protected void CheckExists (string full_name)
166 ClassTableItem item = table[full_name] as ClassTableItem;
168 if ((item != null) && (item.Defined)) {
169 throw new Exception (String.Format ("Class: {0} defined in multiple locations.",
174 protected void AddDefined (string full_name, ClassDef klass, Location location)
176 if (table.Contains (full_name))
179 ClassTableItem item = new ClassTableItem (klass, location);
182 table[full_name] = item;
185 protected void AddReference (string full_name, ClassDef klass, Location location)
187 if (table.Contains (full_name))
190 ClassTableItem item = new ClassTableItem (klass, location);
192 table[full_name] = item;
195 protected void GetNameAndNamespace (string full_name,
196 out string name_space, out string name) {
198 int last_dot = full_name.LastIndexOf ('.');
201 name_space = String.Empty;
206 name_space = full_name.Substring (0, last_dot);
207 name = full_name.Substring (last_dot + 1);