1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                             S E M _ C A S E                              --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1996-2020, 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------------------------------------------------------------------------------
25
26--  Package containing the routines to process a list of discrete choices.
27--  Such lists can occur in two different constructs: case statements and
28--  record variants. We have factorized what used to be two very similar
29--  sets of routines in one place. These are not currently used for the
30--  aggregate case, since issues with nested aggregates make that case
31--  substantially different.
32
33--  The following processing is required for such cases:
34
35--    1. Analysis of names of subtypes, constants, expressions appearing within
36--    the choices. This must be done when the construct is encountered to get
37--    proper visibility of names.
38
39--    2. Checking for semantic correctness of the choices. A lot of this could
40--    be done at the time when the construct is encountered, but not all, since
41--    in the case of variants, statically predicated subtypes won't be frozen
42--    (and the choice sets known) till the enclosing record type is frozen. So
43--    at least the check for no overlaps and covering the range must be delayed
44--    till the freeze point in this case.
45
46--    3. Set the Others_Discrete_Choices list for an others choice. This is
47--    used in various ways, e.g. to construct the disriminant checking function
48--    for the case of a variant with an others choice.
49
50--    4. In the case of static predicates, we need to expand out choices that
51--    correspond to the predicate for the back end. This expansion destroys
52--    the list of choices, so it should be delayed to expansion time.
53
54--  Step 1 is performed by the generic procedure Analyze_Choices, which is
55--  called when the variant record or case statement/expression is first
56--  encountered.
57
58--  Step 2 is performed by the generic procedure Check_Choices. We decide to
59--  do all semantic checking in that step, since as noted above some of this
60--  has to be deferred to the freeze point in any case for variants. For case
61--  statements and expressions, this procedure can be called at the time the
62--  case construct is encountered (after calling Analyze_Choices).
63
64--  Step 3 is also performed by Check_Choices, since we need the static ranges
65--  for predicated subtypes to accurately construct this.
66
67--  Step 4 is performed by the procedure Expand_Static_Predicates_In_Choices.
68--  For case statements, this call only happens during expansion. The reason
69--  we do the expansion unconditionally for variants is that other processing,
70--  for example for aggregates, relies on having a complete list of choices.
71
72--  Historical note: We used to perform all four of these functions at once in
73--  a single procedure called Analyze_Choices. This routine was called at the
74--  time the construct was first encountered. That seemed to work OK up to Ada
75--  2005, but the introduction of statically predicated subtypes with delayed
76--  evaluation of the static ranges made this completely wrong, both because
77--  the ASIS tree got destroyed by step 4, and steps 2 and 3 were too early
78--  in the variant record case.
79
80with Types; use Types;
81
82package Sem_Case is
83
84   procedure No_OP (C : Node_Id);
85   --  The no-operation routine. Does mostly nothing. Can be used
86   --  in the following generics for the parameters Process_Empty_Choice,
87   --  or Process_Associated_Node. In the case of an empty range choice,
88   --  routine emits a warning when Warn_On_Redundant_Constructs is enabled.
89
90   generic
91      with procedure Process_Associated_Node (A : Node_Id);
92      --  Associated with each case alternative or record variant A there is
93      --  a node or list of nodes that need additional processing. This routine
94      --  implements that processing.
95
96   package Generic_Analyze_Choices is
97
98      procedure Analyze_Choices
99        (Alternatives : List_Id;
100         Subtyp       : Entity_Id);
101      --  From a case expression, case statement, or record variant, this
102      --  routine analyzes the corresponding list of discrete choices which
103      --  appear in each element of the list Alternatives (for the variant
104      --  part case, this is the variants, for a case expression or statement,
105      --  this is the Alternatives).
106      --
107      --  Subtyp is the subtype of the discrete choices. The type against which
108      --  the discrete choices must be resolved is its base type.
109
110   end Generic_Analyze_Choices;
111
112   generic
113      with procedure Process_Empty_Choice (Choice : Node_Id);
114      --  Processing to carry out for an empty Choice. Set to No_Op (declared
115      --  above) if no such processing is required.
116
117      with procedure Process_Non_Static_Choice (Choice : Node_Id);
118      --  Processing to carry out for a non static Choice (gives an error msg)
119
120      with procedure Process_Associated_Node (A : Node_Id);
121      --  Associated with each case alternative or record variant A there is
122      --  a node or list of nodes that need semantic processing. This routine
123      --  implements that processing.
124
125   package Generic_Check_Choices is
126
127      procedure Check_Choices
128        (N                        : Node_Id;
129         Alternatives             : List_Id;
130         Subtyp                   : Entity_Id;
131         Others_Present           : out Boolean);
132      --  From a case expression, case statement, or record variant N, this
133      --  routine analyzes the corresponding list of discrete choices which
134      --  appear in each element of the list Alternatives (for the variant
135      --  part case, this is the variants, for a case expression or statement,
136      --  this is the Alternatives).
137      --
138      --  Subtyp is the subtype of the discrete choices. The type against which
139      --  the discrete choices must be resolved is its base type.
140      --
141      --  Others_Present is set to True if an Others choice is present in the
142      --  list of choices, and in this case Others_Discrete_Choices is set in
143      --  the N_Others_Choice node.
144      --
145      --  If a Discrete_Choice list contains at least one instance of a subtype
146      --  with a static predicate, then the Has_SP_Choice flag is set true in
147      --  the parent node (N_Variant, N_Case_Expression/Statement_Alternative).
148
149   end Generic_Check_Choices;
150end Sem_Case;
151