nmem.pas 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for memory related nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nmem;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. symdef,symsym,symtable,symtype;
  23. type
  24. tloadvmtaddrnode = class(tunarynode)
  25. { unless this is for a call, we have to send the "class" message to
  26. the objctype because the type information only gets initialized
  27. after the first message has been sent -> crash if you pass an
  28. uninitialized type to e.g. class_getInstanceSize() or so. No need
  29. to save to/restore from ppu. }
  30. forcall: boolean;
  31. constructor create(l : tnode);virtual;
  32. function pass_1 : tnode;override;
  33. function pass_typecheck:tnode;override;
  34. function docompare(p: tnode): boolean; override;
  35. function dogetcopy: tnode; override;
  36. end;
  37. tloadvmtaddrnodeclass = class of tloadvmtaddrnode;
  38. tloadparentfpkind = (
  39. { as parameter to a nested routine (current routine's frame) }
  40. lpf_forpara,
  41. { to load a local from a parent routine in the current nested routine
  42. (some parent routine's frame) }
  43. lpf_forload
  44. );
  45. tloadparentfpnode = class(tunarynode)
  46. parentpd : tprocdef;
  47. parentpdderef : tderef;
  48. kind: tloadparentfpkind;
  49. constructor create(pd: tprocdef; fpkind: tloadparentfpkind);virtual;
  50. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  51. procedure ppuwrite(ppufile:tcompilerppufile);override;
  52. procedure buildderefimpl;override;
  53. procedure derefimpl;override;
  54. function pass_1 : tnode;override;
  55. function pass_typecheck:tnode;override;
  56. function docompare(p: tnode): boolean; override;
  57. function dogetcopy : tnode;override;
  58. end;
  59. tloadparentfpnodeclass = class of tloadparentfpnode;
  60. taddrnodeflag = (
  61. { generated by the Ofs() internal function }
  62. anf_ofs,
  63. anf_typedaddr
  64. );
  65. taddrnodeflags = set of taddrnodeflag;
  66. taddrnode = class(tunarynode)
  67. getprocvardef : tprocvardef;
  68. getprocvardefderef : tderef;
  69. addrnodeflags : taddrnodeflags;
  70. constructor create(l : tnode);virtual;
  71. constructor create_internal(l : tnode); virtual;
  72. constructor create_internal_nomark(l : tnode); virtual;
  73. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  74. procedure ppuwrite(ppufile:tcompilerppufile);override;
  75. procedure mark_write;override;
  76. procedure buildderefimpl;override;
  77. procedure derefimpl;override;
  78. procedure printnodeinfo(var t: text); override;
  79. function docompare(p: tnode): boolean; override;
  80. function dogetcopy : tnode;override;
  81. function pass_1 : tnode;override;
  82. function pass_typecheck:tnode;override;
  83. function simplify(forinline : boolean) : tnode; override;
  84. protected
  85. mark_read_written: boolean;
  86. procedure set_labelsym_resultdef; virtual;
  87. function typecheck_non_proc(realsource: tnode; out res: tnode): boolean; virtual;
  88. end;
  89. taddrnodeclass = class of taddrnode;
  90. tderefnode = class(tunarynode)
  91. constructor create(l : tnode);virtual;
  92. function pass_1 : tnode;override;
  93. function pass_typecheck:tnode;override;
  94. procedure mark_write;override;
  95. end;
  96. tderefnodeclass = class of tderefnode;
  97. tsubscriptnode = class(tunarynode)
  98. vs : tfieldvarsym;
  99. vsderef : tderef;
  100. constructor create(varsym : tsym;l : tnode);virtual;
  101. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  102. procedure ppuwrite(ppufile:tcompilerppufile);override;
  103. procedure buildderefimpl;override;
  104. procedure derefimpl;override;
  105. function dogetcopy : tnode;override;
  106. function pass_1 : tnode;override;
  107. function docompare(p: tnode): boolean; override;
  108. function pass_typecheck:tnode;override;
  109. procedure mark_write;override;
  110. end;
  111. tsubscriptnodeclass = class of tsubscriptnode;
  112. tvecnode = class(tbinarynode)
  113. protected
  114. function first_arraydef: tnode; virtual;
  115. function gen_array_rangecheck: tnode; virtual;
  116. public
  117. constructor create(l,r : tnode);virtual;
  118. function pass_1 : tnode;override;
  119. function pass_typecheck:tnode;override;
  120. procedure mark_write;override;
  121. end;
  122. tvecnodeclass = class of tvecnode;
  123. twithnode = class(tunarynode)
  124. constructor create(l:tnode);
  125. destructor destroy;override;
  126. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  127. procedure ppuwrite(ppufile:tcompilerppufile);override;
  128. function dogetcopy : tnode;override;
  129. function pass_1 : tnode;override;
  130. function docompare(p: tnode): boolean; override;
  131. function pass_typecheck:tnode;override;
  132. end;
  133. twithnodeclass = class of twithnode;
  134. var
  135. cloadvmtaddrnode : tloadvmtaddrnodeclass= tloadvmtaddrnode;
  136. caddrnode : taddrnodeclass= taddrnode;
  137. cderefnode : tderefnodeclass= tderefnode;
  138. csubscriptnode : tsubscriptnodeclass= tsubscriptnode;
  139. cvecnode : tvecnodeclass= tvecnode;
  140. cwithnode : twithnodeclass= twithnode;
  141. cloadparentfpnode : tloadparentfpnodeclass = tloadparentfpnode;
  142. function is_big_untyped_addrnode(p: tnode): boolean;
  143. implementation
  144. uses
  145. globtype,systems,constexp,
  146. cutils,verbose,globals,
  147. symconst,defutil,defcmp,
  148. nadd,nbas,nflw,nutils,objcutil,
  149. wpobase,
  150. {$ifdef i8086}
  151. cpuinfo,
  152. {$endif i8086}
  153. htypechk,pass_1,ncal,nld,ncon,ncnv,cgbase,procinfo
  154. ;
  155. {*****************************************************************************
  156. TLOADVMTADDRNODE
  157. *****************************************************************************}
  158. constructor tloadvmtaddrnode.create(l : tnode);
  159. begin
  160. inherited create(loadvmtaddrn,l);
  161. end;
  162. function tloadvmtaddrnode.pass_typecheck:tnode;
  163. var
  164. defaultresultdef : boolean;
  165. begin
  166. result:=nil;
  167. typecheckpass(left);
  168. if codegenerror then
  169. exit;
  170. case left.resultdef.typ of
  171. classrefdef :
  172. resultdef:=left.resultdef;
  173. recorddef,
  174. objectdef:
  175. begin
  176. if (left.resultdef.typ=objectdef) or
  177. ((target_info.system in systems_jvm) and
  178. (left.resultdef.typ=recorddef)) then
  179. begin
  180. { access to the classtype while specializing? }
  181. if tstoreddef(left.resultdef).is_generic then
  182. begin
  183. defaultresultdef:=true;
  184. if assigned(current_structdef) then
  185. begin
  186. if assigned(current_structdef.genericdef) then
  187. if current_structdef.genericdef=left.resultdef then
  188. begin
  189. resultdef:=cclassrefdef.create(current_structdef);
  190. defaultresultdef:=false;
  191. end
  192. else
  193. CGMessage(parser_e_cant_create_generics_of_this_type);
  194. end
  195. else
  196. message(parser_e_cant_create_generics_of_this_type);
  197. if defaultresultdef then
  198. resultdef:=cclassrefdef.create(left.resultdef);
  199. end
  200. else
  201. resultdef:=cclassrefdef.create(left.resultdef);
  202. end
  203. else
  204. CGMessage(parser_e_pointer_to_class_expected);
  205. end
  206. else
  207. CGMessage(parser_e_pointer_to_class_expected);
  208. end;
  209. end;
  210. function tloadvmtaddrnode.docompare(p: tnode): boolean;
  211. begin
  212. result:=inherited docompare(p);
  213. if result then
  214. result:=forcall=tloadvmtaddrnode(p).forcall;
  215. end;
  216. function tloadvmtaddrnode.dogetcopy: tnode;
  217. begin
  218. result:=inherited dogetcopy;
  219. tloadvmtaddrnode(result).forcall:=forcall;
  220. end;
  221. function tloadvmtaddrnode.pass_1 : tnode;
  222. var
  223. vs: tsym;
  224. begin
  225. result:=nil;
  226. expectloc:=LOC_REGISTER;
  227. if (left.nodetype=typen) and
  228. (cs_create_pic in current_settings.moduleswitches) then
  229. include(current_procinfo.flags,pi_needs_got);
  230. if left.nodetype<>typen then
  231. begin
  232. if (is_objc_class_or_protocol(left.resultdef) or
  233. is_objcclassref(left.resultdef)) then
  234. begin
  235. { on non-fragile ABI platforms, the ISA pointer may be opaque
  236. and we must call Object_getClass to obtain the real ISA
  237. pointer }
  238. if target_info.system in systems_objc_nfabi then
  239. begin
  240. result:=ccallnode.createinternfromunit('OBJC','OBJECT_GETCLASS',ccallparanode.create(left,nil));
  241. inserttypeconv_explicit(result,resultdef);
  242. end
  243. else
  244. result:=objcloadbasefield(left,'ISA');
  245. end
  246. else
  247. result:=ctypeconvnode.create_internal(load_vmt_for_self_node(left),resultdef);
  248. { reused }
  249. left:=nil;
  250. end
  251. else if not is_objcclass(left.resultdef) and
  252. not is_objcclassref(left.resultdef) then
  253. begin
  254. if not(nf_ignore_for_wpo in flags) and
  255. (not assigned(current_procinfo) or
  256. (po_inline in current_procinfo.procdef.procoptions) or
  257. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  258. begin
  259. { keep track of which classes might be instantiated via a classrefdef }
  260. if (left.resultdef.typ=classrefdef) then
  261. tobjectdef(tclassrefdef(left.resultdef).pointeddef).register_maybe_created_object_type
  262. else if (left.resultdef.typ=objectdef) then
  263. tobjectdef(left.resultdef).register_maybe_created_object_type
  264. end
  265. end
  266. else if is_objcclass(left.resultdef) and
  267. not(forcall) then
  268. begin
  269. { call "class" method (= "classclass" in FPC), because otherwise
  270. we may use the class information before it has been
  271. initialized }
  272. vs:=search_struct_member(tobjectdef(left.resultdef),'CLASSCLASS');
  273. if not assigned(vs) or
  274. (vs.typ<>procsym) then
  275. internalerror(2011080601);
  276. { can't reuse "self", because it will be freed when we return }
  277. result:=ccallnode.create(nil,tprocsym(vs),vs.owner,self.getcopy,[],nil);
  278. end;
  279. end;
  280. {*****************************************************************************
  281. TLOADPARENTFPNODE
  282. *****************************************************************************}
  283. constructor tloadparentfpnode.create(pd: tprocdef; fpkind: tloadparentfpkind);
  284. begin
  285. inherited create(loadparentfpn,nil);
  286. if not assigned(pd) then
  287. internalerror(200309288);
  288. if (pd.parast.symtablelevel>current_procinfo.procdef.parast.symtablelevel) then
  289. internalerror(200309284);
  290. parentpd:=pd;
  291. kind:=fpkind;
  292. end;
  293. constructor tloadparentfpnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  294. begin
  295. inherited ppuload(t,ppufile);
  296. ppufile.getderef(parentpdderef);
  297. kind:=tloadparentfpkind(ppufile.getbyte);
  298. end;
  299. procedure tloadparentfpnode.ppuwrite(ppufile:tcompilerppufile);
  300. begin
  301. inherited ppuwrite(ppufile);
  302. ppufile.putderef(parentpdderef);
  303. ppufile.putbyte(byte(kind));
  304. end;
  305. procedure tloadparentfpnode.buildderefimpl;
  306. begin
  307. inherited buildderefimpl;
  308. parentpdderef.build(parentpd);
  309. end;
  310. procedure tloadparentfpnode.derefimpl;
  311. begin
  312. inherited derefimpl;
  313. parentpd:=tprocdef(parentpdderef.resolve);
  314. end;
  315. function tloadparentfpnode.docompare(p: tnode): boolean;
  316. begin
  317. result:=
  318. inherited docompare(p) and
  319. (tloadparentfpnode(p).parentpd=parentpd) and
  320. (tloadparentfpnode(p).kind=kind);
  321. end;
  322. function tloadparentfpnode.dogetcopy : tnode;
  323. var
  324. p : tloadparentfpnode;
  325. begin
  326. p:=tloadparentfpnode(inherited dogetcopy);
  327. p.parentpd:=parentpd;
  328. p.kind:=kind;
  329. dogetcopy:=p;
  330. end;
  331. function tloadparentfpnode.pass_typecheck:tnode;
  332. {$ifdef dummy}
  333. var
  334. currpi : tprocinfo;
  335. hsym : tparavarsym;
  336. {$endif dummy}
  337. begin
  338. result:=nil;
  339. resultdef:=parentfpvoidpointertype;
  340. {$ifdef dummy}
  341. { currently parentfps are never loaded in registers (FK) }
  342. if (current_procinfo.procdef.parast.symtablelevel<>parentpd.parast.symtablelevel) then
  343. begin
  344. currpi:=current_procinfo;
  345. { walk parents }
  346. while (currpi.procdef.owner.symtablelevel>parentpd.parast.symtablelevel) do
  347. begin
  348. currpi:=currpi.parent;
  349. if not assigned(currpi) then
  350. internalerror(2005040602);
  351. hsym:=tparavarsym(currpi.procdef.parast.Find('parentfp'));
  352. if not assigned(hsym) then
  353. internalerror(2005040601);
  354. hsym.varregable:=vr_none;
  355. end;
  356. end;
  357. {$endif dummy}
  358. end;
  359. function tloadparentfpnode.pass_1 : tnode;
  360. begin
  361. result:=nil;
  362. expectloc:=LOC_REGISTER;
  363. end;
  364. {*****************************************************************************
  365. TADDRNODE
  366. *****************************************************************************}
  367. constructor taddrnode.create(l : tnode);
  368. begin
  369. inherited create(addrn,l);
  370. getprocvardef:=nil;
  371. addrnodeflags:=[];
  372. mark_read_written := true;
  373. end;
  374. constructor taddrnode.create_internal(l : tnode);
  375. begin
  376. self.create(l);
  377. include(flags,nf_internal);
  378. end;
  379. constructor taddrnode.create_internal_nomark(l : tnode);
  380. begin
  381. self.create_internal(l);
  382. mark_read_written := false;
  383. end;
  384. constructor taddrnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  385. begin
  386. inherited ppuload(t,ppufile);
  387. ppufile.getderef(getprocvardefderef);
  388. ppufile.getsmallset(addrnodeflags);
  389. end;
  390. procedure taddrnode.ppuwrite(ppufile:tcompilerppufile);
  391. begin
  392. inherited ppuwrite(ppufile);
  393. ppufile.putderef(getprocvardefderef);
  394. ppufile.putsmallset(addrnodeflags);
  395. end;
  396. procedure Taddrnode.mark_write;
  397. begin
  398. {@procvar:=nil is legal in Delphi mode.}
  399. left.mark_write;
  400. end;
  401. procedure taddrnode.buildderefimpl;
  402. begin
  403. inherited buildderefimpl;
  404. getprocvardefderef.build(getprocvardef);
  405. end;
  406. procedure taddrnode.derefimpl;
  407. begin
  408. inherited derefimpl;
  409. getprocvardef:=tprocvardef(getprocvardefderef.resolve);
  410. end;
  411. procedure taddrnode.printnodeinfo(var t: text);
  412. var
  413. first: Boolean;
  414. i: taddrnodeflag;
  415. begin
  416. inherited printnodeinfo(t);
  417. write(t,', addrnodeflags = [');
  418. first:=true;
  419. for i:=low(taddrnodeflag) to high(taddrnodeflag) do
  420. if i in addrnodeflags then
  421. begin
  422. if not first then
  423. write(t,',')
  424. else
  425. first:=false;
  426. write(t,i);
  427. end;
  428. write(t,']');
  429. end;
  430. function taddrnode.docompare(p: tnode): boolean;
  431. begin
  432. result:=
  433. inherited docompare(p) and
  434. (taddrnode(p).getprocvardef=getprocvardef) and
  435. (taddrnode(p).addrnodeflags=addrnodeflags);
  436. end;
  437. function taddrnode.dogetcopy : tnode;
  438. var
  439. p : taddrnode;
  440. begin
  441. p:=taddrnode(inherited dogetcopy);
  442. p.getprocvardef:=getprocvardef;
  443. p.addrnodeflags:=addrnodeflags;
  444. dogetcopy:=p;
  445. end;
  446. function taddrnode.pass_typecheck:tnode;
  447. var
  448. hp : tnode;
  449. hsym : tfieldvarsym;
  450. isprocvar,need_conv_to_voidptr: boolean;
  451. procpointertype: tdef;
  452. begin
  453. result:=nil;
  454. typecheckpass(left);
  455. if codegenerror then
  456. exit;
  457. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  458. { don't allow constants, for internal use we also
  459. allow taking the address of strings and sets }
  460. if is_constnode(left) and
  461. not(
  462. (nf_internal in flags) and
  463. (left.nodetype in [stringconstn,setconstn])
  464. ) then
  465. begin
  466. CGMessagePos(left.fileinfo,type_e_no_addr_of_constant);
  467. exit;
  468. end;
  469. { Handle @proc special, also @procvar in tp-mode needs
  470. special handling }
  471. if (left.resultdef.typ=procdef) or
  472. (
  473. { in case of nf_internal, follow the normal FPC semantics so that
  474. we can easily get the actual address of a procvar }
  475. not(nf_internal in flags) and
  476. (left.resultdef.typ=procvardef) and
  477. ((m_tp_procvar in current_settings.modeswitches) or
  478. (m_mac_procvar in current_settings.modeswitches))
  479. ) then
  480. begin
  481. isprocvar:=(left.resultdef.typ=procvardef);
  482. need_conv_to_voidptr:=
  483. (m_tp_procvar in current_settings.modeswitches) or
  484. (m_mac_procvar in current_settings.modeswitches);
  485. if not isprocvar then
  486. begin
  487. left:=ctypeconvnode.create_proc_to_procvar(left);
  488. if need_conv_to_voidptr then
  489. include(ttypeconvnode(left).convnodeflags,tcnf_proc_2_procvar_2_voidpointer);
  490. if anf_ofs in addrnodeflags then
  491. include(ttypeconvnode(left).convnodeflags,tcnf_proc_2_procvar_get_offset_only);
  492. left.fileinfo:=fileinfo;
  493. typecheckpass(left);
  494. end;
  495. { In tp procvar mode the result is always a voidpointer. Insert
  496. a typeconversion to voidpointer. For methodpointers we need
  497. to load the proc field }
  498. if need_conv_to_voidptr then
  499. begin
  500. if tabstractprocdef(left.resultdef).is_addressonly then
  501. begin
  502. if anf_ofs in addrnodeflags then
  503. result:=ctypeconvnode.create_internal(left,tabstractprocdef(left.resultdef).ofs_address_type)
  504. else
  505. result:=ctypeconvnode.create_internal(left,voidcodepointertype);
  506. include(result.flags,nf_load_procvar);
  507. left:=nil;
  508. end
  509. else
  510. begin
  511. { For procvars and for nested routines we need to return
  512. the proc field of the methodpointer }
  513. if isprocvar or
  514. is_nested_pd(tabstractprocdef(left.resultdef)) then
  515. begin
  516. if tabstractprocdef(left.resultdef).is_methodpointer then
  517. procpointertype:=methodpointertype
  518. else
  519. procpointertype:=nestedprocpointertype;
  520. { find proc field in methodpointer record }
  521. hsym:=tfieldvarsym(trecorddef(procpointertype).symtable.Find('proc'));
  522. if not assigned(hsym) then
  523. internalerror(200412041);
  524. { Load tmehodpointer(left).proc }
  525. result:=csubscriptnode.create(
  526. hsym,
  527. ctypeconvnode.create_internal(left,procpointertype));
  528. left:=nil;
  529. end
  530. else
  531. CGMessage(type_e_variable_id_expected);
  532. end;
  533. end
  534. else
  535. begin
  536. { Return the typeconvn only }
  537. result:=left;
  538. left:=nil;
  539. end;
  540. end
  541. else
  542. begin
  543. hp:=left;
  544. while assigned(hp) and (hp.nodetype in [typeconvn,derefn,subscriptn]) do
  545. hp:=tunarynode(hp).left;
  546. if not assigned(hp) then
  547. internalerror(200412042);
  548. if typecheck_non_proc(hp,result) then
  549. begin
  550. if assigned(result) then
  551. exit;
  552. end
  553. else
  554. CGMessage(type_e_variable_id_expected);
  555. end;
  556. if mark_read_written then
  557. begin
  558. { This is actually only "read", but treat it nevertheless as }
  559. { modified due to the possible use of pointers }
  560. { To avoid false positives regarding "uninitialised" }
  561. { warnings when using arrays, perform it in two steps }
  562. set_varstate(left,vs_written,[]);
  563. { vsf_must_be_valid so it doesn't get changed into }
  564. { vsf_referred_not_inited }
  565. set_varstate(left,vs_read,[vsf_must_be_valid]);
  566. end;
  567. if not(assigned(result)) then
  568. result:=simplify(false);
  569. end;
  570. function taddrnode.simplify(forinline : boolean) : tnode;
  571. var
  572. hsym : tfieldvarsym;
  573. begin
  574. result:=nil;
  575. if ((left.nodetype=subscriptn) and
  576. (tsubscriptnode(left).left.nodetype=derefn) and
  577. (tsubscriptnode(left).left.resultdef.typ=recorddef) and
  578. (tderefnode(tsubscriptnode(left).left).left.nodetype=niln)) or
  579. ((left.nodetype=subscriptn) and
  580. (tsubscriptnode(left).left.nodetype=typeconvn) and
  581. (tsubscriptnode(left).left.resultdef.typ=recorddef) and
  582. (ttypeconvnode(tsubscriptnode(left).left).left.nodetype=derefn) and
  583. (tderefnode(ttypeconvnode(tsubscriptnode(left).left).left).left.nodetype=niln)) then
  584. begin
  585. hsym:=tsubscriptnode(left).vs;
  586. if tabstractrecordsymtable(hsym.owner).is_packed then
  587. result:=cpointerconstnode.create(hsym.fieldoffset div 8,resultdef)
  588. else
  589. result:=cpointerconstnode.create(hsym.fieldoffset,resultdef);
  590. end;
  591. end;
  592. procedure taddrnode.set_labelsym_resultdef;
  593. begin
  594. resultdef:=voidcodepointertype;
  595. end;
  596. function taddrnode.typecheck_non_proc(realsource: tnode; out res: tnode): boolean;
  597. var
  598. hp : tnode;
  599. hsym : tfieldvarsym;
  600. offset: asizeint;
  601. begin
  602. result:=false;
  603. res:=nil;
  604. if (realsource.nodetype=loadn) and
  605. (tloadnode(realsource).symtableentry.typ=labelsym) then
  606. begin
  607. set_labelsym_resultdef;
  608. result:=true;
  609. end
  610. else if (realsource.nodetype=loadn) and
  611. (tloadnode(realsource).symtableentry.typ=absolutevarsym) and
  612. (tabsolutevarsym(tloadnode(realsource).symtableentry).abstyp=toaddr) then
  613. begin
  614. offset:=tabsolutevarsym(tloadnode(realsource).symtableentry).addroffset;
  615. hp:=left;
  616. while assigned(hp)and(hp.nodetype=subscriptn) do
  617. begin
  618. hsym:=tsubscriptnode(hp).vs;
  619. if tabstractrecordsymtable(hsym.owner).is_packed then
  620. begin
  621. { can't calculate the address of a non-byte aligned field }
  622. if (hsym.fieldoffset mod 8)<>0 then
  623. begin
  624. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  625. exit
  626. end;
  627. inc(offset,hsym.fieldoffset div 8)
  628. end
  629. else
  630. inc(offset,hsym.fieldoffset);
  631. hp:=tunarynode(hp).left;
  632. end;
  633. if anf_typedaddr in addrnodeflags then
  634. res:=cpointerconstnode.create(offset,cpointerdef.getreusable(left.resultdef))
  635. else
  636. res:=cpointerconstnode.create(offset,voidpointertype);
  637. result:=true;
  638. end
  639. else if (nf_internal in flags) or
  640. valid_for_addr(left,true) then
  641. begin
  642. if not(anf_typedaddr in addrnodeflags) then
  643. resultdef:=voidpointertype
  644. else
  645. resultdef:=cpointerdef.getreusable(left.resultdef);
  646. result:=true;
  647. end
  648. end;
  649. function taddrnode.pass_1 : tnode;
  650. begin
  651. result:=nil;
  652. firstpass(left);
  653. if codegenerror then
  654. exit;
  655. { is this right for object of methods ?? }
  656. expectloc:=LOC_REGISTER;
  657. end;
  658. {*****************************************************************************
  659. TDEREFNODE
  660. *****************************************************************************}
  661. constructor tderefnode.create(l : tnode);
  662. begin
  663. inherited create(derefn,l);
  664. end;
  665. function tderefnode.pass_typecheck:tnode;
  666. begin
  667. result:=nil;
  668. typecheckpass(left);
  669. set_varstate(left,vs_read,[vsf_must_be_valid]);
  670. if codegenerror then
  671. exit;
  672. { tp procvar support }
  673. maybe_call_procvar(left,true);
  674. if left.resultdef.typ=pointerdef then
  675. resultdef:=tpointerdef(left.resultdef).pointeddef
  676. else
  677. CGMessage(parser_e_invalid_qualifier);
  678. end;
  679. procedure Tderefnode.mark_write;
  680. begin
  681. include(flags,nf_write);
  682. end;
  683. function tderefnode.pass_1 : tnode;
  684. begin
  685. result:=nil;
  686. firstpass(left);
  687. if codegenerror then
  688. exit;
  689. expectloc:=LOC_REFERENCE;
  690. end;
  691. {*****************************************************************************
  692. TSUBSCRIPTNODE
  693. *****************************************************************************}
  694. constructor tsubscriptnode.create(varsym : tsym;l : tnode);
  695. begin
  696. inherited create(subscriptn,l);
  697. { vs should be changed to tsym! }
  698. vs:=tfieldvarsym(varsym);
  699. end;
  700. constructor tsubscriptnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  701. begin
  702. inherited ppuload(t,ppufile);
  703. ppufile.getderef(vsderef);
  704. end;
  705. procedure tsubscriptnode.ppuwrite(ppufile:tcompilerppufile);
  706. begin
  707. inherited ppuwrite(ppufile);
  708. ppufile.putderef(vsderef);
  709. end;
  710. procedure tsubscriptnode.buildderefimpl;
  711. begin
  712. inherited buildderefimpl;
  713. vsderef.build(vs);
  714. end;
  715. procedure tsubscriptnode.derefimpl;
  716. begin
  717. inherited derefimpl;
  718. vs:=tfieldvarsym(vsderef.resolve);
  719. end;
  720. function tsubscriptnode.dogetcopy : tnode;
  721. var
  722. p : tsubscriptnode;
  723. begin
  724. p:=tsubscriptnode(inherited dogetcopy);
  725. p.vs:=vs;
  726. dogetcopy:=p;
  727. end;
  728. function tsubscriptnode.pass_typecheck:tnode;
  729. begin
  730. result:=nil;
  731. typecheckpass(left);
  732. { tp procvar support }
  733. maybe_call_procvar(left,true);
  734. resultdef:=vs.vardef;
  735. // don't put records from which we load float fields
  736. // in integer registers
  737. if (left.resultdef.typ=recorddef) and
  738. (resultdef.typ=floatdef) then
  739. make_not_regable(left,[ra_addr_regable]);
  740. end;
  741. procedure Tsubscriptnode.mark_write;
  742. begin
  743. include(flags,nf_write);
  744. { if an element of a record is written, then the whole record is changed/it is written to it,
  745. for data types being implicit pointers this does not apply as the object itself does not change }
  746. if not(is_implicit_pointer_object_type(left.resultdef)) then
  747. left.mark_write;
  748. end;
  749. function tsubscriptnode.pass_1 : tnode;
  750. begin
  751. result:=nil;
  752. firstpass(left);
  753. if codegenerror then
  754. exit;
  755. { several object types must be dereferenced implicitly }
  756. if is_implicit_pointer_object_type(left.resultdef) then
  757. expectloc:=LOC_REFERENCE
  758. else
  759. begin
  760. case left.expectloc of
  761. { if a floating point value is casted into a record, it
  762. can happen that we get here an fpu or mm register }
  763. LOC_CMMREGISTER,
  764. LOC_CFPUREGISTER,
  765. LOC_MMREGISTER,
  766. LOC_FPUREGISTER,
  767. LOC_CONSTANT,
  768. LOC_REGISTER,
  769. LOC_SUBSETREG:
  770. // can happen for function results on win32 and darwin/x86
  771. if (left.resultdef.size > sizeof(pint)) then
  772. expectloc:=LOC_REFERENCE
  773. else
  774. expectloc:=LOC_SUBSETREG;
  775. LOC_CREGISTER,
  776. LOC_CSUBSETREG:
  777. expectloc:=LOC_CSUBSETREG;
  778. LOC_REFERENCE,
  779. LOC_CREFERENCE:
  780. expectloc:=left.expectloc;
  781. else internalerror(20060521);
  782. end;
  783. end;
  784. end;
  785. function tsubscriptnode.docompare(p: tnode): boolean;
  786. begin
  787. docompare :=
  788. inherited docompare(p) and
  789. (vs = tsubscriptnode(p).vs);
  790. end;
  791. {*****************************************************************************
  792. TVECNODE
  793. *****************************************************************************}
  794. constructor tvecnode.create(l,r : tnode);
  795. begin
  796. inherited create(vecn,l,r);
  797. end;
  798. function tvecnode.pass_typecheck:tnode;
  799. var
  800. htype,elementdef,elementptrdef : tdef;
  801. newordtyp: tordtype;
  802. valid : boolean;
  803. begin
  804. result:=nil;
  805. typecheckpass(left);
  806. typecheckpass(right);
  807. { implicitly convert stringconstant to stringdef,
  808. see tbs/tb0476.pp for a test }
  809. if (left.nodetype=stringconstn) and
  810. (tstringconstnode(left).cst_type=cst_conststring) then
  811. begin
  812. if tstringconstnode(left).len>255 then
  813. inserttypeconv(left,getansistringdef)
  814. else
  815. inserttypeconv(left,cshortstringtype);
  816. end;
  817. { In p[1] p is always valid, it is not possible to
  818. declared a shortstring or normal array that has
  819. undefined number of elements. Dynamic array and
  820. ansi/widestring needs to be valid }
  821. valid:=is_dynamic_array(left.resultdef) or
  822. is_ansistring(left.resultdef) or
  823. is_wide_or_unicode_string(left.resultdef) or
  824. { implicit pointer dereference -> pointer is read }
  825. (left.resultdef.typ = pointerdef);
  826. if valid then
  827. set_varstate(left,vs_read,[vsf_must_be_valid]);
  828. {
  829. A vecn is, just like a loadn, always part of an expression with its
  830. own read/write and must_be_valid semantics. Therefore we don't have
  831. to do anything else here, just like for loadn's
  832. }
  833. set_varstate(right,vs_read,[vsf_must_be_valid]);
  834. if codegenerror then
  835. exit;
  836. { maybe type conversion for the index value, but
  837. do not convert range nodes }
  838. if (right.nodetype<>rangen) then
  839. case left.resultdef.typ of
  840. arraydef:
  841. begin
  842. htype:=Tarraydef(left.resultdef).rangedef;
  843. if ado_isvariant in Tarraydef(left.resultdef).arrayoptions then
  844. {Variant arrays are a special array, can have negative indexes and would therefore
  845. need s32bit. However, they should not appear in a vecn, as they are handled in
  846. handle_variantarray in pexpr.pas. Therefore, encountering a variant array is an
  847. internal error... }
  848. internalerror(200707031)
  849. { open array and array constructor range checking is handled
  850. below at the node level, where the validity of the index
  851. will be checked -> use a regular type conversion to either
  852. the signed or unsigned native int type to prevent another
  853. range check from getting inserted here (unless the type is
  854. larger than the int type). Exception: if it's an ordinal
  855. constant, because then this check should be performed at
  856. compile time }
  857. else if is_open_array(left.resultdef) or
  858. is_array_constructor(left.resultdef) then
  859. begin
  860. if is_signed(right.resultdef) and
  861. not is_constnode(right) then
  862. inserttypeconv(right,sizesinttype)
  863. else
  864. inserttypeconv(right,sizeuinttype)
  865. end
  866. else if is_special_array(left.resultdef) then
  867. {Arrays without a high bound (dynamic arrays, open arrays) are zero based,
  868. convert indexes into these arrays to aword.}
  869. inserttypeconv(right,uinttype)
  870. { note: <> rather than </>, because indexing e.g. an array 0..0
  871. must not result in truncating the indexing value from 2/4/8
  872. bytes to 1 byte (with range checking off, the full index
  873. value must be used) }
  874. else if (htype.typ=enumdef) and
  875. (right.resultdef.typ=enumdef) and
  876. (tenumdef(htype).basedef=tenumdef(right.resultdef).basedef) and
  877. ((tarraydef(left.resultdef).lowrange<>tenumdef(htype).min) or
  878. (tarraydef(left.resultdef).highrange<>tenumdef(htype).max)) then
  879. {Convert array indexes to low_bound..high_bound.}
  880. inserttypeconv(right,cenumdef.create_subrange(tenumdef(right.resultdef),
  881. asizeint(Tarraydef(left.resultdef).lowrange),
  882. asizeint(Tarraydef(left.resultdef).highrange)
  883. ))
  884. else if (htype.typ=orddef) and
  885. { right can also be a variant or another type with
  886. overloaded assignment }
  887. (right.resultdef.typ=orddef) and
  888. { don't try to create boolean types with custom ranges }
  889. not is_boolean(right.resultdef) and
  890. { ordtype determines the size of the loaded value -> make
  891. sure we don't truncate }
  892. ((Torddef(right.resultdef).ordtype<>torddef(htype).ordtype) or
  893. (tarraydef(left.resultdef).lowrange<>torddef(htype).low) or
  894. (tarraydef(left.resultdef).highrange<>torddef(htype).high)) then
  895. {Convert array indexes to low_bound..high_bound.}
  896. begin
  897. if (right.resultdef.typ=orddef)
  898. {$ifndef cpu64bitaddr}
  899. { do truncate 64 bit values on 32 bit cpus, since
  900. a) the arrays cannot be > 32 bit anyway
  901. b) their code generators can't directly handle 64 bit
  902. loads
  903. }
  904. and not is_64bit(right.resultdef)
  905. {$endif not cpu64bitaddr}
  906. then
  907. newordtyp:=Torddef(right.resultdef).ordtype
  908. else
  909. newordtyp:=torddef(sizesinttype).ordtype;
  910. inserttypeconv(right,corddef.create(newordtyp,
  911. int64(Tarraydef(left.resultdef).lowrange),
  912. int64(Tarraydef(left.resultdef).highrange),
  913. true
  914. ))
  915. end
  916. else
  917. inserttypeconv(right,htype)
  918. end;
  919. stringdef:
  920. if is_open_string(left.resultdef) then
  921. inserttypeconv(right,u8inttype)
  922. else if is_shortstring(left.resultdef) then
  923. {Convert shortstring indexes to 0..length.}
  924. inserttypeconv(right,corddef.create(u8bit,0,int64(Tstringdef(left.resultdef).len),true))
  925. else
  926. {Convert indexes into dynamically allocated strings to aword.}
  927. inserttypeconv(right,uinttype);
  928. pointerdef:
  929. inserttypeconv(right,tpointerdef(left.resultdef).pointer_arithmetic_int_type);
  930. else
  931. {Others, (are there any?) indexes to aint.}
  932. inserttypeconv(right,sinttype);
  933. end;
  934. { although we never put regular arrays or shortstrings in registers,
  935. it's possible that another type was typecasted to a small record
  936. that has a field of one of these types -> in that case the record
  937. can't be a regvar either }
  938. if ((left.resultdef.typ=arraydef) and
  939. not is_special_array(left.resultdef)) or
  940. ((left.resultdef.typ=stringdef) and
  941. (tstringdef(left.resultdef).stringtype in [st_shortstring,st_longstring])) then
  942. make_not_regable(left,[ra_addr_regable]);
  943. case left.resultdef.typ of
  944. arraydef :
  945. begin
  946. { check type of the index value }
  947. if (compare_defs(right.resultdef,tarraydef(left.resultdef).rangedef,right.nodetype)=te_incompatible) then
  948. IncompatibleTypes(right.resultdef,tarraydef(left.resultdef).rangedef);
  949. if right.nodetype=rangen then
  950. resultdef:=left.resultdef
  951. else
  952. resultdef:=Tarraydef(left.resultdef).elementdef;
  953. result:=gen_array_rangecheck;
  954. if assigned(result) then
  955. exit;
  956. { in case of a bitpacked array of enums that are size 2 (due to
  957. packenum 2) but whose values all fit in one byte, the size of
  958. bitpacked array elements will be 1 byte while the resultdef of
  959. will currently say it's two bytes) -> create a temp enumdef
  960. with packenum=1 for the resultdef as subtype of the main
  961. enumdef }
  962. if is_enum(resultdef) and
  963. is_packed_array(left.resultdef) and
  964. ((tarraydef(left.resultdef).elepackedbitsize div 8) <> resultdef.size) then
  965. begin
  966. resultdef:=cenumdef.create_subrange(tenumdef(resultdef),tenumdef(resultdef).min,tenumdef(resultdef).max);
  967. tenumdef(resultdef).calcsavesize(1);
  968. end
  969. end;
  970. pointerdef :
  971. begin
  972. { are we accessing a pointer[], then convert the pointer to
  973. an array first, in FPC this is allowed for all pointers
  974. (except voidpointer) in delphi/tp7 it's only allowed for pchars. }
  975. if not is_voidpointer(left.resultdef) and
  976. (
  977. (cs_pointermath in current_settings.localswitches) or
  978. tpointerdef(left.resultdef).has_pointer_math or
  979. is_pchar(left.resultdef) or
  980. is_pwidechar(left.resultdef)
  981. ) then
  982. begin
  983. { convert pointer to array }
  984. htype:=carraydef.create_from_pointer(tpointerdef(left.resultdef));
  985. inserttypeconv(left,htype);
  986. if right.nodetype=rangen then
  987. resultdef:=htype
  988. else
  989. resultdef:=tarraydef(htype).elementdef;
  990. end
  991. else
  992. CGMessage(type_e_array_required);
  993. end;
  994. stringdef :
  995. begin
  996. case tstringdef(left.resultdef).stringtype of
  997. st_unicodestring,
  998. st_widestring :
  999. begin
  1000. elementdef:=cwidechartype;
  1001. elementptrdef:=widecharpointertype;
  1002. end;
  1003. st_ansistring,
  1004. st_longstring,
  1005. st_shortstring :
  1006. begin
  1007. elementdef:=cansichartype;
  1008. elementptrdef:=charpointertype;
  1009. end;
  1010. else
  1011. internalerror(2013112902);
  1012. end;
  1013. if right.nodetype=rangen then
  1014. begin
  1015. htype:=carraydef.create_from_pointer(tpointerdef(elementptrdef));
  1016. resultdef:=htype;
  1017. end
  1018. else
  1019. begin
  1020. { indexed access to 0 element is only allowed for shortstrings or if
  1021. zero based strings is turned on }
  1022. if (right.nodetype=ordconstn) and
  1023. (Tordconstnode(right).value.svalue=0) and
  1024. not is_shortstring(left.resultdef) and
  1025. not(cs_zerobasedstrings in current_settings.localswitches) then
  1026. CGMessage(cg_e_can_access_element_zero);
  1027. resultdef:=elementdef;
  1028. end;
  1029. end;
  1030. variantdef :
  1031. resultdef:=cvarianttype;
  1032. else
  1033. CGMessage(type_e_array_required);
  1034. end;
  1035. end;
  1036. procedure Tvecnode.mark_write;
  1037. begin
  1038. include(flags,nf_write);
  1039. { see comment in tsubscriptnode.mark_write }
  1040. if not(is_implicit_pointer_object_type(left.resultdef)) then
  1041. left.mark_write;
  1042. end;
  1043. function tvecnode.pass_1 : tnode;
  1044. begin
  1045. result:=nil;
  1046. firstpass(left);
  1047. firstpass(right);
  1048. if codegenerror then
  1049. exit;
  1050. if (nf_callunique in flags) and
  1051. (is_ansistring(left.resultdef) or
  1052. is_unicodestring(left.resultdef) or
  1053. (is_widestring(left.resultdef) and not(tf_winlikewidestring in target_info.flags))) then
  1054. begin
  1055. left := ctypeconvnode.create_internal(ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+'_unique',
  1056. ccallparanode.create(
  1057. ctypeconvnode.create_internal(left,voidpointertype),nil)),
  1058. left.resultdef);
  1059. firstpass(left);
  1060. { double resultdef passes somwhere else may cause this to be }
  1061. { reset though :/ }
  1062. exclude(flags,nf_callunique);
  1063. end
  1064. else if is_widestring(left.resultdef) and (tf_winlikewidestring in target_info.flags) then
  1065. exclude(flags,nf_callunique);
  1066. { a range node as array index can only appear in function calls, and
  1067. those convert the range node into something else in
  1068. tcallnode.gen_high_tree }
  1069. if (right.nodetype=rangen) then
  1070. CGMessagePos(right.fileinfo,parser_e_illegal_expression)
  1071. else if left.resultdef.typ=arraydef then
  1072. result:=first_arraydef
  1073. else
  1074. begin
  1075. if left.expectloc=LOC_CREFERENCE then
  1076. expectloc:=LOC_CREFERENCE
  1077. else
  1078. expectloc:=LOC_REFERENCE
  1079. end;
  1080. end;
  1081. function tvecnode.first_arraydef: tnode;
  1082. begin
  1083. result:=nil;
  1084. if (not is_packed_array(left.resultdef)) or
  1085. ((tarraydef(left.resultdef).elepackedbitsize mod 8) = 0) then
  1086. if left.expectloc=LOC_CREFERENCE then
  1087. expectloc:=LOC_CREFERENCE
  1088. else
  1089. expectloc:=LOC_REFERENCE
  1090. else
  1091. if left.expectloc=LOC_CREFERENCE then
  1092. expectloc:=LOC_CSUBSETREF
  1093. else
  1094. expectloc:=LOC_SUBSETREF;
  1095. end;
  1096. function tvecnode.gen_array_rangecheck: tnode;
  1097. var
  1098. htype: tdef;
  1099. temp: ttempcreatenode;
  1100. stat: tstatementnode;
  1101. indextree: tnode;
  1102. hightree: tnode;
  1103. begin
  1104. result:=nil;
  1105. { Range checking an array of const/open array/dynamic array is
  1106. more complicated than regular arrays, because the bounds must
  1107. be checked dynamically. Additionally, in case of array of const
  1108. and open array we need the high parameter, which must not be
  1109. made a regvar in case this is a nested rountine relative to the
  1110. array parameter -> generate te check at the node tree level
  1111. rather than in the code generator }
  1112. if (cs_check_range in current_settings.localswitches) and
  1113. (is_open_array(left.resultdef) or
  1114. is_array_of_const(left.resultdef)) and
  1115. (right.nodetype<>rangen) then
  1116. begin
  1117. { expect to find the load node }
  1118. if get_open_const_array(left).nodetype<>loadn then
  1119. internalerror(2014040601);
  1120. { cdecl functions don't have high() so we can not check the range }
  1121. { (can't use current_procdef, since it may be a nested procedure) }
  1122. if not(tprocdef(tparasymtable(tparavarsym(tloadnode(get_open_const_array(left)).symtableentry).owner).defowner).proccalloption in cdecl_pocalls) then
  1123. begin
  1124. temp:=nil;
  1125. result:=internalstatements(stat);
  1126. { can't use node_complexity here, assumes that the code has
  1127. already been firstpassed }
  1128. if not is_const(right) then
  1129. begin
  1130. temp:=ctempcreatenode.create(right.resultdef,right.resultdef.size,tt_persistent,true);
  1131. addstatement(stat,temp);
  1132. { needed so we can typecheck its temprefnodes }
  1133. typecheckpass(tnode(temp));
  1134. addstatement(stat,cassignmentnode.create(
  1135. ctemprefnode.create(temp),right)
  1136. );
  1137. right:=ctemprefnode.create(temp);
  1138. { right.resultdef is used below }
  1139. typecheckpass(right);
  1140. end;
  1141. { range check will be made explicit here }
  1142. exclude(localswitches,cs_check_range);
  1143. hightree:=load_high_value_node(tparavarsym(tloadnode(
  1144. get_open_const_array(left)).symtableentry));
  1145. { make index unsigned so we only need one comparison;
  1146. lower bound is always zero for these arrays, but
  1147. hightree can be -1 in case the array was empty ->
  1148. add 1 before comparing (ignoring overflows) }
  1149. htype:=get_unsigned_inttype(right.resultdef);
  1150. inserttypeconv_explicit(hightree,htype);
  1151. hightree:=caddnode.create(addn,hightree,genintconstnode(1));
  1152. hightree.localswitches:=hightree.localswitches-[cs_check_range,
  1153. cs_check_overflow];
  1154. indextree:=ctypeconvnode.create_explicit(right.getcopy,htype);
  1155. { range error if index >= hightree+1 }
  1156. addstatement(stat,
  1157. cifnode.create_internal(
  1158. caddnode.create_internal(gten,indextree,hightree),
  1159. ccallnode.createintern('fpc_rangeerror',nil),
  1160. nil
  1161. )
  1162. );
  1163. if assigned(temp) then
  1164. addstatement(stat,ctempdeletenode.create_normal_temp(temp));
  1165. addstatement(stat,self.getcopy);
  1166. end;
  1167. end;
  1168. end;
  1169. {*****************************************************************************
  1170. TWITHNODE
  1171. *****************************************************************************}
  1172. constructor twithnode.create(l:tnode);
  1173. begin
  1174. inherited create(withn,l);
  1175. fileinfo:=l.fileinfo;
  1176. end;
  1177. destructor twithnode.destroy;
  1178. begin
  1179. inherited destroy;
  1180. end;
  1181. constructor twithnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1182. begin
  1183. inherited ppuload(t,ppufile);
  1184. end;
  1185. procedure twithnode.ppuwrite(ppufile:tcompilerppufile);
  1186. begin
  1187. inherited ppuwrite(ppufile);
  1188. end;
  1189. function twithnode.dogetcopy : tnode;
  1190. var
  1191. p : twithnode;
  1192. begin
  1193. p:=twithnode(inherited dogetcopy);
  1194. result:=p;
  1195. end;
  1196. function twithnode.pass_typecheck:tnode;
  1197. begin
  1198. result:=nil;
  1199. resultdef:=voidtype;
  1200. if assigned(left) then
  1201. typecheckpass(left);
  1202. end;
  1203. function twithnode.pass_1 : tnode;
  1204. begin
  1205. result:=nil;
  1206. expectloc:=LOC_VOID;
  1207. end;
  1208. function twithnode.docompare(p: tnode): boolean;
  1209. begin
  1210. docompare :=
  1211. inherited docompare(p);
  1212. end;
  1213. function is_big_untyped_addrnode(p: tnode): boolean;
  1214. begin
  1215. is_big_untyped_addrnode:=(p.nodetype=addrn) and
  1216. not (anf_typedaddr in taddrnode(p).addrnodeflags) and
  1217. (taddrnode(p).left.resultdef.size > 1);
  1218. end;
  1219. end.