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);
79 Class klass = pefile.AddClass (DefaultAttr, name_space, name);
80 AddReference (full_name, klass, location);
85 public Class AddDefinition (string name_space, string name,
86 TypeAttr attr, Location location)
88 string full_name = String.Format ("{0}.{1}", name_space, name);
89 ClassTableItem item = table[full_name] as ClassTableItem;
91 if ((item != null) && (item.Defined)) {
92 throw new Exception (String.Format ("Class: {0} defined in multiple locations.",
96 Class klass = pefile.AddClass (attr, name_space, name);
97 AddDefined (full_name, klass, location);
102 protected void AddDefined (string full_name, Class klass, Location location)
104 if (table.Contains (full_name))
107 ClassTableItem item = new ClassTableItem (klass, location);
110 table[full_name] = item;
113 protected void AddReference (string full_name, Class klass, Location location)
115 if (table.Contains (full_name))
118 ClassTableItem item = new ClassTableItem (klass, location);
120 table[full_name] = item;
123 protected void GetNameAndNamespace (string full_name,
124 out string name_space, out string name) {
126 int last_dot = full_name.LastIndexOf ('.');
129 name_space = String.Empty;
134 name_space = full_name.Substring (0, last_dot);
135 name = full_name.Substring (last_dot + 1);