pstatmnt.pas 41 KB

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