File : sem_ch12.ads


   1 ------------------------------------------------------------------------------
   2 --                                                                          --
   3 --                         GNAT COMPILER COMPONENTS                         --
   4 --                                                                          --
   5 --                             S E M _ C H 1 2                              --
   6 --                                                                          --
   7 --                                 S p e c                                  --
   8 --                                                                          --
   9 --          Copyright (C) 1992-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 with Inline; use Inline;
  27 with Types;  use Types;
  28 
  29 package Sem_Ch12 is
  30    procedure Analyze_Generic_Package_Declaration        (N : Node_Id);
  31    procedure Analyze_Generic_Subprogram_Declaration     (N : Node_Id);
  32    procedure Analyze_Package_Instantiation              (N : Node_Id);
  33    procedure Analyze_Procedure_Instantiation            (N : Node_Id);
  34    procedure Analyze_Function_Instantiation             (N : Node_Id);
  35    procedure Analyze_Formal_Object_Declaration          (N : Node_Id);
  36    procedure Analyze_Formal_Type_Declaration            (N : Node_Id);
  37    procedure Analyze_Formal_Subprogram_Declaration      (N : Node_Id);
  38    procedure Analyze_Formal_Package_Declaration         (N : Node_Id);
  39 
  40    procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id);
  41    --  Add an entry in the table of instance bodies that must be analyzed
  42    --  when inlining requires its body or the body of a nested instance.
  43 
  44    function Build_Function_Wrapper
  45      (Formal_Subp : Entity_Id;
  46       Actual_Subp : Entity_Id) return Node_Id;
  47    --  In GNATprove mode, create a wrapper function for actuals that are
  48    --  functions with any number of formal parameters, in order to propagate
  49    --  their contract to the renaming declarations generated for them. This
  50    --  is called after the renaming declaration created for the formal in the
  51    --  instance has been analyzed, and the actual is known.
  52 
  53    function Build_Operator_Wrapper
  54      (Formal_Subp : Entity_Id;
  55       Actual_Subp : Entity_Id) return Node_Id;
  56    --  In GNATprove mode, create a wrapper function for actuals that are
  57    --  operators, in order to propagate their contract to the renaming
  58    --  declarations generated for them. The types are (the instances of)
  59    --  the types of the formal subprogram.
  60 
  61    procedure Start_Generic;
  62    --  Must be invoked before starting to process a generic spec or body
  63 
  64    procedure End_Generic;
  65    --  Must be invoked just at the end of the end of the processing of a
  66    --  generic spec or body.
  67 
  68    procedure Check_Generic_Child_Unit
  69      (Gen_Id           : Node_Id;
  70       Parent_Installed : in out Boolean);
  71    --  If the name of the generic unit in an instantiation or a renaming is a
  72    --  selected component, then the prefix may be an instance and the selector
  73    --  may designate a child unit. Retrieve the parent generic and search for
  74    --  the child unit that must be declared within. Similarly, if this is the
  75    --  name of a generic child unit within an instantiation of its own parent,
  76    --  retrieve the parent generic. If the parent is installed as a result of
  77    --  this call, then Parent_Installed is set True, otherwise Parent_Installed
  78    --  is unchanged by the call.
  79 
  80    function Copy_Generic_Node
  81      (N             : Node_Id;
  82       Parent_Id     : Node_Id;
  83       Instantiating : Boolean) return Node_Id;
  84    --  Copy the tree for a generic unit or its body. The unit is copied
  85    --  repeatedly: once to produce a copy on which semantic analysis of
  86    --  the generic is performed, and once for each instantiation. The tree
  87    --  being copied is not semantically analyzed, except that references to
  88    --  global entities are marked on terminal nodes. Note that this function
  89    --  copies any aspect specifications from the input node N to the returned
  90    --  node, as well as the setting of the Has_Aspects flag.
  91 
  92    function Get_Instance_Of (A : Entity_Id) return Entity_Id;
  93    --  Retrieve actual associated with given generic parameter.
  94    --  If A is uninstantiated or not a generic parameter, return A.
  95 
  96    function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id;
  97    --  Given the entity of a unit that is an instantiation, retrieve the
  98    --  original instance node. This is used when loading the instantiations
  99    --  of the ancestors of a child generic that is being instantiated.
 100 
 101    procedure Instantiate_Package_Body
 102      (Body_Info     : Pending_Body_Info;
 103       Inlined_Body  : Boolean := False;
 104       Body_Optional : Boolean := False);
 105    --  Called after semantic analysis, to complete the instantiation of
 106    --  package instances. The flag Inlined_Body is set if the body is
 107    --  being instantiated on the fly for inlining purposes.
 108    --
 109    --  The flag Body_Optional indicates that the call is for an instance
 110    --  that precedes the current instance in the same declarative part.
 111    --  This call is needed when instantiating a nested generic whose body
 112    --  is to be found in the body of an instance. Normally we instantiate
 113    --  package bodies only when they appear in the main unit, or when their
 114    --  contents are needed for a nested generic G. If unit U contains several
 115    --  instances I1, I2, etc. and I2 contains a nested generic, then when U
 116    --  appears in the context of some other unit P that contains an instance
 117    --  of G, we compile the body of I2, but not that of I1. However, when we
 118    --  compile U as the main unit, we compile both bodies. This will lead to
 119    --  link-time errors if the compilation of I1 generates public symbols,
 120    --  because those in I2 will receive different names in both cases. This
 121    --  forces us to analyze the body of I1 even when U is not the main unit.
 122    --  We don't want this additional mechanism to generate an error when the
 123    --  body of the generic for I1 is not present, and this is the reason for
 124    --  the presence of the flag Body_Optional, which is exchanged between the
 125    --  current procedure and Load_Parent_Of_Generic.
 126 
 127    procedure Instantiate_Subprogram_Body
 128      (Body_Info     : Pending_Body_Info;
 129       Body_Optional : Boolean := False);
 130    --  Called after semantic analysis, to complete the instantiation of
 131    --  function and procedure instances. The flag Body_Optional has the
 132    --  same purpose as described for Instantiate_Package_Body.
 133 
 134    function Need_Subprogram_Instance_Body
 135      (N    : Node_Id;
 136       Subp : Entity_Id) return Boolean;
 137    --  If a subprogram instance is inlined, indicate that the body of it
 138    --  must be created, to be used in inlined calls by the back-end. The
 139    --  subprogram may be inlined because the generic itself carries the
 140    --  pragma, or because a pragma appears for the instance in the scope.
 141    --  of the instance.
 142 
 143    procedure Save_Global_References (Templ : Node_Id);
 144    --  Traverse the original generic unit, and capture all references to
 145    --  entities that are defined outside of the generic in the analyzed tree
 146    --  for the template. These references are copied into the original tree,
 147    --  so that they appear automatically in every instantiation. A critical
 148    --  invariant in this approach is that if an id in the generic resolves to
 149    --  a local entity, the corresponding id in the instance will resolve to
 150    --  the homologous entity in the instance, even though the enclosing context
 151    --  for resolution is different, as long as the global references have been
 152    --  captured as described here.
 153 
 154    --  Because instantiations can be nested, the environment of the instance,
 155    --  involving the actuals and other data-structures, must be saved and
 156    --  restored in stack-like fashion. Front-end inlining also uses these
 157    --  structures for the management of private/full views.
 158 
 159    procedure Save_Global_References_In_Aspects (N : Node_Id);
 160    --  Save all global references found within the expressions of all aspects
 161    --  that appear on node N.
 162 
 163    procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id);
 164    --  This procedure is used when a subprogram body is inlined. This process
 165    --  shares the same circuitry as the creation of an instantiated copy of
 166    --  a generic template. The call to this procedure establishes a new source
 167    --  file entry representing the inlined body as an instantiation, marked as
 168    --  an inlined body (so that errout can distinguish cases for generating
 169    --  error messages, otherwise the treatment is identical). In this call
 170    --  N is the subprogram body and E is the defining identifier of the
 171    --  subprogram in question. The resulting Sloc adjustment factor is
 172    --  saved as part of the internal state of the Sem_Ch12 package for use
 173    --  in subsequent calls to copy nodes.
 174 
 175    procedure Set_Copied_Sloc_For_Inherited_Pragma
 176      (N : Node_Id;
 177       E : Entity_Id);
 178    --  This procedure is used when a class-wide pre- or postcondition is
 179    --  inherited. This process shares the same circuitry as the creation of
 180    --  an instantiated copy of a generic template. The call to this procedure
 181    --  establishes a new source file entry representing the inherited pragma
 182    --  as an instantiation, marked as an inherited pragma (so that errout can
 183    --  distinguish cases for generating error messages, otherwise the treatment
 184    --  is identical). In this call, N is the subprogram declaration from
 185    --  which the pragma is inherited and E is the defining identifier of
 186    --  the overriding subprogram (when the subprogram is redefined) or the
 187    --  defining identifier of the extension type (when the subprogram is
 188    --  inherited). The resulting Sloc adjustment factor is saved as part of the
 189    --  internal state of the Sem_Ch12 package for use in subsequent calls to
 190    --  copy nodes.
 191 
 192    procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id);
 193    --  This procedure is used when a class-wide pre- or postcondition
 194    --  is inherited. It is called on each node of the pragma expression
 195    --  to adjust its sloc. These call should be preceded by a call to
 196    --  Set_Copied_Sloc_For_Inherited_Pragma that sets the required sloc
 197    --  adjustment. This is done directly, instead of using Copy_Generic_Node
 198    --  to copy nodes and adjust slocs, as Copy_Generic_Node expects a specific
 199    --  structure to be in place, which is not the case for inherited pragmas.
 200 
 201    procedure Save_Env
 202      (Gen_Unit : Entity_Id;
 203       Act_Unit : Entity_Id);
 204    --  Because instantiations can be nested, the compiler maintains a stack
 205    --  of environments that holds variables relevant to the current instance:
 206    --  most importanty Instantiated_Parent, Exchanged_Views, Hidden_Entities,
 207    --  and others (see full list in Instance_Env).
 208 
 209    procedure Restore_Env;
 210    --  After processing an instantiation, or aborting one because of semantic
 211    --  errors, remove the current Instantiation_Env from Instantation_Envs.
 212 
 213    procedure Initialize;
 214    --  Initializes internal data structures
 215 
 216    procedure Check_Private_View (N : Node_Id);
 217    --  Check whether the type of a generic entity has a different view between
 218    --  the point of generic analysis and the point of instantiation. If the
 219    --  view has changed, then at the point of instantiation we restore the
 220    --  correct view to perform semantic analysis of the instance, and reset
 221    --  the current view after instantiation. The processing is driven by the
 222    --  current private status of the type of the node, and Has_Private_View,
 223    --  a flag that is set at the point of generic compilation. If view and
 224    --  flag are inconsistent then the type is updated appropriately.
 225    --
 226    --  This subprogram is used in Check_Generic_Actuals and Copy_Generic_Node,
 227    --  and is exported here for the purpose of front-end inlining (see Exp_Ch6.
 228    --  Expand_Inlined_Call.Process_Formals).
 229 
 230 end Sem_Ch12;