pinline.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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,ngenutil,
  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. if target_info.system in systems_managed_vm then
  64. message(parser_e_feature_unsupported_for_vm);
  65. consume(_LKLAMMER);
  66. p:=comp_expr(true,false);
  67. { calc return type }
  68. if is_new then
  69. begin
  70. set_varstate(p,vs_written,[]);
  71. valid_for_var(p,true);
  72. end
  73. else
  74. set_varstate(p,vs_readwritten,[vsf_must_be_valid]);
  75. if (m_mac in current_settings.modeswitches) and
  76. is_class(p.resultdef) then
  77. begin
  78. classh:=tobjectdef(p.resultdef);
  79. { make sure we call ObjPas.TObject.Create/Free and not a random }
  80. { create/free method in a macpas descendent object (since those }
  81. { are not supposed to be called automatically when you call }
  82. { new/dispose) }
  83. while assigned(classh.childof) do
  84. classh := classh.childof;
  85. if is_new then
  86. begin
  87. sym:=search_struct_member(classh,'CREATE');
  88. p2 := cloadvmtaddrnode.create(ctypenode.create(p.resultdef));
  89. end
  90. else
  91. begin
  92. sym:=search_struct_member(classh,'FREE');
  93. p2 := p;
  94. end;
  95. if not(assigned(sym)) then
  96. begin
  97. p.free;
  98. if is_new then
  99. p2.free;
  100. new_dispose_statement := cerrornode.create;
  101. consume_all_until(_RKLAMMER);
  102. consume(_RKLAMMER);
  103. exit;
  104. end;
  105. do_member_read(classh,false,sym,p2,again,[]);
  106. { we need the real called method }
  107. do_typecheckpass(p2);
  108. if (p2.nodetype=calln) and
  109. assigned(tcallnode(p2).procdefinition) then
  110. begin
  111. if is_new then
  112. begin
  113. if (tcallnode(p2).procdefinition.proctypeoption<>potype_constructor) then
  114. Message(parser_e_expr_have_to_be_constructor_call);
  115. p2.resultdef:=p.resultdef;
  116. p2:=cassignmentnode.create(p,p2);
  117. typecheckpass(p2);
  118. end
  119. else
  120. begin
  121. { Free is not a destructor
  122. if (tcallnode(p2).procdefinition.proctypeoption<>potype_destructor) then
  123. Message(parser_e_expr_have_to_be_destructor_call);
  124. }
  125. end
  126. end
  127. else
  128. internalerror(2005061202);
  129. new_dispose_statement := p2;
  130. end
  131. { constructor,destructor specified }
  132. else if not(m_mac in current_settings.modeswitches) and
  133. try_to_consume(_COMMA) then
  134. begin
  135. { extended syntax of new and dispose }
  136. { function styled new is handled in factor }
  137. { destructors have no parameters }
  138. destructorname:=pattern;
  139. destructorpos:=current_tokenpos;
  140. consume(_ID);
  141. if (p.resultdef.typ<>pointerdef) then
  142. begin
  143. Message1(type_e_pointer_type_expected,p.resultdef.typename);
  144. p.free;
  145. p:=factor(false,false);
  146. p.free;
  147. consume(_RKLAMMER);
  148. new_dispose_statement:=cerrornode.create;
  149. exit;
  150. end;
  151. { first parameter must be an object or class }
  152. if tpointerdef(p.resultdef).pointeddef.typ<>objectdef then
  153. begin
  154. Message(parser_e_pointer_to_class_expected);
  155. p.free;
  156. new_dispose_statement:=factor(false,false);
  157. consume_all_until(_RKLAMMER);
  158. consume(_RKLAMMER);
  159. exit;
  160. end;
  161. { check, if the first parameter is a pointer to a _class_ }
  162. classh:=tobjectdef(tpointerdef(p.resultdef).pointeddef);
  163. if is_class(classh) then
  164. begin
  165. Message(parser_e_no_new_or_dispose_for_classes);
  166. new_dispose_statement:=factor(false,false);
  167. consume_all_until(_RKLAMMER);
  168. consume(_RKLAMMER);
  169. exit;
  170. end;
  171. { search cons-/destructor, also in parent classes }
  172. storepos:=current_tokenpos;
  173. current_tokenpos:=destructorpos;
  174. sym:=search_struct_member(classh,destructorname);
  175. current_tokenpos:=storepos;
  176. { the second parameter of new/dispose must be a call }
  177. { to a cons-/destructor }
  178. if (not assigned(sym)) or (sym.typ<>procsym) then
  179. begin
  180. if is_new then
  181. Message(parser_e_expr_have_to_be_constructor_call)
  182. else
  183. Message(parser_e_expr_have_to_be_destructor_call);
  184. p.free;
  185. new_dispose_statement:=cerrornode.create;
  186. end
  187. else
  188. begin
  189. { For new(var,constructor) we need to take a copy because
  190. p is also used in the assignmentn below }
  191. if is_new then
  192. begin
  193. p2:=cderefnode.create(p.getcopy);
  194. include(p2.flags,nf_no_checkpointer);
  195. end
  196. else
  197. p2:=cderefnode.create(p);
  198. do_typecheckpass(p2);
  199. if is_new then
  200. callflag:=cnf_new_call
  201. else
  202. callflag:=cnf_dispose_call;
  203. if is_new then
  204. do_member_read(classh,false,sym,p2,again,[callflag])
  205. else
  206. begin
  207. if not(m_fpc in current_settings.modeswitches) then
  208. do_member_read(classh,false,sym,p2,again,[callflag])
  209. else
  210. begin
  211. p2:=ccallnode.create(nil,tprocsym(sym),sym.owner,p2,[callflag]);
  212. { support dispose(p,done()); }
  213. if try_to_consume(_LKLAMMER) then
  214. begin
  215. if not try_to_consume(_RKLAMMER) then
  216. begin
  217. Message(parser_e_no_paras_for_destructor);
  218. consume_all_until(_RKLAMMER);
  219. consume(_RKLAMMER);
  220. end;
  221. end;
  222. end;
  223. end;
  224. { we need the real called method }
  225. do_typecheckpass(p2);
  226. if (p2.nodetype=calln) and
  227. assigned(tcallnode(p2).procdefinition) then
  228. begin
  229. if is_new then
  230. begin
  231. if (tcallnode(p2).procdefinition.proctypeoption<>potype_constructor) then
  232. Message(parser_e_expr_have_to_be_constructor_call);
  233. p2.resultdef:=p.resultdef;
  234. p2:=cassignmentnode.create(p,p2);
  235. end
  236. else
  237. begin
  238. if (tcallnode(p2).procdefinition.proctypeoption<>potype_destructor) then
  239. Message(parser_e_expr_have_to_be_destructor_call);
  240. end;
  241. end
  242. else
  243. begin
  244. if is_new then
  245. CGMessage(parser_e_expr_have_to_be_constructor_call)
  246. else
  247. CGMessage(parser_e_expr_have_to_be_destructor_call);
  248. end;
  249. result:=p2;
  250. end;
  251. end
  252. else
  253. begin
  254. if (p.resultdef.typ<>pointerdef) then
  255. Begin
  256. Message1(type_e_pointer_type_expected,p.resultdef.typename);
  257. new_dispose_statement:=cerrornode.create;
  258. end
  259. else
  260. begin
  261. if (tpointerdef(p.resultdef).pointeddef.typ=objectdef) and
  262. (oo_has_vmt in tobjectdef(tpointerdef(p.resultdef).pointeddef).objectoptions) then
  263. Message(parser_w_use_extended_syntax_for_objects);
  264. if (tpointerdef(p.resultdef).pointeddef.typ=orddef) and
  265. (torddef(tpointerdef(p.resultdef).pointeddef).ordtype=uvoid) then
  266. begin
  267. if (m_tp7 in current_settings.modeswitches) or
  268. (m_delphi in current_settings.modeswitches) then
  269. Message(parser_w_no_new_dispose_on_void_pointers)
  270. else
  271. Message(parser_e_no_new_dispose_on_void_pointers);
  272. end;
  273. { create statements with call to getmem+initialize or
  274. finalize+freemem }
  275. new_dispose_statement:=internalstatements(newstatement);
  276. if is_new then
  277. begin
  278. { create temp for result }
  279. temp := ctempcreatenode.create(p.resultdef,p.resultdef.size,tt_persistent,true);
  280. addstatement(newstatement,temp);
  281. { create call to fpc_getmem }
  282. para := ccallparanode.create(cordconstnode.create
  283. (tpointerdef(p.resultdef).pointeddef.size,s32inttype,true),nil);
  284. addstatement(newstatement,cassignmentnode.create(
  285. ctemprefnode.create(temp),
  286. ccallnode.createintern('fpc_getmem',para)));
  287. { create call to fpc_initialize }
  288. if is_managed_type(tpointerdef(p.resultdef).pointeddef) or
  289. ((m_iso in current_settings.modeswitches) and (tpointerdef(p.resultdef).pointeddef.typ=filedef)) then
  290. addstatement(newstatement,cnodeutils.initialize_data_node(cderefnode.create(ctemprefnode.create(temp)),false));
  291. { copy the temp to the destination }
  292. addstatement(newstatement,cassignmentnode.create(
  293. p,
  294. ctemprefnode.create(temp)));
  295. { release temp }
  296. addstatement(newstatement,ctempdeletenode.create(temp));
  297. end
  298. else
  299. begin
  300. { create call to fpc_finalize }
  301. if is_managed_type(tpointerdef(p.resultdef).pointeddef) then
  302. addstatement(newstatement,cnodeutils.finalize_data_node(cderefnode.create(p.getcopy)));
  303. { create call to fpc_freemem }
  304. para := ccallparanode.create(p,nil);
  305. addstatement(newstatement,ccallnode.createintern('fpc_freemem',para));
  306. end;
  307. end;
  308. end;
  309. consume(_RKLAMMER);
  310. end;
  311. function new_function : tnode;
  312. var
  313. newstatement : tstatementnode;
  314. newblock : tblocknode;
  315. temp : ttempcreatenode;
  316. para : tcallparanode;
  317. p1,p2 : tnode;
  318. classh : tobjectdef;
  319. srsym : tsym;
  320. srsymtable : TSymtable;
  321. again : boolean; { dummy for do_proc_call }
  322. begin
  323. if target_info.system in systems_managed_vm then
  324. message(parser_e_feature_unsupported_for_vm);
  325. consume(_LKLAMMER);
  326. p1:=factor(false,false);
  327. if p1.nodetype<>typen then
  328. begin
  329. Message(type_e_type_id_expected);
  330. consume_all_until(_RKLAMMER);
  331. consume(_RKLAMMER);
  332. p1.destroy;
  333. new_function:=cerrornode.create;
  334. exit;
  335. end;
  336. if (p1.resultdef.typ<>pointerdef) then
  337. begin
  338. Message1(type_e_pointer_type_expected,p1.resultdef.typename);
  339. consume_all_until(_RKLAMMER);
  340. consume(_RKLAMMER);
  341. p1.destroy;
  342. new_function:=cerrornode.create;
  343. exit;
  344. end;
  345. if try_to_consume(_RKLAMMER) then
  346. begin
  347. if (tpointerdef(p1.resultdef).pointeddef.typ=objectdef) and
  348. (oo_has_vmt in tobjectdef(tpointerdef(p1.resultdef).pointeddef).objectoptions) then
  349. Message(parser_w_use_extended_syntax_for_objects);
  350. { create statements with call to getmem+initialize }
  351. newblock:=internalstatements(newstatement);
  352. { create temp for result }
  353. temp := ctempcreatenode.create(p1.resultdef,p1.resultdef.size,tt_persistent,true);
  354. addstatement(newstatement,temp);
  355. { create call to fpc_getmem }
  356. para := ccallparanode.create(cordconstnode.create
  357. (tpointerdef(p1.resultdef).pointeddef.size,s32inttype,true),nil);
  358. addstatement(newstatement,cassignmentnode.create(
  359. ctemprefnode.create(temp),
  360. ccallnode.createintern('fpc_getmem',para)));
  361. { create call to fpc_initialize }
  362. if is_managed_type(tpointerdef(p1.resultdef).pointeddef) then
  363. begin
  364. para := ccallparanode.create(caddrnode.create_internal(crttinode.create
  365. (tstoreddef(tpointerdef(p1.resultdef).pointeddef),initrtti,rdt_normal)),
  366. ccallparanode.create(ctemprefnode.create
  367. (temp),nil));
  368. addstatement(newstatement,ccallnode.createintern('fpc_initialize',para));
  369. end;
  370. { the last statement should return the value as
  371. location and type, this is done be referencing the
  372. temp and converting it first from a persistent temp to
  373. normal temp }
  374. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  375. addstatement(newstatement,ctemprefnode.create(temp));
  376. p1.destroy;
  377. p1:=newblock;
  378. end
  379. else
  380. begin
  381. consume(_COMMA);
  382. if tpointerdef(p1.resultdef).pointeddef.typ<>objectdef then
  383. begin
  384. Message(parser_e_pointer_to_class_expected);
  385. consume_all_until(_RKLAMMER);
  386. consume(_RKLAMMER);
  387. p1.destroy;
  388. new_function:=cerrornode.create;
  389. exit;
  390. end;
  391. classh:=tobjectdef(tpointerdef(p1.resultdef).pointeddef);
  392. { use the objectdef for loading the VMT }
  393. p2:=p1;
  394. p1:=ctypenode.create(tpointerdef(p1.resultdef).pointeddef);
  395. do_typecheckpass(p1);
  396. { search the constructor also in the symbol tables of
  397. the parents }
  398. afterassignment:=false;
  399. searchsym_in_class(classh,classh,pattern,srsym,srsymtable,true);
  400. consume(_ID);
  401. do_member_read(classh,false,srsym,p1,again,[cnf_new_call]);
  402. { we need to know which procedure is called }
  403. do_typecheckpass(p1);
  404. if not(
  405. (p1.nodetype=calln) and
  406. assigned(tcallnode(p1).procdefinition) and
  407. (tcallnode(p1).procdefinition.proctypeoption=potype_constructor)
  408. ) then
  409. Message(parser_e_expr_have_to_be_constructor_call);
  410. { constructors return boolean, update resultdef to return
  411. the pointer to the object }
  412. p1.resultdef:=p2.resultdef;
  413. p2.free;
  414. consume(_RKLAMMER);
  415. end;
  416. new_function:=p1;
  417. end;
  418. function inline_setlength : tnode;
  419. var
  420. paras: tnode;
  421. begin
  422. consume(_LKLAMMER);
  423. paras:=parse_paras(false,false,_RKLAMMER);
  424. consume(_RKLAMMER);
  425. if not assigned(paras) then
  426. begin
  427. result:=cerrornode.create;
  428. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  429. exit;
  430. end;
  431. result:=cinlinenode.create(in_setlength_x,false,paras);
  432. end;
  433. function inline_initfinal(isinit: boolean): tnode;
  434. var
  435. newblock,
  436. paras : tnode;
  437. npara,
  438. destppn,
  439. ppn : tcallparanode;
  440. begin
  441. { for easy exiting if something goes wrong }
  442. result := cerrornode.create;
  443. consume(_LKLAMMER);
  444. paras:=parse_paras(false,false,_RKLAMMER);
  445. consume(_RKLAMMER);
  446. ppn:=tcallparanode(paras);
  447. if not assigned(paras) or
  448. (assigned(ppn.right) and
  449. assigned(tcallparanode(ppn.right).right)) then
  450. begin
  451. if isinit then
  452. CGMessage1(parser_e_wrong_parameter_size,'Initialize')
  453. else
  454. CGMessage1(parser_e_wrong_parameter_size,'Finalize');
  455. exit;
  456. end;
  457. { 2 arguments? }
  458. if assigned(ppn.right) then
  459. begin
  460. destppn:=tcallparanode(ppn.right);
  461. { create call to fpc_initialize/finalize_array }
  462. npara:=ccallparanode.create(ctypeconvnode.create
  463. (ppn.left,s32inttype),
  464. ccallparanode.create(caddrnode.create_internal
  465. (crttinode.create(tstoreddef(destppn.left.resultdef),initrtti,rdt_normal)),
  466. ccallparanode.create(caddrnode.create_internal
  467. (destppn.left),nil)));
  468. if isinit then
  469. newblock:=ccallnode.createintern('fpc_initialize_array',npara)
  470. else
  471. newblock:=ccallnode.createintern('fpc_finalize_array',npara);
  472. destppn.left:=nil;
  473. end
  474. else
  475. begin
  476. if isinit then
  477. newblock:=cnodeutils.initialize_data_node(ppn.left,true)
  478. else
  479. newblock:=cnodeutils.finalize_data_node(ppn.left);
  480. end;
  481. ppn.left:=nil;
  482. paras.free;
  483. result.free;
  484. result:=newblock;
  485. end;
  486. function inline_initialize : tnode;
  487. begin
  488. result:=inline_initfinal(true);
  489. end;
  490. function inline_finalize : tnode;
  491. begin
  492. result:=inline_initfinal(false);
  493. end;
  494. function inline_copy : tnode;
  495. var
  496. paras : tnode;
  497. { for easy exiting if something goes wrong }
  498. begin
  499. result := cerrornode.create;
  500. consume(_LKLAMMER);
  501. paras:=parse_paras(false,false,_RKLAMMER);
  502. consume(_RKLAMMER);
  503. if not assigned(paras) then
  504. begin
  505. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  506. exit;
  507. end;
  508. result.free;
  509. result:=cinlinenode.create(in_copy_x,false,paras);
  510. end;
  511. end.