Debian Codegraph @

gcc/m2/gm2-compiler/M2CaseList.mod

raw
1 (* M2CaseList.mod implement ISO case label lists.
2
3 Copyright (C) 2009-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE M2CaseList ;
23
24
25 FROM M2Debug IMPORT Assert ;
26 FROM M2GCCDeclare IMPORT TryDeclareConstant, GetTypeMin, GetTypeMax ;
27 FROM M2MetaError IMPORT MetaError1, MetaError2, MetaErrorT0, MetaErrorT1, MetaErrorT2, MetaErrorT3, MetaErrorT4, MetaErrorStringT0, MetaErrorString1 ;
28 FROM M2Error IMPORT InternalError ;
29 FROM M2Range IMPORT OverlapsRange, IsEqual, IsGreater ;
30 FROM M2ALU IMPORT PushIntegerTree, PopIntegerTree, Addn, Sub, PushInt, PushCard ;
31 FROM Indexing IMPORT Index, InitIndex, PutIndice, GetIndice, ForeachIndiceInIndexDo, HighIndice ;
32 FROM Lists IMPORT InitList, IncludeItemIntoList, RemoveItemFromList, NoOfItemsInList, GetItemFromList ;
33 FROM NameKey IMPORT NulName, KeyToCharStar ;
34 FROM SymbolConversion IMPORT GccKnowsAbout, Mod2Gcc, AddModGcc ;
35 FROM DynamicStrings IMPORT InitString, InitStringCharStar, InitStringChar, ConCat, Mark, KillString ;
36 FROM gcctypes IMPORT tree ;
37 FROM m2block IMPORT RememberType ;
38 FROM m2type IMPORT GetMinFrom ;
39 FROM m2expr IMPORT GetIntegerOne, CSTIntToString, CSTIntToChar ;
40 FROM Storage IMPORT ALLOCATE ;
41 FROM M2Base IMPORT IsExpressionCompatible, Char ;
42 FROM M2LexBuf IMPORT TokenToLocation ;
43 FROM NumberIO IMPORT WriteCard ;
44
45 FROM SymbolTable IMPORT NulSym, IsConst, IsFieldVarient, IsRecord, IsRecordField, GetVarientTag, GetType,
46 ForeachLocalSymDo, GetSymName, IsEnumeration, SkipType, NoOfElements, GetNth,
47 IsSubrange, MakeConstLit, IsConstString, GetStringLength, MakeConstVar, PutConst,
48 PopValue ;
49
50 TYPE
51 RangePair = POINTER TO RECORD
52 low, high: CARDINAL ;
53 tokenno : CARDINAL ;
54 END ;
55
56 ConflictingPair = POINTER TO RECORD
57 a, b: RangePair ;
58 END ;
59
60 CaseList = POINTER TO RECORD
61 maxRangeId : CARDINAL ;
62 rangeArray : Index ;
63 currentRange: RangePair ;
64 varientField: CARDINAL ;
65 END ;
66
67 CaseDescriptor = POINTER TO RECORD
68 resolved : BOOLEAN ;
69 elseClause : BOOLEAN ;
70 elseField : CARDINAL ;
71 record : CARDINAL ;
72 varient : CARDINAL ;
73 expression : CARDINAL ;
74 maxCaseId : CARDINAL ;
75 caseListArray: Index ;
76 currentCase : CaseList ;
77 next : CaseDescriptor ;
78 END ;
79
80 SetRange = POINTER TO RECORD
81 low, high: tree ;
82 next : SetRange ;
83 END ;
84
85 VAR
86 caseStack : CaseDescriptor ;
87 caseId : CARDINAL ;
88 caseArray : Index ;
89 conflictArray: Index ;
90 FreeRangeList: SetRange ;
91
92
93
94 (*
95 PushCase - create a case entity and push it to an internal stack.
96 rec is NulSym if this is a CASE statement.
97 If rec is a record then it indicates a possible
98 varients reside in the record to check.
99 Both rec and va might be NulSym and then the expr
100 will contain the selector expression to a case statement.
101 Return the case id.
102 *)
103
104 PROCEDURE PushCase (rec, va, expr: CARDINAL) : CARDINAL ;
105 VAR
106 c: CaseDescriptor ;
107 BEGIN
108 INC (caseId) ;
109 NEW (c) ;
110 IF c = NIL
111 THEN
112 InternalError ('out of memory error')
113 ELSE
114 WITH c^ DO
115 resolved := FALSE ;
116 elseClause := FALSE ;
117 elseField := NulSym ;
118 record := rec ;
119 varient := va ;
120 expression := expr ;
121 maxCaseId := 0 ;
122 caseListArray := InitIndex (1) ;
123 next := caseStack ;
124 currentCase := NIL
125 END ;
126 caseStack := c ;
127 PutIndice (caseArray, caseId, c)
128 END ;
129 RETURN caseId
130 END PushCase ;
131
132
133 (*
134 PopCase - pop the top element of the case entity from the internal
135 stack.
136 *)
137
138 PROCEDURE PopCase ;
139 BEGIN
140 IF caseStack=NIL
141 THEN
142 InternalError ('case stack is empty')
143 END ;
144 caseStack := caseStack^.next
145 END PopCase ;
146
147
148 (*
149 ElseCase - indicates that this case varient does have an else clause.
150 *)
151
152 PROCEDURE ElseCase (f: CARDINAL) ;
153 BEGIN
154 WITH caseStack^ DO
155 elseClause := TRUE ;
156 elseField := f
157 END
158 END ElseCase ;
159
160
161 (*
162 BeginCaseList - create a new label list.
163 *)
164
165 PROCEDURE BeginCaseList (v: CARDINAL) ;
166 VAR
167 l: CaseList ;
168 BEGIN
169 NEW(l) ;
170 IF l=NIL
171 THEN
172 InternalError ('out of memory error')
173 END ;
174 WITH l^ DO
175 maxRangeId := 0 ;
176 rangeArray := InitIndex(1) ;
177 currentRange := NIL ;
178 varientField := v
179 END ;
180 WITH caseStack^ DO
181 INC(maxCaseId) ;
182 PutIndice(caseListArray, maxCaseId, l) ;
183 currentCase := l
184 END
185 END BeginCaseList ;
186
187
188 (*
189 EndCaseList - terminate the current label list.
190 *)
191
192 PROCEDURE EndCaseList ;
193 BEGIN
194 caseStack^.currentCase := NIL
195 END EndCaseList ;
196
197
198 (*
199 AddRange - add a range to the current label list.
200 *)
201
202 PROCEDURE AddRange (r1, r2: CARDINAL; tok: CARDINAL) ;
203 VAR
204 r: RangePair ;
205 BEGIN
206 NEW(r) ;
207 IF r=NIL
208 THEN
209 InternalError ('out of memory error')
210 ELSE
211 WITH r^ DO
212 low := r1 ;
213 high := r2 ;
214 tokenno := tok
215 END ;
216 WITH caseStack^.currentCase^ DO
217 INC(maxRangeId) ;
218 PutIndice(rangeArray, maxRangeId, r) ;
219 currentRange := r
220 END
221 END
222 END AddRange ;
223
224
225 (*
226 GetVariantTagType - returns the type associated with, variant.
227 *)
228
229 PROCEDURE GetVariantTagType (variant: CARDINAL) : CARDINAL ;
230 VAR
231 tag: CARDINAL ;
232 BEGIN
233 tag := GetVarientTag(variant) ;
234 IF IsFieldVarient(tag) OR IsRecordField(tag)
235 THEN
236 RETURN( GetType(tag) )
237 ELSE
238 RETURN( tag )
239 END
240 END GetVariantTagType ;
241
242
243 (*
244 CaseBoundsResolved - returns TRUE if all constants in the case list, c,
245 are known to GCC.
246 *)
247
248 PROCEDURE CaseBoundsResolved (tokenno: CARDINAL; c: CARDINAL) : BOOLEAN ;
249 VAR
250 p: CaseDescriptor ;
251 BEGIN
252 p := GetIndice (caseArray, c) ;
253 IF p^.resolved
254 THEN
255 RETURN TRUE
256 ELSE
257 IF CheckCaseBoundsResolved (tokenno, c)
258 THEN
259 ConvertNulStr2NulChar (tokenno, c) ;
260 RETURN TRUE
261 ELSE
262 RETURN FALSE
263 END
264 END
265 END CaseBoundsResolved ;
266
267
268 (*
269 CheckCaseBoundsResolved - return TRUE if all constants in the case list c are known to GCC.
270 *)
271
272 PROCEDURE CheckCaseBoundsResolved (tokenno: CARDINAL; c: CARDINAL) : BOOLEAN ;
273 VAR
274 p : CaseDescriptor ;
275 q : CaseList ;
276 r : RangePair ;
277 min,
278 max,
279 type,
280 i, j : CARDINAL ;
281 BEGIN
282 p := GetIndice(caseArray, c) ;
283 WITH p^ DO
284 IF varient#NulSym
285 THEN
286 (* not a CASE statement, but a varient record containing without an ELSE clause *)
287 type := GetVariantTagType(varient) ;
288 resolved := TRUE ;
289 IF NOT GccKnowsAbout(type)
290 THEN
291 (* do we need to add, type, to the list of types required to be resolved? *)
292 resolved := FALSE
293 END ;
294 min := GetTypeMin(type) ;
295 IF NOT GccKnowsAbout(min)
296 THEN
297 TryDeclareConstant(tokenno, min) ;
298 resolved := FALSE
299 END ;
300 max := GetTypeMax(type) ;
301 IF NOT GccKnowsAbout(max)
302 THEN
303 TryDeclareConstant(tokenno, max) ;
304 resolved := FALSE
305 END ;
306 IF NOT resolved
307 THEN
308 RETURN( FALSE )
309 END
310 END ;
311 i := 1 ;
312 WHILE i<=maxCaseId DO
313 q := GetIndice(caseListArray, i) ;
314 j := 1 ;
315 WHILE j<=q^.maxRangeId DO
316 r := GetIndice(q^.rangeArray, j) ;
317 IF r^.low#NulSym
318 THEN
319 IF IsConst(r^.low)
320 THEN
321 TryDeclareConstant(tokenno, r^.low) ;
322 IF NOT GccKnowsAbout(r^.low)
323 THEN
324 RETURN( FALSE )
325 END
326 ELSE
327 IF r^.high=NulSym
328 THEN
329 MetaError1('the CASE statement variant must be defined by a constant {%1Da:is a {%1d}}', r^.low)
330 ELSE
331 MetaError1('the CASE statement variant low value in a range must be defined by a constant {%1Da:is a {%1d}}',
332 r^.low)
333 END
334 END
335 END ;
336 IF r^.high#NulSym
337 THEN
338 IF IsConst(r^.high)
339 THEN
340 TryDeclareConstant(tokenno, r^.high) ;
341 IF NOT GccKnowsAbout(r^.high)
342 THEN
343 RETURN( FALSE )
344 END
345 ELSE
346 MetaError1('the CASE statement variant high value in a range must be defined by a constant {%1Da:is a {%1d}}',
347 r^.high)
348 END
349 END ;
350 INC(j)
351 END ;
352 INC(i)
353 END
354 END ;
355 RETURN( TRUE )
356 END CheckCaseBoundsResolved ;
357
358
359 (*
360 ConvertNulStr2NulChar -
361 *)
362
363 PROCEDURE ConvertNulStr2NulChar (tokenno: CARDINAL; c: CARDINAL) ;
364 VAR
365 p : CaseDescriptor ;
366 q : CaseList ;
367 r : RangePair ;
368 i, j: CARDINAL ;
369 BEGIN
370 p := GetIndice (caseArray, c) ;
371 WITH p^ DO
372 i := 1 ;
373 WHILE i <= maxCaseId DO
374 q := GetIndice (caseListArray, i) ;
375 j := 1 ;
376 WHILE j<=q^.maxRangeId DO
377 r := GetIndice (q^.rangeArray, j) ;
378 r^.low := NulStr2NulChar (tokenno, r^.low) ;
379 r^.high := NulStr2NulChar (tokenno, r^.high) ;
380 INC (j)
381 END ;
382 INC (i)
383 END
384 END
385 END ConvertNulStr2NulChar ;
386
387
388 (*
389 NulStr2NulChar - if sym is a const string of length 0 then return
390 a nul char instead otherwise return sym.
391 *)
392
393 PROCEDURE NulStr2NulChar (tok: CARDINAL; sym: CARDINAL) : CARDINAL ;
394 BEGIN
395 IF sym # NulSym
396 THEN
397 IF IsConst (sym) AND IsConstString (sym) AND GccKnowsAbout (sym)
398 THEN
399 IF GetStringLength (tok, sym) = 0
400 THEN
401 sym := MakeConstVar (tok, NulName) ;
402 PutConst (sym, Char) ;
403 PushCard (0) ;
404 PopValue (sym) ;
405 TryDeclareConstant (tok, sym) ;
406 Assert (GccKnowsAbout (sym))
407 END
408 END
409 END ;
410 RETURN sym
411 END NulStr2NulChar ;
412
413
414 (*
415 IsSame - return TRUE if r, s, are in, e.
416 *)
417
418 PROCEDURE IsSame (e: ConflictingPair; r, s: RangePair) : BOOLEAN ;
419 BEGIN
420 WITH e^ DO
421 RETURN( ((a=r) AND (b=s)) OR ((a=s) AND (b=r)) )
422 END
423 END IsSame ;
424
425
426 (*
427 SeenBefore -
428 *)
429
430 PROCEDURE SeenBefore (r, s: RangePair) : BOOLEAN ;
431 VAR
432 i, h: CARDINAL ;
433 e : ConflictingPair ;
434 BEGIN
435 h := HighIndice(conflictArray) ;
436 i := 1 ;
437 WHILE i<=h DO
438 e := GetIndice(conflictArray, i) ;
439 IF IsSame(e, r, s)
440 THEN
441 RETURN( TRUE )
442 END ;
443 INC(i)
444 END ;
445 NEW(e) ;
446 WITH e^ DO
447 a := r ;
448 b := s
449 END ;
450 PutIndice(conflictArray, h+1, e) ;
451 RETURN( FALSE )
452 END SeenBefore ;
453
454
455 (*
456 Overlaps -
457 *)
458
459 PROCEDURE Overlaps (r, s: RangePair) : BOOLEAN ;
460 VAR
461 a, b, c, d: CARDINAL ;
462 BEGIN
463 a := r^.low ;
464 c := s^.low ;
465 IF r^.high=NulSym
466 THEN
467 b := a ;
468 IF s^.high=NulSym
469 THEN
470 d := c ;
471 IF OverlapsRange(Mod2Gcc(a), Mod2Gcc(b), Mod2Gcc(c), Mod2Gcc(d))
472 THEN
473 IF NOT SeenBefore(r, s)
474 THEN
475 MetaErrorT2 (r^.tokenno, 'case label {%1ad} is a duplicate with {%2ad}', a, c) ;
476 MetaErrorT2 (s^.tokenno, 'case label {%1ad} is a duplicate with {%2ad}', c, a)
477 END ;
478 RETURN( TRUE )
479 END
480 ELSE
481 d := s^.high ;
482 IF OverlapsRange(Mod2Gcc(a), Mod2Gcc(b), Mod2Gcc(c), Mod2Gcc(d))
483 THEN
484 IF NOT SeenBefore (r, s)
485 THEN
486 MetaErrorT3 (r^.tokenno, 'case label {%1ad} is a duplicate in the range {%2ad}..{%3ad}', a, c, d) ;
487 MetaErrorT3 (s^.tokenno, 'case range {%2ad}..{%3ad} is a duplicate of case label {%1ad}', c, d, a)
488 END ;
489 RETURN( TRUE )
490 END
491 END
492 ELSE
493 b := r^.high ;
494 IF s^.high=NulSym
495 THEN
496 d := c ;
497 IF OverlapsRange (Mod2Gcc(a), Mod2Gcc(b), Mod2Gcc(c), Mod2Gcc(d))
498 THEN
499 IF NOT SeenBefore(r, s)
500 THEN
501 MetaErrorT3 (r^.tokenno, 'case range {%1ad}..{%2ad} is a duplicate with case label {%3ad}', a, b, c) ;
502 MetaErrorT3 (s^.tokenno, 'case label {%1ad} is a duplicate with case range %{2ad}..{%3ad}', c, a, b)
503 END ;
504 RETURN( TRUE )
505 END
506 ELSE
507 d := s^.high ;
508 IF OverlapsRange(Mod2Gcc(a), Mod2Gcc(b), Mod2Gcc(c), Mod2Gcc(d))
509 THEN
510 IF NOT SeenBefore(r, s)
511 THEN
512 MetaErrorT4 (r^.tokenno, 'case range {%1ad}..{%2ad} overlaps case range {%3ad}..{%4ad}', a, b, c, d) ;
513 MetaErrorT4 (s^.tokenno, 'case range {%1ad}..{%2ad} overlaps case range {%3ad}..{%4ad}', c, d, a, b)
514 END ;
515 RETURN( TRUE )
516 END
517 END
518 END ;
519 RETURN( FALSE )
520 END Overlaps ;
521
522
523 (*
524 GetCaseExpression - return the type from the expression.
525 *)
526
527 PROCEDURE GetCaseExpression (p: CaseDescriptor) : CARDINAL ;
528 VAR
529 type: CARDINAL ;
530 BEGIN
531 WITH p^ DO
532 IF expression = NulSym
533 THEN
534 type := NulSym
535 ELSE
536 type := SkipType (GetType (expression))
537 END
538 END ;
539 RETURN type
540 END GetCaseExpression ;
541
542
543 (*
544 OverlappingCaseBound - returns TRUE if, r, overlaps any case bound in the
545 case statement, c.
546 *)
547
548 PROCEDURE OverlappingCaseBound (r: RangePair; c: CARDINAL) : BOOLEAN ;
549 VAR
550 p : CaseDescriptor ;
551 q : CaseList ;
552 s : RangePair ;
553 i, j : CARDINAL ;
554 overlap: BOOLEAN ;
555 BEGIN
556 p := GetIndice (caseArray, c) ;
557 overlap := FALSE ;
558 WITH p^ DO
559 i := 1 ;
560 WHILE i<=maxCaseId DO
561 q := GetIndice (caseListArray, i) ;
562 j := 1 ;
563 WHILE j<=q^.maxRangeId DO
564 s := GetIndice (q^.rangeArray, j) ;
565 IF (s#r) AND Overlaps (r, s)
566 THEN
567 overlap := TRUE
568 END ;
569 INC (j)
570 END ;
571 INC (i)
572 END
573 END ;
574 RETURN( overlap )
575 END OverlappingCaseBound ;
576
577
578 (*
579 OverlappingCaseBounds - returns TRUE if there were any overlapping bounds
580 in the case list, c. It will generate an error
581 messages for each overlapping bound found.
582 *)
583
584 PROCEDURE OverlappingCaseBounds (c: CARDINAL) : BOOLEAN ;
585 VAR
586 p : CaseDescriptor ;
587 q : CaseList ;
588 r : RangePair ;
589 i, j : CARDINAL ;
590 overlap: BOOLEAN ;
591 BEGIN
592 p := GetIndice (caseArray, c) ;
593 overlap := FALSE ;
594 WITH p^ DO
595 i := 1 ;
596 WHILE i<=maxCaseId DO
597 q := GetIndice (caseListArray, i) ;
598 j := 1 ;
599 WHILE j<=q^.maxRangeId DO
600 r := GetIndice (q^.rangeArray, j) ;
601 IF OverlappingCaseBound (r, c)
602 THEN
603 overlap := TRUE
604 END ;
605 INC(j)
606 END ;
607 INC(i)
608 END
609 END ;
610 RETURN( overlap )
611 END OverlappingCaseBounds ;
612
613
614 (*
615 NewRanges - return a new range from the freelist or heap.
616 *)
617
618 PROCEDURE NewRanges () : SetRange ;
619 VAR
620 s: SetRange ;
621 BEGIN
622 IF FreeRangeList=NIL
623 THEN
624 NEW(s)
625 ELSE
626 s := FreeRangeList ;
627 FreeRangeList := FreeRangeList^.next
628 END ;
629 s^.next := NIL ;
630 RETURN( s )
631 END NewRanges ;
632
633
634 (*
635 NewSet - returns a new set based on type with the low and high fields assigned
636 to the min and max values for the type.
637 *)
638
639 PROCEDURE NewSet (type: CARDINAL) : SetRange ;
640 VAR
641 s: SetRange ;
642 BEGIN
643 s := NewRanges() ;
644 WITH s^ DO
645 low := Mod2Gcc(GetTypeMin(type)) ;
646 high := Mod2Gcc(GetTypeMax(type)) ;
647 next := NIL
648 END ;
649 RETURN( s )
650 END NewSet ;
651
652
653 (*
654 DisposeRanges - place set and its list onto the free list.
655 *)
656
657 PROCEDURE DisposeRanges (set: SetRange) : SetRange ;
658 VAR
659 t: SetRange ;
660 BEGIN
661 IF set#NIL
662 THEN
663 IF FreeRangeList=NIL
664 THEN
665 FreeRangeList := set
666 ELSE
667 t := set ;
668 WHILE t^.next#NIL DO
669 t := t^.next
670 END ;
671 t^.next := FreeRangeList ;
672 FreeRangeList := set
673 END
674 END ;
675 RETURN( NIL )
676 END DisposeRanges ;
677
678
679 (*
680 RemoveRange - removes the range descriptor h from set and return the
681 possibly new head of set.
682 *)
683
684 PROCEDURE RemoveRange (set: SetRange; h: SetRange) : SetRange ;
685 VAR
686 i: SetRange ;
687 BEGIN
688 IF h=set
689 THEN
690 set := set^.next ;
691 h^.next := NIL ;
692 h := DisposeRanges(h) ;
693 ELSE
694 i := set ;
695 WHILE i^.next#h DO
696 i := i^.next
697 END ;
698 i^.next := h^.next ;
699 i := h ;
700 h := h^.next ;
701 i^.next := NIL ;
702 i := DisposeRanges(i)
703 END ;
704 RETURN set
705 END RemoveRange ;
706
707
708 (*
709 SubBitRange - subtracts bits, lo..hi, from, set.
710 *)
711
712 PROCEDURE SubBitRange (set: SetRange; lo, hi: tree; tokenno: CARDINAL) : SetRange ;
713 VAR
714 h, i: SetRange ;
715 BEGIN
716 h := set ;
717 WHILE h#NIL DO
718 (* Check to see if a single set element h is obliterated by lo..hi. *)
719 IF (h^.high=NIL) OR IsEqual(h^.high, h^.low)
720 THEN
721 IF IsEqual(h^.low, lo) OR OverlapsRange(lo, hi, h^.low, h^.low)
722 THEN
723 set := RemoveRange (set, h) ;
724 h := set
725 ELSE
726 h := h^.next
727 END
728 (* Now check to see if the lo..hi match exactly with the set range. *)
729 ELSIF (h^.high#NIL) AND IsEqual (lo, h^.low) AND IsEqual (hi, h^.high)
730 THEN
731 (* Remove h and return as lo..hi have been removed. *)
732 RETURN RemoveRange (set, h)
733 ELSE
734 (* All other cases require modifying the existing set range. *)
735 IF OverlapsRange(lo, hi, h^.low, h^.high)
736 THEN
737 IF IsGreater(h^.low, lo) OR IsGreater(hi, h^.high)
738 THEN
739 MetaErrorT0 (tokenno, 'variant case range lies outside tag value')
740 ELSE
741 IF IsEqual(h^.low, lo)
742 THEN
743 PushIntegerTree(hi) ;
744 PushInt(1) ;
745 Addn ;
746 h^.low := PopIntegerTree()
747 ELSIF IsEqual(h^.high, hi)
748 THEN
749 PushIntegerTree(lo) ;
750 PushInt(1) ;
751 Sub ;
752 h^.high := PopIntegerTree()
753 ELSE
754 (* lo..hi exist inside range h^.low..h^.high *)
755 i := NewRanges() ;
756 i^.next := h^.next ;
757 h^.next := i ;
758 i^.high := h^.high ;
759 PushIntegerTree(lo) ;
760 PushInt(1) ;
761 Sub ;
762 h^.high := PopIntegerTree() ;
763 PushIntegerTree(hi) ;
764 PushInt(1) ;
765 Addn ;
766 i^.low := PopIntegerTree()
767 END
768 END
769 ELSE
770 h := h^.next
771 END
772 END
773 END ;
774 RETURN( set )
775 END SubBitRange ;
776
777
778 (*
779 CheckLowHigh - checks to see the low value <= high value and issues an error
780 if this is not true.
781 *)
782
783 PROCEDURE CheckLowHigh (rp: RangePair) ;
784 VAR
785 lo, hi: tree ;
786 temp : CARDINAL ;
787 BEGIN
788 lo := Mod2Gcc (rp^.low) ;
789 hi := Mod2Gcc (rp^.high) ;
790 IF IsGreater (lo, hi)
791 THEN
792 MetaErrorT2 (rp^.tokenno, 'case range should be low..high rather than high..low, range specified as {%1Euad}..{%2Euad}', rp^.low, rp^.high) ;
793 temp := rp^.high ;
794 rp^.high := rp^.low ;
795 rp^.low := temp
796 END
797 END CheckLowHigh ;
798
799
800 (*
801 ExcludeCaseRanges - excludes all case ranges found in, p, from, set
802 *)
803
804 PROCEDURE ExcludeCaseRanges (set: SetRange; cd: CaseDescriptor) : SetRange ;
805 VAR
806 i, j: CARDINAL ;
807 cl : CaseList ;
808 rp : RangePair ;
809 BEGIN
810 WITH cd^ DO
811 i := 1 ;
812 WHILE i <= maxCaseId DO
813 cl := GetIndice (caseListArray, i) ;
814 j := 1 ;
815 WHILE j <= cl^.maxRangeId DO
816 rp := GetIndice (cl^.rangeArray, j) ;
817 IF rp^.high = NulSym
818 THEN
819 set := SubBitRange (set,
820 Mod2Gcc (rp^.low),
821 Mod2Gcc (rp^.low), rp^.tokenno)
822 ELSE
823 CheckLowHigh (rp) ;
824 set := SubBitRange (set,
825 Mod2Gcc (rp^.low),
826 Mod2Gcc (rp^.high), rp^.tokenno)
827 END ;
828 INC (j)
829 END ;
830 INC (i)
831 END
832 END ;
833 RETURN set
834 END ExcludeCaseRanges ;
835
836
837 VAR
838 errorString: String ;
839
840
841 (*
842 IncludeElement - only include enumeration field into errorString if it lies between low..high.
843 *)
844
845 PROCEDURE IncludeElement (enumList: List; field: CARDINAL; low, high: tree) ;
846 VAR
847 fieldTree: tree ;
848 BEGIN
849 IF field # NulSym
850 THEN
851 fieldTree := Mod2Gcc (field) ;
852 IF OverlapsRange (fieldTree, fieldTree, low, high)
853 THEN
854 IncludeItemIntoList (enumList, field)
855 END
856 END
857 END IncludeElement ;
858
859
860 (*
861 IncludeElements - only include enumeration field values low..high in errorString.
862 *)
863
864 PROCEDURE IncludeElements (type: CARDINAL; enumList: List; low, high: tree) ;
865 VAR
866 field : CARDINAL ;
867 i,
868 NoElements: CARDINAL ;
869 BEGIN
870 NoElements := NoOfElements (type) ;
871 i := 1 ;
872 WHILE i <= NoElements DO
873 field := GetNth (type, i) ;
874 IncludeElement (enumList, field, low, high) ;
875 INC (i)
876 END
877 END IncludeElements ;
878
879
880 (*
881 ErrorRangeEnum - include enumeration fields Low to High in errorString.
882 *)
883
884 PROCEDURE ErrorRangeEnum (type: CARDINAL; set: SetRange; enumList: List) ;
885 VAR
886 Low, High: tree ;
887 BEGIN
888 Low := set^.low ;
889 High := set^.high ;
890 IF Low = NIL
891 THEN
892 Low := High
893 END ;
894 IF High = NIL
895 THEN
896 High := Low
897 END ;
898 IF (Low # NIL) AND (High # NIL)
899 THEN
900 IncludeElements (type, enumList, Low, High)
901 END
902 END ErrorRangeEnum ;
903
904
905 (*
906 ErrorRanges - return a list of all enumeration fields not present in the case statement.
907 The return value will be nil if type is not an enumeration type.
908 *)
909
910 PROCEDURE ErrorRanges (type: CARDINAL; set: SetRange) : List ;
911 VAR
912 enumSet: List ;
913 BEGIN
914 type := SkipType (type) ;
915 IF IsEnumeration (type)
916 THEN
917 InitList (enumSet) ;
918 WHILE set#NIL DO
919 ErrorRangeEnum (type, set, enumSet) ;
920 set := set^.next
921 END ;
922 RETURN enumSet
923 END ;
924 RETURN NIL
925 END ErrorRanges ;
926
927
928 (*
929 appendString - appends str to errorString.
930 *)
931
932 PROCEDURE appendString (str: String) ;
933 BEGIN
934 errorString := ConCat (errorString, str)
935 END appendString ;
936
937
938 (*
939 appendEnum - appends enum to errorString.
940 *)
941
942 PROCEDURE appendEnum (enum: CARDINAL) ;
943 BEGIN
944 appendString (Mark (InitStringCharStar (KeyToCharStar (GetSymName (enum)))))
945 END appendEnum ;
946
947
948 (*
949 appendStr - appends str to errorString.
950 *)
951
952 PROCEDURE appendStr (str: ARRAY OF CHAR) ;
953 BEGIN
954 appendString (Mark (InitString (str)))
955 END appendStr ;
956
957
958 (*
959 EnumerateErrors - populate errorString with the contents of enumList.
960 *)
961
962 PROCEDURE EnumerateErrors (enumList: List) ;
963 VAR
964 i, n: CARDINAL ;
965 BEGIN
966 n := NoOfItemsInList (enumList) ;
967 IF (enumList # NIL) AND (n > 0)
968 THEN
969 IF n = 1
970 THEN
971 errorString := InitString ('{%W}the missing enumeration field is: ') ;
972 ELSE
973 errorString := InitString ('{%W}the missing enumeration fields are: ') ;
974 END ;
975 appendEnum (GetItemFromList (enumList, 1)) ;
976 IF n > 1
977 THEN
978 IF n > 2
979 THEN
980 i := 2 ;
981 WHILE i <= n-1 DO
982 appendStr (', ') ;
983 appendEnum (GetItemFromList (enumList, i)) ;
984 INC (i)
985 END
986 END ;
987 appendStr (' and ') ;
988 appendEnum (GetItemFromList (enumList, n))
989 END
990 END
991 END EnumerateErrors ;
992
993
994 (*
995 NoOfSetElements - return the number of set elements.
996 *)
997
998 PROCEDURE NoOfSetElements (set: SetRange) : tree ;
999 BEGIN
1000 PushInt (0) ;
1001 WHILE set # NIL DO
1002 IF ((set^.low # NIL) AND (set^.high = NIL)) OR
1003 ((set^.low = NIL) AND (set^.high # NIL))
1004 THEN
1005 PushInt (1) ;
1006 Addn
1007 ELSIF (set^.low # NIL) AND (set^.high # NIL)
1008 THEN
1009 PushIntegerTree (set^.high) ;
1010 PushIntegerTree (set^.low) ;
1011 Sub ;
1012 PushInt (1) ;
1013 Addn ;
1014 Addn
1015 END ;
1016 set := set^.next
1017 END ;
1018 RETURN PopIntegerTree ()
1019 END NoOfSetElements ;
1020
1021
1022 (*
1023 isPrintableChar - a cautious isprint.
1024 *)
1025
1026 PROCEDURE isPrintableChar (value: tree) : BOOLEAN ;
1027 BEGIN
1028 CASE CSTIntToChar (value) OF
1029
1030 'a'..'z': RETURN TRUE |
1031 'A'..'Z': RETURN TRUE |
1032 '0'..'9': RETURN TRUE |
1033 '!', '@': RETURN TRUE |
1034 '#', '$': RETURN TRUE |
1035 '%', '^': RETURN TRUE |
1036 '&', '*': RETURN TRUE |
1037 '(', ')': RETURN TRUE |
1038 '[', ']': RETURN TRUE |
1039 '{', '}': RETURN TRUE |
1040 '-', '+': RETURN TRUE |
1041 '_', '=': RETURN TRUE |
1042 ':', ';': RETURN TRUE |
1043 "'", '"': RETURN TRUE |
1044 ',', '.': RETURN TRUE |
1045 '<', '>': RETURN TRUE |
1046 '/', '?': RETURN TRUE |
1047 '\', '|': RETURN TRUE |
1048 '~', '`': RETURN TRUE |
1049 ' ' : RETURN TRUE
1050
1051 ELSE
1052 RETURN FALSE
1053 END
1054 END isPrintableChar ;
1055
1056
1057 (*
1058 appendTree - append tree value to the errorString. It attempts to pretty print
1059 CHAR constants and will fall back to CHR (x) if necessary.
1060 *)
1061
1062 PROCEDURE appendTree (value: tree; type: CARDINAL) ;
1063 BEGIN
1064 IF SkipType (GetType (type)) = Char
1065 THEN
1066 IF isPrintableChar (value)
1067 THEN
1068 IF CSTIntToChar (value) = "'"
1069 THEN
1070 appendString (InitStringChar ('"')) ;
1071 appendString (InitStringChar (CSTIntToChar (value))) ;
1072 appendString (InitStringChar ('"'))
1073 ELSE
1074 appendString (InitStringChar ("'")) ;
1075 appendString (InitStringChar (CSTIntToChar (value))) ;
1076 appendString (InitStringChar ("'"))
1077 END
1078 ELSE
1079 appendString (InitString ('CHR (')) ;
1080 appendString (InitStringCharStar (CSTIntToString (value))) ;
1081 appendString (InitStringChar (')'))
1082 END
1083 ELSE
1084 appendString (InitStringCharStar (CSTIntToString (value)))
1085 END
1086 END appendTree ;
1087
1088
1089 (*
1090 SubrangeErrors - create an errorString containing all set ranges.
1091 *)
1092
1093 PROCEDURE SubrangeErrors (subrangetype: CARDINAL; set: SetRange) ;
1094 VAR
1095 sr : SetRange ;
1096 rangeNo : CARDINAL ;
1097 nMissing,
1098 zero, one: tree ;
1099 BEGIN
1100 nMissing := NoOfSetElements (set) ;
1101 PushInt (0) ;
1102 zero := PopIntegerTree () ;
1103 IF IsGreater (nMissing, zero)
1104 THEN
1105 PushInt (1) ;
1106 one := PopIntegerTree () ;
1107 IF IsGreater (nMissing, one)
1108 THEN
1109 errorString := InitString ('{%W}there are a total of ')
1110 ELSE
1111 errorString := InitString ('{%W}there is a total of ')
1112 END ;
1113 appendString (InitStringCharStar (CSTIntToString (nMissing))) ;
1114 appendStr (' missing values in the subrange, the {%kCASE} statement needs labels (or an {%kELSE} statement)') ;
1115 appendStr (' for the following values: ') ;
1116 sr := set ;
1117 rangeNo := 0 ;
1118 WHILE sr # NIL DO
1119 INC (rangeNo) ;
1120 IF rangeNo > 1
1121 THEN
1122 IF sr^.next = NIL
1123 THEN
1124 appendStr (' and ')
1125 ELSE
1126 appendStr (', ')
1127 END
1128 END ;
1129 IF sr^.low = NIL
1130 THEN
1131 appendTree (sr^.high, subrangetype)
1132 ELSIF (sr^.high = NIL) OR IsEqual (sr^.low, sr^.high)
1133 THEN
1134 appendTree (sr^.low, subrangetype)
1135 ELSE
1136 appendTree (sr^.low, subrangetype) ;
1137 appendStr ('..') ;
1138 appendTree (sr^.high, subrangetype)
1139 END ;
1140 sr := sr^.next
1141 END
1142 END
1143 END SubrangeErrors ;
1144
1145
1146 (*
1147 EmitMissingRangeErrors - emits a singular/plural error message for an enumeration type.
1148 *)
1149
1150 PROCEDURE EmitMissingRangeErrors (tokenno: CARDINAL; type: CARDINAL; set: SetRange) ;
1151 BEGIN
1152 errorString := NIL ;
1153 IF IsEnumeration (type)
1154 THEN
1155 EnumerateErrors (ErrorRanges (type, set))
1156 ELSIF IsSubrange (type)
1157 THEN
1158 SubrangeErrors (type, set)
1159 END ;
1160 IF errorString # NIL
1161 THEN
1162 MetaErrorStringT0 (tokenno, errorString)
1163 END
1164 END EmitMissingRangeErrors ;
1165
1166
1167 (*
1168 MissingCaseBounds - returns true if there were any missing bounds
1169 in the varient record case list, c. It will
1170 generate an error message for each missing
1171 bounds found.
1172 *)
1173
1174 PROCEDURE MissingCaseBounds (tokenno: CARDINAL; c: CARDINAL) : BOOLEAN ;
1175 VAR
1176 p : CaseDescriptor ;
1177 type : CARDINAL ;
1178 missing: BOOLEAN ;
1179 set : SetRange ;
1180 BEGIN
1181 p := GetIndice (caseArray, c) ;
1182 missing := FALSE ;
1183 WITH p^ DO
1184 IF NOT elseClause
1185 THEN
1186 IF (record # NulSym) AND (varient # NulSym)
1187 THEN
1188 (* Not a case statement, but a varient record without an else clause. *)
1189 type := GetVariantTagType (varient) ;
1190 set := NewSet (type) ;
1191 set := ExcludeCaseRanges (set, p) ;
1192 IF set # NIL
1193 THEN
1194 missing := TRUE ;
1195 MetaErrorT2 (tokenno,
1196 'not all variant record alternatives in the {%kCASE} clause are specified, hint you either need to specify each value of {%2ad} or use an {%kELSE} clause',
1197 varient, type) ;
1198 EmitMissingRangeErrors (tokenno, type, set)
1199 END ;
1200 set := DisposeRanges (set)
1201 END
1202 END
1203 END ;
1204 RETURN missing
1205 END MissingCaseBounds ;
1206
1207
1208 (*
1209 MissingCaseStatementBounds - returns true if the case statement has a missing
1210 clause. It will also generate error messages.
1211 *)
1212
1213 PROCEDURE MissingCaseStatementBounds (tokenno: CARDINAL; c: CARDINAL) : BOOLEAN ;
1214 VAR
1215 p : CaseDescriptor ;
1216 type : CARDINAL ;
1217 missing: BOOLEAN ;
1218 set : SetRange ;
1219 BEGIN
1220 p := GetIndice (caseArray, c) ;
1221 missing := FALSE ;
1222 WITH p^ DO
1223 IF NOT elseClause
1224 THEN
1225 type := GetCaseExpression (p) ;
1226 IF type # NulSym
1227 THEN
1228 IF IsEnumeration (type) OR IsSubrange (type)
1229 THEN
1230 (* A case statement sequence without an else clause but
1231 selecting using an enumeration type. *)
1232 set := NewSet (type) ;
1233 set := ExcludeCaseRanges (set, p) ;
1234 IF set # NIL
1235 THEN
1236 missing := TRUE ;
1237 MetaErrorT1 (tokenno,
1238 'not all {%1Wd} values in the {%kCASE} statements are specified, hint you either need to specify each value of {%1ad} or use an {%kELSE} clause',
1239 type) ;
1240 EmitMissingRangeErrors (tokenno, type, set)
1241 END ;
1242 set := DisposeRanges (set)
1243 END
1244 END
1245 END
1246 END ;
1247 RETURN missing
1248 END MissingCaseStatementBounds ;
1249
1250
1251 (*
1252 InRangeList - returns true if the value, tag, is defined in the case list.
1253
1254 procedure InRangeList (cl: CaseList; tag: cardinal) : boolean ;
1255 var
1256 i, h: cardinal ;
1257 r : RangePair ;
1258 a : tree ;
1259 begin
1260 with cl^ do
1261 i := 1 ;
1262 h := HighIndice(rangeArray) ;
1263 while i<=h do
1264 r := GetIndice(rangeArray, i) ;
1265 with r^ do
1266 if high=NulSym
1267 then
1268 a := Mod2Gcc(low)
1269 else
1270 a := Mod2Gcc(high)
1271 end ;
1272 if OverlapsRange(Mod2Gcc(low), a, Mod2Gcc(tag), Mod2Gcc(tag))
1273 then
1274 return( true )
1275 end
1276 end ;
1277 inc(i)
1278 end
1279 end ;
1280 return( false )
1281 end InRangeList ;
1282 *)
1283
1284
1285 (*
1286 WriteCase - dump out the case list (internal debugging).
1287 *)
1288
1289 PROCEDURE WriteCase (c: CARDINAL) ;
1290 BEGIN
1291 (* this debugging PROCEDURE should be finished. *)
1292 WriteCard (c, 0)
1293 END WriteCase ;
1294
1295
1296 (*
1297 checkTypes - checks to see that, constant, and, type, are compatible.
1298 *)
1299
1300 PROCEDURE checkTypes (constant, type: CARDINAL) : BOOLEAN ;
1301 VAR
1302 consttype: CARDINAL ;
1303 BEGIN
1304 IF (constant # NulSym) AND IsConst (constant)
1305 THEN
1306 consttype := GetType (constant) ;
1307 IF NOT IsExpressionCompatible (consttype, type)
1308 THEN
1309 MetaError2 ('the case statement variant tag {%1ad} must be type compatible with the constant {%2Da:is a {%2d}}',
1310 type, constant) ;
1311 RETURN FALSE
1312 END
1313 END ;
1314 RETURN TRUE
1315 END checkTypes ;
1316
1317
1318 (*
1319 inRange - returns true if, min <= i <= max.
1320 *)
1321
1322 PROCEDURE inRange (i, min, max: CARDINAL) : BOOLEAN ;
1323 BEGIN
1324 RETURN OverlapsRange (Mod2Gcc (i), Mod2Gcc (i), Mod2Gcc (min), Mod2Gcc (max))
1325 END inRange ;
1326
1327
1328 (*
1329 TypeCaseBounds - returns true if all bounds in case list, c, are
1330 compatible with the tagged type.
1331 *)
1332
1333 PROCEDURE TypeCaseBounds (c: CARDINAL) : BOOLEAN ;
1334 VAR
1335 p : CaseDescriptor ;
1336 q : CaseList ;
1337 r : RangePair ;
1338 min, max,
1339 type,
1340 i, j : CARDINAL ;
1341 compatible: BOOLEAN ;
1342 BEGIN
1343 p := GetIndice(caseArray, c) ;
1344 type := NulSym ;
1345 WITH p^ DO
1346 type := NulSym ;
1347 IF varient#NulSym
1348 THEN
1349 (* not a CASE statement, but a varient record containing without an ELSE clause *)
1350 type := GetVariantTagType(varient) ;
1351 min := GetTypeMin(type) ;
1352 max := GetTypeMax(type)
1353 END ;
1354 IF type=NulSym
1355 THEN
1356 RETURN( TRUE )
1357 END ;
1358 compatible := TRUE ;
1359 i := 1 ;
1360 WHILE i<=maxCaseId DO
1361 q := GetIndice(caseListArray, i) ;
1362 j := 1 ;
1363 WHILE j<=q^.maxRangeId DO
1364 r := GetIndice(q^.rangeArray, j) ;
1365 IF (r^.low#NulSym) AND (NOT inRange(r^.low, min, max))
1366 THEN
1367 MetaError2('the CASE statement variant range {%1ad} exceeds that of the tag type {%2ad}',
1368 r^.low, type) ;
1369 compatible := FALSE
1370 END ;
1371 IF NOT checkTypes(r^.low, type)
1372 THEN
1373 compatible := FALSE
1374 END ;
1375 IF (r^.high#NulSym) AND (NOT inRange(r^.high, min, max))
1376 THEN
1377 MetaError2('the CASE statement variant range {%1ad} exceeds that of the tag type {%2ad}',
1378 r^.high, type) ;
1379 compatible := FALSE
1380 END ;
1381 IF NOT checkTypes(r^.high, type)
1382 THEN
1383 compatible := FALSE
1384 END ;
1385 INC (j)
1386 END ;
1387 INC (i)
1388 END ;
1389 RETURN compatible
1390 END
1391 END TypeCaseBounds ;
1392
1393
1394 BEGIN
1395 caseStack := NIL ;
1396 caseId := 0 ;
1397 caseArray := InitIndex(1) ;
1398 conflictArray := InitIndex(1) ;
1399 FreeRangeList := NIL
1400 END M2CaseList.