pstatmnt.pas 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  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,aasmbase,
  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. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  987. { We assume the function result is always used in the TP mode }
  988. if (m_tp7 in current_settings.modeswitches) and
  989. not (po_assembler in current_procinfo.procdef.procoptions) and
  990. assigned(current_procinfo.procdef.funcretsym) then
  991. current_procinfo.procdef.funcretsym.IncRefCount;
  992. {$endif}
  993. { Read first the _ASM statement }
  994. consume(_ASM);
  995. { Force an empty register list for pure assembler routines,
  996. so that pass2 won't allocate volatile registers for them. }
  997. asmstat.has_registerlist:=(po_assembler in current_procinfo.procdef.procoptions);
  998. { END is read, got a list of changed registers? }
  999. if try_to_consume(_LECKKLAMMER) then
  1000. begin
  1001. if token<>_RECKKLAMMER then
  1002. begin
  1003. if po_assembler in current_procinfo.procdef.procoptions then
  1004. Message(parser_w_register_list_ignored);
  1005. repeat
  1006. { it's possible to specify the modified registers }
  1007. if token=_CSTRING then
  1008. reg:=std_regnum_search(lower(cstringpattern))
  1009. else if token=_CCHAR then
  1010. reg:=std_regnum_search(lower(pattern))
  1011. else
  1012. reg:=NR_NO;
  1013. if reg<>NR_NO then
  1014. begin
  1015. if not(po_assembler in current_procinfo.procdef.procoptions) and assigned(hl) then
  1016. begin
  1017. hl.Insert(tai_regalloc.alloc(reg,nil));
  1018. hl.Insert(tai_regalloc.markused(reg));
  1019. hl.Concat(tai_regalloc.dealloc(reg,nil));
  1020. end;
  1021. end
  1022. else
  1023. Message(asmr_e_invalid_register);
  1024. if token=_CCHAR then
  1025. consume(_CCHAR)
  1026. else
  1027. consume(_CSTRING);
  1028. if not try_to_consume(_COMMA) then
  1029. break;
  1030. until false;
  1031. asmstat.has_registerlist:=true;
  1032. end;
  1033. consume(_RECKKLAMMER);
  1034. end;
  1035. Inside_asm_statement:=false;
  1036. _asm_statement:=asmstat;
  1037. end;
  1038. { Old Turbo Pascal INLINE(data/data/...) }
  1039. function tp_inline_statement : tnode;
  1040. var
  1041. actype : taiconst_type;
  1042. function eval_intconst: asizeint;
  1043. var
  1044. cv : Tconstexprint;
  1045. def: tdef;
  1046. begin
  1047. cv:=get_intconst;
  1048. case actype of
  1049. aitconst_8bit:
  1050. def:=s8inttype;
  1051. aitconst_16bit:
  1052. def:=s16inttype;
  1053. else
  1054. def:=sizesinttype;
  1055. end;
  1056. if cv.uvalue>get_max_value(def).uvalue then
  1057. def:=get_unsigned_inttype(def);
  1058. adaptrange(def,cv,rc_implicit);
  1059. result:=cv.svalue;
  1060. end;
  1061. var
  1062. cur_line : longint;
  1063. w : asizeint;
  1064. hl : TAsmList;
  1065. asmstat : tasmnode;
  1066. sym : tsym;
  1067. symtable : TSymtable;
  1068. s : tsymstr;
  1069. ac : tai_const;
  1070. nesting : integer;
  1071. tokenbuf : tdynamicarray;
  1072. begin
  1073. consume(_INLINE);
  1074. consume(_LKLAMMER);
  1075. hl:=TAsmList.create;
  1076. asmstat:=casmnode.create(hl);
  1077. asmstat.fileinfo:=current_filepos;
  1078. tokenbuf:=tdynamicarray.Create(16);
  1079. cur_line:=0;
  1080. { Parse data blocks }
  1081. repeat
  1082. { Record one data block for further replaying.
  1083. This is needed since / is used as a data block delimiter and cause troubles
  1084. with constant evaluation which is allowed inside a data block. }
  1085. tokenbuf.reset;
  1086. current_scanner.startrecordtokens(tokenbuf);
  1087. nesting:=0;
  1088. while token<>_SLASH do
  1089. begin
  1090. case token of
  1091. _LKLAMMER:
  1092. inc(nesting);
  1093. _RKLAMMER:
  1094. begin
  1095. dec(nesting);
  1096. if nesting<0 then
  1097. break;
  1098. end;
  1099. _SEMICOLON:
  1100. consume(_RKLAMMER); { error }
  1101. else
  1102. ; {no action}
  1103. end;
  1104. consume(token);
  1105. end;
  1106. current_scanner.stoprecordtokens;
  1107. { Set the current token to ; to make the constant evaluator happy }
  1108. token:=_SEMICOLON;
  1109. { Parse recorded tokens }
  1110. current_scanner.startreplaytokens(tokenbuf,false);
  1111. if cur_line<>current_filepos.line then
  1112. begin
  1113. hl.concat(tai_force_line.Create);
  1114. cur_line:=current_filepos.line;
  1115. end;
  1116. { Data size override }
  1117. if try_to_consume(_GT) then
  1118. actype:=aitconst_16bit
  1119. else
  1120. if try_to_consume(_LT) then
  1121. actype:=aitconst_8bit
  1122. else
  1123. actype:=aitconst_128bit; { default size }
  1124. sym:=nil;
  1125. if token=_ID then
  1126. begin
  1127. if searchsym(pattern,sym,symtable) then
  1128. begin
  1129. if sym.typ in [staticvarsym,localvarsym,paravarsym] then
  1130. begin
  1131. { Address of the static symbol or base offset for local symbols }
  1132. consume(_ID);
  1133. if (sym.typ=staticvarsym) and not (actype in [aitconst_128bit,aitconst_ptr]) then
  1134. Message1(type_e_integer_expr_expected,sym.name);
  1135. { Additional offset }
  1136. if token in [_PLUS,_MINUS] then
  1137. w:=eval_intconst
  1138. else
  1139. w:=0;
  1140. if sym.typ=staticvarsym then
  1141. s:=sym.mangledname
  1142. else
  1143. s:=sym.name;
  1144. ac:=tai_const.Createname(s,w);
  1145. if actype=aitconst_128bit then
  1146. ac.consttype:=aitconst_ptr
  1147. else
  1148. ac.consttype:=actype;
  1149. { For a local symbol it is needed to generate a constant with the symbols's stack offset.
  1150. The stack offset is unavailable rigth now and will be resolved later in tcgasmnode.pass_generate_code.
  1151. Set sym.bind:=AB_NONE to indicate that this is a local symbol. }
  1152. if sym.typ<>staticvarsym then
  1153. ac.sym.bind:=AB_NONE;
  1154. hl.concat(ac);
  1155. end
  1156. else
  1157. if sym.typ=constsym then
  1158. sym:=nil
  1159. else
  1160. begin
  1161. consume(_ID);
  1162. Message(asmr_e_wrong_sym_type);
  1163. end;
  1164. end;
  1165. end;
  1166. if sym=nil then
  1167. begin
  1168. { Integer constant expression }
  1169. w:=eval_intconst;
  1170. case actype of
  1171. aitconst_8bit:
  1172. hl.concat(tai_const.Create_8bit(w));
  1173. aitconst_16bit:
  1174. hl.concat(tai_const.Create_16bit(w));
  1175. else
  1176. if w<$100 then
  1177. hl.concat(tai_const.Create_8bit(w))
  1178. else
  1179. hl.concat(tai_const.Create_sizeint(w));
  1180. end;
  1181. end;
  1182. if not try_to_consume(_SEMICOLON) then
  1183. consume(_RKLAMMER); {error}
  1184. until nesting<0;
  1185. tokenbuf.free;
  1186. { mark boundaries of assembler block, this is necessary for optimizer }
  1187. hl.insert(tai_marker.create(mark_asmblockstart));
  1188. hl.concat(tai_marker.create(mark_asmblockend));
  1189. { Mark procedure that it has assembler blocks }
  1190. include(current_procinfo.flags,pi_has_assembler_block);
  1191. { Assume the function result is always used }
  1192. if assigned(current_procinfo.procdef.funcretsym) then
  1193. current_procinfo.procdef.funcretsym.IncRefCount;
  1194. result:=asmstat;
  1195. end;
  1196. function statement : tnode;
  1197. var
  1198. p,
  1199. code : tnode;
  1200. filepos : tfileposinfo;
  1201. srsym : tsym;
  1202. srsymtable : TSymtable;
  1203. s : TIDString;
  1204. begin
  1205. filepos:=current_tokenpos;
  1206. code:=nil;
  1207. case token of
  1208. _GOTO :
  1209. begin
  1210. if not(cs_support_goto in current_settings.moduleswitches) then
  1211. Message(sym_e_goto_and_label_not_supported);
  1212. consume(_GOTO);
  1213. if (token<>_INTCONST) and (token<>_ID) then
  1214. begin
  1215. Message(sym_e_label_not_found);
  1216. code:=cerrornode.create;
  1217. end
  1218. else
  1219. begin
  1220. if token=_ID then
  1221. consume_sym(srsym,srsymtable)
  1222. else
  1223. begin
  1224. if token<>_INTCONST then
  1225. internalerror(201008021);
  1226. { strip leading 0's in iso mode }
  1227. if (([m_iso,m_extpas]*current_settings.modeswitches)<>[]) then
  1228. while (length(pattern)>1) and (pattern[1]='0') do
  1229. delete(pattern,1,1);
  1230. searchsym(pattern,srsym,srsymtable);
  1231. if srsym=nil then
  1232. begin
  1233. identifier_not_found(pattern);
  1234. srsym:=generrorsym;
  1235. srsymtable:=nil;
  1236. end;
  1237. consume(token);
  1238. end;
  1239. if srsym.typ<>labelsym then
  1240. begin
  1241. Message(sym_e_id_is_no_label_id);
  1242. code:=cerrornode.create;
  1243. end
  1244. else
  1245. begin
  1246. { goto outside the current scope? }
  1247. if srsym.owner<>current_procinfo.procdef.localst then
  1248. begin
  1249. { allowed? }
  1250. if not(m_non_local_goto in current_settings.modeswitches) then
  1251. Message(parser_e_goto_outside_proc);
  1252. include(current_procinfo.flags,pi_has_global_goto);
  1253. if is_nested_pd(current_procinfo.procdef) then
  1254. current_procinfo.set_needs_parentfp(srsym.owner.symtablelevel);
  1255. end;
  1256. code:=cgotonode.create(tlabelsym(srsym));
  1257. tgotonode(code).labelsym:=tlabelsym(srsym);
  1258. { set flag that this label is used }
  1259. tlabelsym(srsym).used:=true;
  1260. end;
  1261. end;
  1262. end;
  1263. _BEGIN :
  1264. code:=statement_block(_BEGIN);
  1265. _IF :
  1266. code:=if_statement;
  1267. _CASE :
  1268. code:=case_statement;
  1269. _REPEAT :
  1270. code:=repeat_statement;
  1271. _WHILE :
  1272. code:=while_statement;
  1273. _FOR :
  1274. code:=for_statement;
  1275. _WITH :
  1276. code:=with_statement;
  1277. _TRY :
  1278. code:=try_statement;
  1279. _RAISE :
  1280. code:=raise_statement;
  1281. { semicolons,else until and end are ignored }
  1282. _SEMICOLON,
  1283. _ELSE,
  1284. _UNTIL,
  1285. _END:
  1286. code:=cnothingnode.create;
  1287. _FAIL :
  1288. begin
  1289. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  1290. Message(parser_e_fail_only_in_constructor);
  1291. consume(_FAIL);
  1292. code:=cnodeutils.call_fail_node;
  1293. end;
  1294. _ASM :
  1295. begin
  1296. if parse_generic then
  1297. Message(parser_e_no_assembler_in_generic);
  1298. code:=_asm_statement;
  1299. end;
  1300. _PLUS:
  1301. begin
  1302. Message(parser_e_syntax_error);
  1303. consume(_PLUS);
  1304. end;
  1305. _INLINE:
  1306. begin
  1307. code:=tp_inline_statement;
  1308. end;
  1309. _EOF :
  1310. Message(scan_f_end_of_file);
  1311. else
  1312. begin
  1313. { don't typecheck yet, because that will also simplify, which may
  1314. result in not detecting certain kinds of syntax errors --
  1315. see mantis #15594 }
  1316. p:=expr(false);
  1317. { save the pattern here for latter usage, the label could be "000",
  1318. even if we read an expression, the pattern is still valid if it's really
  1319. a label (FK)
  1320. if you want to mess here, take care of
  1321. tests/webtbs/tw3546.pp
  1322. }
  1323. s:=pattern;
  1324. { When a colon follows a intconst then transform it into a label }
  1325. if (p.nodetype=ordconstn) and
  1326. try_to_consume(_COLON) then
  1327. begin
  1328. { in iso mode, 0003: is equal to 3: }
  1329. if (([m_iso,m_extpas]*current_settings.modeswitches)<>[]) then
  1330. searchsym(tostr(tordconstnode(p).value),srsym,srsymtable)
  1331. else
  1332. searchsym(s,srsym,srsymtable);
  1333. p.free;
  1334. if assigned(srsym) and
  1335. (srsym.typ=labelsym) then
  1336. begin
  1337. if tlabelsym(srsym).defined then
  1338. Message(sym_e_label_already_defined);
  1339. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1340. begin
  1341. include(current_procinfo.flags,pi_has_interproclabel);
  1342. if (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1343. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  1344. end;
  1345. tlabelsym(srsym).defined:=true;
  1346. p:=clabelnode.create(nil,tlabelsym(srsym));
  1347. tlabelsym(srsym).code:=p;
  1348. end
  1349. else
  1350. begin
  1351. Message1(sym_e_label_used_and_not_defined,s);
  1352. p:=cnothingnode.create;
  1353. end;
  1354. end;
  1355. if p.nodetype=labeln then
  1356. begin
  1357. { the pointer to the following instruction }
  1358. { isn't a very clean way }
  1359. if token in endtokens then
  1360. tlabelnode(p).left:=cnothingnode.create
  1361. else
  1362. tlabelnode(p).left:=statement();
  1363. { be sure to have left also typecheckpass }
  1364. typecheckpass(tlabelnode(p).left);
  1365. end
  1366. else
  1367. { change a load of a procvar to a call. this is also
  1368. supported in fpc mode }
  1369. if p.nodetype in [vecn,derefn,typeconvn,subscriptn,loadn] then
  1370. maybe_call_procvar(p,false);
  1371. { blockn support because a read/write is changed into a blocknode
  1372. with a separate statement for each read/write operation (JM)
  1373. the same is true for val() if the third parameter is not 32 bit
  1374. goto nodes are created by the compiler for non local exit statements, so
  1375. include them as well
  1376. }
  1377. if not(p.nodetype in [nothingn,errorn,calln,ifn,assignn,breakn,inlinen,
  1378. continuen,labeln,blockn,exitn,goton]) or
  1379. ((p.nodetype=inlinen) and
  1380. not is_void(p.resultdef)) or
  1381. ((p.nodetype=calln) and
  1382. (assigned(tcallnode(p).procdefinition)) and
  1383. (tcallnode(p).procdefinition.proctypeoption=potype_operator)) then
  1384. Message(parser_e_illegal_expression);
  1385. if not assigned(p.resultdef) then
  1386. do_typecheckpass(p);
  1387. { Specify that we don't use the value returned by the call.
  1388. This is used for :
  1389. - dispose of temp stack space
  1390. - dispose on FPU stack
  1391. - extended syntax checking }
  1392. if (p.nodetype=calln) then
  1393. begin
  1394. exclude(tcallnode(p).callnodeflags,cnf_return_value_used);
  1395. { in $x- state, the function result must not be ignored }
  1396. if not(cs_extsyntax in current_settings.moduleswitches) and
  1397. not(is_void(p.resultdef)) and
  1398. { can be nil in case there was an error in the expression }
  1399. assigned(tcallnode(p).procdefinition) and
  1400. { allow constructor calls to drop the result if they are
  1401. called as instance methods instead of class methods }
  1402. not(
  1403. (tcallnode(p).procdefinition.proctypeoption=potype_constructor) and
  1404. is_class_or_object(tprocdef(tcallnode(p).procdefinition).struct) and
  1405. assigned(tcallnode(p).methodpointer) and
  1406. (tnode(tcallnode(p).methodpointer).resultdef.typ=objectdef)
  1407. ) then
  1408. Message(parser_e_illegal_expression);
  1409. end;
  1410. code:=p;
  1411. end;
  1412. end;
  1413. if assigned(code) then
  1414. begin
  1415. typecheckpass(code);
  1416. code.fileinfo:=filepos;
  1417. end;
  1418. statement:=code;
  1419. end;
  1420. function statement_block(starttoken : ttoken) : tnode;
  1421. var
  1422. first,last : tnode;
  1423. filepos : tfileposinfo;
  1424. begin
  1425. first:=nil;
  1426. last:=nil;
  1427. filepos:=current_tokenpos;
  1428. consume(starttoken);
  1429. while not((token=_END) or (token=_FINALIZATION)) do
  1430. begin
  1431. if first=nil then
  1432. begin
  1433. last:=cstatementnode.create(statement,nil);
  1434. first:=last;
  1435. end
  1436. else
  1437. begin
  1438. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  1439. last:=tstatementnode(last).right;
  1440. end;
  1441. if ((token=_END) or (token=_FINALIZATION)) then
  1442. break
  1443. else
  1444. begin
  1445. { if no semicolon, then error and go on }
  1446. if token<>_SEMICOLON then
  1447. begin
  1448. consume(_SEMICOLON);
  1449. consume_all_until(_SEMICOLON);
  1450. end;
  1451. consume(_SEMICOLON);
  1452. end;
  1453. consume_emptystats;
  1454. end;
  1455. { don't consume the finalization token, it is consumed when
  1456. reading the finalization block, but allow it only after
  1457. an initalization ! }
  1458. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1459. consume(_END);
  1460. last:=cblocknode.create(first);
  1461. last.fileinfo:=filepos;
  1462. statement_block:=last;
  1463. end;
  1464. function assembler_block : tnode;
  1465. var
  1466. p : tnode;
  1467. {$if not(defined(sparcgen)) and not(defined(arm)) and not(defined(avr)) and not(defined(mips))}
  1468. locals : longint;
  1469. {$endif not(defined(sparcgen)) and not(defined(arm)) and not(defined(avr)) and not(defined(mips))}
  1470. srsym : tsym;
  1471. begin
  1472. if parse_generic then
  1473. message(parser_e_no_assembler_in_generic);
  1474. { Rename the funcret so that recursive calls are possible }
  1475. if not is_void(current_procinfo.procdef.returndef) then
  1476. begin
  1477. srsym:=TSym(current_procinfo.procdef.localst.Find(current_procinfo.procdef.procsym.name));
  1478. if assigned(srsym) then
  1479. srsym.realname:='$hiddenresult';
  1480. end;
  1481. { delphi uses register calling for assembler methods }
  1482. if (m_delphi in current_settings.modeswitches) and
  1483. (po_assembler in current_procinfo.procdef.procoptions) and
  1484. not(po_hascallingconvention in current_procinfo.procdef.procoptions) then
  1485. current_procinfo.procdef.proccalloption:=pocall_register;
  1486. { force the asm statement }
  1487. if token<>_ASM then
  1488. consume(_ASM);
  1489. include(current_procinfo.flags,pi_is_assembler);
  1490. p:=_asm_statement;
  1491. {$if not(defined(sparcgen)) and not(defined(arm)) and not(defined(avr)) and not(defined(mips))}
  1492. if (po_assembler in current_procinfo.procdef.procoptions) then
  1493. begin
  1494. { set the framepointer to esp for assembler functions when the
  1495. following conditions are met:
  1496. - if the are no local variables and parameters (except the allocated result)
  1497. - no reference to the result variable (refcount<=1)
  1498. - result is not stored as parameter
  1499. - target processor has optional frame pointer save
  1500. (vm, i386, vm only currently)
  1501. }
  1502. locals:=tabstractlocalsymtable(current_procinfo.procdef.parast).count_locals;
  1503. if (current_procinfo.procdef.localst.symtabletype=localsymtable) then
  1504. inc(locals,tabstractlocalsymtable(current_procinfo.procdef.localst).count_locals);
  1505. if (locals=0) and
  1506. not (current_procinfo.procdef.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  1507. (not assigned(current_procinfo.procdef.funcretsym) or
  1508. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs<=1)) and
  1509. not (df_generic in current_procinfo.procdef.defoptions) and
  1510. not(paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef)) then
  1511. begin
  1512. { Only need to set the framepointer, the locals will
  1513. be inserted with the correct reference in tcgasmnode.pass_generate_code }
  1514. current_procinfo.framepointer:=NR_STACK_POINTER_REG;
  1515. end;
  1516. end;
  1517. {$endif not(defined(sparcgen)) and not(defined(arm)) and not(defined(avr)) not(defined(mipsel))}
  1518. { Flag the result as assigned when it is returned in a
  1519. register.
  1520. }
  1521. if assigned(current_procinfo.procdef.funcretsym) and
  1522. not (df_generic in current_procinfo.procdef.defoptions) and
  1523. (not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef)) then
  1524. tabstractvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_initialised;
  1525. { because the END is already read we need to get the
  1526. last_endtoken_filepos here (PFV) }
  1527. last_endtoken_filepos:=current_tokenpos;
  1528. assembler_block:=p;
  1529. end;
  1530. end.