1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2014, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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
;
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
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
;
72 Others_Present
: Boolean;
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
;
114 Others_Present
: Boolean;
117 Predicate_Error
: Boolean;
118 -- Flag to prevent cascaded errors when a static predicate is known to
119 -- be violated by one choice.
121 procedure Check_Against_Predicate
122 (Pred
: in out Node_Id
;
123 Choice
: Choice_Bounds
;
124 Prev_Lo
: in out Uint
;
125 Prev_Hi
: in out Uint
;
126 Error
: in out Boolean);
127 -- Determine whether a choice covers legal values as defined by a static
128 -- predicate set. Pred is a static predicate range. Choice is the choice
129 -- to be examined. Prev_Lo and Prev_Hi are the bounds of the previous
130 -- choice that covered a predicate set. Error denotes whether the check
131 -- found an illegal intersection.
133 procedure Dup_Choice
(Lo
, Hi
: Uint
; C
: Node_Id
);
134 -- Post message "duplication of choice value(s) bla bla at xx". Message
135 -- is posted at location C. Caller sets Error_Msg_Sloc for xx.
137 procedure Explain_Non_Static_Bound
;
138 -- Called when we find a non-static bound, requiring the base type to
139 -- be covered. Provides where possible a helpful explanation of why the
140 -- bounds are non-static, since this is not always obvious.
142 function Lt_Choice
(C1
, C2
: Natural) return Boolean;
143 -- Comparison routine for comparing Choice_Table entries. Use the lower
144 -- bound of each Choice as the key.
146 procedure Missing_Choice
(Value1
: Node_Id
; Value2
: Node_Id
);
147 procedure Missing_Choice
(Value1
: Node_Id
; Value2
: Uint
);
148 procedure Missing_Choice
(Value1
: Uint
; Value2
: Node_Id
);
149 procedure Missing_Choice
(Value1
: Uint
; Value2
: Uint
);
150 -- Issue an error message indicating that there are missing choices,
151 -- followed by the image of the missing choices themselves which lie
152 -- between Value1 and Value2 inclusive.
154 procedure Missing_Choices
(Pred
: Node_Id
; Prev_Hi
: Uint
);
155 -- Emit an error message for each non-covered static predicate set.
156 -- Prev_Hi denotes the upper bound of the last choice covering a set.
158 procedure Move_Choice
(From
: Natural; To
: Natural);
159 -- Move routine for sorting the Choice_Table
161 package Sorting
is new GNAT
.Heap_Sort_G
(Move_Choice
, Lt_Choice
);
163 -----------------------------
164 -- Check_Against_Predicate --
165 -----------------------------
167 procedure Check_Against_Predicate
168 (Pred
: in out Node_Id
;
169 Choice
: Choice_Bounds
;
170 Prev_Lo
: in out Uint
;
171 Prev_Hi
: in out Uint
;
172 Error
: in out Boolean)
174 procedure Illegal_Range
178 -- Emit an error message regarding a choice that clashes with the
179 -- legal static predicate sets. Loc is the location of the choice
180 -- that introduced the illegal range. Lo .. Hi is the range.
182 function Inside_Range
185 Val
: Uint
) return Boolean;
186 -- Determine whether position Val within a discrete type is within
187 -- the range Lo .. Hi inclusive.
193 procedure Illegal_Range
199 Error_Msg_Name_1
:= Chars
(Bounds_Type
);
204 if Is_Integer_Type
(Bounds_Type
) then
205 Error_Msg_Uint_1
:= Lo
;
206 Error_Msg
("static predicate on % excludes value ^!", Loc
);
208 Error_Msg_Name_2
:= Choice_Image
(Lo
, Bounds_Type
);
209 Error_Msg
("static predicate on % excludes value %!", Loc
);
215 if Is_Integer_Type
(Bounds_Type
) then
216 Error_Msg_Uint_1
:= Lo
;
217 Error_Msg_Uint_2
:= Hi
;
219 ("static predicate on % excludes range ^ .. ^!", Loc
);
221 Error_Msg_Name_2
:= Choice_Image
(Lo
, Bounds_Type
);
222 Error_Msg_Name_3
:= Choice_Image
(Hi
, Bounds_Type
);
224 ("static predicate on % excludes range % .. %!", Loc
);
233 function Inside_Range
236 Val
: Uint
) return Boolean
240 Val
= Lo
or else Val
= Hi
or else (Lo
< Val
and then Val
< Hi
);
245 Choice_Hi
: constant Uint
:= Expr_Value
(Choice
.Hi
);
246 Choice_Lo
: constant Uint
:= Expr_Value
(Choice
.Lo
);
254 -- Start of processing for Check_Against_Predicate
257 -- Find the proper error message location
259 if Present
(Choice
.Node
) then
267 if Present
(Pred
) then
268 Pred_Lo
:= Expr_Value
(Low_Bound
(Pred
));
269 Pred_Hi
:= Expr_Value
(High_Bound
(Pred
));
271 -- Previous choices managed to satisfy all static predicate sets
274 Illegal_Range
(Loc
, Choice_Lo
, Choice_Hi
);
279 -- Step 1: Detect duplicate choices
281 if Inside_Range
(Choice_Lo
, Choice_Hi
, Prev_Lo
) then
282 Dup_Choice
(Prev_Lo
, UI_Min
(Prev_Hi
, Choice_Hi
), LocN
);
285 elsif Inside_Range
(Choice_Lo
, Choice_Hi
, Prev_Hi
) then
286 Dup_Choice
(UI_Max
(Choice_Lo
, Prev_Lo
), Prev_Hi
, LocN
);
289 -- Step 2: Detect full coverage
291 -- Choice_Lo Choice_Hi
295 elsif Choice_Lo
= Pred_Lo
and then Choice_Hi
= Pred_Hi
then
296 Prev_Lo
:= Choice_Lo
;
297 Prev_Hi
:= Choice_Hi
;
300 -- Step 3: Detect all cases where a choice mentions values that are
301 -- not part of the static predicate sets.
303 -- Choice_Lo Choice_Hi Pred_Lo Pred_Hi
304 -- +-----------+ . . . . . +=========+
307 elsif Choice_Lo
< Pred_Lo
and then Choice_Hi
< Pred_Lo
then
308 Illegal_Range
(Loc
, Choice_Lo
, Choice_Hi
);
311 -- Choice_Lo Pred_Lo Choice_Hi Pred_Hi
312 -- +-----------+=========+===========+
315 elsif Choice_Lo
< Pred_Lo
316 and then Inside_Range
(Pred_Lo
, Pred_Hi
, Choice_Hi
)
318 Illegal_Range
(Loc
, Choice_Lo
, Pred_Lo
- 1);
321 -- Pred_Lo Pred_Hi Choice_Lo Choice_Hi
322 -- +=========+ . . . . +-----------+
325 elsif Pred_Lo
< Choice_Lo
and then Pred_Hi
< Choice_Lo
then
326 if Others_Present
then
328 -- Current predicate set is covered by others clause.
333 Missing_Choice
(Pred_Lo
, Pred_Hi
);
337 -- There may be several static predicate sets between the current
338 -- one and the choice. Inspect the next static predicate set.
341 Check_Against_Predicate
348 -- Pred_Lo Choice_Lo Pred_Hi Choice_Hi
349 -- +=========+===========+-----------+
352 elsif Pred_Hi
< Choice_Hi
353 and then Inside_Range
(Pred_Lo
, Pred_Hi
, Choice_Lo
)
357 -- The choice may fall in a static predicate set. If this is the
358 -- case, avoid mentioning legal values in the error message.
360 if Present
(Pred
) then
361 Next_Lo
:= Expr_Value
(Low_Bound
(Pred
));
362 Next_Hi
:= Expr_Value
(High_Bound
(Pred
));
364 -- The next static predicate set is to the right of the choice
366 if Choice_Hi
< Next_Lo
and then Choice_Hi
< Next_Hi
then
367 Illegal_Range
(Loc
, Pred_Hi
+ 1, Choice_Hi
);
369 Illegal_Range
(Loc
, Pred_Hi
+ 1, Next_Lo
- 1);
372 Illegal_Range
(Loc
, Pred_Hi
+ 1, Choice_Hi
);
377 -- Choice_Lo Pred_Lo Pred_Hi Choice_Hi
378 -- +-----------+=========+-----------+
379 -- ^ illegal ^ ^ illegal ^
381 -- Emit an error on the low gap, disregard the upper gap
383 elsif Choice_Lo
< Pred_Lo
and then Pred_Hi
< Choice_Hi
then
384 Illegal_Range
(Loc
, Choice_Lo
, Pred_Lo
- 1);
387 -- Step 4: Detect all cases of partial or missing coverage
389 -- Pred_Lo Choice_Lo Choice_Hi Pred_Hi
390 -- +=========+==========+===========+
394 -- An "others" choice covers all gaps
396 if Others_Present
then
397 Prev_Lo
:= Choice_Lo
;
398 Prev_Hi
:= Choice_Hi
;
400 -- Check whether predicate set is fully covered by choice
402 if Pred_Hi
= Choice_Hi
then
406 -- Choice_Lo Choice_Hi Pred_Hi
407 -- +===========+===========+
410 -- The upper gap may be covered by a subsequent choice
412 elsif Pred_Lo
= Choice_Lo
then
413 Prev_Lo
:= Choice_Lo
;
414 Prev_Hi
:= Choice_Hi
;
416 -- Pred_Lo Prev_Hi Choice_Lo Choice_Hi Pred_Hi
417 -- +===========+=========+===========+===========+
418 -- ^ covered ^ ^ gap ^
420 else pragma Assert
(Pred_Lo
< Choice_Lo
);
422 -- A previous choice covered the gap up to the current choice
424 if Prev_Hi
= Choice_Lo
- 1 then
425 Prev_Lo
:= Choice_Lo
;
426 Prev_Hi
:= Choice_Hi
;
428 if Choice_Hi
= Pred_Hi
then
432 -- The previous choice did not intersect with the current
433 -- static predicate set.
435 elsif Prev_Hi
< Pred_Lo
then
436 Missing_Choice
(Pred_Lo
, Choice_Lo
- 1);
439 -- The previous choice covered part of the static predicate set
440 -- but there is a gap after Prev_Hi.
443 Missing_Choice
(Prev_Hi
+ 1, Choice_Lo
- 1);
448 end Check_Against_Predicate
;
454 procedure Dup_Choice
(Lo
, Hi
: Uint
; C
: Node_Id
) is
456 -- In some situations, we call this with a null range, and obviously
457 -- we don't want to complain in this case.
463 -- Case of only one value that is duplicated
469 if Is_Integer_Type
(Bounds_Type
) then
471 -- We have an integer value, Lo, but if the given choice
472 -- placement is a constant with that value, then use the
473 -- name of that constant instead in the message:
475 if Nkind
(C
) = N_Identifier
476 and then Compile_Time_Known_Value
(C
)
477 and then Expr_Value
(C
) = Lo
479 Error_Msg_N
("duplication of choice value: &#!", C
);
481 -- Not that special case, so just output the integer value
484 Error_Msg_Uint_1
:= Lo
;
485 Error_Msg_N
("duplication of choice value: ^#!", C
);
491 Error_Msg_Name_1
:= Choice_Image
(Lo
, Bounds_Type
);
492 Error_Msg_N
("duplication of choice value: %#!", C
);
495 -- More than one choice value, so print range of values
500 if Is_Integer_Type
(Bounds_Type
) then
502 -- Similar to the above, if C is a range of known values which
503 -- match Lo and Hi, then use the names. We have to go to the
504 -- original nodes, since the values will have been rewritten
505 -- to their integer values.
507 if Nkind
(C
) = N_Range
508 and then Nkind
(Original_Node
(Low_Bound
(C
))) = N_Identifier
509 and then Nkind
(Original_Node
(High_Bound
(C
))) = N_Identifier
510 and then Compile_Time_Known_Value
(Low_Bound
(C
))
511 and then Compile_Time_Known_Value
(High_Bound
(C
))
512 and then Expr_Value
(Low_Bound
(C
)) = Lo
513 and then Expr_Value
(High_Bound
(C
)) = Hi
515 Error_Msg_Node_2
:= Original_Node
(High_Bound
(C
));
517 ("duplication of choice values: & .. &#!",
518 Original_Node
(Low_Bound
(C
)));
520 -- Not that special case, output integer values
523 Error_Msg_Uint_1
:= Lo
;
524 Error_Msg_Uint_2
:= Hi
;
525 Error_Msg_N
("duplication of choice values: ^ .. ^#!", C
);
531 Error_Msg_Name_1
:= Choice_Image
(Lo
, Bounds_Type
);
532 Error_Msg_Name_2
:= Choice_Image
(Hi
, Bounds_Type
);
533 Error_Msg_N
("duplication of choice values: % .. %#!", C
);
538 ------------------------------
539 -- Explain_Non_Static_Bound --
540 ------------------------------
542 procedure Explain_Non_Static_Bound
is
546 if Nkind
(Case_Node
) = N_Variant_Part
then
547 Expr
:= Name
(Case_Node
);
549 Expr
:= Expression
(Case_Node
);
552 if Bounds_Type
/= Subtyp
then
554 -- If the case is a variant part, the expression is given by the
555 -- discriminant itself, and the bounds are the culprits.
557 if Nkind
(Case_Node
) = N_Variant_Part
then
559 ("bounds of & are not static, "
560 & "alternatives must cover base type!", Expr
, Expr
);
562 -- If this is a case statement, the expression may be non-static
563 -- or else the subtype may be at fault.
565 elsif Is_Entity_Name
(Expr
) then
567 ("bounds of & are not static, "
568 & "alternatives must cover base type!", Expr
, Expr
);
572 ("subtype of expression is not static, "
573 & "alternatives must cover base type!", Expr
);
576 -- Otherwise the expression is not static, even if the bounds of the
577 -- type are, or else there are missing alternatives. If both, the
578 -- additional information may be redundant but harmless.
580 elsif not Is_Entity_Name
(Expr
) then
582 ("subtype of expression is not static, "
583 & "alternatives must cover base type!", Expr
);
585 end Explain_Non_Static_Bound
;
591 function Lt_Choice
(C1
, C2
: Natural) return Boolean is
594 Expr_Value
(Choice_Table
(Nat
(C1
)).Lo
)
596 Expr_Value
(Choice_Table
(Nat
(C2
)).Lo
);
603 procedure Missing_Choice
(Value1
: Node_Id
; Value2
: Node_Id
) is
605 Missing_Choice
(Expr_Value
(Value1
), Expr_Value
(Value2
));
608 procedure Missing_Choice
(Value1
: Node_Id
; Value2
: Uint
) is
610 Missing_Choice
(Expr_Value
(Value1
), Value2
);
613 procedure Missing_Choice
(Value1
: Uint
; Value2
: Node_Id
) is
615 Missing_Choice
(Value1
, Expr_Value
(Value2
));
622 procedure Missing_Choice
(Value1
: Uint
; Value2
: Uint
) is
623 Msg_Sloc
: constant Source_Ptr
:= Sloc
(Case_Node
);
626 -- AI05-0188 : within an instance the non-others choices do not have
627 -- to belong to the actual subtype.
629 if Ada_Version
>= Ada_2012
and then In_Instance
then
632 -- In some situations, we call this with a null range, and obviously
633 -- we don't want to complain in this case.
635 elsif Value1
> Value2
then
638 -- If predicate is already known to be violated, do no check for
639 -- coverage error, to prevent cascaded messages.
641 elsif Predicate_Error
then
645 -- Case of only one value that is missing
647 if Value1
= Value2
then
648 if Is_Integer_Type
(Bounds_Type
) then
649 Error_Msg_Uint_1
:= Value1
;
650 Error_Msg
("missing case value: ^!", Msg_Sloc
);
652 Error_Msg_Name_1
:= Choice_Image
(Value1
, Bounds_Type
);
653 Error_Msg
("missing case value: %!", Msg_Sloc
);
656 -- More than one choice value, so print range of values
659 if Is_Integer_Type
(Bounds_Type
) then
660 Error_Msg_Uint_1
:= Value1
;
661 Error_Msg_Uint_2
:= Value2
;
662 Error_Msg
("missing case values: ^ .. ^!", Msg_Sloc
);
664 Error_Msg_Name_1
:= Choice_Image
(Value1
, Bounds_Type
);
665 Error_Msg_Name_2
:= Choice_Image
(Value2
, Bounds_Type
);
666 Error_Msg
("missing case values: % .. %!", Msg_Sloc
);
671 ---------------------
672 -- Missing_Choices --
673 ---------------------
675 procedure Missing_Choices
(Pred
: Node_Id
; Prev_Hi
: Uint
) is
682 while Present
(Set
) loop
683 Lo
:= Expr_Value
(Low_Bound
(Set
));
684 Hi
:= Expr_Value
(High_Bound
(Set
));
686 -- A choice covered part of a static predicate set
688 if Lo
<= Prev_Hi
and then Prev_Hi
< Hi
then
689 Missing_Choice
(Prev_Hi
+ 1, Hi
);
692 Missing_Choice
(Lo
, Hi
);
703 procedure Move_Choice
(From
: Natural; To
: Natural) is
705 Choice_Table
(Nat
(To
)) := Choice_Table
(Nat
(From
));
710 Bounds_Hi
: constant Node_Id
:= Type_High_Bound
(Bounds_Type
);
711 Bounds_Lo
: constant Node_Id
:= Type_Low_Bound
(Bounds_Type
);
712 Num_Choices
: constant Nat
:= Choice_Table
'Last;
713 Has_Predicate
: constant Boolean :=
714 Is_OK_Static_Subtype
(Bounds_Type
)
715 and then Has_Static_Predicate
(Bounds_Type
);
722 Prev_Choice
: Node_Id
;
726 -- Start of processing for Check_Choice_Set
729 -- If the case is part of a predicate aspect specification, do not
730 -- recheck it against itself.
732 if Present
(Parent
(Case_Node
))
733 and then Nkind
(Parent
(Case_Node
)) = N_Aspect_Specification
738 Predicate_Error
:= False;
740 -- Choice_Table must start at 0 which is an unused location used by the
741 -- sorting algorithm. However the first valid position for a discrete
744 pragma Assert
(Choice_Table
'First = 0);
746 -- The choices do not cover the base range. Emit an error if "others" is
747 -- not available and return as there is no need for further processing.
749 if Num_Choices
= 0 then
750 if not Others_Present
then
751 Missing_Choice
(Bounds_Lo
, Bounds_Hi
);
757 Sorting
.Sort
(Positive (Choice_Table
'Last));
759 -- The type covered by the list of choices is actually a static subtype
760 -- subject to a static predicate. The predicate defines subsets of legal
761 -- values and requires finer grained analysis.
763 -- Note that in GNAT the predicate is considered static if the predicate
764 -- expression is static, independently of whether the aspect mentions
765 -- Static explicitly.
767 if Has_Predicate
then
768 Pred
:= First
(Static_Discrete_Predicate
(Bounds_Type
));
769 Prev_Lo
:= Uint_Minus_1
;
770 Prev_Hi
:= Uint_Minus_1
;
773 for Index
in 1 .. Num_Choices
loop
774 Check_Against_Predicate
776 Choice
=> Choice_Table
(Index
),
781 -- The analysis detected an illegal intersection between a choice
782 -- and a static predicate set. Do not examine other choices unless
783 -- all errors are requested.
786 Predicate_Error
:= True;
788 if not All_Errors_Mode
then
794 if Predicate_Error
then
798 -- The choices may legally cover some of the static predicate sets,
799 -- but not all. Emit an error for each non-covered set.
801 if not Others_Present
then
802 Missing_Choices
(Pred
, Prev_Hi
);
808 Choice_Lo
:= Expr_Value
(Choice_Table
(1).Lo
);
809 Choice_Hi
:= Expr_Value
(Choice_Table
(1).Hi
);
810 Prev_Hi
:= Choice_Hi
;
812 if not Others_Present
and then Expr_Value
(Bounds_Lo
) < Choice_Lo
then
813 Missing_Choice
(Bounds_Lo
, Choice_Lo
- 1);
815 -- If values are missing outside of the subtype, add explanation.
816 -- No additional message if only one value is missing.
818 if Expr_Value
(Bounds_Lo
) < Choice_Lo
- 1 then
819 Explain_Non_Static_Bound
;
823 for Outer_Index
in 2 .. Num_Choices
loop
824 Choice_Lo
:= Expr_Value
(Choice_Table
(Outer_Index
).Lo
);
825 Choice_Hi
:= Expr_Value
(Choice_Table
(Outer_Index
).Hi
);
827 if Choice_Lo
<= Prev_Hi
then
828 Choice
:= Choice_Table
(Outer_Index
).Node
;
830 -- Find first previous choice that overlaps
832 for Inner_Index
in 1 .. Outer_Index
- 1 loop
834 Expr_Value
(Choice_Table
(Inner_Index
).Hi
)
836 Prev_Choice
:= Choice_Table
(Inner_Index
).Node
;
841 if Sloc
(Prev_Choice
) <= Sloc
(Choice
) then
842 Error_Msg_Sloc
:= Sloc
(Prev_Choice
);
844 (Choice_Lo
, UI_Min
(Choice_Hi
, Prev_Hi
), Choice
);
846 Error_Msg_Sloc
:= Sloc
(Choice
);
848 (Choice_Lo
, UI_Min
(Choice_Hi
, Prev_Hi
), Prev_Choice
);
851 elsif not Others_Present
and then Choice_Lo
/= Prev_Hi
+ 1 then
852 Missing_Choice
(Prev_Hi
+ 1, Choice_Lo
- 1);
855 if Choice_Hi
> Prev_Hi
then
856 Prev_Hi
:= Choice_Hi
;
860 if not Others_Present
and then Expr_Value
(Bounds_Hi
) > Prev_Hi
then
861 Missing_Choice
(Prev_Hi
+ 1, Bounds_Hi
);
863 if Expr_Value
(Bounds_Hi
) > Prev_Hi
+ 1 then
864 Explain_Non_Static_Bound
;
868 end Check_Choice_Set
;
874 function Choice_Image
(Value
: Uint
; Ctype
: Entity_Id
) return Name_Id
is
875 Rtp
: constant Entity_Id
:= Root_Type
(Ctype
);
880 -- For character, or wide [wide] character. If 7-bit ASCII graphic
881 -- range, then build and return appropriate character literal name
883 if Is_Standard_Character_Type
(Ctype
) then
884 C
:= UI_To_Int
(Value
);
886 if C
in 16#
20#
.. 16#
7E#
then
887 Set_Character_Literal_Name
(Char_Code
(UI_To_Int
(Value
)));
891 -- For user defined enumeration type, find enum/char literal
894 Lit
:= First_Literal
(Rtp
);
896 for J
in 1 .. UI_To_Int
(Value
) loop
900 -- If enumeration literal, just return its value
902 if Nkind
(Lit
) = N_Defining_Identifier
then
905 -- For character literal, get the name and use it if it is
906 -- for a 7-bit ASCII graphic character in 16#20#..16#7E#.
909 Get_Decoded_Name_String
(Chars
(Lit
));
912 and then Name_Buffer
(2) in
913 Character'Val (16#
20#
) .. Character'Val (16#
7E#
)
920 -- If we fall through, we have a character literal which is not in
921 -- the 7-bit ASCII graphic set. For such cases, we construct the
922 -- name "type'val(nnn)" where type is the choice type, and nnn is
923 -- the pos value passed as an argument to Choice_Image.
925 Get_Name_String
(Chars
(First_Subtype
(Ctype
)));
927 Add_Str_To_Name_Buffer
("'val(");
929 Add_Str_To_Name_Buffer
(UI_Image_Buffer
(1 .. UI_Image_Length
));
930 Add_Char_To_Name_Buffer
(')');
934 --------------------------
935 -- Expand_Others_Choice --
936 --------------------------
938 procedure Expand_Others_Choice
939 (Case_Table
: Choice_Table_Type
;
940 Others_Choice
: Node_Id
;
941 Choice_Type
: Entity_Id
)
943 Loc
: constant Source_Ptr
:= Sloc
(Others_Choice
);
944 Choice_List
: constant List_Id
:= New_List
;
952 function Build_Choice
(Value1
, Value2
: Uint
) return Node_Id
;
953 -- Builds a node representing the missing choices given by Value1 and
954 -- Value2. A N_Range node is built if there is more than one literal
955 -- value missing. Otherwise a single N_Integer_Literal, N_Identifier
956 -- or N_Character_Literal is built depending on what Choice_Type is.
958 function Lit_Of
(Value
: Uint
) return Node_Id
;
959 -- Returns the Node_Id for the enumeration literal corresponding to the
960 -- position given by Value within the enumeration type Choice_Type.
966 function Build_Choice
(Value1
, Value2
: Uint
) return Node_Id
is
971 -- If there is only one choice value missing between Value1 and
972 -- Value2, build an integer or enumeration literal to represent it.
974 if (Value2
- Value1
) = 0 then
975 if Is_Integer_Type
(Choice_Type
) then
976 Lit_Node
:= Make_Integer_Literal
(Loc
, Value1
);
977 Set_Etype
(Lit_Node
, Choice_Type
);
979 Lit_Node
:= Lit_Of
(Value1
);
982 -- Otherwise is more that one choice value that is missing between
983 -- Value1 and Value2, therefore build a N_Range node of either
984 -- integer or enumeration literals.
987 if Is_Integer_Type
(Choice_Type
) then
988 Lo
:= Make_Integer_Literal
(Loc
, Value1
);
989 Set_Etype
(Lo
, Choice_Type
);
990 Hi
:= Make_Integer_Literal
(Loc
, Value2
);
991 Set_Etype
(Hi
, Choice_Type
);
1000 Low_Bound
=> Lit_Of
(Value1
),
1001 High_Bound
=> Lit_Of
(Value2
));
1012 function Lit_Of
(Value
: Uint
) return Node_Id
is
1016 -- In the case where the literal is of type Character, there needs
1017 -- to be some special handling since there is no explicit chain
1018 -- of literals to search. Instead, a N_Character_Literal node
1019 -- is created with the appropriate Char_Code and Chars fields.
1021 if Is_Standard_Character_Type
(Choice_Type
) then
1022 Set_Character_Literal_Name
(Char_Code
(UI_To_Int
(Value
)));
1023 Lit
:= New_Node
(N_Character_Literal
, Loc
);
1024 Set_Chars
(Lit
, Name_Find
);
1025 Set_Char_Literal_Value
(Lit
, Value
);
1026 Set_Etype
(Lit
, Choice_Type
);
1027 Set_Is_Static_Expression
(Lit
, True);
1030 -- Otherwise, iterate through the literals list of Choice_Type
1031 -- "Value" number of times until the desired literal is reached
1032 -- and then return an occurrence of it.
1035 Lit
:= First_Literal
(Choice_Type
);
1036 for J
in 1 .. UI_To_Int
(Value
) loop
1040 return New_Occurrence_Of
(Lit
, Loc
);
1044 -- Start of processing for Expand_Others_Choice
1047 if Case_Table
'Last = 0 then
1049 -- Special case: only an others case is present. The others case
1050 -- covers the full range of the type.
1052 if Is_OK_Static_Subtype
(Choice_Type
) then
1053 Choice
:= New_Occurrence_Of
(Choice_Type
, Loc
);
1055 Choice
:= New_Occurrence_Of
(Base_Type
(Choice_Type
), Loc
);
1058 Set_Others_Discrete_Choices
(Others_Choice
, New_List
(Choice
));
1062 -- Establish the bound values for the choice depending upon whether the
1063 -- type of the case statement is static or not.
1065 if Is_OK_Static_Subtype
(Choice_Type
) then
1066 Exp_Lo
:= Type_Low_Bound
(Choice_Type
);
1067 Exp_Hi
:= Type_High_Bound
(Choice_Type
);
1069 Exp_Lo
:= Type_Low_Bound
(Base_Type
(Choice_Type
));
1070 Exp_Hi
:= Type_High_Bound
(Base_Type
(Choice_Type
));
1073 Lo
:= Expr_Value
(Case_Table
(1).Lo
);
1074 Hi
:= Expr_Value
(Case_Table
(1).Hi
);
1075 Previous_Hi
:= Expr_Value
(Case_Table
(1).Hi
);
1077 -- Build the node for any missing choices that are smaller than any
1078 -- explicit choices given in the case.
1080 if Expr_Value
(Exp_Lo
) < Lo
then
1081 Append
(Build_Choice
(Expr_Value
(Exp_Lo
), Lo
- 1), Choice_List
);
1084 -- Build the nodes representing any missing choices that lie between
1085 -- the explicit ones given in the case.
1087 for J
in 2 .. Case_Table
'Last loop
1088 Lo
:= Expr_Value
(Case_Table
(J
).Lo
);
1089 Hi
:= Expr_Value
(Case_Table
(J
).Hi
);
1091 if Lo
/= (Previous_Hi
+ 1) then
1092 Append_To
(Choice_List
, Build_Choice
(Previous_Hi
+ 1, Lo
- 1));
1098 -- Build the node for any missing choices that are greater than any
1099 -- explicit choices given in the case.
1101 if Expr_Value
(Exp_Hi
) > Hi
then
1102 Append
(Build_Choice
(Hi
+ 1, Expr_Value
(Exp_Hi
)), Choice_List
);
1105 Set_Others_Discrete_Choices
(Others_Choice
, Choice_List
);
1107 -- Warn on null others list if warning option set
1109 if Warn_On_Redundant_Constructs
1110 and then Comes_From_Source
(Others_Choice
)
1111 and then Is_Empty_List
(Choice_List
)
1113 Error_Msg_N
("?r?OTHERS choice is redundant", Others_Choice
);
1114 Error_Msg_N
("\?r?previous choices cover all values", Others_Choice
);
1116 end Expand_Others_Choice
;
1122 procedure No_OP
(C
: Node_Id
) is
1123 pragma Warnings
(Off
, C
);
1128 -----------------------------
1129 -- Generic_Analyze_Choices --
1130 -----------------------------
1132 package body Generic_Analyze_Choices
is
1134 -- The following type is used to gather the entries for the choice
1135 -- table, so that we can then allocate the right length.
1138 type Link_Ptr
is access all Link
;
1141 Val
: Choice_Bounds
;
1145 ---------------------
1146 -- Analyze_Choices --
1147 ---------------------
1149 procedure Analyze_Choices
1150 (Alternatives
: List_Id
;
1153 Choice_Type
: constant Entity_Id
:= Base_Type
(Subtyp
);
1154 -- The actual type against which the discrete choices are resolved.
1155 -- Note that this type is always the base type not the subtype of the
1156 -- ruling expression, index or discriminant.
1158 Expected_Type
: Entity_Id
;
1159 -- The expected type of each choice. Equal to Choice_Type, except if
1160 -- the expression is universal, in which case the choices can be of
1161 -- any integer type.
1164 -- A case statement alternative or a variant in a record type
1169 -- The node kind of the current Choice
1172 -- Set Expected type (= choice type except for universal integer,
1173 -- where we accept any integer type as a choice).
1175 if Choice_Type
= Universal_Integer
then
1176 Expected_Type
:= Any_Integer
;
1178 Expected_Type
:= Choice_Type
;
1181 -- Now loop through the case alternatives or record variants
1183 Alt
:= First
(Alternatives
);
1184 while Present
(Alt
) loop
1186 -- If pragma, just analyze it
1188 if Nkind
(Alt
) = N_Pragma
then
1191 -- Otherwise we have an alternative. In most cases the semantic
1192 -- processing leaves the list of choices unchanged
1194 -- Check each choice against its base type
1197 Choice
:= First
(Discrete_Choices
(Alt
));
1198 while Present
(Choice
) loop
1200 Kind
:= Nkind
(Choice
);
1202 -- Choice is a Range
1205 or else (Kind
= N_Attribute_Reference
1206 and then Attribute_Name
(Choice
) = Name_Range
)
1208 Resolve
(Choice
, Expected_Type
);
1210 -- Choice is a subtype name, nothing further to do now
1212 elsif Is_Entity_Name
(Choice
)
1213 and then Is_Type
(Entity
(Choice
))
1217 -- Choice is a subtype indication
1219 elsif Kind
= N_Subtype_Indication
then
1220 Resolve_Discrete_Subtype_Indication
1221 (Choice
, Expected_Type
);
1223 -- Others choice, no analysis needed
1225 elsif Kind
= N_Others_Choice
then
1228 -- Only other possibility is an expression
1231 Resolve
(Choice
, Expected_Type
);
1234 -- Move to next choice
1239 Process_Associated_Node
(Alt
);
1244 end Analyze_Choices
;
1246 end Generic_Analyze_Choices
;
1248 ---------------------------
1249 -- Generic_Check_Choices --
1250 ---------------------------
1252 package body Generic_Check_Choices
is
1254 -- The following type is used to gather the entries for the choice
1255 -- table, so that we can then allocate the right length.
1258 type Link_Ptr
is access all Link
;
1261 Val
: Choice_Bounds
;
1265 procedure Free
is new Ada
.Unchecked_Deallocation
(Link
, Link_Ptr
);
1271 procedure Check_Choices
1273 Alternatives
: List_Id
;
1275 Others_Present
: out Boolean)
1279 Raises_CE
: Boolean;
1280 -- Set True if one of the bounds of a choice raises CE
1283 -- This is where we post error messages for bounds out of range
1285 Choice_List
: Link_Ptr
:= null;
1286 -- Gather list of choices
1288 Num_Choices
: Nat
:= 0;
1289 -- Number of entries in Choice_List
1291 Choice_Type
: constant Entity_Id
:= Base_Type
(Subtyp
);
1292 -- The actual type against which the discrete choices are resolved.
1293 -- Note that this type is always the base type not the subtype of the
1294 -- ruling expression, index or discriminant.
1296 Bounds_Type
: Entity_Id
;
1297 -- The type from which are derived the bounds of the values covered
1298 -- by the discrete choices (see 3.8.1 (4)). If a discrete choice
1299 -- specifies a value outside of these bounds we have an error.
1303 -- The actual bounds of the above type
1305 Expected_Type
: Entity_Id
;
1306 -- The expected type of each choice. Equal to Choice_Type, except if
1307 -- the expression is universal, in which case the choices can be of
1308 -- any integer type.
1311 -- A case statement alternative or a variant in a record type
1316 -- The node kind of the current Choice
1318 Others_Choice
: Node_Id
:= Empty
;
1319 -- Remember others choice if it is present (empty otherwise)
1321 procedure Check
(Choice
: Node_Id
; Lo
, Hi
: Node_Id
);
1322 -- Checks the validity of the bounds of a choice. When the bounds
1323 -- are static and no error occurred the bounds are collected for
1324 -- later entry into the choices table so that they can be sorted
1331 procedure Check
(Choice
: Node_Id
; Lo
, Hi
: Node_Id
) is
1336 -- First check if an error was already detected on either bounds
1338 if Etype
(Lo
) = Any_Type
or else Etype
(Hi
) = Any_Type
then
1341 -- Do not insert non static choices in the table to be sorted
1343 elsif not Is_OK_Static_Expression
(Lo
)
1345 not Is_OK_Static_Expression
(Hi
)
1347 Process_Non_Static_Choice
(Choice
);
1350 -- Ignore range which raise constraint error
1352 elsif Raises_Constraint_Error
(Lo
)
1353 or else Raises_Constraint_Error
(Hi
)
1358 -- AI05-0188 : Within an instance the non-others choices do not
1359 -- have to belong to the actual subtype.
1361 elsif Ada_Version
>= Ada_2012
and then In_Instance
then
1364 -- Otherwise we have an OK static choice
1367 Lo_Val
:= Expr_Value
(Lo
);
1368 Hi_Val
:= Expr_Value
(Hi
);
1370 -- Do not insert null ranges in the choices table
1372 if Lo_Val
> Hi_Val
then
1373 Process_Empty_Choice
(Choice
);
1378 -- Check for low bound out of range
1380 if Lo_Val
< Bounds_Lo
then
1382 -- If the choice is an entity name, then it is a type, and we
1383 -- want to post the message on the reference to this entity.
1384 -- Otherwise post it on the lower bound of the range.
1386 if Is_Entity_Name
(Choice
) then
1392 -- Specialize message for integer/enum type
1394 if Is_Integer_Type
(Bounds_Type
) then
1395 Error_Msg_Uint_1
:= Bounds_Lo
;
1396 Error_Msg_N
("minimum allowed choice value is^", Enode
);
1398 Error_Msg_Name_1
:= Choice_Image
(Bounds_Lo
, Bounds_Type
);
1399 Error_Msg_N
("minimum allowed choice value is%", Enode
);
1403 -- Check for high bound out of range
1405 if Hi_Val
> Bounds_Hi
then
1407 -- If the choice is an entity name, then it is a type, and we
1408 -- want to post the message on the reference to this entity.
1409 -- Otherwise post it on the upper bound of the range.
1411 if Is_Entity_Name
(Choice
) then
1417 -- Specialize message for integer/enum type
1419 if Is_Integer_Type
(Bounds_Type
) then
1420 Error_Msg_Uint_1
:= Bounds_Hi
;
1421 Error_Msg_N
("maximum allowed choice value is^", Enode
);
1423 Error_Msg_Name_1
:= Choice_Image
(Bounds_Hi
, Bounds_Type
);
1424 Error_Msg_N
("maximum allowed choice value is%", Enode
);
1428 -- Collect bounds in the list
1430 -- Note: we still store the bounds, even if they are out of range,
1431 -- since this may prevent unnecessary cascaded errors for values
1432 -- that are covered by such an excessive range.
1435 new Link
'(Val => (Lo, Hi, Choice), Nxt => Choice_List);
1436 Num_Choices := Num_Choices + 1;
1439 -- Start of processing for Check_Choices
1443 Others_Present := False;
1445 -- If Subtyp is not a discrete type or there was some other error,
1446 -- then don't try any semantic checking on the choices since we have
1449 if not Is_Discrete_Type (Subtyp) or else Subtyp = Any_Type then
1453 -- If Subtyp is not a static subtype Ada 95 requires then we use the
1454 -- bounds of its base type to determine the values covered by the
1455 -- discrete choices.
1457 -- In Ada 2012, if the subtype has a non-static predicate the full
1458 -- range of the base type must be covered as well.
1460 if Is_OK_Static_Subtype (Subtyp) then
1461 if not Has_Predicates (Subtyp)
1462 or else Has_Static_Predicate (Subtyp)
1464 Bounds_Type := Subtyp;
1466 Bounds_Type := Choice_Type;
1470 Bounds_Type := Choice_Type;
1473 -- Obtain static bounds of type, unless this is a generic formal
1474 -- discrete type for which all choices will be non-static.
1476 if not Is_Generic_Type (Root_Type (Bounds_Type))
1477 or else Ekind (Bounds_Type) /= E_Enumeration_Type
1479 Bounds_Lo := Expr_Value (Type_Low_Bound (Bounds_Type));
1480 Bounds_Hi := Expr_Value (Type_High_Bound (Bounds_Type));
1483 if Choice_Type = Universal_Integer then
1484 Expected_Type := Any_Integer;
1486 Expected_Type := Choice_Type;
1489 -- Now loop through the case alternatives or record variants
1491 Alt := First (Alternatives);
1492 while Present (Alt) loop
1494 -- If pragma, just analyze it
1496 if Nkind (Alt) = N_Pragma then
1499 -- Otherwise we have an alternative. In most cases the semantic
1500 -- processing leaves the list of choices unchanged
1502 -- Check each choice against its base type
1505 Choice := First (Discrete_Choices (Alt));
1506 while Present (Choice) loop
1507 Kind := Nkind (Choice);
1509 -- Choice is a Range
1512 or else (Kind = N_Attribute_Reference
1513 and then Attribute_Name (Choice) = Name_Range)
1515 Check (Choice, Low_Bound (Choice), High_Bound (Choice));
1517 -- Choice is a subtype name
1519 elsif Is_Entity_Name (Choice)
1520 and then Is_Type (Entity (Choice))
1522 -- Check for inappropriate type
1524 if not Covers (Expected_Type, Etype (Choice)) then
1525 Wrong_Type (Choice, Choice_Type);
1527 -- Type is OK, so check further
1530 E := Entity (Choice);
1532 -- Case of predicated subtype
1534 if Has_Predicates (E) then
1536 -- Use of non-static predicate is an error
1538 if not Is_Discrete_Type (E)
1539 or else not Has_Static_Predicate (E)
1540 or else Has_Dynamic_Predicate_Aspect (E)
1542 Bad_Predicated_Subtype_Use
1543 ("cannot use subtype& with non-static "
1544 & "predicate as case alternative",
1545 Choice, E, Suggest_Static => True);
1547 -- Static predicate case
1555 -- Loop through entries in predicate list,
1556 -- checking each entry. Note that if the
1557 -- list is empty, corresponding to a False
1558 -- predicate, then no choices are checked.
1560 P := First (Static_Discrete_Predicate (E));
1561 while Present (P) loop
1563 Set_Sloc (C, Sloc (Choice));
1564 Check (C, Low_Bound (C), High_Bound (C));
1569 Set_Has_SP_Choice (Alt);
1572 -- Not predicated subtype case
1574 elsif not Is_OK_Static_Subtype (E) then
1575 Process_Non_Static_Choice (Choice);
1578 (Choice, Type_Low_Bound (E), Type_High_Bound (E));
1582 -- Choice is a subtype indication
1584 elsif Kind = N_Subtype_Indication then
1585 Resolve_Discrete_Subtype_Indication
1586 (Choice, Expected_Type);
1588 if Etype (Choice) /= Any_Type then
1590 C : constant Node_Id := Constraint (Choice);
1591 R : constant Node_Id := Range_Expression (C);
1592 L : constant Node_Id := Low_Bound (R);
1593 H : constant Node_Id := High_Bound (R);
1596 E := Entity (Subtype_Mark (Choice));
1598 if not Is_OK_Static_Subtype (E) then
1599 Process_Non_Static_Choice (Choice);
1602 if Is_OK_Static_Expression (L)
1604 Is_OK_Static_Expression (H)
1606 if Expr_Value (L) > Expr_Value (H) then
1607 Process_Empty_Choice (Choice);
1609 if Is_Out_Of_Range (L, E) then
1610 Apply_Compile_Time_Constraint_Error
1611 (L, "static value out of range",
1612 CE_Range_Check_Failed);
1615 if Is_Out_Of_Range (H, E) then
1616 Apply_Compile_Time_Constraint_Error
1617 (H, "static value out of range",
1618 CE_Range_Check_Failed);
1623 Check (Choice, L, H);
1628 -- The others choice is only allowed for the last
1629 -- alternative and as its only choice.
1631 elsif Kind = N_Others_Choice then
1632 if not (Choice = First (Discrete_Choices (Alt))
1633 and then Choice = Last (Discrete_Choices (Alt))
1634 and then Alt = Last (Alternatives))
1637 ("the choice OTHERS must appear alone and last",
1642 Others_Present := True;
1643 Others_Choice := Choice;
1645 -- Only other possibility is an expression
1648 Check (Choice, Choice, Choice);
1651 -- Move to next choice
1656 Process_Associated_Node (Alt);
1662 -- Now we can create the Choice_Table, since we know how long
1663 -- it needs to be so we can allocate exactly the right length.
1666 Choice_Table : Choice_Table_Type (0 .. Num_Choices);
1669 -- Now copy the items we collected in the linked list into this
1670 -- newly allocated table (leave entry 0 unused for sorting).
1675 for J in 1 .. Num_Choices loop
1677 Choice_List := T.Nxt;
1678 Choice_Table (J) := T.Val;
1687 Others_Present or else (Choice_Type = Universal_Integer),
1690 -- If no others choice we are all done, otherwise we have one more
1691 -- step, which is to set the Others_Discrete_Choices field of the
1692 -- others choice (to contain all otherwise unspecified choices).
1693 -- Skip this if CE is known to be raised.
1695 if Others_Present and not Raises_CE then
1696 Expand_Others_Choice
1697 (Case_Table => Choice_Table,
1698 Others_Choice => Others_Choice,
1699 Choice_Type => Bounds_Type);
1704 end Generic_Check_Choices;