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;
100 public string FullName {
101 get { return MakeFullName (); }
104 public string NestedFullName {
105 get { return (outer == null ? FullName : (outer.NestedFullName + "/" + FullName)); }
108 public TypeDef OuterType {
109 get { return outer; }
112 public PEAPI.ClassDef PeapiType {
113 get { return classdef; }
116 public PEAPI.ClassDef ClassDef {
117 get { return classdef; }
120 public bool IsGenericType {
121 get { return (gen_params == null); }
124 public bool IsDefined {
125 get { return is_defined; }
128 public EventDef CurrentEvent {
129 get { return current_event; }
132 public PropertyDef CurrentProperty {
133 get { return current_property; }
136 public bool IsInterface {
137 get { return (attr & PEAPI.TypeAttr.Interface) != 0; }
140 public bool IsAbstract {
141 get { return (attr & PEAPI.TypeAttr.Abstract) != 0; }
144 public GenericParameters TypeParameters {
145 get { return gen_params; }
148 public DeclSecurity DeclSecurity {
150 if (decl_sec == null)
151 decl_sec = new DeclSecurity ();
156 public void AddOverride (MethodDef body, BaseTypeRef parent, string name)
158 if (override_list == null)
159 override_list = new ArrayList ();
160 override_list.Add (new DictionaryEntry (body,
161 new DictionaryEntry (parent, name)));
164 public void AddOverride (string sig, BaseMethodRef decl)
166 if (override_long_list == null)
167 override_long_list = new ArrayList ();
168 override_long_list.Add (new DictionaryEntry (sig,
172 public void MakeValueClass ()
174 is_value_class = true;
177 public void MakeEnumClass ()
179 is_enum_class = true;
182 public void SetSize (int size)
187 public void SetPack (int pack)
192 public void AddFieldDef (FieldDef fielddef)
194 if (IsInterface && !fielddef.IsStatic) {
195 Report.Warning ("Non-static field in interface, set to such");
196 fielddef.Attributes |= PEAPI.FieldAttr.Static;
199 DictionaryEntry entry = new DictionaryEntry (fielddef.Name, fielddef.Type.FullName);
200 if (field_table [entry] != null)
201 Report.Error ("Duplicate field declaration: " + fielddef.Type.FullName + " " + fielddef.Name);
202 field_table.Add (entry, fielddef);
203 field_list.Add (fielddef);
206 public void AddMethodDef (MethodDef methoddef)
208 if (IsInterface && !methoddef.IsStatic && (!methoddef.IsVirtual || !methoddef.IsAbstract)) {
209 Report.Warning (methoddef.StartLocation, "Non-virtual or non-abstract instance method in interface, set to such");
210 methoddef.Attributes |= PEAPI.MethAttr.Abstract | PEAPI.MethAttr.Virtual;
213 if (method_table [methoddef.Signature] != null)
214 Report.Error (methoddef.StartLocation, "Duplicate method declaration: " + methoddef.Signature);
216 method_table.Add (methoddef.Signature, methoddef);
217 method_list.Add (methoddef);
220 public void BeginEventDef (EventDef event_def)
222 if (current_event != null)
223 Report.Error ("An event definition was not closed.");
225 current_event = event_def;
228 public void EndEventDef ()
230 if (event_list == null)
231 event_list = new ArrayList ();
233 event_list.Add (current_event);
234 current_event = null;
237 public void BeginPropertyDef (PropertyDef property_def)
239 if (current_property != null)
240 Report.Error ("A property definition was not closed.");
242 current_property = property_def;
245 public void EndPropertyDef ()
247 if (property_list == null)
248 property_list = new ArrayList ();
250 property_list.Add (current_property);
251 current_property = null;
254 public void AddCustomAttribute (CustomAttr customattr)
256 if (customattr_list == null)
257 customattr_list = new ArrayList ();
259 customattr_list.Add (customattr);
262 public GenericParameter GetGenericParam (string id)
264 if (gen_params == null)
267 return gen_params.GetGenericParam (id);
270 public GenericParameter GetGenericParam (int index)
272 if (gen_params == null || index < 0 || index >= gen_params.Count)
275 return gen_params [index];
278 public int GetGenericParamNum (string id)
280 if (gen_params == null)
283 return gen_params.GetGenericParamNum (id);
286 /* Resolve any GenParams in constraints, parent & impl_list */
287 private void ResolveGenParams ()
289 if (gen_params == null)
292 gen_params.ResolveConstraints (gen_params, null);
294 BaseGenericTypeRef gtr = parent as BaseGenericTypeRef;
296 gtr.Resolve (gen_params, null);
298 if (impl_list == null)
301 foreach (BaseClassRef impl in impl_list) {
302 gtr = impl as BaseGenericTypeRef;
304 gtr.Resolve (gen_params, null);
308 private bool IsValueType (string ns, string name)
310 return (ns == "System" && name == "ValueType");
313 private bool IsEnumType (string ns, string name)
315 return (ns == "System" && name == "Enum");
318 public void Define (CodeGen code_gen)
324 // Circular definition
325 Report.Error ("Circular definition of class: " + FullName);
329 PEAPI.TypeAttr vis = attr & PEAPI.TypeAttr.VisibilityMask;
331 if (vis == PEAPI.TypeAttr.Private || vis == PEAPI.TypeAttr.Public) {
332 /* Nested class, but attr not set accordingly. */
333 Report.Warning (location, String.Format ("Nested class '{0}' has non-nested visibility, set to such.", NestedFullName));
335 attr |= (vis == PEAPI.TypeAttr.Public ? PEAPI.TypeAttr.NestedPublic : PEAPI.TypeAttr.NestedPrivate);
339 if (parent != null) {
341 parent.Resolve (code_gen);
343 is_intransit = false;
344 if (parent.PeapiClass == null) {
345 Report.Error ("this type can not be a base type: "
349 if (IsValueType (parent.PeapiClass.nameSpace, parent.PeapiClass.name))
350 is_value_class = true;
351 else if (IsEnumType (parent.PeapiClass.nameSpace, parent.PeapiClass.name))
352 is_enum_class = true;
354 if (!IsValueType (name_space, name) && !IsEnumType (name_space, name) &&
355 is_value_class && (attr & PEAPI.TypeAttr.Sealed) == 0) {
357 attr |= PEAPI.TypeAttr.Sealed;
361 if (!outer.IsDefined)
362 outer.Define (code_gen);
363 classdef = outer.PeapiType.AddNestedClass (attr,
364 name_space, name, parent.PeapiClass);
366 if (is_value_class || is_enum_class) {
367 // Should probably confirm that the parent is System.ValueType
368 classdef = code_gen.PEFile.AddValueClass (attr,
369 name_space, name, is_value_class ? PEAPI.ValueClass.ValueType : PEAPI.ValueClass.Enum);
371 classdef = code_gen.PEFile.AddClass (attr,
372 name_space, name, parent.PeapiClass);
377 if (!outer.IsDefined)
378 outer.Define (code_gen);
379 classdef = outer.PeapiType.AddNestedClass (attr,
382 if (is_value_class || is_enum_class) {
383 classdef = code_gen.PEFile.AddValueClass (attr,
384 name_space, name, is_value_class ? PEAPI.ValueClass.ValueType : PEAPI.ValueClass.Enum);
386 classdef = code_gen.PEFile.AddClass (attr,
390 if (FullName == "System.Object")
391 classdef.SpecialNoSuper ();
396 if (size != -1 || pack != -1)
397 classdef.AddLayoutInfo ( (pack == -1) ? 1 : pack, (size == -1) ? 0 : size);
399 if (impl_list != null) {
400 foreach (BaseClassRef impl in impl_list) {
401 impl.Resolve (code_gen);
402 classdef.AddImplementedInterface (impl.PeapiClass);
406 if (gen_params != null)
407 gen_params.Resolve (code_gen, classdef);
409 is_intransit = false;
411 code_gen.AddToDefineContentsList (this);
414 public void DefineContents (CodeGen code_gen)
416 ArrayList fielddef_list = new ArrayList ();
417 foreach (FieldDef fielddef in field_list) {
418 if (is_enum_class && fielddef.Name == "value__") {
419 fielddef.Attributes |= PEAPI.FieldAttr.SpecialName | PEAPI.FieldAttr.RTSpecialName;
422 fielddef.Define (code_gen, classdef);
423 fielddef_list.Add (fielddef.PeapiFieldDef);
426 classdef.SetFieldOrder (fielddef_list);
428 foreach (MethodDef methoddef in method_list) {
429 methoddef.Define (code_gen);
432 if (event_list != null) {
433 foreach (EventDef eventdef in event_list) {
434 eventdef.Define (code_gen, classdef);
438 if (property_list != null) {
439 foreach (PropertyDef propdef in property_list) {
440 propdef.Define (code_gen, classdef);
445 if (customattr_list != null) {
446 foreach (CustomAttr customattr in customattr_list) {
447 customattr.AddTo (code_gen, classdef);
448 if (customattr.IsSuppressUnmanaged (code_gen))
449 classdef.AddAttribute (PEAPI.TypeAttr.HasSecurity);
453 /// Add declarative security to this class
454 if (decl_sec != null) {
455 decl_sec.AddTo (code_gen, classdef);
456 classdef.AddAttribute (PEAPI.TypeAttr.HasSecurity);
459 if (override_list != null) {
460 foreach (DictionaryEntry entry in override_list) {
461 MethodDef body = (MethodDef) entry.Key;
462 DictionaryEntry decl = (DictionaryEntry) entry.Value;
463 BaseTypeRef parent_type = (BaseTypeRef) decl.Key;
464 parent_type.Resolve (code_gen);
465 string over_name = (string) decl.Value;
466 BaseMethodRef over_meth = parent_type.GetMethodRef (body.RetType,
467 body.CallConv, over_name, body.ParamTypeList (), body.GenParamCount);
468 over_meth.Resolve (code_gen);
469 classdef.AddMethodOverride (over_meth.PeapiMethod,
470 body.PeapiMethodDef);
474 if (override_long_list != null) {
475 foreach (DictionaryEntry entry in override_long_list) {
476 string sig = (string) entry.Key;
477 BaseMethodRef decl = (BaseMethodRef) entry.Value;
478 MethodDef body = (MethodDef) method_table[sig];
479 decl.Resolve (code_gen);
480 classdef.AddMethodOverride (decl.PeapiMethod,
481 body.PeapiMethodDef);
486 public PEAPI.Method ResolveMethod (BaseTypeRef ret_type, PEAPI.CallConv call_conv,
487 string name, BaseTypeRef [] param, int gen_param_count, CodeGen code_gen)
489 string signature = MethodDef.CreateSignature (ret_type, call_conv, name, param, gen_param_count, false);
490 MethodDef methoddef = (MethodDef) method_table[signature];
492 if (methoddef != null)
493 return methoddef.Resolve (code_gen, classdef);
494 return ResolveAsMethodRef (ret_type, call_conv, name, param, gen_param_count, code_gen);
497 public PEAPI.Method ResolveVarargMethod (BaseTypeRef ret_type, PEAPI.CallConv call_conv,
498 string name, BaseTypeRef [] param, int gen_param_count, PEAPI.Type [] opt, CodeGen code_gen)
500 // Only MethodDef sig required to lookup in the method_table
501 string signature = MethodDef.CreateSignature (ret_type, call_conv, name, param, 0, false);
502 MethodDef methoddef = (MethodDef) method_table[signature];
503 if (methoddef != null) {
504 methoddef.Resolve (code_gen, classdef);
505 return methoddef.GetVarargSig (
507 MethodDef.CreateSignature (ret_type, call_conv, name, param, 0, true));
510 return ResolveAsMethodRef (ret_type, call_conv, name, param, gen_param_count, code_gen);
513 private PEAPI.Method ResolveAsMethodRef (BaseTypeRef ret_type, PEAPI.CallConv call_conv,
514 string name, BaseTypeRef [] param, int gen_param_count, CodeGen code_gen)
516 ExternTypeRef type_ref = code_gen.ThisModule.GetTypeRef (FullName, false);
517 ExternMethodRef methodref = (ExternMethodRef) type_ref.GetMethodRef (ret_type, call_conv, name, param, gen_param_count);
518 methodref.Resolve (code_gen);
520 return methodref.PeapiMethod;
523 public PEAPI.Field ResolveField (string name, BaseTypeRef ret_type, CodeGen code_gen)
525 FieldDef fielddef = (FieldDef) field_table[new DictionaryEntry (name, ret_type.FullName)];
527 return fielddef.Resolve (code_gen, classdef);
529 ExternTypeRef type_ref = code_gen.ThisModule.GetTypeRef (FullName, false);
530 IFieldRef fieldref = type_ref.GetFieldRef (ret_type, name);
531 fieldref.Resolve (code_gen);
533 return fieldref.PeapiField;
536 private string MakeFullName ()
538 if (name_space == null || name_space == String.Empty)
541 return name_space + "." + name;
544 public int CompareTo (object obj)
546 TypeDef type_def = (TypeDef) obj;
548 return FullName.CompareTo (type_def.FullName);