pinline.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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,
  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,rdt_normal)),
  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. valid_for_var(destppn,true);
  456. set_varstate(destppn,vs_written,[]);
  457. { first param must be a string or dynamic array ...}
  458. isarray:=is_dynamic_array(destppn.resultdef);
  459. if not((destppn.resultdef.typ=stringdef) or
  460. isarray) then
  461. begin
  462. CGMessage(type_e_mismatch);
  463. paras.free;
  464. exit;
  465. end;
  466. { only dynamic arrays accept more dimensions }
  467. if (dims>1) then
  468. begin
  469. if (not isarray) then
  470. CGMessage(type_e_mismatch)
  471. else
  472. begin
  473. { check if the amount of dimensions is valid }
  474. def := tarraydef(destppn.resultdef).elementdef;
  475. counter:=dims;
  476. while counter > 1 do
  477. begin
  478. if not(is_dynamic_array(def)) then
  479. begin
  480. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  481. break;
  482. end;
  483. dec(counter);
  484. def := tarraydef(def).elementdef;
  485. end;
  486. end;
  487. end;
  488. if isarray then
  489. begin
  490. { create statements with call initialize the arguments and
  491. call fpc_dynarr_setlength }
  492. newblock:=internalstatements(newstatement);
  493. { get temp for array of lengths }
  494. temp := ctempcreatenode.create(sinttype,dims*sinttype.size,tt_persistent,false);
  495. addstatement(newstatement,temp);
  496. { load array of lengths }
  497. ppn:=tcallparanode(paras);
  498. counter:=0;
  499. while assigned(ppn.right) do
  500. begin
  501. addstatement(newstatement,cassignmentnode.create(
  502. ctemprefnode.create_offset(temp,counter*sinttype.size),
  503. ppn.left));
  504. ppn.left:=nil;
  505. inc(counter);
  506. ppn:=tcallparanode(ppn.right);
  507. end;
  508. { destppn is also reused }
  509. ppn.left:=nil;
  510. { create call to fpc_dynarr_setlength }
  511. npara:=ccallparanode.create(caddrnode.create_internal
  512. (ctemprefnode.create(temp)),
  513. ccallparanode.create(cordconstnode.create
  514. (counter,s32inttype,true),
  515. ccallparanode.create(caddrnode.create_internal
  516. (crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  517. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  518. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  519. addstatement(newstatement,ctempdeletenode.create(temp));
  520. { we don't need original the callparanodes tree }
  521. paras.free;
  522. end
  523. else
  524. begin
  525. { we can reuse the supplied parameters }
  526. newblock:=ccallnode.createintern(
  527. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  528. end;
  529. result.free;
  530. result:=newblock;
  531. end;
  532. function inline_initialize : tnode;
  533. var
  534. newblock,
  535. paras : tnode;
  536. ppn : tcallparanode;
  537. begin
  538. { for easy exiting if something goes wrong }
  539. result := cerrornode.create;
  540. consume(_LKLAMMER);
  541. paras:=parse_paras(false,false,_RKLAMMER);
  542. consume(_RKLAMMER);
  543. if not assigned(paras) then
  544. begin
  545. CGMessage1(parser_e_wrong_parameter_size,'Initialize');
  546. exit;
  547. end;
  548. ppn:=tcallparanode(paras);
  549. { 2 arguments? }
  550. if assigned(ppn.right) then
  551. begin
  552. CGMessage1(parser_e_wrong_parameter_size,'Initialize');
  553. paras.free;
  554. exit;
  555. end;
  556. newblock:=initialize_data_node(ppn.left);
  557. ppn.left:=nil;
  558. paras.free;
  559. result.free;
  560. result:=newblock;
  561. end;
  562. function inline_finalize : tnode;
  563. var
  564. newblock,
  565. paras : tnode;
  566. npara,
  567. destppn,
  568. ppn : tcallparanode;
  569. begin
  570. { for easy exiting if something goes wrong }
  571. result := cerrornode.create;
  572. consume(_LKLAMMER);
  573. paras:=parse_paras(false,false,_RKLAMMER);
  574. consume(_RKLAMMER);
  575. if not assigned(paras) then
  576. begin
  577. CGMessage1(parser_e_wrong_parameter_size,'Finalize');
  578. exit;
  579. end;
  580. ppn:=tcallparanode(paras);
  581. { 2 arguments? }
  582. if assigned(ppn.right) then
  583. begin
  584. destppn:=tcallparanode(ppn.right);
  585. { 3 arguments is invalid }
  586. if assigned(destppn.right) then
  587. begin
  588. CGMessage1(parser_e_wrong_parameter_size,'Finalize');
  589. paras.free;
  590. exit;
  591. end;
  592. { create call to fpc_finalize_array }
  593. npara:=ccallparanode.create(cordconstnode.create
  594. (destppn.left.resultdef.size,s32inttype,true),
  595. ccallparanode.create(ctypeconvnode.create
  596. (ppn.left,s32inttype),
  597. ccallparanode.create(caddrnode.create_internal
  598. (crttinode.create(tstoreddef(destppn.left.resultdef),initrtti,rdt_normal)),
  599. ccallparanode.create(caddrnode.create_internal
  600. (destppn.left),nil))));
  601. newblock:=ccallnode.createintern('fpc_finalize_array',npara);
  602. destppn.left:=nil;
  603. ppn.left:=nil;
  604. end
  605. else
  606. begin
  607. newblock:=finalize_data_node(ppn.left);
  608. ppn.left:=nil;
  609. end;
  610. paras.free;
  611. result.free;
  612. result:=newblock;
  613. end;
  614. function inline_copy : tnode;
  615. var
  616. copynode,
  617. lowppn,
  618. highppn,
  619. npara,
  620. paras : tnode;
  621. ppn : tcallparanode;
  622. paradef : tdef;
  623. counter : integer;
  624. begin
  625. { for easy exiting if something goes wrong }
  626. result := cerrornode.create;
  627. consume(_LKLAMMER);
  628. paras:=parse_paras(false,false,_RKLAMMER);
  629. consume(_RKLAMMER);
  630. if not assigned(paras) then
  631. begin
  632. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  633. exit;
  634. end;
  635. { determine copy function to use based on the first argument,
  636. also count the number of arguments in this loop }
  637. counter:=1;
  638. ppn:=tcallparanode(paras);
  639. while assigned(ppn.right) do
  640. begin
  641. inc(counter);
  642. ppn:=tcallparanode(ppn.right);
  643. end;
  644. paradef:=ppn.left.resultdef;
  645. if is_ansistring(paradef) or
  646. (is_chararray(paradef) and
  647. (paradef.size>255)) or
  648. ((cs_ansistrings in current_settings.localswitches) and
  649. is_pchar(paradef)) then
  650. copynode:=ccallnode.createintern('fpc_ansistr_copy',paras)
  651. else
  652. if is_widestring(paradef) or
  653. is_widechararray(paradef) or
  654. is_pwidechar(paradef) then
  655. copynode:=ccallnode.createintern('fpc_widestr_copy',paras)
  656. else
  657. if is_char(paradef) then
  658. copynode:=ccallnode.createintern('fpc_char_copy',paras)
  659. else
  660. if is_dynamic_array(paradef) then
  661. begin
  662. { Only allow 1 or 3 arguments }
  663. if (counter<>1) and (counter<>3) then
  664. begin
  665. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  666. exit;
  667. end;
  668. { create statements with call }
  669. if (counter=3) then
  670. begin
  671. highppn:=tcallparanode(paras).left.getcopy;
  672. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  673. end
  674. else
  675. begin
  676. { use special -1,-1 argument to copy the whole array }
  677. highppn:=cordconstnode.create(int64(-1),s32inttype,false);
  678. lowppn:=cordconstnode.create(int64(-1),s32inttype,false);
  679. end;
  680. { create call to fpc_dynarray_copy }
  681. npara:=ccallparanode.create(highppn,
  682. ccallparanode.create(lowppn,
  683. ccallparanode.create(caddrnode.create_internal
  684. (crttinode.create(tstoreddef(ppn.left.resultdef),initrtti,rdt_normal)),
  685. ccallparanode.create
  686. (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
  687. copynode:=ccallnode.createinternres('fpc_dynarray_copy',npara,ppn.left.resultdef);
  688. ppn.left:=nil;
  689. paras.free;
  690. end
  691. else
  692. begin
  693. { generic fallback that will give an error if a wrong
  694. type is passed }
  695. copynode:=ccallnode.createintern('fpc_shortstr_copy',paras)
  696. end;
  697. result.free;
  698. result:=copynode;
  699. end;
  700. end.