nmem.pas 38 KB

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