nflw.pas 71 KB

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