* ClassTable.cs: Add method for adding class definitions to the
[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 Class AddDefinition (string name_space, string name, 
86                         TypeAttr attr, Location location) 
87                 {
88                         string full_name = String.Format ("{0}.{1}", name_space, name);
89                         ClassTableItem item = table[full_name] as ClassTableItem;
90                         
91                         if ((item != null) && (item.Defined)) {
92                                 throw new Exception (String.Format ("Class: {0} defined in multiple locations.", 
93                                         full_name));
94                         }
95                         
96                         Class klass = pefile.AddClass (attr, name_space, name);
97                         AddDefined (full_name, klass, location);
98
99                         return klass;
100                 }
101
102                 protected void AddDefined (string full_name, Class klass, Location location)
103                 {
104                         if (table.Contains (full_name))
105                                 return; 
106
107                         ClassTableItem item = new ClassTableItem (klass, location);
108                         item.Defined = true;
109
110                         table[full_name] = item;
111                 }
112
113                 protected void AddReference (string full_name, Class klass, Location location)
114                 {
115                         if (table.Contains (full_name))
116                                 return;
117
118                         ClassTableItem item = new ClassTableItem (klass, location);
119                         
120                         table[full_name] = item;
121                 }
122
123                 protected void GetNameAndNamespace (string full_name,
124                         out string name_space, out string name) {
125                         
126                         int last_dot = full_name.LastIndexOf ('.');
127         
128                         if (last_dot < 0) {
129                                 name_space = String.Empty;
130                                 name = full_name;
131                                 return;
132                         }
133                                 
134                         name_space = full_name.Substring (0, last_dot);
135                         name = full_name.Substring (last_dot + 1);
136                 }
137
138         }
139
140 }
141