File : sem_case.ads


   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-2016, 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 
  84 with Types; use Types;
  85 
  86 package Sem_Case is
  87 
  88    procedure No_OP (C : Node_Id);
  89    --  The no-operation routine. Does mostly nothing. Can be used
  90    --  in the following generics for the parameters Process_Empty_Choice,
  91    --  or Process_Associated_Node. In the case of an empty range choice,
  92    --  routine emits a warning when Warn_On_Redundant_Constructs is enabled.
  93 
  94    generic
  95       with procedure Process_Associated_Node (A : Node_Id);
  96       --  Associated with each case alternative or record variant A there is
  97       --  a node or list of nodes that need additional processing. This routine
  98       --  implements that processing.
  99 
 100    package Generic_Analyze_Choices is
 101 
 102       procedure Analyze_Choices
 103         (Alternatives : List_Id;
 104          Subtyp       : Entity_Id);
 105       --  From a case expression, case statement, or record variant, this
 106       --  routine analyzes the corresponding list of discrete choices which
 107       --  appear in each element of the list Alternatives (for the variant
 108       --  part case, this is the variants, for a case expression or statement,
 109       --  this is the Alternatives).
 110       --
 111       --  Subtyp is the subtype of the discrete choices. The type against which
 112       --  the discrete choices must be resolved is its base type.
 113 
 114    end Generic_Analyze_Choices;
 115 
 116    generic
 117       with procedure Process_Empty_Choice (Choice : Node_Id);
 118       --  Processing to carry out for an empty Choice. Set to No_Op (declared
 119       --  above) if no such processing is required.
 120 
 121       with procedure Process_Non_Static_Choice (Choice : Node_Id);
 122       --  Processing to carry out for a non static Choice (gives an error msg)
 123 
 124       with procedure Process_Associated_Node (A : Node_Id);
 125       --  Associated with each case alternative or record variant A there is
 126       --  a node or list of nodes that need semantic processing. This routine
 127       --  implements that processing.
 128 
 129    package Generic_Check_Choices is
 130 
 131       procedure Check_Choices
 132         (N                        : Node_Id;
 133          Alternatives             : List_Id;
 134          Subtyp                   : Entity_Id;
 135          Others_Present           : out Boolean);
 136       --  From a case expression, case statement, or record variant N, this
 137       --  routine analyzes the corresponding list of discrete choices which
 138       --  appear in each element of the list Alternatives (for the variant
 139       --  part case, this is the variants, for a case expression or statement,
 140       --  this is the Alternatives).
 141       --
 142       --  Subtyp is the subtype of the discrete choices. The type against which
 143       --  the discrete choices must be resolved is its base type.
 144       --
 145       --  Others_Present is set to True if an Others choice is present in the
 146       --  list of choices, and in this case Others_Discrete_Choices is set in
 147       --  the N_Others_Choice node.
 148       --
 149       --  If a Discrete_Choice list contains at least one instance of a subtype
 150       --  with a static predicate, then the Has_SP_Choice flag is set true in
 151       --  the parent node (N_Variant, N_Case_Expression/Statement_Alternative).
 152 
 153    end Generic_Check_Choices;
 154 end Sem_Case;