* ClassTable.cs: Add file, this is a 'table' for storing classes
[mono.git] / mcs / ilasm / codegen / ClassTable.cs
1 //
2 // Mono.ILASM.ClassTable.cs
3 //
4 // Author(s):
5 //  Jackson Harper (Jackson@LatitudeGeo.com)
6 //
7 // (C) 2003 Jackson Harper, All rights reserved
8 //
9
10 using PEAPI;
11 using System;
12 using System.Collections;
13
14 namespace Mono.ILASM {
15
16         public class ClassTable {
17
18                 private class ClassTableItem {
19                 
20                         private static readonly int DefinedFlag = 2;
21         
22                         private int flags;
23
24                         public ArrayList LocationList;
25                         public Class Class;
26
27                         public ClassTableItem (Class klass, Location location)
28                         {
29                                 flags = 0;
30                                 Class = klass;
31                                 LocationList = new ArrayList ();
32                                 LocationList.Add (location);
33                         }
34                 
35                         public bool Defined {
36                                 get { return ((flags & DefinedFlag) != 0); }
37                                 set {
38                                         if (value)
39                                                 flags |= DefinedFlag;
40                                         else
41                                                 flags ^= DefinedFlag;
42                                 }
43                         }
44                 }
45
46                 protected readonly TypeAttr DefaultAttr;
47                 protected Hashtable table;
48                 protected PEFile pefile;
49                 
50                 public ClassTable (PEFile pefile)
51                 {
52                         DefaultAttr = TypeAttr.Public;
53                         this.pefile = pefile;
54                         table = new Hashtable ();
55                 }
56
57                 public Class Get (string full_name)
58                 {
59                         ClassTableItem item = table[full_name] as ClassTableItem;
60                         
61                         if (item == null)
62                                 return null;
63
64                         return item.Class;
65                 }
66                 
67                 public Class GetReference (string full_name, Location location)
68                 {
69                         ClassTableItem item = table[full_name] as ClassTableItem;
70                         
71                         if (item != null) {
72                                 item.LocationList.Add (location);
73                                 return item.Class;
74                         }
75                         
76                         string name_space, name;                        
77                         GetNameAndNamespace (full_name, out name_space, out name);
78
79                         Class klass = pefile.AddClass (DefaultAttr, name_space, name);
80                         AddReference (full_name, klass, location);
81                         
82                         return klass;
83                 }
84
85                 public void AddDefined (string full_name, Class klass, Location location)
86                 {
87                         ClassTableItem item = table[full_name] as ClassTableItem;
88                         
89                         if (item == null) {
90                                 item = new ClassTableItem (klass, location);
91                         } else if (item.Defined) {
92                                 throw new Exception (String.Format 
93                                         ("Class: {0} defined in multiple locations.", full_name));
94                         }
95
96                         item.Defined = true;
97                 }
98
99                 protected void AddReference (string full_name, Class klass, Location location)
100                 {
101                         if (table.Contains (full_name))
102                                 return;
103
104                         ClassTableItem item = new ClassTableItem (klass, location);
105                         
106                         table[full_name] = item;
107                 }
108
109                 protected void GetNameAndNamespace (string full_name,
110                         out string name_space, out string name) {
111                         
112                         int last_dot = full_name.LastIndexOf ('.');
113         
114                         if (last_dot < 0) {
115                                 name_space = String.Empty;
116                                 name = full_name;
117                                 return;
118                         }
119                                 
120                         name_space = full_name.Substring (0, last_dot);
121                         name = full_name.Substring (last_dot + 1);
122                 }
123
124         }
125
126 }
127