a0d7f76f41127fffdd4991804247b2e0d7689cc1
[mono.git] / mcs / ilasm / codegen / TypeDef.cs
1 //
2 // Mono.ILASM.TypeDef
3 //
4 // Author(s):
5 //  Jackson Harper (Jackson@LatitudeGeo.com)
6 //
7 // (C) 2003 Jackson Harper, All rights reserved
8 //
9
10
11 using System;
12 using System.Collections;
13
14 namespace Mono.ILASM {
15
16         public class TypeDef {
17
18                 protected class GenericInfo {
19                         public string Id;
20                         public ArrayList ConstraintList;
21                 }
22
23                 private PEAPI.TypeAttr attr;
24                 private string name_space;
25                 private string name;
26                 private bool is_defined;
27                 private bool is_intransit;
28                 private IClassRef parent;
29                 private ArrayList impl_list;
30                 private PEAPI.ClassDef classdef;
31                 private Hashtable field_table;
32                 private Hashtable method_table;
33                 private ArrayList data_list;
34                 private ArrayList customattr_list;
35                 private ArrayList event_list;
36                 private ArrayList property_list;
37                 private ArrayList typar_list;
38                 private ArrayList override_list;
39                 private ArrayList override_long_list;
40                 private TypeDef outer;
41
42                 private EventDef current_event;
43                 private PropertyDef current_property;
44
45                 private int size;
46                 private int pack;
47
48                 private bool is_value_class;
49                 private bool is_enum_class;
50
51                 public TypeDef (PEAPI.TypeAttr attr, string name_space, string name,
52                                 IClassRef parent, ArrayList impl_list, Location location)
53                 {
54                         this.attr = attr;
55                         this.name_space = name_space;
56                         this.name = name;
57                         this.parent = parent;
58                         this.impl_list = impl_list;
59                         field_table = new Hashtable ();
60                         method_table = new Hashtable ();
61                         data_list = new ArrayList ();
62
63                         size = -1;
64                         pack = -1;
65
66                         is_defined = false;
67                         is_intransit = false;
68
69                         is_value_class = false;
70                         is_value_class = false;
71                 }
72
73                 public string Name {
74                         get { return name; }
75                 }
76
77                 public string FullName {
78                         get { return MakeFullName (); }
79                 }
80
81                 public TypeDef OuterType {
82                         get { return outer; }
83                         set { outer = value; }
84                 }
85
86                 public PEAPI.ClassDef PeapiType {
87                         get { return classdef; }
88                 }
89
90                 public PEAPI.ClassDef ClassDef {
91                         get { return classdef; }
92                 }
93
94                 public bool IsGenericType {
95                         get { return (typar_list == null); }
96                 }
97
98                 public bool IsDefined {
99                         get { return is_defined; }
100                 }
101
102                 public EventDef CurrentEvent {
103                         get { return current_event; }
104                 }
105
106                 public PropertyDef CurrentProperty {
107                         get { return current_property; }
108                 }
109
110
111                 public void AddOverride (MethodDef body, ITypeRef parent, string name)
112                 {
113                         if (override_list == null)
114                                 override_list = new ArrayList ();
115                         override_list.Add (new DictionaryEntry (body,
116                                            new DictionaryEntry (parent, name)));
117                 }
118
119                 public void AddOverride (string sig, IMethodRef decl)
120                 {
121                         if (override_long_list == null)
122                                 override_long_list = new ArrayList ();
123                         override_long_list.Add (new DictionaryEntry (sig,
124                                                                 decl));
125                 }
126
127                 public void MakeValueClass ()
128                 {
129                         is_value_class = true;
130                 }
131
132                 public void MakeEnumClass ()
133                 {
134                         is_enum_class = true;
135                 }
136
137                 public void SetSize (int size)
138                 {
139                         this.size = size;
140                 }
141
142                 public void SetPack (int pack)
143                 {
144                         this.pack = pack;
145                 }
146
147                 public void AddFieldDef (FieldDef fielddef)
148                 {
149                         field_table.Add (fielddef.Name, fielddef);
150                 }
151
152                 public void AddDataDef (DataDef datadef)
153                 {
154                         data_list.Add (datadef);
155                 }
156
157                 public DataDef GetDataDef (string name)
158                 {
159                         foreach (DataDef def in data_list) {
160                                 if (def.Name == name)
161                                         return def;
162                         }
163                         return null;
164                 }
165
166                 public void AddMethodDef (MethodDef methoddef)
167                 {
168                         method_table.Add (methoddef.Signature, methoddef);
169                 }
170
171                 public void BeginEventDef (EventDef event_def)
172                 {
173                         if (current_event != null)
174                                 throw new Exception ("An event definition was not closed.");
175
176                         current_event = event_def;
177                 }
178
179                 public void EndEventDef ()
180                 {
181                         if (event_list == null)
182                                 event_list = new ArrayList ();
183
184                         event_list.Add (current_event);
185                         current_event = null;
186                 }
187
188                 public void BeginPropertyDef (PropertyDef property_def)
189                 {
190                         if (current_property != null)
191                                 throw new Exception ("A property definition was not closed.");
192
193                         current_property = property_def;
194                 }
195
196                 public void EndPropertyDef ()
197                 {
198                         if (property_list == null)
199                                 property_list = new ArrayList ();
200
201                         property_list.Add (current_property);
202                         current_property = null;
203                 }
204
205                 public void AddCustomAttribute (CustomAttr customattr)
206                 {
207                         if (customattr_list == null)
208                                 customattr_list = new ArrayList ();
209
210                         customattr_list.Add (customattr);
211                 }
212
213                 public void AddGenericParam (string id)
214                 {
215                         if (typar_list == null)
216                                 typar_list = new ArrayList ();
217
218                         GenericInfo gi = new GenericInfo ();
219                         gi.Id = id;
220
221                         typar_list.Add (gi);
222                 }
223
224                 public void AddGenericConstraint (int index, ITypeRef constraint)
225                 {
226                         GenericInfo gi = (GenericInfo) typar_list[index];
227
228                         if (gi.ConstraintList == null)
229                                 gi.ConstraintList = new ArrayList ();
230                         gi.ConstraintList.Add (constraint);
231                 }
232
233                 public void Define (CodeGen code_gen)
234                 {
235                         if (is_defined)
236                                 return;
237
238                         if (is_intransit) {
239                                 // Circular definition
240                                 throw new Exception ("Circular definition of class: " + FullName);
241                         }
242
243                         if (parent != null) {
244                                 is_intransit = true;
245                                 parent.Resolve (code_gen);
246                                 is_intransit = false;
247                                 if (parent.PeapiClass == null) {
248                                         throw new Exception ("this type can not be a base type: "
249                                                         + parent);
250                                 }
251                                 if (outer != null) {
252                                         if (!outer.IsDefined)
253                                                 outer.Define (code_gen);
254                                         classdef = outer.PeapiType.AddNestedClass (attr,
255                                                         name_space, name, parent.PeapiClass);
256                                 } else {
257                                         if (is_value_class) {
258                                                 // Should probably confirm that the parent is System.ValueType
259                                                 classdef = code_gen.PEFile.AddValueClass (attr,
260                                                         name_space, name);
261                                         } else {
262                                                 classdef = code_gen.PEFile.AddClass (attr,
263                                                         name_space, name, parent.PeapiClass);
264                                         }
265                                 }
266                         } else {
267                                 if (outer != null) {
268                                         if (!outer.IsDefined)
269                                                 outer.Define (code_gen);
270                                         classdef = outer.PeapiType.AddNestedClass (attr,
271                                                 name_space, name);
272                                 } else {
273                                         if (is_value_class) {
274                                                 classdef = code_gen.PEFile.AddValueClass (attr,
275                                                         name_space, name);
276                                         } else {
277                                                 classdef = code_gen.PEFile.AddClass (attr,
278                                                         name_space, name);
279                                         }
280                                 }
281                                 if (FullName == "System.Object")
282                                         classdef.SpecialNoSuper ();
283                         }
284
285                         if (size != -1)
286                                 classdef.AddLayoutInfo (pack, size);
287
288                         if (typar_list != null) {
289                                 short index = 0;
290                                 foreach (GenericInfo gi in typar_list) {
291                                         PEAPI.GenericParameter gp = classdef.AddGenericParameter (index++, gi.Id);
292                                         if (gi.ConstraintList != null) {
293                                                 foreach (ITypeRef cnst in gi.ConstraintList) {
294                                                         cnst.Resolve (code_gen);
295                                                         gp.AddConstraint (cnst.PeapiType);
296                                                 }
297                                         }
298                                 }
299                         }
300
301                         is_intransit = false;
302                         is_defined = true;
303
304                         code_gen.AddToDefineContentsList (this);
305                 }
306
307                 public void DefineContents (CodeGen code_gen)
308                 {
309                         foreach (FieldDef fielddef in field_table.Values) {
310                                 fielddef.Define (code_gen, classdef);
311                         }
312
313                         foreach (MethodDef methoddef in method_table.Values) {
314                                 methoddef.Define (code_gen, classdef);
315                         }
316
317                         if (event_list != null) {
318                                 foreach (EventDef eventdef in event_list) {
319                                         eventdef.Define (code_gen, classdef);
320                                 }
321                         }
322
323                         if (property_list != null) {
324                                 foreach (PropertyDef propdef in property_list) {
325                                         propdef.Define (code_gen, classdef);
326                                 }
327
328                         }
329
330                         if (customattr_list != null) {
331                                 foreach (CustomAttr customattr in customattr_list)
332                                         customattr.AddTo (code_gen, classdef);
333                         }
334
335                         if (override_list != null) {
336                                 foreach (DictionaryEntry entry in override_list) {
337                                         MethodDef body = (MethodDef) entry.Key;
338                                         DictionaryEntry decl = (DictionaryEntry) entry.Value;
339                                         ITypeRef parent_type = (ITypeRef) decl.Key;
340                                         parent_type.Resolve (code_gen);
341                                         string over_name = (string) decl.Value;
342                                         IMethodRef over_meth = parent_type.GetMethodRef (body.RetType,
343                                                         body.CallConv, over_name, body.ParamTypeList ());
344                                         over_meth.Resolve (code_gen);
345                                         classdef.AddMethodOverride (over_meth.PeapiMethod,
346                                                         body.PeapiMethodDef);
347                                 }
348                         }
349
350                         if (override_long_list != null) {
351                                 foreach (DictionaryEntry entry in override_long_list) {
352                                         string sig = (string) entry.Key;
353                                         IMethodRef decl = (IMethodRef) entry.Value;
354                                         MethodDef body = (MethodDef) method_table[sig];
355                                         decl.Resolve (code_gen);
356                                         classdef.AddMethodOverride (decl.PeapiMethod,
357                                                         body.PeapiMethodDef);
358                                 }
359                         }
360                 }
361
362                 public PEAPI.MethodDef ResolveMethod (string signature, CodeGen code_gen)
363                 {
364                         MethodDef methoddef = (MethodDef) method_table[signature];
365
366                         return methoddef.Resolve (code_gen, classdef);
367                 }
368
369                 public PEAPI.Method ResolveVarargMethod (string signature,
370                                 CodeGen code_gen, PEAPI.Type[] opt)
371                 {
372                         MethodDef methoddef = (MethodDef) method_table[signature];
373                         methoddef.Resolve (code_gen, classdef);
374
375                         return methoddef.GetVarargSig (opt);
376                 }
377
378                 public PEAPI.Field ResolveField (string name, CodeGen code_gen)
379                 {
380                         FieldDef fielddef = (FieldDef) field_table[name];
381
382                         return fielddef.Resolve (code_gen, classdef);
383                 }
384
385                 private string MakeFullName ()
386                 {
387                         if (name_space == null || name_space == String.Empty)
388                                 return name;
389
390                         return name_space + "." + name;
391                 }
392         }
393
394 }
395