pstatmnt.pas 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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. {$ifndef cpu64bitaddr}
  337. or is_64bitint(hloopvar.resultdef)
  338. {$endif not cpu64bitaddr}
  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.insert(sym);
  897. symtablestack.push(excepTSymtable);
  898. end
  899. else
  900. consume(_ID);
  901. consume(_DO);
  902. hp:=connode.create(nil,statement);
  903. if ot.typ=errordef then
  904. begin
  905. hp.free;
  906. hp:=cerrornode.create;
  907. end;
  908. if p_specific=nil then
  909. begin
  910. last:=hp;
  911. p_specific:=last;
  912. end
  913. else
  914. begin
  915. tonnode(last).left:=hp;
  916. last:=tonnode(last).left;
  917. end;
  918. { set the informations }
  919. { only if the creation of the onnode was succesful, it's possible }
  920. { that last and hp are errornodes (JM) }
  921. if last.nodetype = onn then
  922. begin
  923. tonnode(last).excepttype:=tobjectdef(ot);
  924. tonnode(last).excepTSymtable:=excepTSymtable;
  925. end;
  926. { remove exception symtable }
  927. if assigned(excepTSymtable) then
  928. begin
  929. symtablestack.pop(excepTSymtable);
  930. if last.nodetype <> onn then
  931. excepTSymtable.free;
  932. end;
  933. if not try_to_consume(_SEMICOLON) then
  934. break;
  935. consume_emptystats;
  936. until (token in [_END,_ELSE]);
  937. if try_to_consume(_ELSE) then
  938. begin
  939. { catch the other exceptions }
  940. p_default:=statements_til_end;
  941. end
  942. else
  943. consume(_END);
  944. end
  945. else
  946. begin
  947. { catch all exceptions }
  948. p_default:=statements_til_end;
  949. end;
  950. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  951. end;
  952. block_type:=old_block_type;
  953. current_exceptblock := oldcurrent_exceptblock;
  954. end;
  955. function _asm_statement : tnode;
  956. var
  957. asmstat : tasmnode;
  958. reg : tregister;
  959. asmreader : tbaseasmreader;
  960. entrypos : tfileposinfo;
  961. hl : TAsmList;
  962. begin
  963. Inside_asm_statement:=true;
  964. asmstat:=nil;
  965. hl:=nil;
  966. if assigned(asmmodeinfos[current_settings.asmmode]) then
  967. begin
  968. asmreader:=asmmodeinfos[current_settings.asmmode]^.casmreader.create;
  969. entrypos:=current_filepos;
  970. hl:=asmreader.assemble as TAsmList;
  971. if (not hl.empty) then
  972. begin
  973. { mark boundaries of assembler block, this is necessary for optimizer }
  974. hl.insert(tai_marker.create(mark_asmblockstart));
  975. hl.concat(tai_marker.create(mark_asmblockend));
  976. end;
  977. asmstat:=casmnode.create(hl);
  978. asmstat.fileinfo:=entrypos;
  979. asmreader.free;
  980. end
  981. else
  982. Message(parser_f_assembler_reader_not_supported);
  983. { Mark procedure that it has assembler blocks }
  984. include(current_procinfo.flags,pi_has_assembler_block);
  985. { Read first the _ASM statement }
  986. consume(_ASM);
  987. { Force an empty register list for pure assembler routines,
  988. so that pass2 won't allocate volatile registers for them. }
  989. asmstat.has_registerlist:=(po_assembler in current_procinfo.procdef.procoptions);
  990. { END is read, got a list of changed registers? }
  991. if try_to_consume(_LECKKLAMMER) then
  992. begin
  993. if token<>_RECKKLAMMER then
  994. begin
  995. if po_assembler in current_procinfo.procdef.procoptions then
  996. Message(parser_w_register_list_ignored);
  997. repeat
  998. { it's possible to specify the modified registers }
  999. if token=_CSTRING then
  1000. reg:=std_regnum_search(lower(cstringpattern))
  1001. else if token=_CCHAR then
  1002. reg:=std_regnum_search(lower(pattern))
  1003. else
  1004. reg:=NR_NO;
  1005. if reg<>NR_NO then
  1006. begin
  1007. if not(po_assembler in current_procinfo.procdef.procoptions) and assigned(hl) then
  1008. begin
  1009. hl.Insert(tai_regalloc.alloc(reg,nil));
  1010. hl.Insert(tai_regalloc.markused(reg));
  1011. hl.Concat(tai_regalloc.dealloc(reg,nil));
  1012. end;
  1013. end
  1014. else
  1015. Message(asmr_e_invalid_register);
  1016. if token=_CCHAR then
  1017. consume(_CCHAR)
  1018. else
  1019. consume(_CSTRING);
  1020. if not try_to_consume(_COMMA) then
  1021. break;
  1022. until false;
  1023. asmstat.has_registerlist:=true;
  1024. end;
  1025. consume(_RECKKLAMMER);
  1026. end;
  1027. Inside_asm_statement:=false;
  1028. _asm_statement:=asmstat;
  1029. end;
  1030. function statement : tnode;
  1031. var
  1032. p,
  1033. code : tnode;
  1034. filepos : tfileposinfo;
  1035. srsym : tsym;
  1036. srsymtable : TSymtable;
  1037. s : TIDString;
  1038. begin
  1039. filepos:=current_tokenpos;
  1040. code:=nil;
  1041. case token of
  1042. _GOTO :
  1043. begin
  1044. if not(cs_support_goto in current_settings.moduleswitches) then
  1045. Message(sym_e_goto_and_label_not_supported);
  1046. consume(_GOTO);
  1047. if (token<>_INTCONST) and (token<>_ID) then
  1048. begin
  1049. Message(sym_e_label_not_found);
  1050. code:=cerrornode.create;
  1051. end
  1052. else
  1053. begin
  1054. if token=_ID then
  1055. consume_sym(srsym,srsymtable)
  1056. else
  1057. begin
  1058. if token<>_INTCONST then
  1059. internalerror(201008021);
  1060. { strip leading 0's in iso mode }
  1061. if (([m_iso,m_extpas]*current_settings.modeswitches)<>[]) then
  1062. while (length(pattern)>1) and (pattern[1]='0') do
  1063. delete(pattern,1,1);
  1064. searchsym(pattern,srsym,srsymtable);
  1065. if srsym=nil then
  1066. begin
  1067. identifier_not_found(pattern);
  1068. srsym:=generrorsym;
  1069. srsymtable:=nil;
  1070. end;
  1071. consume(token);
  1072. end;
  1073. if srsym.typ<>labelsym then
  1074. begin
  1075. Message(sym_e_id_is_no_label_id);
  1076. code:=cerrornode.create;
  1077. end
  1078. else
  1079. begin
  1080. { goto outside the current scope? }
  1081. if srsym.owner<>current_procinfo.procdef.localst then
  1082. begin
  1083. { allowed? }
  1084. if not(m_non_local_goto in current_settings.modeswitches) then
  1085. Message(parser_e_goto_outside_proc);
  1086. include(current_procinfo.flags,pi_has_global_goto);
  1087. if is_nested_pd(current_procinfo.procdef) then
  1088. current_procinfo.set_needs_parentfp(srsym.owner.symtablelevel);
  1089. end;
  1090. code:=cgotonode.create(tlabelsym(srsym));
  1091. tgotonode(code).labelsym:=tlabelsym(srsym);
  1092. { set flag that this label is used }
  1093. tlabelsym(srsym).used:=true;
  1094. end;
  1095. end;
  1096. end;
  1097. _BEGIN :
  1098. code:=statement_block(_BEGIN);
  1099. _IF :
  1100. code:=if_statement;
  1101. _CASE :
  1102. code:=case_statement;
  1103. _REPEAT :
  1104. code:=repeat_statement;
  1105. _WHILE :
  1106. code:=while_statement;
  1107. _FOR :
  1108. code:=for_statement;
  1109. _WITH :
  1110. code:=with_statement;
  1111. _TRY :
  1112. code:=try_statement;
  1113. _RAISE :
  1114. code:=raise_statement;
  1115. { semicolons,else until and end are ignored }
  1116. _SEMICOLON,
  1117. _ELSE,
  1118. _UNTIL,
  1119. _END:
  1120. code:=cnothingnode.create;
  1121. _FAIL :
  1122. begin
  1123. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  1124. Message(parser_e_fail_only_in_constructor);
  1125. consume(_FAIL);
  1126. code:=cnodeutils.call_fail_node;
  1127. end;
  1128. _ASM :
  1129. begin
  1130. if parse_generic then
  1131. Message(parser_e_no_assembler_in_generic);
  1132. code:=_asm_statement;
  1133. end;
  1134. _PLUS:
  1135. begin
  1136. Message(parser_e_syntax_error);
  1137. consume(_PLUS);
  1138. end;
  1139. _EOF :
  1140. Message(scan_f_end_of_file);
  1141. else
  1142. begin
  1143. { don't typecheck yet, because that will also simplify, which may
  1144. result in not detecting certain kinds of syntax errors --
  1145. see mantis #15594 }
  1146. p:=expr(false);
  1147. { save the pattern here for latter usage, the label could be "000",
  1148. even if we read an expression, the pattern is still valid if it's really
  1149. a label (FK)
  1150. if you want to mess here, take care of
  1151. tests/webtbs/tw3546.pp
  1152. }
  1153. s:=pattern;
  1154. { When a colon follows a intconst then transform it into a label }
  1155. if (p.nodetype=ordconstn) and
  1156. try_to_consume(_COLON) then
  1157. begin
  1158. { in iso mode, 0003: is equal to 3: }
  1159. if (([m_iso,m_extpas]*current_settings.modeswitches)<>[]) then
  1160. searchsym(tostr(tordconstnode(p).value),srsym,srsymtable)
  1161. else
  1162. searchsym(s,srsym,srsymtable);
  1163. p.free;
  1164. if assigned(srsym) and
  1165. (srsym.typ=labelsym) then
  1166. begin
  1167. if tlabelsym(srsym).defined then
  1168. Message(sym_e_label_already_defined);
  1169. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1170. begin
  1171. include(current_procinfo.flags,pi_has_interproclabel);
  1172. if (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1173. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  1174. end;
  1175. tlabelsym(srsym).defined:=true;
  1176. p:=clabelnode.create(nil,tlabelsym(srsym));
  1177. tlabelsym(srsym).code:=p;
  1178. end
  1179. else
  1180. begin
  1181. Message1(sym_e_label_used_and_not_defined,s);
  1182. p:=cnothingnode.create;
  1183. end;
  1184. end;
  1185. if p.nodetype=labeln then
  1186. begin
  1187. { the pointer to the following instruction }
  1188. { isn't a very clean way }
  1189. if token in endtokens then
  1190. tlabelnode(p).left:=cnothingnode.create
  1191. else
  1192. tlabelnode(p).left:=statement();
  1193. { be sure to have left also typecheckpass }
  1194. typecheckpass(tlabelnode(p).left);
  1195. end
  1196. else
  1197. { change a load of a procvar to a call. this is also
  1198. supported in fpc mode }
  1199. if p.nodetype in [vecn,derefn,typeconvn,subscriptn,loadn] then
  1200. maybe_call_procvar(p,false);
  1201. { blockn support because a read/write is changed into a blocknode
  1202. with a separate statement for each read/write operation (JM)
  1203. the same is true for val() if the third parameter is not 32 bit
  1204. goto nodes are created by the compiler for non local exit statements, so
  1205. include them as well
  1206. }
  1207. if not(p.nodetype in [nothingn,errorn,calln,ifn,assignn,breakn,inlinen,
  1208. continuen,labeln,blockn,exitn,goton]) or
  1209. ((p.nodetype=inlinen) and
  1210. not is_void(p.resultdef)) or
  1211. ((p.nodetype=calln) and
  1212. (assigned(tcallnode(p).procdefinition)) and
  1213. (tcallnode(p).procdefinition.proctypeoption=potype_operator)) then
  1214. Message(parser_e_illegal_expression);
  1215. if not assigned(p.resultdef) then
  1216. do_typecheckpass(p);
  1217. { Specify that we don't use the value returned by the call.
  1218. This is used for :
  1219. - dispose of temp stack space
  1220. - dispose on FPU stack
  1221. - extended syntax checking }
  1222. if (p.nodetype=calln) then
  1223. begin
  1224. exclude(tcallnode(p).callnodeflags,cnf_return_value_used);
  1225. { in $x- state, the function result must not be ignored }
  1226. if not(cs_extsyntax in current_settings.moduleswitches) and
  1227. not(is_void(p.resultdef)) and
  1228. { can be nil in case there was an error in the expression }
  1229. assigned(tcallnode(p).procdefinition) and
  1230. { allow constructor calls to drop the result if they are
  1231. called as instance methods instead of class methods }
  1232. not(
  1233. (tcallnode(p).procdefinition.proctypeoption=potype_constructor) and
  1234. is_class_or_object(tprocdef(tcallnode(p).procdefinition).struct) and
  1235. assigned(tcallnode(p).methodpointer) and
  1236. (tnode(tcallnode(p).methodpointer).resultdef.typ=objectdef)
  1237. ) then
  1238. Message(parser_e_illegal_expression);
  1239. end;
  1240. code:=p;
  1241. end;
  1242. end;
  1243. if assigned(code) then
  1244. begin
  1245. typecheckpass(code);
  1246. code.fileinfo:=filepos;
  1247. end;
  1248. statement:=code;
  1249. end;
  1250. function statement_block(starttoken : ttoken) : tnode;
  1251. var
  1252. first,last : tnode;
  1253. filepos : tfileposinfo;
  1254. begin
  1255. first:=nil;
  1256. last:=nil;
  1257. filepos:=current_tokenpos;
  1258. consume(starttoken);
  1259. while not((token=_END) or (token=_FINALIZATION)) do
  1260. begin
  1261. if first=nil then
  1262. begin
  1263. last:=cstatementnode.create(statement,nil);
  1264. first:=last;
  1265. end
  1266. else
  1267. begin
  1268. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  1269. last:=tstatementnode(last).right;
  1270. end;
  1271. if ((token=_END) or (token=_FINALIZATION)) then
  1272. break
  1273. else
  1274. begin
  1275. { if no semicolon, then error and go on }
  1276. if token<>_SEMICOLON then
  1277. begin
  1278. consume(_SEMICOLON);
  1279. consume_all_until(_SEMICOLON);
  1280. end;
  1281. consume(_SEMICOLON);
  1282. end;
  1283. consume_emptystats;
  1284. end;
  1285. { don't consume the finalization token, it is consumed when
  1286. reading the finalization block, but allow it only after
  1287. an initalization ! }
  1288. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1289. consume(_END);
  1290. last:=cblocknode.create(first);
  1291. last.fileinfo:=filepos;
  1292. statement_block:=last;
  1293. end;
  1294. function assembler_block : tnode;
  1295. var
  1296. p : tnode;
  1297. {$if not(defined(sparcgen)) and not(defined(arm)) and not(defined(avr)) and not(defined(mips))}
  1298. locals : longint;
  1299. {$endif not(defined(sparcgen)) and not(defined(arm)) and not(defined(avr)) and not(defined(mips))}
  1300. srsym : tsym;
  1301. begin
  1302. if parse_generic then
  1303. message(parser_e_no_assembler_in_generic);
  1304. { Rename the funcret so that recursive calls are possible }
  1305. if not is_void(current_procinfo.procdef.returndef) then
  1306. begin
  1307. srsym:=TSym(current_procinfo.procdef.localst.Find(current_procinfo.procdef.procsym.name));
  1308. if assigned(srsym) then
  1309. srsym.realname:='$hiddenresult';
  1310. end;
  1311. { delphi uses register calling for assembler methods }
  1312. if (m_delphi in current_settings.modeswitches) and
  1313. (po_assembler in current_procinfo.procdef.procoptions) and
  1314. not(po_hascallingconvention in current_procinfo.procdef.procoptions) then
  1315. current_procinfo.procdef.proccalloption:=pocall_register;
  1316. { force the asm statement }
  1317. if token<>_ASM then
  1318. consume(_ASM);
  1319. include(current_procinfo.flags,pi_is_assembler);
  1320. p:=_asm_statement;
  1321. {$if not(defined(sparcgen)) and not(defined(arm)) and not(defined(avr)) and not(defined(mips))}
  1322. if (po_assembler in current_procinfo.procdef.procoptions) then
  1323. begin
  1324. { set the framepointer to esp for assembler functions when the
  1325. following conditions are met:
  1326. - if the are no local variables and parameters (except the allocated result)
  1327. - no reference to the result variable (refcount<=1)
  1328. - result is not stored as parameter
  1329. - target processor has optional frame pointer save
  1330. (vm, i386, vm only currently)
  1331. }
  1332. locals:=tabstractlocalsymtable(current_procinfo.procdef.parast).count_locals;
  1333. if (current_procinfo.procdef.localst.symtabletype=localsymtable) then
  1334. inc(locals,tabstractlocalsymtable(current_procinfo.procdef.localst).count_locals);
  1335. if (locals=0) and
  1336. not (current_procinfo.procdef.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  1337. (not assigned(current_procinfo.procdef.funcretsym) or
  1338. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs<=1)) and
  1339. not (df_generic in current_procinfo.procdef.defoptions) and
  1340. not(paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef)) then
  1341. begin
  1342. { Only need to set the framepointer, the locals will
  1343. be inserted with the correct reference in tcgasmnode.pass_generate_code }
  1344. current_procinfo.framepointer:=NR_STACK_POINTER_REG;
  1345. end;
  1346. end;
  1347. {$endif not(defined(sparcgen)) and not(defined(arm)) and not(defined(avr)) not(defined(mipsel))}
  1348. { Flag the result as assigned when it is returned in a
  1349. register.
  1350. }
  1351. if assigned(current_procinfo.procdef.funcretsym) and
  1352. not (df_generic in current_procinfo.procdef.defoptions) and
  1353. (not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef)) then
  1354. tabstractvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_initialised;
  1355. { because the END is already read we need to get the
  1356. last_endtoken_filepos here (PFV) }
  1357. last_endtoken_filepos:=current_tokenpos;
  1358. assembler_block:=p;
  1359. end;
  1360. end.