objcdef.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. {
  2. Copyright (c) 2010 by Jonas Maebe
  3. This unit implements some Objective-C type helper routines (minimal
  4. unit dependencies, usable in symdef).
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$i fpcdefs.inc}
  19. unit objcdef;
  20. interface
  21. uses
  22. symtype;
  23. { The internals of Objective-C's @encode() functionality: encode a
  24. type into the internal format used by the run time. Returns false
  25. if a type is not representable by the Objective-C run time, and in
  26. that case also the failing definition. }
  27. function objctryencodetype(def: tdef; out encodedtype: ansistring; out founderror: tdef): boolean;
  28. { Check whether a type can be used in an Objective-C method
  29. signature or field declaration. }
  30. function objcchecktype(def: tdef; out founderror: tdef): boolean;
  31. { add type info for def at the end of encodedstr. recordinfostate influences
  32. whether a record-style type will be fully encoded, or just using its
  33. type name. bpacked indicates whether a record/array is bitpacked.
  34. On error, founderror contains the type that triggered the error. }
  35. type
  36. trecordinfostate = (ris_initial, ris_afterpointer, ris_dontprint);
  37. function objcaddencodedtype(def: tdef; recordinfostate: trecordinfostate; bpacked: boolean; var encodedstr: ansistring; out founderror: tdef): boolean;
  38. implementation
  39. uses
  40. globtype,
  41. cutils,cclasses,
  42. verbose,
  43. symtable,symconst,symsym,symdef,
  44. defutil,paramgr;
  45. {******************************************************************
  46. Type encoding
  47. *******************************************************************}
  48. function encoderecst(const recname: ansistring; recst: tabstractrecordsymtable; var encodedstr: ansistring; out founderror: tdef): boolean;
  49. var
  50. variantstarts: tfplist;
  51. i, varindex: longint;
  52. field,
  53. firstfield: tfieldvarsym;
  54. firstfieldvariant,
  55. bpacked: boolean;
  56. begin
  57. result:=false;
  58. bpacked:=recst.fieldalignment=bit_alignment;
  59. { Is the first field already the start of a variant? }
  60. firstfield:=nil;
  61. firstfieldvariant:=false;
  62. for i:=0 to recst.symlist.count-1 do
  63. begin
  64. if (tsym(recst.symlist[i]).typ<>fieldvarsym) then
  65. continue;
  66. field:=tfieldvarsym(recst.symlist[i]);
  67. if not assigned(firstfield) then
  68. firstfield:=field
  69. else if (vo_is_first_field in field.varoptions) then
  70. begin
  71. if (field.fieldoffset=firstfield.fieldoffset) then
  72. firstfieldvariant:=true;
  73. end;
  74. end;
  75. variantstarts:=tfplist.create;
  76. encodedstr:=encodedstr+'{'+recname+'=';
  77. for i:=0 to recst.symlist.count-1 do
  78. begin
  79. if (tsym(recst.symlist[i]).typ<>fieldvarsym) then
  80. continue;
  81. field:=tfieldvarsym(recst.symlist[i]);
  82. { start of a variant part? }
  83. if ((field=firstfield) and
  84. firstfieldvariant) or
  85. ((field<>firstfield) and
  86. (vo_is_first_field in field.varoptions)) then
  87. begin
  88. varindex:=variantstarts.count-1;
  89. if (varindex=-1) or
  90. (tfieldvarsym(variantstarts[varindex]).fieldoffset<field.fieldoffset) then
  91. begin
  92. { new, more deeply nested variant }
  93. encodedstr:=encodedstr+'(?={?=';
  94. variantstarts.add(field);
  95. end
  96. else
  97. begin
  98. { close existing nested variants if any }
  99. while (varindex>=0) and
  100. (tfieldvarsym(variantstarts[varindex]).fieldoffset>field.fieldoffset) do
  101. begin
  102. { close more deeply nested variants }
  103. encodedstr:=encodedstr+'})';
  104. dec(varindex);
  105. end;
  106. if (varindex<0) then
  107. internalerror(2009081805);
  108. { variant at the same level as a previous one }
  109. variantstarts.count:=varindex+1;
  110. { No need to add this field, it has the same offset as the
  111. previous one at this position. }
  112. if tfieldvarsym(variantstarts[varindex]).fieldoffset<>field.fieldoffset then
  113. internalerror(2009081601);
  114. { close previous variant sub-part and start new one }
  115. encodedstr:=encodedstr+'}{?=';
  116. end
  117. end;
  118. if not objcaddencodedtype(field.vardef,ris_afterpointer,bpacked,encodedstr,founderror) then
  119. exit;
  120. end;
  121. for i:=0 to variantstarts.count-1 do
  122. encodedstr:=encodedstr+'})';
  123. variantstarts.free;
  124. encodedstr:=encodedstr+'}';
  125. result:=true
  126. end;
  127. function objcaddencodedtype(def: tdef; recordinfostate: trecordinfostate; bpacked: boolean; var encodedstr: ansistring; out founderror: tdef): boolean;
  128. var
  129. recname: ansistring;
  130. recdef: trecorddef;
  131. objdef: tobjectdef;
  132. len: aint;
  133. c: char;
  134. newstate: trecordinfostate;
  135. addrpara: boolean;
  136. begin
  137. result:=true;
  138. case def.typ of
  139. stringdef :
  140. begin
  141. case tstringdef(def).stringtype of
  142. st_shortstring:
  143. { include length byte }
  144. encodedstr:=encodedstr+'['+tostr(tstringdef(def).len+1)+'C]';
  145. else
  146. { While we could handle refcounted Pascal strings correctly
  147. when such methods are called from Pascal code, things would
  148. completely break down if they were called from Objective-C
  149. code/reflection since the necessary refcount helper calls
  150. would be missing on the caller side (unless we'd
  151. automatically generate wrappers). }
  152. result:=false;
  153. end;
  154. end;
  155. enumdef,
  156. orddef :
  157. begin
  158. if bpacked and
  159. not is_void(def) then
  160. encodedstr:=encodedstr+'b'+tostr(def.packedbitsize)
  161. else
  162. begin
  163. if is_void(def) then
  164. c:='v'
  165. { in gcc, sizeof(_Bool) = sizeof(char) }
  166. else if is_boolean(def) and
  167. (def.size=1) then
  168. c:='B'
  169. else
  170. begin
  171. case def.size of
  172. 1:
  173. c:='c';
  174. 2:
  175. c:='s';
  176. 4:
  177. c:='i';
  178. 8:
  179. c:='q';
  180. else
  181. internalerror(2009081502);
  182. end;
  183. if not is_signed(def) then
  184. c:=upcase(c);
  185. end;
  186. encodedstr:=encodedstr+c;
  187. end;
  188. end;
  189. pointerdef :
  190. begin
  191. if is_pchar(def) then
  192. encodedstr:=encodedstr+'*'
  193. else if (def=objc_idtype) then
  194. encodedstr:=encodedstr+'@'
  195. else if (def=objc_seltype) then
  196. encodedstr:=encodedstr+':'
  197. else if (def=objc_metaclasstype) then
  198. encodedstr:=encodedstr+'#'
  199. else
  200. begin
  201. encodedstr:=encodedstr+'^';
  202. newstate:=recordinfostate;
  203. if (recordinfostate<ris_dontprint) then
  204. newstate:=succ(newstate);
  205. if not objcaddencodedtype(tpointerdef(def).pointeddef,newstate,false,encodedstr,founderror) then
  206. begin
  207. result:=false;
  208. { report the exact (nested) error defintion }
  209. exit;
  210. end;
  211. end;
  212. end;
  213. floatdef :
  214. begin
  215. case tfloatdef(def).floattype of
  216. s32real:
  217. c:='f';
  218. s64real:
  219. c:='d';
  220. else
  221. begin
  222. c:='!';
  223. result:=false;
  224. end;
  225. end;
  226. encodedstr:=encodedstr+c;
  227. end;
  228. filedef :
  229. result:=false;
  230. recorddef :
  231. begin
  232. if assigned(def.typesym) then
  233. recname:=def.typename
  234. else
  235. recname:='?';
  236. if (recordinfostate<>ris_dontprint) then
  237. begin
  238. if not encoderecst(recname,tabstractrecordsymtable(trecorddef(def).symtable),encodedstr,founderror) then
  239. begin
  240. result:=false;
  241. { report the exact (nested) error defintion }
  242. exit;
  243. end
  244. end
  245. else
  246. encodedstr:=encodedstr+'{'+recname+'}'
  247. end;
  248. variantdef :
  249. begin
  250. recdef:=trecorddef(search_system_type('TVARDATA').typedef);
  251. if (recordinfostate<>ris_dontprint) then
  252. begin
  253. if not encoderecst(recdef.typename,tabstractrecordsymtable(recdef.symtable),encodedstr,founderror) then
  254. begin
  255. result:=false;
  256. { report the exact (nested) error defintion }
  257. exit;
  258. end
  259. end
  260. else
  261. encodedstr:=encodedstr+'{'+recdef.typename+'}';
  262. end;
  263. classrefdef :
  264. begin
  265. encodedstr:=encodedstr+'^';
  266. newstate:=recordinfostate;
  267. if (recordinfostate<>ris_dontprint) then
  268. newstate:=succ(newstate);
  269. if is_objcclassref(def) then
  270. begin
  271. objdef:=tobjectdef(tclassrefdef(def).pointeddef);
  272. if (newstate<>ris_dontprint) then
  273. { anonymous (objc)class definitions do not exist }
  274. begin
  275. if not encoderecst(objdef.objextname^,tabstractrecordsymtable(objdef.symtable),encodedstr,founderror) then
  276. { The fields of an Objective-C class should always be
  277. encodeable. }
  278. internalerror(2009081702);
  279. end
  280. else
  281. encodedstr:=encodedstr+'{'+objdef.objextname^+'}'
  282. end
  283. { Object Pascal classrefdefs point to a vmt, not really useful
  284. to completely write those here. I'm not even sure what the
  285. Objective-C run time uses this information for, since in C you
  286. can have forward struct definitions so not all structs passed
  287. to functions can be written out here either -> treat
  288. classrefdefs the same as such forward-defined structs. }
  289. else
  290. begin
  291. if assigned(def.typesym) then
  292. recname:=def.typename
  293. else
  294. recname:='?';
  295. encodedstr:=encodedstr+'{'+recname;
  296. if (newstate<>ris_dontprint) then
  297. encodedstr:=encodedstr+'=';
  298. encodedstr:=encodedstr+'}'
  299. end;
  300. end;
  301. setdef :
  302. begin
  303. addrpara:=paramanager.push_addr_param(vs_value,def,pocall_cdecl);
  304. if not addrpara then
  305. { encode as an record, they are always passed by value in C. }
  306. encodedstr:=encodedstr+'{?=';
  307. { Encode the set itself as an array. Without an encompassing
  308. record, these are always passed by reference in C. }
  309. encodedstr:=encodedstr+'['+tostr(def.size)+'C]';
  310. if not addrpara then
  311. encodedstr:=encodedstr+'}';
  312. end;
  313. formaldef :
  314. begin
  315. encodedstr:=encodedstr+'^v';
  316. end;
  317. arraydef :
  318. begin
  319. if is_array_of_const(def) then
  320. { do nothing, varargs are ignored in signatures }
  321. else if is_special_array(def) then
  322. result:=false
  323. else
  324. begin
  325. len:=tarraydef(def).highrange-tarraydef(def).lowrange+1;
  326. if is_packed_array(def) then
  327. begin
  328. { convert from bits to bytes for bitpacked arrays }
  329. len:=(len+7) div 8;
  330. { and encode as plain array of bytes }
  331. encodedstr:=encodedstr+'['+tostr(len)+'C]';
  332. end
  333. else
  334. begin
  335. encodedstr:=encodedstr+'['+tostr(len);
  336. { Embedded structured types in the array are printed
  337. in full regardless of the current recordinfostate. }
  338. if not objcaddencodedtype(tarraydef(def).elementdef,ris_initial,false,encodedstr,founderror) then
  339. begin
  340. result:=false;
  341. { report the exact (nested) error defintion }
  342. exit;
  343. end;
  344. encodedstr:=encodedstr+']';
  345. end;
  346. end;
  347. end;
  348. procvardef :
  349. if not(po_is_block in tprocvardef(def).procoptions) then
  350. encodedstr:=encodedstr+'^?'
  351. else
  352. encodedstr:=encodedstr+'@?';
  353. objectdef :
  354. case tobjectdef(def).objecttype of
  355. odt_helper,
  356. odt_class,
  357. odt_object,
  358. odt_cppclass:
  359. begin
  360. newstate:=recordinfostate;
  361. { implicit pointer for classes }
  362. if (tobjectdef(def).objecttype in [odt_class,odt_helper]) then
  363. begin
  364. encodedstr:=encodedstr+'^';
  365. { make all classes opaque, so even if they contain a
  366. reference-counted field there is no problem. Since a
  367. "dereferenced class" object does not exist, this should
  368. not cause problems }
  369. newstate:=ris_dontprint;
  370. end;
  371. if newstate<>ris_dontprint then
  372. begin
  373. if not encoderecst(def.typename,tabstractrecordsymtable(tobjectdef(def).symtable),encodedstr,founderror) then
  374. begin
  375. result:=false;
  376. { report the exact (nested) error defintion }
  377. exit;
  378. end
  379. end
  380. else
  381. encodedstr:=encodedstr+'{'+def.typename+'}'
  382. end;
  383. odt_interfacecom,
  384. odt_interfacecom_property,
  385. odt_interfacecom_function,
  386. odt_dispinterface:
  387. result:=false;
  388. odt_interfacecorba:
  389. encodedstr:=encodedstr+'^{'+def.typename+'=}';
  390. { In Objective-C, the actual types of class instances are
  391. NSObject* etc, and those are encoded as "@". In FPC, to keep
  392. the similarity with Delphi-style Object Pascal, the type is
  393. NSObject and the pointer is implicit. Objective-C's "NSObject"
  394. has "class of NSObject" as equivalent here. }
  395. odt_objcclass,
  396. odt_objcprotocol:
  397. encodedstr:=encodedstr+'@';
  398. else
  399. internalerror(2009081509);
  400. end;
  401. undefineddef,
  402. errordef :
  403. result:=false;
  404. procdef :
  405. { must be done via objcencodemethod() }
  406. internalerror(2009081511);
  407. else
  408. internalerror(2009150812);
  409. end;
  410. if not result then
  411. founderror:=def;
  412. end;
  413. function objctryencodetype(def: tdef; out encodedtype: ansistring; out founderror: tdef): boolean;
  414. begin
  415. result:=objcaddencodedtype(def,ris_initial,false,encodedtype,founderror);
  416. end;
  417. {******************************************************************
  418. ObjC type validity checking
  419. *******************************************************************}
  420. function objcdochecktype(def: tdef; recordinfostate: trecordinfostate; out founderror: tdef): boolean; forward;
  421. function checkrecsttype(recst: tabstractrecordsymtable; recordinfostate: trecordinfostate; out founderror: tdef): boolean;
  422. var
  423. i: longint;
  424. field: tfieldvarsym;
  425. newstate: trecordinfostate;
  426. begin
  427. result:=false;
  428. newstate:=recordinfostate;
  429. { Although we never have to print the type info for nested
  430. records, check them anyway in case we're not after a pointer
  431. since if such records contain refcounted types then they
  432. can cause just as much trouble as if they were a simple
  433. refcounted field. }
  434. if (newstate=ris_afterpointer) then
  435. newstate:=ris_dontprint;
  436. for i:=0 to recst.symlist.count-1 do
  437. begin
  438. if (tsym(recst.symlist[i]).typ<>fieldvarsym) then
  439. continue;
  440. field:=tfieldvarsym(recst.symlist[i]);
  441. if not objcdochecktype(field.vardef,newstate,founderror) then
  442. exit;
  443. end;
  444. result:=true
  445. end;
  446. function objcdochecktype(def: tdef; recordinfostate: trecordinfostate; out founderror: tdef): boolean;
  447. var
  448. recdef: trecorddef;
  449. objdef: tobjectdef;
  450. newstate: trecordinfostate;
  451. begin
  452. result:=true;
  453. case def.typ of
  454. stringdef :
  455. begin
  456. case tstringdef(def).stringtype of
  457. st_shortstring:
  458. ;
  459. else
  460. { While we could handle refcounted Pascal strings correctly
  461. when such methods are called from Pascal code, things would
  462. completely break down if they were called from Objective-C
  463. code/reflection since the necessary refcount helper calls
  464. would be missing on the caller side (unless we'd
  465. automatically generate wrappers). }
  466. result:=false;
  467. end;
  468. end;
  469. enumdef,
  470. orddef :
  471. ;
  472. pointerdef :
  473. begin
  474. newstate:=recordinfostate;
  475. if (recordinfostate<ris_dontprint) then
  476. newstate:=succ(newstate);
  477. if not objcdochecktype(tpointerdef(def).pointeddef,newstate,founderror) then
  478. begin
  479. result:=false;
  480. { report the exact (nested) error defintion }
  481. exit;
  482. end;
  483. end;
  484. floatdef :
  485. begin
  486. case tfloatdef(def).floattype of
  487. s32real,
  488. s64real:
  489. ;
  490. else
  491. result:=false;
  492. end;
  493. end;
  494. filedef :
  495. result:=false;
  496. recorddef :
  497. begin
  498. if (recordinfostate<>ris_dontprint) then
  499. begin
  500. if not checkrecsttype(tabstractrecordsymtable(trecorddef(def).symtable),recordinfostate,founderror) then
  501. begin
  502. result:=false;
  503. { report the exact (nested) error defintion }
  504. exit;
  505. end
  506. end
  507. end;
  508. variantdef :
  509. begin
  510. recdef:=trecorddef(search_system_type('TVARDATA').typedef);
  511. if (recordinfostate<>ris_dontprint) then
  512. begin
  513. if not checkrecsttype(tabstractrecordsymtable(recdef.symtable),recordinfostate,founderror) then
  514. begin
  515. result:=false;
  516. { report the exact (nested) error defintion }
  517. exit;
  518. end
  519. end;
  520. end;
  521. classrefdef:
  522. begin
  523. if is_objcclassref(def) then
  524. begin
  525. objdef:=tobjectdef(tclassrefdef(def).pointeddef);
  526. newstate:=recordinfostate;
  527. if (recordinfostate<ris_dontprint) then
  528. newstate:=succ(newstate);
  529. if (newstate<>ris_dontprint) then
  530. begin
  531. if not checkrecsttype(tabstractrecordsymtable(objdef.symtable),recordinfostate,founderror) then
  532. begin
  533. result:=false;
  534. { report the exact (nested) error defintion }
  535. exit;
  536. end
  537. end
  538. end
  539. end;
  540. setdef,
  541. formaldef :
  542. ;
  543. arraydef :
  544. begin
  545. if is_array_of_const(def) then
  546. { ok, varargs are ignored in signatures }
  547. else if is_special_array(def) then
  548. result:=false
  549. else
  550. begin
  551. if not is_packed_array(def) then
  552. begin
  553. if not objcdochecktype(tarraydef(def).elementdef,ris_initial,founderror) then
  554. begin
  555. result:=false;
  556. { report the exact (nested) error defintion }
  557. exit;
  558. end;
  559. end;
  560. end;
  561. end;
  562. procvardef :
  563. ;
  564. objectdef :
  565. case tobjectdef(def).objecttype of
  566. odt_helper,
  567. odt_class,
  568. odt_object,
  569. odt_cppclass:
  570. begin
  571. newstate:=recordinfostate;
  572. { implicit pointer for classes }
  573. if (tobjectdef(def).objecttype in [odt_class,odt_helper]) then
  574. begin
  575. { make all classes opaque, so even if they contain a
  576. reference-counted field there is no problem. Since a
  577. "dereferenced class" object does not exist, this should
  578. not cause problems }
  579. newstate:=ris_dontprint;
  580. end;
  581. if newstate<>ris_dontprint then
  582. begin
  583. if not checkrecsttype(tabstractrecordsymtable(tobjectdef(def).symtable),newstate,founderror) then
  584. begin
  585. result:=false;
  586. { report the exact (nested) error defintion }
  587. exit;
  588. end
  589. end
  590. end;
  591. odt_interfacecom,
  592. odt_interfacecom_property,
  593. odt_interfacecom_function,
  594. odt_dispinterface:
  595. result:=false;
  596. odt_interfacecorba,
  597. odt_objcclass,
  598. odt_objcprotocol:
  599. ;
  600. else
  601. internalerror(2009081709);
  602. end;
  603. undefineddef,
  604. errordef :
  605. result:=false;
  606. procdef :
  607. result:=false;
  608. else
  609. internalerror(2009170812);
  610. end;
  611. if not result then
  612. founderror:=def;
  613. end;
  614. function objcchecktype(def: tdef; out founderror: tdef): boolean;
  615. begin
  616. result:=objcdochecktype(def,ris_initial,founderror);
  617. end;
  618. end.