1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2013, 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 procedure Check_Against_Predicate
118 (Pred
: in out Node_Id
;
119 Choice
: Choice_Bounds
;
120 Prev_Lo
: in out Uint
;
121 Prev_Hi
: in out Uint
;
122 Error
: in out Boolean);
123 -- Determine whether a choice covers legal values as defined by a static
124 -- predicate set. Pred is a static predicate range. Choice is the choice
125 -- to be examined. Prev_Lo and Prev_Hi are the bounds of the previous
126 -- choice that covered a predicate set. Error denotes whether the check
127 -- found an illegal intersection.
129 procedure Dup_Choice
(Lo
, Hi
: Uint
; C
: Node_Id
);
130 -- Post message "duplication of choice value(s) bla bla at xx". Message
131 -- is posted at location C. Caller sets Error_Msg_Sloc for xx.
133 procedure Explain_Non_Static_Bound
;
134 -- Called when we find a non-static bound, requiring the base type to
135 -- be covered. Provides where possible a helpful explanation of why the
136 -- bounds are non-static, since this is not always obvious.
138 function Lt_Choice
(C1
, C2
: Natural) return Boolean;
139 -- Comparison routine for comparing Choice_Table entries. Use the lower
140 -- bound of each Choice as the key.
142 procedure Missing_Choice
(Value1
: Node_Id
; Value2
: Node_Id
);
143 procedure Missing_Choice
(Value1
: Node_Id
; Value2
: Uint
);
144 procedure Missing_Choice
(Value1
: Uint
; Value2
: Node_Id
);
145 procedure Missing_Choice
(Value1
: Uint
; Value2
: Uint
);
146 -- Issue an error message indicating that there are missing choices,
147 -- followed by the image of the missing choices themselves which lie
148 -- between Value1 and Value2 inclusive.
150 procedure Missing_Choices
(Pred
: Node_Id
; Prev_Hi
: Uint
);
151 -- Emit an error message for each non-covered static predicate set.
152 -- Prev_Hi denotes the upper bound of the last choice covering a set.
154 procedure Move_Choice
(From
: Natural; To
: Natural);
155 -- Move routine for sorting the Choice_Table
157 package Sorting
is new GNAT
.Heap_Sort_G
(Move_Choice
, Lt_Choice
);
159 -----------------------------
160 -- Check_Against_Predicate --
161 -----------------------------
163 procedure Check_Against_Predicate
164 (Pred
: in out Node_Id
;
165 Choice
: Choice_Bounds
;
166 Prev_Lo
: in out Uint
;
167 Prev_Hi
: in out Uint
;
168 Error
: in out Boolean)
170 procedure Illegal_Range
174 -- Emit an error message regarding a choice that clashes with the
175 -- legal static predicate sets. Loc is the location of the choice
176 -- that introduced the illegal range. Lo .. Hi is the range.
178 function Inside_Range
181 Val
: Uint
) return Boolean;
182 -- Determine whether position Val within a discrete type is within
183 -- the range Lo .. Hi inclusive.
189 procedure Illegal_Range
195 Error_Msg_Name_1
:= Chars
(Bounds_Type
);
200 if Is_Integer_Type
(Bounds_Type
) then
201 Error_Msg_Uint_1
:= Lo
;
202 Error_Msg
("static predicate on % excludes value ^!", Loc
);
204 Error_Msg_Name_2
:= Choice_Image
(Lo
, Bounds_Type
);
205 Error_Msg
("static predicate on % excludes value %!", Loc
);
211 if Is_Integer_Type
(Bounds_Type
) then
212 Error_Msg_Uint_1
:= Lo
;
213 Error_Msg_Uint_2
:= Hi
;
215 ("static predicate on % excludes range ^ .. ^!", Loc
);
217 Error_Msg_Name_2
:= Choice_Image
(Lo
, Bounds_Type
);
218 Error_Msg_Name_3
:= Choice_Image
(Hi
, Bounds_Type
);
220 ("static predicate on % excludes range % .. %!", Loc
);
229 function Inside_Range
232 Val
: Uint
) return Boolean
236 Val
= Lo
or else Val
= Hi
or else (Lo
< Val
and then Val
< Hi
);
241 Choice_Hi
: constant Uint
:= Expr_Value
(Choice
.Hi
);
242 Choice_Lo
: constant Uint
:= Expr_Value
(Choice
.Lo
);
250 -- Start of processing for Check_Against_Predicate
253 -- Find the proper error message location
255 if Present
(Choice
.Node
) then
263 if Present
(Pred
) then
264 Pred_Lo
:= Expr_Value
(Low_Bound
(Pred
));
265 Pred_Hi
:= Expr_Value
(High_Bound
(Pred
));
267 -- Previous choices managed to satisfy all static predicate sets
270 Illegal_Range
(Loc
, Choice_Lo
, Choice_Hi
);
275 -- Step 1: Detect duplicate choices
277 if Inside_Range
(Choice_Lo
, Choice_Hi
, Prev_Lo
) then
278 Dup_Choice
(Prev_Lo
, UI_Min
(Prev_Hi
, Choice_Hi
), LocN
);
281 elsif Inside_Range
(Choice_Lo
, Choice_Hi
, Prev_Hi
) then
282 Dup_Choice
(UI_Max
(Choice_Lo
, Prev_Lo
), Prev_Hi
, LocN
);
285 -- Step 2: Detect full coverage
287 -- Choice_Lo Choice_Hi
291 elsif Choice_Lo
= Pred_Lo
and then Choice_Hi
= Pred_Hi
then
292 Prev_Lo
:= Choice_Lo
;
293 Prev_Hi
:= Choice_Hi
;
296 -- Step 3: Detect all cases where a choice mentions values that are
297 -- not part of the static predicate sets.
299 -- Choice_Lo Choice_Hi Pred_Lo Pred_Hi
300 -- +-----------+ . . . . . +=========+
303 elsif Choice_Lo
< Pred_Lo
and then Choice_Hi
< Pred_Lo
then
304 Illegal_Range
(Loc
, Choice_Lo
, Choice_Hi
);
307 -- Choice_Lo Pred_Lo Choice_Hi Pred_Hi
308 -- +-----------+=========+===========+
311 elsif Choice_Lo
< Pred_Lo
312 and then Inside_Range
(Pred_Lo
, Pred_Hi
, Choice_Hi
)
314 Illegal_Range
(Loc
, Choice_Lo
, Pred_Lo
- 1);
317 -- Pred_Lo Pred_Hi Choice_Lo Choice_Hi
318 -- +=========+ . . . . +-----------+
321 elsif Pred_Lo
< Choice_Lo
and then Pred_Hi
< Choice_Lo
then
322 if Others_Present
then
324 -- Current predicate set is covered by others clause.
329 Missing_Choice
(Pred_Lo
, Pred_Hi
);
333 -- There may be several static predicate sets between the current
334 -- one and the choice. Inspect the next static predicate set.
337 Check_Against_Predicate
344 -- Pred_Lo Choice_Lo Pred_Hi Choice_Hi
345 -- +=========+===========+-----------+
348 elsif Pred_Hi
< Choice_Hi
349 and then Inside_Range
(Pred_Lo
, Pred_Hi
, Choice_Lo
)
353 -- The choice may fall in a static predicate set. If this is the
354 -- case, avoid mentioning legal values in the error message.
356 if Present
(Pred
) then
357 Next_Lo
:= Expr_Value
(Low_Bound
(Pred
));
358 Next_Hi
:= Expr_Value
(High_Bound
(Pred
));
360 -- The next static predicate set is to the right of the choice
362 if Choice_Hi
< Next_Lo
and then Choice_Hi
< Next_Hi
then
363 Illegal_Range
(Loc
, Pred_Hi
+ 1, Choice_Hi
);
365 Illegal_Range
(Loc
, Pred_Hi
+ 1, Next_Lo
- 1);
368 Illegal_Range
(Loc
, Pred_Hi
+ 1, Choice_Hi
);
373 -- Choice_Lo Pred_Lo Pred_Hi Choice_Hi
374 -- +-----------+=========+-----------+
375 -- ^ illegal ^ ^ illegal ^
377 -- Emit an error on the low gap, disregard the upper gap
379 elsif Choice_Lo
< Pred_Lo
and then Pred_Hi
< Choice_Hi
then
380 Illegal_Range
(Loc
, Choice_Lo
, Pred_Lo
- 1);
383 -- Step 4: Detect all cases of partial or missing coverage
385 -- Pred_Lo Choice_Lo Choice_Hi Pred_Hi
386 -- +=========+==========+===========+
390 -- An "others" choice covers all gaps
392 if Others_Present
then
393 Prev_Lo
:= Choice_Lo
;
394 Prev_Hi
:= Choice_Hi
;
396 -- Check whether predicate set is fully covered by choice
398 if Pred_Hi
= Choice_Hi
then
402 -- Choice_Lo Choice_Hi Pred_Hi
403 -- +===========+===========+
406 -- The upper gap may be covered by a subsequent choice
408 elsif Pred_Lo
= Choice_Lo
then
409 Prev_Lo
:= Choice_Lo
;
410 Prev_Hi
:= Choice_Hi
;
412 -- Pred_Lo Prev_Hi Choice_Lo Choice_Hi Pred_Hi
413 -- +===========+=========+===========+===========+
414 -- ^ covered ^ ^ gap ^
416 else pragma Assert
(Pred_Lo
< Choice_Lo
);
418 -- A previous choice covered the gap up to the current choice
420 if Prev_Hi
= Choice_Lo
- 1 then
421 Prev_Lo
:= Choice_Lo
;
422 Prev_Hi
:= Choice_Hi
;
424 if Choice_Hi
= Pred_Hi
then
428 -- The previous choice did not intersect with the current
429 -- static predicate set.
431 elsif Prev_Hi
< Pred_Lo
then
432 Missing_Choice
(Pred_Lo
, Choice_Lo
- 1);
435 -- The previous choice covered part of the static predicate set
438 Missing_Choice
(Prev_Hi
, Choice_Lo
- 1);
443 end Check_Against_Predicate
;
449 procedure Dup_Choice
(Lo
, Hi
: Uint
; C
: Node_Id
) is
451 -- In some situations, we call this with a null range, and obviously
452 -- we don't want to complain in this case.
458 -- Case of only one value that is missing
461 if Is_Integer_Type
(Bounds_Type
) then
462 Error_Msg_Uint_1
:= Lo
;
463 Error_Msg_N
("duplication of choice value: ^#!", C
);
465 Error_Msg_Name_1
:= Choice_Image
(Lo
, Bounds_Type
);
466 Error_Msg_N
("duplication of choice value: %#!", C
);
469 -- More than one choice value, so print range of values
472 if Is_Integer_Type
(Bounds_Type
) then
473 Error_Msg_Uint_1
:= Lo
;
474 Error_Msg_Uint_2
:= Hi
;
475 Error_Msg_N
("duplication of choice values: ^ .. ^#!", C
);
477 Error_Msg_Name_1
:= Choice_Image
(Lo
, Bounds_Type
);
478 Error_Msg_Name_2
:= Choice_Image
(Hi
, Bounds_Type
);
479 Error_Msg_N
("duplication of choice values: % .. %#!", C
);
484 ------------------------------
485 -- Explain_Non_Static_Bound --
486 ------------------------------
488 procedure Explain_Non_Static_Bound
is
492 if Nkind
(Case_Node
) = N_Variant_Part
then
493 Expr
:= Name
(Case_Node
);
495 Expr
:= Expression
(Case_Node
);
498 if Bounds_Type
/= Subtyp
then
500 -- If the case is a variant part, the expression is given by the
501 -- discriminant itself, and the bounds are the culprits.
503 if Nkind
(Case_Node
) = N_Variant_Part
then
505 ("bounds of & are not static, "
506 & "alternatives must cover base type!", Expr
, Expr
);
508 -- If this is a case statement, the expression may be non-static
509 -- or else the subtype may be at fault.
511 elsif Is_Entity_Name
(Expr
) then
513 ("bounds of & are not static, "
514 & "alternatives must cover base type!", Expr
, Expr
);
518 ("subtype of expression is not static, "
519 & "alternatives must cover base type!", Expr
);
522 -- Otherwise the expression is not static, even if the bounds of the
523 -- type are, or else there are missing alternatives. If both, the
524 -- additional information may be redundant but harmless.
526 elsif not Is_Entity_Name
(Expr
) then
528 ("subtype of expression is not static, "
529 & "alternatives must cover base type!", Expr
);
531 end Explain_Non_Static_Bound
;
537 function Lt_Choice
(C1
, C2
: Natural) return Boolean is
540 Expr_Value
(Choice_Table
(Nat
(C1
)).Lo
)
542 Expr_Value
(Choice_Table
(Nat
(C2
)).Lo
);
549 procedure Missing_Choice
(Value1
: Node_Id
; Value2
: Node_Id
) is
551 Missing_Choice
(Expr_Value
(Value1
), Expr_Value
(Value2
));
554 procedure Missing_Choice
(Value1
: Node_Id
; Value2
: Uint
) is
556 Missing_Choice
(Expr_Value
(Value1
), Value2
);
559 procedure Missing_Choice
(Value1
: Uint
; Value2
: Node_Id
) is
561 Missing_Choice
(Value1
, Expr_Value
(Value2
));
564 procedure Missing_Choice
(Value1
: Uint
; Value2
: Uint
) is
565 Msg_Sloc
: constant Source_Ptr
:= Sloc
(Case_Node
);
568 -- AI05-0188 : within an instance the non-others choices do not have
569 -- to belong to the actual subtype.
571 if Ada_Version
>= Ada_2012
and then In_Instance
then
574 -- In some situations, we call this with a null range, and obviously
575 -- we don't want to complain in this case.
577 elsif Value1
> Value2
then
581 -- Case of only one value that is missing
583 if Value1
= Value2
then
584 if Is_Integer_Type
(Bounds_Type
) then
585 Error_Msg_Uint_1
:= Value1
;
586 Error_Msg
("missing case value: ^!", Msg_Sloc
);
588 Error_Msg_Name_1
:= Choice_Image
(Value1
, Bounds_Type
);
589 Error_Msg
("missing case value: %!", Msg_Sloc
);
592 -- More than one choice value, so print range of values
595 if Is_Integer_Type
(Bounds_Type
) then
596 Error_Msg_Uint_1
:= Value1
;
597 Error_Msg_Uint_2
:= Value2
;
598 Error_Msg
("missing case values: ^ .. ^!", Msg_Sloc
);
600 Error_Msg_Name_1
:= Choice_Image
(Value1
, Bounds_Type
);
601 Error_Msg_Name_2
:= Choice_Image
(Value2
, Bounds_Type
);
602 Error_Msg
("missing case values: % .. %!", Msg_Sloc
);
607 ---------------------
608 -- Missing_Choices --
609 ---------------------
611 procedure Missing_Choices
(Pred
: Node_Id
; Prev_Hi
: Uint
) is
618 while Present
(Set
) loop
619 Lo
:= Expr_Value
(Low_Bound
(Set
));
620 Hi
:= Expr_Value
(High_Bound
(Set
));
622 -- A choice covered part of a static predicate set
624 if Lo
<= Prev_Hi
and then Prev_Hi
< Hi
then
625 Missing_Choice
(Prev_Hi
+ 1, Hi
);
628 Missing_Choice
(Lo
, Hi
);
639 procedure Move_Choice
(From
: Natural; To
: Natural) is
641 Choice_Table
(Nat
(To
)) := Choice_Table
(Nat
(From
));
646 Bounds_Hi
: constant Node_Id
:= Type_High_Bound
(Bounds_Type
);
647 Bounds_Lo
: constant Node_Id
:= Type_Low_Bound
(Bounds_Type
);
648 Has_Predicate
: constant Boolean :=
649 Is_Static_Subtype
(Bounds_Type
)
650 and then Present
(Static_Predicate
(Bounds_Type
));
651 Num_Choices
: constant Nat
:= Choice_Table
'Last;
658 Prev_Choice
: Node_Id
;
662 -- Start of processing for Check_Choice_Set
665 -- Choice_Table must start at 0 which is an unused location used by the
666 -- sorting algorithm. However the first valid position for a discrete
669 pragma Assert
(Choice_Table
'First = 0);
671 -- The choices do not cover the base range. Emit an error if "others" is
672 -- not available and return as there is no need for further processing.
674 if Num_Choices
= 0 then
675 if not Others_Present
then
676 Missing_Choice
(Bounds_Lo
, Bounds_Hi
);
682 Sorting
.Sort
(Positive (Choice_Table
'Last));
684 -- The type covered by the list of choices is actually a static subtype
685 -- subject to a static predicate. The predicate defines subsets of legal
686 -- values and requires finer grained analysis.
688 if Has_Predicate
then
689 Pred
:= First
(Static_Predicate
(Bounds_Type
));
690 Prev_Lo
:= Uint_Minus_1
;
691 Prev_Hi
:= Uint_Minus_1
;
694 for Index
in 1 .. Num_Choices
loop
695 Check_Against_Predicate
697 Choice
=> Choice_Table
(Index
),
702 -- The analysis detected an illegal intersection between a choice
703 -- and a static predicate set.
710 -- The choices may legally cover some of the static predicate sets,
711 -- but not all. Emit an error for each non-covered set.
713 if not Others_Present
then
714 Missing_Choices
(Pred
, Prev_Hi
);
720 Choice_Lo
:= Expr_Value
(Choice_Table
(1).Lo
);
721 Choice_Hi
:= Expr_Value
(Choice_Table
(1).Hi
);
722 Prev_Hi
:= Choice_Hi
;
724 if not Others_Present
and then Expr_Value
(Bounds_Lo
) < Choice_Lo
then
725 Missing_Choice
(Bounds_Lo
, Choice_Lo
- 1);
727 -- If values are missing outside of the subtype, add explanation.
728 -- No additional message if only one value is missing.
730 if Expr_Value
(Bounds_Lo
) < Choice_Lo
- 1 then
731 Explain_Non_Static_Bound
;
735 for Outer_Index
in 2 .. Num_Choices
loop
736 Choice_Lo
:= Expr_Value
(Choice_Table
(Outer_Index
).Lo
);
737 Choice_Hi
:= Expr_Value
(Choice_Table
(Outer_Index
).Hi
);
739 if Choice_Lo
<= Prev_Hi
then
740 Choice
:= Choice_Table
(Outer_Index
).Node
;
742 -- Find first previous choice that overlaps
744 for Inner_Index
in 1 .. Outer_Index
- 1 loop
746 Expr_Value
(Choice_Table
(Inner_Index
).Hi
)
748 Prev_Choice
:= Choice_Table
(Inner_Index
).Node
;
753 if Sloc
(Prev_Choice
) <= Sloc
(Choice
) then
754 Error_Msg_Sloc
:= Sloc
(Prev_Choice
);
756 (Choice_Lo
, UI_Min
(Choice_Hi
, Prev_Hi
), Choice
);
758 Error_Msg_Sloc
:= Sloc
(Choice
);
760 (Choice_Lo
, UI_Min
(Choice_Hi
, Prev_Hi
), Prev_Choice
);
763 elsif not Others_Present
and then Choice_Lo
/= Prev_Hi
+ 1 then
764 Missing_Choice
(Prev_Hi
+ 1, Choice_Lo
- 1);
767 if Choice_Hi
> Prev_Hi
then
768 Prev_Hi
:= Choice_Hi
;
772 if not Others_Present
and then Expr_Value
(Bounds_Hi
) > Prev_Hi
then
773 Missing_Choice
(Prev_Hi
+ 1, Bounds_Hi
);
775 if Expr_Value
(Bounds_Hi
) > Prev_Hi
+ 1 then
776 Explain_Non_Static_Bound
;
780 end Check_Choice_Set
;
786 function Choice_Image
(Value
: Uint
; Ctype
: Entity_Id
) return Name_Id
is
787 Rtp
: constant Entity_Id
:= Root_Type
(Ctype
);
792 -- For character, or wide [wide] character. If 7-bit ASCII graphic
793 -- range, then build and return appropriate character literal name
795 if Is_Standard_Character_Type
(Ctype
) then
796 C
:= UI_To_Int
(Value
);
798 if C
in 16#
20#
.. 16#
7E#
then
799 Set_Character_Literal_Name
(Char_Code
(UI_To_Int
(Value
)));
803 -- For user defined enumeration type, find enum/char literal
806 Lit
:= First_Literal
(Rtp
);
808 for J
in 1 .. UI_To_Int
(Value
) loop
812 -- If enumeration literal, just return its value
814 if Nkind
(Lit
) = N_Defining_Identifier
then
817 -- For character literal, get the name and use it if it is
818 -- for a 7-bit ASCII graphic character in 16#20#..16#7E#.
821 Get_Decoded_Name_String
(Chars
(Lit
));
824 and then Name_Buffer
(2) in
825 Character'Val (16#
20#
) .. Character'Val (16#
7E#
)
832 -- If we fall through, we have a character literal which is not in
833 -- the 7-bit ASCII graphic set. For such cases, we construct the
834 -- name "type'val(nnn)" where type is the choice type, and nnn is
835 -- the pos value passed as an argument to Choice_Image.
837 Get_Name_String
(Chars
(First_Subtype
(Ctype
)));
839 Add_Str_To_Name_Buffer
("'val(");
841 Add_Str_To_Name_Buffer
(UI_Image_Buffer
(1 .. UI_Image_Length
));
842 Add_Char_To_Name_Buffer
(')');
846 --------------------------
847 -- Expand_Others_Choice --
848 --------------------------
850 procedure Expand_Others_Choice
851 (Case_Table
: Choice_Table_Type
;
852 Others_Choice
: Node_Id
;
853 Choice_Type
: Entity_Id
)
855 Loc
: constant Source_Ptr
:= Sloc
(Others_Choice
);
856 Choice_List
: constant List_Id
:= New_List
;
864 function Build_Choice
(Value1
, Value2
: Uint
) return Node_Id
;
865 -- Builds a node representing the missing choices given by Value1 and
866 -- Value2. A N_Range node is built if there is more than one literal
867 -- value missing. Otherwise a single N_Integer_Literal, N_Identifier
868 -- or N_Character_Literal is built depending on what Choice_Type is.
870 function Lit_Of
(Value
: Uint
) return Node_Id
;
871 -- Returns the Node_Id for the enumeration literal corresponding to the
872 -- position given by Value within the enumeration type Choice_Type.
878 function Build_Choice
(Value1
, Value2
: Uint
) return Node_Id
is
883 -- If there is only one choice value missing between Value1 and
884 -- Value2, build an integer or enumeration literal to represent it.
886 if (Value2
- Value1
) = 0 then
887 if Is_Integer_Type
(Choice_Type
) then
888 Lit_Node
:= Make_Integer_Literal
(Loc
, Value1
);
889 Set_Etype
(Lit_Node
, Choice_Type
);
891 Lit_Node
:= Lit_Of
(Value1
);
894 -- Otherwise is more that one choice value that is missing between
895 -- Value1 and Value2, therefore build a N_Range node of either
896 -- integer or enumeration literals.
899 if Is_Integer_Type
(Choice_Type
) then
900 Lo
:= Make_Integer_Literal
(Loc
, Value1
);
901 Set_Etype
(Lo
, Choice_Type
);
902 Hi
:= Make_Integer_Literal
(Loc
, Value2
);
903 Set_Etype
(Hi
, Choice_Type
);
912 Low_Bound
=> Lit_Of
(Value1
),
913 High_Bound
=> Lit_Of
(Value2
));
924 function Lit_Of
(Value
: Uint
) return Node_Id
is
928 -- In the case where the literal is of type Character, there needs
929 -- to be some special handling since there is no explicit chain
930 -- of literals to search. Instead, a N_Character_Literal node
931 -- is created with the appropriate Char_Code and Chars fields.
933 if Is_Standard_Character_Type
(Choice_Type
) then
934 Set_Character_Literal_Name
(Char_Code
(UI_To_Int
(Value
)));
935 Lit
:= New_Node
(N_Character_Literal
, Loc
);
936 Set_Chars
(Lit
, Name_Find
);
937 Set_Char_Literal_Value
(Lit
, Value
);
938 Set_Etype
(Lit
, Choice_Type
);
939 Set_Is_Static_Expression
(Lit
, True);
942 -- Otherwise, iterate through the literals list of Choice_Type
943 -- "Value" number of times until the desired literal is reached
944 -- and then return an occurrence of it.
947 Lit
:= First_Literal
(Choice_Type
);
948 for J
in 1 .. UI_To_Int
(Value
) loop
952 return New_Occurrence_Of
(Lit
, Loc
);
956 -- Start of processing for Expand_Others_Choice
959 if Case_Table
'Last = 0 then
961 -- Special case: only an others case is present. The others case
962 -- covers the full range of the type.
964 if Is_Static_Subtype
(Choice_Type
) then
965 Choice
:= New_Occurrence_Of
(Choice_Type
, Loc
);
967 Choice
:= New_Occurrence_Of
(Base_Type
(Choice_Type
), Loc
);
970 Set_Others_Discrete_Choices
(Others_Choice
, New_List
(Choice
));
974 -- Establish the bound values for the choice depending upon whether the
975 -- type of the case statement is static or not.
977 if Is_OK_Static_Subtype
(Choice_Type
) then
978 Exp_Lo
:= Type_Low_Bound
(Choice_Type
);
979 Exp_Hi
:= Type_High_Bound
(Choice_Type
);
981 Exp_Lo
:= Type_Low_Bound
(Base_Type
(Choice_Type
));
982 Exp_Hi
:= Type_High_Bound
(Base_Type
(Choice_Type
));
985 Lo
:= Expr_Value
(Case_Table
(1).Lo
);
986 Hi
:= Expr_Value
(Case_Table
(1).Hi
);
987 Previous_Hi
:= Expr_Value
(Case_Table
(1).Hi
);
989 -- Build the node for any missing choices that are smaller than any
990 -- explicit choices given in the case.
992 if Expr_Value
(Exp_Lo
) < Lo
then
993 Append
(Build_Choice
(Expr_Value
(Exp_Lo
), Lo
- 1), Choice_List
);
996 -- Build the nodes representing any missing choices that lie between
997 -- the explicit ones given in the case.
999 for J
in 2 .. Case_Table
'Last loop
1000 Lo
:= Expr_Value
(Case_Table
(J
).Lo
);
1001 Hi
:= Expr_Value
(Case_Table
(J
).Hi
);
1003 if Lo
/= (Previous_Hi
+ 1) then
1004 Append_To
(Choice_List
, Build_Choice
(Previous_Hi
+ 1, Lo
- 1));
1010 -- Build the node for any missing choices that are greater than any
1011 -- explicit choices given in the case.
1013 if Expr_Value
(Exp_Hi
) > Hi
then
1014 Append
(Build_Choice
(Hi
+ 1, Expr_Value
(Exp_Hi
)), Choice_List
);
1017 Set_Others_Discrete_Choices
(Others_Choice
, Choice_List
);
1019 -- Warn on null others list if warning option set
1021 if Warn_On_Redundant_Constructs
1022 and then Comes_From_Source
(Others_Choice
)
1023 and then Is_Empty_List
(Choice_List
)
1025 Error_Msg_N
("?r?OTHERS choice is redundant", Others_Choice
);
1026 Error_Msg_N
("\?r?previous choices cover all values", Others_Choice
);
1028 end Expand_Others_Choice
;
1034 procedure No_OP
(C
: Node_Id
) is
1035 pragma Warnings
(Off
, C
);
1040 -----------------------------
1041 -- Generic_Analyze_Choices --
1042 -----------------------------
1044 package body Generic_Analyze_Choices
is
1046 -- The following type is used to gather the entries for the choice
1047 -- table, so that we can then allocate the right length.
1050 type Link_Ptr
is access all Link
;
1053 Val
: Choice_Bounds
;
1057 ---------------------
1058 -- Analyze_Choices --
1059 ---------------------
1061 procedure Analyze_Choices
1062 (Alternatives
: List_Id
;
1065 Choice_Type
: constant Entity_Id
:= Base_Type
(Subtyp
);
1066 -- The actual type against which the discrete choices are resolved.
1067 -- Note that this type is always the base type not the subtype of the
1068 -- ruling expression, index or discriminant.
1070 Expected_Type
: Entity_Id
;
1071 -- The expected type of each choice. Equal to Choice_Type, except if
1072 -- the expression is universal, in which case the choices can be of
1073 -- any integer type.
1076 -- A case statement alternative or a variant in a record type
1081 -- The node kind of the current Choice
1084 -- Set Expected type (= choice type except for universal integer,
1085 -- where we accept any integer type as a choice).
1087 if Choice_Type
= Universal_Integer
then
1088 Expected_Type
:= Any_Integer
;
1090 Expected_Type
:= Choice_Type
;
1093 -- Now loop through the case alternatives or record variants
1095 Alt
:= First
(Alternatives
);
1096 while Present
(Alt
) loop
1098 -- If pragma, just analyze it
1100 if Nkind
(Alt
) = N_Pragma
then
1103 -- Otherwise we have an alternative. In most cases the semantic
1104 -- processing leaves the list of choices unchanged
1106 -- Check each choice against its base type
1109 Choice
:= First
(Discrete_Choices
(Alt
));
1110 while Present
(Choice
) loop
1112 Kind
:= Nkind
(Choice
);
1114 -- Choice is a Range
1117 or else (Kind
= N_Attribute_Reference
1118 and then Attribute_Name
(Choice
) = Name_Range
)
1120 Resolve
(Choice
, Expected_Type
);
1122 -- Choice is a subtype name, nothing further to do now
1124 elsif Is_Entity_Name
(Choice
)
1125 and then Is_Type
(Entity
(Choice
))
1129 -- Choice is a subtype indication
1131 elsif Kind
= N_Subtype_Indication
then
1132 Resolve_Discrete_Subtype_Indication
1133 (Choice
, Expected_Type
);
1135 -- Others choice, no analysis needed
1137 elsif Kind
= N_Others_Choice
then
1140 -- Only other possibility is an expression
1143 Resolve
(Choice
, Expected_Type
);
1146 -- Move to next choice
1151 Process_Associated_Node
(Alt
);
1156 end Analyze_Choices
;
1158 end Generic_Analyze_Choices
;
1160 ---------------------------
1161 -- Generic_Check_Choices --
1162 ---------------------------
1164 package body Generic_Check_Choices
is
1166 -- The following type is used to gather the entries for the choice
1167 -- table, so that we can then allocate the right length.
1170 type Link_Ptr
is access all Link
;
1173 Val
: Choice_Bounds
;
1177 procedure Free
is new Ada
.Unchecked_Deallocation
(Link
, Link_Ptr
);
1183 procedure Check_Choices
1185 Alternatives
: List_Id
;
1187 Others_Present
: out Boolean)
1191 Raises_CE
: Boolean;
1192 -- Set True if one of the bounds of a choice raises CE
1195 -- This is where we post error messages for bounds out of range
1197 Choice_List
: Link_Ptr
:= null;
1198 -- Gather list of choices
1200 Num_Choices
: Nat
:= 0;
1201 -- Number of entries in Choice_List
1203 Choice_Type
: constant Entity_Id
:= Base_Type
(Subtyp
);
1204 -- The actual type against which the discrete choices are resolved.
1205 -- Note that this type is always the base type not the subtype of the
1206 -- ruling expression, index or discriminant.
1208 Bounds_Type
: Entity_Id
;
1209 -- The type from which are derived the bounds of the values covered
1210 -- by the discrete choices (see 3.8.1 (4)). If a discrete choice
1211 -- specifies a value outside of these bounds we have an error.
1215 -- The actual bounds of the above type
1217 Expected_Type
: Entity_Id
;
1218 -- The expected type of each choice. Equal to Choice_Type, except if
1219 -- the expression is universal, in which case the choices can be of
1220 -- any integer type.
1223 -- A case statement alternative or a variant in a record type
1228 -- The node kind of the current Choice
1230 Others_Choice
: Node_Id
:= Empty
;
1231 -- Remember others choice if it is present (empty otherwise)
1233 procedure Check
(Choice
: Node_Id
; Lo
, Hi
: Node_Id
);
1234 -- Checks the validity of the bounds of a choice. When the bounds
1235 -- are static and no error occurred the bounds are collected for
1236 -- later entry into the choices table so that they can be sorted
1243 procedure Check
(Choice
: Node_Id
; Lo
, Hi
: Node_Id
) is
1248 -- First check if an error was already detected on either bounds
1250 if Etype
(Lo
) = Any_Type
or else Etype
(Hi
) = Any_Type
then
1253 -- Do not insert non static choices in the table to be sorted
1255 elsif not Is_Static_Expression
(Lo
)
1257 not Is_Static_Expression
(Hi
)
1259 Process_Non_Static_Choice
(Choice
);
1262 -- Ignore range which raise constraint error
1264 elsif Raises_Constraint_Error
(Lo
)
1265 or else Raises_Constraint_Error
(Hi
)
1270 -- AI05-0188 : Within an instance the non-others choices do not
1271 -- have to belong to the actual subtype.
1273 elsif Ada_Version
>= Ada_2012
and then In_Instance
then
1276 -- Otherwise we have an OK static choice
1279 Lo_Val
:= Expr_Value
(Lo
);
1280 Hi_Val
:= Expr_Value
(Hi
);
1282 -- Do not insert null ranges in the choices table
1284 if Lo_Val
> Hi_Val
then
1285 Process_Empty_Choice
(Choice
);
1290 -- Check for low bound out of range
1292 if Lo_Val
< Bounds_Lo
then
1294 -- If the choice is an entity name, then it is a type, and we
1295 -- want to post the message on the reference to this entity.
1296 -- Otherwise post it on the lower bound of the range.
1298 if Is_Entity_Name
(Choice
) then
1304 -- Specialize message for integer/enum type
1306 if Is_Integer_Type
(Bounds_Type
) then
1307 Error_Msg_Uint_1
:= Bounds_Lo
;
1308 Error_Msg_N
("minimum allowed choice value is^", Enode
);
1310 Error_Msg_Name_1
:= Choice_Image
(Bounds_Lo
, Bounds_Type
);
1311 Error_Msg_N
("minimum allowed choice value is%", Enode
);
1315 -- Check for high bound out of range
1317 if Hi_Val
> Bounds_Hi
then
1319 -- If the choice is an entity name, then it is a type, and we
1320 -- want to post the message on the reference to this entity.
1321 -- Otherwise post it on the upper bound of the range.
1323 if Is_Entity_Name
(Choice
) then
1329 -- Specialize message for integer/enum type
1331 if Is_Integer_Type
(Bounds_Type
) then
1332 Error_Msg_Uint_1
:= Bounds_Hi
;
1333 Error_Msg_N
("maximum allowed choice value is^", Enode
);
1335 Error_Msg_Name_1
:= Choice_Image
(Bounds_Hi
, Bounds_Type
);
1336 Error_Msg_N
("maximum allowed choice value is%", Enode
);
1340 -- Collect bounds in the list
1342 -- Note: we still store the bounds, even if they are out of range,
1343 -- since this may prevent unnecessary cascaded errors for values
1344 -- that are covered by such an excessive range.
1347 new Link
'(Val => (Lo, Hi, Choice), Nxt => Choice_List);
1348 Num_Choices := Num_Choices + 1;
1351 -- Start of processing for Check_Choices
1355 Others_Present := False;
1357 -- If Subtyp is not a discrete type or there was some other error,
1358 -- then don't try any semantic checking on the choices since we have
1361 if not Is_Discrete_Type (Subtyp) or else Subtyp = Any_Type then
1365 -- If Subtyp is not a static subtype Ada 95 requires then we use the
1366 -- bounds of its base type to determine the values covered by the
1367 -- discrete choices.
1369 -- In Ada 2012, if the subtype has a non-static predicate the full
1370 -- range of the base type must be covered as well.
1372 if Is_OK_Static_Subtype (Subtyp) then
1373 if not Has_Predicates (Subtyp)
1374 or else Present (Static_Predicate (Subtyp))
1376 Bounds_Type := Subtyp;
1378 Bounds_Type := Choice_Type;
1382 Bounds_Type := Choice_Type;
1385 -- Obtain static bounds of type, unless this is a generic formal
1386 -- discrete type for which all choices will be non-static.
1388 if not Is_Generic_Type (Root_Type (Bounds_Type))
1389 or else Ekind (Bounds_Type) /= E_Enumeration_Type
1391 Bounds_Lo := Expr_Value (Type_Low_Bound (Bounds_Type));
1392 Bounds_Hi := Expr_Value (Type_High_Bound (Bounds_Type));
1395 if Choice_Type = Universal_Integer then
1396 Expected_Type := Any_Integer;
1398 Expected_Type := Choice_Type;
1401 -- Now loop through the case alternatives or record variants
1403 Alt := First (Alternatives);
1404 while Present (Alt) loop
1406 -- If pragma, just analyze it
1408 if Nkind (Alt) = N_Pragma then
1411 -- Otherwise we have an alternative. In most cases the semantic
1412 -- processing leaves the list of choices unchanged
1414 -- Check each choice against its base type
1417 Choice := First (Discrete_Choices (Alt));
1418 while Present (Choice) loop
1419 Kind := Nkind (Choice);
1421 -- Choice is a Range
1424 or else (Kind = N_Attribute_Reference
1425 and then Attribute_Name (Choice) = Name_Range)
1427 Check (Choice, Low_Bound (Choice), High_Bound (Choice));
1429 -- Choice is a subtype name
1431 elsif Is_Entity_Name (Choice)
1432 and then Is_Type (Entity (Choice))
1434 -- Check for inappropriate type
1436 if not Covers (Expected_Type, Etype (Choice)) then
1437 Wrong_Type (Choice, Choice_Type);
1439 -- Type is OK, so check further
1442 E := Entity (Choice);
1444 -- Case of predicated subtype
1446 if Has_Predicates (E) then
1448 -- Use of non-static predicate is an error
1450 if not Is_Discrete_Type (E)
1451 or else No (Static_Predicate (E))
1453 Bad_Predicated_Subtype_Use
1454 ("cannot use subtype& with non-static "
1455 & "predicate as case alternative",
1456 Choice, E, Suggest_Static => True);
1458 -- Static predicate case
1466 -- Loop through entries in predicate list,
1467 -- checking each entry. Note that if the
1468 -- list is empty, corresponding to a False
1469 -- predicate, then no choices are checked.
1471 P := First (Static_Predicate (E));
1472 while Present (P) loop
1474 Set_Sloc (C, Sloc (Choice));
1475 Check (C, Low_Bound (C), High_Bound (C));
1480 Set_Has_SP_Choice (Alt);
1483 -- Not predicated subtype case
1485 elsif not Is_Static_Subtype (E) then
1486 Process_Non_Static_Choice (Choice);
1489 (Choice, Type_Low_Bound (E), Type_High_Bound (E));
1493 -- Choice is a subtype indication
1495 elsif Kind = N_Subtype_Indication then
1496 Resolve_Discrete_Subtype_Indication
1497 (Choice, Expected_Type);
1499 if Etype (Choice) /= Any_Type then
1501 C : constant Node_Id := Constraint (Choice);
1502 R : constant Node_Id := Range_Expression (C);
1503 L : constant Node_Id := Low_Bound (R);
1504 H : constant Node_Id := High_Bound (R);
1507 E := Entity (Subtype_Mark (Choice));
1509 if not Is_Static_Subtype (E) then
1510 Process_Non_Static_Choice (Choice);
1513 if Is_OK_Static_Expression (L)
1515 Is_OK_Static_Expression (H)
1517 if Expr_Value (L) > Expr_Value (H) then
1518 Process_Empty_Choice (Choice);
1520 if Is_Out_Of_Range (L, E) then
1521 Apply_Compile_Time_Constraint_Error
1522 (L, "static value out of range",
1523 CE_Range_Check_Failed);
1526 if Is_Out_Of_Range (H, E) then
1527 Apply_Compile_Time_Constraint_Error
1528 (H, "static value out of range",
1529 CE_Range_Check_Failed);
1534 Check (Choice, L, H);
1539 -- The others choice is only allowed for the last
1540 -- alternative and as its only choice.
1542 elsif Kind = N_Others_Choice then
1543 if not (Choice = First (Discrete_Choices (Alt))
1544 and then Choice = Last (Discrete_Choices (Alt))
1545 and then Alt = Last (Alternatives))
1548 ("the choice OTHERS must appear alone and last",
1553 Others_Present := True;
1554 Others_Choice := Choice;
1556 -- Only other possibility is an expression
1559 Check (Choice, Choice, Choice);
1562 -- Move to next choice
1567 Process_Associated_Node (Alt);
1573 -- Now we can create the Choice_Table, since we know how long
1574 -- it needs to be so we can allocate exactly the right length.
1577 Choice_Table : Choice_Table_Type (0 .. Num_Choices);
1580 -- Now copy the items we collected in the linked list into this
1581 -- newly allocated table (leave entry 0 unused for sorting).
1586 for J in 1 .. Num_Choices loop
1588 Choice_List := T.Nxt;
1589 Choice_Table (J) := T.Val;
1598 Others_Present or else (Choice_Type = Universal_Integer),
1601 -- If no others choice we are all done, otherwise we have one more
1602 -- step, which is to set the Others_Discrete_Choices field of the
1603 -- others choice (to contain all otherwise unspecified choices).
1604 -- Skip this if CE is known to be raised.
1606 if Others_Present and not Raises_CE then
1607 Expand_Others_Choice
1608 (Case_Table => Choice_Table,
1609 Others_Choice => Others_Choice,
1610 Choice_Type => Bounds_Type);
1615 end Generic_Check_Choices;