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 not (nf_generic_para in left.flags) and not (nf_generic_para in right.flags) and
  392. (compare_defs(left.resultdef,right.resultdef,left.nodetype)=te_incompatible) then
  393. IncompatibleTypes(left.resultdef,right.resultdef);
  394. { check if only when its a constant set and
  395. ignore range nodes which are generic parameter derived }
  396. if not (nf_generic_para in flags) and (left.nodetype=ordconstn) and (right.nodetype=ordconstn) then
  397. begin
  398. { upper limit must be greater or equal than lower limit }
  399. if (tordconstnode(left).value>tordconstnode(right).value) and
  400. ((tordconstnode(left).value<0) or (tordconstnode(right).value>=0)) then
  401. CGMessage(parser_e_upper_lower_than_lower);
  402. end;
  403. resultdef:=left.resultdef;
  404. end;
  405. function trangenode.pass_1 : tnode;
  406. begin
  407. result:=nil;
  408. firstpass(left);
  409. firstpass(right);
  410. if codegenerror then
  411. exit;
  412. expectloc:=left.expectloc;
  413. end;
  414. {*****************************************************************************
  415. Case Helpers
  416. *****************************************************************************}
  417. { labels is the number of case-labels, while cases includes each individual
  418. value in a range (e.g. "0..2" counts as 3) }
  419. procedure case_count_labels(root : pcaselabel; out labels, cases: longint);
  420. procedure count(p : pcaselabel);
  421. begin
  422. inc(labels);
  423. inc(cases, (p^._high.svalue - p^._low.svalue) + 1);
  424. if assigned(p^.less) then
  425. count(p^.less);
  426. if assigned(p^.greater) then
  427. count(p^.greater);
  428. end;
  429. begin
  430. labels:=0;
  431. cases:=0;
  432. count(root);
  433. end;
  434. function case_get_max(root : pcaselabel) : tconstexprint;
  435. var
  436. hp : pcaselabel;
  437. begin
  438. hp:=root;
  439. while assigned(hp^.greater) do
  440. hp:=hp^.greater;
  441. case_get_max:=hp^._high;
  442. end;
  443. function case_get_min(root : pcaselabel) : tconstexprint;
  444. var
  445. hp : pcaselabel;
  446. begin
  447. hp:=root;
  448. while assigned(hp^.less) do
  449. hp:=hp^.less;
  450. case_get_min:=hp^._low;
  451. end;
  452. procedure deletecaselabels(p : pcaselabel);
  453. begin
  454. if assigned(p^.greater) then
  455. deletecaselabels(p^.greater);
  456. if assigned(p^.less) then
  457. deletecaselabels(p^.less);
  458. if (p^.label_type = ltConstString) then
  459. begin
  460. p^._low_str.Free;
  461. p^._high_str.Free;
  462. end;
  463. dispose(p);
  464. end;
  465. function copycaselabel(p : pcaselabel) : pcaselabel;
  466. var
  467. n : pcaselabel;
  468. begin
  469. new(n);
  470. n^:=p^;
  471. if (p^.label_type = ltConstString) then
  472. begin
  473. n^._low_str := tstringconstnode(p^._low_str.getcopy);
  474. n^._high_str := tstringconstnode(p^._high_str.getcopy);
  475. end;
  476. if assigned(p^.greater) then
  477. n^.greater:=copycaselabel(p^.greater);
  478. if assigned(p^.less) then
  479. n^.less:=copycaselabel(p^.less);
  480. copycaselabel:=n;
  481. end;
  482. procedure ppuwritecaselabel(ppufile:tcompilerppufile;p : pcaselabel);
  483. var
  484. b : byte;
  485. begin
  486. ppufile.putboolean(p^.label_type = ltConstString);
  487. if (p^.label_type = ltConstString) then
  488. begin
  489. p^._low_str.ppuwrite(ppufile);
  490. p^._high_str.ppuwrite(ppufile);
  491. end
  492. else
  493. begin
  494. ppufile.putexprint(p^._low);
  495. ppufile.putexprint(p^._high);
  496. end;
  497. ppufile.putlongint(p^.blockid);
  498. b:=ord(assigned(p^.greater)) or (ord(assigned(p^.less)) shl 1);
  499. ppufile.putbyte(b);
  500. if assigned(p^.greater) then
  501. ppuwritecaselabel(ppufile,p^.greater);
  502. if assigned(p^.less) then
  503. ppuwritecaselabel(ppufile,p^.less);
  504. end;
  505. function ppuloadcaselabel(ppufile:tcompilerppufile):pcaselabel;
  506. var
  507. b : byte;
  508. p : pcaselabel;
  509. begin
  510. new(p);
  511. if ppufile.getboolean then
  512. begin
  513. p^.label_type := ltConstString;
  514. p^._low_str := cstringconstnode.ppuload(stringconstn,ppufile);
  515. p^._high_str := cstringconstnode.ppuload(stringconstn,ppufile);
  516. end
  517. else
  518. begin
  519. p^.label_type := ltOrdinal;
  520. p^._low:=ppufile.getexprint;
  521. p^._high:=ppufile.getexprint;
  522. end;
  523. p^.blockid:=ppufile.getlongint;
  524. b:=ppufile.getbyte;
  525. if (b and 1)=1 then
  526. p^.greater:=ppuloadcaselabel(ppufile)
  527. else
  528. p^.greater:=nil;
  529. if (b and 2)=2 then
  530. p^.less:=ppuloadcaselabel(ppufile)
  531. else
  532. p^.less:=nil;
  533. ppuloadcaselabel:=p;
  534. end;
  535. {*****************************************************************************
  536. TCASENODE
  537. *****************************************************************************}
  538. constructor tcasenode.create(l:tnode);
  539. begin
  540. inherited create(casen,l);
  541. flabels:=nil;
  542. blocks:=TFPList.create;
  543. elseblock:=nil;
  544. end;
  545. destructor tcasenode.destroy;
  546. var
  547. i : longint;
  548. hp : pcaseblock;
  549. begin
  550. elseblock.free;
  551. deletecaselabels(flabels);
  552. for i:=0 to blocks.count-1 do
  553. begin
  554. pcaseblock(blocks[i])^.statement.free;
  555. hp:=pcaseblock(blocks[i]);
  556. dispose(hp);
  557. end;
  558. blocks.free;
  559. inherited destroy;
  560. end;
  561. constructor tcasenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  562. var
  563. cnt,i : longint;
  564. begin
  565. inherited ppuload(t,ppufile);
  566. elseblock:=ppuloadnode(ppufile);
  567. cnt:=ppufile.getlongint();
  568. blocks:=TFPList.create;
  569. for i:=0 to cnt-1 do
  570. addblock(i,ppuloadnode(ppufile));
  571. flabels:=ppuloadcaselabel(ppufile);
  572. { we don't save/restore the label counts, but recalculate them if needed }
  573. fcountsuptodate:=false;
  574. end;
  575. procedure tcasenode.ppuwrite(ppufile:tcompilerppufile);
  576. var
  577. i : longint;
  578. begin
  579. inherited ppuwrite(ppufile);
  580. ppuwritenode(ppufile,elseblock);
  581. ppufile.putlongint(blocks.count);
  582. for i:=0 to blocks.count-1 do
  583. ppuwritenode(ppufile,pcaseblock(blocks[i])^.statement);
  584. ppuwritecaselabel(ppufile,flabels);
  585. { we don't save/restore the label counts, but recalculate them if needed }
  586. end;
  587. procedure tcasenode.buildderefimpl;
  588. var
  589. i : integer;
  590. begin
  591. inherited buildderefimpl;
  592. if assigned(elseblock) then
  593. elseblock.buildderefimpl;
  594. for i:=0 to blocks.count-1 do
  595. pcaseblock(blocks[i])^.statement.buildderefimpl;
  596. end;
  597. procedure tcasenode.derefimpl;
  598. var
  599. i : integer;
  600. begin
  601. inherited derefimpl;
  602. if assigned(elseblock) then
  603. elseblock.derefimpl;
  604. for i:=0 to blocks.count-1 do
  605. pcaseblock(blocks[i])^.statement.derefimpl;
  606. end;
  607. function tcasenode.pass_typecheck : tnode;
  608. var
  609. i : integer;
  610. begin
  611. result:=nil;
  612. do_typecheckpass(left);
  613. for i:=0 to blocks.count-1 do
  614. typecheckpass(pcaseblock(blocks[i])^.statement);
  615. if assigned(elseblock) then
  616. typecheckpass(elseblock);
  617. resultdef:=voidtype;
  618. result:=simplify(false);
  619. end;
  620. type
  621. TLinkedListCaseLabelItem = class(TLinkedListItem)
  622. casenode: pcaselabel;
  623. constructor create(c: pcaselabel);
  624. end;
  625. constructor TLinkedListCaseLabelItem.create(c: pcaselabel);
  626. begin
  627. inherited create;
  628. casenode:=c;
  629. end;
  630. function tcasenode.pass_1 : tnode;
  631. var
  632. i: integer;
  633. node_thenblock, node_elseblock, if_node,temp_cleanup : tnode;
  634. tempcaseexpr : ttempcreatenode;
  635. if_block, init_block: tblocknode;
  636. stmt: tstatementnode;
  637. procedure add_label_to_blockid_list(list: tfpobjectlist; lab: pcaselabel);
  638. begin
  639. if not assigned(lab) then
  640. exit;
  641. if not assigned(list[lab^.blockid]) then
  642. list[lab^.blockid]:=tfpobjectlist.create(true);
  643. tfpobjectlist(list[lab^.blockid]).add(TLinkedListCaseLabelItem.create(lab));
  644. add_label_to_blockid_list(list,lab^.less);
  645. add_label_to_blockid_list(list,lab^.greater);
  646. end;
  647. function order_labels_by_blockid: tfpobjectlist;
  648. begin
  649. result:=tfpobjectlist.create(true);
  650. result.count:=blocks.count;
  651. add_label_to_blockid_list(result,flabels);
  652. end;
  653. function makeifblock(elseblock : tnode): tnode;
  654. var
  655. i, j: longint;
  656. check: taddnode;
  657. newcheck: ^taddnode;
  658. blocklist, lablist: tfpobjectlist;
  659. labitem: pcaselabel;
  660. begin
  661. result:=elseblock;
  662. blocklist:=order_labels_by_blockid;
  663. { in reverse order so that the case options at the start of the case
  664. statement are evaluated first, as they presumably are the most
  665. common }
  666. for i:=blocklist.count-1 downto 0 do
  667. begin
  668. lablist:=tfpobjectlist(blocklist[i]);
  669. check:=nil;
  670. for j:=0 to lablist.count-1 do
  671. begin
  672. if assigned(check) then
  673. begin
  674. check:=caddnode.create(orn,check,nil);
  675. newcheck:[email protected]
  676. end
  677. else
  678. newcheck:=@check;
  679. labitem:=TLinkedListCaseLabelItem(lablist[j]).casenode;
  680. newcheck^:=caddnode.create(equaln,left.getcopy,labitem^._low_str.getcopy);
  681. if (labitem^._low_str.fullcompare(labitem^._high_str)<>0) then
  682. begin
  683. newcheck^.nodetype:=gten;
  684. newcheck^:=caddnode.create(
  685. andn,newcheck^,caddnode.create(
  686. lten,left.getcopy,labitem^._high_str.getcopy));
  687. end;
  688. end;
  689. result:=cifnode.create(check,
  690. pcaseblock(blocks[i])^.statement,result);
  691. pcaseblock(blocks[i])^.statement:=nil;
  692. end;
  693. { will free its elements too because of create(true) }
  694. blocklist.free;
  695. typecheckpass(result);
  696. end;
  697. begin
  698. result:=nil;
  699. init_block:=nil;
  700. temp_cleanup:=nil;
  701. expectloc:=LOC_VOID;
  702. { only do in pass_1, so that simplify can run first and
  703. 1) possibly simplify the case node without triggering a warning
  704. 2) possibly give a compile-time error if not all cases are handled
  705. in ISO/Extended Pascal mode }
  706. if is_ordinal(left.resultdef) then
  707. checkordinalcoverage;
  708. { ideally this would be in simplify, but then checkordinalcoverage can
  709. false positives about case statements not handling all cases }
  710. if assigned(elseblock) and
  711. has_no_code(elseblock) then
  712. begin
  713. elseblock.free;
  714. elseblock:=nil;
  715. end;
  716. { evalutes the case expression }
  717. firstpass(left);
  718. set_varstate(left,vs_read,[vsf_must_be_valid]);
  719. if codegenerror then
  720. exit;
  721. { Load caseexpr into temp var if complex. }
  722. { No need to do this for ordinal, because }
  723. { in that case caseexpr is generated once }
  724. if (flabels^.label_type = ltConstString) and (not valid_for_addr(left, false)) and
  725. (blocks.count > 0) then
  726. begin
  727. init_block := internalstatements(stmt);
  728. tempcaseexpr :=
  729. ctempcreatenode.create(
  730. left.resultdef, left.resultdef.size, tt_persistent, true);
  731. temp_cleanup := ctempdeletenode.create(tempcaseexpr);
  732. typecheckpass(tnode(tempcaseexpr));
  733. addstatement(stmt, tempcaseexpr);
  734. addstatement(
  735. stmt, cassignmentnode.create(
  736. ctemprefnode.create(tempcaseexpr), left));
  737. left := ctemprefnode.create(tempcaseexpr);
  738. typecheckpass(left);
  739. end;
  740. { first case }
  741. for i:=0 to blocks.count-1 do
  742. firstpass(pcaseblock(blocks[i])^.statement);
  743. { may be handle else tree }
  744. if assigned(elseblock) then
  745. begin
  746. firstpass(elseblock);
  747. { kill case? }
  748. if blocks.count=0 then
  749. begin
  750. result:=elseblock;
  751. elseblock:=nil;
  752. exit;
  753. end;
  754. end
  755. else
  756. if blocks.count=0 then
  757. begin
  758. result:=cnothingnode.create;
  759. exit;
  760. end;
  761. if (flabels^.label_type = ltConstString) then
  762. begin
  763. if_node:=makeifblock(elseblock);
  764. if assigned(init_block) then
  765. firstpass(tnode(init_block));
  766. if_block:=internalstatements(stmt);
  767. if assigned(init_block) then
  768. addstatement(stmt, init_block);
  769. addstatement(stmt,if_node);
  770. if assigned(temp_cleanup) then
  771. addstatement(stmt,temp_cleanup);
  772. result:=if_block;
  773. elseblock:= nil;
  774. exit;
  775. end;
  776. if is_boolean(left.resultdef) then
  777. begin
  778. case blocks.count of
  779. 2:
  780. begin
  781. if boolean(qword(flabels^._low))=false then
  782. begin
  783. node_thenblock:=pcaseblock(blocks[flabels^.greater^.blockid])^.statement;
  784. node_elseblock:=pcaseblock(blocks[flabels^.blockid])^.statement;
  785. pcaseblock(blocks[flabels^.greater^.blockid])^.statement:=nil;
  786. end
  787. else
  788. begin
  789. node_thenblock:=pcaseblock(blocks[flabels^.blockid])^.statement;
  790. node_elseblock:=pcaseblock(blocks[flabels^.less^.blockid])^.statement;
  791. pcaseblock(blocks[flabels^.less^.blockid])^.statement:=nil;
  792. end;
  793. pcaseblock(blocks[flabels^.blockid])^.statement:=nil;
  794. end;
  795. 1:
  796. begin
  797. if flabels^._low=flabels^._high then
  798. begin
  799. if boolean(qword(flabels^._low))=false then
  800. begin
  801. node_thenblock:=elseblock;
  802. node_elseblock:=pcaseblock(blocks[flabels^.blockid])^.statement;
  803. end
  804. else
  805. begin
  806. node_thenblock:=pcaseblock(blocks[flabels^.blockid])^.statement;
  807. node_elseblock:=elseblock;
  808. end;
  809. pcaseblock(blocks[flabels^.blockid])^.statement:=nil;
  810. elseblock:=nil;
  811. end
  812. else
  813. begin
  814. result:=pcaseblock(blocks[flabels^.blockid])^.statement;
  815. pcaseblock(blocks[flabels^.blockid])^.statement:=nil;
  816. elseblock:=nil;
  817. exit;
  818. end;
  819. end;
  820. else
  821. internalerror(200805031);
  822. end;
  823. result:=cifnode.create(left,node_thenblock,node_elseblock);
  824. left:=nil;
  825. exit;
  826. end;
  827. { convert single case branch into if-statement }
  828. if (flabels^.greater=nil) and (flabels^.less=nil) then
  829. if flabels^.label_type=ltOrdinal then
  830. begin
  831. if flabels^._low=flabels^._high then
  832. begin
  833. result:=cifnode.create_internal(
  834. caddnode.create_internal(equaln,left.getcopy,cordconstnode.create(flabels^._low,left.resultdef,false)),
  835. pcaseblock(blocks[flabels^.blockid])^.statement,elseblock);
  836. end
  837. else if not(might_have_sideeffects(left,[mhs_exceptions])) and (node_complexity(left)<=1) then
  838. begin
  839. result:=cifnode.create_internal(
  840. caddnode.create_internal(andn,
  841. caddnode.create_internal(gten,left.getcopy,cordconstnode.create(flabels^._low,left.resultdef,false)),
  842. caddnode.create_internal(lten,left.getcopy,cordconstnode.create(flabels^._high,left.resultdef,false))
  843. ),
  844. pcaseblock(blocks[flabels^.blockid])^.statement,elseblock);
  845. end
  846. else
  847. begin
  848. init_block:=internalstatements(stmt);
  849. tempcaseexpr:=ctempcreatenode.create(
  850. left.resultdef,left.resultdef.size,tt_persistent,true);
  851. temp_cleanup:=ctempdeletenode.create(tempcaseexpr);
  852. typecheckpass(tnode(tempcaseexpr));
  853. addstatement(stmt,tempcaseexpr);
  854. addstatement(stmt,cassignmentnode.create(
  855. ctemprefnode.create(tempcaseexpr),left.getcopy));
  856. left:=ctemprefnode.create(tempcaseexpr);
  857. typecheckpass(left);
  858. addstatement(stmt,cifnode.create_internal(
  859. caddnode.create_internal(andn,
  860. caddnode.create_internal(gten,left.getcopy,cordconstnode.create(flabels^._low,left.resultdef,false)),
  861. caddnode.create_internal(lten,left.getcopy,cordconstnode.create(flabels^._high,left.resultdef,false))
  862. ),
  863. pcaseblock(blocks[flabels^.blockid])^.statement,elseblock));
  864. addstatement(stmt,temp_cleanup);
  865. result:=init_block;
  866. end;
  867. elseblock:=nil;
  868. pcaseblock(blocks[flabels^.blockid])^.statement:=nil;
  869. exit;
  870. end;
  871. end;
  872. function tcasenode.simplify(forinline:boolean):tnode;
  873. var
  874. tmp: pcaselabel;
  875. begin
  876. result:=nil;
  877. if left.nodetype=ordconstn then
  878. begin
  879. tmp:=flabels;
  880. { check all case labels until we find one that fits }
  881. while assigned(tmp) do
  882. begin
  883. if (tmp^._low<=tordconstnode(left).value) and
  884. (tmp^._high>=tordconstnode(left).value) then
  885. begin
  886. if tmp^.blockid>=blocks.count then
  887. internalerror(2014022101);
  888. result:=pcaseblock(blocks[tmp^.blockid])^.statement;
  889. if not assigned(result) then
  890. internalerror(2014022102);
  891. result:=result.getcopy;
  892. exit;
  893. end;
  894. if tmp^._high<tordconstnode(left).value then
  895. tmp:=tmp^.greater
  896. else
  897. tmp:=tmp^.less;
  898. end;
  899. { no label did match; use the else block if available }
  900. if assigned(elseblock) then
  901. result:=elseblock.getcopy
  902. else
  903. begin
  904. if ([m_iso,m_extpas]*current_settings.modeswitches)<>[] then
  905. cgmessage1(cg_e_case_missing_value,tostr(tordconstnode(left).value))
  906. else
  907. cgmessage(cg_w_case_incomplete);
  908. { no else block, so there is no code to execute at all }
  909. result:=cnothingnode.create;
  910. end;
  911. end;
  912. end;
  913. function tcasenode.dogetcopy : tnode;
  914. var
  915. n : tcasenode;
  916. i : longint;
  917. begin
  918. n:=tcasenode(inherited dogetcopy);
  919. if assigned(elseblock) then
  920. n.elseblock:=elseblock.dogetcopy
  921. else
  922. n.elseblock:=nil;
  923. if assigned(flabels) then
  924. n.flabels:=copycaselabel(flabels)
  925. else
  926. n.flabels:=nil;
  927. if assigned(blocks) then
  928. begin
  929. n.blocks:=TFPList.create;
  930. for i:=0 to blocks.count-1 do
  931. begin
  932. if not assigned(blocks[i]) then
  933. internalerror(200411302);
  934. n.addblock(i,pcaseblock(blocks[i])^.statement.dogetcopy);
  935. end;
  936. end
  937. else
  938. n.blocks:=nil;
  939. n.fcountsuptodate:=fcountsuptodate;
  940. n.flabelcnt:=flabelcnt;
  941. n.flabelcoverage:=flabelcoverage;
  942. dogetcopy:=n;
  943. end;
  944. procedure tcasenode.printnodetree(var t: text);
  945. var
  946. i : longint;
  947. begin
  948. write(t,printnodeindention,'(');
  949. printnodeindent;
  950. printnodeinfo(t);
  951. writeln(t);
  952. printnode(t,left);
  953. i:=0;
  954. for i:=0 to blocks.count-1 do
  955. begin
  956. writeln(t,printnodeindention,'(caseblock blockid: ',i);
  957. printnodeindent;
  958. printnode(t,pcaseblock(blocks[i])^.statement);
  959. printnodeunindent;
  960. writeln(t,printnodeindention,')');
  961. end;
  962. if assigned(elseblock) then
  963. begin
  964. writeln(t,printnodeindention,'(else: ',i);
  965. printnodeindent;
  966. printnode(t,elseblock);
  967. printnodeunindent;
  968. writeln(t,printnodeindention,')');
  969. end;
  970. printnodeunindent;
  971. writeln(t,printnodeindention,')');
  972. end;
  973. {$ifdef DEBUG_NODE_XML}
  974. procedure TCaseNode.XMLPrintNodeTree(var T: Text);
  975. var
  976. i : longint;
  977. begin
  978. Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
  979. XMLPrintNodeInfo(T);
  980. WriteLn(T, '>');
  981. PrintNodeIndent;
  982. WriteLn(T, PrintNodeIndention, '<condition>');
  983. PrintNodeIndent;
  984. XMLPrintNode(T, Left);
  985. PrintNodeUnindent;
  986. WriteLn(T, PrintNodeIndention, '</condition>');
  987. i:=0;
  988. for i:=0 to blocks.count-1 do
  989. begin
  990. WriteLn(T, PrintNodeIndention, '<block id="', i, '">');
  991. PrintNodeIndent;
  992. XMLPrintNode(T, PCaseBlock(blocks[i])^.statement);
  993. PrintNodeUnindent;
  994. WriteLn(T, PrintNodeIndention, '</block>');
  995. end;
  996. if assigned(elseblock) then
  997. begin
  998. WriteLn(T, PrintNodeIndention, '<block id="else">');
  999. PrintNodeIndent;
  1000. XMLPrintNode(T, ElseBlock);
  1001. PrintNodeUnindent;
  1002. WriteLn(T, PrintNodeIndention, '</block>');
  1003. end;
  1004. PrintNodeUnindent;
  1005. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  1006. end;
  1007. {$endif DEBUG_NODE_XML}
  1008. procedure tcasenode.insertintolist(l : tnodelist);
  1009. begin
  1010. end;
  1011. function caselabelsequal(n1,n2: pcaselabel): boolean;
  1012. begin
  1013. result :=
  1014. (not assigned(n1) and not assigned(n2)) or
  1015. (assigned(n1) and assigned(n2) and
  1016. (n1^._low = n2^._low) and
  1017. (n1^._high = n2^._high) and
  1018. { the rest of the fields don't matter for equality (JM) }
  1019. caselabelsequal(n1^.less,n2^.less) and
  1020. caselabelsequal(n1^.greater,n2^.greater))
  1021. end;
  1022. function caseblocksequal(b1,b2:TFPList): boolean;
  1023. var
  1024. i : longint;
  1025. begin
  1026. result:=false;
  1027. if b1.count<>b2.count then
  1028. exit;
  1029. for i:=0 to b1.count-1 do
  1030. begin
  1031. if not pcaseblock(b1[i])^.statement.isequal(pcaseblock(b2[i])^.statement) then
  1032. exit;
  1033. end;
  1034. result:=true;
  1035. end;
  1036. function tcasenode.docompare(p: tnode): boolean;
  1037. begin
  1038. result :=
  1039. inherited docompare(p) and
  1040. caselabelsequal(flabels,tcasenode(p).flabels) and
  1041. caseblocksequal(blocks,tcasenode(p).blocks) and
  1042. elseblock.isequal(tcasenode(p).elseblock);
  1043. end;
  1044. procedure tcasenode.addblock(blockid:longint;instr:tnode);
  1045. var
  1046. hcaseblock : pcaseblock;
  1047. begin
  1048. new(hcaseblock);
  1049. fillchar(hcaseblock^,sizeof(hcaseblock^),0);
  1050. hcaseblock^.statement:=instr;
  1051. if blockid>=blocks.count then
  1052. blocks.count:=blockid+1;
  1053. blocks[blockid]:=hcaseblock;
  1054. end;
  1055. procedure tcasenode.addelseblock(instr:tnode);
  1056. begin
  1057. elseblock:=instr;
  1058. end;
  1059. function tcasenode.getlabelcnt: cardinal;
  1060. begin
  1061. if not fcountsuptodate then
  1062. updatecoverage;
  1063. result:=flabelcnt;
  1064. end;
  1065. function tcasenode.getlabelcoverage: qword;
  1066. begin
  1067. if not fcountsuptodate then
  1068. updatecoverage;
  1069. result:=flabelcoverage;
  1070. end;
  1071. procedure tcasenode.updatecoverage;
  1072. var
  1073. isord, first: boolean;
  1074. procedure count(p : pcaselabel);
  1075. begin
  1076. inc(flabelcnt);
  1077. if isord then
  1078. begin
  1079. flabelcoverage:=flabelcoverage + (p^._high - p^._low);
  1080. { ensure we don't overflow in case it covers the
  1081. full range of qword }
  1082. if not first then
  1083. inc(flabelcoverage);
  1084. first:=false;
  1085. end;
  1086. if assigned(p^.less) then
  1087. count(p^.less);
  1088. if assigned(p^.greater) then
  1089. count(p^.greater);
  1090. end;
  1091. begin
  1092. isord:=is_ordinal(left.resultdef);
  1093. flabelcnt:=0;
  1094. flabelcoverage:=0;
  1095. first:=true;
  1096. count(flabels);
  1097. fcountsuptodate:=true;
  1098. end;
  1099. procedure tcasenode.checkordinalcoverage;
  1100. function orddefspansfullrange(def: torddef): boolean;
  1101. var
  1102. packedbitsize: cardinal;
  1103. dummy: longint;
  1104. val: qword;
  1105. begin
  1106. result:=false;
  1107. packedbitsize:=def.packedbitsize;
  1108. if ((packedbitsize mod 8) <> 0) or
  1109. not ispowerof2(packedbitsize div 8,dummy) then
  1110. exit;
  1111. dec(packedbitsize);
  1112. if is_signed(def) then
  1113. begin
  1114. {$push}{$q-}
  1115. if def.low<>(-(int64(1) shl packedbitsize)) then
  1116. exit;
  1117. if def.high<>((int64(1) shl packedbitsize)-1) then
  1118. exit;
  1119. {$pop}
  1120. end
  1121. else
  1122. begin
  1123. if def.low<>0 then
  1124. exit;
  1125. val:=qword(1) shl packedbitsize;
  1126. val:=(val-1)+val;
  1127. if def.high<>val then
  1128. exit;
  1129. end;
  1130. result:=true;
  1131. end;
  1132. var
  1133. lv, hv, typcount: tconstexprint;
  1134. begin
  1135. { Check label type coverage for enumerations and small types }
  1136. getrange(left.resultdef,lv,hv);
  1137. { low/high value of c-style booleans are not suitable for calculating their "type count" }
  1138. if is_cbool(left.resultdef) then
  1139. typcount:=1
  1140. else
  1141. typcount:=hv-lv;
  1142. if not assigned(elseblock) then
  1143. begin
  1144. { unless cs_check_all_case_coverage is set, only check for enums, booleans and
  1145. subrange types different from the default ones }
  1146. if (cs_check_all_case_coverage in current_settings.localswitches) or
  1147. (is_enum(left.resultdef) or
  1148. is_boolean(left.resultdef) or
  1149. not orddefspansfullrange(torddef(left.resultdef))) and
  1150. (labelcoverage<typcount) then
  1151. begin
  1152. { labels for some values of the operand are missing, and no else block is present }
  1153. cgmessage(cg_w_case_incomplete);
  1154. { in Standard/Extended Pascal, this is a dynamic violation error if it actually happens }
  1155. if ([m_extpas,m_iso]*current_settings.modeswitches)<>[] then
  1156. begin
  1157. elseblock:=ccallnode.createintern('fpc_rangeerror',nil);
  1158. typecheckpass(elseblock);
  1159. end;
  1160. end
  1161. end
  1162. else if labelcoverage=typcount then
  1163. begin
  1164. { labels for all values of the operand are present, but an extra else block is present }
  1165. MessagePos(elseblock.fileinfo, cg_w_unreachable_code);
  1166. end;
  1167. end;
  1168. procedure tcasenode.addlabel(blockid:longint;const l,h : TConstExprInt);
  1169. var
  1170. hcaselabel : pcaselabel;
  1171. function insertlabel(var p : pcaselabel):pcaselabel;
  1172. begin
  1173. if p=nil then
  1174. begin
  1175. p:=hcaselabel;
  1176. result:=p;
  1177. end
  1178. else
  1179. if (p^._low>hcaselabel^._low) and
  1180. (p^._low>hcaselabel^._high) then
  1181. begin
  1182. if (hcaselabel^.blockid = p^.blockid) and
  1183. (p^._low = hcaselabel^._high + 1) then
  1184. begin
  1185. p^._low := hcaselabel^._low;
  1186. dispose(hcaselabel);
  1187. result:=p;
  1188. end
  1189. else
  1190. result:=insertlabel(p^.less)
  1191. end
  1192. else
  1193. if (p^._high<hcaselabel^._low) and
  1194. (p^._high<hcaselabel^._high) then
  1195. begin
  1196. if (hcaselabel^.blockid = p^.blockid) and
  1197. (p^._high+1 = hcaselabel^._low) then
  1198. begin
  1199. p^._high := hcaselabel^._high;
  1200. dispose(hcaselabel);
  1201. result:=p;
  1202. end
  1203. else
  1204. result:=insertlabel(p^.greater);
  1205. end
  1206. else
  1207. begin
  1208. dispose(hcaselabel);
  1209. Message(parser_e_double_caselabel);
  1210. result:=nil;
  1211. end
  1212. end;
  1213. begin
  1214. new(hcaselabel);
  1215. fillchar(hcaselabel^,sizeof(tcaselabel),0);
  1216. hcaselabel^.blockid:=blockid;
  1217. hcaselabel^.label_type:=ltOrdinal;
  1218. hcaselabel^._low:=l;
  1219. hcaselabel^._high:=h;
  1220. insertlabel(flabels);
  1221. fcountsuptodate:=false;
  1222. end;
  1223. procedure tcasenode.addlabel(blockid: longint; l, h: tstringconstnode);
  1224. var
  1225. hcaselabel : pcaselabel;
  1226. function insertlabel(var p : pcaselabel) : pcaselabel;
  1227. begin
  1228. if not assigned(p) then
  1229. begin
  1230. p := hcaselabel;
  1231. result := p;
  1232. end
  1233. else
  1234. if (p^._low_str.fullcompare(hcaselabel^._high_str) > 0) then
  1235. result := insertlabel(p^.less)
  1236. else
  1237. if (p^._high_str.fullcompare(hcaselabel^._low_str) < 0) then
  1238. result := insertlabel(p^.greater)
  1239. else
  1240. begin
  1241. hcaselabel^._low_str.free;
  1242. hcaselabel^._high_str.free;
  1243. dispose(hcaselabel);
  1244. Message(parser_e_double_caselabel);
  1245. result:=nil;
  1246. end;
  1247. end;
  1248. begin
  1249. new(hcaselabel);
  1250. fillchar(hcaselabel^, sizeof(tcaselabel), 0);
  1251. hcaselabel^.blockid := blockid;
  1252. hcaselabel^.label_type := ltConstString;
  1253. hcaselabel^._low_str := tstringconstnode(l.getcopy);
  1254. hcaselabel^._high_str := tstringconstnode(h.getcopy);
  1255. insertlabel(flabels);
  1256. end;
  1257. end.