pinline.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generates nodes for routines that need compiler support
  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. unit pinline;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,
  22. node,
  23. globals,
  24. cpuinfo;
  25. function new_dispose_statement(is_new:boolean) : tnode;
  26. function new_function : tnode;
  27. function inline_setlength : tnode;
  28. function inline_initialize : tnode;
  29. function inline_finalize : tnode;
  30. function inline_copy : tnode;
  31. implementation
  32. uses
  33. { common }
  34. cutils,
  35. { global }
  36. globtype,tokens,verbose,constexp,
  37. systems,
  38. { symtable }
  39. symbase,symconst,symdef,symsym,symtable,defutil,
  40. { pass 1 }
  41. pass_1,htypechk,
  42. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,ngenutil,
  43. { parser }
  44. scanner,
  45. pbase,pexpr,
  46. { codegen }
  47. cgbase
  48. ;
  49. function new_dispose_statement(is_new:boolean) : tnode;
  50. var
  51. newstatement : tstatementnode;
  52. temp : ttempcreatenode;
  53. para : tcallparanode;
  54. p,p2 : tnode;
  55. again : boolean; { dummy for do_proc_call }
  56. destructorname : TIDString;
  57. sym : tsym;
  58. classh : tobjectdef;
  59. callflag : tcallnodeflag;
  60. destructorpos,
  61. storepos : tfileposinfo;
  62. begin
  63. consume(_LKLAMMER);
  64. p:=comp_expr(true,false);
  65. { calc return type }
  66. if is_new then
  67. begin
  68. set_varstate(p,vs_written,[]);
  69. valid_for_var(p,true);
  70. end
  71. else
  72. set_varstate(p,vs_readwritten,[vsf_must_be_valid]);
  73. if (m_mac in current_settings.modeswitches) and
  74. is_class(p.resultdef) then
  75. begin
  76. classh:=tobjectdef(p.resultdef);
  77. { make sure we call ObjPas.TObject.Create/Free and not a random }
  78. { create/free method in a macpas descendent object (since those }
  79. { are not supposed to be called automatically when you call }
  80. { new/dispose) }
  81. while assigned(classh.childof) do
  82. classh := classh.childof;
  83. if is_new then
  84. begin
  85. sym:=search_struct_member(classh,'CREATE');
  86. p2 := cloadvmtaddrnode.create(ctypenode.create(p.resultdef));
  87. end
  88. else
  89. begin
  90. sym:=search_struct_member(classh,'FREE');
  91. p2 := p;
  92. end;
  93. if not(assigned(sym)) then
  94. begin
  95. p.free;
  96. if is_new then
  97. p2.free;
  98. new_dispose_statement := cerrornode.create;
  99. consume_all_until(_RKLAMMER);
  100. consume(_RKLAMMER);
  101. exit;
  102. end;
  103. do_member_read(classh,false,sym,p2,again,[]);
  104. { we need the real called method }
  105. do_typecheckpass(p2);
  106. if (p2.nodetype=calln) and
  107. assigned(tcallnode(p2).procdefinition) then
  108. begin
  109. if is_new then
  110. begin
  111. if (tcallnode(p2).procdefinition.proctypeoption<>potype_constructor) then
  112. Message(parser_e_expr_have_to_be_constructor_call);
  113. p2.resultdef:=p.resultdef;
  114. p2:=cassignmentnode.create(p,p2);
  115. typecheckpass(p2);
  116. end
  117. else
  118. begin
  119. { Free is not a destructor
  120. if (tcallnode(p2).procdefinition.proctypeoption<>potype_destructor) then
  121. Message(parser_e_expr_have_to_be_destructor_call);
  122. }
  123. end
  124. end
  125. else
  126. internalerror(2005061202);
  127. new_dispose_statement := p2;
  128. end
  129. { constructor,destructor specified }
  130. else if not(m_mac in current_settings.modeswitches) and
  131. try_to_consume(_COMMA) then
  132. begin
  133. { extended syntax of new and dispose }
  134. { function styled new is handled in factor }
  135. { destructors have no parameters }
  136. destructorname:=pattern;
  137. destructorpos:=current_tokenpos;
  138. consume(_ID);
  139. if (p.resultdef.typ<>pointerdef) then
  140. begin
  141. Message1(type_e_pointer_type_expected,p.resultdef.typename);
  142. p.free;
  143. p:=factor(false,false);
  144. p.free;
  145. consume(_RKLAMMER);
  146. new_dispose_statement:=cerrornode.create;
  147. exit;
  148. end;
  149. { first parameter must be an object or class }
  150. if tpointerdef(p.resultdef).pointeddef.typ<>objectdef then
  151. begin
  152. Message(parser_e_pointer_to_class_expected);
  153. p.free;
  154. new_dispose_statement:=factor(false,false);
  155. consume_all_until(_RKLAMMER);
  156. consume(_RKLAMMER);
  157. exit;
  158. end;
  159. { check, if the first parameter is a pointer to a _class_ }
  160. classh:=tobjectdef(tpointerdef(p.resultdef).pointeddef);
  161. if is_class(classh) then
  162. begin
  163. Message(parser_e_no_new_or_dispose_for_classes);
  164. new_dispose_statement:=factor(false,false);
  165. consume_all_until(_RKLAMMER);
  166. consume(_RKLAMMER);
  167. exit;
  168. end;
  169. { search cons-/destructor, also in parent classes }
  170. storepos:=current_tokenpos;
  171. current_tokenpos:=destructorpos;
  172. sym:=search_struct_member(classh,destructorname);
  173. current_tokenpos:=storepos;
  174. { the second parameter of new/dispose must be a call }
  175. { to a cons-/destructor }
  176. if (not assigned(sym)) or (sym.typ<>procsym) then
  177. begin
  178. if is_new then
  179. Message(parser_e_expr_have_to_be_constructor_call)
  180. else
  181. Message(parser_e_expr_have_to_be_destructor_call);
  182. p.free;
  183. new_dispose_statement:=cerrornode.create;
  184. end
  185. else
  186. begin
  187. { For new(var,constructor) we need to take a copy because
  188. p is also used in the assignmentn below }
  189. if is_new then
  190. begin
  191. p2:=cderefnode.create(p.getcopy);
  192. include(p2.flags,nf_no_checkpointer);
  193. end
  194. else
  195. p2:=cderefnode.create(p);
  196. do_typecheckpass(p2);
  197. if is_new then
  198. callflag:=cnf_new_call
  199. else
  200. callflag:=cnf_dispose_call;
  201. if is_new then
  202. do_member_read(classh,false,sym,p2,again,[callflag])
  203. else
  204. begin
  205. if not(m_fpc in current_settings.modeswitches) then
  206. do_member_read(classh,false,sym,p2,again,[callflag])
  207. else
  208. begin
  209. p2:=ccallnode.create(nil,tprocsym(sym),sym.owner,p2,[callflag]);
  210. { support dispose(p,done()); }
  211. if try_to_consume(_LKLAMMER) then
  212. begin
  213. if not try_to_consume(_RKLAMMER) then
  214. begin
  215. Message(parser_e_no_paras_for_destructor);
  216. consume_all_until(_RKLAMMER);
  217. consume(_RKLAMMER);
  218. end;
  219. end;
  220. end;
  221. end;
  222. { we need the real called method }
  223. do_typecheckpass(p2);
  224. if (p2.nodetype=calln) and
  225. assigned(tcallnode(p2).procdefinition) then
  226. begin
  227. if is_new then
  228. begin
  229. if (tcallnode(p2).procdefinition.proctypeoption<>potype_constructor) then
  230. Message(parser_e_expr_have_to_be_constructor_call);
  231. p2.resultdef:=p.resultdef;
  232. p2:=cassignmentnode.create(p,p2);
  233. end
  234. else
  235. begin
  236. if (tcallnode(p2).procdefinition.proctypeoption<>potype_destructor) then
  237. Message(parser_e_expr_have_to_be_destructor_call);
  238. end;
  239. end
  240. else
  241. begin
  242. if is_new then
  243. CGMessage(parser_e_expr_have_to_be_constructor_call)
  244. else
  245. CGMessage(parser_e_expr_have_to_be_destructor_call);
  246. end;
  247. result:=p2;
  248. end;
  249. end
  250. else
  251. begin
  252. if (p.resultdef.typ<>pointerdef) then
  253. Begin
  254. Message1(type_e_pointer_type_expected,p.resultdef.typename);
  255. new_dispose_statement:=cerrornode.create;
  256. end
  257. else
  258. begin
  259. if (tpointerdef(p.resultdef).pointeddef.typ=objectdef) and
  260. (oo_has_vmt in tobjectdef(tpointerdef(p.resultdef).pointeddef).objectoptions) then
  261. Message(parser_w_use_extended_syntax_for_objects);
  262. if (tpointerdef(p.resultdef).pointeddef.typ=orddef) and
  263. (torddef(tpointerdef(p.resultdef).pointeddef).ordtype=uvoid) then
  264. begin
  265. if (m_tp7 in current_settings.modeswitches) or
  266. (m_delphi in current_settings.modeswitches) then
  267. Message(parser_w_no_new_dispose_on_void_pointers)
  268. else
  269. Message(parser_e_no_new_dispose_on_void_pointers);
  270. end;
  271. { create statements with call to getmem+initialize or
  272. finalize+freemem }
  273. new_dispose_statement:=internalstatements(newstatement);
  274. if is_new then
  275. begin
  276. { create temp for result }
  277. temp := ctempcreatenode.create(p.resultdef,p.resultdef.size,tt_persistent,true);
  278. addstatement(newstatement,temp);
  279. { create call to fpc_getmem }
  280. para := ccallparanode.create(cordconstnode.create
  281. (tpointerdef(p.resultdef).pointeddef.size,s32inttype,true),nil);
  282. addstatement(newstatement,cassignmentnode.create(
  283. ctemprefnode.create(temp),
  284. ccallnode.createintern('fpc_getmem',para)));
  285. { create call to fpc_initialize }
  286. if is_managed_type(tpointerdef(p.resultdef).pointeddef) or
  287. ((m_iso in current_settings.modeswitches) and (tpointerdef(p.resultdef).pointeddef.typ=filedef)) then
  288. addstatement(newstatement,cnodeutils.initialize_data_node(cderefnode.create(ctemprefnode.create(temp))));
  289. { copy the temp to the destination }
  290. addstatement(newstatement,cassignmentnode.create(
  291. p,
  292. ctemprefnode.create(temp)));
  293. { release temp }
  294. addstatement(newstatement,ctempdeletenode.create(temp));
  295. end
  296. else
  297. begin
  298. { create call to fpc_finalize }
  299. if is_managed_type(tpointerdef(p.resultdef).pointeddef) then
  300. addstatement(newstatement,cnodeutils.finalize_data_node(cderefnode.create(p.getcopy)));
  301. { create call to fpc_freemem }
  302. para := ccallparanode.create(p,nil);
  303. addstatement(newstatement,ccallnode.createintern('fpc_freemem',para));
  304. end;
  305. end;
  306. end;
  307. consume(_RKLAMMER);
  308. end;
  309. function new_function : tnode;
  310. var
  311. newstatement : tstatementnode;
  312. newblock : tblocknode;
  313. temp : ttempcreatenode;
  314. para : tcallparanode;
  315. p1,p2 : tnode;
  316. classh : tobjectdef;
  317. srsym : tsym;
  318. srsymtable : TSymtable;
  319. again : boolean; { dummy for do_proc_call }
  320. begin
  321. consume(_LKLAMMER);
  322. p1:=factor(false,false);
  323. if p1.nodetype<>typen then
  324. begin
  325. Message(type_e_type_id_expected);
  326. consume_all_until(_RKLAMMER);
  327. consume(_RKLAMMER);
  328. p1.destroy;
  329. new_function:=cerrornode.create;
  330. exit;
  331. end;
  332. if (p1.resultdef.typ<>pointerdef) then
  333. begin
  334. Message1(type_e_pointer_type_expected,p1.resultdef.typename);
  335. consume_all_until(_RKLAMMER);
  336. consume(_RKLAMMER);
  337. p1.destroy;
  338. new_function:=cerrornode.create;
  339. exit;
  340. end;
  341. if try_to_consume(_RKLAMMER) then
  342. begin
  343. if (tpointerdef(p1.resultdef).pointeddef.typ=objectdef) and
  344. (oo_has_vmt in tobjectdef(tpointerdef(p1.resultdef).pointeddef).objectoptions) then
  345. Message(parser_w_use_extended_syntax_for_objects);
  346. { create statements with call to getmem+initialize }
  347. newblock:=internalstatements(newstatement);
  348. { create temp for result }
  349. temp := ctempcreatenode.create(p1.resultdef,p1.resultdef.size,tt_persistent,true);
  350. addstatement(newstatement,temp);
  351. { create call to fpc_getmem }
  352. para := ccallparanode.create(cordconstnode.create
  353. (tpointerdef(p1.resultdef).pointeddef.size,s32inttype,true),nil);
  354. addstatement(newstatement,cassignmentnode.create(
  355. ctemprefnode.create(temp),
  356. ccallnode.createintern('fpc_getmem',para)));
  357. { create call to fpc_initialize }
  358. if is_managed_type(tpointerdef(p1.resultdef).pointeddef) then
  359. begin
  360. para := ccallparanode.create(caddrnode.create_internal(crttinode.create
  361. (tstoreddef(tpointerdef(p1.resultdef).pointeddef),initrtti,rdt_normal)),
  362. ccallparanode.create(ctemprefnode.create
  363. (temp),nil));
  364. addstatement(newstatement,ccallnode.createintern('fpc_initialize',para));
  365. end;
  366. { the last statement should return the value as
  367. location and type, this is done be referencing the
  368. temp and converting it first from a persistent temp to
  369. normal temp }
  370. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  371. addstatement(newstatement,ctemprefnode.create(temp));
  372. p1.destroy;
  373. p1:=newblock;
  374. end
  375. else
  376. begin
  377. consume(_COMMA);
  378. if tpointerdef(p1.resultdef).pointeddef.typ<>objectdef then
  379. begin
  380. Message(parser_e_pointer_to_class_expected);
  381. consume_all_until(_RKLAMMER);
  382. consume(_RKLAMMER);
  383. p1.destroy;
  384. new_function:=cerrornode.create;
  385. exit;
  386. end;
  387. classh:=tobjectdef(tpointerdef(p1.resultdef).pointeddef);
  388. { use the objectdef for loading the VMT }
  389. p2:=p1;
  390. p1:=ctypenode.create(tpointerdef(p1.resultdef).pointeddef);
  391. do_typecheckpass(p1);
  392. { search the constructor also in the symbol tables of
  393. the parents }
  394. afterassignment:=false;
  395. searchsym_in_class(classh,classh,pattern,srsym,srsymtable,true);
  396. consume(_ID);
  397. do_member_read(classh,false,srsym,p1,again,[cnf_new_call]);
  398. { we need to know which procedure is called }
  399. do_typecheckpass(p1);
  400. if not(
  401. (p1.nodetype=calln) and
  402. assigned(tcallnode(p1).procdefinition) and
  403. (tcallnode(p1).procdefinition.proctypeoption=potype_constructor)
  404. ) then
  405. Message(parser_e_expr_have_to_be_constructor_call);
  406. { constructors return boolean, update resultdef to return
  407. the pointer to the object }
  408. p1.resultdef:=p2.resultdef;
  409. p2.free;
  410. consume(_RKLAMMER);
  411. end;
  412. new_function:=p1;
  413. end;
  414. function inline_setlength : tnode;
  415. var
  416. paras: tnode;
  417. begin
  418. consume(_LKLAMMER);
  419. paras:=parse_paras(false,false,_RKLAMMER);
  420. consume(_RKLAMMER);
  421. if not assigned(paras) then
  422. begin
  423. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  424. exit;
  425. end;
  426. result:=cinlinenode.create(in_setlength_x,false,paras);
  427. end;
  428. function inline_initfinal(isinit: boolean): tnode;
  429. var
  430. newblock,
  431. paras : tnode;
  432. npara,
  433. destppn,
  434. ppn : tcallparanode;
  435. begin
  436. { for easy exiting if something goes wrong }
  437. result := cerrornode.create;
  438. consume(_LKLAMMER);
  439. paras:=parse_paras(false,false,_RKLAMMER);
  440. consume(_RKLAMMER);
  441. ppn:=tcallparanode(paras);
  442. if not assigned(paras) or
  443. (assigned(ppn.right) and
  444. assigned(tcallparanode(ppn.right).right)) then
  445. begin
  446. if isinit then
  447. CGMessage1(parser_e_wrong_parameter_size,'Initialize')
  448. else
  449. CGMessage1(parser_e_wrong_parameter_size,'Finalize');
  450. exit;
  451. end;
  452. { 2 arguments? }
  453. if assigned(ppn.right) then
  454. begin
  455. destppn:=tcallparanode(ppn.right);
  456. { create call to fpc_initialize/finalize_array }
  457. npara:=ccallparanode.create(ctypeconvnode.create
  458. (ppn.left,s32inttype),
  459. ccallparanode.create(caddrnode.create_internal
  460. (crttinode.create(tstoreddef(destppn.left.resultdef),initrtti,rdt_normal)),
  461. ccallparanode.create(caddrnode.create_internal
  462. (destppn.left),nil)));
  463. if isinit then
  464. newblock:=ccallnode.createintern('fpc_initialize_array',npara)
  465. else
  466. newblock:=ccallnode.createintern('fpc_finalize_array',npara);
  467. destppn.left:=nil;
  468. end
  469. else
  470. begin
  471. if isinit then
  472. newblock:=cnodeutils.initialize_data_node(ppn.left)
  473. else
  474. newblock:=cnodeutils.finalize_data_node(ppn.left);
  475. end;
  476. ppn.left:=nil;
  477. paras.free;
  478. result.free;
  479. result:=newblock;
  480. end;
  481. function inline_initialize : tnode;
  482. begin
  483. result:=inline_initfinal(true);
  484. end;
  485. function inline_finalize : tnode;
  486. begin
  487. result:=inline_initfinal(false);
  488. end;
  489. function inline_copy : tnode;
  490. var
  491. copynode,
  492. lowppn,
  493. highppn,
  494. npara,
  495. paras : tnode;
  496. ppn : tcallparanode;
  497. paradef : tdef;
  498. counter : integer;
  499. begin
  500. { for easy exiting if something goes wrong }
  501. result := cerrornode.create;
  502. consume(_LKLAMMER);
  503. paras:=parse_paras(false,false,_RKLAMMER);
  504. consume(_RKLAMMER);
  505. if not assigned(paras) then
  506. begin
  507. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  508. exit;
  509. end;
  510. { determine copy function to use based on the first argument,
  511. also count the number of arguments in this loop }
  512. counter:=1;
  513. ppn:=tcallparanode(paras);
  514. while assigned(ppn.right) do
  515. begin
  516. inc(counter);
  517. ppn:=tcallparanode(ppn.right);
  518. end;
  519. paradef:=ppn.left.resultdef;
  520. if is_ansistring(paradef) or
  521. (is_chararray(paradef) and
  522. (paradef.size>255)) or
  523. ((cs_ansistrings in current_settings.localswitches) and
  524. is_pchar(paradef)) then
  525. copynode:=ccallnode.createintern('fpc_ansistr_copy',paras)
  526. else
  527. if is_widestring(paradef) then
  528. copynode:=ccallnode.createintern('fpc_widestr_copy',paras)
  529. else
  530. if is_unicodestring(paradef) or
  531. is_widechararray(paradef) or
  532. is_pwidechar(paradef) then
  533. copynode:=ccallnode.createintern('fpc_unicodestr_copy',paras)
  534. else
  535. if is_char(paradef) then
  536. copynode:=ccallnode.createintern('fpc_char_copy',paras)
  537. else
  538. if is_dynamic_array(paradef) then
  539. begin
  540. { Only allow 1 or 3 arguments }
  541. if (counter<>1) and (counter<>3) then
  542. begin
  543. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  544. exit;
  545. end;
  546. { create statements with call }
  547. if (counter=3) then
  548. begin
  549. highppn:=tcallparanode(paras).left.getcopy;
  550. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  551. end
  552. else
  553. begin
  554. { use special -1,-1 argument to copy the whole array }
  555. highppn:=cordconstnode.create(int64(-1),s32inttype,false);
  556. lowppn:=cordconstnode.create(int64(-1),s32inttype,false);
  557. end;
  558. { create call to fpc_dynarray_copy }
  559. npara:=ccallparanode.create(highppn,
  560. ccallparanode.create(lowppn,
  561. ccallparanode.create(caddrnode.create_internal
  562. (crttinode.create(tstoreddef(ppn.left.resultdef),initrtti,rdt_normal)),
  563. ccallparanode.create
  564. (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
  565. copynode:=ccallnode.createinternres('fpc_dynarray_copy',npara,ppn.left.resultdef);
  566. ppn.left:=nil;
  567. paras.free;
  568. end
  569. else
  570. begin
  571. { generic fallback that will give an error if a wrong
  572. type is passed }
  573. if (counter=3) then
  574. copynode:=ccallnode.createintern('fpc_shortstr_copy',paras)
  575. else
  576. begin
  577. CGMessagePos(ppn.left.fileinfo,type_e_mismatch);
  578. copynode:=cerrornode.create;
  579. end
  580. end;
  581. result.free;
  582. result:=copynode;
  583. end;
  584. end.