jit: document union types
[official-gcc.git] / gcc / ada / exp_ch8.adb
blob2c47b7f2894018b7a18ba30235e2f2f680e909ee
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 8 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, 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 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Einfo; use Einfo;
28 with Exp_Ch4; use Exp_Ch4;
29 with Exp_Ch6; use Exp_Ch6;
30 with Exp_Dbug; use Exp_Dbug;
31 with Exp_Util; use Exp_Util;
32 with Freeze; use Freeze;
33 with Ghost; use Ghost;
34 with Namet; use Namet;
35 with Nmake; use Nmake;
36 with Nlists; use Nlists;
37 with Opt; use Opt;
38 with Sem; use Sem;
39 with Sem_Ch8; use Sem_Ch8;
40 with Sem_Util; use Sem_Util;
41 with Sinfo; use Sinfo;
42 with Snames; use Snames;
43 with Stand; use Stand;
44 with Tbuild; use Tbuild;
46 package body Exp_Ch8 is
48 ---------------------------------------------
49 -- Expand_N_Exception_Renaming_Declaration --
50 ---------------------------------------------
52 procedure Expand_N_Exception_Renaming_Declaration (N : Node_Id) is
53 GM : constant Ghost_Mode_Type := Ghost_Mode;
54 Decl : Node_Id;
56 begin
57 -- The exception renaming declaration may be subject to pragma Ghost
58 -- with policy Ignore. Set the mode now to ensure that any nodes
59 -- generated during expansion are properly flagged as ignored Ghost.
61 Set_Ghost_Mode (N);
63 Decl := Debug_Renaming_Declaration (N);
65 if Present (Decl) then
66 Insert_Action (N, Decl);
67 end if;
69 -- Restore the original Ghost mode once analysis and expansion have
70 -- taken place.
72 Ghost_Mode := GM;
73 end Expand_N_Exception_Renaming_Declaration;
75 ------------------------------------------
76 -- Expand_N_Object_Renaming_Declaration --
77 ------------------------------------------
79 -- Most object renaming cases can be done by just capturing the address
80 -- of the renamed object. The cases in which this is not true are when
81 -- this address is not computable, since it involves extraction of a
82 -- packed array element, or of a record component to which a component
83 -- clause applies (that can specify an arbitrary bit boundary), or where
84 -- the enclosing record itself has a non-standard representation.
86 -- In these two cases, we pre-evaluate the renaming expression, by
87 -- extracting and freezing the values of any subscripts, and then we
88 -- set the flag Is_Renaming_Of_Object which means that any reference
89 -- to the object will be handled by macro substitution in the front
90 -- end, and the back end will know to ignore the renaming declaration.
92 -- An additional odd case that requires processing by expansion is
93 -- the renaming of a discriminant of a mutable record type. The object
94 -- is a constant because it renames something that cannot be assigned to,
95 -- but in fact the underlying value can change and must be reevaluated
96 -- at each reference. Gigi does have a notion of a "constant view" of
97 -- an object, and therefore the front-end must perform the expansion.
98 -- For simplicity, and to bypass some obscure code-generation problem,
99 -- we use macro substitution for all renamed discriminants, whether the
100 -- enclosing type is constrained or not.
102 -- The other special processing required is for the case of renaming
103 -- of an object of a class wide type, where it is necessary to build
104 -- the appropriate subtype for the renamed object.
105 -- More comments needed for this para ???
107 procedure Expand_N_Object_Renaming_Declaration (N : Node_Id) is
108 Nam : constant Node_Id := Name (N);
109 Decl : Node_Id;
110 T : Entity_Id;
112 function Evaluation_Required (Nam : Node_Id) return Boolean;
113 -- Determines whether it is necessary to do static name evaluation for
114 -- renaming of Nam. It is considered necessary if evaluating the name
115 -- involves indexing a packed array, or extracting a component of a
116 -- record to which a component clause applies. Note that we are only
117 -- interested in these operations if they occur as part of the name
118 -- itself, subscripts are just values that are computed as part of the
119 -- evaluation, so their form is unimportant.
121 -------------------------
122 -- Evaluation_Required --
123 -------------------------
125 function Evaluation_Required (Nam : Node_Id) return Boolean is
126 begin
127 if Nkind_In (Nam, N_Indexed_Component, N_Slice) then
128 if Is_Packed (Etype (Prefix (Nam))) then
129 return True;
130 else
131 return Evaluation_Required (Prefix (Nam));
132 end if;
134 elsif Nkind (Nam) = N_Selected_Component then
135 declare
136 Rec_Type : constant Entity_Id := Etype (Prefix (Nam));
138 begin
139 if Present (Component_Clause (Entity (Selector_Name (Nam))))
140 or else Has_Non_Standard_Rep (Rec_Type)
141 then
142 return True;
144 elsif Ekind (Entity (Selector_Name (Nam))) = E_Discriminant
145 and then Is_Record_Type (Rec_Type)
146 and then not Is_Concurrent_Record_Type (Rec_Type)
147 then
148 return True;
150 else
151 return Evaluation_Required (Prefix (Nam));
152 end if;
153 end;
155 else
156 return False;
157 end if;
158 end Evaluation_Required;
160 -- Local variables
162 GM : constant Ghost_Mode_Type := Ghost_Mode;
164 -- Start of processing for Expand_N_Object_Renaming_Declaration
166 begin
167 -- The object renaming declaration may be subject to pragma Ghost with
168 -- policy Ignore. Set the mode now to ensure that any nodes generated
169 -- during expansion are properly flagged as ignored Ghost.
171 Set_Ghost_Mode (N);
173 -- Perform name evaluation if required
175 if Evaluation_Required (Nam) then
176 Evaluate_Name (Nam);
177 Set_Is_Renaming_Of_Object (Defining_Identifier (N));
178 end if;
180 -- Deal with construction of subtype in class-wide case
182 T := Etype (Defining_Identifier (N));
184 if Is_Class_Wide_Type (T) then
185 Expand_Subtype_From_Expr (N, T, Subtype_Mark (N), Name (N));
186 Find_Type (Subtype_Mark (N));
187 Set_Etype (Defining_Identifier (N), Entity (Subtype_Mark (N)));
189 -- Freeze the class-wide subtype here to ensure that the subtype
190 -- and equivalent type are frozen before the renaming.
192 Freeze_Before (N, Entity (Subtype_Mark (N)));
193 end if;
195 -- Ada 2005 (AI-318-02): If the renamed object is a call to a build-in-
196 -- place function, then a temporary return object needs to be created
197 -- and access to it must be passed to the function. Currently we limit
198 -- such functions to those with inherently limited result subtypes, but
199 -- eventually we plan to expand the functions that are treated as
200 -- build-in-place to include other composite result types.
202 if Ada_Version >= Ada_2005
203 and then Is_Build_In_Place_Function_Call (Nam)
204 then
205 Make_Build_In_Place_Call_In_Anonymous_Context (Nam);
206 end if;
208 -- Create renaming entry for debug information
210 Decl := Debug_Renaming_Declaration (N);
212 if Present (Decl) then
213 Insert_Action (N, Decl);
214 end if;
216 -- Restore the original Ghost mode once analysis and expansion have
217 -- taken place.
219 Ghost_Mode := GM;
220 end Expand_N_Object_Renaming_Declaration;
222 -------------------------------------------
223 -- Expand_N_Package_Renaming_Declaration --
224 -------------------------------------------
226 procedure Expand_N_Package_Renaming_Declaration (N : Node_Id) is
227 GM : constant Ghost_Mode_Type := Ghost_Mode;
228 Decl : Node_Id;
230 begin
231 -- The package renaming declaration may be subject to pragma Ghost with
232 -- policy Ignore. Set the mode now to ensure that any nodes generated
233 -- during expansion are properly flagged as ignored Ghost.
235 Set_Ghost_Mode (N);
237 Decl := Debug_Renaming_Declaration (N);
239 if Present (Decl) then
241 -- If we are in a compilation unit, then this is an outer
242 -- level declaration, and must have a scope of Standard
244 if Nkind (Parent (N)) = N_Compilation_Unit then
245 declare
246 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
248 begin
249 Push_Scope (Standard_Standard);
251 if No (Actions (Aux)) then
252 Set_Actions (Aux, New_List (Decl));
253 else
254 Append (Decl, Actions (Aux));
255 end if;
257 Analyze (Decl);
259 -- Enter the debug variable in the qualification list, which
260 -- must be done at this point because auxiliary declarations
261 -- occur at the library level and aren't associated with a
262 -- normal scope.
264 Qualify_Entity_Names (Decl);
266 Pop_Scope;
267 end;
269 -- Otherwise, just insert after the package declaration
271 else
272 Insert_Action (N, Decl);
273 end if;
274 end if;
276 -- Restore the original Ghost mode once analysis and expansion have
277 -- taken place.
279 Ghost_Mode := GM;
280 end Expand_N_Package_Renaming_Declaration;
282 ----------------------------------------------
283 -- Expand_N_Subprogram_Renaming_Declaration --
284 ----------------------------------------------
286 procedure Expand_N_Subprogram_Renaming_Declaration (N : Node_Id) is
287 Loc : constant Source_Ptr := Sloc (N);
288 Id : constant Entity_Id := Defining_Entity (N);
290 function Build_Body_For_Renaming return Node_Id;
291 -- Build and return the body for the renaming declaration of an equality
292 -- or inequality operator.
294 -----------------------------
295 -- Build_Body_For_Renaming --
296 -----------------------------
298 function Build_Body_For_Renaming return Node_Id is
299 Body_Id : Entity_Id;
300 Decl : Node_Id;
302 begin
303 Set_Alias (Id, Empty);
304 Set_Has_Completion (Id, False);
305 Rewrite (N,
306 Make_Subprogram_Declaration (Sloc (N),
307 Specification => Specification (N)));
308 Set_Has_Delayed_Freeze (Id);
310 Body_Id := Make_Defining_Identifier (Sloc (N), Chars (Id));
311 Set_Debug_Info_Needed (Body_Id);
313 Decl :=
314 Make_Subprogram_Body (Loc,
315 Specification =>
316 Make_Function_Specification (Loc,
317 Defining_Unit_Name => Body_Id,
318 Parameter_Specifications => Copy_Parameter_List (Id),
319 Result_Definition =>
320 New_Occurrence_Of (Standard_Boolean, Loc)),
321 Declarations => Empty_List,
322 Handled_Statement_Sequence => Empty);
324 return Decl;
325 end Build_Body_For_Renaming;
327 -- Local variables
329 GM : constant Ghost_Mode_Type := Ghost_Mode;
330 Nam : constant Node_Id := Name (N);
332 -- Start of processing for Expand_N_Subprogram_Renaming_Declaration
334 begin
335 -- The subprogram renaming declaration may be subject to pragma Ghost
336 -- with policy Ignore. Set the mode now to ensure that any nodes created
337 -- during expansion are properly flagged as ignored Ghost.
339 Set_Ghost_Mode (N);
341 -- When the prefix of the name is a function call, we must force the
342 -- call to be made by removing side effects from the call, since we
343 -- must only call the function once.
345 if Nkind (Nam) = N_Selected_Component
346 and then Nkind (Prefix (Nam)) = N_Function_Call
347 then
348 Remove_Side_Effects (Prefix (Nam));
350 -- For an explicit dereference, the prefix must be captured to prevent
351 -- reevaluation on calls through the renaming, which could result in
352 -- calling the wrong subprogram if the access value were to be changed.
354 elsif Nkind (Nam) = N_Explicit_Dereference then
355 Force_Evaluation (Prefix (Nam));
356 end if;
358 -- Handle cases where we build a body for a renamed equality
360 if Is_Entity_Name (Nam)
361 and then Chars (Entity (Nam)) = Name_Op_Eq
362 and then Scope (Entity (Nam)) = Standard_Standard
363 then
364 declare
365 Left : constant Entity_Id := First_Formal (Id);
366 Right : constant Entity_Id := Next_Formal (Left);
367 Typ : constant Entity_Id := Etype (Left);
368 Decl : Node_Id;
370 begin
371 -- Check whether this is a renaming of a predefined equality on an
372 -- untagged record type (AI05-0123).
374 if Ada_Version >= Ada_2012
375 and then Is_Record_Type (Typ)
376 and then not Is_Tagged_Type (Typ)
377 and then not Is_Frozen (Typ)
378 then
379 -- Build body for renamed equality, to capture its current
380 -- meaning. It may be redefined later, but the renaming is
381 -- elaborated where it occurs. This is technically known as
382 -- Squirreling semantics. Renaming is rewritten as a subprogram
383 -- declaration, and the generated body is inserted into the
384 -- freeze actions for the subprogram.
386 Decl := Build_Body_For_Renaming;
388 Set_Handled_Statement_Sequence (Decl,
389 Make_Handled_Sequence_Of_Statements (Loc,
390 Statements => New_List (
391 Make_Simple_Return_Statement (Loc,
392 Expression =>
393 Expand_Record_Equality
394 (Id,
395 Typ => Typ,
396 Lhs => Make_Identifier (Loc, Chars (Left)),
397 Rhs => Make_Identifier (Loc, Chars (Right)),
398 Bodies => Declarations (Decl))))));
400 Append_Freeze_Action (Id, Decl);
401 end if;
402 end;
403 end if;
405 -- Restore the original Ghost mode once analysis and expansion have
406 -- taken place.
408 Ghost_Mode := GM;
409 end Expand_N_Subprogram_Renaming_Declaration;
411 end Exp_Ch8;