Daily bump.
[official-gcc.git] / gcc / ada / sem_case.adb
blob5433bb1de9a73ec9d43764c836c62cc8a2af8b6f
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-2007, 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_Eval; use Sem_Eval;
35 with Sem_Res; use Sem_Res;
36 with Sem_Util; use Sem_Util;
37 with Sem_Type; use Sem_Type;
38 with Snames; use Snames;
39 with Stand; use Stand;
40 with Sinfo; use Sinfo;
41 with Tbuild; use Tbuild;
42 with Uintp; use Uintp;
44 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
46 package body Sem_Case is
48 -----------------------
49 -- Local Subprograms --
50 -----------------------
52 type Sort_Choice_Table_Type is array (Nat range <>) of Choice_Bounds;
53 -- This new array type is used as the actual table type for sorting
54 -- discrete choices. The reason for not using Choice_Table_Type, is that
55 -- in Sort_Choice_Table_Type we reserve entry 0 for the sorting algortim
56 -- (this is not absolutely necessary but it makes the code more
57 -- efficient).
59 procedure Check_Choices
60 (Choice_Table : in out Sort_Choice_Table_Type;
61 Bounds_Type : Entity_Id;
62 Others_Present : Boolean;
63 Msg_Sloc : Source_Ptr);
64 -- This is the procedure which verifies that a set of case alternatives
65 -- or record variant choices has no duplicates, and covers the range
66 -- specified by Bounds_Type. Choice_Table contains the discrete choices
67 -- to check. These must start at position 1.
68 -- Furthermore Choice_Table (0) must exist. This element is used by
69 -- the sorting algorithm as a temporary. Others_Present is a flag
70 -- indicating whether or not an Others choice is present. Finally
71 -- Msg_Sloc gives the source location of the construct containing the
72 -- choices in the Choice_Table.
74 function Choice_Image (Value : Uint; Ctype : Entity_Id) return Name_Id;
75 -- Given a Pos value of enumeration type Ctype, returns the name
76 -- ID of an appropriate string to be used in error message output.
78 procedure Expand_Others_Choice
79 (Case_Table : Choice_Table_Type;
80 Others_Choice : Node_Id;
81 Choice_Type : Entity_Id);
82 -- The case table is the table generated by a call to Analyze_Choices
83 -- (with just 1 .. Last_Choice entries present). Others_Choice is a
84 -- pointer to the N_Others_Choice node (this routine is only called if
85 -- an others choice is present), and Choice_Type is the discrete type
86 -- of the bounds. The effect of this call is to analyze the cases and
87 -- determine the set of values covered by others. This choice list is
88 -- set in the Others_Discrete_Choices field of the N_Others_Choice node.
90 -------------------
91 -- Check_Choices --
92 -------------------
94 procedure Check_Choices
95 (Choice_Table : in out Sort_Choice_Table_Type;
96 Bounds_Type : Entity_Id;
97 Others_Present : Boolean;
98 Msg_Sloc : Source_Ptr)
100 function Lt_Choice (C1, C2 : Natural) return Boolean;
101 -- Comparison routine for comparing Choice_Table entries. Use the lower
102 -- bound of each Choice as the key.
104 procedure Move_Choice (From : Natural; To : Natural);
105 -- Move routine for sorting the Choice_Table
107 procedure Issue_Msg (Value1 : Node_Id; Value2 : Node_Id);
108 procedure Issue_Msg (Value1 : Node_Id; Value2 : Uint);
109 procedure Issue_Msg (Value1 : Uint; Value2 : Node_Id);
110 procedure Issue_Msg (Value1 : Uint; Value2 : Uint);
111 -- Issue an error message indicating that there are missing choices,
112 -- followed by the image of the missing choices themselves which lie
113 -- between Value1 and Value2 inclusive.
115 ---------------
116 -- Issue_Msg --
117 ---------------
119 procedure Issue_Msg (Value1 : Node_Id; Value2 : Node_Id) is
120 begin
121 Issue_Msg (Expr_Value (Value1), Expr_Value (Value2));
122 end Issue_Msg;
124 procedure Issue_Msg (Value1 : Node_Id; Value2 : Uint) is
125 begin
126 Issue_Msg (Expr_Value (Value1), Value2);
127 end Issue_Msg;
129 procedure Issue_Msg (Value1 : Uint; Value2 : Node_Id) is
130 begin
131 Issue_Msg (Value1, Expr_Value (Value2));
132 end Issue_Msg;
134 procedure Issue_Msg (Value1 : Uint; Value2 : Uint) is
135 begin
136 -- In some situations, we call this with a null range, and
137 -- obviously we don't want to complain in this case!
139 if Value1 > Value2 then
140 return;
141 end if;
143 -- Case of only one value that is missing
145 if Value1 = Value2 then
146 if Is_Integer_Type (Bounds_Type) then
147 Error_Msg_Uint_1 := Value1;
148 Error_Msg ("missing case value: ^!", Msg_Sloc);
149 else
150 Error_Msg_Name_1 := Choice_Image (Value1, Bounds_Type);
151 Error_Msg ("missing case value: %!", Msg_Sloc);
152 end if;
154 -- More than one choice value, so print range of values
156 else
157 if Is_Integer_Type (Bounds_Type) then
158 Error_Msg_Uint_1 := Value1;
159 Error_Msg_Uint_2 := Value2;
160 Error_Msg ("missing case values: ^ .. ^!", Msg_Sloc);
161 else
162 Error_Msg_Name_1 := Choice_Image (Value1, Bounds_Type);
163 Error_Msg_Name_2 := Choice_Image (Value2, Bounds_Type);
164 Error_Msg ("missing case values: % .. %!", Msg_Sloc);
165 end if;
166 end if;
167 end Issue_Msg;
169 ---------------
170 -- Lt_Choice --
171 ---------------
173 function Lt_Choice (C1, C2 : Natural) return Boolean is
174 begin
175 return
176 Expr_Value (Choice_Table (Nat (C1)).Lo)
178 Expr_Value (Choice_Table (Nat (C2)).Lo);
179 end Lt_Choice;
181 -----------------
182 -- Move_Choice --
183 -----------------
185 procedure Move_Choice (From : Natural; To : Natural) is
186 begin
187 Choice_Table (Nat (To)) := Choice_Table (Nat (From));
188 end Move_Choice;
190 -- Variables local to Check_Choices
192 Choice : Node_Id;
193 Bounds_Lo : constant Node_Id := Type_Low_Bound (Bounds_Type);
194 Bounds_Hi : constant Node_Id := Type_High_Bound (Bounds_Type);
196 Prev_Choice : Node_Id;
198 Hi : Uint;
199 Lo : Uint;
200 Prev_Hi : Uint;
202 -- Start processing for Check_Choices
204 begin
205 -- Choice_Table must start at 0 which is an unused location used
206 -- by the sorting algorithm. However the first valid position for
207 -- a discrete choice is 1.
209 pragma Assert (Choice_Table'First = 0);
211 if Choice_Table'Last = 0 then
212 if not Others_Present then
213 Issue_Msg (Bounds_Lo, Bounds_Hi);
214 end if;
215 return;
216 end if;
218 Sort
219 (Positive (Choice_Table'Last),
220 Move_Choice'Unrestricted_Access,
221 Lt_Choice'Unrestricted_Access);
223 Lo := Expr_Value (Choice_Table (1).Lo);
224 Hi := Expr_Value (Choice_Table (1).Hi);
225 Prev_Hi := Hi;
227 if not Others_Present and then Expr_Value (Bounds_Lo) < Lo then
228 Issue_Msg (Bounds_Lo, Lo - 1);
229 end if;
231 for J in 2 .. Choice_Table'Last loop
232 Lo := Expr_Value (Choice_Table (J).Lo);
233 Hi := Expr_Value (Choice_Table (J).Hi);
235 if Lo <= Prev_Hi then
236 Prev_Choice := Choice_Table (J - 1).Node;
237 Choice := Choice_Table (J).Node;
239 if Sloc (Prev_Choice) <= Sloc (Choice) then
240 Error_Msg_Sloc := Sloc (Prev_Choice);
241 Error_Msg_N ("duplication of choice value#", Choice);
242 else
243 Error_Msg_Sloc := Sloc (Choice);
244 Error_Msg_N ("duplication of choice value#", Prev_Choice);
245 end if;
247 elsif not Others_Present and then Lo /= Prev_Hi + 1 then
248 Issue_Msg (Prev_Hi + 1, Lo - 1);
249 end if;
251 Prev_Hi := Hi;
252 end loop;
254 if not Others_Present and then Expr_Value (Bounds_Hi) > Hi then
255 Issue_Msg (Hi + 1, Bounds_Hi);
256 end if;
257 end Check_Choices;
259 ------------------
260 -- Choice_Image --
261 ------------------
263 function Choice_Image (Value : Uint; Ctype : Entity_Id) return Name_Id is
264 Rtp : constant Entity_Id := Root_Type (Ctype);
265 Lit : Entity_Id;
266 C : Int;
268 begin
269 -- For character, or wide [wide] character. If 7-bit ASCII graphic
270 -- range, then build and return appropriate character literal name
272 if Rtp = Standard_Character
273 or else Rtp = Standard_Wide_Character
274 or else Rtp = Standard_Wide_Wide_Character
275 then
276 C := UI_To_Int (Value);
278 if C in 16#20# .. 16#7E# then
279 Set_Character_Literal_Name (Char_Code (UI_To_Int (Value)));
280 return Name_Find;
281 end if;
283 -- For user defined enumeration type, find enum/char literal
285 else
286 Lit := First_Literal (Rtp);
288 for J in 1 .. UI_To_Int (Value) loop
289 Next_Literal (Lit);
290 end loop;
292 -- If enumeration literal, just return its value
294 if Nkind (Lit) = N_Defining_Identifier then
295 return Chars (Lit);
297 -- For character literal, get the name and use it if it is
298 -- for a 7-bit ASCII graphic character in 16#20#..16#7E#.
300 else
301 Get_Decoded_Name_String (Chars (Lit));
303 if Name_Len = 3
304 and then Name_Buffer (2) in
305 Character'Val (16#20#) .. Character'Val (16#7E#)
306 then
307 return Chars (Lit);
308 end if;
309 end if;
310 end if;
312 -- If we fall through, we have a character literal which is not in
313 -- the 7-bit ASCII graphic set. For such cases, we construct the
314 -- name "type'val(nnn)" where type is the choice type, and nnn is
315 -- the pos value passed as an argument to Choice_Image.
317 Get_Name_String (Chars (First_Subtype (Ctype)));
318 Name_Len := Name_Len + 1;
319 Name_Buffer (Name_Len) := ''';
320 Name_Len := Name_Len + 1;
321 Name_Buffer (Name_Len) := 'v';
322 Name_Len := Name_Len + 1;
323 Name_Buffer (Name_Len) := 'a';
324 Name_Len := Name_Len + 1;
325 Name_Buffer (Name_Len) := 'l';
326 Name_Len := Name_Len + 1;
327 Name_Buffer (Name_Len) := '(';
329 UI_Image (Value);
331 for J in 1 .. UI_Image_Length loop
332 Name_Len := Name_Len + 1;
333 Name_Buffer (Name_Len) := UI_Image_Buffer (J);
334 end loop;
336 Name_Len := Name_Len + 1;
337 Name_Buffer (Name_Len) := ')';
338 return Name_Find;
339 end Choice_Image;
341 --------------------------
342 -- Expand_Others_Choice --
343 --------------------------
345 procedure Expand_Others_Choice
346 (Case_Table : Choice_Table_Type;
347 Others_Choice : Node_Id;
348 Choice_Type : Entity_Id)
350 Loc : constant Source_Ptr := Sloc (Others_Choice);
351 Choice_List : constant List_Id := New_List;
352 Choice : Node_Id;
353 Exp_Lo : Node_Id;
354 Exp_Hi : Node_Id;
355 Hi : Uint;
356 Lo : Uint;
357 Previous_Hi : Uint;
359 function Build_Choice (Value1, Value2 : Uint) return Node_Id;
360 -- Builds a node representing the missing choices given by the
361 -- Value1 and Value2. A N_Range node is built if there is more than
362 -- one literal value missing. Otherwise a single N_Integer_Literal,
363 -- N_Identifier or N_Character_Literal is built depending on what
364 -- Choice_Type is.
366 function Lit_Of (Value : Uint) return Node_Id;
367 -- Returns the Node_Id for the enumeration literal corresponding to the
368 -- position given by Value within the enumeration type Choice_Type.
370 ------------------
371 -- Build_Choice --
372 ------------------
374 function Build_Choice (Value1, Value2 : Uint) return Node_Id is
375 Lit_Node : Node_Id;
376 Lo, Hi : Node_Id;
378 begin
379 -- If there is only one choice value missing between Value1 and
380 -- Value2, build an integer or enumeration literal to represent it.
382 if (Value2 - Value1) = 0 then
383 if Is_Integer_Type (Choice_Type) then
384 Lit_Node := Make_Integer_Literal (Loc, Value1);
385 Set_Etype (Lit_Node, Choice_Type);
386 else
387 Lit_Node := Lit_Of (Value1);
388 end if;
390 -- Otherwise is more that one choice value that is missing between
391 -- Value1 and Value2, therefore build a N_Range node of either
392 -- integer or enumeration literals.
394 else
395 if Is_Integer_Type (Choice_Type) then
396 Lo := Make_Integer_Literal (Loc, Value1);
397 Set_Etype (Lo, Choice_Type);
398 Hi := Make_Integer_Literal (Loc, Value2);
399 Set_Etype (Hi, Choice_Type);
400 Lit_Node :=
401 Make_Range (Loc,
402 Low_Bound => Lo,
403 High_Bound => Hi);
405 else
406 Lit_Node :=
407 Make_Range (Loc,
408 Low_Bound => Lit_Of (Value1),
409 High_Bound => Lit_Of (Value2));
410 end if;
411 end if;
413 return Lit_Node;
414 end Build_Choice;
416 ------------
417 -- Lit_Of --
418 ------------
420 function Lit_Of (Value : Uint) return Node_Id is
421 Lit : Entity_Id;
423 begin
424 -- In the case where the literal is of type Character, there needs
425 -- to be some special handling since there is no explicit chain
426 -- of literals to search. Instead, a N_Character_Literal node
427 -- is created with the appropriate Char_Code and Chars fields.
429 if Root_Type (Choice_Type) = Standard_Character
430 or else
431 Root_Type (Choice_Type) = Standard_Wide_Character
432 or else
433 Root_Type (Choice_Type) = Standard_Wide_Wide_Character
434 then
435 Set_Character_Literal_Name (Char_Code (UI_To_Int (Value)));
436 Lit := New_Node (N_Character_Literal, Loc);
437 Set_Chars (Lit, Name_Find);
438 Set_Char_Literal_Value (Lit, Value);
439 Set_Etype (Lit, Choice_Type);
440 Set_Is_Static_Expression (Lit, True);
441 return Lit;
443 -- Otherwise, iterate through the literals list of Choice_Type
444 -- "Value" number of times until the desired literal is reached
445 -- and then return an occurrence of it.
447 else
448 Lit := First_Literal (Choice_Type);
449 for J in 1 .. UI_To_Int (Value) loop
450 Next_Literal (Lit);
451 end loop;
453 return New_Occurrence_Of (Lit, Loc);
454 end if;
455 end Lit_Of;
457 -- Start of processing for Expand_Others_Choice
459 begin
460 if Case_Table'Length = 0 then
462 -- Special case: only an others case is present.
463 -- The others case covers the full range of the type.
465 if Is_Static_Subtype (Choice_Type) then
466 Choice := New_Occurrence_Of (Choice_Type, Loc);
467 else
468 Choice := New_Occurrence_Of (Base_Type (Choice_Type), Loc);
469 end if;
471 Set_Others_Discrete_Choices (Others_Choice, New_List (Choice));
472 return;
473 end if;
475 -- Establish the bound values for the choice depending upon whether
476 -- the type of the case statement is static or not.
478 if Is_OK_Static_Subtype (Choice_Type) then
479 Exp_Lo := Type_Low_Bound (Choice_Type);
480 Exp_Hi := Type_High_Bound (Choice_Type);
481 else
482 Exp_Lo := Type_Low_Bound (Base_Type (Choice_Type));
483 Exp_Hi := Type_High_Bound (Base_Type (Choice_Type));
484 end if;
486 Lo := Expr_Value (Case_Table (Case_Table'First).Lo);
487 Hi := Expr_Value (Case_Table (Case_Table'First).Hi);
488 Previous_Hi := Expr_Value (Case_Table (Case_Table'First).Hi);
490 -- Build the node for any missing choices that are smaller than any
491 -- explicit choices given in the case.
493 if Expr_Value (Exp_Lo) < Lo then
494 Append (Build_Choice (Expr_Value (Exp_Lo), Lo - 1), Choice_List);
495 end if;
497 -- Build the nodes representing any missing choices that lie between
498 -- the explicit ones given in the case.
500 for J in Case_Table'First + 1 .. Case_Table'Last loop
501 Lo := Expr_Value (Case_Table (J).Lo);
502 Hi := Expr_Value (Case_Table (J).Hi);
504 if Lo /= (Previous_Hi + 1) then
505 Append_To (Choice_List, Build_Choice (Previous_Hi + 1, Lo - 1));
506 end if;
508 Previous_Hi := Hi;
509 end loop;
511 -- Build the node for any missing choices that are greater than any
512 -- explicit choices given in the case.
514 if Expr_Value (Exp_Hi) > Hi then
515 Append (Build_Choice (Hi + 1, Expr_Value (Exp_Hi)), Choice_List);
516 end if;
518 Set_Others_Discrete_Choices (Others_Choice, Choice_List);
520 -- Warn on null others list if warning option set
522 if Warn_On_Redundant_Constructs
523 and then Comes_From_Source (Others_Choice)
524 and then Is_Empty_List (Choice_List)
525 then
526 Error_Msg_N ("?OTHERS choice is redundant", Others_Choice);
527 Error_Msg_N ("\previous choices cover all values", Others_Choice);
528 end if;
529 end Expand_Others_Choice;
531 -----------
532 -- No_OP --
533 -----------
535 procedure No_OP (C : Node_Id) is
536 pragma Warnings (Off, C);
538 begin
539 null;
540 end No_OP;
542 --------------------------------
543 -- Generic_Choices_Processing --
544 --------------------------------
546 package body Generic_Choices_Processing is
548 ---------------------
549 -- Analyze_Choices --
550 ---------------------
552 procedure Analyze_Choices
553 (N : Node_Id;
554 Subtyp : Entity_Id;
555 Choice_Table : out Choice_Table_Type;
556 Last_Choice : out Nat;
557 Raises_CE : out Boolean;
558 Others_Present : out Boolean)
560 pragma Assert (Choice_Table'First = 1);
562 E : Entity_Id;
564 Enode : Node_Id;
565 -- This is where we post error messages for bounds out of range
567 Nb_Choices : constant Nat := Choice_Table'Length;
568 Sort_Choice_Table : Sort_Choice_Table_Type (0 .. Nb_Choices);
570 Choice_Type : constant Entity_Id := Base_Type (Subtyp);
571 -- The actual type against which the discrete choices are
572 -- resolved. Note that this type is always the base type not the
573 -- subtype of the ruling expression, index or discriminant.
575 Bounds_Type : Entity_Id;
576 -- The type from which are derived the bounds of the values
577 -- covered by the discrete choices (see 3.8.1 (4)). If a discrete
578 -- choice specifies a value outside of these bounds we have an error.
580 Bounds_Lo : Uint;
581 Bounds_Hi : Uint;
582 -- The actual bounds of the above type
584 Expected_Type : Entity_Id;
585 -- The expected type of each choice. Equal to Choice_Type, except
586 -- if the expression is universal, in which case the choices can
587 -- be of any integer type.
589 Alt : Node_Id;
590 -- A case statement alternative or a variant in a record type
591 -- declaration
593 Choice : Node_Id;
594 Kind : Node_Kind;
595 -- The node kind of the current Choice
597 Others_Choice : Node_Id := Empty;
598 -- Remember others choice if it is present (empty otherwise)
600 procedure Check (Choice : Node_Id; Lo, Hi : Node_Id);
601 -- Checks the validity of the bounds of a choice. When the bounds
602 -- are static and no error occurred the bounds are entered into
603 -- the choices table so that they can be sorted later on.
605 -----------
606 -- Check --
607 -----------
609 procedure Check (Choice : Node_Id; Lo, Hi : Node_Id) is
610 Lo_Val : Uint;
611 Hi_Val : Uint;
613 begin
614 -- First check if an error was already detected on either bounds
616 if Etype (Lo) = Any_Type or else Etype (Hi) = Any_Type then
617 return;
619 -- Do not insert non static choices in the table to be sorted
621 elsif not Is_Static_Expression (Lo)
622 or else not Is_Static_Expression (Hi)
623 then
624 Process_Non_Static_Choice (Choice);
625 return;
627 -- Ignore range which raise constraint error
629 elsif Raises_Constraint_Error (Lo)
630 or else Raises_Constraint_Error (Hi)
631 then
632 Raises_CE := True;
633 return;
635 -- Otherwise we have an OK static choice
637 else
638 Lo_Val := Expr_Value (Lo);
639 Hi_Val := Expr_Value (Hi);
641 -- Do not insert null ranges in the choices table
643 if Lo_Val > Hi_Val then
644 Process_Empty_Choice (Choice);
645 return;
646 end if;
647 end if;
649 -- Check for low bound out of range
651 if Lo_Val < Bounds_Lo then
653 -- If the choice is an entity name, then it is a type, and
654 -- we want to post the message on the reference to this
655 -- entity. Otherwise we want to post it on the lower bound
656 -- of the range.
658 if Is_Entity_Name (Choice) then
659 Enode := Choice;
660 else
661 Enode := Lo;
662 end if;
664 -- Specialize message for integer/enum type
666 if Is_Integer_Type (Bounds_Type) then
667 Error_Msg_Uint_1 := Bounds_Lo;
668 Error_Msg_N ("minimum allowed choice value is^", Enode);
669 else
670 Error_Msg_Name_1 := Choice_Image (Bounds_Lo, Bounds_Type);
671 Error_Msg_N ("minimum allowed choice value is%", Enode);
672 end if;
673 end if;
675 -- Check for high bound out of range
677 if Hi_Val > Bounds_Hi then
679 -- If the choice is an entity name, then it is a type, and
680 -- we want to post the message on the reference to this
681 -- entity. Otherwise we want to post it on the upper bound
682 -- of the range.
684 if Is_Entity_Name (Choice) then
685 Enode := Choice;
686 else
687 Enode := Hi;
688 end if;
690 -- Specialize message for integer/enum type
692 if Is_Integer_Type (Bounds_Type) then
693 Error_Msg_Uint_1 := Bounds_Hi;
694 Error_Msg_N ("maximum allowed choice value is^", Enode);
695 else
696 Error_Msg_Name_1 := Choice_Image (Bounds_Hi, Bounds_Type);
697 Error_Msg_N ("maximum allowed choice value is%", Enode);
698 end if;
699 end if;
701 -- Store bounds in the table
703 -- Note: we still store the bounds, even if they are out of
704 -- range, since this may prevent unnecessary cascaded errors
705 -- for values that are covered by such an excessive range.
707 Last_Choice := Last_Choice + 1;
708 Sort_Choice_Table (Last_Choice).Lo := Lo;
709 Sort_Choice_Table (Last_Choice).Hi := Hi;
710 Sort_Choice_Table (Last_Choice).Node := Choice;
711 end Check;
713 -- Start of processing for Analyze_Choices
715 begin
716 Last_Choice := 0;
717 Raises_CE := False;
718 Others_Present := False;
720 -- If Subtyp is not a static subtype Ada 95 requires then we use
721 -- the bounds of its base type to determine the values covered by
722 -- the discrete choices.
724 if Is_OK_Static_Subtype (Subtyp) then
725 Bounds_Type := Subtyp;
726 else
727 Bounds_Type := Choice_Type;
728 end if;
730 -- Obtain static bounds of type, unless this is a generic formal
731 -- discrete type for which all choices will be non-static.
733 if not Is_Generic_Type (Root_Type (Bounds_Type))
734 or else Ekind (Bounds_Type) /= E_Enumeration_Type
735 then
736 Bounds_Lo := Expr_Value (Type_Low_Bound (Bounds_Type));
737 Bounds_Hi := Expr_Value (Type_High_Bound (Bounds_Type));
738 end if;
740 if Choice_Type = Universal_Integer then
741 Expected_Type := Any_Integer;
742 else
743 Expected_Type := Choice_Type;
744 end if;
746 -- Now loop through the case alternatives or record variants
748 Alt := First (Get_Alternatives (N));
749 while Present (Alt) loop
751 -- If pragma, just analyze it
753 if Nkind (Alt) = N_Pragma then
754 Analyze (Alt);
756 -- Otherwise check each choice against its base type
758 else
759 Choice := First (Get_Choices (Alt));
761 while Present (Choice) loop
762 Analyze (Choice);
763 Kind := Nkind (Choice);
765 -- Choice is a Range
767 if Kind = N_Range
768 or else (Kind = N_Attribute_Reference
769 and then Attribute_Name (Choice) = Name_Range)
770 then
771 Resolve (Choice, Expected_Type);
772 Check (Choice, Low_Bound (Choice), High_Bound (Choice));
774 -- Choice is a subtype name
776 elsif Is_Entity_Name (Choice)
777 and then Is_Type (Entity (Choice))
778 then
779 if not Covers (Expected_Type, Etype (Choice)) then
780 Wrong_Type (Choice, Choice_Type);
782 else
783 E := Entity (Choice);
785 if not Is_Static_Subtype (E) then
786 Process_Non_Static_Choice (Choice);
787 else
788 Check
789 (Choice, Type_Low_Bound (E), Type_High_Bound (E));
790 end if;
791 end if;
793 -- Choice is a subtype indication
795 elsif Kind = N_Subtype_Indication then
796 Resolve_Discrete_Subtype_Indication
797 (Choice, Expected_Type);
799 if Etype (Choice) /= Any_Type then
800 declare
801 C : constant Node_Id := Constraint (Choice);
802 R : constant Node_Id := Range_Expression (C);
803 L : constant Node_Id := Low_Bound (R);
804 H : constant Node_Id := High_Bound (R);
806 begin
807 E := Entity (Subtype_Mark (Choice));
809 if not Is_Static_Subtype (E) then
810 Process_Non_Static_Choice (Choice);
812 else
813 if Is_OK_Static_Expression (L)
814 and then Is_OK_Static_Expression (H)
815 then
816 if Expr_Value (L) > Expr_Value (H) then
817 Process_Empty_Choice (Choice);
818 else
819 if Is_Out_Of_Range (L, E) then
820 Apply_Compile_Time_Constraint_Error
821 (L, "static value out of range",
822 CE_Range_Check_Failed);
823 end if;
825 if Is_Out_Of_Range (H, E) then
826 Apply_Compile_Time_Constraint_Error
827 (H, "static value out of range",
828 CE_Range_Check_Failed);
829 end if;
830 end if;
831 end if;
833 Check (Choice, L, H);
834 end if;
835 end;
836 end if;
838 -- The others choice is only allowed for the last
839 -- alternative and as its only choice.
841 elsif Kind = N_Others_Choice then
842 if not (Choice = First (Get_Choices (Alt))
843 and then Choice = Last (Get_Choices (Alt))
844 and then Alt = Last (Get_Alternatives (N)))
845 then
846 Error_Msg_N
847 ("the choice OTHERS must appear alone and last",
848 Choice);
849 return;
850 end if;
852 Others_Present := True;
853 Others_Choice := Choice;
855 -- Only other possibility is an expression
857 else
858 Resolve (Choice, Expected_Type);
859 Check (Choice, Choice, Choice);
860 end if;
862 Next (Choice);
863 end loop;
865 Process_Associated_Node (Alt);
866 end if;
868 Next (Alt);
869 end loop;
871 Check_Choices
872 (Sort_Choice_Table (0 .. Last_Choice),
873 Bounds_Type,
874 Others_Present or else (Choice_Type = Universal_Integer),
875 Sloc (N));
877 -- Now copy the sorted discrete choices
879 for J in 1 .. Last_Choice loop
880 Choice_Table (Choice_Table'First - 1 + J) := Sort_Choice_Table (J);
881 end loop;
883 -- If no others choice we are all done, otherwise we have one more
884 -- step, which is to set the Others_Discrete_Choices field of the
885 -- others choice (to contain all otherwise unspecified choices).
886 -- Skip this if CE is known to be raised.
888 if Others_Present and not Raises_CE then
889 Expand_Others_Choice
890 (Case_Table => Choice_Table (1 .. Last_Choice),
891 Others_Choice => Others_Choice,
892 Choice_Type => Bounds_Type);
893 end if;
894 end Analyze_Choices;
896 -----------------------
897 -- Number_Of_Choices --
898 -----------------------
900 function Number_Of_Choices (N : Node_Id) return Nat is
901 Alt : Node_Id;
902 -- A case statement alternative or a record variant
904 Choice : Node_Id;
905 Count : Nat := 0;
907 begin
908 if No (Get_Alternatives (N)) then
909 return 0;
910 end if;
912 Alt := First_Non_Pragma (Get_Alternatives (N));
913 while Present (Alt) loop
915 Choice := First (Get_Choices (Alt));
916 while Present (Choice) loop
917 if Nkind (Choice) /= N_Others_Choice then
918 Count := Count + 1;
919 end if;
921 Next (Choice);
922 end loop;
924 Next_Non_Pragma (Alt);
925 end loop;
927 return Count;
928 end Number_Of_Choices;
930 end Generic_Choices_Processing;
932 end Sem_Case;