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 TypeDef {
18 private PEAPI.TypeAttr attr;
19 private string name_space;
21 private bool is_defined;
22 private bool is_intransit;
23 private IClassRef parent;
24 private ArrayList impl_list;
25 private PEAPI.ClassDef classdef;
26 private Hashtable field_table;
27 private Hashtable method_table;
28 private ArrayList data_list;
29 private ArrayList customattr_list;
30 private ArrayList event_list;
31 private ArrayList property_list;
32 private ArrayList typar_list;
33 private Hashtable constraint_table;
34 private TypeDef outer;
36 private EventDef current_event;
37 private PropertyDef current_property;
42 private bool is_value_class;
43 private bool is_enum_class;
45 public TypeDef (PEAPI.TypeAttr attr, string name_space, string name,
46 IClassRef parent, ArrayList impl_list, Location location)
49 this.name_space = name_space;
52 this.impl_list = impl_list;
53 field_table = new Hashtable ();
54 method_table = new Hashtable ();
55 data_list = new ArrayList ();
63 is_value_class = false;
64 is_value_class = false;
71 public string FullName {
72 get { return MakeFullName (); }
75 public TypeDef OuterType {
77 set { outer = value; }
80 public PEAPI.ClassDef PeapiType {
81 get { return classdef; }
84 public PEAPI.ClassDef ClassDef {
85 get { return classdef; }
88 public bool IsGenericType {
89 get { return (typar_list == null); }
92 public bool IsDefined {
93 get { return is_defined; }
96 public EventDef CurrentEvent {
97 get { return current_event; }
100 public PropertyDef CurrentProperty {
101 get { return current_property; }
104 public void MakeValueClass ()
106 is_value_class = true;
109 public void MakeEnumClass ()
111 is_enum_class = true;
114 public void SetSize (int size)
119 public void SetPack (int pack)
124 public void AddFieldDef (FieldDef fielddef)
126 field_table.Add (fielddef.Name, fielddef);
129 public void AddDataDef (DataDef datadef)
131 data_list.Add (datadef);
134 public void AddMethodDef (MethodDef methoddef)
136 method_table.Add (methoddef.Signature, methoddef);
139 public void BeginEventDef (EventDef event_def)
141 if (current_event != null)
142 throw new Exception ("An event definition was not closed.");
144 current_event = event_def;
147 public void EndEventDef ()
149 if (event_list == null)
150 event_list = new ArrayList ();
152 event_list.Add (current_event);
153 current_event = null;
156 public void BeginPropertyDef (PropertyDef property_def)
158 if (current_property != null)
159 throw new Exception ("A property definition was not closed.");
161 current_property = property_def;
164 public void EndPropertyDef ()
166 if (property_list == null)
167 property_list = new ArrayList ();
169 property_list.Add (current_property);
170 current_property = null;
173 public void AddCustomAttribute (CustomAttr customattr)
175 if (customattr_list == null)
176 customattr_list = new ArrayList ();
178 customattr_list.Add (customattr);
181 public void AddGenericParam (string id)
183 if (typar_list == null)
184 typar_list = new ArrayList ();
189 public void AddGenericConstraint (int index, ITypeRef constraint)
191 if (constraint_table == null)
192 constraint_table = new Hashtable ();
194 constraint_table.Add (index, constraint);
197 public void Define (CodeGen code_gen)
203 // Circular definition
204 throw new Exception ("Circular definition of class: " + FullName);
207 if (parent != null) {
209 parent.Resolve (code_gen);
210 is_intransit = false;
211 if (parent.PeapiClass == null) {
212 throw new Exception ("this type can not be a base type: "
216 if (!outer.IsDefined)
217 outer.Define (code_gen);
218 classdef = outer.PeapiType.AddNestedClass (attr,
219 name_space, name, parent.PeapiClass);
221 if (is_value_class) {
222 // Should probably confirm that the parent is System.ValueType
223 classdef = code_gen.PEFile.AddValueClass (attr,
226 classdef = code_gen.PEFile.AddClass (attr,
227 name_space, name, parent.PeapiClass);
232 if (!outer.IsDefined)
233 outer.Define (code_gen);
234 classdef = outer.PeapiType.AddNestedClass (attr,
237 if (is_value_class) {
238 classdef = code_gen.PEFile.AddValueClass (attr,
241 classdef = code_gen.PEFile.AddClass (attr,
248 classdef.AddLayoutInfo (pack, size);
250 if (typar_list != null) {
252 foreach (string id in typar_list) {
253 if (constraint_table != null && constraint_table.Contains ((int) index)) {
254 ITypeRef constraint = (ITypeRef) constraint_table[(int) index];
255 constraint.Resolve (code_gen);
256 classdef.AddGenericParameter (index++, id, constraint.PeapiType);
258 classdef.AddGenericParameter (index++, id);
263 is_intransit = false;
266 code_gen.AddToDefineContentsList (this);
269 public void DefineContents (CodeGen code_gen)
271 foreach (FieldDef fielddef in field_table.Values) {
272 fielddef.Define (code_gen, classdef);
275 foreach (MethodDef methoddef in method_table.Values) {
276 methoddef.Define (code_gen, classdef);
279 if (event_list != null) {
280 foreach (EventDef eventdef in event_list) {
281 eventdef.Define (code_gen, classdef);
285 if (property_list != null) {
286 foreach (PropertyDef propdef in property_list) {
287 propdef.Define (code_gen, classdef);
292 if (customattr_list != null) {
293 foreach (CustomAttr customattr in customattr_list)
294 customattr.AddTo (code_gen, classdef);
298 public PEAPI.MethodDef ResolveMethod (string signature, CodeGen code_gen)
300 MethodDef methoddef = (MethodDef) method_table[signature];
302 return methoddef.Resolve (code_gen, classdef);
305 public PEAPI.Method ResolveVarargMethod (string signature,
306 CodeGen code_gen, PEAPI.Type[] opt)
308 MethodDef methoddef = (MethodDef) method_table[signature];
309 methoddef.Resolve (code_gen, classdef);
311 return methoddef.GetVarargSig (opt);
314 public PEAPI.Field ResolveField (string name, CodeGen code_gen)
316 FieldDef fielddef = (FieldDef) field_table[name];
318 return fielddef.Resolve (code_gen, classdef);
321 private string MakeFullName ()
323 if (name_space == null || name_space == String.Empty)
326 return name_space + "." + name;