5 // Jackson Harper (Jackson@LatitudeGeo.com)
7 // (C) 2003 Jackson Harper, All rights reserved
12 using System.Collections;
13 using System.Security;
15 namespace Mono.ILASM {
17 public class TypeDef : ICustomAttrTarget, IDeclSecurityTarget, IComparable {
19 private PEAPI.TypeAttr attr;
20 private string name_space;
22 private bool is_defined;
23 private bool is_intransit;
24 private BaseClassRef parent;
25 private ArrayList impl_list;
26 private PEAPI.ClassDef classdef;
27 private Hashtable field_table;
28 private ArrayList field_list;
29 private Hashtable method_table;
30 private ArrayList method_list;
31 private ArrayList customattr_list;
32 private DeclSecurity decl_sec;
33 private ArrayList event_list;
34 private ArrayList property_list;
35 private GenericParameters gen_params;
36 private ArrayList override_list;
37 private ArrayList override_long_list;
38 private TypeDef outer;
40 private EventDef current_event;
41 private PropertyDef current_property;
46 private bool is_value_class;
47 private bool is_enum_class;
49 private Location location;
51 public TypeDef (PEAPI.TypeAttr attr, string name_space, string name,
52 BaseClassRef parent, ArrayList impl_list, Location location, GenericParameters gen_params, TypeDef outer)
56 this.impl_list = impl_list;
57 this.gen_params = gen_params;
59 this.location = location;
61 field_table = new Hashtable ();
62 field_list = new ArrayList ();
64 method_table = new Hashtable ();
65 method_list = new ArrayList ();
73 is_value_class = false;
74 is_enum_class = false;
78 int lastdot = name.LastIndexOf ('.');
79 /* Namespace . name split should not be done for nested classes */
80 if (lastdot >= 0 && outer == null) {
81 if (name_space == null || name_space == "")
82 this.name_space = name.Substring (0, lastdot);
84 this.name_space = name_space + "." + name.Substring (0, lastdot);
85 this.name = name.Substring (lastdot + 1);
87 this.name_space = name_space;
93 this.attr |= PEAPI.TypeAttr.Abstract;
96 public bool NoAutoInherit { get; set; }
102 public string FullName {
103 get { return MakeFullName (); }
106 public string NestedFullName {
107 get { return (outer == null ? FullName : (outer.NestedFullName + "/" + FullName)); }
110 public TypeDef OuterType {
111 get { return outer; }
114 public PEAPI.ClassDef PeapiType {
115 get { return classdef; }
118 public PEAPI.ClassDef ClassDef {
119 get { return classdef; }
122 public bool IsGenericType {
123 get { return (gen_params == null); }
126 public bool IsDefined {
127 get { return is_defined; }
130 public EventDef CurrentEvent {
131 get { return current_event; }
134 public PropertyDef CurrentProperty {
135 get { return current_property; }
138 public bool IsInterface {
139 get { return (attr & PEAPI.TypeAttr.Interface) != 0; }
142 public bool IsAbstract {
143 get { return (attr & PEAPI.TypeAttr.Abstract) != 0; }
146 public GenericParameters TypeParameters {
147 get { return gen_params; }
150 public DeclSecurity DeclSecurity {
152 if (decl_sec == null)
153 decl_sec = new DeclSecurity ();
158 public void AddOverride (MethodDef body, BaseTypeRef parent, string name)
160 if (override_list == null)
161 override_list = new ArrayList ();
162 override_list.Add (new DictionaryEntry (body,
163 new DictionaryEntry (parent, name)));
166 public void AddOverride (string sig, BaseMethodRef decl)
168 if (override_long_list == null)
169 override_long_list = new ArrayList ();
170 override_long_list.Add (new DictionaryEntry (sig,
174 public void MakeValueClass ()
176 is_value_class = true;
179 public void MakeEnumClass ()
181 is_enum_class = true;
184 public void SetSize (int size)
189 public void SetPack (int pack)
194 public void AddFieldDef (FieldDef fielddef)
196 if (IsInterface && !fielddef.IsStatic) {
197 Report.Warning ("Non-static field in interface, set to such");
198 fielddef.Attributes |= PEAPI.FieldAttr.Static;
201 DictionaryEntry entry = new DictionaryEntry (fielddef.Name, fielddef.Type.FullName);
202 if (field_table [entry] != null)
203 Report.Error ("Duplicate field declaration: " + fielddef.Type.FullName + " " + fielddef.Name);
204 field_table.Add (entry, fielddef);
205 field_list.Add (fielddef);
208 public void AddMethodDef (MethodDef methoddef)
210 if (IsInterface && !methoddef.IsStatic && (!methoddef.IsVirtual || !methoddef.IsAbstract)) {
211 Report.Warning (methoddef.StartLocation, "Non-virtual or non-abstract instance method in interface, set to such");
212 methoddef.Attributes |= PEAPI.MethAttr.Abstract | PEAPI.MethAttr.Virtual;
215 if (method_table [methoddef.Signature] != null)
216 Report.Error (methoddef.StartLocation, "Duplicate method declaration: " + methoddef.Signature);
218 method_table.Add (methoddef.Signature, methoddef);
219 method_list.Add (methoddef);
222 public void BeginEventDef (EventDef event_def)
224 if (current_event != null)
225 Report.Error ("An event definition was not closed.");
227 current_event = event_def;
230 public void EndEventDef ()
232 if (event_list == null)
233 event_list = new ArrayList ();
235 event_list.Add (current_event);
236 current_event = null;
239 public void BeginPropertyDef (PropertyDef property_def)
241 if (current_property != null)
242 Report.Error ("A property definition was not closed.");
244 current_property = property_def;
247 public void EndPropertyDef ()
249 if (property_list == null)
250 property_list = new ArrayList ();
252 property_list.Add (current_property);
253 current_property = null;
256 public void AddCustomAttribute (CustomAttr customattr)
258 if (customattr_list == null)
259 customattr_list = new ArrayList ();
261 customattr_list.Add (customattr);
264 public GenericParameter GetGenericParam (string id)
266 if (gen_params == null)
269 return gen_params.GetGenericParam (id);
272 public GenericParameter GetGenericParam (int index)
274 if (gen_params == null || index < 0 || index >= gen_params.Count)
277 return gen_params [index];
280 public int GetGenericParamNum (string id)
282 if (gen_params == null)
285 return gen_params.GetGenericParamNum (id);
288 /* Resolve any GenParams in constraints, parent & impl_list */
289 private void ResolveGenParams ()
291 if (gen_params == null)
294 gen_params.ResolveConstraints (gen_params, null);
296 BaseGenericTypeRef gtr = parent as BaseGenericTypeRef;
298 gtr.Resolve (gen_params, null);
300 if (impl_list == null)
303 foreach (BaseClassRef impl in impl_list) {
304 gtr = impl as BaseGenericTypeRef;
306 gtr.Resolve (gen_params, null);
310 private bool IsValueType (string ns, string name)
312 return (ns == "System" && name == "ValueType");
315 private bool IsEnumType (string ns, string name)
317 return (ns == "System" && name == "Enum");
320 public void Define (CodeGen code_gen)
326 // Circular definition
327 Report.Error ("Circular definition of class: " + FullName);
331 PEAPI.TypeAttr vis = attr & PEAPI.TypeAttr.VisibilityMask;
333 if (vis == PEAPI.TypeAttr.Private || vis == PEAPI.TypeAttr.Public) {
334 /* Nested class, but attr not set accordingly. */
335 Report.Warning (location, String.Format ("Nested class '{0}' has non-nested visibility, set to such.", NestedFullName));
337 attr |= (vis == PEAPI.TypeAttr.Public ? PEAPI.TypeAttr.NestedPublic : PEAPI.TypeAttr.NestedPrivate);
341 if (parent != null) {
343 parent.Resolve (code_gen);
345 is_intransit = false;
346 if (parent.PeapiClass == null) {
347 Report.Error ("this type can not be a base type: "
351 if (IsValueType (parent.PeapiClass.nameSpace, parent.PeapiClass.name))
352 is_value_class = true;
353 else if (IsEnumType (parent.PeapiClass.nameSpace, parent.PeapiClass.name)) {
354 is_enum_class = true;
355 is_value_class = false;
358 if (!IsValueType (name_space, name) && !IsEnumType (name_space, name) &&
359 is_value_class && (attr & PEAPI.TypeAttr.Sealed) == 0) {
361 attr |= PEAPI.TypeAttr.Sealed;
365 if (!outer.IsDefined)
366 outer.Define (code_gen);
367 classdef = outer.PeapiType.AddNestedClass (attr,
368 name_space, name, parent.PeapiClass);
370 if (is_value_class || is_enum_class) {
371 // Should probably confirm that the parent is System.ValueType
372 classdef = code_gen.PEFile.AddValueClass (attr,
373 name_space, name, is_value_class ? PEAPI.ValueClass.ValueType : PEAPI.ValueClass.Enum);
375 classdef = code_gen.PEFile.AddClass (attr,
376 name_space, name, parent.PeapiClass);
381 if (!outer.IsDefined)
382 outer.Define (code_gen);
383 classdef = outer.PeapiType.AddNestedClass (attr,
386 if (is_value_class || is_enum_class) {
387 classdef = code_gen.PEFile.AddValueClass (attr,
388 name_space, name, is_value_class ? PEAPI.ValueClass.ValueType : PEAPI.ValueClass.Enum);
390 classdef = code_gen.PEFile.AddClass (attr,
394 if (FullName == "System.Object" || NoAutoInherit)
395 classdef.SpecialNoSuper ();
400 if (size != -1 || pack != -1)
401 classdef.AddLayoutInfo ( (pack == -1) ? 1 : pack, (size == -1) ? 0 : size);
403 if (impl_list != null) {
404 foreach (BaseClassRef impl in impl_list) {
405 impl.Resolve (code_gen);
406 classdef.AddImplementedInterface (impl.PeapiClass);
410 if (gen_params != null)
411 gen_params.Resolve (code_gen, classdef);
413 is_intransit = false;
415 code_gen.AddToDefineContentsList (this);
418 public void DefineContents (CodeGen code_gen)
420 ArrayList fielddef_list = new ArrayList ();
421 foreach (FieldDef fielddef in field_list) {
422 if (is_enum_class && fielddef.Name == "value__") {
423 fielddef.Attributes |= PEAPI.FieldAttr.SpecialName | PEAPI.FieldAttr.RTSpecialName;
426 fielddef.Define (code_gen, classdef);
427 fielddef_list.Add (fielddef.PeapiFieldDef);
430 classdef.SetFieldOrder (fielddef_list);
432 foreach (MethodDef methoddef in method_list) {
433 methoddef.Define (code_gen);
436 if (event_list != null) {
437 foreach (EventDef eventdef in event_list) {
438 eventdef.Define (code_gen, classdef);
442 if (property_list != null) {
443 foreach (PropertyDef propdef in property_list) {
444 propdef.Define (code_gen, classdef);
449 if (customattr_list != null) {
450 foreach (CustomAttr customattr in customattr_list) {
451 customattr.AddTo (code_gen, classdef);
452 if (customattr.IsSuppressUnmanaged (code_gen))
453 classdef.AddAttribute (PEAPI.TypeAttr.HasSecurity);
457 /// Add declarative security to this class
458 if (decl_sec != null) {
459 decl_sec.AddTo (code_gen, classdef);
460 classdef.AddAttribute (PEAPI.TypeAttr.HasSecurity);
463 if (override_list != null) {
464 foreach (DictionaryEntry entry in override_list) {
465 MethodDef body = (MethodDef) entry.Key;
466 DictionaryEntry decl = (DictionaryEntry) entry.Value;
467 BaseTypeRef parent_type = (BaseTypeRef) decl.Key;
468 parent_type.Resolve (code_gen);
469 string over_name = (string) decl.Value;
470 BaseMethodRef over_meth = parent_type.GetMethodRef (body.RetType,
471 body.CallConv, over_name, body.ParamTypeList (), body.GenParamCount);
472 over_meth.Resolve (code_gen);
473 classdef.AddMethodOverride (over_meth.PeapiMethod,
474 body.PeapiMethodDef);
478 if (override_long_list != null) {
479 foreach (DictionaryEntry entry in override_long_list) {
480 string sig = (string) entry.Key;
481 BaseMethodRef decl = (BaseMethodRef) entry.Value;
482 MethodDef body = (MethodDef) method_table[sig];
483 decl.Resolve (code_gen);
484 classdef.AddMethodOverride (decl.PeapiMethod,
485 body.PeapiMethodDef);
490 public PEAPI.Method ResolveMethod (BaseTypeRef ret_type, PEAPI.CallConv call_conv,
491 string name, BaseTypeRef [] param, int gen_param_count, CodeGen code_gen)
493 string signature = MethodDef.CreateSignature (ret_type, call_conv, name, param, gen_param_count, false);
494 MethodDef methoddef = (MethodDef) method_table[signature];
496 if (methoddef != null)
497 return methoddef.Resolve (code_gen, classdef);
498 return ResolveAsMethodRef (ret_type, call_conv, name, param, gen_param_count, code_gen);
501 public PEAPI.Method ResolveVarargMethod (BaseTypeRef ret_type, PEAPI.CallConv call_conv,
502 string name, BaseTypeRef [] param, int gen_param_count, PEAPI.Type [] opt, CodeGen code_gen)
504 // Only MethodDef sig required to lookup in the method_table
505 string signature = MethodDef.CreateSignature (ret_type, call_conv, name, param, 0, false);
506 MethodDef methoddef = (MethodDef) method_table[signature];
507 if (methoddef != null) {
508 methoddef.Resolve (code_gen, classdef);
509 return methoddef.GetVarargSig (
511 MethodDef.CreateSignature (ret_type, call_conv, name, param, 0, true));
514 return ResolveAsMethodRef (ret_type, call_conv, name, param, gen_param_count, code_gen);
517 private PEAPI.Method ResolveAsMethodRef (BaseTypeRef ret_type, PEAPI.CallConv call_conv,
518 string name, BaseTypeRef [] param, int gen_param_count, CodeGen code_gen)
520 ExternTypeRef type_ref = code_gen.ThisModule.GetTypeRef (FullName, false);
521 ExternMethodRef methodref = (ExternMethodRef) type_ref.GetMethodRef (ret_type, call_conv, name, param, gen_param_count);
522 methodref.Resolve (code_gen);
524 return methodref.PeapiMethod;
527 public PEAPI.Field ResolveField (string name, BaseTypeRef ret_type, CodeGen code_gen)
529 FieldDef fielddef = (FieldDef) field_table[new DictionaryEntry (name, ret_type.FullName)];
531 return fielddef.Resolve (code_gen, classdef);
533 ExternTypeRef type_ref = code_gen.ThisModule.GetTypeRef (FullName, false);
534 IFieldRef fieldref = type_ref.GetFieldRef (ret_type, name);
535 fieldref.Resolve (code_gen);
537 return fieldref.PeapiField;
540 private string MakeFullName ()
542 if (name_space == null || name_space == String.Empty)
545 return name_space + "." + name;
548 public int CompareTo (object obj)
550 TypeDef type_def = (TypeDef) obj;
552 return FullName.CompareTo (type_def.FullName);