pstatmnt.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does the parsing of the statements
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pstatmnt;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,node;
  22. function statement_block(starttoken : ttoken) : tnode;
  23. { reads an assembler block }
  24. function assembler_block : tnode;
  25. implementation
  26. uses
  27. { common }
  28. cutils,cclasses,
  29. { global }
  30. globtype,globals,verbose,constexp,
  31. systems,
  32. { aasm }
  33. cpubase,aasmbase,aasmtai,aasmdata,
  34. { symtable }
  35. symconst,symbase,symtype,symdef,symsym,symtable,defutil,defcmp,
  36. paramgr,symutil,
  37. { pass 1 }
  38. pass_1,htypechk,
  39. nutils,nbas,nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  40. { parser }
  41. scanner,
  42. pbase,pexpr,
  43. { codegen }
  44. procinfo,cgbase,
  45. { assembler reader }
  46. rabase
  47. ;
  48. function statement : tnode;forward;
  49. function if_statement : tnode;
  50. var
  51. ex,if_a,else_a : tnode;
  52. begin
  53. consume(_IF);
  54. ex:=comp_expr(true);
  55. consume(_THEN);
  56. if token<>_ELSE then
  57. if_a:=statement
  58. else
  59. if_a:=nil;
  60. if try_to_consume(_ELSE) then
  61. else_a:=statement
  62. else
  63. else_a:=nil;
  64. result:=cifnode.create(ex,if_a,else_a);
  65. end;
  66. { creates a block (list) of statements, til the next END token }
  67. function statements_til_end : tnode;
  68. var
  69. first,last : tstatementnode;
  70. begin
  71. first:=nil;
  72. while token<>_END do
  73. begin
  74. if first=nil then
  75. begin
  76. last:=cstatementnode.create(statement,nil);
  77. first:=last;
  78. end
  79. else
  80. begin
  81. last.right:=cstatementnode.create(statement,nil);
  82. last:=tstatementnode(last.right);
  83. end;
  84. if not try_to_consume(_SEMICOLON) then
  85. break;
  86. consume_emptystats;
  87. end;
  88. consume(_END);
  89. statements_til_end:=cblocknode.create(first);
  90. end;
  91. function case_statement : tnode;
  92. var
  93. casedef : tdef;
  94. caseexpr,p : tnode;
  95. blockid : longint;
  96. hl1,hl2 : TConstExprInt;
  97. casedeferror : boolean;
  98. casenode : tcasenode;
  99. begin
  100. consume(_CASE);
  101. caseexpr:=comp_expr(true);
  102. { determines result type }
  103. do_typecheckpass(caseexpr);
  104. { variants must be accepted, but first they must be converted to integer }
  105. if caseexpr.resultdef.typ=variantdef then
  106. begin
  107. caseexpr:=ctypeconvnode.create_internal(caseexpr,sinttype);
  108. do_typecheckpass(caseexpr);
  109. end;
  110. set_varstate(caseexpr,vs_read,[vsf_must_be_valid]);
  111. casedeferror:=false;
  112. casedef:=caseexpr.resultdef;
  113. if (not assigned(casedef)) or
  114. not(is_ordinal(casedef)) then
  115. begin
  116. CGMessage(type_e_ordinal_expr_expected);
  117. { create a correct tree }
  118. caseexpr.free;
  119. caseexpr:=cordconstnode.create(0,u32inttype,false);
  120. { set error flag so no rangechecks are done }
  121. casedeferror:=true;
  122. end;
  123. { Create casenode }
  124. casenode:=ccasenode.create(caseexpr);
  125. consume(_OF);
  126. { Parse all case blocks }
  127. blockid:=0;
  128. repeat
  129. { maybe an instruction has more case labels }
  130. repeat
  131. p:=expr;
  132. if is_widechar(casedef) then
  133. begin
  134. if (p.nodetype=rangen) then
  135. begin
  136. trangenode(p).left:=ctypeconvnode.create(trangenode(p).left,cwidechartype);
  137. trangenode(p).right:=ctypeconvnode.create(trangenode(p).right,cwidechartype);
  138. do_typecheckpass(trangenode(p).left);
  139. do_typecheckpass(trangenode(p).right);
  140. end
  141. else
  142. begin
  143. p:=ctypeconvnode.create(p,cwidechartype);
  144. do_typecheckpass(p);
  145. end;
  146. end;
  147. hl1:=0;
  148. hl2:=0;
  149. if (p.nodetype=rangen) then
  150. begin
  151. { type checking for case statements }
  152. if is_subequal(casedef, trangenode(p).left.resultdef) and
  153. is_subequal(casedef, trangenode(p).right.resultdef) then
  154. begin
  155. hl1:=get_ordinal_value(trangenode(p).left);
  156. hl2:=get_ordinal_value(trangenode(p).right);
  157. if hl1>hl2 then
  158. CGMessage(parser_e_case_lower_less_than_upper_bound);
  159. if not casedeferror then
  160. begin
  161. testrange(casedef,hl1,false);
  162. testrange(casedef,hl2,false);
  163. end;
  164. end
  165. else
  166. CGMessage(parser_e_case_mismatch);
  167. casenode.addlabel(blockid,hl1,hl2);
  168. end
  169. else
  170. begin
  171. { type checking for case statements }
  172. if not is_subequal(casedef, p.resultdef) then
  173. CGMessage(parser_e_case_mismatch);
  174. hl1:=get_ordinal_value(p);
  175. if not casedeferror then
  176. testrange(casedef,hl1,false);
  177. casenode.addlabel(blockid,hl1,hl1);
  178. end;
  179. p.free;
  180. if token=_COMMA then
  181. consume(_COMMA)
  182. else
  183. break;
  184. until false;
  185. consume(_COLON);
  186. { add instruction block }
  187. casenode.addblock(blockid,statement);
  188. { next block }
  189. inc(blockid);
  190. if not(token in [_ELSE,_OTHERWISE,_END]) then
  191. consume(_SEMICOLON);
  192. until (token in [_ELSE,_OTHERWISE,_END]);
  193. if (token in [_ELSE,_OTHERWISE]) then
  194. begin
  195. if not try_to_consume(_ELSE) then
  196. consume(_OTHERWISE);
  197. casenode.addelseblock(statements_til_end);
  198. end
  199. else
  200. consume(_END);
  201. result:=casenode;
  202. end;
  203. function repeat_statement : tnode;
  204. var
  205. first,last,p_e : tnode;
  206. begin
  207. consume(_REPEAT);
  208. first:=nil;
  209. while token<>_UNTIL do
  210. begin
  211. if first=nil then
  212. begin
  213. last:=cstatementnode.create(statement,nil);
  214. first:=last;
  215. end
  216. else
  217. begin
  218. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  219. last:=tstatementnode(last).right;
  220. end;
  221. if not try_to_consume(_SEMICOLON) then
  222. break;
  223. consume_emptystats;
  224. end;
  225. consume(_UNTIL);
  226. first:=cblocknode.create(first);
  227. p_e:=comp_expr(true);
  228. result:=cwhilerepeatnode.create(p_e,first,false,true);
  229. end;
  230. function while_statement : tnode;
  231. var
  232. p_e,p_a : tnode;
  233. begin
  234. consume(_WHILE);
  235. p_e:=comp_expr(true);
  236. consume(_DO);
  237. p_a:=statement;
  238. result:=cwhilerepeatnode.create(p_e,p_a,true,false);
  239. end;
  240. function for_statement : tnode;
  241. procedure check_range(hp:tnode);
  242. begin
  243. {$ifndef cpu64bit}
  244. if hp.nodetype=ordconstn then
  245. begin
  246. if (tordconstnode(hp).value<int64(low(longint))) or
  247. (tordconstnode(hp).value>high(longint)) then
  248. begin
  249. CGMessage(parser_e_range_check_error);
  250. { recover, prevent more warnings/errors }
  251. tordconstnode(hp).value:=0;
  252. end;
  253. end;
  254. {$endif cpu64bit}
  255. end;
  256. var
  257. hp,
  258. hloopvar,
  259. hblock,
  260. hto,hfrom : tnode;
  261. backward : boolean;
  262. loopvarsym : tabstractvarsym;
  263. begin
  264. { parse loop header }
  265. consume(_FOR);
  266. hloopvar:=factor(false);
  267. valid_for_loopvar(hloopvar,true);
  268. { Check loop variable }
  269. loopvarsym:=nil;
  270. { variable must be an ordinal, int64 is not allowed for 32bit targets }
  271. if not(is_ordinal(hloopvar.resultdef))
  272. {$ifndef cpu64bit}
  273. or is_64bitint(hloopvar.resultdef)
  274. {$endif cpu64bit}
  275. then
  276. MessagePos(hloopvar.fileinfo,type_e_ordinal_expr_expected);
  277. hp:=hloopvar;
  278. while assigned(hp) and
  279. (
  280. { record/object fields and array elements are allowed }
  281. { in tp7 mode only }
  282. (
  283. (m_tp7 in current_settings.modeswitches) and
  284. (
  285. ((hp.nodetype=subscriptn) and
  286. ((tsubscriptnode(hp).left.resultdef.typ=recorddef) or
  287. is_object(tsubscriptnode(hp).left.resultdef))
  288. ) or
  289. { constant array index }
  290. (
  291. (hp.nodetype=vecn) and
  292. is_constintnode(tvecnode(hp).right)
  293. )
  294. )
  295. ) or
  296. { equal typeconversions }
  297. (
  298. (hp.nodetype=typeconvn) and
  299. (ttypeconvnode(hp).convtype=tc_equal)
  300. )
  301. ) do
  302. begin
  303. { Use the recordfield for loopvarsym }
  304. if not assigned(loopvarsym) and
  305. (hp.nodetype=subscriptn) then
  306. loopvarsym:=tsubscriptnode(hp).vs;
  307. hp:=tunarynode(hp).left;
  308. end;
  309. if assigned(hp) and
  310. (hp.nodetype=loadn) then
  311. begin
  312. case tloadnode(hp).symtableentry.typ of
  313. staticvarsym,
  314. localvarsym,
  315. paravarsym :
  316. begin
  317. { we need a simple loadn:
  318. 1. The load must be in a global symtable or
  319. in the same level as the para of the current proc.
  320. 2. value variables (no const,out or var)
  321. 3. No threadvar, readonly or typedconst
  322. }
  323. if (
  324. (tloadnode(hp).symtable.symtablelevel=main_program_level) or
  325. (tloadnode(hp).symtable.symtablelevel=current_procinfo.procdef.parast.symtablelevel)
  326. ) and
  327. (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_value) and
  328. ([vo_is_thread_var,vo_is_typed_const] * tabstractvarsym(tloadnode(hp).symtableentry).varoptions=[]) then
  329. begin
  330. { Assigning for-loop variable is only allowed in tp7 and macpas }
  331. if ([m_tp7,m_mac] * current_settings.modeswitches = []) then
  332. begin
  333. if not assigned(loopvarsym) then
  334. loopvarsym:=tabstractvarsym(tloadnode(hp).symtableentry);
  335. include(loopvarsym.varoptions,vo_is_loop_counter);
  336. end;
  337. end
  338. else
  339. begin
  340. { Typed const is allowed in tp7 }
  341. if not(m_tp7 in current_settings.modeswitches) or
  342. not(vo_is_typed_const in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  343. MessagePos(hp.fileinfo,type_e_illegal_count_var);
  344. end;
  345. end;
  346. else
  347. MessagePos(hp.fileinfo,type_e_illegal_count_var);
  348. end;
  349. end
  350. else
  351. MessagePos(hloopvar.fileinfo,type_e_illegal_count_var);
  352. consume(_ASSIGNMENT);
  353. hfrom:=comp_expr(true);
  354. if try_to_consume(_DOWNTO) then
  355. backward:=true
  356. else
  357. begin
  358. consume(_TO);
  359. backward:=false;
  360. end;
  361. hto:=comp_expr(true);
  362. consume(_DO);
  363. { Check if the constants fit in the range }
  364. check_range(hfrom);
  365. check_range(hto);
  366. { first set the varstate for from and to, so
  367. uses of loopvar in those expressions will also
  368. trigger a warning when it is not used yet. This
  369. needs to be done before the instruction block is
  370. parsed to have a valid hloopvar }
  371. typecheckpass(hfrom);
  372. set_varstate(hfrom,vs_read,[vsf_must_be_valid]);
  373. typecheckpass(hto);
  374. set_varstate(hto,vs_read,[vsf_must_be_valid]);
  375. typecheckpass(hloopvar);
  376. { in two steps, because vs_readwritten may turn on vsf_must_be_valid }
  377. { for some subnodes }
  378. set_varstate(hloopvar,vs_written,[]);
  379. set_varstate(hloopvar,vs_read,[vsf_must_be_valid]);
  380. { ... now the instruction block }
  381. hblock:=statement;
  382. { variable is not used for loop counter anymore }
  383. if assigned(loopvarsym) then
  384. exclude(loopvarsym.varoptions,vo_is_loop_counter);
  385. result:=cfornode.create(hloopvar,hfrom,hto,hblock,backward);
  386. end;
  387. function _with_statement : tnode;
  388. var
  389. p : tnode;
  390. i : longint;
  391. st : TSymtable;
  392. newblock : tblocknode;
  393. newstatement : tstatementnode;
  394. calltempnode,
  395. tempnode : ttempcreatenode;
  396. valuenode,
  397. hp,
  398. refnode : tnode;
  399. hdef : tdef;
  400. hasimplicitderef : boolean;
  401. withsymtablelist : TFPObjectList;
  402. procedure pushobjchild(withdef,obj:tobjectdef);
  403. begin
  404. if not assigned(obj) then
  405. exit;
  406. pushobjchild(withdef,obj.childof);
  407. { keep the original tobjectdef as owner, because that is used for
  408. visibility of the symtable }
  409. st:=twithsymtable.create(withdef,obj.symtable.SymList,refnode.getcopy);
  410. symtablestack.push(st);
  411. withsymtablelist.add(st);
  412. end;
  413. begin
  414. p:=comp_expr(true);
  415. do_typecheckpass(p);
  416. if (p.nodetype=vecn) and
  417. (nf_memseg in p.flags) then
  418. CGMessage(parser_e_no_with_for_variable_in_other_segments);
  419. if (p.resultdef.typ in [objectdef,recorddef,classrefdef]) then
  420. begin
  421. newblock:=nil;
  422. valuenode:=nil;
  423. tempnode:=nil;
  424. { ignore nodes that don't add instructions in the tree }
  425. hp:=p;
  426. while { equal type conversions }
  427. (
  428. (hp.nodetype=typeconvn) and
  429. (ttypeconvnode(hp).convtype=tc_equal)
  430. ) or
  431. { constant array index }
  432. (
  433. (hp.nodetype=vecn) and
  434. (tvecnode(hp).right.nodetype=ordconstn)
  435. ) do
  436. hp:=tunarynode(hp).left;
  437. if (hp.nodetype=loadn) and
  438. (
  439. (tloadnode(hp).symtable=current_procinfo.procdef.localst) or
  440. (tloadnode(hp).symtable=current_procinfo.procdef.parast) or
  441. (tloadnode(hp).symtable.symtabletype in [staticsymtable,globalsymtable])
  442. ) then
  443. begin
  444. { simple load, we can reference direct }
  445. refnode:=p;
  446. end
  447. else
  448. begin
  449. calltempnode:=nil;
  450. { complex load, load in temp first }
  451. newblock:=internalstatements(newstatement);
  452. { when we can't take the address of p, load it in a temp }
  453. { since we may need its address later on }
  454. if not valid_for_addr(p,false) then
  455. begin
  456. calltempnode:=ctempcreatenode.create(p.resultdef,p.resultdef.size,tt_persistent,true);
  457. addstatement(newstatement,calltempnode);
  458. addstatement(newstatement,cassignmentnode.create(
  459. ctemprefnode.create(calltempnode),
  460. p));
  461. p:=ctemprefnode.create(calltempnode);
  462. typecheckpass(p);
  463. end;
  464. { classes and interfaces have implicit dereferencing }
  465. hasimplicitderef:=is_class_or_interface(p.resultdef) or
  466. (p.resultdef.typ = classrefdef);
  467. if hasimplicitderef then
  468. hdef:=p.resultdef
  469. else
  470. hdef:=tpointerdef.create(p.resultdef);
  471. { load address of the value in a temp }
  472. tempnode:=ctempcreatenode.create_withnode(hdef,sizeof(aint),tt_persistent,true,p);
  473. typecheckpass(tempnode);
  474. valuenode:=p;
  475. refnode:=ctemprefnode.create(tempnode);
  476. fillchar(refnode.fileinfo,sizeof(tfileposinfo),0);
  477. { add address call for valuenode and deref for refnode if this
  478. is not done implicitly }
  479. if not hasimplicitderef then
  480. begin
  481. valuenode:=caddrnode.create_internal_nomark(valuenode);
  482. refnode:=cderefnode.create(refnode);
  483. fillchar(refnode.fileinfo,sizeof(tfileposinfo),0);
  484. end;
  485. addstatement(newstatement,tempnode);
  486. addstatement(newstatement,cassignmentnode.create(
  487. ctemprefnode.create(tempnode),
  488. valuenode));
  489. typecheckpass(refnode);
  490. end;
  491. withsymtablelist:=TFPObjectList.create(true);
  492. case p.resultdef.typ of
  493. objectdef :
  494. begin
  495. { push symtables of all parents in reverse order }
  496. pushobjchild(tobjectdef(p.resultdef),tobjectdef(p.resultdef).childof);
  497. { push object symtable }
  498. st:=twithsymtable.Create(tobjectdef(p.resultdef),tobjectdef(p.resultdef).symtable.SymList,refnode);
  499. symtablestack.push(st);
  500. withsymtablelist.add(st);
  501. end;
  502. classrefdef :
  503. begin
  504. { push symtables of all parents in reverse order }
  505. pushobjchild(tobjectdef(tclassrefdef(p.resultdef).pointeddef),tobjectdef(tclassrefdef(p.resultdef).pointeddef).childof);
  506. { push object symtable }
  507. st:=twithsymtable.Create(tobjectdef(tclassrefdef(p.resultdef).pointeddef),tobjectdef(tclassrefdef(p.resultdef).pointeddef).symtable.SymList,refnode);
  508. symtablestack.push(st);
  509. withsymtablelist.add(st);
  510. end;
  511. recorddef :
  512. begin
  513. st:=twithsymtable.create(trecorddef(p.resultdef),trecorddef(p.resultdef).symtable.SymList,refnode);
  514. symtablestack.push(st);
  515. withsymtablelist.add(st);
  516. end;
  517. else
  518. internalerror(200601271);
  519. end;
  520. if try_to_consume(_COMMA) then
  521. p:=_with_statement()
  522. else
  523. begin
  524. consume(_DO);
  525. if token<>_SEMICOLON then
  526. p:=statement
  527. else
  528. p:=cerrornode.create;
  529. end;
  530. { remove symtables in reverse order from the stack }
  531. for i:=withsymtablelist.count-1 downto 0 do
  532. symtablestack.pop(TSymtable(withsymtablelist[i]));
  533. withsymtablelist.free;
  534. // p:=cwithnode.create(right,twithsymtable(withsymtable),levelcount,refnode);
  535. { Finalize complex withnode with destroy of temp }
  536. if assigned(newblock) then
  537. begin
  538. addstatement(newstatement,p);
  539. if assigned(tempnode) then
  540. addstatement(newstatement,ctempdeletenode.create(tempnode));
  541. if assigned(calltempnode) then
  542. addstatement(newstatement,ctempdeletenode.create(calltempnode));
  543. p:=newblock;
  544. end;
  545. result:=p;
  546. end
  547. else
  548. begin
  549. p.free;
  550. Message(parser_e_false_with_expr);
  551. { try to recover from error }
  552. if try_to_consume(_COMMA) then
  553. begin
  554. hp:=_with_statement();
  555. if (hp=nil) then; { remove warning about unused }
  556. end
  557. else
  558. begin
  559. consume(_DO);
  560. { ignore all }
  561. if token<>_SEMICOLON then
  562. statement;
  563. end;
  564. result:=nil;
  565. end;
  566. end;
  567. function with_statement : tnode;
  568. begin
  569. consume(_WITH);
  570. with_statement:=_with_statement();
  571. end;
  572. function raise_statement : tnode;
  573. var
  574. p,pobj,paddr,pframe : tnode;
  575. begin
  576. pobj:=nil;
  577. paddr:=nil;
  578. pframe:=nil;
  579. consume(_RAISE);
  580. if not(token in endtokens) then
  581. begin
  582. { object }
  583. pobj:=comp_expr(true);
  584. if try_to_consume(_AT) then
  585. begin
  586. paddr:=comp_expr(true);
  587. if try_to_consume(_COMMA) then
  588. pframe:=comp_expr(true);
  589. end;
  590. end
  591. else
  592. begin
  593. if (block_type<>bt_except) then
  594. Message(parser_e_no_reraise_possible);
  595. end;
  596. p:=craisenode.create(pobj,paddr,pframe);
  597. raise_statement:=p;
  598. end;
  599. function try_statement : tnode;
  600. var
  601. p_try_block,p_finally_block,first,last,
  602. p_default,p_specific,hp : tnode;
  603. ot : tDef;
  604. sym : tlocalvarsym;
  605. old_block_type : tblock_type;
  606. excepTSymtable : TSymtable;
  607. objname,objrealname : TIDString;
  608. srsym : tsym;
  609. srsymtable : TSymtable;
  610. oldaktexceptblock: integer;
  611. begin
  612. include(current_procinfo.flags,pi_uses_exceptions);
  613. p_default:=nil;
  614. p_specific:=nil;
  615. { read statements to try }
  616. consume(_TRY);
  617. first:=nil;
  618. inc(exceptblockcounter);
  619. oldaktexceptblock := aktexceptblock;
  620. aktexceptblock := exceptblockcounter;
  621. while (token<>_FINALLY) and (token<>_EXCEPT) do
  622. begin
  623. if first=nil then
  624. begin
  625. last:=cstatementnode.create(statement,nil);
  626. first:=last;
  627. end
  628. else
  629. begin
  630. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  631. last:=tstatementnode(last).right;
  632. end;
  633. if not try_to_consume(_SEMICOLON) then
  634. break;
  635. consume_emptystats;
  636. end;
  637. p_try_block:=cblocknode.create(first);
  638. if try_to_consume(_FINALLY) then
  639. begin
  640. inc(exceptblockcounter);
  641. aktexceptblock := exceptblockcounter;
  642. p_finally_block:=statements_til_end;
  643. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  644. end
  645. else
  646. begin
  647. consume(_EXCEPT);
  648. old_block_type:=block_type;
  649. block_type:=bt_except;
  650. inc(exceptblockcounter);
  651. aktexceptblock := exceptblockcounter;
  652. ot:=generrordef;
  653. p_specific:=nil;
  654. if (idtoken=_ON) then
  655. { catch specific exceptions }
  656. begin
  657. repeat
  658. consume(_ON);
  659. if token=_ID then
  660. begin
  661. objname:=pattern;
  662. objrealname:=orgpattern;
  663. { can't use consume_sym here, because we need already
  664. to check for the colon }
  665. searchsym(objname,srsym,srsymtable);
  666. consume(_ID);
  667. { is a explicit name for the exception given ? }
  668. if try_to_consume(_COLON) then
  669. begin
  670. consume_sym(srsym,srsymtable);
  671. if (srsym.typ=typesym) and
  672. is_class(ttypesym(srsym).typedef) then
  673. begin
  674. ot:=ttypesym(srsym).typedef;
  675. sym:=tlocalvarsym.create(objrealname,vs_value,ot,[]);
  676. end
  677. else
  678. begin
  679. sym:=tlocalvarsym.create(objrealname,vs_value,generrordef,[]);
  680. if (srsym.typ=typesym) then
  681. Message1(type_e_class_type_expected,ttypesym(srsym).typedef.typename)
  682. else
  683. Message1(type_e_class_type_expected,ot.typename);
  684. end;
  685. excepTSymtable:=tstt_excepTSymtable.create;
  686. excepTSymtable.insert(sym);
  687. symtablestack.push(excepTSymtable);
  688. end
  689. else
  690. begin
  691. { check if type is valid, must be done here because
  692. with "e: Exception" the e is not necessary }
  693. if srsym=nil then
  694. begin
  695. identifier_not_found(objrealname);
  696. srsym:=generrorsym;
  697. end;
  698. { support unit.identifier }
  699. if srsym.typ=unitsym then
  700. begin
  701. consume(_POINT);
  702. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  703. if srsym=nil then
  704. begin
  705. identifier_not_found(orgpattern);
  706. srsym:=generrorsym;
  707. end;
  708. consume(_ID);
  709. end;
  710. { check if type is valid, must be done here because
  711. with "e: Exception" the e is not necessary }
  712. if (srsym.typ=typesym) and
  713. is_class(ttypesym(srsym).typedef) then
  714. ot:=ttypesym(srsym).typedef
  715. else
  716. begin
  717. ot:=generrordef;
  718. if (srsym.typ=typesym) then
  719. Message1(type_e_class_type_expected,ttypesym(srsym).typedef.typename)
  720. else
  721. Message1(type_e_class_type_expected,ot.typename);
  722. end;
  723. excepTSymtable:=nil;
  724. end;
  725. end
  726. else
  727. consume(_ID);
  728. consume(_DO);
  729. hp:=connode.create(nil,statement);
  730. if ot.typ=errordef then
  731. begin
  732. hp.free;
  733. hp:=cerrornode.create;
  734. end;
  735. if p_specific=nil then
  736. begin
  737. last:=hp;
  738. p_specific:=last;
  739. end
  740. else
  741. begin
  742. tonnode(last).left:=hp;
  743. last:=tonnode(last).left;
  744. end;
  745. { set the informations }
  746. { only if the creation of the onnode was succesful, it's possible }
  747. { that last and hp are errornodes (JM) }
  748. if last.nodetype = onn then
  749. begin
  750. tonnode(last).excepttype:=tobjectdef(ot);
  751. tonnode(last).excepTSymtable:=excepTSymtable;
  752. end;
  753. { remove exception symtable }
  754. if assigned(excepTSymtable) then
  755. begin
  756. symtablestack.pop(excepTSymtable);
  757. if last.nodetype <> onn then
  758. excepTSymtable.free;
  759. end;
  760. if not try_to_consume(_SEMICOLON) then
  761. break;
  762. consume_emptystats;
  763. until (token in [_END,_ELSE]);
  764. if try_to_consume(_ELSE) then
  765. begin
  766. { catch the other exceptions }
  767. p_default:=statements_til_end;
  768. end
  769. else
  770. consume(_END);
  771. end
  772. else
  773. begin
  774. { catch all exceptions }
  775. p_default:=statements_til_end;
  776. end;
  777. block_type:=old_block_type;
  778. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  779. end;
  780. aktexceptblock := oldaktexceptblock;
  781. end;
  782. function _asm_statement : tnode;
  783. var
  784. asmstat : tasmnode;
  785. Marker : tai;
  786. reg : tregister;
  787. asmreader : tbaseasmreader;
  788. begin
  789. Inside_asm_statement:=true;
  790. if assigned(asmmodeinfos[current_settings.asmmode]) then
  791. begin
  792. asmreader:=asmmodeinfos[current_settings.asmmode]^.casmreader.create;
  793. asmstat:=casmnode.create(asmreader.assemble as TAsmList);
  794. asmreader.free;
  795. end
  796. else
  797. Message(parser_f_assembler_reader_not_supported);
  798. { Mark procedure that it has assembler blocks }
  799. include(current_procinfo.flags,pi_has_assembler_block);
  800. { Read first the _ASM statement }
  801. consume(_ASM);
  802. { END is read, got a list of changed registers? }
  803. if try_to_consume(_LECKKLAMMER) then
  804. begin
  805. asmstat.used_regs_fpu:=[0..first_fpu_imreg-1];
  806. if token<>_RECKKLAMMER then
  807. begin
  808. repeat
  809. { it's possible to specify the modified registers }
  810. reg:=std_regnum_search(lower(pattern));
  811. if reg<>NR_NO then
  812. begin
  813. if getregtype(reg)=R_INTREGISTER then
  814. include(asmstat.used_regs_int,getsupreg(reg));
  815. end
  816. else
  817. Message(asmr_e_invalid_register);
  818. consume(_CSTRING);
  819. if not try_to_consume(_COMMA) then
  820. break;
  821. until false;
  822. end;
  823. consume(_RECKKLAMMER);
  824. end
  825. else
  826. begin
  827. asmstat.used_regs_int:=paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  828. asmstat.used_regs_fpu:=paramanager.get_volatile_registers_fpu(current_procinfo.procdef.proccalloption);
  829. end;
  830. { mark the start and the end of the assembler block
  831. this is needed for the optimizer }
  832. If Assigned(AsmStat.p_asm) Then
  833. Begin
  834. Marker := Tai_Marker.Create(mark_AsmBlockStart);
  835. AsmStat.p_asm.Insert(Marker);
  836. Marker := Tai_Marker.Create(mark_AsmBlockEnd);
  837. AsmStat.p_asm.Concat(Marker);
  838. End;
  839. Inside_asm_statement:=false;
  840. _asm_statement:=asmstat;
  841. end;
  842. function statement : tnode;
  843. var
  844. p : tnode;
  845. code : tnode;
  846. filepos : tfileposinfo;
  847. srsym : tsym;
  848. srsymtable : TSymtable;
  849. s : TIDString;
  850. begin
  851. filepos:=current_tokenpos;
  852. case token of
  853. _GOTO :
  854. begin
  855. if not(cs_support_goto in current_settings.moduleswitches)then
  856. Message(sym_e_goto_and_label_not_supported);
  857. consume(_GOTO);
  858. if (token<>_INTCONST) and (token<>_ID) then
  859. begin
  860. Message(sym_e_label_not_found);
  861. code:=cerrornode.create;
  862. end
  863. else
  864. begin
  865. if token=_ID then
  866. consume_sym(srsym,srsymtable)
  867. else
  868. begin
  869. searchsym(pattern,srsym,srsymtable);
  870. if srsym=nil then
  871. begin
  872. identifier_not_found(pattern);
  873. srsym:=generrorsym;
  874. srsymtable:=nil;
  875. end;
  876. consume(token);
  877. end;
  878. if srsym.typ<>labelsym then
  879. begin
  880. Message(sym_e_id_is_no_label_id);
  881. code:=cerrornode.create;
  882. end
  883. else
  884. begin
  885. { goto is only allowed to labels within the current scope }
  886. if srsym.owner<>current_procinfo.procdef.localst then
  887. CGMessage(parser_e_goto_outside_proc);
  888. code:=cgotonode.create(tlabelsym(srsym));
  889. tgotonode(code).labelsym:=tlabelsym(srsym);
  890. { set flag that this label is used }
  891. tlabelsym(srsym).used:=true;
  892. end;
  893. end;
  894. end;
  895. _BEGIN :
  896. code:=statement_block(_BEGIN);
  897. _IF :
  898. code:=if_statement;
  899. _CASE :
  900. code:=case_statement;
  901. _REPEAT :
  902. code:=repeat_statement;
  903. _WHILE :
  904. code:=while_statement;
  905. _FOR :
  906. code:=for_statement;
  907. _WITH :
  908. code:=with_statement;
  909. _TRY :
  910. code:=try_statement;
  911. _RAISE :
  912. code:=raise_statement;
  913. { semicolons,else until and end are ignored }
  914. _SEMICOLON,
  915. _ELSE,
  916. _UNTIL,
  917. _END:
  918. code:=cnothingnode.create;
  919. _FAIL :
  920. begin
  921. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  922. Message(parser_e_fail_only_in_constructor);
  923. consume(_FAIL);
  924. code:=call_fail_node;
  925. end;
  926. _ASM :
  927. code:=_asm_statement;
  928. _EOF :
  929. Message(scan_f_end_of_file);
  930. else
  931. begin
  932. p:=expr;
  933. { save the pattern here for latter usage, the label could be "000",
  934. even if we read an expression, the pattern is still valid if it's really
  935. a label (FK)
  936. if you want to mess here, take care of
  937. tests/webtbs/tw3546.pp
  938. }
  939. s:=pattern;
  940. { When a colon follows a intconst then transform it into a label }
  941. if (p.nodetype=ordconstn) and
  942. try_to_consume(_COLON) then
  943. begin
  944. p.free;
  945. searchsym(s,srsym,srsymtable);
  946. if assigned(srsym) and
  947. (srsym.typ=labelsym) then
  948. begin
  949. if tlabelsym(srsym).defined then
  950. Message(sym_e_label_already_defined);
  951. tlabelsym(srsym).defined:=true;
  952. p:=clabelnode.create(nil,tlabelsym(srsym));
  953. tlabelsym(srsym).code:=p;
  954. end
  955. else
  956. begin
  957. Message1(sym_e_label_used_and_not_defined,s);
  958. p:=cnothingnode.create;
  959. end;
  960. end;
  961. if p.nodetype=labeln then
  962. begin
  963. { the pointer to the following instruction }
  964. { isn't a very clean way }
  965. if token in endtokens then
  966. tlabelnode(p).left:=cnothingnode.create
  967. else
  968. tlabelnode(p).left:=statement();
  969. { be sure to have left also typecheckpass }
  970. typecheckpass(tlabelnode(p).left);
  971. end
  972. else
  973. { change a load of a procvar to a call. this is also
  974. supported in fpc mode }
  975. if p.nodetype in [vecn,derefn,typeconvn,subscriptn,loadn] then
  976. maybe_call_procvar(p,false);
  977. { blockn support because a read/write is changed into a blocknode }
  978. { with a separate statement for each read/write operation (JM) }
  979. { the same is true for val() if the third parameter is not 32 bit }
  980. if not(p.nodetype in [nothingn,calln,ifn,assignn,breakn,inlinen,
  981. continuen,labeln,blockn,exitn]) or
  982. ((p.nodetype=inlinen) and
  983. not is_void(p.resultdef)) then
  984. Message(parser_e_illegal_expression);
  985. { Specify that we don't use the value returned by the call.
  986. This is used for :
  987. - dispose of temp stack space
  988. - dispose on FPU stack }
  989. if (p.nodetype=calln) then
  990. exclude(tcallnode(p).callnodeflags,cnf_return_value_used);
  991. code:=p;
  992. end;
  993. end;
  994. if assigned(code) then
  995. begin
  996. typecheckpass(code);
  997. code.fileinfo:=filepos;
  998. end;
  999. statement:=code;
  1000. end;
  1001. function statement_block(starttoken : ttoken) : tnode;
  1002. var
  1003. first,last : tnode;
  1004. filepos : tfileposinfo;
  1005. begin
  1006. first:=nil;
  1007. filepos:=current_tokenpos;
  1008. consume(starttoken);
  1009. while not(token in [_END,_FINALIZATION]) do
  1010. begin
  1011. if first=nil then
  1012. begin
  1013. last:=cstatementnode.create(statement,nil);
  1014. first:=last;
  1015. end
  1016. else
  1017. begin
  1018. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  1019. last:=tstatementnode(last).right;
  1020. end;
  1021. if (token in [_END,_FINALIZATION]) then
  1022. break
  1023. else
  1024. begin
  1025. { if no semicolon, then error and go on }
  1026. if token<>_SEMICOLON then
  1027. begin
  1028. consume(_SEMICOLON);
  1029. consume_all_until(_SEMICOLON);
  1030. end;
  1031. consume(_SEMICOLON);
  1032. end;
  1033. consume_emptystats;
  1034. end;
  1035. { don't consume the finalization token, it is consumed when
  1036. reading the finalization block, but allow it only after
  1037. an initalization ! }
  1038. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1039. consume(_END);
  1040. last:=cblocknode.create(first);
  1041. last.fileinfo:=filepos;
  1042. statement_block:=last;
  1043. end;
  1044. function assembler_block : tnode;
  1045. var
  1046. p : tnode;
  1047. locals : longint;
  1048. srsym : tsym;
  1049. begin
  1050. { Rename the funcret so that recursive calls are possible }
  1051. if not is_void(current_procinfo.procdef.returndef) then
  1052. begin
  1053. srsym:=TSym(current_procinfo.procdef.localst.Find(current_procinfo.procdef.procsym.name));
  1054. if assigned(srsym) then
  1055. srsym.realname:='$hiddenresult';
  1056. end;
  1057. { delphi uses register calling for assembler methods }
  1058. if (m_delphi in current_settings.modeswitches) and
  1059. (po_assembler in current_procinfo.procdef.procoptions) and
  1060. not(po_hascallingconvention in current_procinfo.procdef.procoptions) then
  1061. current_procinfo.procdef.proccalloption:=pocall_register;
  1062. { force the asm statement }
  1063. if token<>_ASM then
  1064. consume(_ASM);
  1065. include(current_procinfo.flags,pi_is_assembler);
  1066. p:=_asm_statement;
  1067. {$ifndef sparc}
  1068. {$ifndef arm}
  1069. if (po_assembler in current_procinfo.procdef.procoptions) then
  1070. begin
  1071. { set the framepointer to esp for assembler functions when the
  1072. following conditions are met:
  1073. - if the are no local variables and parameters (except the allocated result)
  1074. - no reference to the result variable (refcount<=1)
  1075. - result is not stored as parameter
  1076. - target processor has optional frame pointer save
  1077. (vm, i386, vm only currently)
  1078. }
  1079. locals:=tabstractlocalsymtable(current_procinfo.procdef.parast).count_locals;
  1080. if (current_procinfo.procdef.localst.symtabletype=localsymtable) then
  1081. inc(locals,tabstractlocalsymtable(current_procinfo.procdef.localst).count_locals);
  1082. if (locals=0) and
  1083. (current_procinfo.procdef.owner.symtabletype<>ObjectSymtable) and
  1084. (not assigned(current_procinfo.procdef.funcretsym) or
  1085. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs<=1)) and
  1086. not(paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption)) then
  1087. begin
  1088. { Only need to set the framepointer, the locals will
  1089. be inserted with the correct reference in tcgasmnode.pass_generate_code }
  1090. current_procinfo.framepointer:=NR_STACK_POINTER_REG;
  1091. end;
  1092. end;
  1093. {$endif arm}
  1094. {$endif sparc}
  1095. { Flag the result as assigned when it is returned in a
  1096. register.
  1097. }
  1098. if assigned(current_procinfo.procdef.funcretsym) and
  1099. (not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption)) then
  1100. tabstractvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_initialised;
  1101. { because the END is already read we need to get the
  1102. last_endtoken_filepos here (PFV) }
  1103. last_endtoken_filepos:=current_tokenpos;
  1104. assembler_block:=p;
  1105. end;
  1106. end.