nset.pas 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for set/case nodes
  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 nset;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,constexp,
  22. node,globtype,globals,
  23. aasmbase,aasmtai,aasmdata,ncon,symtype;
  24. type
  25. TLabelType = (ltOrdinal, ltConstString);
  26. pcaselabel = ^tcaselabel;
  27. tcaselabel = record
  28. { unique blockid }
  29. blockid : longint;
  30. { left and right tree node }
  31. less,
  32. greater : pcaselabel;
  33. { range type }
  34. case label_type : TLabelType of
  35. ltOrdinal:
  36. (
  37. _low,
  38. _high : TConstExprInt;
  39. );
  40. ltConstString:
  41. (
  42. _low_str,
  43. _high_str : tstringconstnode;
  44. );
  45. end;
  46. pcaseblock = ^tcaseblock;
  47. tcaseblock = record
  48. { label (only used in pass_generate_code) }
  49. blocklabel : tasmlabel;
  50. { instructions }
  51. statement : tnode;
  52. end;
  53. tsetelementnode = class(tbinarynode)
  54. constructor create(l,r : tnode);virtual;
  55. function pass_typecheck:tnode;override;
  56. function pass_1 : tnode;override;
  57. end;
  58. tsetelementnodeclass = class of tsetelementnode;
  59. tinnode = class(tbinopnode)
  60. constructor create(l,r : tnode);virtual;reintroduce;
  61. function pass_typecheck:tnode;override;
  62. function simplify:tnode;override;
  63. function pass_1 : tnode;override;
  64. end;
  65. tinnodeclass = class of tinnode;
  66. trangenode = class(tbinarynode)
  67. constructor create(l,r : tnode);virtual;
  68. function pass_typecheck:tnode;override;
  69. function pass_1 : tnode;override;
  70. end;
  71. trangenodeclass = class of trangenode;
  72. tcasenode = class(tunarynode)
  73. labels : pcaselabel;
  74. blocks : TFPList;
  75. elseblock : tnode;
  76. constructor create(l:tnode);virtual;
  77. destructor destroy;override;
  78. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  79. procedure ppuwrite(ppufile:tcompilerppufile);override;
  80. procedure buildderefimpl;override;
  81. procedure derefimpl;override;
  82. function dogetcopy : tnode;override;
  83. procedure insertintolist(l : tnodelist);override;
  84. function pass_typecheck:tnode;override;
  85. function pass_1 : tnode;override;
  86. function docompare(p: tnode): boolean; override;
  87. procedure addlabel(blockid:longint;l,h : TConstExprInt); overload;
  88. procedure addlabel(blockid:longint;l,h : tstringconstnode); overload;
  89. procedure addblock(blockid:longint;instr:tnode);
  90. procedure addelseblock(instr:tnode);
  91. end;
  92. tcasenodeclass = class of tcasenode;
  93. var
  94. csetelementnode : tsetelementnodeclass;
  95. cinnode : tinnodeclass;
  96. crangenode : trangenodeclass;
  97. ccasenode : tcasenodeclass;
  98. { counts the labels }
  99. function case_count_labels(root : pcaselabel) : longint;
  100. { searches the highest label }
  101. function case_get_max(root : pcaselabel) : tconstexprint;
  102. { searches the lowest label }
  103. function case_get_min(root : pcaselabel) : tconstexprint;
  104. implementation
  105. uses
  106. systems,
  107. verbose,
  108. symconst,symdef,symsym,symtable,defutil,defcmp,
  109. htypechk,pass_1,
  110. nadd,nbas,ncnv,nld,nflw,cgobj,cgbase,
  111. widestr;
  112. {*****************************************************************************
  113. TSETELEMENTNODE
  114. *****************************************************************************}
  115. constructor tsetelementnode.create(l,r : tnode);
  116. begin
  117. inherited create(setelementn,l,r);
  118. end;
  119. function tsetelementnode.pass_typecheck:tnode;
  120. begin
  121. result:=nil;
  122. typecheckpass(left);
  123. if assigned(right) then
  124. typecheckpass(right);
  125. set_varstate(left,vs_read,[vsf_must_be_valid]);
  126. if codegenerror then
  127. exit;
  128. resultdef:=left.resultdef;
  129. end;
  130. function tsetelementnode.pass_1 : tnode;
  131. begin
  132. result:=nil;
  133. firstpass(left);
  134. if assigned(right) then
  135. firstpass(right);
  136. if codegenerror then
  137. exit;
  138. expectloc:=left.expectloc;
  139. end;
  140. {*****************************************************************************
  141. TINNODE
  142. *****************************************************************************}
  143. constructor tinnode.create(l,r : tnode);
  144. begin
  145. inherited create(inn,l,r);
  146. end;
  147. function tinnode.pass_typecheck:tnode;
  148. var
  149. t : tnode;
  150. pst : pconstset;
  151. function createsetconst(psd : tsetdef) : pconstset;
  152. var
  153. pcs : pconstset;
  154. i : longint;
  155. begin
  156. new(pcs);
  157. case psd.elementdef.typ of
  158. enumdef :
  159. begin
  160. for i := 0 to tenumdef(psd.elementdef).symtable.SymList.Count - 1 do
  161. begin
  162. include(pcs^,tenumsym(tenumdef(psd.elementdef).symtable.SymList[i]).value);
  163. end;
  164. end;
  165. orddef :
  166. begin
  167. for i:=int64(torddef(psd.elementdef).low) to int64(torddef(psd.elementdef).high) do
  168. include(pcs^,i);
  169. end;
  170. end;
  171. createsetconst:=pcs;
  172. end;
  173. begin
  174. result:=nil;
  175. resultdef:=booltype;
  176. typecheckpass(right);
  177. set_varstate(right,vs_read,[vsf_must_be_valid]);
  178. if codegenerror then
  179. exit;
  180. { Convert array constructor first to set }
  181. if is_array_constructor(right.resultdef) then
  182. begin
  183. arrayconstructor_to_set(right);
  184. firstpass(right);
  185. if codegenerror then
  186. exit;
  187. end;
  188. if right.resultdef.typ<>setdef then
  189. CGMessage(sym_e_set_expected);
  190. if codegenerror then
  191. exit;
  192. if (right.nodetype=typen) then
  193. begin
  194. { we need to create a setconstn }
  195. pst:=createsetconst(tsetdef(ttypenode(right).resultdef));
  196. t:=csetconstnode.create(pst,ttypenode(right).resultdef);
  197. dispose(pst);
  198. right.free;
  199. right:=t;
  200. typecheckpass(right);
  201. end;
  202. typecheckpass(left);
  203. set_varstate(left,vs_read,[vsf_must_be_valid]);
  204. if codegenerror then
  205. exit;
  206. if not assigned(left.resultdef) then
  207. internalerror(20021126);
  208. if (m_tp7 in current_settings.modeswitches) then
  209. begin
  210. { insert a hint that a range check error might occur on non-byte
  211. elements with the in operator.
  212. }
  213. if (
  214. (left.resultdef.typ = orddef) and not
  215. (torddef(left.resultdef).ordtype in [s8bit,u8bit,uchar,pasbool,bool8bit])
  216. )
  217. or
  218. (
  219. (left.resultdef.typ = enumdef) and
  220. (tenumdef(left.resultdef).maxval > 255)
  221. )
  222. then
  223. CGMessage(type_h_in_range_check);
  224. { type conversion/check }
  225. if assigned(tsetdef(right.resultdef).elementdef) then
  226. inserttypeconv(left,tsetdef(right.resultdef).elementdef);
  227. end
  228. else if not is_ordinal(left.resultdef) or (left.resultdef.size > u32inttype.size) then
  229. begin
  230. CGMessage(type_h_in_range_check);
  231. if is_signed(left.resultdef) then
  232. inserttypeconv(left,s32inttype)
  233. else
  234. inserttypeconv(left,u32inttype);
  235. end
  236. else if assigned(tsetdef(right.resultdef).elementdef) and
  237. not(is_integer(tsetdef(right.resultdef).elementdef) and
  238. is_integer(left.resultdef)) then
  239. { Type conversion to check things like 'char in set_of_byte'. }
  240. { Can't use is_subequal because that will fail for }
  241. { 'widechar in set_of_char' }
  242. { Can't use the type conversion for integers because then }
  243. { "longint in set_of_byte" will give a range check error }
  244. { instead of false }
  245. inserttypeconv(left,tsetdef(right.resultdef).elementdef);
  246. { empty set then return false }
  247. if not assigned(tsetdef(right.resultdef).elementdef) or
  248. ((right.nodetype = setconstn) and
  249. (tnormalset(tsetconstnode(right).value_set^) = [])) then
  250. begin
  251. t:=cordconstnode.create(0,booltype,false);
  252. typecheckpass(t);
  253. result:=t;
  254. exit;
  255. end;
  256. result:=simplify;
  257. end;
  258. function tinnode.simplify:tnode;
  259. var
  260. t : tnode;
  261. begin
  262. result:=nil;
  263. { constant evaluation }
  264. if (left.nodetype=ordconstn) then
  265. begin
  266. if (right.nodetype=setconstn) then
  267. begin
  268. { tordconstnode.value is int64 -> signed -> the expression }
  269. { below will be converted to longint on 32 bit systems due }
  270. { to the rule above -> will give range check error if }
  271. { value > high(longint) if we don't take the signedness }
  272. { into account }
  273. if Tordconstnode(left).value.signed then
  274. t:=cordconstnode.create(byte(tordconstnode(left).value.svalue in Tsetconstnode(right).value_set^),
  275. booltype,true)
  276. else
  277. t:=cordconstnode.create(byte(tordconstnode(left).value.uvalue in Tsetconstnode(right).value_set^),
  278. booltype,true);
  279. typecheckpass(t);
  280. result:=t;
  281. exit;
  282. end
  283. else
  284. begin
  285. if (Tordconstnode(left).value<int64(tsetdef(right.resultdef).setbase)) or
  286. (Tordconstnode(left).value>int64(Tsetdef(right.resultdef).setmax)) then
  287. begin
  288. t:=cordconstnode.create(0, booltype, true);
  289. typecheckpass(t);
  290. result:=t;
  291. exit;
  292. end;
  293. end;
  294. end;
  295. end;
  296. function tinnode.pass_1 : tnode;
  297. begin
  298. result:=nil;
  299. expectloc:=LOC_REGISTER;
  300. firstpass(right);
  301. firstpass(left);
  302. if codegenerror then
  303. exit;
  304. end;
  305. {*****************************************************************************
  306. TRANGENODE
  307. *****************************************************************************}
  308. constructor trangenode.create(l,r : tnode);
  309. var
  310. value: string;
  311. begin
  312. { if right is char and left is string then }
  313. { right should be treated as one-symbol string }
  314. if is_conststringnode(l) and is_constcharnode(r) then
  315. begin
  316. value := char(tordconstnode(r).value.uvalue) + ''#0;
  317. r.free;
  318. r := cstringconstnode.createstr(value);
  319. do_typecheckpass(r);
  320. end;
  321. inherited create(rangen,l,r);
  322. end;
  323. function trangenode.pass_typecheck : tnode;
  324. begin
  325. result:=nil;
  326. typecheckpass(left);
  327. typecheckpass(right);
  328. set_varstate(left,vs_read,[vsf_must_be_valid]);
  329. set_varstate(right,vs_read,[vsf_must_be_valid]);
  330. if codegenerror then
  331. exit;
  332. { both types must be compatible }
  333. if compare_defs(left.resultdef,right.resultdef,left.nodetype)=te_incompatible then
  334. IncompatibleTypes(left.resultdef,right.resultdef);
  335. { Check if only when its a constant set }
  336. if (left.nodetype=ordconstn) and (right.nodetype=ordconstn) then
  337. begin
  338. { upper limit must be greater or equal than lower limit }
  339. if (tordconstnode(left).value>tordconstnode(right).value) and
  340. ((tordconstnode(left).value<0) or (tordconstnode(right).value>=0)) then
  341. CGMessage(parser_e_upper_lower_than_lower);
  342. end;
  343. resultdef:=left.resultdef;
  344. end;
  345. function trangenode.pass_1 : tnode;
  346. begin
  347. result:=nil;
  348. firstpass(left);
  349. firstpass(right);
  350. if codegenerror then
  351. exit;
  352. expectloc:=left.expectloc;
  353. end;
  354. {*****************************************************************************
  355. Case Helpers
  356. *****************************************************************************}
  357. function case_count_labels(root : pcaselabel) : longint;
  358. var
  359. _l : longint;
  360. procedure count(p : pcaselabel);
  361. begin
  362. inc(_l);
  363. if assigned(p^.less) then
  364. count(p^.less);
  365. if assigned(p^.greater) then
  366. count(p^.greater);
  367. end;
  368. begin
  369. _l:=0;
  370. count(root);
  371. case_count_labels:=_l;
  372. end;
  373. function case_get_max(root : pcaselabel) : tconstexprint;
  374. var
  375. hp : pcaselabel;
  376. begin
  377. hp:=root;
  378. while assigned(hp^.greater) do
  379. hp:=hp^.greater;
  380. case_get_max:=hp^._high;
  381. end;
  382. function case_get_min(root : pcaselabel) : tconstexprint;
  383. var
  384. hp : pcaselabel;
  385. begin
  386. hp:=root;
  387. while assigned(hp^.less) do
  388. hp:=hp^.less;
  389. case_get_min:=hp^._low;
  390. end;
  391. procedure deletecaselabels(p : pcaselabel);
  392. begin
  393. if assigned(p^.greater) then
  394. deletecaselabels(p^.greater);
  395. if assigned(p^.less) then
  396. deletecaselabels(p^.less);
  397. if (p^.label_type = ltConstString) then
  398. begin
  399. p^._low_str.Free;
  400. p^._high_str.Free;
  401. end;
  402. dispose(p);
  403. end;
  404. function copycaselabel(p : pcaselabel) : pcaselabel;
  405. var
  406. n : pcaselabel;
  407. begin
  408. new(n);
  409. n^:=p^;
  410. if (p^.label_type = ltConstString) then
  411. begin
  412. n^._low_str := tstringconstnode(p^._low_str.getcopy);
  413. n^._high_str := tstringconstnode(p^._high_str.getcopy);
  414. end;
  415. if assigned(p^.greater) then
  416. n^.greater:=copycaselabel(p^.greater);
  417. if assigned(p^.less) then
  418. n^.less:=copycaselabel(p^.less);
  419. copycaselabel:=n;
  420. end;
  421. procedure ppuwritecaselabel(ppufile:tcompilerppufile;p : pcaselabel);
  422. var
  423. b : byte;
  424. begin
  425. ppufile.putbyte(byte(p^.label_type = ltConstString));
  426. if (p^.label_type = ltConstString) then
  427. begin
  428. p^._low_str.ppuwrite(ppufile);
  429. p^._high_str.ppuwrite(ppufile);
  430. end
  431. else
  432. begin
  433. ppufile.putexprint(p^._low);
  434. ppufile.putexprint(p^._high);
  435. end;
  436. ppufile.putlongint(p^.blockid);
  437. b:=ord(assigned(p^.greater)) or (ord(assigned(p^.less)) shl 1);
  438. ppufile.putbyte(b);
  439. if assigned(p^.greater) then
  440. ppuwritecaselabel(ppufile,p^.greater);
  441. if assigned(p^.less) then
  442. ppuwritecaselabel(ppufile,p^.less);
  443. end;
  444. function ppuloadcaselabel(ppufile:tcompilerppufile):pcaselabel;
  445. var
  446. b : byte;
  447. p : pcaselabel;
  448. begin
  449. new(p);
  450. if boolean(ppufile.getbyte) then
  451. begin
  452. p^.label_type := ltConstString;
  453. p^._low_str := cstringconstnode.ppuload(stringconstn,ppufile);
  454. p^._high_str := cstringconstnode.ppuload(stringconstn,ppufile);
  455. end
  456. else
  457. begin
  458. p^.label_type := ltOrdinal;
  459. p^._low:=ppufile.getexprint;
  460. p^._high:=ppufile.getexprint;
  461. end;
  462. p^.blockid:=ppufile.getlongint;
  463. b:=ppufile.getbyte;
  464. if (b and 1)=1 then
  465. p^.greater:=ppuloadcaselabel(ppufile)
  466. else
  467. p^.greater:=nil;
  468. if (b and 2)=2 then
  469. p^.less:=ppuloadcaselabel(ppufile)
  470. else
  471. p^.less:=nil;
  472. ppuloadcaselabel:=p;
  473. end;
  474. {*****************************************************************************
  475. TCASENODE
  476. *****************************************************************************}
  477. constructor tcasenode.create(l:tnode);
  478. begin
  479. inherited create(casen,l);
  480. labels:=nil;
  481. blocks:=TFPList.create;
  482. elseblock:=nil;
  483. end;
  484. destructor tcasenode.destroy;
  485. var
  486. i : longint;
  487. hp : pcaseblock;
  488. begin
  489. elseblock.free;
  490. deletecaselabels(labels);
  491. for i:=0 to blocks.count-1 do
  492. begin
  493. pcaseblock(blocks[i])^.statement.free;
  494. hp:=pcaseblock(blocks[i]);
  495. dispose(hp);
  496. end;
  497. blocks.free;
  498. inherited destroy;
  499. end;
  500. constructor tcasenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  501. var
  502. cnt,i : longint;
  503. begin
  504. inherited ppuload(t,ppufile);
  505. elseblock:=ppuloadnode(ppufile);
  506. cnt:=ppufile.getlongint();
  507. blocks:=TFPList.create;
  508. for i:=0 to cnt-1 do
  509. addblock(i,ppuloadnode(ppufile));
  510. labels:=ppuloadcaselabel(ppufile);
  511. end;
  512. procedure tcasenode.ppuwrite(ppufile:tcompilerppufile);
  513. var
  514. i : longint;
  515. begin
  516. inherited ppuwrite(ppufile);
  517. ppuwritenode(ppufile,elseblock);
  518. ppufile.putlongint(blocks.count);
  519. for i:=0 to blocks.count-1 do
  520. ppuwritenode(ppufile,pcaseblock(blocks[i])^.statement);
  521. ppuwritecaselabel(ppufile,labels);
  522. end;
  523. procedure tcasenode.buildderefimpl;
  524. var
  525. i : integer;
  526. begin
  527. inherited buildderefimpl;
  528. if assigned(elseblock) then
  529. elseblock.buildderefimpl;
  530. for i:=0 to blocks.count-1 do
  531. pcaseblock(blocks[i])^.statement.buildderefimpl;
  532. end;
  533. procedure tcasenode.derefimpl;
  534. var
  535. i : integer;
  536. begin
  537. inherited derefimpl;
  538. if assigned(elseblock) then
  539. elseblock.derefimpl;
  540. for i:=0 to blocks.count-1 do
  541. pcaseblock(blocks[i])^.statement.derefimpl;
  542. end;
  543. function tcasenode.pass_typecheck : tnode;
  544. begin
  545. result:=nil;
  546. resultdef:=voidtype;
  547. end;
  548. function tcasenode.pass_1 : tnode;
  549. var
  550. i : integer;
  551. node_thenblock,node_elseblock,if_node : tnode;
  552. tempcaseexpr : ttempcreatenode;
  553. if_block, init_block : tblocknode;
  554. stmt : tstatementnode;
  555. function makeifblock(const labtree : pcaselabel; prevconditblock : tnode): tnode;
  556. var
  557. condit : tnode;
  558. begin
  559. if assigned(labtree^.less) then
  560. result := makeifblock(labtree^.less, prevconditblock)
  561. else
  562. result := prevconditblock;
  563. condit := caddnode.create(equaln, left.getcopy, labtree^._low_str.getcopy);
  564. if (labtree^._low_str.fullcompare(labtree^._high_str)<>0) then
  565. begin
  566. condit.nodetype := gten;
  567. condit := caddnode.create(
  568. andn, condit, caddnode.create(
  569. lten, left.getcopy, labtree^._high_str.getcopy));
  570. end;
  571. result :=
  572. cifnode.create(
  573. condit, pcaseblock(blocks[labtree^.blockid])^.statement, result);
  574. pcaseblock(blocks[labtree^.blockid])^.statement := nil;
  575. if assigned(labtree^.greater) then
  576. result := makeifblock(labtree^.greater, result);
  577. typecheckpass(result);
  578. end;
  579. begin
  580. result:=nil;
  581. init_block:=nil;
  582. expectloc:=LOC_VOID;
  583. { evalutes the case expression }
  584. firstpass(left);
  585. set_varstate(left,vs_read,[vsf_must_be_valid]);
  586. if codegenerror then
  587. exit;
  588. { Load caseexpr into temp var if complex. }
  589. { No need to do this for ordinal, because }
  590. { in that case caseexpr is generated once }
  591. if (labels^.label_type = ltConstString) and (not valid_for_addr(left, false)) and
  592. (blocks.count > 0) then
  593. begin
  594. init_block := internalstatements(stmt);
  595. tempcaseexpr :=
  596. ctempcreatenode.create(
  597. left.resultdef, left.resultdef.size, tt_persistent, true);
  598. typecheckpass(tnode(tempcaseexpr));
  599. addstatement(stmt, tempcaseexpr);
  600. addstatement(
  601. stmt, cassignmentnode.create(
  602. ctemprefnode.create(tempcaseexpr), left));
  603. left := ctemprefnode.create(tempcaseexpr);
  604. typecheckpass(left);
  605. end;
  606. { first case }
  607. for i:=0 to blocks.count-1 do
  608. firstpass(pcaseblock(blocks[i])^.statement);
  609. { may be handle else tree }
  610. if assigned(elseblock) then
  611. begin
  612. firstpass(elseblock);
  613. { kill case? }
  614. if blocks.count=0 then
  615. begin
  616. result:=elseblock;
  617. elseblock:=nil;
  618. exit;
  619. end;
  620. end
  621. else
  622. if blocks.count=0 then
  623. begin
  624. result:=cnothingnode.create;
  625. exit;
  626. end;
  627. if (labels^.label_type = ltConstString) then
  628. begin
  629. if_node := makeifblock(labels, elseblock);
  630. if assigned(init_block) then
  631. begin
  632. firstpass(tnode(init_block));
  633. if_block := internalstatements(stmt);
  634. addstatement(stmt, init_block);
  635. addstatement(stmt, if_node);
  636. result := if_block;
  637. end
  638. else
  639. result := if_node;
  640. elseblock := nil;
  641. exit;
  642. end;
  643. if is_boolean(left.resultdef) then
  644. begin
  645. case blocks.count of
  646. 2:
  647. begin
  648. if boolean(qword(labels^._low))=false then
  649. begin
  650. node_thenblock:=pcaseblock(blocks[labels^.greater^.blockid])^.statement;
  651. node_elseblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  652. pcaseblock(blocks[labels^.greater^.blockid])^.statement:=nil;
  653. end
  654. else
  655. begin
  656. node_thenblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  657. node_elseblock:=pcaseblock(blocks[labels^.less^.blockid])^.statement;
  658. pcaseblock(blocks[labels^.less^.blockid])^.statement:=nil;
  659. end;
  660. pcaseblock(blocks[labels^.blockid])^.statement:=nil;
  661. end;
  662. 1:
  663. begin
  664. if labels^._low=labels^._high then
  665. begin
  666. if boolean(qword(labels^._low))=false then
  667. begin
  668. node_thenblock:=elseblock;
  669. node_elseblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  670. end
  671. else
  672. begin
  673. node_thenblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  674. node_elseblock:=elseblock;
  675. end;
  676. pcaseblock(blocks[labels^.blockid])^.statement:=nil;
  677. elseblock:=nil;
  678. end
  679. else
  680. begin
  681. result:=pcaseblock(blocks[labels^.blockid])^.statement;
  682. pcaseblock(blocks[labels^.blockid])^.statement:=nil;
  683. elseblock:=nil;
  684. exit;
  685. end;
  686. end;
  687. else
  688. internalerror(200805031);
  689. end;
  690. result:=cifnode.create(left,node_thenblock,node_elseblock);
  691. left:=nil;
  692. end;
  693. end;
  694. function tcasenode.dogetcopy : tnode;
  695. var
  696. n : tcasenode;
  697. i : longint;
  698. begin
  699. n:=tcasenode(inherited dogetcopy);
  700. if assigned(elseblock) then
  701. n.elseblock:=elseblock.dogetcopy
  702. else
  703. n.elseblock:=nil;
  704. if assigned(labels) then
  705. n.labels:=copycaselabel(labels)
  706. else
  707. n.labels:=nil;
  708. if assigned(blocks) then
  709. begin
  710. n.blocks:=TFPList.create;
  711. for i:=0 to blocks.count-1 do
  712. begin
  713. if not assigned(blocks[i]) then
  714. internalerror(200411302);
  715. n.addblock(i,pcaseblock(blocks[i])^.statement.dogetcopy);
  716. end;
  717. end
  718. else
  719. n.blocks:=nil;
  720. dogetcopy:=n;
  721. end;
  722. procedure tcasenode.insertintolist(l : tnodelist);
  723. begin
  724. end;
  725. function caselabelsequal(n1,n2: pcaselabel): boolean;
  726. begin
  727. result :=
  728. (not assigned(n1) and not assigned(n2)) or
  729. (assigned(n1) and assigned(n2) and
  730. (n1^._low = n2^._low) and
  731. (n1^._high = n2^._high) and
  732. { the rest of the fields don't matter for equality (JM) }
  733. caselabelsequal(n1^.less,n2^.less) and
  734. caselabelsequal(n1^.greater,n2^.greater))
  735. end;
  736. function caseblocksequal(b1,b2:TFPList): boolean;
  737. var
  738. i : longint;
  739. begin
  740. result:=false;
  741. if b1.count<>b2.count then
  742. exit;
  743. for i:=0 to b1.count-1 do
  744. begin
  745. if not pcaseblock(b1[i])^.statement.isequal(pcaseblock(b2[i])^.statement) then
  746. exit;
  747. end;
  748. result:=true;
  749. end;
  750. function tcasenode.docompare(p: tnode): boolean;
  751. begin
  752. result :=
  753. inherited docompare(p) and
  754. caselabelsequal(labels,tcasenode(p).labels) and
  755. caseblocksequal(blocks,tcasenode(p).blocks) and
  756. elseblock.isequal(tcasenode(p).elseblock);
  757. end;
  758. procedure tcasenode.addblock(blockid:longint;instr:tnode);
  759. var
  760. hcaseblock : pcaseblock;
  761. begin
  762. new(hcaseblock);
  763. fillchar(hcaseblock^,sizeof(hcaseblock^),0);
  764. hcaseblock^.statement:=instr;
  765. if blockid>=blocks.count then
  766. blocks.count:=blockid+1;
  767. blocks[blockid]:=hcaseblock;
  768. end;
  769. procedure tcasenode.addelseblock(instr:tnode);
  770. begin
  771. elseblock:=instr;
  772. end;
  773. procedure tcasenode.addlabel(blockid:longint;l,h : TConstExprInt);
  774. var
  775. hcaselabel : pcaselabel;
  776. function insertlabel(var p : pcaselabel):pcaselabel;
  777. begin
  778. if p=nil then
  779. begin
  780. p:=hcaselabel;
  781. result:=p;
  782. end
  783. else
  784. if (p^._low>hcaselabel^._low) and
  785. (p^._low>hcaselabel^._high) then
  786. begin
  787. if (hcaselabel^.blockid = p^.blockid) and
  788. (p^._low = hcaselabel^._high + 1) then
  789. begin
  790. p^._low := hcaselabel^._low;
  791. dispose(hcaselabel);
  792. result:=p;
  793. end
  794. else
  795. result:=insertlabel(p^.less)
  796. end
  797. else
  798. if (p^._high<hcaselabel^._low) and
  799. (p^._high<hcaselabel^._high) then
  800. begin
  801. if (hcaselabel^.blockid = p^.blockid) and
  802. (p^._high+1 = hcaselabel^._low) then
  803. begin
  804. p^._high := hcaselabel^._high;
  805. dispose(hcaselabel);
  806. result:=p;
  807. end
  808. else
  809. result:=insertlabel(p^.greater);
  810. end
  811. else
  812. begin
  813. dispose(hcaselabel);
  814. Message(parser_e_double_caselabel);
  815. end
  816. end;
  817. begin
  818. new(hcaselabel);
  819. fillchar(hcaselabel^,sizeof(tcaselabel),0);
  820. hcaselabel^.blockid:=blockid;
  821. hcaselabel^.label_type:=ltOrdinal;
  822. hcaselabel^._low:=l;
  823. hcaselabel^._high:=h;
  824. insertlabel(labels);
  825. end;
  826. procedure tcasenode.addlabel(blockid: longint; l, h: tstringconstnode);
  827. var
  828. hcaselabel : pcaselabel;
  829. function insertlabel(var p : pcaselabel) : pcaselabel;
  830. begin
  831. if not assigned(p) then
  832. begin
  833. p := hcaselabel;
  834. result := p;
  835. end
  836. else
  837. if (p^._low_str.fullcompare(hcaselabel^._high_str) > 0) then
  838. result := insertlabel(p^.less)
  839. else
  840. if (p^._high_str.fullcompare(hcaselabel^._low_str) < 0) then
  841. result := insertlabel(p^.greater)
  842. else
  843. begin
  844. hcaselabel^._low_str.free;
  845. hcaselabel^._high_str.free;
  846. dispose(hcaselabel);
  847. Message(parser_e_double_caselabel);
  848. end;
  849. end;
  850. begin
  851. new(hcaselabel);
  852. fillchar(hcaselabel^, sizeof(tcaselabel), 0);
  853. hcaselabel^.blockid := blockid;
  854. hcaselabel^.label_type := ltConstString;
  855. hcaselabel^._low_str := tstringconstnode(l.getcopy);
  856. hcaselabel^._high_str := tstringconstnode(h.getcopy);
  857. insertlabel(labels);
  858. end;
  859. begin
  860. csetelementnode:=tsetelementnode;
  861. cinnode:=tinnode;
  862. crangenode:=trangenode;
  863. ccasenode:=tcasenode;
  864. end.