nflw.pas 76 KB

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