pinline.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. if target_info.system in systems_managed_vm then
  64. message(parser_e_feature_unsupported_for_vm);
  65. consume(_LKLAMMER);
  66. p:=comp_expr(true,false);
  67. { calc return type }
  68. if is_new then
  69. begin
  70. set_varstate(p,vs_written,[]);
  71. valid_for_var(p,true);
  72. end
  73. else
  74. set_varstate(p,vs_readwritten,[vsf_must_be_valid]);
  75. if (m_mac in current_settings.modeswitches) and
  76. is_class(p.resultdef) then
  77. begin
  78. classh:=tobjectdef(p.resultdef);
  79. { make sure we call ObjPas.TObject.Create/Free and not a random }
  80. { create/free method in a macpas descendent object (since those }
  81. { are not supposed to be called automatically when you call }
  82. { new/dispose) }
  83. while assigned(classh.childof) do
  84. classh := classh.childof;
  85. if is_new then
  86. begin
  87. sym:=search_struct_member(classh,'CREATE');
  88. p2 := cloadvmtaddrnode.create(ctypenode.create(p.resultdef));
  89. end
  90. else
  91. begin
  92. sym:=search_struct_member(classh,'FREE');
  93. p2 := p;
  94. end;
  95. if not(assigned(sym)) then
  96. begin
  97. p.free;
  98. if is_new then
  99. p2.free;
  100. new_dispose_statement := cerrornode.create;
  101. consume_all_until(_RKLAMMER);
  102. consume(_RKLAMMER);
  103. exit;
  104. end;
  105. do_member_read(classh,false,sym,p2,again,[]);
  106. { we need the real called method }
  107. do_typecheckpass(p2);
  108. if (p2.nodetype=calln) and
  109. assigned(tcallnode(p2).procdefinition) then
  110. begin
  111. if is_new then
  112. begin
  113. if (tcallnode(p2).procdefinition.proctypeoption<>potype_constructor) then
  114. Message(parser_e_expr_have_to_be_constructor_call);
  115. p2.resultdef:=p.resultdef;
  116. p2:=cassignmentnode.create(p,p2);
  117. typecheckpass(p2);
  118. end
  119. else
  120. begin
  121. { Free is not a destructor
  122. if (tcallnode(p2).procdefinition.proctypeoption<>potype_destructor) then
  123. Message(parser_e_expr_have_to_be_destructor_call);
  124. }
  125. end
  126. end
  127. else
  128. internalerror(2005061202);
  129. new_dispose_statement := p2;
  130. end
  131. { constructor,destructor specified }
  132. else if not(m_mac in current_settings.modeswitches) and
  133. try_to_consume(_COMMA) then
  134. begin
  135. { extended syntax of new and dispose }
  136. { function styled new is handled in factor }
  137. { destructors have no parameters }
  138. destructorname:=pattern;
  139. destructorpos:=current_tokenpos;
  140. consume(_ID);
  141. if is_typeparam(p.resultdef) then
  142. begin
  143. p.free;
  144. p:=factor(false,false);
  145. p.free;
  146. consume(_RKLAMMER);
  147. new_dispose_statement:=cnothingnode.create;
  148. exit;
  149. end;
  150. if (p.resultdef.typ<>pointerdef) then
  151. begin
  152. Message1(type_e_pointer_type_expected,p.resultdef.typename);
  153. p.free;
  154. p:=factor(false,false);
  155. p.free;
  156. consume(_RKLAMMER);
  157. new_dispose_statement:=cerrornode.create;
  158. exit;
  159. end;
  160. { first parameter must be an object or class }
  161. if tpointerdef(p.resultdef).pointeddef.typ<>objectdef then
  162. begin
  163. Message(parser_e_pointer_to_class_expected);
  164. p.free;
  165. new_dispose_statement:=factor(false,false);
  166. consume_all_until(_RKLAMMER);
  167. consume(_RKLAMMER);
  168. exit;
  169. end;
  170. { check, if the first parameter is a pointer to a _class_ }
  171. classh:=tobjectdef(tpointerdef(p.resultdef).pointeddef);
  172. if is_class(classh) then
  173. begin
  174. Message(parser_e_no_new_or_dispose_for_classes);
  175. new_dispose_statement:=factor(false,false);
  176. consume_all_until(_RKLAMMER);
  177. consume(_RKLAMMER);
  178. exit;
  179. end;
  180. { search cons-/destructor, also in parent classes }
  181. storepos:=current_tokenpos;
  182. current_tokenpos:=destructorpos;
  183. sym:=search_struct_member(classh,destructorname);
  184. current_tokenpos:=storepos;
  185. { the second parameter of new/dispose must be a call }
  186. { to a cons-/destructor }
  187. if (not assigned(sym)) or (sym.typ<>procsym) then
  188. begin
  189. if is_new then
  190. Message(parser_e_expr_have_to_be_constructor_call)
  191. else
  192. Message(parser_e_expr_have_to_be_destructor_call);
  193. p.free;
  194. new_dispose_statement:=cerrornode.create;
  195. end
  196. else
  197. begin
  198. { For new(var,constructor) we need to take a copy because
  199. p is also used in the assignmentn below }
  200. if is_new then
  201. begin
  202. p2:=cderefnode.create(p.getcopy);
  203. include(p2.flags,nf_no_checkpointer);
  204. end
  205. else
  206. p2:=cderefnode.create(p);
  207. do_typecheckpass(p2);
  208. if is_new then
  209. callflag:=cnf_new_call
  210. else
  211. callflag:=cnf_dispose_call;
  212. if is_new then
  213. do_member_read(classh,false,sym,p2,again,[callflag])
  214. else
  215. begin
  216. if not(m_fpc in current_settings.modeswitches) then
  217. do_member_read(classh,false,sym,p2,again,[callflag])
  218. else
  219. begin
  220. p2:=ccallnode.create(nil,tprocsym(sym),sym.owner,p2,[callflag]);
  221. { support dispose(p,done()); }
  222. if try_to_consume(_LKLAMMER) then
  223. begin
  224. if not try_to_consume(_RKLAMMER) then
  225. begin
  226. Message(parser_e_no_paras_for_destructor);
  227. consume_all_until(_RKLAMMER);
  228. consume(_RKLAMMER);
  229. end;
  230. end;
  231. end;
  232. end;
  233. { we need the real called method }
  234. do_typecheckpass(p2);
  235. if (p2.nodetype=calln) and
  236. assigned(tcallnode(p2).procdefinition) then
  237. begin
  238. if is_new then
  239. begin
  240. if (tcallnode(p2).procdefinition.proctypeoption<>potype_constructor) then
  241. Message(parser_e_expr_have_to_be_constructor_call);
  242. p2.resultdef:=p.resultdef;
  243. p2:=cassignmentnode.create(p,p2);
  244. end
  245. else
  246. begin
  247. if (tcallnode(p2).procdefinition.proctypeoption<>potype_destructor) then
  248. Message(parser_e_expr_have_to_be_destructor_call);
  249. end;
  250. end
  251. else
  252. begin
  253. if is_new then
  254. CGMessage(parser_e_expr_have_to_be_constructor_call)
  255. else
  256. CGMessage(parser_e_expr_have_to_be_destructor_call);
  257. end;
  258. result:=p2;
  259. end;
  260. end
  261. else
  262. begin
  263. if (p.resultdef.typ<>pointerdef) then
  264. Begin
  265. if is_typeparam(p.resultdef) then
  266. begin
  267. p.free;
  268. consume(_RKLAMMER);
  269. new_dispose_statement:=cnothingnode.create;
  270. exit;
  271. end
  272. else
  273. begin
  274. Message1(type_e_pointer_type_expected,p.resultdef.typename);
  275. new_dispose_statement:=cerrornode.create;
  276. end;
  277. end
  278. else
  279. begin
  280. if (tpointerdef(p.resultdef).pointeddef.typ=objectdef) and
  281. (oo_has_vmt in tobjectdef(tpointerdef(p.resultdef).pointeddef).objectoptions) then
  282. Message(parser_w_use_extended_syntax_for_objects);
  283. if (tpointerdef(p.resultdef).pointeddef.typ=orddef) and
  284. (torddef(tpointerdef(p.resultdef).pointeddef).ordtype=uvoid) then
  285. begin
  286. if (m_tp7 in current_settings.modeswitches) or
  287. (m_delphi in current_settings.modeswitches) then
  288. Message(parser_w_no_new_dispose_on_void_pointers)
  289. else
  290. Message(parser_e_no_new_dispose_on_void_pointers);
  291. end;
  292. { create statements with call to getmem+initialize or
  293. finalize+freemem }
  294. new_dispose_statement:=internalstatements(newstatement);
  295. if is_new then
  296. begin
  297. { create temp for result }
  298. temp := ctempcreatenode.create(p.resultdef,p.resultdef.size,tt_persistent,true);
  299. addstatement(newstatement,temp);
  300. { create call to fpc_getmem }
  301. para := ccallparanode.create(cordconstnode.create
  302. (tpointerdef(p.resultdef).pointeddef.size,s32inttype,true),nil);
  303. addstatement(newstatement,cassignmentnode.create(
  304. ctemprefnode.create(temp),
  305. ccallnode.createintern('fpc_getmem',para)));
  306. { create call to fpc_initialize }
  307. if is_managed_type(tpointerdef(p.resultdef).pointeddef) or
  308. ((m_iso in current_settings.modeswitches) and (tpointerdef(p.resultdef).pointeddef.typ=filedef)) then
  309. addstatement(newstatement,cnodeutils.initialize_data_node(cderefnode.create(ctemprefnode.create(temp)),false));
  310. { copy the temp to the destination }
  311. addstatement(newstatement,cassignmentnode.create(
  312. p,
  313. ctemprefnode.create(temp)));
  314. { release temp }
  315. addstatement(newstatement,ctempdeletenode.create(temp));
  316. end
  317. else
  318. begin
  319. { create call to fpc_finalize }
  320. if is_managed_type(tpointerdef(p.resultdef).pointeddef) then
  321. addstatement(newstatement,cnodeutils.finalize_data_node(cderefnode.create(p.getcopy)));
  322. { create call to fpc_freemem }
  323. para := ccallparanode.create(p,nil);
  324. addstatement(newstatement,ccallnode.createintern('fpc_freemem',para));
  325. end;
  326. end;
  327. end;
  328. consume(_RKLAMMER);
  329. end;
  330. function new_function : tnode;
  331. var
  332. newstatement : tstatementnode;
  333. newblock : tblocknode;
  334. temp : ttempcreatenode;
  335. para : tcallparanode;
  336. p1,p2 : tnode;
  337. classh : tobjectdef;
  338. srsym : tsym;
  339. srsymtable : TSymtable;
  340. again : boolean; { dummy for do_proc_call }
  341. begin
  342. if target_info.system in systems_managed_vm then
  343. message(parser_e_feature_unsupported_for_vm);
  344. consume(_LKLAMMER);
  345. p1:=factor(false,false);
  346. if p1.nodetype<>typen then
  347. begin
  348. Message(type_e_type_id_expected);
  349. consume_all_until(_RKLAMMER);
  350. consume(_RKLAMMER);
  351. p1.destroy;
  352. new_function:=cerrornode.create;
  353. exit;
  354. end;
  355. if (p1.resultdef.typ<>pointerdef) then
  356. begin
  357. Message1(type_e_pointer_type_expected,p1.resultdef.typename);
  358. consume_all_until(_RKLAMMER);
  359. consume(_RKLAMMER);
  360. p1.destroy;
  361. new_function:=cerrornode.create;
  362. exit;
  363. end;
  364. if try_to_consume(_RKLAMMER) then
  365. begin
  366. if (tpointerdef(p1.resultdef).pointeddef.typ=objectdef) and
  367. (oo_has_vmt in tobjectdef(tpointerdef(p1.resultdef).pointeddef).objectoptions) then
  368. Message(parser_w_use_extended_syntax_for_objects);
  369. { create statements with call to getmem+initialize }
  370. newblock:=internalstatements(newstatement);
  371. { create temp for result }
  372. temp := ctempcreatenode.create(p1.resultdef,p1.resultdef.size,tt_persistent,true);
  373. addstatement(newstatement,temp);
  374. { create call to fpc_getmem }
  375. para := ccallparanode.create(cordconstnode.create
  376. (tpointerdef(p1.resultdef).pointeddef.size,s32inttype,true),nil);
  377. addstatement(newstatement,cassignmentnode.create(
  378. ctemprefnode.create(temp),
  379. ccallnode.createintern('fpc_getmem',para)));
  380. { create call to fpc_initialize }
  381. if is_managed_type(tpointerdef(p1.resultdef).pointeddef) then
  382. begin
  383. para := ccallparanode.create(caddrnode.create_internal(crttinode.create
  384. (tstoreddef(tpointerdef(p1.resultdef).pointeddef),initrtti,rdt_normal)),
  385. ccallparanode.create(ctemprefnode.create
  386. (temp),nil));
  387. addstatement(newstatement,ccallnode.createintern('fpc_initialize',para));
  388. end;
  389. { the last statement should return the value as
  390. location and type, this is done be referencing the
  391. temp and converting it first from a persistent temp to
  392. normal temp }
  393. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  394. addstatement(newstatement,ctemprefnode.create(temp));
  395. p1.destroy;
  396. p1:=newblock;
  397. end
  398. else
  399. begin
  400. consume(_COMMA);
  401. if tpointerdef(p1.resultdef).pointeddef.typ<>objectdef then
  402. begin
  403. Message(parser_e_pointer_to_class_expected);
  404. consume_all_until(_RKLAMMER);
  405. consume(_RKLAMMER);
  406. p1.destroy;
  407. new_function:=cerrornode.create;
  408. exit;
  409. end;
  410. classh:=tobjectdef(tpointerdef(p1.resultdef).pointeddef);
  411. { use the objectdef for loading the VMT }
  412. p2:=p1;
  413. p1:=ctypenode.create(tpointerdef(p1.resultdef).pointeddef);
  414. do_typecheckpass(p1);
  415. { search the constructor also in the symbol tables of
  416. the parents }
  417. afterassignment:=false;
  418. searchsym_in_class(classh,classh,pattern,srsym,srsymtable,true);
  419. consume(_ID);
  420. do_member_read(classh,false,srsym,p1,again,[cnf_new_call]);
  421. { we need to know which procedure is called }
  422. do_typecheckpass(p1);
  423. if not(
  424. (p1.nodetype=calln) and
  425. assigned(tcallnode(p1).procdefinition) and
  426. (tcallnode(p1).procdefinition.proctypeoption=potype_constructor)
  427. ) then
  428. Message(parser_e_expr_have_to_be_constructor_call);
  429. { constructors return boolean, update resultdef to return
  430. the pointer to the object }
  431. p1.resultdef:=p2.resultdef;
  432. p2.free;
  433. consume(_RKLAMMER);
  434. end;
  435. new_function:=p1;
  436. end;
  437. function inline_setlength : tnode;
  438. var
  439. paras: tnode;
  440. begin
  441. consume(_LKLAMMER);
  442. paras:=parse_paras(false,false,_RKLAMMER);
  443. consume(_RKLAMMER);
  444. if not assigned(paras) then
  445. begin
  446. result:=cerrornode.create;
  447. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  448. exit;
  449. end;
  450. result:=cinlinenode.create(in_setlength_x,false,paras);
  451. end;
  452. function inline_initfinal(isinit: boolean): tnode;
  453. var
  454. newblock,
  455. paras : tnode;
  456. npara,
  457. destppn,
  458. ppn : tcallparanode;
  459. begin
  460. { for easy exiting if something goes wrong }
  461. result := cerrornode.create;
  462. consume(_LKLAMMER);
  463. paras:=parse_paras(false,false,_RKLAMMER);
  464. consume(_RKLAMMER);
  465. ppn:=tcallparanode(paras);
  466. if not assigned(paras) or
  467. (assigned(ppn.right) and
  468. assigned(tcallparanode(ppn.right).right)) then
  469. begin
  470. if isinit then
  471. CGMessage1(parser_e_wrong_parameter_size,'Initialize')
  472. else
  473. CGMessage1(parser_e_wrong_parameter_size,'Finalize');
  474. exit;
  475. end;
  476. { 2 arguments? }
  477. if assigned(ppn.right) then
  478. begin
  479. destppn:=tcallparanode(ppn.right);
  480. { create call to fpc_initialize/finalize_array }
  481. npara:=ccallparanode.create(ctypeconvnode.create
  482. (ppn.left,s32inttype),
  483. ccallparanode.create(caddrnode.create_internal
  484. (crttinode.create(tstoreddef(destppn.left.resultdef),initrtti,rdt_normal)),
  485. ccallparanode.create(caddrnode.create_internal
  486. (destppn.left),nil)));
  487. if isinit then
  488. newblock:=ccallnode.createintern('fpc_initialize_array',npara)
  489. else
  490. newblock:=ccallnode.createintern('fpc_finalize_array',npara);
  491. destppn.left:=nil;
  492. end
  493. else
  494. begin
  495. if isinit then
  496. newblock:=cnodeutils.initialize_data_node(ppn.left,true)
  497. else
  498. newblock:=cnodeutils.finalize_data_node(ppn.left);
  499. end;
  500. ppn.left:=nil;
  501. paras.free;
  502. result.free;
  503. result:=newblock;
  504. end;
  505. function inline_initialize : tnode;
  506. begin
  507. result:=inline_initfinal(true);
  508. end;
  509. function inline_finalize : tnode;
  510. begin
  511. result:=inline_initfinal(false);
  512. end;
  513. function inline_copy : tnode;
  514. var
  515. paras : tnode;
  516. { for easy exiting if something goes wrong }
  517. begin
  518. result := cerrornode.create;
  519. consume(_LKLAMMER);
  520. paras:=parse_paras(false,false,_RKLAMMER);
  521. consume(_RKLAMMER);
  522. if not assigned(paras) then
  523. begin
  524. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  525. exit;
  526. end;
  527. result.free;
  528. result:=cinlinenode.create(in_copy_x,false,paras);
  529. end;
  530. end.