nmem.pas 32 KB

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