pinline.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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,
  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,
  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);
  65. { calc return type }
  66. if is_new then
  67. set_varstate(p,vs_written,[])
  68. else
  69. set_varstate(p,vs_readwritten,[vsf_must_be_valid]);
  70. if (m_mac in current_settings.modeswitches) and
  71. is_class(p.resultdef) then
  72. begin
  73. classh:=tobjectdef(p.resultdef);
  74. { make sure we call ObjPas.TObject.Create/Free and not a random }
  75. { create/free method in a macpas descendent object (since those }
  76. { are not supposed to be called automatically when you call }
  77. { new/dispose) }
  78. while assigned(classh.childof) do
  79. classh := classh.childof;
  80. if is_new then
  81. begin
  82. sym:=search_class_member(classh,'CREATE');
  83. p2 := cloadvmtaddrnode.create(ctypenode.create(p.resultdef));;
  84. end
  85. else
  86. begin
  87. sym:=search_class_member(classh,'FREE');
  88. p2 := p;
  89. end;
  90. if not(assigned(sym)) then
  91. begin
  92. p.free;
  93. if is_new then
  94. p2.free;
  95. new_dispose_statement := cerrornode.create;
  96. consume_all_until(_RKLAMMER);
  97. consume(_RKLAMMER);
  98. exit;
  99. end;
  100. do_member_read(classh,false,sym,p2,again,[]);
  101. { we need the real called method }
  102. do_typecheckpass(p2);
  103. if (p2.nodetype=calln) and
  104. assigned(tcallnode(p2).procdefinition) then
  105. begin
  106. if is_new then
  107. begin
  108. if (tcallnode(p2).procdefinition.proctypeoption<>potype_constructor) then
  109. Message(parser_e_expr_have_to_be_constructor_call);
  110. p2.resultdef:=p.resultdef;
  111. p2:=cassignmentnode.create(p,p2);
  112. typecheckpass(p2);
  113. end
  114. else
  115. begin
  116. { Free is not a destructor
  117. if (tcallnode(p2).procdefinition.proctypeoption<>potype_destructor) then
  118. Message(parser_e_expr_have_to_be_destructor_call);
  119. }
  120. end
  121. end
  122. else
  123. internalerror(2005061202);
  124. new_dispose_statement := p2;
  125. end
  126. { constructor,destructor specified }
  127. else if not(m_mac in current_settings.modeswitches) and
  128. try_to_consume(_COMMA) then
  129. begin
  130. { extended syntax of new and dispose }
  131. { function styled new is handled in factor }
  132. { destructors have no parameters }
  133. destructorname:=pattern;
  134. destructorpos:=current_tokenpos;
  135. consume(_ID);
  136. if (p.resultdef.typ<>pointerdef) then
  137. begin
  138. Message1(type_e_pointer_type_expected,p.resultdef.typename);
  139. p.free;
  140. p:=factor(false);
  141. p.free;
  142. consume(_RKLAMMER);
  143. new_dispose_statement:=cerrornode.create;
  144. exit;
  145. end;
  146. { first parameter must be an object or class }
  147. if tpointerdef(p.resultdef).pointeddef.typ<>objectdef then
  148. begin
  149. Message(parser_e_pointer_to_class_expected);
  150. p.free;
  151. new_dispose_statement:=factor(false);
  152. consume_all_until(_RKLAMMER);
  153. consume(_RKLAMMER);
  154. exit;
  155. end;
  156. { check, if the first parameter is a pointer to a _class_ }
  157. classh:=tobjectdef(tpointerdef(p.resultdef).pointeddef);
  158. if is_class(classh) then
  159. begin
  160. Message(parser_e_no_new_or_dispose_for_classes);
  161. new_dispose_statement:=factor(false);
  162. consume_all_until(_RKLAMMER);
  163. consume(_RKLAMMER);
  164. exit;
  165. end;
  166. { search cons-/destructor, also in parent classes }
  167. storepos:=current_tokenpos;
  168. current_tokenpos:=destructorpos;
  169. sym:=search_class_member(classh,destructorname);
  170. current_tokenpos:=storepos;
  171. { the second parameter of new/dispose must be a call }
  172. { to a cons-/destructor }
  173. if (not assigned(sym)) or (sym.typ<>procsym) then
  174. begin
  175. if is_new then
  176. Message(parser_e_expr_have_to_be_constructor_call)
  177. else
  178. Message(parser_e_expr_have_to_be_destructor_call);
  179. p.free;
  180. new_dispose_statement:=cerrornode.create;
  181. end
  182. else
  183. begin
  184. { For new(var,constructor) we need to take a copy because
  185. p is also used in the assignmentn below }
  186. if is_new then
  187. begin
  188. p2:=cderefnode.create(p.getcopy);
  189. include(p2.flags,nf_no_checkpointer);
  190. end
  191. else
  192. p2:=cderefnode.create(p);
  193. do_typecheckpass(p2);
  194. if is_new then
  195. callflag:=cnf_new_call
  196. else
  197. callflag:=cnf_dispose_call;
  198. if is_new then
  199. do_member_read(classh,false,sym,p2,again,[callflag])
  200. else
  201. begin
  202. if not(m_fpc in current_settings.modeswitches) then
  203. do_member_read(classh,false,sym,p2,again,[callflag])
  204. else
  205. begin
  206. p2:=ccallnode.create(nil,tprocsym(sym),sym.owner,p2,[callflag]);
  207. { support dispose(p,done()); }
  208. if try_to_consume(_LKLAMMER) then
  209. begin
  210. if not try_to_consume(_RKLAMMER) then
  211. begin
  212. Message(parser_e_no_paras_for_destructor);
  213. consume_all_until(_RKLAMMER);
  214. consume(_RKLAMMER);
  215. end;
  216. end;
  217. end;
  218. end;
  219. { we need the real called method }
  220. do_typecheckpass(p2);
  221. if (p2.nodetype=calln) and
  222. assigned(tcallnode(p2).procdefinition) then
  223. begin
  224. if is_new then
  225. begin
  226. if (tcallnode(p2).procdefinition.proctypeoption<>potype_constructor) then
  227. Message(parser_e_expr_have_to_be_constructor_call);
  228. p2.resultdef:=p.resultdef;
  229. p2:=cassignmentnode.create(p,p2);
  230. end
  231. else
  232. begin
  233. if (tcallnode(p2).procdefinition.proctypeoption<>potype_destructor) then
  234. Message(parser_e_expr_have_to_be_destructor_call);
  235. end;
  236. end
  237. else
  238. begin
  239. if is_new then
  240. CGMessage(parser_e_expr_have_to_be_constructor_call)
  241. else
  242. CGMessage(parser_e_expr_have_to_be_destructor_call);
  243. end;
  244. result:=p2;
  245. end;
  246. end
  247. else
  248. begin
  249. if (p.resultdef.typ<>pointerdef) then
  250. Begin
  251. Message1(type_e_pointer_type_expected,p.resultdef.typename);
  252. new_dispose_statement:=cerrornode.create;
  253. end
  254. else
  255. begin
  256. if (tpointerdef(p.resultdef).pointeddef.typ=objectdef) and
  257. (oo_has_vmt in tobjectdef(tpointerdef(p.resultdef).pointeddef).objectoptions) then
  258. Message(parser_w_use_extended_syntax_for_objects);
  259. if (tpointerdef(p.resultdef).pointeddef.typ=orddef) and
  260. (torddef(tpointerdef(p.resultdef).pointeddef).ordtype=uvoid) then
  261. begin
  262. if (m_tp7 in current_settings.modeswitches) or
  263. (m_delphi in current_settings.modeswitches) then
  264. Message(parser_w_no_new_dispose_on_void_pointers)
  265. else
  266. Message(parser_e_no_new_dispose_on_void_pointers);
  267. end;
  268. { create statements with call to getmem+initialize or
  269. finalize+freemem }
  270. new_dispose_statement:=internalstatements(newstatement);
  271. if is_new then
  272. begin
  273. { create temp for result }
  274. temp := ctempcreatenode.create(p.resultdef,p.resultdef.size,tt_persistent,true);
  275. addstatement(newstatement,temp);
  276. { create call to fpc_getmem }
  277. para := ccallparanode.create(cordconstnode.create
  278. (tpointerdef(p.resultdef).pointeddef.size,s32inttype,true),nil);
  279. addstatement(newstatement,cassignmentnode.create(
  280. ctemprefnode.create(temp),
  281. ccallnode.createintern('fpc_getmem',para)));
  282. { create call to fpc_initialize }
  283. if tpointerdef(p.resultdef).pointeddef.needs_inittable then
  284. addstatement(newstatement,initialize_data_node(cderefnode.create(ctemprefnode.create(temp))));
  285. { copy the temp to the destination }
  286. addstatement(newstatement,cassignmentnode.create(
  287. p,
  288. ctemprefnode.create(temp)));
  289. { release temp }
  290. addstatement(newstatement,ctempdeletenode.create(temp));
  291. end
  292. else
  293. begin
  294. { create call to fpc_finalize }
  295. if tpointerdef(p.resultdef).pointeddef.needs_inittable then
  296. addstatement(newstatement,finalize_data_node(cderefnode.create(p.getcopy)));
  297. { create call to fpc_freemem }
  298. para := ccallparanode.create(p,nil);
  299. addstatement(newstatement,ccallnode.createintern('fpc_freemem',para));
  300. end;
  301. end;
  302. end;
  303. consume(_RKLAMMER);
  304. end;
  305. function new_function : tnode;
  306. var
  307. newstatement : tstatementnode;
  308. newblock : tblocknode;
  309. temp : ttempcreatenode;
  310. para : tcallparanode;
  311. p1,p2 : tnode;
  312. classh : tobjectdef;
  313. srsym : tsym;
  314. srsymtable : TSymtable;
  315. again : boolean; { dummy for do_proc_call }
  316. begin
  317. consume(_LKLAMMER);
  318. p1:=factor(false);
  319. if p1.nodetype<>typen then
  320. begin
  321. Message(type_e_type_id_expected);
  322. consume_all_until(_RKLAMMER);
  323. consume(_RKLAMMER);
  324. p1.destroy;
  325. new_function:=cerrornode.create;
  326. exit;
  327. end;
  328. if (p1.resultdef.typ<>pointerdef) then
  329. begin
  330. Message1(type_e_pointer_type_expected,p1.resultdef.typename);
  331. consume_all_until(_RKLAMMER);
  332. consume(_RKLAMMER);
  333. p1.destroy;
  334. new_function:=cerrornode.create;
  335. exit;
  336. end;
  337. if try_to_consume(_RKLAMMER) then
  338. begin
  339. if (tpointerdef(p1.resultdef).pointeddef.typ=objectdef) and
  340. (oo_has_vmt in tobjectdef(tpointerdef(p1.resultdef).pointeddef).objectoptions) then
  341. Message(parser_w_use_extended_syntax_for_objects);
  342. { create statements with call to getmem+initialize }
  343. newblock:=internalstatements(newstatement);
  344. { create temp for result }
  345. temp := ctempcreatenode.create(p1.resultdef,p1.resultdef.size,tt_persistent,true);
  346. addstatement(newstatement,temp);
  347. { create call to fpc_getmem }
  348. para := ccallparanode.create(cordconstnode.create
  349. (tpointerdef(p1.resultdef).pointeddef.size,s32inttype,true),nil);
  350. addstatement(newstatement,cassignmentnode.create(
  351. ctemprefnode.create(temp),
  352. ccallnode.createintern('fpc_getmem',para)));
  353. { create call to fpc_initialize }
  354. if tpointerdef(p1.resultdef).pointeddef.needs_inittable then
  355. begin
  356. para := ccallparanode.create(caddrnode.create_internal(crttinode.create
  357. (tstoreddef(tpointerdef(p1.resultdef).pointeddef),initrtti)),
  358. ccallparanode.create(ctemprefnode.create
  359. (temp),nil));
  360. addstatement(newstatement,ccallnode.createintern('fpc_initialize',para));
  361. end;
  362. { the last statement should return the value as
  363. location and type, this is done be referencing the
  364. temp and converting it first from a persistent temp to
  365. normal temp }
  366. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  367. addstatement(newstatement,ctemprefnode.create(temp));
  368. p1.destroy;
  369. p1:=newblock;
  370. end
  371. else
  372. begin
  373. consume(_COMMA);
  374. if tpointerdef(p1.resultdef).pointeddef.typ<>objectdef then
  375. begin
  376. Message(parser_e_pointer_to_class_expected);
  377. consume_all_until(_RKLAMMER);
  378. consume(_RKLAMMER);
  379. p1.destroy;
  380. new_function:=cerrornode.create;
  381. exit;
  382. end;
  383. classh:=tobjectdef(tpointerdef(p1.resultdef).pointeddef);
  384. { use the objectdef for loading the VMT }
  385. p2:=p1;
  386. p1:=ctypenode.create(tpointerdef(p1.resultdef).pointeddef);
  387. do_typecheckpass(p1);
  388. { search the constructor also in the symbol tables of
  389. the parents }
  390. afterassignment:=false;
  391. searchsym_in_class(classh,nil,pattern,srsym,srsymtable);
  392. consume(_ID);
  393. do_member_read(classh,false,srsym,p1,again,[cnf_new_call]);
  394. { we need to know which procedure is called }
  395. do_typecheckpass(p1);
  396. if not(
  397. (p1.nodetype=calln) and
  398. assigned(tcallnode(p1).procdefinition) and
  399. (tcallnode(p1).procdefinition.proctypeoption=potype_constructor)
  400. ) then
  401. Message(parser_e_expr_have_to_be_constructor_call);
  402. { constructors return boolean, update resultdef to return
  403. the pointer to the object }
  404. p1.resultdef:=p2.resultdef;
  405. p2.free;
  406. consume(_RKLAMMER);
  407. end;
  408. new_function:=p1;
  409. end;
  410. function inline_setlength : tnode;
  411. var
  412. paras : tnode;
  413. npara,
  414. ppn : tcallparanode;
  415. dims,
  416. counter : integer;
  417. isarray : boolean;
  418. def : tdef;
  419. destppn : tnode;
  420. newstatement : tstatementnode;
  421. temp : ttempcreatenode;
  422. newblock : tnode;
  423. begin
  424. { for easy exiting if something goes wrong }
  425. result := cerrornode.create;
  426. consume(_LKLAMMER);
  427. paras:=parse_paras(false,false,_RKLAMMER);
  428. consume(_RKLAMMER);
  429. if not assigned(paras) then
  430. begin
  431. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  432. exit;
  433. end;
  434. dims:=0;
  435. if assigned(paras) then
  436. begin
  437. { check type of lengths }
  438. ppn:=tcallparanode(paras);
  439. while assigned(ppn.right) do
  440. begin
  441. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  442. inserttypeconv(ppn.left,sinttype);
  443. inc(dims);
  444. ppn:=tcallparanode(ppn.right);
  445. end;
  446. end;
  447. if dims=0 then
  448. begin
  449. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  450. paras.free;
  451. exit;
  452. end;
  453. { last param must be var }
  454. destppn:=ppn.left;
  455. inc(parsing_para_level);
  456. valid_for_var(destppn,true);
  457. set_varstate(destppn,vs_written,[]);
  458. dec(parsing_para_level);
  459. { first param must be a string or dynamic array ...}
  460. isarray:=is_dynamic_array(destppn.resultdef);
  461. if not((destppn.resultdef.typ=stringdef) or
  462. isarray) then
  463. begin
  464. CGMessage(type_e_mismatch);
  465. paras.free;
  466. exit;
  467. end;
  468. { only dynamic arrays accept more dimensions }
  469. if (dims>1) then
  470. begin
  471. if (not isarray) then
  472. CGMessage(type_e_mismatch)
  473. else
  474. begin
  475. { check if the amount of dimensions is valid }
  476. def := tarraydef(destppn.resultdef).elementdef;
  477. counter:=dims;
  478. while counter > 1 do
  479. begin
  480. if not(is_dynamic_array(def)) then
  481. begin
  482. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  483. break;
  484. end;
  485. dec(counter);
  486. def := tarraydef(def).elementdef;
  487. end;
  488. end;
  489. end;
  490. if isarray then
  491. begin
  492. { create statements with call initialize the arguments and
  493. call fpc_dynarr_setlength }
  494. newblock:=internalstatements(newstatement);
  495. { get temp for array of lengths }
  496. temp := ctempcreatenode.create(sinttype,dims*sinttype.size,tt_persistent,false);
  497. addstatement(newstatement,temp);
  498. { load array of lengths }
  499. ppn:=tcallparanode(paras);
  500. counter:=0;
  501. while assigned(ppn.right) do
  502. begin
  503. addstatement(newstatement,cassignmentnode.create(
  504. ctemprefnode.create_offset(temp,counter*sinttype.size),
  505. ppn.left));
  506. ppn.left:=nil;
  507. inc(counter);
  508. ppn:=tcallparanode(ppn.right);
  509. end;
  510. { destppn is also reused }
  511. ppn.left:=nil;
  512. { create call to fpc_dynarr_setlength }
  513. npara:=ccallparanode.create(caddrnode.create_internal
  514. (ctemprefnode.create(temp)),
  515. ccallparanode.create(cordconstnode.create
  516. (counter,s32inttype,true),
  517. ccallparanode.create(caddrnode.create_internal
  518. (crttinode.create(tstoreddef(destppn.resultdef),initrtti)),
  519. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  520. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  521. addstatement(newstatement,ctempdeletenode.create(temp));
  522. { we don't need original the callparanodes tree }
  523. paras.free;
  524. end
  525. else
  526. begin
  527. { we can reuse the supplied parameters }
  528. newblock:=ccallnode.createintern(
  529. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  530. end;
  531. result.free;
  532. result:=newblock;
  533. end;
  534. function inline_initialize : tnode;
  535. var
  536. newblock,
  537. paras : tnode;
  538. ppn : tcallparanode;
  539. begin
  540. { for easy exiting if something goes wrong }
  541. result := cerrornode.create;
  542. consume(_LKLAMMER);
  543. paras:=parse_paras(false,false,_RKLAMMER);
  544. consume(_RKLAMMER);
  545. if not assigned(paras) then
  546. begin
  547. CGMessage1(parser_e_wrong_parameter_size,'Initialize');
  548. exit;
  549. end;
  550. ppn:=tcallparanode(paras);
  551. { 2 arguments? }
  552. if assigned(ppn.right) then
  553. begin
  554. CGMessage1(parser_e_wrong_parameter_size,'Initialize');
  555. paras.free;
  556. exit;
  557. end;
  558. newblock:=initialize_data_node(ppn.left);
  559. ppn.left:=nil;
  560. paras.free;
  561. result.free;
  562. result:=newblock;
  563. end;
  564. function inline_finalize : tnode;
  565. var
  566. newblock,
  567. paras : tnode;
  568. npara,
  569. destppn,
  570. ppn : tcallparanode;
  571. begin
  572. { for easy exiting if something goes wrong }
  573. result := cerrornode.create;
  574. consume(_LKLAMMER);
  575. paras:=parse_paras(false,false,_RKLAMMER);
  576. consume(_RKLAMMER);
  577. if not assigned(paras) then
  578. begin
  579. CGMessage1(parser_e_wrong_parameter_size,'Finalize');
  580. exit;
  581. end;
  582. ppn:=tcallparanode(paras);
  583. { 2 arguments? }
  584. if assigned(ppn.right) then
  585. begin
  586. destppn:=tcallparanode(ppn.right);
  587. { 3 arguments is invalid }
  588. if assigned(destppn.right) then
  589. begin
  590. CGMessage1(parser_e_wrong_parameter_size,'Finalize');
  591. paras.free;
  592. exit;
  593. end;
  594. { create call to fpc_finalize_array }
  595. npara:=ccallparanode.create(cordconstnode.create
  596. (destppn.left.resultdef.size,s32inttype,true),
  597. ccallparanode.create(ctypeconvnode.create
  598. (ppn.left,s32inttype),
  599. ccallparanode.create(caddrnode.create_internal
  600. (crttinode.create(tstoreddef(destppn.left.resultdef),initrtti)),
  601. ccallparanode.create(caddrnode.create_internal
  602. (destppn.left),nil))));
  603. newblock:=ccallnode.createintern('fpc_finalize_array',npara);
  604. destppn.left:=nil;
  605. ppn.left:=nil;
  606. end
  607. else
  608. begin
  609. newblock:=finalize_data_node(ppn.left);
  610. ppn.left:=nil;
  611. end;
  612. paras.free;
  613. result.free;
  614. result:=newblock;
  615. end;
  616. function inline_copy : tnode;
  617. var
  618. copynode,
  619. lowppn,
  620. highppn,
  621. npara,
  622. paras : tnode;
  623. ppn : tcallparanode;
  624. paradef : tdef;
  625. counter : integer;
  626. begin
  627. { for easy exiting if something goes wrong }
  628. result := cerrornode.create;
  629. consume(_LKLAMMER);
  630. paras:=parse_paras(false,false,_RKLAMMER);
  631. consume(_RKLAMMER);
  632. if not assigned(paras) then
  633. begin
  634. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  635. exit;
  636. end;
  637. { determine copy function to use based on the first argument,
  638. also count the number of arguments in this loop }
  639. counter:=1;
  640. ppn:=tcallparanode(paras);
  641. while assigned(ppn.right) do
  642. begin
  643. inc(counter);
  644. ppn:=tcallparanode(ppn.right);
  645. end;
  646. paradef:=ppn.left.resultdef;
  647. if is_ansistring(paradef) or
  648. (is_chararray(paradef) and
  649. (paradef.size>255)) or
  650. ((cs_ansistrings in current_settings.localswitches) and
  651. is_pchar(paradef)) then
  652. copynode:=ccallnode.createintern('fpc_ansistr_copy',paras)
  653. else
  654. if is_widestring(paradef) or
  655. is_widechararray(paradef) or
  656. is_pwidechar(paradef) then
  657. copynode:=ccallnode.createintern('fpc_widestr_copy',paras)
  658. else
  659. if is_char(paradef) then
  660. copynode:=ccallnode.createintern('fpc_char_copy',paras)
  661. else
  662. if is_dynamic_array(paradef) then
  663. begin
  664. { Only allow 1 or 3 arguments }
  665. if (counter<>1) and (counter<>3) then
  666. begin
  667. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  668. exit;
  669. end;
  670. { create statements with call }
  671. if (counter=3) then
  672. begin
  673. highppn:=tcallparanode(paras).left.getcopy;
  674. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  675. end
  676. else
  677. begin
  678. { use special -1,-1 argument to copy the whole array }
  679. highppn:=cordconstnode.create(-1,s32inttype,false);
  680. lowppn:=cordconstnode.create(-1,s32inttype,false);
  681. end;
  682. { create call to fpc_dynarray_copy }
  683. npara:=ccallparanode.create(highppn,
  684. ccallparanode.create(lowppn,
  685. ccallparanode.create(caddrnode.create_internal
  686. (crttinode.create(tstoreddef(ppn.left.resultdef),initrtti)),
  687. ccallparanode.create
  688. (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
  689. copynode:=ccallnode.createinternres('fpc_dynarray_copy',npara,ppn.left.resultdef);
  690. ppn.left:=nil;
  691. paras.free;
  692. end
  693. else
  694. begin
  695. { generic fallback that will give an error if a wrong
  696. type is passed }
  697. copynode:=ccallnode.createintern('fpc_shortstr_copy',paras)
  698. end;
  699. result.free;
  700. result:=copynode;
  701. end;
  702. end.