tobjc11.pp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. { %target=darwin }
  2. { %cpu=powerpc,powerpc64,i386,x86_64,arm }
  3. { Written by Jonas Maebe in 2009, released into the public domain }
  4. {$mode objfpc}
  5. {$modeswitch objectivec1}
  6. {$packenum 1}
  7. {$packset 1}
  8. procedure check(const name,a,b: string);
  9. begin
  10. if (a<>b) then
  11. begin
  12. writeln('For ',name,' got: "',a,'", expected: "',b,'"');
  13. halt(1);
  14. end;
  15. end;
  16. procedure checksimpletypes;
  17. type
  18. tenum = (ea,eb,ec);
  19. tprocedure = procedure;
  20. var
  21. p: pointer;
  22. begin
  23. check('char',objcencode(char),'C');
  24. check('widechar',objcencode(widechar),'S');
  25. check('void',objcencode(p^),'v');
  26. check('tenum',objcencode(tenum),'C');
  27. check('shortint',objcencode(shortint),'c');
  28. check('byte',objcencode(byte),'C');
  29. check('smallint',objcencode(smallint),'s');
  30. check('word',objcencode(word),'S');
  31. check('longint',objcencode(longint),'i');
  32. check('cardinal',objcencode(cardinal),'I');
  33. check('int64',objcencode(int64),'q');
  34. check('qword',objcencode(qword),'Q');
  35. check('shortstring',objcencode(shortstring),'[256C]');
  36. check('pointer',objcencode(pointer),'^v');
  37. check('single',objcencode(single),'f');
  38. check('double',objcencode(double),'d');
  39. check('tprocedure',objcencode(tprocedure),'^?');
  40. check('id',objcencode(id),'@');
  41. check('NSObject',objcencode(NSObject),'@');
  42. check('pobjc_class',objcencode(pobjc_class),'#');
  43. check('selector',objcencode(objcselector('alloc')),':');
  44. end;
  45. procedure checkarrays;
  46. type
  47. ta = array[5..6] of byte;
  48. tb = array[1..10] of pointer;
  49. tc = array[0..3] of tb;
  50. begin
  51. check('ta',objcencode(ta),'[2C]');
  52. check('tb',objcencode(tb),'[10^v]');
  53. check('tc',objcencode(tc),'[4[10^v]]');
  54. end;
  55. procedure checkrecords;
  56. type
  57. tra=record
  58. a,b: longint;
  59. end;
  60. TStrippedVarRec = record
  61. case VType : shortint of
  62. vtInteger : (VInteger: Longint);
  63. vtBoolean : (VBoolean: Boolean);
  64. vtChar : (VChar: Char);
  65. vtWideChar : (VWideChar: WideChar);
  66. vtString : (VString: PShortString);
  67. vtPointer : (VPointer: Pointer);
  68. vtPChar : (VPChar: PChar);
  69. vtObject : (VObject: TObject);
  70. vtClass : (VClass: TClass);
  71. vtPWideChar : (VPWideChar: PWideChar);
  72. vtAnsiString : (VAnsiString: Pointer);
  73. vtInterface : (VInterface: Pointer);
  74. vtWideString : (VWideString: Pointer);
  75. vtInt64 : (VInt64: PInt64);
  76. vtQWord : (VQWord: PQWord);
  77. end;
  78. tnestedvarrechelper1 = record
  79. case byte of
  80. 1: (f: single);
  81. 2: (d: double);
  82. end;
  83. tnestedvarrechelper2 = record
  84. x: longint;
  85. y: shortint;
  86. end;
  87. tnestedvarrec = record
  88. a: longint;
  89. p: ^tra;
  90. case byte of
  91. 1: (t: tnestedvarrechelper1);
  92. 2: (t2: tnestedvarrechelper2);
  93. 3: (bb: longint);
  94. end;
  95. begin
  96. check('tra',objcencode(tra),'{tra=ii}');
  97. check('TStrippedVarRec',objcencode(TStrippedVarRec),'{TStrippedVarRec=c(?={?=i}{?=B}{?=C}{?=S}{?=^[256C]}{?=^v}{?=*}{?=^{TObject}}{?=^{TClass}}{?=^S}{?=^v}{?=^v}{?=^v}{?=^q}{?=^Q})}');
  98. check('TObject',objcencode(TObject),'^{TObject}');
  99. check('tnestedvarrec',objcencode(tnestedvarrec),'{tnestedvarrec=i^{tra}(?={?={tnestedvarrechelper1=(?={?=f}{?=d})}}{?={tnestedvarrechelper2=ic}}{?=i})}');
  100. end;
  101. procedure checksets;
  102. type
  103. tset1 = set of 0..4;
  104. tset2 = set of 0..31;
  105. tset3 = set of 0..128;
  106. begin
  107. check('tset1',objcencode(tset1),'{?=[1C]}');
  108. check('tset2',objcencode(tset2),'{?=[4C]}');
  109. {$ifdef cpui386}
  110. { for some mysterious reason, sets are always passed by value for cdecl on
  111. i386 }
  112. check('tset3',objcencode(tset3),'{?=[17C]}');
  113. {$else cpui386}
  114. check('tset3',objcencode(tset3),'[17C]');
  115. {$endif cpui386}
  116. end;
  117. begin
  118. checksimpletypes;
  119. checkarrays;
  120. checkrecords;
  121. checksets;
  122. end.