pstatmnt.pas 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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,constexp,
  31. systems,
  32. { aasm }
  33. cpubase,aasmtai,aasmdata,
  34. { symtable }
  35. symconst,symbase,symtype,symdef,symsym,symtable,defutil,defcmp,
  36. paramgr,
  37. { pass 1 }
  38. pass_1,htypechk,
  39. nutils,ngenutil,nbas,ncal,nmem,nset,ncnv,ncon,nld,nflw,
  40. { parser }
  41. scanner,
  42. pbase,ptype,pexpr,
  43. { codegen }
  44. procinfo,cgbase,
  45. { assembler reader }
  46. rabase;
  47. function statement : tnode;forward;
  48. function if_statement : tnode;
  49. var
  50. ex,if_a,else_a : tnode;
  51. begin
  52. consume(_IF);
  53. ex:=comp_expr([ef_accept_equal]);
  54. consume(_THEN);
  55. if not(token in endtokens) then
  56. if_a:=statement
  57. else
  58. if_a:=nil;
  59. if try_to_consume(_ELSE) then
  60. else_a:=statement
  61. else
  62. else_a:=nil;
  63. result:=cifnode.create(ex,if_a,else_a);
  64. end;
  65. { creates a block (list) of statements, til the next END token }
  66. function statements_til_end : tnode;
  67. var
  68. first,last : tstatementnode;
  69. begin
  70. first:=nil;
  71. last:=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. sl1,sl2 : tstringconstnode;
  98. casedeferror, caseofstring : boolean;
  99. casenode : tcasenode;
  100. begin
  101. consume(_CASE);
  102. caseexpr:=comp_expr([ef_accept_equal]);
  103. { determines result type }
  104. do_typecheckpass(caseexpr);
  105. { variants must be accepted, but first they must be converted to integer }
  106. if caseexpr.resultdef.typ=variantdef then
  107. begin
  108. caseexpr:=ctypeconvnode.create_internal(caseexpr,sinttype);
  109. do_typecheckpass(caseexpr);
  110. end;
  111. set_varstate(caseexpr,vs_read,[vsf_must_be_valid]);
  112. casedeferror:=false;
  113. casedef:=caseexpr.resultdef;
  114. { case of string must be rejected in delphi-, }
  115. { tp7/bp7-, mac-compatibility modes. }
  116. caseofstring :=
  117. ([m_delphi, m_mac, m_tp7] * current_settings.modeswitches = []) and
  118. is_string(casedef);
  119. if (not assigned(casedef)) or
  120. ( not(is_ordinal(casedef)) and (not caseofstring) ) then
  121. begin
  122. CGMessage(type_e_ordinal_or_string_expr_expected);
  123. { create a correct tree }
  124. caseexpr.free;
  125. caseexpr:=cordconstnode.create(0,u32inttype,false);
  126. { set error flag so no rangechecks are done }
  127. casedeferror:=true;
  128. end;
  129. { Create casenode }
  130. casenode:=ccasenode.create(caseexpr);
  131. consume(_OF);
  132. { Parse all case blocks }
  133. blockid:=0;
  134. repeat
  135. { maybe an instruction has more case labels }
  136. repeat
  137. p:=expr(true);
  138. if is_widechar(casedef) then
  139. begin
  140. if (p.nodetype=rangen) then
  141. begin
  142. trangenode(p).left:=ctypeconvnode.create(trangenode(p).left,cwidechartype);
  143. trangenode(p).right:=ctypeconvnode.create(trangenode(p).right,cwidechartype);
  144. do_typecheckpass(trangenode(p).left);
  145. do_typecheckpass(trangenode(p).right);
  146. end
  147. else
  148. begin
  149. p:=ctypeconvnode.create(p,cwidechartype);
  150. do_typecheckpass(p);
  151. end;
  152. end
  153. else
  154. begin
  155. if is_char(casedef) and is_widechar(p.resultdef) then
  156. begin
  157. if (p.nodetype=ordconstn) then
  158. begin
  159. p:=ctypeconvnode.create(p,cansichartype);
  160. do_typecheckpass(p);
  161. end
  162. else if (p.nodetype=rangen) then
  163. begin
  164. trangenode(p).left:=ctypeconvnode.create(trangenode(p).left,cansichartype);
  165. trangenode(p).right:=ctypeconvnode.create(trangenode(p).right,cansichartype);
  166. do_typecheckpass(trangenode(p).left);
  167. do_typecheckpass(trangenode(p).right);
  168. end;
  169. end;
  170. end;
  171. hl1:=0;
  172. hl2:=0;
  173. sl1:=nil;
  174. sl2:=nil;
  175. if (p.nodetype=rangen) then
  176. begin
  177. { type check for string case statements }
  178. if caseofstring and
  179. is_conststring_or_constcharnode(trangenode(p).left) and
  180. is_conststring_or_constcharnode(trangenode(p).right) then
  181. begin
  182. { we need stringconstnodes, even if expression contains single chars }
  183. sl1 := get_string_value(trangenode(p).left, tstringdef(casedef));
  184. sl2 := get_string_value(trangenode(p).right, tstringdef(casedef));
  185. if sl1.fullcompare(sl2) > 0 then
  186. CGMessage(parser_e_case_lower_less_than_upper_bound);
  187. end
  188. { type checking for ordinal case statements }
  189. else if (not caseofstring) and
  190. is_subequal(casedef, trangenode(p).left.resultdef) and
  191. is_subequal(casedef, trangenode(p).right.resultdef) then
  192. begin
  193. hl1:=get_ordinal_value(trangenode(p).left);
  194. hl2:=get_ordinal_value(trangenode(p).right);
  195. if hl1>hl2 then
  196. CGMessage(parser_e_case_lower_less_than_upper_bound);
  197. if not casedeferror then
  198. begin
  199. testrange(casedef,hl1,false,false);
  200. testrange(casedef,hl2,false,false);
  201. end;
  202. end
  203. else
  204. CGMessage(parser_e_case_mismatch);
  205. if caseofstring then
  206. casenode.addlabel(blockid,sl1,sl2)
  207. else
  208. casenode.addlabel(blockid,hl1,hl2);
  209. end
  210. else
  211. begin
  212. { type check for string case statements }
  213. if (caseofstring and (not is_conststring_or_constcharnode(p))) or
  214. { type checking for ordinal case statements }
  215. ((not caseofstring) and (not is_subequal(casedef, p.resultdef))) then
  216. CGMessage(parser_e_case_mismatch);
  217. if caseofstring then
  218. begin
  219. sl1:=get_string_value(p, tstringdef(casedef));
  220. casenode.addlabel(blockid,sl1,sl1);
  221. end
  222. else
  223. begin
  224. hl1:=get_ordinal_value(p);
  225. if not casedeferror then
  226. testrange(casedef,hl1,false,false);
  227. casenode.addlabel(blockid,hl1,hl1);
  228. end;
  229. end;
  230. p.free;
  231. sl1.free;
  232. sl2.free;
  233. if token=_COMMA then
  234. consume(_COMMA)
  235. else
  236. break;
  237. until false;
  238. consume(_COLON);
  239. { add instruction block }
  240. casenode.addblock(blockid,statement);
  241. { next block }
  242. inc(blockid);
  243. if not(token in [_ELSE,_OTHERWISE,_END]) then
  244. consume(_SEMICOLON);
  245. until (token in [_ELSE,_OTHERWISE,_END]);
  246. if (token in [_ELSE,_OTHERWISE]) then
  247. begin
  248. if not try_to_consume(_ELSE) then
  249. consume(_OTHERWISE);
  250. casenode.addelseblock(statements_til_end);
  251. end
  252. else
  253. consume(_END);
  254. result:=casenode;
  255. end;
  256. function repeat_statement : tnode;
  257. var
  258. first,last,p_e : tnode;
  259. begin
  260. consume(_REPEAT);
  261. first:=nil;
  262. last:=nil;
  263. while token<>_UNTIL do
  264. begin
  265. if first=nil then
  266. begin
  267. last:=cstatementnode.create(statement,nil);
  268. first:=last;
  269. end
  270. else
  271. begin
  272. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  273. last:=tstatementnode(last).right;
  274. end;
  275. if not try_to_consume(_SEMICOLON) then
  276. break;
  277. consume_emptystats;
  278. end;
  279. consume(_UNTIL);
  280. first:=cblocknode.create(first);
  281. p_e:=comp_expr([ef_accept_equal]);
  282. result:=cwhilerepeatnode.create(p_e,first,false,true);
  283. end;
  284. function while_statement : tnode;
  285. var
  286. p_e,p_a : tnode;
  287. begin
  288. consume(_WHILE);
  289. p_e:=comp_expr([ef_accept_equal]);
  290. consume(_DO);
  291. p_a:=statement;
  292. result:=cwhilerepeatnode.create(p_e,p_a,true,false);
  293. end;
  294. { a helper function which is used both by "with" and "for-in loop" nodes }
  295. function skip_nodes_before_load(p: tnode): tnode;
  296. begin
  297. { ignore nodes that don't add instructions in the tree }
  298. while assigned(p) and
  299. { equal type conversions }
  300. (
  301. (p.nodetype=typeconvn) and
  302. (ttypeconvnode(p).convtype=tc_equal)
  303. ) or
  304. { constant array index }
  305. (
  306. (p.nodetype=vecn) and
  307. (tvecnode(p).right.nodetype=ordconstn)
  308. ) do
  309. p:=tunarynode(p).left;
  310. result:=p;
  311. end;
  312. function for_statement : tnode;
  313. procedure check_range(hp:tnode; fordef: tdef);
  314. begin
  315. if (hp.nodetype=ordconstn) and
  316. (fordef.typ<>errordef) then
  317. testrange(fordef,tordconstnode(hp).value,false,true);
  318. end;
  319. function for_loop_create(hloopvar: tnode): tnode;
  320. var
  321. hp,
  322. hblock,
  323. hto,hfrom : tnode;
  324. backward : boolean;
  325. loopvarsym : tabstractvarsym;
  326. begin
  327. { Check loop variable }
  328. loopvarsym:=nil;
  329. { variable must be an ordinal, int64 is not allowed for 32bit targets }
  330. if (
  331. not(is_ordinal(hloopvar.resultdef))
  332. {$ifndef cpu64bitaddr}
  333. or is_64bitint(hloopvar.resultdef)
  334. {$endif not cpu64bitaddr}
  335. ) and
  336. (hloopvar.resultdef.typ<>undefineddef)
  337. then
  338. MessagePos(hloopvar.fileinfo,type_e_ordinal_expr_expected);
  339. hp:=hloopvar;
  340. while assigned(hp) and
  341. (
  342. { record/object fields and array elements are allowed }
  343. { in tp7 mode only }
  344. (
  345. (m_tp7 in current_settings.modeswitches) and
  346. (
  347. ((hp.nodetype=subscriptn) and
  348. ((tsubscriptnode(hp).left.resultdef.typ=recorddef) or
  349. is_object(tsubscriptnode(hp).left.resultdef))
  350. ) or
  351. { constant array index }
  352. (
  353. (hp.nodetype=vecn) and
  354. is_constintnode(tvecnode(hp).right)
  355. )
  356. )
  357. ) or
  358. { equal typeconversions }
  359. (
  360. (hp.nodetype=typeconvn) and
  361. (ttypeconvnode(hp).convtype=tc_equal)
  362. )
  363. ) do
  364. begin
  365. { Use the recordfield for loopvarsym }
  366. if not assigned(loopvarsym) and
  367. (hp.nodetype=subscriptn) then
  368. loopvarsym:=tsubscriptnode(hp).vs;
  369. hp:=tunarynode(hp).left;
  370. end;
  371. if assigned(hp) and
  372. (hp.nodetype=loadn) then
  373. begin
  374. case tloadnode(hp).symtableentry.typ of
  375. staticvarsym,
  376. localvarsym,
  377. paravarsym :
  378. begin
  379. { we need a simple loadn:
  380. 1. The load must be in a global symtable or
  381. in the same level as the para of the current proc.
  382. 2. value variables (no const,out or var)
  383. 3. No threadvar, readonly or typedconst
  384. }
  385. if (
  386. (tloadnode(hp).symtable.symtablelevel=main_program_level) or
  387. (tloadnode(hp).symtable.symtablelevel=current_procinfo.procdef.parast.symtablelevel)
  388. ) and
  389. (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_value) and
  390. ([vo_is_thread_var,vo_is_typed_const] * tabstractvarsym(tloadnode(hp).symtableentry).varoptions=[]) then
  391. begin
  392. { Assigning for-loop variable is only allowed in tp7 and macpas }
  393. if ([m_tp7,m_mac] * current_settings.modeswitches = []) then
  394. begin
  395. if not assigned(loopvarsym) then
  396. loopvarsym:=tabstractvarsym(tloadnode(hp).symtableentry);
  397. include(loopvarsym.varoptions,vo_is_loop_counter);
  398. end;
  399. end
  400. else
  401. begin
  402. { Typed const is allowed in tp7 }
  403. if not(m_tp7 in current_settings.modeswitches) or
  404. not(vo_is_typed_const in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  405. MessagePos(hp.fileinfo,type_e_illegal_count_var);
  406. end;
  407. end;
  408. else
  409. MessagePos(hp.fileinfo,type_e_illegal_count_var);
  410. end;
  411. end
  412. else
  413. MessagePos(hloopvar.fileinfo,type_e_illegal_count_var);
  414. hfrom:=comp_expr([ef_accept_equal]);
  415. if try_to_consume(_DOWNTO) then
  416. backward:=true
  417. else
  418. begin
  419. consume(_TO);
  420. backward:=false;
  421. end;
  422. hto:=comp_expr([ef_accept_equal]);
  423. consume(_DO);
  424. { Check if the constants fit in the range }
  425. check_range(hfrom,hloopvar.resultdef);
  426. check_range(hto,hloopvar.resultdef);
  427. { first set the varstate for from and to, so
  428. uses of loopvar in those expressions will also
  429. trigger a warning when it is not used yet. This
  430. needs to be done before the instruction block is
  431. parsed to have a valid hloopvar }
  432. typecheckpass(hfrom);
  433. set_varstate(hfrom,vs_read,[vsf_must_be_valid]);
  434. typecheckpass(hto);
  435. set_varstate(hto,vs_read,[vsf_must_be_valid]);
  436. typecheckpass(hloopvar);
  437. { in two steps, because vs_readwritten may turn on vsf_must_be_valid }
  438. { for some subnodes }
  439. set_varstate(hloopvar,vs_written,[]);
  440. set_varstate(hloopvar,vs_read,[vsf_must_be_valid]);
  441. { ... now the instruction block }
  442. hblock:=statement;
  443. { variable is not used for loop counter anymore }
  444. if assigned(loopvarsym) then
  445. exclude(loopvarsym.varoptions,vo_is_loop_counter);
  446. result:=cfornode.create(hloopvar,hfrom,hto,hblock,backward);
  447. end;
  448. function for_in_loop_create(hloopvar: tnode): tnode;
  449. var
  450. expr,hloopbody,hp: tnode;
  451. loopvarsym: tabstractvarsym;
  452. begin
  453. hp:=skip_nodes_before_load(hloopvar);
  454. if assigned(hp)and(hp.nodetype=loadn) then
  455. begin
  456. loopvarsym:=tabstractvarsym(tloadnode(hp).symtableentry);
  457. include(loopvarsym.varoptions,vo_is_loop_counter);
  458. end
  459. else
  460. loopvarsym:=nil;
  461. expr:=comp_expr([ef_accept_equal]);
  462. consume(_DO);
  463. set_varstate(hloopvar,vs_written,[]);
  464. set_varstate(hloopvar,vs_read,[vsf_must_be_valid]);
  465. hloopbody:=statement;
  466. if assigned(loopvarsym) then
  467. exclude(loopvarsym.varoptions,vo_is_loop_counter);
  468. result:=create_for_in_loop(hloopvar,hloopbody,expr);
  469. expr.free;
  470. end;
  471. var
  472. hloopvar: tnode;
  473. begin
  474. { parse loop header }
  475. consume(_FOR);
  476. hloopvar:=factor(false,[]);
  477. valid_for_loopvar(hloopvar,true);
  478. if try_to_consume(_ASSIGNMENT) then
  479. result:=for_loop_create(hloopvar)
  480. else if try_to_consume(_IN) then
  481. result:=for_in_loop_create(hloopvar)
  482. else
  483. begin
  484. consume(_ASSIGNMENT); // fail
  485. result:=cerrornode.create;
  486. end;
  487. end;
  488. function _with_statement : tnode;
  489. var
  490. p : tnode;
  491. i : longint;
  492. st : TSymtable;
  493. newblock : tblocknode;
  494. newstatement : tstatementnode;
  495. calltempnode,
  496. tempnode : ttempcreatenode;
  497. valuenode,
  498. hp,
  499. refnode : tnode;
  500. hdef : tdef;
  501. helperdef : tobjectdef;
  502. hasimplicitderef : boolean;
  503. withsymtablelist : TFPObjectList;
  504. procedure pushobjchild(withdef,obj:tobjectdef);
  505. var
  506. parenthelperdef : tobjectdef;
  507. begin
  508. if not assigned(obj) then
  509. exit;
  510. pushobjchild(withdef,obj.childof);
  511. { we need to look for helpers that were defined for the parent
  512. class as well }
  513. search_last_objectpascal_helper(obj,current_structdef,parenthelperdef);
  514. { push the symtables of the helper's parents in reverse order }
  515. if assigned(parenthelperdef) then
  516. pushobjchild(withdef,parenthelperdef.childof);
  517. { keep the original tobjectdef as owner, because that is used for
  518. visibility of the symtable }
  519. st:=twithsymtable.create(withdef,obj.symtable.SymList,refnode.getcopy);
  520. symtablestack.push(st);
  521. withsymtablelist.add(st);
  522. { push the symtable of the helper }
  523. if assigned(parenthelperdef) then
  524. begin
  525. st:=twithsymtable.create(withdef,parenthelperdef.symtable.SymList,refnode.getcopy);
  526. symtablestack.push(st);
  527. withsymtablelist.add(st);
  528. end;
  529. end;
  530. begin
  531. calltempnode:=nil;
  532. p:=comp_expr([ef_accept_equal]);
  533. do_typecheckpass(p);
  534. if (p.nodetype=vecn) and
  535. (nf_memseg in p.flags) then
  536. CGMessage(parser_e_no_with_for_variable_in_other_segments);
  537. { "with procvar" can never mean anything, so always try
  538. to call it in case it returns a record/object/... }
  539. maybe_call_procvar(p,false);
  540. if (p.resultdef.typ in [objectdef,recorddef,classrefdef]) or
  541. ((p.resultdef.typ=undefineddef) and (df_generic in current_procinfo.procdef.defoptions)) then
  542. begin
  543. newblock:=nil;
  544. valuenode:=nil;
  545. tempnode:=nil;
  546. hp:=skip_nodes_before_load(p);
  547. if (hp.nodetype=loadn) and
  548. (
  549. (tloadnode(hp).symtable=current_procinfo.procdef.localst) or
  550. (tloadnode(hp).symtable=current_procinfo.procdef.parast) or
  551. (tloadnode(hp).symtable.symtabletype in [staticsymtable,globalsymtable])
  552. ) and
  553. { MacPas objects are mapped to classes, and the MacPas compilers
  554. interpret with-statements with MacPas objects the same way
  555. as records (the object referenced by the with-statement
  556. must remain constant)
  557. }
  558. not(is_class(hp.resultdef) and
  559. (m_mac in current_settings.modeswitches)) then
  560. begin
  561. { simple load, we can reference direct }
  562. refnode:=p;
  563. end
  564. else
  565. begin
  566. { complex load, load in temp first }
  567. newblock:=internalstatements(newstatement);
  568. { when we can't take the address of p, load it in a temp }
  569. { since we may need its address later on }
  570. if not valid_for_addr(p,false) then
  571. begin
  572. calltempnode:=ctempcreatenode.create(p.resultdef,p.resultdef.size,tt_persistent,true);
  573. addstatement(newstatement,calltempnode);
  574. addstatement(newstatement,cassignmentnode.create(
  575. ctemprefnode.create(calltempnode),
  576. p));
  577. p:=ctemprefnode.create(calltempnode);
  578. typecheckpass(p);
  579. end;
  580. { several object types have implicit dereferencing }
  581. { is_implicit_pointer_object_type() returns true for records
  582. on the JVM target because they are implemented as classes
  583. there, but we definitely have to take their address here
  584. since otherwise a deep copy is made and changes are made to
  585. this copy rather than to the original one }
  586. hasimplicitderef:=
  587. (is_implicit_pointer_object_type(p.resultdef) or
  588. (p.resultdef.typ=classrefdef)) and
  589. not((target_info.system in systems_jvm) and
  590. ((p.resultdef.typ=recorddef) or
  591. is_object(p.resultdef)));
  592. if hasimplicitderef then
  593. hdef:=p.resultdef
  594. else
  595. hdef:=cpointerdef.create(p.resultdef);
  596. { load address of the value in a temp }
  597. tempnode:=ctempcreatenode.create_withnode(hdef,sizeof(pint),tt_persistent,true,p);
  598. typecheckpass(tnode(tempnode));
  599. valuenode:=p;
  600. refnode:=ctemprefnode.create(tempnode);
  601. fillchar(refnode.fileinfo,sizeof(tfileposinfo),0);
  602. { add address call for valuenode and deref for refnode if this
  603. is not done implicitly }
  604. if not hasimplicitderef then
  605. begin
  606. valuenode:=caddrnode.create_internal_nomark(valuenode);
  607. include(taddrnode(valuenode).addrnodeflags,anf_typedaddr);
  608. refnode:=cderefnode.create(refnode);
  609. fillchar(refnode.fileinfo,sizeof(tfileposinfo),0);
  610. end;
  611. addstatement(newstatement,tempnode);
  612. addstatement(newstatement,cassignmentnode.create(
  613. ctemprefnode.create(tempnode),
  614. valuenode));
  615. typecheckpass(refnode);
  616. end;
  617. { Note: the symtable of the helper is pushed after the following
  618. "case", the symtables of the helper's parents are passed in
  619. the "case" branches }
  620. withsymtablelist:=TFPObjectList.create(true);
  621. case p.resultdef.typ of
  622. objectdef :
  623. begin
  624. { do we have a helper for this type? }
  625. search_last_objectpascal_helper(tabstractrecorddef(p.resultdef),current_structdef,helperdef);
  626. { push symtables of all parents in reverse order }
  627. pushobjchild(tobjectdef(p.resultdef),tobjectdef(p.resultdef).childof);
  628. { push symtables of all parents of the helper in reverse order }
  629. if assigned(helperdef) then
  630. pushobjchild(helperdef,helperdef.childof);
  631. { push object symtable }
  632. st:=twithsymtable.Create(tobjectdef(p.resultdef),tobjectdef(p.resultdef).symtable.SymList,refnode);
  633. symtablestack.push(st);
  634. withsymtablelist.add(st);
  635. end;
  636. classrefdef :
  637. begin
  638. { do we have a helper for this type? }
  639. search_last_objectpascal_helper(tobjectdef(tclassrefdef(p.resultdef).pointeddef),current_structdef,helperdef);
  640. { push symtables of all parents in reverse order }
  641. pushobjchild(tobjectdef(tclassrefdef(p.resultdef).pointeddef),tobjectdef(tclassrefdef(p.resultdef).pointeddef).childof);
  642. { push symtables of all parents of the helper in reverse order }
  643. if assigned(helperdef) then
  644. pushobjchild(helperdef,helperdef.childof);
  645. { push object symtable }
  646. st:=twithsymtable.Create(tobjectdef(tclassrefdef(p.resultdef).pointeddef),tobjectdef(tclassrefdef(p.resultdef).pointeddef).symtable.SymList,refnode);
  647. symtablestack.push(st);
  648. withsymtablelist.add(st);
  649. end;
  650. recorddef :
  651. begin
  652. { do we have a helper for this type? }
  653. search_last_objectpascal_helper(tabstractrecorddef(p.resultdef),current_structdef,helperdef);
  654. { push symtables of all parents of the helper in reverse order }
  655. if assigned(helperdef) then
  656. pushobjchild(helperdef,helperdef.childof);
  657. { push record symtable }
  658. st:=twithsymtable.create(trecorddef(p.resultdef),trecorddef(p.resultdef).symtable.SymList,refnode);
  659. symtablestack.push(st);
  660. withsymtablelist.add(st);
  661. end;
  662. undefineddef :
  663. begin
  664. if not(df_generic in current_procinfo.procdef.defoptions) then
  665. internalerror(2012122802);
  666. helperdef:=nil;
  667. { push record symtable }
  668. st:=twithsymtable.create(p.resultdef,nil,refnode);
  669. symtablestack.push(st);
  670. withsymtablelist.add(st);
  671. end;
  672. else
  673. internalerror(200601271);
  674. end;
  675. { push helper symtable }
  676. if assigned(helperdef) then
  677. begin
  678. st:=twithsymtable.Create(helperdef,helperdef.symtable.SymList,refnode.getcopy);
  679. symtablestack.push(st);
  680. withsymtablelist.add(st);
  681. end;
  682. if try_to_consume(_COMMA) then
  683. p:=_with_statement()
  684. else
  685. begin
  686. consume(_DO);
  687. if token<>_SEMICOLON then
  688. p:=statement
  689. else
  690. p:=cnothingnode.create;
  691. end;
  692. { remove symtables in reverse order from the stack }
  693. for i:=withsymtablelist.count-1 downto 0 do
  694. symtablestack.pop(TSymtable(withsymtablelist[i]));
  695. withsymtablelist.free;
  696. // p:=cwithnode.create(right,twithsymtable(withsymtable),levelcount,refnode);
  697. { Finalize complex withnode with destroy of temp }
  698. if assigned(newblock) then
  699. begin
  700. addstatement(newstatement,p);
  701. if assigned(tempnode) then
  702. addstatement(newstatement,ctempdeletenode.create(tempnode));
  703. if assigned(calltempnode) then
  704. addstatement(newstatement,ctempdeletenode.create(calltempnode));
  705. p:=newblock;
  706. end;
  707. result:=p;
  708. end
  709. else
  710. begin
  711. p.free;
  712. Message1(parser_e_false_with_expr,p.resultdef.GetTypeName);
  713. { try to recover from error }
  714. if try_to_consume(_COMMA) then
  715. begin
  716. hp:=_with_statement();
  717. if (hp=nil) then; { remove warning about unused }
  718. end
  719. else
  720. begin
  721. consume(_DO);
  722. { ignore all }
  723. if token<>_SEMICOLON then
  724. statement;
  725. end;
  726. result:=nil;
  727. end;
  728. end;
  729. function with_statement : tnode;
  730. begin
  731. consume(_WITH);
  732. with_statement:=_with_statement();
  733. end;
  734. function raise_statement : tnode;
  735. var
  736. p,pobj,paddr,pframe : tnode;
  737. begin
  738. pobj:=nil;
  739. paddr:=nil;
  740. pframe:=nil;
  741. consume(_RAISE);
  742. if not(token in endtokens) then
  743. begin
  744. { object }
  745. pobj:=comp_expr([ef_accept_equal]);
  746. if try_to_consume(_AT) then
  747. begin
  748. paddr:=comp_expr([ef_accept_equal]);
  749. if try_to_consume(_COMMA) then
  750. pframe:=comp_expr([ef_accept_equal]);
  751. end;
  752. end
  753. else
  754. begin
  755. if (block_type<>bt_except) then
  756. Message(parser_e_no_reraise_possible);
  757. end;
  758. p:=craisenode.create(pobj,paddr,pframe);
  759. raise_statement:=p;
  760. end;
  761. function try_statement : tnode;
  762. procedure check_type_valid(var def: tdef);
  763. begin
  764. if not (is_class(def) or is_javaclass(def) or
  765. { skip showing error message the second time }
  766. (def.typ=errordef)) then
  767. begin
  768. Message1(type_e_class_type_expected,def.typename);
  769. def:=generrordef;
  770. end;
  771. end;
  772. var
  773. p_try_block,p_finally_block,first,last,
  774. p_default,p_specific,hp : tnode;
  775. ot : tDef;
  776. sym : tlocalvarsym;
  777. old_block_type : tblock_type;
  778. excepTSymtable : TSymtable;
  779. objname,objrealname : TIDString;
  780. srsym : tsym;
  781. srsymtable : TSymtable;
  782. t:ttoken;
  783. unit_found:boolean;
  784. oldcurrent_exceptblock: integer;
  785. begin
  786. p_default:=nil;
  787. p_specific:=nil;
  788. excepTSymtable:=nil;
  789. last:=nil;
  790. { read statements to try }
  791. consume(_TRY);
  792. first:=nil;
  793. inc(exceptblockcounter);
  794. oldcurrent_exceptblock := current_exceptblock;
  795. current_exceptblock := exceptblockcounter;
  796. old_block_type := block_type;
  797. block_type := bt_body;
  798. while (token<>_FINALLY) and (token<>_EXCEPT) do
  799. begin
  800. if first=nil then
  801. begin
  802. last:=cstatementnode.create(statement,nil);
  803. first:=last;
  804. end
  805. else
  806. begin
  807. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  808. last:=tstatementnode(last).right;
  809. end;
  810. if not try_to_consume(_SEMICOLON) then
  811. break;
  812. consume_emptystats;
  813. end;
  814. p_try_block:=cblocknode.create(first);
  815. if try_to_consume(_FINALLY) then
  816. begin
  817. inc(exceptblockcounter);
  818. current_exceptblock := exceptblockcounter;
  819. p_finally_block:=statements_til_end;
  820. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  821. end
  822. else
  823. begin
  824. consume(_EXCEPT);
  825. block_type:=bt_except;
  826. inc(exceptblockcounter);
  827. current_exceptblock := exceptblockcounter;
  828. ot:=generrordef;
  829. p_specific:=nil;
  830. if (idtoken=_ON) then
  831. { catch specific exceptions }
  832. begin
  833. repeat
  834. consume(_ON);
  835. if token=_ID then
  836. begin
  837. objname:=pattern;
  838. objrealname:=orgpattern;
  839. { can't use consume_sym here, because we need already
  840. to check for the colon }
  841. searchsym(objname,srsym,srsymtable);
  842. consume(_ID);
  843. { is a explicit name for the exception given ? }
  844. if try_to_consume(_COLON) then
  845. begin
  846. single_type(ot,[]);
  847. check_type_valid(ot);
  848. sym:=clocalvarsym.create(objrealname,vs_value,ot,[],true);
  849. end
  850. else
  851. begin
  852. { check if type is valid, must be done here because
  853. with "e: Exception" the e is not necessary }
  854. { support unit.identifier }
  855. unit_found:=try_consume_unitsym_no_specialize(srsym,srsymtable,t,false,objname);
  856. if srsym=nil then
  857. begin
  858. identifier_not_found(orgpattern);
  859. srsym:=generrorsym;
  860. end;
  861. if unit_found then
  862. consume(t);
  863. { check if type is valid, must be done here because
  864. with "e: Exception" the e is not necessary }
  865. if (srsym.typ=typesym) then
  866. begin
  867. ot:=ttypesym(srsym).typedef;
  868. parse_nested_types(ot,false,false,nil);
  869. check_type_valid(ot);
  870. end
  871. else
  872. begin
  873. Message(type_e_type_id_expected);
  874. ot:=generrordef;
  875. end;
  876. { create dummy symbol so we don't need a special
  877. case in ncgflw, and so that we always know the
  878. type }
  879. sym:=clocalvarsym.create('$exceptsym',vs_value,ot,[],true);
  880. end;
  881. excepTSymtable:=tstt_excepTSymtable.create;
  882. excepTSymtable.insert(sym);
  883. symtablestack.push(excepTSymtable);
  884. end
  885. else
  886. consume(_ID);
  887. consume(_DO);
  888. hp:=connode.create(nil,statement);
  889. if ot.typ=errordef then
  890. begin
  891. hp.free;
  892. hp:=cerrornode.create;
  893. end;
  894. if p_specific=nil then
  895. begin
  896. last:=hp;
  897. p_specific:=last;
  898. end
  899. else
  900. begin
  901. tonnode(last).left:=hp;
  902. last:=tonnode(last).left;
  903. end;
  904. { set the informations }
  905. { only if the creation of the onnode was succesful, it's possible }
  906. { that last and hp are errornodes (JM) }
  907. if last.nodetype = onn then
  908. begin
  909. tonnode(last).excepttype:=tobjectdef(ot);
  910. tonnode(last).excepTSymtable:=excepTSymtable;
  911. end;
  912. { remove exception symtable }
  913. if assigned(excepTSymtable) then
  914. begin
  915. symtablestack.pop(excepTSymtable);
  916. if last.nodetype <> onn then
  917. excepTSymtable.free;
  918. end;
  919. if not try_to_consume(_SEMICOLON) then
  920. break;
  921. consume_emptystats;
  922. until (token in [_END,_ELSE]);
  923. if try_to_consume(_ELSE) then
  924. begin
  925. { catch the other exceptions }
  926. p_default:=statements_til_end;
  927. end
  928. else
  929. consume(_END);
  930. end
  931. else
  932. begin
  933. { catch all exceptions }
  934. p_default:=statements_til_end;
  935. end;
  936. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  937. end;
  938. block_type:=old_block_type;
  939. current_exceptblock := oldcurrent_exceptblock;
  940. end;
  941. function _asm_statement : tnode;
  942. var
  943. asmstat : tasmnode;
  944. reg : tregister;
  945. asmreader : tbaseasmreader;
  946. entrypos : tfileposinfo;
  947. hl : TAsmList;
  948. begin
  949. Inside_asm_statement:=true;
  950. asmstat:=nil;
  951. hl:=nil;
  952. if assigned(asmmodeinfos[current_settings.asmmode]) then
  953. begin
  954. asmreader:=asmmodeinfos[current_settings.asmmode]^.casmreader.create;
  955. entrypos:=current_filepos;
  956. hl:=asmreader.assemble as TAsmList;
  957. if (not hl.empty) then
  958. begin
  959. { mark boundaries of assembler block, this is necessary for optimizer }
  960. hl.insert(tai_marker.create(mark_asmblockstart));
  961. hl.concat(tai_marker.create(mark_asmblockend));
  962. end;
  963. asmstat:=casmnode.create(hl);
  964. asmstat.fileinfo:=entrypos;
  965. asmreader.free;
  966. end
  967. else
  968. Message(parser_f_assembler_reader_not_supported);
  969. { Mark procedure that it has assembler blocks }
  970. include(current_procinfo.flags,pi_has_assembler_block);
  971. { Read first the _ASM statement }
  972. consume(_ASM);
  973. { Force an empty register list for pure assembler routines,
  974. so that pass2 won't allocate volatile registers for them. }
  975. asmstat.has_registerlist:=(po_assembler in current_procinfo.procdef.procoptions);
  976. { END is read, got a list of changed registers? }
  977. if try_to_consume(_LECKKLAMMER) then
  978. begin
  979. if token<>_RECKKLAMMER then
  980. begin
  981. if po_assembler in current_procinfo.procdef.procoptions then
  982. Message(parser_w_register_list_ignored);
  983. repeat
  984. { it's possible to specify the modified registers }
  985. reg:=std_regnum_search(lower(cstringpattern));
  986. if reg<>NR_NO then
  987. begin
  988. if not(po_assembler in current_procinfo.procdef.procoptions) and assigned(hl) then
  989. begin
  990. hl.Insert(tai_regalloc.alloc(reg,nil));
  991. hl.Insert(tai_regalloc.markused(reg));
  992. hl.Concat(tai_regalloc.dealloc(reg,nil));
  993. end;
  994. end
  995. else
  996. Message(asmr_e_invalid_register);
  997. consume(_CSTRING);
  998. if not try_to_consume(_COMMA) then
  999. break;
  1000. until false;
  1001. asmstat.has_registerlist:=true;
  1002. end;
  1003. consume(_RECKKLAMMER);
  1004. end;
  1005. Inside_asm_statement:=false;
  1006. _asm_statement:=asmstat;
  1007. end;
  1008. function statement : tnode;
  1009. var
  1010. p,
  1011. code : tnode;
  1012. filepos : tfileposinfo;
  1013. srsym : tsym;
  1014. srsymtable : TSymtable;
  1015. s : TIDString;
  1016. begin
  1017. filepos:=current_tokenpos;
  1018. code:=nil;
  1019. case token of
  1020. _GOTO :
  1021. begin
  1022. if not(cs_support_goto in current_settings.moduleswitches) then
  1023. Message(sym_e_goto_and_label_not_supported);
  1024. consume(_GOTO);
  1025. if (token<>_INTCONST) and (token<>_ID) then
  1026. begin
  1027. Message(sym_e_label_not_found);
  1028. code:=cerrornode.create;
  1029. end
  1030. else
  1031. begin
  1032. if token=_ID then
  1033. consume_sym(srsym,srsymtable)
  1034. else
  1035. begin
  1036. if token<>_INTCONST then
  1037. internalerror(201008021);
  1038. { strip leading 0's in iso mode }
  1039. if (([m_iso,m_extpas]*current_settings.modeswitches)<>[]) then
  1040. while pattern[1]='0' do
  1041. delete(pattern,1,1);
  1042. searchsym(pattern,srsym,srsymtable);
  1043. if srsym=nil then
  1044. begin
  1045. identifier_not_found(pattern);
  1046. srsym:=generrorsym;
  1047. srsymtable:=nil;
  1048. end;
  1049. consume(token);
  1050. end;
  1051. if srsym.typ<>labelsym then
  1052. begin
  1053. Message(sym_e_id_is_no_label_id);
  1054. code:=cerrornode.create;
  1055. end
  1056. else
  1057. begin
  1058. { goto outside the current scope? }
  1059. if srsym.owner<>current_procinfo.procdef.localst then
  1060. begin
  1061. { allowed? }
  1062. if not(m_non_local_goto in current_settings.modeswitches) then
  1063. Message(parser_e_goto_outside_proc);
  1064. include(current_procinfo.flags,pi_has_global_goto);
  1065. end;
  1066. code:=cgotonode.create(tlabelsym(srsym));
  1067. tgotonode(code).labelsym:=tlabelsym(srsym);
  1068. { set flag that this label is used }
  1069. tlabelsym(srsym).used:=true;
  1070. end;
  1071. end;
  1072. end;
  1073. _BEGIN :
  1074. code:=statement_block(_BEGIN);
  1075. _IF :
  1076. code:=if_statement;
  1077. _CASE :
  1078. code:=case_statement;
  1079. _REPEAT :
  1080. code:=repeat_statement;
  1081. _WHILE :
  1082. code:=while_statement;
  1083. _FOR :
  1084. code:=for_statement;
  1085. _WITH :
  1086. code:=with_statement;
  1087. _TRY :
  1088. code:=try_statement;
  1089. _RAISE :
  1090. code:=raise_statement;
  1091. { semicolons,else until and end are ignored }
  1092. _SEMICOLON,
  1093. _ELSE,
  1094. _UNTIL,
  1095. _END:
  1096. code:=cnothingnode.create;
  1097. _FAIL :
  1098. begin
  1099. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  1100. Message(parser_e_fail_only_in_constructor);
  1101. consume(_FAIL);
  1102. code:=cnodeutils.call_fail_node;
  1103. end;
  1104. _ASM :
  1105. begin
  1106. if parse_generic then
  1107. Message(parser_e_no_assembler_in_generic);
  1108. code:=_asm_statement;
  1109. end;
  1110. _PLUS:
  1111. begin
  1112. Message(parser_e_syntax_error);
  1113. consume(_PLUS);
  1114. end;
  1115. _EOF :
  1116. Message(scan_f_end_of_file);
  1117. else
  1118. begin
  1119. { don't typecheck yet, because that will also simplify, which may
  1120. result in not detecting certain kinds of syntax errors --
  1121. see mantis #15594 }
  1122. p:=expr(false);
  1123. { save the pattern here for latter usage, the label could be "000",
  1124. even if we read an expression, the pattern is still valid if it's really
  1125. a label (FK)
  1126. if you want to mess here, take care of
  1127. tests/webtbs/tw3546.pp
  1128. }
  1129. s:=pattern;
  1130. { When a colon follows a intconst then transform it into a label }
  1131. if (p.nodetype=ordconstn) and
  1132. try_to_consume(_COLON) then
  1133. begin
  1134. { in iso mode, 0003: is equal to 3: }
  1135. if (([m_iso,m_extpas]*current_settings.modeswitches)<>[]) then
  1136. searchsym(tostr(tordconstnode(p).value),srsym,srsymtable)
  1137. else
  1138. searchsym(s,srsym,srsymtable);
  1139. p.free;
  1140. if assigned(srsym) and
  1141. (srsym.typ=labelsym) then
  1142. begin
  1143. if tlabelsym(srsym).defined then
  1144. Message(sym_e_label_already_defined);
  1145. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1146. begin
  1147. tlabelsym(srsym).nonlocal:=true;
  1148. exclude(current_procinfo.procdef.procoptions,po_inline);
  1149. end;
  1150. if tlabelsym(srsym).nonlocal and
  1151. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1152. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  1153. tlabelsym(srsym).defined:=true;
  1154. p:=clabelnode.create(nil,tlabelsym(srsym));
  1155. tlabelsym(srsym).code:=p;
  1156. end
  1157. else
  1158. begin
  1159. Message1(sym_e_label_used_and_not_defined,s);
  1160. p:=cnothingnode.create;
  1161. end;
  1162. end;
  1163. if p.nodetype=labeln then
  1164. begin
  1165. { the pointer to the following instruction }
  1166. { isn't a very clean way }
  1167. if token in endtokens then
  1168. tlabelnode(p).left:=cnothingnode.create
  1169. else
  1170. tlabelnode(p).left:=statement();
  1171. { be sure to have left also typecheckpass }
  1172. typecheckpass(tlabelnode(p).left);
  1173. end
  1174. else
  1175. { change a load of a procvar to a call. this is also
  1176. supported in fpc mode }
  1177. if p.nodetype in [vecn,derefn,typeconvn,subscriptn,loadn] then
  1178. maybe_call_procvar(p,false);
  1179. { blockn support because a read/write is changed into a blocknode
  1180. with a separate statement for each read/write operation (JM)
  1181. the same is true for val() if the third parameter is not 32 bit
  1182. goto nodes are created by the compiler for non local exit statements, so
  1183. include them as well
  1184. }
  1185. if not(p.nodetype in [nothingn,errorn,calln,ifn,assignn,breakn,inlinen,
  1186. continuen,labeln,blockn,exitn,goton]) or
  1187. ((p.nodetype=inlinen) and
  1188. not is_void(p.resultdef)) or
  1189. ((p.nodetype=calln) and
  1190. (assigned(tcallnode(p).procdefinition)) and
  1191. (tcallnode(p).procdefinition.proctypeoption=potype_operator)) then
  1192. Message(parser_e_illegal_expression);
  1193. if not assigned(p.resultdef) then
  1194. do_typecheckpass(p);
  1195. { Specify that we don't use the value returned by the call.
  1196. This is used for :
  1197. - dispose of temp stack space
  1198. - dispose on FPU stack
  1199. - extended syntax checking }
  1200. if (p.nodetype=calln) then
  1201. begin
  1202. exclude(tcallnode(p).callnodeflags,cnf_return_value_used);
  1203. { in $x- state, the function result must not be ignored }
  1204. if not(cs_extsyntax in current_settings.moduleswitches) and
  1205. not(is_void(p.resultdef)) and
  1206. { can be nil in case there was an error in the expression }
  1207. assigned(tcallnode(p).procdefinition) and
  1208. { allow constructor calls to drop the result if they are
  1209. called as instance methods instead of class methods }
  1210. not(
  1211. (tcallnode(p).procdefinition.proctypeoption=potype_constructor) and
  1212. is_class_or_object(tprocdef(tcallnode(p).procdefinition).struct) and
  1213. assigned(tcallnode(p).methodpointer) and
  1214. (tnode(tcallnode(p).methodpointer).resultdef.typ=objectdef)
  1215. ) then
  1216. Message(parser_e_illegal_expression);
  1217. end;
  1218. code:=p;
  1219. end;
  1220. end;
  1221. if assigned(code) then
  1222. begin
  1223. typecheckpass(code);
  1224. code.fileinfo:=filepos;
  1225. end;
  1226. statement:=code;
  1227. end;
  1228. function statement_block(starttoken : ttoken) : tnode;
  1229. var
  1230. first,last : tnode;
  1231. filepos : tfileposinfo;
  1232. begin
  1233. first:=nil;
  1234. last:=nil;
  1235. filepos:=current_tokenpos;
  1236. consume(starttoken);
  1237. while not((token=_END) or (token=_FINALIZATION)) do
  1238. begin
  1239. if first=nil then
  1240. begin
  1241. last:=cstatementnode.create(statement,nil);
  1242. first:=last;
  1243. end
  1244. else
  1245. begin
  1246. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  1247. last:=tstatementnode(last).right;
  1248. end;
  1249. if ((token=_END) or (token=_FINALIZATION)) then
  1250. break
  1251. else
  1252. begin
  1253. { if no semicolon, then error and go on }
  1254. if token<>_SEMICOLON then
  1255. begin
  1256. consume(_SEMICOLON);
  1257. consume_all_until(_SEMICOLON);
  1258. end;
  1259. consume(_SEMICOLON);
  1260. end;
  1261. consume_emptystats;
  1262. end;
  1263. { don't consume the finalization token, it is consumed when
  1264. reading the finalization block, but allow it only after
  1265. an initalization ! }
  1266. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1267. consume(_END);
  1268. last:=cblocknode.create(first);
  1269. last.fileinfo:=filepos;
  1270. statement_block:=last;
  1271. end;
  1272. function assembler_block : tnode;
  1273. var
  1274. p : tnode;
  1275. {$if not(defined(sparcgen)) and not(defined(arm)) and not(defined(avr)) and not(defined(mips))}
  1276. locals : longint;
  1277. {$endif not(defined(sparcgen)) and not(defined(arm)) and not(defined(avr)) and not(defined(mips))}
  1278. srsym : tsym;
  1279. begin
  1280. if parse_generic then
  1281. message(parser_e_no_assembler_in_generic);
  1282. { Rename the funcret so that recursive calls are possible }
  1283. if not is_void(current_procinfo.procdef.returndef) then
  1284. begin
  1285. srsym:=TSym(current_procinfo.procdef.localst.Find(current_procinfo.procdef.procsym.name));
  1286. if assigned(srsym) then
  1287. srsym.realname:='$hiddenresult';
  1288. end;
  1289. { delphi uses register calling for assembler methods }
  1290. if (m_delphi in current_settings.modeswitches) and
  1291. (po_assembler in current_procinfo.procdef.procoptions) and
  1292. not(po_hascallingconvention in current_procinfo.procdef.procoptions) then
  1293. current_procinfo.procdef.proccalloption:=pocall_register;
  1294. { force the asm statement }
  1295. if token<>_ASM then
  1296. consume(_ASM);
  1297. include(current_procinfo.flags,pi_is_assembler);
  1298. p:=_asm_statement;
  1299. {$if not(defined(sparcgen)) and not(defined(arm)) and not(defined(avr)) and not(defined(mips))}
  1300. if (po_assembler in current_procinfo.procdef.procoptions) then
  1301. begin
  1302. { set the framepointer to esp for assembler functions when the
  1303. following conditions are met:
  1304. - if the are no local variables and parameters (except the allocated result)
  1305. - no reference to the result variable (refcount<=1)
  1306. - result is not stored as parameter
  1307. - target processor has optional frame pointer save
  1308. (vm, i386, vm only currently)
  1309. }
  1310. locals:=tabstractlocalsymtable(current_procinfo.procdef.parast).count_locals;
  1311. if (current_procinfo.procdef.localst.symtabletype=localsymtable) then
  1312. inc(locals,tabstractlocalsymtable(current_procinfo.procdef.localst).count_locals);
  1313. if (locals=0) and
  1314. not (current_procinfo.procdef.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  1315. (not assigned(current_procinfo.procdef.funcretsym) or
  1316. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs<=1)) and
  1317. not (df_generic in current_procinfo.procdef.defoptions) and
  1318. not(paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef)) then
  1319. begin
  1320. { Only need to set the framepointer, the locals will
  1321. be inserted with the correct reference in tcgasmnode.pass_generate_code }
  1322. current_procinfo.framepointer:=NR_STACK_POINTER_REG;
  1323. end;
  1324. end;
  1325. {$endif not(defined(sparcgen)) and not(defined(arm)) and not(defined(avr)) not(defined(mipsel))}
  1326. { Flag the result as assigned when it is returned in a
  1327. register.
  1328. }
  1329. if assigned(current_procinfo.procdef.funcretsym) and
  1330. not (df_generic in current_procinfo.procdef.defoptions) and
  1331. (not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef)) then
  1332. tabstractvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_initialised;
  1333. { because the END is already read we need to get the
  1334. last_endtoken_filepos here (PFV) }
  1335. last_endtoken_filepos:=current_tokenpos;
  1336. assembler_block:=p;
  1337. end;
  1338. end.