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;
27 public ClassTableItem (Class klass, Location location)
31 LocationList = new ArrayList ();
32 LocationList.Add (location);
36 get { return ((flags & DefinedFlag) != 0); }
46 protected readonly TypeAttr DefaultAttr;
47 protected Hashtable table;
48 protected PEFile pefile;
50 public ClassTable (PEFile pefile)
52 DefaultAttr = TypeAttr.Public;
54 table = new Hashtable ();
57 public Class Get (string full_name)
59 ClassTableItem item = table[full_name] as ClassTableItem;
67 public Class GetReference (string full_name, Location location)
69 ClassTableItem item = table[full_name] as ClassTableItem;
72 item.LocationList.Add (location);
76 string name_space, name;
77 GetNameAndNamespace (full_name, out name_space, out name);
78 Class klass = pefile.AddClass (DefaultAttr, name_space, name);
79 AddReference (full_name, klass, location);
85 public ClassDef AddDefinition (string name_space, string name,
86 TypeAttr attr, Location location)
88 string full_name = String.Format ("{0}.{1}", name_space, name);
90 CheckExists (full_name);
92 ClassDef klass = pefile.AddClass (attr, name_space, name);
93 AddDefined (full_name, klass, location);
98 public ClassDef AddDefinition (string name_space, string name,
99 TypeAttr attr, Class parent, Location location)
101 string full_name = String.Format ("{0}.{1}", name_space, name);
103 CheckExists (full_name);
105 ClassDef klass = pefile.AddClass (attr, name_space, name, parent);
106 AddDefined (full_name, klass, location);
112 /// When there is no code left to compile, check to make sure referenced types where defined
113 /// TODO: Proper error reporting
115 public void CheckForUndefined ()
117 foreach (DictionaryEntry dic_entry in table) {
118 ClassTableItem table_item = (ClassTableItem) dic_entry.Value;
119 if (table_item.Defined)
121 throw new Exception (String.Format ("Type: {0} is not defined.", dic_entry.Key));
126 /// If a type is allready defined throw an Error
128 protected void CheckExists (string full_name)
130 ClassTableItem item = table[full_name] as ClassTableItem;
132 if ((item != null) && (item.Defined)) {
133 throw new Exception (String.Format ("Class: {0} defined in multiple locations.",
138 protected void AddDefined (string full_name, Class klass, Location location)
140 if (table.Contains (full_name))
143 ClassTableItem item = new ClassTableItem (klass, location);
146 table[full_name] = item;
149 protected void AddReference (string full_name, Class klass, Location location)
151 if (table.Contains (full_name))
154 ClassTableItem item = new ClassTableItem (klass, location);
156 table[full_name] = item;
159 protected void GetNameAndNamespace (string full_name,
160 out string name_space, out string name) {
162 int last_dot = full_name.LastIndexOf ('.');
165 name_space = String.Empty;
170 name_space = full_name.Substring (0, last_dot);
171 name = full_name.Substring (last_dot + 1);