pinline.pas 27 KB

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