nmem.pas 31 KB

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