| 1 | 
         281 | 
         jeremybenn | 
         ------------------------------------------------------------------------------
  | 
      
      
         | 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-2009, 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                  (N : Node_Id);
  | 
      
      
         | 38 | 
          | 
          | 
            procedure Analyze_Formal_Package                     (N : Node_Id);
  | 
      
      
         | 39 | 
          | 
          | 
          
  | 
      
      
         | 40 | 
          | 
          | 
            procedure Start_Generic;
  | 
      
      
         | 41 | 
          | 
          | 
            --  Must be invoked before starting to process a generic spec or body
  | 
      
      
         | 42 | 
          | 
          | 
          
  | 
      
      
         | 43 | 
          | 
          | 
            procedure End_Generic;
  | 
      
      
         | 44 | 
          | 
          | 
            --  Must be invoked just at the end of the end of the processing of a
  | 
      
      
         | 45 | 
          | 
          | 
            --  generic spec or body.
  | 
      
      
         | 46 | 
          | 
          | 
          
  | 
      
      
         | 47 | 
          | 
          | 
            procedure Check_Generic_Child_Unit
  | 
      
      
         | 48 | 
          | 
          | 
              (Gen_Id           : Node_Id;
  | 
      
      
         | 49 | 
          | 
          | 
               Parent_Installed : in out Boolean);
  | 
      
      
         | 50 | 
          | 
          | 
            --  If the name of the generic unit in an instantiation or a renaming is a
  | 
      
      
         | 51 | 
          | 
          | 
            --  selected component, then the prefix may be an instance and the selector
  | 
      
      
         | 52 | 
          | 
          | 
            --  may designate a child unit. Retrieve the parent generic and search for
  | 
      
      
         | 53 | 
          | 
          | 
            --  the child unit that must be declared within. Similarly, if this is the
  | 
      
      
         | 54 | 
          | 
          | 
            --  name of a generic child unit within an instantiation of its own parent,
  | 
      
      
         | 55 | 
          | 
          | 
            --  retrieve the parent generic. If the parent is installed as a result of
  | 
      
      
         | 56 | 
          | 
          | 
            --  this call, then Parent_Installed is set True, otherwise Parent_Intalled
  | 
      
      
         | 57 | 
          | 
          | 
            --  is unchanged by the call.
  | 
      
      
         | 58 | 
          | 
          | 
          
  | 
      
      
         | 59 | 
          | 
          | 
            function Copy_Generic_Node
  | 
      
      
         | 60 | 
          | 
          | 
              (N             : Node_Id;
  | 
      
      
         | 61 | 
          | 
          | 
               Parent_Id     : Node_Id;
  | 
      
      
         | 62 | 
          | 
          | 
               Instantiating : Boolean) return Node_Id;
  | 
      
      
         | 63 | 
          | 
          | 
            --  Copy the tree for a generic unit or its body. The unit is copied
  | 
      
      
         | 64 | 
          | 
          | 
            --  repeatedly: once to produce a copy on which semantic analysis of
  | 
      
      
         | 65 | 
          | 
          | 
            --  the generic is performed, and once for each instantiation. The tree
  | 
      
      
         | 66 | 
          | 
          | 
            --  being copied is not semantically analyzed, except that references to
  | 
      
      
         | 67 | 
          | 
          | 
            --  global entities are marked on terminal nodes.
  | 
      
      
         | 68 | 
          | 
          | 
          
  | 
      
      
         | 69 | 
          | 
          | 
            function Get_Instance_Of (A : Entity_Id) return Entity_Id;
  | 
      
      
         | 70 | 
          | 
          | 
            --  Retrieve actual associated with given generic parameter.
  | 
      
      
         | 71 | 
          | 
          | 
            --  If A is uninstantiated or not a generic parameter, return A.
  | 
      
      
         | 72 | 
          | 
          | 
          
  | 
      
      
         | 73 | 
          | 
          | 
            function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id;
  | 
      
      
         | 74 | 
          | 
          | 
            --  Given the entity of a unit that is an instantiation, retrieve the
  | 
      
      
         | 75 | 
          | 
          | 
            --  original instance node. This is used when loading the instantiations
  | 
      
      
         | 76 | 
          | 
          | 
            --  of the ancestors of a child generic that is being instantiated.
  | 
      
      
         | 77 | 
          | 
          | 
          
  | 
      
      
         | 78 | 
          | 
          | 
            procedure Instantiate_Package_Body
  | 
      
      
         | 79 | 
          | 
          | 
              (Body_Info     : Pending_Body_Info;
  | 
      
      
         | 80 | 
          | 
          | 
               Inlined_Body  : Boolean := False;
  | 
      
      
         | 81 | 
          | 
          | 
               Body_Optional : Boolean := False);
  | 
      
      
         | 82 | 
          | 
          | 
            --  Called after semantic analysis, to complete the instantiation of
  | 
      
      
         | 83 | 
          | 
          | 
            --  package instances. The flag Inlined_Body is set if the body is
  | 
      
      
         | 84 | 
          | 
          | 
            --  being instantiated on the fly for inlined purposes.
  | 
      
      
         | 85 | 
          | 
          | 
            --
  | 
      
      
         | 86 | 
          | 
          | 
            --  The flag Body_Optional indicates that the call is for an instance
  | 
      
      
         | 87 | 
          | 
          | 
            --  that precedes the current instance in the same declarative part.
  | 
      
      
         | 88 | 
          | 
          | 
            --  This call is needed when instantiating a nested generic whose body
  | 
      
      
         | 89 | 
          | 
          | 
            --  is to be found in the body of an instance. Normally we instantiate
  | 
      
      
         | 90 | 
          | 
          | 
            --  package bodies only when they appear in the main unit, or when their
  | 
      
      
         | 91 | 
          | 
          | 
            --  contents are needed for a nested generic G. If unit U contains several
  | 
      
      
         | 92 | 
          | 
          | 
            --  instances I1, I2, etc. and I2 contains a nested generic, then when U
  | 
      
      
         | 93 | 
          | 
          | 
            --  appears in the context of some other unit P that contains an instance
  | 
      
      
         | 94 | 
          | 
          | 
            --  of G, we compile the body of I2, but not that of I1. However, when we
  | 
      
      
         | 95 | 
          | 
          | 
            --  compile U as the main unit, we compile both bodies. This will lead to
  | 
      
      
         | 96 | 
          | 
          | 
            --  lead to link-time errors if the compilation of I1 generates public
  | 
      
      
         | 97 | 
          | 
          | 
            --  symbols, because those in I2 will receive different names in both
  | 
      
      
         | 98 | 
          | 
          | 
            --  cases. This forces us to analyze the body of I1 even when U is not the
  | 
      
      
         | 99 | 
          | 
          | 
            --  main unit. We don't want this additional mechanism to generate an error
  | 
      
      
         | 100 | 
          | 
          | 
            --  when the body of the generic for I1 is not present, and this is the
  | 
      
      
         | 101 | 
          | 
          | 
            --  reason for the presence of the flag Body_Optional, which is exchanged
  | 
      
      
         | 102 | 
          | 
          | 
            --  between the current procedure and Load_Parent_Of_Generic.
  | 
      
      
         | 103 | 
          | 
          | 
          
  | 
      
      
         | 104 | 
          | 
          | 
            procedure Instantiate_Subprogram_Body
  | 
      
      
         | 105 | 
          | 
          | 
              (Body_Info     : Pending_Body_Info;
  | 
      
      
         | 106 | 
          | 
          | 
               Body_Optional : Boolean := False);
  | 
      
      
         | 107 | 
          | 
          | 
            --  Called after semantic analysis, to complete the instantiation of
  | 
      
      
         | 108 | 
          | 
          | 
            --  function and procedure instances. The flag Body_Optional has the
  | 
      
      
         | 109 | 
          | 
          | 
            --  same purpose as described for Instantiate_Package_Body.
  | 
      
      
         | 110 | 
          | 
          | 
          
  | 
      
      
         | 111 | 
          | 
          | 
            function Need_Subprogram_Instance_Body
  | 
      
      
         | 112 | 
          | 
          | 
              (N    : Node_Id;
  | 
      
      
         | 113 | 
          | 
          | 
               Subp : Entity_Id) return Boolean;
  | 
      
      
         | 114 | 
          | 
          | 
          
  | 
      
      
         | 115 | 
          | 
          | 
            --  If a subprogram instance is inlined, indicate that the body of it
  | 
      
      
         | 116 | 
          | 
          | 
            --  must be created, to be used in inlined calls by the back-end. The
  | 
      
      
         | 117 | 
          | 
          | 
            --  subprogram may be inlined because the generic itself carries the
  | 
      
      
         | 118 | 
          | 
          | 
            --  pragma, or because a pragma appears for the instance in the scope.
  | 
      
      
         | 119 | 
          | 
          | 
            --  of the instance.
  | 
      
      
         | 120 | 
          | 
          | 
          
  | 
      
      
         | 121 | 
          | 
          | 
            procedure Save_Global_References (N : Node_Id);
  | 
      
      
         | 122 | 
          | 
          | 
            --  Traverse the original generic unit, and capture all references to
  | 
      
      
         | 123 | 
          | 
          | 
            --  entities that are defined outside of the generic in the analyzed
  | 
      
      
         | 124 | 
          | 
          | 
            --  tree for the template. These references are copied into the original
  | 
      
      
         | 125 | 
          | 
          | 
            --  tree, so that they appear automatically in every instantiation.
  | 
      
      
         | 126 | 
          | 
          | 
            --  A critical invariant in this approach is that if an id in the generic
  | 
      
      
         | 127 | 
          | 
          | 
            --  resolves to a local entity, the corresponding id in the instance
  | 
      
      
         | 128 | 
          | 
          | 
            --  will resolve to the homologous entity in the instance, even though
  | 
      
      
         | 129 | 
          | 
          | 
            --  the enclosing context for resolution is different, as long as the
  | 
      
      
         | 130 | 
          | 
          | 
            --  global references have been captured as described here.
  | 
      
      
         | 131 | 
          | 
          | 
          
  | 
      
      
         | 132 | 
          | 
          | 
            --  Because instantiations can be nested, the environment of the instance,
  | 
      
      
         | 133 | 
          | 
          | 
            --  involving the actuals and other data-structures, must be saved and
  | 
      
      
         | 134 | 
          | 
          | 
            --  restored in stack-like fashion. Front-end inlining also uses these
  | 
      
      
         | 135 | 
          | 
          | 
            --  structures for the management of private/full views.
  | 
      
      
         | 136 | 
          | 
          | 
          
  | 
      
      
         | 137 | 
          | 
          | 
            procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id);
  | 
      
      
         | 138 | 
          | 
          | 
            --  This procedure is used when a subprogram body is inlined. This process
  | 
      
      
         | 139 | 
          | 
          | 
            --  shares the same circuitry as the creation of an instantiated copy of
  | 
      
      
         | 140 | 
          | 
          | 
            --  a generic template. The call to this procedure establishes a new source
  | 
      
      
         | 141 | 
          | 
          | 
            --  file entry representing the inlined body as an instantiation, marked as
  | 
      
      
         | 142 | 
          | 
          | 
            --  an inlined body (so that errout can distinguish cases for generating
  | 
      
      
         | 143 | 
          | 
          | 
            --  error messages, otherwise the treatment is identical). In this call
  | 
      
      
         | 144 | 
          | 
          | 
            --  N is the subprogram body and E is the defining identifier of the
  | 
      
      
         | 145 | 
          | 
          | 
            --  subprogram in question. The resulting Sloc adjustment factor is
  | 
      
      
         | 146 | 
          | 
          | 
            --  saved as part of the internal state of the Sem_Ch12 package for use
  | 
      
      
         | 147 | 
          | 
          | 
            --  in subsequent calls to copy nodes.
  | 
      
      
         | 148 | 
          | 
          | 
          
  | 
      
      
         | 149 | 
          | 
          | 
            procedure Save_Env
  | 
      
      
         | 150 | 
          | 
          | 
              (Gen_Unit : Entity_Id;
  | 
      
      
         | 151 | 
          | 
          | 
               Act_Unit : Entity_Id);
  | 
      
      
         | 152 | 
          | 
          | 
            --   ??? comment needed
  | 
      
      
         | 153 | 
          | 
          | 
          
  | 
      
      
         | 154 | 
          | 
          | 
            procedure Restore_Env;
  | 
      
      
         | 155 | 
          | 
          | 
            --   ??? comment needed
  | 
      
      
         | 156 | 
          | 
          | 
          
  | 
      
      
         | 157 | 
          | 
          | 
            procedure Initialize;
  | 
      
      
         | 158 | 
          | 
          | 
            --  Initializes internal data structures
  | 
      
      
         | 159 | 
          | 
          | 
          
  | 
      
      
         | 160 | 
          | 
          | 
            procedure Check_Private_View (N : Node_Id);
  | 
      
      
         | 161 | 
          | 
          | 
            --  Check whether the type of a generic entity has a different view between
  | 
      
      
         | 162 | 
          | 
          | 
            --  the point of generic analysis and the point of instantiation. If the
  | 
      
      
         | 163 | 
          | 
          | 
            --  view has changed, then at the point of instantiation we restore the
  | 
      
      
         | 164 | 
          | 
          | 
            --  correct view to perform semantic analysis of the instance, and reset
  | 
      
      
         | 165 | 
          | 
          | 
            --  the current view after instantiation. The processing is driven by the
  | 
      
      
         | 166 | 
          | 
          | 
            --  current private status of the type of the node, and Has_Private_View,
  | 
      
      
         | 167 | 
          | 
          | 
            --  a flag that is set at the point of generic compilation. If view and
  | 
      
      
         | 168 | 
          | 
          | 
            --  flag are inconsistent then the type is updated appropriately.
  | 
      
      
         | 169 | 
          | 
          | 
            --
  | 
      
      
         | 170 | 
          | 
          | 
            --  This subprogram is used in Check_Generic_Actuals and Copy_Generic_Node,
  | 
      
      
         | 171 | 
          | 
          | 
            --  and is exported here for the purpose of front-end inlining (see Exp_Ch6.
  | 
      
      
         | 172 | 
          | 
          | 
            --  Expand_Inlined_Call.Process_Formals).
  | 
      
      
         | 173 | 
          | 
          | 
          
  | 
      
      
         | 174 | 
          | 
          | 
         end Sem_Ch12;
  |