nmem.pas 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for memory related nodes
  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 nmem;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. symdef,symsym,symtable,symtype;
  23. type
  24. tloadvmtaddrnode = class(tunarynode)
  25. constructor create(l : tnode);virtual;
  26. function pass_1 : tnode;override;
  27. function pass_typecheck:tnode;override;
  28. end;
  29. tloadvmtaddrnodeclass = class of tloadvmtaddrnode;
  30. tloadparentfpnode = class(tunarynode)
  31. parentpd : tprocdef;
  32. parentpdderef : tderef;
  33. constructor create(pd:tprocdef);virtual;
  34. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  35. procedure ppuwrite(ppufile:tcompilerppufile);override;
  36. procedure buildderefimpl;override;
  37. procedure derefimpl;override;
  38. function pass_1 : tnode;override;
  39. function pass_typecheck:tnode;override;
  40. function docompare(p: tnode): boolean; override;
  41. function dogetcopy : tnode;override;
  42. end;
  43. tloadparentfpnodeclass = class of tloadparentfpnode;
  44. taddrnode = class(tunarynode)
  45. getprocvardef : tprocvardef;
  46. getprocvardefderef : tderef;
  47. constructor create(l : tnode);virtual;
  48. constructor create_internal(l : tnode); virtual;
  49. constructor create_internal_nomark(l : tnode); virtual;
  50. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  51. procedure ppuwrite(ppufile:tcompilerppufile);override;
  52. procedure mark_write;override;
  53. procedure buildderefimpl;override;
  54. procedure derefimpl;override;
  55. function docompare(p: tnode): boolean; override;
  56. function dogetcopy : tnode;override;
  57. function pass_1 : tnode;override;
  58. function pass_typecheck:tnode;override;
  59. private
  60. mark_read_written: boolean;
  61. end;
  62. taddrnodeclass = class of taddrnode;
  63. tderefnode = class(tunarynode)
  64. constructor create(l : tnode);virtual;
  65. function pass_1 : tnode;override;
  66. function pass_typecheck:tnode;override;
  67. procedure mark_write;override;
  68. end;
  69. tderefnodeclass = class of tderefnode;
  70. tsubscriptnode = class(tunarynode)
  71. vs : tfieldvarsym;
  72. vsderef : tderef;
  73. constructor create(varsym : tsym;l : tnode);virtual;
  74. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  75. procedure ppuwrite(ppufile:tcompilerppufile);override;
  76. procedure buildderefimpl;override;
  77. procedure derefimpl;override;
  78. function dogetcopy : tnode;override;
  79. function pass_1 : tnode;override;
  80. function docompare(p: tnode): boolean; override;
  81. function pass_typecheck:tnode;override;
  82. procedure mark_write;override;
  83. end;
  84. tsubscriptnodeclass = class of tsubscriptnode;
  85. tvecnode = class(tbinarynode)
  86. constructor create(l,r : tnode);virtual;
  87. function pass_1 : tnode;override;
  88. function pass_typecheck:tnode;override;
  89. procedure mark_write;override;
  90. end;
  91. tvecnodeclass = class of tvecnode;
  92. twithnode = class(tunarynode)
  93. constructor create(l:tnode);
  94. destructor destroy;override;
  95. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  96. procedure ppuwrite(ppufile:tcompilerppufile);override;
  97. function dogetcopy : tnode;override;
  98. function pass_1 : tnode;override;
  99. function docompare(p: tnode): boolean; override;
  100. function pass_typecheck:tnode;override;
  101. end;
  102. twithnodeclass = class of twithnode;
  103. var
  104. cloadvmtaddrnode : tloadvmtaddrnodeclass= tloadvmtaddrnode;
  105. caddrnode : taddrnodeclass= taddrnode;
  106. cderefnode : tderefnodeclass= tderefnode;
  107. csubscriptnode : tsubscriptnodeclass= tsubscriptnode;
  108. cvecnode : tvecnodeclass= tvecnode;
  109. cwithnode : twithnodeclass= twithnode;
  110. cloadparentfpnode : tloadparentfpnodeclass = tloadparentfpnode;
  111. function is_big_untyped_addrnode(p: tnode): boolean;
  112. implementation
  113. uses
  114. globtype,systems,constexp,
  115. cutils,verbose,globals,
  116. symconst,symbase,defutil,defcmp,
  117. nbas,nutils,
  118. wpobase,
  119. htypechk,pass_1,ncal,nld,ncon,ncnv,cgbase,procinfo
  120. ;
  121. {*****************************************************************************
  122. TLOADVMTADDRNODE
  123. *****************************************************************************}
  124. constructor tloadvmtaddrnode.create(l : tnode);
  125. begin
  126. inherited create(loadvmtaddrn,l);
  127. end;
  128. function tloadvmtaddrnode.pass_typecheck:tnode;
  129. var
  130. defaultresultdef : boolean;
  131. begin
  132. result:=nil;
  133. typecheckpass(left);
  134. if codegenerror then
  135. exit;
  136. case left.resultdef.typ of
  137. classrefdef :
  138. resultdef:=left.resultdef;
  139. objectdef,
  140. recorddef:
  141. { access to the classtype while specializing? }
  142. if (df_generic in left.resultdef.defoptions) then
  143. begin
  144. defaultresultdef:=true;
  145. if assigned(current_objectdef) then
  146. begin
  147. if assigned(current_objectdef.genericdef) then
  148. if current_objectdef.genericdef=left.resultdef then
  149. begin
  150. resultdef:=tclassrefdef.create(current_objectdef);
  151. defaultresultdef:=false;
  152. end
  153. else
  154. message(parser_e_cant_create_generics_of_this_type);
  155. end
  156. else
  157. message(parser_e_cant_create_generics_of_this_type);
  158. if defaultresultdef then
  159. resultdef:=tclassrefdef.create(left.resultdef);
  160. end
  161. else
  162. resultdef:=tclassrefdef.create(left.resultdef);
  163. else
  164. Message(parser_e_pointer_to_class_expected);
  165. end;
  166. end;
  167. function tloadvmtaddrnode.pass_1 : tnode;
  168. var
  169. vs: tsym;
  170. begin
  171. result:=nil;
  172. expectloc:=LOC_REGISTER;
  173. if left.nodetype<>typen then
  174. begin
  175. { make sure that the isa field is loaded correctly in case
  176. of the non-fragile ABI }
  177. if is_objcclass(left.resultdef) and
  178. (left.nodetype<>typen) then
  179. begin
  180. vs:=search_struct_member(tobjectdef(left.resultdef),'ISA');
  181. if not assigned(vs) or
  182. (tsym(vs).typ<>fieldvarsym) then
  183. internalerror(2009092502);
  184. result:=csubscriptnode.create(tfieldvarsym(vs),left);
  185. inserttypeconv_internal(result,resultdef);
  186. { reused }
  187. left:=nil;
  188. end
  189. else
  190. firstpass(left)
  191. end
  192. else if not is_objcclass(left.resultdef) and
  193. not is_objcclassref(left.resultdef) then
  194. begin
  195. if not(nf_ignore_for_wpo in flags) and
  196. (not assigned(current_procinfo) or
  197. (po_inline in current_procinfo.procdef.procoptions) or
  198. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  199. begin
  200. { keep track of which classes might be instantiated via a classrefdef }
  201. if (left.resultdef.typ=classrefdef) then
  202. tobjectdef(tclassrefdef(left.resultdef).pointeddef).register_maybe_created_object_type
  203. else if (left.resultdef.typ=objectdef) then
  204. tobjectdef(left.resultdef).register_maybe_created_object_type
  205. end
  206. end;
  207. end;
  208. {*****************************************************************************
  209. TLOADPARENTFPNODE
  210. *****************************************************************************}
  211. constructor tloadparentfpnode.create(pd:tprocdef);
  212. begin
  213. inherited create(loadparentfpn,nil);
  214. if not assigned(pd) then
  215. internalerror(200309288);
  216. if (pd.parast.symtablelevel>current_procinfo.procdef.parast.symtablelevel) then
  217. internalerror(200309284);
  218. parentpd:=pd;
  219. end;
  220. constructor tloadparentfpnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  221. begin
  222. inherited ppuload(t,ppufile);
  223. ppufile.getderef(parentpdderef);
  224. end;
  225. procedure tloadparentfpnode.ppuwrite(ppufile:tcompilerppufile);
  226. begin
  227. inherited ppuwrite(ppufile);
  228. ppufile.putderef(parentpdderef);
  229. end;
  230. procedure tloadparentfpnode.buildderefimpl;
  231. begin
  232. inherited buildderefimpl;
  233. parentpdderef.build(parentpd);
  234. end;
  235. procedure tloadparentfpnode.derefimpl;
  236. begin
  237. inherited derefimpl;
  238. parentpd:=tprocdef(parentpdderef.resolve);
  239. end;
  240. function tloadparentfpnode.docompare(p: tnode): boolean;
  241. begin
  242. result:=
  243. inherited docompare(p) and
  244. (tloadparentfpnode(p).parentpd=parentpd);
  245. end;
  246. function tloadparentfpnode.dogetcopy : tnode;
  247. var
  248. p : tloadparentfpnode;
  249. begin
  250. p:=tloadparentfpnode(inherited dogetcopy);
  251. p.parentpd:=parentpd;
  252. dogetcopy:=p;
  253. end;
  254. function tloadparentfpnode.pass_typecheck:tnode;
  255. {$ifdef dummy}
  256. var
  257. currpi : tprocinfo;
  258. hsym : tparavarsym;
  259. {$endif dummy}
  260. begin
  261. result:=nil;
  262. resultdef:=voidpointertype;
  263. {$ifdef dummy}
  264. { currently parentfps are never loaded in registers (FK) }
  265. if (current_procinfo.procdef.parast.symtablelevel<>parentpd.parast.symtablelevel) then
  266. begin
  267. currpi:=current_procinfo;
  268. { walk parents }
  269. while (currpi.procdef.owner.symtablelevel>parentpd.parast.symtablelevel) do
  270. begin
  271. currpi:=currpi.parent;
  272. if not assigned(currpi) then
  273. internalerror(2005040602);
  274. hsym:=tparavarsym(currpi.procdef.parast.Find('parentfp'));
  275. if not assigned(hsym) then
  276. internalerror(2005040601);
  277. hsym.varregable:=vr_none;
  278. end;
  279. end;
  280. {$endif dummy}
  281. end;
  282. function tloadparentfpnode.pass_1 : tnode;
  283. begin
  284. result:=nil;
  285. expectloc:=LOC_REGISTER;
  286. end;
  287. {*****************************************************************************
  288. TADDRNODE
  289. *****************************************************************************}
  290. constructor taddrnode.create(l : tnode);
  291. begin
  292. inherited create(addrn,l);
  293. getprocvardef:=nil;
  294. mark_read_written := true;
  295. end;
  296. constructor taddrnode.create_internal(l : tnode);
  297. begin
  298. self.create(l);
  299. include(flags,nf_internal);
  300. end;
  301. constructor taddrnode.create_internal_nomark(l : tnode);
  302. begin
  303. self.create_internal(l);
  304. mark_read_written := false;
  305. end;
  306. constructor taddrnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  307. begin
  308. inherited ppuload(t,ppufile);
  309. ppufile.getderef(getprocvardefderef);
  310. end;
  311. procedure taddrnode.ppuwrite(ppufile:tcompilerppufile);
  312. begin
  313. inherited ppuwrite(ppufile);
  314. ppufile.putderef(getprocvardefderef);
  315. end;
  316. procedure Taddrnode.mark_write;
  317. begin
  318. {@procvar:=nil is legal in Delphi mode.}
  319. left.mark_write;
  320. end;
  321. procedure taddrnode.buildderefimpl;
  322. begin
  323. inherited buildderefimpl;
  324. getprocvardefderef.build(getprocvardef);
  325. end;
  326. procedure taddrnode.derefimpl;
  327. begin
  328. inherited derefimpl;
  329. getprocvardef:=tprocvardef(getprocvardefderef.resolve);
  330. end;
  331. function taddrnode.docompare(p: tnode): boolean;
  332. begin
  333. result:=
  334. inherited docompare(p) and
  335. (taddrnode(p).getprocvardef=getprocvardef);
  336. end;
  337. function taddrnode.dogetcopy : tnode;
  338. var
  339. p : taddrnode;
  340. begin
  341. p:=taddrnode(inherited dogetcopy);
  342. p.getprocvardef:=getprocvardef;
  343. dogetcopy:=p;
  344. end;
  345. function taddrnode.pass_typecheck:tnode;
  346. var
  347. hp : tnode;
  348. hsym : tfieldvarsym;
  349. isprocvar : boolean;
  350. begin
  351. result:=nil;
  352. typecheckpass(left);
  353. if codegenerror then
  354. exit;
  355. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  356. { don't allow constants, for internal use we also
  357. allow taking the address of strings }
  358. if is_constnode(left) and
  359. not(
  360. (nf_internal in flags) and
  361. (left.nodetype in [stringconstn])
  362. ) then
  363. begin
  364. CGMessagePos(left.fileinfo,type_e_no_addr_of_constant);
  365. exit;
  366. end;
  367. { Handle @proc special, also @procvar in tp-mode needs
  368. special handling }
  369. if (left.resultdef.typ=procdef) or
  370. (
  371. (left.resultdef.typ=procvardef) and
  372. ((m_tp_procvar in current_settings.modeswitches) or
  373. (m_mac_procvar in current_settings.modeswitches))
  374. ) then
  375. begin
  376. isprocvar:=(left.resultdef.typ=procvardef);
  377. if not isprocvar then
  378. begin
  379. left:=ctypeconvnode.create_proc_to_procvar(left);
  380. left.fileinfo:=fileinfo;
  381. typecheckpass(left);
  382. end;
  383. { In tp procvar mode the result is always a voidpointer. Insert
  384. a typeconversion to voidpointer. For methodpointers we need
  385. to load the proc field }
  386. if (m_tp_procvar in current_settings.modeswitches) or
  387. (m_mac_procvar in current_settings.modeswitches) then
  388. begin
  389. if tabstractprocdef(left.resultdef).is_addressonly then
  390. begin
  391. result:=ctypeconvnode.create_internal(left,voidpointertype);
  392. include(result.flags,nf_load_procvar);
  393. left:=nil;
  394. end
  395. else
  396. begin
  397. { For procvars and for nested routines we need to return
  398. the proc field of the methodpointer }
  399. if isprocvar or
  400. is_nested_pd(tabstractprocdef(left.resultdef)) then
  401. begin
  402. { find proc field in methodpointer record }
  403. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  404. if not assigned(hsym) then
  405. internalerror(200412041);
  406. { Load tmehodpointer(left).proc }
  407. result:=csubscriptnode.create(
  408. hsym,
  409. ctypeconvnode.create_internal(left,methodpointertype));
  410. left:=nil;
  411. end
  412. else
  413. CGMessage(type_e_variable_id_expected);
  414. end;
  415. end
  416. else
  417. begin
  418. { Return the typeconvn only }
  419. result:=left;
  420. left:=nil;
  421. end;
  422. end
  423. else
  424. begin
  425. { what are we getting the address from an absolute sym? }
  426. hp:=left;
  427. while assigned(hp) and (hp.nodetype in [typeconvn,vecn,derefn,subscriptn]) do
  428. hp:=tunarynode(hp).left;
  429. if not assigned(hp) then
  430. internalerror(200412042);
  431. {$ifdef i386}
  432. if (hp.nodetype=loadn) and
  433. ((tloadnode(hp).symtableentry.typ=absolutevarsym) and
  434. tabsolutevarsym(tloadnode(hp).symtableentry).absseg) then
  435. begin
  436. if not(nf_typedaddr in flags) then
  437. resultdef:=voidfarpointertype
  438. else
  439. resultdef:=tpointerdef.createfar(left.resultdef);
  440. end
  441. else
  442. {$endif i386}
  443. if (hp.nodetype=loadn) and
  444. (tloadnode(hp).symtableentry.typ=absolutevarsym) and
  445. {$ifdef i386}
  446. not(tabsolutevarsym(tloadnode(hp).symtableentry).absseg) and
  447. {$endif i386}
  448. (tabsolutevarsym(tloadnode(hp).symtableentry).abstyp=toaddr) then
  449. begin
  450. if nf_typedaddr in flags then
  451. result:=cpointerconstnode.create(tabsolutevarsym(tloadnode(hp).symtableentry).addroffset,tpointerdef.create(left.resultdef))
  452. else
  453. result:=cpointerconstnode.create(tabsolutevarsym(tloadnode(hp).symtableentry).addroffset,voidpointertype);
  454. exit;
  455. end
  456. else if (nf_internal in flags) or
  457. valid_for_addr(left,true) then
  458. begin
  459. if not(nf_typedaddr in flags) then
  460. resultdef:=voidpointertype
  461. else
  462. resultdef:=tpointerdef.create(left.resultdef);
  463. end
  464. else
  465. CGMessage(type_e_variable_id_expected);
  466. end;
  467. if mark_read_written then
  468. begin
  469. { This is actually only "read", but treat it nevertheless as }
  470. { modified due to the possible use of pointers }
  471. { To avoid false positives regarding "uninitialised" }
  472. { warnings when using arrays, perform it in two steps }
  473. set_varstate(left,vs_written,[]);
  474. { vsf_must_be_valid so it doesn't get changed into }
  475. { vsf_referred_not_inited }
  476. set_varstate(left,vs_read,[vsf_must_be_valid]);
  477. end;
  478. end;
  479. function taddrnode.pass_1 : tnode;
  480. begin
  481. result:=nil;
  482. firstpass(left);
  483. if codegenerror then
  484. exit;
  485. { is this right for object of methods ?? }
  486. expectloc:=LOC_REGISTER;
  487. end;
  488. {*****************************************************************************
  489. TDEREFNODE
  490. *****************************************************************************}
  491. constructor tderefnode.create(l : tnode);
  492. begin
  493. inherited create(derefn,l);
  494. end;
  495. function tderefnode.pass_typecheck:tnode;
  496. begin
  497. result:=nil;
  498. typecheckpass(left);
  499. set_varstate(left,vs_read,[vsf_must_be_valid]);
  500. if codegenerror then
  501. exit;
  502. { tp procvar support }
  503. maybe_call_procvar(left,true);
  504. if left.resultdef.typ=pointerdef then
  505. resultdef:=tpointerdef(left.resultdef).pointeddef
  506. else
  507. CGMessage(parser_e_invalid_qualifier);
  508. end;
  509. procedure Tderefnode.mark_write;
  510. begin
  511. include(flags,nf_write);
  512. end;
  513. function tderefnode.pass_1 : tnode;
  514. begin
  515. result:=nil;
  516. firstpass(left);
  517. if codegenerror then
  518. exit;
  519. expectloc:=LOC_REFERENCE;
  520. end;
  521. {*****************************************************************************
  522. TSUBSCRIPTNODE
  523. *****************************************************************************}
  524. constructor tsubscriptnode.create(varsym : tsym;l : tnode);
  525. begin
  526. inherited create(subscriptn,l);
  527. { vs should be changed to tsym! }
  528. vs:=tfieldvarsym(varsym);
  529. end;
  530. constructor tsubscriptnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  531. begin
  532. inherited ppuload(t,ppufile);
  533. ppufile.getderef(vsderef);
  534. end;
  535. procedure tsubscriptnode.ppuwrite(ppufile:tcompilerppufile);
  536. begin
  537. inherited ppuwrite(ppufile);
  538. ppufile.putderef(vsderef);
  539. end;
  540. procedure tsubscriptnode.buildderefimpl;
  541. begin
  542. inherited buildderefimpl;
  543. vsderef.build(vs);
  544. end;
  545. procedure tsubscriptnode.derefimpl;
  546. begin
  547. inherited derefimpl;
  548. vs:=tfieldvarsym(vsderef.resolve);
  549. end;
  550. function tsubscriptnode.dogetcopy : tnode;
  551. var
  552. p : tsubscriptnode;
  553. begin
  554. p:=tsubscriptnode(inherited dogetcopy);
  555. p.vs:=vs;
  556. dogetcopy:=p;
  557. end;
  558. function tsubscriptnode.pass_typecheck:tnode;
  559. begin
  560. result:=nil;
  561. typecheckpass(left);
  562. { tp procvar support }
  563. maybe_call_procvar(left,true);
  564. resultdef:=vs.vardef;
  565. // don't put records from which we load float fields
  566. // in integer registers
  567. if (left.resultdef.typ=recorddef) and
  568. (resultdef.typ=floatdef) then
  569. make_not_regable(left,[ra_addr_regable]);
  570. end;
  571. procedure Tsubscriptnode.mark_write;
  572. begin
  573. include(flags,nf_write);
  574. end;
  575. function tsubscriptnode.pass_1 : tnode;
  576. begin
  577. result:=nil;
  578. firstpass(left);
  579. if codegenerror then
  580. exit;
  581. { classes must be dereferenced implicitly }
  582. if is_class_or_interface_or_dispinterface_or_objc(left.resultdef) then
  583. expectloc:=LOC_REFERENCE
  584. else
  585. begin
  586. case left.expectloc of
  587. LOC_REGISTER,
  588. LOC_SUBSETREG:
  589. // can happen for function results on win32 and darwin/x86
  590. if (left.resultdef.size > sizeof(pint)) then
  591. expectloc:=LOC_REFERENCE
  592. else
  593. expectloc:=LOC_SUBSETREG;
  594. LOC_CREGISTER,
  595. LOC_CSUBSETREG:
  596. expectloc:=LOC_CSUBSETREG;
  597. LOC_REFERENCE,
  598. LOC_CREFERENCE:
  599. expectloc:=left.expectloc;
  600. else internalerror(20060521);
  601. end;
  602. end;
  603. end;
  604. function tsubscriptnode.docompare(p: tnode): boolean;
  605. begin
  606. docompare :=
  607. inherited docompare(p) and
  608. (vs = tsubscriptnode(p).vs);
  609. end;
  610. {*****************************************************************************
  611. TVECNODE
  612. *****************************************************************************}
  613. constructor tvecnode.create(l,r : tnode);
  614. begin
  615. inherited create(vecn,l,r);
  616. end;
  617. function tvecnode.pass_typecheck:tnode;
  618. var
  619. hightree: tnode;
  620. htype,elementdef : tdef;
  621. valid : boolean;
  622. begin
  623. result:=nil;
  624. typecheckpass(left);
  625. typecheckpass(right);
  626. { implicitly convert stringconstant to stringdef,
  627. see tbs/tb0476.pp for a test }
  628. if (left.nodetype=stringconstn) and
  629. (tstringconstnode(left).cst_type=cst_conststring) then
  630. begin
  631. if tstringconstnode(left).len>255 then
  632. inserttypeconv(left,cansistringtype)
  633. else
  634. inserttypeconv(left,cshortstringtype);
  635. end;
  636. { In p[1] p is always valid, it is not possible to
  637. declared a shortstring or normal array that has
  638. undefined number of elements. Dynamic array and
  639. ansi/widestring needs to be valid }
  640. valid:=is_dynamic_array(left.resultdef) or
  641. is_ansistring(left.resultdef) or
  642. is_wide_or_unicode_string(left.resultdef) or
  643. { implicit pointer dereference -> pointer is read }
  644. (left.resultdef.typ = pointerdef);
  645. if valid then
  646. set_varstate(left,vs_read,[vsf_must_be_valid]);
  647. {
  648. A vecn is, just like a loadn, always part of an expression with its
  649. own read/write and must_be_valid semantics. Therefore we don't have
  650. to do anything else here, just like for loadn's
  651. }
  652. set_varstate(right,vs_read,[vsf_must_be_valid]);
  653. if codegenerror then
  654. exit;
  655. { maybe type conversion for the index value, but
  656. do not convert enums, char (why not? (JM))
  657. and do not convert range nodes }
  658. if (right.nodetype<>rangen) and (is_integer(right.resultdef) or is_boolean(right.resultdef) or (left.resultdef.typ<>arraydef)) then
  659. case left.resultdef.typ of
  660. arraydef:
  661. if ado_isvariant in Tarraydef(left.resultdef).arrayoptions then
  662. {Variant arrays are a special array, can have negative indexes and would therefore
  663. need s32bit. However, they should not appear in a vecn, as they are handled in
  664. handle_variantarray in pexpr.pas. Therefore, encountering a variant array is an
  665. internal error... }
  666. internalerror(200707031)
  667. else if is_special_array(left.resultdef) then
  668. {Arrays without a high bound (dynamic arrays, open arrays) are zero based,
  669. convert indexes into these arrays to aword.}
  670. inserttypeconv(right,uinttype)
  671. { convert between pasbool and cbool if necessary }
  672. else if is_boolean(right.resultdef) then
  673. inserttypeconv(right,tarraydef(left.resultdef).rangedef)
  674. else
  675. {Convert array indexes to low_bound..high_bound.}
  676. inserttypeconv(right,Torddef.create(Torddef(sinttype).ordtype,
  677. int64(Tarraydef(left.resultdef).lowrange),
  678. int64(Tarraydef(left.resultdef).highrange)
  679. ));
  680. stringdef:
  681. if is_open_string(left.resultdef) then
  682. inserttypeconv(right,u8inttype)
  683. else if is_shortstring(left.resultdef) then
  684. {Convert shortstring indexes to 0..length.}
  685. inserttypeconv(right,Torddef.create(u8bit,0,int64(Tstringdef(left.resultdef).len)))
  686. else
  687. {Convert indexes into dynamically allocated strings to aword.}
  688. inserttypeconv(right,uinttype);
  689. else
  690. {Others, i.e. pointer indexes to aint.}
  691. inserttypeconv(right,sinttype);
  692. end;
  693. { although we never put regular arrays or shortstrings in registers,
  694. it's possible that another type was typecasted to a small record
  695. that has a field of one of these types -> in that case the record
  696. can't be a regvar either }
  697. if ((left.resultdef.typ=arraydef) and
  698. not is_special_array(left.resultdef)) or
  699. ((left.resultdef.typ=stringdef) and
  700. (tstringdef(left.resultdef).stringtype in [st_shortstring,st_longstring])) then
  701. make_not_regable(left,[ra_addr_regable]);
  702. case left.resultdef.typ of
  703. arraydef :
  704. begin
  705. { check type of the index value }
  706. if (compare_defs(right.resultdef,tarraydef(left.resultdef).rangedef,right.nodetype)=te_incompatible) then
  707. IncompatibleTypes(right.resultdef,tarraydef(left.resultdef).rangedef);
  708. if right.nodetype=rangen then
  709. resultdef:=left.resultdef
  710. else
  711. resultdef:=Tarraydef(left.resultdef).elementdef;
  712. { if we are range checking an open array or array of const, we }
  713. { need to load the high parameter. If the current procedure is }
  714. { nested inside the procedure to which the open array/of const }
  715. { was passed, then the high parameter must not be a regvar. }
  716. { So create a loadnode for the high parameter here and }
  717. { typecheck it, then the loadnode will make the high parameter }
  718. { not regable. Otherwise this would only happen inside pass_2, }
  719. { which is too late since by then the regvars are already }
  720. { assigned (pass_1 is also already too late, because then the }
  721. { regvars of the parent are also already assigned). }
  722. { webtbs/tw8975 }
  723. if (cs_check_range in current_settings.localswitches) and
  724. (is_open_array(left.resultdef) or
  725. is_array_of_const(left.resultdef)) and
  726. { cdecl functions don't have high() so we can not check the range }
  727. { (can't use current_procdef, since it may be a nested procedure) }
  728. not(tprocdef(tparasymtable(tparavarsym(tloadnode(left).symtableentry).owner).defowner).proccalloption in cdecl_pocalls) then
  729. begin
  730. { load_high_value_node already typechecks }
  731. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  732. hightree.free;
  733. end;
  734. end;
  735. pointerdef :
  736. begin
  737. { are we accessing a pointer[], then convert the pointer to
  738. an array first, in FPC this is allowed for all pointers
  739. (except voidpointer) in delphi/tp7 it's only allowed for pchars. }
  740. if not is_voidpointer(left.resultdef) and
  741. (
  742. (m_fpc in current_settings.modeswitches) or
  743. is_pchar(left.resultdef) or
  744. is_pwidechar(left.resultdef)
  745. ) then
  746. begin
  747. { convert pointer to array }
  748. htype:=tarraydef.create_from_pointer(tpointerdef(left.resultdef).pointeddef);
  749. inserttypeconv(left,htype);
  750. if right.nodetype=rangen then
  751. resultdef:=htype
  752. else
  753. resultdef:=tarraydef(htype).elementdef;
  754. end
  755. else
  756. CGMessage(type_e_array_required);
  757. end;
  758. stringdef :
  759. begin
  760. case tstringdef(left.resultdef).stringtype of
  761. st_unicodestring,
  762. st_widestring :
  763. elementdef:=cwidechartype;
  764. st_ansistring :
  765. elementdef:=cchartype;
  766. st_longstring :
  767. elementdef:=cchartype;
  768. st_shortstring :
  769. elementdef:=cchartype;
  770. end;
  771. if right.nodetype=rangen then
  772. begin
  773. htype:=Tarraydef.create_from_pointer(elementdef);
  774. resultdef:=htype;
  775. end
  776. else
  777. begin
  778. { indexed access to 0 element is only allowed for shortstrings }
  779. if (right.nodetype=ordconstn) and
  780. (Tordconstnode(right).value.svalue=0) and
  781. not is_shortstring(left.resultdef) then
  782. CGMessage(cg_e_can_access_element_zero);
  783. resultdef:=elementdef;
  784. end;
  785. end;
  786. variantdef :
  787. resultdef:=cvarianttype;
  788. else
  789. CGMessage(type_e_array_required);
  790. end;
  791. end;
  792. procedure Tvecnode.mark_write;
  793. begin
  794. include(flags,nf_write);
  795. end;
  796. function tvecnode.pass_1 : tnode;
  797. begin
  798. result:=nil;
  799. firstpass(left);
  800. firstpass(right);
  801. if codegenerror then
  802. exit;
  803. if (nf_callunique in flags) and
  804. (is_ansistring(left.resultdef) or
  805. is_unicodestring(left.resultdef) or
  806. (is_widestring(left.resultdef) and not(tf_winlikewidestring in target_info.flags))) then
  807. begin
  808. left := ctypeconvnode.create_internal(ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+'_unique',
  809. ccallparanode.create(
  810. ctypeconvnode.create_internal(left,voidpointertype),nil)),
  811. left.resultdef);
  812. firstpass(left);
  813. { double resultdef passes somwhere else may cause this to be }
  814. { reset though :/ }
  815. exclude(flags,nf_callunique);
  816. end
  817. else if is_widestring(left.resultdef) and (tf_winlikewidestring in target_info.flags) then
  818. exclude(flags,nf_callunique);
  819. { a range node as array index can only appear in function calls, and
  820. those convert the range node into something else in
  821. tcallnode.gen_high_tree }
  822. if (right.nodetype=rangen) then
  823. CGMessagePos(right.fileinfo,parser_e_illegal_expression)
  824. else if (not is_packed_array(left.resultdef)) or
  825. ((tarraydef(left.resultdef).elepackedbitsize mod 8) = 0) then
  826. if left.expectloc=LOC_CREFERENCE then
  827. expectloc:=LOC_CREFERENCE
  828. else
  829. expectloc:=LOC_REFERENCE
  830. else
  831. if left.expectloc=LOC_CREFERENCE then
  832. expectloc:=LOC_CSUBSETREF
  833. else
  834. expectloc:=LOC_SUBSETREF;
  835. end;
  836. {*****************************************************************************
  837. TWITHNODE
  838. *****************************************************************************}
  839. constructor twithnode.create(l:tnode);
  840. begin
  841. inherited create(withn,l);
  842. fileinfo:=l.fileinfo;
  843. end;
  844. destructor twithnode.destroy;
  845. begin
  846. inherited destroy;
  847. end;
  848. constructor twithnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  849. begin
  850. inherited ppuload(t,ppufile);
  851. end;
  852. procedure twithnode.ppuwrite(ppufile:tcompilerppufile);
  853. begin
  854. inherited ppuwrite(ppufile);
  855. end;
  856. function twithnode.dogetcopy : tnode;
  857. var
  858. p : twithnode;
  859. begin
  860. p:=twithnode(inherited dogetcopy);
  861. result:=p;
  862. end;
  863. function twithnode.pass_typecheck:tnode;
  864. begin
  865. result:=nil;
  866. resultdef:=voidtype;
  867. if assigned(left) then
  868. typecheckpass(left);
  869. end;
  870. function twithnode.pass_1 : tnode;
  871. begin
  872. result:=nil;
  873. expectloc:=LOC_VOID;
  874. end;
  875. function twithnode.docompare(p: tnode): boolean;
  876. begin
  877. docompare :=
  878. inherited docompare(p);
  879. end;
  880. function is_big_untyped_addrnode(p: tnode): boolean;
  881. begin
  882. is_big_untyped_addrnode:=(p.nodetype=addrn) and
  883. not (nf_typedaddr in p.flags) and (taddrnode(p).left.resultdef.size > 1);
  884. end;
  885. end.