pinline.pas 28 KB

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