nflw.pas 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638
  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. symconst,symtype,symbase,symdef,symsym,
  25. optloop;
  26. type
  27. { flags used by loop nodes }
  28. tloopflag = (
  29. { set if it is a for ... downto ... do loop }
  30. lnf_backward,
  31. { Do we need to parse childs to set var state? }
  32. lnf_varstate,
  33. { Do a test at the begin of the loop?}
  34. lnf_testatbegin,
  35. { Negate the loop test? }
  36. lnf_checknegate,
  37. { Should the value of the loop variable on exit be correct. }
  38. lnf_dont_mind_loopvar_on_exit,
  39. { Loop simplify flag }
  40. lnf_simplify_processing);
  41. tloopflags = set of tloopflag;
  42. const
  43. { loop flags which must match to consider loop nodes equal regarding the flags }
  44. loopflagsequal = [lnf_backward];
  45. type
  46. tlabelnode = class;
  47. tloopnode = class(tbinarynode)
  48. t1,t2 : tnode;
  49. loopflags : tloopflags;
  50. constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
  51. destructor destroy;override;
  52. function dogetcopy : tnode;override;
  53. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  54. procedure ppuwrite(ppufile:tcompilerppufile);override;
  55. procedure buildderefimpl;override;
  56. procedure derefimpl;override;
  57. procedure insertintolist(l : tnodelist);override;
  58. procedure printnodetree(var t:text);override;
  59. {$ifdef DEBUG_NODE_XML}
  60. procedure XMLPrintNodeInfo(var T: Text); override;
  61. procedure XMLPrintNodeTree(var T: Text); override;
  62. {$endif DEBUG_NODE_XML}
  63. function docompare(p: tnode): boolean; override;
  64. end;
  65. twhilerepeatnode = class(tloopnode)
  66. { l: condition; r: body; tab: test at begin; cn: negate condition
  67. x,y,true,false: while loop
  68. x,y,false,true: repeat until loop }
  69. constructor create(l,r:Tnode;tab,cn:boolean);virtual;reintroduce;
  70. function pass_typecheck:tnode;override;
  71. function pass_1 : tnode;override;
  72. {$ifdef state_tracking}
  73. function track_state_pass(exec_known:boolean):boolean;override;
  74. {$endif}
  75. end;
  76. twhilerepeatnodeclass = class of twhilerepeatnode;
  77. tifnode = class(tloopnode)
  78. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  79. constructor create_internal(l,r,_t1 : tnode);virtual;reintroduce;
  80. function pass_typecheck:tnode;override;
  81. function pass_1 : tnode;override;
  82. function simplify(forinline : boolean) : tnode;override;
  83. private
  84. function internalsimplify(warn: boolean) : tnode;
  85. end;
  86. tifnodeclass = class of tifnode;
  87. tfornode = class(tloopnode)
  88. { if count isn divisable by unrolls then
  89. the for loop must jump to this label to get the correct
  90. number of executions }
  91. entrylabel,
  92. { this is a dummy node used by the dfa to store life information for the loop iteration }
  93. loopiteration : tnode;
  94. loopvar_notid:cardinal;
  95. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;reintroduce;
  96. function pass_typecheck:tnode;override;
  97. function pass_1 : tnode;override;
  98. function makewhileloop : tnode;
  99. function simplify(forinline : boolean) : tnode;override;
  100. end;
  101. tfornodeclass = class of tfornode;
  102. texitnode = class(tunarynode)
  103. constructor create(l:tnode);virtual;
  104. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  105. procedure ppuwrite(ppufile:tcompilerppufile);override;
  106. function pass_typecheck:tnode;override;
  107. function pass_1 : tnode;override;
  108. property resultexpr : tnode read left write left;
  109. end;
  110. texitnodeclass = class of texitnode;
  111. tbreaknode = class(tnode)
  112. constructor create;virtual;
  113. function pass_typecheck:tnode;override;
  114. function pass_1 : tnode;override;
  115. end;
  116. tbreaknodeclass = class of tbreaknode;
  117. tcontinuenode = class(tnode)
  118. constructor create;virtual;
  119. function pass_typecheck:tnode;override;
  120. function pass_1 : tnode;override;
  121. end;
  122. tcontinuenodeclass = class of tcontinuenode;
  123. tgotonode = class(tnode)
  124. private
  125. labelnodeidx : longint;
  126. public
  127. { * Set when creating the gotonode (since that's all we know at that
  128. point).
  129. * Used in pass_typecheck to find the corresponding labelnode (when a
  130. labelnode is created for a tlabelsym, the label assigns itself to
  131. the "code" field of the labelsym), which is then assigned to the
  132. labelnode field
  133. * After this, the labelsym is (and must) never be used anymore, and
  134. instead the labelnode must always be used. The reason is that the
  135. labelsym may not be owned by anything, and will be freed by the
  136. label node when it gets freed
  137. * The above is the reason why the labelsym field does not get copied
  138. by tgotonode.dogetcopy, but instead the copy of the labelnode gets
  139. tracked (both the labelnode and its goto nodes must always all be
  140. copied).
  141. The labelnode itself will not copy the labelsym either in dogetcopy.
  142. Instead, since the link between the gotos and the labels gets
  143. tracked via node tree references, the label node will generate a new
  144. asmlabel on the fly and the goto node will get it from there (if the
  145. goto node gets processed before the label node has been processed,
  146. it will ask the label node to generate the asmsymbol at that point).
  147. The original tlabelsym will get emitted only for the original
  148. label node. It is only actually used if there is a reference to it
  149. from
  150. * an inline assembly block. Since inline assembly blocks cannot be
  151. inlined at this point, it doesn't matter that this would break
  152. in case the node gets copied
  153. * global goto/label. Inlining is not supported for these, so no
  154. problem here either for now.
  155. * a load node (its symtableentry field). Since the symtableentry
  156. of loadnodes is always expected to be valid, we cannot do like
  157. with the goto nodes. Instead, we will create a new labelsym
  158. when performing a dogetcopy of such a load node and assign this
  159. labelsym to the copied labelnode (and vice versa)
  160. }
  161. labelsym : tlabelsym;
  162. labelnode : tlabelnode;
  163. exceptionblock : integer;
  164. constructor create(p : tlabelsym);virtual;
  165. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  166. procedure ppuwrite(ppufile:tcompilerppufile);override;
  167. procedure buildderefimpl;override;
  168. procedure derefimpl;override;
  169. procedure resolveppuidx;override;
  170. function dogetcopy : tnode;override;
  171. function pass_typecheck:tnode;override;
  172. function pass_1 : tnode;override;
  173. function docompare(p: tnode): boolean; override;
  174. end;
  175. tgotonodeclass = class of tgotonode;
  176. tlabelnode = class(tunarynode)
  177. exceptionblock : integer;
  178. { when copying trees, this points to the newly created copy of a label }
  179. copiedto : tlabelnode;
  180. labsym : tlabelsym;
  181. constructor create(l:tnode;alabsym:tlabelsym);virtual;
  182. destructor destroy;override;
  183. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  184. procedure ppuwrite(ppufile:tcompilerppufile);override;
  185. procedure buildderefimpl;override;
  186. procedure derefimpl;override;
  187. function dogetcopy : tnode;override;
  188. function pass_typecheck:tnode;override;
  189. function pass_1 : tnode;override;
  190. function docompare(p: tnode): boolean; override;
  191. end;
  192. tlabelnodeclass = class of tlabelnode;
  193. traisenode = class(ttertiarynode)
  194. constructor create(l,taddr,tframe:tnode);virtual;
  195. function pass_typecheck:tnode;override;
  196. function pass_1 : tnode;override;
  197. end;
  198. traisenodeclass = class of traisenode;
  199. ttryexceptnode = class(tloopnode)
  200. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  201. function pass_typecheck:tnode;override;
  202. function pass_1 : tnode;override;
  203. function simplify(forinline: boolean): tnode; override;
  204. protected
  205. procedure adjust_estimated_stack_size; virtual;
  206. end;
  207. ttryexceptnodeclass = class of ttryexceptnode;
  208. { the third node is to store a copy of the finally code for llvm:
  209. it needs one copy to execute in case an exception occurs, and
  210. one in case no exception occurs }
  211. ttryfinallynode = class(ttertiarynode)
  212. implicitframe : boolean;
  213. constructor create(l,r:tnode);virtual;reintroduce;
  214. constructor create_implicit(l,r:tnode);virtual;
  215. function pass_typecheck:tnode;override;
  216. function pass_1 : tnode;override;
  217. function simplify(forinline:boolean): tnode;override;
  218. protected
  219. procedure adjust_estimated_stack_size; virtual;
  220. end;
  221. ttryfinallynodeclass = class of ttryfinallynode;
  222. tonnode = class(tbinarynode)
  223. excepTSymtable : TSymtable;
  224. excepttype : tobjectdef;
  225. constructor create(l,r:tnode);virtual;
  226. destructor destroy;override;
  227. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  228. function pass_typecheck:tnode;override;
  229. function pass_1 : tnode;override;
  230. function dogetcopy : tnode;override;
  231. function docompare(p: tnode): boolean; override;
  232. end;
  233. tonnodeclass = class of tonnode;
  234. var
  235. cwhilerepeatnode : twhilerepeatnodeclass=twhilerepeatnode;
  236. cifnode : tifnodeclass = tifnode;
  237. cfornode : tfornodeclass = tfornode;
  238. cexitnode : texitnodeclass = texitnode;
  239. cgotonode : tgotonodeclass = tgotonode;
  240. clabelnode : tlabelnodeclass = tlabelnode;
  241. craisenode : traisenodeclass = traisenode;
  242. ctryexceptnode : ttryexceptnodeclass = ttryexceptnode;
  243. ctryfinallynode : ttryfinallynodeclass = ttryfinallynode;
  244. connode : tonnodeclass = tonnode;
  245. cbreaknode : tbreaknodeclass = tbreaknode;
  246. ccontinuenode : tcontinuenodeclass = tcontinuenode;
  247. // for-in loop helpers
  248. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  249. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  250. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  251. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  252. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  253. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  254. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  255. { converts all for nodes in the tree into while nodes,
  256. returns true if something was converted }
  257. function ConvertForLoops(var n : tnode) : Boolean;
  258. implementation
  259. uses
  260. globtype,systems,constexp,compinnr,
  261. cutils,verbose,globals,ppu,
  262. symtable,paramgr,defcmp,defutil,htypechk,pass_1,
  263. ncal,nadd,ncon,nmem,nld,ncnv,nbas,nutils,ninl,nset,ngenutil,
  264. {$ifdef state_tracking}
  265. nstate,
  266. {$endif}
  267. {$ifdef i8086}
  268. cpuinfo,
  269. {$endif i8086}
  270. cgbase,procinfo
  271. ;
  272. // for-in loop helpers
  273. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  274. begin
  275. result:=cfornode.create(hloopvar,
  276. cinlinenode.create(in_low_x,false,expr.getcopy),
  277. cinlinenode.create(in_high_x,false,expr.getcopy),
  278. hloopbody,
  279. false);
  280. end;
  281. function create_objc_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  282. var
  283. mainstatement, outerloopbodystatement, innerloopbodystatement, tempstatement: tstatementnode;
  284. state, mutationcheck, currentamount, innerloopcounter, items, expressiontemp: ttempcreatenode;
  285. outerloop, innerloop, hp: tnode;
  286. itemsarraydef: tarraydef;
  287. sym: tsym;
  288. begin
  289. { Objective-C enumerators require Objective-C 2.0 }
  290. if not(m_objectivec2 in current_settings.modeswitches) then
  291. begin
  292. result:=cerrornode.create;
  293. MessagePos(expr.fileinfo,parser_e_objc_enumerator_2_0);
  294. exit;
  295. end;
  296. { Requires the NSFastEnumeration protocol and NSFastEnumerationState
  297. record }
  298. maybeloadcocoatypes;
  299. if not assigned(objc_fastenumeration) or
  300. not assigned(objc_fastenumerationstate) then
  301. begin
  302. result:=cerrornode.create;
  303. MessagePos(expr.fileinfo,parser_e_objc_missing_enumeration_defs);
  304. exit;
  305. end;
  306. (* Original code:
  307. for hloopvar in expression do
  308. <hloopbody>
  309. Pascal code equivalent into which it has to be transformed
  310. (sure would be nice if the compiler had some kind of templates ;) :
  311. var
  312. state: NSFastEnumerationState;
  313. expressiontemp: NSFastEnumerationProtocol;
  314. mutationcheck,
  315. currentamount,
  316. innerloopcounter: culong;
  317. { size can be increased/decreased if desired }
  318. items: array[1..16] of id;
  319. begin
  320. fillchar(state,sizeof(state),0);
  321. expressiontemp:=expression;
  322. repeat
  323. currentamount:=expressiontemp.countByEnumeratingWithState_objects_count(@state,@items,length(items));
  324. if currentamount=0 then
  325. begin
  326. { "The iterating variable is set to nil when the loop ends by
  327. exhausting the source pool of objects" }
  328. hloopvar:=nil;
  329. break;
  330. end;
  331. mutationcheck:=state.mutationsptr^;
  332. innerloopcounter:=culong(-1);
  333. repeat
  334. { at the start so that "continue" in <loopbody> works correctly }
  335. { don't use for-loop, because then the value of the iteration
  336. counter is undefined on exit and we have to check it in the
  337. outer repeat/until condition }
  338. {$push}
  339. {$r-,q-}
  340. inc(innerloopcounter);
  341. {$pop}
  342. if innerloopcounter=currentamount then
  343. break;
  344. if mutationcheck<>state.mutationsptr^ then
  345. { raises Objective-C exception... }
  346. objc_enumerationMutation(expressiontemp);
  347. hloopvar:=state.itemsPtr[innerloopcounter];
  348. { if continue in loopbody -> jumps to start, increases count and checks }
  349. { if break in loopbody: goes to outer repeat/until and innerloopcount
  350. will be < currentamount -> stops }
  351. <hloopbody>
  352. until false;
  353. { if the inner loop terminated early, "break" was used and we have
  354. to stop }
  355. { "If the loop is terminated early, the iterating variable is left
  356. pointing to the last iteration item." }
  357. until innerloopcounter<currentamount;
  358. end;
  359. *)
  360. result:=internalstatements(mainstatement);
  361. { the fast enumeration state }
  362. state:=ctempcreatenode.create(objc_fastenumerationstate,objc_fastenumerationstate.size,tt_persistent,false);
  363. typecheckpass(tnode(state));
  364. addstatement(mainstatement,state);
  365. { the temporary items array }
  366. itemsarraydef:=carraydef.create(1,16,u32inttype);
  367. itemsarraydef.elementdef:=objc_idtype;
  368. items:=ctempcreatenode.create(itemsarraydef,itemsarraydef.size,tt_persistent,false);
  369. addstatement(mainstatement,items);
  370. typecheckpass(tnode(items));
  371. { temp for the expression/collection through which we iterate }
  372. expressiontemp:=ctempcreatenode.create(objc_fastenumeration,objc_fastenumeration.size,tt_persistent,true);
  373. addstatement(mainstatement,expressiontemp);
  374. { currentamount temp (not really clean: we use ptruint instead of
  375. culong) }
  376. currentamount:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  377. typecheckpass(tnode(currentamount));
  378. addstatement(mainstatement,currentamount);
  379. { mutationcheck temp (idem) }
  380. mutationcheck:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  381. typecheckpass(tnode(mutationcheck));
  382. addstatement(mainstatement,mutationcheck);
  383. { innerloopcounter temp (idem) }
  384. innerloopcounter:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  385. typecheckpass(tnode(innerloopcounter));
  386. addstatement(mainstatement,innerloopcounter);
  387. { initialise the state with 0 }
  388. addstatement(mainstatement,ccallnode.createinternfromunit('SYSTEM','FILLCHAR',
  389. ccallparanode.create(genintconstnode(0),
  390. ccallparanode.create(genintconstnode(objc_fastenumerationstate.size),
  391. ccallparanode.create(ctemprefnode.create(state),nil)
  392. )
  393. )
  394. ));
  395. { this will also check whether the expression (potentially) conforms
  396. to the NSFastEnumeration protocol (use expr.getcopy, because the
  397. caller will free expr) }
  398. addstatement(mainstatement,cassignmentnode.create(ctemprefnode.create(expressiontemp),expr.getcopy));
  399. { we add the "repeat..until" afterwards, now just create the body }
  400. outerloop:=internalstatements(outerloopbodystatement);
  401. { the countByEnumeratingWithState_objects_count call }
  402. hp:=ccallparanode.create(cinlinenode.create(in_length_x,false,ctypenode.create(itemsarraydef)),
  403. ccallparanode.create(caddrnode.create(ctemprefnode.create(items)),
  404. ccallparanode.create(caddrnode.create(ctemprefnode.create(state)),nil)
  405. )
  406. );
  407. sym:=search_struct_member(objc_fastenumeration,'COUNTBYENUMERATINGWITHSTATE_OBJECTS_COUNT');
  408. if not assigned(sym) or
  409. (sym.typ<>procsym) then
  410. internalerror(2010061901);
  411. hp:=ccallnode.create(hp,tprocsym(sym),sym.owner,ctemprefnode.create(expressiontemp),[],nil);
  412. addstatement(outerloopbodystatement,cassignmentnode.create(
  413. ctemprefnode.create(currentamount),hp));
  414. { if currentamount = 0, bail out (use copy of hloopvar, because we
  415. have to use it again below) }
  416. hp:=internalstatements(tempstatement);
  417. addstatement(tempstatement,cassignmentnode.create(
  418. hloopvar.getcopy,cnilnode.create));
  419. addstatement(tempstatement,cbreaknode.create);
  420. addstatement(outerloopbodystatement,cifnode.create(
  421. caddnode.create(equaln,ctemprefnode.create(currentamount),genintconstnode(0)),
  422. hp,nil));
  423. { initial value of mutationcheck }
  424. hp:=ctemprefnode.create(state);
  425. typecheckpass(hp);
  426. hp:=cderefnode.create(genloadfield(hp,'MUTATIONSPTR'));
  427. addstatement(outerloopbodystatement,cassignmentnode.create(
  428. ctemprefnode.create(mutationcheck),hp));
  429. { initialise innerloopcounter }
  430. addstatement(outerloopbodystatement,cassignmentnode.create(
  431. ctemprefnode.create(innerloopcounter),cordconstnode.create(-1,ptruinttype,false)));
  432. { and now the inner loop, again adding the repeat/until afterwards }
  433. innerloop:=internalstatements(innerloopbodystatement);
  434. { inc(innerloopcounter) without range/overflowchecking (because
  435. we go from culong(-1) to 0 during the first iteration }
  436. hp:=cinlinenode.create(
  437. in_inc_x,false,ccallparanode.create(
  438. ctemprefnode.create(innerloopcounter),nil));
  439. hp.localswitches:=hp.localswitches-[cs_check_range,cs_check_overflow];
  440. addstatement(innerloopbodystatement,hp);
  441. { if innerloopcounter=currentamount then break to the outer loop }
  442. addstatement(innerloopbodystatement,cifnode.create(
  443. caddnode.create(equaln,
  444. ctemprefnode.create(innerloopcounter),
  445. ctemprefnode.create(currentamount)),
  446. cbreaknode.create,
  447. nil));
  448. { verify that the collection didn't change in the mean time }
  449. hp:=ctemprefnode.create(state);
  450. typecheckpass(hp);
  451. addstatement(innerloopbodystatement,cifnode.create(
  452. caddnode.create(unequaln,
  453. ctemprefnode.create(mutationcheck),
  454. cderefnode.create(genloadfield(hp,'MUTATIONSPTR'))
  455. ),
  456. ccallnode.createinternfromunit('OBJC','OBJC_ENUMERATIONMUTATION',
  457. ccallparanode.create(ctemprefnode.create(expressiontemp),nil)),
  458. nil));
  459. { finally: actually get the next element }
  460. hp:=ctemprefnode.create(state);
  461. typecheckpass(hp);
  462. hp:=genloadfield(hp,'ITEMSPTR');
  463. typecheckpass(hp);
  464. { don't simply use a vecn, because indexing a pointer won't work in
  465. non-FPC modes }
  466. if hp.resultdef.typ<>pointerdef then
  467. internalerror(2010061904);
  468. inserttypeconv(hp,
  469. carraydef.create_from_pointer(tpointerdef(hp.resultdef)));
  470. hp:=cvecnode.create(hp,ctemprefnode.create(innerloopcounter));
  471. addstatement(innerloopbodystatement,
  472. cassignmentnode.create(hloopvar,hp));
  473. { the actual loop body! }
  474. addstatement(innerloopbodystatement,hloopbody);
  475. { create the inner repeat/until and add it to the body of the outer
  476. one }
  477. hp:=cwhilerepeatnode.create(
  478. { repeat .. until false }
  479. cordconstnode.create(0,pasbool1type,false),innerloop,false,true);
  480. addstatement(outerloopbodystatement,hp);
  481. { create the outer repeat/until and add it to the the main body }
  482. hp:=cwhilerepeatnode.create(
  483. { repeat .. until innerloopcounter<currentamount }
  484. caddnode.create(ltn,
  485. ctemprefnode.create(innerloopcounter),
  486. ctemprefnode.create(currentamount)),
  487. outerloop,false,true);
  488. addstatement(mainstatement,hp);
  489. { release the temps }
  490. addstatement(mainstatement,ctempdeletenode.create(state));
  491. addstatement(mainstatement,ctempdeletenode.create(mutationcheck));
  492. addstatement(mainstatement,ctempdeletenode.create(currentamount));
  493. addstatement(mainstatement,ctempdeletenode.create(innerloopcounter));
  494. addstatement(mainstatement,ctempdeletenode.create(items));
  495. addstatement(mainstatement,ctempdeletenode.create(expressiontemp));
  496. end;
  497. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  498. var
  499. loopstatement, loopbodystatement: tstatementnode;
  500. loopvar, stringvar: ttempcreatenode;
  501. stringindex, loopbody, forloopnode: tnode;
  502. begin
  503. { result is a block of statements }
  504. result:=internalstatements(loopstatement);
  505. { create a temp variable for expression }
  506. stringvar := ctempcreatenode.create(
  507. expr.resultdef,
  508. expr.resultdef.size,
  509. tt_persistent,true);
  510. addstatement(loopstatement,stringvar);
  511. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(stringvar),expr.getcopy));
  512. { create a loop counter: signed integer with size of string length }
  513. loopvar := ctempcreatenode.create(
  514. sinttype,
  515. sinttype.size,
  516. tt_persistent,true);
  517. addstatement(loopstatement,loopvar);
  518. stringindex:=ctemprefnode.create(loopvar);
  519. loopbody:=internalstatements(loopbodystatement);
  520. // for-in loop variable := string_expression[index]
  521. addstatement(loopbodystatement,
  522. cassignmentnode.create(hloopvar, cvecnode.create(ctemprefnode.create(stringvar),stringindex)));
  523. { add the actual statement to the loop }
  524. addstatement(loopbodystatement,hloopbody);
  525. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  526. genintconstnode(1),
  527. cinlinenode.create(in_length_x,false,ctemprefnode.create(stringvar)),
  528. loopbody,
  529. false);
  530. addstatement(loopstatement,forloopnode);
  531. { free the loop counter }
  532. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  533. { free the temp variable for expression }
  534. addstatement(loopstatement,ctempdeletenode.create(stringvar));
  535. end;
  536. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  537. var
  538. loopstatement, loopbodystatement: tstatementnode;
  539. loopvar, arrayvar: ttempcreatenode;
  540. arrayindex, lowbound, highbound, loopbody, forloopnode, expression: tnode;
  541. is_string: boolean;
  542. tmpdef, convertdef: tdef;
  543. elementcount: aword;
  544. begin
  545. expression := expr;
  546. { result is a block of statements }
  547. result:=internalstatements(loopstatement);
  548. is_string:=ado_IsConstString in tarraydef(expr.resultdef).arrayoptions;
  549. // if array element type <> loovar type then create a conversion if possible
  550. if compare_defs(tarraydef(expression.resultdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible then
  551. begin
  552. tmpdef:=expression.resultdef;
  553. elementcount:=1;
  554. while assigned(tmpdef) and (tmpdef.typ=arraydef) and
  555. (tarraydef(tmpdef).arrayoptions = []) and
  556. (compare_defs(tarraydef(tmpdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible) do
  557. begin
  558. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  559. tmpdef:=tarraydef(tmpdef).elementdef;
  560. end;
  561. if assigned(tmpdef) and (tmpdef.typ=arraydef) and (tarraydef(tmpdef).arrayoptions = []) then
  562. begin
  563. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  564. convertdef:=carraydef.create(0,elementcount-1,s32inttype);
  565. tarraydef(convertdef).elementdef:=tarraydef(tmpdef).elementdef;
  566. expression:=expr.getcopy;
  567. expression:=ctypeconvnode.create_internal(expression,convertdef);
  568. typecheckpass(expression);
  569. addstatement(loopstatement,expression);
  570. end;
  571. end;
  572. if (node_complexity(expression) > 1) and
  573. not(is_open_array(expression.resultdef)) and not(is_array_of_const(expression.resultdef)) then
  574. begin
  575. { create a temp variable for expression }
  576. arrayvar := ctempcreatenode.create(
  577. expression.resultdef,
  578. expression.resultdef.size,
  579. tt_persistent,true);
  580. if is_string then
  581. begin
  582. lowbound:=genintconstnode(1);
  583. highbound:=cinlinenode.create(in_length_x,false,ctemprefnode.create(arrayvar))
  584. end
  585. else
  586. begin
  587. lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
  588. highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
  589. end;
  590. addstatement(loopstatement,arrayvar);
  591. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(arrayvar),expression.getcopy));
  592. end
  593. else
  594. begin
  595. arrayvar:=nil;
  596. if is_string then
  597. begin
  598. lowbound:=genintconstnode(1);
  599. highbound:=cinlinenode.create(in_length_x,false,expression.getcopy);
  600. end
  601. else
  602. begin
  603. lowbound:=cinlinenode.create(in_low_x,false,expression.getcopy);
  604. highbound:=cinlinenode.create(in_high_x,false,expression.getcopy);
  605. end;
  606. end;
  607. { create a loop counter }
  608. loopvar := ctempcreatenode.create(
  609. tarraydef(expression.resultdef).rangedef,
  610. tarraydef(expression.resultdef).rangedef.size,
  611. tt_persistent,true);
  612. addstatement(loopstatement,loopvar);
  613. arrayindex:=ctemprefnode.create(loopvar);
  614. loopbody:=internalstatements(loopbodystatement);
  615. // for-in loop variable := array_expression[index]
  616. if assigned(arrayvar) then
  617. addstatement(loopbodystatement,
  618. cassignmentnode.create(hloopvar,cvecnode.create(ctemprefnode.create(arrayvar),arrayindex)))
  619. else
  620. addstatement(loopbodystatement,
  621. cassignmentnode.create(hloopvar,cvecnode.create(expression.getcopy,arrayindex)));
  622. { add the actual statement to the loop }
  623. addstatement(loopbodystatement,hloopbody);
  624. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  625. lowbound,
  626. highbound,
  627. loopbody,
  628. false);
  629. addstatement(loopstatement,forloopnode);
  630. { free the loop counter }
  631. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  632. { free the temp variable for expression if needed }
  633. if arrayvar<>nil then
  634. addstatement(loopstatement,ctempdeletenode.create(arrayvar));
  635. end;
  636. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  637. var
  638. loopstatement, loopbodystatement: tstatementnode;
  639. loopvar, setvar: ttempcreatenode;
  640. loopbody, forloopnode: tnode;
  641. begin
  642. // first check is set is empty and if it so then skip other processing
  643. if not Assigned(tsetdef(expr.resultdef).elementdef) then
  644. begin
  645. result:=cnothingnode.create;
  646. // free unused nodes
  647. hloopvar.free;
  648. hloopbody.free;
  649. exit;
  650. end;
  651. { result is a block of statements }
  652. result:=internalstatements(loopstatement);
  653. { create a temp variable for expression }
  654. setvar := ctempcreatenode.create(
  655. expr.resultdef,
  656. expr.resultdef.size,
  657. tt_persistent,true);
  658. addstatement(loopstatement,setvar);
  659. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(setvar),expr.getcopy));
  660. { create a loop counter }
  661. loopvar := ctempcreatenode.create(
  662. tsetdef(expr.resultdef).elementdef,
  663. tsetdef(expr.resultdef).elementdef.size,
  664. tt_persistent,true);
  665. addstatement(loopstatement,loopvar);
  666. // if loopvar in set then
  667. // begin
  668. // hloopvar := loopvar
  669. // for-in loop body
  670. // end
  671. loopbody:=cifnode.create(
  672. cinnode.create(ctemprefnode.create(loopvar),ctemprefnode.create(setvar)),
  673. internalstatements(loopbodystatement),
  674. nil);
  675. addstatement(loopbodystatement,cassignmentnode.create(hloopvar,ctemprefnode.create(loopvar)));
  676. { add the actual statement to the loop }
  677. addstatement(loopbodystatement,hloopbody);
  678. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  679. cinlinenode.create(in_low_x,false,ctemprefnode.create(setvar)),
  680. cinlinenode.create(in_high_x,false,ctemprefnode.create(setvar)),
  681. loopbody,
  682. false);
  683. addstatement(loopstatement,forloopnode);
  684. { free the loop counter }
  685. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  686. { free the temp variable for expression }
  687. addstatement(loopstatement,ctempdeletenode.create(setvar));
  688. end;
  689. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  690. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  691. var
  692. loopstatement, loopbodystatement: tstatementnode;
  693. enumvar: ttempcreatenode;
  694. loopbody, whileloopnode,
  695. enum_get, enum_move, enum_current, enum_get_params: tnode;
  696. propaccesslist: tpropaccesslist;
  697. enumerator_is_class: boolean;
  698. enumerator_destructor: tprocdef;
  699. begin
  700. { result is a block of statements }
  701. result:=internalstatements(loopstatement);
  702. enumerator_is_class := is_class(enumerator_get.returndef);
  703. { create a temp variable for enumerator }
  704. enumvar := ctempcreatenode.create(
  705. enumerator_get.returndef,
  706. enumerator_get.returndef.size,
  707. tt_persistent,true);
  708. addstatement(loopstatement,enumvar);
  709. if enumerator_get.proctypeoption=potype_operator then
  710. begin
  711. enum_get_params:=ccallparanode.create(expr.getcopy,nil);
  712. enum_get:=ccallnode.create(enum_get_params, tprocsym(enumerator_get.procsym), nil, nil, [],nil);
  713. tcallnode(enum_get).procdefinition:=enumerator_get;
  714. addsymref(enumerator_get.procsym,enumerator_get);
  715. end
  716. else
  717. enum_get:=ccallnode.create(nil, tprocsym(enumerator_get.procsym), enumerator_get.owner, expr.getcopy, [],nil);
  718. addstatement(loopstatement,
  719. cassignmentnode.create(
  720. ctemprefnode.create(enumvar),
  721. enum_get
  722. ));
  723. loopbody:=internalstatements(loopbodystatement);
  724. { for-in loop variable := enumerator.current }
  725. if enumerator_current.getpropaccesslist(palt_read,propaccesslist) then
  726. begin
  727. case propaccesslist.firstsym^.sym.typ of
  728. fieldvarsym :
  729. begin
  730. { generate access code }
  731. enum_current:=ctemprefnode.create(enumvar);
  732. propaccesslist_to_node(enum_current,enumerator_current.owner,propaccesslist);
  733. include(enum_current.flags,nf_isproperty);
  734. end;
  735. procsym :
  736. begin
  737. { generate the method call }
  738. enum_current:=ccallnode.create(nil,tprocsym(propaccesslist.firstsym^.sym),enumerator_current.owner,ctemprefnode.create(enumvar),[],nil);
  739. include(enum_current.flags,nf_isproperty);
  740. end
  741. else
  742. begin
  743. enum_current:=cerrornode.create;
  744. Message(type_e_mismatch);
  745. end;
  746. end;
  747. end
  748. else
  749. enum_current:=cerrornode.create;
  750. addstatement(loopbodystatement,
  751. cassignmentnode.create(hloopvar, enum_current));
  752. { add the actual statement to the loop }
  753. addstatement(loopbodystatement,hloopbody);
  754. enum_move:=ccallnode.create(nil, tprocsym(enumerator_move.procsym), enumerator_move.owner, ctemprefnode.create(enumvar), [],nil);
  755. whileloopnode:=cwhilerepeatnode.create(enum_move,loopbody,true,false);
  756. if enumerator_is_class then
  757. begin
  758. { insert a try-finally and call the destructor for the enumerator in the finally section }
  759. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  760. if assigned(enumerator_destructor) then
  761. begin
  762. whileloopnode:=ctryfinallynode.create(
  763. whileloopnode, // try node
  764. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym), // finally node
  765. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[],nil));
  766. end;
  767. { if getenumerator <> nil then do the loop }
  768. whileloopnode:=cifnode.create(
  769. caddnode.create(unequaln, ctemprefnode.create(enumvar), cnilnode.create),
  770. whileloopnode,
  771. nil);
  772. end;
  773. addstatement(loopstatement, whileloopnode);
  774. if is_object(enumerator_get.returndef) then
  775. begin
  776. // call the object destructor too
  777. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  778. if assigned(enumerator_destructor) then
  779. begin
  780. addstatement(loopstatement,
  781. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym),
  782. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[],nil));
  783. end;
  784. end;
  785. { free the temp variable for enumerator }
  786. addstatement(loopstatement,ctempdeletenode.create(enumvar));
  787. end;
  788. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  789. var
  790. pd, movenext: tprocdef;
  791. helperdef: tobjectdef;
  792. current: tpropertysym;
  793. storefilepos: tfileposinfo;
  794. begin
  795. storefilepos:=current_filepos;
  796. current_filepos:=hloopvar.fileinfo;
  797. if expr.nodetype=typen then
  798. begin
  799. if (expr.resultdef.typ=enumdef) and tenumdef(expr.resultdef).has_jumps then
  800. begin
  801. result:=cerrornode.create;
  802. hloopvar.free;
  803. hloopbody.free;
  804. MessagePos1(expr.fileinfo,parser_e_for_in_loop_cannot_be_used_for_the_type,expr.resultdef.typename);
  805. end
  806. else
  807. result:=create_type_for_in_loop(hloopvar, hloopbody, expr);
  808. end
  809. else
  810. begin
  811. { loop is made for an expression }
  812. // Objective-C uses different conventions (and it's only supported for Objective-C 2.0)
  813. if is_objc_class_or_protocol(hloopvar.resultdef) or
  814. is_objc_class_or_protocol(expr.resultdef) then
  815. begin
  816. result:=create_objc_for_in_loop(hloopvar,hloopbody,expr);
  817. if result.nodetype=errorn then
  818. begin
  819. hloopvar.free;
  820. hloopbody.free;
  821. end;
  822. end
  823. { "for x in [] do ..." always results in a never executed loop body }
  824. else if (is_array_constructor(expr.resultdef) and
  825. (tarraydef(expr.resultdef).elementdef=voidtype)) then
  826. begin
  827. if assigned(hloopbody) then
  828. MessagePos(hloopbody.fileinfo,cg_w_unreachable_code);
  829. result:=cnothingnode.create;
  830. end
  831. else
  832. begin
  833. // search for operator first
  834. pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
  835. // if there is no operator then search for class/object enumerator method
  836. if (pd=nil) and (expr.resultdef.typ in [objectdef,recorddef]) then
  837. begin
  838. { first search using the helper hierarchy }
  839. if search_last_objectpascal_helper(tabstractrecorddef(expr.resultdef),nil,helperdef) then
  840. repeat
  841. pd:=helperdef.search_enumerator_get;
  842. helperdef:=helperdef.childof;
  843. until (pd<>nil) or (helperdef=nil);
  844. { we didn't find an enumerator in a helper, so search in the
  845. class/record/object itself }
  846. if pd=nil then
  847. pd:=tabstractrecorddef(expr.resultdef).search_enumerator_get;
  848. end;
  849. if pd<>nil then
  850. begin
  851. // seach movenext and current symbols
  852. movenext:=tabstractrecorddef(pd.returndef).search_enumerator_move;
  853. if movenext = nil then
  854. begin
  855. result:=cerrornode.create;
  856. hloopvar.free;
  857. hloopbody.free;
  858. MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
  859. end
  860. else
  861. begin
  862. current:=tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  863. if current = nil then
  864. begin
  865. result:=cerrornode.create;
  866. hloopvar.free;
  867. hloopbody.free;
  868. MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
  869. end
  870. else
  871. result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
  872. end;
  873. end
  874. else
  875. begin
  876. { prefer set if loop var could be a set var and the loop
  877. expression can indeed be a set }
  878. if (expr.nodetype=arrayconstructorn) and
  879. (hloopvar.resultdef.typ in [enumdef,orddef]) and
  880. arrayconstructor_can_be_set(expr) then
  881. begin
  882. expr:=arrayconstructor_to_set(expr,false);
  883. typecheckpass(expr);
  884. end;
  885. case expr.resultdef.typ of
  886. stringdef: result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  887. arraydef: result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  888. setdef: result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  889. else
  890. begin
  891. result:=cerrornode.create;
  892. hloopvar.free;
  893. hloopbody.free;
  894. MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
  895. end;
  896. end;
  897. end;
  898. end;
  899. end;
  900. current_filepos:=storefilepos;
  901. end;
  902. function _ConvertForLoops(var n: tnode; arg: pointer): foreachnoderesult;
  903. var
  904. hp : tnode;
  905. begin
  906. Result:=fen_false;
  907. if n.nodetype=forn then
  908. begin
  909. Result:=fen_true;
  910. hp:=n;
  911. n:=tfornode(n).makewhileloop;
  912. do_firstpass(n);
  913. hp.Free;
  914. end;
  915. end;
  916. function ConvertForLoops(var n : tnode) : boolean;
  917. begin
  918. result:=foreachnodestatic(pm_postprocess,n,@_ConvertForLoops,nil);
  919. end;
  920. {****************************************************************************
  921. TLOOPNODE
  922. *****************************************************************************}
  923. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  924. begin
  925. inherited create(tt,l,r);
  926. t1:=_t1;
  927. t2:=_t2;
  928. fileinfo:=l.fileinfo;
  929. end;
  930. destructor tloopnode.destroy;
  931. begin
  932. t1.free;
  933. t2.free;
  934. inherited destroy;
  935. end;
  936. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  937. begin
  938. inherited ppuload(t,ppufile);
  939. t1:=ppuloadnode(ppufile);
  940. t2:=ppuloadnode(ppufile);
  941. ppufile.getset(tppuset1(loopflags));
  942. end;
  943. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  944. begin
  945. inherited ppuwrite(ppufile);
  946. ppuwritenode(ppufile,t1);
  947. ppuwritenode(ppufile,t2);
  948. ppufile.putset(tppuset1(loopflags));
  949. end;
  950. procedure tloopnode.buildderefimpl;
  951. begin
  952. inherited buildderefimpl;
  953. if assigned(t1) then
  954. t1.buildderefimpl;
  955. if assigned(t2) then
  956. t2.buildderefimpl;
  957. end;
  958. procedure tloopnode.derefimpl;
  959. begin
  960. inherited derefimpl;
  961. if assigned(t1) then
  962. t1.derefimpl;
  963. if assigned(t2) then
  964. t2.derefimpl;
  965. end;
  966. function tloopnode.dogetcopy : tnode;
  967. var
  968. p : tloopnode;
  969. begin
  970. p:=tloopnode(inherited dogetcopy);
  971. if assigned(t1) then
  972. p.t1:=t1.dogetcopy
  973. else
  974. p.t1:=nil;
  975. if assigned(t2) then
  976. p.t2:=t2.dogetcopy
  977. else
  978. p.t2:=nil;
  979. p.loopflags:=loopflags;
  980. dogetcopy:=p;
  981. end;
  982. procedure tloopnode.insertintolist(l : tnodelist);
  983. begin
  984. end;
  985. procedure tloopnode.printnodetree(var t:text);
  986. begin
  987. write(t,printnodeindention,'(');
  988. printnodeindent;
  989. printnodeinfo(t);
  990. writeln(t);
  991. printnode(t,left);
  992. printnode(t,right);
  993. printnode(t,t1);
  994. printnode(t,t2);
  995. printnodeunindent;
  996. writeln(t,printnodeindention,')');
  997. end;
  998. {$ifdef DEBUG_NODE_XML}
  999. procedure TLoopNode.XMLPrintNodeInfo(var T: Text);
  1000. var
  1001. i: TLoopFlag;
  1002. First: Boolean;
  1003. begin
  1004. inherited XMLPrintNodeInfo(T);
  1005. First := True;
  1006. for i := Low(TLoopFlag) to High(TLoopFlag) do
  1007. if i in loopflags then
  1008. begin
  1009. if First then
  1010. begin
  1011. Write(T, ' loopflags="', i);
  1012. First := False;
  1013. end
  1014. else
  1015. Write(T, ',', i)
  1016. end;
  1017. if not First then
  1018. Write(T, '"');
  1019. end;
  1020. procedure TLoopNode.XMLPrintNodeTree(var T: Text);
  1021. begin
  1022. Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
  1023. XMLPrintNodeInfo(T);
  1024. WriteLn(T, '>');
  1025. PrintNodeIndent;
  1026. if Assigned(Left) then
  1027. begin
  1028. if nodetype = forn then
  1029. WriteLn(T, PrintNodeIndention, '<counter>')
  1030. else
  1031. WriteLn(T, PrintNodeIndention, '<condition>');
  1032. PrintNodeIndent;
  1033. XMLPrintNode(T, Left);
  1034. PrintNodeUnindent;
  1035. if nodetype = forn then
  1036. WriteLn(T, PrintNodeIndention, '</counter>')
  1037. else
  1038. WriteLn(T, PrintNodeIndention, '</condition>');
  1039. end;
  1040. if Assigned(Right) then
  1041. begin
  1042. case nodetype of
  1043. ifn:
  1044. WriteLn(T, PrintNodeIndention, '<then>');
  1045. forn:
  1046. WriteLn(T, PrintNodeIndention, '<first>');
  1047. else
  1048. WriteLn(T, PrintNodeIndention, '<right>');
  1049. end;
  1050. PrintNodeIndent;
  1051. XMLPrintNode(T, Right);
  1052. PrintNodeUnindent;
  1053. case nodetype of
  1054. ifn:
  1055. WriteLn(T, PrintNodeIndention, '</then>');
  1056. forn:
  1057. WriteLn(T, PrintNodeIndention, '</first>');
  1058. else
  1059. WriteLn(T, PrintNodeIndention, '</right>');
  1060. end;
  1061. end;
  1062. if Assigned(t1) then
  1063. begin
  1064. case nodetype of
  1065. ifn:
  1066. WriteLn(T, PrintNodeIndention, '<else>');
  1067. forn:
  1068. WriteLn(T, PrintNodeIndention, '<last>');
  1069. else
  1070. WriteLn(T, PrintNodeIndention, '<t1>');
  1071. end;
  1072. PrintNodeIndent;
  1073. XMLPrintNode(T, t1);
  1074. PrintNodeUnindent;
  1075. case nodetype of
  1076. ifn:
  1077. WriteLn(T, PrintNodeIndention, '</else>');
  1078. forn:
  1079. WriteLn(T, PrintNodeIndention, '</last>');
  1080. else
  1081. WriteLn(T, PrintNodeIndention, '</t1>');
  1082. end;
  1083. end;
  1084. if Assigned(t2) then
  1085. begin
  1086. if nodetype <> forn then
  1087. begin
  1088. WriteLn(T, PrintNodeIndention, '<loop>');
  1089. PrintNodeIndent;
  1090. end;
  1091. XMLPrintNode(T, t2);
  1092. if nodetype <> forn then
  1093. begin
  1094. PrintNodeUnindent;
  1095. WriteLn(T, PrintNodeIndention, '</loop>');
  1096. end;
  1097. end;
  1098. PrintNodeUnindent;
  1099. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  1100. end;
  1101. {$endif DEBUG_NODE_XML}
  1102. function tloopnode.docompare(p: tnode): boolean;
  1103. begin
  1104. docompare :=
  1105. inherited docompare(p) and
  1106. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  1107. t1.isequal(tloopnode(p).t1) and
  1108. t2.isequal(tloopnode(p).t2);
  1109. end;
  1110. {****************************************************************************
  1111. TWHILEREPEATNODE
  1112. *****************************************************************************}
  1113. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  1114. begin
  1115. inherited create(whilerepeatn,l,r,nil,nil);
  1116. if tab then
  1117. include(loopflags, lnf_testatbegin);
  1118. if cn then
  1119. include(loopflags,lnf_checknegate);
  1120. end;
  1121. function twhilerepeatnode.pass_typecheck:tnode;
  1122. var
  1123. t:Tunarynode;
  1124. begin
  1125. result:=nil;
  1126. resultdef:=voidtype;
  1127. typecheckpass(left);
  1128. { tp procvar support }
  1129. maybe_call_procvar(left,true);
  1130. {A not node can be removed.}
  1131. if left.nodetype=notn then
  1132. begin
  1133. t:=Tunarynode(left);
  1134. left:=Tunarynode(left).left;
  1135. t.left:=nil;
  1136. t.destroy;
  1137. {Symdif operator, in case you are wondering:}
  1138. loopflags:=loopflags >< [lnf_checknegate];
  1139. end;
  1140. { loop instruction }
  1141. if assigned(right) then
  1142. typecheckpass(right);
  1143. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1144. if codegenerror then
  1145. exit;
  1146. if not(is_boolean(left.resultdef)) and
  1147. not(is_typeparam(left.resultdef)) then
  1148. inserttypeconv(left,pasbool1type);
  1149. { Give warnings for code that will never be executed for
  1150. while false do }
  1151. if (lnf_testatbegin in loopflags) and
  1152. (left.nodetype=ordconstn) and
  1153. (tordconstnode(left).value.uvalue=0) and
  1154. assigned(right) then
  1155. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1156. end;
  1157. {$ifdef prefetchnext}
  1158. type
  1159. passignmentquery = ^tassignmentquery;
  1160. tassignmentquery = record
  1161. towhat: tnode;
  1162. source: tassignmentnode;
  1163. statementcount: cardinal;
  1164. end;
  1165. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  1166. var
  1167. query: passignmentquery absolute arg;
  1168. temp, prederef: tnode;
  1169. begin
  1170. result := fen_norecurse_false;
  1171. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  1172. inc(query^.statementcount);
  1173. { make sure there's something else in the loop besides going to the }
  1174. { next item }
  1175. if (query^.statementcount > 1) and
  1176. (n.nodetype = assignn) then
  1177. begin
  1178. { skip type conversions of assignment target }
  1179. temp := tassignmentnode(n).left;
  1180. while (temp.nodetype = typeconvn) do
  1181. temp := ttypeconvnode(temp).left;
  1182. { assignment to x of the while assigned(x) check? }
  1183. if not(temp.isequal(query^.towhat)) then
  1184. exit;
  1185. { right hand side of assignment dereferenced field of }
  1186. { x? (no derefn in case of class) }
  1187. temp := tassignmentnode(n).right;
  1188. while (temp.nodetype = typeconvn) do
  1189. temp := ttypeconvnode(temp).left;
  1190. if (temp.nodetype <> subscriptn) then
  1191. exit;
  1192. prederef := tsubscriptnode(temp).left;
  1193. temp := prederef;
  1194. while (temp.nodetype = typeconvn) do
  1195. temp := ttypeconvnode(temp).left;
  1196. { see tests/test/prefetch1.pp }
  1197. if (temp.nodetype = derefn) then
  1198. temp := tderefnode(temp).left
  1199. else
  1200. temp := prederef;
  1201. if temp.isequal(query^.towhat) then
  1202. begin
  1203. query^.source := tassignmentnode(n);
  1204. result := fen_norecurse_true;
  1205. end
  1206. end
  1207. { don't check nodes which can't contain an assignment or whose }
  1208. { final assignment can vary a lot }
  1209. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  1210. result := fen_false;
  1211. end;
  1212. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  1213. var
  1214. query: tassignmentquery;
  1215. begin
  1216. query.towhat := towhat;
  1217. query.source := nil;
  1218. query.statementcount := 0;
  1219. if foreachnodestatic(where,@checkassignment,@query) then
  1220. result := query.source
  1221. else
  1222. result := nil;
  1223. end;
  1224. {$endif prefetchnext}
  1225. function twhilerepeatnode.pass_1 : tnode;
  1226. {$ifdef prefetchnext}
  1227. var
  1228. runnernode, prefetchcode: tnode;
  1229. assignmentnode: tassignmentnode;
  1230. prefetchstatements: tstatementnode;
  1231. {$endif prefetchnext}
  1232. begin
  1233. result:=nil;
  1234. expectloc:=LOC_VOID;
  1235. firstpass(left);
  1236. if codegenerror then
  1237. exit;
  1238. { loop instruction }
  1239. if assigned(right) then
  1240. begin
  1241. firstpass(right);
  1242. if codegenerror then
  1243. exit;
  1244. end;
  1245. {$ifdef prefetchnext}
  1246. { do at the end so all complex typeconversions are already }
  1247. { converted to calln's }
  1248. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1249. (lnf_testatbegin in loopflags) then
  1250. begin
  1251. { get first component of the while check }
  1252. runnernode := left;
  1253. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  1254. runnernode := tunarynode(runnernode).left;
  1255. { is it an assigned(x) check? }
  1256. if ((runnernode.nodetype = inlinen) and
  1257. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  1258. ((runnernode.nodetype = unequaln) and
  1259. (taddnode(runnernode).right.nodetype = niln)) then
  1260. begin
  1261. runnernode := tunarynode(runnernode).left;
  1262. { in case of in_assigned_x, there's a callparan in between }
  1263. if (runnernode.nodetype = callparan) then
  1264. runnernode := tcallparanode(runnernode).left;
  1265. while (runnernode.nodetype = typeconvn) do
  1266. runnernode := ttypeconvnode(runnernode).left;
  1267. { is there an "x := x(^).somefield"? }
  1268. assignmentnode := findassignment(right,runnernode);
  1269. if assigned(assignmentnode) then
  1270. begin
  1271. prefetchcode := internalstatements(prefetchstatements);
  1272. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  1273. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  1274. addstatement(prefetchstatements,right);
  1275. right := prefetchcode;
  1276. typecheckpass(right);
  1277. end;
  1278. end;
  1279. end;
  1280. {$endif prefetchnext}
  1281. end;
  1282. {$ifdef state_tracking}
  1283. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  1284. var condition:Tnode;
  1285. code:Tnode;
  1286. done:boolean;
  1287. value:boolean;
  1288. change:boolean;
  1289. firsttest:boolean;
  1290. factval:Tnode;
  1291. begin
  1292. track_state_pass:=false;
  1293. done:=false;
  1294. firsttest:=true;
  1295. {For repeat until statements, first do a pass through the code.}
  1296. if not(lnf_testatbegin in flags) then
  1297. begin
  1298. code:=right.getcopy;
  1299. if code.track_state_pass(exec_known) then
  1300. track_state_pass:=true;
  1301. code.destroy;
  1302. end;
  1303. repeat
  1304. condition:=left.getcopy;
  1305. code:=right.getcopy;
  1306. change:=condition.track_state_pass(exec_known);
  1307. factval:=aktstate.find_fact(left);
  1308. if factval<>nil then
  1309. begin
  1310. condition.destroy;
  1311. condition:=factval.getcopy;
  1312. change:=true;
  1313. end;
  1314. if change then
  1315. begin
  1316. track_state_pass:=true;
  1317. {Force new resultdef pass.}
  1318. condition.resultdef:=nil;
  1319. do_typecheckpass(condition);
  1320. end;
  1321. if is_constboolnode(condition) then
  1322. begin
  1323. {Try to turn a while loop into a repeat loop.}
  1324. if firsttest then
  1325. exclude(flags,testatbegin);
  1326. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  1327. if value then
  1328. begin
  1329. if code.track_state_pass(exec_known) then
  1330. track_state_pass:=true;
  1331. end
  1332. else
  1333. done:=true;
  1334. end
  1335. else
  1336. begin
  1337. {Remove any modified variables from the state.}
  1338. code.track_state_pass(false);
  1339. done:=true;
  1340. end;
  1341. code.destroy;
  1342. condition.destroy;
  1343. firsttest:=false;
  1344. until done;
  1345. {The loop condition is also known, for example:
  1346. while i<10 do
  1347. begin
  1348. ...
  1349. end;
  1350. When the loop is done, we do know that i<10 = false.
  1351. }
  1352. condition:=left.getcopy;
  1353. if condition.track_state_pass(exec_known) then
  1354. begin
  1355. track_state_pass:=true;
  1356. {Force new resultdef pass.}
  1357. condition.resultdef:=nil;
  1358. do_typecheckpass(condition);
  1359. end;
  1360. if not is_constboolnode(condition) then
  1361. aktstate.store_fact(condition,
  1362. cordconstnode.create(byte(checknegate),pasbool1type,true))
  1363. else
  1364. condition.destroy;
  1365. end;
  1366. {$endif}
  1367. {*****************************************************************************
  1368. TIFNODE
  1369. *****************************************************************************}
  1370. constructor tifnode.create(l,r,_t1 : tnode);
  1371. begin
  1372. inherited create(ifn,l,r,_t1,nil);
  1373. end;
  1374. constructor tifnode.create_internal(l,r,_t1 : tnode);
  1375. begin
  1376. create(l,r,_t1);
  1377. include(flags,nf_internal);
  1378. end;
  1379. function tifnode.internalsimplify(warn: boolean) : tnode;
  1380. begin
  1381. result:=nil;
  1382. { optimize constant expressions }
  1383. if (left.nodetype=ordconstn) then
  1384. begin
  1385. if tordconstnode(left).value.uvalue<>0 then
  1386. begin
  1387. if assigned(right) then
  1388. result:=right
  1389. else
  1390. result:=cnothingnode.create;
  1391. right:=nil;
  1392. if warn and assigned(t1) then
  1393. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1394. end
  1395. else
  1396. begin
  1397. if assigned(t1) then
  1398. result:=t1
  1399. else
  1400. result:=cnothingnode.create;
  1401. t1:=nil;
  1402. if warn and assigned(right) then
  1403. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1404. end;
  1405. end;
  1406. end;
  1407. function tifnode.simplify(forinline : boolean) : tnode;
  1408. begin
  1409. result:=internalsimplify(false);
  1410. end;
  1411. function tifnode.pass_typecheck:tnode;
  1412. begin
  1413. result:=nil;
  1414. resultdef:=voidtype;
  1415. typecheckpass(left);
  1416. { tp procvar support }
  1417. maybe_call_procvar(left,true);
  1418. { if path }
  1419. if assigned(right) then
  1420. typecheckpass(right);
  1421. { else path }
  1422. if assigned(t1) then
  1423. typecheckpass(t1);
  1424. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1425. if codegenerror then
  1426. exit;
  1427. if not(is_boolean(left.resultdef)) and
  1428. not(is_typeparam(left.resultdef)) then
  1429. inserttypeconv(left,pasbool1type);
  1430. result:=internalsimplify(not(nf_internal in flags));
  1431. end;
  1432. function tifnode.pass_1 : tnode;
  1433. begin
  1434. result:=nil;
  1435. expectloc:=LOC_VOID;
  1436. firstpass(left);
  1437. { if path }
  1438. if assigned(right) then
  1439. firstpass(right);
  1440. { else path }
  1441. if assigned(t1) then
  1442. firstpass(t1);
  1443. { leave if we've got an error in one of the paths }
  1444. if codegenerror then
  1445. exit;
  1446. end;
  1447. {*****************************************************************************
  1448. TFORNODE
  1449. *****************************************************************************}
  1450. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1451. begin
  1452. inherited create(forn,l,r,_t1,_t2);
  1453. if back then
  1454. include(loopflags,lnf_backward);
  1455. include(loopflags,lnf_testatbegin);
  1456. end;
  1457. function tfornode.simplify(forinline : boolean) : tnode;
  1458. begin
  1459. result:=nil;
  1460. { Can we spare the first comparision? }
  1461. if (t1.nodetype=ordconstn) and
  1462. (right.nodetype=ordconstn) and
  1463. (
  1464. (
  1465. (lnf_backward in loopflags) and
  1466. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1467. ) or
  1468. (
  1469. not(lnf_backward in loopflags) and
  1470. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1471. )
  1472. ) then
  1473. exclude(loopflags,lnf_testatbegin);
  1474. if (t1.nodetype=ordconstn) and
  1475. (right.nodetype=ordconstn) and
  1476. (
  1477. (
  1478. (lnf_backward in loopflags) and
  1479. (tordconstnode(right).value<tordconstnode(t1).value)
  1480. ) or
  1481. (
  1482. not(lnf_backward in loopflags) and
  1483. (tordconstnode(right).value>tordconstnode(t1).value)
  1484. )
  1485. ) then
  1486. result:=cnothingnode.create;
  1487. end;
  1488. function tfornode.pass_typecheck:tnode;
  1489. var
  1490. res : tnode;
  1491. rangedef: tdef;
  1492. begin
  1493. result:=nil;
  1494. resultdef:=voidtype;
  1495. { process the loopvar, from and to, varstates are already set }
  1496. typecheckpass(left);
  1497. typecheckpass(right);
  1498. typecheckpass(t1);
  1499. set_varstate(left,vs_written,[]);
  1500. { Make sure that the loop var and the
  1501. from and to values are compatible types }
  1502. if not(m_iso in current_settings.modeswitches) then
  1503. rangedef:=left.resultdef
  1504. else
  1505. rangedef:=get_iso_range_type(left.resultdef);
  1506. check_ranges(right.fileinfo,right,rangedef);
  1507. inserttypeconv(right,rangedef);
  1508. check_ranges(t1.fileinfo,t1,rangedef);
  1509. inserttypeconv(t1,rangedef);
  1510. if assigned(t2) then
  1511. typecheckpass(t2);
  1512. result:=simplify(false);
  1513. { loop unrolling }
  1514. if not(assigned(result)) and
  1515. (cs_opt_loopunroll in current_settings.optimizerswitches) and
  1516. assigned(t2) and
  1517. { statements must be error free }
  1518. not(nf_error in t2.flags) then
  1519. begin
  1520. typecheckpass(t2);
  1521. res:=t2.simplify(false);
  1522. if assigned(res) then
  1523. t2:=res;
  1524. res:=unroll_loop(self);
  1525. if assigned(res) then
  1526. begin
  1527. typecheckpass(res);
  1528. result:=res;
  1529. exit;
  1530. end;
  1531. end;
  1532. end;
  1533. function tfornode.pass_1 : tnode;
  1534. begin
  1535. result:=nil;
  1536. expectloc:=LOC_VOID;
  1537. firstpass(left);
  1538. firstpass(right);
  1539. firstpass(t1);
  1540. if assigned(t2) then
  1541. firstpass(t2);
  1542. end;
  1543. function tfornode.makewhileloop : tnode;
  1544. var
  1545. ifblock,loopblock : tblocknode;
  1546. ifstatements,statements,loopstatements : tstatementnode;
  1547. fromtemp,totemp : ttempcreatenode;
  1548. do_loopvar_at_end : Boolean;
  1549. { if the lower and/or upper bound are variable, we need a surrounding if }
  1550. needsifblock : Boolean;
  1551. cond : tnodetype;
  1552. fromexpr : tnode;
  1553. toexpr : tnode;
  1554. { if the upper bound is not constant, it must be store in a temp initially }
  1555. usetotemp : boolean;
  1556. { if the lower bound is not constant, it must be store in a temp before calculating the upper bound }
  1557. usefromtemp : boolean;
  1558. storefilepos: tfileposinfo;
  1559. procedure iterate_counter(var s : tstatementnode;fw : boolean);
  1560. begin
  1561. if fw then
  1562. addstatement(s,
  1563. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_succ_x,false,left.getcopy)))
  1564. else
  1565. addstatement(s,
  1566. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_pred_x,false,left.getcopy)));
  1567. end;
  1568. function iterate_counter_func(arg : tnode;fw : boolean) : tnode;
  1569. begin
  1570. if fw then
  1571. result:=cinlinenode.createintern(in_succ_x,false,arg)
  1572. else
  1573. result:=cinlinenode.createintern(in_pred_x,false,arg);
  1574. end;
  1575. begin
  1576. result:=nil;
  1577. totemp:=nil;
  1578. fromtemp:=nil;
  1579. storefilepos:=current_filepos;
  1580. current_filepos:=fileinfo;
  1581. do_loopvar_at_end:=(lnf_dont_mind_loopvar_on_exit in loopflags)
  1582. { if the loop is unrolled and there is a jump into the loop,
  1583. then we can't do the trick with incrementing the loop var only at the
  1584. end
  1585. }
  1586. and not(assigned(entrylabel));
  1587. { calculate pointer value and check if changeable and if so
  1588. load into temporary variable }
  1589. if (right.nodetype<>ordconstn) or (t1.nodetype<>ordconstn) then
  1590. begin
  1591. do_loopvar_at_end:=false;
  1592. needsifblock:=true;
  1593. end
  1594. else
  1595. needsifblock:=false;
  1596. { convert the for loop into a while loop }
  1597. result:=internalstatements(statements);
  1598. ifblock:=internalstatements(ifstatements);
  1599. loopblock:=internalstatements(loopstatements);
  1600. usefromtemp:=(might_have_sideeffects(t1) and not(is_const(right))) or (node_complexity(right)>1);
  1601. usetotemp:=not(is_const(t1));
  1602. if needsifblock then
  1603. begin
  1604. { do not generate a temp. for the from node, if it is a const, it can be copied directly since
  1605. no side effect might change it }
  1606. if usefromtemp then
  1607. begin
  1608. fromtemp:=ctempcreatenode.create(right.resultdef,right.resultdef.size,tt_persistent,true);
  1609. { the if block might be optimized out, so we put the deletetempnode after the if-block, however,
  1610. this causes a long life time of the fromtemp. If the final regsync is left away, the reg. allocator
  1611. figures out the needed life time. As their are no loops involved between the uses of the fromtemp,
  1612. this does no hurt }
  1613. fromtemp.includetempflag(ti_no_final_regsync);
  1614. addstatement(statements,fromtemp);
  1615. { while it would be beneficial to fold the initial reverse succ/pred into this assignment, this is
  1616. not possible because it might wrap around and the if check later on goes wrong }
  1617. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(fromtemp),right.getcopy));
  1618. end;
  1619. if usetotemp then
  1620. begin
  1621. totemp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1622. addstatement(statements,totemp);
  1623. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(totemp),t1.getcopy));
  1624. end;
  1625. if usefromtemp then
  1626. begin
  1627. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,ctemprefnode.create(fromtemp)));
  1628. if not(do_loopvar_at_end) then
  1629. iterate_counter(ifstatements,lnf_backward in loopflags);
  1630. end
  1631. else
  1632. begin
  1633. if not(do_loopvar_at_end) then
  1634. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1635. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1636. else
  1637. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1638. end;
  1639. end
  1640. else
  1641. begin
  1642. if not(do_loopvar_at_end) then
  1643. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1644. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1645. else
  1646. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1647. end;
  1648. if assigned(entrylabel) then
  1649. addstatement(ifstatements,cgotonode.create(tlabelnode(entrylabel).labsym));
  1650. if not(do_loopvar_at_end) then
  1651. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1652. { avoid copying t2, it is used only once and it might be big }
  1653. addstatement(loopstatements,t2);
  1654. t2:=nil;
  1655. if do_loopvar_at_end then
  1656. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1657. if do_loopvar_at_end then
  1658. begin
  1659. if lnf_backward in loopflags then
  1660. cond:=ltn
  1661. else
  1662. cond:=gtn;
  1663. end
  1664. else
  1665. begin
  1666. if lnf_backward in loopflags then
  1667. cond:=lten
  1668. else
  1669. cond:=gten;
  1670. end;
  1671. if needsifblock then
  1672. begin
  1673. if usetotemp then
  1674. toexpr:=ctemprefnode.create(totemp)
  1675. else
  1676. toexpr:=t1.getcopy;
  1677. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,left.getcopy,toexpr),loopblock,false,true));
  1678. if usefromtemp then
  1679. fromexpr:=ctemprefnode.create(fromtemp)
  1680. else
  1681. fromexpr:=right.getcopy;
  1682. if usetotemp then
  1683. toexpr:=ctemprefnode.create(totemp)
  1684. else
  1685. toexpr:=t1.getcopy;
  1686. if lnf_backward in loopflags then
  1687. addstatement(statements,cifnode.create(caddnode.create_internal(gten,
  1688. fromexpr,toexpr),ifblock,nil))
  1689. else
  1690. addstatement(statements,cifnode.create(caddnode.create_internal(lten,
  1691. fromexpr,toexpr),ifblock,nil));
  1692. if usetotemp then
  1693. addstatement(statements,ctempdeletenode.create(totemp));
  1694. if usefromtemp then
  1695. addstatement(statements,ctempdeletenode.create(fromtemp));
  1696. end
  1697. else
  1698. begin
  1699. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,left.getcopy,t1.getcopy),loopblock,false,true));
  1700. addstatement(statements,ifblock);
  1701. end;
  1702. current_filepos:=storefilepos;
  1703. end;
  1704. {*****************************************************************************
  1705. TEXITNODE
  1706. *****************************************************************************}
  1707. constructor texitnode.create(l:tnode);
  1708. begin
  1709. inherited create(exitn,l);
  1710. if assigned(left) then
  1711. begin
  1712. { add assignment to funcretsym }
  1713. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1714. left:=cassignmentnode.create(
  1715. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1716. left);
  1717. end;
  1718. end;
  1719. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1720. begin
  1721. inherited ppuload(t,ppufile);
  1722. end;
  1723. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1724. begin
  1725. inherited ppuwrite(ppufile);
  1726. end;
  1727. function texitnode.pass_typecheck:tnode;
  1728. var
  1729. newstatement : tstatementnode;
  1730. ressym: tsym;
  1731. resdef: tdef;
  1732. begin
  1733. result:=nil;
  1734. newstatement:=nil;
  1735. if assigned(left) then
  1736. begin
  1737. result:=internalstatements(newstatement);
  1738. addstatement(newstatement,left);
  1739. left:=nil;
  1740. end;
  1741. { if the function result has been migrated to the parentfpstruct,
  1742. we have to load it back to the original location (from which the
  1743. code generator will load it into the function result location),
  1744. because the code to this that we add in tnodeutils.wrap_proc_body()
  1745. gets inserted before the exit label to which this node will jump }
  1746. if (target_info.system in systems_fpnestedstruct) and
  1747. not(nf_internal in flags) and
  1748. current_procinfo.procdef.get_funcretsym_info(ressym,resdef) and
  1749. (tabstractnormalvarsym(ressym).inparentfpstruct) then
  1750. begin
  1751. if not assigned(result) then
  1752. result:=internalstatements(newstatement);
  1753. cnodeutils.load_parentfpstruct_nested_funcret(ressym,newstatement);
  1754. end;
  1755. if assigned(result) then
  1756. begin
  1757. addstatement(newstatement,self.getcopy);
  1758. { ensure we don't insert the function result loading code again for
  1759. this node }
  1760. include(newstatement.left.flags,nf_internal);
  1761. end;
  1762. resultdef:=voidtype;
  1763. end;
  1764. function texitnode.pass_1 : tnode;
  1765. begin
  1766. result:=nil;
  1767. expectloc:=LOC_VOID;
  1768. if assigned(left) then
  1769. internalerror(2011052801);
  1770. end;
  1771. {*****************************************************************************
  1772. TBREAKNODE
  1773. *****************************************************************************}
  1774. constructor tbreaknode.create;
  1775. begin
  1776. inherited create(breakn);
  1777. end;
  1778. function tbreaknode.pass_typecheck:tnode;
  1779. begin
  1780. result:=nil;
  1781. resultdef:=voidtype;
  1782. end;
  1783. function tbreaknode.pass_1 : tnode;
  1784. begin
  1785. result:=nil;
  1786. expectloc:=LOC_VOID;
  1787. end;
  1788. {*****************************************************************************
  1789. TCONTINUENODE
  1790. *****************************************************************************}
  1791. constructor tcontinuenode.create;
  1792. begin
  1793. inherited create(continuen);
  1794. end;
  1795. function tcontinuenode.pass_typecheck:tnode;
  1796. begin
  1797. result:=nil;
  1798. resultdef:=voidtype;
  1799. end;
  1800. function tcontinuenode.pass_1 : tnode;
  1801. begin
  1802. result:=nil;
  1803. expectloc:=LOC_VOID;
  1804. end;
  1805. {*****************************************************************************
  1806. TGOTONODE
  1807. *****************************************************************************}
  1808. constructor tgotonode.create(p : tlabelsym);
  1809. begin
  1810. inherited create(goton);
  1811. exceptionblock:=current_exceptblock;
  1812. labelnode:=nil;
  1813. labelsym:=p;
  1814. end;
  1815. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1816. begin
  1817. inherited ppuload(t,ppufile);
  1818. labelnodeidx:=ppufile.getlongint;
  1819. exceptionblock:=ppufile.getbyte;
  1820. end;
  1821. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  1822. begin
  1823. inherited ppuwrite(ppufile);
  1824. labelnodeidx:=labelnode.ppuidx;
  1825. ppufile.putlongint(labelnodeidx);
  1826. ppufile.putbyte(exceptionblock);
  1827. end;
  1828. procedure tgotonode.buildderefimpl;
  1829. begin
  1830. inherited buildderefimpl;
  1831. end;
  1832. procedure tgotonode.derefimpl;
  1833. begin
  1834. inherited derefimpl;
  1835. end;
  1836. procedure tgotonode.resolveppuidx;
  1837. begin
  1838. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  1839. if labelnode.nodetype<>labeln then
  1840. internalerror(200809021);
  1841. end;
  1842. function tgotonode.pass_typecheck:tnode;
  1843. begin
  1844. result:=nil;
  1845. resultdef:=voidtype;
  1846. end;
  1847. function tgotonode.pass_1 : tnode;
  1848. var
  1849. p2 : tprocinfo;
  1850. begin
  1851. result:=nil;
  1852. expectloc:=LOC_VOID;
  1853. { The labelnode can already be set when
  1854. this node was copied }
  1855. if not(assigned(labelnode)) then
  1856. begin
  1857. { inner procedure goto? }
  1858. if assigned(labelsym.code) and
  1859. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1860. { generated by the optimizer? }
  1861. not(assigned(labelsym.owner))) then
  1862. labelnode:=tlabelnode(labelsym.code)
  1863. else if ((m_non_local_goto in current_settings.modeswitches) and
  1864. assigned(labelsym.owner)) or
  1865. { nested exits don't need the non local goto switch }
  1866. (labelsym.realname='$nestedexit') then
  1867. begin
  1868. if current_procinfo.procdef.parast.symtablelevel>labelsym.owner.symtablelevel then
  1869. begin
  1870. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  1871. if exceptionblock>0 then
  1872. CGMessage(cg_e_goto_inout_of_exception_block);
  1873. { goto across procedures using exception?
  1874. this is not allowed because we cannot
  1875. easily unwind the exception frame
  1876. stack
  1877. }
  1878. p2:=current_procinfo;
  1879. while true do
  1880. begin
  1881. if (p2.flags*[pi_needs_implicit_finally,pi_uses_exceptions,pi_has_implicit_finally])<>[] then
  1882. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  1883. if labelsym.owner=p2.procdef.localst then
  1884. break;
  1885. p2:=p2.parent
  1886. end;
  1887. if assigned(labelsym.jumpbuf) then
  1888. begin
  1889. labelsym.nonlocal:=true;
  1890. result:=ccallnode.createintern('fpc_longjmp',
  1891. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  1892. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  1893. nil)));
  1894. end
  1895. else
  1896. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1897. end
  1898. else
  1899. CGMessage(cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  1900. end
  1901. else
  1902. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1903. end;
  1904. { check if we don't mess with exception blocks }
  1905. if assigned(labelnode) and
  1906. (exceptionblock<>labelnode.exceptionblock) then
  1907. CGMessage(cg_e_goto_inout_of_exception_block);
  1908. end;
  1909. function tgotonode.dogetcopy : tnode;
  1910. var
  1911. p : tgotonode;
  1912. begin
  1913. p:=tgotonode(inherited dogetcopy);
  1914. p.exceptionblock:=exceptionblock;
  1915. { generate labelnode if not done yet }
  1916. if not(assigned(labelnode)) then
  1917. begin
  1918. if assigned(labelsym) and assigned(labelsym.code) then
  1919. labelnode:=tlabelnode(labelsym.code)
  1920. end;
  1921. p.labelsym:=labelsym;
  1922. { do not copy the label node here as we do not know if the label node is part of the tree or not,
  1923. this will be fixed after the copying in node.setuplabelnode: if the labelnode has copiedto set,
  1924. labelnode of the goto node is update }
  1925. if assigned(labelnode) then
  1926. p.labelnode:=labelnode
  1927. else
  1928. begin
  1929. { don't trigger IE when there was already an error, i.e. the
  1930. label is not defined. See tw11763 (PFV) }
  1931. if (errorcount=0) and
  1932. { don't trigger IE if it's a global goto }
  1933. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1934. not(assigned(labelsym.owner))) then
  1935. internalerror(200610291);
  1936. end;
  1937. result:=p;
  1938. end;
  1939. function tgotonode.docompare(p: tnode): boolean;
  1940. begin
  1941. docompare := false;
  1942. end;
  1943. {*****************************************************************************
  1944. TLABELNODE
  1945. *****************************************************************************}
  1946. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  1947. begin
  1948. inherited create(labeln,l);
  1949. exceptionblock:=current_exceptblock;
  1950. labsym:=alabsym;
  1951. { Register labelnode in labelsym }
  1952. labsym.code:=self;
  1953. end;
  1954. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1955. begin
  1956. inherited ppuload(t,ppufile);
  1957. exceptionblock:=ppufile.getbyte;
  1958. end;
  1959. destructor tlabelnode.destroy;
  1960. begin
  1961. if assigned(labsym) then
  1962. begin
  1963. if not assigned(labsym.Owner) then
  1964. labsym.Free // Free labelsym if it has no owner
  1965. else
  1966. if labsym.code=pointer(self) then
  1967. begin
  1968. { Remove reference in labelsym, this is to prevent
  1969. goto's to this label }
  1970. labsym.code:=nil;
  1971. end;
  1972. end;
  1973. inherited destroy;
  1974. end;
  1975. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  1976. begin
  1977. inherited ppuwrite(ppufile);
  1978. ppufile.putbyte(exceptionblock);
  1979. end;
  1980. procedure tlabelnode.buildderefimpl;
  1981. begin
  1982. inherited buildderefimpl;
  1983. end;
  1984. procedure tlabelnode.derefimpl;
  1985. begin
  1986. inherited derefimpl;
  1987. end;
  1988. function tlabelnode.pass_typecheck:tnode;
  1989. begin
  1990. result:=nil;
  1991. { left could still be unassigned }
  1992. if assigned(left) then
  1993. typecheckpass(left);
  1994. resultdef:=voidtype;
  1995. end;
  1996. function tlabelnode.pass_1 : tnode;
  1997. begin
  1998. result:=nil;
  1999. expectloc:=LOC_VOID;
  2000. include(current_procinfo.flags,pi_has_label);
  2001. if assigned(left) then
  2002. firstpass(left);
  2003. if (m_non_local_goto in current_settings.modeswitches) and
  2004. { the owner can be Nil for internal labels }
  2005. assigned(labsym.owner) and
  2006. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  2007. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  2008. end;
  2009. function tlabelnode.dogetcopy : tnode;
  2010. begin
  2011. if not(assigned(copiedto)) then
  2012. copiedto:=tlabelnode(inherited dogetcopy);
  2013. copiedto.exceptionblock:=exceptionblock;
  2014. result:=copiedto;
  2015. end;
  2016. function tlabelnode.docompare(p: tnode): boolean;
  2017. begin
  2018. docompare := false;
  2019. end;
  2020. {*****************************************************************************
  2021. TRAISENODE
  2022. *****************************************************************************}
  2023. constructor traisenode.create(l,taddr,tframe:tnode);
  2024. begin
  2025. inherited create(raisen,l,taddr,tframe);
  2026. end;
  2027. function traisenode.pass_typecheck:tnode;
  2028. begin
  2029. result:=nil;
  2030. resultdef:=voidtype;
  2031. if assigned(left) then
  2032. begin
  2033. { first para must be a _class_ }
  2034. typecheckpass(left);
  2035. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2036. if codegenerror then
  2037. exit;
  2038. if not is_class(left.resultdef) and
  2039. not is_javaclass(left.resultdef) then
  2040. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2041. { insert needed typeconvs for addr,frame }
  2042. if assigned(right) then
  2043. begin
  2044. { addr }
  2045. typecheckpass(right);
  2046. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2047. inserttypeconv(right,voidcodepointertype);
  2048. { frame }
  2049. if assigned(third) then
  2050. begin
  2051. typecheckpass(third);
  2052. set_varstate(third,vs_read,[vsf_must_be_valid]);
  2053. inserttypeconv(third,voidpointertype);
  2054. end;
  2055. end;
  2056. end;
  2057. end;
  2058. function traisenode.pass_1 : tnode;
  2059. var
  2060. statements : tstatementnode;
  2061. current_addr : tlabelnode;
  2062. raisenode : tcallnode;
  2063. begin
  2064. result:=internalstatements(statements);
  2065. if assigned(left) then
  2066. begin
  2067. { first para must be a class }
  2068. firstpass(left);
  2069. { insert needed typeconvs for addr,frame }
  2070. if assigned(right) then
  2071. begin
  2072. { addr }
  2073. firstpass(right);
  2074. { frame }
  2075. if assigned(third) then
  2076. firstpass(third)
  2077. else
  2078. third:=cpointerconstnode.Create(0,voidpointertype);
  2079. end
  2080. else
  2081. begin
  2082. third:=cinlinenode.create(in_get_frame,false,nil);
  2083. current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
  2084. addstatement(statements,current_addr);
  2085. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  2086. { raise address off by one so we are for sure inside the action area for the raise }
  2087. if tf_use_psabieh in target_info.flags then
  2088. right:=caddnode.create_internal(addn,right,cordconstnode.create(1,sizesinttype,false));
  2089. end;
  2090. raisenode:=ccallnode.createintern('fpc_raiseexception',
  2091. ccallparanode.create(third,
  2092. ccallparanode.create(right,
  2093. ccallparanode.create(left,nil)))
  2094. );
  2095. include(raisenode.callnodeflags,cnf_call_never_returns);
  2096. addstatement(statements,raisenode);
  2097. end
  2098. else
  2099. begin
  2100. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  2101. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  2102. include(raisenode.callnodeflags,cnf_call_never_returns);
  2103. addstatement(statements,raisenode);
  2104. end;
  2105. left:=nil;
  2106. right:=nil;
  2107. third:=nil;
  2108. end;
  2109. {*****************************************************************************
  2110. TTRYEXCEPTNODE
  2111. *****************************************************************************}
  2112. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  2113. begin
  2114. inherited create(tryexceptn,l,r,_t1,nil);
  2115. end;
  2116. function ttryexceptnode.pass_typecheck:tnode;
  2117. begin
  2118. result:=nil;
  2119. typecheckpass(left);
  2120. { on statements }
  2121. if assigned(right) then
  2122. typecheckpass(right);
  2123. { else block }
  2124. if assigned(t1) then
  2125. typecheckpass(t1);
  2126. resultdef:=voidtype;
  2127. end;
  2128. function ttryexceptnode.pass_1 : tnode;
  2129. begin
  2130. result:=nil;
  2131. expectloc:=LOC_VOID;
  2132. firstpass(left);
  2133. { on statements }
  2134. if assigned(right) then
  2135. firstpass(right);
  2136. { else block }
  2137. if assigned(t1) then
  2138. firstpass(t1);
  2139. include(current_procinfo.flags,pi_do_call);
  2140. include(current_procinfo.flags,pi_uses_exceptions);
  2141. adjust_estimated_stack_size;
  2142. end;
  2143. function ttryexceptnode.simplify(forinline: boolean): tnode;
  2144. begin
  2145. result:=nil;
  2146. { empty try -> can never raise exception -> do nothing }
  2147. if has_no_code(left) then
  2148. result:=cnothingnode.create;
  2149. end;
  2150. procedure ttryexceptnode.adjust_estimated_stack_size;
  2151. begin
  2152. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size*2);
  2153. end;
  2154. {*****************************************************************************
  2155. TTRYFINALLYNODE
  2156. *****************************************************************************}
  2157. constructor ttryfinallynode.create(l,r:tnode);
  2158. begin
  2159. inherited create(tryfinallyn,l,r,nil);
  2160. third:=nil;
  2161. implicitframe:=false;
  2162. end;
  2163. constructor ttryfinallynode.create_implicit(l,r:tnode);
  2164. begin
  2165. inherited create(tryfinallyn,l,r,nil);
  2166. third:=nil;
  2167. implicitframe:=true;
  2168. end;
  2169. function ttryfinallynode.pass_typecheck:tnode;
  2170. begin
  2171. result:=nil;
  2172. resultdef:=voidtype;
  2173. typecheckpass(left);
  2174. // "try block" is "used"? (JM)
  2175. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  2176. typecheckpass(right);
  2177. // "except block" is "used"? (JM)
  2178. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  2179. if assigned(third) then
  2180. begin
  2181. typecheckpass(third);
  2182. set_varstate(third,vs_readwritten,[vsf_must_be_valid]);
  2183. end;
  2184. end;
  2185. function ttryfinallynode.pass_1 : tnode;
  2186. begin
  2187. result:=nil;
  2188. expectloc:=LOC_VOID;
  2189. firstpass(left);
  2190. firstpass(right);
  2191. if assigned(third) then
  2192. firstpass(third);
  2193. include(current_procinfo.flags,pi_do_call);
  2194. { pi_uses_exceptions is an information for the optimizer and it
  2195. is only interested in exceptions if they appear inside the body,
  2196. so ignore implicit frames when setting the flag }
  2197. if not(implicitframe) then
  2198. include(current_procinfo.flags,pi_uses_exceptions);
  2199. adjust_estimated_stack_size;
  2200. end;
  2201. function ttryfinallynode.simplify(forinline : boolean): tnode;
  2202. begin
  2203. result:=nil;
  2204. { if the try contains no code, we can kill
  2205. the try and except and return only the
  2206. finally part }
  2207. if has_no_code(left) then
  2208. begin
  2209. result:=right;
  2210. right:=nil;
  2211. end;
  2212. end;
  2213. procedure ttryfinallynode.adjust_estimated_stack_size;
  2214. begin
  2215. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size);
  2216. end;
  2217. {*****************************************************************************
  2218. TONNODE
  2219. *****************************************************************************}
  2220. constructor tonnode.create(l,r:tnode);
  2221. begin
  2222. inherited create(onn,l,r);
  2223. excepTSymtable:=nil;
  2224. excepttype:=nil;
  2225. end;
  2226. destructor tonnode.destroy;
  2227. begin
  2228. { copied nodes don't need to release the symtable }
  2229. if assigned(excepTSymtable) then
  2230. excepTSymtable.free;
  2231. inherited destroy;
  2232. end;
  2233. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2234. begin
  2235. inherited ppuload(t,ppufile);
  2236. excepTSymtable:=nil;
  2237. excepttype:=nil;
  2238. end;
  2239. function tonnode.dogetcopy : tnode;
  2240. var
  2241. n : tonnode;
  2242. begin
  2243. n:=tonnode(inherited dogetcopy);
  2244. if assigned(exceptsymtable) then
  2245. n.exceptsymtable:=exceptsymtable.getcopy
  2246. else
  2247. n.exceptsymtable:=nil;
  2248. n.excepttype:=excepttype;
  2249. result:=n;
  2250. end;
  2251. function tonnode.pass_typecheck:tnode;
  2252. begin
  2253. result:=nil;
  2254. resultdef:=voidtype;
  2255. if not is_class(excepttype) and
  2256. not is_javaclass(excepttype) then
  2257. CGMessage1(type_e_class_type_expected,excepttype.typename);
  2258. if assigned(left) then
  2259. typecheckpass(left);
  2260. if assigned(right) then
  2261. typecheckpass(right);
  2262. end;
  2263. function tonnode.pass_1 : tnode;
  2264. begin
  2265. result:=nil;
  2266. include(current_procinfo.flags,pi_do_call);
  2267. expectloc:=LOC_VOID;
  2268. if assigned(left) then
  2269. firstpass(left);
  2270. if assigned(right) then
  2271. firstpass(right);
  2272. end;
  2273. function tonnode.docompare(p: tnode): boolean;
  2274. begin
  2275. docompare := false;
  2276. end;
  2277. end.