Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ClassTable.cs « codegen « ilasm « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0c2b5adcacaeb63d6fc29dc9fa097ccfaa4401a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// Mono.ILASM.ClassTable.cs
//
// Author(s):
//  Jackson Harper (Jackson@LatitudeGeo.com)
//
// (C) 2003 Jackson Harper, All rights reserved
//

using PEAPI;
using System;
using System.Collections;

namespace Mono.ILASM {

        public class ClassTable {

                private class ClassTableItem {

                        private static readonly int DefinedFlag = 2;

                        private int flags;

                        public ArrayList LocationList;
                        public ClassDef Class;
                        public MethodTable method_table;
                        public FieldTable field_table;

                        public ClassTableItem (ClassDef klass, Location location)
                        {
                                flags = 0;
                                Class = klass;
                                LocationList = new ArrayList ();
                                LocationList.Add (location);
                                method_table = new MethodTable (klass);
                                field_table = new FieldTable (klass);
                        }

                        public bool Defined {
                                get { return ((flags & DefinedFlag) != 0); }
                                set {
                                        if (value)
                                                flags |= DefinedFlag;
                                        else
                                                flags ^= DefinedFlag;
                                }
                        }

                        public bool CheckDefined ()
                        {
                                if (!Defined)
                                        return false;

                                if (!FieldTable.CheckDefined ())
                                        return false;

                                if (!MethodTable.CheckDefined ())
                                        return false;

                                return true;
                        }

                        public MethodTable MethodTable {
                                get { return method_table; }
                        }

                        public FieldTable FieldTable {
                                get { return field_table; }
                        }

                }

                protected readonly TypeAttr DefaultAttr;
                protected Hashtable table;
                protected PEFile pefile;

                public ClassTable (PEFile pefile)
                {
                        DefaultAttr = TypeAttr.Public;
                        this.pefile = pefile;
                        table = new Hashtable ();
                }

                public Class Get (string full_name)
                {
                        ClassTableItem item = table[full_name] as ClassTableItem;

                        if (item == null)
                                return null;

                        return item.Class;
                }

                public Class GetReference (string full_name, Location location)
                {
                        ClassTableItem item = table[full_name] as ClassTableItem;

                        if (item != null) {
                                item.LocationList.Add (location);
                                return item.Class;
                        }

                        string name_space, name;
                        GetNameAndNamespace (full_name, out name_space, out name);
                        ClassDef klass = pefile.AddClass (DefaultAttr, name_space, name);
                        AddReference (full_name, klass, location);

                        return klass;
                }

                public MethodTable GetMethodTable (string full_name, Location location)
                {
                        ClassTableItem item = table[full_name] as ClassTableItem;

                        if (item == null) {
                                GetReference (full_name, location);
                                return GetMethodTable (full_name, location);
                        }

                        return item.MethodTable;
                }

                public FieldTable GetFieldTable (string full_name, Location location)
                {
                        ClassTableItem item = table[full_name] as ClassTableItem;

                        if (item == null) {
                                GetReference (full_name, location);
                                return GetFieldTable (full_name, location);
                        }

                        return item.FieldTable;
                }

                public ClassDef AddDefinition (string name_space, string name,
                        TypeAttr attr, Location location)
                {
                        string full_name;

                        if (name_space != null)
                                full_name = String.Format ("{0}.{1}", name_space, name);
                        else
                                full_name = name;

                        ClassTableItem item = (ClassTableItem) table[full_name];

                        if (item == null) {
                                ClassDef klass = pefile.AddClass (attr, name_space, name);
                                AddDefined (full_name, klass, location);
                                return klass;
                        }

                        item.Class.AddAttribute (attr);
                        item.Defined = true;

                        return item.Class;
                }

                public ClassDef AddDefinition (string name_space, string name,
                        TypeAttr attr, Class parent, Location location)
                {
                        string full_name;

                        if (name_space != null)
                                full_name = String.Format ("{0}.{1}", name_space, name);
                        else
                                full_name = name;

                        ClassTableItem item = (ClassTableItem) table[full_name];

                        if (item == null) {
                                ClassDef klass = pefile.AddClass (attr, name_space, name, parent);
                                AddDefined (full_name, klass, location);
                                return klass;
                        }

                        /// TODO: Need to set parent, will need to modify PEAPI for this.
                        item.Class.AddAttribute (attr);
                        item.Defined = true;

                        return item.Class;
                }

                /// <summary>
                ///  When there is no code left to compile, check to make sure referenced types where defined
                ///  TODO: Proper error reporting
                /// </summary>
                public void CheckForUndefined ()
                {
                        foreach (DictionaryEntry dic_entry in table) {
                                ClassTableItem table_item = (ClassTableItem) dic_entry.Value;
                                if (table_item.CheckDefined ())
                                        continue;
                                Report.Error (String.Format ("Type: {0} is not defined.", dic_entry.Key));
                        }
                }

                /// <summary>
                ///  If a type is allready defined throw an Error
                /// </summary>
                protected void CheckExists (string full_name)
                {
                        ClassTableItem item = table[full_name] as ClassTableItem;

                        if ((item != null) && (item.Defined)) {
                                Report.Error (String.Format ("Class: {0} defined in multiple locations.",
                                        full_name));
                        }
                }

                protected void AddDefined (string full_name, ClassDef klass, Location location)
                {
                        if (table.Contains (full_name))
                                return;

                        ClassTableItem item = new ClassTableItem (klass, location);
                        item.Defined = true;

                        table[full_name] = item;
                }

                protected void AddReference (string full_name, ClassDef klass, Location location)
                {
                        if (table.Contains (full_name))
                                return;

                        ClassTableItem item = new ClassTableItem (klass, location);

                        table[full_name] = item;
                }

                public static void GetNameAndNamespace (string full_name,
                        out string name_space, out string name) {

                        int last_dot = full_name.LastIndexOf ('.');

                        if (last_dot < 0) {
                                name_space = String.Empty;
                                name = full_name;
                                return;
                        }

                        name_space = full_name.Substring (0, last_dot);
                        name = full_name.Substring (last_dot + 1);
                }

        }

}