pstatmnt.pas 56 KB

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