pstatmnt.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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_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 : TFPObjectList;
  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:=TFPObjectList.create(true);
  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. symtablestack.pop(tsymtable(withsymtablelist[i]));
  508. withsymtablelist.free;
  509. // p:=cwithnode.create(right,twithsymtable(withsymtable),levelcount,refnode);
  510. { Finalize complex withnode with destroy of temp }
  511. if assigned(newblock) then
  512. begin
  513. addstatement(newstatement,p);
  514. if assigned(tempnode) then
  515. addstatement(newstatement,ctempdeletenode.create(tempnode));
  516. if assigned(calltempnode) then
  517. addstatement(newstatement,ctempdeletenode.create(calltempnode));
  518. p:=newblock;
  519. end;
  520. result:=p;
  521. end
  522. else
  523. begin
  524. p.free;
  525. Message(parser_e_false_with_expr);
  526. { try to recover from error }
  527. if try_to_consume(_COMMA) then
  528. begin
  529. hp:=_with_statement();
  530. if (hp=nil) then; { remove warning about unused }
  531. end
  532. else
  533. begin
  534. consume(_DO);
  535. { ignore all }
  536. if token<>_SEMICOLON then
  537. statement;
  538. end;
  539. result:=nil;
  540. end;
  541. end;
  542. function with_statement : tnode;
  543. begin
  544. consume(_WITH);
  545. with_statement:=_with_statement();
  546. end;
  547. function raise_statement : tnode;
  548. var
  549. p,pobj,paddr,pframe : tnode;
  550. begin
  551. pobj:=nil;
  552. paddr:=nil;
  553. pframe:=nil;
  554. consume(_RAISE);
  555. if not(token in endtokens) then
  556. begin
  557. { object }
  558. pobj:=comp_expr(true);
  559. if try_to_consume(_AT) then
  560. begin
  561. paddr:=comp_expr(true);
  562. if try_to_consume(_COMMA) then
  563. pframe:=comp_expr(true);
  564. end;
  565. end
  566. else
  567. begin
  568. if (block_type<>bt_except) then
  569. Message(parser_e_no_reraise_possible);
  570. end;
  571. p:=craisenode.create(pobj,paddr,pframe);
  572. raise_statement:=p;
  573. end;
  574. function try_statement : tnode;
  575. var
  576. p_try_block,p_finally_block,first,last,
  577. p_default,p_specific,hp : tnode;
  578. ot : ttype;
  579. sym : tlocalvarsym;
  580. old_block_type : tblock_type;
  581. exceptsymtable : tsymtable;
  582. objname,objrealname : stringid;
  583. srsym : tsym;
  584. srsymtable : tsymtable;
  585. oldaktexceptblock: integer;
  586. begin
  587. include(current_procinfo.flags,pi_uses_exceptions);
  588. p_default:=nil;
  589. p_specific:=nil;
  590. { read statements to try }
  591. consume(_TRY);
  592. first:=nil;
  593. inc(exceptblockcounter);
  594. oldaktexceptblock := aktexceptblock;
  595. aktexceptblock := exceptblockcounter;
  596. while (token<>_FINALLY) and (token<>_EXCEPT) do
  597. begin
  598. if first=nil then
  599. begin
  600. last:=cstatementnode.create(statement,nil);
  601. first:=last;
  602. end
  603. else
  604. begin
  605. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  606. last:=tstatementnode(last).right;
  607. end;
  608. if not try_to_consume(_SEMICOLON) then
  609. break;
  610. consume_emptystats;
  611. end;
  612. p_try_block:=cblocknode.create(first);
  613. if try_to_consume(_FINALLY) then
  614. begin
  615. inc(exceptblockcounter);
  616. aktexceptblock := exceptblockcounter;
  617. p_finally_block:=statements_til_end;
  618. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  619. end
  620. else
  621. begin
  622. consume(_EXCEPT);
  623. old_block_type:=block_type;
  624. block_type:=bt_except;
  625. inc(exceptblockcounter);
  626. aktexceptblock := exceptblockcounter;
  627. ot:=generrortype;
  628. p_specific:=nil;
  629. if (idtoken=_ON) then
  630. { catch specific exceptions }
  631. begin
  632. repeat
  633. consume(_ON);
  634. if token=_ID then
  635. begin
  636. objname:=pattern;
  637. objrealname:=orgpattern;
  638. { can't use consume_sym here, because we need already
  639. to check for the colon }
  640. searchsym(objname,srsym,srsymtable);
  641. consume(_ID);
  642. { is a explicit name for the exception given ? }
  643. if try_to_consume(_COLON) then
  644. begin
  645. consume_sym(srsym,srsymtable);
  646. if (srsym.typ=typesym) and
  647. is_class(ttypesym(srsym).restype.def) then
  648. begin
  649. ot:=ttypesym(srsym).restype;
  650. sym:=tlocalvarsym.create(objrealname,vs_value,ot,[]);
  651. end
  652. else
  653. begin
  654. sym:=tlocalvarsym.create(objrealname,vs_value,generrortype,[]);
  655. if (srsym.typ=typesym) then
  656. Message1(type_e_class_type_expected,ttypesym(srsym).restype.def.typename)
  657. else
  658. Message1(type_e_class_type_expected,ot.def.typename);
  659. end;
  660. exceptsymtable:=tstt_exceptsymtable.create;
  661. exceptsymtable.insert(sym);
  662. symtablestack.push(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. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  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.pop(exceptsymtable);
  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 TAsmList);
  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(mark_AsmBlockStart);
  810. AsmStat.p_asm.Insert(Marker);
  811. Marker := Tai_Marker.Create(mark_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. current_procinfo.procdef.localst.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.