* gcc.dg/Wtrampolines.c: XFAIL AIX.
[official-gcc.git] / gcc / ada / sem_case.adb
blobf8368536c50178f152c8dc38d2fa1caf07cd47fd
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C A S E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1996-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Einfo; use Einfo;
28 with Errout; use Errout;
29 with Namet; use Namet;
30 with Nlists; use Nlists;
31 with Nmake; use Nmake;
32 with Opt; use Opt;
33 with Sem; use Sem;
34 with Sem_Aux; use Sem_Aux;
35 with Sem_Eval; use Sem_Eval;
36 with Sem_Res; use Sem_Res;
37 with Sem_Util; use Sem_Util;
38 with Sem_Type; use Sem_Type;
39 with Snames; use Snames;
40 with Stand; use Stand;
41 with Sinfo; use Sinfo;
42 with Tbuild; use Tbuild;
43 with Uintp; use Uintp;
45 with Ada.Unchecked_Deallocation;
47 with GNAT.Heap_Sort_G;
49 package body Sem_Case is
51 type Choice_Bounds is record
52 Lo : Node_Id;
53 Hi : Node_Id;
54 Node : Node_Id;
55 end record;
56 -- Represent one choice bounds entry with Lo and Hi values, Node points
57 -- to the choice node itself.
59 type Choice_Table_Type is array (Nat range <>) of Choice_Bounds;
60 -- Table type used to sort the choices present in a case statement or
61 -- record variant. The actual entries are stored in 1 .. Last, but we
62 -- have a 0 entry for use in sorting.
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
68 procedure Check_Choice_Set
69 (Choice_Table : in out Choice_Table_Type;
70 Bounds_Type : Entity_Id;
71 Subtyp : Entity_Id;
72 Others_Present : Boolean;
73 Case_Node : Node_Id);
74 -- This is the procedure which verifies that a set of case alternatives
75 -- or record variant choices has no duplicates, and covers the range
76 -- specified by Bounds_Type. Choice_Table contains the discrete choices
77 -- to check. These must start at position 1.
79 -- Furthermore Choice_Table (0) must exist. This element is used by
80 -- the sorting algorithm as a temporary. Others_Present is a flag
81 -- indicating whether or not an Others choice is present. Finally
82 -- Msg_Sloc gives the source location of the construct containing the
83 -- choices in the Choice_Table.
85 -- Bounds_Type is the type whose range must be covered by the alternatives
87 -- Subtyp is the subtype of the expression. If its bounds are non-static
88 -- the alternatives must cover its base type.
90 function Choice_Image (Value : Uint; Ctype : Entity_Id) return Name_Id;
91 -- Given a Pos value of enumeration type Ctype, returns the name
92 -- ID of an appropriate string to be used in error message output.
94 procedure Expand_Others_Choice
95 (Case_Table : Choice_Table_Type;
96 Others_Choice : Node_Id;
97 Choice_Type : Entity_Id);
98 -- The case table is the table generated by a call to Check_Choices
99 -- (with just 1 .. Last_Choice entries present). Others_Choice is a
100 -- pointer to the N_Others_Choice node (this routine is only called if
101 -- an others choice is present), and Choice_Type is the discrete type
102 -- of the bounds. The effect of this call is to analyze the cases and
103 -- determine the set of values covered by others. This choice list is
104 -- set in the Others_Discrete_Choices field of the N_Others_Choice node.
106 ----------------------
107 -- Check_Choice_Set --
108 ----------------------
110 procedure Check_Choice_Set
111 (Choice_Table : in out Choice_Table_Type;
112 Bounds_Type : Entity_Id;
113 Subtyp : Entity_Id;
114 Others_Present : Boolean;
115 Case_Node : Node_Id)
117 Predicate_Error : Boolean := False;
118 -- Flag to prevent cascaded errors when a static predicate is known to
119 -- be violated by one choice.
121 Num_Choices : constant Nat := Choice_Table'Last;
123 procedure Check_Against_Predicate
124 (Pred : in out Node_Id;
125 Choice : Choice_Bounds;
126 Prev_Lo : in out Uint;
127 Prev_Hi : in out Uint;
128 Error : in out Boolean);
129 -- Determine whether a choice covers legal values as defined by a static
130 -- predicate set. Pred is a static predicate range. Choice is the choice
131 -- to be examined. Prev_Lo and Prev_Hi are the bounds of the previous
132 -- choice that covered a predicate set. Error denotes whether the check
133 -- found an illegal intersection.
135 procedure Check_Duplicates;
136 -- Check for duplicate choices, and call Dup_Choice if there are any
137 -- such errors. Note that predicates are irrelevant here.
139 procedure Dup_Choice (Lo, Hi : Uint; C : Node_Id);
140 -- Post message "duplication of choice value(s) bla bla at xx". Message
141 -- is posted at location C. Caller sets Error_Msg_Sloc for xx.
143 procedure Explain_Non_Static_Bound;
144 -- Called when we find a non-static bound, requiring the base type to
145 -- be covered. Provides where possible a helpful explanation of why the
146 -- bounds are non-static, since this is not always obvious.
148 function Lt_Choice (C1, C2 : Natural) return Boolean;
149 -- Comparison routine for comparing Choice_Table entries. Use the lower
150 -- bound of each Choice as the key.
152 procedure Missing_Choice (Value1 : Node_Id; Value2 : Node_Id);
153 procedure Missing_Choice (Value1 : Node_Id; Value2 : Uint);
154 procedure Missing_Choice (Value1 : Uint; Value2 : Node_Id);
155 procedure Missing_Choice (Value1 : Uint; Value2 : Uint);
156 -- Issue an error message indicating that there are missing choices,
157 -- followed by the image of the missing choices themselves which lie
158 -- between Value1 and Value2 inclusive.
160 procedure Missing_Choices (Pred : Node_Id; Prev_Hi : Uint);
161 -- Emit an error message for each non-covered static predicate set.
162 -- Prev_Hi denotes the upper bound of the last choice covering a set.
164 procedure Move_Choice (From : Natural; To : Natural);
165 -- Move routine for sorting the Choice_Table
167 package Sorting is new GNAT.Heap_Sort_G (Move_Choice, Lt_Choice);
169 -----------------------------
170 -- Check_Against_Predicate --
171 -----------------------------
173 procedure Check_Against_Predicate
174 (Pred : in out Node_Id;
175 Choice : Choice_Bounds;
176 Prev_Lo : in out Uint;
177 Prev_Hi : in out Uint;
178 Error : in out Boolean)
180 procedure Illegal_Range
181 (Loc : Source_Ptr;
182 Lo : Uint;
183 Hi : Uint);
184 -- Emit an error message regarding a choice that clashes with the
185 -- legal static predicate sets. Loc is the location of the choice
186 -- that introduced the illegal range. Lo .. Hi is the range.
188 function Inside_Range
189 (Lo : Uint;
190 Hi : Uint;
191 Val : Uint) return Boolean;
192 -- Determine whether position Val within a discrete type is within
193 -- the range Lo .. Hi inclusive.
195 -------------------
196 -- Illegal_Range --
197 -------------------
199 procedure Illegal_Range
200 (Loc : Source_Ptr;
201 Lo : Uint;
202 Hi : Uint)
204 begin
205 Error_Msg_Name_1 := Chars (Bounds_Type);
207 -- Single value
209 if Lo = Hi then
210 if Is_Integer_Type (Bounds_Type) then
211 Error_Msg_Uint_1 := Lo;
212 Error_Msg ("static predicate on % excludes value ^!", Loc);
213 else
214 Error_Msg_Name_2 := Choice_Image (Lo, Bounds_Type);
215 Error_Msg ("static predicate on % excludes value %!", Loc);
216 end if;
218 -- Range
220 else
221 if Is_Integer_Type (Bounds_Type) then
222 Error_Msg_Uint_1 := Lo;
223 Error_Msg_Uint_2 := Hi;
224 Error_Msg
225 ("static predicate on % excludes range ^ .. ^!", Loc);
226 else
227 Error_Msg_Name_2 := Choice_Image (Lo, Bounds_Type);
228 Error_Msg_Name_3 := Choice_Image (Hi, Bounds_Type);
229 Error_Msg
230 ("static predicate on % excludes range % .. %!", Loc);
231 end if;
232 end if;
233 end Illegal_Range;
235 ------------------
236 -- Inside_Range --
237 ------------------
239 function Inside_Range
240 (Lo : Uint;
241 Hi : Uint;
242 Val : Uint) return Boolean
244 begin
245 return Lo <= Val and then Val <= Hi;
246 end Inside_Range;
248 -- Local variables
250 Choice_Hi : constant Uint := Expr_Value (Choice.Hi);
251 Choice_Lo : constant Uint := Expr_Value (Choice.Lo);
252 Loc : Source_Ptr;
253 LocN : Node_Id;
254 Next_Hi : Uint;
255 Next_Lo : Uint;
256 Pred_Hi : Uint;
257 Pred_Lo : Uint;
259 -- Start of processing for Check_Against_Predicate
261 begin
262 -- Find the proper error message location
264 if Present (Choice.Node) then
265 LocN := Choice.Node;
266 else
267 LocN := Case_Node;
268 end if;
270 Loc := Sloc (LocN);
272 if Present (Pred) then
273 Pred_Lo := Expr_Value (Low_Bound (Pred));
274 Pred_Hi := Expr_Value (High_Bound (Pred));
276 -- Previous choices managed to satisfy all static predicate sets
278 else
279 Illegal_Range (Loc, Choice_Lo, Choice_Hi);
280 Error := True;
281 return;
282 end if;
284 -- Step 1: Ignore duplicate choices, other than to set the flag,
285 -- because these were already detected by Check_Duplicates.
287 if Inside_Range (Choice_Lo, Choice_Hi, Prev_Lo)
288 or else Inside_Range (Choice_Lo, Choice_Hi, Prev_Hi)
289 then
290 Error := True;
292 -- Step 2: Detect full coverage
294 -- Choice_Lo Choice_Hi
295 -- +============+
296 -- Pred_Lo Pred_Hi
298 elsif Choice_Lo = Pred_Lo and then Choice_Hi = Pred_Hi then
299 Prev_Lo := Choice_Lo;
300 Prev_Hi := Choice_Hi;
301 Next (Pred);
303 -- Step 3: Detect all cases where a choice mentions values that are
304 -- not part of the static predicate sets.
306 -- Choice_Lo Choice_Hi Pred_Lo Pred_Hi
307 -- +-----------+ . . . . . +=========+
308 -- ^ illegal ^
310 elsif Choice_Lo < Pred_Lo and then Choice_Hi < Pred_Lo then
311 Illegal_Range (Loc, Choice_Lo, Choice_Hi);
312 Error := True;
314 -- Choice_Lo Pred_Lo Choice_Hi Pred_Hi
315 -- +-----------+=========+===========+
316 -- ^ illegal ^
318 elsif Choice_Lo < Pred_Lo
319 and then Inside_Range (Pred_Lo, Pred_Hi, Choice_Hi)
320 then
321 Illegal_Range (Loc, Choice_Lo, Pred_Lo - 1);
322 Error := True;
324 -- Pred_Lo Pred_Hi Choice_Lo Choice_Hi
325 -- +=========+ . . . . +-----------+
326 -- ^ illegal ^
328 elsif Pred_Lo < Choice_Lo and then Pred_Hi < Choice_Lo then
329 if Others_Present then
331 -- Current predicate set is covered by others clause.
333 null;
335 else
336 Missing_Choice (Pred_Lo, Pred_Hi);
337 Error := True;
338 end if;
340 -- There may be several static predicate sets between the current
341 -- one and the choice. Inspect the next static predicate set.
343 Next (Pred);
344 Check_Against_Predicate
345 (Pred => Pred,
346 Choice => Choice,
347 Prev_Lo => Prev_Lo,
348 Prev_Hi => Prev_Hi,
349 Error => Error);
351 -- Pred_Lo Choice_Lo Pred_Hi Choice_Hi
352 -- +=========+===========+-----------+
353 -- ^ illegal ^
355 elsif Pred_Hi < Choice_Hi
356 and then Inside_Range (Pred_Lo, Pred_Hi, Choice_Lo)
357 then
358 Next (Pred);
360 -- The choice may fall in a static predicate set. If this is the
361 -- case, avoid mentioning legal values in the error message.
363 if Present (Pred) then
364 Next_Lo := Expr_Value (Low_Bound (Pred));
365 Next_Hi := Expr_Value (High_Bound (Pred));
367 -- The next static predicate set is to the right of the choice
369 if Choice_Hi < Next_Lo and then Choice_Hi < Next_Hi then
370 Illegal_Range (Loc, Pred_Hi + 1, Choice_Hi);
371 else
372 Illegal_Range (Loc, Pred_Hi + 1, Next_Lo - 1);
373 end if;
374 else
375 Illegal_Range (Loc, Pred_Hi + 1, Choice_Hi);
376 end if;
378 Error := True;
380 -- Choice_Lo Pred_Lo Pred_Hi Choice_Hi
381 -- +-----------+=========+-----------+
382 -- ^ illegal ^ ^ illegal ^
384 -- Emit an error on the low gap, disregard the upper gap
386 elsif Choice_Lo < Pred_Lo and then Pred_Hi < Choice_Hi then
387 Illegal_Range (Loc, Choice_Lo, Pred_Lo - 1);
388 Error := True;
390 -- Step 4: Detect all cases of partial or missing coverage
392 -- Pred_Lo Choice_Lo Choice_Hi Pred_Hi
393 -- +=========+==========+===========+
394 -- ^ gap ^ ^ gap ^
396 else
397 -- An "others" choice covers all gaps
399 if Others_Present then
400 Prev_Lo := Choice_Lo;
401 Prev_Hi := Choice_Hi;
403 -- Check whether predicate set is fully covered by choice
405 if Pred_Hi = Choice_Hi then
406 Next (Pred);
407 end if;
409 -- Choice_Lo Choice_Hi Pred_Hi
410 -- +===========+===========+
411 -- Pred_Lo ^ gap ^
413 -- The upper gap may be covered by a subsequent choice
415 elsif Pred_Lo = Choice_Lo then
416 Prev_Lo := Choice_Lo;
417 Prev_Hi := Choice_Hi;
419 -- Pred_Lo Prev_Hi Choice_Lo Choice_Hi Pred_Hi
420 -- +===========+=========+===========+===========+
421 -- ^ covered ^ ^ gap ^
423 else pragma Assert (Pred_Lo < Choice_Lo);
425 -- A previous choice covered the gap up to the current choice
427 if Prev_Hi = Choice_Lo - 1 then
428 Prev_Lo := Choice_Lo;
429 Prev_Hi := Choice_Hi;
431 if Choice_Hi = Pred_Hi then
432 Next (Pred);
433 end if;
435 -- The previous choice did not intersect with the current
436 -- static predicate set.
438 elsif Prev_Hi < Pred_Lo then
439 Missing_Choice (Pred_Lo, Choice_Lo - 1);
440 Error := True;
442 -- The previous choice covered part of the static predicate set
443 -- but there is a gap after Prev_Hi.
445 else
446 Missing_Choice (Prev_Hi + 1, Choice_Lo - 1);
447 Error := True;
448 end if;
449 end if;
450 end if;
451 end Check_Against_Predicate;
453 ----------------------
454 -- Check_Duplicates --
455 ----------------------
457 procedure Check_Duplicates is
458 Choice : Node_Id;
459 Choice_Hi : Uint;
460 Choice_Lo : Uint;
461 Prev_Choice : Node_Id;
462 Prev_Hi : Uint;
464 begin
465 Prev_Hi := Expr_Value (Choice_Table (1).Hi);
467 for Outer_Index in 2 .. Num_Choices loop
468 Choice_Lo := Expr_Value (Choice_Table (Outer_Index).Lo);
469 Choice_Hi := Expr_Value (Choice_Table (Outer_Index).Hi);
471 -- Choices overlap; this is an error
473 if Choice_Lo <= Prev_Hi then
474 Choice := Choice_Table (Outer_Index).Node;
476 -- Find first previous choice that overlaps
478 for Inner_Index in 1 .. Outer_Index - 1 loop
479 if Choice_Lo <=
480 Expr_Value (Choice_Table (Inner_Index).Hi)
481 then
482 Prev_Choice := Choice_Table (Inner_Index).Node;
483 exit;
484 end if;
485 end loop;
487 if Sloc (Prev_Choice) <= Sloc (Choice) then
488 Error_Msg_Sloc := Sloc (Prev_Choice);
489 Dup_Choice (Choice_Lo, UI_Min (Choice_Hi, Prev_Hi), Choice);
490 else
491 Error_Msg_Sloc := Sloc (Choice);
492 Dup_Choice
493 (Choice_Lo, UI_Min (Choice_Hi, Prev_Hi), Prev_Choice);
494 end if;
495 end if;
497 if Choice_Hi > Prev_Hi then
498 Prev_Hi := Choice_Hi;
499 end if;
500 end loop;
501 end Check_Duplicates;
503 ----------------
504 -- Dup_Choice --
505 ----------------
507 procedure Dup_Choice (Lo, Hi : Uint; C : Node_Id) is
508 begin
509 -- In some situations, we call this with a null range, and obviously
510 -- we don't want to complain in this case.
512 if Lo > Hi then
513 return;
514 end if;
516 -- Case of only one value that is duplicated
518 if Lo = Hi then
520 -- Integer type
522 if Is_Integer_Type (Bounds_Type) then
524 -- We have an integer value, Lo, but if the given choice
525 -- placement is a constant with that value, then use the
526 -- name of that constant instead in the message:
528 if Nkind (C) = N_Identifier
529 and then Compile_Time_Known_Value (C)
530 and then Expr_Value (C) = Lo
531 then
532 Error_Msg_N ("duplication of choice value: &#!", C);
534 -- Not that special case, so just output the integer value
536 else
537 Error_Msg_Uint_1 := Lo;
538 Error_Msg_N ("duplication of choice value: ^#!", C);
539 end if;
541 -- Enumeration type
543 else
544 Error_Msg_Name_1 := Choice_Image (Lo, Bounds_Type);
545 Error_Msg_N ("duplication of choice value: %#!", C);
546 end if;
548 -- More than one choice value, so print range of values
550 else
551 -- Integer type
553 if Is_Integer_Type (Bounds_Type) then
555 -- Similar to the above, if C is a range of known values which
556 -- match Lo and Hi, then use the names. We have to go to the
557 -- original nodes, since the values will have been rewritten
558 -- to their integer values.
560 if Nkind (C) = N_Range
561 and then Nkind (Original_Node (Low_Bound (C))) = N_Identifier
562 and then Nkind (Original_Node (High_Bound (C))) = N_Identifier
563 and then Compile_Time_Known_Value (Low_Bound (C))
564 and then Compile_Time_Known_Value (High_Bound (C))
565 and then Expr_Value (Low_Bound (C)) = Lo
566 and then Expr_Value (High_Bound (C)) = Hi
567 then
568 Error_Msg_Node_2 := Original_Node (High_Bound (C));
569 Error_Msg_N
570 ("duplication of choice values: & .. &#!",
571 Original_Node (Low_Bound (C)));
573 -- Not that special case, output integer values
575 else
576 Error_Msg_Uint_1 := Lo;
577 Error_Msg_Uint_2 := Hi;
578 Error_Msg_N ("duplication of choice values: ^ .. ^#!", C);
579 end if;
581 -- Enumeration type
583 else
584 Error_Msg_Name_1 := Choice_Image (Lo, Bounds_Type);
585 Error_Msg_Name_2 := Choice_Image (Hi, Bounds_Type);
586 Error_Msg_N ("duplication of choice values: % .. %#!", C);
587 end if;
588 end if;
589 end Dup_Choice;
591 ------------------------------
592 -- Explain_Non_Static_Bound --
593 ------------------------------
595 procedure Explain_Non_Static_Bound is
596 Expr : Node_Id;
598 begin
599 if Nkind (Case_Node) = N_Variant_Part then
600 Expr := Name (Case_Node);
601 else
602 Expr := Expression (Case_Node);
603 end if;
605 if Bounds_Type /= Subtyp then
607 -- If the case is a variant part, the expression is given by the
608 -- discriminant itself, and the bounds are the culprits.
610 if Nkind (Case_Node) = N_Variant_Part then
611 Error_Msg_NE
612 ("bounds of & are not static, "
613 & "alternatives must cover base type!", Expr, Expr);
615 -- If this is a case statement, the expression may be non-static
616 -- or else the subtype may be at fault.
618 elsif Is_Entity_Name (Expr) then
619 Error_Msg_NE
620 ("bounds of & are not static, "
621 & "alternatives must cover base type!", Expr, Expr);
623 else
624 Error_Msg_N
625 ("subtype of expression is not static, "
626 & "alternatives must cover base type!", Expr);
627 end if;
629 -- Otherwise the expression is not static, even if the bounds of the
630 -- type are, or else there are missing alternatives. If both, the
631 -- additional information may be redundant but harmless.
633 elsif not Is_Entity_Name (Expr) then
634 Error_Msg_N
635 ("subtype of expression is not static, "
636 & "alternatives must cover base type!", Expr);
637 end if;
638 end Explain_Non_Static_Bound;
640 ---------------
641 -- Lt_Choice --
642 ---------------
644 function Lt_Choice (C1, C2 : Natural) return Boolean is
645 begin
646 return
647 Expr_Value (Choice_Table (Nat (C1)).Lo)
649 Expr_Value (Choice_Table (Nat (C2)).Lo);
650 end Lt_Choice;
652 --------------------
653 -- Missing_Choice --
654 --------------------
656 procedure Missing_Choice (Value1 : Node_Id; Value2 : Node_Id) is
657 begin
658 Missing_Choice (Expr_Value (Value1), Expr_Value (Value2));
659 end Missing_Choice;
661 procedure Missing_Choice (Value1 : Node_Id; Value2 : Uint) is
662 begin
663 Missing_Choice (Expr_Value (Value1), Value2);
664 end Missing_Choice;
666 procedure Missing_Choice (Value1 : Uint; Value2 : Node_Id) is
667 begin
668 Missing_Choice (Value1, Expr_Value (Value2));
669 end Missing_Choice;
671 --------------------
672 -- Missing_Choice --
673 --------------------
675 procedure Missing_Choice (Value1 : Uint; Value2 : Uint) is
676 Msg_Sloc : constant Source_Ptr := Sloc (Case_Node);
678 begin
679 -- AI05-0188 : within an instance the non-others choices do not have
680 -- to belong to the actual subtype.
682 if Ada_Version >= Ada_2012 and then In_Instance then
683 return;
685 -- In some situations, we call this with a null range, and obviously
686 -- we don't want to complain in this case.
688 elsif Value1 > Value2 then
689 return;
691 -- If predicate is already known to be violated, do no check for
692 -- coverage error, to prevent cascaded messages.
694 elsif Predicate_Error then
695 return;
696 end if;
698 -- Case of only one value that is missing
700 if Value1 = Value2 then
701 if Is_Integer_Type (Bounds_Type) then
702 Error_Msg_Uint_1 := Value1;
703 Error_Msg ("missing case value: ^!", Msg_Sloc);
704 else
705 Error_Msg_Name_1 := Choice_Image (Value1, Bounds_Type);
706 Error_Msg ("missing case value: %!", Msg_Sloc);
707 end if;
709 -- More than one choice value, so print range of values
711 else
712 if Is_Integer_Type (Bounds_Type) then
713 Error_Msg_Uint_1 := Value1;
714 Error_Msg_Uint_2 := Value2;
715 Error_Msg ("missing case values: ^ .. ^!", Msg_Sloc);
716 else
717 Error_Msg_Name_1 := Choice_Image (Value1, Bounds_Type);
718 Error_Msg_Name_2 := Choice_Image (Value2, Bounds_Type);
719 Error_Msg ("missing case values: % .. %!", Msg_Sloc);
720 end if;
721 end if;
722 end Missing_Choice;
724 ---------------------
725 -- Missing_Choices --
726 ---------------------
728 procedure Missing_Choices (Pred : Node_Id; Prev_Hi : Uint) is
729 Hi : Uint;
730 Lo : Uint;
731 Set : Node_Id;
733 begin
734 Set := Pred;
735 while Present (Set) loop
736 Lo := Expr_Value (Low_Bound (Set));
737 Hi := Expr_Value (High_Bound (Set));
739 -- A choice covered part of a static predicate set
741 if Lo <= Prev_Hi and then Prev_Hi < Hi then
742 Missing_Choice (Prev_Hi + 1, Hi);
744 else
745 Missing_Choice (Lo, Hi);
746 end if;
748 Next (Set);
749 end loop;
750 end Missing_Choices;
752 -----------------
753 -- Move_Choice --
754 -----------------
756 procedure Move_Choice (From : Natural; To : Natural) is
757 begin
758 Choice_Table (Nat (To)) := Choice_Table (Nat (From));
759 end Move_Choice;
761 -- Local variables
763 Bounds_Hi : constant Node_Id := Type_High_Bound (Bounds_Type);
764 Bounds_Lo : constant Node_Id := Type_Low_Bound (Bounds_Type);
765 Has_Predicate : constant Boolean :=
766 Is_OK_Static_Subtype (Bounds_Type)
767 and then Has_Static_Predicate (Bounds_Type);
769 Choice_Hi : Uint;
770 Choice_Lo : Uint;
771 Pred : Node_Id;
772 Prev_Lo : Uint;
773 Prev_Hi : Uint;
775 -- Start of processing for Check_Choice_Set
777 begin
778 -- If the case is part of a predicate aspect specification, do not
779 -- recheck it against itself.
781 if Present (Parent (Case_Node))
782 and then Nkind (Parent (Case_Node)) = N_Aspect_Specification
783 then
784 return;
785 end if;
787 -- Choice_Table must start at 0 which is an unused location used by the
788 -- sorting algorithm. However the first valid position for a discrete
789 -- choice is 1.
791 pragma Assert (Choice_Table'First = 0);
793 -- The choices do not cover the base range. Emit an error if "others" is
794 -- not available and return as there is no need for further processing.
796 if Num_Choices = 0 then
797 if not Others_Present then
798 Missing_Choice (Bounds_Lo, Bounds_Hi);
799 end if;
801 return;
802 end if;
804 Sorting.Sort (Positive (Choice_Table'Last));
806 -- First check for duplicates. This involved the choices; predicates, if
807 -- any, are irrelevant.
809 Check_Duplicates;
811 -- Then check for overlaps
813 -- If the subtype has a static predicate, the predicate defines subsets
814 -- of legal values and requires finer-grained analysis.
816 -- Note that in GNAT the predicate is considered static if the predicate
817 -- expression is static, independently of whether the aspect mentions
818 -- Static explicitly.
820 if Has_Predicate then
821 Pred := First (Static_Discrete_Predicate (Bounds_Type));
823 -- Make initial value smaller than 'First of type, so that first
824 -- range comparison succeeds. This applies both to integer types
825 -- and to enumeration types.
827 Prev_Lo := Expr_Value (Type_Low_Bound (Bounds_Type)) - 1;
828 Prev_Hi := Prev_Lo;
830 declare
831 Error : Boolean := False;
832 begin
833 for Index in 1 .. Num_Choices loop
834 Check_Against_Predicate
835 (Pred => Pred,
836 Choice => Choice_Table (Index),
837 Prev_Lo => Prev_Lo,
838 Prev_Hi => Prev_Hi,
839 Error => Error);
841 -- The analysis detected an illegal intersection between a
842 -- choice and a static predicate set. Do not examine other
843 -- choices unless all errors are requested.
845 if Error then
846 Predicate_Error := True;
848 if not All_Errors_Mode then
849 return;
850 end if;
851 end if;
852 end loop;
853 end;
855 if Predicate_Error then
856 return;
857 end if;
859 -- The choices may legally cover some of the static predicate sets,
860 -- but not all. Emit an error for each non-covered set.
862 if not Others_Present then
863 Missing_Choices (Pred, Prev_Hi);
864 end if;
866 -- Default analysis
868 else
869 Choice_Lo := Expr_Value (Choice_Table (1).Lo);
870 Choice_Hi := Expr_Value (Choice_Table (1).Hi);
871 Prev_Hi := Choice_Hi;
873 if not Others_Present and then Expr_Value (Bounds_Lo) < Choice_Lo then
874 Missing_Choice (Bounds_Lo, Choice_Lo - 1);
876 -- If values are missing outside of the subtype, add explanation.
877 -- No additional message if only one value is missing.
879 if Expr_Value (Bounds_Lo) < Choice_Lo - 1 then
880 Explain_Non_Static_Bound;
881 end if;
882 end if;
884 for Index in 2 .. Num_Choices loop
885 Choice_Lo := Expr_Value (Choice_Table (Index).Lo);
886 Choice_Hi := Expr_Value (Choice_Table (Index).Hi);
888 if Choice_Lo > Prev_Hi + 1 and then not Others_Present then
889 Missing_Choice (Prev_Hi + 1, Choice_Lo - 1);
890 end if;
892 if Choice_Hi > Prev_Hi then
893 Prev_Hi := Choice_Hi;
894 end if;
895 end loop;
897 if not Others_Present and then Expr_Value (Bounds_Hi) > Prev_Hi then
898 Missing_Choice (Prev_Hi + 1, Bounds_Hi);
900 if Expr_Value (Bounds_Hi) > Prev_Hi + 1 then
901 Explain_Non_Static_Bound;
902 end if;
903 end if;
904 end if;
905 end Check_Choice_Set;
907 ------------------
908 -- Choice_Image --
909 ------------------
911 function Choice_Image (Value : Uint; Ctype : Entity_Id) return Name_Id is
912 Rtp : constant Entity_Id := Root_Type (Ctype);
913 Lit : Entity_Id;
914 C : Int;
916 begin
917 -- For character, or wide [wide] character. If 7-bit ASCII graphic
918 -- range, then build and return appropriate character literal name
920 if Is_Standard_Character_Type (Ctype) then
921 C := UI_To_Int (Value);
923 if C in 16#20# .. 16#7E# then
924 Set_Character_Literal_Name (Char_Code (UI_To_Int (Value)));
925 return Name_Find;
926 end if;
928 -- For user defined enumeration type, find enum/char literal
930 else
931 Lit := First_Literal (Rtp);
933 for J in 1 .. UI_To_Int (Value) loop
934 Next_Literal (Lit);
935 end loop;
937 -- If enumeration literal, just return its value
939 if Nkind (Lit) = N_Defining_Identifier then
940 return Chars (Lit);
942 -- For character literal, get the name and use it if it is
943 -- for a 7-bit ASCII graphic character in 16#20#..16#7E#.
945 else
946 Get_Decoded_Name_String (Chars (Lit));
948 if Name_Len = 3
949 and then Name_Buffer (2) in
950 Character'Val (16#20#) .. Character'Val (16#7E#)
951 then
952 return Chars (Lit);
953 end if;
954 end if;
955 end if;
957 -- If we fall through, we have a character literal which is not in
958 -- the 7-bit ASCII graphic set. For such cases, we construct the
959 -- name "type'val(nnn)" where type is the choice type, and nnn is
960 -- the pos value passed as an argument to Choice_Image.
962 Get_Name_String (Chars (First_Subtype (Ctype)));
964 Add_Str_To_Name_Buffer ("'val(");
965 UI_Image (Value);
966 Add_Str_To_Name_Buffer (UI_Image_Buffer (1 .. UI_Image_Length));
967 Add_Char_To_Name_Buffer (')');
968 return Name_Find;
969 end Choice_Image;
971 --------------------------
972 -- Expand_Others_Choice --
973 --------------------------
975 procedure Expand_Others_Choice
976 (Case_Table : Choice_Table_Type;
977 Others_Choice : Node_Id;
978 Choice_Type : Entity_Id)
980 Loc : constant Source_Ptr := Sloc (Others_Choice);
981 Choice_List : constant List_Id := New_List;
982 Choice : Node_Id;
983 Exp_Lo : Node_Id;
984 Exp_Hi : Node_Id;
985 Hi : Uint;
986 Lo : Uint;
987 Previous_Hi : Uint;
989 function Build_Choice (Value1, Value2 : Uint) return Node_Id;
990 -- Builds a node representing the missing choices given by Value1 and
991 -- Value2. A N_Range node is built if there is more than one literal
992 -- value missing. Otherwise a single N_Integer_Literal, N_Identifier
993 -- or N_Character_Literal is built depending on what Choice_Type is.
995 function Lit_Of (Value : Uint) return Node_Id;
996 -- Returns the Node_Id for the enumeration literal corresponding to the
997 -- position given by Value within the enumeration type Choice_Type.
999 ------------------
1000 -- Build_Choice --
1001 ------------------
1003 function Build_Choice (Value1, Value2 : Uint) return Node_Id is
1004 Lit_Node : Node_Id;
1005 Lo, Hi : Node_Id;
1007 begin
1008 -- If there is only one choice value missing between Value1 and
1009 -- Value2, build an integer or enumeration literal to represent it.
1011 if (Value2 - Value1) = 0 then
1012 if Is_Integer_Type (Choice_Type) then
1013 Lit_Node := Make_Integer_Literal (Loc, Value1);
1014 Set_Etype (Lit_Node, Choice_Type);
1015 else
1016 Lit_Node := Lit_Of (Value1);
1017 end if;
1019 -- Otherwise is more that one choice value that is missing between
1020 -- Value1 and Value2, therefore build a N_Range node of either
1021 -- integer or enumeration literals.
1023 else
1024 if Is_Integer_Type (Choice_Type) then
1025 Lo := Make_Integer_Literal (Loc, Value1);
1026 Set_Etype (Lo, Choice_Type);
1027 Hi := Make_Integer_Literal (Loc, Value2);
1028 Set_Etype (Hi, Choice_Type);
1029 Lit_Node :=
1030 Make_Range (Loc,
1031 Low_Bound => Lo,
1032 High_Bound => Hi);
1034 else
1035 Lit_Node :=
1036 Make_Range (Loc,
1037 Low_Bound => Lit_Of (Value1),
1038 High_Bound => Lit_Of (Value2));
1039 end if;
1040 end if;
1042 return Lit_Node;
1043 end Build_Choice;
1045 ------------
1046 -- Lit_Of --
1047 ------------
1049 function Lit_Of (Value : Uint) return Node_Id is
1050 Lit : Entity_Id;
1052 begin
1053 -- In the case where the literal is of type Character, there needs
1054 -- to be some special handling since there is no explicit chain
1055 -- of literals to search. Instead, a N_Character_Literal node
1056 -- is created with the appropriate Char_Code and Chars fields.
1058 if Is_Standard_Character_Type (Choice_Type) then
1059 Set_Character_Literal_Name (Char_Code (UI_To_Int (Value)));
1060 Lit := New_Node (N_Character_Literal, Loc);
1061 Set_Chars (Lit, Name_Find);
1062 Set_Char_Literal_Value (Lit, Value);
1063 Set_Etype (Lit, Choice_Type);
1064 Set_Is_Static_Expression (Lit, True);
1065 return Lit;
1067 -- Otherwise, iterate through the literals list of Choice_Type
1068 -- "Value" number of times until the desired literal is reached
1069 -- and then return an occurrence of it.
1071 else
1072 Lit := First_Literal (Choice_Type);
1073 for J in 1 .. UI_To_Int (Value) loop
1074 Next_Literal (Lit);
1075 end loop;
1077 return New_Occurrence_Of (Lit, Loc);
1078 end if;
1079 end Lit_Of;
1081 -- Start of processing for Expand_Others_Choice
1083 begin
1084 if Case_Table'Last = 0 then
1086 -- Special case: only an others case is present. The others case
1087 -- covers the full range of the type.
1089 if Is_OK_Static_Subtype (Choice_Type) then
1090 Choice := New_Occurrence_Of (Choice_Type, Loc);
1091 else
1092 Choice := New_Occurrence_Of (Base_Type (Choice_Type), Loc);
1093 end if;
1095 Set_Others_Discrete_Choices (Others_Choice, New_List (Choice));
1096 return;
1097 end if;
1099 -- Establish the bound values for the choice depending upon whether the
1100 -- type of the case statement is static or not.
1102 if Is_OK_Static_Subtype (Choice_Type) then
1103 Exp_Lo := Type_Low_Bound (Choice_Type);
1104 Exp_Hi := Type_High_Bound (Choice_Type);
1105 else
1106 Exp_Lo := Type_Low_Bound (Base_Type (Choice_Type));
1107 Exp_Hi := Type_High_Bound (Base_Type (Choice_Type));
1108 end if;
1110 Lo := Expr_Value (Case_Table (1).Lo);
1111 Hi := Expr_Value (Case_Table (1).Hi);
1112 Previous_Hi := Expr_Value (Case_Table (1).Hi);
1114 -- Build the node for any missing choices that are smaller than any
1115 -- explicit choices given in the case.
1117 if Expr_Value (Exp_Lo) < Lo then
1118 Append (Build_Choice (Expr_Value (Exp_Lo), Lo - 1), Choice_List);
1119 end if;
1121 -- Build the nodes representing any missing choices that lie between
1122 -- the explicit ones given in the case.
1124 for J in 2 .. Case_Table'Last loop
1125 Lo := Expr_Value (Case_Table (J).Lo);
1126 Hi := Expr_Value (Case_Table (J).Hi);
1128 if Lo /= (Previous_Hi + 1) then
1129 Append_To (Choice_List, Build_Choice (Previous_Hi + 1, Lo - 1));
1130 end if;
1132 Previous_Hi := Hi;
1133 end loop;
1135 -- Build the node for any missing choices that are greater than any
1136 -- explicit choices given in the case.
1138 if Expr_Value (Exp_Hi) > Hi then
1139 Append (Build_Choice (Hi + 1, Expr_Value (Exp_Hi)), Choice_List);
1140 end if;
1142 Set_Others_Discrete_Choices (Others_Choice, Choice_List);
1144 -- Warn on null others list if warning option set
1146 if Warn_On_Redundant_Constructs
1147 and then Comes_From_Source (Others_Choice)
1148 and then Is_Empty_List (Choice_List)
1149 then
1150 Error_Msg_N ("?r?OTHERS choice is redundant", Others_Choice);
1151 Error_Msg_N ("\?r?previous choices cover all values", Others_Choice);
1152 end if;
1153 end Expand_Others_Choice;
1155 -----------
1156 -- No_OP --
1157 -----------
1159 procedure No_OP (C : Node_Id) is
1160 begin
1161 if Nkind (C) = N_Range and then Warn_On_Redundant_Constructs then
1162 Error_Msg_N ("choice is an empty range?r?", C);
1163 end if;
1164 end No_OP;
1166 -----------------------------
1167 -- Generic_Analyze_Choices --
1168 -----------------------------
1170 package body Generic_Analyze_Choices is
1172 -- The following type is used to gather the entries for the choice
1173 -- table, so that we can then allocate the right length.
1175 type Link;
1176 type Link_Ptr is access all Link;
1178 type Link is record
1179 Val : Choice_Bounds;
1180 Nxt : Link_Ptr;
1181 end record;
1183 ---------------------
1184 -- Analyze_Choices --
1185 ---------------------
1187 procedure Analyze_Choices
1188 (Alternatives : List_Id;
1189 Subtyp : Entity_Id)
1191 Choice_Type : constant Entity_Id := Base_Type (Subtyp);
1192 -- The actual type against which the discrete choices are resolved.
1193 -- Note that this type is always the base type not the subtype of the
1194 -- ruling expression, index or discriminant.
1196 Expected_Type : Entity_Id;
1197 -- The expected type of each choice. Equal to Choice_Type, except if
1198 -- the expression is universal, in which case the choices can be of
1199 -- any integer type.
1201 Alt : Node_Id;
1202 -- A case statement alternative or a variant in a record type
1203 -- declaration.
1205 Choice : Node_Id;
1206 Kind : Node_Kind;
1207 -- The node kind of the current Choice
1209 begin
1210 -- Set Expected type (= choice type except for universal integer,
1211 -- where we accept any integer type as a choice).
1213 if Choice_Type = Universal_Integer then
1214 Expected_Type := Any_Integer;
1215 else
1216 Expected_Type := Choice_Type;
1217 end if;
1219 -- Now loop through the case alternatives or record variants
1221 Alt := First (Alternatives);
1222 while Present (Alt) loop
1224 -- If pragma, just analyze it
1226 if Nkind (Alt) = N_Pragma then
1227 Analyze (Alt);
1229 -- Otherwise we have an alternative. In most cases the semantic
1230 -- processing leaves the list of choices unchanged
1232 -- Check each choice against its base type
1234 else
1235 Choice := First (Discrete_Choices (Alt));
1236 while Present (Choice) loop
1237 Analyze (Choice);
1238 Kind := Nkind (Choice);
1240 -- Choice is a Range
1242 if Kind = N_Range
1243 or else (Kind = N_Attribute_Reference
1244 and then Attribute_Name (Choice) = Name_Range)
1245 then
1246 Resolve (Choice, Expected_Type);
1248 -- Choice is a subtype name, nothing further to do now
1250 elsif Is_Entity_Name (Choice)
1251 and then Is_Type (Entity (Choice))
1252 then
1253 null;
1255 -- Choice is a subtype indication
1257 elsif Kind = N_Subtype_Indication then
1258 Resolve_Discrete_Subtype_Indication
1259 (Choice, Expected_Type);
1261 -- Others choice, no analysis needed
1263 elsif Kind = N_Others_Choice then
1264 null;
1266 -- Only other possibility is an expression
1268 else
1269 Resolve (Choice, Expected_Type);
1270 end if;
1272 -- Move to next choice
1274 Next (Choice);
1275 end loop;
1277 Process_Associated_Node (Alt);
1278 end if;
1280 Next (Alt);
1281 end loop;
1282 end Analyze_Choices;
1284 end Generic_Analyze_Choices;
1286 ---------------------------
1287 -- Generic_Check_Choices --
1288 ---------------------------
1290 package body Generic_Check_Choices is
1292 -- The following type is used to gather the entries for the choice
1293 -- table, so that we can then allocate the right length.
1295 type Link;
1296 type Link_Ptr is access all Link;
1298 type Link is record
1299 Val : Choice_Bounds;
1300 Nxt : Link_Ptr;
1301 end record;
1303 procedure Free is new Ada.Unchecked_Deallocation (Link, Link_Ptr);
1305 -------------------
1306 -- Check_Choices --
1307 -------------------
1309 procedure Check_Choices
1310 (N : Node_Id;
1311 Alternatives : List_Id;
1312 Subtyp : Entity_Id;
1313 Others_Present : out Boolean)
1315 E : Entity_Id;
1317 Raises_CE : Boolean;
1318 -- Set True if one of the bounds of a choice raises CE
1320 Enode : Node_Id;
1321 -- This is where we post error messages for bounds out of range
1323 Choice_List : Link_Ptr := null;
1324 -- Gather list of choices
1326 Num_Choices : Nat := 0;
1327 -- Number of entries in Choice_List
1329 Choice_Type : constant Entity_Id := Base_Type (Subtyp);
1330 -- The actual type against which the discrete choices are resolved.
1331 -- Note that this type is always the base type not the subtype of the
1332 -- ruling expression, index or discriminant.
1334 Bounds_Type : Entity_Id;
1335 -- The type from which are derived the bounds of the values covered
1336 -- by the discrete choices (see 3.8.1 (4)). If a discrete choice
1337 -- specifies a value outside of these bounds we have an error.
1339 Bounds_Lo : Uint;
1340 Bounds_Hi : Uint;
1341 -- The actual bounds of the above type
1343 Expected_Type : Entity_Id;
1344 -- The expected type of each choice. Equal to Choice_Type, except if
1345 -- the expression is universal, in which case the choices can be of
1346 -- any integer type.
1348 Alt : Node_Id;
1349 -- A case statement alternative or a variant in a record type
1350 -- declaration.
1352 Choice : Node_Id;
1353 Kind : Node_Kind;
1354 -- The node kind of the current Choice
1356 Others_Choice : Node_Id := Empty;
1357 -- Remember others choice if it is present (empty otherwise)
1359 procedure Check (Choice : Node_Id; Lo, Hi : Node_Id);
1360 -- Checks the validity of the bounds of a choice. When the bounds
1361 -- are static and no error occurred the bounds are collected for
1362 -- later entry into the choices table so that they can be sorted
1363 -- later on.
1365 -----------
1366 -- Check --
1367 -----------
1369 procedure Check (Choice : Node_Id; Lo, Hi : Node_Id) is
1370 Lo_Val : Uint;
1371 Hi_Val : Uint;
1373 begin
1374 -- First check if an error was already detected on either bounds
1376 if Etype (Lo) = Any_Type or else Etype (Hi) = Any_Type then
1377 return;
1379 -- Do not insert non static choices in the table to be sorted
1381 elsif not Is_OK_Static_Expression (Lo)
1382 or else
1383 not Is_OK_Static_Expression (Hi)
1384 then
1385 Process_Non_Static_Choice (Choice);
1386 return;
1388 -- Ignore range which raise constraint error
1390 elsif Raises_Constraint_Error (Lo)
1391 or else Raises_Constraint_Error (Hi)
1392 then
1393 Raises_CE := True;
1394 return;
1396 -- AI05-0188 : Within an instance the non-others choices do not
1397 -- have to belong to the actual subtype.
1399 elsif Ada_Version >= Ada_2012 and then In_Instance then
1400 return;
1402 -- Otherwise we have an OK static choice
1404 else
1405 Lo_Val := Expr_Value (Lo);
1406 Hi_Val := Expr_Value (Hi);
1408 -- Do not insert null ranges in the choices table
1410 if Lo_Val > Hi_Val then
1411 Process_Empty_Choice (Choice);
1412 return;
1413 end if;
1414 end if;
1416 -- Check for low bound out of range
1418 if Lo_Val < Bounds_Lo then
1420 -- If the choice is an entity name, then it is a type, and we
1421 -- want to post the message on the reference to this entity.
1422 -- Otherwise post it on the lower bound of the range.
1424 if Is_Entity_Name (Choice) then
1425 Enode := Choice;
1426 else
1427 Enode := Lo;
1428 end if;
1430 -- Specialize message for integer/enum type
1432 if Is_Integer_Type (Bounds_Type) then
1433 Error_Msg_Uint_1 := Bounds_Lo;
1434 Error_Msg_N ("minimum allowed choice value is^", Enode);
1435 else
1436 Error_Msg_Name_1 := Choice_Image (Bounds_Lo, Bounds_Type);
1437 Error_Msg_N ("minimum allowed choice value is%", Enode);
1438 end if;
1439 end if;
1441 -- Check for high bound out of range
1443 if Hi_Val > Bounds_Hi then
1445 -- If the choice is an entity name, then it is a type, and we
1446 -- want to post the message on the reference to this entity.
1447 -- Otherwise post it on the upper bound of the range.
1449 if Is_Entity_Name (Choice) then
1450 Enode := Choice;
1451 else
1452 Enode := Hi;
1453 end if;
1455 -- Specialize message for integer/enum type
1457 if Is_Integer_Type (Bounds_Type) then
1458 Error_Msg_Uint_1 := Bounds_Hi;
1459 Error_Msg_N ("maximum allowed choice value is^", Enode);
1460 else
1461 Error_Msg_Name_1 := Choice_Image (Bounds_Hi, Bounds_Type);
1462 Error_Msg_N ("maximum allowed choice value is%", Enode);
1463 end if;
1464 end if;
1466 -- Collect bounds in the list
1468 -- Note: we still store the bounds, even if they are out of range,
1469 -- since this may prevent unnecessary cascaded errors for values
1470 -- that are covered by such an excessive range.
1472 Choice_List :=
1473 new Link'(Val => (Lo, Hi, Choice), Nxt => Choice_List);
1474 Num_Choices := Num_Choices + 1;
1475 end Check;
1477 -- Start of processing for Check_Choices
1479 begin
1480 Raises_CE := False;
1481 Others_Present := False;
1483 -- If Subtyp is not a discrete type or there was some other error,
1484 -- then don't try any semantic checking on the choices since we have
1485 -- a complete mess.
1487 if not Is_Discrete_Type (Subtyp) or else Subtyp = Any_Type then
1488 return;
1489 end if;
1491 -- If Subtyp is not a static subtype Ada 95 requires then we use the
1492 -- bounds of its base type to determine the values covered by the
1493 -- discrete choices.
1495 -- In Ada 2012, if the subtype has a non-static predicate the full
1496 -- range of the base type must be covered as well.
1498 if Is_OK_Static_Subtype (Subtyp) then
1499 if not Has_Predicates (Subtyp)
1500 or else Has_Static_Predicate (Subtyp)
1501 then
1502 Bounds_Type := Subtyp;
1503 else
1504 Bounds_Type := Choice_Type;
1505 end if;
1507 else
1508 Bounds_Type := Choice_Type;
1509 end if;
1511 -- Obtain static bounds of type, unless this is a generic formal
1512 -- discrete type for which all choices will be non-static.
1514 if not Is_Generic_Type (Root_Type (Bounds_Type))
1515 or else Ekind (Bounds_Type) /= E_Enumeration_Type
1516 then
1517 Bounds_Lo := Expr_Value (Type_Low_Bound (Bounds_Type));
1518 Bounds_Hi := Expr_Value (Type_High_Bound (Bounds_Type));
1519 end if;
1521 if Choice_Type = Universal_Integer then
1522 Expected_Type := Any_Integer;
1523 else
1524 Expected_Type := Choice_Type;
1525 end if;
1527 -- Now loop through the case alternatives or record variants
1529 Alt := First (Alternatives);
1530 while Present (Alt) loop
1532 -- If pragma, just analyze it
1534 if Nkind (Alt) = N_Pragma then
1535 Analyze (Alt);
1537 -- Otherwise we have an alternative. In most cases the semantic
1538 -- processing leaves the list of choices unchanged
1540 -- Check each choice against its base type
1542 else
1543 Choice := First (Discrete_Choices (Alt));
1544 while Present (Choice) loop
1545 Kind := Nkind (Choice);
1547 -- Choice is a Range
1549 if Kind = N_Range
1550 or else (Kind = N_Attribute_Reference
1551 and then Attribute_Name (Choice) = Name_Range)
1552 then
1553 Check (Choice, Low_Bound (Choice), High_Bound (Choice));
1555 -- Choice is a subtype name
1557 elsif Is_Entity_Name (Choice)
1558 and then Is_Type (Entity (Choice))
1559 then
1560 -- Check for inappropriate type
1562 if not Covers (Expected_Type, Etype (Choice)) then
1563 Wrong_Type (Choice, Choice_Type);
1565 -- Type is OK, so check further
1567 else
1568 E := Entity (Choice);
1570 -- Case of predicated subtype
1572 if Has_Predicates (E) then
1574 -- Use of non-static predicate is an error
1576 if not Is_Discrete_Type (E)
1577 or else not Has_Static_Predicate (E)
1578 or else Has_Dynamic_Predicate_Aspect (E)
1579 then
1580 Bad_Predicated_Subtype_Use
1581 ("cannot use subtype& with non-static "
1582 & "predicate as case alternative",
1583 Choice, E, Suggest_Static => True);
1585 -- Static predicate case
1587 else
1588 declare
1589 P : Node_Id;
1590 C : Node_Id;
1592 begin
1593 -- Loop through entries in predicate list,
1594 -- checking each entry. Note that if the
1595 -- list is empty, corresponding to a False
1596 -- predicate, then no choices are checked.
1598 P := First (Static_Discrete_Predicate (E));
1599 while Present (P) loop
1600 C := New_Copy (P);
1601 Set_Sloc (C, Sloc (Choice));
1602 Check (C, Low_Bound (C), High_Bound (C));
1603 Next (P);
1604 end loop;
1605 end;
1607 Set_Has_SP_Choice (Alt);
1608 end if;
1610 -- Not predicated subtype case
1612 elsif not Is_OK_Static_Subtype (E) then
1613 Process_Non_Static_Choice (Choice);
1614 else
1615 Check
1616 (Choice, Type_Low_Bound (E), Type_High_Bound (E));
1617 end if;
1618 end if;
1620 -- Choice is a subtype indication
1622 elsif Kind = N_Subtype_Indication then
1623 Resolve_Discrete_Subtype_Indication
1624 (Choice, Expected_Type);
1626 if Etype (Choice) /= Any_Type then
1627 declare
1628 C : constant Node_Id := Constraint (Choice);
1629 R : constant Node_Id := Range_Expression (C);
1630 L : constant Node_Id := Low_Bound (R);
1631 H : constant Node_Id := High_Bound (R);
1633 begin
1634 E := Entity (Subtype_Mark (Choice));
1636 if not Is_OK_Static_Subtype (E) then
1637 Process_Non_Static_Choice (Choice);
1639 else
1640 if Is_OK_Static_Expression (L)
1641 and then
1642 Is_OK_Static_Expression (H)
1643 then
1644 if Expr_Value (L) > Expr_Value (H) then
1645 Process_Empty_Choice (Choice);
1646 else
1647 if Is_Out_Of_Range (L, E) then
1648 Apply_Compile_Time_Constraint_Error
1649 (L, "static value out of range",
1650 CE_Range_Check_Failed);
1651 end if;
1653 if Is_Out_Of_Range (H, E) then
1654 Apply_Compile_Time_Constraint_Error
1655 (H, "static value out of range",
1656 CE_Range_Check_Failed);
1657 end if;
1658 end if;
1659 end if;
1661 Check (Choice, L, H);
1662 end if;
1663 end;
1664 end if;
1666 -- The others choice is only allowed for the last
1667 -- alternative and as its only choice.
1669 elsif Kind = N_Others_Choice then
1670 if not (Choice = First (Discrete_Choices (Alt))
1671 and then Choice = Last (Discrete_Choices (Alt))
1672 and then Alt = Last (Alternatives))
1673 then
1674 Error_Msg_N
1675 ("the choice OTHERS must appear alone and last",
1676 Choice);
1677 return;
1678 end if;
1680 Others_Present := True;
1681 Others_Choice := Choice;
1683 -- Only other possibility is an expression
1685 else
1686 Check (Choice, Choice, Choice);
1687 end if;
1689 -- Move to next choice
1691 Next (Choice);
1692 end loop;
1694 Process_Associated_Node (Alt);
1695 end if;
1697 Next (Alt);
1698 end loop;
1700 -- Now we can create the Choice_Table, since we know how long
1701 -- it needs to be so we can allocate exactly the right length.
1703 declare
1704 Choice_Table : Choice_Table_Type (0 .. Num_Choices);
1706 begin
1707 -- Now copy the items we collected in the linked list into this
1708 -- newly allocated table (leave entry 0 unused for sorting).
1710 declare
1711 T : Link_Ptr;
1712 begin
1713 for J in 1 .. Num_Choices loop
1714 T := Choice_List;
1715 Choice_List := T.Nxt;
1716 Choice_Table (J) := T.Val;
1717 Free (T);
1718 end loop;
1719 end;
1721 Check_Choice_Set
1722 (Choice_Table,
1723 Bounds_Type,
1724 Subtyp,
1725 Others_Present or else (Choice_Type = Universal_Integer),
1728 -- If no others choice we are all done, otherwise we have one more
1729 -- step, which is to set the Others_Discrete_Choices field of the
1730 -- others choice (to contain all otherwise unspecified choices).
1731 -- Skip this if CE is known to be raised.
1733 if Others_Present and not Raises_CE then
1734 Expand_Others_Choice
1735 (Case_Table => Choice_Table,
1736 Others_Choice => Others_Choice,
1737 Choice_Type => Bounds_Type);
1738 end if;
1739 end;
1740 end Check_Choices;
1742 end Generic_Check_Choices;
1744 end Sem_Case;