pstatmnt.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Florian Klaempfl
  4. Does the parsing of the statements
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pstatmnt;
  19. interface
  20. uses tree;
  21. var
  22. { true, if we are in a except block }
  23. in_except_block : boolean;
  24. { reads a block }
  25. function block(islibrary : boolean) : ptree;
  26. { reads an assembler block }
  27. function assembler_block : ptree;
  28. implementation
  29. uses
  30. cobjects,globals,files,verbose,systems,
  31. symtable,aasm,pass_1,types,scanner,hcodegen
  32. {$ifndef OLDPPU}
  33. ,ppu
  34. {$endif}
  35. ,pbase,pexpr,pdecl
  36. {$ifdef i386}
  37. ,i386,tgeni386
  38. {$ifndef NoRa386Int}
  39. ,ra386int
  40. {$endif NoRa386Int}
  41. {$ifndef NoRa386Att}
  42. ,ra386att
  43. {$endif NoRa386Att}
  44. {$ifndef NoRa386Dir}
  45. ,ra386dir
  46. {$endif NoRa386Dir}
  47. {$endif i386}
  48. {$ifdef m68k}
  49. ,m68k,tgen68k
  50. {$ifndef NoRa68kMot}
  51. ,ra68kmot
  52. {$endif NoRa68kMot}
  53. {$endif m68k}
  54. ;
  55. const
  56. statement_level : longint = 0;
  57. function statement : ptree;forward;
  58. function if_statement : ptree;
  59. var
  60. ex,if_a,else_a : ptree;
  61. begin
  62. consume(_IF);
  63. ex:=comp_expr(true);
  64. consume(_THEN);
  65. if token<>_ELSE then
  66. if_a:=statement
  67. else
  68. if_a:=nil;
  69. if token=_ELSE then
  70. begin
  71. consume(_ELSE);
  72. else_a:=statement;
  73. end
  74. else
  75. else_a:=nil;
  76. if_statement:=genloopnode(ifn,ex,if_a,else_a,false);
  77. end;
  78. { creates a block (list) of statements, til the next END token }
  79. function statements_til_end : ptree;
  80. var
  81. first,last : ptree;
  82. begin
  83. first:=nil;
  84. while token<>_END do
  85. begin
  86. if first=nil then
  87. begin
  88. last:=gennode(statementn,nil,statement);
  89. first:=last;
  90. end
  91. else
  92. begin
  93. last^.left:=gennode(statementn,nil,statement);
  94. last:=last^.left;
  95. end;
  96. if token<>SEMICOLON then
  97. break
  98. else
  99. consume(SEMICOLON);
  100. while token=SEMICOLON do
  101. consume(SEMICOLON);
  102. end;
  103. consume(_END);
  104. statements_til_end:=gensinglenode(blockn,first);
  105. end;
  106. function case_statement : ptree;
  107. var
  108. { contains the label number of currently parsed case block }
  109. aktcaselabel : plabel;
  110. root : pcaserecord;
  111. { the typ of the case expression }
  112. casedef : pdef;
  113. procedure newcaselabel(l,h : longint);
  114. var
  115. hcaselabel : pcaserecord;
  116. procedure insertlabel(var p : pcaserecord);
  117. begin
  118. if p=nil then p:=hcaselabel
  119. else
  120. if (p^._low>hcaselabel^._low) and
  121. (p^._low>hcaselabel^._high) then
  122. insertlabel(p^.less)
  123. else if (p^._high<hcaselabel^._low) and
  124. (p^._high<hcaselabel^._high) then
  125. insertlabel(p^.greater)
  126. else Message(parser_e_double_caselabel);
  127. end;
  128. begin
  129. new(hcaselabel);
  130. hcaselabel^.less:=nil;
  131. hcaselabel^.greater:=nil;
  132. hcaselabel^.statement:=aktcaselabel;
  133. getlabel(hcaselabel^._at);
  134. hcaselabel^._low:=l;
  135. hcaselabel^._high:=h;
  136. insertlabel(root);
  137. end;
  138. var
  139. code,caseexpr,p,instruc,elseblock : ptree;
  140. hl1,hl2 : longint;
  141. ranges : boolean;
  142. begin
  143. consume(_CASE);
  144. caseexpr:=comp_expr(true);
  145. { determines result type }
  146. cleartempgen;
  147. do_firstpass(caseexpr);
  148. casedef:=caseexpr^.resulttype;
  149. if not(is_ordinal(casedef)) then
  150. Message(parser_e_ordinal_expected);
  151. consume(_OF);
  152. inc(statement_level);
  153. root:=nil;
  154. ranges:=false;
  155. instruc:=nil;
  156. repeat
  157. getlabel(aktcaselabel);
  158. {aktcaselabel^.is_used:=true; }
  159. { may be an instruction has more case labels }
  160. repeat
  161. p:=expr;
  162. cleartempgen;
  163. do_firstpass(p);
  164. if (p^.treetype=rangen) then
  165. begin
  166. { type checking for case statements }
  167. if not is_subequal(casedef, p^.left^.resulttype) then
  168. Message(parser_e_case_mismatch);
  169. { type checking for case statements }
  170. if not is_subequal(casedef, p^.right^.resulttype) then
  171. Message(parser_e_case_mismatch);
  172. hl1:=get_ordinal_value(p^.left);
  173. hl2:=get_ordinal_value(p^.right);
  174. testrange(casedef,hl1);
  175. testrange(casedef,hl2);
  176. newcaselabel(hl1,hl2);
  177. ranges:=true;
  178. end
  179. else
  180. begin
  181. { type checking for case statements }
  182. if not is_subequal(casedef, p^.resulttype) then
  183. Message(parser_e_case_mismatch);
  184. hl1:=get_ordinal_value(p);
  185. testrange(casedef,hl1);
  186. newcaselabel(hl1,hl1);
  187. end;
  188. disposetree(p);
  189. if token=COMMA then consume(COMMA)
  190. else break;
  191. until false;
  192. consume(COLON);
  193. { handles instruction block }
  194. p:=gensinglenode(labeln,statement);
  195. p^.labelnr:=aktcaselabel;
  196. { concats instruction }
  197. instruc:=gennode(statementn,instruc,p);
  198. if not((token=_ELSE) or (token=_OTHERWISE) or (token=_END)) then
  199. consume(SEMICOLON);
  200. until (token=_ELSE) or (token=_OTHERWISE) or (token=_END);
  201. if (token=_ELSE) or (token=_OTHERWISE) then
  202. begin
  203. if token=_ELSE then consume(_ELSE)
  204. else consume(_OTHERWISE);
  205. elseblock:=statements_til_end;
  206. end
  207. else
  208. begin
  209. elseblock:=nil;
  210. consume(_END);
  211. end;
  212. dec(statement_level);
  213. code:=gencasenode(caseexpr,instruc,root);
  214. code^.elseblock:=elseblock;
  215. case_statement:=code;
  216. end;
  217. function repeat_statement : ptree;
  218. var
  219. first,last,p_e : ptree;
  220. begin
  221. consume(_REPEAT);
  222. first:=nil;
  223. inc(statement_level);
  224. while token<>_UNTIL do
  225. begin
  226. if first=nil then
  227. begin
  228. last:=gennode(statementn,nil,statement);
  229. first:=last;
  230. end
  231. else
  232. begin
  233. last^.left:=gennode(statementn,nil,statement);
  234. last:=last^.left;
  235. end;
  236. if token<>SEMICOLON then
  237. break;
  238. consume(SEMICOLON);
  239. while token=SEMICOLON do
  240. consume(SEMICOLON);
  241. end;
  242. consume(_UNTIL);
  243. dec(statement_level);
  244. first:=gensinglenode(blockn,first);
  245. p_e:=comp_expr(true);
  246. repeat_statement:=genloopnode(repeatn,p_e,first,nil,false);
  247. end;
  248. function while_statement : ptree;
  249. var
  250. p_e,p_a : ptree;
  251. begin
  252. consume(_WHILE);
  253. p_e:=comp_expr(true);
  254. consume(_DO);
  255. p_a:=statement;
  256. while_statement:=genloopnode(whilen,p_e,p_a,nil,false);
  257. end;
  258. function for_statement : ptree;
  259. var
  260. p_e,tovalue,p_a : ptree;
  261. backward : boolean;
  262. begin
  263. { parse loop header }
  264. consume(_FOR);
  265. p_e:=expr;
  266. if token=_DOWNTO then
  267. begin
  268. consume(_DOWNTO);
  269. backward:=true;
  270. end
  271. else
  272. begin
  273. consume(_TO);
  274. backward:=false;
  275. end;
  276. tovalue:=comp_expr(true);
  277. consume(_DO);
  278. { ... now the instruction }
  279. p_a:=statement;
  280. for_statement:=genloopnode(forn,p_e,tovalue,p_a,backward);
  281. end;
  282. function _with_statement : ptree;
  283. var
  284. right,hp,p : ptree;
  285. i,levelcount : longint;
  286. withsymtable,symtab : psymtable;
  287. obj : pobjectdef;
  288. begin
  289. Must_be_valid:=false;
  290. p:=comp_expr(true);
  291. do_firstpass(p);
  292. right:=nil;
  293. case p^.resulttype^.deftype of
  294. objectdef : begin
  295. obj:=pobjectdef(p^.resulttype);
  296. { this creates the stack in the wrong order !!
  297. levelcount:=0;
  298. while assigned(obj) do
  299. begin
  300. symtab:=obj^.publicsyms;
  301. withsymtable:=new(psymtable,init(symtable.withsymtable));
  302. withsymtable^.root:=symtab^.root;
  303. withsymtable^.next:=symtablestack;
  304. symtablestack:=withsymtable;
  305. obj:=obj^.childof;
  306. inc(levelcount);
  307. end; }
  308. withsymtable:=new(psymtable,init(symtable.withsymtable));
  309. withsymtable^.root:=obj^.publicsyms^.root;
  310. symtab:=withsymtable;
  311. levelcount:=1;
  312. obj:=obj^.childof;
  313. while assigned(obj) do
  314. begin
  315. symtab^.next:=new(psymtable,init(symtable.withsymtable));
  316. symtab:=symtab^.next;
  317. symtab^.root:=obj^.publicsyms^.root;
  318. obj:=obj^.childof;
  319. inc(levelcount);
  320. end;
  321. symtab^.next:=symtablestack;
  322. symtablestack:=withsymtable;
  323. end;
  324. recorddef : begin
  325. symtab:=precdef(p^.resulttype)^.symtable;
  326. levelcount:=1;
  327. withsymtable:=new(psymtable,init(symtable.withsymtable));
  328. withsymtable^.root:=symtab^.root;
  329. withsymtable^.next:=symtablestack;
  330. symtablestack:=withsymtable;
  331. end;
  332. else
  333. begin
  334. Message(parser_e_false_with_expr);
  335. { try to recover from error }
  336. if token=COMMA then
  337. begin
  338. consume(COMMA);
  339. {$ifdef tp}
  340. hp:=_with_statement;
  341. {$else}
  342. hp:=_with_statement();
  343. {$endif}
  344. end
  345. else
  346. begin
  347. consume(_DO);
  348. { ignore all }
  349. if token<>SEMICOLON then
  350. statement;
  351. end;
  352. _with_statement:=nil;
  353. exit;
  354. end;
  355. end;
  356. if token=COMMA then
  357. begin
  358. consume(COMMA);
  359. {$ifdef tp}
  360. right:=_with_statement;
  361. {$else}
  362. right:=_with_statement();
  363. {$endif}
  364. end
  365. else
  366. begin
  367. consume(_DO);
  368. if token<>SEMICOLON then
  369. right:=statement
  370. else
  371. right:=nil;
  372. end;
  373. for i:=1 to levelcount do
  374. symtablestack:=symtablestack^.next;
  375. _with_statement:=genwithnode(withsymtable,p,right,levelcount);
  376. end;
  377. function with_statement : ptree;
  378. begin
  379. consume(_WITH);
  380. with_statement:=_with_statement;
  381. end;
  382. function raise_statement : ptree;
  383. var
  384. p1,p2 : ptree;
  385. begin
  386. p1:=nil;
  387. p2:=nil;
  388. consume(_RAISE);
  389. if token<>SEMICOLON then
  390. begin
  391. p1:=comp_expr(true);
  392. if (token=ID) and (pattern='AT') then
  393. begin
  394. consume(ID);
  395. p2:=comp_expr(true);
  396. end;
  397. end
  398. else
  399. begin
  400. if not(in_except_block) then
  401. Message(parser_e_no_reraise_possible);
  402. end;
  403. raise_statement:=gennode(raisen,p1,p2);
  404. end;
  405. function try_statement : ptree;
  406. var
  407. p_try_block,p_finally_block,first,last,
  408. p_default,e1,e2,p_specific : ptree;
  409. old_in_except_block : boolean;
  410. begin
  411. procinfo.flags:=procinfo.flags or
  412. pi_uses_exceptions;
  413. p_default:=nil;
  414. p_specific:=nil;
  415. { read statements to try }
  416. consume(_TRY);
  417. first:=nil;
  418. inc(statement_level);
  419. while (token<>_FINALLY) and (token<>_EXCEPT) do
  420. begin
  421. if first=nil then
  422. begin
  423. last:=gennode(statementn,nil,statement);
  424. first:=last;
  425. end
  426. else
  427. begin
  428. last^.left:=gennode(statementn,nil,statement);
  429. last:=last^.left;
  430. end;
  431. if token<>SEMICOLON then
  432. break;
  433. consume(SEMICOLON);
  434. emptystats;
  435. end;
  436. p_try_block:=gensinglenode(blockn,first);
  437. if token=_FINALLY then
  438. begin
  439. consume(_FINALLY);
  440. p_finally_block:=statements_til_end;
  441. try_statement:=gennode(tryfinallyn,p_try_block,p_finally_block);
  442. dec(statement_level);
  443. end
  444. else
  445. begin
  446. consume(_EXCEPT);
  447. old_in_except_block:=in_except_block;
  448. in_except_block:=true;
  449. if token=_ON then
  450. { catch specific exceptions }
  451. begin
  452. repeat
  453. consume(_ON);
  454. e1:=comp_expr(true);
  455. if token=COLON then
  456. begin
  457. consume(COLON);
  458. e2:=comp_expr(true);
  459. { !!!!! }
  460. end
  461. else
  462. begin
  463. { !!!!! }
  464. end;
  465. consume(_DO);
  466. statement;
  467. if token<>SEMICOLON then
  468. break;
  469. emptystats;
  470. until false;
  471. if token=_ELSE then
  472. { catch the other exceptions }
  473. begin
  474. consume(_ELSE);
  475. p_default:=statements_til_end;
  476. end;
  477. end
  478. else
  479. { catch all exceptions }
  480. begin
  481. p_default:=statements_til_end;
  482. end;
  483. dec(statement_level);
  484. in_except_block:=old_in_except_block;
  485. try_statement:=genloopnode(tryexceptn,p_try_block,p_specific,p_default,false);
  486. end;
  487. end;
  488. function exit_statement : ptree;
  489. var
  490. p : ptree;
  491. begin
  492. consume(_EXIT);
  493. if token=LKLAMMER then
  494. begin
  495. consume(LKLAMMER);
  496. p:=comp_expr(true);
  497. consume(RKLAMMER);
  498. if procinfo.retdef=pdef(voiddef) then
  499. Message(parser_e_void_function)
  500. else
  501. procinfo.funcret_is_valid:=true;
  502. end
  503. else
  504. p:=nil;
  505. exit_statement:=gensinglenode(exitn,p);
  506. end;
  507. function _asm_statement : ptree;
  508. var
  509. asmstat : ptree;
  510. begin
  511. if (aktprocsym^.definition^.options and poinline)<>0 then
  512. Begin
  513. Message1(parser_w_not_supported_for_inline,'asm statement');
  514. Message(parser_w_inlining_disabled);
  515. aktprocsym^.definition^.options:= aktprocsym^.definition^.options and not poinline;
  516. End;
  517. case aktasmmode of
  518. {$ifdef i386}
  519. {$ifndef NoRA386Att}
  520. I386_ATT : asmstat:=ra386att.assemble;
  521. {$endif NoRA386Att}
  522. {$ifndef NoRA386Int}
  523. I386_INTEL : asmstat:=ra386int.assemble;
  524. {$endif NoRA386Int}
  525. {$ifndef NoRA386Dir}
  526. I386_DIRECT : asmstat:=ra386dir.assemble;
  527. {$endif NoRA386Dir}
  528. {$endif}
  529. {$ifdef m68k}
  530. {$ifndef NoRA68kMot}
  531. M68K_MOT : asmstat:=ra68kmot.assemble;
  532. {$endif NoRA68kMot}
  533. {$endif}
  534. else
  535. Message(parser_f_assembler_reader_not_supported);
  536. end;
  537. { Read first the _ASM statement }
  538. consume(_ASM);
  539. { END is read }
  540. if token=LECKKLAMMER then
  541. begin
  542. { it's possible to specify the modified registers }
  543. consume(LECKKLAMMER);
  544. asmstat^.object_preserved:=true;
  545. if token<>RECKKLAMMER then
  546. repeat
  547. { uppercase, because it's a CSTRING }
  548. uppervar(pattern);
  549. {$ifdef i386}
  550. if pattern='EAX' then
  551. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  552. else if pattern='EBX' then
  553. usedinproc:=usedinproc or ($80 shr byte(R_EBX))
  554. else if pattern='ECX' then
  555. usedinproc:=usedinproc or ($80 shr byte(R_ECX))
  556. else if pattern='EDX' then
  557. usedinproc:=usedinproc or ($80 shr byte(R_EDX))
  558. else if pattern='ESI' then
  559. begin
  560. usedinproc:=usedinproc or ($80 shr byte(R_ESI));
  561. asmstat^.object_preserved:=false;
  562. end
  563. else if pattern='EDI' then
  564. usedinproc:=usedinproc or ($80 shr byte(R_EDI))
  565. {$endif i386}
  566. {$ifdef m68k}
  567. if pattern='D0' then
  568. usedinproc:=usedinproc or ($800 shr word(R_D0))
  569. else if pattern='D1' then
  570. usedinproc:=usedinproc or ($800 shr word(R_D1))
  571. else if pattern='D6' then
  572. usedinproc:=usedinproc or ($800 shr word(R_D6))
  573. else if pattern='A0' then
  574. usedinproc:=usedinproc or ($800 shr word(R_A0))
  575. else if pattern='A1' then
  576. usedinproc:=usedinproc or ($800 shr word(R_A1))
  577. {$endif m68k}
  578. else consume(RECKKLAMMER);
  579. consume(CSTRING);
  580. if token=COMMA then consume(COMMA)
  581. else break;
  582. until false;
  583. consume(RECKKLAMMER);
  584. end
  585. else usedinproc:=$ff;
  586. _asm_statement:=asmstat;
  587. end;
  588. function new_dispose_statement : ptree;
  589. var
  590. p,p2 : ptree;
  591. ht : ttoken;
  592. again : boolean; { dummy for do_proc_call }
  593. destrukname : stringid;
  594. sym : psym;
  595. classh : pobjectdef;
  596. pd,pd2 : pdef;
  597. store_valid : boolean;
  598. tt : ttreetyp;
  599. begin
  600. ht:=token;
  601. if token=_NEW then consume(_NEW)
  602. else consume(_DISPOSE);
  603. if ht=_NEW then
  604. tt:=hnewn
  605. else
  606. tt:=hdisposen;
  607. consume(LKLAMMER);
  608. p:=comp_expr(true);
  609. { calc return type }
  610. cleartempgen;
  611. Store_valid := Must_be_valid;
  612. Must_be_valid := False;
  613. do_firstpass(p);
  614. Must_be_valid := Store_valid;
  615. {var o:Pobject;
  616. begin
  617. new(o,init); (*Also a valid new statement*)
  618. end;}
  619. if token=COMMA then
  620. begin
  621. { extended syntax of new and dispose }
  622. { function styled new is handled in factor }
  623. consume(COMMA);
  624. { destructors have no parameters }
  625. destrukname:=pattern;
  626. consume(ID);
  627. pd:=p^.resulttype;
  628. pd2:=pd;
  629. if (p^.resulttype = nil) or (pd^.deftype<>pointerdef) then
  630. begin
  631. Message(parser_e_pointer_type_expected);
  632. p:=factor(false);
  633. consume(RKLAMMER);
  634. new_dispose_statement:=genzeronode(errorn);
  635. exit;
  636. end;
  637. { first parameter must be an object or class }
  638. if ppointerdef(pd)^.definition^.deftype<>objectdef then
  639. begin
  640. Message(parser_e_pointer_to_class_expected);
  641. new_dispose_statement:=factor(false);
  642. consume_all_until(RKLAMMER);
  643. consume(RKLAMMER);
  644. exit;
  645. end;
  646. { check, if the first parameter is a pointer to a _class_ }
  647. classh:=pobjectdef(ppointerdef(pd)^.definition);
  648. if (classh^.options and oois_class)<>0 then
  649. begin
  650. Message(parser_e_no_new_or_dispose_for_classes);
  651. new_dispose_statement:=factor(false);
  652. { while token<>RKLAMMER do
  653. consume(token); }
  654. consume_all_until(RKLAMMER);
  655. consume(RKLAMMER);
  656. exit;
  657. end;
  658. { search cons-/destructor, also in parent classes }
  659. sym:=nil;
  660. while assigned(classh) do
  661. begin
  662. sym:=classh^.publicsyms^.search(pattern);
  663. srsymtable:=classh^.publicsyms;
  664. if assigned(sym) then
  665. break;
  666. classh:=classh^.childof;
  667. end;
  668. { the second parameter of new/dispose must be a call }
  669. { to a cons-/destructor }
  670. if (sym^.typ<>procsym) then
  671. begin
  672. Message(parser_e_expr_have_to_be_destructor_call);
  673. new_dispose_statement:=genzeronode(errorn);
  674. end
  675. else
  676. begin
  677. p2:=gensinglenode(tt,p);
  678. if ht=_NEW then
  679. begin
  680. { Constructors can take parameters.}
  681. p2^.resulttype:=ppointerdef(pd)^.definition;
  682. do_member_read(false,sym,p2,pd,again);
  683. end
  684. else
  685. { destructors can't.}
  686. p2:=genmethodcallnode(pprocsym(sym),srsymtable,p2);
  687. { we need the real called method }
  688. cleartempgen;
  689. do_firstpass(p2);
  690. if (ht=_NEW) and ((p2^.procdefinition^.options and poconstructor)=0) then
  691. Message(parser_e_expr_have_to_be_constructor_call);
  692. if (ht=_DISPOSE) and ((p2^.procdefinition^.options and podestructor)=0) then
  693. Message(parser_e_expr_have_to_be_destructor_call);
  694. if ht=_NEW then
  695. begin
  696. p2:=gennode(assignn,getcopy(p),gensinglenode(newn,p2));
  697. p2^.right^.resulttype:=pd2;
  698. end;
  699. new_dispose_statement:=p2;
  700. end;
  701. end
  702. else
  703. begin
  704. if (p^.resulttype=nil) or (p^.resulttype^.deftype<>pointerdef) then
  705. Begin
  706. Message(parser_e_pointer_type_expected);
  707. new_dispose_statement:=genzeronode(errorn);
  708. end
  709. else
  710. begin
  711. if (ppointerdef(p^.resulttype)^.definition^.deftype=objectdef) then
  712. Message(parser_w_use_extended_syntax_for_objects);
  713. case ht of
  714. _NEW : new_dispose_statement:=gensinglenode(simplenewn,p);
  715. _DISPOSE : new_dispose_statement:=gensinglenode(simpledisposen,p);
  716. end;
  717. end;
  718. end;
  719. consume(RKLAMMER);
  720. end;
  721. function statement_block(starttoken : ttoken) : ptree;
  722. var
  723. first,last : ptree;
  724. filepos : tfileposinfo;
  725. begin
  726. first:=nil;
  727. filepos:=tokenpos;
  728. consume(starttoken);
  729. inc(statement_level);
  730. while not(
  731. (token=_END) or
  732. ((starttoken=_INITIALIZATION) and (token=_FINALIZATION))
  733. ) do
  734. begin
  735. if first=nil then
  736. begin
  737. last:=gennode(statementn,nil,statement);
  738. first:=last;
  739. end
  740. else
  741. begin
  742. last^.left:=gennode(statementn,nil,statement);
  743. last:=last^.left;
  744. end;
  745. if (token=_END) or
  746. ((starttoken=_INITIALIZATION) and (token=_FINALIZATION)) then
  747. break
  748. else
  749. begin
  750. { if no semicolon, then error and go on }
  751. if token<>SEMICOLON then
  752. begin
  753. consume(SEMICOLON);
  754. { while token<>SEMICOLON do
  755. consume(token); }
  756. consume_all_until(SEMICOLON);
  757. end;
  758. consume(SEMICOLON);
  759. end;
  760. emptystats;
  761. end;
  762. { don't consume the finalization token, it is consumed when
  763. reading the finalization block !
  764. }
  765. if token=_END then
  766. consume(_END);
  767. dec(statement_level);
  768. last:=gensinglenode(blockn,first);
  769. set_tree_filepos(last,filepos);
  770. statement_block:=last;
  771. end;
  772. function statement : ptree;
  773. var
  774. p : ptree;
  775. code : ptree;
  776. labelnr : plabel;
  777. filepos : tfileposinfo;
  778. label
  779. ready;
  780. begin
  781. filepos:=tokenpos;
  782. case token of
  783. _GOTO : begin
  784. if not(cs_support_goto in aktswitches)then
  785. Message(sym_e_goto_and_label_not_supported);
  786. consume(_GOTO);
  787. if (token<>INTCONST) and (token<>ID) then
  788. begin
  789. Message(sym_e_label_not_found);
  790. code:=genzeronode(errorn);
  791. end
  792. else
  793. begin
  794. getsym(pattern,true);
  795. consume(token);
  796. if srsym^.typ<>labelsym then
  797. begin
  798. Message(sym_e_id_is_no_label_id);
  799. code:=genzeronode(errorn);
  800. end
  801. else
  802. code:=genlabelnode(goton,
  803. plabelsym(srsym)^.number);
  804. end;
  805. end;
  806. _BEGIN : code:=statement_block(_BEGIN);
  807. _IF : code:=if_statement;
  808. _CASE : code:=case_statement;
  809. _REPEAT : code:=repeat_statement;
  810. _WHILE : code:=while_statement;
  811. _FOR : code:=for_statement;
  812. _NEW,_DISPOSE : code:=new_dispose_statement;
  813. _WITH : code:=with_statement;
  814. _TRY : code:=try_statement;
  815. _RAISE : code:=raise_statement;
  816. { semicolons,else until and end are ignored }
  817. SEMICOLON,
  818. _ELSE,
  819. _UNTIL,
  820. _END:
  821. code:=genzeronode(niln);
  822. _CONTINUE:
  823. begin
  824. consume(_CONTINUE);
  825. code:=genzeronode(continuen);
  826. end;
  827. _FAIL : begin
  828. { internalerror(100); }
  829. if (aktprocsym^.definition^.options and poconstructor)=0 then
  830. Message(parser_e_fail_only_in_constructor);
  831. consume(_FAIL);
  832. code:=genzeronode(failn);
  833. end;
  834. {
  835. _BREAK:
  836. begin
  837. consume(_BREAK);
  838. code:=genzeronode(breakn);
  839. end;
  840. }
  841. _EXIT : code:=exit_statement;
  842. _ASM : begin
  843. code:=_asm_statement;
  844. end;
  845. else
  846. begin
  847. if (token=INTCONST) or
  848. ((token=ID) and
  849. not((cs_delphi2_compatible in aktswitches) and
  850. (pattern='RESULT'))) then
  851. begin
  852. getsym(pattern,true);
  853. lastsymknown:=true;
  854. lastsrsym:=srsym;
  855. { it is NOT necessarily the owner
  856. it can be a withsymtable !!! }
  857. lastsrsymtable:=srsymtable;
  858. if assigned(srsym) and (srsym^.typ=labelsym) then
  859. begin
  860. consume(token);
  861. consume(COLON);
  862. if plabelsym(srsym)^.defined then
  863. Message(sym_e_label_already_defined);
  864. plabelsym(srsym)^.defined:=true;
  865. { statement modifies srsym }
  866. labelnr:=plabelsym(srsym)^.number;
  867. lastsymknown:=false;
  868. { the pointer to the following instruction }
  869. { isn't a very clean way }
  870. {$ifdef tp}
  871. code:=gensinglenode(labeln,statement);
  872. {$else}
  873. code:=gensinglenode(labeln,statement());
  874. {$endif}
  875. code^.labelnr:=labelnr;
  876. { sorry, but there is a jump the easiest way }
  877. goto ready;
  878. end;
  879. end;
  880. p:=expr;
  881. if not(p^.treetype in [calln,assignn,breakn,inlinen,
  882. continuen]) then
  883. Message(cg_e_illegal_expression);
  884. { specify that we don't use the value returned by the call }
  885. { Question : can this be also improtant
  886. for inlinen ??
  887. it is used for :
  888. - dispose of temp stack space
  889. - dispose on FPU stack }
  890. if p^.treetype=calln then
  891. p^.return_value_used:=false;
  892. code:=p;
  893. end;
  894. end;
  895. ready:
  896. set_tree_filepos(code,filepos);
  897. statement:=code;
  898. end;
  899. function block(islibrary : boolean) : ptree;
  900. {$ifdef TEST_FUNCRET }
  901. var
  902. funcretsym : pfuncretsym;
  903. {$endif TEST_FUNCRET }
  904. begin
  905. {$ifdef TEST_FUNCRET }
  906. if procinfo.retdef<>pdef(voiddef) then
  907. begin
  908. { if the current is a function aktprocsym is non nil }
  909. { and there is a local symtable set }
  910. funcretsym:=new(pfuncretsym,init(aktprocsym^.name),@procinfo);
  911. procinfo.retoffset:=-funcretsym^.address;
  912. { insert in local symtable }
  913. symtablestack^.insert(funcretsym);
  914. end;
  915. {$endif TEST_FUNCRET }
  916. read_declarations(islibrary);
  917. { temporary space is set, while the BEGIN of the procedure }
  918. if (symtablestack^.symtabletype=localsymtable) then
  919. procinfo.firsttemp := -symtablestack^.datasize
  920. else procinfo.firsttemp := 0;
  921. { space for the return value }
  922. { !!!!! this means that we can not set the return value
  923. in a subfunction !!!!! }
  924. { because we don't know yet where the address is }
  925. if procinfo.retdef<>pdef(voiddef) then
  926. begin
  927. if ret_in_acc(procinfo.retdef) or (procinfo.retdef^.deftype=floatdef) then
  928. { if (procinfo.retdef^.deftype=orddef) or
  929. (procinfo.retdef^.deftype=pointerdef) or
  930. (procinfo.retdef^.deftype=enumdef) or
  931. (procinfo.retdef^.deftype=procvardef) or
  932. (procinfo.retdef^.deftype=floatdef) or
  933. (
  934. (procinfo.retdef^.deftype=setdef) and
  935. (psetdef(procinfo.retdef)^.settype=smallset)
  936. ) then }
  937. begin
  938. {$ifdef TEST_FUNCRET }
  939. { the space has been set in the local symtable }
  940. procinfo.retoffset:=-funcretsym^.address;
  941. strdispose(funcretsym^._name);
  942. { lowercase name unreachable }
  943. { as it is handled differently }
  944. funcretsym^._name:=strpnew('func_result');
  945. {$else TEST_FUNCRET }
  946. { align func result at 4 byte }
  947. procinfo.retoffset:=
  948. -((-procinfo.firsttemp+(procinfo.retdef^.size+3)) div 4)*4;
  949. procinfo.firsttemp:=procinfo.retoffset;
  950. {$endif TEST_FUNCRET }
  951. if (procinfo.flags and pi_operator)<>0 then
  952. {opsym^.address:=procinfo.call_offset; is wrong PM }
  953. opsym^.address:=-procinfo.retoffset;
  954. { eax is modified by a function }
  955. {$ifdef i386}
  956. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  957. {$endif}
  958. {$ifdef m68k}
  959. usedinproc:=usedinproc or ($800 shr word(R_D0))
  960. {$endif}
  961. end;
  962. end;
  963. {Unit initialization?.}
  964. if (lexlevel=1) and (current_module^.is_unit) then
  965. if (token=_END) then
  966. begin
  967. consume(_END);
  968. block:=nil;
  969. end
  970. else
  971. begin
  972. if token=_INITIALIZATION then
  973. begin
  974. current_module^.flags:=current_module^.flags or uf_init;
  975. block:=statement_block(_INITIALIZATION);
  976. end
  977. else if (token=_FINALIZATION) then
  978. begin
  979. if (current_module^.flags and uf_finalize)<>0 then
  980. block:=statement_block(_FINALIZATION)
  981. else
  982. begin
  983. block:=nil;
  984. exit;
  985. end;
  986. end
  987. else
  988. begin
  989. current_module^.flags:=current_module^.flags or uf_init;
  990. block:=statement_block(_BEGIN);
  991. end;
  992. end
  993. else
  994. block:=statement_block(_BEGIN);
  995. end;
  996. function assembler_block : ptree;
  997. begin
  998. read_declarations(false);
  999. { temporary space is set, while the BEGIN of the procedure }
  1000. if symtablestack^.symtabletype=localsymtable then
  1001. procinfo.firsttemp := -symtablestack^.datasize
  1002. else procinfo.firsttemp := 0;
  1003. { assembler code does not allocate }
  1004. { space for the return value }
  1005. if procinfo.retdef<>pdef(voiddef) then
  1006. begin
  1007. if ret_in_acc(procinfo.retdef) then
  1008. begin
  1009. { in assembler code the result should be directly in %eax
  1010. procinfo.retoffset:=procinfo.firsttemp-procinfo.retdef^.size;
  1011. procinfo.firsttemp:=procinfo.retoffset; }
  1012. {$ifdef i386}
  1013. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  1014. {$endif}
  1015. {$ifdef m68k}
  1016. usedinproc:=usedinproc or ($800 shr word(R_D0))
  1017. {$endif}
  1018. end
  1019. else if not is_fpu(procinfo.retdef) then
  1020. { should we allow assembler functions of big elements ? }
  1021. Message(parser_e_asm_incomp_with_function_return);
  1022. end;
  1023. { set the framepointer to esp for assembler functions }
  1024. { but only if the are no local variables }
  1025. { added no parameter also (PM) }
  1026. if ((aktprocsym^.definition^.options and poassembler)<>0) and
  1027. (aktprocsym^.definition^.localst^.datasize=0) and
  1028. (aktprocsym^.definition^.parast^.datasize=0) then
  1029. begin
  1030. {$ifdef i386}
  1031. procinfo.framepointer:=R_ESP;
  1032. {$endif}
  1033. {$ifdef m68k}
  1034. procinfo.framepointer:=R_SP;
  1035. {$endif}
  1036. { set the right value for parameters }
  1037. dec(aktprocsym^.definition^.parast^.call_offset,sizeof(pointer));
  1038. dec(procinfo.call_offset,sizeof(pointer));
  1039. end;
  1040. assembler_block:=_asm_statement;
  1041. { becuase the END is already read we need to get the
  1042. last_endtoken_filepos here (PFV) }
  1043. last_endtoken_filepos:=tokenpos;
  1044. end;
  1045. end.
  1046. {
  1047. $Log$
  1048. Revision 1.27 1998-07-28 21:52:55 florian
  1049. + implementation of raise and try..finally
  1050. + some misc. exception stuff
  1051. Revision 1.26 1998/07/27 21:57:14 florian
  1052. * fix to allow tv like stream registration:
  1053. @tmenu.load doesn't work if load had parameters or if load was only
  1054. declared in an anchestor class of tmenu
  1055. Revision 1.25 1998/07/14 21:46:53 peter
  1056. * updated messages file
  1057. Revision 1.24 1998/07/10 10:48:42 peter
  1058. * fixed realnumber scanning
  1059. * [] after asmblock was not uppercased anymore
  1060. Revision 1.23 1998/06/25 08:48:18 florian
  1061. * first version of rtti support
  1062. Revision 1.22 1998/06/24 14:48:36 peter
  1063. * ifdef newppu -> ifndef oldppu
  1064. Revision 1.21 1998/06/24 14:06:34 peter
  1065. * fixed the name changes
  1066. Revision 1.20 1998/06/23 14:00:16 peter
  1067. * renamed RA* units
  1068. Revision 1.19 1998/06/08 22:59:50 peter
  1069. * smartlinking works for win32
  1070. * some defines to exclude some compiler parts
  1071. Revision 1.18 1998/06/05 14:37:35 pierre
  1072. * fixes for inline for operators
  1073. * inline procedure more correctly restricted
  1074. Revision 1.17 1998/06/04 09:55:43 pierre
  1075. * demangled name of procsym reworked to become independant of the mangling scheme
  1076. Revision 1.16 1998/06/02 17:03:04 pierre
  1077. * with node corrected for objects
  1078. * small bugs for SUPPORT_MMX fixed
  1079. Revision 1.15 1998/05/30 14:31:06 peter
  1080. + $ASMMODE
  1081. Revision 1.14 1998/05/29 09:58:14 pierre
  1082. * OPR_REGISTER for 1 arg was missing in ratti386.pas
  1083. (probably a merging problem)
  1084. * errors at start of line were lost
  1085. Revision 1.13 1998/05/28 17:26:50 peter
  1086. * fixed -R switch, it didn't work after my previous akt/init patch
  1087. * fixed bugs 110,130,136
  1088. Revision 1.12 1998/05/21 19:33:33 peter
  1089. + better procedure directive handling and only one table
  1090. Revision 1.11 1998/05/20 09:42:35 pierre
  1091. + UseTokenInfo now default
  1092. * unit in interface uses and implementation uses gives error now
  1093. * only one error for unknown symbol (uses lastsymknown boolean)
  1094. the problem came from the label code !
  1095. + first inlined procedures and function work
  1096. (warning there might be allowed cases were the result is still wrong !!)
  1097. * UseBrower updated gives a global list of all position of all used symbols
  1098. with switch -gb
  1099. Revision 1.10 1998/05/11 13:07:56 peter
  1100. + $ifdef NEWPPU for the new ppuformat
  1101. + $define GDB not longer required
  1102. * removed all warnings and stripped some log comments
  1103. * no findfirst/findnext anymore to remove smartlink *.o files
  1104. Revision 1.9 1998/05/06 08:38:46 pierre
  1105. * better position info with UseTokenInfo
  1106. UseTokenInfo greatly simplified
  1107. + added check for changed tree after first time firstpass
  1108. (if we could remove all the cases were it happen
  1109. we could skip all firstpass if firstpasscount > 1)
  1110. Only with ExtDebug
  1111. Revision 1.8 1998/05/05 12:05:42 florian
  1112. * problems with properties fixed
  1113. * crash fixed: i:=l when i and l are undefined, was a problem with
  1114. implementation of private/protected
  1115. Revision 1.7 1998/05/01 16:38:46 florian
  1116. * handling of private and protected fixed
  1117. + change_keywords_to_tp implemented to remove
  1118. keywords which aren't supported by tp
  1119. * break and continue are now symbols of the system unit
  1120. + widestring, longstring and ansistring type released
  1121. Revision 1.6 1998/04/30 15:59:42 pierre
  1122. * GDB works again better :
  1123. correct type info in one pass
  1124. + UseTokenInfo for better source position
  1125. * fixed one remaining bug in scanner for line counts
  1126. * several little fixes
  1127. Revision 1.5 1998/04/29 10:33:59 pierre
  1128. + added some code for ansistring (not complete nor working yet)
  1129. * corrected operator overloading
  1130. * corrected nasm output
  1131. + started inline procedures
  1132. + added starstarn : use ** for exponentiation (^ gave problems)
  1133. + started UseTokenInfo cond to get accurate positions
  1134. Revision 1.4 1998/04/08 16:58:05 pierre
  1135. * several bugfixes
  1136. ADD ADC and AND are also sign extended
  1137. nasm output OK (program still crashes at end
  1138. and creates wrong assembler files !!)
  1139. procsym types sym in tdef removed !!
  1140. }