1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Inline
; use Inline
;
27 with Types
; use Types
;
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
);
40 procedure Start_Generic
;
41 -- Must be invoked before starting to process a generic spec or body
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.
47 procedure Check_Generic_Child_Unit
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_Installed
57 -- is unchanged by the call.
59 function Copy_Generic_Node
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. Note that this function
68 -- copies any aspect specifications from the input node N to the returned
69 -- node, as well as the setting of the Has_Aspects flag.
71 function Get_Instance_Of
(A
: Entity_Id
) return Entity_Id
;
72 -- Retrieve actual associated with given generic parameter.
73 -- If A is uninstantiated or not a generic parameter, return A.
75 function Get_Unit_Instantiation_Node
(A
: Entity_Id
) return Node_Id
;
76 -- Given the entity of a unit that is an instantiation, retrieve the
77 -- original instance node. This is used when loading the instantiations
78 -- of the ancestors of a child generic that is being instantiated.
80 procedure Instantiate_Package_Body
81 (Body_Info
: Pending_Body_Info
;
82 Inlined_Body
: Boolean := False;
83 Body_Optional
: Boolean := False);
84 -- Called after semantic analysis, to complete the instantiation of
85 -- package instances. The flag Inlined_Body is set if the body is
86 -- being instantiated on the fly for inlining purposes.
88 -- The flag Body_Optional indicates that the call is for an instance
89 -- that precedes the current instance in the same declarative part.
90 -- This call is needed when instantiating a nested generic whose body
91 -- is to be found in the body of an instance. Normally we instantiate
92 -- package bodies only when they appear in the main unit, or when their
93 -- contents are needed for a nested generic G. If unit U contains several
94 -- instances I1, I2, etc. and I2 contains a nested generic, then when U
95 -- appears in the context of some other unit P that contains an instance
96 -- of G, we compile the body of I2, but not that of I1. However, when we
97 -- compile U as the main unit, we compile both bodies. This will lead to
98 -- link-time errors if the compilation of I1 generates public symbols,
99 -- because those in I2 will receive different names in both cases. This
100 -- forces us to analyze the body of I1 even when U is not the main unit.
101 -- We don't want this additional mechanism to generate an error when the
102 -- body of the generic for I1 is not present, and this is the reason for
103 -- the presence of the flag Body_Optional, which is exchanged between the
104 -- current procedure and Load_Parent_Of_Generic.
106 procedure Instantiate_Subprogram_Body
107 (Body_Info
: Pending_Body_Info
;
108 Body_Optional
: Boolean := False);
109 -- Called after semantic analysis, to complete the instantiation of
110 -- function and procedure instances. The flag Body_Optional has the
111 -- same purpose as described for Instantiate_Package_Body.
113 function Is_Abbreviated_Instance
(E
: Entity_Id
) return Boolean;
114 -- Return true if E is a package created for an abbreviated instantiation
115 -- to check conformance between formal package and corresponding actual.
117 function Need_Subprogram_Instance_Body
119 Subp
: Entity_Id
) return Boolean;
120 -- If a subprogram instance is inlined, indicate that the body of it
121 -- must be created, to be used in inlined calls by the back-end. The
122 -- subprogram may be inlined because the generic itself carries the
123 -- pragma, or because a pragma appears for the instance in the scope.
126 procedure Save_Global_References
(Templ
: Node_Id
);
127 -- Traverse the original generic unit, and capture all references to
128 -- entities that are defined outside of the generic in the analyzed tree
129 -- for the template. These references are copied into the original tree,
130 -- so that they appear automatically in every instantiation. A critical
131 -- invariant in this approach is that if an id in the generic resolves to
132 -- a local entity, the corresponding id in the instance will resolve to
133 -- the homologous entity in the instance, even though the enclosing context
134 -- for resolution is different, as long as the global references have been
135 -- captured as described here.
137 -- Because instantiations can be nested, the environment of the instance,
138 -- involving the actuals and other data-structures, must be saved and
139 -- restored in stack-like fashion. Front-end inlining also uses these
140 -- structures for the management of private/full views.
142 procedure Save_Global_References_In_Aspects
(N
: Node_Id
);
143 -- Save all global references found within the expressions of all aspects
144 -- that appear on node N.
146 procedure Set_Copied_Sloc_For_Inlined_Body
(N
: Node_Id
; E
: Entity_Id
);
147 -- This procedure is used when a subprogram body is inlined. This process
148 -- shares the same circuitry as the creation of an instantiated copy of
149 -- a generic template. The call to this procedure establishes a new source
150 -- file entry representing the inlined body as an instantiation, marked as
151 -- an inlined body (so that errout can distinguish cases for generating
152 -- error messages, otherwise the treatment is identical). In this call
153 -- N is the subprogram body and E is the defining identifier of the
154 -- subprogram in question. The resulting Sloc adjustment factor is
155 -- saved as part of the internal state of the Sem_Ch12 package for use
156 -- in subsequent calls to copy nodes.
158 procedure Set_Copied_Sloc_For_Inherited_Pragma
161 -- This procedure is used when a class-wide pre- or postcondition is
162 -- inherited. This process shares the same circuitry as the creation of
163 -- an instantiated copy of a generic template. The call to this procedure
164 -- establishes a new source file entry representing the inherited pragma
165 -- as an instantiation, marked as an inherited pragma (so that errout can
166 -- distinguish cases for generating error messages, otherwise the treatment
167 -- is identical). In this call, N is the subprogram declaration from
168 -- which the pragma is inherited and E is the defining identifier of
169 -- the overriding subprogram (when the subprogram is redefined) or the
170 -- defining identifier of the extension type (when the subprogram is
171 -- inherited). The resulting Sloc adjustment factor is saved as part of the
172 -- internal state of the Sem_Ch12 package for use in subsequent calls to
175 procedure Adjust_Inherited_Pragma_Sloc
(N
: Node_Id
);
176 -- This procedure is used when a class-wide pre- or postcondition
177 -- is inherited. It is called on each node of the pragma expression
178 -- to adjust its sloc. These call should be preceded by a call to
179 -- Set_Copied_Sloc_For_Inherited_Pragma that sets the required sloc
180 -- adjustment. This is done directly, instead of using Copy_Generic_Node
181 -- to copy nodes and adjust slocs, as Copy_Generic_Node expects a specific
182 -- structure to be in place, which is not the case for inherited pragmas.
185 (Gen_Unit
: Entity_Id
;
186 Act_Unit
: Entity_Id
);
187 -- Because instantiations can be nested, the compiler maintains a stack
188 -- of environments that holds variables relevant to the current instance:
189 -- most importanty Instantiated_Parent, Exchanged_Views, Hidden_Entities,
190 -- and others (see full list in Instance_Env).
192 procedure Restore_Env
;
193 -- After processing an instantiation, or aborting one because of semantic
194 -- errors, remove the current Instantiation_Env from Instantation_Envs.
196 procedure Initialize
;
197 -- Initializes internal data structures
199 procedure Check_Private_View
(N
: Node_Id
);
200 -- Check whether the type of a generic entity has a different view between
201 -- the point of generic analysis and the point of instantiation. If the
202 -- view has changed, then at the point of instantiation we restore the
203 -- correct view to perform semantic analysis of the instance, and reset
204 -- the current view after instantiation. The processing is driven by the
205 -- current private status of the type of the node, and Has_Private_View,
206 -- a flag that is set at the point of generic compilation. If view and
207 -- flag are inconsistent then the type is updated appropriately. A second
208 -- flag Has_Secondary_Private_View is used to update a second type related
209 -- to this type if need be.
211 -- This subprogram is used in Check_Generic_Actuals and Copy_Generic_Node,
212 -- and is exported here for the purpose of front-end inlining (see Exp_Ch6.
213 -- Expand_Inlined_Call.Process_Formals).