pstatmnt.pas 42 KB

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