pinline.pas 27 KB

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