objcutil.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. {
  2. Copyright (c) 2009 by Jonas Maebe
  3. This unit implements some Objective-C helper routines at the node tree
  4. level.
  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 objcutil;
  20. interface
  21. uses
  22. node,
  23. symtype,symdef;
  24. { Check whether a string contains a syntactically valid selector name. }
  25. function objcvalidselectorname(value_str: pchar; len: longint): boolean;
  26. { Generate a node loading the superclass structure necessary to call
  27. an inherited Objective-C method. }
  28. function objcsuperclassnode(def: tdef): tnode;
  29. { The internals of Objective-C's @encode() functionality: encode a
  30. type into the internal format used by the run time. Returns false
  31. if a type is not representable by the Objective-C run time, and in
  32. that case also the failing definition. }
  33. function objctryencodetype(def: tdef; out encodedtype: ansistring; out founderror: tdef): boolean;
  34. { Encode a method's parameters and result type into the format used by the
  35. run time (for generating protocol and class rtti). }
  36. function objcencodemethod(pd: tprocdef): ansistring;
  37. { Check whether a type can be used in an Objective-C method
  38. signature or field declaration. }
  39. function objcchecktype(def: tdef; out founderror: tdef): boolean;
  40. { Exports all assembler symbols related to the obj-c class }
  41. procedure exportobjcclass(def: tobjectdef);
  42. implementation
  43. uses
  44. globtype,
  45. cutils,cclasses,
  46. pass_1,
  47. verbose,systems,
  48. symtable,symconst,symsym,
  49. defutil,paramgr,
  50. nbas,nmem,ncal,nld,ncon,ncnv,
  51. export;
  52. {******************************************************************
  53. validselectorname
  54. *******************************************************************}
  55. function objcvalidselectorname(value_str: pchar; len: longint): boolean;
  56. var
  57. i : longint;
  58. gotcolon : boolean;
  59. begin
  60. result:=false;
  61. { empty name is not allowed }
  62. if (len=0) then
  63. exit;
  64. gotcolon:=false;
  65. { if the first character is a colon, all of them must be colons }
  66. if (value_str[0] = ':') then
  67. begin
  68. for i:=1 to len-1 do
  69. if (value_str[i]<>':') then
  70. exit;
  71. end
  72. else
  73. begin
  74. { no special characters other than ':'
  75. }
  76. for i:=0 to len-1 do
  77. if (value_str[i] = ':') then
  78. gotcolon:=true
  79. else if not(value_str[i] in ['_','A'..'Z','a'..'z','0'..'9',':']) then
  80. exit;
  81. { if there is at least one colon, the final character must
  82. also be a colon (in case it's only one character that is
  83. a colon, this was already checked before the above loop)
  84. }
  85. if gotcolon and
  86. (value_str[len-1] <> ':') then
  87. exit;
  88. end;
  89. result:=true;
  90. end;
  91. {******************************************************************
  92. objcsuperclassnode
  93. *******************************************************************}
  94. function objcloadbasefield(n: tnode; const fieldname: string): tnode;
  95. var
  96. vs : tsym;
  97. begin
  98. result:=ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(n,voidpointertype)),objc_objecttype);
  99. vs:=tsym(tabstractrecorddef(objc_objecttype).symtable.Find(fieldname));
  100. if not assigned(vs) or
  101. (vs.typ<>fieldvarsym) then
  102. internalerror(200911301);
  103. result:=csubscriptnode.create(vs,result);
  104. end;
  105. function objcsuperclassnode(def: tdef): tnode;
  106. var
  107. para : tcallparanode;
  108. begin
  109. { only valid for Objective-C classes and classrefs }
  110. if not is_objcclass(def) and
  111. not is_objcclassref(def) then
  112. internalerror(2009090901);
  113. { Can be done a lot more efficiently with direct symbol accesses, but
  114. requires extra node types. Maybe later. }
  115. if is_objcclassref(def) then
  116. begin
  117. if (oo_is_classhelper in tobjectdef(tclassrefdef(def).pointeddef).objectoptions) then
  118. begin
  119. { in case we are in a category method, we need the metaclass of the
  120. superclass class extended by this category (= metaclass of superclass of superclass) }
  121. result:=cloadvmtaddrnode.create(ctypenode.create(tobjectdef(tclassrefdef(def).pointeddef).childof.childof));
  122. result:=objcloadbasefield(result,'ISA');
  123. typecheckpass(result);
  124. { we're done }
  125. exit;
  126. end
  127. else
  128. begin
  129. { otherwise we need the superclass of the metaclass }
  130. para:=ccallparanode.create(cstringconstnode.createstr(tobjectdef(tclassrefdef(def).pointeddef).objextname^),nil);
  131. result:=ccallnode.createinternfromunit('OBJC','OBJC_GETMETACLASS',para);
  132. end
  133. end
  134. else
  135. begin
  136. if not(oo_is_classhelper in tobjectdef(def).objectoptions) then
  137. result:=cloadvmtaddrnode.create(ctypenode.create(def))
  138. else
  139. result:=cloadvmtaddrnode.create(ctypenode.create(tobjectdef(def).childof))
  140. end;
  141. {$if defined(onlymacosx10_6) or defined(arm) }
  142. { For the non-fragile ABI, the superclass send2 method itself loads the
  143. superclass. For the fragile ABI, we have to do this ourselves.
  144. NOTE: those send2 methods are only available on Mac OS X 10.6 and later!
  145. (but also on all iPhone SDK revisions we support) }
  146. if not(target_info.system in system_objc_nfabi) then
  147. {$endif onlymacosx10_6 or arm}
  148. result:=objcloadbasefield(result,'SUPERCLASS');
  149. typecheckpass(result);
  150. end;
  151. {******************************************************************
  152. Type encoding
  153. *******************************************************************}
  154. type
  155. trecordinfostate = (ris_initial, ris_afterpointer, ris_dontprint);
  156. function objcparasize(vs: tparavarsym): ptrint;
  157. begin
  158. result:=vs.paraloc[callerside].intsize;
  159. { In Objective-C, all ordinal types are widened to at least the
  160. size of the C "int" type. Assume __LP64__/4 byte ints for now. }
  161. if is_ordinal(vs.vardef) and
  162. (result<4) then
  163. result:=4;
  164. end;
  165. function addencodedtype(def: tdef; recordinfostate: trecordinfostate; bpacked: boolean; var encodedstr: ansistring; out founderror: tdef): boolean; forward;
  166. function encoderecst(const recname: ansistring; recst: tabstractrecordsymtable; var encodedstr: ansistring; out founderror: tdef): boolean;
  167. var
  168. variantstarts: tfplist;
  169. i, varindex: longint;
  170. field,
  171. firstfield: tfieldvarsym;
  172. firstfieldvariant,
  173. bpacked: boolean;
  174. begin
  175. result:=false;
  176. bpacked:=recst.fieldalignment=bit_alignment;
  177. { Is the first field already the start of a variant? }
  178. firstfield:=nil;
  179. firstfieldvariant:=false;
  180. for i:=0 to recst.symlist.count-1 do
  181. begin
  182. if (tsym(recst.symlist[i]).typ<>fieldvarsym) then
  183. continue;
  184. field:=tfieldvarsym(recst.symlist[i]);
  185. if not assigned(firstfield) then
  186. firstfield:=field
  187. else if (vo_is_first_field in field.varoptions) then
  188. begin
  189. if (field.fieldoffset=firstfield.fieldoffset) then
  190. firstfieldvariant:=true;
  191. end;
  192. end;
  193. variantstarts:=tfplist.create;
  194. encodedstr:=encodedstr+'{'+recname+'=';
  195. for i:=0 to recst.symlist.count-1 do
  196. begin
  197. if (tsym(recst.symlist[i]).typ<>fieldvarsym) then
  198. continue;
  199. field:=tfieldvarsym(recst.symlist[i]);
  200. { start of a variant part? }
  201. if ((field=firstfield) and
  202. firstfieldvariant) or
  203. ((field<>firstfield) and
  204. (vo_is_first_field in field.varoptions)) then
  205. begin
  206. varindex:=variantstarts.count-1;
  207. if (varindex=-1) or
  208. (tfieldvarsym(variantstarts[varindex]).fieldoffset<field.fieldoffset) then
  209. begin
  210. { new, more deeply nested variant }
  211. encodedstr:=encodedstr+'(?={?=';
  212. variantstarts.add(field);
  213. end
  214. else
  215. begin
  216. { close existing nested variants if any }
  217. while (varindex>=0) and
  218. (tfieldvarsym(variantstarts[varindex]).fieldoffset>field.fieldoffset) do
  219. begin
  220. { close more deeply nested variants }
  221. encodedstr:=encodedstr+'})';
  222. dec(varindex);
  223. end;
  224. if (varindex<0) then
  225. internalerror(2009081805);
  226. if (tfieldvarsym(variantstarts[varindex]).fieldoffset<>field.fieldoffset) then
  227. internalerror(2009081804);
  228. { variant at the same level as a previous one }
  229. variantstarts.count:=varindex+1;
  230. { No need to add this field, it has the same offset as the
  231. previous one at this position. }
  232. if tfieldvarsym(variantstarts[varindex]).fieldoffset<>field.fieldoffset then
  233. internalerror(2009081601);
  234. { close previous variant sub-part and start new one }
  235. encodedstr:=encodedstr+'}{?=';
  236. end
  237. end;
  238. if not addencodedtype(field.vardef,ris_afterpointer,bpacked,encodedstr,founderror) then
  239. exit;
  240. end;
  241. for i:=0 to variantstarts.count-1 do
  242. encodedstr:=encodedstr+'})';
  243. variantstarts.free;
  244. encodedstr:=encodedstr+'}';
  245. result:=true
  246. end;
  247. function addencodedtype(def: tdef; recordinfostate: trecordinfostate; bpacked: boolean; var encodedstr: ansistring; out founderror: tdef): boolean;
  248. var
  249. recname: ansistring;
  250. recdef: trecorddef;
  251. objdef: tobjectdef;
  252. len: aint;
  253. c: char;
  254. newstate: trecordinfostate;
  255. addrpara: boolean;
  256. begin
  257. result:=true;
  258. case def.typ of
  259. stringdef :
  260. begin
  261. case tstringdef(def).stringtype of
  262. st_shortstring:
  263. { include length byte }
  264. encodedstr:=encodedstr+'['+tostr(tstringdef(def).len+1)+'C]';
  265. else
  266. { While we could handle refcounted Pascal strings correctly
  267. when such methods are called from Pascal code, things would
  268. completely break down if they were called from Objective-C
  269. code/reflection since the necessary refcount helper calls
  270. would be missing on the caller side (unless we'd
  271. automatically generate wrappers). }
  272. result:=false;
  273. end;
  274. end;
  275. enumdef,
  276. orddef :
  277. begin
  278. if bpacked and
  279. not is_void(def) then
  280. encodedstr:=encodedstr+'b'+tostr(def.packedbitsize)
  281. else
  282. begin
  283. if is_void(def) then
  284. c:='v'
  285. { in gcc, sizeof(_Bool) = sizeof(char) }
  286. else if is_boolean(def) and
  287. (def.size=1) then
  288. c:='B'
  289. else
  290. begin
  291. case def.size of
  292. 1:
  293. c:='c';
  294. 2:
  295. c:='s';
  296. 4:
  297. c:='i';
  298. 8:
  299. c:='q';
  300. else
  301. internalerror(2009081502);
  302. end;
  303. if not is_signed(def) then
  304. c:=upcase(c);
  305. end;
  306. encodedstr:=encodedstr+c;
  307. end;
  308. end;
  309. pointerdef :
  310. begin
  311. if is_pchar(def) then
  312. encodedstr:=encodedstr+'*'
  313. else if (def=objc_idtype) then
  314. encodedstr:=encodedstr+'@'
  315. else if (def=objc_seltype) then
  316. encodedstr:=encodedstr+':'
  317. else if (def=objc_metaclasstype) then
  318. encodedstr:=encodedstr+'#'
  319. else
  320. begin
  321. encodedstr:=encodedstr+'^';
  322. newstate:=recordinfostate;
  323. if (recordinfostate<ris_dontprint) then
  324. newstate:=succ(newstate);
  325. if not addencodedtype(tpointerdef(def).pointeddef,newstate,false,encodedstr,founderror) then
  326. begin
  327. result:=false;
  328. { report the exact (nested) error defintion }
  329. exit;
  330. end;
  331. end;
  332. end;
  333. floatdef :
  334. begin
  335. case tfloatdef(def).floattype of
  336. s32real:
  337. c:='f';
  338. s64real:
  339. c:='d';
  340. else
  341. begin
  342. c:='!';
  343. result:=false;
  344. end;
  345. end;
  346. encodedstr:=encodedstr+c;
  347. end;
  348. filedef :
  349. result:=false;
  350. recorddef :
  351. begin
  352. if assigned(def.typesym) then
  353. recname:=def.typename
  354. else
  355. recname:='?';
  356. if (recordinfostate<>ris_dontprint) then
  357. begin
  358. if not encoderecst(recname,tabstractrecordsymtable(trecorddef(def).symtable),encodedstr,founderror) then
  359. begin
  360. result:=false;
  361. { report the exact (nested) error defintion }
  362. exit;
  363. end
  364. end
  365. else
  366. encodedstr:=encodedstr+'{'+recname+'}'
  367. end;
  368. variantdef :
  369. begin
  370. recdef:=trecorddef(search_system_type('TVARDATA').typedef);
  371. if (recordinfostate<>ris_dontprint) then
  372. begin
  373. if not encoderecst(recdef.typename,tabstractrecordsymtable(recdef.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+'{'+recdef.typename+'}';
  382. end;
  383. classrefdef :
  384. begin
  385. encodedstr:=encodedstr+'^';
  386. newstate:=recordinfostate;
  387. if (recordinfostate<>ris_dontprint) then
  388. newstate:=succ(newstate);
  389. if is_objcclassref(def) then
  390. begin
  391. objdef:=tobjectdef(tclassrefdef(def).pointeddef);
  392. if (newstate<>ris_dontprint) then
  393. { anonymous (objc)class definitions do not exist }
  394. begin
  395. if not encoderecst(objdef.objextname^,tabstractrecordsymtable(objdef.symtable),encodedstr,founderror) then
  396. { The fields of an Objective-C class should always be
  397. encodeable. }
  398. internalerror(2009081702);
  399. end
  400. else
  401. encodedstr:=encodedstr+'{'+objdef.objextname^+'}'
  402. end
  403. { Object Pascal classrefdefs point to a vmt, not really useful
  404. to completely write those here. I'm not even sure what the
  405. Objective-C run time uses this information for, since in C you
  406. can have forward struct definitions so not all structs passed
  407. to functions can be written out here either -> treat
  408. classrefdefs the same as such forward-defined structs. }
  409. else
  410. begin
  411. if assigned(def.typesym) then
  412. recname:=def.typename
  413. else
  414. recname:='?';
  415. encodedstr:=encodedstr+'{'+recname;
  416. if (newstate<>ris_dontprint) then
  417. encodedstr:=encodedstr+'=';
  418. encodedstr:=encodedstr+'}'
  419. end;
  420. end;
  421. setdef :
  422. begin
  423. addrpara:=paramanager.push_addr_param(vs_value,def,pocall_cdecl);
  424. if not addrpara then
  425. { encode as an record, they are always passed by value in C. }
  426. encodedstr:=encodedstr+'{?=';
  427. { Encode the set itself as an array. Without an encompassing
  428. record, these are always passed by reference in C. }
  429. encodedstr:=encodedstr+'['+tostr(def.size)+'C]';
  430. if not addrpara then
  431. encodedstr:=encodedstr+'}';
  432. end;
  433. formaldef :
  434. begin
  435. encodedstr:=encodedstr+'^v';
  436. end;
  437. arraydef :
  438. begin
  439. if is_array_of_const(def) then
  440. { do nothing, varargs are ignored in signatures }
  441. else if is_special_array(def) then
  442. result:=false
  443. else
  444. begin
  445. len:=tarraydef(def).highrange-tarraydef(def).lowrange+1;
  446. if is_packed_array(def) then
  447. begin
  448. { convert from bits to bytes for bitpacked arrays }
  449. len:=(len+7) div 8;
  450. { and encode as plain array of bytes }
  451. encodedstr:=encodedstr+'['+tostr(len)+'C]';
  452. end
  453. else
  454. begin
  455. encodedstr:=encodedstr+'['+tostr(len);
  456. { Embedded structured types in the array are printed
  457. in full regardless of the current recordinfostate. }
  458. if not addencodedtype(tarraydef(def).elementdef,ris_initial,false,encodedstr,founderror) then
  459. begin
  460. result:=false;
  461. { report the exact (nested) error defintion }
  462. exit;
  463. end;
  464. encodedstr:=encodedstr+']';
  465. end;
  466. end;
  467. end;
  468. procvardef :
  469. encodedstr:=encodedstr+'^?';
  470. objectdef :
  471. case tobjectdef(def).objecttype of
  472. odt_class,
  473. odt_object,
  474. odt_cppclass:
  475. begin
  476. newstate:=recordinfostate;
  477. { implicit pointer for classes }
  478. if (tobjectdef(def).objecttype=odt_class) then
  479. begin
  480. encodedstr:=encodedstr+'^';
  481. if (recordinfostate<ris_dontprint) then
  482. newstate:=succ(newstate);
  483. end;
  484. if newstate<>ris_dontprint then
  485. begin
  486. if not encoderecst(def.typename,tabstractrecordsymtable(tobjectdef(def).symtable),encodedstr,founderror) then
  487. begin
  488. result:=false;
  489. { report the exact (nested) error defintion }
  490. exit;
  491. end
  492. end
  493. else
  494. encodedstr:=encodedstr+'{'+def.typename+'}'
  495. end;
  496. odt_interfacecom,
  497. odt_interfacecom_property,
  498. odt_interfacecom_function,
  499. odt_dispinterface:
  500. result:=false;
  501. odt_interfacecorba:
  502. encodedstr:=encodedstr+'^{'+def.typename+'=}';
  503. { In Objective-C, the actual types of class instances are
  504. NSObject* etc, and those are encoded as "@". In FPC, to keep
  505. the similarity with Delphi-style Object Pascal, the type is
  506. NSObject and the pointer is implicit. Objective-C's "NSObject"
  507. has "class of NSObject" as equivalent here. }
  508. odt_objcclass,
  509. odt_objcprotocol:
  510. encodedstr:=encodedstr+'@';
  511. else
  512. internalerror(2009081509);
  513. end;
  514. undefineddef,
  515. errordef :
  516. result:=false;
  517. procdef :
  518. { must be done via objcencodemethod() }
  519. internalerror(2009081511);
  520. else
  521. internalerror(2009150812);
  522. end;
  523. if not result then
  524. founderror:=def;
  525. end;
  526. function objctryencodetype(def: tdef; out encodedtype: ansistring; out founderror: tdef): boolean;
  527. begin
  528. result:=addencodedtype(def,ris_initial,false,encodedtype,founderror);
  529. end;
  530. function objcencodemethod(pd: tprocdef): ansistring;
  531. var
  532. parasize,
  533. totalsize: aint;
  534. vs: tparavarsym;
  535. i: longint;
  536. temp: ansistring;
  537. founderror: tdef;
  538. begin
  539. result:='';
  540. totalsize:=0;
  541. if not pd.has_paraloc_info then
  542. begin
  543. pd.requiredargarea:=paramanager.create_paraloc_info(pd,callerside);
  544. pd.has_paraloc_info:=true;
  545. end;
  546. {$if defined(powerpc) and defined(dummy)}
  547. { Disabled, because neither Clang nor gcc does this, and the ObjC
  548. runtime contains an explicit fix to detect this error. }
  549. { On ppc, the callee is responsible for removing the hidden function
  550. result parameter from the stack, so it has to know. On i386, it's
  551. the caller that does this. }
  552. if (pd.returndef<>voidtype) and
  553. paramgr.ret_in_param(pd.returndef,pocall_cdecl) then
  554. inc(totalsize,sizeof(pint));
  555. {$endif}
  556. for i:=0 to pd.paras.count-1 do
  557. begin
  558. vs:=tparavarsym(pd.paras[i]);
  559. if (vo_is_funcret in vs.varoptions) then
  560. continue;
  561. { addencodedtype always assumes a value parameter, so add
  562. a pointer indirection for var/out parameters. }
  563. if not paramanager.push_addr_param(vs_value,vs.vardef,pocall_cdecl) and
  564. (vs.varspez in [vs_var,vs_out]) then
  565. result:=result+'^';
  566. { Add the parameter type. }
  567. if not addencodedtype(vs.vardef,ris_initial,false,result,founderror) then
  568. { should be checked earlier on }
  569. internalerror(2009081701);
  570. { And the total size of the parameters coming before this one
  571. (i.e., the "offset" of this parameter). }
  572. result:=result+tostr(totalsize);
  573. { Update the total parameter size }
  574. parasize:=objcparasize(vs);
  575. inc(totalsize,parasize);
  576. end;
  577. { Prepend the total parameter size. }
  578. result:=tostr(totalsize)+result;
  579. { And the type of the function result (void in case of a procedure). }
  580. temp:='';
  581. if not addencodedtype(pd.returndef,ris_initial,false,temp,founderror) then
  582. internalerror(2009081801);
  583. result:=temp+result;
  584. end;
  585. {******************************************************************
  586. ObjC type validity checking
  587. *******************************************************************}
  588. function objcdochecktype(def: tdef; recordinfostate: trecordinfostate; out founderror: tdef): boolean; forward;
  589. function checkrecsttype(recst: tabstractrecordsymtable; recordinfostate: trecordinfostate; out founderror: tdef): boolean;
  590. var
  591. i: longint;
  592. field: tfieldvarsym;
  593. newstate: trecordinfostate;
  594. begin
  595. result:=false;
  596. newstate:=recordinfostate;
  597. { Although we never have to print the type info for nested
  598. records, check them anyway in case we're not after a pointer
  599. since if such records contain refcounted types then they
  600. can cause just as much trouble as if they were a simple
  601. refcounted field. }
  602. if (newstate=ris_afterpointer) then
  603. newstate:=ris_dontprint;
  604. for i:=0 to recst.symlist.count-1 do
  605. begin
  606. if (tsym(recst.symlist[i]).typ<>fieldvarsym) then
  607. continue;
  608. field:=tfieldvarsym(recst.symlist[i]);
  609. if not objcdochecktype(field.vardef,newstate,founderror) then
  610. exit;
  611. end;
  612. result:=true
  613. end;
  614. function objcdochecktype(def: tdef; recordinfostate: trecordinfostate; out founderror: tdef): boolean;
  615. var
  616. recdef: trecorddef;
  617. objdef: tobjectdef;
  618. newstate: trecordinfostate;
  619. begin
  620. result:=true;
  621. case def.typ of
  622. stringdef :
  623. begin
  624. case tstringdef(def).stringtype of
  625. st_shortstring:
  626. ;
  627. else
  628. { While we could handle refcounted Pascal strings correctly
  629. when such methods are called from Pascal code, things would
  630. completely break down if they were called from Objective-C
  631. code/reflection since the necessary refcount helper calls
  632. would be missing on the caller side (unless we'd
  633. automatically generate wrappers). }
  634. result:=false;
  635. end;
  636. end;
  637. enumdef,
  638. orddef :
  639. ;
  640. pointerdef :
  641. begin
  642. newstate:=recordinfostate;
  643. if (recordinfostate<ris_dontprint) then
  644. newstate:=succ(newstate);
  645. if not objcdochecktype(tpointerdef(def).pointeddef,newstate,founderror) then
  646. begin
  647. result:=false;
  648. { report the exact (nested) error defintion }
  649. exit;
  650. end;
  651. end;
  652. floatdef :
  653. begin
  654. case tfloatdef(def).floattype of
  655. s32real,
  656. s64real:
  657. ;
  658. else
  659. result:=false;
  660. end;
  661. end;
  662. filedef :
  663. result:=false;
  664. recorddef :
  665. begin
  666. if (recordinfostate<>ris_dontprint) then
  667. begin
  668. if not checkrecsttype(tabstractrecordsymtable(trecorddef(def).symtable),recordinfostate,founderror) then
  669. begin
  670. result:=false;
  671. { report the exact (nested) error defintion }
  672. exit;
  673. end
  674. end
  675. end;
  676. variantdef :
  677. begin
  678. recdef:=trecorddef(search_system_type('TVARDATA').typedef);
  679. if (recordinfostate<>ris_dontprint) then
  680. begin
  681. if not checkrecsttype(tabstractrecordsymtable(recdef.symtable),recordinfostate,founderror) then
  682. begin
  683. result:=false;
  684. { report the exact (nested) error defintion }
  685. exit;
  686. end
  687. end;
  688. end;
  689. classrefdef:
  690. begin
  691. if is_objcclassref(def) then
  692. begin
  693. objdef:=tobjectdef(tclassrefdef(def).pointeddef);
  694. newstate:=recordinfostate;
  695. if (recordinfostate<ris_dontprint) then
  696. newstate:=succ(newstate);
  697. if (newstate<>ris_dontprint) then
  698. begin
  699. if not checkrecsttype(tabstractrecordsymtable(objdef.symtable),recordinfostate,founderror) then
  700. begin
  701. result:=false;
  702. { report the exact (nested) error defintion }
  703. exit;
  704. end
  705. end
  706. end
  707. end;
  708. setdef,
  709. formaldef :
  710. ;
  711. arraydef :
  712. begin
  713. if is_array_of_const(def) then
  714. { ok, varargs are ignored in signatures }
  715. else if is_special_array(def) then
  716. result:=false
  717. else
  718. begin
  719. if not is_packed_array(def) then
  720. begin
  721. if not objcdochecktype(tarraydef(def).elementdef,ris_initial,founderror) then
  722. begin
  723. result:=false;
  724. { report the exact (nested) error defintion }
  725. exit;
  726. end;
  727. end;
  728. end;
  729. end;
  730. procvardef :
  731. ;
  732. objectdef :
  733. case tobjectdef(def).objecttype of
  734. odt_class,
  735. odt_object,
  736. odt_cppclass:
  737. begin
  738. newstate:=recordinfostate;
  739. { implicit pointer for classes }
  740. if (tobjectdef(def).objecttype=odt_class) then
  741. begin
  742. if (recordinfostate<ris_dontprint) then
  743. newstate:=succ(newstate);
  744. end;
  745. if newstate<>ris_dontprint then
  746. begin
  747. if not checkrecsttype(tabstractrecordsymtable(tobjectdef(def).symtable),newstate,founderror) then
  748. begin
  749. result:=false;
  750. { report the exact (nested) error defintion }
  751. exit;
  752. end
  753. end
  754. end;
  755. odt_interfacecom,
  756. odt_interfacecom_property,
  757. odt_interfacecom_function,
  758. odt_dispinterface:
  759. result:=false;
  760. odt_interfacecorba,
  761. odt_objcclass,
  762. odt_objcprotocol:
  763. ;
  764. else
  765. internalerror(2009081709);
  766. end;
  767. undefineddef,
  768. errordef :
  769. result:=false;
  770. procdef :
  771. result:=false;
  772. else
  773. internalerror(2009170812);
  774. end;
  775. if not result then
  776. founderror:=def;
  777. end;
  778. function objcchecktype(def: tdef; out founderror: tdef): boolean;
  779. begin
  780. result:=objcdochecktype(def,ris_initial,founderror);
  781. end;
  782. {******************************************************************
  783. ObjC class exporting
  784. *******************************************************************}
  785. procedure exportobjcclassfields(objccls: tobjectdef);
  786. var
  787. i: longint;
  788. vf: tfieldvarsym;
  789. prefix: string;
  790. begin
  791. prefix:=target_info.cprefix+'OBJC_IVAR_$_'+objccls.objextname^+'.';
  792. for i:=0 to objccls.symtable.SymList.Count-1 do
  793. if tsym(objccls.symtable.SymList[i]).typ=fieldvarsym then
  794. begin
  795. vf:=tfieldvarsym(objccls.symtable.SymList[i]);
  796. { TODO: package visibility (private_extern) -- must not be exported
  797. either}
  798. if not(vf.visibility in [vis_private,vis_strictprivate]) then
  799. exportname(prefix+vf.RealName,0);
  800. end;
  801. end;
  802. procedure exportobjcclass(def: tobjectdef);
  803. begin
  804. if (target_info.system in system_objc_nfabi) then
  805. begin
  806. { export class and metaclass symbols }
  807. exportname(def.rtti_mangledname(objcclassrtti),0);
  808. exportname(def.rtti_mangledname(objcmetartti),0);
  809. { export public/protected instance variable offset symbols }
  810. exportobjcclassfields(def);
  811. end
  812. else
  813. begin
  814. { export the class symbol }
  815. exportname('.objc_class_name_'+def.objextname^,0);
  816. end;
  817. end;
  818. end.