pinline.pas 27 KB

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