pinline.pas 25 KB

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