nflw.pas 73 KB

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