nflw.pas 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and register allocation for nodes that influence
  4. the flow
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nflw;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. node,cpubase,
  24. symnot,
  25. symtype,symbase,symdef,symsym,
  26. optloop;
  27. type
  28. { flags used by loop nodes }
  29. tloopflag = (
  30. { set if it is a for ... downto ... do loop }
  31. lnf_backward,
  32. { Do we need to parse childs to set var state? }
  33. lnf_varstate,
  34. { Do a test at the begin of the loop?}
  35. lnf_testatbegin,
  36. { Negate the loop test? }
  37. lnf_checknegate,
  38. { Should the value of the loop variable on exit be correct. }
  39. lnf_dont_mind_loopvar_on_exit,
  40. { Loop simplify flag }
  41. lnf_simplify_processing);
  42. tloopflags = set of tloopflag;
  43. const
  44. { loop flags which must match to consider loop nodes equal regarding the flags }
  45. loopflagsequal = [lnf_backward];
  46. type
  47. tlabelnode = class;
  48. tloopnode = class(tbinarynode)
  49. t1,t2 : tnode;
  50. loopflags : tloopflags;
  51. constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
  52. destructor destroy;override;
  53. function dogetcopy : tnode;override;
  54. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  55. procedure ppuwrite(ppufile:tcompilerppufile);override;
  56. procedure buildderefimpl;override;
  57. procedure derefimpl;override;
  58. procedure insertintolist(l : tnodelist);override;
  59. procedure printnodetree(var t:text);override;
  60. function docompare(p: tnode): boolean; override;
  61. end;
  62. twhilerepeatnode = class(tloopnode)
  63. constructor create(l,r:Tnode;tab,cn:boolean);virtual;reintroduce;
  64. function pass_typecheck:tnode;override;
  65. function pass_1 : tnode;override;
  66. {$ifdef state_tracking}
  67. function track_state_pass(exec_known:boolean):boolean;override;
  68. {$endif}
  69. end;
  70. twhilerepeatnodeclass = class of twhilerepeatnode;
  71. tifnode = class(tloopnode)
  72. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  73. function pass_typecheck:tnode;override;
  74. function pass_1 : tnode;override;
  75. function simplify(forinline : boolean) : tnode;override;
  76. private
  77. function internalsimplify(warn: boolean) : tnode;
  78. end;
  79. tifnodeclass = class of tifnode;
  80. tfornode = class(tloopnode)
  81. { if count isn divisable by unrolls then
  82. the for loop must jump to this label to get the correct
  83. number of executions }
  84. entrylabel : tnode;
  85. loopvar_notid:cardinal;
  86. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;reintroduce;
  87. procedure loop_var_access(not_type:Tnotification_flag;symbol:Tsym);
  88. function pass_typecheck:tnode;override;
  89. function pass_1 : tnode;override;
  90. function simplify(forinline : boolean) : tnode;override;
  91. end;
  92. tfornodeclass = class of tfornode;
  93. texitnode = class(tunarynode)
  94. constructor create(l:tnode);virtual;
  95. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  96. procedure ppuwrite(ppufile:tcompilerppufile);override;
  97. function pass_typecheck:tnode;override;
  98. function pass_1 : tnode;override;
  99. end;
  100. texitnodeclass = class of texitnode;
  101. tbreaknode = class(tnode)
  102. constructor create;virtual;
  103. function pass_typecheck:tnode;override;
  104. function pass_1 : tnode;override;
  105. end;
  106. tbreaknodeclass = class of tbreaknode;
  107. tcontinuenode = class(tnode)
  108. constructor create;virtual;
  109. function pass_typecheck:tnode;override;
  110. function pass_1 : tnode;override;
  111. end;
  112. tcontinuenodeclass = class of tcontinuenode;
  113. tgotonode = class(tnode)
  114. private
  115. labelnodeidx : longint;
  116. public
  117. labelsym : tlabelsym;
  118. labelnode : tlabelnode;
  119. exceptionblock : integer;
  120. constructor create(p : tlabelsym);virtual;
  121. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  122. procedure ppuwrite(ppufile:tcompilerppufile);override;
  123. procedure buildderefimpl;override;
  124. procedure derefimpl;override;
  125. procedure resolveppuidx;override;
  126. function dogetcopy : tnode;override;
  127. function pass_typecheck:tnode;override;
  128. function pass_1 : tnode;override;
  129. function docompare(p: tnode): boolean; override;
  130. end;
  131. tgotonodeclass = class of tgotonode;
  132. tlabelnode = class(tunarynode)
  133. exceptionblock : integer;
  134. { when copying trees, this points to the newly created copy of a label }
  135. copiedto : tlabelnode;
  136. labsym : tlabelsym;
  137. constructor create(l:tnode;alabsym:tlabelsym);virtual;
  138. destructor destroy;override;
  139. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  140. procedure ppuwrite(ppufile:tcompilerppufile);override;
  141. procedure buildderefimpl;override;
  142. procedure derefimpl;override;
  143. function dogetcopy : tnode;override;
  144. function pass_typecheck:tnode;override;
  145. function pass_1 : tnode;override;
  146. function docompare(p: tnode): boolean; override;
  147. end;
  148. tlabelnodeclass = class of tlabelnode;
  149. traisenode = class(ttertiarynode)
  150. constructor create(l,taddr,tframe:tnode);virtual;
  151. function pass_typecheck:tnode;override;
  152. function pass_1 : tnode;override;
  153. end;
  154. traisenodeclass = class of traisenode;
  155. ttryexceptnode = class(tloopnode)
  156. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  157. function pass_typecheck:tnode;override;
  158. function pass_1 : tnode;override;
  159. end;
  160. ttryexceptnodeclass = class of ttryexceptnode;
  161. ttryfinallynode = class(tloopnode)
  162. implicitframe : boolean;
  163. constructor create(l,r:tnode);virtual;reintroduce;
  164. constructor create_implicit(l,r,_t1:tnode);virtual;
  165. function pass_typecheck:tnode;override;
  166. function pass_1 : tnode;override;
  167. function simplify(forinline:boolean): tnode;override;
  168. end;
  169. ttryfinallynodeclass = class of ttryfinallynode;
  170. tonnode = class(tbinarynode)
  171. excepTSymtable : TSymtable;
  172. excepttype : tobjectdef;
  173. constructor create(l,r:tnode);virtual;
  174. destructor destroy;override;
  175. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  176. function pass_typecheck:tnode;override;
  177. function pass_1 : tnode;override;
  178. function dogetcopy : tnode;override;
  179. function docompare(p: tnode): boolean; override;
  180. end;
  181. tonnodeclass = class of tonnode;
  182. var
  183. cwhilerepeatnode : twhilerepeatnodeclass=twhilerepeatnode;
  184. cifnode : tifnodeclass = tifnode;
  185. cfornode : tfornodeclass = tfornode;
  186. cexitnode : texitnodeclass = texitnode;
  187. cgotonode : tgotonodeclass = tgotonode;
  188. clabelnode : tlabelnodeclass = tlabelnode;
  189. craisenode : traisenodeclass = traisenode;
  190. ctryexceptnode : ttryexceptnodeclass = ttryexceptnode;
  191. ctryfinallynode : ttryfinallynodeclass = ttryfinallynode;
  192. connode : tonnodeclass = tonnode;
  193. cbreaknode : tbreaknodeclass = tbreaknode;
  194. ccontinuenode : tcontinuenodeclass = tcontinuenode;
  195. // for-in loop helpers
  196. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  197. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  198. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  199. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  200. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  201. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  202. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  203. implementation
  204. uses
  205. globtype,systems,constexp,
  206. cutils,verbose,globals,
  207. symconst,symtable,paramgr,defcmp,defutil,htypechk,pass_1,
  208. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,nutils,ninl,nset,
  209. {$ifdef state_tracking}
  210. nstate,
  211. {$endif}
  212. cgbase,procinfo
  213. ;
  214. // for-in loop helpers
  215. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  216. begin
  217. result:=cfornode.create(hloopvar,
  218. cinlinenode.create(in_low_x,false,expr.getcopy),
  219. cinlinenode.create(in_high_x,false,expr.getcopy),
  220. hloopbody,
  221. false);
  222. end;
  223. function create_objc_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  224. var
  225. mainstatement, outerloopbodystatement, innerloopbodystatement, tempstatement: tstatementnode;
  226. state, mutationcheck, currentamount, innerloopcounter, items, expressiontemp: ttempcreatenode;
  227. outerloop, innerloop, hp: tnode;
  228. itemsarraydef: tarraydef;
  229. sym: tsym;
  230. begin
  231. { Objective-C enumerators require Objective-C 2.0 }
  232. if not(m_objectivec2 in current_settings.modeswitches) then
  233. begin
  234. result:=cerrornode.create;
  235. MessagePos(expr.fileinfo,parser_e_objc_enumerator_2_0);
  236. exit;
  237. end;
  238. { Requires the NSFastEnumeration protocol and NSFastEnumerationState
  239. record }
  240. maybeloadcocoatypes;
  241. if not assigned(objc_fastenumeration) or
  242. not assigned(objc_fastenumerationstate) then
  243. begin
  244. result:=cerrornode.create;
  245. MessagePos(expr.fileinfo,parser_e_objc_missing_enumeration_defs);
  246. exit;
  247. end;
  248. (* Original code:
  249. for hloopvar in expression do
  250. <hloopbody>
  251. Pascal code equivalent into which it has to be transformed
  252. (sure would be nice if the compiler had some kind of templates ;) :
  253. var
  254. state: NSFastEnumerationState;
  255. expressiontemp: NSFastEnumerationProtocol;
  256. mutationcheck,
  257. currentamount,
  258. innerloopcounter: culong;
  259. { size can be increased/decreased if desired }
  260. items: array[1..16] of id;
  261. begin
  262. fillchar(state,sizeof(state),0);
  263. expressiontemp:=expression;
  264. repeat
  265. currentamount:=expressiontemp.countByEnumeratingWithState_objects_count(@state,@items,length(items));
  266. if currentamount=0 then
  267. begin
  268. { "The iterating variable is set to nil when the loop ends by
  269. exhausting the source pool of objects" }
  270. hloopvar:=nil;
  271. break;
  272. end;
  273. mutationcheck:=state.mutationsptr^;
  274. innerloopcounter:=culong(-1);
  275. repeat
  276. { at the start so that "continue" in <loopbody> works correctly }
  277. { don't use for-loop, because then the value of the iteration
  278. counter is undefined on exit and we have to check it in the
  279. outer repeat/until condition }
  280. {$push}
  281. {$r-,q-}
  282. inc(innerloopcounter);
  283. {$pop}
  284. if innerloopcounter=currentamount then
  285. break;
  286. if mutationcheck<>state.mutationsptr^ then
  287. { raises Objective-C exception... }
  288. objc_enumerationMutation(expressiontemp);
  289. hloopvar:=state.itemsPtr[innerloopcounter];
  290. { if continue in loopbody -> jumps to start, increases count and checks }
  291. { if break in loopbody: goes to outer repeat/until and innerloopcount
  292. will be < currentamount -> stops }
  293. <hloopbody>
  294. until false;
  295. { if the inner loop terminated early, "break" was used and we have
  296. to stop }
  297. { "If the loop is terminated early, the iterating variable is left
  298. pointing to the last iteration item." }
  299. until innerloopcounter<currentamount;
  300. end;
  301. *)
  302. result:=internalstatements(mainstatement);
  303. { the fast enumeration state }
  304. state:=ctempcreatenode.create(objc_fastenumerationstate,objc_fastenumerationstate.size,tt_persistent,false);
  305. typecheckpass(tnode(state));
  306. addstatement(mainstatement,state);
  307. { the temporary items array }
  308. itemsarraydef:=tarraydef.create(1,16,u32inttype);
  309. itemsarraydef.elementdef:=objc_idtype;
  310. items:=ctempcreatenode.create(itemsarraydef,itemsarraydef.size,tt_persistent,false);
  311. addstatement(mainstatement,items);
  312. typecheckpass(tnode(items));
  313. { temp for the expression/collection through which we iterate }
  314. expressiontemp:=ctempcreatenode.create(objc_fastenumeration,objc_fastenumeration.size,tt_persistent,true);
  315. addstatement(mainstatement,expressiontemp);
  316. { currentamount temp (not really clean: we use ptruint instead of
  317. culong) }
  318. currentamount:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  319. typecheckpass(tnode(currentamount));
  320. addstatement(mainstatement,currentamount);
  321. { mutationcheck temp (idem) }
  322. mutationcheck:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  323. typecheckpass(tnode(mutationcheck));
  324. addstatement(mainstatement,mutationcheck);
  325. { innerloopcounter temp (idem) }
  326. innerloopcounter:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  327. typecheckpass(tnode(innerloopcounter));
  328. addstatement(mainstatement,innerloopcounter);
  329. { initialise the state with 0 }
  330. addstatement(mainstatement,ccallnode.createinternfromunit('SYSTEM','FILLCHAR',
  331. ccallparanode.create(genintconstnode(0),
  332. ccallparanode.create(genintconstnode(objc_fastenumerationstate.size),
  333. ccallparanode.create(ctemprefnode.create(state),nil)
  334. )
  335. )
  336. ));
  337. { this will also check whether the expression (potentially) conforms
  338. to the NSFastEnumeration protocol (use expr.getcopy, because the
  339. caller will free expr) }
  340. addstatement(mainstatement,cassignmentnode.create(ctemprefnode.create(expressiontemp),expr.getcopy));
  341. { we add the "repeat..until" afterwards, now just create the body }
  342. outerloop:=internalstatements(outerloopbodystatement);
  343. { the countByEnumeratingWithState_objects_count call }
  344. hp:=ccallparanode.create(cinlinenode.create(in_length_x,false,ctypenode.create(itemsarraydef)),
  345. ccallparanode.create(caddrnode.create(ctemprefnode.create(items)),
  346. ccallparanode.create(caddrnode.create(ctemprefnode.create(state)),nil)
  347. )
  348. );
  349. sym:=search_struct_member(objc_fastenumeration,'COUNTBYENUMERATINGWITHSTATE_OBJECTS_COUNT');
  350. if not assigned(sym) or
  351. (sym.typ<>procsym) then
  352. internalerror(2010061901);
  353. hp:=ccallnode.create(hp,tprocsym(sym),sym.owner,ctemprefnode.create(expressiontemp),[]);
  354. addstatement(outerloopbodystatement,cassignmentnode.create(
  355. ctemprefnode.create(currentamount),hp));
  356. { if currentamount = 0, bail out (use copy of hloopvar, because we
  357. have to use it again below) }
  358. hp:=internalstatements(tempstatement);
  359. addstatement(tempstatement,cassignmentnode.create(
  360. hloopvar.getcopy,cnilnode.create));
  361. addstatement(tempstatement,cbreaknode.create);
  362. addstatement(outerloopbodystatement,cifnode.create(
  363. caddnode.create(equaln,ctemprefnode.create(currentamount),genintconstnode(0)),
  364. hp,nil));
  365. { initial value of mutationcheck }
  366. hp:=ctemprefnode.create(state);
  367. typecheckpass(hp);
  368. hp:=cderefnode.create(genloadfield(hp,'MUTATIONSPTR'));
  369. addstatement(outerloopbodystatement,cassignmentnode.create(
  370. ctemprefnode.create(mutationcheck),hp));
  371. { initialise innerloopcounter }
  372. addstatement(outerloopbodystatement,cassignmentnode.create(
  373. ctemprefnode.create(innerloopcounter),cordconstnode.create(-1,ptruinttype,false)));
  374. { and now the inner loop, again adding the repeat/until afterwards }
  375. innerloop:=internalstatements(innerloopbodystatement);
  376. { inc(innerloopcounter) without range/overflowchecking (because
  377. we go from culong(-1) to 0 during the first iteration }
  378. hp:=cinlinenode.create(
  379. in_inc_x,false,ccallparanode.create(
  380. ctemprefnode.create(innerloopcounter),nil));
  381. hp.localswitches:=hp.localswitches-[cs_check_range,cs_check_overflow];
  382. addstatement(innerloopbodystatement,hp);
  383. { if innerloopcounter=currentamount then break to the outer loop }
  384. addstatement(innerloopbodystatement,cifnode.create(
  385. caddnode.create(equaln,
  386. ctemprefnode.create(innerloopcounter),
  387. ctemprefnode.create(currentamount)),
  388. cbreaknode.create,
  389. nil));
  390. { verify that the collection didn't change in the mean time }
  391. hp:=ctemprefnode.create(state);
  392. typecheckpass(hp);
  393. addstatement(innerloopbodystatement,cifnode.create(
  394. caddnode.create(unequaln,
  395. ctemprefnode.create(mutationcheck),
  396. cderefnode.create(genloadfield(hp,'MUTATIONSPTR'))
  397. ),
  398. ccallnode.createinternfromunit('OBJC','OBJC_ENUMERATIONMUTATION',
  399. ccallparanode.create(ctemprefnode.create(expressiontemp),nil)),
  400. nil));
  401. { finally: actually get the next element }
  402. hp:=ctemprefnode.create(state);
  403. typecheckpass(hp);
  404. hp:=genloadfield(hp,'ITEMSPTR');
  405. typecheckpass(hp);
  406. { don't simply use a vecn, because indexing a pointer won't work in
  407. non-FPC modes }
  408. if hp.resultdef.typ<>pointerdef then
  409. internalerror(2010061904);
  410. inserttypeconv(hp,
  411. tarraydef.create_from_pointer(tpointerdef(hp.resultdef).pointeddef));
  412. hp:=cvecnode.create(hp,ctemprefnode.create(innerloopcounter));
  413. addstatement(innerloopbodystatement,
  414. cassignmentnode.create(hloopvar,hp));
  415. { the actual loop body! }
  416. addstatement(innerloopbodystatement,hloopbody);
  417. { create the inner repeat/until and add it to the body of the outer
  418. one }
  419. hp:=cwhilerepeatnode.create(
  420. { repeat .. until false }
  421. cordconstnode.create(0,pasbool8type,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. helperdef: tobjectdef;
  733. current: tpropertysym;
  734. storefilepos: tfileposinfo;
  735. begin
  736. storefilepos:=current_filepos;
  737. current_filepos:=hloopvar.fileinfo;
  738. if expr.nodetype=typen then
  739. begin
  740. if (expr.resultdef.typ=enumdef) and tenumdef(expr.resultdef).has_jumps then
  741. begin
  742. result:=cerrornode.create;
  743. hloopvar.free;
  744. hloopbody.free;
  745. MessagePos1(expr.fileinfo,parser_e_for_in_loop_cannot_be_used_for_the_type,expr.resultdef.typename);
  746. end
  747. else
  748. result:=create_type_for_in_loop(hloopvar, hloopbody, expr);
  749. end
  750. else
  751. begin
  752. { loop is made for an expression }
  753. // Objective-C uses different conventions (and it's only supported for Objective-C 2.0)
  754. if is_objc_class_or_protocol(hloopvar.resultdef) or
  755. is_objc_class_or_protocol(expr.resultdef) then
  756. begin
  757. result:=create_objc_for_in_loop(hloopvar,hloopbody,expr);
  758. if result.nodetype=errorn then
  759. begin
  760. hloopvar.free;
  761. hloopbody.free;
  762. end;
  763. end
  764. else
  765. begin
  766. // search for operator first
  767. pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
  768. // if there is no operator then search for class/object enumerator method
  769. if (pd=nil) and (expr.resultdef.typ in [objectdef,recorddef]) then
  770. begin
  771. { first search using the helper hierarchy }
  772. if search_last_objectpascal_helper(tabstractrecorddef(expr.resultdef),nil,helperdef) then
  773. repeat
  774. pd:=helperdef.search_enumerator_get;
  775. helperdef:=helperdef.childof;
  776. until (pd<>nil) or (helperdef=nil);
  777. { we didn't find an enumerator in a helper, so search in the
  778. class/record/object itself }
  779. if pd=nil then
  780. pd:=tabstractrecorddef(expr.resultdef).search_enumerator_get;
  781. end;
  782. if pd<>nil then
  783. begin
  784. // seach movenext and current symbols
  785. movenext:=tabstractrecorddef(pd.returndef).search_enumerator_move;
  786. if movenext = nil then
  787. begin
  788. result:=cerrornode.create;
  789. hloopvar.free;
  790. hloopbody.free;
  791. MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
  792. end
  793. else
  794. begin
  795. current:=tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  796. if current = nil then
  797. begin
  798. result:=cerrornode.create;
  799. hloopvar.free;
  800. hloopbody.free;
  801. MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
  802. end
  803. else
  804. result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
  805. end;
  806. end
  807. else
  808. begin
  809. case expr.resultdef.typ of
  810. stringdef: result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  811. arraydef: result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  812. setdef: result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  813. else
  814. begin
  815. result:=cerrornode.create;
  816. hloopvar.free;
  817. hloopbody.free;
  818. MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
  819. end;
  820. end;
  821. end;
  822. end;
  823. end;
  824. current_filepos:=storefilepos;
  825. end;
  826. {****************************************************************************
  827. TLOOPNODE
  828. *****************************************************************************}
  829. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  830. begin
  831. inherited create(tt,l,r);
  832. t1:=_t1;
  833. t2:=_t2;
  834. fileinfo:=l.fileinfo;
  835. end;
  836. destructor tloopnode.destroy;
  837. begin
  838. t1.free;
  839. t2.free;
  840. inherited destroy;
  841. end;
  842. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  843. begin
  844. inherited ppuload(t,ppufile);
  845. t1:=ppuloadnode(ppufile);
  846. t2:=ppuloadnode(ppufile);
  847. ppufile.getsmallset(loopflags);
  848. end;
  849. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  850. begin
  851. inherited ppuwrite(ppufile);
  852. ppuwritenode(ppufile,t1);
  853. ppuwritenode(ppufile,t2);
  854. ppufile.putsmallset(loopflags);
  855. end;
  856. procedure tloopnode.buildderefimpl;
  857. begin
  858. inherited buildderefimpl;
  859. if assigned(t1) then
  860. t1.buildderefimpl;
  861. if assigned(t2) then
  862. t2.buildderefimpl;
  863. end;
  864. procedure tloopnode.derefimpl;
  865. begin
  866. inherited derefimpl;
  867. if assigned(t1) then
  868. t1.derefimpl;
  869. if assigned(t2) then
  870. t2.derefimpl;
  871. end;
  872. function tloopnode.dogetcopy : tnode;
  873. var
  874. p : tloopnode;
  875. begin
  876. p:=tloopnode(inherited dogetcopy);
  877. if assigned(t1) then
  878. p.t1:=t1.dogetcopy
  879. else
  880. p.t1:=nil;
  881. if assigned(t2) then
  882. p.t2:=t2.dogetcopy
  883. else
  884. p.t2:=nil;
  885. p.loopflags:=loopflags;
  886. dogetcopy:=p;
  887. end;
  888. procedure tloopnode.insertintolist(l : tnodelist);
  889. begin
  890. end;
  891. procedure tloopnode.printnodetree(var t:text);
  892. begin
  893. write(t,printnodeindention,'(');
  894. printnodeindent;
  895. printnodeinfo(t);
  896. writeln(t);
  897. printnode(t,left);
  898. printnode(t,right);
  899. printnode(t,t1);
  900. printnode(t,t2);
  901. printnodeunindent;
  902. writeln(t,printnodeindention,')');
  903. end;
  904. function tloopnode.docompare(p: tnode): boolean;
  905. begin
  906. docompare :=
  907. inherited docompare(p) and
  908. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  909. t1.isequal(tloopnode(p).t1) and
  910. t2.isequal(tloopnode(p).t2);
  911. end;
  912. {****************************************************************************
  913. TWHILEREPEATNODE
  914. *****************************************************************************}
  915. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  916. begin
  917. inherited create(whilerepeatn,l,r,nil,nil);
  918. if tab then
  919. include(loopflags, lnf_testatbegin);
  920. if cn then
  921. include(loopflags,lnf_checknegate);
  922. end;
  923. function twhilerepeatnode.pass_typecheck:tnode;
  924. var
  925. t:Tunarynode;
  926. begin
  927. result:=nil;
  928. resultdef:=voidtype;
  929. typecheckpass(left);
  930. { tp procvar support }
  931. maybe_call_procvar(left,true);
  932. {A not node can be removed.}
  933. if left.nodetype=notn then
  934. begin
  935. t:=Tunarynode(left);
  936. left:=Tunarynode(left).left;
  937. t.left:=nil;
  938. t.destroy;
  939. {Symdif operator, in case you are wondering:}
  940. loopflags:=loopflags >< [lnf_checknegate];
  941. end;
  942. { loop instruction }
  943. if assigned(right) then
  944. typecheckpass(right);
  945. set_varstate(left,vs_read,[vsf_must_be_valid]);
  946. if codegenerror then
  947. exit;
  948. if not(is_boolean(left.resultdef)) and
  949. not(is_typeparam(left.resultdef)) then
  950. begin
  951. if left.resultdef.typ=variantdef then
  952. inserttypeconv(left,pasbool8type)
  953. else
  954. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  955. end;
  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=1 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. begin
  1232. if left.resultdef.typ=variantdef then
  1233. inserttypeconv(left,pasbool8type)
  1234. else
  1235. Message1(type_e_boolean_expr_expected,left.resultdef.typename);
  1236. end;
  1237. result:=internalsimplify(true);
  1238. end;
  1239. function tifnode.pass_1 : tnode;
  1240. begin
  1241. result:=nil;
  1242. expectloc:=LOC_VOID;
  1243. firstpass(left);
  1244. { if path }
  1245. if assigned(right) then
  1246. firstpass(right);
  1247. { else path }
  1248. if assigned(t1) then
  1249. firstpass(t1);
  1250. { leave if we've got an error in one of the paths }
  1251. if codegenerror then
  1252. exit;
  1253. end;
  1254. {*****************************************************************************
  1255. TFORNODE
  1256. *****************************************************************************}
  1257. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1258. begin
  1259. inherited create(forn,l,r,_t1,_t2);
  1260. if back then
  1261. include(loopflags,lnf_backward);
  1262. include(loopflags,lnf_testatbegin);
  1263. end;
  1264. procedure Tfornode.loop_var_access(not_type:Tnotification_flag;
  1265. symbol:Tsym);
  1266. begin
  1267. {If there is a read access, the value of the loop counter is important;
  1268. at the end of the loop the loop variable should contain the value it
  1269. had in the last iteration.}
  1270. if not_type=vn_onwrite then
  1271. begin
  1272. writeln('Loopvar does not matter on exit');
  1273. end
  1274. else
  1275. begin
  1276. exclude(loopflags,lnf_dont_mind_loopvar_on_exit);
  1277. writeln('Loopvar does matter on exit');
  1278. end;
  1279. Tabstractvarsym(symbol).unregister_notification(loopvar_notid);
  1280. end;
  1281. function tfornode.simplify(forinline : boolean) : tnode;
  1282. begin
  1283. result:=nil;
  1284. if (t1.nodetype=ordconstn) and
  1285. (right.nodetype=ordconstn) and
  1286. (
  1287. (
  1288. (lnf_backward in loopflags) and
  1289. (tordconstnode(right).value<tordconstnode(t1).value)
  1290. ) or
  1291. (
  1292. not(lnf_backward in loopflags) and
  1293. (tordconstnode(right).value>tordconstnode(t1).value)
  1294. )
  1295. ) then
  1296. result:=cnothingnode.create;
  1297. end;
  1298. function tfornode.pass_typecheck:tnode;
  1299. var
  1300. res : tnode;
  1301. begin
  1302. result:=nil;
  1303. resultdef:=voidtype;
  1304. { process the loopvar, from and to, varstates are already set }
  1305. typecheckpass(left);
  1306. typecheckpass(right);
  1307. typecheckpass(t1);
  1308. set_varstate(left,vs_written,[]);
  1309. { loop unrolling }
  1310. if cs_opt_loopunroll in current_settings.optimizerswitches then
  1311. begin
  1312. res:=unroll_loop(self);
  1313. if assigned(res) then
  1314. begin
  1315. typecheckpass(res);
  1316. result:=res;
  1317. exit;
  1318. end;
  1319. end;
  1320. { Can we spare the first comparision? }
  1321. if (t1.nodetype=ordconstn) and
  1322. (right.nodetype=ordconstn) and
  1323. (
  1324. (
  1325. (lnf_backward in loopflags) and
  1326. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1327. ) or
  1328. (
  1329. not(lnf_backward in loopflags) and
  1330. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1331. )
  1332. ) then
  1333. exclude(loopflags,lnf_testatbegin);
  1334. { Make sure that the loop var and the
  1335. from and to values are compatible types }
  1336. check_ranges(right.fileinfo,right,left.resultdef);
  1337. inserttypeconv(right,left.resultdef);
  1338. check_ranges(t1.fileinfo,t1,left.resultdef);
  1339. inserttypeconv(t1,left.resultdef);
  1340. if assigned(t2) then
  1341. typecheckpass(t2);
  1342. end;
  1343. function tfornode.pass_1 : tnode;
  1344. begin
  1345. result:=nil;
  1346. expectloc:=LOC_VOID;
  1347. firstpass(left);
  1348. firstpass(right);
  1349. firstpass(t1);
  1350. if assigned(t2) then
  1351. begin
  1352. firstpass(t2);
  1353. if codegenerror then
  1354. exit;
  1355. end;
  1356. end;
  1357. {*****************************************************************************
  1358. TEXITNODE
  1359. *****************************************************************************}
  1360. constructor texitnode.create(l:tnode);
  1361. begin
  1362. inherited create(exitn,l);
  1363. if assigned(left) then
  1364. begin
  1365. { add assignment to funcretsym }
  1366. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1367. left:=cassignmentnode.create(
  1368. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1369. left);
  1370. end;
  1371. end;
  1372. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1373. begin
  1374. inherited ppuload(t,ppufile);
  1375. end;
  1376. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1377. begin
  1378. inherited ppuwrite(ppufile);
  1379. end;
  1380. function texitnode.pass_typecheck:tnode;
  1381. var
  1382. newstatement : tstatementnode;
  1383. begin
  1384. result:=nil;
  1385. if assigned(left) then
  1386. begin
  1387. result:=internalstatements(newstatement);
  1388. addstatement(newstatement,left);
  1389. left:=nil;
  1390. addstatement(newstatement,self.getcopy);
  1391. end;
  1392. resultdef:=voidtype;
  1393. end;
  1394. function texitnode.pass_1 : tnode;
  1395. begin
  1396. result:=nil;
  1397. expectloc:=LOC_VOID;
  1398. if assigned(left) then
  1399. internalerror(2011052801);
  1400. end;
  1401. {*****************************************************************************
  1402. TBREAKNODE
  1403. *****************************************************************************}
  1404. constructor tbreaknode.create;
  1405. begin
  1406. inherited create(breakn);
  1407. end;
  1408. function tbreaknode.pass_typecheck:tnode;
  1409. begin
  1410. result:=nil;
  1411. resultdef:=voidtype;
  1412. end;
  1413. function tbreaknode.pass_1 : tnode;
  1414. begin
  1415. result:=nil;
  1416. expectloc:=LOC_VOID;
  1417. end;
  1418. {*****************************************************************************
  1419. TCONTINUENODE
  1420. *****************************************************************************}
  1421. constructor tcontinuenode.create;
  1422. begin
  1423. inherited create(continuen);
  1424. end;
  1425. function tcontinuenode.pass_typecheck:tnode;
  1426. begin
  1427. result:=nil;
  1428. resultdef:=voidtype;
  1429. end;
  1430. function tcontinuenode.pass_1 : tnode;
  1431. begin
  1432. result:=nil;
  1433. expectloc:=LOC_VOID;
  1434. end;
  1435. {*****************************************************************************
  1436. TGOTONODE
  1437. *****************************************************************************}
  1438. constructor tgotonode.create(p : tlabelsym);
  1439. begin
  1440. inherited create(goton);
  1441. exceptionblock:=current_exceptblock;
  1442. labelnode:=nil;
  1443. labelsym:=p;
  1444. end;
  1445. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1446. begin
  1447. inherited ppuload(t,ppufile);
  1448. labelnodeidx:=ppufile.getlongint;
  1449. exceptionblock:=ppufile.getbyte;
  1450. end;
  1451. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  1452. begin
  1453. inherited ppuwrite(ppufile);
  1454. labelnodeidx:=labelnode.ppuidx;
  1455. ppufile.putlongint(labelnodeidx);
  1456. ppufile.putbyte(exceptionblock);
  1457. end;
  1458. procedure tgotonode.buildderefimpl;
  1459. begin
  1460. inherited buildderefimpl;
  1461. end;
  1462. procedure tgotonode.derefimpl;
  1463. begin
  1464. inherited derefimpl;
  1465. end;
  1466. procedure tgotonode.resolveppuidx;
  1467. begin
  1468. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  1469. if labelnode.nodetype<>labeln then
  1470. internalerror(200809021);
  1471. end;
  1472. function tgotonode.pass_typecheck:tnode;
  1473. begin
  1474. result:=nil;
  1475. resultdef:=voidtype;
  1476. end;
  1477. function tgotonode.pass_1 : tnode;
  1478. var
  1479. p2 : tprocinfo;
  1480. begin
  1481. result:=nil;
  1482. expectloc:=LOC_VOID;
  1483. { The labelnode can already be set when
  1484. this node was copied }
  1485. if not(assigned(labelnode)) then
  1486. begin
  1487. { inner procedure goto? }
  1488. if assigned(labelsym.code) and
  1489. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1490. { generated by the optimizer? }
  1491. not(assigned(labelsym.owner))) then
  1492. labelnode:=tlabelnode(labelsym.code)
  1493. else if (m_non_local_goto in current_settings.modeswitches) and
  1494. assigned(labelsym.owner) 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. result:=ccallnode.createintern('fpc_longjmp',
  1519. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  1520. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  1521. nil)));
  1522. end
  1523. else
  1524. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1525. end
  1526. else
  1527. CGMessage(cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  1528. end
  1529. else
  1530. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1531. end;
  1532. { check if we don't mess with exception blocks }
  1533. if assigned(labelnode) and
  1534. (exceptionblock<>labelnode.exceptionblock) then
  1535. CGMessage(cg_e_goto_inout_of_exception_block);
  1536. end;
  1537. function tgotonode.dogetcopy : tnode;
  1538. var
  1539. p : tgotonode;
  1540. begin
  1541. p:=tgotonode(inherited dogetcopy);
  1542. p.exceptionblock:=exceptionblock;
  1543. { generate labelnode if not done yet }
  1544. if not(assigned(labelnode)) then
  1545. begin
  1546. if assigned(labelsym) and assigned(labelsym.code) then
  1547. labelnode:=tlabelnode(labelsym.code)
  1548. end;
  1549. p.labelsym:=labelsym;
  1550. if assigned(labelnode) then
  1551. p.labelnode:=tlabelnode(labelnode.dogetcopy)
  1552. else
  1553. begin
  1554. { don't trigger IE when there was already an error, i.e. the
  1555. label is not defined. See tw11763 (PFV) }
  1556. if errorcount=0 then
  1557. internalerror(200610291);
  1558. end;
  1559. result:=p;
  1560. end;
  1561. function tgotonode.docompare(p: tnode): boolean;
  1562. begin
  1563. docompare := false;
  1564. end;
  1565. {*****************************************************************************
  1566. TLABELNODE
  1567. *****************************************************************************}
  1568. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  1569. begin
  1570. inherited create(labeln,l);
  1571. exceptionblock:=current_exceptblock;
  1572. labsym:=alabsym;
  1573. { Register labelnode in labelsym }
  1574. labsym.code:=self;
  1575. end;
  1576. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1577. begin
  1578. inherited ppuload(t,ppufile);
  1579. exceptionblock:=ppufile.getbyte;
  1580. end;
  1581. destructor tlabelnode.destroy;
  1582. begin
  1583. { Remove reference in labelsym, this is to prevent
  1584. goto's to this label }
  1585. if assigned(labsym) and (labsym.code=pointer(self)) then
  1586. labsym.code:=nil;
  1587. inherited destroy;
  1588. end;
  1589. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  1590. begin
  1591. inherited ppuwrite(ppufile);
  1592. ppufile.putbyte(exceptionblock);
  1593. end;
  1594. procedure tlabelnode.buildderefimpl;
  1595. begin
  1596. inherited buildderefimpl;
  1597. end;
  1598. procedure tlabelnode.derefimpl;
  1599. begin
  1600. inherited derefimpl;
  1601. end;
  1602. function tlabelnode.pass_typecheck:tnode;
  1603. begin
  1604. result:=nil;
  1605. { left could still be unassigned }
  1606. if assigned(left) then
  1607. typecheckpass(left);
  1608. resultdef:=voidtype;
  1609. end;
  1610. function tlabelnode.pass_1 : tnode;
  1611. begin
  1612. result:=nil;
  1613. expectloc:=LOC_VOID;
  1614. include(current_procinfo.flags,pi_has_label);
  1615. if assigned(labsym) and labsym.nonlocal then
  1616. include(current_procinfo.flags,pi_has_interproclabel);
  1617. if assigned(left) then
  1618. firstpass(left);
  1619. if (m_non_local_goto in current_settings.modeswitches) and
  1620. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  1621. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  1622. end;
  1623. function tlabelnode.dogetcopy : tnode;
  1624. begin
  1625. if not(assigned(copiedto)) then
  1626. copiedto:=tlabelnode(inherited dogetcopy);
  1627. copiedto.exceptionblock:=exceptionblock;
  1628. result:=copiedto;
  1629. end;
  1630. function tlabelnode.docompare(p: tnode): boolean;
  1631. begin
  1632. docompare := false;
  1633. end;
  1634. {*****************************************************************************
  1635. TRAISENODE
  1636. *****************************************************************************}
  1637. constructor traisenode.create(l,taddr,tframe:tnode);
  1638. begin
  1639. inherited create(raisen,l,taddr,tframe);
  1640. end;
  1641. function traisenode.pass_typecheck:tnode;
  1642. begin
  1643. result:=nil;
  1644. resultdef:=voidtype;
  1645. if assigned(left) then
  1646. begin
  1647. { first para must be a _class_ }
  1648. typecheckpass(left);
  1649. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1650. if codegenerror then
  1651. exit;
  1652. if not(is_class(left.resultdef)) then
  1653. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  1654. { insert needed typeconvs for addr,frame }
  1655. if assigned(right) then
  1656. begin
  1657. { addr }
  1658. typecheckpass(right);
  1659. inserttypeconv(right,voidpointertype);
  1660. { frame }
  1661. if assigned(third) then
  1662. begin
  1663. typecheckpass(third);
  1664. inserttypeconv(third,voidpointertype);
  1665. end;
  1666. end;
  1667. end;
  1668. end;
  1669. function traisenode.pass_1 : tnode;
  1670. begin
  1671. result:=nil;
  1672. include(current_procinfo.flags,pi_do_call);
  1673. expectloc:=LOC_VOID;
  1674. if assigned(left) then
  1675. begin
  1676. { first para must be a _class_ }
  1677. firstpass(left);
  1678. { insert needed typeconvs for addr,frame }
  1679. if assigned(right) then
  1680. begin
  1681. { addr }
  1682. firstpass(right);
  1683. { frame }
  1684. if assigned(third) then
  1685. firstpass(third);
  1686. end;
  1687. end;
  1688. end;
  1689. {*****************************************************************************
  1690. TTRYEXCEPTNODE
  1691. *****************************************************************************}
  1692. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1693. begin
  1694. inherited create(tryexceptn,l,r,_t1,nil);
  1695. end;
  1696. function ttryexceptnode.pass_typecheck:tnode;
  1697. begin
  1698. result:=nil;
  1699. typecheckpass(left);
  1700. { on statements }
  1701. if assigned(right) then
  1702. typecheckpass(right);
  1703. { else block }
  1704. if assigned(t1) then
  1705. typecheckpass(t1);
  1706. resultdef:=voidtype;
  1707. end;
  1708. function ttryexceptnode.pass_1 : tnode;
  1709. begin
  1710. result:=nil;
  1711. include(current_procinfo.flags,pi_do_call);
  1712. expectloc:=LOC_VOID;
  1713. firstpass(left);
  1714. { on statements }
  1715. if assigned(right) then
  1716. firstpass(right);
  1717. { else block }
  1718. if assigned(t1) then
  1719. firstpass(t1);
  1720. end;
  1721. {*****************************************************************************
  1722. TTRYFINALLYNODE
  1723. *****************************************************************************}
  1724. constructor ttryfinallynode.create(l,r:tnode);
  1725. begin
  1726. inherited create(tryfinallyn,l,r,nil,nil);
  1727. implicitframe:=false;
  1728. end;
  1729. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1730. begin
  1731. inherited create(tryfinallyn,l,r,_t1,nil);
  1732. implicitframe:=true;
  1733. end;
  1734. function ttryfinallynode.pass_typecheck:tnode;
  1735. begin
  1736. result:=nil;
  1737. include(current_procinfo.flags,pi_do_call);
  1738. resultdef:=voidtype;
  1739. typecheckpass(left);
  1740. // "try block" is "used"? (JM)
  1741. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  1742. typecheckpass(right);
  1743. // "except block" is "used"? (JM)
  1744. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  1745. { special finally block only executed when there was an exception }
  1746. if assigned(t1) then
  1747. begin
  1748. typecheckpass(t1);
  1749. // "finally block" is "used"? (JM)
  1750. set_varstate(t1,vs_readwritten,[vsf_must_be_valid]);
  1751. end;
  1752. end;
  1753. function ttryfinallynode.pass_1 : tnode;
  1754. begin
  1755. result:=nil;
  1756. expectloc:=LOC_VOID;
  1757. firstpass(left);
  1758. firstpass(right);
  1759. if assigned(t1) then
  1760. firstpass(t1);
  1761. end;
  1762. function ttryfinallynode.simplify(forinline : boolean): tnode;
  1763. begin
  1764. result:=nil;
  1765. { if the try contains no code, we can kill
  1766. the try and except and return only the
  1767. finally part }
  1768. if has_no_code(left) then
  1769. begin
  1770. result:=right;
  1771. right:=nil;
  1772. end;
  1773. end;
  1774. {*****************************************************************************
  1775. TONNODE
  1776. *****************************************************************************}
  1777. constructor tonnode.create(l,r:tnode);
  1778. begin
  1779. inherited create(onn,l,r);
  1780. excepTSymtable:=nil;
  1781. excepttype:=nil;
  1782. end;
  1783. destructor tonnode.destroy;
  1784. begin
  1785. { copied nodes don't need to release the symtable }
  1786. if assigned(excepTSymtable) then
  1787. excepTSymtable.free;
  1788. inherited destroy;
  1789. end;
  1790. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1791. begin
  1792. inherited ppuload(t,ppufile);
  1793. excepTSymtable:=nil;
  1794. excepttype:=nil;
  1795. end;
  1796. function tonnode.dogetcopy : tnode;
  1797. var
  1798. n : tonnode;
  1799. begin
  1800. n:=tonnode(inherited dogetcopy);
  1801. if assigned(exceptsymtable) then
  1802. n.exceptsymtable:=exceptsymtable.getcopy
  1803. else
  1804. n.exceptsymtable:=nil;
  1805. n.excepttype:=excepttype;
  1806. result:=n;
  1807. end;
  1808. function tonnode.pass_typecheck:tnode;
  1809. begin
  1810. result:=nil;
  1811. resultdef:=voidtype;
  1812. if not(is_class(excepttype)) then
  1813. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1814. if assigned(left) then
  1815. typecheckpass(left);
  1816. if assigned(right) then
  1817. typecheckpass(right);
  1818. end;
  1819. function tonnode.pass_1 : tnode;
  1820. begin
  1821. result:=nil;
  1822. include(current_procinfo.flags,pi_do_call);
  1823. expectloc:=LOC_VOID;
  1824. if assigned(left) then
  1825. firstpass(left);
  1826. if assigned(right) then
  1827. firstpass(right);
  1828. end;
  1829. function tonnode.docompare(p: tnode): boolean;
  1830. begin
  1831. docompare := false;
  1832. end;
  1833. end.