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,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,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);
  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. CGMessage(type_e_mismatch);
  467. paras.free;
  468. exit;
  469. end;
  470. { only dynamic arrays accept more dimensions }
  471. if (dims>1) then
  472. begin
  473. if (not isarray) then
  474. CGMessage(type_e_mismatch)
  475. else
  476. begin
  477. { check if the amount of dimensions is valid }
  478. def := tarraydef(destppn.resultdef).elementdef;
  479. counter:=dims;
  480. while counter > 1 do
  481. begin
  482. if not(is_dynamic_array(def)) then
  483. begin
  484. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  485. break;
  486. end;
  487. dec(counter);
  488. def := tarraydef(def).elementdef;
  489. end;
  490. end;
  491. end;
  492. if isarray then
  493. begin
  494. { create statements with call initialize the arguments and
  495. call fpc_dynarr_setlength }
  496. newblock:=internalstatements(newstatement);
  497. { get temp for array of lengths }
  498. temp := ctempcreatenode.create(sinttype,dims*sinttype.size,tt_persistent,false);
  499. addstatement(newstatement,temp);
  500. { load array of lengths }
  501. ppn:=tcallparanode(paras);
  502. counter:=0;
  503. while assigned(ppn.right) do
  504. begin
  505. addstatement(newstatement,cassignmentnode.create(
  506. ctemprefnode.create_offset(temp,counter*sinttype.size),
  507. ppn.left));
  508. ppn.left:=nil;
  509. inc(counter);
  510. ppn:=tcallparanode(ppn.right);
  511. end;
  512. { destppn is also reused }
  513. ppn.left:=nil;
  514. { create call to fpc_dynarr_setlength }
  515. npara:=ccallparanode.create(caddrnode.create_internal
  516. (ctemprefnode.create(temp)),
  517. ccallparanode.create(cordconstnode.create
  518. (counter,s32inttype,true),
  519. ccallparanode.create(caddrnode.create_internal
  520. (crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  521. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  522. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  523. addstatement(newstatement,ctempdeletenode.create(temp));
  524. { we don't need original the callparanodes tree }
  525. paras.free;
  526. end
  527. else
  528. begin
  529. { we can reuse the supplied parameters }
  530. newblock:=ccallnode.createintern(
  531. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  532. end;
  533. result.free;
  534. result:=newblock;
  535. end;
  536. function inline_initfinal(isinit: boolean): tnode;
  537. var
  538. newblock,
  539. paras : tnode;
  540. npara,
  541. destppn,
  542. ppn : tcallparanode;
  543. begin
  544. { for easy exiting if something goes wrong }
  545. result := cerrornode.create;
  546. consume(_LKLAMMER);
  547. paras:=parse_paras(false,false,_RKLAMMER);
  548. consume(_RKLAMMER);
  549. ppn:=tcallparanode(paras);
  550. if not assigned(paras) or
  551. (assigned(ppn.right) and
  552. assigned(tcallparanode(ppn.right).right)) then
  553. begin
  554. if isinit then
  555. CGMessage1(parser_e_wrong_parameter_size,'Initialize')
  556. else
  557. CGMessage1(parser_e_wrong_parameter_size,'Finalize');
  558. exit;
  559. end;
  560. { 2 arguments? }
  561. if assigned(ppn.right) then
  562. begin
  563. destppn:=tcallparanode(ppn.right);
  564. { create call to fpc_initialize/finalize_array }
  565. npara:=ccallparanode.create(cordconstnode.create
  566. (destppn.left.resultdef.size,s32inttype,true),
  567. ccallparanode.create(ctypeconvnode.create
  568. (ppn.left,s32inttype),
  569. ccallparanode.create(caddrnode.create_internal
  570. (crttinode.create(tstoreddef(destppn.left.resultdef),initrtti,rdt_normal)),
  571. ccallparanode.create(caddrnode.create_internal
  572. (destppn.left),nil))));
  573. if isinit then
  574. newblock:=ccallnode.createintern('fpc_initialize_array',npara)
  575. else
  576. newblock:=ccallnode.createintern('fpc_finalize_array',npara);
  577. destppn.left:=nil;
  578. end
  579. else
  580. begin
  581. if isinit then
  582. newblock:=initialize_data_node(ppn.left)
  583. else
  584. newblock:=finalize_data_node(ppn.left);
  585. end;
  586. ppn.left:=nil;
  587. paras.free;
  588. result.free;
  589. result:=newblock;
  590. end;
  591. function inline_initialize : tnode;
  592. begin
  593. result:=inline_initfinal(true);
  594. end;
  595. function inline_finalize : tnode;
  596. begin
  597. result:=inline_initfinal(false);
  598. end;
  599. function inline_copy : tnode;
  600. var
  601. copynode,
  602. lowppn,
  603. highppn,
  604. npara,
  605. paras : tnode;
  606. ppn : tcallparanode;
  607. paradef : tdef;
  608. counter : integer;
  609. begin
  610. { for easy exiting if something goes wrong }
  611. result := cerrornode.create;
  612. consume(_LKLAMMER);
  613. paras:=parse_paras(false,false,_RKLAMMER);
  614. consume(_RKLAMMER);
  615. if not assigned(paras) then
  616. begin
  617. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  618. exit;
  619. end;
  620. { determine copy function to use based on the first argument,
  621. also count the number of arguments in this loop }
  622. counter:=1;
  623. ppn:=tcallparanode(paras);
  624. while assigned(ppn.right) do
  625. begin
  626. inc(counter);
  627. ppn:=tcallparanode(ppn.right);
  628. end;
  629. paradef:=ppn.left.resultdef;
  630. if is_ansistring(paradef) or
  631. (is_chararray(paradef) and
  632. (paradef.size>255)) or
  633. ((cs_ansistrings in current_settings.localswitches) and
  634. is_pchar(paradef)) then
  635. copynode:=ccallnode.createintern('fpc_ansistr_copy',paras)
  636. else
  637. if is_widestring(paradef) then
  638. copynode:=ccallnode.createintern('fpc_widestr_copy',paras)
  639. else
  640. if is_unicodestring(paradef) or
  641. is_widechararray(paradef) or
  642. is_pwidechar(paradef) then
  643. copynode:=ccallnode.createintern('fpc_unicodestr_copy',paras)
  644. else
  645. if is_char(paradef) then
  646. copynode:=ccallnode.createintern('fpc_char_copy',paras)
  647. else
  648. if is_dynamic_array(paradef) then
  649. begin
  650. { Only allow 1 or 3 arguments }
  651. if (counter<>1) and (counter<>3) then
  652. begin
  653. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  654. exit;
  655. end;
  656. { create statements with call }
  657. if (counter=3) then
  658. begin
  659. highppn:=tcallparanode(paras).left.getcopy;
  660. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  661. end
  662. else
  663. begin
  664. { use special -1,-1 argument to copy the whole array }
  665. highppn:=cordconstnode.create(int64(-1),s32inttype,false);
  666. lowppn:=cordconstnode.create(int64(-1),s32inttype,false);
  667. end;
  668. { create call to fpc_dynarray_copy }
  669. npara:=ccallparanode.create(highppn,
  670. ccallparanode.create(lowppn,
  671. ccallparanode.create(caddrnode.create_internal
  672. (crttinode.create(tstoreddef(ppn.left.resultdef),initrtti,rdt_normal)),
  673. ccallparanode.create
  674. (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
  675. copynode:=ccallnode.createinternres('fpc_dynarray_copy',npara,ppn.left.resultdef);
  676. ppn.left:=nil;
  677. paras.free;
  678. end
  679. else
  680. begin
  681. { generic fallback that will give an error if a wrong
  682. type is passed }
  683. if (counter=3) then
  684. copynode:=ccallnode.createintern('fpc_shortstr_copy',paras)
  685. else
  686. begin
  687. CGMessagePos(ppn.left.fileinfo,type_e_mismatch);
  688. copynode:=cerrornode.create;
  689. end
  690. end;
  691. result.free;
  692. result:=copynode;
  693. end;
  694. end.