nflw.pas 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and register allocation for nodes that influence
  4. the flow
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nflw;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. node,cpubase,
  24. symnot,
  25. symtype,symbase,symdef,symsym,
  26. optloop;
  27. type
  28. { flags used by loop nodes }
  29. tloopflag = (
  30. { set if it is a for ... downto ... do loop }
  31. lnf_backward,
  32. { Do we need to parse childs to set var state? }
  33. lnf_varstate,
  34. { Do a test at the begin of the loop?}
  35. lnf_testatbegin,
  36. { Negate the loop test? }
  37. lnf_checknegate,
  38. { Should the value of the loop variable on exit be correct. }
  39. lnf_dont_mind_loopvar_on_exit,
  40. { Loop simplify flag }
  41. lnf_simplify_processing);
  42. tloopflags = set of tloopflag;
  43. const
  44. { loop flags which must match to consider loop nodes equal regarding the flags }
  45. loopflagsequal = [lnf_backward];
  46. type
  47. tlabelnode = class;
  48. tloopnode = class(tbinarynode)
  49. t1,t2 : tnode;
  50. loopflags : tloopflags;
  51. constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
  52. destructor destroy;override;
  53. function dogetcopy : tnode;override;
  54. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  55. procedure ppuwrite(ppufile:tcompilerppufile);override;
  56. procedure buildderefimpl;override;
  57. procedure derefimpl;override;
  58. procedure insertintolist(l : tnodelist);override;
  59. procedure printnodetree(var t:text);override;
  60. function docompare(p: tnode): boolean; override;
  61. end;
  62. twhilerepeatnode = class(tloopnode)
  63. constructor create(l,r:Tnode;tab,cn:boolean);virtual;reintroduce;
  64. function pass_typecheck:tnode;override;
  65. function pass_1 : tnode;override;
  66. {$ifdef state_tracking}
  67. function track_state_pass(exec_known:boolean):boolean;override;
  68. {$endif}
  69. end;
  70. twhilerepeatnodeclass = class of twhilerepeatnode;
  71. tifnode = class(tloopnode)
  72. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  73. function pass_typecheck:tnode;override;
  74. function pass_1 : tnode;override;
  75. function simplify(forinline : boolean) : tnode;override;
  76. private
  77. function internalsimplify(warn: boolean) : tnode;
  78. end;
  79. tifnodeclass = class of tifnode;
  80. tfornode = class(tloopnode)
  81. { if count isn divisable by unrolls then
  82. the for loop must jump to this label to get the correct
  83. number of executions }
  84. entrylabel : tnode;
  85. loopvar_notid:cardinal;
  86. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;reintroduce;
  87. procedure loop_var_access(not_type:Tnotification_flag;symbol:Tsym);
  88. function pass_typecheck:tnode;override;
  89. function pass_1 : tnode;override;
  90. function simplify(forinline : boolean) : tnode;override;
  91. end;
  92. tfornodeclass = class of tfornode;
  93. texitnode = class(tunarynode)
  94. constructor create(l:tnode);virtual;
  95. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  96. procedure ppuwrite(ppufile:tcompilerppufile);override;
  97. function pass_typecheck:tnode;override;
  98. function pass_1 : tnode;override;
  99. end;
  100. texitnodeclass = class of texitnode;
  101. tbreaknode = class(tnode)
  102. constructor create;virtual;
  103. function pass_typecheck:tnode;override;
  104. function pass_1 : tnode;override;
  105. end;
  106. tbreaknodeclass = class of tbreaknode;
  107. tcontinuenode = class(tnode)
  108. constructor create;virtual;
  109. function pass_typecheck:tnode;override;
  110. function pass_1 : tnode;override;
  111. end;
  112. tcontinuenodeclass = class of tcontinuenode;
  113. tgotonode = class(tnode)
  114. private
  115. labelnodeidx : longint;
  116. public
  117. labelsym : tlabelsym;
  118. labelnode : tlabelnode;
  119. exceptionblock : integer;
  120. constructor create(p : tlabelsym);virtual;
  121. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  122. procedure ppuwrite(ppufile:tcompilerppufile);override;
  123. procedure buildderefimpl;override;
  124. procedure derefimpl;override;
  125. procedure resolveppuidx;override;
  126. function dogetcopy : tnode;override;
  127. function pass_typecheck:tnode;override;
  128. function pass_1 : tnode;override;
  129. function docompare(p: tnode): boolean; override;
  130. end;
  131. tgotonodeclass = class of tgotonode;
  132. tlabelnode = class(tunarynode)
  133. exceptionblock : integer;
  134. { when copying trees, this points to the newly created copy of a label }
  135. copiedto : tlabelnode;
  136. labsym : tlabelsym;
  137. constructor create(l:tnode;alabsym:tlabelsym);virtual;
  138. destructor destroy;override;
  139. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  140. procedure ppuwrite(ppufile:tcompilerppufile);override;
  141. procedure buildderefimpl;override;
  142. procedure derefimpl;override;
  143. function dogetcopy : tnode;override;
  144. function pass_typecheck:tnode;override;
  145. function pass_1 : tnode;override;
  146. function docompare(p: tnode): boolean; override;
  147. end;
  148. tlabelnodeclass = class of tlabelnode;
  149. traisenode = class(ttertiarynode)
  150. constructor create(l,taddr,tframe:tnode);virtual;
  151. function pass_typecheck:tnode;override;
  152. function pass_1 : tnode;override;
  153. end;
  154. traisenodeclass = class of traisenode;
  155. ttryexceptnode = class(tloopnode)
  156. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  157. function pass_typecheck:tnode;override;
  158. function pass_1 : tnode;override;
  159. end;
  160. ttryexceptnodeclass = class of ttryexceptnode;
  161. ttryfinallynode = class(tloopnode)
  162. implicitframe : boolean;
  163. constructor create(l,r:tnode);virtual;reintroduce;
  164. constructor create_implicit(l,r,_t1:tnode);virtual;
  165. function pass_typecheck:tnode;override;
  166. function pass_1 : tnode;override;
  167. function simplify(forinline:boolean): tnode;override;
  168. end;
  169. ttryfinallynodeclass = class of ttryfinallynode;
  170. tonnode = class(tbinarynode)
  171. excepTSymtable : TSymtable;
  172. excepttype : tobjectdef;
  173. constructor create(l,r:tnode);virtual;
  174. destructor destroy;override;
  175. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  176. function pass_typecheck:tnode;override;
  177. function pass_1 : tnode;override;
  178. function dogetcopy : tnode;override;
  179. function docompare(p: tnode): boolean; override;
  180. end;
  181. tonnodeclass = class of tonnode;
  182. var
  183. cwhilerepeatnode : twhilerepeatnodeclass=twhilerepeatnode;
  184. cifnode : tifnodeclass = tifnode;
  185. cfornode : tfornodeclass = tfornode;
  186. cexitnode : texitnodeclass = texitnode;
  187. cgotonode : tgotonodeclass = tgotonode;
  188. clabelnode : tlabelnodeclass = tlabelnode;
  189. craisenode : traisenodeclass = traisenode;
  190. ctryexceptnode : ttryexceptnodeclass = ttryexceptnode;
  191. ctryfinallynode : ttryfinallynodeclass = ttryfinallynode;
  192. connode : tonnodeclass = tonnode;
  193. cbreaknode : tbreaknodeclass = tbreaknode;
  194. ccontinuenode : tcontinuenodeclass = tcontinuenode;
  195. // for-in loop helpers
  196. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  197. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  198. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  199. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  200. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  201. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  202. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  203. implementation
  204. uses
  205. globtype,systems,constexp,
  206. cutils,verbose,globals,
  207. symconst,symtable,paramgr,defcmp,defutil,htypechk,pass_1,
  208. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,nutils,ninl,nset,
  209. {$ifdef state_tracking}
  210. nstate,
  211. {$endif}
  212. cgbase,procinfo
  213. ;
  214. // for-in loop helpers
  215. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  216. begin
  217. result:=cfornode.create(hloopvar,
  218. cinlinenode.create(in_low_x,false,expr.getcopy),
  219. cinlinenode.create(in_high_x,false,expr.getcopy),
  220. hloopbody,
  221. false);
  222. end;
  223. function create_objc_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  224. var
  225. mainstatement, outerloopbodystatement, innerloopbodystatement, tempstatement: tstatementnode;
  226. state, mutationcheck, currentamount, innerloopcounter, items, expressiontemp: ttempcreatenode;
  227. outerloop, innerloop, hp: tnode;
  228. itemsarraydef: tarraydef;
  229. sym: tsym;
  230. begin
  231. { Objective-C enumerators require Objective-C 2.0 }
  232. if not(m_objectivec2 in current_settings.modeswitches) then
  233. begin
  234. result:=cerrornode.create;
  235. MessagePos(expr.fileinfo,parser_e_objc_enumerator_2_0);
  236. exit;
  237. end;
  238. { Requires the NSFastEnumeration protocol and NSFastEnumerationState
  239. record }
  240. maybeloadcocoatypes;
  241. if not assigned(objc_fastenumeration) or
  242. not assigned(objc_fastenumerationstate) then
  243. begin
  244. result:=cerrornode.create;
  245. MessagePos(expr.fileinfo,parser_e_objc_missing_enumeration_defs);
  246. exit;
  247. end;
  248. (* Original code:
  249. for hloopvar in expression do
  250. <hloopbody>
  251. Pascal code equivalent into which it has to be transformed
  252. (sure would be nice if the compiler had some kind of templates ;) :
  253. var
  254. state: NSFastEnumerationState;
  255. expressiontemp: NSFastEnumerationProtocol;
  256. mutationcheck,
  257. currentamount,
  258. innerloopcounter: culong;
  259. { size can be increased/decreased if desired }
  260. items: array[1..16] of id;
  261. begin
  262. fillchar(state,sizeof(state),0);
  263. expressiontemp:=expression;
  264. repeat
  265. currentamount:=expressiontemp.countByEnumeratingWithState_objects_count(@state,@items,length(items));
  266. if currentamount=0 then
  267. begin
  268. { "The iterating variable is set to nil when the loop ends by
  269. exhausting the source pool of objects" }
  270. hloopvar:=nil;
  271. break;
  272. end;
  273. mutationcheck:=state.mutationsptr^;
  274. innerloopcounter:=culong(-1);
  275. repeat
  276. { at the start so that "continue" in <loopbody> works correctly }
  277. { don't use for-loop, because then the value of the iteration
  278. counter is undefined on exit and we have to check it in the
  279. outer repeat/until condition }
  280. {$push}
  281. {$r-,q-}
  282. inc(innerloopcounter);
  283. {$pop}
  284. if innerloopcounter=currentamount then
  285. break;
  286. if mutationcheck<>state.mutationsptr^ then
  287. { raises Objective-C exception... }
  288. objc_enumerationMutation(expressiontemp);
  289. hloopvar:=state.itemsPtr[innerloopcounter];
  290. { if continue in loopbody -> jumps to start, increases count and checks }
  291. { if break in loopbody: goes to outer repeat/until and innerloopcount
  292. will be < currentamount -> stops }
  293. <hloopbody>
  294. until false;
  295. { if the inner loop terminated early, "break" was used and we have
  296. to stop }
  297. { "If the loop is terminated early, the iterating variable is left
  298. pointing to the last iteration item." }
  299. until innerloopcounter<currentamount;
  300. end;
  301. *)
  302. result:=internalstatements(mainstatement);
  303. { the fast enumeration state }
  304. state:=ctempcreatenode.create(objc_fastenumerationstate,objc_fastenumerationstate.size,tt_persistent,false);
  305. typecheckpass(tnode(state));
  306. addstatement(mainstatement,state);
  307. { the temporary items array }
  308. itemsarraydef:=tarraydef.create(1,16,u32inttype);
  309. itemsarraydef.elementdef:=objc_idtype;
  310. items:=ctempcreatenode.create(itemsarraydef,itemsarraydef.size,tt_persistent,false);
  311. addstatement(mainstatement,items);
  312. typecheckpass(tnode(items));
  313. { temp for the expression/collection through which we iterate }
  314. expressiontemp:=ctempcreatenode.create(objc_fastenumeration,objc_fastenumeration.size,tt_persistent,true);
  315. addstatement(mainstatement,expressiontemp);
  316. { currentamount temp (not really clean: we use ptruint instead of
  317. culong) }
  318. currentamount:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  319. typecheckpass(tnode(currentamount));
  320. addstatement(mainstatement,currentamount);
  321. { mutationcheck temp (idem) }
  322. mutationcheck:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  323. typecheckpass(tnode(mutationcheck));
  324. addstatement(mainstatement,mutationcheck);
  325. { innerloopcounter temp (idem) }
  326. innerloopcounter:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  327. typecheckpass(tnode(innerloopcounter));
  328. addstatement(mainstatement,innerloopcounter);
  329. { initialise the state with 0 }
  330. addstatement(mainstatement,ccallnode.createinternfromunit('SYSTEM','FILLCHAR',
  331. ccallparanode.create(genintconstnode(0),
  332. ccallparanode.create(genintconstnode(objc_fastenumerationstate.size),
  333. ccallparanode.create(ctemprefnode.create(state),nil)
  334. )
  335. )
  336. ));
  337. { this will also check whether the expression (potentially) conforms
  338. to the NSFastEnumeration protocol (use expr.getcopy, because the
  339. caller will free expr) }
  340. addstatement(mainstatement,cassignmentnode.create(ctemprefnode.create(expressiontemp),expr.getcopy));
  341. { we add the "repeat..until" afterwards, now just create the body }
  342. outerloop:=internalstatements(outerloopbodystatement);
  343. { the countByEnumeratingWithState_objects_count call }
  344. hp:=ccallparanode.create(cinlinenode.create(in_length_x,false,ctypenode.create(itemsarraydef)),
  345. ccallparanode.create(caddrnode.create(ctemprefnode.create(items)),
  346. ccallparanode.create(caddrnode.create(ctemprefnode.create(state)),nil)
  347. )
  348. );
  349. sym:=search_struct_member(objc_fastenumeration,'COUNTBYENUMERATINGWITHSTATE_OBJECTS_COUNT');
  350. if not assigned(sym) or
  351. (sym.typ<>procsym) then
  352. internalerror(2010061901);
  353. hp:=ccallnode.create(hp,tprocsym(sym),sym.owner,ctemprefnode.create(expressiontemp),[]);
  354. addstatement(outerloopbodystatement,cassignmentnode.create(
  355. ctemprefnode.create(currentamount),hp));
  356. { if currentamount = 0, bail out (use copy of hloopvar, because we
  357. have to use it again below) }
  358. hp:=internalstatements(tempstatement);
  359. addstatement(tempstatement,cassignmentnode.create(
  360. hloopvar.getcopy,cnilnode.create));
  361. addstatement(tempstatement,cbreaknode.create);
  362. addstatement(outerloopbodystatement,cifnode.create(
  363. caddnode.create(equaln,ctemprefnode.create(currentamount),genintconstnode(0)),
  364. hp,nil));
  365. { initial value of mutationcheck }
  366. hp:=ctemprefnode.create(state);
  367. typecheckpass(hp);
  368. hp:=cderefnode.create(genloadfield(hp,'MUTATIONSPTR'));
  369. addstatement(outerloopbodystatement,cassignmentnode.create(
  370. ctemprefnode.create(mutationcheck),hp));
  371. { initialise innerloopcounter }
  372. addstatement(outerloopbodystatement,cassignmentnode.create(
  373. ctemprefnode.create(innerloopcounter),cordconstnode.create(-1,ptruinttype,false)));
  374. { and now the inner loop, again adding the repeat/until afterwards }
  375. innerloop:=internalstatements(innerloopbodystatement);
  376. { inc(innerloopcounter) without range/overflowchecking (because
  377. we go from culong(-1) to 0 during the first iteration }
  378. hp:=cinlinenode.create(
  379. in_inc_x,false,ccallparanode.create(
  380. ctemprefnode.create(innerloopcounter),nil));
  381. hp.localswitches:=hp.localswitches-[cs_check_range,cs_check_overflow];
  382. addstatement(innerloopbodystatement,hp);
  383. { if innerloopcounter=currentamount then break to the outer loop }
  384. addstatement(innerloopbodystatement,cifnode.create(
  385. caddnode.create(equaln,
  386. ctemprefnode.create(innerloopcounter),
  387. ctemprefnode.create(currentamount)),
  388. cbreaknode.create,
  389. nil));
  390. { verify that the collection didn't change in the mean time }
  391. hp:=ctemprefnode.create(state);
  392. typecheckpass(hp);
  393. addstatement(innerloopbodystatement,cifnode.create(
  394. caddnode.create(unequaln,
  395. ctemprefnode.create(mutationcheck),
  396. cderefnode.create(genloadfield(hp,'MUTATIONSPTR'))
  397. ),
  398. ccallnode.createinternfromunit('OBJC','OBJC_ENUMERATIONMUTATION',
  399. ccallparanode.create(ctemprefnode.create(expressiontemp),nil)),
  400. nil));
  401. { finally: actually get the next element }
  402. hp:=ctemprefnode.create(state);
  403. typecheckpass(hp);
  404. hp:=genloadfield(hp,'ITEMSPTR');
  405. typecheckpass(hp);
  406. { don't simply use a vecn, because indexing a pointer won't work in
  407. non-FPC modes }
  408. if hp.resultdef.typ<>pointerdef then
  409. internalerror(2010061904);
  410. inserttypeconv(hp,
  411. tarraydef.create_from_pointer(tpointerdef(hp.resultdef).pointeddef));
  412. hp:=cvecnode.create(hp,ctemprefnode.create(innerloopcounter));
  413. addstatement(innerloopbodystatement,
  414. cassignmentnode.create(hloopvar,hp));
  415. { the actual loop body! }
  416. addstatement(innerloopbodystatement,hloopbody);
  417. { create the inner repeat/until and add it to the body of the outer
  418. one }
  419. hp:=cwhilerepeatnode.create(
  420. { repeat .. until false }
  421. cordconstnode.create(0,booltype,false),innerloop,false,true);
  422. addstatement(outerloopbodystatement,hp);
  423. { create the outer repeat/until and add it to the the main body }
  424. hp:=cwhilerepeatnode.create(
  425. { repeat .. until innerloopcounter<currentamount }
  426. caddnode.create(ltn,
  427. ctemprefnode.create(innerloopcounter),
  428. ctemprefnode.create(currentamount)),
  429. outerloop,false,true);
  430. addstatement(mainstatement,hp);
  431. { release the temps }
  432. addstatement(mainstatement,ctempdeletenode.create(state));
  433. addstatement(mainstatement,ctempdeletenode.create(mutationcheck));
  434. addstatement(mainstatement,ctempdeletenode.create(currentamount));
  435. addstatement(mainstatement,ctempdeletenode.create(innerloopcounter));
  436. addstatement(mainstatement,ctempdeletenode.create(items));
  437. addstatement(mainstatement,ctempdeletenode.create(expressiontemp));
  438. end;
  439. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  440. var
  441. loopstatement, loopbodystatement: tstatementnode;
  442. loopvar, stringvar: ttempcreatenode;
  443. stringindex, loopbody, forloopnode: tnode;
  444. begin
  445. { result is a block of statements }
  446. result:=internalstatements(loopstatement);
  447. { create a temp variable for expression }
  448. stringvar := ctempcreatenode.create(
  449. expr.resultdef,
  450. expr.resultdef.size,
  451. tt_persistent,true);
  452. addstatement(loopstatement,stringvar);
  453. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(stringvar),expr.getcopy));
  454. { create a loop counter: signed integer with size of string length }
  455. loopvar := ctempcreatenode.create(
  456. sinttype,
  457. sinttype.size,
  458. tt_persistent,true);
  459. addstatement(loopstatement,loopvar);
  460. stringindex:=ctemprefnode.create(loopvar);
  461. loopbody:=internalstatements(loopbodystatement);
  462. // for-in loop variable := string_expression[index]
  463. addstatement(loopbodystatement,
  464. cassignmentnode.create(hloopvar, cvecnode.create(ctemprefnode.create(stringvar),stringindex)));
  465. { add the actual statement to the loop }
  466. addstatement(loopbodystatement,hloopbody);
  467. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  468. genintconstnode(1),
  469. cinlinenode.create(in_length_x,false,ctemprefnode.create(stringvar)),
  470. loopbody,
  471. false);
  472. addstatement(loopstatement,forloopnode);
  473. { free the loop counter }
  474. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  475. { free the temp variable for expression }
  476. addstatement(loopstatement,ctempdeletenode.create(stringvar));
  477. end;
  478. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  479. var
  480. loopstatement, loopbodystatement: tstatementnode;
  481. loopvar, arrayvar: ttempcreatenode;
  482. arrayindex, lowbound, highbound, loopbody, forloopnode, expression: tnode;
  483. is_string: boolean;
  484. tmpdef, convertdef: tdef;
  485. elementcount: aword;
  486. begin
  487. expression := expr;
  488. { result is a block of statements }
  489. result:=internalstatements(loopstatement);
  490. is_string:=ado_IsConstString in tarraydef(expr.resultdef).arrayoptions;
  491. // if array element type <> loovar type then create a conversion if possible
  492. if compare_defs(tarraydef(expression.resultdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible then
  493. begin
  494. tmpdef:=expression.resultdef;
  495. elementcount:=1;
  496. while assigned(tmpdef) and (tmpdef.typ=arraydef) and
  497. (tarraydef(tmpdef).arrayoptions = []) and
  498. (compare_defs(tarraydef(tmpdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible) do
  499. begin
  500. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  501. tmpdef:=tarraydef(tmpdef).elementdef;
  502. end;
  503. if assigned(tmpdef) and (tmpdef.typ=arraydef) and (tarraydef(tmpdef).arrayoptions = []) then
  504. begin
  505. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  506. convertdef:=tarraydef.create(0,elementcount-1,s32inttype);
  507. tarraydef(convertdef).elementdef:=tarraydef(tmpdef).elementdef;
  508. expression:=expr.getcopy;
  509. expression:=ctypeconvnode.create_internal(expression,convertdef);
  510. typecheckpass(expression);
  511. addstatement(loopstatement,expression);
  512. end;
  513. end;
  514. if (node_complexity(expression) > 1) and not is_open_array(expression.resultdef) then
  515. begin
  516. { create a temp variable for expression }
  517. arrayvar := ctempcreatenode.create(
  518. expression.resultdef,
  519. expression.resultdef.size,
  520. tt_persistent,true);
  521. if is_string then
  522. begin
  523. lowbound:=genintconstnode(1);
  524. highbound:=cinlinenode.create(in_length_x,false,ctemprefnode.create(arrayvar))
  525. end
  526. else
  527. begin
  528. lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
  529. highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
  530. end;
  531. addstatement(loopstatement,arrayvar);
  532. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(arrayvar),expression.getcopy));
  533. end
  534. else
  535. begin
  536. arrayvar:=nil;
  537. if is_string then
  538. begin
  539. lowbound:=genintconstnode(1);
  540. highbound:=cinlinenode.create(in_length_x,false,expression.getcopy);
  541. end
  542. else
  543. begin
  544. lowbound:=cinlinenode.create(in_low_x,false,expression.getcopy);
  545. highbound:=cinlinenode.create(in_high_x,false,expression.getcopy);
  546. end;
  547. end;
  548. { create a loop counter }
  549. loopvar := ctempcreatenode.create(
  550. tarraydef(expression.resultdef).rangedef,
  551. tarraydef(expression.resultdef).rangedef.size,
  552. tt_persistent,true);
  553. addstatement(loopstatement,loopvar);
  554. arrayindex:=ctemprefnode.create(loopvar);
  555. loopbody:=internalstatements(loopbodystatement);
  556. // for-in loop variable := array_expression[index]
  557. if assigned(arrayvar) then
  558. addstatement(loopbodystatement,
  559. cassignmentnode.create(hloopvar,cvecnode.create(ctemprefnode.create(arrayvar),arrayindex)))
  560. else
  561. addstatement(loopbodystatement,
  562. cassignmentnode.create(hloopvar,cvecnode.create(expression.getcopy,arrayindex)));
  563. { add the actual statement to the loop }
  564. addstatement(loopbodystatement,hloopbody);
  565. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  566. lowbound,
  567. highbound,
  568. loopbody,
  569. false);
  570. addstatement(loopstatement,forloopnode);
  571. { free the loop counter }
  572. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  573. { free the temp variable for expression if needed }
  574. if arrayvar<>nil then
  575. addstatement(loopstatement,ctempdeletenode.create(arrayvar));
  576. end;
  577. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  578. var
  579. loopstatement, loopbodystatement: tstatementnode;
  580. loopvar, setvar: ttempcreatenode;
  581. loopbody, forloopnode: tnode;
  582. begin
  583. // first check is set is empty and if it so then skip other processing
  584. if not Assigned(tsetdef(expr.resultdef).elementdef) then
  585. begin
  586. result:=cnothingnode.create;
  587. // free unused nodes
  588. hloopvar.free;
  589. hloopbody.free;
  590. exit;
  591. end;
  592. { result is a block of statements }
  593. result:=internalstatements(loopstatement);
  594. { create a temp variable for expression }
  595. setvar := ctempcreatenode.create(
  596. expr.resultdef,
  597. expr.resultdef.size,
  598. tt_persistent,true);
  599. addstatement(loopstatement,setvar);
  600. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(setvar),expr.getcopy));
  601. { create a loop counter }
  602. loopvar := ctempcreatenode.create(
  603. tsetdef(expr.resultdef).elementdef,
  604. tsetdef(expr.resultdef).elementdef.size,
  605. tt_persistent,true);
  606. addstatement(loopstatement,loopvar);
  607. // if loopvar in set then
  608. // begin
  609. // hloopvar := loopvar
  610. // for-in loop body
  611. // end
  612. loopbody:=cifnode.create(
  613. cinnode.create(ctemprefnode.create(loopvar),ctemprefnode.create(setvar)),
  614. internalstatements(loopbodystatement),
  615. nil);
  616. addstatement(loopbodystatement,cassignmentnode.create(hloopvar,ctemprefnode.create(loopvar)));
  617. { add the actual statement to the loop }
  618. addstatement(loopbodystatement,hloopbody);
  619. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  620. cinlinenode.create(in_low_x,false,ctemprefnode.create(setvar)),
  621. cinlinenode.create(in_high_x,false,ctemprefnode.create(setvar)),
  622. loopbody,
  623. false);
  624. addstatement(loopstatement,forloopnode);
  625. { free the loop counter }
  626. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  627. { free the temp variable for expression }
  628. addstatement(loopstatement,ctempdeletenode.create(setvar));
  629. end;
  630. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  631. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  632. var
  633. loopstatement, loopbodystatement: tstatementnode;
  634. enumvar: ttempcreatenode;
  635. loopbody, whileloopnode,
  636. enum_get, enum_move, enum_current, enum_get_params: tnode;
  637. propaccesslist: tpropaccesslist;
  638. enumerator_is_class: boolean;
  639. enumerator_destructor: tprocdef;
  640. begin
  641. { result is a block of statements }
  642. result:=internalstatements(loopstatement);
  643. enumerator_is_class := is_class(enumerator_get.returndef);
  644. { create a temp variable for enumerator }
  645. enumvar := ctempcreatenode.create(
  646. enumerator_get.returndef,
  647. enumerator_get.returndef.size,
  648. tt_persistent,true);
  649. addstatement(loopstatement,enumvar);
  650. if enumerator_get.proctypeoption=potype_operator then
  651. begin
  652. enum_get_params:=ccallparanode.create(expr.getcopy,nil);
  653. enum_get:=ccallnode.create(enum_get_params, tprocsym(enumerator_get.procsym), nil, nil, []);
  654. tcallnode(enum_get).procdefinition:=enumerator_get;
  655. addsymref(enumerator_get.procsym);
  656. end
  657. else
  658. enum_get:=ccallnode.create(nil, tprocsym(enumerator_get.procsym), enumerator_get.owner, expr.getcopy, []);
  659. addstatement(loopstatement,
  660. cassignmentnode.create(
  661. ctemprefnode.create(enumvar),
  662. enum_get
  663. ));
  664. loopbody:=internalstatements(loopbodystatement);
  665. { for-in loop variable := enumerator.current }
  666. if getpropaccesslist(enumerator_current,palt_read,propaccesslist) then
  667. begin
  668. case propaccesslist.firstsym^.sym.typ of
  669. fieldvarsym :
  670. begin
  671. { generate access code }
  672. enum_current:=ctemprefnode.create(enumvar);
  673. propaccesslist_to_node(enum_current,enumerator_current.owner,propaccesslist);
  674. include(enum_current.flags,nf_isproperty);
  675. end;
  676. procsym :
  677. begin
  678. { generate the method call }
  679. enum_current:=ccallnode.create(nil,tprocsym(propaccesslist.firstsym^.sym),enumerator_current.owner,ctemprefnode.create(enumvar),[]);
  680. include(enum_current.flags,nf_isproperty);
  681. end
  682. else
  683. begin
  684. enum_current:=cerrornode.create;
  685. Message(type_e_mismatch);
  686. end;
  687. end;
  688. end
  689. else
  690. enum_current:=cerrornode.create;
  691. addstatement(loopbodystatement,
  692. cassignmentnode.create(hloopvar, enum_current));
  693. { add the actual statement to the loop }
  694. addstatement(loopbodystatement,hloopbody);
  695. enum_move:=ccallnode.create(nil, tprocsym(enumerator_move.procsym), enumerator_move.owner, ctemprefnode.create(enumvar), []);
  696. whileloopnode:=cwhilerepeatnode.create(enum_move,loopbody,true,false);
  697. if enumerator_is_class then
  698. begin
  699. { insert a try-finally and call the destructor for the enumerator in the finally section }
  700. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  701. if assigned(enumerator_destructor) then
  702. begin
  703. whileloopnode:=ctryfinallynode.create(
  704. whileloopnode, // try node
  705. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym), // finally node
  706. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[]));
  707. end;
  708. { if getenumerator <> nil then do the loop }
  709. whileloopnode:=cifnode.create(
  710. caddnode.create(unequaln, ctemprefnode.create(enumvar), cnilnode.create),
  711. whileloopnode,
  712. nil);
  713. end;
  714. addstatement(loopstatement, whileloopnode);
  715. if is_object(enumerator_get.returndef) then
  716. begin
  717. // call the object destructor too
  718. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  719. if assigned(enumerator_destructor) then
  720. begin
  721. addstatement(loopstatement,
  722. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym),
  723. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[]));
  724. end;
  725. end;
  726. { free the temp variable for enumerator }
  727. addstatement(loopstatement,ctempdeletenode.create(enumvar));
  728. end;
  729. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  730. var
  731. pd, movenext: tprocdef;
  732. current: tpropertysym;
  733. storefilepos: tfileposinfo;
  734. begin
  735. storefilepos:=current_filepos;
  736. current_filepos:=hloopvar.fileinfo;
  737. if expr.nodetype=typen then
  738. begin
  739. if (expr.resultdef.typ=enumdef) and tenumdef(expr.resultdef).has_jumps then
  740. begin
  741. result:=cerrornode.create;
  742. hloopvar.free;
  743. hloopbody.free;
  744. MessagePos1(expr.fileinfo,parser_e_for_in_loop_cannot_be_used_for_the_type,expr.resultdef.typename);
  745. end
  746. else
  747. result:=create_type_for_in_loop(hloopvar, hloopbody, expr);
  748. end
  749. else
  750. begin
  751. { loop is made for an expression }
  752. // Objective-C uses different conventions (and it's only supported for Objective-C 2.0)
  753. if is_objc_class_or_protocol(hloopvar.resultdef) or
  754. is_objc_class_or_protocol(expr.resultdef) then
  755. begin
  756. result:=create_objc_for_in_loop(hloopvar,hloopbody,expr);
  757. if result.nodetype=errorn then
  758. begin
  759. hloopvar.free;
  760. hloopbody.free;
  761. end;
  762. end
  763. else
  764. begin
  765. // search for operator first
  766. pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
  767. // if there is no operator then search for class/object/record enumerator method
  768. if (pd=nil) and (expr.resultdef.typ in [objectdef,recorddef]) then
  769. pd:=tabstractrecorddef(expr.resultdef).search_enumerator_get;
  770. if pd<>nil then
  771. begin
  772. // seach movenext and current symbols
  773. movenext:=tabstractrecorddef(pd.returndef).search_enumerator_move;
  774. if movenext = nil then
  775. begin
  776. result:=cerrornode.create;
  777. hloopvar.free;
  778. hloopbody.free;
  779. MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
  780. end
  781. else
  782. begin
  783. current:=tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  784. if current = nil then
  785. begin
  786. result:=cerrornode.create;
  787. hloopvar.free;
  788. hloopbody.free;
  789. MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
  790. end
  791. else
  792. result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
  793. end;
  794. end
  795. else
  796. begin
  797. case expr.resultdef.typ of
  798. stringdef: result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  799. arraydef: result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  800. setdef: result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  801. else
  802. begin
  803. result:=cerrornode.create;
  804. hloopvar.free;
  805. hloopbody.free;
  806. MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
  807. end;
  808. end;
  809. end;
  810. end;
  811. end;
  812. current_filepos:=storefilepos;
  813. end;
  814. {****************************************************************************
  815. TLOOPNODE
  816. *****************************************************************************}
  817. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  818. begin
  819. inherited create(tt,l,r);
  820. t1:=_t1;
  821. t2:=_t2;
  822. fileinfo:=l.fileinfo;
  823. end;
  824. destructor tloopnode.destroy;
  825. begin
  826. t1.free;
  827. t2.free;
  828. inherited destroy;
  829. end;
  830. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  831. begin
  832. inherited ppuload(t,ppufile);
  833. t1:=ppuloadnode(ppufile);
  834. t2:=ppuloadnode(ppufile);
  835. ppufile.getsmallset(loopflags);
  836. end;
  837. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  838. begin
  839. inherited ppuwrite(ppufile);
  840. ppuwritenode(ppufile,t1);
  841. ppuwritenode(ppufile,t2);
  842. ppufile.putsmallset(loopflags);
  843. end;
  844. procedure tloopnode.buildderefimpl;
  845. begin
  846. inherited buildderefimpl;
  847. if assigned(t1) then
  848. t1.buildderefimpl;
  849. if assigned(t2) then
  850. t2.buildderefimpl;
  851. end;
  852. procedure tloopnode.derefimpl;
  853. begin
  854. inherited derefimpl;
  855. if assigned(t1) then
  856. t1.derefimpl;
  857. if assigned(t2) then
  858. t2.derefimpl;
  859. end;
  860. function tloopnode.dogetcopy : tnode;
  861. var
  862. p : tloopnode;
  863. begin
  864. p:=tloopnode(inherited dogetcopy);
  865. if assigned(t1) then
  866. p.t1:=t1.dogetcopy
  867. else
  868. p.t1:=nil;
  869. if assigned(t2) then
  870. p.t2:=t2.dogetcopy
  871. else
  872. p.t2:=nil;
  873. p.loopflags:=loopflags;
  874. dogetcopy:=p;
  875. end;
  876. procedure tloopnode.insertintolist(l : tnodelist);
  877. begin
  878. end;
  879. procedure tloopnode.printnodetree(var t:text);
  880. begin
  881. write(t,printnodeindention,'(');
  882. printnodeindent;
  883. printnodeinfo(t);
  884. writeln(t);
  885. printnode(t,left);
  886. printnode(t,right);
  887. printnode(t,t1);
  888. printnode(t,t2);
  889. printnodeunindent;
  890. writeln(t,printnodeindention,')');
  891. end;
  892. function tloopnode.docompare(p: tnode): boolean;
  893. begin
  894. docompare :=
  895. inherited docompare(p) and
  896. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  897. t1.isequal(tloopnode(p).t1) and
  898. t2.isequal(tloopnode(p).t2);
  899. end;
  900. {****************************************************************************
  901. TWHILEREPEATNODE
  902. *****************************************************************************}
  903. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  904. begin
  905. inherited create(whilerepeatn,l,r,nil,nil);
  906. if tab then
  907. include(loopflags, lnf_testatbegin);
  908. if cn then
  909. include(loopflags,lnf_checknegate);
  910. end;
  911. function twhilerepeatnode.pass_typecheck:tnode;
  912. var
  913. t:Tunarynode;
  914. begin
  915. result:=nil;
  916. resultdef:=voidtype;
  917. typecheckpass(left);
  918. { tp procvar support }
  919. maybe_call_procvar(left,true);
  920. {A not node can be removed.}
  921. if left.nodetype=notn then
  922. begin
  923. t:=Tunarynode(left);
  924. left:=Tunarynode(left).left;
  925. t.left:=nil;
  926. t.destroy;
  927. {Symdif operator, in case you are wondering:}
  928. loopflags:=loopflags >< [lnf_checknegate];
  929. end;
  930. { loop instruction }
  931. if assigned(right) then
  932. typecheckpass(right);
  933. set_varstate(left,vs_read,[vsf_must_be_valid]);
  934. if codegenerror then
  935. exit;
  936. if not is_boolean(left.resultdef) then
  937. begin
  938. if left.resultdef.typ=variantdef then
  939. inserttypeconv(left,booltype)
  940. else
  941. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  942. end;
  943. { Give warnings for code that will never be executed for
  944. while false do }
  945. if (lnf_testatbegin in loopflags) and
  946. (left.nodetype=ordconstn) and
  947. (tordconstnode(left).value.uvalue=0) and
  948. assigned(right) then
  949. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  950. end;
  951. {$ifdef prefetchnext}
  952. type
  953. passignmentquery = ^tassignmentquery;
  954. tassignmentquery = record
  955. towhat: tnode;
  956. source: tassignmentnode;
  957. statementcount: cardinal;
  958. end;
  959. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  960. var
  961. query: passignmentquery absolute arg;
  962. temp, prederef: tnode;
  963. begin
  964. result := fen_norecurse_false;
  965. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  966. inc(query^.statementcount);
  967. { make sure there's something else in the loop besides going to the }
  968. { next item }
  969. if (query^.statementcount > 1) and
  970. (n.nodetype = assignn) then
  971. begin
  972. { skip type conversions of assignment target }
  973. temp := tassignmentnode(n).left;
  974. while (temp.nodetype = typeconvn) do
  975. temp := ttypeconvnode(temp).left;
  976. { assignment to x of the while assigned(x) check? }
  977. if not(temp.isequal(query^.towhat)) then
  978. exit;
  979. { right hand side of assignment dereferenced field of }
  980. { x? (no derefn in case of class) }
  981. temp := tassignmentnode(n).right;
  982. while (temp.nodetype = typeconvn) do
  983. temp := ttypeconvnode(temp).left;
  984. if (temp.nodetype <> subscriptn) then
  985. exit;
  986. prederef := tsubscriptnode(temp).left;
  987. temp := prederef;
  988. while (temp.nodetype = typeconvn) do
  989. temp := ttypeconvnode(temp).left;
  990. { see tests/test/prefetch1.pp }
  991. if (temp.nodetype = derefn) then
  992. temp := tderefnode(temp).left
  993. else
  994. temp := prederef;
  995. if temp.isequal(query^.towhat) then
  996. begin
  997. query^.source := tassignmentnode(n);
  998. result := fen_norecurse_true;
  999. end
  1000. end
  1001. { don't check nodes which can't contain an assignment or whose }
  1002. { final assignment can vary a lot }
  1003. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  1004. result := fen_false;
  1005. end;
  1006. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  1007. var
  1008. query: tassignmentquery;
  1009. begin
  1010. query.towhat := towhat;
  1011. query.source := nil;
  1012. query.statementcount := 0;
  1013. if foreachnodestatic(where,@checkassignment,@query) then
  1014. result := query.source
  1015. else
  1016. result := nil;
  1017. end;
  1018. {$endif prefetchnext}
  1019. function twhilerepeatnode.pass_1 : tnode;
  1020. {$ifdef prefetchnext}
  1021. var
  1022. runnernode, prefetchcode: tnode;
  1023. assignmentnode: tassignmentnode;
  1024. prefetchstatements: tstatementnode;
  1025. {$endif prefetchnext}
  1026. begin
  1027. result:=nil;
  1028. expectloc:=LOC_VOID;
  1029. firstpass(left);
  1030. if codegenerror then
  1031. exit;
  1032. { loop instruction }
  1033. if assigned(right) then
  1034. begin
  1035. firstpass(right);
  1036. if codegenerror then
  1037. exit;
  1038. end;
  1039. {$ifdef prefetchnext}
  1040. { do at the end so all complex typeconversions are already }
  1041. { converted to calln's }
  1042. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1043. (lnf_testatbegin in loopflags) then
  1044. begin
  1045. { get first component of the while check }
  1046. runnernode := left;
  1047. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  1048. runnernode := tunarynode(runnernode).left;
  1049. { is it an assigned(x) check? }
  1050. if ((runnernode.nodetype = inlinen) and
  1051. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  1052. ((runnernode.nodetype = unequaln) and
  1053. (taddnode(runnernode).right.nodetype = niln)) then
  1054. begin
  1055. runnernode := tunarynode(runnernode).left;
  1056. { in case of in_assigned_x, there's a callparan in between }
  1057. if (runnernode.nodetype = callparan) then
  1058. runnernode := tcallparanode(runnernode).left;
  1059. while (runnernode.nodetype = typeconvn) do
  1060. runnernode := ttypeconvnode(runnernode).left;
  1061. { is there an "x := x(^).somefield"? }
  1062. assignmentnode := findassignment(right,runnernode);
  1063. if assigned(assignmentnode) then
  1064. begin
  1065. prefetchcode := internalstatements(prefetchstatements);
  1066. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  1067. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  1068. addstatement(prefetchstatements,right);
  1069. right := prefetchcode;
  1070. typecheckpass(right);
  1071. end;
  1072. end;
  1073. end;
  1074. {$endif prefetchnext}
  1075. end;
  1076. {$ifdef state_tracking}
  1077. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  1078. var condition:Tnode;
  1079. code:Tnode;
  1080. done:boolean;
  1081. value:boolean;
  1082. change:boolean;
  1083. firsttest:boolean;
  1084. factval:Tnode;
  1085. begin
  1086. track_state_pass:=false;
  1087. done:=false;
  1088. firsttest:=true;
  1089. {For repeat until statements, first do a pass through the code.}
  1090. if not(lnf_testatbegin in flags) then
  1091. begin
  1092. code:=right.getcopy;
  1093. if code.track_state_pass(exec_known) then
  1094. track_state_pass:=true;
  1095. code.destroy;
  1096. end;
  1097. repeat
  1098. condition:=left.getcopy;
  1099. code:=right.getcopy;
  1100. change:=condition.track_state_pass(exec_known);
  1101. factval:=aktstate.find_fact(left);
  1102. if factval<>nil then
  1103. begin
  1104. condition.destroy;
  1105. condition:=factval.getcopy;
  1106. change:=true;
  1107. end;
  1108. if change then
  1109. begin
  1110. track_state_pass:=true;
  1111. {Force new resultdef pass.}
  1112. condition.resultdef:=nil;
  1113. do_typecheckpass(condition);
  1114. end;
  1115. if is_constboolnode(condition) then
  1116. begin
  1117. {Try to turn a while loop into a repeat loop.}
  1118. if firsttest then
  1119. exclude(flags,testatbegin);
  1120. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  1121. if value then
  1122. begin
  1123. if code.track_state_pass(exec_known) then
  1124. track_state_pass:=true;
  1125. end
  1126. else
  1127. done:=true;
  1128. end
  1129. else
  1130. begin
  1131. {Remove any modified variables from the state.}
  1132. code.track_state_pass(false);
  1133. done:=true;
  1134. end;
  1135. code.destroy;
  1136. condition.destroy;
  1137. firsttest:=false;
  1138. until done;
  1139. {The loop condition is also known, for example:
  1140. while i<10 do
  1141. begin
  1142. ...
  1143. end;
  1144. When the loop is done, we do know that i<10 = false.
  1145. }
  1146. condition:=left.getcopy;
  1147. if condition.track_state_pass(exec_known) then
  1148. begin
  1149. track_state_pass:=true;
  1150. {Force new resultdef pass.}
  1151. condition.resultdef:=nil;
  1152. do_typecheckpass(condition);
  1153. end;
  1154. if not is_constboolnode(condition) then
  1155. aktstate.store_fact(condition,
  1156. cordconstnode.create(byte(checknegate),booltype,true))
  1157. else
  1158. condition.destroy;
  1159. end;
  1160. {$endif}
  1161. {*****************************************************************************
  1162. TIFNODE
  1163. *****************************************************************************}
  1164. constructor tifnode.create(l,r,_t1 : tnode);
  1165. begin
  1166. inherited create(ifn,l,r,_t1,nil);
  1167. end;
  1168. function tifnode.internalsimplify(warn: boolean) : tnode;
  1169. begin
  1170. result:=nil;
  1171. { optimize constant expressions }
  1172. if (left.nodetype=ordconstn) then
  1173. begin
  1174. if tordconstnode(left).value.uvalue=1 then
  1175. begin
  1176. if assigned(right) then
  1177. result:=right
  1178. else
  1179. result:=cnothingnode.create;
  1180. right:=nil;
  1181. if warn and assigned(t1) then
  1182. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1183. end
  1184. else
  1185. begin
  1186. if assigned(t1) then
  1187. result:=t1
  1188. else
  1189. result:=cnothingnode.create;
  1190. t1:=nil;
  1191. if warn and assigned(right) then
  1192. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1193. end;
  1194. end;
  1195. end;
  1196. function tifnode.simplify(forinline : boolean) : tnode;
  1197. begin
  1198. result:=internalsimplify(false);
  1199. end;
  1200. function tifnode.pass_typecheck:tnode;
  1201. begin
  1202. result:=nil;
  1203. resultdef:=voidtype;
  1204. typecheckpass(left);
  1205. { tp procvar support }
  1206. maybe_call_procvar(left,true);
  1207. { if path }
  1208. if assigned(right) then
  1209. typecheckpass(right);
  1210. { else path }
  1211. if assigned(t1) then
  1212. typecheckpass(t1);
  1213. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1214. if codegenerror then
  1215. exit;
  1216. if not is_boolean(left.resultdef) then
  1217. begin
  1218. if left.resultdef.typ=variantdef then
  1219. inserttypeconv(left,booltype)
  1220. else
  1221. Message1(type_e_boolean_expr_expected,left.resultdef.typename);
  1222. end;
  1223. result:=internalsimplify(true);
  1224. end;
  1225. function tifnode.pass_1 : tnode;
  1226. begin
  1227. result:=nil;
  1228. expectloc:=LOC_VOID;
  1229. firstpass(left);
  1230. { if path }
  1231. if assigned(right) then
  1232. firstpass(right);
  1233. { else path }
  1234. if assigned(t1) then
  1235. firstpass(t1);
  1236. { leave if we've got an error in one of the paths }
  1237. if codegenerror then
  1238. exit;
  1239. end;
  1240. {*****************************************************************************
  1241. TFORNODE
  1242. *****************************************************************************}
  1243. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1244. begin
  1245. inherited create(forn,l,r,_t1,_t2);
  1246. if back then
  1247. include(loopflags,lnf_backward);
  1248. include(loopflags,lnf_testatbegin);
  1249. end;
  1250. procedure Tfornode.loop_var_access(not_type:Tnotification_flag;
  1251. symbol:Tsym);
  1252. begin
  1253. {If there is a read access, the value of the loop counter is important;
  1254. at the end of the loop the loop variable should contain the value it
  1255. had in the last iteration.}
  1256. if not_type=vn_onwrite then
  1257. begin
  1258. writeln('Loopvar does not matter on exit');
  1259. end
  1260. else
  1261. begin
  1262. exclude(loopflags,lnf_dont_mind_loopvar_on_exit);
  1263. writeln('Loopvar does matter on exit');
  1264. end;
  1265. Tabstractvarsym(symbol).unregister_notification(loopvar_notid);
  1266. end;
  1267. function tfornode.simplify(forinline : boolean) : tnode;
  1268. begin
  1269. result:=nil;
  1270. if (t1.nodetype=ordconstn) and
  1271. (right.nodetype=ordconstn) and
  1272. (
  1273. (
  1274. (lnf_backward in loopflags) and
  1275. (tordconstnode(right).value<tordconstnode(t1).value)
  1276. ) or
  1277. (
  1278. not(lnf_backward in loopflags) and
  1279. (tordconstnode(right).value>tordconstnode(t1).value)
  1280. )
  1281. ) then
  1282. result:=cnothingnode.create;
  1283. end;
  1284. function tfornode.pass_typecheck:tnode;
  1285. var
  1286. res : tnode;
  1287. begin
  1288. result:=nil;
  1289. resultdef:=voidtype;
  1290. { process the loopvar, from and to, varstates are already set }
  1291. typecheckpass(left);
  1292. typecheckpass(right);
  1293. typecheckpass(t1);
  1294. set_varstate(left,vs_written,[]);
  1295. { loop unrolling }
  1296. if cs_opt_loopunroll in current_settings.optimizerswitches then
  1297. begin
  1298. res:=unroll_loop(self);
  1299. if assigned(res) then
  1300. begin
  1301. typecheckpass(res);
  1302. result:=res;
  1303. exit;
  1304. end;
  1305. end;
  1306. { Can we spare the first comparision? }
  1307. if (t1.nodetype=ordconstn) and
  1308. (right.nodetype=ordconstn) and
  1309. (
  1310. (
  1311. (lnf_backward in loopflags) and
  1312. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1313. ) or
  1314. (
  1315. not(lnf_backward in loopflags) and
  1316. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1317. )
  1318. ) then
  1319. exclude(loopflags,lnf_testatbegin);
  1320. { Make sure that the loop var and the
  1321. from and to values are compatible types }
  1322. check_ranges(right.fileinfo,right,left.resultdef);
  1323. inserttypeconv(right,left.resultdef);
  1324. check_ranges(t1.fileinfo,t1,left.resultdef);
  1325. inserttypeconv(t1,left.resultdef);
  1326. if assigned(t2) then
  1327. typecheckpass(t2);
  1328. end;
  1329. function tfornode.pass_1 : tnode;
  1330. begin
  1331. result:=nil;
  1332. expectloc:=LOC_VOID;
  1333. firstpass(left);
  1334. firstpass(right);
  1335. firstpass(t1);
  1336. if assigned(t2) then
  1337. begin
  1338. firstpass(t2);
  1339. if codegenerror then
  1340. exit;
  1341. end;
  1342. end;
  1343. {*****************************************************************************
  1344. TEXITNODE
  1345. *****************************************************************************}
  1346. constructor texitnode.create(l:tnode);
  1347. begin
  1348. inherited create(exitn,l);
  1349. if assigned(left) then
  1350. begin
  1351. { add assignment to funcretsym }
  1352. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1353. left:=cassignmentnode.create(
  1354. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1355. left);
  1356. end;
  1357. end;
  1358. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1359. begin
  1360. inherited ppuload(t,ppufile);
  1361. end;
  1362. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1363. begin
  1364. inherited ppuwrite(ppufile);
  1365. end;
  1366. function texitnode.pass_typecheck:tnode;
  1367. begin
  1368. result:=nil;
  1369. if assigned(left) then
  1370. typecheckpass(left);
  1371. resultdef:=voidtype;
  1372. end;
  1373. function texitnode.pass_1 : tnode;
  1374. begin
  1375. result:=nil;
  1376. expectloc:=LOC_VOID;
  1377. if assigned(left) then
  1378. begin
  1379. firstpass(left);
  1380. if codegenerror then
  1381. exit;
  1382. end;
  1383. end;
  1384. {*****************************************************************************
  1385. TBREAKNODE
  1386. *****************************************************************************}
  1387. constructor tbreaknode.create;
  1388. begin
  1389. inherited create(breakn);
  1390. end;
  1391. function tbreaknode.pass_typecheck:tnode;
  1392. begin
  1393. result:=nil;
  1394. resultdef:=voidtype;
  1395. end;
  1396. function tbreaknode.pass_1 : tnode;
  1397. begin
  1398. result:=nil;
  1399. expectloc:=LOC_VOID;
  1400. end;
  1401. {*****************************************************************************
  1402. TCONTINUENODE
  1403. *****************************************************************************}
  1404. constructor tcontinuenode.create;
  1405. begin
  1406. inherited create(continuen);
  1407. end;
  1408. function tcontinuenode.pass_typecheck:tnode;
  1409. begin
  1410. result:=nil;
  1411. resultdef:=voidtype;
  1412. end;
  1413. function tcontinuenode.pass_1 : tnode;
  1414. begin
  1415. result:=nil;
  1416. expectloc:=LOC_VOID;
  1417. end;
  1418. {*****************************************************************************
  1419. TGOTONODE
  1420. *****************************************************************************}
  1421. constructor tgotonode.create(p : tlabelsym);
  1422. begin
  1423. inherited create(goton);
  1424. exceptionblock:=current_exceptblock;
  1425. labelnode:=nil;
  1426. labelsym:=p;
  1427. end;
  1428. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1429. begin
  1430. inherited ppuload(t,ppufile);
  1431. labelnodeidx:=ppufile.getlongint;
  1432. exceptionblock:=ppufile.getbyte;
  1433. end;
  1434. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  1435. begin
  1436. inherited ppuwrite(ppufile);
  1437. labelnodeidx:=labelnode.ppuidx;
  1438. ppufile.putlongint(labelnodeidx);
  1439. ppufile.putbyte(exceptionblock);
  1440. end;
  1441. procedure tgotonode.buildderefimpl;
  1442. begin
  1443. inherited buildderefimpl;
  1444. end;
  1445. procedure tgotonode.derefimpl;
  1446. begin
  1447. inherited derefimpl;
  1448. end;
  1449. procedure tgotonode.resolveppuidx;
  1450. begin
  1451. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  1452. if labelnode.nodetype<>labeln then
  1453. internalerror(200809021);
  1454. end;
  1455. function tgotonode.pass_typecheck:tnode;
  1456. begin
  1457. result:=nil;
  1458. resultdef:=voidtype;
  1459. end;
  1460. function tgotonode.pass_1 : tnode;
  1461. var
  1462. p2 : tprocinfo;
  1463. begin
  1464. result:=nil;
  1465. expectloc:=LOC_VOID;
  1466. { The labelnode can already be set when
  1467. this node was copied }
  1468. if not(assigned(labelnode)) then
  1469. begin
  1470. { inner procedure goto? }
  1471. if assigned(labelsym.code) and
  1472. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1473. { generated by the optimizer? }
  1474. not(assigned(labelsym.owner))) then
  1475. labelnode:=tlabelnode(labelsym.code)
  1476. else if (m_non_local_goto in current_settings.modeswitches) and
  1477. assigned(labelsym.owner) then
  1478. begin
  1479. if current_procinfo.procdef.parast.symtablelevel>labelsym.owner.symtablelevel then
  1480. begin
  1481. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  1482. if exceptionblock>0 then
  1483. CGMessage(cg_e_goto_inout_of_exception_block);
  1484. { goto across procedures using exception?
  1485. this is not allowed because we cannot
  1486. easily unwind the exception frame
  1487. stack
  1488. }
  1489. p2:=current_procinfo;
  1490. while true do
  1491. begin
  1492. if (p2.flags*[pi_needs_implicit_finally,pi_uses_exceptions,pi_has_implicit_finally])<>[] then
  1493. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  1494. if labelsym.owner=p2.procdef.localst then
  1495. break;
  1496. p2:=p2.parent
  1497. end;
  1498. if assigned(labelsym.jumpbuf) then
  1499. begin
  1500. labelsym.nonlocal:=true;
  1501. result:=ccallnode.createintern('fpc_longjmp',
  1502. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  1503. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  1504. nil)));
  1505. end
  1506. else
  1507. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1508. end
  1509. else
  1510. CGMessage(cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  1511. end
  1512. else
  1513. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1514. end;
  1515. { check if we don't mess with exception blocks }
  1516. if assigned(labelnode) and
  1517. (exceptionblock<>labelnode.exceptionblock) then
  1518. CGMessage(cg_e_goto_inout_of_exception_block);
  1519. end;
  1520. function tgotonode.dogetcopy : tnode;
  1521. var
  1522. p : tgotonode;
  1523. begin
  1524. p:=tgotonode(inherited dogetcopy);
  1525. p.exceptionblock:=exceptionblock;
  1526. { generate labelnode if not done yet }
  1527. if not(assigned(labelnode)) then
  1528. begin
  1529. if assigned(labelsym) and assigned(labelsym.code) then
  1530. labelnode:=tlabelnode(labelsym.code)
  1531. end;
  1532. p.labelsym:=labelsym;
  1533. if assigned(labelnode) then
  1534. p.labelnode:=tlabelnode(labelnode.dogetcopy)
  1535. else
  1536. begin
  1537. { don't trigger IE when there was already an error, i.e. the
  1538. label is not defined. See tw11763 (PFV) }
  1539. if errorcount=0 then
  1540. internalerror(200610291);
  1541. end;
  1542. result:=p;
  1543. end;
  1544. function tgotonode.docompare(p: tnode): boolean;
  1545. begin
  1546. docompare := false;
  1547. end;
  1548. {*****************************************************************************
  1549. TLABELNODE
  1550. *****************************************************************************}
  1551. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  1552. begin
  1553. inherited create(labeln,l);
  1554. exceptionblock:=current_exceptblock;
  1555. labsym:=alabsym;
  1556. { Register labelnode in labelsym }
  1557. labsym.code:=self;
  1558. end;
  1559. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1560. begin
  1561. inherited ppuload(t,ppufile);
  1562. exceptionblock:=ppufile.getbyte;
  1563. end;
  1564. destructor tlabelnode.destroy;
  1565. begin
  1566. { Remove reference in labelsym, this is to prevent
  1567. goto's to this label }
  1568. if assigned(labsym) and (labsym.code=pointer(self)) then
  1569. labsym.code:=nil;
  1570. inherited destroy;
  1571. end;
  1572. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  1573. begin
  1574. inherited ppuwrite(ppufile);
  1575. ppufile.putbyte(exceptionblock);
  1576. end;
  1577. procedure tlabelnode.buildderefimpl;
  1578. begin
  1579. inherited buildderefimpl;
  1580. end;
  1581. procedure tlabelnode.derefimpl;
  1582. begin
  1583. inherited derefimpl;
  1584. end;
  1585. function tlabelnode.pass_typecheck:tnode;
  1586. begin
  1587. result:=nil;
  1588. { left could still be unassigned }
  1589. if assigned(left) then
  1590. typecheckpass(left);
  1591. resultdef:=voidtype;
  1592. end;
  1593. function tlabelnode.pass_1 : tnode;
  1594. begin
  1595. result:=nil;
  1596. expectloc:=LOC_VOID;
  1597. include(current_procinfo.flags,pi_has_label);
  1598. if assigned(labsym) and labsym.nonlocal then
  1599. include(current_procinfo.flags,pi_has_interproclabel);
  1600. if assigned(left) then
  1601. firstpass(left);
  1602. if (m_non_local_goto in current_settings.modeswitches) and
  1603. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  1604. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  1605. end;
  1606. function tlabelnode.dogetcopy : tnode;
  1607. begin
  1608. if not(assigned(copiedto)) then
  1609. copiedto:=tlabelnode(inherited dogetcopy);
  1610. copiedto.exceptionblock:=exceptionblock;
  1611. result:=copiedto;
  1612. end;
  1613. function tlabelnode.docompare(p: tnode): boolean;
  1614. begin
  1615. docompare := false;
  1616. end;
  1617. {*****************************************************************************
  1618. TRAISENODE
  1619. *****************************************************************************}
  1620. constructor traisenode.create(l,taddr,tframe:tnode);
  1621. begin
  1622. inherited create(raisen,l,taddr,tframe);
  1623. end;
  1624. function traisenode.pass_typecheck:tnode;
  1625. begin
  1626. result:=nil;
  1627. resultdef:=voidtype;
  1628. if assigned(left) then
  1629. begin
  1630. { first para must be a _class_ }
  1631. typecheckpass(left);
  1632. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1633. if codegenerror then
  1634. exit;
  1635. if not(is_class(left.resultdef)) then
  1636. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  1637. { insert needed typeconvs for addr,frame }
  1638. if assigned(right) then
  1639. begin
  1640. { addr }
  1641. typecheckpass(right);
  1642. inserttypeconv(right,voidpointertype);
  1643. { frame }
  1644. if assigned(third) then
  1645. begin
  1646. typecheckpass(third);
  1647. inserttypeconv(third,voidpointertype);
  1648. end;
  1649. end;
  1650. end;
  1651. end;
  1652. function traisenode.pass_1 : tnode;
  1653. begin
  1654. result:=nil;
  1655. include(current_procinfo.flags,pi_do_call);
  1656. expectloc:=LOC_VOID;
  1657. if assigned(left) then
  1658. begin
  1659. { first para must be a _class_ }
  1660. firstpass(left);
  1661. { insert needed typeconvs for addr,frame }
  1662. if assigned(right) then
  1663. begin
  1664. { addr }
  1665. firstpass(right);
  1666. { frame }
  1667. if assigned(third) then
  1668. firstpass(third);
  1669. end;
  1670. end;
  1671. end;
  1672. {*****************************************************************************
  1673. TTRYEXCEPTNODE
  1674. *****************************************************************************}
  1675. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1676. begin
  1677. inherited create(tryexceptn,l,r,_t1,nil);
  1678. end;
  1679. function ttryexceptnode.pass_typecheck:tnode;
  1680. begin
  1681. result:=nil;
  1682. typecheckpass(left);
  1683. { on statements }
  1684. if assigned(right) then
  1685. typecheckpass(right);
  1686. { else block }
  1687. if assigned(t1) then
  1688. typecheckpass(t1);
  1689. resultdef:=voidtype;
  1690. end;
  1691. function ttryexceptnode.pass_1 : tnode;
  1692. begin
  1693. result:=nil;
  1694. include(current_procinfo.flags,pi_do_call);
  1695. expectloc:=LOC_VOID;
  1696. firstpass(left);
  1697. { on statements }
  1698. if assigned(right) then
  1699. firstpass(right);
  1700. { else block }
  1701. if assigned(t1) then
  1702. firstpass(t1);
  1703. end;
  1704. {*****************************************************************************
  1705. TTRYFINALLYNODE
  1706. *****************************************************************************}
  1707. constructor ttryfinallynode.create(l,r:tnode);
  1708. begin
  1709. inherited create(tryfinallyn,l,r,nil,nil);
  1710. implicitframe:=false;
  1711. end;
  1712. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1713. begin
  1714. inherited create(tryfinallyn,l,r,_t1,nil);
  1715. implicitframe:=true;
  1716. end;
  1717. function ttryfinallynode.pass_typecheck:tnode;
  1718. begin
  1719. result:=nil;
  1720. include(current_procinfo.flags,pi_do_call);
  1721. resultdef:=voidtype;
  1722. typecheckpass(left);
  1723. // "try block" is "used"? (JM)
  1724. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  1725. typecheckpass(right);
  1726. // "except block" is "used"? (JM)
  1727. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  1728. { special finally block only executed when there was an exception }
  1729. if assigned(t1) then
  1730. begin
  1731. typecheckpass(t1);
  1732. // "finally block" is "used"? (JM)
  1733. set_varstate(t1,vs_readwritten,[vsf_must_be_valid]);
  1734. end;
  1735. end;
  1736. function ttryfinallynode.pass_1 : tnode;
  1737. begin
  1738. result:=nil;
  1739. expectloc:=LOC_VOID;
  1740. firstpass(left);
  1741. firstpass(right);
  1742. if assigned(t1) then
  1743. firstpass(t1);
  1744. end;
  1745. function ttryfinallynode.simplify(forinline : boolean): tnode;
  1746. begin
  1747. result:=nil;
  1748. { if the try contains no code, we can kill
  1749. the try and except and return only the
  1750. finally part }
  1751. if has_no_code(left) then
  1752. begin
  1753. result:=right;
  1754. right:=nil;
  1755. end;
  1756. end;
  1757. {*****************************************************************************
  1758. TONNODE
  1759. *****************************************************************************}
  1760. constructor tonnode.create(l,r:tnode);
  1761. begin
  1762. inherited create(onn,l,r);
  1763. excepTSymtable:=nil;
  1764. excepttype:=nil;
  1765. end;
  1766. destructor tonnode.destroy;
  1767. begin
  1768. { copied nodes don't need to release the symtable }
  1769. if assigned(excepTSymtable) then
  1770. excepTSymtable.free;
  1771. inherited destroy;
  1772. end;
  1773. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1774. begin
  1775. inherited ppuload(t,ppufile);
  1776. excepTSymtable:=nil;
  1777. excepttype:=nil;
  1778. end;
  1779. function tonnode.dogetcopy : tnode;
  1780. var
  1781. n : tonnode;
  1782. begin
  1783. n:=tonnode(inherited dogetcopy);
  1784. if assigned(exceptsymtable) then
  1785. n.exceptsymtable:=exceptsymtable.getcopy
  1786. else
  1787. n.exceptsymtable:=nil;
  1788. n.excepttype:=excepttype;
  1789. result:=n;
  1790. end;
  1791. function tonnode.pass_typecheck:tnode;
  1792. begin
  1793. result:=nil;
  1794. resultdef:=voidtype;
  1795. if not(is_class(excepttype)) then
  1796. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1797. if assigned(left) then
  1798. typecheckpass(left);
  1799. if assigned(right) then
  1800. typecheckpass(right);
  1801. end;
  1802. function tonnode.pass_1 : tnode;
  1803. begin
  1804. result:=nil;
  1805. include(current_procinfo.flags,pi_do_call);
  1806. expectloc:=LOC_VOID;
  1807. if assigned(left) then
  1808. firstpass(left);
  1809. if assigned(right) then
  1810. firstpass(right);
  1811. end;
  1812. function tonnode.docompare(p: tnode): boolean;
  1813. begin
  1814. docompare := false;
  1815. end;
  1816. end.