nflw.pas 71 KB

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