nset.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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,ncon,nflw,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. labellabel : TAsmLabel;
  34. { range type }
  35. case label_type : TLabelType of
  36. ltOrdinal:
  37. (
  38. _low,
  39. _high : TConstExprInt;
  40. );
  41. ltConstString:
  42. (
  43. _low_str,
  44. _high_str : tstringconstnode;
  45. );
  46. end;
  47. pcaseblock = ^tcaseblock;
  48. tcaseblock = record
  49. { label (only used in pass_generate_code) }
  50. blocklabel : tasmlabel;
  51. { shortcut - set to true if blocklabel isn't actually unique to the
  52. case block due to one of the following conditions:
  53. - if the node contains a jump, then the label is set to that jump's destination,
  54. - if the node is empty, the label is set to the end label. }
  55. shortcut: Boolean;
  56. statementlabel : tlabelnode;
  57. { instructions }
  58. statement : tnode;
  59. end;
  60. tsetelementnode = class(tbinarynode)
  61. constructor create(l,r : tnode);virtual;
  62. function pass_typecheck:tnode;override;
  63. function pass_1 : tnode;override;
  64. end;
  65. tsetelementnodeclass = class of tsetelementnode;
  66. tinnode = class(tbinopnode)
  67. constructor create(l,r : tnode);virtual;reintroduce;
  68. function pass_typecheck:tnode;override;
  69. function simplify(forinline : boolean):tnode;override;
  70. function pass_1 : tnode;override;
  71. end;
  72. tinnodeclass = class of tinnode;
  73. trangenode = class(tbinarynode)
  74. constructor create(l,r : tnode);virtual;
  75. function pass_typecheck:tnode;override;
  76. function pass_1 : tnode;override;
  77. end;
  78. trangenodeclass = class of trangenode;
  79. tcasenode = class(tunarynode)
  80. strict private
  81. { Number of labels }
  82. flabelcnt: cardinal;
  83. { Number of individual values checked, counting each value in a range
  84. individually (e.g. 0..2 counts as 3). }
  85. flabelcoverage: qword;
  86. fcountsuptodate: boolean;
  87. function getlabelcnt: cardinal;
  88. function getlabelcoverage: qword;
  89. procedure updatecoverage;
  90. procedure checkordinalcoverage;
  91. public
  92. blocks : TFPList;
  93. elseblock : tnode;
  94. constructor create(l:tnode);virtual;
  95. destructor destroy;override;
  96. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  97. procedure ppuwrite(ppufile:tcompilerppufile);override;
  98. procedure buildderefimpl;override;
  99. procedure derefimpl;override;
  100. function dogetcopy : tnode;override;
  101. procedure printnodetree(var t:text);override;
  102. {$ifdef DEBUG_NODE_XML}
  103. procedure XMLPrintNodeTree(var t:text); override;
  104. {$endif DEBUG_NODE_XML}
  105. procedure insertintolist(l : tnodelist);override;
  106. function pass_typecheck:tnode;override;
  107. function pass_1 : tnode;override;
  108. function simplify(forinline:boolean):tnode;override;
  109. function docompare(p: tnode): boolean; override;
  110. procedure addlabel(blockid:longint;const l,h : TConstExprInt); overload;
  111. procedure addlabel(blockid:longint;l,h : tstringconstnode); overload;
  112. procedure addblock(blockid:longint;instr:tnode);
  113. procedure addelseblock(instr:tnode);
  114. property labelcnt: cardinal read getlabelcnt;
  115. { returns one less than the covered case range, so that it
  116. does not overflow for a fully covered qword range }
  117. property labelcoverage: qword read getlabelcoverage;
  118. protected
  119. flabels : pcaselabel;
  120. public
  121. property labels: pcaselabel read flabels;
  122. end;
  123. tcasenodeclass = class of tcasenode;
  124. var
  125. csetelementnode : tsetelementnodeclass = tsetelementnode;
  126. cinnode : tinnodeclass = tinnode;
  127. crangenode : trangenodeclass = trangenode;
  128. ccasenode : tcasenodeclass = tcasenode;
  129. { searches the highest label }
  130. function case_get_max(root : pcaselabel) : tconstexprint;
  131. { searches the lowest label }
  132. function case_get_min(root : pcaselabel) : tconstexprint;
  133. implementation
  134. uses
  135. verbose,cutils,
  136. symconst,symdef,symsym,symtable,defutil,defcmp,
  137. htypechk,pass_1,
  138. nadd,nbas,ncal,ncnv,nld,nutils,
  139. cgbase;
  140. {*****************************************************************************
  141. TSETELEMENTNODE
  142. *****************************************************************************}
  143. constructor tsetelementnode.create(l,r : tnode);
  144. begin
  145. inherited create(setelementn,l,r);
  146. end;
  147. function tsetelementnode.pass_typecheck:tnode;
  148. begin
  149. result:=nil;
  150. typecheckpass(left);
  151. if assigned(right) then
  152. typecheckpass(right);
  153. set_varstate(left,vs_read,[vsf_must_be_valid]);
  154. if codegenerror then
  155. exit;
  156. resultdef:=left.resultdef;
  157. end;
  158. function tsetelementnode.pass_1 : tnode;
  159. begin
  160. result:=nil;
  161. firstpass(left);
  162. if assigned(right) then
  163. firstpass(right);
  164. if codegenerror then
  165. exit;
  166. expectloc:=left.expectloc;
  167. end;
  168. {*****************************************************************************
  169. TINNODE
  170. *****************************************************************************}
  171. constructor tinnode.create(l,r : tnode);
  172. begin
  173. inherited create(inn,l,r);
  174. end;
  175. function tinnode.pass_typecheck:tnode;
  176. var
  177. t : tnode;
  178. function createsetconst(psd : tsetdef) : pconstset;
  179. var
  180. pcs : pconstset;
  181. i : longint;
  182. begin
  183. new(pcs);
  184. case psd.elementdef.typ of
  185. enumdef :
  186. begin
  187. for i := 0 to tenumdef(psd.elementdef).symtable.SymList.Count - 1 do
  188. begin
  189. include(pcs^,tenumsym(tenumdef(psd.elementdef).symtable.SymList[i]).value);
  190. end;
  191. end;
  192. orddef :
  193. begin
  194. for i:=int64(torddef(psd.elementdef).low) to int64(torddef(psd.elementdef).high) do
  195. include(pcs^,i);
  196. end;
  197. else
  198. internalerror(2019050516);
  199. end;
  200. createsetconst:=pcs;
  201. end;
  202. begin
  203. result:=nil;
  204. resultdef:=pasbool1type;
  205. typecheckpass(right);
  206. set_varstate(right,vs_read,[vsf_must_be_valid]);
  207. if codegenerror then
  208. exit;
  209. { Convert array constructor first to set }
  210. if is_array_constructor(right.resultdef) then
  211. begin
  212. arrayconstructor_to_set(right);
  213. if codegenerror then
  214. exit;
  215. end;
  216. typecheckpass(left);
  217. set_varstate(left,vs_read,[vsf_must_be_valid]);
  218. if codegenerror then
  219. exit;
  220. if not assigned(left.resultdef) then
  221. internalerror(20021126);
  222. { avoid any problems with type parameters later on }
  223. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  224. begin
  225. resultdef:=cundefinedtype;
  226. exit;
  227. end;
  228. t:=self;
  229. if isbinaryoverloaded(t,[]) then
  230. begin
  231. result:=t;
  232. exit;
  233. end;
  234. if right.resultdef.typ<>setdef then
  235. CGMessage(sym_e_set_expected);
  236. if codegenerror then
  237. exit;
  238. if (m_tp7 in current_settings.modeswitches) then
  239. begin
  240. { insert a hint that a range check error might occur on non-byte
  241. elements with the in operator.
  242. }
  243. if (
  244. (left.resultdef.typ = orddef) and not
  245. (torddef(left.resultdef).ordtype in [s8bit,u8bit,uchar,pasbool1,pasbool8,bool8bit])
  246. )
  247. or
  248. (
  249. (left.resultdef.typ = enumdef) and
  250. (tenumdef(left.resultdef).maxval > 255)
  251. )
  252. then
  253. CGMessage(type_h_in_range_check);
  254. { type conversion/check }
  255. if assigned(tsetdef(right.resultdef).elementdef) then
  256. inserttypeconv(left,tsetdef(right.resultdef).elementdef);
  257. end
  258. else if not is_ordinal(left.resultdef) or (left.resultdef.size > u32inttype.size) then
  259. begin
  260. CGMessage(type_h_in_range_check);
  261. if is_signed(left.resultdef) then
  262. inserttypeconv(left,s32inttype)
  263. else
  264. inserttypeconv(left,u32inttype);
  265. end
  266. else if assigned(tsetdef(right.resultdef).elementdef) and
  267. not(is_integer(tsetdef(right.resultdef).elementdef) and
  268. is_integer(left.resultdef)) then
  269. { Type conversion to check things like 'char in set_of_byte'. }
  270. { Can't use is_subequal because that will fail for }
  271. { 'widechar in set_of_char' }
  272. { Can't use the type conversion for integers because then }
  273. { "longint in set_of_byte" will give a range check error }
  274. { instead of false }
  275. inserttypeconv(left,tsetdef(right.resultdef).elementdef);
  276. { empty set then return false }
  277. if not assigned(tsetdef(right.resultdef).elementdef) or
  278. ((right.nodetype = setconstn) and
  279. (tnormalset(tsetconstnode(right).value_set^) = [])) then
  280. begin
  281. t:=cordconstnode.create(0,pasbool1type,false);
  282. typecheckpass(t);
  283. result:=t;
  284. exit;
  285. end;
  286. result:=simplify(false);
  287. end;
  288. function tinnode.simplify(forinline : boolean):tnode;
  289. var
  290. t : tnode;
  291. begin
  292. result:=nil;
  293. { constant evaluation }
  294. if (left.nodetype=ordconstn) then
  295. begin
  296. if (right.nodetype=setconstn) then
  297. begin
  298. { tordconstnode.value is int64 -> signed -> the expression }
  299. { below will be converted to longint on 32 bit systems due }
  300. { to the rule above -> will give range check error if }
  301. { value > high(longint) if we don't take the signedness }
  302. { into account }
  303. if Tordconstnode(left).value.signed then
  304. t:=cordconstnode.create(byte(tordconstnode(left).value.svalue in Tsetconstnode(right).value_set^),
  305. pasbool1type,true)
  306. else
  307. t:=cordconstnode.create(byte(tordconstnode(left).value.uvalue in Tsetconstnode(right).value_set^),
  308. pasbool1type,true);
  309. typecheckpass(t);
  310. result:=t;
  311. exit;
  312. end
  313. else
  314. begin
  315. if (Tordconstnode(left).value<int64(tsetdef(right.resultdef).setbase)) or
  316. (Tordconstnode(left).value>int64(Tsetdef(right.resultdef).setmax)) then
  317. begin
  318. t:=cordconstnode.create(0, pasbool1type, true);
  319. typecheckpass(t);
  320. result:=t;
  321. exit;
  322. end;
  323. end;
  324. end
  325. { a in [a] => true, if a has no side effects }
  326. else if (right.nodetype=addn) and
  327. (taddnode(right).left.nodetype=setconstn) and
  328. (tsetconstnode(taddnode(right).left).elements=0) and
  329. (taddnode(right).right.nodetype=setelementn) and
  330. (tsetelementnode(taddnode(right).right).right=nil) and
  331. ((tsetelementnode(taddnode(right).right).left.isequal(left)) or
  332. (
  333. (tsetelementnode(taddnode(right).right).left.nodetype=typeconvn) and
  334. (ttypeconvnode(tsetelementnode(taddnode(right).right).left).left.isequal(left))
  335. )
  336. ) and
  337. not(might_have_sideeffects(left,[mhs_exceptions])) then
  338. begin
  339. t:=cordconstnode.create(1, pasbool1type, true);
  340. typecheckpass(t);
  341. result:=t;
  342. exit;
  343. end
  344. { ... in [] is always false }
  345. else if is_emptyset(right) and
  346. not(might_have_sideeffects(left,[mhs_exceptions])) then
  347. begin
  348. t:=cordconstnode.create(1, pasbool1type, false);
  349. typecheckpass(t);
  350. result:=t;
  351. exit;
  352. end;
  353. end;
  354. function tinnode.pass_1 : tnode;
  355. begin
  356. result:=nil;
  357. expectloc:=LOC_REGISTER;
  358. firstpass(right);
  359. firstpass(left);
  360. if codegenerror then
  361. exit;
  362. end;
  363. {*****************************************************************************
  364. TRANGENODE
  365. *****************************************************************************}
  366. constructor trangenode.create(l,r : tnode);
  367. var
  368. value: string;
  369. begin
  370. { if right is char and left is string then }
  371. { right should be treated as one-symbol string }
  372. if is_conststringnode(l) and is_constcharnode(r) then
  373. begin
  374. value := char(tordconstnode(r).value.uvalue) + ''#0;
  375. r.free;
  376. r := cstringconstnode.createstr(value);
  377. do_typecheckpass(r);
  378. end;
  379. inherited create(rangen,l,r);
  380. end;
  381. function trangenode.pass_typecheck : tnode;
  382. begin
  383. result:=nil;
  384. typecheckpass(left);
  385. typecheckpass(right);
  386. set_varstate(left,vs_read,[vsf_must_be_valid]);
  387. set_varstate(right,vs_read,[vsf_must_be_valid]);
  388. if codegenerror then
  389. exit;
  390. { both types must be compatible }
  391. if compare_defs(left.resultdef,right.resultdef,left.nodetype)=te_incompatible then
  392. IncompatibleTypes(left.resultdef,right.resultdef);
  393. { check if only when its a constant set and
  394. ignore range nodes which are generic parameter derived }
  395. if not (nf_generic_para in flags) and (left.nodetype=ordconstn) and (right.nodetype=ordconstn) then
  396. begin
  397. { upper limit must be greater or equal than lower limit }
  398. if (tordconstnode(left).value>tordconstnode(right).value) and
  399. ((tordconstnode(left).value<0) or (tordconstnode(right).value>=0)) then
  400. CGMessage(parser_e_upper_lower_than_lower);
  401. end;
  402. resultdef:=left.resultdef;
  403. end;
  404. function trangenode.pass_1 : tnode;
  405. begin
  406. result:=nil;
  407. firstpass(left);
  408. firstpass(right);
  409. if codegenerror then
  410. exit;
  411. expectloc:=left.expectloc;
  412. end;
  413. {*****************************************************************************
  414. Case Helpers
  415. *****************************************************************************}
  416. { labels is the number of case-labels, while cases includes each individual
  417. value in a range (e.g. "0..2" counts as 3) }
  418. procedure case_count_labels(root : pcaselabel; out labels, cases: longint);
  419. procedure count(p : pcaselabel);
  420. begin
  421. inc(labels);
  422. inc(cases, (p^._high.svalue - p^._low.svalue) + 1);
  423. if assigned(p^.less) then
  424. count(p^.less);
  425. if assigned(p^.greater) then
  426. count(p^.greater);
  427. end;
  428. begin
  429. labels:=0;
  430. cases:=0;
  431. count(root);
  432. end;
  433. function case_get_max(root : pcaselabel) : tconstexprint;
  434. var
  435. hp : pcaselabel;
  436. begin
  437. hp:=root;
  438. while assigned(hp^.greater) do
  439. hp:=hp^.greater;
  440. case_get_max:=hp^._high;
  441. end;
  442. function case_get_min(root : pcaselabel) : tconstexprint;
  443. var
  444. hp : pcaselabel;
  445. begin
  446. hp:=root;
  447. while assigned(hp^.less) do
  448. hp:=hp^.less;
  449. case_get_min:=hp^._low;
  450. end;
  451. procedure deletecaselabels(p : pcaselabel);
  452. begin
  453. if assigned(p^.greater) then
  454. deletecaselabels(p^.greater);
  455. if assigned(p^.less) then
  456. deletecaselabels(p^.less);
  457. if (p^.label_type = ltConstString) then
  458. begin
  459. p^._low_str.Free;
  460. p^._high_str.Free;
  461. end;
  462. dispose(p);
  463. end;
  464. function copycaselabel(p : pcaselabel) : pcaselabel;
  465. var
  466. n : pcaselabel;
  467. begin
  468. new(n);
  469. n^:=p^;
  470. if (p^.label_type = ltConstString) then
  471. begin
  472. n^._low_str := tstringconstnode(p^._low_str.getcopy);
  473. n^._high_str := tstringconstnode(p^._high_str.getcopy);
  474. end;
  475. if assigned(p^.greater) then
  476. n^.greater:=copycaselabel(p^.greater);
  477. if assigned(p^.less) then
  478. n^.less:=copycaselabel(p^.less);
  479. copycaselabel:=n;
  480. end;
  481. procedure ppuwritecaselabel(ppufile:tcompilerppufile;p : pcaselabel);
  482. var
  483. b : byte;
  484. begin
  485. ppufile.putboolean(p^.label_type = ltConstString);
  486. if (p^.label_type = ltConstString) then
  487. begin
  488. p^._low_str.ppuwrite(ppufile);
  489. p^._high_str.ppuwrite(ppufile);
  490. end
  491. else
  492. begin
  493. ppufile.putexprint(p^._low);
  494. ppufile.putexprint(p^._high);
  495. end;
  496. ppufile.putlongint(p^.blockid);
  497. b:=ord(assigned(p^.greater)) or (ord(assigned(p^.less)) shl 1);
  498. ppufile.putbyte(b);
  499. if assigned(p^.greater) then
  500. ppuwritecaselabel(ppufile,p^.greater);
  501. if assigned(p^.less) then
  502. ppuwritecaselabel(ppufile,p^.less);
  503. end;
  504. function ppuloadcaselabel(ppufile:tcompilerppufile):pcaselabel;
  505. var
  506. b : byte;
  507. p : pcaselabel;
  508. begin
  509. new(p);
  510. if ppufile.getboolean then
  511. begin
  512. p^.label_type := ltConstString;
  513. p^._low_str := cstringconstnode.ppuload(stringconstn,ppufile);
  514. p^._high_str := cstringconstnode.ppuload(stringconstn,ppufile);
  515. end
  516. else
  517. begin
  518. p^.label_type := ltOrdinal;
  519. p^._low:=ppufile.getexprint;
  520. p^._high:=ppufile.getexprint;
  521. end;
  522. p^.blockid:=ppufile.getlongint;
  523. b:=ppufile.getbyte;
  524. if (b and 1)=1 then
  525. p^.greater:=ppuloadcaselabel(ppufile)
  526. else
  527. p^.greater:=nil;
  528. if (b and 2)=2 then
  529. p^.less:=ppuloadcaselabel(ppufile)
  530. else
  531. p^.less:=nil;
  532. ppuloadcaselabel:=p;
  533. end;
  534. {*****************************************************************************
  535. TCASENODE
  536. *****************************************************************************}
  537. constructor tcasenode.create(l:tnode);
  538. begin
  539. inherited create(casen,l);
  540. flabels:=nil;
  541. blocks:=TFPList.create;
  542. elseblock:=nil;
  543. end;
  544. destructor tcasenode.destroy;
  545. var
  546. i : longint;
  547. hp : pcaseblock;
  548. begin
  549. elseblock.free;
  550. deletecaselabels(flabels);
  551. for i:=0 to blocks.count-1 do
  552. begin
  553. pcaseblock(blocks[i])^.statement.free;
  554. hp:=pcaseblock(blocks[i]);
  555. dispose(hp);
  556. end;
  557. blocks.free;
  558. inherited destroy;
  559. end;
  560. constructor tcasenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  561. var
  562. cnt,i : longint;
  563. begin
  564. inherited ppuload(t,ppufile);
  565. elseblock:=ppuloadnode(ppufile);
  566. cnt:=ppufile.getlongint();
  567. blocks:=TFPList.create;
  568. for i:=0 to cnt-1 do
  569. addblock(i,ppuloadnode(ppufile));
  570. flabels:=ppuloadcaselabel(ppufile);
  571. { we don't save/restore the label counts, but recalculate them if needed }
  572. fcountsuptodate:=false;
  573. end;
  574. procedure tcasenode.ppuwrite(ppufile:tcompilerppufile);
  575. var
  576. i : longint;
  577. begin
  578. inherited ppuwrite(ppufile);
  579. ppuwritenode(ppufile,elseblock);
  580. ppufile.putlongint(blocks.count);
  581. for i:=0 to blocks.count-1 do
  582. ppuwritenode(ppufile,pcaseblock(blocks[i])^.statement);
  583. ppuwritecaselabel(ppufile,flabels);
  584. { we don't save/restore the label counts, but recalculate them if needed }
  585. end;
  586. procedure tcasenode.buildderefimpl;
  587. var
  588. i : integer;
  589. begin
  590. inherited buildderefimpl;
  591. if assigned(elseblock) then
  592. elseblock.buildderefimpl;
  593. for i:=0 to blocks.count-1 do
  594. pcaseblock(blocks[i])^.statement.buildderefimpl;
  595. end;
  596. procedure tcasenode.derefimpl;
  597. var
  598. i : integer;
  599. begin
  600. inherited derefimpl;
  601. if assigned(elseblock) then
  602. elseblock.derefimpl;
  603. for i:=0 to blocks.count-1 do
  604. pcaseblock(blocks[i])^.statement.derefimpl;
  605. end;
  606. function tcasenode.pass_typecheck : tnode;
  607. var
  608. i : integer;
  609. begin
  610. result:=nil;
  611. do_typecheckpass(left);
  612. for i:=0 to blocks.count-1 do
  613. typecheckpass(pcaseblock(blocks[i])^.statement);
  614. if assigned(elseblock) then
  615. typecheckpass(elseblock);
  616. resultdef:=voidtype;
  617. result:=simplify(false);
  618. end;
  619. type
  620. TLinkedListCaseLabelItem = class(TLinkedListItem)
  621. casenode: pcaselabel;
  622. constructor create(c: pcaselabel);
  623. end;
  624. constructor TLinkedListCaseLabelItem.create(c: pcaselabel);
  625. begin
  626. inherited create;
  627. casenode:=c;
  628. end;
  629. function tcasenode.pass_1 : tnode;
  630. var
  631. i: integer;
  632. node_thenblock, node_elseblock, if_node,temp_cleanup : tnode;
  633. tempcaseexpr : ttempcreatenode;
  634. if_block, init_block: tblocknode;
  635. stmt: tstatementnode;
  636. procedure add_label_to_blockid_list(list: tfpobjectlist; lab: pcaselabel);
  637. begin
  638. if not assigned(lab) then
  639. exit;
  640. if not assigned(list[lab^.blockid]) then
  641. list[lab^.blockid]:=tfpobjectlist.create(true);
  642. tfpobjectlist(list[lab^.blockid]).add(TLinkedListCaseLabelItem.create(lab));
  643. add_label_to_blockid_list(list,lab^.less);
  644. add_label_to_blockid_list(list,lab^.greater);
  645. end;
  646. function order_labels_by_blockid: tfpobjectlist;
  647. begin
  648. result:=tfpobjectlist.create(true);
  649. result.count:=blocks.count;
  650. add_label_to_blockid_list(result,flabels);
  651. end;
  652. function makeifblock(elseblock : tnode): tnode;
  653. var
  654. i, j: longint;
  655. check: taddnode;
  656. newcheck: ^taddnode;
  657. blocklist, lablist: tfpobjectlist;
  658. labitem: pcaselabel;
  659. begin
  660. result:=elseblock;
  661. blocklist:=order_labels_by_blockid;
  662. { in reverse order so that the case options at the start of the case
  663. statement are evaluated first, as they presumably are the most
  664. common }
  665. for i:=blocklist.count-1 downto 0 do
  666. begin
  667. lablist:=tfpobjectlist(blocklist[i]);
  668. check:=nil;
  669. for j:=0 to lablist.count-1 do
  670. begin
  671. if assigned(check) then
  672. begin
  673. check:=caddnode.create(orn,check,nil);
  674. newcheck:[email protected]
  675. end
  676. else
  677. newcheck:=@check;
  678. labitem:=TLinkedListCaseLabelItem(lablist[j]).casenode;
  679. newcheck^:=caddnode.create(equaln,left.getcopy,labitem^._low_str.getcopy);
  680. if (labitem^._low_str.fullcompare(labitem^._high_str)<>0) then
  681. begin
  682. newcheck^.nodetype:=gten;
  683. newcheck^:=caddnode.create(
  684. andn,newcheck^,caddnode.create(
  685. lten,left.getcopy,labitem^._high_str.getcopy));
  686. end;
  687. end;
  688. result:=cifnode.create(check,
  689. pcaseblock(blocks[i])^.statement,result);
  690. pcaseblock(blocks[i])^.statement:=nil;
  691. end;
  692. { will free its elements too because of create(true) }
  693. blocklist.free;
  694. typecheckpass(result);
  695. end;
  696. begin
  697. result:=nil;
  698. init_block:=nil;
  699. temp_cleanup:=nil;
  700. expectloc:=LOC_VOID;
  701. { only do in pass_1, so that simplify can run first and
  702. 1) possibly simplify the case node without triggering a warning
  703. 2) possibly give a compile-time error if not all cases are handled
  704. in ISO/Extended Pascal mode }
  705. if is_ordinal(left.resultdef) then
  706. checkordinalcoverage;
  707. { ideally this would be in simplify, but then checkordinalcoverage can
  708. false positives about case statements not handling all cases }
  709. if assigned(elseblock) and
  710. has_no_code(elseblock) then
  711. begin
  712. elseblock.free;
  713. elseblock:=nil;
  714. end;
  715. { evalutes the case expression }
  716. firstpass(left);
  717. set_varstate(left,vs_read,[vsf_must_be_valid]);
  718. if codegenerror then
  719. exit;
  720. { Load caseexpr into temp var if complex. }
  721. { No need to do this for ordinal, because }
  722. { in that case caseexpr is generated once }
  723. if (flabels^.label_type = ltConstString) and (not valid_for_addr(left, false)) and
  724. (blocks.count > 0) then
  725. begin
  726. init_block := internalstatements(stmt);
  727. tempcaseexpr :=
  728. ctempcreatenode.create(
  729. left.resultdef, left.resultdef.size, tt_persistent, true);
  730. temp_cleanup := ctempdeletenode.create(tempcaseexpr);
  731. typecheckpass(tnode(tempcaseexpr));
  732. addstatement(stmt, tempcaseexpr);
  733. addstatement(
  734. stmt, cassignmentnode.create(
  735. ctemprefnode.create(tempcaseexpr), left));
  736. left := ctemprefnode.create(tempcaseexpr);
  737. typecheckpass(left);
  738. end;
  739. { first case }
  740. for i:=0 to blocks.count-1 do
  741. firstpass(pcaseblock(blocks[i])^.statement);
  742. { may be handle else tree }
  743. if assigned(elseblock) then
  744. begin
  745. firstpass(elseblock);
  746. { kill case? }
  747. if blocks.count=0 then
  748. begin
  749. result:=elseblock;
  750. elseblock:=nil;
  751. exit;
  752. end;
  753. end
  754. else
  755. if blocks.count=0 then
  756. begin
  757. result:=cnothingnode.create;
  758. exit;
  759. end;
  760. if (flabels^.label_type = ltConstString) then
  761. begin
  762. if_node:=makeifblock(elseblock);
  763. if assigned(init_block) then
  764. firstpass(tnode(init_block));
  765. if_block:=internalstatements(stmt);
  766. if assigned(init_block) then
  767. addstatement(stmt, init_block);
  768. addstatement(stmt,if_node);
  769. if assigned(temp_cleanup) then
  770. addstatement(stmt,temp_cleanup);
  771. result:=if_block;
  772. elseblock:= nil;
  773. exit;
  774. end;
  775. if is_boolean(left.resultdef) then
  776. begin
  777. case blocks.count of
  778. 2:
  779. begin
  780. if boolean(qword(flabels^._low))=false then
  781. begin
  782. node_thenblock:=pcaseblock(blocks[flabels^.greater^.blockid])^.statement;
  783. node_elseblock:=pcaseblock(blocks[flabels^.blockid])^.statement;
  784. pcaseblock(blocks[flabels^.greater^.blockid])^.statement:=nil;
  785. end
  786. else
  787. begin
  788. node_thenblock:=pcaseblock(blocks[flabels^.blockid])^.statement;
  789. node_elseblock:=pcaseblock(blocks[flabels^.less^.blockid])^.statement;
  790. pcaseblock(blocks[flabels^.less^.blockid])^.statement:=nil;
  791. end;
  792. pcaseblock(blocks[flabels^.blockid])^.statement:=nil;
  793. end;
  794. 1:
  795. begin
  796. if flabels^._low=flabels^._high then
  797. begin
  798. if boolean(qword(flabels^._low))=false then
  799. begin
  800. node_thenblock:=elseblock;
  801. node_elseblock:=pcaseblock(blocks[flabels^.blockid])^.statement;
  802. end
  803. else
  804. begin
  805. node_thenblock:=pcaseblock(blocks[flabels^.blockid])^.statement;
  806. node_elseblock:=elseblock;
  807. end;
  808. pcaseblock(blocks[flabels^.blockid])^.statement:=nil;
  809. elseblock:=nil;
  810. end
  811. else
  812. begin
  813. result:=pcaseblock(blocks[flabels^.blockid])^.statement;
  814. pcaseblock(blocks[flabels^.blockid])^.statement:=nil;
  815. elseblock:=nil;
  816. exit;
  817. end;
  818. end;
  819. else
  820. internalerror(200805031);
  821. end;
  822. result:=cifnode.create(left,node_thenblock,node_elseblock);
  823. left:=nil;
  824. exit;
  825. end;
  826. { convert single case branch into if-statement }
  827. if (flabels^.greater=nil) and (flabels^.less=nil) then
  828. if flabels^.label_type=ltOrdinal then
  829. begin
  830. if flabels^._low=flabels^._high then
  831. begin
  832. result:=cifnode.create_internal(
  833. caddnode.create_internal(equaln,left.getcopy,cordconstnode.create(flabels^._low,left.resultdef,false)),
  834. pcaseblock(blocks[flabels^.blockid])^.statement,elseblock);
  835. end
  836. else if not(might_have_sideeffects(left,[mhs_exceptions])) and (node_complexity(left)<=1) then
  837. begin
  838. result:=cifnode.create_internal(
  839. caddnode.create_internal(andn,
  840. caddnode.create_internal(gten,left.getcopy,cordconstnode.create(flabels^._low,left.resultdef,false)),
  841. caddnode.create_internal(lten,left.getcopy,cordconstnode.create(flabels^._high,left.resultdef,false))
  842. ),
  843. pcaseblock(blocks[flabels^.blockid])^.statement,elseblock);
  844. end
  845. else
  846. begin
  847. init_block:=internalstatements(stmt);
  848. tempcaseexpr:=ctempcreatenode.create(
  849. left.resultdef,left.resultdef.size,tt_persistent,true);
  850. temp_cleanup:=ctempdeletenode.create(tempcaseexpr);
  851. typecheckpass(tnode(tempcaseexpr));
  852. addstatement(stmt,tempcaseexpr);
  853. addstatement(stmt,cassignmentnode.create(
  854. ctemprefnode.create(tempcaseexpr),left.getcopy));
  855. left:=ctemprefnode.create(tempcaseexpr);
  856. typecheckpass(left);
  857. addstatement(stmt,cifnode.create_internal(
  858. caddnode.create_internal(andn,
  859. caddnode.create_internal(gten,left.getcopy,cordconstnode.create(flabels^._low,left.resultdef,false)),
  860. caddnode.create_internal(lten,left.getcopy,cordconstnode.create(flabels^._high,left.resultdef,false))
  861. ),
  862. pcaseblock(blocks[flabels^.blockid])^.statement,elseblock));
  863. addstatement(stmt,temp_cleanup);
  864. result:=init_block;
  865. end;
  866. elseblock:=nil;
  867. pcaseblock(blocks[flabels^.blockid])^.statement:=nil;
  868. exit;
  869. end;
  870. end;
  871. function tcasenode.simplify(forinline:boolean):tnode;
  872. var
  873. tmp: pcaselabel;
  874. begin
  875. result:=nil;
  876. if left.nodetype=ordconstn then
  877. begin
  878. tmp:=flabels;
  879. { check all case labels until we find one that fits }
  880. while assigned(tmp) do
  881. begin
  882. if (tmp^._low<=tordconstnode(left).value) and
  883. (tmp^._high>=tordconstnode(left).value) then
  884. begin
  885. if tmp^.blockid>=blocks.count then
  886. internalerror(2014022101);
  887. result:=pcaseblock(blocks[tmp^.blockid])^.statement;
  888. if not assigned(result) then
  889. internalerror(2014022102);
  890. result:=result.getcopy;
  891. exit;
  892. end;
  893. if tmp^._high<tordconstnode(left).value then
  894. tmp:=tmp^.greater
  895. else
  896. tmp:=tmp^.less;
  897. end;
  898. { no label did match; use the else block if available }
  899. if assigned(elseblock) then
  900. result:=elseblock.getcopy
  901. else
  902. begin
  903. if ([m_iso,m_extpas]*current_settings.modeswitches)<>[] then
  904. cgmessage1(cg_e_case_missing_value,tostr(tordconstnode(left).value))
  905. else
  906. cgmessage(cg_w_case_incomplete);
  907. { no else block, so there is no code to execute at all }
  908. result:=cnothingnode.create;
  909. end;
  910. end;
  911. end;
  912. function tcasenode.dogetcopy : tnode;
  913. var
  914. n : tcasenode;
  915. i : longint;
  916. begin
  917. n:=tcasenode(inherited dogetcopy);
  918. if assigned(elseblock) then
  919. n.elseblock:=elseblock.dogetcopy
  920. else
  921. n.elseblock:=nil;
  922. if assigned(flabels) then
  923. n.flabels:=copycaselabel(flabels)
  924. else
  925. n.flabels:=nil;
  926. if assigned(blocks) then
  927. begin
  928. n.blocks:=TFPList.create;
  929. for i:=0 to blocks.count-1 do
  930. begin
  931. if not assigned(blocks[i]) then
  932. internalerror(200411302);
  933. n.addblock(i,pcaseblock(blocks[i])^.statement.dogetcopy);
  934. end;
  935. end
  936. else
  937. n.blocks:=nil;
  938. n.fcountsuptodate:=fcountsuptodate;
  939. n.flabelcnt:=flabelcnt;
  940. n.flabelcoverage:=flabelcoverage;
  941. dogetcopy:=n;
  942. end;
  943. procedure tcasenode.printnodetree(var t: text);
  944. var
  945. i : longint;
  946. begin
  947. write(t,printnodeindention,'(');
  948. printnodeindent;
  949. printnodeinfo(t);
  950. writeln(t);
  951. printnode(t,left);
  952. i:=0;
  953. for i:=0 to blocks.count-1 do
  954. begin
  955. writeln(t,printnodeindention,'(caseblock blockid: ',i);
  956. printnodeindent;
  957. printnode(t,pcaseblock(blocks[i])^.statement);
  958. printnodeunindent;
  959. writeln(t,printnodeindention,')');
  960. end;
  961. if assigned(elseblock) then
  962. begin
  963. writeln(t,printnodeindention,'(else: ',i);
  964. printnodeindent;
  965. printnode(t,elseblock);
  966. printnodeunindent;
  967. writeln(t,printnodeindention,')');
  968. end;
  969. printnodeunindent;
  970. writeln(t,printnodeindention,')');
  971. end;
  972. {$ifdef DEBUG_NODE_XML}
  973. procedure TCaseNode.XMLPrintNodeTree(var T: Text);
  974. var
  975. i : longint;
  976. begin
  977. Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
  978. XMLPrintNodeInfo(T);
  979. WriteLn(T, '>');
  980. PrintNodeIndent;
  981. WriteLn(T, PrintNodeIndention, '<condition>');
  982. PrintNodeIndent;
  983. XMLPrintNode(T, Left);
  984. PrintNodeUnindent;
  985. WriteLn(T, PrintNodeIndention, '</condition>');
  986. i:=0;
  987. for i:=0 to blocks.count-1 do
  988. begin
  989. WriteLn(T, PrintNodeIndention, '<block id="', i, '">');
  990. PrintNodeIndent;
  991. XMLPrintNode(T, PCaseBlock(blocks[i])^.statement);
  992. PrintNodeUnindent;
  993. WriteLn(T, PrintNodeIndention, '</block>');
  994. end;
  995. if assigned(elseblock) then
  996. begin
  997. WriteLn(T, PrintNodeIndention, '<block id="else">');
  998. PrintNodeIndent;
  999. XMLPrintNode(T, ElseBlock);
  1000. PrintNodeUnindent;
  1001. WriteLn(T, PrintNodeIndention, '</block>');
  1002. end;
  1003. PrintNodeUnindent;
  1004. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  1005. end;
  1006. {$endif DEBUG_NODE_XML}
  1007. procedure tcasenode.insertintolist(l : tnodelist);
  1008. begin
  1009. end;
  1010. function caselabelsequal(n1,n2: pcaselabel): boolean;
  1011. begin
  1012. result :=
  1013. (not assigned(n1) and not assigned(n2)) or
  1014. (assigned(n1) and assigned(n2) and
  1015. (n1^._low = n2^._low) and
  1016. (n1^._high = n2^._high) and
  1017. { the rest of the fields don't matter for equality (JM) }
  1018. caselabelsequal(n1^.less,n2^.less) and
  1019. caselabelsequal(n1^.greater,n2^.greater))
  1020. end;
  1021. function caseblocksequal(b1,b2:TFPList): boolean;
  1022. var
  1023. i : longint;
  1024. begin
  1025. result:=false;
  1026. if b1.count<>b2.count then
  1027. exit;
  1028. for i:=0 to b1.count-1 do
  1029. begin
  1030. if not pcaseblock(b1[i])^.statement.isequal(pcaseblock(b2[i])^.statement) then
  1031. exit;
  1032. end;
  1033. result:=true;
  1034. end;
  1035. function tcasenode.docompare(p: tnode): boolean;
  1036. begin
  1037. result :=
  1038. inherited docompare(p) and
  1039. caselabelsequal(flabels,tcasenode(p).flabels) and
  1040. caseblocksequal(blocks,tcasenode(p).blocks) and
  1041. elseblock.isequal(tcasenode(p).elseblock);
  1042. end;
  1043. procedure tcasenode.addblock(blockid:longint;instr:tnode);
  1044. var
  1045. hcaseblock : pcaseblock;
  1046. begin
  1047. new(hcaseblock);
  1048. fillchar(hcaseblock^,sizeof(hcaseblock^),0);
  1049. hcaseblock^.statement:=instr;
  1050. if blockid>=blocks.count then
  1051. blocks.count:=blockid+1;
  1052. blocks[blockid]:=hcaseblock;
  1053. end;
  1054. procedure tcasenode.addelseblock(instr:tnode);
  1055. begin
  1056. elseblock:=instr;
  1057. end;
  1058. function tcasenode.getlabelcnt: cardinal;
  1059. begin
  1060. if not fcountsuptodate then
  1061. updatecoverage;
  1062. result:=flabelcnt;
  1063. end;
  1064. function tcasenode.getlabelcoverage: qword;
  1065. begin
  1066. if not fcountsuptodate then
  1067. updatecoverage;
  1068. result:=flabelcoverage;
  1069. end;
  1070. procedure tcasenode.updatecoverage;
  1071. var
  1072. isord, first: boolean;
  1073. procedure count(p : pcaselabel);
  1074. begin
  1075. inc(flabelcnt);
  1076. if isord then
  1077. begin
  1078. flabelcoverage:=flabelcoverage + (p^._high - p^._low);
  1079. { ensure we don't overflow in case it covers the
  1080. full range of qword }
  1081. if not first then
  1082. inc(flabelcoverage);
  1083. first:=false;
  1084. end;
  1085. if assigned(p^.less) then
  1086. count(p^.less);
  1087. if assigned(p^.greater) then
  1088. count(p^.greater);
  1089. end;
  1090. begin
  1091. isord:=is_ordinal(left.resultdef);
  1092. flabelcnt:=0;
  1093. flabelcoverage:=0;
  1094. first:=true;
  1095. count(flabels);
  1096. fcountsuptodate:=true;
  1097. end;
  1098. procedure tcasenode.checkordinalcoverage;
  1099. function orddefspansfullrange(def: torddef): boolean;
  1100. var
  1101. packedbitsize: cardinal;
  1102. dummy: longint;
  1103. val: qword;
  1104. begin
  1105. result:=false;
  1106. packedbitsize:=def.packedbitsize;
  1107. if ((packedbitsize mod 8) <> 0) or
  1108. not ispowerof2(packedbitsize div 8,dummy) then
  1109. exit;
  1110. dec(packedbitsize);
  1111. if is_signed(def) then
  1112. begin
  1113. {$push}{$q-}
  1114. if def.low<>(-(int64(1) shl packedbitsize)) then
  1115. exit;
  1116. if def.high<>((int64(1) shl packedbitsize)-1) then
  1117. exit;
  1118. {$pop}
  1119. end
  1120. else
  1121. begin
  1122. if def.low<>0 then
  1123. exit;
  1124. val:=qword(1) shl packedbitsize;
  1125. val:=(val-1)+val;
  1126. if def.high<>val then
  1127. exit;
  1128. end;
  1129. result:=true;
  1130. end;
  1131. var
  1132. lv, hv, typcount: tconstexprint;
  1133. begin
  1134. { Check label type coverage for enumerations and small types }
  1135. getrange(left.resultdef,lv,hv);
  1136. { low/high value of c-style booleans are not suitable for calculating their "type count" }
  1137. if is_cbool(left.resultdef) then
  1138. typcount:=1
  1139. else
  1140. typcount:=hv-lv;
  1141. if not assigned(elseblock) then
  1142. begin
  1143. { unless cs_check_all_case_coverage is set, only check for enums, booleans and
  1144. subrange types different from the default ones }
  1145. if (cs_check_all_case_coverage in current_settings.localswitches) or
  1146. (is_enum(left.resultdef) or
  1147. is_boolean(left.resultdef) or
  1148. not orddefspansfullrange(torddef(left.resultdef))) and
  1149. (labelcoverage<typcount) then
  1150. begin
  1151. { labels for some values of the operand are missing, and no else block is present }
  1152. cgmessage(cg_w_case_incomplete);
  1153. { in Standard/Extended Pascal, this is a dynamic violation error if it actually happens }
  1154. if ([m_extpas,m_iso]*current_settings.modeswitches)<>[] then
  1155. begin
  1156. elseblock:=ccallnode.createintern('fpc_rangeerror',nil);
  1157. typecheckpass(elseblock);
  1158. end;
  1159. end
  1160. end
  1161. else if labelcoverage=typcount then
  1162. begin
  1163. { labels for all values of the operand are present, but an extra else block is present }
  1164. MessagePos(elseblock.fileinfo, cg_w_unreachable_code);
  1165. end;
  1166. end;
  1167. procedure tcasenode.addlabel(blockid:longint;const l,h : TConstExprInt);
  1168. var
  1169. hcaselabel : pcaselabel;
  1170. function insertlabel(var p : pcaselabel):pcaselabel;
  1171. begin
  1172. if p=nil then
  1173. begin
  1174. p:=hcaselabel;
  1175. result:=p;
  1176. end
  1177. else
  1178. if (p^._low>hcaselabel^._low) and
  1179. (p^._low>hcaselabel^._high) then
  1180. begin
  1181. if (hcaselabel^.blockid = p^.blockid) and
  1182. (p^._low = hcaselabel^._high + 1) then
  1183. begin
  1184. p^._low := hcaselabel^._low;
  1185. dispose(hcaselabel);
  1186. result:=p;
  1187. end
  1188. else
  1189. result:=insertlabel(p^.less)
  1190. end
  1191. else
  1192. if (p^._high<hcaselabel^._low) and
  1193. (p^._high<hcaselabel^._high) then
  1194. begin
  1195. if (hcaselabel^.blockid = p^.blockid) and
  1196. (p^._high+1 = hcaselabel^._low) then
  1197. begin
  1198. p^._high := hcaselabel^._high;
  1199. dispose(hcaselabel);
  1200. result:=p;
  1201. end
  1202. else
  1203. result:=insertlabel(p^.greater);
  1204. end
  1205. else
  1206. begin
  1207. dispose(hcaselabel);
  1208. Message(parser_e_double_caselabel);
  1209. result:=nil;
  1210. end
  1211. end;
  1212. begin
  1213. new(hcaselabel);
  1214. fillchar(hcaselabel^,sizeof(tcaselabel),0);
  1215. hcaselabel^.blockid:=blockid;
  1216. hcaselabel^.label_type:=ltOrdinal;
  1217. hcaselabel^._low:=l;
  1218. hcaselabel^._high:=h;
  1219. insertlabel(flabels);
  1220. fcountsuptodate:=false;
  1221. end;
  1222. procedure tcasenode.addlabel(blockid: longint; l, h: tstringconstnode);
  1223. var
  1224. hcaselabel : pcaselabel;
  1225. function insertlabel(var p : pcaselabel) : pcaselabel;
  1226. begin
  1227. if not assigned(p) then
  1228. begin
  1229. p := hcaselabel;
  1230. result := p;
  1231. end
  1232. else
  1233. if (p^._low_str.fullcompare(hcaselabel^._high_str) > 0) then
  1234. result := insertlabel(p^.less)
  1235. else
  1236. if (p^._high_str.fullcompare(hcaselabel^._low_str) < 0) then
  1237. result := insertlabel(p^.greater)
  1238. else
  1239. begin
  1240. hcaselabel^._low_str.free;
  1241. hcaselabel^._high_str.free;
  1242. dispose(hcaselabel);
  1243. Message(parser_e_double_caselabel);
  1244. result:=nil;
  1245. end;
  1246. end;
  1247. begin
  1248. new(hcaselabel);
  1249. fillchar(hcaselabel^, sizeof(tcaselabel), 0);
  1250. hcaselabel^.blockid := blockid;
  1251. hcaselabel^.label_type := ltConstString;
  1252. hcaselabel^._low_str := tstringconstnode(l.getcopy);
  1253. hcaselabel^._high_str := tstringconstnode(h.getcopy);
  1254. insertlabel(flabels);
  1255. end;
  1256. end.