1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2019, 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 Atree
; use Atree
;
27 with Einfo
; use Einfo
;
28 with Exp_Ch3
; use Exp_Ch3
;
29 with Exp_Ch4
; use Exp_Ch4
;
30 with Exp_Ch6
; use Exp_Ch6
;
31 with Exp_Dbug
; use Exp_Dbug
;
32 with Exp_Util
; use Exp_Util
;
33 with Freeze
; use Freeze
;
34 with Namet
; use Namet
;
35 with Nmake
; use Nmake
;
36 with Nlists
; use Nlists
;
39 with Sem_Aux
; use Sem_Aux
;
40 with Sem_Ch8
; use Sem_Ch8
;
41 with Sem_Util
; use Sem_Util
;
42 with Sinfo
; use Sinfo
;
43 with Snames
; use Snames
;
44 with Stand
; use Stand
;
45 with Tbuild
; use Tbuild
;
47 package body Exp_Ch8
is
49 ---------------------------------------------
50 -- Expand_N_Exception_Renaming_Declaration --
51 ---------------------------------------------
53 procedure Expand_N_Exception_Renaming_Declaration
(N
: Node_Id
) is
57 Decl
:= Debug_Renaming_Declaration
(N
);
59 if Present
(Decl
) then
60 Insert_Action
(N
, Decl
);
62 end Expand_N_Exception_Renaming_Declaration
;
64 ------------------------------------------
65 -- Expand_N_Object_Renaming_Declaration --
66 ------------------------------------------
68 -- Most object renaming cases can be done by just capturing the address
69 -- of the renamed object. The cases in which this is not true are when
70 -- this address is not computable, since it involves extraction of a
71 -- packed array element, or of a record component to which a component
72 -- clause applies (that can specify an arbitrary bit boundary), or where
73 -- the enclosing record itself has a non-standard representation.
75 -- In these two cases, we pre-evaluate the renaming expression, by
76 -- extracting and freezing the values of any subscripts, and then we
77 -- set the flag Is_Renaming_Of_Object which means that any reference
78 -- to the object will be handled by macro substitution in the front
79 -- end, and the back end will know to ignore the renaming declaration.
81 -- An additional odd case that requires processing by expansion is
82 -- the renaming of a discriminant of a mutable record type. The object
83 -- is a constant because it renames something that cannot be assigned to,
84 -- but in fact the underlying value can change and must be reevaluated
85 -- at each reference. Gigi does have a notion of a "constant view" of
86 -- an object, and therefore the front-end must perform the expansion.
87 -- For simplicity, and to bypass some obscure code-generation problem,
88 -- we use macro substitution for all renamed discriminants, whether the
89 -- enclosing type is constrained or not.
91 -- The other special processing required is for the case of renaming
92 -- of an object of a class wide type, where it is necessary to build
93 -- the appropriate subtype for the renamed object.
94 -- More comments needed for this para ???
96 procedure Expand_N_Object_Renaming_Declaration
(N
: Node_Id
) is
97 Nam
: constant Node_Id
:= Name
(N
);
101 function Evaluation_Required
(Nam
: Node_Id
) return Boolean;
102 -- Determines whether it is necessary to do static name evaluation for
103 -- renaming of Nam. It is considered necessary if evaluating the name
104 -- involves indexing a packed array, or extracting a component of a
105 -- record to which a component clause applies. Note that we are only
106 -- interested in these operations if they occur as part of the name
107 -- itself, subscripts are just values that are computed as part of the
108 -- evaluation, so their form is unimportant.
109 -- In addition, always return True for Modify_Tree_For_C since the
110 -- code generator doesn't know how to handle renamings.
112 -------------------------
113 -- Evaluation_Required --
114 -------------------------
116 function Evaluation_Required
(Nam
: Node_Id
) return Boolean is
118 if Modify_Tree_For_C
then
121 elsif Nkind_In
(Nam
, N_Indexed_Component
, N_Slice
) then
122 if Is_Packed
(Etype
(Prefix
(Nam
))) then
125 return Evaluation_Required
(Prefix
(Nam
));
128 elsif Nkind
(Nam
) = N_Selected_Component
then
130 Rec_Type
: constant Entity_Id
:= Etype
(Prefix
(Nam
));
133 if Present
(Component_Clause
(Entity
(Selector_Name
(Nam
))))
134 or else Has_Non_Standard_Rep
(Rec_Type
)
138 elsif Ekind
(Entity
(Selector_Name
(Nam
))) = E_Discriminant
139 and then Is_Record_Type
(Rec_Type
)
140 and then not Is_Concurrent_Record_Type
(Rec_Type
)
145 return Evaluation_Required
(Prefix
(Nam
));
152 end Evaluation_Required
;
154 -- Start of processing for Expand_N_Object_Renaming_Declaration
157 -- Perform name evaluation if required
159 if Evaluation_Required
(Nam
) then
161 Set_Is_Renaming_Of_Object
(Defining_Identifier
(N
));
164 -- Deal with construction of subtype in class-wide case
166 T
:= Etype
(Defining_Identifier
(N
));
168 if Is_Class_Wide_Type
(T
) then
169 Expand_Subtype_From_Expr
(N
, T
, Subtype_Mark
(N
), Name
(N
));
170 Find_Type
(Subtype_Mark
(N
));
171 Set_Etype
(Defining_Identifier
(N
), Entity
(Subtype_Mark
(N
)));
173 -- Freeze the class-wide subtype here to ensure that the subtype
174 -- and equivalent type are frozen before the renaming.
176 Freeze_Before
(N
, Entity
(Subtype_Mark
(N
)));
179 -- Ada 2005 (AI-318-02): If the renamed object is a call to a build-in-
180 -- place function, then a temporary return object needs to be created
181 -- and access to it must be passed to the function.
183 if Is_Build_In_Place_Function_Call
(Nam
) then
184 Make_Build_In_Place_Call_In_Anonymous_Context
(Nam
);
186 -- Ada 2005 (AI-318-02): Specialization of previous case for renaming
187 -- containing build-in-place function calls whose returned object covers
190 elsif Present
(Unqual_BIP_Iface_Function_Call
(Nam
)) then
191 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(Nam
);
194 -- Create renaming entry for debug information. Mark the entity as
195 -- needing debug info if it comes from sources because the current
196 -- setting in Freeze_Entity occurs too late. ???
198 if Comes_From_Source
(Defining_Identifier
(N
)) then
199 Set_Debug_Info_Needed
(Defining_Identifier
(N
));
202 Decl
:= Debug_Renaming_Declaration
(N
);
204 if Present
(Decl
) then
205 Insert_Action
(N
, Decl
);
207 end Expand_N_Object_Renaming_Declaration
;
209 -------------------------------------------
210 -- Expand_N_Package_Renaming_Declaration --
211 -------------------------------------------
213 procedure Expand_N_Package_Renaming_Declaration
(N
: Node_Id
) is
217 Decl
:= Debug_Renaming_Declaration
(N
);
219 if Present
(Decl
) then
221 -- If we are in a compilation unit, then this is an outer
222 -- level declaration, and must have a scope of Standard
224 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
226 Aux
: constant Node_Id
:= Aux_Decls_Node
(Parent
(N
));
229 Push_Scope
(Standard_Standard
);
231 if No
(Actions
(Aux
)) then
232 Set_Actions
(Aux
, New_List
(Decl
));
234 Append
(Decl
, Actions
(Aux
));
239 -- Enter the debug variable in the qualification list, which
240 -- must be done at this point because auxiliary declarations
241 -- occur at the library level and aren't associated with a
244 Qualify_Entity_Names
(Decl
);
249 -- Otherwise, just insert after the package declaration
252 Insert_Action
(N
, Decl
);
255 end Expand_N_Package_Renaming_Declaration
;
257 ----------------------------------------------
258 -- Expand_N_Subprogram_Renaming_Declaration --
259 ----------------------------------------------
261 procedure Expand_N_Subprogram_Renaming_Declaration
(N
: Node_Id
) is
262 Loc
: constant Source_Ptr
:= Sloc
(N
);
263 Id
: constant Entity_Id
:= Defining_Entity
(N
);
265 function Build_Body_For_Renaming
(Typ
: Entity_Id
) return Node_Id
;
266 -- Build and return the body for the renaming declaration of an equality
267 -- or inequality operator of type Typ.
269 -----------------------------
270 -- Build_Body_For_Renaming --
271 -----------------------------
273 function Build_Body_For_Renaming
(Typ
: Entity_Id
) return Node_Id
is
274 Left
: constant Entity_Id
:= First_Formal
(Id
);
275 Right
: constant Entity_Id
:= Next_Formal
(Left
);
280 Set_Alias
(Id
, Empty
);
281 Set_Has_Completion
(Id
, False);
283 Make_Subprogram_Declaration
(Sloc
(N
),
284 Specification
=> Specification
(N
)));
285 Set_Has_Delayed_Freeze
(Id
);
287 Body_Id
:= Make_Defining_Identifier
(Sloc
(N
), Chars
(Id
));
288 Set_Debug_Info_Needed
(Body_Id
);
290 if Has_Variant_Part
(Typ
) then
292 Build_Variant_Record_Equality
295 Param_Specs
=> Copy_Parameter_List
(Id
));
297 -- Build body for renamed equality, to capture its current meaning.
298 -- It may be redefined later, but the renaming is elaborated where
299 -- it occurs. This is technically known as Squirreling semantics.
300 -- Renaming is rewritten as a subprogram declaration, and the
301 -- generated body is inserted into the freeze actions for the
306 Make_Subprogram_Body
(Loc
,
308 Make_Function_Specification
(Loc
,
309 Defining_Unit_Name
=> Body_Id
,
310 Parameter_Specifications
=> Copy_Parameter_List
(Id
),
312 New_Occurrence_Of
(Standard_Boolean
, Loc
)),
313 Declarations
=> Empty_List
,
314 Handled_Statement_Sequence
=> Empty
);
316 Set_Handled_Statement_Sequence
(Decl
,
317 Make_Handled_Sequence_Of_Statements
(Loc
,
318 Statements
=> New_List
(
319 Make_Simple_Return_Statement
(Loc
,
321 Expand_Record_Equality
324 Lhs
=> Make_Identifier
(Loc
, Chars
(Left
)),
325 Rhs
=> Make_Identifier
(Loc
, Chars
(Right
)),
326 Bodies
=> Declarations
(Decl
))))));
330 end Build_Body_For_Renaming
;
334 Nam
: constant Node_Id
:= Name
(N
);
336 -- Start of processing for Expand_N_Subprogram_Renaming_Declaration
339 -- When the prefix of the name is a function call, we must force the
340 -- call to be made by removing side effects from the call, since we
341 -- must only call the function once.
343 if Nkind
(Nam
) = N_Selected_Component
344 and then Nkind
(Prefix
(Nam
)) = N_Function_Call
346 Remove_Side_Effects
(Prefix
(Nam
));
348 -- For an explicit dereference, the prefix must be captured to prevent
349 -- reevaluation on calls through the renaming, which could result in
350 -- calling the wrong subprogram if the access value were to be changed.
352 elsif Nkind
(Nam
) = N_Explicit_Dereference
then
353 Force_Evaluation
(Prefix
(Nam
));
356 -- Handle cases where we build a body for a renamed equality
358 if Is_Entity_Name
(Nam
)
359 and then Chars
(Entity
(Nam
)) = Name_Op_Eq
360 and then Scope
(Entity
(Nam
)) = Standard_Standard
363 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Id
));
366 -- Check whether this is a renaming of a predefined equality on an
367 -- untagged record type (AI05-0123).
369 if Ada_Version
>= Ada_2012
370 and then Is_Record_Type
(Typ
)
371 and then not Is_Tagged_Type
(Typ
)
372 and then not Is_Frozen
(Typ
)
374 Append_Freeze_Action
(Id
, Build_Body_For_Renaming
(Typ
));
378 end Expand_N_Subprogram_Renaming_Declaration
;