nobjc.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. {
  2. Copyright (c) 2009 by Jonas Maebe
  3. This unit implements Objective-C nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { @abstract(This unit implements Objective-C nodes)
  18. This unit contains various nodes to implement Objective-Pascal and to
  19. interface with the Objective-C runtime.
  20. }
  21. unit nobjc;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. node;
  26. type
  27. tobjcselectornode = class(tunarynode)
  28. public
  29. constructor create(formethod: tnode);
  30. function pass_typecheck: tnode;override;
  31. function pass_1: tnode;override;
  32. end;
  33. tobjcselectornodeclass = class of tobjcselectornode;
  34. tobjcprotocolnode = class(tunarynode)
  35. public
  36. constructor create(forprotocol: tnode);
  37. function pass_typecheck: tnode;override;
  38. function pass_1: tnode;override;
  39. end;
  40. tobjcprotocolnodeclass = class of tobjcprotocolnode;
  41. tobjcmessagesendnode = class(tunarynode)
  42. public
  43. constructor create(forcall: tnode);
  44. function pass_typecheck: tnode;override;
  45. function pass_1: tnode;override;
  46. end;
  47. tobjcmessagesendnodeclass = class of tobjcmessagesendnode;
  48. var
  49. cobjcselectornode : tobjcselectornodeclass;
  50. cobjcmessagesendnode : tobjcmessagesendnodeclass;
  51. cobjcprotocolnode : tobjcprotocolnodeclass;
  52. implementation
  53. uses
  54. sysutils,
  55. globtype,cclasses,
  56. verbose,pass_1,
  57. defutil,
  58. symtype,symtable,symdef,symconst,symsym,
  59. paramgr,
  60. nutils,
  61. nbas,nld,ncnv,ncon,ncal,nmem,
  62. objcutil,
  63. cgbase;
  64. {*****************************************************************************
  65. TOBJCSELECTORNODE
  66. *****************************************************************************}
  67. constructor tobjcselectornode.create(formethod: tnode);
  68. begin
  69. inherited create(objcselectorn,formethod);
  70. end;
  71. function tobjcselectornode.pass_typecheck: tnode;
  72. var
  73. len: longint;
  74. s: shortstring;
  75. begin
  76. result:=nil;
  77. typecheckpass(left);
  78. { argument can be
  79. a) an objc method
  80. b) a pchar, zero-based chararray or ansistring
  81. }
  82. case left.nodetype of
  83. loadn:
  84. begin
  85. if (left.resultdef.typ=procdef) and
  86. (po_objc in tprocdef(left.resultdef).procoptions) then
  87. begin
  88. { ok }
  89. end
  90. else
  91. CGMessage1(type_e_expected_objc_method_but_got,left.resultdef.typename);
  92. end;
  93. stringconstn:
  94. begin
  95. if not objcvalidselectorname(tstringconstnode(left).value_str,
  96. tstringconstnode(left).len) then
  97. begin
  98. len:=tstringconstnode(left).len;
  99. if (len>255) then
  100. len:=255;
  101. setlength(s,len);
  102. move(tstringconstnode(left).value_str^,s[1],len);
  103. CGMessage1(type_e_invalid_objc_selector_name,s);
  104. exit;
  105. end;
  106. end
  107. else
  108. CGMessage(type_e_expected_objc_method);
  109. end;
  110. resultdef:=objc_seltype;
  111. end;
  112. function tobjcselectornode.pass_1: tnode;
  113. begin
  114. result:=nil;
  115. expectloc:=LOC_CREFERENCE;
  116. end;
  117. {*****************************************************************************
  118. TOBJPROTOCOLNODE
  119. *****************************************************************************}
  120. constructor tobjcprotocolnode.create(forprotocol: tnode);
  121. begin
  122. inherited create(objcprotocoln,forprotocol);
  123. end;
  124. function tobjcprotocolnode.pass_typecheck: tnode;
  125. begin
  126. result:=nil;
  127. typecheckpass(left);
  128. if (left.nodetype<>typen) then
  129. MessagePos(left.fileinfo,type_e_type_id_expected)
  130. else if not is_objcprotocol(left.resultdef) then
  131. MessagePos2(left.fileinfo,type_e_incompatible_types,left.resultdef.typename,'ObjCProtocol');
  132. resultdef:=objc_protocoltype;
  133. end;
  134. function tobjcprotocolnode.pass_1: tnode;
  135. begin
  136. result:=nil;
  137. expectloc:=LOC_CREFERENCE;
  138. end;
  139. {*****************************************************************************
  140. TOBJCMESSAGESENDNODE
  141. *****************************************************************************}
  142. constructor tobjcmessagesendnode.create(forcall: tnode);
  143. begin
  144. if (forcall.nodetype<>calln) then
  145. internalerror(2009032502);
  146. { typecheck pass (and pass1) must already have run on the call node,
  147. because pass1 of the callnode creates this node
  148. }
  149. inherited create(objcmessagesendn,forcall);
  150. end;
  151. function tobjcmessagesendnode.pass_typecheck: tnode;
  152. begin
  153. { typecheckpass of left has already run, see constructor }
  154. resultdef:=left.resultdef;
  155. result:=nil;
  156. expectloc:=left.expectloc;
  157. end;
  158. function tobjcmessagesendnode.pass_1: tnode;
  159. var
  160. msgsendname: string;
  161. newparas,
  162. para: tcallparanode;
  163. block,
  164. selftree : tnode;
  165. statements: tstatementnode;
  166. temp: ttempcreatenode;
  167. objcsupertype: tdef;
  168. field: tfieldvarsym;
  169. selfpara,
  170. msgselpara,
  171. respara,
  172. prerespara,
  173. prevpara: tcallparanode;
  174. begin
  175. { pass1 of left has already run, see constructor }
  176. { default behaviour: call objc_msgSend and friends;
  177. ppc64 and x86_64 for Mac OS X have to override this as they
  178. call messages via an indirect function call similar to
  179. dynamically linked functions, ARM maybe as well (not checked)
  180. Which variant of objc_msgSend is used depends on the
  181. result type, and on whether or not it's an inherited call.
  182. }
  183. newparas:=tcallparanode(tcallnode(left).left);
  184. { Find the self and msgsel parameters. }
  185. para:=newparas;
  186. selfpara:=nil;
  187. msgselpara:=nil;
  188. respara:=nil;
  189. prevpara:=nil;
  190. while assigned(para) do
  191. begin
  192. if (vo_is_self in para.parasym.varoptions) then
  193. selfpara:=para
  194. else if (vo_is_msgsel in para.parasym.varoptions) then
  195. msgselpara:=para
  196. else if (vo_is_funcret in para.parasym.varoptions) then
  197. begin
  198. prerespara:=prevpara;
  199. respara:=para;
  200. end;
  201. prevpara:=para;
  202. para:=tcallparanode(para.right);
  203. end;
  204. if not assigned(selfpara) then
  205. internalerror(2009051801);
  206. if not assigned(msgselpara) then
  207. internalerror(2009051802);
  208. { record returned via implicit pointer }
  209. if paramanager.ret_in_param(left.resultdef,tcallnode(left).procdefinition.proccalloption) then
  210. begin
  211. if not assigned(respara) then
  212. internalerror(2009091101);
  213. { Since the result parameter is also hidden in the routine we'll
  214. call now, it will be inserted again by the callnode. So we have to
  215. remove the old one, otherwise we'll have two result parameters.
  216. }
  217. if (tcallparanode(respara).left.nodetype<>nothingn) then
  218. internalerror(2009091102);
  219. if assigned(prerespara) then
  220. tcallparanode(prerespara).right:=tcallparanode(respara).right
  221. else
  222. begin
  223. tcallnode(left).left:=tcallparanode(respara).right;
  224. newparas:=tcallparanode(tcallnode(left).left);
  225. end;
  226. tcallparanode(respara).right:=nil;
  227. respara.free;
  228. if not(cnf_inherited in tcallnode(left).callnodeflags) then
  229. msgsendname:='OBJC_MSGSEND_STRET'
  230. else
  231. msgsendname:='OBJC_MSGSENDSUPER_STRET'
  232. end
  233. {$ifdef i386}
  234. { special case for fpu results on i386 for non-inherited calls }
  235. else if (left.resultdef.typ=floatdef) and
  236. not(cnf_inherited in tcallnode(left).callnodeflags) then
  237. msgsendname:='OBJC_MSGSEND_FPRET'
  238. {$endif}
  239. { default }
  240. else if not(cnf_inherited in tcallnode(left).callnodeflags) then
  241. msgsendname:='OBJC_MSGSEND'
  242. else
  243. msgsendname:='OBJC_MSGSENDSUPER';
  244. { Handle self }
  245. { 1) in case of sending a message to a superclass, self is a pointer to
  246. an objc_super record
  247. }
  248. if (cnf_inherited in tcallnode(left).callnodeflags) then
  249. begin
  250. block:=internalstatements(statements);
  251. objcsupertype:=search_named_unit_globaltype('OBJC1','OBJC_SUPER').typedef;
  252. if (objcsupertype.typ<>recorddef) then
  253. internalerror(2009032901);
  254. { temp for the for the objc_super record }
  255. temp:=ctempcreatenode.create(objcsupertype,objcsupertype.size,tt_persistent,false);
  256. addstatement(statements,temp);
  257. { initialize objc_super record }
  258. selftree:=load_self_node;
  259. { we can call an inherited class static/method from a regular method
  260. -> self node must change from instance pointer to vmt pointer)
  261. }
  262. if (po_classmethod in tcallnode(left).procdefinition.procoptions) and
  263. (selftree.resultdef.typ<>classrefdef) then
  264. begin
  265. selftree:=cloadvmtaddrnode.create(selftree);
  266. typecheckpass(selftree);
  267. end;
  268. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('RECEIVER'));
  269. if not assigned(field) then
  270. internalerror(2009032902);
  271. { first the destination object/class instance }
  272. addstatement(statements,
  273. cassignmentnode.create(
  274. csubscriptnode.create(field,ctemprefnode.create(temp)),
  275. selftree
  276. )
  277. );
  278. { and secondly, the class type in which the selector must be looked
  279. up (the parent class in case of an instance method, the parent's
  280. metaclass in case of a class method) }
  281. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('_CLASS'));
  282. if not assigned(field) then
  283. internalerror(2009032903);
  284. addstatement(statements,
  285. cassignmentnode.create(
  286. csubscriptnode.create(field,ctemprefnode.create(temp)),
  287. objcsuperclassnode(selftree.resultdef)
  288. )
  289. );
  290. { result of this block is the address of this temp }
  291. addstatement(statements,caddrnode.create_internal(ctemprefnode.create(temp)));
  292. { replace the method pointer with the address of this temp }
  293. tcallnode(left).methodpointer.free;
  294. tcallnode(left).methodpointer:=block;
  295. typecheckpass(block);
  296. end
  297. else
  298. { 2) regular call (not inherited) }
  299. begin
  300. { a) If we're calling a class method, use a class ref. }
  301. if (po_classmethod in tcallnode(left).procdefinition.procoptions) and
  302. ((tcallnode(left).methodpointer.nodetype=typen) or
  303. (tcallnode(left).methodpointer.resultdef.typ<>classrefdef)) then
  304. begin
  305. tcallnode(left).methodpointer:=cloadvmtaddrnode.create(tcallnode(left).methodpointer);
  306. firstpass(tcallnode(left).methodpointer);
  307. end;
  308. { b) convert methodpointer parameter to match objc_MsgSend* signatures }
  309. inserttypeconv_internal(tcallnode(left).methodpointer,objc_idtype);
  310. end;
  311. { replace self parameter }
  312. selfpara.left.free;
  313. selfpara.left:=tcallnode(left).methodpointer;
  314. { replace selector parameter }
  315. msgselpara.left.free;
  316. msgselpara.left:=
  317. cobjcselectornode.create(
  318. cstringconstnode.createstr(tprocdef(tcallnode(left).procdefinition).messageinf.str^)
  319. );
  320. { parameters are reused -> make sure they don't get freed }
  321. tcallnode(left).left:=nil;
  322. { methodpointer is also reused }
  323. tcallnode(left).methodpointer:=nil;
  324. { and now the call to the Objective-C rtl }
  325. result:=ccallnode.createinternresfromunit('OBJC1',msgsendname,newparas,left.resultdef);
  326. { in case an explicit function result was specified, keep it }
  327. tcallnode(result).funcretnode:=tcallnode(left).funcretnode;
  328. tcallnode(left).funcretnode:=nil;
  329. { keep variable paras }
  330. tcallnode(result).varargsparas:=tcallnode(left).varargsparas;
  331. tcallnode(left).varargsparas:=nil;
  332. if (cnf_inherited in tcallnode(left).callnodeflags) then
  333. begin
  334. { free the objc_super temp after the call. We cannout use
  335. ctempdeletenode.create_normal_temp before the call, because then
  336. the temp will be released while evaluating the parameters, and thus
  337. may be reused while evaluating another parameter
  338. }
  339. block:=internalstatements(statements);
  340. addstatement(statements,result);
  341. addstatement(statements,ctempdeletenode.create(temp));
  342. typecheckpass(block);
  343. result:=block;
  344. end;
  345. end;
  346. begin
  347. cobjcmessagesendnode:=tobjcmessagesendnode;
  348. end.