2
0

pinline.pas 28 KB

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