pstatmnt.pas 43 KB

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