PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/sys/dotnet/fanx/emit/FClassEmit.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 106 lines | 65 code | 16 blank | 25 comment | 8 complexity | 19ed7e543254dccdf16d3f1808f6afee MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2006, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 4 Oct 06 Andy Frank Creation
  7. //
  8. using System.Collections;
  9. using Fan.Sys;
  10. using Fanx.Fcode;
  11. namespace Fanx.Emit
  12. {
  13. /// <summary>
  14. /// FClassEmit emits a normal class type.
  15. /// </summary>
  16. public class FClassEmit : FTypeEmit
  17. {
  18. //////////////////////////////////////////////////////////////////////////
  19. // Constructor
  20. //////////////////////////////////////////////////////////////////////////
  21. public FClassEmit(Emitter emitter, Type parent, FType type)
  22. : base(emitter, parent, type) {}
  23. //////////////////////////////////////////////////////////////////////////
  24. // Overrides
  25. //////////////////////////////////////////////////////////////////////////
  26. protected override string @base()
  27. {
  28. // if the base is a generic instance, then this must be a closure
  29. // method type (since we can't subclass List or Map). We subclass
  30. // from one of the canned Func.Indirect inner classes.
  31. FTypeRef refer = pod.typeRef(type.m_base);
  32. if (refer.isGenericInstance())
  33. {
  34. this.funcType = (FuncType)Type.find(refer.signature, true);
  35. int paramCount = funcType.m_params.Length;
  36. if (paramCount > Func.MaxIndirectParams)
  37. return "Fan.Sys.Func/IndirectX";
  38. else
  39. return "Fan.Sys.Func/Indirect" + paramCount;
  40. }
  41. else
  42. {
  43. string baset = nname(type.m_base);
  44. if (baset == "System.Object") return "Fan.Sys.FanObj";
  45. if (baset == "Fan.Sys.Type") return "Fan.Sys.ClassType";
  46. return baset;
  47. }
  48. }
  49. protected override void emitType()
  50. {
  51. PERWAPI.TypeAttr classAttr = PERWAPI.TypeAttr.Public;
  52. if (isAbstract) classAttr |= PERWAPI.TypeAttr.Abstract;
  53. emitter.emitClass(baseClassName, className, interfaces, classAttr);
  54. // generate private static Type $Type; set in clinit
  55. typeField = emitter.classDef.AddField(
  56. PERWAPI.FieldAttr.Public | PERWAPI.FieldAttr.Static,
  57. "$type", emitter.findType("Fan.Sys.Type"));
  58. // generate type() instance method
  59. PERWAPI.MethodDef m = emitter.classDef.AddMethod(
  60. PERWAPI.MethAttr.Public | PERWAPI.MethAttr.Virtual, PERWAPI.ImplAttr.IL,
  61. "typeof", emitter.findType("Fan.Sys.Type"), new PERWAPI.Param[0]);
  62. m.AddCallConv(PERWAPI.CallConv.Instance);
  63. emitter.addToMethodMap(className, "typeof", new string[0], m);
  64. PERWAPI.CILInstructions code = m.CreateCodeBuffer();
  65. code.FieldInst(PERWAPI.FieldOp.ldsfld, typeField);
  66. code.Inst(PERWAPI.Op.ret);
  67. // generate peer field if native
  68. if (isNative)
  69. {
  70. peerField = emitter.classDef.AddField(PERWAPI.FieldAttr.Public,
  71. "m_peer", emitter.findType(className + "Peer"));
  72. }
  73. // Create ctor emit objects first so we can reference them
  74. // .ctor
  75. ctor = emitter.findMethod(selfName, ".ctor", new string[0], "System.Void") as PERWAPI.MethodDef;
  76. ctor.SetMethAttributes(
  77. PERWAPI.MethAttr.Public |
  78. PERWAPI.MethAttr.HideBySig |
  79. PERWAPI.MethAttr.SpecialRTSpecialName);
  80. ctor.AddCallConv(PERWAPI.CallConv.Instance);
  81. // .cctor
  82. cctor = emitter.findMethod(selfName, ".cctor", new string[0], "System.Void") as PERWAPI.MethodDef;
  83. cctor.SetMethAttributes(
  84. PERWAPI.MethAttr.Private |
  85. PERWAPI.MethAttr.Static |
  86. PERWAPI.MethAttr.HideBySig |
  87. PERWAPI.MethAttr.SpecialRTSpecialName);
  88. }
  89. }
  90. }