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-2013, 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. We do
53--    not want to mess up the -gnatct ASIS tree, which needs to be able to
54
55--  Step 1 is performed by the generic procedure Analyze_Choices, which is
56--  called when the variant record or case statement/expression is first
57--  encountered.
58
59--  Step 2 is performed by the generic procedure Check_Choices. We decide to
60--  do all semantic checking in that step, since as noted above some of this
61--  has to be deferred to the freeze point in any case for variants. For case
62--  statements and expressions, this procedure can be called at the time the
63--  case construct is encountered (after calling Analyze_Choices).
64
65--  Step 3 is also performed by Check_Choices, since we need the static ranges
66--  for predicated subtypes to accurately construct this.
67
68--  Step 4 is performed by the procedure Expand_Static_Predicates_In_Choices.
69--  For case statements, this call only happens during expansion, so the tree
70--  generated for ASIS does not have this expansion. For the Variant case, the
71--  expansion is done in the ASIS -gnatct case, but with a proper Rewrite call
72--  on the N_Variant node, so ASIS can retrieve the original. The reason we do
73--  the expansion unconditionally for variants is that other processing, for
74--  example for aggregates, relies on having a complete list of choices.
75
76--  Historical note: We used to perform all four of these functions at once in
77--  a single procedure called Analyze_Choices. This routine was called at the
78--  time the construct was first encountered. That seemed to work OK up to Ada
79--  2005, but the introduction of statically predicated subtypes with delayed
80--  evaluation of the static ranges made this completely wrong, both because
81--  the ASIS tree got destroyed by step 4, and steps 2 and 3 were too early
82--  in the variant record case.
83
84with Types; use Types;
85
86package Sem_Case is
87
88   procedure No_OP (C : Node_Id);
89   --  The no-operation routine. Does absolutely nothing. Can be used
90   --  in the following generics for the parameters Process_Empty_Choice,
91   --  or Process_Associated_Node.
92
93   generic
94      with procedure Process_Associated_Node (A : Node_Id);
95      --  Associated with each case alternative or record variant A there is
96      --  a node or list of nodes that need additional processing. This routine
97      --  implements that processing.
98
99   package Generic_Analyze_Choices is
100
101      procedure Analyze_Choices
102        (Alternatives : List_Id;
103         Subtyp       : Entity_Id);
104      --  From a case expression, case statement, or record variant, this
105      --  routine analyzes the corresponding list of discrete choices which
106      --  appear in each element of the list Alternatives (for the variant
107      --  part case, this is the variants, for a case expression or statement,
108      --  this is the Alternatives).
109      --
110      --  Subtyp is the subtype of the discrete choices. The type against which
111      --  the discrete choices must be resolved is its base type.
112
113   end Generic_Analyze_Choices;
114
115   generic
116      with procedure Process_Empty_Choice (Choice : Node_Id);
117      --  Processing to carry out for an empty Choice. Set to No_Op (declared
118      --  above) if no such processing is required.
119
120      with procedure Process_Non_Static_Choice (Choice : Node_Id);
121      --  Processing to carry out for a non static Choice (gives an error msg)
122
123      with procedure Process_Associated_Node (A : Node_Id);
124      --  Associated with each case alternative or record variant A there is
125      --  a node or list of nodes that need semantic processing. This routine
126      --  implements that processing.
127
128   package Generic_Check_Choices is
129
130      procedure Check_Choices
131        (N                        : Node_Id;
132         Alternatives             : List_Id;
133         Subtyp                   : Entity_Id;
134         Others_Present           : out Boolean);
135      --  From a case expression, case statement, or record variant N, this
136      --  routine analyzes the corresponding list of discrete choices which
137      --  appear in each element of the list Alternatives (for the variant
138      --  part case, this is the variants, for a case expression or statement,
139      --  this is the Alternatives).
140      --
141      --  Subtyp is the subtype of the discrete choices. The type against which
142      --  the discrete choices must be resolved is its base type.
143      --
144      --  Others_Present is set to True if an Others choice is present in the
145      --  list of choices, and in this case Others_Discrete_Choices is set in
146      --  the N_Others_Choice node.
147      --
148      --  If a Discrete_Choice list contains at least one instance of a subtype
149      --  with a static predicate, then the Has_SP_Choice flag is set true in
150      --  the parent node (N_Variant, N_Case_Expression/Statement_Alternative).
151
152   end Generic_Check_Choices;
153end Sem_Case;
154