2
0

pstatmnt.pas 56 KB

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