2
0

pstatmnt.pas 42 KB

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