nmem.pas 35 KB

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