nobjc.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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: tcallparanode;
  171. begin
  172. { pass1 of left has already run, see constructor }
  173. { default behaviour: call objc_msgSend and friends;
  174. ppc64 and x86_64 for Mac OS X have to override this as they
  175. call messages via an indirect function call similar to
  176. dynamically linked functions, ARM maybe as well (not checked)
  177. Which variant of objc_msgSend is used depends on the
  178. result type, and on whether or not it's an inherited call.
  179. }
  180. { record returned via implicit pointer }
  181. if paramanager.ret_in_param(left.resultdef,tcallnode(left).procdefinition.proccalloption) then
  182. if not(cnf_inherited in tcallnode(left).callnodeflags) then
  183. msgsendname:='OBJC_MSGSEND_STRET'
  184. else
  185. msgsendname:='OBJC_MSGSENDSUPER_STRET'
  186. {$ifdef i386}
  187. { special case for fpu results on i386 for non-inherited calls }
  188. else if (left.resultdef.typ=floatdef) and
  189. not(cnf_inherited in tcallnode(left).callnodeflags) then
  190. msgsendname:='OBJC_MSGSEND_FPRET'
  191. {$endif}
  192. { default }
  193. else if not(cnf_inherited in tcallnode(left).callnodeflags) then
  194. msgsendname:='OBJC_MSGSEND'
  195. else
  196. msgsendname:='OBJC_MSGSENDSUPER';
  197. newparas:=tcallparanode(tcallnode(left).left);
  198. { Find the self and msgsel parameters. }
  199. para:=newparas;
  200. selfpara:=nil;
  201. msgselpara:=nil;
  202. while assigned(para) do
  203. begin
  204. if (vo_is_self in para.parasym.varoptions) then
  205. selfpara:=para
  206. else if (vo_is_msgsel in para.parasym.varoptions) then
  207. msgselpara:=para;
  208. para:=tcallparanode(para.right);
  209. end;
  210. if not assigned(selfpara) then
  211. internalerror(2009051801);
  212. if not assigned(msgselpara) then
  213. internalerror(2009051802);
  214. { Handle self }
  215. { 1) in case of sending a message to a superclass, self is a pointer to
  216. an objc_super record
  217. }
  218. if (cnf_inherited in tcallnode(left).callnodeflags) then
  219. begin
  220. block:=internalstatements(statements);
  221. objcsupertype:=search_named_unit_globaltype('OBJC1','OBJC_SUPER').typedef;
  222. if (objcsupertype.typ<>recorddef) then
  223. internalerror(2009032901);
  224. { temp for the for the objc_super record }
  225. temp:=ctempcreatenode.create(objcsupertype,objcsupertype.size,tt_persistent,false);
  226. addstatement(statements,temp);
  227. { initialize objc_super record }
  228. selftree:=load_self_node;
  229. { we can call an inherited class static/method from a regular method
  230. -> self node must change from instance pointer to vmt pointer)
  231. }
  232. if (po_classmethod in tcallnode(left).procdefinition.procoptions) and
  233. (selftree.resultdef.typ<>classrefdef) then
  234. begin
  235. selftree:=cloadvmtaddrnode.create(selftree);
  236. typecheckpass(selftree);
  237. end;
  238. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('RECEIVER'));
  239. if not assigned(field) then
  240. internalerror(2009032902);
  241. { first the destination object/class instance }
  242. addstatement(statements,
  243. cassignmentnode.create(
  244. csubscriptnode.create(field,ctemprefnode.create(temp)),
  245. selftree
  246. )
  247. );
  248. { and secondly, the class type in which the selector must be looked
  249. up (the parent class in case of an instance method, the parent's
  250. metaclass in case of a class method) }
  251. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('_CLASS'));
  252. if not assigned(field) then
  253. internalerror(2009032903);
  254. addstatement(statements,
  255. cassignmentnode.create(
  256. csubscriptnode.create(field,ctemprefnode.create(temp)),
  257. objcsuperclassnode(selftree.resultdef)
  258. )
  259. );
  260. { result of this block is the address of this temp }
  261. addstatement(statements,caddrnode.create_internal(ctemprefnode.create(temp)));
  262. { replace the method pointer with the address of this temp }
  263. tcallnode(left).methodpointer.free;
  264. tcallnode(left).methodpointer:=block;
  265. typecheckpass(block);
  266. end
  267. else
  268. { 2) regular call (not inherited) }
  269. begin
  270. { a) If we're calling a class method, use a class ref. }
  271. if (po_classmethod in tcallnode(left).procdefinition.procoptions) and
  272. ((tcallnode(left).methodpointer.nodetype=typen) or
  273. (tcallnode(left).methodpointer.resultdef.typ<>classrefdef)) then
  274. begin
  275. tcallnode(left).methodpointer:=cloadvmtaddrnode.create(tcallnode(left).methodpointer);
  276. firstpass(tcallnode(left).methodpointer);
  277. end;
  278. { b) convert methodpointer parameter to match objc_MsgSend* signatures }
  279. inserttypeconv_internal(tcallnode(left).methodpointer,objc_idtype);
  280. end;
  281. { replace self parameter }
  282. selfpara.left.free;
  283. selfpara.left:=tcallnode(left).methodpointer;
  284. { replace selector parameter }
  285. msgselpara.left.Free;
  286. msgselpara.left:=
  287. cobjcselectornode.create(
  288. cstringconstnode.createstr(tprocdef(tcallnode(left).procdefinition).messageinf.str^)
  289. );
  290. { parameters are reused -> make sure they don't get freed }
  291. tcallnode(left).left:=nil;
  292. { methodpointer is also reused }
  293. tcallnode(left).methodpointer:=nil;
  294. { and now the call to the Objective-C rtl }
  295. result:=ccallnode.createinternresfromunit('OBJC1',msgsendname,newparas,left.resultdef);
  296. if (cnf_inherited in tcallnode(left).callnodeflags) then
  297. begin
  298. { free the objc_super temp after the call. We cannout use
  299. ctempdeletenode.create_normal_temp before the call, because then
  300. the temp will be released while evaluating the parameters, and thus
  301. may be reused while evaluating another parameter
  302. }
  303. block:=internalstatements(statements);
  304. addstatement(statements,result);
  305. addstatement(statements,ctempdeletenode.create(temp));
  306. typecheckpass(block);
  307. result:=block;
  308. end;
  309. end;
  310. begin
  311. cobjcmessagesendnode:=tobjcmessagesendnode;
  312. end.