pstatmnt.pas 57 KB

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