pstatmnt.pas 42 KB

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