pinline.pas 22 KB

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