nmem.pas 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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_structdef) then
  146. begin
  147. if assigned(current_structdef.genericdef) then
  148. if current_structdef.genericdef=left.resultdef then
  149. begin
  150. resultdef:=tclassrefdef.create(current_structdef);
  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. { several object types must be dereferenced implicitly }
  582. if is_implicit_pointer_object_type(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. (cs_pointermath in current_settings.localswitches) or
  743. tpointerdef(left.resultdef).has_pointer_math or
  744. is_pchar(left.resultdef) or
  745. is_pwidechar(left.resultdef)
  746. ) then
  747. begin
  748. { convert pointer to array }
  749. htype:=tarraydef.create_from_pointer(tpointerdef(left.resultdef).pointeddef);
  750. inserttypeconv(left,htype);
  751. if right.nodetype=rangen then
  752. resultdef:=htype
  753. else
  754. resultdef:=tarraydef(htype).elementdef;
  755. end
  756. else
  757. CGMessage(type_e_array_required);
  758. end;
  759. stringdef :
  760. begin
  761. case tstringdef(left.resultdef).stringtype of
  762. st_unicodestring,
  763. st_widestring :
  764. elementdef:=cwidechartype;
  765. st_ansistring :
  766. elementdef:=cchartype;
  767. st_longstring :
  768. elementdef:=cchartype;
  769. st_shortstring :
  770. elementdef:=cchartype;
  771. end;
  772. if right.nodetype=rangen then
  773. begin
  774. htype:=Tarraydef.create_from_pointer(elementdef);
  775. resultdef:=htype;
  776. end
  777. else
  778. begin
  779. { indexed access to 0 element is only allowed for shortstrings }
  780. if (right.nodetype=ordconstn) and
  781. (Tordconstnode(right).value.svalue=0) and
  782. not is_shortstring(left.resultdef) then
  783. CGMessage(cg_e_can_access_element_zero);
  784. resultdef:=elementdef;
  785. end;
  786. end;
  787. variantdef :
  788. resultdef:=cvarianttype;
  789. else
  790. CGMessage(type_e_array_required);
  791. end;
  792. end;
  793. procedure Tvecnode.mark_write;
  794. begin
  795. include(flags,nf_write);
  796. end;
  797. function tvecnode.pass_1 : tnode;
  798. begin
  799. result:=nil;
  800. firstpass(left);
  801. firstpass(right);
  802. if codegenerror then
  803. exit;
  804. if (nf_callunique in flags) and
  805. (is_ansistring(left.resultdef) or
  806. is_unicodestring(left.resultdef) or
  807. (is_widestring(left.resultdef) and not(tf_winlikewidestring in target_info.flags))) then
  808. begin
  809. left := ctypeconvnode.create_internal(ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+'_unique',
  810. ccallparanode.create(
  811. ctypeconvnode.create_internal(left,voidpointertype),nil)),
  812. left.resultdef);
  813. firstpass(left);
  814. { double resultdef passes somwhere else may cause this to be }
  815. { reset though :/ }
  816. exclude(flags,nf_callunique);
  817. end
  818. else if is_widestring(left.resultdef) and (tf_winlikewidestring in target_info.flags) then
  819. exclude(flags,nf_callunique);
  820. { a range node as array index can only appear in function calls, and
  821. those convert the range node into something else in
  822. tcallnode.gen_high_tree }
  823. if (right.nodetype=rangen) then
  824. CGMessagePos(right.fileinfo,parser_e_illegal_expression)
  825. else if (not is_packed_array(left.resultdef)) or
  826. ((tarraydef(left.resultdef).elepackedbitsize mod 8) = 0) then
  827. if left.expectloc=LOC_CREFERENCE then
  828. expectloc:=LOC_CREFERENCE
  829. else
  830. expectloc:=LOC_REFERENCE
  831. else
  832. if left.expectloc=LOC_CREFERENCE then
  833. expectloc:=LOC_CSUBSETREF
  834. else
  835. expectloc:=LOC_SUBSETREF;
  836. end;
  837. {*****************************************************************************
  838. TWITHNODE
  839. *****************************************************************************}
  840. constructor twithnode.create(l:tnode);
  841. begin
  842. inherited create(withn,l);
  843. fileinfo:=l.fileinfo;
  844. end;
  845. destructor twithnode.destroy;
  846. begin
  847. inherited destroy;
  848. end;
  849. constructor twithnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  850. begin
  851. inherited ppuload(t,ppufile);
  852. end;
  853. procedure twithnode.ppuwrite(ppufile:tcompilerppufile);
  854. begin
  855. inherited ppuwrite(ppufile);
  856. end;
  857. function twithnode.dogetcopy : tnode;
  858. var
  859. p : twithnode;
  860. begin
  861. p:=twithnode(inherited dogetcopy);
  862. result:=p;
  863. end;
  864. function twithnode.pass_typecheck:tnode;
  865. begin
  866. result:=nil;
  867. resultdef:=voidtype;
  868. if assigned(left) then
  869. typecheckpass(left);
  870. end;
  871. function twithnode.pass_1 : tnode;
  872. begin
  873. result:=nil;
  874. expectloc:=LOC_VOID;
  875. end;
  876. function twithnode.docompare(p: tnode): boolean;
  877. begin
  878. docompare :=
  879. inherited docompare(p);
  880. end;
  881. function is_big_untyped_addrnode(p: tnode): boolean;
  882. begin
  883. is_big_untyped_addrnode:=(p.nodetype=addrn) and
  884. not (nf_typedaddr in p.flags) and (taddrnode(p).left.resultdef.size > 1);
  885. end;
  886. end.