]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/exp_ch4.adb
[multiple changes]
[thirdparty/gcc.git] / gcc / ada / exp_ch4.adb
CommitLineData
70482933
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- E X P _ C H 4 --
59262ebb 6-- --
70482933
RK
7-- B o d y --
8-- --
760804f3 9-- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
70482933
RK
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- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
70482933
RK
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 --
b5c84c3c
RD
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. --
70482933
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
70482933
RK
23-- --
24------------------------------------------------------------------------------
25
26with Atree; use Atree;
27with Checks; use Checks;
bded454f 28with Debug; use Debug;
70482933
RK
29with Einfo; use Einfo;
30with Elists; use Elists;
31with Errout; use Errout;
32with Exp_Aggr; use Exp_Aggr;
0669bebe 33with Exp_Atag; use Exp_Atag;
6cce2156 34with Exp_Ch2; use Exp_Ch2;
70482933 35with Exp_Ch3; use Exp_Ch3;
20b5d666 36with Exp_Ch6; use Exp_Ch6;
70482933
RK
37with Exp_Ch7; use Exp_Ch7;
38with Exp_Ch9; use Exp_Ch9;
20b5d666 39with Exp_Disp; use Exp_Disp;
70482933 40with Exp_Fixd; use Exp_Fixd;
437f8c1e 41with Exp_Intr; use Exp_Intr;
70482933
RK
42with Exp_Pakd; use Exp_Pakd;
43with Exp_Tss; use Exp_Tss;
44with Exp_Util; use Exp_Util;
f02b8bb8 45with Freeze; use Freeze;
70482933 46with Inline; use Inline;
df3e68b1 47with Lib; use Lib;
26bff3d9 48with Namet; use Namet;
70482933
RK
49with Nlists; use Nlists;
50with Nmake; use Nmake;
51with Opt; use Opt;
25adc5fb 52with Par_SCO; use Par_SCO;
0669bebe
GB
53with Restrict; use Restrict;
54with Rident; use Rident;
70482933
RK
55with Rtsfind; use Rtsfind;
56with Sem; use Sem;
a4100e55 57with Sem_Aux; use Sem_Aux;
70482933 58with Sem_Cat; use Sem_Cat;
5d09245e 59with Sem_Ch3; use Sem_Ch3;
11fa950b 60with Sem_Ch8; use Sem_Ch8;
70482933
RK
61with Sem_Ch13; use Sem_Ch13;
62with Sem_Eval; use Sem_Eval;
63with Sem_Res; use Sem_Res;
64with Sem_Type; use Sem_Type;
65with Sem_Util; use Sem_Util;
07fc65c4 66with Sem_Warn; use Sem_Warn;
70482933 67with Sinfo; use Sinfo;
70482933
RK
68with Snames; use Snames;
69with Stand; use Stand;
7665e4bd 70with SCIL_LL; use SCIL_LL;
07fc65c4 71with Targparm; use Targparm;
70482933
RK
72with Tbuild; use Tbuild;
73with Ttypes; use Ttypes;
74with Uintp; use Uintp;
75with Urealp; use Urealp;
76with Validsw; use Validsw;
77
78package body Exp_Ch4 is
79
15ce9ca2
AC
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
70482933
RK
83
84 procedure Binary_Op_Validity_Checks (N : Node_Id);
85 pragma Inline (Binary_Op_Validity_Checks);
86 -- Performs validity checks for a binary operator
87
fbf5a39b
AC
88 procedure Build_Boolean_Array_Proc_Call
89 (N : Node_Id;
90 Op1 : Node_Id;
91 Op2 : Node_Id);
303b4d58 92 -- If a boolean array assignment can be done in place, build call to
fbf5a39b
AC
93 -- corresponding library procedure.
94
11fa950b
AC
95 function Current_Anonymous_Master return Entity_Id;
96 -- Return the entity of the heterogeneous finalization master belonging to
97 -- the current unit (either function, package or procedure). This master
98 -- services all anonymous access-to-controlled types. If the current unit
99 -- does not have such master, create one.
df3e68b1 100
26bff3d9
JM
101 procedure Displace_Allocator_Pointer (N : Node_Id);
102 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
103 -- Expand_Allocator_Expression. Allocating class-wide interface objects
104 -- this routine displaces the pointer to the allocated object to reference
105 -- the component referencing the corresponding secondary dispatch table.
106
fbf5a39b
AC
107 procedure Expand_Allocator_Expression (N : Node_Id);
108 -- Subsidiary to Expand_N_Allocator, for the case when the expression
109 -- is a qualified expression or an aggregate.
110
70482933
RK
111 procedure Expand_Array_Comparison (N : Node_Id);
112 -- This routine handles expansion of the comparison operators (N_Op_Lt,
113 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
114 -- code for these operators is similar, differing only in the details of
fbf5a39b
AC
115 -- the actual comparison call that is made. Special processing (call a
116 -- run-time routine)
70482933
RK
117
118 function Expand_Array_Equality
119 (Nod : Node_Id;
70482933
RK
120 Lhs : Node_Id;
121 Rhs : Node_Id;
0da2c8ac
AC
122 Bodies : List_Id;
123 Typ : Entity_Id) return Node_Id;
70482933 124 -- Expand an array equality into a call to a function implementing this
685094bf
RD
125 -- equality, and a call to it. Loc is the location for the generated nodes.
126 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
127 -- on which to attach bodies of local functions that are created in the
128 -- process. It is the responsibility of the caller to insert those bodies
129 -- at the right place. Nod provides the Sloc value for the generated code.
130 -- Normally the types used for the generated equality routine are taken
131 -- from Lhs and Rhs. However, in some situations of generated code, the
132 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
133 -- the type to be used for the formal parameters.
70482933
RK
134
135 procedure Expand_Boolean_Operator (N : Node_Id);
685094bf
RD
136 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
137 -- case of array type arguments.
70482933 138
5875f8d6
AC
139 procedure Expand_Short_Circuit_Operator (N : Node_Id);
140 -- Common expansion processing for short-circuit boolean operators
141
456cbfa5 142 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id);
5707e389
AC
143 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
144 -- where we allow comparison of "out of range" values.
456cbfa5 145
70482933
RK
146 function Expand_Composite_Equality
147 (Nod : Node_Id;
148 Typ : Entity_Id;
149 Lhs : Node_Id;
150 Rhs : Node_Id;
2e071734 151 Bodies : List_Id) return Node_Id;
685094bf
RD
152 -- Local recursive function used to expand equality for nested composite
153 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
d26d790d
AC
154 -- to attach bodies of local functions that are created in the process. It
155 -- is the responsibility of the caller to insert those bodies at the right
156 -- place. Nod provides the Sloc value for generated code. Lhs and Rhs are
157 -- the left and right sides for the comparison, and Typ is the type of the
158 -- objects to compare.
70482933 159
fdac1f80
AC
160 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
161 -- Routine to expand concatenation of a sequence of two or more operands
162 -- (in the list Operands) and replace node Cnode with the result of the
163 -- concatenation. The operands can be of any appropriate type, and can
164 -- include both arrays and singleton elements.
70482933 165
f6194278 166 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id);
5707e389
AC
167 -- N is an N_In membership test mode, with the overflow check mode set to
168 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
169 -- integer type. This is a case where top level processing is required to
170 -- handle overflow checks in subtrees.
f6194278 171
70482933 172 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
685094bf
RD
173 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
174 -- fixed. We do not have such a type at runtime, so the purpose of this
175 -- routine is to find the real type by looking up the tree. We also
176 -- determine if the operation must be rounded.
70482933 177
5d09245e
AC
178 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
179 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
180 -- discriminants if it has a constrained nominal type, unless the object
181 -- is a component of an enclosing Unchecked_Union object that is subject
182 -- to a per-object constraint and the enclosing object lacks inferable
183 -- discriminants.
184 --
185 -- An expression of an Unchecked_Union type has inferable discriminants
186 -- if it is either a name of an object with inferable discriminants or a
187 -- qualified expression whose subtype mark denotes a constrained subtype.
188
70482933 189 procedure Insert_Dereference_Action (N : Node_Id);
e6f69614
AC
190 -- N is an expression whose type is an access. When the type of the
191 -- associated storage pool is derived from Checked_Pool, generate a
192 -- call to the 'Dereference' primitive operation.
70482933
RK
193
194 function Make_Array_Comparison_Op
2e071734
AC
195 (Typ : Entity_Id;
196 Nod : Node_Id) return Node_Id;
685094bf
RD
197 -- Comparisons between arrays are expanded in line. This function produces
198 -- the body of the implementation of (a > b), where a and b are one-
199 -- dimensional arrays of some discrete type. The original node is then
200 -- expanded into the appropriate call to this function. Nod provides the
201 -- Sloc value for the generated code.
70482933
RK
202
203 function Make_Boolean_Array_Op
2e071734
AC
204 (Typ : Entity_Id;
205 N : Node_Id) return Node_Id;
685094bf
RD
206 -- Boolean operations on boolean arrays are expanded in line. This function
207 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
208 -- b). It is used only the normal case and not the packed case. The type
209 -- involved, Typ, is the Boolean array type, and the logical operations in
210 -- the body are simple boolean operations. Note that Typ is always a
211 -- constrained type (the caller has ensured this by using
212 -- Convert_To_Actual_Subtype if necessary).
70482933 213
b6b5cca8 214 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean;
a7f1b24f
RD
215 -- For signed arithmetic operations when the current overflow mode is
216 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
217 -- as the first thing we do. We then return. We count on the recursive
218 -- apparatus for overflow checks to call us back with an equivalent
219 -- operation that is in CHECKED mode, avoiding a recursive entry into this
220 -- routine, and that is when we will proceed with the expansion of the
221 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
222 -- these optimizations without first making this check, since there may be
223 -- operands further down the tree that are relying on the recursive calls
224 -- triggered by the top level nodes to properly process overflow checking
225 -- and remaining expansion on these nodes. Note that this call back may be
226 -- skipped if the operation is done in Bignum mode but that's fine, since
227 -- the Bignum call takes care of everything.
b6b5cca8 228
0580d807
AC
229 procedure Optimize_Length_Comparison (N : Node_Id);
230 -- Given an expression, if it is of the form X'Length op N (or the other
231 -- way round), where N is known at compile time to be 0 or 1, and X is a
232 -- simple entity, and op is a comparison operator, optimizes it into a
233 -- comparison of First and Last.
234
b2c28399
AC
235 procedure Process_Transient_Object
236 (Decl : Node_Id;
237 Rel_Node : Node_Id);
238 -- Subsidiary routine to the expansion of expression_with_actions and if
239 -- expressions. Generate all the necessary code to finalize a transient
240 -- controlled object when the enclosing context is elaborated or evaluated.
241 -- Decl denotes the declaration of the transient controlled object which is
242 -- usually the result of a controlled function call. Rel_Node denotes the
243 -- context, either an expression_with_actions or an if expression.
244
70482933 245 procedure Rewrite_Comparison (N : Node_Id);
20b5d666 246 -- If N is the node for a comparison whose outcome can be determined at
d26dc4b5
AC
247 -- compile time, then the node N can be rewritten with True or False. If
248 -- the outcome cannot be determined at compile time, the call has no
249 -- effect. If N is a type conversion, then this processing is applied to
250 -- its expression. If N is neither comparison nor a type conversion, the
251 -- call has no effect.
70482933 252
82878151
AC
253 procedure Tagged_Membership
254 (N : Node_Id;
255 SCIL_Node : out Node_Id;
256 Result : out Node_Id);
70482933
RK
257 -- Construct the expression corresponding to the tagged membership test.
258 -- Deals with a second operand being (or not) a class-wide type.
259
fbf5a39b 260 function Safe_In_Place_Array_Op
2e071734
AC
261 (Lhs : Node_Id;
262 Op1 : Node_Id;
263 Op2 : Node_Id) return Boolean;
685094bf
RD
264 -- In the context of an assignment, where the right-hand side is a boolean
265 -- operation on arrays, check whether operation can be performed in place.
fbf5a39b 266
70482933
RK
267 procedure Unary_Op_Validity_Checks (N : Node_Id);
268 pragma Inline (Unary_Op_Validity_Checks);
269 -- Performs validity checks for a unary operator
270
271 -------------------------------
272 -- Binary_Op_Validity_Checks --
273 -------------------------------
274
275 procedure Binary_Op_Validity_Checks (N : Node_Id) is
276 begin
277 if Validity_Checks_On and Validity_Check_Operands then
278 Ensure_Valid (Left_Opnd (N));
279 Ensure_Valid (Right_Opnd (N));
280 end if;
281 end Binary_Op_Validity_Checks;
282
fbf5a39b
AC
283 ------------------------------------
284 -- Build_Boolean_Array_Proc_Call --
285 ------------------------------------
286
287 procedure Build_Boolean_Array_Proc_Call
288 (N : Node_Id;
289 Op1 : Node_Id;
290 Op2 : Node_Id)
291 is
292 Loc : constant Source_Ptr := Sloc (N);
293 Kind : constant Node_Kind := Nkind (Expression (N));
294 Target : constant Node_Id :=
295 Make_Attribute_Reference (Loc,
296 Prefix => Name (N),
297 Attribute_Name => Name_Address);
298
bed8af19 299 Arg1 : Node_Id := Op1;
fbf5a39b
AC
300 Arg2 : Node_Id := Op2;
301 Call_Node : Node_Id;
302 Proc_Name : Entity_Id;
303
304 begin
305 if Kind = N_Op_Not then
306 if Nkind (Op1) in N_Binary_Op then
307
5e1c00fa 308 -- Use negated version of the binary operators
fbf5a39b
AC
309
310 if Nkind (Op1) = N_Op_And then
311 Proc_Name := RTE (RE_Vector_Nand);
312
313 elsif Nkind (Op1) = N_Op_Or then
314 Proc_Name := RTE (RE_Vector_Nor);
315
316 else pragma Assert (Nkind (Op1) = N_Op_Xor);
317 Proc_Name := RTE (RE_Vector_Xor);
318 end if;
319
320 Call_Node :=
321 Make_Procedure_Call_Statement (Loc,
322 Name => New_Occurrence_Of (Proc_Name, Loc),
323
324 Parameter_Associations => New_List (
325 Target,
326 Make_Attribute_Reference (Loc,
327 Prefix => Left_Opnd (Op1),
328 Attribute_Name => Name_Address),
329
330 Make_Attribute_Reference (Loc,
331 Prefix => Right_Opnd (Op1),
332 Attribute_Name => Name_Address),
333
334 Make_Attribute_Reference (Loc,
335 Prefix => Left_Opnd (Op1),
336 Attribute_Name => Name_Length)));
337
338 else
339 Proc_Name := RTE (RE_Vector_Not);
340
341 Call_Node :=
342 Make_Procedure_Call_Statement (Loc,
343 Name => New_Occurrence_Of (Proc_Name, Loc),
344 Parameter_Associations => New_List (
345 Target,
346
347 Make_Attribute_Reference (Loc,
348 Prefix => Op1,
349 Attribute_Name => Name_Address),
350
351 Make_Attribute_Reference (Loc,
352 Prefix => Op1,
353 Attribute_Name => Name_Length)));
354 end if;
355
356 else
357 -- We use the following equivalences:
358
359 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
360 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
361 -- (not X) xor (not Y) = X xor Y
362 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
363
364 if Nkind (Op1) = N_Op_Not then
bed8af19
AC
365 Arg1 := Right_Opnd (Op1);
366 Arg2 := Right_Opnd (Op2);
533369aa 367
fbf5a39b
AC
368 if Kind = N_Op_And then
369 Proc_Name := RTE (RE_Vector_Nor);
fbf5a39b
AC
370 elsif Kind = N_Op_Or then
371 Proc_Name := RTE (RE_Vector_Nand);
fbf5a39b
AC
372 else
373 Proc_Name := RTE (RE_Vector_Xor);
374 end if;
375
376 else
377 if Kind = N_Op_And then
378 Proc_Name := RTE (RE_Vector_And);
fbf5a39b
AC
379 elsif Kind = N_Op_Or then
380 Proc_Name := RTE (RE_Vector_Or);
fbf5a39b
AC
381 elsif Nkind (Op2) = N_Op_Not then
382 Proc_Name := RTE (RE_Vector_Nxor);
383 Arg2 := Right_Opnd (Op2);
fbf5a39b
AC
384 else
385 Proc_Name := RTE (RE_Vector_Xor);
386 end if;
387 end if;
388
389 Call_Node :=
390 Make_Procedure_Call_Statement (Loc,
391 Name => New_Occurrence_Of (Proc_Name, Loc),
392 Parameter_Associations => New_List (
393 Target,
955871d3
AC
394 Make_Attribute_Reference (Loc,
395 Prefix => Arg1,
396 Attribute_Name => Name_Address),
397 Make_Attribute_Reference (Loc,
398 Prefix => Arg2,
399 Attribute_Name => Name_Address),
400 Make_Attribute_Reference (Loc,
a8ef12e5 401 Prefix => Arg1,
955871d3 402 Attribute_Name => Name_Length)));
fbf5a39b
AC
403 end if;
404
405 Rewrite (N, Call_Node);
406 Analyze (N);
407
408 exception
409 when RE_Not_Available =>
410 return;
411 end Build_Boolean_Array_Proc_Call;
412
11fa950b
AC
413 ------------------------------
414 -- Current_Anonymous_Master --
415 ------------------------------
df3e68b1 416
11fa950b 417 function Current_Anonymous_Master return Entity_Id is
57ae790f 418 function Create_Anonymous_Master
57d22af2
AC
419 (Unit_Id : Entity_Id;
420 Unit_Decl : Node_Id) return Entity_Id;
421 -- Create a new anonymous master for a compilation unit denoted by its
422 -- entity Unit_Id and declaration Unit_Decl. The declaration of the new
423 -- master along with any specialized initialization is inserted at the
424 -- top of the unit's declarations (see body for special cases). Return
425 -- the entity of the anonymous master.
57ae790f
HK
426
427 -----------------------------
428 -- Create_Anonymous_Master --
429 -----------------------------
430
431 function Create_Anonymous_Master
57d22af2
AC
432 (Unit_Id : Entity_Id;
433 Unit_Decl : Node_Id) return Entity_Id
57ae790f 434 is
57d22af2
AC
435 Insert_Nod : Node_Id := Empty;
436 -- The point of insertion into the declarative list of the unit. All
437 -- nodes are inserted before Insert_Nod.
57ae790f 438
57d22af2
AC
439 procedure Insert_And_Analyze (Decls : List_Id; N : Node_Id);
440 -- Insert arbitrary node N in declarative list Decls and analyze it
57ae790f
HK
441
442 ------------------------
443 -- Insert_And_Analyze --
444 ------------------------
445
57d22af2 446 procedure Insert_And_Analyze (Decls : List_Id; N : Node_Id) is
57ae790f 447 begin
57d22af2
AC
448 -- The declarative list is already populated, the nodes are
449 -- inserted at the top of the list, preserving their order.
11fa950b 450
57d22af2
AC
451 if Present (Insert_Nod) then
452 Insert_Before (Insert_Nod, N);
11fa950b 453
57ae790f 454 -- Otherwise append to the declarations to preserve order
df3e68b1 455
57ae790f 456 else
57d22af2 457 Append_To (Decls, N);
57ae790f 458 end if;
57d22af2
AC
459
460 Analyze (N);
57ae790f 461 end Insert_And_Analyze;
df3e68b1 462
57ae790f 463 -- Local variables
df3e68b1 464
57d22af2
AC
465 Loc : constant Source_Ptr := Sloc (Unit_Id);
466 Spec_Id : constant Entity_Id := Corresponding_Spec_Of (Unit_Decl);
467 Decls : List_Id;
468 FM_Id : Entity_Id;
469 Pref : Character;
470 Unit_Spec : Node_Id;
f553e7bc 471
57ae790f 472 -- Start of processing for Create_Anonymous_Master
df3e68b1 473
57ae790f 474 begin
57d22af2
AC
475 -- Find the declarative list of the unit
476
477 if Nkind (Unit_Decl) = N_Package_Declaration then
478 Unit_Spec := Specification (Unit_Decl);
479 Decls := Visible_Declarations (Unit_Spec);
480
481 if No (Decls) then
482 Decls := New_List (Make_Null_Statement (Loc));
483 Set_Visible_Declarations (Unit_Spec, Decls);
484 end if;
485
486 -- Package or subprogram body
487
488 -- ??? A subprogram declaration that acts as a compilation unit may
489 -- contain a formal parameter of an anonymous access-to-controlled
490 -- type initialized by an allocator.
491
492 -- procedure Comp_Unit_Proc (Param : access Ctrl := new Ctrl);
493
494 -- There is no suitable place to create the anonymous master as the
495 -- subprogram is not in a declarative list.
496
497 else
498 Decls := Declarations (Unit_Decl);
499
500 if No (Decls) then
501 Decls := New_List (Make_Null_Statement (Loc));
502 Set_Declarations (Unit_Decl, Decls);
503 end if;
ca5af305 504 end if;
df3e68b1 505
57d22af2
AC
506 -- The anonymous master and all initialization actions are inserted
507 -- before the first declaration (if any).
508
509 Insert_Nod := First (Decls);
510
57ae790f
HK
511 -- Since the anonymous master and all its initialization actions are
512 -- inserted at top level, use the scope of the unit when analyzing.
2c17ca0a 513
57d22af2 514 Push_Scope (Spec_Id);
2c17ca0a 515
57d22af2
AC
516 -- Step 1: Anonymous master creation
517
518 -- Use a unique prefix in case the same unit requires two anonymous
519 -- masters, one for the spec (S) and one for the body (B).
520
521 if Ekind_In (Unit_Id, E_Function, E_Package, E_Procedure) then
522 Pref := 'S';
523 else
524 Pref := 'B';
525 end if;
2c17ca0a 526
57ae790f
HK
527 FM_Id :=
528 Make_Defining_Identifier (Loc,
57d22af2
AC
529 New_External_Name
530 (Related_Id => Chars (Unit_Id),
531 Suffix => "AM",
532 Prefix => Pref));
533
57ae790f 534 Set_Anonymous_Master (Unit_Id, FM_Id);
df3e68b1 535
57ae790f
HK
536 -- Generate:
537 -- <FM_Id> : Finalization_Master;
11fa950b 538
57d22af2
AC
539 Insert_And_Analyze (Decls,
540 Make_Object_Declaration (Loc,
57ae790f
HK
541 Defining_Identifier => FM_Id,
542 Object_Definition =>
543 New_Occurrence_Of (RTE (RE_Finalization_Master), Loc)));
11fa950b 544
57d22af2
AC
545 -- Step 2: Initialization actions
546
535a8637
AC
547 -- Generate:
548 -- Set_Base_Pool
549 -- (<FM_Id>, Global_Pool_Object'Unrestricted_Access);
11fa950b 550
535a8637
AC
551 Insert_And_Analyze (Decls,
552 Make_Procedure_Call_Statement (Loc,
553 Name =>
554 New_Occurrence_Of (RTE (RE_Set_Base_Pool), Loc),
555 Parameter_Associations => New_List (
556 New_Occurrence_Of (FM_Id, Loc),
557 Make_Attribute_Reference (Loc,
558 Prefix =>
559 New_Occurrence_Of (RTE (RE_Global_Pool_Object), Loc),
560 Attribute_Name => Name_Unrestricted_Access))));
11fa950b 561
535a8637
AC
562 -- Generate:
563 -- Set_Is_Heterogeneous (<FM_Id>);
11fa950b 564
535a8637
AC
565 Insert_And_Analyze (Decls,
566 Make_Procedure_Call_Statement (Loc,
567 Name =>
568 New_Occurrence_Of (RTE (RE_Set_Is_Heterogeneous), Loc),
569 Parameter_Associations => New_List (
570 New_Occurrence_Of (FM_Id, Loc))));
11fa950b 571
57ae790f 572 Pop_Scope;
57ae790f
HK
573 return FM_Id;
574 end Create_Anonymous_Master;
11fa950b 575
57ae790f 576 -- Local declarations
11fa950b 577
57d22af2
AC
578 Unit_Decl : Node_Id;
579 Unit_Id : Entity_Id;
11fa950b 580
57ae790f 581 -- Start of processing for Current_Anonymous_Master
11fa950b 582
57ae790f 583 begin
57d22af2
AC
584 Unit_Decl := Unit (Cunit (Current_Sem_Unit));
585 Unit_Id := Defining_Entity (Unit_Decl);
586
587 -- The compilation unit is a package instantiation. In this case the
588 -- anonymous master is associated with the package spec as both the
589 -- spec and body appear at the same level.
590
591 if Nkind (Unit_Decl) = N_Package_Body
592 and then Nkind (Original_Node (Unit_Decl)) = N_Package_Instantiation
593 then
594 Unit_Id := Corresponding_Spec (Unit_Decl);
595 Unit_Decl := Unit_Declaration_Node (Unit_Id);
596 end if;
597
598 if Present (Anonymous_Master (Unit_Id)) then
599 return Anonymous_Master (Unit_Id);
11fa950b 600
57ae790f
HK
601 -- Create a new anonymous master when allocating an object of anonymous
602 -- access-to-controlled type for the first time.
11fa950b 603
57d22af2
AC
604 else
605 return Create_Anonymous_Master (Unit_Id, Unit_Decl);
11fa950b
AC
606 end if;
607 end Current_Anonymous_Master;
df3e68b1 608
26bff3d9
JM
609 --------------------------------
610 -- Displace_Allocator_Pointer --
611 --------------------------------
612
613 procedure Displace_Allocator_Pointer (N : Node_Id) is
614 Loc : constant Source_Ptr := Sloc (N);
615 Orig_Node : constant Node_Id := Original_Node (N);
616 Dtyp : Entity_Id;
617 Etyp : Entity_Id;
618 PtrT : Entity_Id;
619
620 begin
303b4d58
AC
621 -- Do nothing in case of VM targets: the virtual machine will handle
622 -- interfaces directly.
623
1f110335 624 if not Tagged_Type_Expansion then
303b4d58
AC
625 return;
626 end if;
627
26bff3d9
JM
628 pragma Assert (Nkind (N) = N_Identifier
629 and then Nkind (Orig_Node) = N_Allocator);
630
631 PtrT := Etype (Orig_Node);
d6a24cdb 632 Dtyp := Available_View (Designated_Type (PtrT));
26bff3d9
JM
633 Etyp := Etype (Expression (Orig_Node));
634
533369aa
AC
635 if Is_Class_Wide_Type (Dtyp) and then Is_Interface (Dtyp) then
636
26bff3d9
JM
637 -- If the type of the allocator expression is not an interface type
638 -- we can generate code to reference the record component containing
639 -- the pointer to the secondary dispatch table.
640
641 if not Is_Interface (Etyp) then
642 declare
643 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
644
645 begin
646 -- 1) Get access to the allocated object
647
648 Rewrite (N,
5972791c 649 Make_Explicit_Dereference (Loc, Relocate_Node (N)));
26bff3d9
JM
650 Set_Etype (N, Etyp);
651 Set_Analyzed (N);
652
653 -- 2) Add the conversion to displace the pointer to reference
654 -- the secondary dispatch table.
655
656 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
657 Analyze_And_Resolve (N, Dtyp);
658
659 -- 3) The 'access to the secondary dispatch table will be used
660 -- as the value returned by the allocator.
661
662 Rewrite (N,
663 Make_Attribute_Reference (Loc,
664 Prefix => Relocate_Node (N),
665 Attribute_Name => Name_Access));
666 Set_Etype (N, Saved_Typ);
667 Set_Analyzed (N);
668 end;
669
670 -- If the type of the allocator expression is an interface type we
671 -- generate a run-time call to displace "this" to reference the
672 -- component containing the pointer to the secondary dispatch table
673 -- or else raise Constraint_Error if the actual object does not
533369aa 674 -- implement the target interface. This case corresponds to the
26bff3d9
JM
675 -- following example:
676
8fc789c8 677 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
26bff3d9
JM
678 -- begin
679 -- return new Iface_2'Class'(Obj);
680 -- end Op;
681
682 else
683 Rewrite (N,
684 Unchecked_Convert_To (PtrT,
685 Make_Function_Call (Loc,
e4494292 686 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
26bff3d9
JM
687 Parameter_Associations => New_List (
688 Unchecked_Convert_To (RTE (RE_Address),
689 Relocate_Node (N)),
690
691 New_Occurrence_Of
692 (Elists.Node
693 (First_Elmt
694 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
695 Loc)))));
696 Analyze_And_Resolve (N, PtrT);
697 end if;
698 end if;
699 end Displace_Allocator_Pointer;
700
fbf5a39b
AC
701 ---------------------------------
702 -- Expand_Allocator_Expression --
703 ---------------------------------
704
705 procedure Expand_Allocator_Expression (N : Node_Id) is
f02b8bb8
RD
706 Loc : constant Source_Ptr := Sloc (N);
707 Exp : constant Node_Id := Expression (Expression (N));
f02b8bb8
RD
708 PtrT : constant Entity_Id := Etype (N);
709 DesigT : constant Entity_Id := Designated_Type (PtrT);
26bff3d9
JM
710
711 procedure Apply_Accessibility_Check
712 (Ref : Node_Id;
713 Built_In_Place : Boolean := False);
714 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
685094bf
RD
715 -- type, generate an accessibility check to verify that the level of the
716 -- type of the created object is not deeper than the level of the access
50878404 717 -- type. If the type of the qualified expression is class-wide, then
685094bf
RD
718 -- always generate the check (except in the case where it is known to be
719 -- unnecessary, see comment below). Otherwise, only generate the check
720 -- if the level of the qualified expression type is statically deeper
721 -- than the access type.
722 --
723 -- Although the static accessibility will generally have been performed
724 -- as a legality check, it won't have been done in cases where the
725 -- allocator appears in generic body, so a run-time check is needed in
726 -- general. One special case is when the access type is declared in the
727 -- same scope as the class-wide allocator, in which case the check can
728 -- never fail, so it need not be generated.
729 --
730 -- As an open issue, there seem to be cases where the static level
731 -- associated with the class-wide object's underlying type is not
732 -- sufficient to perform the proper accessibility check, such as for
733 -- allocators in nested subprograms or accept statements initialized by
734 -- class-wide formals when the actual originates outside at a deeper
735 -- static level. The nested subprogram case might require passing
736 -- accessibility levels along with class-wide parameters, and the task
737 -- case seems to be an actual gap in the language rules that needs to
738 -- be fixed by the ARG. ???
26bff3d9
JM
739
740 -------------------------------
741 -- Apply_Accessibility_Check --
742 -------------------------------
743
744 procedure Apply_Accessibility_Check
745 (Ref : Node_Id;
746 Built_In_Place : Boolean := False)
747 is
a98838ff
HK
748 Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT);
749 Cond : Node_Id;
750 Fin_Call : Node_Id;
751 Free_Stmt : Node_Id;
752 Obj_Ref : Node_Id;
753 Stmts : List_Id;
26bff3d9
JM
754
755 begin
0791fbe9 756 if Ada_Version >= Ada_2005
26bff3d9 757 and then Is_Class_Wide_Type (DesigT)
535a8637 758 and then Tagged_Type_Expansion
3217f71e 759 and then not Scope_Suppress.Suppress (Accessibility_Check)
26bff3d9
JM
760 and then
761 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
762 or else
763 (Is_Class_Wide_Type (Etype (Exp))
764 and then Scope (PtrT) /= Current_Scope))
765 then
e761d11c 766 -- If the allocator was built in place, Ref is already a reference
26bff3d9 767 -- to the access object initialized to the result of the allocator
e761d11c
AC
768 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
769 -- Remove_Side_Effects for cases where the build-in-place call may
770 -- still be the prefix of the reference (to avoid generating
771 -- duplicate calls). Otherwise, it is the entity associated with
772 -- the object containing the address of the allocated object.
26bff3d9
JM
773
774 if Built_In_Place then
e761d11c 775 Remove_Side_Effects (Ref);
a98838ff 776 Obj_Ref := New_Copy_Tree (Ref);
26bff3d9 777 else
e4494292 778 Obj_Ref := New_Occurrence_Of (Ref, Loc);
50878404
AC
779 end if;
780
b6c8e5be
AC
781 -- For access to interface types we must generate code to displace
782 -- the pointer to the base of the object since the subsequent code
783 -- references components located in the TSD of the object (which
784 -- is associated with the primary dispatch table --see a-tags.ads)
785 -- and also generates code invoking Free, which requires also a
786 -- reference to the base of the unallocated object.
787
cc6f5d75 788 if Is_Interface (DesigT) and then Tagged_Type_Expansion then
b6c8e5be
AC
789 Obj_Ref :=
790 Unchecked_Convert_To (Etype (Obj_Ref),
791 Make_Function_Call (Loc,
662c2ad4
RD
792 Name =>
793 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
b6c8e5be
AC
794 Parameter_Associations => New_List (
795 Unchecked_Convert_To (RTE (RE_Address),
796 New_Copy_Tree (Obj_Ref)))));
797 end if;
798
50878404
AC
799 -- Step 1: Create the object clean up code
800
801 Stmts := New_List;
802
a98838ff
HK
803 -- Deallocate the object if the accessibility check fails. This
804 -- is done only on targets or profiles that support deallocation.
805
806 -- Free (Obj_Ref);
807
808 if RTE_Available (RE_Free) then
809 Free_Stmt := Make_Free_Statement (Loc, New_Copy_Tree (Obj_Ref));
810 Set_Storage_Pool (Free_Stmt, Pool_Id);
811
812 Append_To (Stmts, Free_Stmt);
813
814 -- The target or profile cannot deallocate objects
815
816 else
817 Free_Stmt := Empty;
818 end if;
819
820 -- Finalize the object if applicable. Generate:
a530b8bb
AC
821
822 -- [Deep_]Finalize (Obj_Ref.all);
823
2cbac6c6 824 if Needs_Finalization (DesigT) then
a98838ff 825 Fin_Call :=
cc6f5d75
AC
826 Make_Final_Call
827 (Obj_Ref =>
828 Make_Explicit_Dereference (Loc, New_Copy (Obj_Ref)),
829 Typ => DesigT);
a98838ff
HK
830
831 -- When the target or profile supports deallocation, wrap the
832 -- finalization call in a block to ensure proper deallocation
833 -- even if finalization fails. Generate:
834
835 -- begin
836 -- <Fin_Call>
837 -- exception
838 -- when others =>
839 -- <Free_Stmt>
840 -- raise;
841 -- end;
842
843 if Present (Free_Stmt) then
844 Fin_Call :=
845 Make_Block_Statement (Loc,
846 Handled_Statement_Sequence =>
847 Make_Handled_Sequence_Of_Statements (Loc,
848 Statements => New_List (Fin_Call),
849
850 Exception_Handlers => New_List (
851 Make_Exception_Handler (Loc,
852 Exception_Choices => New_List (
853 Make_Others_Choice (Loc)),
a98838ff
HK
854 Statements => New_List (
855 New_Copy_Tree (Free_Stmt),
856 Make_Raise_Statement (Loc))))));
857 end if;
858
859 Prepend_To (Stmts, Fin_Call);
f46faa08
AC
860 end if;
861
50878404
AC
862 -- Signal the accessibility failure through a Program_Error
863
864 Append_To (Stmts,
865 Make_Raise_Program_Error (Loc,
e4494292 866 Condition => New_Occurrence_Of (Standard_True, Loc),
50878404
AC
867 Reason => PE_Accessibility_Check_Failed));
868
869 -- Step 2: Create the accessibility comparison
870
871 -- Generate:
872 -- Ref'Tag
873
b6c8e5be
AC
874 Obj_Ref :=
875 Make_Attribute_Reference (Loc,
876 Prefix => Obj_Ref,
877 Attribute_Name => Name_Tag);
f46faa08 878
50878404
AC
879 -- For tagged types, determine the accessibility level by looking
880 -- at the type specific data of the dispatch table. Generate:
881
882 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
883
f46faa08 884 if Tagged_Type_Expansion then
50878404 885 Cond := Build_Get_Access_Level (Loc, Obj_Ref);
f46faa08 886
50878404
AC
887 -- Use a runtime call to determine the accessibility level when
888 -- compiling on virtual machine targets. Generate:
f46faa08 889
50878404 890 -- Get_Access_Level (Ref'Tag)
f46faa08
AC
891
892 else
50878404
AC
893 Cond :=
894 Make_Function_Call (Loc,
895 Name =>
e4494292 896 New_Occurrence_Of (RTE (RE_Get_Access_Level), Loc),
50878404 897 Parameter_Associations => New_List (Obj_Ref));
26bff3d9
JM
898 end if;
899
50878404
AC
900 Cond :=
901 Make_Op_Gt (Loc,
902 Left_Opnd => Cond,
903 Right_Opnd =>
904 Make_Integer_Literal (Loc, Type_Access_Level (PtrT)));
905
906 -- Due to the complexity and side effects of the check, utilize an
907 -- if statement instead of the regular Program_Error circuitry.
908
26bff3d9 909 Insert_Action (N,
8b1011c0 910 Make_Implicit_If_Statement (N,
50878404
AC
911 Condition => Cond,
912 Then_Statements => Stmts));
26bff3d9
JM
913 end if;
914 end Apply_Accessibility_Check;
915
916 -- Local variables
917
df3e68b1
HK
918 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
919 Indic : constant Node_Id := Subtype_Mark (Expression (N));
920 T : constant Entity_Id := Entity (Indic);
921 Node : Node_Id;
922 Tag_Assign : Node_Id;
923 Temp : Entity_Id;
924 Temp_Decl : Node_Id;
fbf5a39b 925
d26dc4b5
AC
926 TagT : Entity_Id := Empty;
927 -- Type used as source for tag assignment
928
929 TagR : Node_Id := Empty;
930 -- Target reference for tag assignment
931
26bff3d9
JM
932 -- Start of processing for Expand_Allocator_Expression
933
fbf5a39b 934 begin
3bfb3c03
JM
935 -- Handle call to C++ constructor
936
937 if Is_CPP_Constructor_Call (Exp) then
938 Make_CPP_Constructor_Call_In_Allocator
939 (Allocator => N,
940 Function_Call => Exp);
941 return;
942 end if;
943
885c4871 944 -- In the case of an Ada 2012 allocator whose initial value comes from a
63585f75
SB
945 -- function call, pass "the accessibility level determined by the point
946 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
947 -- Expand_Call but it couldn't be done there (because the Etype of the
948 -- allocator wasn't set then) so we generate the parameter here. See
949 -- the Boolean variable Defer in (a block within) Expand_Call.
950
951 if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then
952 declare
953 Subp : Entity_Id;
954
955 begin
956 if Nkind (Name (Exp)) = N_Explicit_Dereference then
957 Subp := Designated_Type (Etype (Prefix (Name (Exp))));
958 else
959 Subp := Entity (Name (Exp));
960 end if;
961
57a3fca9
AC
962 Subp := Ultimate_Alias (Subp);
963
63585f75
SB
964 if Present (Extra_Accessibility_Of_Result (Subp)) then
965 Add_Extra_Actual_To_Call
966 (Subprogram_Call => Exp,
967 Extra_Formal => Extra_Accessibility_Of_Result (Subp),
968 Extra_Actual => Dynamic_Accessibility_Level (PtrT));
969 end if;
970 end;
971 end if;
972
f6194278 973 -- Case of tagged type or type requiring finalization
63585f75
SB
974
975 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
fadcf313 976
685094bf
RD
977 -- Ada 2005 (AI-318-02): If the initialization expression is a call
978 -- to a build-in-place function, then access to the allocated object
979 -- must be passed to the function. Currently we limit such functions
980 -- to those with constrained limited result subtypes, but eventually
981 -- we plan to expand the allowed forms of functions that are treated
982 -- as build-in-place.
20b5d666 983
0791fbe9 984 if Ada_Version >= Ada_2005
20b5d666
JM
985 and then Is_Build_In_Place_Function_Call (Exp)
986 then
987 Make_Build_In_Place_Call_In_Allocator (N, Exp);
26bff3d9
JM
988 Apply_Accessibility_Check (N, Built_In_Place => True);
989 return;
20b5d666
JM
990 end if;
991
ca5af305
AC
992 -- Actions inserted before:
993 -- Temp : constant ptr_T := new T'(Expression);
994 -- Temp._tag = T'tag; -- when not class-wide
995 -- [Deep_]Adjust (Temp.all);
fbf5a39b 996
ca5af305 997 -- We analyze by hand the new internal allocator to avoid any
6b6041ec 998 -- recursion and inappropriate call to Initialize.
7324bf49 999
20b5d666
JM
1000 -- We don't want to remove side effects when the expression must be
1001 -- built in place. In the case of a build-in-place function call,
1002 -- that could lead to a duplication of the call, which was already
1003 -- substituted for the allocator.
1004
26bff3d9 1005 if not Aggr_In_Place then
fbf5a39b
AC
1006 Remove_Side_Effects (Exp);
1007 end if;
1008
e86a3a7e 1009 Temp := Make_Temporary (Loc, 'P', N);
fbf5a39b
AC
1010
1011 -- For a class wide allocation generate the following code:
1012
1013 -- type Equiv_Record is record ... end record;
1014 -- implicit subtype CW is <Class_Wide_Subytpe>;
1015 -- temp : PtrT := new CW'(CW!(expr));
1016
1017 if Is_Class_Wide_Type (T) then
1018 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
1019
26bff3d9
JM
1020 -- Ada 2005 (AI-251): If the expression is a class-wide interface
1021 -- object we generate code to move up "this" to reference the
1022 -- base of the object before allocating the new object.
1023
1024 -- Note that Exp'Address is recursively expanded into a call
1025 -- to Base_Address (Exp.Tag)
1026
1027 if Is_Class_Wide_Type (Etype (Exp))
1028 and then Is_Interface (Etype (Exp))
1f110335 1029 and then Tagged_Type_Expansion
26bff3d9
JM
1030 then
1031 Set_Expression
1032 (Expression (N),
1033 Unchecked_Convert_To (Entity (Indic),
1034 Make_Explicit_Dereference (Loc,
1035 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
1036 Make_Attribute_Reference (Loc,
1037 Prefix => Exp,
1038 Attribute_Name => Name_Address)))));
26bff3d9
JM
1039 else
1040 Set_Expression
1041 (Expression (N),
1042 Unchecked_Convert_To (Entity (Indic), Exp));
1043 end if;
fbf5a39b
AC
1044
1045 Analyze_And_Resolve (Expression (N), Entity (Indic));
1046 end if;
1047
df3e68b1 1048 -- Processing for allocators returning non-interface types
fbf5a39b 1049
26bff3d9
JM
1050 if not Is_Interface (Directly_Designated_Type (PtrT)) then
1051 if Aggr_In_Place then
df3e68b1 1052 Temp_Decl :=
26bff3d9
JM
1053 Make_Object_Declaration (Loc,
1054 Defining_Identifier => Temp,
e4494292 1055 Object_Definition => New_Occurrence_Of (PtrT, Loc),
26bff3d9
JM
1056 Expression =>
1057 Make_Allocator (Loc,
df3e68b1 1058 Expression =>
e4494292 1059 New_Occurrence_Of (Etype (Exp), Loc)));
fbf5a39b 1060
fad0600d
AC
1061 -- Copy the Comes_From_Source flag for the allocator we just
1062 -- built, since logically this allocator is a replacement of
1063 -- the original allocator node. This is for proper handling of
1064 -- restriction No_Implicit_Heap_Allocations.
1065
26bff3d9 1066 Set_Comes_From_Source
df3e68b1 1067 (Expression (Temp_Decl), Comes_From_Source (N));
fbf5a39b 1068
df3e68b1
HK
1069 Set_No_Initialization (Expression (Temp_Decl));
1070 Insert_Action (N, Temp_Decl);
fbf5a39b 1071
ca5af305 1072 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
df3e68b1 1073 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
fad0600d 1074
26bff3d9
JM
1075 else
1076 Node := Relocate_Node (N);
1077 Set_Analyzed (Node);
df3e68b1
HK
1078
1079 Temp_Decl :=
26bff3d9
JM
1080 Make_Object_Declaration (Loc,
1081 Defining_Identifier => Temp,
1082 Constant_Present => True,
e4494292 1083 Object_Definition => New_Occurrence_Of (PtrT, Loc),
df3e68b1
HK
1084 Expression => Node);
1085
1086 Insert_Action (N, Temp_Decl);
ca5af305 1087 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
fbf5a39b
AC
1088 end if;
1089
26bff3d9
JM
1090 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
1091 -- interface type. In this case we use the type of the qualified
1092 -- expression to allocate the object.
1093
fbf5a39b 1094 else
26bff3d9 1095 declare
191fcb3a 1096 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
26bff3d9 1097 New_Decl : Node_Id;
fbf5a39b 1098
26bff3d9
JM
1099 begin
1100 New_Decl :=
1101 Make_Full_Type_Declaration (Loc,
1102 Defining_Identifier => Def_Id,
cc6f5d75 1103 Type_Definition =>
26bff3d9
JM
1104 Make_Access_To_Object_Definition (Loc,
1105 All_Present => True,
1106 Null_Exclusion_Present => False,
0929eaeb
AC
1107 Constant_Present =>
1108 Is_Access_Constant (Etype (N)),
26bff3d9 1109 Subtype_Indication =>
e4494292 1110 New_Occurrence_Of (Etype (Exp), Loc)));
26bff3d9
JM
1111
1112 Insert_Action (N, New_Decl);
1113
df3e68b1
HK
1114 -- Inherit the allocation-related attributes from the original
1115 -- access type.
26bff3d9 1116
24d4b3d5
AC
1117 Set_Finalization_Master
1118 (Def_Id, Finalization_Master (PtrT));
df3e68b1 1119
24d4b3d5
AC
1120 Set_Associated_Storage_Pool
1121 (Def_Id, Associated_Storage_Pool (PtrT));
758c442c 1122
26bff3d9
JM
1123 -- Declare the object using the previous type declaration
1124
1125 if Aggr_In_Place then
df3e68b1 1126 Temp_Decl :=
26bff3d9
JM
1127 Make_Object_Declaration (Loc,
1128 Defining_Identifier => Temp,
e4494292 1129 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
26bff3d9
JM
1130 Expression =>
1131 Make_Allocator (Loc,
e4494292 1132 New_Occurrence_Of (Etype (Exp), Loc)));
26bff3d9 1133
fad0600d
AC
1134 -- Copy the Comes_From_Source flag for the allocator we just
1135 -- built, since logically this allocator is a replacement of
1136 -- the original allocator node. This is for proper handling
1137 -- of restriction No_Implicit_Heap_Allocations.
1138
26bff3d9 1139 Set_Comes_From_Source
df3e68b1 1140 (Expression (Temp_Decl), Comes_From_Source (N));
26bff3d9 1141
df3e68b1
HK
1142 Set_No_Initialization (Expression (Temp_Decl));
1143 Insert_Action (N, Temp_Decl);
26bff3d9 1144
ca5af305 1145 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
df3e68b1 1146 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
26bff3d9 1147
26bff3d9
JM
1148 else
1149 Node := Relocate_Node (N);
1150 Set_Analyzed (Node);
df3e68b1
HK
1151
1152 Temp_Decl :=
26bff3d9
JM
1153 Make_Object_Declaration (Loc,
1154 Defining_Identifier => Temp,
1155 Constant_Present => True,
e4494292 1156 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
df3e68b1
HK
1157 Expression => Node);
1158
1159 Insert_Action (N, Temp_Decl);
ca5af305 1160 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
26bff3d9
JM
1161 end if;
1162
1163 -- Generate an additional object containing the address of the
1164 -- returned object. The type of this second object declaration
685094bf
RD
1165 -- is the correct type required for the common processing that
1166 -- is still performed by this subprogram. The displacement of
1167 -- this pointer to reference the component associated with the
1168 -- interface type will be done at the end of common processing.
26bff3d9
JM
1169
1170 New_Decl :=
1171 Make_Object_Declaration (Loc,
243cae0a 1172 Defining_Identifier => Make_Temporary (Loc, 'P'),
e4494292 1173 Object_Definition => New_Occurrence_Of (PtrT, Loc),
243cae0a 1174 Expression =>
df3e68b1 1175 Unchecked_Convert_To (PtrT,
e4494292 1176 New_Occurrence_Of (Temp, Loc)));
26bff3d9
JM
1177
1178 Insert_Action (N, New_Decl);
1179
df3e68b1
HK
1180 Temp_Decl := New_Decl;
1181 Temp := Defining_Identifier (New_Decl);
26bff3d9 1182 end;
758c442c
GD
1183 end if;
1184
26bff3d9
JM
1185 Apply_Accessibility_Check (Temp);
1186
1187 -- Generate the tag assignment
1188
535a8637 1189 -- Suppress the tag assignment for VM targets because VM tags are
26bff3d9
JM
1190 -- represented implicitly in objects.
1191
1f110335 1192 if not Tagged_Type_Expansion then
26bff3d9 1193 null;
fbf5a39b 1194
26bff3d9
JM
1195 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1196 -- interface objects because in this case the tag does not change.
d26dc4b5 1197
26bff3d9
JM
1198 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1199 pragma Assert (Is_Class_Wide_Type
1200 (Directly_Designated_Type (Etype (N))));
d26dc4b5
AC
1201 null;
1202
1203 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1204 TagT := T;
e4494292 1205 TagR := New_Occurrence_Of (Temp, Loc);
d26dc4b5
AC
1206
1207 elsif Is_Private_Type (T)
1208 and then Is_Tagged_Type (Underlying_Type (T))
fbf5a39b 1209 then
d26dc4b5 1210 TagT := Underlying_Type (T);
dfd99a80
TQ
1211 TagR :=
1212 Unchecked_Convert_To (Underlying_Type (T),
1213 Make_Explicit_Dereference (Loc,
e4494292 1214 Prefix => New_Occurrence_Of (Temp, Loc)));
d26dc4b5
AC
1215 end if;
1216
1217 if Present (TagT) then
38171f43
AC
1218 declare
1219 Full_T : constant Entity_Id := Underlying_Type (TagT);
e4494292 1220
38171f43
AC
1221 begin
1222 Tag_Assign :=
1223 Make_Assignment_Statement (Loc,
cc6f5d75 1224 Name =>
38171f43 1225 Make_Selected_Component (Loc,
cc6f5d75 1226 Prefix => TagR,
38171f43 1227 Selector_Name =>
e4494292
RD
1228 New_Occurrence_Of
1229 (First_Tag_Component (Full_T), Loc)),
1230
38171f43
AC
1231 Expression =>
1232 Unchecked_Convert_To (RTE (RE_Tag),
e4494292 1233 New_Occurrence_Of
38171f43
AC
1234 (Elists.Node
1235 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1236 end;
fbf5a39b
AC
1237
1238 -- The previous assignment has to be done in any case
1239
1240 Set_Assignment_OK (Name (Tag_Assign));
1241 Insert_Action (N, Tag_Assign);
fbf5a39b
AC
1242 end if;
1243
533369aa
AC
1244 if Needs_Finalization (DesigT) and then Needs_Finalization (T) then
1245
df3e68b1
HK
1246 -- Generate an Adjust call if the object will be moved. In Ada
1247 -- 2005, the object may be inherently limited, in which case
1248 -- there is no Adjust procedure, and the object is built in
1249 -- place. In Ada 95, the object can be limited but not
1250 -- inherently limited if this allocator came from a return
1251 -- statement (we're allocating the result on the secondary
1252 -- stack). In that case, the object will be moved, so we _do_
1253 -- want to Adjust.
1254
1255 if not Aggr_In_Place
51245e2d 1256 and then not Is_Limited_View (T)
df3e68b1
HK
1257 then
1258 Insert_Action (N,
fbf5a39b 1259
533369aa
AC
1260 -- An unchecked conversion is needed in the classwide case
1261 -- because the designated type can be an ancestor of the
1262 -- subtype mark of the allocator.
fbf5a39b 1263
533369aa
AC
1264 Make_Adjust_Call
1265 (Obj_Ref =>
1266 Unchecked_Convert_To (T,
1267 Make_Explicit_Dereference (Loc,
e4494292 1268 Prefix => New_Occurrence_Of (Temp, Loc))),
533369aa 1269 Typ => T));
df3e68b1 1270 end if;
fbf5a39b
AC
1271 end if;
1272
e4494292 1273 Rewrite (N, New_Occurrence_Of (Temp, Loc));
fbf5a39b
AC
1274 Analyze_And_Resolve (N, PtrT);
1275
685094bf
RD
1276 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1277 -- component containing the secondary dispatch table of the interface
1278 -- type.
26bff3d9
JM
1279
1280 if Is_Interface (Directly_Designated_Type (PtrT)) then
1281 Displace_Allocator_Pointer (N);
1282 end if;
1283
fbf5a39b 1284 elsif Aggr_In_Place then
e86a3a7e 1285 Temp := Make_Temporary (Loc, 'P', N);
df3e68b1 1286 Temp_Decl :=
fbf5a39b
AC
1287 Make_Object_Declaration (Loc,
1288 Defining_Identifier => Temp,
e4494292 1289 Object_Definition => New_Occurrence_Of (PtrT, Loc),
df3e68b1
HK
1290 Expression =>
1291 Make_Allocator (Loc,
e4494292 1292 Expression => New_Occurrence_Of (Etype (Exp), Loc)));
fbf5a39b 1293
fad0600d
AC
1294 -- Copy the Comes_From_Source flag for the allocator we just built,
1295 -- since logically this allocator is a replacement of the original
1296 -- allocator node. This is for proper handling of restriction
1297 -- No_Implicit_Heap_Allocations.
1298
fbf5a39b 1299 Set_Comes_From_Source
df3e68b1
HK
1300 (Expression (Temp_Decl), Comes_From_Source (N));
1301
1302 Set_No_Initialization (Expression (Temp_Decl));
1303 Insert_Action (N, Temp_Decl);
1304
ca5af305 1305 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
df3e68b1 1306 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
fbf5a39b 1307
e4494292 1308 Rewrite (N, New_Occurrence_Of (Temp, Loc));
fbf5a39b
AC
1309 Analyze_And_Resolve (N, PtrT);
1310
533369aa 1311 elsif Is_Access_Type (T) and then Can_Never_Be_Null (T) then
51e4c4b9
AC
1312 Install_Null_Excluding_Check (Exp);
1313
f02b8bb8 1314 elsif Is_Access_Type (DesigT)
fbf5a39b
AC
1315 and then Nkind (Exp) = N_Allocator
1316 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1317 then
0da2c8ac 1318 -- Apply constraint to designated subtype indication
fbf5a39b 1319
cc6f5d75
AC
1320 Apply_Constraint_Check
1321 (Expression (Exp), Designated_Type (DesigT), No_Sliding => True);
fbf5a39b
AC
1322
1323 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1324
1325 -- Propagate constraint_error to enclosing allocator
1326
1327 Rewrite (Exp, New_Copy (Expression (Exp)));
1328 end if;
1df4f514 1329
fbf5a39b 1330 else
14f0f659
AC
1331 Build_Allocate_Deallocate_Proc (N, True);
1332
36c73552
AC
1333 -- If we have:
1334 -- type A is access T1;
1335 -- X : A := new T2'(...);
1336 -- T1 and T2 can be different subtypes, and we might need to check
1337 -- both constraints. First check against the type of the qualified
1338 -- expression.
1339
1340 Apply_Constraint_Check (Exp, T, No_Sliding => True);
fbf5a39b 1341
d79e621a 1342 if Do_Range_Check (Exp) then
d79e621a
GD
1343 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1344 end if;
1345
685094bf
RD
1346 -- A check is also needed in cases where the designated subtype is
1347 -- constrained and differs from the subtype given in the qualified
1348 -- expression. Note that the check on the qualified expression does
1349 -- not allow sliding, but this check does (a relaxation from Ada 83).
fbf5a39b 1350
f02b8bb8 1351 if Is_Constrained (DesigT)
9450205a 1352 and then not Subtypes_Statically_Match (T, DesigT)
fbf5a39b
AC
1353 then
1354 Apply_Constraint_Check
f02b8bb8 1355 (Exp, DesigT, No_Sliding => False);
d79e621a
GD
1356
1357 if Do_Range_Check (Exp) then
d79e621a
GD
1358 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1359 end if;
f02b8bb8
RD
1360 end if;
1361
685094bf
RD
1362 -- For an access to unconstrained packed array, GIGI needs to see an
1363 -- expression with a constrained subtype in order to compute the
1364 -- proper size for the allocator.
f02b8bb8
RD
1365
1366 if Is_Array_Type (T)
1367 and then not Is_Constrained (T)
1368 and then Is_Packed (T)
1369 then
1370 declare
191fcb3a 1371 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
f02b8bb8
RD
1372 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1373 begin
1374 Insert_Action (Exp,
1375 Make_Subtype_Declaration (Loc,
1376 Defining_Identifier => ConstrT,
25ebc085
AC
1377 Subtype_Indication =>
1378 Make_Subtype_From_Expr (Internal_Exp, T)));
f02b8bb8
RD
1379 Freeze_Itype (ConstrT, Exp);
1380 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1381 end;
fbf5a39b 1382 end if;
f02b8bb8 1383
685094bf
RD
1384 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1385 -- to a build-in-place function, then access to the allocated object
1386 -- must be passed to the function. Currently we limit such functions
1387 -- to those with constrained limited result subtypes, but eventually
1388 -- we plan to expand the allowed forms of functions that are treated
1389 -- as build-in-place.
20b5d666 1390
0791fbe9 1391 if Ada_Version >= Ada_2005
20b5d666
JM
1392 and then Is_Build_In_Place_Function_Call (Exp)
1393 then
1394 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1395 end if;
fbf5a39b
AC
1396 end if;
1397
1398 exception
1399 when RE_Not_Available =>
1400 return;
1401 end Expand_Allocator_Expression;
1402
70482933
RK
1403 -----------------------------
1404 -- Expand_Array_Comparison --
1405 -----------------------------
1406
685094bf
RD
1407 -- Expansion is only required in the case of array types. For the unpacked
1408 -- case, an appropriate runtime routine is called. For packed cases, and
1409 -- also in some other cases where a runtime routine cannot be called, the
1410 -- form of the expansion is:
70482933
RK
1411
1412 -- [body for greater_nn; boolean_expression]
1413
1414 -- The body is built by Make_Array_Comparison_Op, and the form of the
1415 -- Boolean expression depends on the operator involved.
1416
1417 procedure Expand_Array_Comparison (N : Node_Id) is
1418 Loc : constant Source_Ptr := Sloc (N);
1419 Op1 : Node_Id := Left_Opnd (N);
1420 Op2 : Node_Id := Right_Opnd (N);
1421 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
fbf5a39b 1422 Ctyp : constant Entity_Id := Component_Type (Typ1);
70482933
RK
1423
1424 Expr : Node_Id;
1425 Func_Body : Node_Id;
1426 Func_Name : Entity_Id;
1427
fbf5a39b
AC
1428 Comp : RE_Id;
1429
9bc43c53
AC
1430 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1431 -- True for byte addressable target
91b1417d 1432
fbf5a39b 1433 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
685094bf
RD
1434 -- Returns True if the length of the given operand is known to be less
1435 -- than 4. Returns False if this length is known to be four or greater
1436 -- or is not known at compile time.
fbf5a39b
AC
1437
1438 ------------------------
1439 -- Length_Less_Than_4 --
1440 ------------------------
1441
1442 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1443 Otyp : constant Entity_Id := Etype (Opnd);
1444
1445 begin
1446 if Ekind (Otyp) = E_String_Literal_Subtype then
1447 return String_Literal_Length (Otyp) < 4;
1448
1449 else
1450 declare
1451 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1452 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1453 Hi : constant Node_Id := Type_High_Bound (Ityp);
1454 Lov : Uint;
1455 Hiv : Uint;
1456
1457 begin
1458 if Compile_Time_Known_Value (Lo) then
1459 Lov := Expr_Value (Lo);
1460 else
1461 return False;
1462 end if;
1463
1464 if Compile_Time_Known_Value (Hi) then
1465 Hiv := Expr_Value (Hi);
1466 else
1467 return False;
1468 end if;
1469
1470 return Hiv < Lov + 3;
1471 end;
1472 end if;
1473 end Length_Less_Than_4;
1474
1475 -- Start of processing for Expand_Array_Comparison
1476
70482933 1477 begin
fbf5a39b
AC
1478 -- Deal first with unpacked case, where we can call a runtime routine
1479 -- except that we avoid this for targets for which are not addressable
535a8637 1480 -- by bytes.
fbf5a39b
AC
1481
1482 if not Is_Bit_Packed_Array (Typ1)
9bc43c53 1483 and then Byte_Addressable
fbf5a39b
AC
1484 then
1485 -- The call we generate is:
1486
1487 -- Compare_Array_xn[_Unaligned]
1488 -- (left'address, right'address, left'length, right'length) <op> 0
1489
1490 -- x = U for unsigned, S for signed
1491 -- n = 8,16,32,64 for component size
1492 -- Add _Unaligned if length < 4 and component size is 8.
1493 -- <op> is the standard comparison operator
1494
1495 if Component_Size (Typ1) = 8 then
1496 if Length_Less_Than_4 (Op1)
1497 or else
1498 Length_Less_Than_4 (Op2)
1499 then
1500 if Is_Unsigned_Type (Ctyp) then
1501 Comp := RE_Compare_Array_U8_Unaligned;
1502 else
1503 Comp := RE_Compare_Array_S8_Unaligned;
1504 end if;
1505
1506 else
1507 if Is_Unsigned_Type (Ctyp) then
1508 Comp := RE_Compare_Array_U8;
1509 else
1510 Comp := RE_Compare_Array_S8;
1511 end if;
1512 end if;
1513
1514 elsif Component_Size (Typ1) = 16 then
1515 if Is_Unsigned_Type (Ctyp) then
1516 Comp := RE_Compare_Array_U16;
1517 else
1518 Comp := RE_Compare_Array_S16;
1519 end if;
1520
1521 elsif Component_Size (Typ1) = 32 then
1522 if Is_Unsigned_Type (Ctyp) then
1523 Comp := RE_Compare_Array_U32;
1524 else
1525 Comp := RE_Compare_Array_S32;
1526 end if;
1527
1528 else pragma Assert (Component_Size (Typ1) = 64);
1529 if Is_Unsigned_Type (Ctyp) then
1530 Comp := RE_Compare_Array_U64;
1531 else
1532 Comp := RE_Compare_Array_S64;
1533 end if;
1534 end if;
1535
9fe696a3 1536 if RTE_Available (Comp) then
fbf5a39b 1537
9fe696a3 1538 -- Expand to a call only if the runtime function is available,
744c73a5 1539 -- otherwise fall back to inline code.
fbf5a39b 1540
9fe696a3
AC
1541 Remove_Side_Effects (Op1, Name_Req => True);
1542 Remove_Side_Effects (Op2, Name_Req => True);
fbf5a39b 1543
9fe696a3
AC
1544 Rewrite (Op1,
1545 Make_Function_Call (Sloc (Op1),
1546 Name => New_Occurrence_Of (RTE (Comp), Loc),
fbf5a39b 1547
9fe696a3
AC
1548 Parameter_Associations => New_List (
1549 Make_Attribute_Reference (Loc,
1550 Prefix => Relocate_Node (Op1),
1551 Attribute_Name => Name_Address),
fbf5a39b 1552
9fe696a3
AC
1553 Make_Attribute_Reference (Loc,
1554 Prefix => Relocate_Node (Op2),
1555 Attribute_Name => Name_Address),
fbf5a39b 1556
9fe696a3
AC
1557 Make_Attribute_Reference (Loc,
1558 Prefix => Relocate_Node (Op1),
1559 Attribute_Name => Name_Length),
fbf5a39b 1560
9fe696a3
AC
1561 Make_Attribute_Reference (Loc,
1562 Prefix => Relocate_Node (Op2),
1563 Attribute_Name => Name_Length))));
1564
1565 Rewrite (Op2,
1566 Make_Integer_Literal (Sloc (Op2),
1567 Intval => Uint_0));
1568
1569 Analyze_And_Resolve (Op1, Standard_Integer);
1570 Analyze_And_Resolve (Op2, Standard_Integer);
1571 return;
1572 end if;
fbf5a39b
AC
1573 end if;
1574
1575 -- Cases where we cannot make runtime call
1576
70482933
RK
1577 -- For (a <= b) we convert to not (a > b)
1578
1579 if Chars (N) = Name_Op_Le then
1580 Rewrite (N,
1581 Make_Op_Not (Loc,
1582 Right_Opnd =>
1583 Make_Op_Gt (Loc,
1584 Left_Opnd => Op1,
1585 Right_Opnd => Op2)));
1586 Analyze_And_Resolve (N, Standard_Boolean);
1587 return;
1588
1589 -- For < the Boolean expression is
1590 -- greater__nn (op2, op1)
1591
1592 elsif Chars (N) = Name_Op_Lt then
1593 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1594
1595 -- Switch operands
1596
1597 Op1 := Right_Opnd (N);
1598 Op2 := Left_Opnd (N);
1599
1600 -- For (a >= b) we convert to not (a < b)
1601
1602 elsif Chars (N) = Name_Op_Ge then
1603 Rewrite (N,
1604 Make_Op_Not (Loc,
1605 Right_Opnd =>
1606 Make_Op_Lt (Loc,
1607 Left_Opnd => Op1,
1608 Right_Opnd => Op2)));
1609 Analyze_And_Resolve (N, Standard_Boolean);
1610 return;
1611
1612 -- For > the Boolean expression is
1613 -- greater__nn (op1, op2)
1614
1615 else
1616 pragma Assert (Chars (N) = Name_Op_Gt);
1617 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1618 end if;
1619
1620 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1621 Expr :=
1622 Make_Function_Call (Loc,
e4494292 1623 Name => New_Occurrence_Of (Func_Name, Loc),
70482933
RK
1624 Parameter_Associations => New_List (Op1, Op2));
1625
1626 Insert_Action (N, Func_Body);
1627 Rewrite (N, Expr);
1628 Analyze_And_Resolve (N, Standard_Boolean);
70482933
RK
1629 end Expand_Array_Comparison;
1630
1631 ---------------------------
1632 -- Expand_Array_Equality --
1633 ---------------------------
1634
685094bf
RD
1635 -- Expand an equality function for multi-dimensional arrays. Here is an
1636 -- example of such a function for Nb_Dimension = 2
70482933 1637
0da2c8ac 1638 -- function Enn (A : atyp; B : btyp) return boolean is
70482933 1639 -- begin
fbf5a39b
AC
1640 -- if (A'length (1) = 0 or else A'length (2) = 0)
1641 -- and then
1642 -- (B'length (1) = 0 or else B'length (2) = 0)
1643 -- then
1644 -- return True; -- RM 4.5.2(22)
1645 -- end if;
0da2c8ac 1646
fbf5a39b
AC
1647 -- if A'length (1) /= B'length (1)
1648 -- or else
1649 -- A'length (2) /= B'length (2)
1650 -- then
1651 -- return False; -- RM 4.5.2(23)
1652 -- end if;
0da2c8ac 1653
fbf5a39b 1654 -- declare
523456db
AC
1655 -- A1 : Index_T1 := A'first (1);
1656 -- B1 : Index_T1 := B'first (1);
fbf5a39b 1657 -- begin
523456db 1658 -- loop
fbf5a39b 1659 -- declare
523456db
AC
1660 -- A2 : Index_T2 := A'first (2);
1661 -- B2 : Index_T2 := B'first (2);
fbf5a39b 1662 -- begin
523456db 1663 -- loop
fbf5a39b
AC
1664 -- if A (A1, A2) /= B (B1, B2) then
1665 -- return False;
70482933 1666 -- end if;
0da2c8ac 1667
523456db
AC
1668 -- exit when A2 = A'last (2);
1669 -- A2 := Index_T2'succ (A2);
0da2c8ac 1670 -- B2 := Index_T2'succ (B2);
70482933 1671 -- end loop;
fbf5a39b 1672 -- end;
0da2c8ac 1673
523456db
AC
1674 -- exit when A1 = A'last (1);
1675 -- A1 := Index_T1'succ (A1);
0da2c8ac 1676 -- B1 := Index_T1'succ (B1);
70482933 1677 -- end loop;
fbf5a39b 1678 -- end;
0da2c8ac 1679
70482933
RK
1680 -- return true;
1681 -- end Enn;
1682
685094bf
RD
1683 -- Note on the formal types used (atyp and btyp). If either of the arrays
1684 -- is of a private type, we use the underlying type, and do an unchecked
1685 -- conversion of the actual. If either of the arrays has a bound depending
1686 -- on a discriminant, then we use the base type since otherwise we have an
1687 -- escaped discriminant in the function.
0da2c8ac 1688
685094bf
RD
1689 -- If both arrays are constrained and have the same bounds, we can generate
1690 -- a loop with an explicit iteration scheme using a 'Range attribute over
1691 -- the first array.
523456db 1692
70482933
RK
1693 function Expand_Array_Equality
1694 (Nod : Node_Id;
70482933
RK
1695 Lhs : Node_Id;
1696 Rhs : Node_Id;
0da2c8ac
AC
1697 Bodies : List_Id;
1698 Typ : Entity_Id) return Node_Id
70482933
RK
1699 is
1700 Loc : constant Source_Ptr := Sloc (Nod);
fbf5a39b
AC
1701 Decls : constant List_Id := New_List;
1702 Index_List1 : constant List_Id := New_List;
1703 Index_List2 : constant List_Id := New_List;
1704
1705 Actuals : List_Id;
1706 Formals : List_Id;
1707 Func_Name : Entity_Id;
1708 Func_Body : Node_Id;
70482933
RK
1709
1710 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1711 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1712
0da2c8ac
AC
1713 Ltyp : Entity_Id;
1714 Rtyp : Entity_Id;
1715 -- The parameter types to be used for the formals
1716
fbf5a39b
AC
1717 function Arr_Attr
1718 (Arr : Entity_Id;
1719 Nam : Name_Id;
2e071734 1720 Num : Int) return Node_Id;
5e1c00fa 1721 -- This builds the attribute reference Arr'Nam (Expr)
fbf5a39b 1722
70482933 1723 function Component_Equality (Typ : Entity_Id) return Node_Id;
685094bf 1724 -- Create one statement to compare corresponding components, designated
3b42c566 1725 -- by a full set of indexes.
70482933 1726
0da2c8ac 1727 function Get_Arg_Type (N : Node_Id) return Entity_Id;
685094bf
RD
1728 -- Given one of the arguments, computes the appropriate type to be used
1729 -- for that argument in the corresponding function formal
0da2c8ac 1730
fbf5a39b 1731 function Handle_One_Dimension
70482933 1732 (N : Int;
2e071734 1733 Index : Node_Id) return Node_Id;
0da2c8ac 1734 -- This procedure returns the following code
fbf5a39b
AC
1735 --
1736 -- declare
523456db 1737 -- Bn : Index_T := B'First (N);
fbf5a39b 1738 -- begin
523456db 1739 -- loop
fbf5a39b 1740 -- xxx
523456db
AC
1741 -- exit when An = A'Last (N);
1742 -- An := Index_T'Succ (An)
0da2c8ac 1743 -- Bn := Index_T'Succ (Bn)
fbf5a39b
AC
1744 -- end loop;
1745 -- end;
1746 --
3b42c566 1747 -- If both indexes are constrained and identical, the procedure
523456db
AC
1748 -- returns a simpler loop:
1749 --
1750 -- for An in A'Range (N) loop
1751 -- xxx
1752 -- end loop
0da2c8ac 1753 --
523456db 1754 -- N is the dimension for which we are generating a loop. Index is the
685094bf
RD
1755 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1756 -- xxx statement is either the loop or declare for the next dimension
1757 -- or if this is the last dimension the comparison of corresponding
1758 -- components of the arrays.
fbf5a39b 1759 --
685094bf 1760 -- The actual way the code works is to return the comparison of
a90bd866 1761 -- corresponding components for the N+1 call. That's neater.
fbf5a39b
AC
1762
1763 function Test_Empty_Arrays return Node_Id;
1764 -- This function constructs the test for both arrays being empty
1765 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1766 -- and then
1767 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1768
1769 function Test_Lengths_Correspond return Node_Id;
685094bf
RD
1770 -- This function constructs the test for arrays having different lengths
1771 -- in at least one index position, in which case the resulting code is:
fbf5a39b
AC
1772
1773 -- A'length (1) /= B'length (1)
1774 -- or else
1775 -- A'length (2) /= B'length (2)
1776 -- or else
1777 -- ...
1778
1779 --------------
1780 -- Arr_Attr --
1781 --------------
1782
1783 function Arr_Attr
1784 (Arr : Entity_Id;
1785 Nam : Name_Id;
2e071734 1786 Num : Int) return Node_Id
fbf5a39b
AC
1787 is
1788 begin
1789 return
1790 Make_Attribute_Reference (Loc,
cc6f5d75
AC
1791 Attribute_Name => Nam,
1792 Prefix => New_Occurrence_Of (Arr, Loc),
1793 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
fbf5a39b 1794 end Arr_Attr;
70482933
RK
1795
1796 ------------------------
1797 -- Component_Equality --
1798 ------------------------
1799
1800 function Component_Equality (Typ : Entity_Id) return Node_Id is
1801 Test : Node_Id;
1802 L, R : Node_Id;
1803
1804 begin
1805 -- if a(i1...) /= b(j1...) then return false; end if;
1806
1807 L :=
1808 Make_Indexed_Component (Loc,
7675ad4f 1809 Prefix => Make_Identifier (Loc, Chars (A)),
70482933
RK
1810 Expressions => Index_List1);
1811
1812 R :=
1813 Make_Indexed_Component (Loc,
7675ad4f 1814 Prefix => Make_Identifier (Loc, Chars (B)),
70482933
RK
1815 Expressions => Index_List2);
1816
1817 Test := Expand_Composite_Equality
1818 (Nod, Component_Type (Typ), L, R, Decls);
1819
a9d8907c
JM
1820 -- If some (sub)component is an unchecked_union, the whole operation
1821 -- will raise program error.
8aceda64
AC
1822
1823 if Nkind (Test) = N_Raise_Program_Error then
a9d8907c
JM
1824
1825 -- This node is going to be inserted at a location where a
685094bf
RD
1826 -- statement is expected: clear its Etype so analysis will set
1827 -- it to the expected Standard_Void_Type.
a9d8907c
JM
1828
1829 Set_Etype (Test, Empty);
8aceda64
AC
1830 return Test;
1831
1832 else
1833 return
1834 Make_Implicit_If_Statement (Nod,
cc6f5d75 1835 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
8aceda64 1836 Then_Statements => New_List (
d766cee3 1837 Make_Simple_Return_Statement (Loc,
8aceda64
AC
1838 Expression => New_Occurrence_Of (Standard_False, Loc))));
1839 end if;
70482933
RK
1840 end Component_Equality;
1841
0da2c8ac
AC
1842 ------------------
1843 -- Get_Arg_Type --
1844 ------------------
1845
1846 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1847 T : Entity_Id;
1848 X : Node_Id;
1849
1850 begin
1851 T := Etype (N);
1852
1853 if No (T) then
1854 return Typ;
1855
1856 else
1857 T := Underlying_Type (T);
1858
1859 X := First_Index (T);
1860 while Present (X) loop
761f7dcb
AC
1861 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1862 or else
1863 Denotes_Discriminant (Type_High_Bound (Etype (X)))
0da2c8ac
AC
1864 then
1865 T := Base_Type (T);
1866 exit;
1867 end if;
1868
1869 Next_Index (X);
1870 end loop;
1871
1872 return T;
1873 end if;
1874 end Get_Arg_Type;
1875
fbf5a39b
AC
1876 --------------------------
1877 -- Handle_One_Dimension --
1878 ---------------------------
70482933 1879
fbf5a39b 1880 function Handle_One_Dimension
70482933 1881 (N : Int;
2e071734 1882 Index : Node_Id) return Node_Id
70482933 1883 is
0da2c8ac 1884 Need_Separate_Indexes : constant Boolean :=
761f7dcb 1885 Ltyp /= Rtyp or else not Is_Constrained (Ltyp);
0da2c8ac 1886 -- If the index types are identical, and we are working with
685094bf
RD
1887 -- constrained types, then we can use the same index for both
1888 -- of the arrays.
0da2c8ac 1889
191fcb3a 1890 An : constant Entity_Id := Make_Temporary (Loc, 'A');
0da2c8ac
AC
1891
1892 Bn : Entity_Id;
1893 Index_T : Entity_Id;
1894 Stm_List : List_Id;
1895 Loop_Stm : Node_Id;
70482933
RK
1896
1897 begin
0da2c8ac
AC
1898 if N > Number_Dimensions (Ltyp) then
1899 return Component_Equality (Ltyp);
fbf5a39b 1900 end if;
70482933 1901
0da2c8ac
AC
1902 -- Case where we generate a loop
1903
1904 Index_T := Base_Type (Etype (Index));
1905
1906 if Need_Separate_Indexes then
191fcb3a 1907 Bn := Make_Temporary (Loc, 'B');
0da2c8ac
AC
1908 else
1909 Bn := An;
1910 end if;
70482933 1911
e4494292
RD
1912 Append (New_Occurrence_Of (An, Loc), Index_List1);
1913 Append (New_Occurrence_Of (Bn, Loc), Index_List2);
70482933 1914
0da2c8ac
AC
1915 Stm_List := New_List (
1916 Handle_One_Dimension (N + 1, Next_Index (Index)));
70482933 1917
0da2c8ac 1918 if Need_Separate_Indexes then
a9d8907c 1919
3b42c566 1920 -- Generate guard for loop, followed by increments of indexes
523456db
AC
1921
1922 Append_To (Stm_List,
1923 Make_Exit_Statement (Loc,
1924 Condition =>
1925 Make_Op_Eq (Loc,
cc6f5d75 1926 Left_Opnd => New_Occurrence_Of (An, Loc),
523456db
AC
1927 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1928
1929 Append_To (Stm_List,
1930 Make_Assignment_Statement (Loc,
e4494292 1931 Name => New_Occurrence_Of (An, Loc),
523456db
AC
1932 Expression =>
1933 Make_Attribute_Reference (Loc,
e4494292 1934 Prefix => New_Occurrence_Of (Index_T, Loc),
523456db 1935 Attribute_Name => Name_Succ,
e4494292
RD
1936 Expressions => New_List (
1937 New_Occurrence_Of (An, Loc)))));
523456db 1938
0da2c8ac
AC
1939 Append_To (Stm_List,
1940 Make_Assignment_Statement (Loc,
e4494292 1941 Name => New_Occurrence_Of (Bn, Loc),
0da2c8ac
AC
1942 Expression =>
1943 Make_Attribute_Reference (Loc,
e4494292 1944 Prefix => New_Occurrence_Of (Index_T, Loc),
0da2c8ac 1945 Attribute_Name => Name_Succ,
e4494292
RD
1946 Expressions => New_List (
1947 New_Occurrence_Of (Bn, Loc)))));
0da2c8ac
AC
1948 end if;
1949
a9d8907c
JM
1950 -- If separate indexes, we need a declare block for An and Bn, and a
1951 -- loop without an iteration scheme.
0da2c8ac
AC
1952
1953 if Need_Separate_Indexes then
523456db
AC
1954 Loop_Stm :=
1955 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1956
0da2c8ac
AC
1957 return
1958 Make_Block_Statement (Loc,
1959 Declarations => New_List (
523456db
AC
1960 Make_Object_Declaration (Loc,
1961 Defining_Identifier => An,
e4494292 1962 Object_Definition => New_Occurrence_Of (Index_T, Loc),
523456db
AC
1963 Expression => Arr_Attr (A, Name_First, N)),
1964
0da2c8ac
AC
1965 Make_Object_Declaration (Loc,
1966 Defining_Identifier => Bn,
e4494292 1967 Object_Definition => New_Occurrence_Of (Index_T, Loc),
0da2c8ac 1968 Expression => Arr_Attr (B, Name_First, N))),
523456db 1969
0da2c8ac
AC
1970 Handled_Statement_Sequence =>
1971 Make_Handled_Sequence_Of_Statements (Loc,
1972 Statements => New_List (Loop_Stm)));
1973
523456db
AC
1974 -- If no separate indexes, return loop statement with explicit
1975 -- iteration scheme on its own
0da2c8ac
AC
1976
1977 else
523456db
AC
1978 Loop_Stm :=
1979 Make_Implicit_Loop_Statement (Nod,
1980 Statements => Stm_List,
1981 Iteration_Scheme =>
1982 Make_Iteration_Scheme (Loc,
1983 Loop_Parameter_Specification =>
1984 Make_Loop_Parameter_Specification (Loc,
1985 Defining_Identifier => An,
1986 Discrete_Subtype_Definition =>
1987 Arr_Attr (A, Name_Range, N))));
0da2c8ac
AC
1988 return Loop_Stm;
1989 end if;
fbf5a39b
AC
1990 end Handle_One_Dimension;
1991
1992 -----------------------
1993 -- Test_Empty_Arrays --
1994 -----------------------
1995
1996 function Test_Empty_Arrays return Node_Id is
1997 Alist : Node_Id;
1998 Blist : Node_Id;
1999
2000 Atest : Node_Id;
2001 Btest : Node_Id;
70482933 2002
fbf5a39b
AC
2003 begin
2004 Alist := Empty;
2005 Blist := Empty;
0da2c8ac 2006 for J in 1 .. Number_Dimensions (Ltyp) loop
fbf5a39b
AC
2007 Atest :=
2008 Make_Op_Eq (Loc,
2009 Left_Opnd => Arr_Attr (A, Name_Length, J),
2010 Right_Opnd => Make_Integer_Literal (Loc, 0));
2011
2012 Btest :=
2013 Make_Op_Eq (Loc,
2014 Left_Opnd => Arr_Attr (B, Name_Length, J),
2015 Right_Opnd => Make_Integer_Literal (Loc, 0));
2016
2017 if No (Alist) then
2018 Alist := Atest;
2019 Blist := Btest;
70482933 2020
fbf5a39b
AC
2021 else
2022 Alist :=
2023 Make_Or_Else (Loc,
2024 Left_Opnd => Relocate_Node (Alist),
2025 Right_Opnd => Atest);
2026
2027 Blist :=
2028 Make_Or_Else (Loc,
2029 Left_Opnd => Relocate_Node (Blist),
2030 Right_Opnd => Btest);
2031 end if;
2032 end loop;
70482933 2033
fbf5a39b
AC
2034 return
2035 Make_And_Then (Loc,
2036 Left_Opnd => Alist,
2037 Right_Opnd => Blist);
2038 end Test_Empty_Arrays;
70482933 2039
fbf5a39b
AC
2040 -----------------------------
2041 -- Test_Lengths_Correspond --
2042 -----------------------------
70482933 2043
fbf5a39b
AC
2044 function Test_Lengths_Correspond return Node_Id is
2045 Result : Node_Id;
2046 Rtest : Node_Id;
2047
2048 begin
2049 Result := Empty;
0da2c8ac 2050 for J in 1 .. Number_Dimensions (Ltyp) loop
fbf5a39b
AC
2051 Rtest :=
2052 Make_Op_Ne (Loc,
2053 Left_Opnd => Arr_Attr (A, Name_Length, J),
2054 Right_Opnd => Arr_Attr (B, Name_Length, J));
2055
2056 if No (Result) then
2057 Result := Rtest;
2058 else
2059 Result :=
2060 Make_Or_Else (Loc,
2061 Left_Opnd => Relocate_Node (Result),
2062 Right_Opnd => Rtest);
2063 end if;
2064 end loop;
2065
2066 return Result;
2067 end Test_Lengths_Correspond;
70482933
RK
2068
2069 -- Start of processing for Expand_Array_Equality
2070
2071 begin
0da2c8ac
AC
2072 Ltyp := Get_Arg_Type (Lhs);
2073 Rtyp := Get_Arg_Type (Rhs);
2074
685094bf
RD
2075 -- For now, if the argument types are not the same, go to the base type,
2076 -- since the code assumes that the formals have the same type. This is
2077 -- fixable in future ???
0da2c8ac
AC
2078
2079 if Ltyp /= Rtyp then
2080 Ltyp := Base_Type (Ltyp);
2081 Rtyp := Base_Type (Rtyp);
2082 pragma Assert (Ltyp = Rtyp);
2083 end if;
2084
2085 -- Build list of formals for function
2086
70482933
RK
2087 Formals := New_List (
2088 Make_Parameter_Specification (Loc,
2089 Defining_Identifier => A,
e4494292 2090 Parameter_Type => New_Occurrence_Of (Ltyp, Loc)),
70482933
RK
2091
2092 Make_Parameter_Specification (Loc,
2093 Defining_Identifier => B,
e4494292 2094 Parameter_Type => New_Occurrence_Of (Rtyp, Loc)));
70482933 2095
191fcb3a 2096 Func_Name := Make_Temporary (Loc, 'E');
70482933 2097
fbf5a39b 2098 -- Build statement sequence for function
70482933
RK
2099
2100 Func_Body :=
2101 Make_Subprogram_Body (Loc,
2102 Specification =>
2103 Make_Function_Specification (Loc,
2104 Defining_Unit_Name => Func_Name,
2105 Parameter_Specifications => Formals,
e4494292 2106 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
fbf5a39b
AC
2107
2108 Declarations => Decls,
2109
70482933
RK
2110 Handled_Statement_Sequence =>
2111 Make_Handled_Sequence_Of_Statements (Loc,
2112 Statements => New_List (
fbf5a39b
AC
2113
2114 Make_Implicit_If_Statement (Nod,
cc6f5d75 2115 Condition => Test_Empty_Arrays,
fbf5a39b 2116 Then_Statements => New_List (
d766cee3 2117 Make_Simple_Return_Statement (Loc,
fbf5a39b
AC
2118 Expression =>
2119 New_Occurrence_Of (Standard_True, Loc)))),
2120
2121 Make_Implicit_If_Statement (Nod,
cc6f5d75 2122 Condition => Test_Lengths_Correspond,
fbf5a39b 2123 Then_Statements => New_List (
d766cee3 2124 Make_Simple_Return_Statement (Loc,
cc6f5d75 2125 Expression => New_Occurrence_Of (Standard_False, Loc)))),
fbf5a39b 2126
0da2c8ac 2127 Handle_One_Dimension (1, First_Index (Ltyp)),
fbf5a39b 2128
d766cee3 2129 Make_Simple_Return_Statement (Loc,
70482933
RK
2130 Expression => New_Occurrence_Of (Standard_True, Loc)))));
2131
2132 Set_Has_Completion (Func_Name, True);
0da2c8ac 2133 Set_Is_Inlined (Func_Name);
70482933 2134
685094bf
RD
2135 -- If the array type is distinct from the type of the arguments, it
2136 -- is the full view of a private type. Apply an unchecked conversion
2137 -- to insure that analysis of the call succeeds.
70482933 2138
0da2c8ac
AC
2139 declare
2140 L, R : Node_Id;
2141
2142 begin
2143 L := Lhs;
2144 R := Rhs;
2145
2146 if No (Etype (Lhs))
2147 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
2148 then
2149 L := OK_Convert_To (Ltyp, Lhs);
2150 end if;
2151
2152 if No (Etype (Rhs))
2153 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
2154 then
2155 R := OK_Convert_To (Rtyp, Rhs);
2156 end if;
2157
2158 Actuals := New_List (L, R);
2159 end;
70482933
RK
2160
2161 Append_To (Bodies, Func_Body);
2162
2163 return
2164 Make_Function_Call (Loc,
e4494292 2165 Name => New_Occurrence_Of (Func_Name, Loc),
70482933
RK
2166 Parameter_Associations => Actuals);
2167 end Expand_Array_Equality;
2168
2169 -----------------------------
2170 -- Expand_Boolean_Operator --
2171 -----------------------------
2172
685094bf
RD
2173 -- Note that we first get the actual subtypes of the operands, since we
2174 -- always want to deal with types that have bounds.
70482933
RK
2175
2176 procedure Expand_Boolean_Operator (N : Node_Id) is
fbf5a39b 2177 Typ : constant Entity_Id := Etype (N);
70482933
RK
2178
2179 begin
685094bf
RD
2180 -- Special case of bit packed array where both operands are known to be
2181 -- properly aligned. In this case we use an efficient run time routine
2182 -- to carry out the operation (see System.Bit_Ops).
a9d8907c
JM
2183
2184 if Is_Bit_Packed_Array (Typ)
2185 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2186 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2187 then
70482933 2188 Expand_Packed_Boolean_Operator (N);
a9d8907c
JM
2189 return;
2190 end if;
70482933 2191
a9d8907c
JM
2192 -- For the normal non-packed case, the general expansion is to build
2193 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2194 -- and then inserting it into the tree. The original operator node is
2195 -- then rewritten as a call to this function. We also use this in the
2196 -- packed case if either operand is a possibly unaligned object.
70482933 2197
a9d8907c
JM
2198 declare
2199 Loc : constant Source_Ptr := Sloc (N);
2200 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2201 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2202 Func_Body : Node_Id;
2203 Func_Name : Entity_Id;
fbf5a39b 2204
a9d8907c
JM
2205 begin
2206 Convert_To_Actual_Subtype (L);
2207 Convert_To_Actual_Subtype (R);
2208 Ensure_Defined (Etype (L), N);
2209 Ensure_Defined (Etype (R), N);
2210 Apply_Length_Check (R, Etype (L));
2211
b4592168
GD
2212 if Nkind (N) = N_Op_Xor then
2213 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2214 end if;
2215
a9d8907c
JM
2216 if Nkind (Parent (N)) = N_Assignment_Statement
2217 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2218 then
2219 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
fbf5a39b 2220
a9d8907c
JM
2221 elsif Nkind (Parent (N)) = N_Op_Not
2222 and then Nkind (N) = N_Op_And
39f0fa29 2223 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
cc6f5d75 2224 and then Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
a9d8907c
JM
2225 then
2226 return;
2227 else
fbf5a39b 2228
a9d8907c
JM
2229 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2230 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2231 Insert_Action (N, Func_Body);
70482933 2232
a9d8907c 2233 -- Now rewrite the expression with a call
70482933 2234
a9d8907c
JM
2235 Rewrite (N,
2236 Make_Function_Call (Loc,
e4494292 2237 Name => New_Occurrence_Of (Func_Name, Loc),
a9d8907c
JM
2238 Parameter_Associations =>
2239 New_List (
2240 L,
2241 Make_Type_Conversion
e4494292 2242 (Loc, New_Occurrence_Of (Etype (L), Loc), R))));
70482933 2243
a9d8907c
JM
2244 Analyze_And_Resolve (N, Typ);
2245 end if;
2246 end;
70482933
RK
2247 end Expand_Boolean_Operator;
2248
456cbfa5
AC
2249 ------------------------------------------------
2250 -- Expand_Compare_Minimize_Eliminate_Overflow --
2251 ------------------------------------------------
2252
2253 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is
2254 Loc : constant Source_Ptr := Sloc (N);
2255
71fb4dc8
AC
2256 Result_Type : constant Entity_Id := Etype (N);
2257 -- Capture result type (could be a derived boolean type)
2258
456cbfa5
AC
2259 Llo, Lhi : Uint;
2260 Rlo, Rhi : Uint;
2261
2262 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
2263 -- Entity for Long_Long_Integer'Base
2264
15c94a55 2265 Check : constant Overflow_Mode_Type := Overflow_Check_Mode;
a7f1b24f 2266 -- Current overflow checking mode
456cbfa5
AC
2267
2268 procedure Set_True;
2269 procedure Set_False;
2270 -- These procedures rewrite N with an occurrence of Standard_True or
2271 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2272
2273 ---------------
2274 -- Set_False --
2275 ---------------
2276
2277 procedure Set_False is
2278 begin
2279 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2280 Warn_On_Known_Condition (N);
2281 end Set_False;
2282
2283 --------------
2284 -- Set_True --
2285 --------------
2286
2287 procedure Set_True is
2288 begin
2289 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2290 Warn_On_Known_Condition (N);
2291 end Set_True;
2292
2293 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2294
2295 begin
2296 -- Nothing to do unless we have a comparison operator with operands
2297 -- that are signed integer types, and we are operating in either
2298 -- MINIMIZED or ELIMINATED overflow checking mode.
2299
2300 if Nkind (N) not in N_Op_Compare
2301 or else Check not in Minimized_Or_Eliminated
2302 or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N)))
2303 then
2304 return;
2305 end if;
2306
2307 -- OK, this is the case we are interested in. First step is to process
2308 -- our operands using the Minimize_Eliminate circuitry which applies
2309 -- this processing to the two operand subtrees.
2310
a7f1b24f 2311 Minimize_Eliminate_Overflows
c7e152b5 2312 (Left_Opnd (N), Llo, Lhi, Top_Level => False);
a7f1b24f 2313 Minimize_Eliminate_Overflows
c7e152b5 2314 (Right_Opnd (N), Rlo, Rhi, Top_Level => False);
456cbfa5 2315
65f7ed64
AC
2316 -- See if the range information decides the result of the comparison.
2317 -- We can only do this if we in fact have full range information (which
2318 -- won't be the case if either operand is bignum at this stage).
456cbfa5 2319
65f7ed64
AC
2320 if Llo /= No_Uint and then Rlo /= No_Uint then
2321 case N_Op_Compare (Nkind (N)) is
456cbfa5
AC
2322 when N_Op_Eq =>
2323 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2324 Set_True;
a40ada7e 2325 elsif Llo > Rhi or else Lhi < Rlo then
456cbfa5
AC
2326 Set_False;
2327 end if;
2328
2329 when N_Op_Ge =>
2330 if Llo >= Rhi then
2331 Set_True;
2332 elsif Lhi < Rlo then
2333 Set_False;
2334 end if;
2335
2336 when N_Op_Gt =>
2337 if Llo > Rhi then
2338 Set_True;
2339 elsif Lhi <= Rlo then
2340 Set_False;
2341 end if;
2342
2343 when N_Op_Le =>
2344 if Llo > Rhi then
2345 Set_False;
2346 elsif Lhi <= Rlo then
2347 Set_True;
2348 end if;
2349
2350 when N_Op_Lt =>
2351 if Llo >= Rhi then
456cbfa5 2352 Set_False;
b6b5cca8
AC
2353 elsif Lhi < Rlo then
2354 Set_True;
456cbfa5
AC
2355 end if;
2356
2357 when N_Op_Ne =>
2358 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
456cbfa5 2359 Set_False;
a40ada7e
RD
2360 elsif Llo > Rhi or else Lhi < Rlo then
2361 Set_True;
456cbfa5 2362 end if;
65f7ed64 2363 end case;
456cbfa5 2364
65f7ed64 2365 -- All done if we did the rewrite
456cbfa5 2366
65f7ed64
AC
2367 if Nkind (N) not in N_Op_Compare then
2368 return;
2369 end if;
456cbfa5
AC
2370 end if;
2371
2372 -- Otherwise, time to do the comparison
2373
2374 declare
2375 Ltype : constant Entity_Id := Etype (Left_Opnd (N));
2376 Rtype : constant Entity_Id := Etype (Right_Opnd (N));
2377
2378 begin
2379 -- If the two operands have the same signed integer type we are
2380 -- all set, nothing more to do. This is the case where either
2381 -- both operands were unchanged, or we rewrote both of them to
2382 -- be Long_Long_Integer.
2383
2384 -- Note: Entity for the comparison may be wrong, but it's not worth
2385 -- the effort to change it, since the back end does not use it.
2386
2387 if Is_Signed_Integer_Type (Ltype)
2388 and then Base_Type (Ltype) = Base_Type (Rtype)
2389 then
2390 return;
2391
2392 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2393
2394 elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then
2395 declare
2396 Left : Node_Id := Left_Opnd (N);
2397 Right : Node_Id := Right_Opnd (N);
2398 -- Bignum references for left and right operands
2399
2400 begin
2401 if not Is_RTE (Ltype, RE_Bignum) then
2402 Left := Convert_To_Bignum (Left);
2403 elsif not Is_RTE (Rtype, RE_Bignum) then
2404 Right := Convert_To_Bignum (Right);
2405 end if;
2406
71fb4dc8 2407 -- We rewrite our node with:
456cbfa5 2408
71fb4dc8
AC
2409 -- do
2410 -- Bnn : Result_Type;
2411 -- declare
2412 -- M : Mark_Id := SS_Mark;
2413 -- begin
2414 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2415 -- SS_Release (M);
2416 -- end;
2417 -- in
2418 -- Bnn
2419 -- end
456cbfa5
AC
2420
2421 declare
71fb4dc8 2422 Blk : constant Node_Id := Make_Bignum_Block (Loc);
456cbfa5
AC
2423 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
2424 Ent : RE_Id;
2425
2426 begin
2427 case N_Op_Compare (Nkind (N)) is
2428 when N_Op_Eq => Ent := RE_Big_EQ;
2429 when N_Op_Ge => Ent := RE_Big_GE;
2430 when N_Op_Gt => Ent := RE_Big_GT;
2431 when N_Op_Le => Ent := RE_Big_LE;
2432 when N_Op_Lt => Ent := RE_Big_LT;
2433 when N_Op_Ne => Ent := RE_Big_NE;
2434 end case;
2435
71fb4dc8 2436 -- Insert assignment to Bnn into the bignum block
456cbfa5
AC
2437
2438 Insert_Before
2439 (First (Statements (Handled_Statement_Sequence (Blk))),
2440 Make_Assignment_Statement (Loc,
2441 Name => New_Occurrence_Of (Bnn, Loc),
2442 Expression =>
2443 Make_Function_Call (Loc,
2444 Name =>
2445 New_Occurrence_Of (RTE (Ent), Loc),
2446 Parameter_Associations => New_List (Left, Right))));
2447
71fb4dc8
AC
2448 -- Now do the rewrite with expression actions
2449
2450 Rewrite (N,
2451 Make_Expression_With_Actions (Loc,
2452 Actions => New_List (
2453 Make_Object_Declaration (Loc,
2454 Defining_Identifier => Bnn,
2455 Object_Definition =>
2456 New_Occurrence_Of (Result_Type, Loc)),
2457 Blk),
2458 Expression => New_Occurrence_Of (Bnn, Loc)));
2459 Analyze_And_Resolve (N, Result_Type);
456cbfa5
AC
2460 end;
2461 end;
2462
2463 -- No bignums involved, but types are different, so we must have
2464 -- rewritten one of the operands as a Long_Long_Integer but not
2465 -- the other one.
2466
2467 -- If left operand is Long_Long_Integer, convert right operand
2468 -- and we are done (with a comparison of two Long_Long_Integers).
2469
2470 elsif Ltype = LLIB then
2471 Convert_To_And_Rewrite (LLIB, Right_Opnd (N));
2472 Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks);
2473 return;
2474
2475 -- If right operand is Long_Long_Integer, convert left operand
2476 -- and we are done (with a comparison of two Long_Long_Integers).
2477
2478 -- This is the only remaining possibility
2479
2480 else pragma Assert (Rtype = LLIB);
2481 Convert_To_And_Rewrite (LLIB, Left_Opnd (N));
2482 Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks);
2483 return;
2484 end if;
2485 end;
2486 end Expand_Compare_Minimize_Eliminate_Overflow;
2487
70482933
RK
2488 -------------------------------
2489 -- Expand_Composite_Equality --
2490 -------------------------------
2491
2492 -- This function is only called for comparing internal fields of composite
2493 -- types when these fields are themselves composites. This is a special
2494 -- case because it is not possible to respect normal Ada visibility rules.
2495
2496 function Expand_Composite_Equality
2497 (Nod : Node_Id;
2498 Typ : Entity_Id;
2499 Lhs : Node_Id;
2500 Rhs : Node_Id;
2e071734 2501 Bodies : List_Id) return Node_Id
70482933
RK
2502 is
2503 Loc : constant Source_Ptr := Sloc (Nod);
2504 Full_Type : Entity_Id;
2505 Prim : Elmt_Id;
2506 Eq_Op : Entity_Id;
2507
7efc3f2d
AC
2508 function Find_Primitive_Eq return Node_Id;
2509 -- AI05-0123: Locate primitive equality for type if it exists, and
2510 -- build the corresponding call. If operation is abstract, replace
2511 -- call with an explicit raise. Return Empty if there is no primitive.
2512
2513 -----------------------
2514 -- Find_Primitive_Eq --
2515 -----------------------
2516
2517 function Find_Primitive_Eq return Node_Id is
2518 Prim_E : Elmt_Id;
2519 Prim : Node_Id;
2520
2521 begin
2522 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2523 while Present (Prim_E) loop
2524 Prim := Node (Prim_E);
2525
2526 -- Locate primitive equality with the right signature
2527
2528 if Chars (Prim) = Name_Op_Eq
2529 and then Etype (First_Formal (Prim)) =
39ade2f9 2530 Etype (Next_Formal (First_Formal (Prim)))
7efc3f2d
AC
2531 and then Etype (Prim) = Standard_Boolean
2532 then
2533 if Is_Abstract_Subprogram (Prim) then
2534 return
2535 Make_Raise_Program_Error (Loc,
2536 Reason => PE_Explicit_Raise);
2537
2538 else
2539 return
2540 Make_Function_Call (Loc,
e4494292 2541 Name => New_Occurrence_Of (Prim, Loc),
7efc3f2d
AC
2542 Parameter_Associations => New_List (Lhs, Rhs));
2543 end if;
2544 end if;
2545
2546 Next_Elmt (Prim_E);
2547 end loop;
2548
2549 -- If not found, predefined operation will be used
2550
2551 return Empty;
2552 end Find_Primitive_Eq;
2553
2554 -- Start of processing for Expand_Composite_Equality
2555
70482933
RK
2556 begin
2557 if Is_Private_Type (Typ) then
2558 Full_Type := Underlying_Type (Typ);
2559 else
2560 Full_Type := Typ;
2561 end if;
2562
ced8450b
ES
2563 -- If the private type has no completion the context may be the
2564 -- expansion of a composite equality for a composite type with some
2565 -- still incomplete components. The expression will not be analyzed
2566 -- until the enclosing type is completed, at which point this will be
2567 -- properly expanded, unless there is a bona fide completion error.
70482933
RK
2568
2569 if No (Full_Type) then
ced8450b 2570 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
70482933
RK
2571 end if;
2572
2573 Full_Type := Base_Type (Full_Type);
2574
da1b76c1
HK
2575 -- When the base type itself is private, use the full view to expand
2576 -- the composite equality.
2577
2578 if Is_Private_Type (Full_Type) then
2579 Full_Type := Underlying_Type (Full_Type);
2580 end if;
2581
16788d44
RD
2582 -- Case of array types
2583
70482933
RK
2584 if Is_Array_Type (Full_Type) then
2585
2586 -- If the operand is an elementary type other than a floating-point
2587 -- type, then we can simply use the built-in block bitwise equality,
2588 -- since the predefined equality operators always apply and bitwise
2589 -- equality is fine for all these cases.
2590
2591 if Is_Elementary_Type (Component_Type (Full_Type))
2592 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2593 then
39ade2f9 2594 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
70482933 2595
685094bf
RD
2596 -- For composite component types, and floating-point types, use the
2597 -- expansion. This deals with tagged component types (where we use
2598 -- the applicable equality routine) and floating-point, (where we
2599 -- need to worry about negative zeroes), and also the case of any
2600 -- composite type recursively containing such fields.
70482933
RK
2601
2602 else
0da2c8ac 2603 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
70482933
RK
2604 end if;
2605
16788d44
RD
2606 -- Case of tagged record types
2607
70482933
RK
2608 elsif Is_Tagged_Type (Full_Type) then
2609
2610 -- Call the primitive operation "=" of this type
2611
2612 if Is_Class_Wide_Type (Full_Type) then
2613 Full_Type := Root_Type (Full_Type);
2614 end if;
2615
685094bf
RD
2616 -- If this is derived from an untagged private type completed with a
2617 -- tagged type, it does not have a full view, so we use the primitive
2618 -- operations of the private type. This check should no longer be
2619 -- necessary when these types receive their full views ???
70482933
RK
2620
2621 if Is_Private_Type (Typ)
2622 and then not Is_Tagged_Type (Typ)
2623 and then not Is_Controlled (Typ)
2624 and then Is_Derived_Type (Typ)
2625 and then No (Full_View (Typ))
2626 then
2627 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2628 else
2629 Prim := First_Elmt (Primitive_Operations (Full_Type));
2630 end if;
2631
2632 loop
2633 Eq_Op := Node (Prim);
2634 exit when Chars (Eq_Op) = Name_Op_Eq
2635 and then Etype (First_Formal (Eq_Op)) =
e6f69614
AC
2636 Etype (Next_Formal (First_Formal (Eq_Op)))
2637 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
70482933
RK
2638 Next_Elmt (Prim);
2639 pragma Assert (Present (Prim));
2640 end loop;
2641
2642 Eq_Op := Node (Prim);
2643
2644 return
2645 Make_Function_Call (Loc,
e4494292 2646 Name => New_Occurrence_Of (Eq_Op, Loc),
70482933
RK
2647 Parameter_Associations =>
2648 New_List
2649 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2650 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2651
16788d44
RD
2652 -- Case of untagged record types
2653
70482933 2654 elsif Is_Record_Type (Full_Type) then
fbf5a39b 2655 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
70482933
RK
2656
2657 if Present (Eq_Op) then
2658 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2659
685094bf
RD
2660 -- Inherited equality from parent type. Convert the actuals to
2661 -- match signature of operation.
70482933
RK
2662
2663 declare
fbf5a39b 2664 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
70482933
RK
2665
2666 begin
2667 return
2668 Make_Function_Call (Loc,
e4494292 2669 Name => New_Occurrence_Of (Eq_Op, Loc),
39ade2f9
AC
2670 Parameter_Associations => New_List (
2671 OK_Convert_To (T, Lhs),
2672 OK_Convert_To (T, Rhs)));
70482933
RK
2673 end;
2674
2675 else
5d09245e
AC
2676 -- Comparison between Unchecked_Union components
2677
2678 if Is_Unchecked_Union (Full_Type) then
2679 declare
2680 Lhs_Type : Node_Id := Full_Type;
2681 Rhs_Type : Node_Id := Full_Type;
2682 Lhs_Discr_Val : Node_Id;
2683 Rhs_Discr_Val : Node_Id;
2684
2685 begin
2686 -- Lhs subtype
2687
2688 if Nkind (Lhs) = N_Selected_Component then
2689 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2690 end if;
2691
2692 -- Rhs subtype
2693
2694 if Nkind (Rhs) = N_Selected_Component then
2695 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2696 end if;
2697
2698 -- Lhs of the composite equality
2699
2700 if Is_Constrained (Lhs_Type) then
2701
685094bf 2702 -- Since the enclosing record type can never be an
5d09245e
AC
2703 -- Unchecked_Union (this code is executed for records
2704 -- that do not have variants), we may reference its
2705 -- discriminant(s).
2706
2707 if Nkind (Lhs) = N_Selected_Component
533369aa
AC
2708 and then Has_Per_Object_Constraint
2709 (Entity (Selector_Name (Lhs)))
5d09245e
AC
2710 then
2711 Lhs_Discr_Val :=
2712 Make_Selected_Component (Loc,
39ade2f9 2713 Prefix => Prefix (Lhs),
5d09245e 2714 Selector_Name =>
39ade2f9
AC
2715 New_Copy
2716 (Get_Discriminant_Value
2717 (First_Discriminant (Lhs_Type),
2718 Lhs_Type,
2719 Stored_Constraint (Lhs_Type))));
5d09245e
AC
2720
2721 else
39ade2f9
AC
2722 Lhs_Discr_Val :=
2723 New_Copy
2724 (Get_Discriminant_Value
2725 (First_Discriminant (Lhs_Type),
2726 Lhs_Type,
2727 Stored_Constraint (Lhs_Type)));
5d09245e
AC
2728
2729 end if;
2730 else
2731 -- It is not possible to infer the discriminant since
2732 -- the subtype is not constrained.
2733
8aceda64 2734 return
5d09245e 2735 Make_Raise_Program_Error (Loc,
8aceda64 2736 Reason => PE_Unchecked_Union_Restriction);
5d09245e
AC
2737 end if;
2738
2739 -- Rhs of the composite equality
2740
2741 if Is_Constrained (Rhs_Type) then
2742 if Nkind (Rhs) = N_Selected_Component
39ade2f9
AC
2743 and then Has_Per_Object_Constraint
2744 (Entity (Selector_Name (Rhs)))
5d09245e
AC
2745 then
2746 Rhs_Discr_Val :=
2747 Make_Selected_Component (Loc,
39ade2f9 2748 Prefix => Prefix (Rhs),
5d09245e 2749 Selector_Name =>
39ade2f9
AC
2750 New_Copy
2751 (Get_Discriminant_Value
2752 (First_Discriminant (Rhs_Type),
2753 Rhs_Type,
2754 Stored_Constraint (Rhs_Type))));
5d09245e
AC
2755
2756 else
39ade2f9
AC
2757 Rhs_Discr_Val :=
2758 New_Copy
2759 (Get_Discriminant_Value
2760 (First_Discriminant (Rhs_Type),
2761 Rhs_Type,
2762 Stored_Constraint (Rhs_Type)));
5d09245e
AC
2763
2764 end if;
2765 else
8aceda64 2766 return
5d09245e 2767 Make_Raise_Program_Error (Loc,
8aceda64 2768 Reason => PE_Unchecked_Union_Restriction);
5d09245e
AC
2769 end if;
2770
2771 -- Call the TSS equality function with the inferred
2772 -- discriminant values.
2773
2774 return
2775 Make_Function_Call (Loc,
e4494292 2776 Name => New_Occurrence_Of (Eq_Op, Loc),
5d09245e
AC
2777 Parameter_Associations => New_List (
2778 Lhs,
2779 Rhs,
2780 Lhs_Discr_Val,
2781 Rhs_Discr_Val));
2782 end;
d151d6a3 2783
316e3a13
RD
2784 -- All cases other than comparing Unchecked_Union types
2785
d151d6a3 2786 else
7f1a5156
EB
2787 declare
2788 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
7f1a5156
EB
2789 begin
2790 return
2791 Make_Function_Call (Loc,
316e3a13
RD
2792 Name =>
2793 New_Occurrence_Of (Eq_Op, Loc),
7f1a5156
EB
2794 Parameter_Associations => New_List (
2795 OK_Convert_To (T, Lhs),
2796 OK_Convert_To (T, Rhs)));
2797 end;
5d09245e 2798 end if;
d151d6a3 2799 end if;
5d09245e 2800
3058f181
BD
2801 -- Equality composes in Ada 2012 for untagged record types. It also
2802 -- composes for bounded strings, because they are part of the
2803 -- predefined environment. We could make it compose for bounded
2804 -- strings by making them tagged, or by making sure all subcomponents
2805 -- are set to the same value, even when not used. Instead, we have
2806 -- this special case in the compiler, because it's more efficient.
2807
2808 elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then
5d09245e 2809
08daa782 2810 -- If no TSS has been created for the type, check whether there is
7efc3f2d 2811 -- a primitive equality declared for it.
d151d6a3
AC
2812
2813 declare
3058f181 2814 Op : constant Node_Id := Find_Primitive_Eq;
d151d6a3
AC
2815
2816 begin
a1fc903a
AC
2817 -- Use user-defined primitive if it exists, otherwise use
2818 -- predefined equality.
2819
3058f181
BD
2820 if Present (Op) then
2821 return Op;
7efc3f2d 2822 else
7efc3f2d
AC
2823 return Make_Op_Eq (Loc, Lhs, Rhs);
2824 end if;
d151d6a3
AC
2825 end;
2826
70482933
RK
2827 else
2828 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2829 end if;
2830
16788d44 2831 -- Non-composite types (always use predefined equality)
70482933 2832
16788d44 2833 else
70482933
RK
2834 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2835 end if;
2836 end Expand_Composite_Equality;
2837
fdac1f80
AC
2838 ------------------------
2839 -- Expand_Concatenate --
2840 ------------------------
70482933 2841
fdac1f80
AC
2842 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2843 Loc : constant Source_Ptr := Sloc (Cnode);
70482933 2844
fdac1f80
AC
2845 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2846 -- Result type of concatenation
70482933 2847
fdac1f80
AC
2848 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2849 -- Component type. Elements of this component type can appear as one
2850 -- of the operands of concatenation as well as arrays.
70482933 2851
ecc4ddde
AC
2852 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2853 -- Index subtype
2854
2855 Ityp : constant Entity_Id := Base_Type (Istyp);
2856 -- Index type. This is the base type of the index subtype, and is used
2857 -- for all computed bounds (which may be out of range of Istyp in the
2858 -- case of null ranges).
70482933 2859
46ff89f3 2860 Artyp : Entity_Id;
fdac1f80
AC
2861 -- This is the type we use to do arithmetic to compute the bounds and
2862 -- lengths of operands. The choice of this type is a little subtle and
2863 -- is discussed in a separate section at the start of the body code.
70482933 2864
fdac1f80
AC
2865 Concatenation_Error : exception;
2866 -- Raised if concatenation is sure to raise a CE
70482933 2867
0ac73189
AC
2868 Result_May_Be_Null : Boolean := True;
2869 -- Reset to False if at least one operand is encountered which is known
2870 -- at compile time to be non-null. Used for handling the special case
2871 -- of setting the high bound to the last operand high bound for a null
2872 -- result, thus ensuring a proper high bound in the super-flat case.
2873
df46b832 2874 N : constant Nat := List_Length (Opnds);
fdac1f80 2875 -- Number of concatenation operands including possibly null operands
df46b832
AC
2876
2877 NN : Nat := 0;
a29262fd
AC
2878 -- Number of operands excluding any known to be null, except that the
2879 -- last operand is always retained, in case it provides the bounds for
2880 -- a null result.
2881
2882 Opnd : Node_Id;
2883 -- Current operand being processed in the loop through operands. After
2884 -- this loop is complete, always contains the last operand (which is not
2885 -- the same as Operands (NN), since null operands are skipped).
df46b832
AC
2886
2887 -- Arrays describing the operands, only the first NN entries of each
2888 -- array are set (NN < N when we exclude known null operands).
2889
2890 Is_Fixed_Length : array (1 .. N) of Boolean;
2891 -- True if length of corresponding operand known at compile time
2892
2893 Operands : array (1 .. N) of Node_Id;
a29262fd
AC
2894 -- Set to the corresponding entry in the Opnds list (but note that null
2895 -- operands are excluded, so not all entries in the list are stored).
df46b832
AC
2896
2897 Fixed_Length : array (1 .. N) of Uint;
fdac1f80
AC
2898 -- Set to length of operand. Entries in this array are set only if the
2899 -- corresponding entry in Is_Fixed_Length is True.
df46b832 2900
0ac73189
AC
2901 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2902 -- Set to lower bound of operand. Either an integer literal in the case
2903 -- where the bound is known at compile time, else actual lower bound.
2904 -- The operand low bound is of type Ityp.
2905
df46b832
AC
2906 Var_Length : array (1 .. N) of Entity_Id;
2907 -- Set to an entity of type Natural that contains the length of an
2908 -- operand whose length is not known at compile time. Entries in this
2909 -- array are set only if the corresponding entry in Is_Fixed_Length
46ff89f3 2910 -- is False. The entity is of type Artyp.
df46b832
AC
2911
2912 Aggr_Length : array (0 .. N) of Node_Id;
fdac1f80
AC
2913 -- The J'th entry in an expression node that represents the total length
2914 -- of operands 1 through J. It is either an integer literal node, or a
2915 -- reference to a constant entity with the right value, so it is fine
2916 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
46ff89f3 2917 -- entry always is set to zero. The length is of type Artyp.
df46b832
AC
2918
2919 Low_Bound : Node_Id;
0ac73189
AC
2920 -- A tree node representing the low bound of the result (of type Ityp).
2921 -- This is either an integer literal node, or an identifier reference to
2922 -- a constant entity initialized to the appropriate value.
2923
88a27b18
AC
2924 Last_Opnd_Low_Bound : Node_Id;
2925 -- A tree node representing the low bound of the last operand. This
2926 -- need only be set if the result could be null. It is used for the
2927 -- special case of setting the right low bound for a null result.
2928 -- This is of type Ityp.
2929
a29262fd
AC
2930 Last_Opnd_High_Bound : Node_Id;
2931 -- A tree node representing the high bound of the last operand. This
2932 -- need only be set if the result could be null. It is used for the
2933 -- special case of setting the right high bound for a null result.
2934 -- This is of type Ityp.
2935
0ac73189
AC
2936 High_Bound : Node_Id;
2937 -- A tree node representing the high bound of the result (of type Ityp)
df46b832
AC
2938
2939 Result : Node_Id;
0ac73189 2940 -- Result of the concatenation (of type Ityp)
df46b832 2941
d0f8d157 2942 Actions : constant List_Id := New_List;
4c9fe6c7 2943 -- Collect actions to be inserted
d0f8d157 2944
fa969310 2945 Known_Non_Null_Operand_Seen : Boolean;
308e6f3a 2946 -- Set True during generation of the assignments of operands into
fa969310
AC
2947 -- result once an operand known to be non-null has been seen.
2948
2949 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2950 -- This function makes an N_Integer_Literal node that is returned in
2951 -- analyzed form with the type set to Artyp. Importantly this literal
2952 -- is not flagged as static, so that if we do computations with it that
2953 -- result in statically detected out of range conditions, we will not
2954 -- generate error messages but instead warning messages.
2955
46ff89f3 2956 function To_Artyp (X : Node_Id) return Node_Id;
fdac1f80 2957 -- Given a node of type Ityp, returns the corresponding value of type
76c597a1
AC
2958 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2959 -- For enum types, the Pos of the value is returned.
fdac1f80
AC
2960
2961 function To_Ityp (X : Node_Id) return Node_Id;
0ac73189 2962 -- The inverse function (uses Val in the case of enumeration types)
fdac1f80 2963
fa969310
AC
2964 ------------------------
2965 -- Make_Artyp_Literal --
2966 ------------------------
2967
2968 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2969 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2970 begin
2971 Set_Etype (Result, Artyp);
2972 Set_Analyzed (Result, True);
2973 Set_Is_Static_Expression (Result, False);
2974 return Result;
2975 end Make_Artyp_Literal;
76c597a1 2976
fdac1f80 2977 --------------
46ff89f3 2978 -- To_Artyp --
fdac1f80
AC
2979 --------------
2980
46ff89f3 2981 function To_Artyp (X : Node_Id) return Node_Id is
fdac1f80 2982 begin
46ff89f3 2983 if Ityp = Base_Type (Artyp) then
fdac1f80
AC
2984 return X;
2985
2986 elsif Is_Enumeration_Type (Ityp) then
2987 return
2988 Make_Attribute_Reference (Loc,
2989 Prefix => New_Occurrence_Of (Ityp, Loc),
2990 Attribute_Name => Name_Pos,
2991 Expressions => New_List (X));
2992
2993 else
46ff89f3 2994 return Convert_To (Artyp, X);
fdac1f80 2995 end if;
46ff89f3 2996 end To_Artyp;
fdac1f80
AC
2997
2998 -------------
2999 -- To_Ityp --
3000 -------------
3001
3002 function To_Ityp (X : Node_Id) return Node_Id is
3003 begin
2fc05e3d 3004 if Is_Enumeration_Type (Ityp) then
fdac1f80
AC
3005 return
3006 Make_Attribute_Reference (Loc,
3007 Prefix => New_Occurrence_Of (Ityp, Loc),
3008 Attribute_Name => Name_Val,
3009 Expressions => New_List (X));
3010
3011 -- Case where we will do a type conversion
3012
3013 else
76c597a1
AC
3014 if Ityp = Base_Type (Artyp) then
3015 return X;
fdac1f80 3016 else
76c597a1 3017 return Convert_To (Ityp, X);
fdac1f80
AC
3018 end if;
3019 end if;
3020 end To_Ityp;
3021
3022 -- Local Declarations
3023
00ba7be8
AC
3024 Lib_Level_Target : constant Boolean :=
3025 Nkind (Parent (Cnode)) = N_Object_Declaration
3026 and then
3027 Is_Library_Level_Entity (Defining_Identifier (Parent (Cnode)));
3028
3029 -- If the concatenation declares a library level entity, we call the
3030 -- built-in concatenation routines to prevent code bloat, regardless
3031 -- of optimization level. This is space-efficient, and prevent linking
3032 -- problems when units are compiled with different optimizations.
3033
0ac73189
AC
3034 Opnd_Typ : Entity_Id;
3035 Ent : Entity_Id;
3036 Len : Uint;
3037 J : Nat;
3038 Clen : Node_Id;
3039 Set : Boolean;
70482933 3040
f46faa08
AC
3041 -- Start of processing for Expand_Concatenate
3042
70482933 3043 begin
fdac1f80
AC
3044 -- Choose an appropriate computational type
3045
3046 -- We will be doing calculations of lengths and bounds in this routine
3047 -- and computing one from the other in some cases, e.g. getting the high
3048 -- bound by adding the length-1 to the low bound.
3049
3050 -- We can't just use the index type, or even its base type for this
3051 -- purpose for two reasons. First it might be an enumeration type which
308e6f3a
RW
3052 -- is not suitable for computations of any kind, and second it may
3053 -- simply not have enough range. For example if the index type is
3054 -- -128..+127 then lengths can be up to 256, which is out of range of
3055 -- the type.
fdac1f80
AC
3056
3057 -- For enumeration types, we can simply use Standard_Integer, this is
3058 -- sufficient since the actual number of enumeration literals cannot
3059 -- possibly exceed the range of integer (remember we will be doing the
0ac73189 3060 -- arithmetic with POS values, not representation values).
fdac1f80
AC
3061
3062 if Is_Enumeration_Type (Ityp) then
46ff89f3 3063 Artyp := Standard_Integer;
fdac1f80 3064
59262ebb
AC
3065 -- If index type is Positive, we use the standard unsigned type, to give
3066 -- more room on the top of the range, obviating the need for an overflow
3067 -- check when creating the upper bound. This is needed to avoid junk
3068 -- overflow checks in the common case of String types.
3069
3070 -- ??? Disabled for now
3071
3072 -- elsif Istyp = Standard_Positive then
3073 -- Artyp := Standard_Unsigned;
3074
2fc05e3d
AC
3075 -- For modular types, we use a 32-bit modular type for types whose size
3076 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
3077 -- identity type, and for larger unsigned types we use 64-bits.
fdac1f80 3078
2fc05e3d 3079 elsif Is_Modular_Integer_Type (Ityp) then
ecc4ddde 3080 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
46ff89f3 3081 Artyp := Standard_Unsigned;
ecc4ddde 3082 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
46ff89f3 3083 Artyp := Ityp;
fdac1f80 3084 else
46ff89f3 3085 Artyp := RTE (RE_Long_Long_Unsigned);
fdac1f80
AC
3086 end if;
3087
2fc05e3d 3088 -- Similar treatment for signed types
fdac1f80
AC
3089
3090 else
ecc4ddde 3091 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
46ff89f3 3092 Artyp := Standard_Integer;
ecc4ddde 3093 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
46ff89f3 3094 Artyp := Ityp;
fdac1f80 3095 else
46ff89f3 3096 Artyp := Standard_Long_Long_Integer;
fdac1f80
AC
3097 end if;
3098 end if;
3099
fa969310
AC
3100 -- Supply dummy entry at start of length array
3101
3102 Aggr_Length (0) := Make_Artyp_Literal (0);
3103
fdac1f80 3104 -- Go through operands setting up the above arrays
70482933 3105
df46b832
AC
3106 J := 1;
3107 while J <= N loop
3108 Opnd := Remove_Head (Opnds);
0ac73189 3109 Opnd_Typ := Etype (Opnd);
fdac1f80
AC
3110
3111 -- The parent got messed up when we put the operands in a list,
d347f572
AC
3112 -- so now put back the proper parent for the saved operand, that
3113 -- is to say the concatenation node, to make sure that each operand
3114 -- is seen as a subexpression, e.g. if actions must be inserted.
fdac1f80 3115
d347f572 3116 Set_Parent (Opnd, Cnode);
fdac1f80
AC
3117
3118 -- Set will be True when we have setup one entry in the array
3119
df46b832
AC
3120 Set := False;
3121
fdac1f80 3122 -- Singleton element (or character literal) case
df46b832 3123
0ac73189 3124 if Base_Type (Opnd_Typ) = Ctyp then
df46b832
AC
3125 NN := NN + 1;
3126 Operands (NN) := Opnd;
3127 Is_Fixed_Length (NN) := True;
3128 Fixed_Length (NN) := Uint_1;
0ac73189 3129 Result_May_Be_Null := False;
fdac1f80 3130
a29262fd
AC
3131 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
3132 -- since we know that the result cannot be null).
fdac1f80 3133
0ac73189
AC
3134 Opnd_Low_Bound (NN) :=
3135 Make_Attribute_Reference (Loc,
e4494292 3136 Prefix => New_Occurrence_Of (Istyp, Loc),
0ac73189
AC
3137 Attribute_Name => Name_First);
3138
df46b832
AC
3139 Set := True;
3140
fdac1f80 3141 -- String literal case (can only occur for strings of course)
df46b832
AC
3142
3143 elsif Nkind (Opnd) = N_String_Literal then
0ac73189 3144 Len := String_Literal_Length (Opnd_Typ);
df46b832 3145
a29262fd
AC
3146 if Len /= 0 then
3147 Result_May_Be_Null := False;
3148 end if;
3149
88a27b18 3150 -- Capture last operand low and high bound if result could be null
a29262fd
AC
3151
3152 if J = N and then Result_May_Be_Null then
88a27b18
AC
3153 Last_Opnd_Low_Bound :=
3154 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3155
a29262fd 3156 Last_Opnd_High_Bound :=
88a27b18 3157 Make_Op_Subtract (Loc,
a29262fd
AC
3158 Left_Opnd =>
3159 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
59262ebb 3160 Right_Opnd => Make_Integer_Literal (Loc, 1));
a29262fd
AC
3161 end if;
3162
3163 -- Skip null string literal
fdac1f80 3164
0ac73189 3165 if J < N and then Len = 0 then
df46b832
AC
3166 goto Continue;
3167 end if;
3168
3169 NN := NN + 1;
3170 Operands (NN) := Opnd;
3171 Is_Fixed_Length (NN) := True;
0ac73189
AC
3172
3173 -- Set length and bounds
3174
df46b832 3175 Fixed_Length (NN) := Len;
0ac73189
AC
3176
3177 Opnd_Low_Bound (NN) :=
3178 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3179
df46b832
AC
3180 Set := True;
3181
3182 -- All other cases
3183
3184 else
3185 -- Check constrained case with known bounds
3186
0ac73189 3187 if Is_Constrained (Opnd_Typ) then
df46b832 3188 declare
df46b832
AC
3189 Index : constant Node_Id := First_Index (Opnd_Typ);
3190 Indx_Typ : constant Entity_Id := Etype (Index);
3191 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
3192 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
3193
3194 begin
fdac1f80
AC
3195 -- Fixed length constrained array type with known at compile
3196 -- time bounds is last case of fixed length operand.
df46b832
AC
3197
3198 if Compile_Time_Known_Value (Lo)
3199 and then
3200 Compile_Time_Known_Value (Hi)
3201 then
3202 declare
3203 Loval : constant Uint := Expr_Value (Lo);
3204 Hival : constant Uint := Expr_Value (Hi);
3205 Len : constant Uint :=
3206 UI_Max (Hival - Loval + 1, Uint_0);
3207
3208 begin
0ac73189
AC
3209 if Len > 0 then
3210 Result_May_Be_Null := False;
df46b832 3211 end if;
0ac73189 3212
88a27b18 3213 -- Capture last operand bounds if result could be null
a29262fd
AC
3214
3215 if J = N and then Result_May_Be_Null then
88a27b18
AC
3216 Last_Opnd_Low_Bound :=
3217 Convert_To (Ityp,
3218 Make_Integer_Literal (Loc, Expr_Value (Lo)));
3219
a29262fd
AC
3220 Last_Opnd_High_Bound :=
3221 Convert_To (Ityp,
39ade2f9 3222 Make_Integer_Literal (Loc, Expr_Value (Hi)));
a29262fd
AC
3223 end if;
3224
3225 -- Exclude null length case unless last operand
0ac73189 3226
a29262fd 3227 if J < N and then Len = 0 then
0ac73189
AC
3228 goto Continue;
3229 end if;
3230
3231 NN := NN + 1;
3232 Operands (NN) := Opnd;
3233 Is_Fixed_Length (NN) := True;
3234 Fixed_Length (NN) := Len;
3235
39ade2f9
AC
3236 Opnd_Low_Bound (NN) :=
3237 To_Ityp
3238 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
0ac73189 3239 Set := True;
df46b832
AC
3240 end;
3241 end if;
3242 end;
3243 end if;
3244
0ac73189
AC
3245 -- All cases where the length is not known at compile time, or the
3246 -- special case of an operand which is known to be null but has a
3247 -- lower bound other than 1 or is other than a string type.
df46b832
AC
3248
3249 if not Set then
3250 NN := NN + 1;
0ac73189
AC
3251
3252 -- Capture operand bounds
3253
3254 Opnd_Low_Bound (NN) :=
3255 Make_Attribute_Reference (Loc,
3256 Prefix =>
3257 Duplicate_Subexpr (Opnd, Name_Req => True),
3258 Attribute_Name => Name_First);
3259
88a27b18
AC
3260 -- Capture last operand bounds if result could be null
3261
a29262fd 3262 if J = N and Result_May_Be_Null then
88a27b18
AC
3263 Last_Opnd_Low_Bound :=
3264 Convert_To (Ityp,
3265 Make_Attribute_Reference (Loc,
3266 Prefix =>
3267 Duplicate_Subexpr (Opnd, Name_Req => True),
3268 Attribute_Name => Name_First));
3269
a29262fd
AC
3270 Last_Opnd_High_Bound :=
3271 Convert_To (Ityp,
3272 Make_Attribute_Reference (Loc,
3273 Prefix =>
3274 Duplicate_Subexpr (Opnd, Name_Req => True),
3275 Attribute_Name => Name_Last));
3276 end if;
0ac73189
AC
3277
3278 -- Capture length of operand in entity
3279
df46b832
AC
3280 Operands (NN) := Opnd;
3281 Is_Fixed_Length (NN) := False;
3282
191fcb3a 3283 Var_Length (NN) := Make_Temporary (Loc, 'L');
df46b832 3284
d0f8d157 3285 Append_To (Actions,
df46b832
AC
3286 Make_Object_Declaration (Loc,
3287 Defining_Identifier => Var_Length (NN),
3288 Constant_Present => True,
39ade2f9 3289 Object_Definition => New_Occurrence_Of (Artyp, Loc),
df46b832
AC
3290 Expression =>
3291 Make_Attribute_Reference (Loc,
3292 Prefix =>
3293 Duplicate_Subexpr (Opnd, Name_Req => True),
d0f8d157 3294 Attribute_Name => Name_Length)));
df46b832
AC
3295 end if;
3296 end if;
3297
3298 -- Set next entry in aggregate length array
3299
3300 -- For first entry, make either integer literal for fixed length
0ac73189 3301 -- or a reference to the saved length for variable length.
df46b832
AC
3302
3303 if NN = 1 then
3304 if Is_Fixed_Length (1) then
39ade2f9 3305 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
df46b832 3306 else
e4494292 3307 Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc);
df46b832
AC
3308 end if;
3309
3310 -- If entry is fixed length and only fixed lengths so far, make
3311 -- appropriate new integer literal adding new length.
3312
3313 elsif Is_Fixed_Length (NN)
3314 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
3315 then
3316 Aggr_Length (NN) :=
3317 Make_Integer_Literal (Loc,
3318 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
3319
d0f8d157
AC
3320 -- All other cases, construct an addition node for the length and
3321 -- create an entity initialized to this length.
df46b832
AC
3322
3323 else
191fcb3a 3324 Ent := Make_Temporary (Loc, 'L');
df46b832
AC
3325
3326 if Is_Fixed_Length (NN) then
3327 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
3328 else
e4494292 3329 Clen := New_Occurrence_Of (Var_Length (NN), Loc);
df46b832
AC
3330 end if;
3331
d0f8d157 3332 Append_To (Actions,
df46b832
AC
3333 Make_Object_Declaration (Loc,
3334 Defining_Identifier => Ent,
3335 Constant_Present => True,
39ade2f9 3336 Object_Definition => New_Occurrence_Of (Artyp, Loc),
df46b832
AC
3337 Expression =>
3338 Make_Op_Add (Loc,
3339 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
d0f8d157 3340 Right_Opnd => Clen)));
df46b832 3341
76c597a1 3342 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
df46b832
AC
3343 end if;
3344
3345 <<Continue>>
3346 J := J + 1;
3347 end loop;
3348
a29262fd 3349 -- If we have only skipped null operands, return the last operand
df46b832
AC
3350
3351 if NN = 0 then
a29262fd 3352 Result := Opnd;
df46b832
AC
3353 goto Done;
3354 end if;
3355
3356 -- If we have only one non-null operand, return it and we are done.
3357 -- There is one case in which this cannot be done, and that is when
fdac1f80
AC
3358 -- the sole operand is of the element type, in which case it must be
3359 -- converted to an array, and the easiest way of doing that is to go
df46b832
AC
3360 -- through the normal general circuit.
3361
533369aa 3362 if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then
df46b832
AC
3363 Result := Operands (1);
3364 goto Done;
3365 end if;
3366
3367 -- Cases where we have a real concatenation
3368
fdac1f80
AC
3369 -- Next step is to find the low bound for the result array that we
3370 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3371
3372 -- If the ultimate ancestor of the index subtype is a constrained array
3373 -- definition, then the lower bound is that of the index subtype as
3374 -- specified by (RM 4.5.3(6)).
3375
3376 -- The right test here is to go to the root type, and then the ultimate
3377 -- ancestor is the first subtype of this root type.
3378
3379 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
0ac73189 3380 Low_Bound :=
fdac1f80
AC
3381 Make_Attribute_Reference (Loc,
3382 Prefix =>
3383 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
0ac73189 3384 Attribute_Name => Name_First);
df46b832
AC
3385
3386 -- If the first operand in the list has known length we know that
3387 -- the lower bound of the result is the lower bound of this operand.
3388
fdac1f80 3389 elsif Is_Fixed_Length (1) then
0ac73189 3390 Low_Bound := Opnd_Low_Bound (1);
df46b832
AC
3391
3392 -- OK, we don't know the lower bound, we have to build a horrible
9b16cb57 3393 -- if expression node of the form
df46b832
AC
3394
3395 -- if Cond1'Length /= 0 then
0ac73189 3396 -- Opnd1 low bound
df46b832
AC
3397 -- else
3398 -- if Opnd2'Length /= 0 then
0ac73189 3399 -- Opnd2 low bound
df46b832
AC
3400 -- else
3401 -- ...
3402
3403 -- The nesting ends either when we hit an operand whose length is known
3404 -- at compile time, or on reaching the last operand, whose low bound we
3405 -- take unconditionally whether or not it is null. It's easiest to do
3406 -- this with a recursive procedure:
3407
3408 else
3409 declare
3410 function Get_Known_Bound (J : Nat) return Node_Id;
3411 -- Returns the lower bound determined by operands J .. NN
3412
3413 ---------------------
3414 -- Get_Known_Bound --
3415 ---------------------
3416
3417 function Get_Known_Bound (J : Nat) return Node_Id is
df46b832 3418 begin
0ac73189
AC
3419 if Is_Fixed_Length (J) or else J = NN then
3420 return New_Copy (Opnd_Low_Bound (J));
70482933
RK
3421
3422 else
df46b832 3423 return
9b16cb57 3424 Make_If_Expression (Loc,
df46b832
AC
3425 Expressions => New_List (
3426
3427 Make_Op_Ne (Loc,
e4494292
RD
3428 Left_Opnd =>
3429 New_Occurrence_Of (Var_Length (J), Loc),
3430 Right_Opnd =>
3431 Make_Integer_Literal (Loc, 0)),
df46b832 3432
0ac73189 3433 New_Copy (Opnd_Low_Bound (J)),
df46b832 3434 Get_Known_Bound (J + 1)));
70482933 3435 end if;
df46b832 3436 end Get_Known_Bound;
70482933 3437
df46b832 3438 begin
191fcb3a 3439 Ent := Make_Temporary (Loc, 'L');
df46b832 3440
d0f8d157 3441 Append_To (Actions,
df46b832
AC
3442 Make_Object_Declaration (Loc,
3443 Defining_Identifier => Ent,
3444 Constant_Present => True,
0ac73189 3445 Object_Definition => New_Occurrence_Of (Ityp, Loc),
d0f8d157 3446 Expression => Get_Known_Bound (1)));
df46b832 3447
e4494292 3448 Low_Bound := New_Occurrence_Of (Ent, Loc);
df46b832
AC
3449 end;
3450 end if;
70482933 3451
76c597a1
AC
3452 -- Now we can safely compute the upper bound, normally
3453 -- Low_Bound + Length - 1.
0ac73189
AC
3454
3455 High_Bound :=
cc6f5d75
AC
3456 To_Ityp
3457 (Make_Op_Add (Loc,
3458 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3459 Right_Opnd =>
3460 Make_Op_Subtract (Loc,
3461 Left_Opnd => New_Copy (Aggr_Length (NN)),
3462 Right_Opnd => Make_Artyp_Literal (1))));
0ac73189 3463
59262ebb 3464 -- Note that calculation of the high bound may cause overflow in some
bded454f
RD
3465 -- very weird cases, so in the general case we need an overflow check on
3466 -- the high bound. We can avoid this for the common case of string types
3467 -- and other types whose index is Positive, since we chose a wider range
3468 -- for the arithmetic type.
76c597a1 3469
59262ebb
AC
3470 if Istyp /= Standard_Positive then
3471 Activate_Overflow_Check (High_Bound);
3472 end if;
76c597a1
AC
3473
3474 -- Handle the exceptional case where the result is null, in which case
a29262fd
AC
3475 -- case the bounds come from the last operand (so that we get the proper
3476 -- bounds if the last operand is super-flat).
3477
0ac73189 3478 if Result_May_Be_Null then
88a27b18 3479 Low_Bound :=
9b16cb57 3480 Make_If_Expression (Loc,
88a27b18
AC
3481 Expressions => New_List (
3482 Make_Op_Eq (Loc,
3483 Left_Opnd => New_Copy (Aggr_Length (NN)),
3484 Right_Opnd => Make_Artyp_Literal (0)),
3485 Last_Opnd_Low_Bound,
3486 Low_Bound));
3487
0ac73189 3488 High_Bound :=
9b16cb57 3489 Make_If_Expression (Loc,
0ac73189
AC
3490 Expressions => New_List (
3491 Make_Op_Eq (Loc,
3492 Left_Opnd => New_Copy (Aggr_Length (NN)),
fa969310 3493 Right_Opnd => Make_Artyp_Literal (0)),
a29262fd 3494 Last_Opnd_High_Bound,
0ac73189
AC
3495 High_Bound));
3496 end if;
3497
d0f8d157
AC
3498 -- Here is where we insert the saved up actions
3499
3500 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3501
602a7ec0
AC
3502 -- Now we construct an array object with appropriate bounds. We mark
3503 -- the target as internal to prevent useless initialization when
e526d0c7
AC
3504 -- Initialize_Scalars is enabled. Also since this is the actual result
3505 -- entity, we make sure we have debug information for the result.
70482933 3506
191fcb3a 3507 Ent := Make_Temporary (Loc, 'S');
008f6fd3 3508 Set_Is_Internal (Ent);
e526d0c7 3509 Set_Needs_Debug_Info (Ent);
70482933 3510
76c597a1 3511 -- If the bound is statically known to be out of range, we do not want
fa969310
AC
3512 -- to abort, we want a warning and a runtime constraint error. Note that
3513 -- we have arranged that the result will not be treated as a static
3514 -- constant, so we won't get an illegality during this insertion.
76c597a1 3515
df46b832
AC
3516 Insert_Action (Cnode,
3517 Make_Object_Declaration (Loc,
3518 Defining_Identifier => Ent,
df46b832
AC
3519 Object_Definition =>
3520 Make_Subtype_Indication (Loc,
fdac1f80 3521 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
df46b832
AC
3522 Constraint =>
3523 Make_Index_Or_Discriminant_Constraint (Loc,
3524 Constraints => New_List (
3525 Make_Range (Loc,
0ac73189
AC
3526 Low_Bound => Low_Bound,
3527 High_Bound => High_Bound))))),
df46b832
AC
3528 Suppress => All_Checks);
3529
d1f453b7
RD
3530 -- If the result of the concatenation appears as the initializing
3531 -- expression of an object declaration, we can just rename the
3532 -- result, rather than copying it.
3533
3534 Set_OK_To_Rename (Ent);
3535
76c597a1
AC
3536 -- Catch the static out of range case now
3537
3538 if Raises_Constraint_Error (High_Bound) then
3539 raise Concatenation_Error;
3540 end if;
3541
df46b832
AC
3542 -- Now we will generate the assignments to do the actual concatenation
3543
bded454f
RD
3544 -- There is one case in which we will not do this, namely when all the
3545 -- following conditions are met:
3546
3547 -- The result type is Standard.String
3548
3549 -- There are nine or fewer retained (non-null) operands
3550
ffec8e81 3551 -- The optimization level is -O0
bded454f
RD
3552
3553 -- The corresponding System.Concat_n.Str_Concat_n routine is
3554 -- available in the run time.
3555
3556 -- The debug flag gnatd.c is not set
3557
3558 -- If all these conditions are met then we generate a call to the
3559 -- relevant concatenation routine. The purpose of this is to avoid
3560 -- undesirable code bloat at -O0.
3561
3562 if Atyp = Standard_String
3563 and then NN in 2 .. 9
00ba7be8 3564 and then (Lib_Level_Target
62a64085 3565 or else ((Optimization_Level = 0 or else Debug_Flag_Dot_CC)
cc6f5d75 3566 and then not Debug_Flag_Dot_C))
bded454f
RD
3567 then
3568 declare
3569 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3570 (RE_Str_Concat_2,
3571 RE_Str_Concat_3,
3572 RE_Str_Concat_4,
3573 RE_Str_Concat_5,
3574 RE_Str_Concat_6,
3575 RE_Str_Concat_7,
3576 RE_Str_Concat_8,
3577 RE_Str_Concat_9);
3578
3579 begin
3580 if RTE_Available (RR (NN)) then
3581 declare
3582 Opnds : constant List_Id :=
3583 New_List (New_Occurrence_Of (Ent, Loc));
3584
3585 begin
3586 for J in 1 .. NN loop
3587 if Is_List_Member (Operands (J)) then
3588 Remove (Operands (J));
3589 end if;
3590
3591 if Base_Type (Etype (Operands (J))) = Ctyp then
3592 Append_To (Opnds,
3593 Make_Aggregate (Loc,
3594 Component_Associations => New_List (
3595 Make_Component_Association (Loc,
3596 Choices => New_List (
3597 Make_Integer_Literal (Loc, 1)),
3598 Expression => Operands (J)))));
3599
3600 else
3601 Append_To (Opnds, Operands (J));
3602 end if;
3603 end loop;
3604
3605 Insert_Action (Cnode,
3606 Make_Procedure_Call_Statement (Loc,
e4494292 3607 Name => New_Occurrence_Of (RTE (RR (NN)), Loc),
bded454f
RD
3608 Parameter_Associations => Opnds));
3609
e4494292 3610 Result := New_Occurrence_Of (Ent, Loc);
bded454f
RD
3611 goto Done;
3612 end;
3613 end if;
3614 end;
3615 end if;
3616
3617 -- Not special case so generate the assignments
3618
76c597a1
AC
3619 Known_Non_Null_Operand_Seen := False;
3620
df46b832
AC
3621 for J in 1 .. NN loop
3622 declare
3623 Lo : constant Node_Id :=
3624 Make_Op_Add (Loc,
46ff89f3 3625 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
df46b832
AC
3626 Right_Opnd => Aggr_Length (J - 1));
3627
3628 Hi : constant Node_Id :=
3629 Make_Op_Add (Loc,
46ff89f3 3630 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
df46b832
AC
3631 Right_Opnd =>
3632 Make_Op_Subtract (Loc,
3633 Left_Opnd => Aggr_Length (J),
fa969310 3634 Right_Opnd => Make_Artyp_Literal (1)));
70482933 3635
df46b832 3636 begin
fdac1f80
AC
3637 -- Singleton case, simple assignment
3638
3639 if Base_Type (Etype (Operands (J))) = Ctyp then
76c597a1 3640 Known_Non_Null_Operand_Seen := True;
df46b832
AC
3641 Insert_Action (Cnode,
3642 Make_Assignment_Statement (Loc,
3643 Name =>
3644 Make_Indexed_Component (Loc,
3645 Prefix => New_Occurrence_Of (Ent, Loc),
fdac1f80 3646 Expressions => New_List (To_Ityp (Lo))),
df46b832
AC
3647 Expression => Operands (J)),
3648 Suppress => All_Checks);
70482933 3649
76c597a1
AC
3650 -- Array case, slice assignment, skipped when argument is fixed
3651 -- length and known to be null.
fdac1f80 3652
76c597a1
AC
3653 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3654 declare
3655 Assign : Node_Id :=
3656 Make_Assignment_Statement (Loc,
3657 Name =>
3658 Make_Slice (Loc,
3659 Prefix =>
3660 New_Occurrence_Of (Ent, Loc),
3661 Discrete_Range =>
3662 Make_Range (Loc,
3663 Low_Bound => To_Ityp (Lo),
3664 High_Bound => To_Ityp (Hi))),
3665 Expression => Operands (J));
3666 begin
3667 if Is_Fixed_Length (J) then
3668 Known_Non_Null_Operand_Seen := True;
3669
3670 elsif not Known_Non_Null_Operand_Seen then
3671
3672 -- Here if operand length is not statically known and no
3673 -- operand known to be non-null has been processed yet.
3674 -- If operand length is 0, we do not need to perform the
3675 -- assignment, and we must avoid the evaluation of the
3676 -- high bound of the slice, since it may underflow if the
3677 -- low bound is Ityp'First.
3678
3679 Assign :=
3680 Make_Implicit_If_Statement (Cnode,
39ade2f9 3681 Condition =>
76c597a1 3682 Make_Op_Ne (Loc,
39ade2f9 3683 Left_Opnd =>
76c597a1
AC
3684 New_Occurrence_Of (Var_Length (J), Loc),
3685 Right_Opnd => Make_Integer_Literal (Loc, 0)),
39ade2f9 3686 Then_Statements => New_List (Assign));
76c597a1 3687 end if;
fa969310 3688
76c597a1
AC
3689 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3690 end;
df46b832
AC
3691 end if;
3692 end;
3693 end loop;
70482933 3694
0ac73189
AC
3695 -- Finally we build the result, which is a reference to the array object
3696
e4494292 3697 Result := New_Occurrence_Of (Ent, Loc);
70482933 3698
df46b832
AC
3699 <<Done>>
3700 Rewrite (Cnode, Result);
fdac1f80
AC
3701 Analyze_And_Resolve (Cnode, Atyp);
3702
3703 exception
3704 when Concatenation_Error =>
76c597a1
AC
3705
3706 -- Kill warning generated for the declaration of the static out of
3707 -- range high bound, and instead generate a Constraint_Error with
3708 -- an appropriate specific message.
3709
3710 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3711 Apply_Compile_Time_Constraint_Error
3712 (N => Cnode,
324ac540 3713 Msg => "concatenation result upper bound out of range??",
76c597a1 3714 Reason => CE_Range_Check_Failed);
fdac1f80 3715 end Expand_Concatenate;
70482933 3716
f6194278
RD
3717 ---------------------------------------------------
3718 -- Expand_Membership_Minimize_Eliminate_Overflow --
3719 ---------------------------------------------------
3720
3721 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is
3722 pragma Assert (Nkind (N) = N_In);
3723 -- Despite the name, this routine applies only to N_In, not to
3724 -- N_Not_In. The latter is always rewritten as not (X in Y).
3725
71fb4dc8
AC
3726 Result_Type : constant Entity_Id := Etype (N);
3727 -- Capture result type, may be a derived boolean type
3728
b6b5cca8
AC
3729 Loc : constant Source_Ptr := Sloc (N);
3730 Lop : constant Node_Id := Left_Opnd (N);
3731 Rop : constant Node_Id := Right_Opnd (N);
3732
3733 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3734 -- is thus tempting to capture these values, but due to the rewrites
3735 -- that occur as a result of overflow checking, these values change
3736 -- as we go along, and it is safe just to always use Etype explicitly.
f6194278
RD
3737
3738 Restype : constant Entity_Id := Etype (N);
3739 -- Save result type
3740
3741 Lo, Hi : Uint;
d8192289 3742 -- Bounds in Minimize calls, not used currently
f6194278
RD
3743
3744 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
3745 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3746
3747 begin
a7f1b24f 3748 Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False);
f6194278
RD
3749
3750 -- If right operand is a subtype name, and the subtype name has no
3751 -- predicate, then we can just replace the right operand with an
3752 -- explicit range T'First .. T'Last, and use the explicit range code.
3753
b6b5cca8
AC
3754 if Nkind (Rop) /= N_Range
3755 and then No (Predicate_Function (Etype (Rop)))
3756 then
3757 declare
3758 Rtyp : constant Entity_Id := Etype (Rop);
3759 begin
3760 Rewrite (Rop,
3761 Make_Range (Loc,
cc6f5d75 3762 Low_Bound =>
b6b5cca8
AC
3763 Make_Attribute_Reference (Loc,
3764 Attribute_Name => Name_First,
e4494292 3765 Prefix => New_Occurrence_Of (Rtyp, Loc)),
b6b5cca8
AC
3766 High_Bound =>
3767 Make_Attribute_Reference (Loc,
3768 Attribute_Name => Name_Last,
e4494292 3769 Prefix => New_Occurrence_Of (Rtyp, Loc))));
b6b5cca8
AC
3770 Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks);
3771 end;
f6194278
RD
3772 end if;
3773
3774 -- Here for the explicit range case. Note that the bounds of the range
3775 -- have not been processed for minimized or eliminated checks.
3776
3777 if Nkind (Rop) = N_Range then
a7f1b24f 3778 Minimize_Eliminate_Overflows
b6b5cca8 3779 (Low_Bound (Rop), Lo, Hi, Top_Level => False);
a7f1b24f 3780 Minimize_Eliminate_Overflows
c7e152b5 3781 (High_Bound (Rop), Lo, Hi, Top_Level => False);
f6194278
RD
3782
3783 -- We have A in B .. C, treated as A >= B and then A <= C
3784
3785 -- Bignum case
3786
b6b5cca8 3787 if Is_RTE (Etype (Lop), RE_Bignum)
f6194278
RD
3788 or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum)
3789 or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum)
3790 then
3791 declare
3792 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3793 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
71fb4dc8
AC
3794 L : constant Entity_Id :=
3795 Make_Defining_Identifier (Loc, Name_uL);
f6194278
RD
3796 Lopnd : constant Node_Id := Convert_To_Bignum (Lop);
3797 Lbound : constant Node_Id :=
3798 Convert_To_Bignum (Low_Bound (Rop));
3799 Hbound : constant Node_Id :=
3800 Convert_To_Bignum (High_Bound (Rop));
3801
71fb4dc8
AC
3802 -- Now we rewrite the membership test node to look like
3803
3804 -- do
3805 -- Bnn : Result_Type;
3806 -- declare
3807 -- M : Mark_Id := SS_Mark;
3808 -- L : Bignum := Lopnd;
3809 -- begin
3810 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3811 -- SS_Release (M);
3812 -- end;
3813 -- in
3814 -- Bnn
3815 -- end
f6194278
RD
3816
3817 begin
71fb4dc8
AC
3818 -- Insert declaration of L into declarations of bignum block
3819
f6194278
RD
3820 Insert_After
3821 (Last (Declarations (Blk)),
3822 Make_Object_Declaration (Loc,
71fb4dc8 3823 Defining_Identifier => L,
f6194278
RD
3824 Object_Definition =>
3825 New_Occurrence_Of (RTE (RE_Bignum), Loc),
3826 Expression => Lopnd));
3827
71fb4dc8
AC
3828 -- Insert assignment to Bnn into expressions of bignum block
3829
f6194278
RD
3830 Insert_Before
3831 (First (Statements (Handled_Statement_Sequence (Blk))),
3832 Make_Assignment_Statement (Loc,
3833 Name => New_Occurrence_Of (Bnn, Loc),
3834 Expression =>
3835 Make_And_Then (Loc,
cc6f5d75 3836 Left_Opnd =>
f6194278
RD
3837 Make_Function_Call (Loc,
3838 Name =>
3839 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
71fb4dc8
AC
3840 Parameter_Associations => New_List (
3841 New_Occurrence_Of (L, Loc),
3842 Lbound)),
cc6f5d75 3843
f6194278
RD
3844 Right_Opnd =>
3845 Make_Function_Call (Loc,
3846 Name =>
71fb4dc8
AC
3847 New_Occurrence_Of (RTE (RE_Big_LE), Loc),
3848 Parameter_Associations => New_List (
3849 New_Occurrence_Of (L, Loc),
3850 Hbound)))));
f6194278 3851
71fb4dc8 3852 -- Now rewrite the node
f6194278 3853
71fb4dc8
AC
3854 Rewrite (N,
3855 Make_Expression_With_Actions (Loc,
3856 Actions => New_List (
3857 Make_Object_Declaration (Loc,
3858 Defining_Identifier => Bnn,
3859 Object_Definition =>
3860 New_Occurrence_Of (Result_Type, Loc)),
3861 Blk),
3862 Expression => New_Occurrence_Of (Bnn, Loc)));
3863 Analyze_And_Resolve (N, Result_Type);
f6194278
RD
3864 return;
3865 end;
3866
3867 -- Here if no bignums around
3868
3869 else
3870 -- Case where types are all the same
3871
b6b5cca8 3872 if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop)))
f6194278 3873 and then
b6b5cca8 3874 Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop)))
f6194278
RD
3875 then
3876 null;
3877
3878 -- If types are not all the same, it means that we have rewritten
3879 -- at least one of them to be of type Long_Long_Integer, and we
3880 -- will convert the other operands to Long_Long_Integer.
3881
3882 else
3883 Convert_To_And_Rewrite (LLIB, Lop);
71fb4dc8
AC
3884 Set_Analyzed (Lop, False);
3885 Analyze_And_Resolve (Lop, LLIB);
3886
3887 -- For the right operand, avoid unnecessary recursion into
3888 -- this routine, we know that overflow is not possible.
f6194278
RD
3889
3890 Convert_To_And_Rewrite (LLIB, Low_Bound (Rop));
3891 Convert_To_And_Rewrite (LLIB, High_Bound (Rop));
3892 Set_Analyzed (Rop, False);
71fb4dc8 3893 Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check);
f6194278
RD
3894 end if;
3895
3896 -- Now the three operands are of the same signed integer type,
b6b5cca8
AC
3897 -- so we can use the normal expansion routine for membership,
3898 -- setting the flag to prevent recursion into this procedure.
f6194278
RD
3899
3900 Set_No_Minimize_Eliminate (N);
3901 Expand_N_In (N);
3902 end if;
3903
3904 -- Right operand is a subtype name and the subtype has a predicate. We
f6636994
AC
3905 -- have to make sure the predicate is checked, and for that we need to
3906 -- use the standard N_In circuitry with appropriate types.
f6194278
RD
3907
3908 else
b6b5cca8 3909 pragma Assert (Present (Predicate_Function (Etype (Rop))));
f6194278
RD
3910
3911 -- If types are "right", just call Expand_N_In preventing recursion
3912
b6b5cca8 3913 if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then
f6194278
RD
3914 Set_No_Minimize_Eliminate (N);
3915 Expand_N_In (N);
3916
3917 -- Bignum case
3918
b6b5cca8 3919 elsif Is_RTE (Etype (Lop), RE_Bignum) then
f6194278 3920
71fb4dc8 3921 -- For X in T, we want to rewrite our node as
f6194278 3922
71fb4dc8
AC
3923 -- do
3924 -- Bnn : Result_Type;
f6194278 3925
71fb4dc8
AC
3926 -- declare
3927 -- M : Mark_Id := SS_Mark;
3928 -- Lnn : Long_Long_Integer'Base
3929 -- Nnn : Bignum;
f6194278 3930
71fb4dc8
AC
3931 -- begin
3932 -- Nnn := X;
3933
3934 -- if not Bignum_In_LLI_Range (Nnn) then
3935 -- Bnn := False;
3936 -- else
3937 -- Lnn := From_Bignum (Nnn);
3938 -- Bnn :=
3939 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3940 -- and then T'Base (Lnn) in T;
3941 -- end if;
cc6f5d75
AC
3942
3943 -- SS_Release (M);
71fb4dc8
AC
3944 -- end
3945 -- in
3946 -- Bnn
3947 -- end
f6194278 3948
f6636994 3949 -- A bit gruesome, but there doesn't seem to be a simpler way
f6194278
RD
3950
3951 declare
b6b5cca8
AC
3952 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3953 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3954 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N);
3955 Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N);
71fb4dc8
AC
3956 T : constant Entity_Id := Etype (Rop);
3957 TB : constant Entity_Id := Base_Type (T);
b6b5cca8 3958 Nin : Node_Id;
f6194278
RD
3959
3960 begin
71fb4dc8 3961 -- Mark the last membership operation to prevent recursion
f6194278
RD
3962
3963 Nin :=
3964 Make_In (Loc,
f6636994
AC
3965 Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)),
3966 Right_Opnd => New_Occurrence_Of (T, Loc));
f6194278
RD
3967 Set_No_Minimize_Eliminate (Nin);
3968
3969 -- Now decorate the block
3970
3971 Insert_After
3972 (Last (Declarations (Blk)),
3973 Make_Object_Declaration (Loc,
3974 Defining_Identifier => Lnn,
3975 Object_Definition => New_Occurrence_Of (LLIB, Loc)));
3976
3977 Insert_After
3978 (Last (Declarations (Blk)),
3979 Make_Object_Declaration (Loc,
3980 Defining_Identifier => Nnn,
3981 Object_Definition =>
3982 New_Occurrence_Of (RTE (RE_Bignum), Loc)));
3983
3984 Insert_List_Before
3985 (First (Statements (Handled_Statement_Sequence (Blk))),
3986 New_List (
3987 Make_Assignment_Statement (Loc,
3988 Name => New_Occurrence_Of (Nnn, Loc),
3989 Expression => Relocate_Node (Lop)),
3990
8b1011c0 3991 Make_Implicit_If_Statement (N,
f6194278 3992 Condition =>
71fb4dc8
AC
3993 Make_Op_Not (Loc,
3994 Right_Opnd =>
3995 Make_Function_Call (Loc,
3996 Name =>
3997 New_Occurrence_Of
3998 (RTE (RE_Bignum_In_LLI_Range), Loc),
3999 Parameter_Associations => New_List (
4000 New_Occurrence_Of (Nnn, Loc)))),
f6194278
RD
4001
4002 Then_Statements => New_List (
4003 Make_Assignment_Statement (Loc,
4004 Name => New_Occurrence_Of (Bnn, Loc),
4005 Expression =>
4006 New_Occurrence_Of (Standard_False, Loc))),
4007
4008 Else_Statements => New_List (
4009 Make_Assignment_Statement (Loc,
4010 Name => New_Occurrence_Of (Lnn, Loc),
4011 Expression =>
4012 Make_Function_Call (Loc,
4013 Name =>
4014 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
4015 Parameter_Associations => New_List (
4016 New_Occurrence_Of (Nnn, Loc)))),
4017
4018 Make_Assignment_Statement (Loc,
71fb4dc8 4019 Name => New_Occurrence_Of (Bnn, Loc),
f6194278
RD
4020 Expression =>
4021 Make_And_Then (Loc,
71fb4dc8 4022 Left_Opnd =>
f6194278 4023 Make_In (Loc,
71fb4dc8 4024 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
f6194278 4025 Right_Opnd =>
71fb4dc8
AC
4026 Make_Range (Loc,
4027 Low_Bound =>
4028 Convert_To (LLIB,
4029 Make_Attribute_Reference (Loc,
4030 Attribute_Name => Name_First,
4031 Prefix =>
4032 New_Occurrence_Of (TB, Loc))),
4033
4034 High_Bound =>
4035 Convert_To (LLIB,
4036 Make_Attribute_Reference (Loc,
4037 Attribute_Name => Name_Last,
4038 Prefix =>
4039 New_Occurrence_Of (TB, Loc))))),
4040
f6194278
RD
4041 Right_Opnd => Nin))))));
4042
71fb4dc8 4043 -- Now we can do the rewrite
f6194278 4044
71fb4dc8
AC
4045 Rewrite (N,
4046 Make_Expression_With_Actions (Loc,
4047 Actions => New_List (
4048 Make_Object_Declaration (Loc,
4049 Defining_Identifier => Bnn,
4050 Object_Definition =>
4051 New_Occurrence_Of (Result_Type, Loc)),
4052 Blk),
4053 Expression => New_Occurrence_Of (Bnn, Loc)));
4054 Analyze_And_Resolve (N, Result_Type);
f6194278
RD
4055 return;
4056 end;
4057
4058 -- Not bignum case, but types don't match (this means we rewrote the
b6b5cca8 4059 -- left operand to be Long_Long_Integer).
f6194278
RD
4060
4061 else
b6b5cca8 4062 pragma Assert (Base_Type (Etype (Lop)) = LLIB);
f6194278 4063
71fb4dc8
AC
4064 -- We rewrite the membership test as (where T is the type with
4065 -- the predicate, i.e. the type of the right operand)
f6194278 4066
71fb4dc8
AC
4067 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
4068 -- and then T'Base (Lop) in T
f6194278
RD
4069
4070 declare
71fb4dc8
AC
4071 T : constant Entity_Id := Etype (Rop);
4072 TB : constant Entity_Id := Base_Type (T);
f6194278
RD
4073 Nin : Node_Id;
4074
4075 begin
4076 -- The last membership test is marked to prevent recursion
4077
4078 Nin :=
4079 Make_In (Loc,
71fb4dc8
AC
4080 Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)),
4081 Right_Opnd => New_Occurrence_Of (T, Loc));
f6194278
RD
4082 Set_No_Minimize_Eliminate (Nin);
4083
4084 -- Now do the rewrite
4085
4086 Rewrite (N,
4087 Make_And_Then (Loc,
71fb4dc8 4088 Left_Opnd =>
f6194278
RD
4089 Make_In (Loc,
4090 Left_Opnd => Lop,
4091 Right_Opnd =>
71fb4dc8
AC
4092 Make_Range (Loc,
4093 Low_Bound =>
4094 Convert_To (LLIB,
4095 Make_Attribute_Reference (Loc,
4096 Attribute_Name => Name_First,
cc6f5d75
AC
4097 Prefix =>
4098 New_Occurrence_Of (TB, Loc))),
71fb4dc8
AC
4099 High_Bound =>
4100 Convert_To (LLIB,
4101 Make_Attribute_Reference (Loc,
4102 Attribute_Name => Name_Last,
cc6f5d75
AC
4103 Prefix =>
4104 New_Occurrence_Of (TB, Loc))))),
f6194278 4105 Right_Opnd => Nin));
71fb4dc8
AC
4106 Set_Analyzed (N, False);
4107 Analyze_And_Resolve (N, Restype);
f6194278
RD
4108 end;
4109 end if;
4110 end if;
4111 end Expand_Membership_Minimize_Eliminate_Overflow;
4112
70482933
RK
4113 ------------------------
4114 -- Expand_N_Allocator --
4115 ------------------------
4116
4117 procedure Expand_N_Allocator (N : Node_Id) is
8b1011c0
AC
4118 Etyp : constant Entity_Id := Etype (Expression (N));
4119 Loc : constant Source_Ptr := Sloc (N);
4120 PtrT : constant Entity_Id := Etype (N);
70482933 4121
26bff3d9
JM
4122 procedure Rewrite_Coextension (N : Node_Id);
4123 -- Static coextensions have the same lifetime as the entity they
8fc789c8 4124 -- constrain. Such occurrences can be rewritten as aliased objects
26bff3d9 4125 -- and their unrestricted access used instead of the coextension.
0669bebe 4126
8aec446b 4127 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
507ed3fd
AC
4128 -- Given a constrained array type E, returns a node representing the
4129 -- code to compute the size in storage elements for the given type.
205c14b0 4130 -- This is done without using the attribute (which malfunctions for
507ed3fd 4131 -- large sizes ???)
8aec446b 4132
26bff3d9
JM
4133 -------------------------
4134 -- Rewrite_Coextension --
4135 -------------------------
4136
4137 procedure Rewrite_Coextension (N : Node_Id) is
e5a22243
AC
4138 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
4139 Temp_Decl : Node_Id;
26bff3d9 4140
df3e68b1 4141 begin
26bff3d9
JM
4142 -- Generate:
4143 -- Cnn : aliased Etyp;
4144
df3e68b1
HK
4145 Temp_Decl :=
4146 Make_Object_Declaration (Loc,
4147 Defining_Identifier => Temp_Id,
243cae0a
AC
4148 Aliased_Present => True,
4149 Object_Definition => New_Occurrence_Of (Etyp, Loc));
26bff3d9 4150
26bff3d9 4151 if Nkind (Expression (N)) = N_Qualified_Expression then
df3e68b1 4152 Set_Expression (Temp_Decl, Expression (Expression (N)));
0669bebe 4153 end if;
26bff3d9 4154
e5a22243 4155 Insert_Action (N, Temp_Decl);
26bff3d9
JM
4156 Rewrite (N,
4157 Make_Attribute_Reference (Loc,
243cae0a 4158 Prefix => New_Occurrence_Of (Temp_Id, Loc),
26bff3d9
JM
4159 Attribute_Name => Name_Unrestricted_Access));
4160
4161 Analyze_And_Resolve (N, PtrT);
4162 end Rewrite_Coextension;
0669bebe 4163
8aec446b
AC
4164 ------------------------------
4165 -- Size_In_Storage_Elements --
4166 ------------------------------
4167
4168 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
4169 begin
4170 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4171 -- However, the reason for the existence of this function is
4172 -- to construct a test for sizes too large, which means near the
4173 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4174 -- is that we get overflows when sizes are greater than 2**31.
4175
507ed3fd 4176 -- So what we end up doing for array types is to use the expression:
8aec446b
AC
4177
4178 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4179
46202729 4180 -- which avoids this problem. All this is a bit bogus, but it does
8aec446b
AC
4181 -- mean we catch common cases of trying to allocate arrays that
4182 -- are too large, and which in the absence of a check results in
4183 -- undetected chaos ???
4184
ce532f42
AC
4185 -- Note in particular that this is a pessimistic estimate in the
4186 -- case of packed array types, where an array element might occupy
4187 -- just a fraction of a storage element???
4188
507ed3fd
AC
4189 declare
4190 Len : Node_Id;
4191 Res : Node_Id;
8aec446b 4192
507ed3fd
AC
4193 begin
4194 for J in 1 .. Number_Dimensions (E) loop
4195 Len :=
4196 Make_Attribute_Reference (Loc,
4197 Prefix => New_Occurrence_Of (E, Loc),
4198 Attribute_Name => Name_Length,
243cae0a 4199 Expressions => New_List (Make_Integer_Literal (Loc, J)));
8aec446b 4200
507ed3fd
AC
4201 if J = 1 then
4202 Res := Len;
8aec446b 4203
507ed3fd
AC
4204 else
4205 Res :=
4206 Make_Op_Multiply (Loc,
4207 Left_Opnd => Res,
4208 Right_Opnd => Len);
4209 end if;
4210 end loop;
8aec446b 4211
8aec446b 4212 return
507ed3fd
AC
4213 Make_Op_Multiply (Loc,
4214 Left_Opnd => Len,
4215 Right_Opnd =>
4216 Make_Attribute_Reference (Loc,
4217 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
4218 Attribute_Name => Name_Max_Size_In_Storage_Elements));
4219 end;
8aec446b
AC
4220 end Size_In_Storage_Elements;
4221
8b1011c0
AC
4222 -- Local variables
4223
70861157 4224 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
8b1011c0
AC
4225 Desig : Entity_Id;
4226 Nod : Node_Id;
4227 Pool : Entity_Id;
4228 Rel_Typ : Entity_Id;
4229 Temp : Entity_Id;
4230
0669bebe
GB
4231 -- Start of processing for Expand_N_Allocator
4232
70482933
RK
4233 begin
4234 -- RM E.2.3(22). We enforce that the expected type of an allocator
4235 -- shall not be a remote access-to-class-wide-limited-private type
4236
4237 -- Why is this being done at expansion time, seems clearly wrong ???
4238
4239 Validate_Remote_Access_To_Class_Wide_Type (N);
4240
ca5af305
AC
4241 -- Processing for anonymous access-to-controlled types. These access
4242 -- types receive a special finalization master which appears in the
4243 -- declarations of the enclosing semantic unit. This expansion is done
84f4072a
JM
4244 -- now to ensure that any additional types generated by this routine or
4245 -- Expand_Allocator_Expression inherit the proper type attributes.
ca5af305 4246
84f4072a 4247 if (Ekind (PtrT) = E_Anonymous_Access_Type
533369aa 4248 or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT))))
ca5af305
AC
4249 and then Needs_Finalization (Dtyp)
4250 then
8b1011c0
AC
4251 -- Detect the allocation of an anonymous controlled object where the
4252 -- type of the context is named. For example:
4253
4254 -- procedure Proc (Ptr : Named_Access_Typ);
4255 -- Proc (new Designated_Typ);
4256
4257 -- Regardless of the anonymous-to-named access type conversion, the
4258 -- lifetime of the object must be associated with the named access
0088ba92 4259 -- type. Use the finalization-related attributes of this type.
8b1011c0
AC
4260
4261 if Nkind_In (Parent (N), N_Type_Conversion,
4262 N_Unchecked_Type_Conversion)
4263 and then Ekind_In (Etype (Parent (N)), E_Access_Subtype,
4264 E_Access_Type,
4265 E_General_Access_Type)
4266 then
4267 Rel_Typ := Etype (Parent (N));
4268 else
4269 Rel_Typ := Empty;
4270 end if;
4271
b254da66 4272 -- Anonymous access-to-controlled types allocate on the global pool.
535a8637 4273 -- Note that this is a "root type only" attribute.
ca5af305 4274
535a8637 4275 if No (Associated_Storage_Pool (PtrT)) then
8b1011c0 4276 if Present (Rel_Typ) then
7a5b62b0 4277 Set_Associated_Storage_Pool
24d4b3d5 4278 (Root_Type (PtrT), Associated_Storage_Pool (Rel_Typ));
8b1011c0 4279 else
7a5b62b0 4280 Set_Associated_Storage_Pool
24d4b3d5 4281 (Root_Type (PtrT), RTE (RE_Global_Pool_Object));
8b1011c0 4282 end if;
ca5af305
AC
4283 end if;
4284
4285 -- The finalization master must be inserted and analyzed as part of
5114f3ff 4286 -- the current semantic unit. Note that the master is updated when
24d4b3d5
AC
4287 -- analysis changes current units. Note that this is a "root type
4288 -- only" attribute.
ca5af305 4289
5114f3ff 4290 if Present (Rel_Typ) then
24d4b3d5
AC
4291 Set_Finalization_Master
4292 (Root_Type (PtrT), Finalization_Master (Rel_Typ));
5114f3ff 4293 else
24d4b3d5
AC
4294 Set_Finalization_Master
4295 (Root_Type (PtrT), Current_Anonymous_Master);
ca5af305
AC
4296 end if;
4297 end if;
4298
4299 -- Set the storage pool and find the appropriate version of Allocate to
8417f4b2
AC
4300 -- call. Do not overwrite the storage pool if it is already set, which
4301 -- can happen for build-in-place function returns (see
200b7162 4302 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
70482933 4303
200b7162
BD
4304 if No (Storage_Pool (N)) then
4305 Pool := Associated_Storage_Pool (Root_Type (PtrT));
70482933 4306
200b7162
BD
4307 if Present (Pool) then
4308 Set_Storage_Pool (N, Pool);
fbf5a39b 4309
200b7162 4310 if Is_RTE (Pool, RE_SS_Pool) then
535a8637 4311 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
fbf5a39b 4312
a8551b5f
AC
4313 -- In the case of an allocator for a simple storage pool, locate
4314 -- and save a reference to the pool type's Allocate routine.
4315
4316 elsif Present (Get_Rep_Pragma
f6205414 4317 (Etype (Pool), Name_Simple_Storage_Pool_Type))
a8551b5f
AC
4318 then
4319 declare
a8551b5f 4320 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
260359e3 4321 Alloc_Op : Entity_Id;
a8551b5f 4322 begin
260359e3 4323 Alloc_Op := Get_Name_Entity_Id (Name_Allocate);
a8551b5f
AC
4324 while Present (Alloc_Op) loop
4325 if Scope (Alloc_Op) = Scope (Pool_Type)
4326 and then Present (First_Formal (Alloc_Op))
4327 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4328 then
4329 Set_Procedure_To_Call (N, Alloc_Op);
a8551b5f 4330 exit;
260359e3
AC
4331 else
4332 Alloc_Op := Homonym (Alloc_Op);
a8551b5f 4333 end if;
a8551b5f
AC
4334 end loop;
4335 end;
4336
200b7162
BD
4337 elsif Is_Class_Wide_Type (Etype (Pool)) then
4338 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
4339
4340 else
4341 Set_Procedure_To_Call (N,
4342 Find_Prim_Op (Etype (Pool), Name_Allocate));
4343 end if;
70482933
RK
4344 end if;
4345 end if;
4346
685094bf
RD
4347 -- Under certain circumstances we can replace an allocator by an access
4348 -- to statically allocated storage. The conditions, as noted in AARM
4349 -- 3.10 (10c) are as follows:
70482933
RK
4350
4351 -- Size and initial value is known at compile time
4352 -- Access type is access-to-constant
4353
fbf5a39b
AC
4354 -- The allocator is not part of a constraint on a record component,
4355 -- because in that case the inserted actions are delayed until the
4356 -- record declaration is fully analyzed, which is too late for the
4357 -- analysis of the rewritten allocator.
4358
70482933
RK
4359 if Is_Access_Constant (PtrT)
4360 and then Nkind (Expression (N)) = N_Qualified_Expression
4361 and then Compile_Time_Known_Value (Expression (Expression (N)))
243cae0a
AC
4362 and then Size_Known_At_Compile_Time
4363 (Etype (Expression (Expression (N))))
fbf5a39b 4364 and then not Is_Record_Type (Current_Scope)
70482933
RK
4365 then
4366 -- Here we can do the optimization. For the allocator
4367
4368 -- new x'(y)
4369
4370 -- We insert an object declaration
4371
4372 -- Tnn : aliased x := y;
4373
685094bf
RD
4374 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4375 -- marked as requiring static allocation.
70482933 4376
df3e68b1 4377 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
70482933
RK
4378 Desig := Subtype_Mark (Expression (N));
4379
4380 -- If context is constrained, use constrained subtype directly,
8fc789c8 4381 -- so that the constant is not labelled as having a nominally
70482933
RK
4382 -- unconstrained subtype.
4383
0da2c8ac
AC
4384 if Entity (Desig) = Base_Type (Dtyp) then
4385 Desig := New_Occurrence_Of (Dtyp, Loc);
70482933
RK
4386 end if;
4387
4388 Insert_Action (N,
4389 Make_Object_Declaration (Loc,
4390 Defining_Identifier => Temp,
4391 Aliased_Present => True,
4392 Constant_Present => Is_Access_Constant (PtrT),
4393 Object_Definition => Desig,
4394 Expression => Expression (Expression (N))));
4395
4396 Rewrite (N,
4397 Make_Attribute_Reference (Loc,
243cae0a 4398 Prefix => New_Occurrence_Of (Temp, Loc),
70482933
RK
4399 Attribute_Name => Name_Unrestricted_Access));
4400
4401 Analyze_And_Resolve (N, PtrT);
4402
685094bf 4403 -- We set the variable as statically allocated, since we don't want
a90bd866 4404 -- it going on the stack of the current procedure.
70482933
RK
4405
4406 Set_Is_Statically_Allocated (Temp);
4407 return;
4408 end if;
4409
0669bebe
GB
4410 -- Same if the allocator is an access discriminant for a local object:
4411 -- instead of an allocator we create a local value and constrain the
308e6f3a 4412 -- enclosing object with the corresponding access attribute.
0669bebe 4413
26bff3d9
JM
4414 if Is_Static_Coextension (N) then
4415 Rewrite_Coextension (N);
0669bebe
GB
4416 return;
4417 end if;
4418
8aec446b
AC
4419 -- Check for size too large, we do this because the back end misses
4420 -- proper checks here and can generate rubbish allocation calls when
4421 -- we are near the limit. We only do this for the 32-bit address case
4422 -- since that is from a practical point of view where we see a problem.
4423
4424 if System_Address_Size = 32
4425 and then not Storage_Checks_Suppressed (PtrT)
4426 and then not Storage_Checks_Suppressed (Dtyp)
4427 and then not Storage_Checks_Suppressed (Etyp)
4428 then
4429 -- The check we want to generate should look like
4430
4431 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4432 -- raise Storage_Error;
4433 -- end if;
4434
308e6f3a 4435 -- where 3.5 gigabytes is a constant large enough to accommodate any
507ed3fd
AC
4436 -- reasonable request for. But we can't do it this way because at
4437 -- least at the moment we don't compute this attribute right, and
4438 -- can silently give wrong results when the result gets large. Since
4439 -- this is all about large results, that's bad, so instead we only
205c14b0 4440 -- apply the check for constrained arrays, and manually compute the
507ed3fd 4441 -- value of the attribute ???
8aec446b 4442
507ed3fd
AC
4443 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
4444 Insert_Action (N,
4445 Make_Raise_Storage_Error (Loc,
4446 Condition =>
4447 Make_Op_Gt (Loc,
4448 Left_Opnd => Size_In_Storage_Elements (Etyp),
4449 Right_Opnd =>
243cae0a 4450 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
507ed3fd
AC
4451 Reason => SE_Object_Too_Large));
4452 end if;
8aec446b
AC
4453 end if;
4454
b3b26ace
AC
4455 -- If no storage pool has been specified and we have the restriction
4456 -- No_Standard_Allocators_After_Elaboration is present, then generate
4457 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4458
4459 if Nkind (N) = N_Allocator
4460 and then No (Storage_Pool (N))
4461 and then Restriction_Active (No_Standard_Allocators_After_Elaboration)
4462 then
4463 Insert_Action (N,
4464 Make_Procedure_Call_Statement (Loc,
4465 Name =>
4466 New_Occurrence_Of (RTE (RE_Check_Standard_Allocator), Loc)));
4467 end if;
4468
0da2c8ac 4469 -- Handle case of qualified expression (other than optimization above)
cac5a801
AC
4470 -- First apply constraint checks, because the bounds or discriminants
4471 -- in the aggregate might not match the subtype mark in the allocator.
0da2c8ac 4472
70482933 4473 if Nkind (Expression (N)) = N_Qualified_Expression then
cac5a801
AC
4474 Apply_Constraint_Check
4475 (Expression (Expression (N)), Etype (Expression (N)));
4476
fbf5a39b 4477 Expand_Allocator_Expression (N);
26bff3d9
JM
4478 return;
4479 end if;
fbf5a39b 4480
26bff3d9
JM
4481 -- If the allocator is for a type which requires initialization, and
4482 -- there is no initial value (i.e. operand is a subtype indication
685094bf
RD
4483 -- rather than a qualified expression), then we must generate a call to
4484 -- the initialization routine using an expressions action node:
70482933 4485
26bff3d9 4486 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
70482933 4487
26bff3d9
JM
4488 -- Here ptr_T is the pointer type for the allocator, and T is the
4489 -- subtype of the allocator. A special case arises if the designated
4490 -- type of the access type is a task or contains tasks. In this case
4491 -- the call to Init (Temp.all ...) is replaced by code that ensures
4492 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
6be44a9a 4493 -- for details). In addition, if the type T is a task type, then the
26bff3d9 4494 -- first argument to Init must be converted to the task record type.
70482933 4495
26bff3d9 4496 declare
df3e68b1
HK
4497 T : constant Entity_Id := Entity (Expression (N));
4498 Args : List_Id;
4499 Decls : List_Id;
4500 Decl : Node_Id;
4501 Discr : Elmt_Id;
4502 Init : Entity_Id;
4503 Init_Arg1 : Node_Id;
4504 Temp_Decl : Node_Id;
4505 Temp_Type : Entity_Id;
70482933 4506
26bff3d9
JM
4507 begin
4508 if No_Initialization (N) then
df3e68b1
HK
4509
4510 -- Even though this might be a simple allocation, create a custom
535a8637 4511 -- Allocate if the context requires it.
df3e68b1 4512
535a8637 4513 if Present (Finalization_Master (PtrT)) then
df3e68b1 4514 Build_Allocate_Deallocate_Proc
ca5af305 4515 (N => N,
df3e68b1
HK
4516 Is_Allocate => True);
4517 end if;
70482933 4518
26bff3d9 4519 -- Case of no initialization procedure present
70482933 4520
26bff3d9 4521 elsif not Has_Non_Null_Base_Init_Proc (T) then
70482933 4522
26bff3d9 4523 -- Case of simple initialization required
70482933 4524
26bff3d9 4525 if Needs_Simple_Initialization (T) then
b4592168 4526 Check_Restriction (No_Default_Initialization, N);
26bff3d9
JM
4527 Rewrite (Expression (N),
4528 Make_Qualified_Expression (Loc,
4529 Subtype_Mark => New_Occurrence_Of (T, Loc),
b4592168 4530 Expression => Get_Simple_Init_Val (T, N)));
70482933 4531
26bff3d9
JM
4532 Analyze_And_Resolve (Expression (Expression (N)), T);
4533 Analyze_And_Resolve (Expression (N), T);
4534 Set_Paren_Count (Expression (Expression (N)), 1);
4535 Expand_N_Allocator (N);
70482933 4536
26bff3d9 4537 -- No initialization required
70482933
RK
4538
4539 else
26bff3d9
JM
4540 null;
4541 end if;
70482933 4542
26bff3d9 4543 -- Case of initialization procedure present, must be called
70482933 4544
26bff3d9 4545 else
b4592168 4546 Check_Restriction (No_Default_Initialization, N);
70482933 4547
b4592168
GD
4548 if not Restriction_Active (No_Default_Initialization) then
4549 Init := Base_Init_Proc (T);
4550 Nod := N;
191fcb3a 4551 Temp := Make_Temporary (Loc, 'P');
70482933 4552
b4592168 4553 -- Construct argument list for the initialization routine call
70482933 4554
df3e68b1 4555 Init_Arg1 :=
b4592168 4556 Make_Explicit_Dereference (Loc,
df3e68b1 4557 Prefix =>
e4494292 4558 New_Occurrence_Of (Temp, Loc));
df3e68b1
HK
4559
4560 Set_Assignment_OK (Init_Arg1);
b4592168 4561 Temp_Type := PtrT;
26bff3d9 4562
b4592168
GD
4563 -- The initialization procedure expects a specific type. if the
4564 -- context is access to class wide, indicate that the object
4565 -- being allocated has the right specific type.
70482933 4566
b4592168 4567 if Is_Class_Wide_Type (Dtyp) then
df3e68b1 4568 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
b4592168 4569 end if;
70482933 4570
b4592168
GD
4571 -- If designated type is a concurrent type or if it is private
4572 -- type whose definition is a concurrent type, the first
4573 -- argument in the Init routine has to be unchecked conversion
4574 -- to the corresponding record type. If the designated type is
243cae0a 4575 -- a derived type, also convert the argument to its root type.
20b5d666 4576
b4592168 4577 if Is_Concurrent_Type (T) then
df3e68b1
HK
4578 Init_Arg1 :=
4579 Unchecked_Convert_To (
4580 Corresponding_Record_Type (T), Init_Arg1);
70482933 4581
b4592168
GD
4582 elsif Is_Private_Type (T)
4583 and then Present (Full_View (T))
4584 and then Is_Concurrent_Type (Full_View (T))
4585 then
df3e68b1 4586 Init_Arg1 :=
b4592168 4587 Unchecked_Convert_To
df3e68b1 4588 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
70482933 4589
b4592168
GD
4590 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
4591 declare
4592 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
df3e68b1 4593
b4592168 4594 begin
df3e68b1
HK
4595 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
4596 Set_Etype (Init_Arg1, Ftyp);
b4592168
GD
4597 end;
4598 end if;
70482933 4599
df3e68b1 4600 Args := New_List (Init_Arg1);
70482933 4601
b4592168
GD
4602 -- For the task case, pass the Master_Id of the access type as
4603 -- the value of the _Master parameter, and _Chain as the value
4604 -- of the _Chain parameter (_Chain will be defined as part of
4605 -- the generated code for the allocator).
70482933 4606
b4592168
GD
4607 -- In Ada 2005, the context may be a function that returns an
4608 -- anonymous access type. In that case the Master_Id has been
4609 -- created when expanding the function declaration.
70482933 4610
b4592168
GD
4611 if Has_Task (T) then
4612 if No (Master_Id (Base_Type (PtrT))) then
70482933 4613
b4592168
GD
4614 -- The designated type was an incomplete type, and the
4615 -- access type did not get expanded. Salvage it now.
70482933 4616
b941ae65 4617 if not Restriction_Active (No_Task_Hierarchy) then
3d67b239
AC
4618 if Present (Parent (Base_Type (PtrT))) then
4619 Expand_N_Full_Type_Declaration
4620 (Parent (Base_Type (PtrT)));
4621
0d5fbf52
AC
4622 -- The only other possibility is an itype. For this
4623 -- case, the master must exist in the context. This is
4624 -- the case when the allocator initializes an access
4625 -- component in an init-proc.
3d67b239 4626
0d5fbf52 4627 else
3d67b239
AC
4628 pragma Assert (Is_Itype (PtrT));
4629 Build_Master_Renaming (PtrT, N);
4630 end if;
b941ae65 4631 end if;
b4592168 4632 end if;
70482933 4633
b4592168
GD
4634 -- If the context of the allocator is a declaration or an
4635 -- assignment, we can generate a meaningful image for it,
4636 -- even though subsequent assignments might remove the
4637 -- connection between task and entity. We build this image
4638 -- when the left-hand side is a simple variable, a simple
4639 -- indexed assignment or a simple selected component.
4640
4641 if Nkind (Parent (N)) = N_Assignment_Statement then
4642 declare
4643 Nam : constant Node_Id := Name (Parent (N));
4644
4645 begin
4646 if Is_Entity_Name (Nam) then
4647 Decls :=
4648 Build_Task_Image_Decls
4649 (Loc,
4650 New_Occurrence_Of
4651 (Entity (Nam), Sloc (Nam)), T);
4652
243cae0a
AC
4653 elsif Nkind_In (Nam, N_Indexed_Component,
4654 N_Selected_Component)
b4592168
GD
4655 and then Is_Entity_Name (Prefix (Nam))
4656 then
4657 Decls :=
4658 Build_Task_Image_Decls
4659 (Loc, Nam, Etype (Prefix (Nam)));
4660 else
4661 Decls := Build_Task_Image_Decls (Loc, T, T);
4662 end if;
4663 end;
70482933 4664
b4592168
GD
4665 elsif Nkind (Parent (N)) = N_Object_Declaration then
4666 Decls :=
4667 Build_Task_Image_Decls
4668 (Loc, Defining_Identifier (Parent (N)), T);
70482933 4669
b4592168
GD
4670 else
4671 Decls := Build_Task_Image_Decls (Loc, T, T);
4672 end if;
26bff3d9 4673
87dc09cb 4674 if Restriction_Active (No_Task_Hierarchy) then
3c1ecd7e
AC
4675 Append_To (Args,
4676 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
87dc09cb
AC
4677 else
4678 Append_To (Args,
e4494292 4679 New_Occurrence_Of
87dc09cb
AC
4680 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
4681 end if;
4682
b4592168 4683 Append_To (Args, Make_Identifier (Loc, Name_uChain));
26bff3d9 4684
b4592168
GD
4685 Decl := Last (Decls);
4686 Append_To (Args,
4687 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
26bff3d9 4688
87dc09cb 4689 -- Has_Task is false, Decls not used
26bff3d9 4690
b4592168
GD
4691 else
4692 Decls := No_List;
26bff3d9
JM
4693 end if;
4694
b4592168
GD
4695 -- Add discriminants if discriminated type
4696
4697 declare
4698 Dis : Boolean := False;
4699 Typ : Entity_Id;
4700
4701 begin
4702 if Has_Discriminants (T) then
4703 Dis := True;
4704 Typ := T;
4705
4706 elsif Is_Private_Type (T)
4707 and then Present (Full_View (T))
4708 and then Has_Discriminants (Full_View (T))
20b5d666 4709 then
b4592168
GD
4710 Dis := True;
4711 Typ := Full_View (T);
20b5d666 4712 end if;
70482933 4713
b4592168 4714 if Dis then
26bff3d9 4715
b4592168 4716 -- If the allocated object will be constrained by the
685094bf
RD
4717 -- default values for discriminants, then build a subtype
4718 -- with those defaults, and change the allocated subtype
4719 -- to that. Note that this happens in fewer cases in Ada
4720 -- 2005 (AI-363).
26bff3d9 4721
b4592168
GD
4722 if not Is_Constrained (Typ)
4723 and then Present (Discriminant_Default_Value
df3e68b1 4724 (First_Discriminant (Typ)))
0791fbe9 4725 and then (Ada_Version < Ada_2005
cc96a1b8 4726 or else not
0fbcb11c
ES
4727 Object_Type_Has_Constrained_Partial_View
4728 (Typ, Current_Scope))
20b5d666 4729 then
b4592168 4730 Typ := Build_Default_Subtype (Typ, N);
e4494292 4731 Set_Expression (N, New_Occurrence_Of (Typ, Loc));
20b5d666
JM
4732 end if;
4733
b4592168
GD
4734 Discr := First_Elmt (Discriminant_Constraint (Typ));
4735 while Present (Discr) loop
4736 Nod := Node (Discr);
4737 Append (New_Copy_Tree (Node (Discr)), Args);
20b5d666 4738
b4592168
GD
4739 -- AI-416: when the discriminant constraint is an
4740 -- anonymous access type make sure an accessibility
4741 -- check is inserted if necessary (3.10.2(22.q/2))
20b5d666 4742
0791fbe9 4743 if Ada_Version >= Ada_2005
b4592168
GD
4744 and then
4745 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
4746 then
e84e11ba
GD
4747 Apply_Accessibility_Check
4748 (Nod, Typ, Insert_Node => Nod);
b4592168 4749 end if;
20b5d666 4750
b4592168
GD
4751 Next_Elmt (Discr);
4752 end loop;
4753 end if;
4754 end;
70482933 4755
4b985e20 4756 -- We set the allocator as analyzed so that when we analyze
9b16cb57
RD
4757 -- the if expression node, we do not get an unwanted recursive
4758 -- expansion of the allocator expression.
70482933 4759
b4592168
GD
4760 Set_Analyzed (N, True);
4761 Nod := Relocate_Node (N);
70482933 4762
b4592168 4763 -- Here is the transformation:
ca5af305
AC
4764 -- input: new Ctrl_Typ
4765 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4766 -- Ctrl_TypIP (Temp.all, ...);
4767 -- [Deep_]Initialize (Temp.all);
70482933 4768
ca5af305
AC
4769 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4770 -- is the subtype of the allocator.
70482933 4771
b4592168
GD
4772 Temp_Decl :=
4773 Make_Object_Declaration (Loc,
4774 Defining_Identifier => Temp,
4775 Constant_Present => True,
e4494292 4776 Object_Definition => New_Occurrence_Of (Temp_Type, Loc),
b4592168 4777 Expression => Nod);
70482933 4778
b4592168
GD
4779 Set_Assignment_OK (Temp_Decl);
4780 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
70482933 4781
ca5af305 4782 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
df3e68b1 4783
b4592168
GD
4784 -- If the designated type is a task type or contains tasks,
4785 -- create block to activate created tasks, and insert
4786 -- declaration for Task_Image variable ahead of call.
70482933 4787
b4592168
GD
4788 if Has_Task (T) then
4789 declare
4790 L : constant List_Id := New_List;
4791 Blk : Node_Id;
4792 begin
4793 Build_Task_Allocate_Block (L, Nod, Args);
4794 Blk := Last (L);
4795 Insert_List_Before (First (Declarations (Blk)), Decls);
4796 Insert_Actions (N, L);
4797 end;
70482933 4798
b4592168
GD
4799 else
4800 Insert_Action (N,
4801 Make_Procedure_Call_Statement (Loc,
e4494292 4802 Name => New_Occurrence_Of (Init, Loc),
b4592168
GD
4803 Parameter_Associations => Args));
4804 end if;
70482933 4805
048e5cef 4806 if Needs_Finalization (T) then
70482933 4807
df3e68b1
HK
4808 -- Generate:
4809 -- [Deep_]Initialize (Init_Arg1);
70482933 4810
df3e68b1 4811 Insert_Action (N,
243cae0a
AC
4812 Make_Init_Call
4813 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4814 Typ => T));
70482933
RK
4815 end if;
4816
e4494292 4817 Rewrite (N, New_Occurrence_Of (Temp, Loc));
b4592168
GD
4818 Analyze_And_Resolve (N, PtrT);
4819 end if;
26bff3d9
JM
4820 end if;
4821 end;
f82944b7 4822
26bff3d9
JM
4823 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4824 -- object that has been rewritten as a reference, we displace "this"
4825 -- to reference properly its secondary dispatch table.
4826
533369aa 4827 if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then
26bff3d9 4828 Displace_Allocator_Pointer (N);
f82944b7
JM
4829 end if;
4830
fbf5a39b
AC
4831 exception
4832 when RE_Not_Available =>
4833 return;
70482933
RK
4834 end Expand_N_Allocator;
4835
4836 -----------------------
4837 -- Expand_N_And_Then --
4838 -----------------------
4839
5875f8d6
AC
4840 procedure Expand_N_And_Then (N : Node_Id)
4841 renames Expand_Short_Circuit_Operator;
70482933 4842
19d846a0
RD
4843 ------------------------------
4844 -- Expand_N_Case_Expression --
4845 ------------------------------
4846
4847 procedure Expand_N_Case_Expression (N : Node_Id) is
4848 Loc : constant Source_Ptr := Sloc (N);
4849 Typ : constant Entity_Id := Etype (N);
4850 Cstmt : Node_Id;
27a8f150 4851 Decl : Node_Id;
19d846a0
RD
4852 Tnn : Entity_Id;
4853 Pnn : Entity_Id;
4854 Actions : List_Id;
4855 Ttyp : Entity_Id;
4856 Alt : Node_Id;
4857 Fexp : Node_Id;
4858
4859 begin
b6b5cca8
AC
4860 -- Check for MINIMIZED/ELIMINATED overflow mode
4861
4862 if Minimized_Eliminated_Overflow_Check (N) then
4b1c4f20
RD
4863 Apply_Arithmetic_Overflow_Check (N);
4864 return;
4865 end if;
4866
ff1f1705
AC
4867 -- If the case expression is a predicate specification, do not
4868 -- expand, because it will be converted to the proper predicate
4869 -- form when building the predicate function.
4870
4871 if Ekind_In (Current_Scope, E_Function, E_Procedure)
4872 and then Is_Predicate_Function (Current_Scope)
4873 then
4874 return;
4875 end if;
4876
19d846a0
RD
4877 -- We expand
4878
4879 -- case X is when A => AX, when B => BX ...
4880
4881 -- to
4882
4883 -- do
4884 -- Tnn : typ;
4885 -- case X is
4886 -- when A =>
4887 -- Tnn := AX;
4888 -- when B =>
4889 -- Tnn := BX;
4890 -- ...
4891 -- end case;
4892 -- in Tnn end;
4893
4894 -- However, this expansion is wrong for limited types, and also
4895 -- wrong for unconstrained types (since the bounds may not be the
4896 -- same in all branches). Furthermore it involves an extra copy
4897 -- for large objects. So we take care of this by using the following
2492305b 4898 -- modified expansion for non-elementary types:
19d846a0
RD
4899
4900 -- do
4901 -- type Pnn is access all typ;
4902 -- Tnn : Pnn;
4903 -- case X is
4904 -- when A =>
4905 -- T := AX'Unrestricted_Access;
4906 -- when B =>
4907 -- T := BX'Unrestricted_Access;
4908 -- ...
4909 -- end case;
4910 -- in Tnn.all end;
4911
4912 Cstmt :=
4913 Make_Case_Statement (Loc,
4914 Expression => Expression (N),
4915 Alternatives => New_List);
4916
414c6563
AC
4917 -- Preserve the original context for which the case statement is being
4918 -- generated. This is needed by the finalization machinery to prevent
4919 -- the premature finalization of controlled objects found within the
4920 -- case statement.
4921
4922 Set_From_Conditional_Expression (Cstmt);
4923
19d846a0
RD
4924 Actions := New_List;
4925
4926 -- Scalar case
4927
2492305b 4928 if Is_Elementary_Type (Typ) then
19d846a0
RD
4929 Ttyp := Typ;
4930
4931 else
4932 Pnn := Make_Temporary (Loc, 'P');
4933 Append_To (Actions,
4934 Make_Full_Type_Declaration (Loc,
4935 Defining_Identifier => Pnn,
11d59a86 4936 Type_Definition =>
19d846a0 4937 Make_Access_To_Object_Definition (Loc,
11d59a86 4938 All_Present => True,
e4494292 4939 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
19d846a0
RD
4940 Ttyp := Pnn;
4941 end if;
4942
4943 Tnn := Make_Temporary (Loc, 'T');
27a8f150
AC
4944
4945 -- Create declaration for target of expression, and indicate that it
4946 -- does not require initialization.
4947
11d59a86
AC
4948 Decl :=
4949 Make_Object_Declaration (Loc,
19d846a0 4950 Defining_Identifier => Tnn,
27a8f150
AC
4951 Object_Definition => New_Occurrence_Of (Ttyp, Loc));
4952 Set_No_Initialization (Decl);
4953 Append_To (Actions, Decl);
19d846a0
RD
4954
4955 -- Now process the alternatives
4956
4957 Alt := First (Alternatives (N));
4958 while Present (Alt) loop
4959 declare
eaed0c37
AC
4960 Aexp : Node_Id := Expression (Alt);
4961 Aloc : constant Source_Ptr := Sloc (Aexp);
4962 Stats : List_Id;
19d846a0
RD
4963
4964 begin
eaed0c37
AC
4965 -- As described above, take Unrestricted_Access for case of non-
4966 -- scalar types, to avoid big copies, and special cases.
05dbd302 4967
2492305b 4968 if not Is_Elementary_Type (Typ) then
19d846a0
RD
4969 Aexp :=
4970 Make_Attribute_Reference (Aloc,
4971 Prefix => Relocate_Node (Aexp),
4972 Attribute_Name => Name_Unrestricted_Access);
4973 end if;
4974
eaed0c37
AC
4975 Stats := New_List (
4976 Make_Assignment_Statement (Aloc,
4977 Name => New_Occurrence_Of (Tnn, Loc),
4978 Expression => Aexp));
4979
4980 -- Propagate declarations inserted in the node by Insert_Actions
4981 -- (for example, temporaries generated to remove side effects).
4982 -- These actions must remain attached to the alternative, given
4983 -- that they are generated by the corresponding expression.
4984
4985 if Present (Sinfo.Actions (Alt)) then
4986 Prepend_List (Sinfo.Actions (Alt), Stats);
4987 end if;
4988
19d846a0
RD
4989 Append_To
4990 (Alternatives (Cstmt),
4991 Make_Case_Statement_Alternative (Sloc (Alt),
4992 Discrete_Choices => Discrete_Choices (Alt),
eaed0c37 4993 Statements => Stats));
19d846a0
RD
4994 end;
4995
4996 Next (Alt);
4997 end loop;
4998
4999 Append_To (Actions, Cstmt);
5000
5001 -- Construct and return final expression with actions
5002
2492305b 5003 if Is_Elementary_Type (Typ) then
19d846a0
RD
5004 Fexp := New_Occurrence_Of (Tnn, Loc);
5005 else
5006 Fexp :=
5007 Make_Explicit_Dereference (Loc,
5008 Prefix => New_Occurrence_Of (Tnn, Loc));
5009 end if;
5010
5011 Rewrite (N,
5012 Make_Expression_With_Actions (Loc,
5013 Expression => Fexp,
5014 Actions => Actions));
5015
5016 Analyze_And_Resolve (N, Typ);
5017 end Expand_N_Case_Expression;
5018
9b16cb57
RD
5019 -----------------------------------
5020 -- Expand_N_Explicit_Dereference --
5021 -----------------------------------
5022
5023 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
5024 begin
5025 -- Insert explicit dereference call for the checked storage pool case
5026
5027 Insert_Dereference_Action (Prefix (N));
5028
5029 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5030 -- we set the atomic sync flag.
5031
5032 if Is_Atomic (Etype (N))
5033 and then not Atomic_Synchronization_Disabled (Etype (N))
5034 then
5035 Activate_Atomic_Synchronization (N);
5036 end if;
5037 end Expand_N_Explicit_Dereference;
5038
5039 --------------------------------------
5040 -- Expand_N_Expression_With_Actions --
5041 --------------------------------------
5042
5043 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
e3d9f448
AC
5044 Acts : constant List_Id := Actions (N);
5045
5046 procedure Force_Boolean_Evaluation (Expr : Node_Id);
5047 -- Force the evaluation of Boolean expression Expr
5048
4c7e0990 5049 function Process_Action (Act : Node_Id) return Traverse_Result;
b2c28399
AC
5050 -- Inspect and process a single action of an expression_with_actions for
5051 -- transient controlled objects. If such objects are found, the routine
5052 -- generates code to clean them up when the context of the expression is
5053 -- evaluated or elaborated.
9b16cb57 5054
e3d9f448
AC
5055 ------------------------------
5056 -- Force_Boolean_Evaluation --
5057 ------------------------------
5058
5059 procedure Force_Boolean_Evaluation (Expr : Node_Id) is
5060 Loc : constant Source_Ptr := Sloc (N);
5061 Flag_Decl : Node_Id;
5062 Flag_Id : Entity_Id;
5063
5064 begin
5065 -- Relocate the expression to the actions list by capturing its value
5066 -- in a Boolean flag. Generate:
5067 -- Flag : constant Boolean := Expr;
5068
5069 Flag_Id := Make_Temporary (Loc, 'F');
5070
5071 Flag_Decl :=
5072 Make_Object_Declaration (Loc,
5073 Defining_Identifier => Flag_Id,
5074 Constant_Present => True,
5075 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
5076 Expression => Relocate_Node (Expr));
5077
5078 Append (Flag_Decl, Acts);
5079 Analyze (Flag_Decl);
5080
5081 -- Replace the expression with a reference to the flag
5082
5083 Rewrite (Expression (N), New_Occurrence_Of (Flag_Id, Loc));
5084 Analyze (Expression (N));
5085 end Force_Boolean_Evaluation;
5086
4c7e0990
AC
5087 --------------------
5088 -- Process_Action --
5089 --------------------
5090
5091 function Process_Action (Act : Node_Id) return Traverse_Result is
4c7e0990
AC
5092 begin
5093 if Nkind (Act) = N_Object_Declaration
5094 and then Is_Finalizable_Transient (Act, N)
5095 then
b2c28399
AC
5096 Process_Transient_Object (Act, N);
5097 return Abandon;
9b16cb57 5098
4c7e0990
AC
5099 -- Avoid processing temporary function results multiple times when
5100 -- dealing with nested expression_with_actions.
9b16cb57 5101
4c7e0990
AC
5102 elsif Nkind (Act) = N_Expression_With_Actions then
5103 return Abandon;
5104
b2c28399
AC
5105 -- Do not process temporary function results in loops. This is done
5106 -- by Expand_N_Loop_Statement and Build_Finalizer.
4c7e0990
AC
5107
5108 elsif Nkind (Act) = N_Loop_Statement then
5109 return Abandon;
9b16cb57
RD
5110 end if;
5111
4c7e0990
AC
5112 return OK;
5113 end Process_Action;
9b16cb57 5114
4c7e0990 5115 procedure Process_Single_Action is new Traverse_Proc (Process_Action);
9b16cb57
RD
5116
5117 -- Local variables
5118
e3d9f448 5119 Act : Node_Id;
9b16cb57
RD
5120
5121 -- Start of processing for Expand_N_Expression_With_Actions
5122
5123 begin
4b17187f
AC
5124 -- Do not evaluate the expression when it denotes an entity because the
5125 -- expression_with_actions node will be replaced by the reference.
5126
e3d9f448 5127 if Is_Entity_Name (Expression (N)) then
4b17187f
AC
5128 null;
5129
5130 -- Do not evaluate the expression when there are no actions because the
5131 -- expression_with_actions node will be replaced by the expression.
5132
5133 elsif No (Acts) or else Is_Empty_List (Acts) then
5134 null;
5135
5136 -- Force the evaluation of the expression by capturing its value in a
5137 -- temporary. This ensures that aliases of transient controlled objects
5138 -- do not leak to the expression of the expression_with_actions node:
5139
5140 -- do
5141 -- Trans_Id : Ctrl_Typ : ...;
5142 -- Alias : ... := Trans_Id;
5143 -- in ... Alias ... end;
5144
5145 -- In the example above, Trans_Id cannot be finalized at the end of the
5146 -- actions list because this may affect the alias and the final value of
5147 -- the expression_with_actions. Forcing the evaluation encapsulates the
5148 -- reference to the Alias within the actions list:
5149
5150 -- do
5151 -- Trans_Id : Ctrl_Typ : ...;
5152 -- Alias : ... := Trans_Id;
5153 -- Val : constant Boolean := ... Alias ...;
5154 -- <finalize Trans_Id>
5155 -- in Val end;
e0f63680 5156
e3d9f448
AC
5157 -- Once this transformation is performed, it is safe to finalize the
5158 -- transient controlled object at the end of the actions list.
5159
5160 -- Note that Force_Evaluation does not remove side effects in operators
5161 -- because it assumes that all operands are evaluated and side effect
5162 -- free. This is not the case when an operand depends implicitly on the
5163 -- transient controlled object through the use of access types.
5164
5165 elsif Is_Boolean_Type (Etype (Expression (N))) then
5166 Force_Boolean_Evaluation (Expression (N));
5167
5168 -- The expression of an expression_with_actions node may not necessarely
5169 -- be Boolean when the node appears in an if expression. In this case do
5170 -- the usual forced evaluation to encapsulate potential aliasing.
4b17187f
AC
5171
5172 else
e3d9f448 5173 Force_Evaluation (Expression (N));
4b17187f
AC
5174 end if;
5175
5176 -- Process all transient controlled objects found within the actions of
5177 -- the EWA node.
5178
5179 Act := First (Acts);
e0f63680
AC
5180 while Present (Act) loop
5181 Process_Single_Action (Act);
5182 Next (Act);
5183 end loop;
5184
ebdaa81b 5185 -- Deal with case where there are no actions. In this case we simply
5a521b8a 5186 -- rewrite the node with its expression since we don't need the actions
ebdaa81b
AC
5187 -- and the specification of this node does not allow a null action list.
5188
5a521b8a
AC
5189 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5190 -- the expanded tree and relying on being able to retrieve the original
5191 -- tree in cases like this. This raises a whole lot of issues of whether
5192 -- we have problems elsewhere, which will be addressed in the future???
5193
4b17187f 5194 if Is_Empty_List (Acts) then
5a521b8a 5195 Rewrite (N, Relocate_Node (Expression (N)));
ebdaa81b 5196 end if;
9b16cb57
RD
5197 end Expand_N_Expression_With_Actions;
5198
5199 ----------------------------
5200 -- Expand_N_If_Expression --
5201 ----------------------------
70482933 5202
4b985e20 5203 -- Deal with limited types and condition actions
70482933 5204
9b16cb57 5205 procedure Expand_N_If_Expression (N : Node_Id) is
b2c28399
AC
5206 procedure Process_Actions (Actions : List_Id);
5207 -- Inspect and process a single action list of an if expression for
5208 -- transient controlled objects. If such objects are found, the routine
5209 -- generates code to clean them up when the context of the expression is
5210 -- evaluated or elaborated.
3cebd1c0 5211
b2c28399
AC
5212 ---------------------
5213 -- Process_Actions --
5214 ---------------------
3cebd1c0 5215
b2c28399
AC
5216 procedure Process_Actions (Actions : List_Id) is
5217 Act : Node_Id;
3cebd1c0
AC
5218
5219 begin
b2c28399
AC
5220 Act := First (Actions);
5221 while Present (Act) loop
5222 if Nkind (Act) = N_Object_Declaration
5223 and then Is_Finalizable_Transient (Act, N)
5224 then
5225 Process_Transient_Object (Act, N);
5226 end if;
3cebd1c0 5227
b2c28399
AC
5228 Next (Act);
5229 end loop;
5230 end Process_Actions;
3cebd1c0
AC
5231
5232 -- Local variables
5233
70482933
RK
5234 Loc : constant Source_Ptr := Sloc (N);
5235 Cond : constant Node_Id := First (Expressions (N));
5236 Thenx : constant Node_Id := Next (Cond);
5237 Elsex : constant Node_Id := Next (Thenx);
5238 Typ : constant Entity_Id := Etype (N);
c471e2da 5239
3cebd1c0 5240 Actions : List_Id;
602a7ec0
AC
5241 Cnn : Entity_Id;
5242 Decl : Node_Id;
3cebd1c0 5243 Expr : Node_Id;
602a7ec0
AC
5244 New_If : Node_Id;
5245 New_N : Node_Id;
b2c28399 5246 Ptr_Typ : Entity_Id;
70482933 5247
a53c5613
AC
5248 -- Start of processing for Expand_N_If_Expression
5249
70482933 5250 begin
b6b5cca8
AC
5251 -- Check for MINIMIZED/ELIMINATED overflow mode
5252
5253 if Minimized_Eliminated_Overflow_Check (N) then
5254 Apply_Arithmetic_Overflow_Check (N);
5255 return;
5256 end if;
5257
602a7ec0 5258 -- Fold at compile time if condition known. We have already folded
9b16cb57
RD
5259 -- static if expressions, but it is possible to fold any case in which
5260 -- the condition is known at compile time, even though the result is
5261 -- non-static.
602a7ec0
AC
5262
5263 -- Note that we don't do the fold of such cases in Sem_Elab because
5264 -- it can cause infinite loops with the expander adding a conditional
5265 -- expression, and Sem_Elab circuitry removing it repeatedly.
5266
5267 if Compile_Time_Known_Value (Cond) then
5268 if Is_True (Expr_Value (Cond)) then
cc6f5d75 5269 Expr := Thenx;
602a7ec0
AC
5270 Actions := Then_Actions (N);
5271 else
cc6f5d75 5272 Expr := Elsex;
602a7ec0
AC
5273 Actions := Else_Actions (N);
5274 end if;
5275
5276 Remove (Expr);
ae77c68b
AC
5277
5278 if Present (Actions) then
ae77c68b
AC
5279 Rewrite (N,
5280 Make_Expression_With_Actions (Loc,
5281 Expression => Relocate_Node (Expr),
5282 Actions => Actions));
5283 Analyze_And_Resolve (N, Typ);
ae77c68b
AC
5284 else
5285 Rewrite (N, Relocate_Node (Expr));
5286 end if;
602a7ec0
AC
5287
5288 -- Note that the result is never static (legitimate cases of static
9b16cb57 5289 -- if expressions were folded in Sem_Eval).
602a7ec0
AC
5290
5291 Set_Is_Static_Expression (N, False);
5292 return;
5293 end if;
5294
113a9fb6
AC
5295 -- If the type is limited, and the back end does not handle limited
5296 -- types, then we expand as follows to avoid the possibility of
5297 -- improper copying.
ac7120ce 5298
c471e2da
AC
5299 -- type Ptr is access all Typ;
5300 -- Cnn : Ptr;
ac7120ce
RD
5301 -- if cond then
5302 -- <<then actions>>
5303 -- Cnn := then-expr'Unrestricted_Access;
5304 -- else
5305 -- <<else actions>>
5306 -- Cnn := else-expr'Unrestricted_Access;
5307 -- end if;
5308
9b16cb57 5309 -- and replace the if expression by a reference to Cnn.all.
ac7120ce 5310
305caf42
AC
5311 -- This special case can be skipped if the back end handles limited
5312 -- types properly and ensures that no incorrect copies are made.
5313
5314 if Is_By_Reference_Type (Typ)
5315 and then not Back_End_Handles_Limited_Types
5316 then
b2c28399
AC
5317 -- When the "then" or "else" expressions involve controlled function
5318 -- calls, generated temporaries are chained on the corresponding list
5319 -- of actions. These temporaries need to be finalized after the if
5320 -- expression is evaluated.
3cebd1c0 5321
b2c28399
AC
5322 Process_Actions (Then_Actions (N));
5323 Process_Actions (Else_Actions (N));
3cebd1c0 5324
b2c28399
AC
5325 -- Generate:
5326 -- type Ann is access all Typ;
3cebd1c0 5327
b2c28399 5328 Ptr_Typ := Make_Temporary (Loc, 'A');
3cebd1c0 5329
b2c28399
AC
5330 Insert_Action (N,
5331 Make_Full_Type_Declaration (Loc,
5332 Defining_Identifier => Ptr_Typ,
5333 Type_Definition =>
5334 Make_Access_To_Object_Definition (Loc,
5335 All_Present => True,
e4494292 5336 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
3cebd1c0 5337
b2c28399
AC
5338 -- Generate:
5339 -- Cnn : Ann;
3cebd1c0 5340
b2c28399 5341 Cnn := Make_Temporary (Loc, 'C', N);
3cebd1c0 5342
b2c28399
AC
5343 Decl :=
5344 Make_Object_Declaration (Loc,
5345 Defining_Identifier => Cnn,
5346 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
3cebd1c0 5347
b2c28399
AC
5348 -- Generate:
5349 -- if Cond then
5350 -- Cnn := <Thenx>'Unrestricted_Access;
5351 -- else
5352 -- Cnn := <Elsex>'Unrestricted_Access;
5353 -- end if;
3cebd1c0 5354
b2c28399
AC
5355 New_If :=
5356 Make_Implicit_If_Statement (N,
5357 Condition => Relocate_Node (Cond),
5358 Then_Statements => New_List (
5359 Make_Assignment_Statement (Sloc (Thenx),
e4494292 5360 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
b2c28399
AC
5361 Expression =>
5362 Make_Attribute_Reference (Loc,
5363 Prefix => Relocate_Node (Thenx),
5364 Attribute_Name => Name_Unrestricted_Access))),
3cebd1c0 5365
b2c28399
AC
5366 Else_Statements => New_List (
5367 Make_Assignment_Statement (Sloc (Elsex),
e4494292 5368 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
b2c28399
AC
5369 Expression =>
5370 Make_Attribute_Reference (Loc,
5371 Prefix => Relocate_Node (Elsex),
5372 Attribute_Name => Name_Unrestricted_Access))));
3cebd1c0 5373
414c6563
AC
5374 -- Preserve the original context for which the if statement is being
5375 -- generated. This is needed by the finalization machinery to prevent
5376 -- the premature finalization of controlled objects found within the
5377 -- if statement.
5378
5379 Set_From_Conditional_Expression (New_If);
5380
5381 New_N :=
5382 Make_Explicit_Dereference (Loc,
5383 Prefix => New_Occurrence_Of (Cnn, Loc));
fb1949a0 5384
113a9fb6
AC
5385 -- If the result is an unconstrained array and the if expression is in a
5386 -- context other than the initializing expression of the declaration of
5387 -- an object, then we pull out the if expression as follows:
5388
5389 -- Cnn : constant typ := if-expression
5390
5391 -- and then replace the if expression with an occurrence of Cnn. This
5392 -- avoids the need in the back end to create on-the-fly variable length
5393 -- temporaries (which it cannot do!)
5394
5395 -- Note that the test for being in an object declaration avoids doing an
5396 -- unnecessary expansion, and also avoids infinite recursion.
5397
5398 elsif Is_Array_Type (Typ) and then not Is_Constrained (Typ)
5399 and then (Nkind (Parent (N)) /= N_Object_Declaration
5400 or else Expression (Parent (N)) /= N)
5401 then
5402 declare
5403 Cnn : constant Node_Id := Make_Temporary (Loc, 'C', N);
5404 begin
5405 Insert_Action (N,
5406 Make_Object_Declaration (Loc,
5407 Defining_Identifier => Cnn,
5408 Constant_Present => True,
5409 Object_Definition => New_Occurrence_Of (Typ, Loc),
5410 Expression => Relocate_Node (N),
5411 Has_Init_Expression => True));
5412
5413 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
5414 return;
5415 end;
5416
c471e2da
AC
5417 -- For other types, we only need to expand if there are other actions
5418 -- associated with either branch.
5419
5420 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
c471e2da 5421
0812b84e 5422 -- We now wrap the actions into the appropriate expression
fb1949a0 5423
0812b84e
AC
5424 if Present (Then_Actions (N)) then
5425 Rewrite (Thenx,
b2c28399
AC
5426 Make_Expression_With_Actions (Sloc (Thenx),
5427 Actions => Then_Actions (N),
5428 Expression => Relocate_Node (Thenx)));
5429
0812b84e
AC
5430 Set_Then_Actions (N, No_List);
5431 Analyze_And_Resolve (Thenx, Typ);
5432 end if;
305caf42 5433
0812b84e
AC
5434 if Present (Else_Actions (N)) then
5435 Rewrite (Elsex,
b2c28399
AC
5436 Make_Expression_With_Actions (Sloc (Elsex),
5437 Actions => Else_Actions (N),
5438 Expression => Relocate_Node (Elsex)));
5439
0812b84e
AC
5440 Set_Else_Actions (N, No_List);
5441 Analyze_And_Resolve (Elsex, Typ);
305caf42
AC
5442 end if;
5443
0812b84e
AC
5444 return;
5445
b2c28399
AC
5446 -- If no actions then no expansion needed, gigi will handle it using the
5447 -- same approach as a C conditional expression.
305caf42
AC
5448
5449 else
c471e2da
AC
5450 return;
5451 end if;
5452
305caf42
AC
5453 -- Fall through here for either the limited expansion, or the case of
5454 -- inserting actions for non-limited types. In both these cases, we must
5455 -- move the SLOC of the parent If statement to the newly created one and
3fc5d116
RD
5456 -- change it to the SLOC of the expression which, after expansion, will
5457 -- correspond to what is being evaluated.
c471e2da 5458
533369aa 5459 if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then
c471e2da
AC
5460 Set_Sloc (New_If, Sloc (Parent (N)));
5461 Set_Sloc (Parent (N), Loc);
5462 end if;
70482933 5463
3fc5d116
RD
5464 -- Make sure Then_Actions and Else_Actions are appropriately moved
5465 -- to the new if statement.
5466
c471e2da
AC
5467 if Present (Then_Actions (N)) then
5468 Insert_List_Before
5469 (First (Then_Statements (New_If)), Then_Actions (N));
70482933 5470 end if;
c471e2da
AC
5471
5472 if Present (Else_Actions (N)) then
5473 Insert_List_Before
5474 (First (Else_Statements (New_If)), Else_Actions (N));
5475 end if;
5476
5477 Insert_Action (N, Decl);
5478 Insert_Action (N, New_If);
5479 Rewrite (N, New_N);
5480 Analyze_And_Resolve (N, Typ);
9b16cb57 5481 end Expand_N_If_Expression;
35a1c212 5482
70482933
RK
5483 -----------------
5484 -- Expand_N_In --
5485 -----------------
5486
5487 procedure Expand_N_In (N : Node_Id) is
7324bf49 5488 Loc : constant Source_Ptr := Sloc (N);
4818e7b9 5489 Restyp : constant Entity_Id := Etype (N);
7324bf49
AC
5490 Lop : constant Node_Id := Left_Opnd (N);
5491 Rop : constant Node_Id := Right_Opnd (N);
5492 Static : constant Boolean := Is_OK_Static_Expression (N);
70482933 5493
4818e7b9
RD
5494 Ltyp : Entity_Id;
5495 Rtyp : Entity_Id;
5496
630d30e9
RD
5497 procedure Substitute_Valid_Check;
5498 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5499 -- test for the left operand being in range of its subtype.
5500
5501 ----------------------------
5502 -- Substitute_Valid_Check --
5503 ----------------------------
5504
5505 procedure Substitute_Valid_Check is
5506 begin
c7532b2d
AC
5507 Rewrite (N,
5508 Make_Attribute_Reference (Loc,
5509 Prefix => Relocate_Node (Lop),
5510 Attribute_Name => Name_Valid));
630d30e9 5511
c7532b2d 5512 Analyze_And_Resolve (N, Restyp);
630d30e9 5513
acad3c0a
AC
5514 -- Give warning unless overflow checking is MINIMIZED or ELIMINATED,
5515 -- in which case, this usage makes sense, and in any case, we have
5516 -- actually eliminated the danger of optimization above.
5517
a7f1b24f 5518 if Overflow_Check_Mode not in Minimized_Or_Eliminated then
324ac540
AC
5519 Error_Msg_N
5520 ("??explicit membership test may be optimized away", N);
acad3c0a 5521 Error_Msg_N -- CODEFIX
324ac540 5522 ("\??use ''Valid attribute instead", N);
acad3c0a
AC
5523 end if;
5524
c7532b2d 5525 return;
630d30e9
RD
5526 end Substitute_Valid_Check;
5527
5528 -- Start of processing for Expand_N_In
5529
70482933 5530 begin
308e6f3a 5531 -- If set membership case, expand with separate procedure
4818e7b9 5532
197e4514 5533 if Present (Alternatives (N)) then
a3068ca6 5534 Expand_Set_Membership (N);
197e4514
AC
5535 return;
5536 end if;
5537
4818e7b9
RD
5538 -- Not set membership, proceed with expansion
5539
5540 Ltyp := Etype (Left_Opnd (N));
5541 Rtyp := Etype (Right_Opnd (N));
5542
5707e389 5543 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
f6194278
RD
5544 -- type, then expand with a separate procedure. Note the use of the
5545 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5546
a7f1b24f 5547 if Overflow_Check_Mode in Minimized_Or_Eliminated
f6194278
RD
5548 and then Is_Signed_Integer_Type (Ltyp)
5549 and then not No_Minimize_Eliminate (N)
5550 then
5551 Expand_Membership_Minimize_Eliminate_Overflow (N);
5552 return;
5553 end if;
5554
630d30e9
RD
5555 -- Check case of explicit test for an expression in range of its
5556 -- subtype. This is suspicious usage and we replace it with a 'Valid
b6b5cca8 5557 -- test and give a warning for scalar types.
630d30e9 5558
4818e7b9 5559 if Is_Scalar_Type (Ltyp)
b6b5cca8
AC
5560
5561 -- Only relevant for source comparisons
5562
5563 and then Comes_From_Source (N)
5564
5565 -- In floating-point this is a standard way to check for finite values
5566 -- and using 'Valid would typically be a pessimization.
5567
4818e7b9 5568 and then not Is_Floating_Point_Type (Ltyp)
b6b5cca8
AC
5569
5570 -- Don't give the message unless right operand is a type entity and
5571 -- the type of the left operand matches this type. Note that this
5572 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5573 -- checks have changed the type of the left operand.
5574
630d30e9 5575 and then Nkind (Rop) in N_Has_Entity
4818e7b9 5576 and then Ltyp = Entity (Rop)
b6b5cca8 5577
b6b5cca8
AC
5578 -- Skip this for predicated types, where such expressions are a
5579 -- reasonable way of testing if something meets the predicate.
5580
3d6db7f8 5581 and then not Present (Predicate_Function (Ltyp))
630d30e9
RD
5582 then
5583 Substitute_Valid_Check;
5584 return;
5585 end if;
5586
20b5d666
JM
5587 -- Do validity check on operands
5588
5589 if Validity_Checks_On and Validity_Check_Operands then
5590 Ensure_Valid (Left_Opnd (N));
5591 Validity_Check_Range (Right_Opnd (N));
5592 end if;
5593
630d30e9 5594 -- Case of explicit range
fbf5a39b
AC
5595
5596 if Nkind (Rop) = N_Range then
5597 declare
630d30e9
RD
5598 Lo : constant Node_Id := Low_Bound (Rop);
5599 Hi : constant Node_Id := High_Bound (Rop);
5600
5601 Lo_Orig : constant Node_Id := Original_Node (Lo);
5602 Hi_Orig : constant Node_Id := Original_Node (Hi);
5603
c800f862
RD
5604 Lcheck : Compare_Result;
5605 Ucheck : Compare_Result;
fbf5a39b 5606
d766cee3
RD
5607 Warn1 : constant Boolean :=
5608 Constant_Condition_Warnings
c800f862
RD
5609 and then Comes_From_Source (N)
5610 and then not In_Instance;
d766cee3 5611 -- This must be true for any of the optimization warnings, we
9a0ddeee
AC
5612 -- clearly want to give them only for source with the flag on. We
5613 -- also skip these warnings in an instance since it may be the
5614 -- case that different instantiations have different ranges.
d766cee3
RD
5615
5616 Warn2 : constant Boolean :=
5617 Warn1
5618 and then Nkind (Original_Node (Rop)) = N_Range
5619 and then Is_Integer_Type (Etype (Lo));
5620 -- For the case where only one bound warning is elided, we also
5621 -- insist on an explicit range and an integer type. The reason is
5622 -- that the use of enumeration ranges including an end point is
9a0ddeee
AC
5623 -- common, as is the use of a subtype name, one of whose bounds is
5624 -- the same as the type of the expression.
d766cee3 5625
fbf5a39b 5626 begin
c95e0edc 5627 -- If test is explicit x'First .. x'Last, replace by valid check
630d30e9 5628
e606088a
AC
5629 -- Could use some individual comments for this complex test ???
5630
d766cee3 5631 if Is_Scalar_Type (Ltyp)
b6b5cca8
AC
5632
5633 -- And left operand is X'First where X matches left operand
5634 -- type (this eliminates cases of type mismatch, including
5635 -- the cases where ELIMINATED/MINIMIZED mode has changed the
5636 -- type of the left operand.
5637
630d30e9
RD
5638 and then Nkind (Lo_Orig) = N_Attribute_Reference
5639 and then Attribute_Name (Lo_Orig) = Name_First
5640 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
d766cee3 5641 and then Entity (Prefix (Lo_Orig)) = Ltyp
b6b5cca8 5642
cc6f5d75 5643 -- Same tests for right operand
b6b5cca8 5644
630d30e9
RD
5645 and then Nkind (Hi_Orig) = N_Attribute_Reference
5646 and then Attribute_Name (Hi_Orig) = Name_Last
5647 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
d766cee3 5648 and then Entity (Prefix (Hi_Orig)) = Ltyp
b6b5cca8
AC
5649
5650 -- Relevant only for source cases
5651
630d30e9
RD
5652 and then Comes_From_Source (N)
5653 then
5654 Substitute_Valid_Check;
4818e7b9 5655 goto Leave;
630d30e9
RD
5656 end if;
5657
d766cee3
RD
5658 -- If bounds of type are known at compile time, and the end points
5659 -- are known at compile time and identical, this is another case
5660 -- for substituting a valid test. We only do this for discrete
5661 -- types, since it won't arise in practice for float types.
5662
5663 if Comes_From_Source (N)
5664 and then Is_Discrete_Type (Ltyp)
5665 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
5666 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
5667 and then Compile_Time_Known_Value (Lo)
5668 and then Compile_Time_Known_Value (Hi)
5669 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
5670 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
94eefd2e 5671
f6194278
RD
5672 -- Kill warnings in instances, since they may be cases where we
5673 -- have a test in the generic that makes sense with some types
5674 -- and not with other types.
94eefd2e
RD
5675
5676 and then not In_Instance
d766cee3
RD
5677 then
5678 Substitute_Valid_Check;
4818e7b9 5679 goto Leave;
d766cee3
RD
5680 end if;
5681
9a0ddeee
AC
5682 -- If we have an explicit range, do a bit of optimization based on
5683 -- range analysis (we may be able to kill one or both checks).
630d30e9 5684
c800f862
RD
5685 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
5686 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
5687
630d30e9
RD
5688 -- If either check is known to fail, replace result by False since
5689 -- the other check does not matter. Preserve the static flag for
5690 -- legality checks, because we are constant-folding beyond RM 4.9.
fbf5a39b
AC
5691
5692 if Lcheck = LT or else Ucheck = GT then
c800f862 5693 if Warn1 then
685bc70f
AC
5694 Error_Msg_N ("?c?range test optimized away", N);
5695 Error_Msg_N ("\?c?value is known to be out of range", N);
d766cee3
RD
5696 end if;
5697
e4494292 5698 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4818e7b9 5699 Analyze_And_Resolve (N, Restyp);
7324bf49 5700 Set_Is_Static_Expression (N, Static);
4818e7b9 5701 goto Leave;
fbf5a39b 5702
685094bf
RD
5703 -- If both checks are known to succeed, replace result by True,
5704 -- since we know we are in range.
fbf5a39b
AC
5705
5706 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
c800f862 5707 if Warn1 then
685bc70f
AC
5708 Error_Msg_N ("?c?range test optimized away", N);
5709 Error_Msg_N ("\?c?value is known to be in range", N);
d766cee3
RD
5710 end if;
5711
e4494292 5712 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4818e7b9 5713 Analyze_And_Resolve (N, Restyp);
7324bf49 5714 Set_Is_Static_Expression (N, Static);
4818e7b9 5715 goto Leave;
fbf5a39b 5716
d766cee3
RD
5717 -- If lower bound check succeeds and upper bound check is not
5718 -- known to succeed or fail, then replace the range check with
5719 -- a comparison against the upper bound.
fbf5a39b
AC
5720
5721 elsif Lcheck in Compare_GE then
94eefd2e 5722 if Warn2 and then not In_Instance then
324ac540
AC
5723 Error_Msg_N ("??lower bound test optimized away", Lo);
5724 Error_Msg_N ("\??value is known to be in range", Lo);
d766cee3
RD
5725 end if;
5726
fbf5a39b
AC
5727 Rewrite (N,
5728 Make_Op_Le (Loc,
5729 Left_Opnd => Lop,
5730 Right_Opnd => High_Bound (Rop)));
4818e7b9
RD
5731 Analyze_And_Resolve (N, Restyp);
5732 goto Leave;
fbf5a39b 5733
d766cee3
RD
5734 -- If upper bound check succeeds and lower bound check is not
5735 -- known to succeed or fail, then replace the range check with
5736 -- a comparison against the lower bound.
fbf5a39b
AC
5737
5738 elsif Ucheck in Compare_LE then
94eefd2e 5739 if Warn2 and then not In_Instance then
324ac540
AC
5740 Error_Msg_N ("??upper bound test optimized away", Hi);
5741 Error_Msg_N ("\??value is known to be in range", Hi);
d766cee3
RD
5742 end if;
5743
fbf5a39b
AC
5744 Rewrite (N,
5745 Make_Op_Ge (Loc,
5746 Left_Opnd => Lop,
5747 Right_Opnd => Low_Bound (Rop)));
4818e7b9
RD
5748 Analyze_And_Resolve (N, Restyp);
5749 goto Leave;
fbf5a39b 5750 end if;
c800f862
RD
5751
5752 -- We couldn't optimize away the range check, but there is one
5753 -- more issue. If we are checking constant conditionals, then we
5754 -- see if we can determine the outcome assuming everything is
5755 -- valid, and if so give an appropriate warning.
5756
5757 if Warn1 and then not Assume_No_Invalid_Values then
5758 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
5759 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
5760
5761 -- Result is out of range for valid value
5762
5763 if Lcheck = LT or else Ucheck = GT then
ed2233dc 5764 Error_Msg_N
685bc70f 5765 ("?c?value can only be in range if it is invalid", N);
c800f862
RD
5766
5767 -- Result is in range for valid value
5768
5769 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
ed2233dc 5770 Error_Msg_N
685bc70f 5771 ("?c?value can only be out of range if it is invalid", N);
c800f862
RD
5772
5773 -- Lower bound check succeeds if value is valid
5774
5775 elsif Warn2 and then Lcheck in Compare_GE then
ed2233dc 5776 Error_Msg_N
685bc70f 5777 ("?c?lower bound check only fails if it is invalid", Lo);
c800f862
RD
5778
5779 -- Upper bound check succeeds if value is valid
5780
5781 elsif Warn2 and then Ucheck in Compare_LE then
ed2233dc 5782 Error_Msg_N
685bc70f 5783 ("?c?upper bound check only fails for invalid values", Hi);
c800f862
RD
5784 end if;
5785 end if;
fbf5a39b
AC
5786 end;
5787
5788 -- For all other cases of an explicit range, nothing to be done
70482933 5789
4818e7b9 5790 goto Leave;
70482933
RK
5791
5792 -- Here right operand is a subtype mark
5793
5794 else
5795 declare
82878151
AC
5796 Typ : Entity_Id := Etype (Rop);
5797 Is_Acc : constant Boolean := Is_Access_Type (Typ);
5798 Cond : Node_Id := Empty;
5799 New_N : Node_Id;
5800 Obj : Node_Id := Lop;
5801 SCIL_Node : Node_Id;
70482933
RK
5802
5803 begin
5804 Remove_Side_Effects (Obj);
5805
5806 -- For tagged type, do tagged membership operation
5807
5808 if Is_Tagged_Type (Typ) then
fbf5a39b 5809
535a8637 5810 -- No expansion will be performed for VM targets, as the VM
69d8d8b4 5811 -- back-ends will handle the membership tests directly.
70482933 5812
1f110335 5813 if Tagged_Type_Expansion then
82878151
AC
5814 Tagged_Membership (N, SCIL_Node, New_N);
5815 Rewrite (N, New_N);
4818e7b9 5816 Analyze_And_Resolve (N, Restyp);
82878151
AC
5817
5818 -- Update decoration of relocated node referenced by the
5819 -- SCIL node.
5820
9a0ddeee 5821 if Generate_SCIL and then Present (SCIL_Node) then
7665e4bd 5822 Set_SCIL_Node (N, SCIL_Node);
82878151 5823 end if;
70482933
RK
5824 end if;
5825
4818e7b9 5826 goto Leave;
70482933 5827
c95e0edc 5828 -- If type is scalar type, rewrite as x in t'First .. t'Last.
70482933 5829 -- This reason we do this is that the bounds may have the wrong
c800f862
RD
5830 -- type if they come from the original type definition. Also this
5831 -- way we get all the processing above for an explicit range.
70482933 5832
f6194278 5833 -- Don't do this for predicated types, since in this case we
a90bd866 5834 -- want to check the predicate.
c0f136cd 5835
c7532b2d
AC
5836 elsif Is_Scalar_Type (Typ) then
5837 if No (Predicate_Function (Typ)) then
5838 Rewrite (Rop,
5839 Make_Range (Loc,
5840 Low_Bound =>
5841 Make_Attribute_Reference (Loc,
5842 Attribute_Name => Name_First,
e4494292 5843 Prefix => New_Occurrence_Of (Typ, Loc)),
c7532b2d
AC
5844
5845 High_Bound =>
5846 Make_Attribute_Reference (Loc,
5847 Attribute_Name => Name_Last,
e4494292 5848 Prefix => New_Occurrence_Of (Typ, Loc))));
c7532b2d
AC
5849 Analyze_And_Resolve (N, Restyp);
5850 end if;
70482933 5851
4818e7b9 5852 goto Leave;
5d09245e
AC
5853
5854 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5855 -- a membership test if the subtype mark denotes a constrained
5856 -- Unchecked_Union subtype and the expression lacks inferable
5857 -- discriminants.
5858
5859 elsif Is_Unchecked_Union (Base_Type (Typ))
5860 and then Is_Constrained (Typ)
5861 and then not Has_Inferable_Discriminants (Lop)
5862 then
5863 Insert_Action (N,
5864 Make_Raise_Program_Error (Loc,
5865 Reason => PE_Unchecked_Union_Restriction));
5866
9a0ddeee 5867 -- Prevent Gigi from generating incorrect code by rewriting the
f6194278 5868 -- test as False. What is this undocumented thing about ???
5d09245e 5869
9a0ddeee 5870 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4818e7b9 5871 goto Leave;
70482933
RK
5872 end if;
5873
fbf5a39b
AC
5874 -- Here we have a non-scalar type
5875
70482933
RK
5876 if Is_Acc then
5877 Typ := Designated_Type (Typ);
5878 end if;
5879
5880 if not Is_Constrained (Typ) then
e4494292 5881 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4818e7b9 5882 Analyze_And_Resolve (N, Restyp);
70482933 5883
685094bf
RD
5884 -- For the constrained array case, we have to check the subscripts
5885 -- for an exact match if the lengths are non-zero (the lengths
5886 -- must match in any case).
70482933
RK
5887
5888 elsif Is_Array_Type (Typ) then
fbf5a39b 5889 Check_Subscripts : declare
9a0ddeee 5890 function Build_Attribute_Reference
2e071734
AC
5891 (E : Node_Id;
5892 Nam : Name_Id;
5893 Dim : Nat) return Node_Id;
9a0ddeee 5894 -- Build attribute reference E'Nam (Dim)
70482933 5895
9a0ddeee
AC
5896 -------------------------------
5897 -- Build_Attribute_Reference --
5898 -------------------------------
fbf5a39b 5899
9a0ddeee 5900 function Build_Attribute_Reference
2e071734
AC
5901 (E : Node_Id;
5902 Nam : Name_Id;
5903 Dim : Nat) return Node_Id
70482933
RK
5904 is
5905 begin
5906 return
5907 Make_Attribute_Reference (Loc,
9a0ddeee 5908 Prefix => E,
70482933 5909 Attribute_Name => Nam,
9a0ddeee 5910 Expressions => New_List (
70482933 5911 Make_Integer_Literal (Loc, Dim)));
9a0ddeee 5912 end Build_Attribute_Reference;
70482933 5913
fad0600d 5914 -- Start of processing for Check_Subscripts
fbf5a39b 5915
70482933
RK
5916 begin
5917 for J in 1 .. Number_Dimensions (Typ) loop
5918 Evolve_And_Then (Cond,
5919 Make_Op_Eq (Loc,
5920 Left_Opnd =>
9a0ddeee 5921 Build_Attribute_Reference
fbf5a39b
AC
5922 (Duplicate_Subexpr_No_Checks (Obj),
5923 Name_First, J),
70482933 5924 Right_Opnd =>
9a0ddeee 5925 Build_Attribute_Reference
70482933
RK
5926 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
5927
5928 Evolve_And_Then (Cond,
5929 Make_Op_Eq (Loc,
5930 Left_Opnd =>
9a0ddeee 5931 Build_Attribute_Reference
fbf5a39b
AC
5932 (Duplicate_Subexpr_No_Checks (Obj),
5933 Name_Last, J),
70482933 5934 Right_Opnd =>
9a0ddeee 5935 Build_Attribute_Reference
70482933
RK
5936 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
5937 end loop;
5938
5939 if Is_Acc then
fbf5a39b
AC
5940 Cond :=
5941 Make_Or_Else (Loc,
cc6f5d75 5942 Left_Opnd =>
fbf5a39b
AC
5943 Make_Op_Eq (Loc,
5944 Left_Opnd => Obj,
5945 Right_Opnd => Make_Null (Loc)),
5946 Right_Opnd => Cond);
70482933
RK
5947 end if;
5948
5949 Rewrite (N, Cond);
4818e7b9 5950 Analyze_And_Resolve (N, Restyp);
fbf5a39b 5951 end Check_Subscripts;
70482933 5952
685094bf
RD
5953 -- These are the cases where constraint checks may be required,
5954 -- e.g. records with possible discriminants
70482933
RK
5955
5956 else
5957 -- Expand the test into a series of discriminant comparisons.
685094bf
RD
5958 -- The expression that is built is the negation of the one that
5959 -- is used for checking discriminant constraints.
70482933
RK
5960
5961 Obj := Relocate_Node (Left_Opnd (N));
5962
5963 if Has_Discriminants (Typ) then
5964 Cond := Make_Op_Not (Loc,
5965 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
5966
5967 if Is_Acc then
5968 Cond := Make_Or_Else (Loc,
cc6f5d75 5969 Left_Opnd =>
70482933
RK
5970 Make_Op_Eq (Loc,
5971 Left_Opnd => Obj,
5972 Right_Opnd => Make_Null (Loc)),
5973 Right_Opnd => Cond);
5974 end if;
5975
5976 else
5977 Cond := New_Occurrence_Of (Standard_True, Loc);
5978 end if;
5979
5980 Rewrite (N, Cond);
4818e7b9 5981 Analyze_And_Resolve (N, Restyp);
70482933 5982 end if;
6cce2156
GD
5983
5984 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
5985 -- expression of an anonymous access type. This can involve an
5986 -- accessibility test and a tagged type membership test in the
5987 -- case of tagged designated types.
5988
5989 if Ada_Version >= Ada_2012
5990 and then Is_Acc
5991 and then Ekind (Ltyp) = E_Anonymous_Access_Type
5992 then
5993 declare
5994 Expr_Entity : Entity_Id := Empty;
5995 New_N : Node_Id;
5996 Param_Level : Node_Id;
5997 Type_Level : Node_Id;
996c8821 5998
6cce2156
GD
5999 begin
6000 if Is_Entity_Name (Lop) then
6001 Expr_Entity := Param_Entity (Lop);
996c8821 6002
6cce2156
GD
6003 if not Present (Expr_Entity) then
6004 Expr_Entity := Entity (Lop);
6005 end if;
6006 end if;
6007
6008 -- If a conversion of the anonymous access value to the
6009 -- tested type would be illegal, then the result is False.
6010
6011 if not Valid_Conversion
6012 (Lop, Rtyp, Lop, Report_Errs => False)
6013 then
6014 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
6015 Analyze_And_Resolve (N, Restyp);
6016
6017 -- Apply an accessibility check if the access object has an
6018 -- associated access level and when the level of the type is
6019 -- less deep than the level of the access parameter. This
6020 -- only occur for access parameters and stand-alone objects
6021 -- of an anonymous access type.
6022
6023 else
6024 if Present (Expr_Entity)
996c8821
RD
6025 and then
6026 Present
6027 (Effective_Extra_Accessibility (Expr_Entity))
6028 and then UI_Gt (Object_Access_Level (Lop),
6029 Type_Access_Level (Rtyp))
6cce2156
GD
6030 then
6031 Param_Level :=
6032 New_Occurrence_Of
d15f9422 6033 (Effective_Extra_Accessibility (Expr_Entity), Loc);
6cce2156
GD
6034
6035 Type_Level :=
6036 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
6037
6038 -- Return True only if the accessibility level of the
6039 -- expression entity is not deeper than the level of
6040 -- the tested access type.
6041
6042 Rewrite (N,
6043 Make_And_Then (Loc,
6044 Left_Opnd => Relocate_Node (N),
6045 Right_Opnd => Make_Op_Le (Loc,
6046 Left_Opnd => Param_Level,
6047 Right_Opnd => Type_Level)));
6048
6049 Analyze_And_Resolve (N);
6050 end if;
6051
6052 -- If the designated type is tagged, do tagged membership
6053 -- operation.
6054
6055 -- *** NOTE: we have to check not null before doing the
6056 -- tagged membership test (but maybe that can be done
6057 -- inside Tagged_Membership?).
6058
6059 if Is_Tagged_Type (Typ) then
6060 Rewrite (N,
6061 Make_And_Then (Loc,
6062 Left_Opnd => Relocate_Node (N),
6063 Right_Opnd =>
6064 Make_Op_Ne (Loc,
6065 Left_Opnd => Obj,
6066 Right_Opnd => Make_Null (Loc))));
6067
535a8637 6068 -- No expansion will be performed for VM targets, as
6cce2156 6069 -- the VM back-ends will handle the membership tests
69d8d8b4 6070 -- directly.
6cce2156
GD
6071
6072 if Tagged_Type_Expansion then
6073
6074 -- Note that we have to pass Original_Node, because
6075 -- the membership test might already have been
6076 -- rewritten by earlier parts of membership test.
6077
6078 Tagged_Membership
6079 (Original_Node (N), SCIL_Node, New_N);
6080
6081 -- Update decoration of relocated node referenced
6082 -- by the SCIL node.
6083
6084 if Generate_SCIL and then Present (SCIL_Node) then
6085 Set_SCIL_Node (New_N, SCIL_Node);
6086 end if;
6087
6088 Rewrite (N,
6089 Make_And_Then (Loc,
6090 Left_Opnd => Relocate_Node (N),
6091 Right_Opnd => New_N));
6092
6093 Analyze_And_Resolve (N, Restyp);
6094 end if;
6095 end if;
6096 end if;
6097 end;
6098 end if;
70482933
RK
6099 end;
6100 end if;
4818e7b9
RD
6101
6102 -- At this point, we have done the processing required for the basic
6103 -- membership test, but not yet dealt with the predicate.
6104
6105 <<Leave>>
6106
c7532b2d
AC
6107 -- If a predicate is present, then we do the predicate test, but we
6108 -- most certainly want to omit this if we are within the predicate
a90bd866 6109 -- function itself, since otherwise we have an infinite recursion.
3d6db7f8
GD
6110 -- The check should also not be emitted when testing against a range
6111 -- (the check is only done when the right operand is a subtype; see
6112 -- RM12-4.5.2 (28.1/3-30/3)).
4818e7b9 6113
c7532b2d
AC
6114 declare
6115 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
4818e7b9 6116
c7532b2d
AC
6117 begin
6118 if Present (PFunc)
6119 and then Current_Scope /= PFunc
3d6db7f8 6120 and then Nkind (Rop) /= N_Range
c7532b2d
AC
6121 then
6122 Rewrite (N,
6123 Make_And_Then (Loc,
6124 Left_Opnd => Relocate_Node (N),
fc142f63 6125 Right_Opnd => Make_Predicate_Call (Rtyp, Lop, Mem => True)));
4818e7b9 6126
c7532b2d 6127 -- Analyze new expression, mark left operand as analyzed to
b2009d46
AC
6128 -- avoid infinite recursion adding predicate calls. Similarly,
6129 -- suppress further range checks on the call.
4818e7b9 6130
c7532b2d 6131 Set_Analyzed (Left_Opnd (N));
b2009d46 6132 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4818e7b9 6133
c7532b2d
AC
6134 -- All done, skip attempt at compile time determination of result
6135
6136 return;
6137 end if;
6138 end;
70482933
RK
6139 end Expand_N_In;
6140
6141 --------------------------------
6142 -- Expand_N_Indexed_Component --
6143 --------------------------------
6144
6145 procedure Expand_N_Indexed_Component (N : Node_Id) is
6146 Loc : constant Source_Ptr := Sloc (N);
6147 Typ : constant Entity_Id := Etype (N);
6148 P : constant Node_Id := Prefix (N);
6149 T : constant Entity_Id := Etype (P);
5972791c 6150 Atp : Entity_Id;
70482933
RK
6151
6152 begin
685094bf
RD
6153 -- A special optimization, if we have an indexed component that is
6154 -- selecting from a slice, then we can eliminate the slice, since, for
6155 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6156 -- the range check required by the slice. The range check for the slice
6157 -- itself has already been generated. The range check for the
6158 -- subscripting operation is ensured by converting the subject to
6159 -- the subtype of the slice.
6160
6161 -- This optimization not only generates better code, avoiding slice
6162 -- messing especially in the packed case, but more importantly bypasses
6163 -- some problems in handling this peculiar case, for example, the issue
6164 -- of dealing specially with object renamings.
70482933 6165
45ec05e1
RD
6166 if Nkind (P) = N_Slice
6167
6168 -- This optimization is disabled for CodePeer because it can transform
6169 -- an index-check constraint_error into a range-check constraint_error
6170 -- and CodePeer cares about that distinction.
6171
6172 and then not CodePeer_Mode
6173 then
70482933
RK
6174 Rewrite (N,
6175 Make_Indexed_Component (Loc,
cc6f5d75 6176 Prefix => Prefix (P),
70482933
RK
6177 Expressions => New_List (
6178 Convert_To
6179 (Etype (First_Index (Etype (P))),
6180 First (Expressions (N))))));
6181 Analyze_And_Resolve (N, Typ);
6182 return;
6183 end if;
6184
b4592168
GD
6185 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6186 -- function, then additional actuals must be passed.
6187
0791fbe9 6188 if Ada_Version >= Ada_2005
b4592168
GD
6189 and then Is_Build_In_Place_Function_Call (P)
6190 then
6191 Make_Build_In_Place_Call_In_Anonymous_Context (P);
6192 end if;
6193
685094bf 6194 -- If the prefix is an access type, then we unconditionally rewrite if
09494c32 6195 -- as an explicit dereference. This simplifies processing for several
685094bf
RD
6196 -- cases, including packed array cases and certain cases in which checks
6197 -- must be generated. We used to try to do this only when it was
6198 -- necessary, but it cleans up the code to do it all the time.
70482933
RK
6199
6200 if Is_Access_Type (T) then
2717634d 6201 Insert_Explicit_Dereference (P);
70482933 6202 Analyze_And_Resolve (P, Designated_Type (T));
5972791c
AC
6203 Atp := Designated_Type (T);
6204 else
6205 Atp := T;
70482933
RK
6206 end if;
6207
fbf5a39b
AC
6208 -- Generate index and validity checks
6209
6210 Generate_Index_Checks (N);
6211
70482933
RK
6212 if Validity_Checks_On and then Validity_Check_Subscripts then
6213 Apply_Subscript_Validity_Checks (N);
6214 end if;
6215
5972791c
AC
6216 -- If selecting from an array with atomic components, and atomic sync
6217 -- is not suppressed for this array type, set atomic sync flag.
6218
6219 if (Has_Atomic_Components (Atp)
6220 and then not Atomic_Synchronization_Disabled (Atp))
6221 or else (Is_Atomic (Typ)
6222 and then not Atomic_Synchronization_Disabled (Typ))
6223 then
4c318253 6224 Activate_Atomic_Synchronization (N);
5972791c
AC
6225 end if;
6226
70482933
RK
6227 -- All done for the non-packed case
6228
6229 if not Is_Packed (Etype (Prefix (N))) then
6230 return;
6231 end if;
6232
6233 -- For packed arrays that are not bit-packed (i.e. the case of an array
8fc789c8 6234 -- with one or more index types with a non-contiguous enumeration type),
70482933
RK
6235 -- we can always use the normal packed element get circuit.
6236
6237 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
6238 Expand_Packed_Element_Reference (N);
6239 return;
6240 end if;
6241
8ca597af
RD
6242 -- For a reference to a component of a bit packed array, we convert it
6243 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
6244 -- want to do this for simple references, and not for:
70482933 6245
685094bf
RD
6246 -- Left side of assignment, or prefix of left side of assignment, or
6247 -- prefix of the prefix, to handle packed arrays of packed arrays,
70482933
RK
6248 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6249
6250 -- Renaming objects in renaming associations
6251 -- This case is handled when a use of the renamed variable occurs
6252
6253 -- Actual parameters for a procedure call
6254 -- This case is handled in Exp_Ch6.Expand_Actuals
6255
6256 -- The second expression in a 'Read attribute reference
6257
47d3b920 6258 -- The prefix of an address or bit or size attribute reference
70482933 6259
e8c84c8f
AC
6260 -- The following circuit detects these exceptions. Note that we need to
6261 -- deal with implicit dereferences when climbing up the parent chain,
6262 -- with the additional difficulty that the type of parents may have yet
6263 -- to be resolved since prefixes are usually resolved first.
70482933
RK
6264
6265 declare
6266 Child : Node_Id := N;
6267 Parnt : Node_Id := Parent (N);
6268
6269 begin
6270 loop
6271 if Nkind (Parnt) = N_Unchecked_Expression then
6272 null;
6273
303b4d58
AC
6274 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
6275 N_Procedure_Call_Statement)
70482933
RK
6276 or else (Nkind (Parnt) = N_Parameter_Association
6277 and then
1b1d88b1 6278 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
70482933
RK
6279 then
6280 return;
6281
6282 elsif Nkind (Parnt) = N_Attribute_Reference
b69cd36a
AC
6283 and then Nam_In (Attribute_Name (Parnt), Name_Address,
6284 Name_Bit,
6285 Name_Size)
70482933
RK
6286 and then Prefix (Parnt) = Child
6287 then
6288 return;
6289
6290 elsif Nkind (Parnt) = N_Assignment_Statement
6291 and then Name (Parnt) = Child
6292 then
6293 return;
6294
685094bf
RD
6295 -- If the expression is an index of an indexed component, it must
6296 -- be expanded regardless of context.
fbf5a39b
AC
6297
6298 elsif Nkind (Parnt) = N_Indexed_Component
6299 and then Child /= Prefix (Parnt)
6300 then
6301 Expand_Packed_Element_Reference (N);
6302 return;
6303
6304 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
6305 and then Name (Parent (Parnt)) = Parnt
6306 then
6307 return;
6308
70482933
RK
6309 elsif Nkind (Parnt) = N_Attribute_Reference
6310 and then Attribute_Name (Parnt) = Name_Read
6311 and then Next (First (Expressions (Parnt))) = Child
6312 then
6313 return;
6314
e8c84c8f
AC
6315 elsif Nkind (Parnt) = N_Indexed_Component
6316 and then Prefix (Parnt) = Child
6317 then
6318 null;
6319
6320 elsif Nkind (Parnt) = N_Selected_Component
533369aa 6321 and then Prefix (Parnt) = Child
e8c84c8f
AC
6322 and then not (Present (Etype (Selector_Name (Parnt)))
6323 and then
6324 Is_Access_Type (Etype (Selector_Name (Parnt))))
70482933
RK
6325 then
6326 null;
6327
e8c84c8f
AC
6328 -- If the parent is a dereference, either implicit or explicit,
6329 -- then the packed reference needs to be expanded.
6330
70482933
RK
6331 else
6332 Expand_Packed_Element_Reference (N);
6333 return;
6334 end if;
6335
685094bf
RD
6336 -- Keep looking up tree for unchecked expression, or if we are the
6337 -- prefix of a possible assignment left side.
70482933
RK
6338
6339 Child := Parnt;
6340 Parnt := Parent (Child);
6341 end loop;
6342 end;
70482933
RK
6343 end Expand_N_Indexed_Component;
6344
6345 ---------------------
6346 -- Expand_N_Not_In --
6347 ---------------------
6348
6349 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6350 -- can be done. This avoids needing to duplicate this expansion code.
6351
6352 procedure Expand_N_Not_In (N : Node_Id) is
630d30e9
RD
6353 Loc : constant Source_Ptr := Sloc (N);
6354 Typ : constant Entity_Id := Etype (N);
6355 Cfs : constant Boolean := Comes_From_Source (N);
70482933
RK
6356
6357 begin
6358 Rewrite (N,
6359 Make_Op_Not (Loc,
6360 Right_Opnd =>
6361 Make_In (Loc,
6362 Left_Opnd => Left_Opnd (N),
d766cee3 6363 Right_Opnd => Right_Opnd (N))));
630d30e9 6364
197e4514
AC
6365 -- If this is a set membership, preserve list of alternatives
6366
6367 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
6368
d766cee3 6369 -- We want this to appear as coming from source if original does (see
8fc789c8 6370 -- transformations in Expand_N_In).
630d30e9
RD
6371
6372 Set_Comes_From_Source (N, Cfs);
6373 Set_Comes_From_Source (Right_Opnd (N), Cfs);
6374
8fc789c8 6375 -- Now analyze transformed node
630d30e9 6376
70482933
RK
6377 Analyze_And_Resolve (N, Typ);
6378 end Expand_N_Not_In;
6379
6380 -------------------
6381 -- Expand_N_Null --
6382 -------------------
6383
a3f2babd
AC
6384 -- The only replacement required is for the case of a null of a type that
6385 -- is an access to protected subprogram, or a subtype thereof. We represent
6386 -- such access values as a record, and so we must replace the occurrence of
6387 -- null by the equivalent record (with a null address and a null pointer in
6388 -- it), so that the backend creates the proper value.
70482933
RK
6389
6390 procedure Expand_N_Null (N : Node_Id) is
6391 Loc : constant Source_Ptr := Sloc (N);
a3f2babd 6392 Typ : constant Entity_Id := Base_Type (Etype (N));
70482933
RK
6393 Agg : Node_Id;
6394
6395 begin
26bff3d9 6396 if Is_Access_Protected_Subprogram_Type (Typ) then
70482933
RK
6397 Agg :=
6398 Make_Aggregate (Loc,
6399 Expressions => New_List (
6400 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
6401 Make_Null (Loc)));
6402
6403 Rewrite (N, Agg);
6404 Analyze_And_Resolve (N, Equivalent_Type (Typ));
6405
685094bf
RD
6406 -- For subsequent semantic analysis, the node must retain its type.
6407 -- Gigi in any case replaces this type by the corresponding record
6408 -- type before processing the node.
70482933
RK
6409
6410 Set_Etype (N, Typ);
6411 end if;
fbf5a39b
AC
6412
6413 exception
6414 when RE_Not_Available =>
6415 return;
70482933
RK
6416 end Expand_N_Null;
6417
6418 ---------------------
6419 -- Expand_N_Op_Abs --
6420 ---------------------
6421
6422 procedure Expand_N_Op_Abs (N : Node_Id) is
6423 Loc : constant Source_Ptr := Sloc (N);
cc6f5d75 6424 Expr : constant Node_Id := Right_Opnd (N);
70482933
RK
6425
6426 begin
6427 Unary_Op_Validity_Checks (N);
6428
b6b5cca8
AC
6429 -- Check for MINIMIZED/ELIMINATED overflow mode
6430
6431 if Minimized_Eliminated_Overflow_Check (N) then
6432 Apply_Arithmetic_Overflow_Check (N);
6433 return;
6434 end if;
6435
70482933
RK
6436 -- Deal with software overflow checking
6437
07fc65c4 6438 if not Backend_Overflow_Checks_On_Target
533369aa
AC
6439 and then Is_Signed_Integer_Type (Etype (N))
6440 and then Do_Overflow_Check (N)
70482933 6441 then
685094bf
RD
6442 -- The only case to worry about is when the argument is equal to the
6443 -- largest negative number, so what we do is to insert the check:
70482933 6444
fbf5a39b 6445 -- [constraint_error when Expr = typ'Base'First]
70482933
RK
6446
6447 -- with the usual Duplicate_Subexpr use coding for expr
6448
fbf5a39b
AC
6449 Insert_Action (N,
6450 Make_Raise_Constraint_Error (Loc,
6451 Condition =>
6452 Make_Op_Eq (Loc,
70482933 6453 Left_Opnd => Duplicate_Subexpr (Expr),
fbf5a39b
AC
6454 Right_Opnd =>
6455 Make_Attribute_Reference (Loc,
cc6f5d75 6456 Prefix =>
fbf5a39b
AC
6457 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
6458 Attribute_Name => Name_First)),
6459 Reason => CE_Overflow_Check_Failed));
6460 end if;
70482933
RK
6461 end Expand_N_Op_Abs;
6462
6463 ---------------------
6464 -- Expand_N_Op_Add --
6465 ---------------------
6466
6467 procedure Expand_N_Op_Add (N : Node_Id) is
6468 Typ : constant Entity_Id := Etype (N);
6469
6470 begin
6471 Binary_Op_Validity_Checks (N);
6472
b6b5cca8
AC
6473 -- Check for MINIMIZED/ELIMINATED overflow mode
6474
6475 if Minimized_Eliminated_Overflow_Check (N) then
6476 Apply_Arithmetic_Overflow_Check (N);
6477 return;
6478 end if;
6479
70482933
RK
6480 -- N + 0 = 0 + N = N for integer types
6481
6482 if Is_Integer_Type (Typ) then
6483 if Compile_Time_Known_Value (Right_Opnd (N))
6484 and then Expr_Value (Right_Opnd (N)) = Uint_0
6485 then
6486 Rewrite (N, Left_Opnd (N));
6487 return;
6488
6489 elsif Compile_Time_Known_Value (Left_Opnd (N))
6490 and then Expr_Value (Left_Opnd (N)) = Uint_0
6491 then
6492 Rewrite (N, Right_Opnd (N));
6493 return;
6494 end if;
6495 end if;
6496
fbf5a39b 6497 -- Arithmetic overflow checks for signed integer/fixed point types
70482933 6498
761f7dcb 6499 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
70482933
RK
6500 Apply_Arithmetic_Overflow_Check (N);
6501 return;
70482933 6502 end if;
dfaff97b
RD
6503
6504 -- Overflow checks for floating-point if -gnateF mode active
6505
6506 Check_Float_Op_Overflow (N);
70482933
RK
6507 end Expand_N_Op_Add;
6508
6509 ---------------------
6510 -- Expand_N_Op_And --
6511 ---------------------
6512
6513 procedure Expand_N_Op_And (N : Node_Id) is
6514 Typ : constant Entity_Id := Etype (N);
6515
6516 begin
6517 Binary_Op_Validity_Checks (N);
6518
6519 if Is_Array_Type (Etype (N)) then
6520 Expand_Boolean_Operator (N);
6521
6522 elsif Is_Boolean_Type (Etype (N)) then
f2d10a02
AC
6523 Adjust_Condition (Left_Opnd (N));
6524 Adjust_Condition (Right_Opnd (N));
6525 Set_Etype (N, Standard_Boolean);
6526 Adjust_Result_Type (N, Typ);
437f8c1e
AC
6527
6528 elsif Is_Intrinsic_Subprogram (Entity (N)) then
6529 Expand_Intrinsic_Call (N, Entity (N));
6530
70482933
RK
6531 end if;
6532 end Expand_N_Op_And;
6533
6534 ------------------------
6535 -- Expand_N_Op_Concat --
6536 ------------------------
6537
6538 procedure Expand_N_Op_Concat (N : Node_Id) is
70482933
RK
6539 Opnds : List_Id;
6540 -- List of operands to be concatenated
6541
70482933 6542 Cnode : Node_Id;
685094bf
RD
6543 -- Node which is to be replaced by the result of concatenating the nodes
6544 -- in the list Opnds.
70482933 6545
70482933 6546 begin
fbf5a39b
AC
6547 -- Ensure validity of both operands
6548
70482933
RK
6549 Binary_Op_Validity_Checks (N);
6550
685094bf
RD
6551 -- If we are the left operand of a concatenation higher up the tree,
6552 -- then do nothing for now, since we want to deal with a series of
6553 -- concatenations as a unit.
70482933
RK
6554
6555 if Nkind (Parent (N)) = N_Op_Concat
6556 and then N = Left_Opnd (Parent (N))
6557 then
6558 return;
6559 end if;
6560
6561 -- We get here with a concatenation whose left operand may be a
6562 -- concatenation itself with a consistent type. We need to process
6563 -- these concatenation operands from left to right, which means
6564 -- from the deepest node in the tree to the highest node.
6565
6566 Cnode := N;
6567 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
6568 Cnode := Left_Opnd (Cnode);
6569 end loop;
6570
64425dff
BD
6571 -- Now Cnode is the deepest concatenation, and its parents are the
6572 -- concatenation nodes above, so now we process bottom up, doing the
64425dff 6573 -- operands.
70482933 6574
df46b832
AC
6575 -- The outer loop runs more than once if more than one concatenation
6576 -- type is involved.
70482933
RK
6577
6578 Outer : loop
6579 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
6580 Set_Parent (Opnds, N);
6581
df46b832 6582 -- The inner loop gathers concatenation operands
70482933
RK
6583
6584 Inner : while Cnode /= N
70482933
RK
6585 and then Base_Type (Etype (Cnode)) =
6586 Base_Type (Etype (Parent (Cnode)))
6587 loop
6588 Cnode := Parent (Cnode);
6589 Append (Right_Opnd (Cnode), Opnds);
6590 end loop Inner;
6591
43c58950
AC
6592 -- Note: The following code is a temporary workaround for N731-034
6593 -- and N829-028 and will be kept until the general issue of internal
6594 -- symbol serialization is addressed. The workaround is kept under a
6595 -- debug switch to avoid permiating into the general case.
6596
6597 -- Wrap the node to concatenate into an expression actions node to
6598 -- keep it nicely packaged. This is useful in the case of an assert
6599 -- pragma with a concatenation where we want to be able to delete
6600 -- the concatenation and all its expansion stuff.
6601
6602 if Debug_Flag_Dot_H then
6603 declare
6604 Cnod : constant Node_Id := Relocate_Node (Cnode);
6605 Typ : constant Entity_Id := Base_Type (Etype (Cnode));
6606
6607 begin
6608 -- Note: use Rewrite rather than Replace here, so that for
6609 -- example Why_Not_Static can find the original concatenation
6610 -- node OK!
6611
6612 Rewrite (Cnode,
6613 Make_Expression_With_Actions (Sloc (Cnode),
6614 Actions => New_List (Make_Null_Statement (Sloc (Cnode))),
6615 Expression => Cnod));
6616
6617 Expand_Concatenate (Cnod, Opnds);
6618 Analyze_And_Resolve (Cnode, Typ);
6619 end;
6620
6621 -- Default case
6622
6623 else
6624 Expand_Concatenate (Cnode, Opnds);
6625 end if;
70482933
RK
6626
6627 exit Outer when Cnode = N;
6628 Cnode := Parent (Cnode);
6629 end loop Outer;
6630 end Expand_N_Op_Concat;
6631
6632 ------------------------
6633 -- Expand_N_Op_Divide --
6634 ------------------------
6635
6636 procedure Expand_N_Op_Divide (N : Node_Id) is
f82944b7
JM
6637 Loc : constant Source_Ptr := Sloc (N);
6638 Lopnd : constant Node_Id := Left_Opnd (N);
6639 Ropnd : constant Node_Id := Right_Opnd (N);
6640 Ltyp : constant Entity_Id := Etype (Lopnd);
6641 Rtyp : constant Entity_Id := Etype (Ropnd);
6642 Typ : Entity_Id := Etype (N);
6643 Rknow : constant Boolean := Is_Integer_Type (Typ)
6644 and then
6645 Compile_Time_Known_Value (Ropnd);
6646 Rval : Uint;
70482933
RK
6647
6648 begin
6649 Binary_Op_Validity_Checks (N);
6650
b6b5cca8
AC
6651 -- Check for MINIMIZED/ELIMINATED overflow mode
6652
6653 if Minimized_Eliminated_Overflow_Check (N) then
6654 Apply_Arithmetic_Overflow_Check (N);
6655 return;
6656 end if;
6657
6658 -- Otherwise proceed with expansion of division
6659
f82944b7
JM
6660 if Rknow then
6661 Rval := Expr_Value (Ropnd);
6662 end if;
6663
70482933
RK
6664 -- N / 1 = N for integer types
6665
f82944b7
JM
6666 if Rknow and then Rval = Uint_1 then
6667 Rewrite (N, Lopnd);
70482933
RK
6668 return;
6669 end if;
6670
6671 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6672 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6673 -- operand is an unsigned integer, as required for this to work.
6674
f82944b7
JM
6675 if Nkind (Ropnd) = N_Op_Expon
6676 and then Is_Power_Of_2_For_Shift (Ropnd)
fbf5a39b
AC
6677
6678 -- We cannot do this transformation in configurable run time mode if we
51bf9bdf 6679 -- have 64-bit integers and long shifts are not available.
fbf5a39b 6680
761f7dcb 6681 and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target)
70482933
RK
6682 then
6683 Rewrite (N,
6684 Make_Op_Shift_Right (Loc,
f82944b7 6685 Left_Opnd => Lopnd,
70482933 6686 Right_Opnd =>
f82944b7 6687 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
70482933
RK
6688 Analyze_And_Resolve (N, Typ);
6689 return;
6690 end if;
6691
6692 -- Do required fixup of universal fixed operation
6693
6694 if Typ = Universal_Fixed then
6695 Fixup_Universal_Fixed_Operation (N);
6696 Typ := Etype (N);
6697 end if;
6698
6699 -- Divisions with fixed-point results
6700
6701 if Is_Fixed_Point_Type (Typ) then
6702
21f30884
AC
6703 -- Deal with divide-by-zero check if back end cannot handle them
6704 -- and the flag is set indicating that we need such a check. Note
6705 -- that we don't need to bother here with the case of mixed-mode
6706 -- (Right operand an integer type), since these will be rewritten
6707 -- with conversions to a divide with a fixed-point right operand.
6708
6709 if Do_Division_Check (N)
6710 and then not Backend_Divide_Checks_On_Target
6711 and then not Is_Integer_Type (Rtyp)
6712 then
6713 Set_Do_Division_Check (N, False);
6714 Insert_Action (N,
6715 Make_Raise_Constraint_Error (Loc,
6716 Condition =>
6717 Make_Op_Eq (Loc,
6718 Left_Opnd => Duplicate_Subexpr_Move_Checks (Ropnd),
6719 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
6720 Reason => CE_Divide_By_Zero));
6721 end if;
6722
685094bf
RD
6723 -- No special processing if Treat_Fixed_As_Integer is set, since
6724 -- from a semantic point of view such operations are simply integer
6725 -- operations and will be treated that way.
70482933
RK
6726
6727 if not Treat_Fixed_As_Integer (N) then
6728 if Is_Integer_Type (Rtyp) then
6729 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
6730 else
6731 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
6732 end if;
6733 end if;
6734
685094bf
RD
6735 -- Other cases of division of fixed-point operands. Again we exclude the
6736 -- case where Treat_Fixed_As_Integer is set.
70482933 6737
761f7dcb 6738 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
70482933
RK
6739 and then not Treat_Fixed_As_Integer (N)
6740 then
6741 if Is_Integer_Type (Typ) then
6742 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
6743 else
6744 pragma Assert (Is_Floating_Point_Type (Typ));
6745 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
6746 end if;
6747
685094bf
RD
6748 -- Mixed-mode operations can appear in a non-static universal context,
6749 -- in which case the integer argument must be converted explicitly.
70482933 6750
533369aa 6751 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
f82944b7
JM
6752 Rewrite (Ropnd,
6753 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
70482933 6754
f82944b7 6755 Analyze_And_Resolve (Ropnd, Universal_Real);
70482933 6756
533369aa 6757 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
f82944b7
JM
6758 Rewrite (Lopnd,
6759 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
70482933 6760
f82944b7 6761 Analyze_And_Resolve (Lopnd, Universal_Real);
70482933 6762
f02b8bb8 6763 -- Non-fixed point cases, do integer zero divide and overflow checks
70482933
RK
6764
6765 elsif Is_Integer_Type (Typ) then
a91e9ac7 6766 Apply_Divide_Checks (N);
70482933 6767 end if;
dfaff97b
RD
6768
6769 -- Overflow checks for floating-point if -gnateF mode active
6770
6771 Check_Float_Op_Overflow (N);
70482933
RK
6772 end Expand_N_Op_Divide;
6773
6774 --------------------
6775 -- Expand_N_Op_Eq --
6776 --------------------
6777
6778 procedure Expand_N_Op_Eq (N : Node_Id) is
fbf5a39b
AC
6779 Loc : constant Source_Ptr := Sloc (N);
6780 Typ : constant Entity_Id := Etype (N);
6781 Lhs : constant Node_Id := Left_Opnd (N);
6782 Rhs : constant Node_Id := Right_Opnd (N);
6783 Bodies : constant List_Id := New_List;
6784 A_Typ : constant Entity_Id := Etype (Lhs);
6785
70482933
RK
6786 Typl : Entity_Id := A_Typ;
6787 Op_Name : Entity_Id;
6788 Prim : Elmt_Id;
70482933
RK
6789
6790 procedure Build_Equality_Call (Eq : Entity_Id);
6791 -- If a constructed equality exists for the type or for its parent,
6792 -- build and analyze call, adding conversions if the operation is
6793 -- inherited.
6794
5d09245e 6795 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
8fc789c8 6796 -- Determines whether a type has a subcomponent of an unconstrained
5d09245e
AC
6797 -- Unchecked_Union subtype. Typ is a record type.
6798
70482933
RK
6799 -------------------------
6800 -- Build_Equality_Call --
6801 -------------------------
6802
6803 procedure Build_Equality_Call (Eq : Entity_Id) is
6804 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
cc6f5d75
AC
6805 L_Exp : Node_Id := Relocate_Node (Lhs);
6806 R_Exp : Node_Id := Relocate_Node (Rhs);
70482933
RK
6807
6808 begin
dda38714
AC
6809 -- Adjust operands if necessary to comparison type
6810
70482933
RK
6811 if Base_Type (Op_Type) /= Base_Type (A_Typ)
6812 and then not Is_Class_Wide_Type (A_Typ)
6813 then
6814 L_Exp := OK_Convert_To (Op_Type, L_Exp);
6815 R_Exp := OK_Convert_To (Op_Type, R_Exp);
6816 end if;
6817
5d09245e
AC
6818 -- If we have an Unchecked_Union, we need to add the inferred
6819 -- discriminant values as actuals in the function call. At this
6820 -- point, the expansion has determined that both operands have
6821 -- inferable discriminants.
6822
6823 if Is_Unchecked_Union (Op_Type) then
6824 declare
fa1608c2
ES
6825 Lhs_Type : constant Node_Id := Etype (L_Exp);
6826 Rhs_Type : constant Node_Id := Etype (R_Exp);
6827
6828 Lhs_Discr_Vals : Elist_Id;
6829 -- List of inferred discriminant values for left operand.
6830
6831 Rhs_Discr_Vals : Elist_Id;
6832 -- List of inferred discriminant values for right operand.
6833
6834 Discr : Entity_Id;
5d09245e
AC
6835
6836 begin
fa1608c2
ES
6837 Lhs_Discr_Vals := New_Elmt_List;
6838 Rhs_Discr_Vals := New_Elmt_List;
6839
5d09245e
AC
6840 -- Per-object constrained selected components require special
6841 -- attention. If the enclosing scope of the component is an
f02b8bb8 6842 -- Unchecked_Union, we cannot reference its discriminants
fa1608c2
ES
6843 -- directly. This is why we use the extra parameters of the
6844 -- equality function of the enclosing Unchecked_Union.
5d09245e
AC
6845
6846 -- type UU_Type (Discr : Integer := 0) is
6847 -- . . .
6848 -- end record;
6849 -- pragma Unchecked_Union (UU_Type);
6850
6851 -- 1. Unchecked_Union enclosing record:
6852
6853 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6854 -- . . .
6855 -- Comp : UU_Type (Discr);
6856 -- . . .
6857 -- end Enclosing_UU_Type;
6858 -- pragma Unchecked_Union (Enclosing_UU_Type);
6859
6860 -- Obj1 : Enclosing_UU_Type;
6861 -- Obj2 : Enclosing_UU_Type (1);
6862
2717634d 6863 -- [. . .] Obj1 = Obj2 [. . .]
5d09245e
AC
6864
6865 -- Generated code:
6866
6867 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6868
6869 -- A and B are the formal parameters of the equality function
6870 -- of Enclosing_UU_Type. The function always has two extra
fa1608c2
ES
6871 -- formals to capture the inferred discriminant values for
6872 -- each discriminant of the type.
5d09245e
AC
6873
6874 -- 2. Non-Unchecked_Union enclosing record:
6875
6876 -- type
6877 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6878 -- is record
6879 -- . . .
6880 -- Comp : UU_Type (Discr);
6881 -- . . .
6882 -- end Enclosing_Non_UU_Type;
6883
6884 -- Obj1 : Enclosing_Non_UU_Type;
6885 -- Obj2 : Enclosing_Non_UU_Type (1);
6886
630d30e9 6887 -- ... Obj1 = Obj2 ...
5d09245e
AC
6888
6889 -- Generated code:
6890
6891 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6892 -- obj1.discr, obj2.discr)) then
6893
6894 -- In this case we can directly reference the discriminants of
6895 -- the enclosing record.
6896
fa1608c2 6897 -- Process left operand of equality
5d09245e
AC
6898
6899 if Nkind (Lhs) = N_Selected_Component
533369aa
AC
6900 and then
6901 Has_Per_Object_Constraint (Entity (Selector_Name (Lhs)))
5d09245e 6902 then
fa1608c2
ES
6903 -- If enclosing record is an Unchecked_Union, use formals
6904 -- corresponding to each discriminant. The name of the
6905 -- formal is that of the discriminant, with added suffix,
6906 -- see Exp_Ch3.Build_Record_Equality for details.
5d09245e 6907
dda38714 6908 if Is_Unchecked_Union (Scope (Entity (Selector_Name (Lhs))))
5d09245e 6909 then
fa1608c2
ES
6910 Discr :=
6911 First_Discriminant
6912 (Scope (Entity (Selector_Name (Lhs))));
6913 while Present (Discr) loop
cc6f5d75
AC
6914 Append_Elmt
6915 (Make_Identifier (Loc,
6916 Chars => New_External_Name (Chars (Discr), 'A')),
6917 To => Lhs_Discr_Vals);
fa1608c2
ES
6918 Next_Discriminant (Discr);
6919 end loop;
5d09245e 6920
fa1608c2
ES
6921 -- If enclosing record is of a non-Unchecked_Union type, it
6922 -- is possible to reference its discriminants directly.
5d09245e
AC
6923
6924 else
fa1608c2
ES
6925 Discr := First_Discriminant (Lhs_Type);
6926 while Present (Discr) loop
cc6f5d75
AC
6927 Append_Elmt
6928 (Make_Selected_Component (Loc,
6929 Prefix => Prefix (Lhs),
6930 Selector_Name =>
6931 New_Copy
6932 (Get_Discriminant_Value (Discr,
6933 Lhs_Type,
6934 Stored_Constraint (Lhs_Type)))),
6935 To => Lhs_Discr_Vals);
fa1608c2
ES
6936 Next_Discriminant (Discr);
6937 end loop;
5d09245e
AC
6938 end if;
6939
fa1608c2
ES
6940 -- Otherwise operand is on object with a constrained type.
6941 -- Infer the discriminant values from the constraint.
5d09245e
AC
6942
6943 else
fa1608c2
ES
6944
6945 Discr := First_Discriminant (Lhs_Type);
6946 while Present (Discr) loop
cc6f5d75
AC
6947 Append_Elmt
6948 (New_Copy
6949 (Get_Discriminant_Value (Discr,
fa1608c2
ES
6950 Lhs_Type,
6951 Stored_Constraint (Lhs_Type))),
cc6f5d75 6952 To => Lhs_Discr_Vals);
fa1608c2
ES
6953 Next_Discriminant (Discr);
6954 end loop;
5d09245e
AC
6955 end if;
6956
fa1608c2 6957 -- Similar processing for right operand of equality
5d09245e
AC
6958
6959 if Nkind (Rhs) = N_Selected_Component
533369aa
AC
6960 and then
6961 Has_Per_Object_Constraint (Entity (Selector_Name (Rhs)))
5d09245e 6962 then
5e1c00fa 6963 if Is_Unchecked_Union
cc6f5d75 6964 (Scope (Entity (Selector_Name (Rhs))))
5d09245e 6965 then
fa1608c2
ES
6966 Discr :=
6967 First_Discriminant
6968 (Scope (Entity (Selector_Name (Rhs))));
6969 while Present (Discr) loop
cc6f5d75
AC
6970 Append_Elmt
6971 (Make_Identifier (Loc,
6972 Chars => New_External_Name (Chars (Discr), 'B')),
6973 To => Rhs_Discr_Vals);
fa1608c2
ES
6974 Next_Discriminant (Discr);
6975 end loop;
5d09245e
AC
6976
6977 else
fa1608c2
ES
6978 Discr := First_Discriminant (Rhs_Type);
6979 while Present (Discr) loop
cc6f5d75
AC
6980 Append_Elmt
6981 (Make_Selected_Component (Loc,
6982 Prefix => Prefix (Rhs),
6983 Selector_Name =>
6984 New_Copy (Get_Discriminant_Value
6985 (Discr,
6986 Rhs_Type,
6987 Stored_Constraint (Rhs_Type)))),
6988 To => Rhs_Discr_Vals);
fa1608c2
ES
6989 Next_Discriminant (Discr);
6990 end loop;
5d09245e 6991 end if;
5d09245e 6992
fa1608c2
ES
6993 else
6994 Discr := First_Discriminant (Rhs_Type);
6995 while Present (Discr) loop
cc6f5d75
AC
6996 Append_Elmt
6997 (New_Copy (Get_Discriminant_Value
6998 (Discr,
6999 Rhs_Type,
7000 Stored_Constraint (Rhs_Type))),
7001 To => Rhs_Discr_Vals);
fa1608c2
ES
7002 Next_Discriminant (Discr);
7003 end loop;
5d09245e
AC
7004 end if;
7005
fa1608c2
ES
7006 -- Now merge the list of discriminant values so that values
7007 -- of corresponding discriminants are adjacent.
7008
7009 declare
7010 Params : List_Id;
7011 L_Elmt : Elmt_Id;
7012 R_Elmt : Elmt_Id;
7013
7014 begin
7015 Params := New_List (L_Exp, R_Exp);
7016 L_Elmt := First_Elmt (Lhs_Discr_Vals);
7017 R_Elmt := First_Elmt (Rhs_Discr_Vals);
7018 while Present (L_Elmt) loop
7019 Append_To (Params, Node (L_Elmt));
7020 Append_To (Params, Node (R_Elmt));
7021 Next_Elmt (L_Elmt);
7022 Next_Elmt (R_Elmt);
7023 end loop;
7024
7025 Rewrite (N,
7026 Make_Function_Call (Loc,
e4494292 7027 Name => New_Occurrence_Of (Eq, Loc),
fa1608c2
ES
7028 Parameter_Associations => Params));
7029 end;
5d09245e
AC
7030 end;
7031
7032 -- Normal case, not an unchecked union
7033
7034 else
7035 Rewrite (N,
7036 Make_Function_Call (Loc,
e4494292 7037 Name => New_Occurrence_Of (Eq, Loc),
5d09245e
AC
7038 Parameter_Associations => New_List (L_Exp, R_Exp)));
7039 end if;
70482933
RK
7040
7041 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7042 end Build_Equality_Call;
7043
5d09245e
AC
7044 ------------------------------------
7045 -- Has_Unconstrained_UU_Component --
7046 ------------------------------------
7047
7048 function Has_Unconstrained_UU_Component
7049 (Typ : Node_Id) return Boolean
7050 is
7051 Tdef : constant Node_Id :=
57848bf7 7052 Type_Definition (Declaration_Node (Base_Type (Typ)));
5d09245e
AC
7053 Clist : Node_Id;
7054 Vpart : Node_Id;
7055
7056 function Component_Is_Unconstrained_UU
7057 (Comp : Node_Id) return Boolean;
7058 -- Determines whether the subtype of the component is an
7059 -- unconstrained Unchecked_Union.
7060
7061 function Variant_Is_Unconstrained_UU
7062 (Variant : Node_Id) return Boolean;
7063 -- Determines whether a component of the variant has an unconstrained
7064 -- Unchecked_Union subtype.
7065
7066 -----------------------------------
7067 -- Component_Is_Unconstrained_UU --
7068 -----------------------------------
7069
7070 function Component_Is_Unconstrained_UU
7071 (Comp : Node_Id) return Boolean
7072 is
7073 begin
7074 if Nkind (Comp) /= N_Component_Declaration then
7075 return False;
7076 end if;
7077
7078 declare
7079 Sindic : constant Node_Id :=
7080 Subtype_Indication (Component_Definition (Comp));
7081
7082 begin
7083 -- Unconstrained nominal type. In the case of a constraint
7084 -- present, the node kind would have been N_Subtype_Indication.
7085
7086 if Nkind (Sindic) = N_Identifier then
7087 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
7088 end if;
7089
7090 return False;
7091 end;
7092 end Component_Is_Unconstrained_UU;
7093
7094 ---------------------------------
7095 -- Variant_Is_Unconstrained_UU --
7096 ---------------------------------
7097
7098 function Variant_Is_Unconstrained_UU
7099 (Variant : Node_Id) return Boolean
7100 is
7101 Clist : constant Node_Id := Component_List (Variant);
7102
7103 begin
7104 if Is_Empty_List (Component_Items (Clist)) then
7105 return False;
7106 end if;
7107
f02b8bb8
RD
7108 -- We only need to test one component
7109
5d09245e
AC
7110 declare
7111 Comp : Node_Id := First (Component_Items (Clist));
7112
7113 begin
7114 while Present (Comp) loop
5d09245e
AC
7115 if Component_Is_Unconstrained_UU (Comp) then
7116 return True;
7117 end if;
7118
7119 Next (Comp);
7120 end loop;
7121 end;
7122
7123 -- None of the components withing the variant were of
7124 -- unconstrained Unchecked_Union type.
7125
7126 return False;
7127 end Variant_Is_Unconstrained_UU;
7128
7129 -- Start of processing for Has_Unconstrained_UU_Component
7130
7131 begin
7132 if Null_Present (Tdef) then
7133 return False;
7134 end if;
7135
7136 Clist := Component_List (Tdef);
7137 Vpart := Variant_Part (Clist);
7138
7139 -- Inspect available components
7140
7141 if Present (Component_Items (Clist)) then
7142 declare
7143 Comp : Node_Id := First (Component_Items (Clist));
7144
7145 begin
7146 while Present (Comp) loop
7147
8fc789c8 7148 -- One component is sufficient
5d09245e
AC
7149
7150 if Component_Is_Unconstrained_UU (Comp) then
7151 return True;
7152 end if;
7153
7154 Next (Comp);
7155 end loop;
7156 end;
7157 end if;
7158
7159 -- Inspect available components withing variants
7160
7161 if Present (Vpart) then
7162 declare
7163 Variant : Node_Id := First (Variants (Vpart));
7164
7165 begin
7166 while Present (Variant) loop
7167
8fc789c8 7168 -- One component within a variant is sufficient
5d09245e
AC
7169
7170 if Variant_Is_Unconstrained_UU (Variant) then
7171 return True;
7172 end if;
7173
7174 Next (Variant);
7175 end loop;
7176 end;
7177 end if;
7178
7179 -- Neither the available components, nor the components inside the
7180 -- variant parts were of an unconstrained Unchecked_Union subtype.
7181
7182 return False;
7183 end Has_Unconstrained_UU_Component;
7184
70482933
RK
7185 -- Start of processing for Expand_N_Op_Eq
7186
7187 begin
7188 Binary_Op_Validity_Checks (N);
7189
456cbfa5
AC
7190 -- Deal with private types
7191
70482933
RK
7192 if Ekind (Typl) = E_Private_Type then
7193 Typl := Underlying_Type (Typl);
70482933
RK
7194 elsif Ekind (Typl) = E_Private_Subtype then
7195 Typl := Underlying_Type (Base_Type (Typl));
f02b8bb8
RD
7196 else
7197 null;
70482933
RK
7198 end if;
7199
7200 -- It may happen in error situations that the underlying type is not
7201 -- set. The error will be detected later, here we just defend the
7202 -- expander code.
7203
7204 if No (Typl) then
7205 return;
7206 end if;
7207
a92230c5
AC
7208 -- Now get the implementation base type (note that plain Base_Type here
7209 -- might lead us back to the private type, which is not what we want!)
7210
7211 Typl := Implementation_Base_Type (Typl);
70482933 7212
dda38714
AC
7213 -- Equality between variant records results in a call to a routine
7214 -- that has conditional tests of the discriminant value(s), and hence
7215 -- violates the No_Implicit_Conditionals restriction.
7216
7217 if Has_Variant_Part (Typl) then
7218 declare
7219 Msg : Boolean;
7220
7221 begin
7222 Check_Restriction (Msg, No_Implicit_Conditionals, N);
7223
7224 if Msg then
7225 Error_Msg_N
7226 ("\comparison of variant records tests discriminants", N);
7227 return;
7228 end if;
7229 end;
7230 end if;
7231
456cbfa5 7232 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
60b68e56 7233 -- means we no longer have a comparison operation, we are all done.
456cbfa5
AC
7234
7235 Expand_Compare_Minimize_Eliminate_Overflow (N);
7236
7237 if Nkind (N) /= N_Op_Eq then
7238 return;
7239 end if;
7240
70482933
RK
7241 -- Boolean types (requiring handling of non-standard case)
7242
f02b8bb8 7243 if Is_Boolean_Type (Typl) then
70482933
RK
7244 Adjust_Condition (Left_Opnd (N));
7245 Adjust_Condition (Right_Opnd (N));
7246 Set_Etype (N, Standard_Boolean);
7247 Adjust_Result_Type (N, Typ);
7248
7249 -- Array types
7250
7251 elsif Is_Array_Type (Typl) then
7252
1033834f
RD
7253 -- If we are doing full validity checking, and it is possible for the
7254 -- array elements to be invalid then expand out array comparisons to
7255 -- make sure that we check the array elements.
fbf5a39b 7256
1033834f
RD
7257 if Validity_Check_Operands
7258 and then not Is_Known_Valid (Component_Type (Typl))
7259 then
fbf5a39b
AC
7260 declare
7261 Save_Force_Validity_Checks : constant Boolean :=
7262 Force_Validity_Checks;
7263 begin
7264 Force_Validity_Checks := True;
7265 Rewrite (N,
0da2c8ac
AC
7266 Expand_Array_Equality
7267 (N,
7268 Relocate_Node (Lhs),
7269 Relocate_Node (Rhs),
7270 Bodies,
7271 Typl));
7272 Insert_Actions (N, Bodies);
fbf5a39b
AC
7273 Analyze_And_Resolve (N, Standard_Boolean);
7274 Force_Validity_Checks := Save_Force_Validity_Checks;
7275 end;
7276
a9d8907c 7277 -- Packed case where both operands are known aligned
70482933 7278
a9d8907c
JM
7279 elsif Is_Bit_Packed_Array (Typl)
7280 and then not Is_Possibly_Unaligned_Object (Lhs)
7281 and then not Is_Possibly_Unaligned_Object (Rhs)
7282 then
70482933
RK
7283 Expand_Packed_Eq (N);
7284
5e1c00fa
RD
7285 -- Where the component type is elementary we can use a block bit
7286 -- comparison (if supported on the target) exception in the case
7287 -- of floating-point (negative zero issues require element by
f280dd8f 7288 -- element comparison), and atomic/VFA types (where we must be sure
a9d8907c 7289 -- to load elements independently) and possibly unaligned arrays.
70482933 7290
70482933
RK
7291 elsif Is_Elementary_Type (Component_Type (Typl))
7292 and then not Is_Floating_Point_Type (Component_Type (Typl))
f280dd8f 7293 and then not Is_Atomic_Or_VFA (Component_Type (Typl))
a9d8907c
JM
7294 and then not Is_Possibly_Unaligned_Object (Lhs)
7295 and then not Is_Possibly_Unaligned_Object (Rhs)
fbf5a39b 7296 and then Support_Composite_Compare_On_Target
70482933
RK
7297 then
7298 null;
7299
685094bf
RD
7300 -- For composite and floating-point cases, expand equality loop to
7301 -- make sure of using proper comparisons for tagged types, and
7302 -- correctly handling the floating-point case.
70482933
RK
7303
7304 else
7305 Rewrite (N,
0da2c8ac
AC
7306 Expand_Array_Equality
7307 (N,
7308 Relocate_Node (Lhs),
7309 Relocate_Node (Rhs),
7310 Bodies,
7311 Typl));
70482933
RK
7312 Insert_Actions (N, Bodies, Suppress => All_Checks);
7313 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7314 end if;
7315
7316 -- Record Types
7317
7318 elsif Is_Record_Type (Typl) then
7319
7320 -- For tagged types, use the primitive "="
7321
7322 if Is_Tagged_Type (Typl) then
7323
0669bebe
GB
7324 -- No need to do anything else compiling under restriction
7325 -- No_Dispatching_Calls. During the semantic analysis we
7326 -- already notified such violation.
7327
7328 if Restriction_Active (No_Dispatching_Calls) then
7329 return;
7330 end if;
7331
685094bf
RD
7332 -- If this is derived from an untagged private type completed with
7333 -- a tagged type, it does not have a full view, so we use the
7334 -- primitive operations of the private type. This check should no
7335 -- longer be necessary when these types get their full views???
70482933
RK
7336
7337 if Is_Private_Type (A_Typ)
7338 and then not Is_Tagged_Type (A_Typ)
7339 and then Is_Derived_Type (A_Typ)
7340 and then No (Full_View (A_Typ))
7341 then
685094bf
RD
7342 -- Search for equality operation, checking that the operands
7343 -- have the same type. Note that we must find a matching entry,
a90bd866 7344 -- or something is very wrong.
2e071734 7345
70482933
RK
7346 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
7347
2e071734
AC
7348 while Present (Prim) loop
7349 exit when Chars (Node (Prim)) = Name_Op_Eq
7350 and then Etype (First_Formal (Node (Prim))) =
7351 Etype (Next_Formal (First_Formal (Node (Prim))))
7352 and then
7353 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7354
70482933 7355 Next_Elmt (Prim);
70482933
RK
7356 end loop;
7357
2e071734 7358 pragma Assert (Present (Prim));
70482933 7359 Op_Name := Node (Prim);
fbf5a39b
AC
7360
7361 -- Find the type's predefined equality or an overriding
3dddb11e 7362 -- user-defined equality. The reason for not simply calling
fbf5a39b 7363 -- Find_Prim_Op here is that there may be a user-defined
3dddb11e
ES
7364 -- overloaded equality op that precedes the equality that we
7365 -- want, so we have to explicitly search (e.g., there could be
7366 -- an equality with two different parameter types).
fbf5a39b 7367
70482933 7368 else
fbf5a39b 7369 if Is_Class_Wide_Type (Typl) then
3dddb11e 7370 Typl := Find_Specific_Type (Typl);
fbf5a39b
AC
7371 end if;
7372
7373 Prim := First_Elmt (Primitive_Operations (Typl));
fbf5a39b
AC
7374 while Present (Prim) loop
7375 exit when Chars (Node (Prim)) = Name_Op_Eq
7376 and then Etype (First_Formal (Node (Prim))) =
7377 Etype (Next_Formal (First_Formal (Node (Prim))))
12e0c41c
AC
7378 and then
7379 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
fbf5a39b
AC
7380
7381 Next_Elmt (Prim);
fbf5a39b
AC
7382 end loop;
7383
2e071734 7384 pragma Assert (Present (Prim));
fbf5a39b 7385 Op_Name := Node (Prim);
70482933
RK
7386 end if;
7387
7388 Build_Equality_Call (Op_Name);
7389
5d09245e
AC
7390 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7391 -- predefined equality operator for a type which has a subcomponent
7392 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7393
7394 elsif Has_Unconstrained_UU_Component (Typl) then
7395 Insert_Action (N,
7396 Make_Raise_Program_Error (Loc,
7397 Reason => PE_Unchecked_Union_Restriction));
7398
7399 -- Prevent Gigi from generating incorrect code by rewriting the
6cb3037c 7400 -- equality as a standard False. (is this documented somewhere???)
5d09245e
AC
7401
7402 Rewrite (N,
7403 New_Occurrence_Of (Standard_False, Loc));
7404
7405 elsif Is_Unchecked_Union (Typl) then
7406
7407 -- If we can infer the discriminants of the operands, we make a
7408 -- call to the TSS equality function.
7409
7410 if Has_Inferable_Discriminants (Lhs)
7411 and then
7412 Has_Inferable_Discriminants (Rhs)
7413 then
7414 Build_Equality_Call
7415 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7416
7417 else
7418 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7419 -- the predefined equality operator for an Unchecked_Union type
7420 -- if either of the operands lack inferable discriminants.
7421
7422 Insert_Action (N,
7423 Make_Raise_Program_Error (Loc,
7424 Reason => PE_Unchecked_Union_Restriction));
7425
29ad9ea5
AC
7426 -- Emit a warning on source equalities only, otherwise the
7427 -- message may appear out of place due to internal use. The
7428 -- warning is unconditional because it is required by the
7429 -- language.
7430
7431 if Comes_From_Source (N) then
7432 Error_Msg_N
facfa165 7433 ("Unchecked_Union discriminants cannot be determined??",
29ad9ea5
AC
7434 N);
7435 Error_Msg_N
facfa165 7436 ("\Program_Error will be raised for equality operation??",
29ad9ea5
AC
7437 N);
7438 end if;
7439
5d09245e 7440 -- Prevent Gigi from generating incorrect code by rewriting
6cb3037c 7441 -- the equality as a standard False (documented where???).
5d09245e
AC
7442
7443 Rewrite (N,
7444 New_Occurrence_Of (Standard_False, Loc));
5d09245e
AC
7445 end if;
7446
70482933
RK
7447 -- If a type support function is present (for complex cases), use it
7448
fbf5a39b
AC
7449 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
7450 Build_Equality_Call
7451 (TSS (Root_Type (Typl), TSS_Composite_Equality));
70482933 7452
8d80ff64
AC
7453 -- When comparing two Bounded_Strings, use the primitive equality of
7454 -- the root Super_String type.
7455
7456 elsif Is_Bounded_String (Typl) then
7457 Prim :=
7458 First_Elmt (Collect_Primitive_Operations (Root_Type (Typl)));
7459
7460 while Present (Prim) loop
7461 exit when Chars (Node (Prim)) = Name_Op_Eq
7462 and then Etype (First_Formal (Node (Prim))) =
7463 Etype (Next_Formal (First_Formal (Node (Prim))))
7464 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7465
7466 Next_Elmt (Prim);
7467 end loop;
7468
7469 -- A Super_String type should always have a primitive equality
7470
7471 pragma Assert (Present (Prim));
7472 Build_Equality_Call (Node (Prim));
7473
70482933 7474 -- Otherwise expand the component by component equality. Note that
8fc789c8 7475 -- we never use block-bit comparisons for records, because of the
70482933
RK
7476 -- problems with gaps. The backend will often be able to recombine
7477 -- the separate comparisons that we generate here.
7478
7479 else
7480 Remove_Side_Effects (Lhs);
7481 Remove_Side_Effects (Rhs);
7482 Rewrite (N,
7483 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
7484
7485 Insert_Actions (N, Bodies, Suppress => All_Checks);
7486 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7487 end if;
7488 end if;
7489
d26dc4b5 7490 -- Test if result is known at compile time
70482933 7491
d26dc4b5 7492 Rewrite_Comparison (N);
f02b8bb8 7493
878e58c8
RD
7494 -- Special optimization of length comparison
7495
0580d807 7496 Optimize_Length_Comparison (N);
878e58c8 7497
088c7e1b 7498 -- One more special case: if we have a comparison of X'Result = expr
878e58c8 7499 -- in floating-point, then if not already there, change expr to be
088c7e1b 7500 -- f'Machine (expr) to eliminate surprise from extra precision.
878e58c8
RD
7501
7502 if Is_Floating_Point_Type (Typl)
7503 and then Nkind (Original_Node (Lhs)) = N_Attribute_Reference
7504 and then Attribute_Name (Original_Node (Lhs)) = Name_Result
7505 then
7506 -- Stick in the Typ'Machine call if not already there
7507
7508 if Nkind (Rhs) /= N_Attribute_Reference
7509 or else Attribute_Name (Rhs) /= Name_Machine
7510 then
7511 Rewrite (Rhs,
7512 Make_Attribute_Reference (Loc,
7513 Prefix => New_Occurrence_Of (Typl, Loc),
7514 Attribute_Name => Name_Machine,
7515 Expressions => New_List (Relocate_Node (Rhs))));
7516 Analyze_And_Resolve (Rhs, Typl);
7517 end if;
7518 end if;
70482933
RK
7519 end Expand_N_Op_Eq;
7520
7521 -----------------------
7522 -- Expand_N_Op_Expon --
7523 -----------------------
7524
7525 procedure Expand_N_Op_Expon (N : Node_Id) is
7526 Loc : constant Source_Ptr := Sloc (N);
7527 Typ : constant Entity_Id := Etype (N);
7528 Rtyp : constant Entity_Id := Root_Type (Typ);
7529 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
07fc65c4 7530 Bastyp : constant Node_Id := Etype (Base);
70482933
RK
7531 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
7532 Exptyp : constant Entity_Id := Etype (Exp);
7533 Ovflo : constant Boolean := Do_Overflow_Check (N);
7534 Expv : Uint;
70482933
RK
7535 Temp : Node_Id;
7536 Rent : RE_Id;
7537 Ent : Entity_Id;
fbf5a39b 7538 Etyp : Entity_Id;
cb42ba5d 7539 Xnode : Node_Id;
70482933 7540
83496138
AC
7541 function Wrap_MA (Exp : Node_Id) return Node_Id;
7542 -- Given an expression Exp, if the root type is Float or Long_Float,
7543 -- then wrap the expression in a call of Bastyp'Machine, to stop any
7544 -- extra precision. This is done to ensure that X**A = X**B when A is
7545 -- a static constant and B is a variable with the same value. For any
7546 -- other type, the node Exp is returned unchanged.
7547
7548 -------------
7549 -- Wrap_MA --
7550 -------------
7551
7552 function Wrap_MA (Exp : Node_Id) return Node_Id is
7553 Loc : constant Source_Ptr := Sloc (Exp);
7554 begin
7555 if Rtyp = Standard_Float or else Rtyp = Standard_Long_Float then
7556 return
7557 Make_Attribute_Reference (Loc,
7558 Attribute_Name => Name_Machine,
7559 Prefix => New_Occurrence_Of (Bastyp, Loc),
7560 Expressions => New_List (Relocate_Node (Exp)));
7561 else
7562 return Exp;
7563 end if;
7564 end Wrap_MA;
7565
7566 -- Start of processing for Expand_N_Op
7567
70482933
RK
7568 begin
7569 Binary_Op_Validity_Checks (N);
7570
5114f3ff 7571 -- CodePeer wants to see the unexpanded N_Op_Expon node
8f66cda7 7572
5114f3ff 7573 if CodePeer_Mode then
8f66cda7
AC
7574 return;
7575 end if;
7576
685094bf
RD
7577 -- If either operand is of a private type, then we have the use of an
7578 -- intrinsic operator, and we get rid of the privateness, by using root
7579 -- types of underlying types for the actual operation. Otherwise the
7580 -- private types will cause trouble if we expand multiplications or
7581 -- shifts etc. We also do this transformation if the result type is
7582 -- different from the base type.
07fc65c4
GB
7583
7584 if Is_Private_Type (Etype (Base))
8f66cda7
AC
7585 or else Is_Private_Type (Typ)
7586 or else Is_Private_Type (Exptyp)
7587 or else Rtyp /= Root_Type (Bastyp)
07fc65c4
GB
7588 then
7589 declare
7590 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
7591 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
07fc65c4
GB
7592 begin
7593 Rewrite (N,
7594 Unchecked_Convert_To (Typ,
7595 Make_Op_Expon (Loc,
7596 Left_Opnd => Unchecked_Convert_To (Bt, Base),
7597 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
7598 Analyze_And_Resolve (N, Typ);
7599 return;
7600 end;
7601 end if;
7602
b6b5cca8 7603 -- Check for MINIMIZED/ELIMINATED overflow mode
6cb3037c 7604
b6b5cca8 7605 if Minimized_Eliminated_Overflow_Check (N) then
6cb3037c
AC
7606 Apply_Arithmetic_Overflow_Check (N);
7607 return;
7608 end if;
7609
cb42ba5d
AC
7610 -- Test for case of known right argument where we can replace the
7611 -- exponentiation by an equivalent expression using multiplication.
70482933 7612
6c3c671e
AC
7613 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
7614 -- configurable run-time mode, we may not have the exponentiation
7615 -- routine available, and we don't want the legality of the program
7616 -- to depend on how clever the compiler is in knowing values.
7617
7618 if CRT_Safe_Compile_Time_Known_Value (Exp) then
70482933
RK
7619 Expv := Expr_Value (Exp);
7620
7621 -- We only fold small non-negative exponents. You might think we
7622 -- could fold small negative exponents for the real case, but we
7623 -- can't because we are required to raise Constraint_Error for
7624 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
83496138 7625 -- See ACVC test C4A012B, and it is not worth generating the test.
70482933
RK
7626
7627 if Expv >= 0 and then Expv <= 4 then
7628
7629 -- X ** 0 = 1 (or 1.0)
7630
7631 if Expv = 0 then
abcbd24c
ST
7632
7633 -- Call Remove_Side_Effects to ensure that any side effects
7634 -- in the ignored left operand (in particular function calls
7635 -- to user defined functions) are properly executed.
7636
7637 Remove_Side_Effects (Base);
7638
70482933
RK
7639 if Ekind (Typ) in Integer_Kind then
7640 Xnode := Make_Integer_Literal (Loc, Intval => 1);
7641 else
7642 Xnode := Make_Real_Literal (Loc, Ureal_1);
7643 end if;
7644
7645 -- X ** 1 = X
7646
7647 elsif Expv = 1 then
7648 Xnode := Base;
7649
7650 -- X ** 2 = X * X
7651
7652 elsif Expv = 2 then
7653 Xnode :=
83496138
AC
7654 Wrap_MA (
7655 Make_Op_Multiply (Loc,
7656 Left_Opnd => Duplicate_Subexpr (Base),
7657 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)));
70482933
RK
7658
7659 -- X ** 3 = X * X * X
7660
7661 elsif Expv = 3 then
7662 Xnode :=
83496138
AC
7663 Wrap_MA (
7664 Make_Op_Multiply (Loc,
7665 Left_Opnd =>
7666 Make_Op_Multiply (Loc,
7667 Left_Opnd => Duplicate_Subexpr (Base),
7668 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
7669 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)));
70482933
RK
7670
7671 -- X ** 4 ->
cb42ba5d
AC
7672
7673 -- do
70482933 7674 -- En : constant base'type := base * base;
cb42ba5d 7675 -- in
70482933
RK
7676 -- En * En
7677
cb42ba5d
AC
7678 else
7679 pragma Assert (Expv = 4);
191fcb3a 7680 Temp := Make_Temporary (Loc, 'E', Base);
70482933 7681
cb42ba5d
AC
7682 Xnode :=
7683 Make_Expression_With_Actions (Loc,
7684 Actions => New_List (
7685 Make_Object_Declaration (Loc,
7686 Defining_Identifier => Temp,
7687 Constant_Present => True,
e4494292 7688 Object_Definition => New_Occurrence_Of (Typ, Loc),
cb42ba5d 7689 Expression =>
83496138
AC
7690 Wrap_MA (
7691 Make_Op_Multiply (Loc,
7692 Left_Opnd =>
7693 Duplicate_Subexpr (Base),
7694 Right_Opnd =>
7695 Duplicate_Subexpr_No_Checks (Base))))),
cb42ba5d 7696
70482933 7697 Expression =>
83496138
AC
7698 Wrap_MA (
7699 Make_Op_Multiply (Loc,
7700 Left_Opnd => New_Occurrence_Of (Temp, Loc),
7701 Right_Opnd => New_Occurrence_Of (Temp, Loc))));
70482933
RK
7702 end if;
7703
7704 Rewrite (N, Xnode);
7705 Analyze_And_Resolve (N, Typ);
7706 return;
7707 end if;
7708 end if;
7709
b502ba3c 7710 -- Deal with optimizing 2 ** expression to shift where possible
685094bf 7711
8b4230c8
AC
7712 -- Note: we used to check that Exptyp was an unsigned type. But that is
7713 -- an unnecessary check, since if Exp is negative, we have a run-time
7714 -- error that is either caught (so we get the right result) or we have
7715 -- suppressed the check, in which case the code is erroneous anyway.
7716
b502ba3c
RD
7717 if Is_Integer_Type (Rtyp)
7718
c2b2b2d7 7719 -- The base value must be "safe compile-time known", and exactly 2
b502ba3c
RD
7720
7721 and then Nkind (Base) = N_Integer_Literal
6c3c671e
AC
7722 and then CRT_Safe_Compile_Time_Known_Value (Base)
7723 and then Expr_Value (Base) = Uint_2
b502ba3c
RD
7724
7725 -- We only handle cases where the right type is a integer
7726
70482933
RK
7727 and then Is_Integer_Type (Root_Type (Exptyp))
7728 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
b502ba3c
RD
7729
7730 -- This transformation is not applicable for a modular type with a
a95f708e 7731 -- nonbinary modulus because we do not handle modular reduction in
b502ba3c
RD
7732 -- a correct manner if we attempt this transformation in this case.
7733
7734 and then not Non_Binary_Modulus (Typ)
70482933 7735 then
b502ba3c
RD
7736 -- Handle the cases where our parent is a division or multiplication
7737 -- specially. In these cases we can convert to using a shift at the
7738 -- parent level if we are not doing overflow checking, since it is
7739 -- too tricky to combine the overflow check at the parent level.
70482933 7740
b502ba3c
RD
7741 if not Ovflo
7742 and then Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply)
7743 then
51bf9bdf
AC
7744 declare
7745 P : constant Node_Id := Parent (N);
7746 L : constant Node_Id := Left_Opnd (P);
7747 R : constant Node_Id := Right_Opnd (P);
7748
7749 begin
7750 if (Nkind (P) = N_Op_Multiply
eb9008b7
AC
7751 and then
7752 ((Is_Integer_Type (Etype (L)) and then R = N)
7753 or else
7754 (Is_Integer_Type (Etype (R)) and then L = N))
7755 and then not Do_Overflow_Check (P))
7756
51bf9bdf
AC
7757 or else
7758 (Nkind (P) = N_Op_Divide
533369aa
AC
7759 and then Is_Integer_Type (Etype (L))
7760 and then Is_Unsigned_Type (Etype (L))
7761 and then R = N
7762 and then not Do_Overflow_Check (P))
51bf9bdf
AC
7763 then
7764 Set_Is_Power_Of_2_For_Shift (N);
7765 return;
7766 end if;
7767 end;
7768
b502ba3c
RD
7769 -- Here we just have 2 ** N on its own, so we can convert this to a
7770 -- shift node. We are prepared to deal with overflow here, and we
7771 -- also have to handle proper modular reduction for binary modular.
51bf9bdf 7772
b502ba3c
RD
7773 else
7774 declare
7775 OK : Boolean;
7776 Lo : Uint;
7777 Hi : Uint;
7778
7779 MaxS : Uint;
7780 -- Maximum shift count with no overflow
7781
7782 TestS : Boolean;
7783 -- Set True if we must test the shift count
7784
5389e4ae
RD
7785 Test_Gt : Node_Id;
7786 -- Node for test against TestS
7787
b502ba3c
RD
7788 begin
7789 -- Compute maximum shift based on the underlying size. For a
7790 -- modular type this is one less than the size.
7791
7792 if Is_Modular_Integer_Type (Typ) then
7793
7794 -- For modular integer types, this is the size of the value
7795 -- being shifted minus one. Any larger values will cause
7796 -- modular reduction to a result of zero. Note that we do
7797 -- want the RM_Size here (e.g. mod 2 ** 7, we want a result
7798 -- of 6, since 2**7 should be reduced to zero).
7799
7800 MaxS := RM_Size (Rtyp) - 1;
7801
7802 -- For signed integer types, we use the size of the value
7803 -- being shifted minus 2. Larger values cause overflow.
7804
7805 else
7806 MaxS := Esize (Rtyp) - 2;
7807 end if;
7808
7809 -- Determine range to see if it can be larger than MaxS
7810
7811 Determine_Range
7812 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
7813 TestS := (not OK) or else Hi > MaxS;
7814
7815 -- Signed integer case
7816
7817 if Is_Signed_Integer_Type (Typ) then
7818
7819 -- Generate overflow check if overflow is active. Note that
7820 -- we can simply ignore the possibility of overflow if the
7821 -- flag is not set (means that overflow cannot happen or
7822 -- that overflow checks are suppressed).
7823
7824 if Ovflo and TestS then
7825 Insert_Action (N,
7826 Make_Raise_Constraint_Error (Loc,
7827 Condition =>
7828 Make_Op_Gt (Loc,
7829 Left_Opnd => Duplicate_Subexpr (Right_Opnd (N)),
7830 Right_Opnd => Make_Integer_Literal (Loc, MaxS)),
7831 Reason => CE_Overflow_Check_Failed));
7832 end if;
7833
7834 -- Now rewrite node as Shift_Left (1, right-operand)
7835
7836 Rewrite (N,
7837 Make_Op_Shift_Left (Loc,
7838 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
7839 Right_Opnd => Right_Opnd (N)));
7840
7841 -- Modular integer case
7842
7843 else pragma Assert (Is_Modular_Integer_Type (Typ));
7844
7845 -- If shift count can be greater than MaxS, we need to wrap
7846 -- the shift in a test that will reduce the result value to
7847 -- zero if this shift count is exceeded.
7848
7849 if TestS then
5389e4ae
RD
7850
7851 -- Note: build node for the comparison first, before we
7852 -- reuse the Right_Opnd, so that we have proper parents
7853 -- in place for the Duplicate_Subexpr call.
7854
7855 Test_Gt :=
7856 Make_Op_Gt (Loc,
7857 Left_Opnd => Duplicate_Subexpr (Right_Opnd (N)),
7858 Right_Opnd => Make_Integer_Literal (Loc, MaxS));
7859
b502ba3c
RD
7860 Rewrite (N,
7861 Make_If_Expression (Loc,
7862 Expressions => New_List (
5389e4ae 7863 Test_Gt,
b502ba3c 7864 Make_Integer_Literal (Loc, Uint_0),
b502ba3c
RD
7865 Make_Op_Shift_Left (Loc,
7866 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
7867 Right_Opnd => Right_Opnd (N)))));
7868
7869 -- If we know shift count cannot be greater than MaxS, then
7870 -- it is safe to just rewrite as a shift with no test.
7871
7872 else
7873 Rewrite (N,
7874 Make_Op_Shift_Left (Loc,
7875 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
7876 Right_Opnd => Right_Opnd (N)));
7877 end if;
7878 end if;
7879
7880 Analyze_And_Resolve (N, Typ);
7881 return;
7882 end;
51bf9bdf 7883 end if;
70482933
RK
7884 end if;
7885
07fc65c4
GB
7886 -- Fall through if exponentiation must be done using a runtime routine
7887
07fc65c4 7888 -- First deal with modular case
70482933
RK
7889
7890 if Is_Modular_Integer_Type (Rtyp) then
7891
83496138
AC
7892 -- Nonbinary modular case, we call the special exponentiation
7893 -- routine for the nonbinary case, converting the argument to
7894 -- Long_Long_Integer and passing the modulus value. Then the
7895 -- result is converted back to the base type.
70482933
RK
7896
7897 if Non_Binary_Modulus (Rtyp) then
70482933
RK
7898 Rewrite (N,
7899 Convert_To (Typ,
7900 Make_Function_Call (Loc,
cc6f5d75
AC
7901 Name =>
7902 New_Occurrence_Of (RTE (RE_Exp_Modular), Loc),
70482933 7903 Parameter_Associations => New_List (
e9daba51 7904 Convert_To (RTE (RE_Unsigned), Base),
70482933
RK
7905 Make_Integer_Literal (Loc, Modulus (Rtyp)),
7906 Exp))));
7907
83496138
AC
7908 -- Binary modular case, in this case, we call one of two routines,
7909 -- either the unsigned integer case, or the unsigned long long
7910 -- integer case, with a final "and" operation to do the required mod.
70482933
RK
7911
7912 else
7913 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
7914 Ent := RTE (RE_Exp_Unsigned);
7915 else
7916 Ent := RTE (RE_Exp_Long_Long_Unsigned);
7917 end if;
7918
7919 Rewrite (N,
7920 Convert_To (Typ,
7921 Make_Op_And (Loc,
cc6f5d75 7922 Left_Opnd =>
70482933 7923 Make_Function_Call (Loc,
cc6f5d75 7924 Name => New_Occurrence_Of (Ent, Loc),
70482933
RK
7925 Parameter_Associations => New_List (
7926 Convert_To (Etype (First_Formal (Ent)), Base),
7927 Exp)),
7928 Right_Opnd =>
7929 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
7930
7931 end if;
7932
7933 -- Common exit point for modular type case
7934
7935 Analyze_And_Resolve (N, Typ);
7936 return;
7937
fbf5a39b
AC
7938 -- Signed integer cases, done using either Integer or Long_Long_Integer.
7939 -- It is not worth having routines for Short_[Short_]Integer, since for
7940 -- most machines it would not help, and it would generate more code that
dfd99a80 7941 -- might need certification when a certified run time is required.
70482933 7942
fbf5a39b 7943 -- In the integer cases, we have two routines, one for when overflow
dfd99a80
TQ
7944 -- checks are required, and one when they are not required, since there
7945 -- is a real gain in omitting checks on many machines.
70482933 7946
fbf5a39b
AC
7947 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
7948 or else (Rtyp = Base_Type (Standard_Long_Integer)
761f7dcb
AC
7949 and then
7950 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
7951 or else Rtyp = Universal_Integer
70482933 7952 then
fbf5a39b
AC
7953 Etyp := Standard_Long_Long_Integer;
7954
ebb6b0bd
AC
7955 -- Overflow checking is the only choice on the AAMP target, where
7956 -- arithmetic instructions check overflow automatically, so only
7957 -- one version of the exponentiation unit is needed.
7958
1037b0f4 7959 if Ovflo or AAMP_On_Target then
70482933
RK
7960 Rent := RE_Exp_Long_Long_Integer;
7961 else
7962 Rent := RE_Exn_Long_Long_Integer;
7963 end if;
7964
fbf5a39b
AC
7965 elsif Is_Signed_Integer_Type (Rtyp) then
7966 Etyp := Standard_Integer;
70482933 7967
ebb6b0bd
AC
7968 -- Overflow checking is the only choice on the AAMP target, where
7969 -- arithmetic instructions check overflow automatically, so only
7970 -- one version of the exponentiation unit is needed.
7971
1037b0f4 7972 if Ovflo or AAMP_On_Target then
fbf5a39b 7973 Rent := RE_Exp_Integer;
70482933 7974 else
fbf5a39b 7975 Rent := RE_Exn_Integer;
70482933 7976 end if;
fbf5a39b 7977
83496138
AC
7978 -- Floating-point cases. We do not need separate routines for the
7979 -- overflow case here, since in the case of floating-point, we generate
7980 -- infinities anyway as a rule (either that or we automatically trap
7981 -- overflow), and if there is an infinity generated and a range check
7982 -- is required, the check will fail anyway.
7983
7984 -- Historical note: we used to convert everything to Long_Long_Float
7985 -- and call a single common routine, but this had the undesirable effect
7986 -- of giving different results for small static exponent values and the
7987 -- same dynamic values.
fbf5a39b
AC
7988
7989 else
7990 pragma Assert (Is_Floating_Point_Type (Rtyp));
83496138
AC
7991
7992 if Rtyp = Standard_Float then
7993 Etyp := Standard_Float;
7994 Rent := RE_Exn_Float;
7995
7996 elsif Rtyp = Standard_Long_Float then
7997 Etyp := Standard_Long_Float;
7998 Rent := RE_Exn_Long_Float;
7999
8000 else
8001 Etyp := Standard_Long_Long_Float;
8002 Rent := RE_Exn_Long_Long_Float;
8003 end if;
70482933
RK
8004 end if;
8005
8006 -- Common processing for integer cases and floating-point cases.
fbf5a39b 8007 -- If we are in the right type, we can call runtime routine directly
70482933 8008
fbf5a39b 8009 if Typ = Etyp
70482933
RK
8010 and then Rtyp /= Universal_Integer
8011 and then Rtyp /= Universal_Real
8012 then
8013 Rewrite (N,
83496138
AC
8014 Wrap_MA (
8015 Make_Function_Call (Loc,
8016 Name => New_Occurrence_Of (RTE (Rent), Loc),
8017 Parameter_Associations => New_List (Base, Exp))));
70482933
RK
8018
8019 -- Otherwise we have to introduce conversions (conversions are also
fbf5a39b 8020 -- required in the universal cases, since the runtime routine is
1147c704 8021 -- typed using one of the standard types).
70482933
RK
8022
8023 else
8024 Rewrite (N,
8025 Convert_To (Typ,
8026 Make_Function_Call (Loc,
e4494292 8027 Name => New_Occurrence_Of (RTE (Rent), Loc),
70482933 8028 Parameter_Associations => New_List (
fbf5a39b 8029 Convert_To (Etyp, Base),
70482933
RK
8030 Exp))));
8031 end if;
8032
8033 Analyze_And_Resolve (N, Typ);
8034 return;
8035
fbf5a39b
AC
8036 exception
8037 when RE_Not_Available =>
8038 return;
70482933
RK
8039 end Expand_N_Op_Expon;
8040
8041 --------------------
8042 -- Expand_N_Op_Ge --
8043 --------------------
8044
8045 procedure Expand_N_Op_Ge (N : Node_Id) is
8046 Typ : constant Entity_Id := Etype (N);
8047 Op1 : constant Node_Id := Left_Opnd (N);
8048 Op2 : constant Node_Id := Right_Opnd (N);
8049 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8050
8051 begin
8052 Binary_Op_Validity_Checks (N);
8053
456cbfa5 8054 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
60b68e56 8055 -- means we no longer have a comparison operation, we are all done.
456cbfa5
AC
8056
8057 Expand_Compare_Minimize_Eliminate_Overflow (N);
8058
8059 if Nkind (N) /= N_Op_Ge then
8060 return;
8061 end if;
8062
8063 -- Array type case
8064
f02b8bb8 8065 if Is_Array_Type (Typ1) then
70482933
RK
8066 Expand_Array_Comparison (N);
8067 return;
8068 end if;
8069
456cbfa5
AC
8070 -- Deal with boolean operands
8071
70482933
RK
8072 if Is_Boolean_Type (Typ1) then
8073 Adjust_Condition (Op1);
8074 Adjust_Condition (Op2);
8075 Set_Etype (N, Standard_Boolean);
8076 Adjust_Result_Type (N, Typ);
8077 end if;
8078
8079 Rewrite_Comparison (N);
f02b8bb8 8080
0580d807 8081 Optimize_Length_Comparison (N);
70482933
RK
8082 end Expand_N_Op_Ge;
8083
8084 --------------------
8085 -- Expand_N_Op_Gt --
8086 --------------------
8087
8088 procedure Expand_N_Op_Gt (N : Node_Id) is
8089 Typ : constant Entity_Id := Etype (N);
8090 Op1 : constant Node_Id := Left_Opnd (N);
8091 Op2 : constant Node_Id := Right_Opnd (N);
8092 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8093
8094 begin
8095 Binary_Op_Validity_Checks (N);
8096
456cbfa5 8097 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
60b68e56 8098 -- means we no longer have a comparison operation, we are all done.
456cbfa5
AC
8099
8100 Expand_Compare_Minimize_Eliminate_Overflow (N);
8101
8102 if Nkind (N) /= N_Op_Gt then
8103 return;
8104 end if;
8105
8106 -- Deal with array type operands
8107
f02b8bb8 8108 if Is_Array_Type (Typ1) then
70482933
RK
8109 Expand_Array_Comparison (N);
8110 return;
8111 end if;
8112
456cbfa5
AC
8113 -- Deal with boolean type operands
8114
70482933
RK
8115 if Is_Boolean_Type (Typ1) then
8116 Adjust_Condition (Op1);
8117 Adjust_Condition (Op2);
8118 Set_Etype (N, Standard_Boolean);
8119 Adjust_Result_Type (N, Typ);
8120 end if;
8121
8122 Rewrite_Comparison (N);
f02b8bb8 8123
0580d807 8124 Optimize_Length_Comparison (N);
70482933
RK
8125 end Expand_N_Op_Gt;
8126
8127 --------------------
8128 -- Expand_N_Op_Le --
8129 --------------------
8130
8131 procedure Expand_N_Op_Le (N : Node_Id) is
8132 Typ : constant Entity_Id := Etype (N);
8133 Op1 : constant Node_Id := Left_Opnd (N);
8134 Op2 : constant Node_Id := Right_Opnd (N);
8135 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8136
8137 begin
8138 Binary_Op_Validity_Checks (N);
8139
456cbfa5 8140 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
60b68e56 8141 -- means we no longer have a comparison operation, we are all done.
456cbfa5
AC
8142
8143 Expand_Compare_Minimize_Eliminate_Overflow (N);
8144
8145 if Nkind (N) /= N_Op_Le then
8146 return;
8147 end if;
8148
8149 -- Deal with array type operands
8150
f02b8bb8 8151 if Is_Array_Type (Typ1) then
70482933
RK
8152 Expand_Array_Comparison (N);
8153 return;
8154 end if;
8155
456cbfa5
AC
8156 -- Deal with Boolean type operands
8157
70482933
RK
8158 if Is_Boolean_Type (Typ1) then
8159 Adjust_Condition (Op1);
8160 Adjust_Condition (Op2);
8161 Set_Etype (N, Standard_Boolean);
8162 Adjust_Result_Type (N, Typ);
8163 end if;
8164
8165 Rewrite_Comparison (N);
f02b8bb8 8166
0580d807 8167 Optimize_Length_Comparison (N);
70482933
RK
8168 end Expand_N_Op_Le;
8169
8170 --------------------
8171 -- Expand_N_Op_Lt --
8172 --------------------
8173
8174 procedure Expand_N_Op_Lt (N : Node_Id) is
8175 Typ : constant Entity_Id := Etype (N);
8176 Op1 : constant Node_Id := Left_Opnd (N);
8177 Op2 : constant Node_Id := Right_Opnd (N);
8178 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8179
8180 begin
8181 Binary_Op_Validity_Checks (N);
8182
456cbfa5 8183 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
60b68e56 8184 -- means we no longer have a comparison operation, we are all done.
456cbfa5
AC
8185
8186 Expand_Compare_Minimize_Eliminate_Overflow (N);
8187
8188 if Nkind (N) /= N_Op_Lt then
8189 return;
8190 end if;
8191
8192 -- Deal with array type operands
8193
f02b8bb8 8194 if Is_Array_Type (Typ1) then
70482933
RK
8195 Expand_Array_Comparison (N);
8196 return;
8197 end if;
8198
456cbfa5
AC
8199 -- Deal with Boolean type operands
8200
70482933
RK
8201 if Is_Boolean_Type (Typ1) then
8202 Adjust_Condition (Op1);
8203 Adjust_Condition (Op2);
8204 Set_Etype (N, Standard_Boolean);
8205 Adjust_Result_Type (N, Typ);
8206 end if;
8207
8208 Rewrite_Comparison (N);
f02b8bb8 8209
0580d807 8210 Optimize_Length_Comparison (N);
70482933
RK
8211 end Expand_N_Op_Lt;
8212
8213 -----------------------
8214 -- Expand_N_Op_Minus --
8215 -----------------------
8216
8217 procedure Expand_N_Op_Minus (N : Node_Id) is
8218 Loc : constant Source_Ptr := Sloc (N);
8219 Typ : constant Entity_Id := Etype (N);
8220
8221 begin
8222 Unary_Op_Validity_Checks (N);
8223
b6b5cca8
AC
8224 -- Check for MINIMIZED/ELIMINATED overflow mode
8225
8226 if Minimized_Eliminated_Overflow_Check (N) then
8227 Apply_Arithmetic_Overflow_Check (N);
8228 return;
8229 end if;
8230
07fc65c4 8231 if not Backend_Overflow_Checks_On_Target
70482933
RK
8232 and then Is_Signed_Integer_Type (Etype (N))
8233 and then Do_Overflow_Check (N)
8234 then
8235 -- Software overflow checking expands -expr into (0 - expr)
8236
8237 Rewrite (N,
8238 Make_Op_Subtract (Loc,
8239 Left_Opnd => Make_Integer_Literal (Loc, 0),
8240 Right_Opnd => Right_Opnd (N)));
8241
8242 Analyze_And_Resolve (N, Typ);
70482933
RK
8243 end if;
8244 end Expand_N_Op_Minus;
8245
8246 ---------------------
8247 -- Expand_N_Op_Mod --
8248 ---------------------
8249
8250 procedure Expand_N_Op_Mod (N : Node_Id) is
8251 Loc : constant Source_Ptr := Sloc (N);
fbf5a39b 8252 Typ : constant Entity_Id := Etype (N);
70482933
RK
8253 DDC : constant Boolean := Do_Division_Check (N);
8254
b6b5cca8
AC
8255 Left : Node_Id;
8256 Right : Node_Id;
8257
70482933
RK
8258 LLB : Uint;
8259 Llo : Uint;
8260 Lhi : Uint;
8261 LOK : Boolean;
8262 Rlo : Uint;
8263 Rhi : Uint;
8264 ROK : Boolean;
8265
1033834f
RD
8266 pragma Warnings (Off, Lhi);
8267
70482933
RK
8268 begin
8269 Binary_Op_Validity_Checks (N);
8270
b6b5cca8
AC
8271 -- Check for MINIMIZED/ELIMINATED overflow mode
8272
8273 if Minimized_Eliminated_Overflow_Check (N) then
8274 Apply_Arithmetic_Overflow_Check (N);
8275 return;
8276 end if;
8277
9a6dc470
RD
8278 if Is_Integer_Type (Etype (N)) then
8279 Apply_Divide_Checks (N);
b6b5cca8
AC
8280
8281 -- All done if we don't have a MOD any more, which can happen as a
8282 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8283
8284 if Nkind (N) /= N_Op_Mod then
8285 return;
8286 end if;
9a6dc470
RD
8287 end if;
8288
b6b5cca8
AC
8289 -- Proceed with expansion of mod operator
8290
8291 Left := Left_Opnd (N);
8292 Right := Right_Opnd (N);
8293
5d5e9775
AC
8294 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
8295 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
70482933 8296
2c9f8c0a
AC
8297 -- Convert mod to rem if operands are both known to be non-negative, or
8298 -- both known to be non-positive (these are the cases in which rem and
8299 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8300 -- likely that this will improve the quality of code, (the operation now
8301 -- corresponds to the hardware remainder), and it does not seem likely
8302 -- that it could be harmful. It also avoids some cases of the elaborate
8303 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8304
8305 if (LOK and ROK)
8306 and then ((Llo >= 0 and then Rlo >= 0)
cc6f5d75 8307 or else
2c9f8c0a
AC
8308 (Lhi <= 0 and then Rhi <= 0))
8309 then
70482933
RK
8310 Rewrite (N,
8311 Make_Op_Rem (Sloc (N),
8312 Left_Opnd => Left_Opnd (N),
8313 Right_Opnd => Right_Opnd (N)));
8314
685094bf
RD
8315 -- Instead of reanalyzing the node we do the analysis manually. This
8316 -- avoids anomalies when the replacement is done in an instance and
8317 -- is epsilon more efficient.
70482933
RK
8318
8319 Set_Entity (N, Standard_Entity (S_Op_Rem));
fbf5a39b 8320 Set_Etype (N, Typ);
70482933
RK
8321 Set_Do_Division_Check (N, DDC);
8322 Expand_N_Op_Rem (N);
8323 Set_Analyzed (N);
2c9f8c0a 8324 return;
70482933
RK
8325
8326 -- Otherwise, normal mod processing
8327
8328 else
fbf5a39b
AC
8329 -- Apply optimization x mod 1 = 0. We don't really need that with
8330 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
8331 -- certainly harmless.
8332
8333 if Is_Integer_Type (Etype (N))
8334 and then Compile_Time_Known_Value (Right)
8335 and then Expr_Value (Right) = Uint_1
8336 then
abcbd24c
ST
8337 -- Call Remove_Side_Effects to ensure that any side effects in
8338 -- the ignored left operand (in particular function calls to
8339 -- user defined functions) are properly executed.
8340
8341 Remove_Side_Effects (Left);
8342
fbf5a39b
AC
8343 Rewrite (N, Make_Integer_Literal (Loc, 0));
8344 Analyze_And_Resolve (N, Typ);
8345 return;
8346 end if;
8347
2c9f8c0a
AC
8348 -- If we still have a mod operator and we are in Modify_Tree_For_C
8349 -- mode, and we have a signed integer type, then here is where we do
8350 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8351 -- for the special handling of the annoying case of largest negative
8352 -- number mod minus one.
8353
8354 if Nkind (N) = N_Op_Mod
8355 and then Is_Signed_Integer_Type (Typ)
8356 and then Modify_Tree_For_C
8357 then
8358 -- In the general case, we expand A mod B as
8359
8360 -- Tnn : constant typ := A rem B;
8361 -- ..
8362 -- (if (A >= 0) = (B >= 0) then Tnn
8363 -- elsif Tnn = 0 then 0
8364 -- else Tnn + B)
8365
8366 -- The comparison can be written simply as A >= 0 if we know that
8367 -- B >= 0 which is a very common case.
8368
8369 -- An important optimization is when B is known at compile time
8370 -- to be 2**K for some constant. In this case we can simply AND
8371 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8372 -- and that works for both the positive and negative cases.
8373
8374 declare
8375 P2 : constant Nat := Power_Of_Two (Right);
8376
8377 begin
8378 if P2 /= 0 then
8379 Rewrite (N,
8380 Unchecked_Convert_To (Typ,
8381 Make_Op_And (Loc,
8382 Left_Opnd =>
8383 Unchecked_Convert_To
8384 (Corresponding_Unsigned_Type (Typ), Left),
8385 Right_Opnd =>
8386 Make_Integer_Literal (Loc, 2 ** P2 - 1))));
8387 Analyze_And_Resolve (N, Typ);
8388 return;
8389 end if;
8390 end;
8391
8392 -- Here for the full rewrite
8393
8394 declare
8395 Tnn : constant Entity_Id := Make_Temporary (Sloc (N), 'T', N);
8396 Cmp : Node_Id;
8397
8398 begin
8399 Cmp :=
8400 Make_Op_Ge (Loc,
8401 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
8402 Right_Opnd => Make_Integer_Literal (Loc, 0));
8403
8404 if not LOK or else Rlo < 0 then
8405 Cmp :=
8406 Make_Op_Eq (Loc,
8407 Left_Opnd => Cmp,
8408 Right_Opnd =>
8409 Make_Op_Ge (Loc,
8410 Left_Opnd => Duplicate_Subexpr_No_Checks (Right),
8411 Right_Opnd => Make_Integer_Literal (Loc, 0)));
8412 end if;
8413
8414 Insert_Action (N,
8415 Make_Object_Declaration (Loc,
8416 Defining_Identifier => Tnn,
8417 Constant_Present => True,
8418 Object_Definition => New_Occurrence_Of (Typ, Loc),
8419 Expression =>
8420 Make_Op_Rem (Loc,
8421 Left_Opnd => Left,
8422 Right_Opnd => Right)));
8423
8424 Rewrite (N,
8425 Make_If_Expression (Loc,
8426 Expressions => New_List (
8427 Cmp,
8428 New_Occurrence_Of (Tnn, Loc),
8429 Make_If_Expression (Loc,
8430 Is_Elsif => True,
8431 Expressions => New_List (
8432 Make_Op_Eq (Loc,
8433 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8434 Right_Opnd => Make_Integer_Literal (Loc, 0)),
8435 Make_Integer_Literal (Loc, 0),
8436 Make_Op_Add (Loc,
8437 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8438 Right_Opnd =>
8439 Duplicate_Subexpr_No_Checks (Right)))))));
8440
8441 Analyze_And_Resolve (N, Typ);
8442 return;
8443 end;
8444 end if;
8445
8446 -- Deal with annoying case of largest negative number mod minus one.
8447 -- Gigi may not handle this case correctly, because on some targets,
8448 -- the mod value is computed using a divide instruction which gives
8449 -- an overflow trap for this case.
b9daa96e
AC
8450
8451 -- It would be a bit more efficient to figure out which targets
8452 -- this is really needed for, but in practice it is reasonable
8453 -- to do the following special check in all cases, since it means
8454 -- we get a clearer message, and also the overhead is minimal given
8455 -- that division is expensive in any case.
70482933 8456
685094bf
RD
8457 -- In fact the check is quite easy, if the right operand is -1, then
8458 -- the mod value is always 0, and we can just ignore the left operand
8459 -- completely in this case.
70482933 8460
9a6dc470
RD
8461 -- This only applies if we still have a mod operator. Skip if we
8462 -- have already rewritten this (e.g. in the case of eliminated
8463 -- overflow checks which have driven us into bignum mode).
fbf5a39b 8464
9a6dc470 8465 if Nkind (N) = N_Op_Mod then
70482933 8466
9a6dc470
RD
8467 -- The operand type may be private (e.g. in the expansion of an
8468 -- intrinsic operation) so we must use the underlying type to get
8469 -- the bounds, and convert the literals explicitly.
70482933 8470
9a6dc470
RD
8471 LLB :=
8472 Expr_Value
8473 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
8474
8475 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
761f7dcb 8476 and then ((not LOK) or else (Llo = LLB))
9a6dc470
RD
8477 then
8478 Rewrite (N,
9b16cb57 8479 Make_If_Expression (Loc,
9a6dc470
RD
8480 Expressions => New_List (
8481 Make_Op_Eq (Loc,
8482 Left_Opnd => Duplicate_Subexpr (Right),
8483 Right_Opnd =>
8484 Unchecked_Convert_To (Typ,
8485 Make_Integer_Literal (Loc, -1))),
8486 Unchecked_Convert_To (Typ,
8487 Make_Integer_Literal (Loc, Uint_0)),
8488 Relocate_Node (N))));
8489
8490 Set_Analyzed (Next (Next (First (Expressions (N)))));
8491 Analyze_And_Resolve (N, Typ);
8492 end if;
70482933
RK
8493 end if;
8494 end if;
8495 end Expand_N_Op_Mod;
8496
8497 --------------------------
8498 -- Expand_N_Op_Multiply --
8499 --------------------------
8500
8501 procedure Expand_N_Op_Multiply (N : Node_Id) is
abcbd24c
ST
8502 Loc : constant Source_Ptr := Sloc (N);
8503 Lop : constant Node_Id := Left_Opnd (N);
8504 Rop : constant Node_Id := Right_Opnd (N);
fbf5a39b 8505
abcbd24c 8506 Lp2 : constant Boolean :=
533369aa 8507 Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop);
abcbd24c 8508 Rp2 : constant Boolean :=
533369aa 8509 Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop);
fbf5a39b 8510
70482933
RK
8511 Ltyp : constant Entity_Id := Etype (Lop);
8512 Rtyp : constant Entity_Id := Etype (Rop);
8513 Typ : Entity_Id := Etype (N);
8514
8515 begin
8516 Binary_Op_Validity_Checks (N);
8517
b6b5cca8
AC
8518 -- Check for MINIMIZED/ELIMINATED overflow mode
8519
8520 if Minimized_Eliminated_Overflow_Check (N) then
8521 Apply_Arithmetic_Overflow_Check (N);
8522 return;
8523 end if;
8524
70482933
RK
8525 -- Special optimizations for integer types
8526
8527 if Is_Integer_Type (Typ) then
8528
abcbd24c 8529 -- N * 0 = 0 for integer types
70482933 8530
abcbd24c
ST
8531 if Compile_Time_Known_Value (Rop)
8532 and then Expr_Value (Rop) = Uint_0
70482933 8533 then
abcbd24c
ST
8534 -- Call Remove_Side_Effects to ensure that any side effects in
8535 -- the ignored left operand (in particular function calls to
8536 -- user defined functions) are properly executed.
8537
8538 Remove_Side_Effects (Lop);
8539
8540 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8541 Analyze_And_Resolve (N, Typ);
8542 return;
8543 end if;
8544
8545 -- Similar handling for 0 * N = 0
8546
8547 if Compile_Time_Known_Value (Lop)
8548 and then Expr_Value (Lop) = Uint_0
8549 then
8550 Remove_Side_Effects (Rop);
70482933
RK
8551 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8552 Analyze_And_Resolve (N, Typ);
8553 return;
8554 end if;
8555
8556 -- N * 1 = 1 * N = N for integer types
8557
fbf5a39b
AC
8558 -- This optimisation is not done if we are going to
8559 -- rewrite the product 1 * 2 ** N to a shift.
8560
8561 if Compile_Time_Known_Value (Rop)
8562 and then Expr_Value (Rop) = Uint_1
8563 and then not Lp2
70482933 8564 then
fbf5a39b 8565 Rewrite (N, Lop);
70482933
RK
8566 return;
8567
fbf5a39b
AC
8568 elsif Compile_Time_Known_Value (Lop)
8569 and then Expr_Value (Lop) = Uint_1
8570 and then not Rp2
70482933 8571 then
fbf5a39b 8572 Rewrite (N, Rop);
70482933
RK
8573 return;
8574 end if;
8575 end if;
8576
70482933
RK
8577 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8578 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8579 -- operand is an integer, as required for this to work.
8580
fbf5a39b
AC
8581 if Rp2 then
8582 if Lp2 then
70482933 8583
fbf5a39b 8584 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
70482933
RK
8585
8586 Rewrite (N,
8587 Make_Op_Expon (Loc,
8588 Left_Opnd => Make_Integer_Literal (Loc, 2),
8589 Right_Opnd =>
8590 Make_Op_Add (Loc,
8591 Left_Opnd => Right_Opnd (Lop),
8592 Right_Opnd => Right_Opnd (Rop))));
8593 Analyze_And_Resolve (N, Typ);
8594 return;
8595
8596 else
eefe3761
AC
8597 -- If the result is modular, perform the reduction of the result
8598 -- appropriately.
8599
8600 if Is_Modular_Integer_Type (Typ)
8601 and then not Non_Binary_Modulus (Typ)
8602 then
8603 Rewrite (N,
573e5dd6
RD
8604 Make_Op_And (Loc,
8605 Left_Opnd =>
8606 Make_Op_Shift_Left (Loc,
8607 Left_Opnd => Lop,
8608 Right_Opnd =>
8609 Convert_To (Standard_Natural, Right_Opnd (Rop))),
8610 Right_Opnd =>
eefe3761 8611 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
573e5dd6 8612
eefe3761
AC
8613 else
8614 Rewrite (N,
8615 Make_Op_Shift_Left (Loc,
8616 Left_Opnd => Lop,
8617 Right_Opnd =>
8618 Convert_To (Standard_Natural, Right_Opnd (Rop))));
8619 end if;
8620
70482933
RK
8621 Analyze_And_Resolve (N, Typ);
8622 return;
8623 end if;
8624
8625 -- Same processing for the operands the other way round
8626
fbf5a39b 8627 elsif Lp2 then
eefe3761
AC
8628 if Is_Modular_Integer_Type (Typ)
8629 and then not Non_Binary_Modulus (Typ)
8630 then
8631 Rewrite (N,
573e5dd6
RD
8632 Make_Op_And (Loc,
8633 Left_Opnd =>
8634 Make_Op_Shift_Left (Loc,
8635 Left_Opnd => Rop,
8636 Right_Opnd =>
8637 Convert_To (Standard_Natural, Right_Opnd (Lop))),
8638 Right_Opnd =>
8639 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
8640
eefe3761
AC
8641 else
8642 Rewrite (N,
8643 Make_Op_Shift_Left (Loc,
8644 Left_Opnd => Rop,
8645 Right_Opnd =>
8646 Convert_To (Standard_Natural, Right_Opnd (Lop))));
8647 end if;
8648
70482933
RK
8649 Analyze_And_Resolve (N, Typ);
8650 return;
8651 end if;
8652
8653 -- Do required fixup of universal fixed operation
8654
8655 if Typ = Universal_Fixed then
8656 Fixup_Universal_Fixed_Operation (N);
8657 Typ := Etype (N);
8658 end if;
8659
8660 -- Multiplications with fixed-point results
8661
8662 if Is_Fixed_Point_Type (Typ) then
8663
685094bf
RD
8664 -- No special processing if Treat_Fixed_As_Integer is set, since from
8665 -- a semantic point of view such operations are simply integer
8666 -- operations and will be treated that way.
70482933
RK
8667
8668 if not Treat_Fixed_As_Integer (N) then
8669
8670 -- Case of fixed * integer => fixed
8671
8672 if Is_Integer_Type (Rtyp) then
8673 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
8674
8675 -- Case of integer * fixed => fixed
8676
8677 elsif Is_Integer_Type (Ltyp) then
8678 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
8679
8680 -- Case of fixed * fixed => fixed
8681
8682 else
8683 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
8684 end if;
8685 end if;
8686
685094bf
RD
8687 -- Other cases of multiplication of fixed-point operands. Again we
8688 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
70482933
RK
8689
8690 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
8691 and then not Treat_Fixed_As_Integer (N)
8692 then
8693 if Is_Integer_Type (Typ) then
8694 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
8695 else
8696 pragma Assert (Is_Floating_Point_Type (Typ));
8697 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
8698 end if;
8699
685094bf
RD
8700 -- Mixed-mode operations can appear in a non-static universal context,
8701 -- in which case the integer argument must be converted explicitly.
70482933 8702
533369aa 8703 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
70482933 8704 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
70482933
RK
8705 Analyze_And_Resolve (Rop, Universal_Real);
8706
533369aa 8707 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
70482933 8708 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
70482933
RK
8709 Analyze_And_Resolve (Lop, Universal_Real);
8710
8711 -- Non-fixed point cases, check software overflow checking required
8712
8713 elsif Is_Signed_Integer_Type (Etype (N)) then
8714 Apply_Arithmetic_Overflow_Check (N);
8715 end if;
dfaff97b
RD
8716
8717 -- Overflow checks for floating-point if -gnateF mode active
8718
8719 Check_Float_Op_Overflow (N);
70482933
RK
8720 end Expand_N_Op_Multiply;
8721
8722 --------------------
8723 -- Expand_N_Op_Ne --
8724 --------------------
8725
70482933 8726 procedure Expand_N_Op_Ne (N : Node_Id) is
f02b8bb8 8727 Typ : constant Entity_Id := Etype (Left_Opnd (N));
70482933
RK
8728
8729 begin
f02b8bb8 8730 -- Case of elementary type with standard operator
70482933 8731
f02b8bb8
RD
8732 if Is_Elementary_Type (Typ)
8733 and then Sloc (Entity (N)) = Standard_Location
8734 then
8735 Binary_Op_Validity_Checks (N);
70482933 8736
456cbfa5 8737 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
60b68e56 8738 -- means we no longer have a /= operation, we are all done.
456cbfa5
AC
8739
8740 Expand_Compare_Minimize_Eliminate_Overflow (N);
8741
8742 if Nkind (N) /= N_Op_Ne then
8743 return;
8744 end if;
8745
f02b8bb8 8746 -- Boolean types (requiring handling of non-standard case)
70482933 8747
f02b8bb8
RD
8748 if Is_Boolean_Type (Typ) then
8749 Adjust_Condition (Left_Opnd (N));
8750 Adjust_Condition (Right_Opnd (N));
8751 Set_Etype (N, Standard_Boolean);
8752 Adjust_Result_Type (N, Typ);
8753 end if;
fbf5a39b 8754
f02b8bb8
RD
8755 Rewrite_Comparison (N);
8756
f02b8bb8
RD
8757 -- For all cases other than elementary types, we rewrite node as the
8758 -- negation of an equality operation, and reanalyze. The equality to be
8759 -- used is defined in the same scope and has the same signature. This
8760 -- signature must be set explicitly since in an instance it may not have
8761 -- the same visibility as in the generic unit. This avoids duplicating
8762 -- or factoring the complex code for record/array equality tests etc.
8763
8764 else
8765 declare
8766 Loc : constant Source_Ptr := Sloc (N);
8767 Neg : Node_Id;
8768 Ne : constant Entity_Id := Entity (N);
8769
8770 begin
8771 Binary_Op_Validity_Checks (N);
8772
8773 Neg :=
8774 Make_Op_Not (Loc,
8775 Right_Opnd =>
8776 Make_Op_Eq (Loc,
8777 Left_Opnd => Left_Opnd (N),
8778 Right_Opnd => Right_Opnd (N)));
8779 Set_Paren_Count (Right_Opnd (Neg), 1);
8780
8781 if Scope (Ne) /= Standard_Standard then
8782 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
8783 end if;
8784
4637729f 8785 -- For navigation purposes, we want to treat the inequality as an
f02b8bb8 8786 -- implicit reference to the corresponding equality. Preserve the
4637729f 8787 -- Comes_From_ source flag to generate proper Xref entries.
f02b8bb8
RD
8788
8789 Preserve_Comes_From_Source (Neg, N);
8790 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
8791 Rewrite (N, Neg);
8792 Analyze_And_Resolve (N, Standard_Boolean);
8793 end;
8794 end if;
0580d807
AC
8795
8796 Optimize_Length_Comparison (N);
70482933
RK
8797 end Expand_N_Op_Ne;
8798
8799 ---------------------
8800 -- Expand_N_Op_Not --
8801 ---------------------
8802
685094bf 8803 -- If the argument is other than a Boolean array type, there is no special
7a5b62b0
AC
8804 -- expansion required, except for dealing with validity checks, and non-
8805 -- standard boolean representations.
70482933 8806
7a5b62b0
AC
8807 -- For the packed array case, we call the special routine in Exp_Pakd,
8808 -- except that if the component size is greater than one, we use the
8809 -- standard routine generating a gruesome loop (it is so peculiar to have
8810 -- packed arrays with non-standard Boolean representations anyway, so it
8811 -- does not matter that we do not handle this case efficiently).
70482933 8812
7a5b62b0
AC
8813 -- For the unpacked array case (and for the special packed case where we
8814 -- have non standard Booleans, as discussed above), we generate and insert
8815 -- into the tree the following function definition:
70482933
RK
8816
8817 -- function Nnnn (A : arr) is
8818 -- B : arr;
8819 -- begin
8820 -- for J in a'range loop
8821 -- B (J) := not A (J);
8822 -- end loop;
8823 -- return B;
8824 -- end Nnnn;
8825
8826 -- Here arr is the actual subtype of the parameter (and hence always
8827 -- constrained). Then we replace the not with a call to this function.
8828
8829 procedure Expand_N_Op_Not (N : Node_Id) is
8830 Loc : constant Source_Ptr := Sloc (N);
8831 Typ : constant Entity_Id := Etype (N);
8832 Opnd : Node_Id;
8833 Arr : Entity_Id;
8834 A : Entity_Id;
8835 B : Entity_Id;
8836 J : Entity_Id;
8837 A_J : Node_Id;
8838 B_J : Node_Id;
8839
8840 Func_Name : Entity_Id;
8841 Loop_Statement : Node_Id;
8842
8843 begin
8844 Unary_Op_Validity_Checks (N);
8845
8846 -- For boolean operand, deal with non-standard booleans
8847
8848 if Is_Boolean_Type (Typ) then
8849 Adjust_Condition (Right_Opnd (N));
8850 Set_Etype (N, Standard_Boolean);
8851 Adjust_Result_Type (N, Typ);
8852 return;
8853 end if;
8854
da94696d 8855 -- Only array types need any other processing
70482933 8856
da94696d 8857 if not Is_Array_Type (Typ) then
70482933
RK
8858 return;
8859 end if;
8860
a9d8907c
JM
8861 -- Case of array operand. If bit packed with a component size of 1,
8862 -- handle it in Exp_Pakd if the operand is known to be aligned.
70482933 8863
a9d8907c
JM
8864 if Is_Bit_Packed_Array (Typ)
8865 and then Component_Size (Typ) = 1
8866 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
8867 then
70482933
RK
8868 Expand_Packed_Not (N);
8869 return;
8870 end if;
8871
fbf5a39b
AC
8872 -- Case of array operand which is not bit-packed. If the context is
8873 -- a safe assignment, call in-place operation, If context is a larger
8874 -- boolean expression in the context of a safe assignment, expansion is
8875 -- done by enclosing operation.
70482933
RK
8876
8877 Opnd := Relocate_Node (Right_Opnd (N));
8878 Convert_To_Actual_Subtype (Opnd);
8879 Arr := Etype (Opnd);
8880 Ensure_Defined (Arr, N);
b4592168 8881 Silly_Boolean_Array_Not_Test (N, Arr);
70482933 8882
fbf5a39b
AC
8883 if Nkind (Parent (N)) = N_Assignment_Statement then
8884 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
8885 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8886 return;
8887
5e1c00fa 8888 -- Special case the negation of a binary operation
fbf5a39b 8889
303b4d58 8890 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
fbf5a39b 8891 and then Safe_In_Place_Array_Op
303b4d58 8892 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
fbf5a39b
AC
8893 then
8894 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8895 return;
8896 end if;
8897
8898 elsif Nkind (Parent (N)) in N_Binary_Op
8899 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
8900 then
8901 declare
8902 Op1 : constant Node_Id := Left_Opnd (Parent (N));
8903 Op2 : constant Node_Id := Right_Opnd (Parent (N));
8904 Lhs : constant Node_Id := Name (Parent (Parent (N)));
8905
8906 begin
8907 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
fbf5a39b 8908
aa9a7dd7
AC
8909 -- (not A) op (not B) can be reduced to a single call
8910
8911 if N = Op1 and then Nkind (Op2) = N_Op_Not then
fbf5a39b
AC
8912 return;
8913
bed8af19
AC
8914 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
8915 return;
8916
aa9a7dd7 8917 -- A xor (not B) can also be special-cased
fbf5a39b 8918
aa9a7dd7 8919 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
fbf5a39b
AC
8920 return;
8921 end if;
8922 end if;
8923 end;
8924 end if;
8925
70482933
RK
8926 A := Make_Defining_Identifier (Loc, Name_uA);
8927 B := Make_Defining_Identifier (Loc, Name_uB);
8928 J := Make_Defining_Identifier (Loc, Name_uJ);
8929
8930 A_J :=
8931 Make_Indexed_Component (Loc,
e4494292
RD
8932 Prefix => New_Occurrence_Of (A, Loc),
8933 Expressions => New_List (New_Occurrence_Of (J, Loc)));
70482933
RK
8934
8935 B_J :=
8936 Make_Indexed_Component (Loc,
e4494292
RD
8937 Prefix => New_Occurrence_Of (B, Loc),
8938 Expressions => New_List (New_Occurrence_Of (J, Loc)));
70482933
RK
8939
8940 Loop_Statement :=
8941 Make_Implicit_Loop_Statement (N,
8942 Identifier => Empty,
8943
8944 Iteration_Scheme =>
8945 Make_Iteration_Scheme (Loc,
8946 Loop_Parameter_Specification =>
8947 Make_Loop_Parameter_Specification (Loc,
0d901290 8948 Defining_Identifier => J,
70482933
RK
8949 Discrete_Subtype_Definition =>
8950 Make_Attribute_Reference (Loc,
0d901290 8951 Prefix => Make_Identifier (Loc, Chars (A)),
70482933
RK
8952 Attribute_Name => Name_Range))),
8953
8954 Statements => New_List (
8955 Make_Assignment_Statement (Loc,
8956 Name => B_J,
8957 Expression => Make_Op_Not (Loc, A_J))));
8958
191fcb3a 8959 Func_Name := Make_Temporary (Loc, 'N');
70482933
RK
8960 Set_Is_Inlined (Func_Name);
8961
8962 Insert_Action (N,
8963 Make_Subprogram_Body (Loc,
8964 Specification =>
8965 Make_Function_Specification (Loc,
8966 Defining_Unit_Name => Func_Name,
8967 Parameter_Specifications => New_List (
8968 Make_Parameter_Specification (Loc,
8969 Defining_Identifier => A,
e4494292
RD
8970 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8971 Result_Definition => New_Occurrence_Of (Typ, Loc)),
70482933
RK
8972
8973 Declarations => New_List (
8974 Make_Object_Declaration (Loc,
8975 Defining_Identifier => B,
e4494292 8976 Object_Definition => New_Occurrence_Of (Arr, Loc))),
70482933
RK
8977
8978 Handled_Statement_Sequence =>
8979 Make_Handled_Sequence_Of_Statements (Loc,
8980 Statements => New_List (
8981 Loop_Statement,
d766cee3 8982 Make_Simple_Return_Statement (Loc,
0d901290 8983 Expression => Make_Identifier (Loc, Chars (B)))))));
70482933
RK
8984
8985 Rewrite (N,
8986 Make_Function_Call (Loc,
e4494292 8987 Name => New_Occurrence_Of (Func_Name, Loc),
70482933
RK
8988 Parameter_Associations => New_List (Opnd)));
8989
8990 Analyze_And_Resolve (N, Typ);
8991 end Expand_N_Op_Not;
8992
8993 --------------------
8994 -- Expand_N_Op_Or --
8995 --------------------
8996
8997 procedure Expand_N_Op_Or (N : Node_Id) is
8998 Typ : constant Entity_Id := Etype (N);
8999
9000 begin
9001 Binary_Op_Validity_Checks (N);
9002
9003 if Is_Array_Type (Etype (N)) then
9004 Expand_Boolean_Operator (N);
9005
9006 elsif Is_Boolean_Type (Etype (N)) then
f2d10a02
AC
9007 Adjust_Condition (Left_Opnd (N));
9008 Adjust_Condition (Right_Opnd (N));
9009 Set_Etype (N, Standard_Boolean);
9010 Adjust_Result_Type (N, Typ);
437f8c1e
AC
9011
9012 elsif Is_Intrinsic_Subprogram (Entity (N)) then
9013 Expand_Intrinsic_Call (N, Entity (N));
9014
70482933
RK
9015 end if;
9016 end Expand_N_Op_Or;
9017
9018 ----------------------
9019 -- Expand_N_Op_Plus --
9020 ----------------------
9021
9022 procedure Expand_N_Op_Plus (N : Node_Id) is
9023 begin
9024 Unary_Op_Validity_Checks (N);
b6b5cca8
AC
9025
9026 -- Check for MINIMIZED/ELIMINATED overflow mode
9027
9028 if Minimized_Eliminated_Overflow_Check (N) then
9029 Apply_Arithmetic_Overflow_Check (N);
9030 return;
9031 end if;
70482933
RK
9032 end Expand_N_Op_Plus;
9033
9034 ---------------------
9035 -- Expand_N_Op_Rem --
9036 ---------------------
9037
9038 procedure Expand_N_Op_Rem (N : Node_Id) is
9039 Loc : constant Source_Ptr := Sloc (N);
fbf5a39b 9040 Typ : constant Entity_Id := Etype (N);
70482933 9041
b6b5cca8
AC
9042 Left : Node_Id;
9043 Right : Node_Id;
70482933 9044
5d5e9775
AC
9045 Lo : Uint;
9046 Hi : Uint;
9047 OK : Boolean;
70482933 9048
5d5e9775
AC
9049 Lneg : Boolean;
9050 Rneg : Boolean;
9051 -- Set if corresponding operand can be negative
9052
9053 pragma Unreferenced (Hi);
1033834f 9054
70482933
RK
9055 begin
9056 Binary_Op_Validity_Checks (N);
9057
b6b5cca8
AC
9058 -- Check for MINIMIZED/ELIMINATED overflow mode
9059
9060 if Minimized_Eliminated_Overflow_Check (N) then
9061 Apply_Arithmetic_Overflow_Check (N);
9062 return;
9063 end if;
9064
70482933 9065 if Is_Integer_Type (Etype (N)) then
a91e9ac7 9066 Apply_Divide_Checks (N);
b6b5cca8
AC
9067
9068 -- All done if we don't have a REM any more, which can happen as a
9069 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
9070
9071 if Nkind (N) /= N_Op_Rem then
9072 return;
9073 end if;
70482933
RK
9074 end if;
9075
b6b5cca8
AC
9076 -- Proceed with expansion of REM
9077
9078 Left := Left_Opnd (N);
9079 Right := Right_Opnd (N);
9080
685094bf
RD
9081 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
9082 -- but it is useful with other back ends (e.g. AAMP), and is certainly
9083 -- harmless.
fbf5a39b
AC
9084
9085 if Is_Integer_Type (Etype (N))
9086 and then Compile_Time_Known_Value (Right)
9087 and then Expr_Value (Right) = Uint_1
9088 then
abcbd24c
ST
9089 -- Call Remove_Side_Effects to ensure that any side effects in the
9090 -- ignored left operand (in particular function calls to user defined
9091 -- functions) are properly executed.
9092
9093 Remove_Side_Effects (Left);
9094
fbf5a39b
AC
9095 Rewrite (N, Make_Integer_Literal (Loc, 0));
9096 Analyze_And_Resolve (N, Typ);
9097 return;
9098 end if;
9099
685094bf 9100 -- Deal with annoying case of largest negative number remainder minus
b9daa96e
AC
9101 -- one. Gigi may not handle this case correctly, because on some
9102 -- targets, the mod value is computed using a divide instruction
9103 -- which gives an overflow trap for this case.
9104
9105 -- It would be a bit more efficient to figure out which targets this
9106 -- is really needed for, but in practice it is reasonable to do the
9107 -- following special check in all cases, since it means we get a clearer
9108 -- message, and also the overhead is minimal given that division is
9109 -- expensive in any case.
70482933 9110
685094bf
RD
9111 -- In fact the check is quite easy, if the right operand is -1, then
9112 -- the remainder is always 0, and we can just ignore the left operand
9113 -- completely in this case.
70482933 9114
5d5e9775
AC
9115 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9116 Lneg := (not OK) or else Lo < 0;
fbf5a39b 9117
5d5e9775
AC
9118 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
9119 Rneg := (not OK) or else Lo < 0;
fbf5a39b 9120
5d5e9775
AC
9121 -- We won't mess with trying to find out if the left operand can really
9122 -- be the largest negative number (that's a pain in the case of private
9123 -- types and this is really marginal). We will just assume that we need
9124 -- the test if the left operand can be negative at all.
fbf5a39b 9125
5d5e9775 9126 if Lneg and Rneg then
70482933 9127 Rewrite (N,
9b16cb57 9128 Make_If_Expression (Loc,
70482933
RK
9129 Expressions => New_List (
9130 Make_Op_Eq (Loc,
0d901290 9131 Left_Opnd => Duplicate_Subexpr (Right),
70482933 9132 Right_Opnd =>
0d901290 9133 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
70482933 9134
fbf5a39b
AC
9135 Unchecked_Convert_To (Typ,
9136 Make_Integer_Literal (Loc, Uint_0)),
70482933
RK
9137
9138 Relocate_Node (N))));
9139
9140 Set_Analyzed (Next (Next (First (Expressions (N)))));
9141 Analyze_And_Resolve (N, Typ);
9142 end if;
9143 end Expand_N_Op_Rem;
9144
9145 -----------------------------
9146 -- Expand_N_Op_Rotate_Left --
9147 -----------------------------
9148
9149 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
9150 begin
9151 Binary_Op_Validity_Checks (N);
5216b599
AC
9152
9153 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
9154 -- so we rewrite in terms of logical shifts
9155
9156 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
9157
9158 -- where Bits is the shift count mod Esize (the mod operation here
9159 -- deals with ludicrous large shift counts, which are apparently OK).
9160
a95f708e 9161 -- What about nonbinary modulus ???
5216b599
AC
9162
9163 declare
9164 Loc : constant Source_Ptr := Sloc (N);
9165 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
9166 Typ : constant Entity_Id := Etype (N);
9167
9168 begin
9169 if Modify_Tree_For_C then
9170 Rewrite (Right_Opnd (N),
9171 Make_Op_Rem (Loc,
9172 Left_Opnd => Relocate_Node (Right_Opnd (N)),
9173 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
9174
9175 Analyze_And_Resolve (Right_Opnd (N), Rtp);
9176
9177 Rewrite (N,
9178 Make_Op_Or (Loc,
9179 Left_Opnd =>
9180 Make_Op_Shift_Left (Loc,
9181 Left_Opnd => Left_Opnd (N),
9182 Right_Opnd => Right_Opnd (N)),
e09a5598 9183
5216b599
AC
9184 Right_Opnd =>
9185 Make_Op_Shift_Right (Loc,
9186 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
9187 Right_Opnd =>
9188 Make_Op_Subtract (Loc,
9189 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9190 Right_Opnd =>
9191 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9192
9193 Analyze_And_Resolve (N, Typ);
9194 end if;
9195 end;
70482933
RK
9196 end Expand_N_Op_Rotate_Left;
9197
9198 ------------------------------
9199 -- Expand_N_Op_Rotate_Right --
9200 ------------------------------
9201
9202 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
9203 begin
9204 Binary_Op_Validity_Checks (N);
5216b599
AC
9205
9206 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
9207 -- so we rewrite in terms of logical shifts
9208
9209 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
9210
9211 -- where Bits is the shift count mod Esize (the mod operation here
9212 -- deals with ludicrous large shift counts, which are apparently OK).
9213
a95f708e 9214 -- What about nonbinary modulus ???
5216b599
AC
9215
9216 declare
9217 Loc : constant Source_Ptr := Sloc (N);
9218 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
9219 Typ : constant Entity_Id := Etype (N);
9220
9221 begin
9222 Rewrite (Right_Opnd (N),
9223 Make_Op_Rem (Loc,
9224 Left_Opnd => Relocate_Node (Right_Opnd (N)),
9225 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
9226
9227 Analyze_And_Resolve (Right_Opnd (N), Rtp);
9228
9229 if Modify_Tree_For_C then
9230 Rewrite (N,
9231 Make_Op_Or (Loc,
9232 Left_Opnd =>
9233 Make_Op_Shift_Right (Loc,
9234 Left_Opnd => Left_Opnd (N),
9235 Right_Opnd => Right_Opnd (N)),
e09a5598 9236
5216b599
AC
9237 Right_Opnd =>
9238 Make_Op_Shift_Left (Loc,
9239 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
9240 Right_Opnd =>
9241 Make_Op_Subtract (Loc,
9242 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9243 Right_Opnd =>
9244 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9245
9246 Analyze_And_Resolve (N, Typ);
9247 end if;
9248 end;
70482933
RK
9249 end Expand_N_Op_Rotate_Right;
9250
9251 ----------------------------
9252 -- Expand_N_Op_Shift_Left --
9253 ----------------------------
9254
e09a5598
AC
9255 -- Note: nothing in this routine depends on left as opposed to right shifts
9256 -- so we share the routine for expanding shift right operations.
9257
70482933
RK
9258 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
9259 begin
9260 Binary_Op_Validity_Checks (N);
e09a5598
AC
9261
9262 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9263 -- operand is not greater than the word size (since that would not
9264 -- be defined properly by the corresponding C shift operator).
9265
9266 if Modify_Tree_For_C then
9267 declare
9268 Right : constant Node_Id := Right_Opnd (N);
9269 Loc : constant Source_Ptr := Sloc (Right);
9270 Typ : constant Entity_Id := Etype (N);
9271 Siz : constant Uint := Esize (Typ);
9272 Orig : Node_Id;
9273 OK : Boolean;
9274 Lo : Uint;
9275 Hi : Uint;
9276
9277 begin
9278 if Compile_Time_Known_Value (Right) then
9279 if Expr_Value (Right) >= Siz then
9280 Rewrite (N, Make_Integer_Literal (Loc, 0));
9281 Analyze_And_Resolve (N, Typ);
9282 end if;
9283
9284 -- Not compile time known, find range
9285
9286 else
9287 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9288
9289 -- Nothing to do if known to be OK range, otherwise expand
9290
9291 if not OK or else Hi >= Siz then
9292
9293 -- Prevent recursion on copy of shift node
9294
9295 Orig := Relocate_Node (N);
9296 Set_Analyzed (Orig);
9297
9298 -- Now do the rewrite
9299
9300 Rewrite (N,
9301 Make_If_Expression (Loc,
9302 Expressions => New_List (
9303 Make_Op_Ge (Loc,
9304 Left_Opnd => Duplicate_Subexpr_Move_Checks (Right),
9305 Right_Opnd => Make_Integer_Literal (Loc, Siz)),
9306 Make_Integer_Literal (Loc, 0),
9307 Orig)));
9308 Analyze_And_Resolve (N, Typ);
9309 end if;
9310 end if;
9311 end;
9312 end if;
70482933
RK
9313 end Expand_N_Op_Shift_Left;
9314
9315 -----------------------------
9316 -- Expand_N_Op_Shift_Right --
9317 -----------------------------
9318
9319 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
9320 begin
e09a5598
AC
9321 -- Share shift left circuit
9322
9323 Expand_N_Op_Shift_Left (N);
70482933
RK
9324 end Expand_N_Op_Shift_Right;
9325
9326 ----------------------------------------
9327 -- Expand_N_Op_Shift_Right_Arithmetic --
9328 ----------------------------------------
9329
9330 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
9331 begin
9332 Binary_Op_Validity_Checks (N);
5216b599
AC
9333
9334 -- If we are in Modify_Tree_For_C mode, there is no shift right
9335 -- arithmetic in C, so we rewrite in terms of logical shifts.
9336
9337 -- Shift_Right (Num, Bits) or
9338 -- (if Num >= Sign
9339 -- then not (Shift_Right (Mask, bits))
9340 -- else 0)
9341
9342 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9343
9344 -- Note: in almost all C compilers it would work to just shift a
9345 -- signed integer right, but it's undefined and we cannot rely on it.
9346
e09a5598
AC
9347 -- Note: the above works fine for shift counts greater than or equal
9348 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9349 -- generates all 1'bits.
9350
a95f708e 9351 -- What about nonbinary modulus ???
5216b599
AC
9352
9353 declare
9354 Loc : constant Source_Ptr := Sloc (N);
9355 Typ : constant Entity_Id := Etype (N);
9356 Sign : constant Uint := 2 ** (Esize (Typ) - 1);
9357 Mask : constant Uint := (2 ** Esize (Typ)) - 1;
9358 Left : constant Node_Id := Left_Opnd (N);
9359 Right : constant Node_Id := Right_Opnd (N);
9360 Maskx : Node_Id;
9361
9362 begin
9363 if Modify_Tree_For_C then
9364
9365 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9366 -- compile time as a single constant.
9367
9368 if Compile_Time_Known_Value (Right) then
9369 declare
9370 Val : constant Uint := Expr_Value (Right);
9371
9372 begin
9373 if Val >= Esize (Typ) then
9374 Maskx := Make_Integer_Literal (Loc, Mask);
9375
9376 else
9377 Maskx :=
9378 Make_Integer_Literal (Loc,
9379 Intval => Mask - (Mask / (2 ** Expr_Value (Right))));
9380 end if;
9381 end;
9382
9383 else
9384 Maskx :=
9385 Make_Op_Not (Loc,
9386 Right_Opnd =>
9387 Make_Op_Shift_Right (Loc,
9388 Left_Opnd => Make_Integer_Literal (Loc, Mask),
9389 Right_Opnd => Duplicate_Subexpr_No_Checks (Right)));
9390 end if;
9391
9392 -- Now do the rewrite
9393
9394 Rewrite (N,
9395 Make_Op_Or (Loc,
9396 Left_Opnd =>
9397 Make_Op_Shift_Right (Loc,
9398 Left_Opnd => Left,
9399 Right_Opnd => Right),
9400 Right_Opnd =>
9401 Make_If_Expression (Loc,
9402 Expressions => New_List (
9403 Make_Op_Ge (Loc,
9404 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
9405 Right_Opnd => Make_Integer_Literal (Loc, Sign)),
9406 Maskx,
9407 Make_Integer_Literal (Loc, 0)))));
9408 Analyze_And_Resolve (N, Typ);
9409 end if;
9410 end;
70482933
RK
9411 end Expand_N_Op_Shift_Right_Arithmetic;
9412
9413 --------------------------
9414 -- Expand_N_Op_Subtract --
9415 --------------------------
9416
9417 procedure Expand_N_Op_Subtract (N : Node_Id) is
9418 Typ : constant Entity_Id := Etype (N);
9419
9420 begin
9421 Binary_Op_Validity_Checks (N);
9422
b6b5cca8
AC
9423 -- Check for MINIMIZED/ELIMINATED overflow mode
9424
9425 if Minimized_Eliminated_Overflow_Check (N) then
9426 Apply_Arithmetic_Overflow_Check (N);
9427 return;
9428 end if;
9429
70482933
RK
9430 -- N - 0 = N for integer types
9431
9432 if Is_Integer_Type (Typ)
9433 and then Compile_Time_Known_Value (Right_Opnd (N))
9434 and then Expr_Value (Right_Opnd (N)) = 0
9435 then
9436 Rewrite (N, Left_Opnd (N));
9437 return;
9438 end if;
9439
8fc789c8 9440 -- Arithmetic overflow checks for signed integer/fixed point types
70482933 9441
761f7dcb 9442 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
70482933 9443 Apply_Arithmetic_Overflow_Check (N);
70482933 9444 end if;
dfaff97b
RD
9445
9446 -- Overflow checks for floating-point if -gnateF mode active
9447
9448 Check_Float_Op_Overflow (N);
70482933
RK
9449 end Expand_N_Op_Subtract;
9450
9451 ---------------------
9452 -- Expand_N_Op_Xor --
9453 ---------------------
9454
9455 procedure Expand_N_Op_Xor (N : Node_Id) is
9456 Typ : constant Entity_Id := Etype (N);
9457
9458 begin
9459 Binary_Op_Validity_Checks (N);
9460
9461 if Is_Array_Type (Etype (N)) then
9462 Expand_Boolean_Operator (N);
9463
9464 elsif Is_Boolean_Type (Etype (N)) then
9465 Adjust_Condition (Left_Opnd (N));
9466 Adjust_Condition (Right_Opnd (N));
9467 Set_Etype (N, Standard_Boolean);
9468 Adjust_Result_Type (N, Typ);
437f8c1e
AC
9469
9470 elsif Is_Intrinsic_Subprogram (Entity (N)) then
9471 Expand_Intrinsic_Call (N, Entity (N));
9472
70482933
RK
9473 end if;
9474 end Expand_N_Op_Xor;
9475
9476 ----------------------
9477 -- Expand_N_Or_Else --
9478 ----------------------
9479
5875f8d6
AC
9480 procedure Expand_N_Or_Else (N : Node_Id)
9481 renames Expand_Short_Circuit_Operator;
70482933
RK
9482
9483 -----------------------------------
9484 -- Expand_N_Qualified_Expression --
9485 -----------------------------------
9486
9487 procedure Expand_N_Qualified_Expression (N : Node_Id) is
9488 Operand : constant Node_Id := Expression (N);
9489 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
9490
9491 begin
f82944b7
JM
9492 -- Do validity check if validity checking operands
9493
533369aa 9494 if Validity_Checks_On and Validity_Check_Operands then
f82944b7
JM
9495 Ensure_Valid (Operand);
9496 end if;
9497
9498 -- Apply possible constraint check
9499
70482933 9500 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
d79e621a
GD
9501
9502 if Do_Range_Check (Operand) then
9503 Set_Do_Range_Check (Operand, False);
9504 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
9505 end if;
70482933
RK
9506 end Expand_N_Qualified_Expression;
9507
a961aa79
AC
9508 ------------------------------------
9509 -- Expand_N_Quantified_Expression --
9510 ------------------------------------
9511
c0f136cd
AC
9512 -- We expand:
9513
9514 -- for all X in range => Cond
a961aa79 9515
c0f136cd 9516 -- into:
a961aa79 9517
c0f136cd
AC
9518 -- T := True;
9519 -- for X in range loop
9520 -- if not Cond then
9521 -- T := False;
9522 -- exit;
9523 -- end if;
9524 -- end loop;
90c63b09 9525
36504e5f 9526 -- Similarly, an existentially quantified expression:
90c63b09 9527
c0f136cd 9528 -- for some X in range => Cond
90c63b09 9529
c0f136cd 9530 -- becomes:
90c63b09 9531
c0f136cd
AC
9532 -- T := False;
9533 -- for X in range loop
9534 -- if Cond then
9535 -- T := True;
9536 -- exit;
9537 -- end if;
9538 -- end loop;
90c63b09 9539
c0f136cd
AC
9540 -- In both cases, the iteration may be over a container in which case it is
9541 -- given by an iterator specification, not a loop parameter specification.
a961aa79 9542
c0f136cd 9543 procedure Expand_N_Quantified_Expression (N : Node_Id) is
804670f1
AC
9544 Actions : constant List_Id := New_List;
9545 For_All : constant Boolean := All_Present (N);
9546 Iter_Spec : constant Node_Id := Iterator_Specification (N);
9547 Loc : constant Source_Ptr := Sloc (N);
9548 Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N);
9549 Cond : Node_Id;
9550 Flag : Entity_Id;
9551 Scheme : Node_Id;
9552 Stmts : List_Id;
c56a9ba4 9553
a961aa79 9554 begin
804670f1
AC
9555 -- Create the declaration of the flag which tracks the status of the
9556 -- quantified expression. Generate:
011f9d5d 9557
804670f1 9558 -- Flag : Boolean := (True | False);
011f9d5d 9559
804670f1 9560 Flag := Make_Temporary (Loc, 'T', N);
011f9d5d 9561
804670f1 9562 Append_To (Actions,
90c63b09 9563 Make_Object_Declaration (Loc,
804670f1 9564 Defining_Identifier => Flag,
c0f136cd
AC
9565 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
9566 Expression =>
804670f1
AC
9567 New_Occurrence_Of (Boolean_Literals (For_All), Loc)));
9568
9569 -- Construct the circuitry which tracks the status of the quantified
9570 -- expression. Generate:
9571
9572 -- if [not] Cond then
9573 -- Flag := (False | True);
9574 -- exit;
9575 -- end if;
a961aa79 9576
c0f136cd 9577 Cond := Relocate_Node (Condition (N));
a961aa79 9578
804670f1 9579 if For_All then
c0f136cd 9580 Cond := Make_Op_Not (Loc, Cond);
a961aa79
AC
9581 end if;
9582
804670f1 9583 Stmts := New_List (
c0f136cd
AC
9584 Make_Implicit_If_Statement (N,
9585 Condition => Cond,
9586 Then_Statements => New_List (
9587 Make_Assignment_Statement (Loc,
804670f1 9588 Name => New_Occurrence_Of (Flag, Loc),
c0f136cd 9589 Expression =>
804670f1
AC
9590 New_Occurrence_Of (Boolean_Literals (not For_All), Loc)),
9591 Make_Exit_Statement (Loc))));
9592
9593 -- Build the loop equivalent of the quantified expression
c0f136cd 9594
804670f1
AC
9595 if Present (Iter_Spec) then
9596 Scheme :=
011f9d5d 9597 Make_Iteration_Scheme (Loc,
804670f1 9598 Iterator_Specification => Iter_Spec);
c56a9ba4 9599 else
804670f1 9600 Scheme :=
011f9d5d 9601 Make_Iteration_Scheme (Loc,
804670f1 9602 Loop_Parameter_Specification => Loop_Spec);
c56a9ba4
AC
9603 end if;
9604
a961aa79
AC
9605 Append_To (Actions,
9606 Make_Loop_Statement (Loc,
804670f1
AC
9607 Iteration_Scheme => Scheme,
9608 Statements => Stmts,
c0f136cd 9609 End_Label => Empty));
a961aa79 9610
804670f1
AC
9611 -- Transform the quantified expression
9612
a961aa79
AC
9613 Rewrite (N,
9614 Make_Expression_With_Actions (Loc,
804670f1 9615 Expression => New_Occurrence_Of (Flag, Loc),
a961aa79 9616 Actions => Actions));
a961aa79
AC
9617 Analyze_And_Resolve (N, Standard_Boolean);
9618 end Expand_N_Quantified_Expression;
9619
70482933
RK
9620 ---------------------------------
9621 -- Expand_N_Selected_Component --
9622 ---------------------------------
9623
70482933
RK
9624 procedure Expand_N_Selected_Component (N : Node_Id) is
9625 Loc : constant Source_Ptr := Sloc (N);
9626 Par : constant Node_Id := Parent (N);
9627 P : constant Node_Id := Prefix (N);
03eb6036 9628 S : constant Node_Id := Selector_Name (N);
fbf5a39b 9629 Ptyp : Entity_Id := Underlying_Type (Etype (P));
70482933 9630 Disc : Entity_Id;
70482933 9631 New_N : Node_Id;
fbf5a39b 9632 Dcon : Elmt_Id;
d606f1df 9633 Dval : Node_Id;
70482933
RK
9634
9635 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
9636 -- Gigi needs a temporary for prefixes that depend on a discriminant,
9637 -- unless the context of an assignment can provide size information.
fbf5a39b
AC
9638 -- Don't we have a general routine that does this???
9639
53f29d4f
AC
9640 function Is_Subtype_Declaration return Boolean;
9641 -- The replacement of a discriminant reference by its value is required
4317e442
AC
9642 -- if this is part of the initialization of an temporary generated by a
9643 -- change of representation. This shows up as the construction of a
53f29d4f 9644 -- discriminant constraint for a subtype declared at the same point as
4317e442
AC
9645 -- the entity in the prefix of the selected component. We recognize this
9646 -- case when the context of the reference is:
9647 -- subtype ST is T(Obj.D);
9648 -- where the entity for Obj comes from source, and ST has the same sloc.
53f29d4f 9649
fbf5a39b
AC
9650 -----------------------
9651 -- In_Left_Hand_Side --
9652 -----------------------
70482933
RK
9653
9654 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
9655 begin
fbf5a39b 9656 return (Nkind (Parent (Comp)) = N_Assignment_Statement
90c63b09 9657 and then Comp = Name (Parent (Comp)))
fbf5a39b 9658 or else (Present (Parent (Comp))
90c63b09
AC
9659 and then Nkind (Parent (Comp)) in N_Subexpr
9660 and then In_Left_Hand_Side (Parent (Comp)));
70482933
RK
9661 end In_Left_Hand_Side;
9662
53f29d4f
AC
9663 -----------------------------
9664 -- Is_Subtype_Declaration --
9665 -----------------------------
9666
9667 function Is_Subtype_Declaration return Boolean is
9668 Par : constant Node_Id := Parent (N);
53f29d4f
AC
9669 begin
9670 return
9671 Nkind (Par) = N_Index_Or_Discriminant_Constraint
9672 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
9673 and then Comes_From_Source (Entity (Prefix (N)))
9674 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
9675 end Is_Subtype_Declaration;
9676
fbf5a39b
AC
9677 -- Start of processing for Expand_N_Selected_Component
9678
70482933 9679 begin
fbf5a39b
AC
9680 -- Insert explicit dereference if required
9681
9682 if Is_Access_Type (Ptyp) then
702d2020
AC
9683
9684 -- First set prefix type to proper access type, in case it currently
9685 -- has a private (non-access) view of this type.
9686
9687 Set_Etype (P, Ptyp);
9688
fbf5a39b 9689 Insert_Explicit_Dereference (P);
e6f69614 9690 Analyze_And_Resolve (P, Designated_Type (Ptyp));
fbf5a39b
AC
9691
9692 if Ekind (Etype (P)) = E_Private_Subtype
9693 and then Is_For_Access_Subtype (Etype (P))
9694 then
9695 Set_Etype (P, Base_Type (Etype (P)));
9696 end if;
9697
9698 Ptyp := Etype (P);
9699 end if;
9700
9701 -- Deal with discriminant check required
9702
70482933 9703 if Do_Discriminant_Check (N) then
03eb6036
AC
9704 if Present (Discriminant_Checking_Func
9705 (Original_Record_Component (Entity (S))))
9706 then
9707 -- Present the discriminant checking function to the backend, so
9708 -- that it can inline the call to the function.
9709
9710 Add_Inlined_Body
9711 (Discriminant_Checking_Func
cf27c5a2
EB
9712 (Original_Record_Component (Entity (S))),
9713 N);
70482933 9714
03eb6036 9715 -- Now reset the flag and generate the call
70482933 9716
03eb6036
AC
9717 Set_Do_Discriminant_Check (N, False);
9718 Generate_Discriminant_Check (N);
70482933 9719
03eb6036
AC
9720 -- In the case of Unchecked_Union, no discriminant checking is
9721 -- actually performed.
70482933 9722
03eb6036
AC
9723 else
9724 Set_Do_Discriminant_Check (N, False);
9725 end if;
70482933
RK
9726 end if;
9727
b4592168
GD
9728 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9729 -- function, then additional actuals must be passed.
9730
0791fbe9 9731 if Ada_Version >= Ada_2005
b4592168
GD
9732 and then Is_Build_In_Place_Function_Call (P)
9733 then
9734 Make_Build_In_Place_Call_In_Anonymous_Context (P);
9735 end if;
9736
fbf5a39b
AC
9737 -- Gigi cannot handle unchecked conversions that are the prefix of a
9738 -- selected component with discriminants. This must be checked during
9739 -- expansion, because during analysis the type of the selector is not
9740 -- known at the point the prefix is analyzed. If the conversion is the
9741 -- target of an assignment, then we cannot force the evaluation.
70482933
RK
9742
9743 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
9744 and then Has_Discriminants (Etype (N))
9745 and then not In_Left_Hand_Side (N)
9746 then
9747 Force_Evaluation (Prefix (N));
9748 end if;
9749
9750 -- Remaining processing applies only if selector is a discriminant
9751
9752 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
9753
9754 -- If the selector is a discriminant of a constrained record type,
fbf5a39b
AC
9755 -- we may be able to rewrite the expression with the actual value
9756 -- of the discriminant, a useful optimization in some cases.
70482933
RK
9757
9758 if Is_Record_Type (Ptyp)
9759 and then Has_Discriminants (Ptyp)
9760 and then Is_Constrained (Ptyp)
70482933 9761 then
fbf5a39b 9762 -- Do this optimization for discrete types only, and not for
a90bd866 9763 -- access types (access discriminants get us into trouble).
70482933 9764
fbf5a39b
AC
9765 if not Is_Discrete_Type (Etype (N)) then
9766 null;
9767
9768 -- Don't do this on the left hand of an assignment statement.
0d901290
AC
9769 -- Normally one would think that references like this would not
9770 -- occur, but they do in generated code, and mean that we really
a90bd866 9771 -- do want to assign the discriminant.
fbf5a39b
AC
9772
9773 elsif Nkind (Par) = N_Assignment_Statement
9774 and then Name (Par) = N
9775 then
9776 null;
9777
685094bf 9778 -- Don't do this optimization for the prefix of an attribute or
e2534738 9779 -- the name of an object renaming declaration since these are
685094bf 9780 -- contexts where we do not want the value anyway.
fbf5a39b
AC
9781
9782 elsif (Nkind (Par) = N_Attribute_Reference
533369aa 9783 and then Prefix (Par) = N)
fbf5a39b
AC
9784 or else Is_Renamed_Object (N)
9785 then
9786 null;
9787
9788 -- Don't do this optimization if we are within the code for a
9789 -- discriminant check, since the whole point of such a check may
a90bd866 9790 -- be to verify the condition on which the code below depends.
fbf5a39b
AC
9791
9792 elsif Is_In_Discriminant_Check (N) then
9793 null;
9794
9795 -- Green light to see if we can do the optimization. There is
685094bf
RD
9796 -- still one condition that inhibits the optimization below but
9797 -- now is the time to check the particular discriminant.
fbf5a39b
AC
9798
9799 else
685094bf
RD
9800 -- Loop through discriminants to find the matching discriminant
9801 -- constraint to see if we can copy it.
fbf5a39b
AC
9802
9803 Disc := First_Discriminant (Ptyp);
9804 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
9805 Discr_Loop : while Present (Dcon) loop
d606f1df 9806 Dval := Node (Dcon);
fbf5a39b 9807
bd949ee2
RD
9808 -- Check if this is the matching discriminant and if the
9809 -- discriminant value is simple enough to make sense to
9810 -- copy. We don't want to copy complex expressions, and
9811 -- indeed to do so can cause trouble (before we put in
9812 -- this guard, a discriminant expression containing an
e7d897b8 9813 -- AND THEN was copied, causing problems for coverage
c228a069 9814 -- analysis tools).
bd949ee2 9815
53f29d4f
AC
9816 -- However, if the reference is part of the initialization
9817 -- code generated for an object declaration, we must use
9818 -- the discriminant value from the subtype constraint,
9819 -- because the selected component may be a reference to the
9820 -- object being initialized, whose discriminant is not yet
9821 -- set. This only happens in complex cases involving changes
9822 -- or representation.
9823
bd949ee2
RD
9824 if Disc = Entity (Selector_Name (N))
9825 and then (Is_Entity_Name (Dval)
170b2989
AC
9826 or else Compile_Time_Known_Value (Dval)
9827 or else Is_Subtype_Declaration)
bd949ee2 9828 then
fbf5a39b
AC
9829 -- Here we have the matching discriminant. Check for
9830 -- the case of a discriminant of a component that is
9831 -- constrained by an outer discriminant, which cannot
9832 -- be optimized away.
9833
d606f1df
AC
9834 if Denotes_Discriminant
9835 (Dval, Check_Concurrent => True)
9836 then
9837 exit Discr_Loop;
9838
9839 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
9840 and then
9841 Denotes_Discriminant
9842 (Selector_Name (Original_Node (Dval)), True)
9843 then
9844 exit Discr_Loop;
9845
9846 -- Do not retrieve value if constraint is not static. It
9847 -- is generally not useful, and the constraint may be a
9848 -- rewritten outer discriminant in which case it is in
9849 -- fact incorrect.
9850
9851 elsif Is_Entity_Name (Dval)
d606f1df 9852 and then
533369aa
AC
9853 Nkind (Parent (Entity (Dval))) = N_Object_Declaration
9854 and then Present (Expression (Parent (Entity (Dval))))
9855 and then not
edab6088 9856 Is_OK_Static_Expression
d606f1df 9857 (Expression (Parent (Entity (Dval))))
fbf5a39b
AC
9858 then
9859 exit Discr_Loop;
70482933 9860
685094bf
RD
9861 -- In the context of a case statement, the expression may
9862 -- have the base type of the discriminant, and we need to
9863 -- preserve the constraint to avoid spurious errors on
9864 -- missing cases.
70482933 9865
fbf5a39b 9866 elsif Nkind (Parent (N)) = N_Case_Statement
d606f1df 9867 and then Etype (Dval) /= Etype (Disc)
70482933
RK
9868 then
9869 Rewrite (N,
9870 Make_Qualified_Expression (Loc,
fbf5a39b
AC
9871 Subtype_Mark =>
9872 New_Occurrence_Of (Etype (Disc), Loc),
9873 Expression =>
d606f1df 9874 New_Copy_Tree (Dval)));
ffe9aba8 9875 Analyze_And_Resolve (N, Etype (Disc));
fbf5a39b
AC
9876
9877 -- In case that comes out as a static expression,
9878 -- reset it (a selected component is never static).
9879
9880 Set_Is_Static_Expression (N, False);
9881 return;
9882
9883 -- Otherwise we can just copy the constraint, but the
a90bd866 9884 -- result is certainly not static. In some cases the
ffe9aba8
AC
9885 -- discriminant constraint has been analyzed in the
9886 -- context of the original subtype indication, but for
9887 -- itypes the constraint might not have been analyzed
9888 -- yet, and this must be done now.
fbf5a39b 9889
70482933 9890 else
d606f1df 9891 Rewrite (N, New_Copy_Tree (Dval));
ffe9aba8 9892 Analyze_And_Resolve (N);
fbf5a39b
AC
9893 Set_Is_Static_Expression (N, False);
9894 return;
70482933 9895 end if;
70482933
RK
9896 end if;
9897
fbf5a39b
AC
9898 Next_Elmt (Dcon);
9899 Next_Discriminant (Disc);
9900 end loop Discr_Loop;
70482933 9901
fbf5a39b
AC
9902 -- Note: the above loop should always find a matching
9903 -- discriminant, but if it does not, we just missed an
c228a069
AC
9904 -- optimization due to some glitch (perhaps a previous
9905 -- error), so ignore.
fbf5a39b
AC
9906
9907 end if;
70482933
RK
9908 end if;
9909
9910 -- The only remaining processing is in the case of a discriminant of
9911 -- a concurrent object, where we rewrite the prefix to denote the
9912 -- corresponding record type. If the type is derived and has renamed
9913 -- discriminants, use corresponding discriminant, which is the one
9914 -- that appears in the corresponding record.
9915
9916 if not Is_Concurrent_Type (Ptyp) then
9917 return;
9918 end if;
9919
9920 Disc := Entity (Selector_Name (N));
9921
9922 if Is_Derived_Type (Ptyp)
9923 and then Present (Corresponding_Discriminant (Disc))
9924 then
9925 Disc := Corresponding_Discriminant (Disc);
9926 end if;
9927
9928 New_N :=
9929 Make_Selected_Component (Loc,
9930 Prefix =>
9931 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
9932 New_Copy_Tree (P)),
9933 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
9934
9935 Rewrite (N, New_N);
9936 Analyze (N);
9937 end if;
5972791c 9938
73fe1679 9939 -- Set Atomic_Sync_Required if necessary for atomic component
5972791c 9940
73fe1679
AC
9941 if Nkind (N) = N_Selected_Component then
9942 declare
9943 E : constant Entity_Id := Entity (Selector_Name (N));
9944 Set : Boolean;
9945
9946 begin
9947 -- If component is atomic, but type is not, setting depends on
9948 -- disable/enable state for the component.
9949
9950 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
9951 Set := not Atomic_Synchronization_Disabled (E);
9952
9953 -- If component is not atomic, but its type is atomic, setting
9954 -- depends on disable/enable state for the type.
9955
9956 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9957 Set := not Atomic_Synchronization_Disabled (Etype (E));
9958
9959 -- If both component and type are atomic, we disable if either
9960 -- component or its type have sync disabled.
9961
9962 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9963 Set := (not Atomic_Synchronization_Disabled (E))
9964 and then
9965 (not Atomic_Synchronization_Disabled (Etype (E)));
9966
9967 else
9968 Set := False;
9969 end if;
9970
9971 -- Set flag if required
9972
9973 if Set then
9974 Activate_Atomic_Synchronization (N);
9975 end if;
9976 end;
5972791c 9977 end if;
70482933
RK
9978 end Expand_N_Selected_Component;
9979
9980 --------------------
9981 -- Expand_N_Slice --
9982 --------------------
9983
9984 procedure Expand_N_Slice (N : Node_Id) is
5ff90f08
AC
9985 Loc : constant Source_Ptr := Sloc (N);
9986 Typ : constant Entity_Id := Etype (N);
fbf5a39b 9987
81a5b587 9988 function Is_Procedure_Actual (N : Node_Id) return Boolean;
685094bf
RD
9989 -- Check whether the argument is an actual for a procedure call, in
9990 -- which case the expansion of a bit-packed slice is deferred until the
9991 -- call itself is expanded. The reason this is required is that we might
9992 -- have an IN OUT or OUT parameter, and the copy out is essential, and
9993 -- that copy out would be missed if we created a temporary here in
9994 -- Expand_N_Slice. Note that we don't bother to test specifically for an
9995 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
9996 -- is harmless to defer expansion in the IN case, since the call
9997 -- processing will still generate the appropriate copy in operation,
9998 -- which will take care of the slice.
81a5b587 9999
b01bf852 10000 procedure Make_Temporary_For_Slice;
685094bf
RD
10001 -- Create a named variable for the value of the slice, in cases where
10002 -- the back-end cannot handle it properly, e.g. when packed types or
10003 -- unaligned slices are involved.
fbf5a39b 10004
81a5b587
AC
10005 -------------------------
10006 -- Is_Procedure_Actual --
10007 -------------------------
10008
10009 function Is_Procedure_Actual (N : Node_Id) return Boolean is
10010 Par : Node_Id := Parent (N);
08aa9a4a 10011
81a5b587 10012 begin
81a5b587 10013 loop
c6a60aa1
RD
10014 -- If our parent is a procedure call we can return
10015
81a5b587
AC
10016 if Nkind (Par) = N_Procedure_Call_Statement then
10017 return True;
6b6fcd3e 10018
685094bf
RD
10019 -- If our parent is a type conversion, keep climbing the tree,
10020 -- since a type conversion can be a procedure actual. Also keep
10021 -- climbing if parameter association or a qualified expression,
10022 -- since these are additional cases that do can appear on
10023 -- procedure actuals.
6b6fcd3e 10024
303b4d58
AC
10025 elsif Nkind_In (Par, N_Type_Conversion,
10026 N_Parameter_Association,
10027 N_Qualified_Expression)
c6a60aa1 10028 then
81a5b587 10029 Par := Parent (Par);
c6a60aa1
RD
10030
10031 -- Any other case is not what we are looking for
10032
10033 else
10034 return False;
81a5b587
AC
10035 end if;
10036 end loop;
81a5b587
AC
10037 end Is_Procedure_Actual;
10038
b01bf852
AC
10039 ------------------------------
10040 -- Make_Temporary_For_Slice --
10041 ------------------------------
fbf5a39b 10042
b01bf852 10043 procedure Make_Temporary_For_Slice is
b01bf852 10044 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
5ff90f08 10045 Decl : Node_Id;
13d923cc 10046
fbf5a39b
AC
10047 begin
10048 Decl :=
10049 Make_Object_Declaration (Loc,
10050 Defining_Identifier => Ent,
10051 Object_Definition => New_Occurrence_Of (Typ, Loc));
10052
10053 Set_No_Initialization (Decl);
10054
10055 Insert_Actions (N, New_List (
10056 Decl,
10057 Make_Assignment_Statement (Loc,
5ff90f08 10058 Name => New_Occurrence_Of (Ent, Loc),
fbf5a39b
AC
10059 Expression => Relocate_Node (N))));
10060
10061 Rewrite (N, New_Occurrence_Of (Ent, Loc));
10062 Analyze_And_Resolve (N, Typ);
b01bf852 10063 end Make_Temporary_For_Slice;
fbf5a39b 10064
5ff90f08
AC
10065 -- Local variables
10066
800da977
AC
10067 Pref : constant Node_Id := Prefix (N);
10068 Pref_Typ : Entity_Id := Etype (Pref);
5ff90f08 10069
fbf5a39b 10070 -- Start of processing for Expand_N_Slice
70482933
RK
10071
10072 begin
10073 -- Special handling for access types
10074
5ff90f08
AC
10075 if Is_Access_Type (Pref_Typ) then
10076 Pref_Typ := Designated_Type (Pref_Typ);
70482933 10077
5ff90f08 10078 Rewrite (Pref,
e6f69614 10079 Make_Explicit_Dereference (Sloc (N),
5ff90f08 10080 Prefix => Relocate_Node (Pref)));
70482933 10081
5ff90f08 10082 Analyze_And_Resolve (Pref, Pref_Typ);
70482933
RK
10083 end if;
10084
b4592168
GD
10085 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10086 -- function, then additional actuals must be passed.
10087
0791fbe9 10088 if Ada_Version >= Ada_2005
5ff90f08 10089 and then Is_Build_In_Place_Function_Call (Pref)
b4592168 10090 then
5ff90f08 10091 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
b4592168
GD
10092 end if;
10093
70482933
RK
10094 -- The remaining case to be handled is packed slices. We can leave
10095 -- packed slices as they are in the following situations:
10096
10097 -- 1. Right or left side of an assignment (we can handle this
10098 -- situation correctly in the assignment statement expansion).
10099
685094bf
RD
10100 -- 2. Prefix of indexed component (the slide is optimized away in this
10101 -- case, see the start of Expand_N_Slice.)
70482933 10102
685094bf
RD
10103 -- 3. Object renaming declaration, since we want the name of the
10104 -- slice, not the value.
70482933 10105
685094bf
RD
10106 -- 4. Argument to procedure call, since copy-in/copy-out handling may
10107 -- be required, and this is handled in the expansion of call
10108 -- itself.
70482933 10109
685094bf
RD
10110 -- 5. Prefix of an address attribute (this is an error which is caught
10111 -- elsewhere, and the expansion would interfere with generating the
10112 -- error message).
70482933 10113
81a5b587 10114 if not Is_Packed (Typ) then
08aa9a4a 10115
685094bf
RD
10116 -- Apply transformation for actuals of a function call, where
10117 -- Expand_Actuals is not used.
81a5b587
AC
10118
10119 if Nkind (Parent (N)) = N_Function_Call
10120 and then Is_Possibly_Unaligned_Slice (N)
10121 then
b01bf852 10122 Make_Temporary_For_Slice;
81a5b587
AC
10123 end if;
10124
10125 elsif Nkind (Parent (N)) = N_Assignment_Statement
10126 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
533369aa 10127 and then Parent (N) = Name (Parent (Parent (N))))
70482933 10128 then
81a5b587 10129 return;
70482933 10130
81a5b587
AC
10131 elsif Nkind (Parent (N)) = N_Indexed_Component
10132 or else Is_Renamed_Object (N)
10133 or else Is_Procedure_Actual (N)
10134 then
10135 return;
70482933 10136
91b1417d
AC
10137 elsif Nkind (Parent (N)) = N_Attribute_Reference
10138 and then Attribute_Name (Parent (N)) = Name_Address
fbf5a39b 10139 then
81a5b587
AC
10140 return;
10141
10142 else
b01bf852 10143 Make_Temporary_For_Slice;
70482933
RK
10144 end if;
10145 end Expand_N_Slice;
10146
10147 ------------------------------
10148 -- Expand_N_Type_Conversion --
10149 ------------------------------
10150
10151 procedure Expand_N_Type_Conversion (N : Node_Id) is
10152 Loc : constant Source_Ptr := Sloc (N);
10153 Operand : constant Node_Id := Expression (N);
10154 Target_Type : constant Entity_Id := Etype (N);
10155 Operand_Type : Entity_Id := Etype (Operand);
10156
10157 procedure Handle_Changed_Representation;
685094bf
RD
10158 -- This is called in the case of record and array type conversions to
10159 -- see if there is a change of representation to be handled. Change of
10160 -- representation is actually handled at the assignment statement level,
10161 -- and what this procedure does is rewrite node N conversion as an
10162 -- assignment to temporary. If there is no change of representation,
10163 -- then the conversion node is unchanged.
70482933 10164
426908f8
RD
10165 procedure Raise_Accessibility_Error;
10166 -- Called when we know that an accessibility check will fail. Rewrites
10167 -- node N to an appropriate raise statement and outputs warning msgs.
91669e7e
AC
10168 -- The Etype of the raise node is set to Target_Type. Note that in this
10169 -- case the rest of the processing should be skipped (i.e. the call to
10170 -- this procedure will be followed by "goto Done").
426908f8 10171
70482933
RK
10172 procedure Real_Range_Check;
10173 -- Handles generation of range check for real target value
10174
d15f9422
AC
10175 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
10176 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
10177 -- evaluates to True.
10178
70482933
RK
10179 -----------------------------------
10180 -- Handle_Changed_Representation --
10181 -----------------------------------
10182
10183 procedure Handle_Changed_Representation is
10184 Temp : Entity_Id;
10185 Decl : Node_Id;
10186 Odef : Node_Id;
10187 Disc : Node_Id;
10188 N_Ix : Node_Id;
10189 Cons : List_Id;
10190
10191 begin
f82944b7 10192 -- Nothing else to do if no change of representation
70482933
RK
10193
10194 if Same_Representation (Operand_Type, Target_Type) then
10195 return;
10196
10197 -- The real change of representation work is done by the assignment
10198 -- statement processing. So if this type conversion is appearing as
10199 -- the expression of an assignment statement, nothing needs to be
10200 -- done to the conversion.
10201
10202 elsif Nkind (Parent (N)) = N_Assignment_Statement then
10203 return;
10204
10205 -- Otherwise we need to generate a temporary variable, and do the
10206 -- change of representation assignment into that temporary variable.
10207 -- The conversion is then replaced by a reference to this variable.
10208
10209 else
10210 Cons := No_List;
10211
685094bf
RD
10212 -- If type is unconstrained we have to add a constraint, copied
10213 -- from the actual value of the left hand side.
70482933
RK
10214
10215 if not Is_Constrained (Target_Type) then
10216 if Has_Discriminants (Operand_Type) then
10217 Disc := First_Discriminant (Operand_Type);
fbf5a39b
AC
10218
10219 if Disc /= First_Stored_Discriminant (Operand_Type) then
10220 Disc := First_Stored_Discriminant (Operand_Type);
10221 end if;
10222
70482933
RK
10223 Cons := New_List;
10224 while Present (Disc) loop
10225 Append_To (Cons,
10226 Make_Selected_Component (Loc,
7675ad4f
AC
10227 Prefix =>
10228 Duplicate_Subexpr_Move_Checks (Operand),
70482933
RK
10229 Selector_Name =>
10230 Make_Identifier (Loc, Chars (Disc))));
10231 Next_Discriminant (Disc);
10232 end loop;
10233
10234 elsif Is_Array_Type (Operand_Type) then
10235 N_Ix := First_Index (Target_Type);
10236 Cons := New_List;
10237
10238 for J in 1 .. Number_Dimensions (Operand_Type) loop
10239
10240 -- We convert the bounds explicitly. We use an unchecked
10241 -- conversion because bounds checks are done elsewhere.
10242
10243 Append_To (Cons,
10244 Make_Range (Loc,
10245 Low_Bound =>
10246 Unchecked_Convert_To (Etype (N_Ix),
10247 Make_Attribute_Reference (Loc,
10248 Prefix =>
fbf5a39b 10249 Duplicate_Subexpr_No_Checks
70482933
RK
10250 (Operand, Name_Req => True),
10251 Attribute_Name => Name_First,
10252 Expressions => New_List (
10253 Make_Integer_Literal (Loc, J)))),
10254
10255 High_Bound =>
10256 Unchecked_Convert_To (Etype (N_Ix),
10257 Make_Attribute_Reference (Loc,
10258 Prefix =>
fbf5a39b 10259 Duplicate_Subexpr_No_Checks
70482933
RK
10260 (Operand, Name_Req => True),
10261 Attribute_Name => Name_Last,
10262 Expressions => New_List (
10263 Make_Integer_Literal (Loc, J))))));
10264
10265 Next_Index (N_Ix);
10266 end loop;
10267 end if;
10268 end if;
10269
10270 Odef := New_Occurrence_Of (Target_Type, Loc);
10271
10272 if Present (Cons) then
10273 Odef :=
10274 Make_Subtype_Indication (Loc,
10275 Subtype_Mark => Odef,
10276 Constraint =>
10277 Make_Index_Or_Discriminant_Constraint (Loc,
10278 Constraints => Cons));
10279 end if;
10280
191fcb3a 10281 Temp := Make_Temporary (Loc, 'C');
70482933
RK
10282 Decl :=
10283 Make_Object_Declaration (Loc,
10284 Defining_Identifier => Temp,
10285 Object_Definition => Odef);
10286
10287 Set_No_Initialization (Decl, True);
10288
10289 -- Insert required actions. It is essential to suppress checks
10290 -- since we have suppressed default initialization, which means
10291 -- that the variable we create may have no discriminants.
10292
10293 Insert_Actions (N,
10294 New_List (
10295 Decl,
10296 Make_Assignment_Statement (Loc,
10297 Name => New_Occurrence_Of (Temp, Loc),
10298 Expression => Relocate_Node (N))),
10299 Suppress => All_Checks);
10300
10301 Rewrite (N, New_Occurrence_Of (Temp, Loc));
10302 return;
10303 end if;
10304 end Handle_Changed_Representation;
10305
426908f8
RD
10306 -------------------------------
10307 -- Raise_Accessibility_Error --
10308 -------------------------------
10309
10310 procedure Raise_Accessibility_Error is
10311 begin
43417b90 10312 Error_Msg_Warn := SPARK_Mode /= On;
426908f8
RD
10313 Rewrite (N,
10314 Make_Raise_Program_Error (Sloc (N),
10315 Reason => PE_Accessibility_Check_Failed));
10316 Set_Etype (N, Target_Type);
10317
4a28b181
AC
10318 Error_Msg_N ("<<accessibility check failure", N);
10319 Error_Msg_NE ("\<<& [", N, Standard_Program_Error);
426908f8
RD
10320 end Raise_Accessibility_Error;
10321
70482933
RK
10322 ----------------------
10323 -- Real_Range_Check --
10324 ----------------------
10325
685094bf
RD
10326 -- Case of conversions to floating-point or fixed-point. If range checks
10327 -- are enabled and the target type has a range constraint, we convert:
70482933
RK
10328
10329 -- typ (x)
10330
10331 -- to
10332
10333 -- Tnn : typ'Base := typ'Base (x);
10334 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10335 -- Tnn
10336
685094bf
RD
10337 -- This is necessary when there is a conversion of integer to float or
10338 -- to fixed-point to ensure that the correct checks are made. It is not
10339 -- necessary for float to float where it is enough to simply set the
10340 -- Do_Range_Check flag.
fbf5a39b 10341
70482933
RK
10342 procedure Real_Range_Check is
10343 Btyp : constant Entity_Id := Base_Type (Target_Type);
10344 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
10345 Hi : constant Node_Id := Type_High_Bound (Target_Type);
fbf5a39b 10346 Xtyp : constant Entity_Id := Etype (Operand);
70482933
RK
10347 Conv : Node_Id;
10348 Tnn : Entity_Id;
10349
10350 begin
10351 -- Nothing to do if conversion was rewritten
10352
10353 if Nkind (N) /= N_Type_Conversion then
10354 return;
10355 end if;
10356
685094bf
RD
10357 -- Nothing to do if range checks suppressed, or target has the same
10358 -- range as the base type (or is the base type).
70482933
RK
10359
10360 if Range_Checks_Suppressed (Target_Type)
533369aa 10361 or else (Lo = Type_Low_Bound (Btyp)
70482933
RK
10362 and then
10363 Hi = Type_High_Bound (Btyp))
10364 then
10365 return;
10366 end if;
10367
685094bf
RD
10368 -- Nothing to do if expression is an entity on which checks have been
10369 -- suppressed.
70482933 10370
fbf5a39b
AC
10371 if Is_Entity_Name (Operand)
10372 and then Range_Checks_Suppressed (Entity (Operand))
10373 then
10374 return;
10375 end if;
10376
685094bf
RD
10377 -- Nothing to do if bounds are all static and we can tell that the
10378 -- expression is within the bounds of the target. Note that if the
10379 -- operand is of an unconstrained floating-point type, then we do
10380 -- not trust it to be in range (might be infinite)
fbf5a39b
AC
10381
10382 declare
f02b8bb8
RD
10383 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
10384 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
fbf5a39b
AC
10385
10386 begin
10387 if (not Is_Floating_Point_Type (Xtyp)
10388 or else Is_Constrained (Xtyp))
10389 and then Compile_Time_Known_Value (S_Lo)
10390 and then Compile_Time_Known_Value (S_Hi)
10391 and then Compile_Time_Known_Value (Hi)
10392 and then Compile_Time_Known_Value (Lo)
10393 then
10394 declare
10395 D_Lov : constant Ureal := Expr_Value_R (Lo);
10396 D_Hiv : constant Ureal := Expr_Value_R (Hi);
10397 S_Lov : Ureal;
10398 S_Hiv : Ureal;
10399
10400 begin
10401 if Is_Real_Type (Xtyp) then
10402 S_Lov := Expr_Value_R (S_Lo);
10403 S_Hiv := Expr_Value_R (S_Hi);
10404 else
10405 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
10406 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
10407 end if;
10408
10409 if D_Hiv > D_Lov
10410 and then S_Lov >= D_Lov
10411 and then S_Hiv <= D_Hiv
10412 then
8b034336
AC
10413 -- Unset the range check flag on the current value of
10414 -- Expression (N), since the captured Operand may have
10415 -- been rewritten (such as for the case of a conversion
10416 -- to a fixed-point type).
10417
10418 Set_Do_Range_Check (Expression (N), False);
10419
fbf5a39b
AC
10420 return;
10421 end if;
10422 end;
10423 end if;
10424 end;
10425
10426 -- For float to float conversions, we are done
10427
10428 if Is_Floating_Point_Type (Xtyp)
10429 and then
10430 Is_Floating_Point_Type (Btyp)
70482933
RK
10431 then
10432 return;
10433 end if;
10434
fbf5a39b 10435 -- Otherwise rewrite the conversion as described above
70482933
RK
10436
10437 Conv := Relocate_Node (N);
eaa826f8 10438 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
70482933
RK
10439 Set_Etype (Conv, Btyp);
10440
f02b8bb8
RD
10441 -- Enable overflow except for case of integer to float conversions,
10442 -- where it is never required, since we can never have overflow in
10443 -- this case.
70482933 10444
fbf5a39b
AC
10445 if not Is_Integer_Type (Etype (Operand)) then
10446 Enable_Overflow_Check (Conv);
70482933
RK
10447 end if;
10448
191fcb3a 10449 Tnn := Make_Temporary (Loc, 'T', Conv);
70482933
RK
10450
10451 Insert_Actions (N, New_List (
10452 Make_Object_Declaration (Loc,
10453 Defining_Identifier => Tnn,
10454 Object_Definition => New_Occurrence_Of (Btyp, Loc),
0ac2a660
AC
10455 Constant_Present => True,
10456 Expression => Conv),
70482933
RK
10457
10458 Make_Raise_Constraint_Error (Loc,
07fc65c4
GB
10459 Condition =>
10460 Make_Or_Else (Loc,
10461 Left_Opnd =>
10462 Make_Op_Lt (Loc,
10463 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
10464 Right_Opnd =>
10465 Make_Attribute_Reference (Loc,
10466 Attribute_Name => Name_First,
10467 Prefix =>
10468 New_Occurrence_Of (Target_Type, Loc))),
70482933 10469
07fc65c4
GB
10470 Right_Opnd =>
10471 Make_Op_Gt (Loc,
10472 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
10473 Right_Opnd =>
10474 Make_Attribute_Reference (Loc,
10475 Attribute_Name => Name_Last,
10476 Prefix =>
10477 New_Occurrence_Of (Target_Type, Loc)))),
10478 Reason => CE_Range_Check_Failed)));
70482933
RK
10479
10480 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
10481 Analyze_And_Resolve (N, Btyp);
10482 end Real_Range_Check;
10483
d15f9422
AC
10484 -----------------------------
10485 -- Has_Extra_Accessibility --
10486 -----------------------------
10487
10488 -- Returns true for a formal of an anonymous access type or for
10489 -- an Ada 2012-style stand-alone object of an anonymous access type.
10490
10491 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
10492 begin
10493 if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then
10494 return Present (Effective_Extra_Accessibility (Id));
10495 else
10496 return False;
10497 end if;
10498 end Has_Extra_Accessibility;
10499
70482933
RK
10500 -- Start of processing for Expand_N_Type_Conversion
10501
10502 begin
83851b23 10503 -- First remove check marks put by the semantic analysis on the type
b2502161
AC
10504 -- conversion between array types. We need these checks, and they will
10505 -- be generated by this expansion routine, but we do not depend on these
10506 -- flags being set, and since we do intend to expand the checks in the
10507 -- front end, we don't want them on the tree passed to the back end.
83851b23
AC
10508
10509 if Is_Array_Type (Target_Type) then
10510 if Is_Constrained (Target_Type) then
10511 Set_Do_Length_Check (N, False);
10512 else
10513 Set_Do_Range_Check (Operand, False);
10514 end if;
10515 end if;
10516
685094bf 10517 -- Nothing at all to do if conversion is to the identical type so remove
76efd572
AC
10518 -- the conversion completely, it is useless, except that it may carry
10519 -- an Assignment_OK attribute, which must be propagated to the operand.
70482933
RK
10520
10521 if Operand_Type = Target_Type then
7b00e31d
AC
10522 if Assignment_OK (N) then
10523 Set_Assignment_OK (Operand);
10524 end if;
10525
fbf5a39b 10526 Rewrite (N, Relocate_Node (Operand));
e606088a 10527 goto Done;
70482933
RK
10528 end if;
10529
685094bf
RD
10530 -- Nothing to do if this is the second argument of read. This is a
10531 -- "backwards" conversion that will be handled by the specialized code
10532 -- in attribute processing.
70482933
RK
10533
10534 if Nkind (Parent (N)) = N_Attribute_Reference
10535 and then Attribute_Name (Parent (N)) = Name_Read
10536 and then Next (First (Expressions (Parent (N)))) = N
10537 then
e606088a
AC
10538 goto Done;
10539 end if;
10540
10541 -- Check for case of converting to a type that has an invariant
10542 -- associated with it. This required an invariant check. We convert
10543
10544 -- typ (expr)
10545
10546 -- into
10547
10548 -- do invariant_check (typ (expr)) in typ (expr);
10549
10550 -- using Duplicate_Subexpr to avoid multiple side effects
10551
10552 -- Note: the Comes_From_Source check, and then the resetting of this
10553 -- flag prevents what would otherwise be an infinite recursion.
10554
fd0ff1cf
RD
10555 if Has_Invariants (Target_Type)
10556 and then Present (Invariant_Procedure (Target_Type))
e606088a
AC
10557 and then Comes_From_Source (N)
10558 then
10559 Set_Comes_From_Source (N, False);
10560 Rewrite (N,
10561 Make_Expression_With_Actions (Loc,
10562 Actions => New_List (
10563 Make_Invariant_Call (Duplicate_Subexpr (N))),
10564 Expression => Duplicate_Subexpr_No_Checks (N)));
10565 Analyze_And_Resolve (N, Target_Type);
10566 goto Done;
70482933
RK
10567 end if;
10568
10569 -- Here if we may need to expand conversion
10570
eaa826f8
RD
10571 -- If the operand of the type conversion is an arithmetic operation on
10572 -- signed integers, and the based type of the signed integer type in
10573 -- question is smaller than Standard.Integer, we promote both of the
10574 -- operands to type Integer.
10575
10576 -- For example, if we have
10577
10578 -- target-type (opnd1 + opnd2)
10579
10580 -- and opnd1 and opnd2 are of type short integer, then we rewrite
10581 -- this as:
10582
10583 -- target-type (integer(opnd1) + integer(opnd2))
10584
10585 -- We do this because we are always allowed to compute in a larger type
10586 -- if we do the right thing with the result, and in this case we are
10587 -- going to do a conversion which will do an appropriate check to make
10588 -- sure that things are in range of the target type in any case. This
10589 -- avoids some unnecessary intermediate overflows.
10590
dfcfdc0a
AC
10591 -- We might consider a similar transformation in the case where the
10592 -- target is a real type or a 64-bit integer type, and the operand
10593 -- is an arithmetic operation using a 32-bit integer type. However,
10594 -- we do not bother with this case, because it could cause significant
308e6f3a 10595 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
dfcfdc0a
AC
10596 -- much cheaper, but we don't want different behavior on 32-bit and
10597 -- 64-bit machines. Note that the exclusion of the 64-bit case also
10598 -- handles the configurable run-time cases where 64-bit arithmetic
10599 -- may simply be unavailable.
eaa826f8
RD
10600
10601 -- Note: this circuit is partially redundant with respect to the circuit
10602 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
10603 -- the processing here. Also we still need the Checks circuit, since we
10604 -- have to be sure not to generate junk overflow checks in the first
a90bd866 10605 -- place, since it would be trick to remove them here.
eaa826f8 10606
fdfcc663 10607 if Integer_Promotion_Possible (N) then
eaa826f8 10608
fdfcc663 10609 -- All conditions met, go ahead with transformation
eaa826f8 10610
fdfcc663
AC
10611 declare
10612 Opnd : Node_Id;
10613 L, R : Node_Id;
dfcfdc0a 10614
fdfcc663
AC
10615 begin
10616 R :=
10617 Make_Type_Conversion (Loc,
e4494292 10618 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
fdfcc663 10619 Expression => Relocate_Node (Right_Opnd (Operand)));
eaa826f8 10620
5f3f175d
AC
10621 Opnd := New_Op_Node (Nkind (Operand), Loc);
10622 Set_Right_Opnd (Opnd, R);
eaa826f8 10623
5f3f175d 10624 if Nkind (Operand) in N_Binary_Op then
fdfcc663 10625 L :=
eaa826f8 10626 Make_Type_Conversion (Loc,
e4494292 10627 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
fdfcc663
AC
10628 Expression => Relocate_Node (Left_Opnd (Operand)));
10629
5f3f175d
AC
10630 Set_Left_Opnd (Opnd, L);
10631 end if;
eaa826f8 10632
5f3f175d
AC
10633 Rewrite (N,
10634 Make_Type_Conversion (Loc,
10635 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
10636 Expression => Opnd));
dfcfdc0a 10637
5f3f175d 10638 Analyze_And_Resolve (N, Target_Type);
e606088a 10639 goto Done;
fdfcc663
AC
10640 end;
10641 end if;
eaa826f8 10642
f82944b7
JM
10643 -- Do validity check if validity checking operands
10644
533369aa 10645 if Validity_Checks_On and Validity_Check_Operands then
f82944b7
JM
10646 Ensure_Valid (Operand);
10647 end if;
10648
70482933
RK
10649 -- Special case of converting from non-standard boolean type
10650
10651 if Is_Boolean_Type (Operand_Type)
10652 and then (Nonzero_Is_True (Operand_Type))
10653 then
10654 Adjust_Condition (Operand);
10655 Set_Etype (Operand, Standard_Boolean);
10656 Operand_Type := Standard_Boolean;
10657 end if;
10658
10659 -- Case of converting to an access type
10660
10661 if Is_Access_Type (Target_Type) then
10662
d766cee3
RD
10663 -- Apply an accessibility check when the conversion operand is an
10664 -- access parameter (or a renaming thereof), unless conversion was
e84e11ba
GD
10665 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
10666 -- Note that other checks may still need to be applied below (such
10667 -- as tagged type checks).
70482933
RK
10668
10669 if Is_Entity_Name (Operand)
d15f9422 10670 and then Has_Extra_Accessibility (Entity (Operand))
70482933 10671 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
d766cee3
RD
10672 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
10673 or else Attribute_Name (Original_Node (N)) = Name_Access)
70482933 10674 then
e84e11ba
GD
10675 Apply_Accessibility_Check
10676 (Operand, Target_Type, Insert_Node => Operand);
70482933 10677
e84e11ba 10678 -- If the level of the operand type is statically deeper than the
685094bf
RD
10679 -- level of the target type, then force Program_Error. Note that this
10680 -- can only occur for cases where the attribute is within the body of
6c56d9b8
AC
10681 -- an instantiation, otherwise the conversion will already have been
10682 -- rejected as illegal.
10683
10684 -- Note: warnings are issued by the analyzer for the instance cases
70482933
RK
10685
10686 elsif In_Instance_Body
6c56d9b8
AC
10687
10688 -- The case where the target type is an anonymous access type of
10689 -- a discriminant is excluded, because the level of such a type
10690 -- depends on the context and currently the level returned for such
10691 -- types is zero, resulting in warnings about about check failures
10692 -- in certain legal cases involving class-wide interfaces as the
10693 -- designated type (some cases, such as return statements, are
10694 -- checked at run time, but not clear if these are handled right
10695 -- in general, see 3.10.2(12/2-12.5/3) ???).
10696
ad5edba5
AC
10697 and then
10698 not (Ekind (Target_Type) = E_Anonymous_Access_Type
10699 and then Present (Associated_Node_For_Itype (Target_Type))
10700 and then Nkind (Associated_Node_For_Itype (Target_Type)) =
10701 N_Discriminant_Specification)
10702 and then
10703 Type_Access_Level (Operand_Type) > Type_Access_Level (Target_Type)
70482933 10704 then
426908f8 10705 Raise_Accessibility_Error;
91669e7e 10706 goto Done;
70482933 10707
685094bf
RD
10708 -- When the operand is a selected access discriminant the check needs
10709 -- to be made against the level of the object denoted by the prefix
10710 -- of the selected name. Force Program_Error for this case as well
10711 -- (this accessibility violation can only happen if within the body
10712 -- of an instantiation).
70482933
RK
10713
10714 elsif In_Instance_Body
10715 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
10716 and then Nkind (Operand) = N_Selected_Component
10717 and then Object_Access_Level (Operand) >
10718 Type_Access_Level (Target_Type)
10719 then
426908f8 10720 Raise_Accessibility_Error;
e606088a 10721 goto Done;
70482933
RK
10722 end if;
10723 end if;
10724
10725 -- Case of conversions of tagged types and access to tagged types
10726
685094bf
RD
10727 -- When needed, that is to say when the expression is class-wide, Add
10728 -- runtime a tag check for (strict) downward conversion by using the
10729 -- membership test, generating:
70482933
RK
10730
10731 -- [constraint_error when Operand not in Target_Type'Class]
10732
10733 -- or in the access type case
10734
10735 -- [constraint_error
10736 -- when Operand /= null
10737 -- and then Operand.all not in
10738 -- Designated_Type (Target_Type)'Class]
10739
10740 if (Is_Access_Type (Target_Type)
10741 and then Is_Tagged_Type (Designated_Type (Target_Type)))
10742 or else Is_Tagged_Type (Target_Type)
10743 then
685094bf
RD
10744 -- Do not do any expansion in the access type case if the parent is a
10745 -- renaming, since this is an error situation which will be caught by
10746 -- Sem_Ch8, and the expansion can interfere with this error check.
70482933 10747
e7e4d230 10748 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
e606088a 10749 goto Done;
70482933
RK
10750 end if;
10751
0669bebe 10752 -- Otherwise, proceed with processing tagged conversion
70482933 10753
e7e4d230 10754 Tagged_Conversion : declare
8cea7b64
HK
10755 Actual_Op_Typ : Entity_Id;
10756 Actual_Targ_Typ : Entity_Id;
10757 Make_Conversion : Boolean := False;
10758 Root_Op_Typ : Entity_Id;
70482933 10759
8cea7b64
HK
10760 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
10761 -- Create a membership check to test whether Operand is a member
10762 -- of Targ_Typ. If the original Target_Type is an access, include
10763 -- a test for null value. The check is inserted at N.
10764
10765 --------------------
10766 -- Make_Tag_Check --
10767 --------------------
10768
10769 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
10770 Cond : Node_Id;
10771
10772 begin
10773 -- Generate:
10774 -- [Constraint_Error
10775 -- when Operand /= null
10776 -- and then Operand.all not in Targ_Typ]
10777
10778 if Is_Access_Type (Target_Type) then
10779 Cond :=
10780 Make_And_Then (Loc,
10781 Left_Opnd =>
10782 Make_Op_Ne (Loc,
10783 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10784 Right_Opnd => Make_Null (Loc)),
10785
10786 Right_Opnd =>
10787 Make_Not_In (Loc,
10788 Left_Opnd =>
10789 Make_Explicit_Dereference (Loc,
10790 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
e4494292 10791 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)));
8cea7b64
HK
10792
10793 -- Generate:
10794 -- [Constraint_Error when Operand not in Targ_Typ]
10795
10796 else
10797 Cond :=
10798 Make_Not_In (Loc,
10799 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
e4494292 10800 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc));
8cea7b64
HK
10801 end if;
10802
10803 Insert_Action (N,
10804 Make_Raise_Constraint_Error (Loc,
10805 Condition => Cond,
10806 Reason => CE_Tag_Check_Failed));
10807 end Make_Tag_Check;
10808
e7e4d230 10809 -- Start of processing for Tagged_Conversion
70482933
RK
10810
10811 begin
9732e886 10812 -- Handle entities from the limited view
852dba80 10813
9732e886 10814 if Is_Access_Type (Operand_Type) then
852dba80
AC
10815 Actual_Op_Typ :=
10816 Available_View (Designated_Type (Operand_Type));
9732e886
JM
10817 else
10818 Actual_Op_Typ := Operand_Type;
10819 end if;
10820
10821 if Is_Access_Type (Target_Type) then
852dba80
AC
10822 Actual_Targ_Typ :=
10823 Available_View (Designated_Type (Target_Type));
70482933 10824 else
8cea7b64 10825 Actual_Targ_Typ := Target_Type;
70482933
RK
10826 end if;
10827
8cea7b64
HK
10828 Root_Op_Typ := Root_Type (Actual_Op_Typ);
10829
20b5d666
JM
10830 -- Ada 2005 (AI-251): Handle interface type conversion
10831
3cb9a885 10832 if Is_Interface (Actual_Op_Typ)
58b81ab0
AC
10833 or else
10834 Is_Interface (Actual_Targ_Typ)
3cb9a885 10835 then
f6f4d8d4 10836 Expand_Interface_Conversion (N);
e606088a 10837 goto Done;
20b5d666
JM
10838 end if;
10839
8cea7b64 10840 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
70482933 10841
8cea7b64
HK
10842 -- Create a runtime tag check for a downward class-wide type
10843 -- conversion.
70482933 10844
8cea7b64 10845 if Is_Class_Wide_Type (Actual_Op_Typ)
852dba80 10846 and then Actual_Op_Typ /= Actual_Targ_Typ
8cea7b64 10847 and then Root_Op_Typ /= Actual_Targ_Typ
4ac2477e
JM
10848 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
10849 Use_Full_View => True)
8cea7b64
HK
10850 then
10851 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
10852 Make_Conversion := True;
10853 end if;
70482933 10854
8cea7b64
HK
10855 -- AI05-0073: If the result subtype of the function is defined
10856 -- by an access_definition designating a specific tagged type
10857 -- T, a check is made that the result value is null or the tag
10858 -- of the object designated by the result value identifies T.
10859 -- Constraint_Error is raised if this check fails.
70482933 10860
92a7cd46 10861 if Nkind (Parent (N)) = N_Simple_Return_Statement then
8cea7b64 10862 declare
e886436a 10863 Func : Entity_Id;
8cea7b64
HK
10864 Func_Typ : Entity_Id;
10865
10866 begin
e886436a 10867 -- Climb scope stack looking for the enclosing function
8cea7b64 10868
e886436a 10869 Func := Current_Scope;
8cea7b64
HK
10870 while Present (Func)
10871 and then Ekind (Func) /= E_Function
10872 loop
10873 Func := Scope (Func);
10874 end loop;
10875
10876 -- The function's return subtype must be defined using
10877 -- an access definition.
10878
10879 if Nkind (Result_Definition (Parent (Func))) =
10880 N_Access_Definition
10881 then
10882 Func_Typ := Directly_Designated_Type (Etype (Func));
10883
10884 -- The return subtype denotes a specific tagged type,
10885 -- in other words, a non class-wide type.
10886
10887 if Is_Tagged_Type (Func_Typ)
10888 and then not Is_Class_Wide_Type (Func_Typ)
10889 then
10890 Make_Tag_Check (Actual_Targ_Typ);
10891 Make_Conversion := True;
10892 end if;
10893 end if;
10894 end;
70482933
RK
10895 end if;
10896
8cea7b64
HK
10897 -- We have generated a tag check for either a class-wide type
10898 -- conversion or for AI05-0073.
70482933 10899
8cea7b64
HK
10900 if Make_Conversion then
10901 declare
10902 Conv : Node_Id;
10903 begin
10904 Conv :=
10905 Make_Unchecked_Type_Conversion (Loc,
10906 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
10907 Expression => Relocate_Node (Expression (N)));
10908 Rewrite (N, Conv);
10909 Analyze_And_Resolve (N, Target_Type);
10910 end;
10911 end if;
70482933 10912 end if;
e7e4d230 10913 end Tagged_Conversion;
70482933
RK
10914
10915 -- Case of other access type conversions
10916
10917 elsif Is_Access_Type (Target_Type) then
10918 Apply_Constraint_Check (Operand, Target_Type);
10919
10920 -- Case of conversions from a fixed-point type
10921
685094bf
RD
10922 -- These conversions require special expansion and processing, found in
10923 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
10924 -- since from a semantic point of view, these are simple integer
70482933
RK
10925 -- conversions, which do not need further processing.
10926
10927 elsif Is_Fixed_Point_Type (Operand_Type)
10928 and then not Conversion_OK (N)
10929 then
10930 -- We should never see universal fixed at this case, since the
10931 -- expansion of the constituent divide or multiply should have
10932 -- eliminated the explicit mention of universal fixed.
10933
10934 pragma Assert (Operand_Type /= Universal_Fixed);
10935
685094bf
RD
10936 -- Check for special case of the conversion to universal real that
10937 -- occurs as a result of the use of a round attribute. In this case,
10938 -- the real type for the conversion is taken from the target type of
10939 -- the Round attribute and the result must be marked as rounded.
70482933
RK
10940
10941 if Target_Type = Universal_Real
10942 and then Nkind (Parent (N)) = N_Attribute_Reference
10943 and then Attribute_Name (Parent (N)) = Name_Round
10944 then
10945 Set_Rounded_Result (N);
10946 Set_Etype (N, Etype (Parent (N)));
10947 end if;
10948
10949 -- Otherwise do correct fixed-conversion, but skip these if the
e7e4d230
AC
10950 -- Conversion_OK flag is set, because from a semantic point of view
10951 -- these are simple integer conversions needing no further processing
10952 -- (the backend will simply treat them as integers).
70482933
RK
10953
10954 if not Conversion_OK (N) then
10955 if Is_Fixed_Point_Type (Etype (N)) then
10956 Expand_Convert_Fixed_To_Fixed (N);
10957 Real_Range_Check;
10958
10959 elsif Is_Integer_Type (Etype (N)) then
10960 Expand_Convert_Fixed_To_Integer (N);
10961
10962 else
10963 pragma Assert (Is_Floating_Point_Type (Etype (N)));
10964 Expand_Convert_Fixed_To_Float (N);
10965 Real_Range_Check;
10966 end if;
10967 end if;
10968
10969 -- Case of conversions to a fixed-point type
10970
685094bf
RD
10971 -- These conversions require special expansion and processing, found in
10972 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
10973 -- since from a semantic point of view, these are simple integer
10974 -- conversions, which do not need further processing.
70482933
RK
10975
10976 elsif Is_Fixed_Point_Type (Target_Type)
10977 and then not Conversion_OK (N)
10978 then
10979 if Is_Integer_Type (Operand_Type) then
10980 Expand_Convert_Integer_To_Fixed (N);
10981 Real_Range_Check;
10982 else
10983 pragma Assert (Is_Floating_Point_Type (Operand_Type));
10984 Expand_Convert_Float_To_Fixed (N);
10985 Real_Range_Check;
10986 end if;
10987
10988 -- Case of float-to-integer conversions
10989
10990 -- We also handle float-to-fixed conversions with Conversion_OK set
10991 -- since semantically the fixed-point target is treated as though it
10992 -- were an integer in such cases.
10993
10994 elsif Is_Floating_Point_Type (Operand_Type)
10995 and then
10996 (Is_Integer_Type (Target_Type)
10997 or else
10998 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
10999 then
70482933
RK
11000 -- One more check here, gcc is still not able to do conversions of
11001 -- this type with proper overflow checking, and so gigi is doing an
11002 -- approximation of what is required by doing floating-point compares
11003 -- with the end-point. But that can lose precision in some cases, and
f02b8bb8 11004 -- give a wrong result. Converting the operand to Universal_Real is
70482933 11005 -- helpful, but still does not catch all cases with 64-bit integers
e7e4d230 11006 -- on targets with only 64-bit floats.
0669bebe
GB
11007
11008 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
11009 -- Can this code be removed ???
70482933 11010
fbf5a39b
AC
11011 if Do_Range_Check (Operand) then
11012 Rewrite (Operand,
70482933
RK
11013 Make_Type_Conversion (Loc,
11014 Subtype_Mark =>
f02b8bb8 11015 New_Occurrence_Of (Universal_Real, Loc),
70482933 11016 Expression =>
fbf5a39b 11017 Relocate_Node (Operand)));
70482933 11018
f02b8bb8 11019 Set_Etype (Operand, Universal_Real);
fbf5a39b
AC
11020 Enable_Range_Check (Operand);
11021 Set_Do_Range_Check (Expression (Operand), False);
70482933
RK
11022 end if;
11023
11024 -- Case of array conversions
11025
685094bf
RD
11026 -- Expansion of array conversions, add required length/range checks but
11027 -- only do this if there is no change of representation. For handling of
11028 -- this case, see Handle_Changed_Representation.
70482933
RK
11029
11030 elsif Is_Array_Type (Target_Type) then
70482933
RK
11031 if Is_Constrained (Target_Type) then
11032 Apply_Length_Check (Operand, Target_Type);
11033 else
11034 Apply_Range_Check (Operand, Target_Type);
11035 end if;
11036
11037 Handle_Changed_Representation;
11038
11039 -- Case of conversions of discriminated types
11040
685094bf
RD
11041 -- Add required discriminant checks if target is constrained. Again this
11042 -- change is skipped if we have a change of representation.
70482933
RK
11043
11044 elsif Has_Discriminants (Target_Type)
11045 and then Is_Constrained (Target_Type)
11046 then
11047 Apply_Discriminant_Check (Operand, Target_Type);
11048 Handle_Changed_Representation;
11049
11050 -- Case of all other record conversions. The only processing required
11051 -- is to check for a change of representation requiring the special
11052 -- assignment processing.
11053
11054 elsif Is_Record_Type (Target_Type) then
5d09245e
AC
11055
11056 -- Ada 2005 (AI-216): Program_Error is raised when converting from
685094bf
RD
11057 -- a derived Unchecked_Union type to an unconstrained type that is
11058 -- not Unchecked_Union if the operand lacks inferable discriminants.
5d09245e
AC
11059
11060 if Is_Derived_Type (Operand_Type)
11061 and then Is_Unchecked_Union (Base_Type (Operand_Type))
11062 and then not Is_Constrained (Target_Type)
11063 and then not Is_Unchecked_Union (Base_Type (Target_Type))
11064 and then not Has_Inferable_Discriminants (Operand)
11065 then
685094bf 11066 -- To prevent Gigi from generating illegal code, we generate a
5d09245e 11067 -- Program_Error node, but we give it the target type of the
6cb3037c 11068 -- conversion (is this requirement documented somewhere ???)
5d09245e
AC
11069
11070 declare
11071 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
11072 Reason => PE_Unchecked_Union_Restriction);
11073
11074 begin
11075 Set_Etype (PE, Target_Type);
11076 Rewrite (N, PE);
11077
11078 end;
11079 else
11080 Handle_Changed_Representation;
11081 end if;
70482933
RK
11082
11083 -- Case of conversions of enumeration types
11084
11085 elsif Is_Enumeration_Type (Target_Type) then
11086
11087 -- Special processing is required if there is a change of
e7e4d230 11088 -- representation (from enumeration representation clauses).
70482933
RK
11089
11090 if not Same_Representation (Target_Type, Operand_Type) then
11091
11092 -- Convert: x(y) to x'val (ytyp'val (y))
11093
11094 Rewrite (N,
1c66c4f5
AC
11095 Make_Attribute_Reference (Loc,
11096 Prefix => New_Occurrence_Of (Target_Type, Loc),
11097 Attribute_Name => Name_Val,
11098 Expressions => New_List (
11099 Make_Attribute_Reference (Loc,
11100 Prefix => New_Occurrence_Of (Operand_Type, Loc),
11101 Attribute_Name => Name_Pos,
11102 Expressions => New_List (Operand)))));
70482933
RK
11103
11104 Analyze_And_Resolve (N, Target_Type);
11105 end if;
11106
11107 -- Case of conversions to floating-point
11108
11109 elsif Is_Floating_Point_Type (Target_Type) then
11110 Real_Range_Check;
70482933
RK
11111 end if;
11112
685094bf 11113 -- At this stage, either the conversion node has been transformed into
e7e4d230
AC
11114 -- some other equivalent expression, or left as a conversion that can be
11115 -- handled by Gigi, in the following cases:
70482933
RK
11116
11117 -- Conversions with no change of representation or type
11118
685094bf
RD
11119 -- Numeric conversions involving integer, floating- and fixed-point
11120 -- values. Fixed-point values are allowed only if Conversion_OK is
11121 -- set, i.e. if the fixed-point values are to be treated as integers.
70482933 11122
5e1c00fa
RD
11123 -- No other conversions should be passed to Gigi
11124
11125 -- Check: are these rules stated in sinfo??? if so, why restate here???
70482933 11126
685094bf
RD
11127 -- The only remaining step is to generate a range check if we still have
11128 -- a type conversion at this stage and Do_Range_Check is set. For now we
f5655e4a
AC
11129 -- do this only for conversions of discrete types and for float-to-float
11130 -- conversions.
fbf5a39b 11131
7b536495 11132 if Nkind (N) = N_Type_Conversion then
fbf5a39b 11133
f5655e4a
AC
11134 -- For now we only support floating-point cases where both source
11135 -- and target are floating-point types. Conversions where the source
11136 -- and target involve integer or fixed-point types are still TBD,
11137 -- though not clear whether those can even happen at this point, due
11138 -- to transformations above. ???
fbf5a39b 11139
7b536495 11140 if Is_Floating_Point_Type (Etype (N))
f5655e4a 11141 and then Is_Floating_Point_Type (Etype (Expression (N)))
7b536495
AC
11142 then
11143 if Do_Range_Check (Expression (N))
11144 and then Is_Floating_Point_Type (Target_Type)
11145 then
11146 Generate_Range_Check
11147 (Expression (N), Target_Type, CE_Range_Check_Failed);
11148 end if;
fbf5a39b 11149
f5655e4a
AC
11150 -- Discrete-to-discrete conversions
11151
7b536495
AC
11152 elsif Is_Discrete_Type (Etype (N)) then
11153 declare
11154 Expr : constant Node_Id := Expression (N);
11155 Ftyp : Entity_Id;
11156 Ityp : Entity_Id;
fbf5a39b 11157
7b536495
AC
11158 begin
11159 if Do_Range_Check (Expr)
11160 and then Is_Discrete_Type (Etype (Expr))
fbf5a39b 11161 then
7b536495 11162 Set_Do_Range_Check (Expr, False);
fbf5a39b 11163
7b536495
AC
11164 -- Before we do a range check, we have to deal with treating
11165 -- a fixed-point operand as an integer. The way we do this
11166 -- is simply to do an unchecked conversion to an appropriate
11167 -- integer type large enough to hold the result.
fbf5a39b 11168
7b536495
AC
11169 -- This code is not active yet, because we are only dealing
11170 -- with discrete types so far ???
fbf5a39b 11171
7b536495
AC
11172 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
11173 and then Treat_Fixed_As_Integer (Expr)
11174 then
11175 Ftyp := Base_Type (Etype (Expr));
fbf5a39b 11176
7b536495
AC
11177 if Esize (Ftyp) >= Esize (Standard_Integer) then
11178 Ityp := Standard_Long_Long_Integer;
11179 else
11180 Ityp := Standard_Integer;
11181 end if;
edab6088 11182
7b536495
AC
11183 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
11184 end if;
11185
11186 -- Reset overflow flag, since the range check will include
11187 -- dealing with possible overflow, and generate the check.
11188 -- If Address is either a source type or target type,
11189 -- suppress range check to avoid typing anomalies when
11190 -- it is a visible integer type.
11191
11192 Set_Do_Overflow_Check (N, False);
11193
11194 if not Is_Descendent_Of_Address (Etype (Expr))
11195 and then not Is_Descendent_Of_Address (Target_Type)
11196 then
11197 Generate_Range_Check
11198 (Expr, Target_Type, CE_Range_Check_Failed);
11199 end if;
8a36a0cc 11200 end if;
7b536495
AC
11201 end;
11202 end if;
fbf5a39b 11203 end if;
f02b8bb8 11204
e606088a
AC
11205 -- Here at end of processing
11206
48f91b44
RD
11207 <<Done>>
11208 -- Apply predicate check if required. Note that we can't just call
11209 -- Apply_Predicate_Check here, because the type looks right after
11210 -- the conversion and it would omit the check. The Comes_From_Source
11211 -- guard is necessary to prevent infinite recursions when we generate
11212 -- internal conversions for the purpose of checking predicates.
11213
11214 if Present (Predicate_Function (Target_Type))
11215 and then Target_Type /= Operand_Type
11216 and then Comes_From_Source (N)
11217 then
00332244
AC
11218 declare
11219 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
11220
11221 begin
11222 -- Avoid infinite recursion on the subsequent expansion of
11223 -- of the copy of the original type conversion.
11224
11225 Set_Comes_From_Source (New_Expr, False);
11226 Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr));
11227 end;
48f91b44 11228 end if;
70482933
RK
11229 end Expand_N_Type_Conversion;
11230
11231 -----------------------------------
11232 -- Expand_N_Unchecked_Expression --
11233 -----------------------------------
11234
e7e4d230 11235 -- Remove the unchecked expression node from the tree. Its job was simply
70482933
RK
11236 -- to make sure that its constituent expression was handled with checks
11237 -- off, and now that that is done, we can remove it from the tree, and
e7e4d230 11238 -- indeed must, since Gigi does not expect to see these nodes.
70482933
RK
11239
11240 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
11241 Exp : constant Node_Id := Expression (N);
70482933 11242 begin
e7e4d230 11243 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
70482933
RK
11244 Rewrite (N, Exp);
11245 end Expand_N_Unchecked_Expression;
11246
11247 ----------------------------------------
11248 -- Expand_N_Unchecked_Type_Conversion --
11249 ----------------------------------------
11250
685094bf
RD
11251 -- If this cannot be handled by Gigi and we haven't already made a
11252 -- temporary for it, do it now.
70482933
RK
11253
11254 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
11255 Target_Type : constant Entity_Id := Etype (N);
11256 Operand : constant Node_Id := Expression (N);
11257 Operand_Type : constant Entity_Id := Etype (Operand);
11258
11259 begin
7b00e31d 11260 -- Nothing at all to do if conversion is to the identical type so remove
76efd572 11261 -- the conversion completely, it is useless, except that it may carry
e7e4d230 11262 -- an Assignment_OK indication which must be propagated to the operand.
7b00e31d
AC
11263
11264 if Operand_Type = Target_Type then
13d923cc 11265
e7e4d230
AC
11266 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
11267
7b00e31d
AC
11268 if Assignment_OK (N) then
11269 Set_Assignment_OK (Operand);
11270 end if;
11271
11272 Rewrite (N, Relocate_Node (Operand));
11273 return;
11274 end if;
11275
70482933
RK
11276 -- If we have a conversion of a compile time known value to a target
11277 -- type and the value is in range of the target type, then we can simply
11278 -- replace the construct by an integer literal of the correct type. We
11279 -- only apply this to integer types being converted. Possibly it may
11280 -- apply in other cases, but it is too much trouble to worry about.
11281
11282 -- Note that we do not do this transformation if the Kill_Range_Check
11283 -- flag is set, since then the value may be outside the expected range.
11284 -- This happens in the Normalize_Scalars case.
11285
20b5d666
JM
11286 -- We also skip this if either the target or operand type is biased
11287 -- because in this case, the unchecked conversion is supposed to
11288 -- preserve the bit pattern, not the integer value.
11289
70482933 11290 if Is_Integer_Type (Target_Type)
20b5d666 11291 and then not Has_Biased_Representation (Target_Type)
70482933 11292 and then Is_Integer_Type (Operand_Type)
20b5d666 11293 and then not Has_Biased_Representation (Operand_Type)
70482933
RK
11294 and then Compile_Time_Known_Value (Operand)
11295 and then not Kill_Range_Check (N)
11296 then
11297 declare
11298 Val : constant Uint := Expr_Value (Operand);
11299
11300 begin
11301 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
11302 and then
11303 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
11304 and then
11305 Val >= Expr_Value (Type_Low_Bound (Target_Type))
11306 and then
11307 Val <= Expr_Value (Type_High_Bound (Target_Type))
11308 then
11309 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
8a36a0cc 11310
685094bf
RD
11311 -- If Address is the target type, just set the type to avoid a
11312 -- spurious type error on the literal when Address is a visible
11313 -- integer type.
8a36a0cc
AC
11314
11315 if Is_Descendent_Of_Address (Target_Type) then
11316 Set_Etype (N, Target_Type);
11317 else
11318 Analyze_And_Resolve (N, Target_Type);
11319 end if;
11320
70482933
RK
11321 return;
11322 end if;
11323 end;
11324 end if;
11325
11326 -- Nothing to do if conversion is safe
11327
11328 if Safe_Unchecked_Type_Conversion (N) then
11329 return;
11330 end if;
11331
11332 -- Otherwise force evaluation unless Assignment_OK flag is set (this
324ac540 11333 -- flag indicates ??? More comments needed here)
70482933
RK
11334
11335 if Assignment_OK (N) then
11336 null;
11337 else
11338 Force_Evaluation (N);
11339 end if;
11340 end Expand_N_Unchecked_Type_Conversion;
11341
11342 ----------------------------
11343 -- Expand_Record_Equality --
11344 ----------------------------
11345
11346 -- For non-variant records, Equality is expanded when needed into:
11347
11348 -- and then Lhs.Discr1 = Rhs.Discr1
11349 -- and then ...
11350 -- and then Lhs.Discrn = Rhs.Discrn
11351 -- and then Lhs.Cmp1 = Rhs.Cmp1
11352 -- and then ...
11353 -- and then Lhs.Cmpn = Rhs.Cmpn
11354
11355 -- The expression is folded by the back-end for adjacent fields. This
11356 -- function is called for tagged record in only one occasion: for imple-
11357 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
11358 -- otherwise the primitive "=" is used directly.
11359
11360 function Expand_Record_Equality
11361 (Nod : Node_Id;
11362 Typ : Entity_Id;
11363 Lhs : Node_Id;
11364 Rhs : Node_Id;
2e071734 11365 Bodies : List_Id) return Node_Id
70482933
RK
11366 is
11367 Loc : constant Source_Ptr := Sloc (Nod);
11368
0ab80019
AC
11369 Result : Node_Id;
11370 C : Entity_Id;
11371
11372 First_Time : Boolean := True;
11373
6b670dcf
AC
11374 function Element_To_Compare (C : Entity_Id) return Entity_Id;
11375 -- Return the next discriminant or component to compare, starting with
11376 -- C, skipping inherited components.
0ab80019 11377
6b670dcf
AC
11378 ------------------------
11379 -- Element_To_Compare --
11380 ------------------------
70482933 11381
6b670dcf
AC
11382 function Element_To_Compare (C : Entity_Id) return Entity_Id is
11383 Comp : Entity_Id;
28270211 11384
70482933 11385 begin
6b670dcf 11386 Comp := C;
6b670dcf
AC
11387 loop
11388 -- Exit loop when the next element to be compared is found, or
11389 -- there is no more such element.
70482933 11390
6b670dcf 11391 exit when No (Comp);
8190087e 11392
6b670dcf
AC
11393 exit when Ekind_In (Comp, E_Discriminant, E_Component)
11394 and then not (
70482933 11395
6b670dcf 11396 -- Skip inherited components
70482933 11397
6b670dcf
AC
11398 -- Note: for a tagged type, we always generate the "=" primitive
11399 -- for the base type (not on the first subtype), so the test for
11400 -- Comp /= Original_Record_Component (Comp) is True for
11401 -- inherited components only.
24558db8 11402
6b670dcf 11403 (Is_Tagged_Type (Typ)
28270211 11404 and then Comp /= Original_Record_Component (Comp))
24558db8 11405
6b670dcf 11406 -- Skip _Tag
26bff3d9 11407
6b670dcf
AC
11408 or else Chars (Comp) = Name_uTag
11409
6b670dcf
AC
11410 -- Skip interface elements (secondary tags???)
11411
11412 or else Is_Interface (Etype (Comp)));
11413
11414 Next_Entity (Comp);
11415 end loop;
11416
11417 return Comp;
11418 end Element_To_Compare;
70482933 11419
70482933
RK
11420 -- Start of processing for Expand_Record_Equality
11421
11422 begin
70482933
RK
11423 -- Generates the following code: (assuming that Typ has one Discr and
11424 -- component C2 is also a record)
11425
11426 -- True
11427 -- and then Lhs.Discr1 = Rhs.Discr1
11428 -- and then Lhs.C1 = Rhs.C1
11429 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
11430 -- and then ...
11431 -- and then Lhs.Cmpn = Rhs.Cmpn
11432
e4494292 11433 Result := New_Occurrence_Of (Standard_True, Loc);
6b670dcf 11434 C := Element_To_Compare (First_Entity (Typ));
70482933 11435 while Present (C) loop
70482933
RK
11436 declare
11437 New_Lhs : Node_Id;
11438 New_Rhs : Node_Id;
8aceda64 11439 Check : Node_Id;
70482933
RK
11440
11441 begin
11442 if First_Time then
11443 First_Time := False;
11444 New_Lhs := Lhs;
11445 New_Rhs := Rhs;
70482933
RK
11446 else
11447 New_Lhs := New_Copy_Tree (Lhs);
11448 New_Rhs := New_Copy_Tree (Rhs);
11449 end if;
11450
8aceda64
AC
11451 Check :=
11452 Expand_Composite_Equality (Nod, Etype (C),
11453 Lhs =>
11454 Make_Selected_Component (Loc,
8d80ff64 11455 Prefix => New_Lhs,
e4494292 11456 Selector_Name => New_Occurrence_Of (C, Loc)),
8aceda64
AC
11457 Rhs =>
11458 Make_Selected_Component (Loc,
8d80ff64 11459 Prefix => New_Rhs,
e4494292 11460 Selector_Name => New_Occurrence_Of (C, Loc)),
8aceda64
AC
11461 Bodies => Bodies);
11462
11463 -- If some (sub)component is an unchecked_union, the whole
11464 -- operation will raise program error.
11465
11466 if Nkind (Check) = N_Raise_Program_Error then
11467 Result := Check;
11468 Set_Etype (Result, Standard_Boolean);
11469 exit;
11470 else
11471 Result :=
11472 Make_And_Then (Loc,
11473 Left_Opnd => Result,
11474 Right_Opnd => Check);
11475 end if;
70482933
RK
11476 end;
11477
6b670dcf 11478 C := Element_To_Compare (Next_Entity (C));
70482933
RK
11479 end loop;
11480
11481 return Result;
11482 end Expand_Record_Equality;
11483
a3068ca6
AC
11484 ---------------------------
11485 -- Expand_Set_Membership --
11486 ---------------------------
11487
11488 procedure Expand_Set_Membership (N : Node_Id) is
11489 Lop : constant Node_Id := Left_Opnd (N);
11490 Alt : Node_Id;
11491 Res : Node_Id;
11492
11493 function Make_Cond (Alt : Node_Id) return Node_Id;
11494 -- If the alternative is a subtype mark, create a simple membership
11495 -- test. Otherwise create an equality test for it.
11496
11497 ---------------
11498 -- Make_Cond --
11499 ---------------
11500
11501 function Make_Cond (Alt : Node_Id) return Node_Id is
11502 Cond : Node_Id;
11503 L : constant Node_Id := New_Copy (Lop);
11504 R : constant Node_Id := Relocate_Node (Alt);
11505
11506 begin
11507 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
11508 or else Nkind (Alt) = N_Range
11509 then
11510 Cond :=
11511 Make_In (Sloc (Alt),
11512 Left_Opnd => L,
11513 Right_Opnd => R);
11514 else
11515 Cond :=
11516 Make_Op_Eq (Sloc (Alt),
11517 Left_Opnd => L,
11518 Right_Opnd => R);
11519 end if;
11520
11521 return Cond;
11522 end Make_Cond;
11523
11524 -- Start of processing for Expand_Set_Membership
11525
11526 begin
11527 Remove_Side_Effects (Lop);
11528
11529 Alt := Last (Alternatives (N));
11530 Res := Make_Cond (Alt);
11531
11532 Prev (Alt);
11533 while Present (Alt) loop
11534 Res :=
11535 Make_Or_Else (Sloc (Alt),
11536 Left_Opnd => Make_Cond (Alt),
11537 Right_Opnd => Res);
11538 Prev (Alt);
11539 end loop;
11540
11541 Rewrite (N, Res);
11542 Analyze_And_Resolve (N, Standard_Boolean);
11543 end Expand_Set_Membership;
11544
5875f8d6
AC
11545 -----------------------------------
11546 -- Expand_Short_Circuit_Operator --
11547 -----------------------------------
11548
955871d3
AC
11549 -- Deal with special expansion if actions are present for the right operand
11550 -- and deal with optimizing case of arguments being True or False. We also
11551 -- deal with the special case of non-standard boolean values.
5875f8d6
AC
11552
11553 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
11554 Loc : constant Source_Ptr := Sloc (N);
11555 Typ : constant Entity_Id := Etype (N);
5875f8d6
AC
11556 Left : constant Node_Id := Left_Opnd (N);
11557 Right : constant Node_Id := Right_Opnd (N);
955871d3 11558 LocR : constant Source_Ptr := Sloc (Right);
5875f8d6
AC
11559 Actlist : List_Id;
11560
11561 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
11562 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
11563 -- If Left = Shortcut_Value then Right need not be evaluated
11564
5875f8d6
AC
11565 begin
11566 -- Deal with non-standard booleans
11567
11568 if Is_Boolean_Type (Typ) then
11569 Adjust_Condition (Left);
11570 Adjust_Condition (Right);
11571 Set_Etype (N, Standard_Boolean);
11572 end if;
11573
11574 -- Check for cases where left argument is known to be True or False
11575
11576 if Compile_Time_Known_Value (Left) then
25adc5fb
AC
11577
11578 -- Mark SCO for left condition as compile time known
11579
11580 if Generate_SCO and then Comes_From_Source (Left) then
11581 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
11582 end if;
11583
5875f8d6
AC
11584 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
11585 -- Any actions associated with Right will be executed unconditionally
11586 -- and can thus be inserted into the tree unconditionally.
11587
11588 if Expr_Value_E (Left) /= Shortcut_Ent then
11589 if Present (Actions (N)) then
11590 Insert_Actions (N, Actions (N));
11591 end if;
11592
11593 Rewrite (N, Right);
11594
11595 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
11596 -- In this case we can forget the actions associated with Right,
11597 -- since they will never be executed.
11598
11599 else
11600 Kill_Dead_Code (Right);
11601 Kill_Dead_Code (Actions (N));
11602 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11603 end if;
11604
11605 Adjust_Result_Type (N, Typ);
11606 return;
11607 end if;
11608
955871d3
AC
11609 -- If Actions are present for the right operand, we have to do some
11610 -- special processing. We can't just let these actions filter back into
11611 -- code preceding the short circuit (which is what would have happened
11612 -- if we had not trapped them in the short-circuit form), since they
11613 -- must only be executed if the right operand of the short circuit is
11614 -- executed and not otherwise.
5875f8d6 11615
955871d3
AC
11616 if Present (Actions (N)) then
11617 Actlist := Actions (N);
5875f8d6 11618
0812b84e
AC
11619 -- We now use an Expression_With_Actions node for the right operand
11620 -- of the short-circuit form. Note that this solves the traceability
11621 -- problems for coverage analysis.
5875f8d6 11622
0812b84e 11623 Rewrite (Right,
4b17187f
AC
11624 Make_Expression_With_Actions (LocR,
11625 Expression => Relocate_Node (Right),
11626 Actions => Actlist));
11627
0812b84e
AC
11628 Set_Actions (N, No_List);
11629 Analyze_And_Resolve (Right, Standard_Boolean);
955871d3 11630
5875f8d6
AC
11631 Adjust_Result_Type (N, Typ);
11632 return;
11633 end if;
11634
11635 -- No actions present, check for cases of right argument True/False
11636
11637 if Compile_Time_Known_Value (Right) then
25adc5fb
AC
11638
11639 -- Mark SCO for left condition as compile time known
11640
11641 if Generate_SCO and then Comes_From_Source (Right) then
11642 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
11643 end if;
11644
5875f8d6
AC
11645 -- Change (Left and then True), (Left or else False) to Left.
11646 -- Note that we know there are no actions associated with the right
11647 -- operand, since we just checked for this case above.
11648
11649 if Expr_Value_E (Right) /= Shortcut_Ent then
11650 Rewrite (N, Left);
11651
11652 -- Change (Left and then False), (Left or else True) to Right,
11653 -- making sure to preserve any side effects associated with the Left
11654 -- operand.
11655
11656 else
11657 Remove_Side_Effects (Left);
11658 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11659 end if;
11660 end if;
11661
11662 Adjust_Result_Type (N, Typ);
11663 end Expand_Short_Circuit_Operator;
11664
70482933
RK
11665 -------------------------------------
11666 -- Fixup_Universal_Fixed_Operation --
11667 -------------------------------------
11668
11669 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
11670 Conv : constant Node_Id := Parent (N);
11671
11672 begin
11673 -- We must have a type conversion immediately above us
11674
11675 pragma Assert (Nkind (Conv) = N_Type_Conversion);
11676
11677 -- Normally the type conversion gives our target type. The exception
11678 -- occurs in the case of the Round attribute, where the conversion
11679 -- will be to universal real, and our real type comes from the Round
11680 -- attribute (as well as an indication that we must round the result)
11681
11682 if Nkind (Parent (Conv)) = N_Attribute_Reference
11683 and then Attribute_Name (Parent (Conv)) = Name_Round
11684 then
11685 Set_Etype (N, Etype (Parent (Conv)));
11686 Set_Rounded_Result (N);
11687
11688 -- Normal case where type comes from conversion above us
11689
11690 else
11691 Set_Etype (N, Etype (Conv));
11692 end if;
11693 end Fixup_Universal_Fixed_Operation;
11694
5d09245e
AC
11695 ---------------------------------
11696 -- Has_Inferable_Discriminants --
11697 ---------------------------------
11698
11699 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
11700
11701 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
11702 -- Determines whether the left-most prefix of a selected component is a
11703 -- formal parameter in a subprogram. Assumes N is a selected component.
11704
11705 --------------------------------
11706 -- Prefix_Is_Formal_Parameter --
11707 --------------------------------
11708
11709 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
83bb90af 11710 Sel_Comp : Node_Id;
5d09245e
AC
11711
11712 begin
11713 -- Move to the left-most prefix by climbing up the tree
11714
83bb90af 11715 Sel_Comp := N;
5d09245e
AC
11716 while Present (Parent (Sel_Comp))
11717 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
11718 loop
11719 Sel_Comp := Parent (Sel_Comp);
11720 end loop;
11721
11722 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
11723 end Prefix_Is_Formal_Parameter;
11724
11725 -- Start of processing for Has_Inferable_Discriminants
11726
11727 begin
5d09245e
AC
11728 -- For selected components, the subtype of the selector must be a
11729 -- constrained Unchecked_Union. If the component is subject to a
11730 -- per-object constraint, then the enclosing object must have inferable
11731 -- discriminants.
11732
83bb90af 11733 if Nkind (N) = N_Selected_Component then
5d09245e
AC
11734 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
11735
11736 -- A small hack. If we have a per-object constrained selected
11737 -- component of a formal parameter, return True since we do not
11738 -- know the actual parameter association yet.
11739
11740 if Prefix_Is_Formal_Parameter (N) then
11741 return True;
5d09245e
AC
11742
11743 -- Otherwise, check the enclosing object and the selector
11744
83bb90af
TQ
11745 else
11746 return Has_Inferable_Discriminants (Prefix (N))
11747 and then Has_Inferable_Discriminants (Selector_Name (N));
11748 end if;
5d09245e
AC
11749
11750 -- The call to Has_Inferable_Discriminants will determine whether
11751 -- the selector has a constrained Unchecked_Union nominal type.
11752
83bb90af
TQ
11753 else
11754 return Has_Inferable_Discriminants (Selector_Name (N));
11755 end if;
5d09245e
AC
11756
11757 -- A qualified expression has inferable discriminants if its subtype
11758 -- mark is a constrained Unchecked_Union subtype.
11759
11760 elsif Nkind (N) = N_Qualified_Expression then
053cf994 11761 return Is_Unchecked_Union (Etype (Subtype_Mark (N)))
5b5b27ad 11762 and then Is_Constrained (Etype (Subtype_Mark (N)));
5d09245e 11763
83bb90af
TQ
11764 -- For all other names, it is sufficient to have a constrained
11765 -- Unchecked_Union nominal subtype.
11766
11767 else
11768 return Is_Unchecked_Union (Base_Type (Etype (N)))
11769 and then Is_Constrained (Etype (N));
11770 end if;
5d09245e
AC
11771 end Has_Inferable_Discriminants;
11772
70482933
RK
11773 -------------------------------
11774 -- Insert_Dereference_Action --
11775 -------------------------------
11776
11777 procedure Insert_Dereference_Action (N : Node_Id) is
8777c5a6 11778
70482933 11779 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
2e071734
AC
11780 -- Return true if type of P is derived from Checked_Pool;
11781
11782 -----------------------------
11783 -- Is_Checked_Storage_Pool --
11784 -----------------------------
70482933
RK
11785
11786 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
11787 T : Entity_Id;
761f7dcb 11788
70482933
RK
11789 begin
11790 if No (P) then
11791 return False;
11792 end if;
11793
11794 T := Etype (P);
11795 while T /= Etype (T) loop
11796 if Is_RTE (T, RE_Checked_Pool) then
11797 return True;
11798 else
11799 T := Etype (T);
11800 end if;
11801 end loop;
11802
11803 return False;
11804 end Is_Checked_Storage_Pool;
11805
b0d71355
HK
11806 -- Local variables
11807
11808 Typ : constant Entity_Id := Etype (N);
11809 Desig : constant Entity_Id := Available_View (Designated_Type (Typ));
11810 Loc : constant Source_Ptr := Sloc (N);
11811 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
11812 Pnod : constant Node_Id := Parent (N);
11813
51dcceec
AC
11814 Addr : Entity_Id;
11815 Alig : Entity_Id;
11816 Deref : Node_Id;
11817 Size : Entity_Id;
11818 Size_Bits : Node_Id;
11819 Stmt : Node_Id;
b0d71355 11820
70482933
RK
11821 -- Start of processing for Insert_Dereference_Action
11822
11823 begin
e6f69614
AC
11824 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
11825
b0d71355
HK
11826 -- Do not re-expand a dereference which has already been processed by
11827 -- this routine.
11828
11829 if Has_Dereference_Action (Pnod) then
70482933 11830 return;
70482933 11831
b0d71355
HK
11832 -- Do not perform this type of expansion for internally-generated
11833 -- dereferences.
70482933 11834
b0d71355
HK
11835 elsif not Comes_From_Source (Original_Node (Pnod)) then
11836 return;
70482933 11837
b0d71355
HK
11838 -- A dereference action is only applicable to objects which have been
11839 -- allocated on a checked pool.
70482933 11840
b0d71355
HK
11841 elsif not Is_Checked_Storage_Pool (Pool) then
11842 return;
11843 end if;
70482933 11844
b0d71355 11845 -- Extract the address of the dereferenced object. Generate:
8777c5a6 11846
b0d71355 11847 -- Addr : System.Address := <N>'Pool_Address;
70482933 11848
b0d71355 11849 Addr := Make_Temporary (Loc, 'P');
70482933 11850
b0d71355
HK
11851 Insert_Action (N,
11852 Make_Object_Declaration (Loc,
11853 Defining_Identifier => Addr,
11854 Object_Definition =>
e4494292 11855 New_Occurrence_Of (RTE (RE_Address), Loc),
b0d71355
HK
11856 Expression =>
11857 Make_Attribute_Reference (Loc,
11858 Prefix => Duplicate_Subexpr_Move_Checks (N),
11859 Attribute_Name => Name_Pool_Address)));
11860
11861 -- Calculate the size of the dereferenced object. Generate:
8777c5a6 11862
b0d71355
HK
11863 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
11864
11865 Deref :=
11866 Make_Explicit_Dereference (Loc,
11867 Prefix => Duplicate_Subexpr_Move_Checks (N));
11868 Set_Has_Dereference_Action (Deref);
70482933 11869
51dcceec
AC
11870 Size_Bits :=
11871 Make_Attribute_Reference (Loc,
11872 Prefix => Deref,
11873 Attribute_Name => Name_Size);
11874
11875 -- Special case of an unconstrained array: need to add descriptor size
11876
11877 if Is_Array_Type (Desig)
11878 and then not Is_Constrained (First_Subtype (Desig))
11879 then
11880 Size_Bits :=
11881 Make_Op_Add (Loc,
11882 Left_Opnd =>
11883 Make_Attribute_Reference (Loc,
11884 Prefix =>
11885 New_Occurrence_Of (First_Subtype (Desig), Loc),
11886 Attribute_Name => Name_Descriptor_Size),
11887 Right_Opnd => Size_Bits);
11888 end if;
b0d71355 11889
51dcceec 11890 Size := Make_Temporary (Loc, 'S');
b0d71355
HK
11891 Insert_Action (N,
11892 Make_Object_Declaration (Loc,
11893 Defining_Identifier => Size,
11894 Object_Definition =>
e4494292 11895 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
b0d71355
HK
11896 Expression =>
11897 Make_Op_Divide (Loc,
51dcceec
AC
11898 Left_Opnd => Size_Bits,
11899 Right_Opnd => Make_Integer_Literal (Loc, System_Storage_Unit))));
70482933 11900
b0d71355
HK
11901 -- Calculate the alignment of the dereferenced object. Generate:
11902 -- Alig : constant Storage_Count := <N>.all'Alignment;
70482933 11903
b0d71355
HK
11904 Deref :=
11905 Make_Explicit_Dereference (Loc,
11906 Prefix => Duplicate_Subexpr_Move_Checks (N));
11907 Set_Has_Dereference_Action (Deref);
11908
11909 Alig := Make_Temporary (Loc, 'A');
b0d71355
HK
11910 Insert_Action (N,
11911 Make_Object_Declaration (Loc,
11912 Defining_Identifier => Alig,
11913 Object_Definition =>
e4494292 11914 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
b0d71355
HK
11915 Expression =>
11916 Make_Attribute_Reference (Loc,
11917 Prefix => Deref,
11918 Attribute_Name => Name_Alignment)));
11919
11920 -- A dereference of a controlled object requires special processing. The
11921 -- finalization machinery requests additional space from the underlying
11922 -- pool to allocate and hide two pointers. As a result, a checked pool
11923 -- may mark the wrong memory as valid. Since checked pools do not have
11924 -- knowledge of hidden pointers, we have to bring the two pointers back
11925 -- in view in order to restore the original state of the object.
11926
11927 if Needs_Finalization (Desig) then
11928
11929 -- Adjust the address and size of the dereferenced object. Generate:
11930 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
11931
11932 Stmt :=
11933 Make_Procedure_Call_Statement (Loc,
11934 Name =>
e4494292 11935 New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc),
b0d71355 11936 Parameter_Associations => New_List (
e4494292
RD
11937 New_Occurrence_Of (Addr, Loc),
11938 New_Occurrence_Of (Size, Loc),
11939 New_Occurrence_Of (Alig, Loc)));
b0d71355
HK
11940
11941 -- Class-wide types complicate things because we cannot determine
11942 -- statically whether the actual object is truly controlled. We must
11943 -- generate a runtime check to detect this property. Generate:
11944 --
11945 -- if Needs_Finalization (<N>.all'Tag) then
11946 -- <Stmt>;
11947 -- end if;
11948
11949 if Is_Class_Wide_Type (Desig) then
11950 Deref :=
11951 Make_Explicit_Dereference (Loc,
11952 Prefix => Duplicate_Subexpr_Move_Checks (N));
11953 Set_Has_Dereference_Action (Deref);
11954
11955 Stmt :=
8b1011c0 11956 Make_Implicit_If_Statement (N,
b0d71355
HK
11957 Condition =>
11958 Make_Function_Call (Loc,
11959 Name =>
e4494292 11960 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
b0d71355
HK
11961 Parameter_Associations => New_List (
11962 Make_Attribute_Reference (Loc,
11963 Prefix => Deref,
11964 Attribute_Name => Name_Tag))),
11965 Then_Statements => New_List (Stmt));
11966 end if;
11967
11968 Insert_Action (N, Stmt);
11969 end if;
11970
11971 -- Generate:
11972 -- Dereference (Pool, Addr, Size, Alig);
11973
11974 Insert_Action (N,
11975 Make_Procedure_Call_Statement (Loc,
11976 Name =>
e4494292 11977 New_Occurrence_Of
b0d71355
HK
11978 (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
11979 Parameter_Associations => New_List (
e4494292
RD
11980 New_Occurrence_Of (Pool, Loc),
11981 New_Occurrence_Of (Addr, Loc),
11982 New_Occurrence_Of (Size, Loc),
11983 New_Occurrence_Of (Alig, Loc))));
b0d71355
HK
11984
11985 -- Mark the explicit dereference as processed to avoid potential
11986 -- infinite expansion.
11987
11988 Set_Has_Dereference_Action (Pnod);
70482933 11989
fbf5a39b
AC
11990 exception
11991 when RE_Not_Available =>
11992 return;
70482933
RK
11993 end Insert_Dereference_Action;
11994
fdfcc663
AC
11995 --------------------------------
11996 -- Integer_Promotion_Possible --
11997 --------------------------------
11998
11999 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
12000 Operand : constant Node_Id := Expression (N);
12001 Operand_Type : constant Entity_Id := Etype (Operand);
12002 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
12003
12004 begin
12005 pragma Assert (Nkind (N) = N_Type_Conversion);
12006
12007 return
12008
12009 -- We only do the transformation for source constructs. We assume
12010 -- that the expander knows what it is doing when it generates code.
12011
12012 Comes_From_Source (N)
12013
12014 -- If the operand type is Short_Integer or Short_Short_Integer,
12015 -- then we will promote to Integer, which is available on all
12016 -- targets, and is sufficient to ensure no intermediate overflow.
12017 -- Furthermore it is likely to be as efficient or more efficient
12018 -- than using the smaller type for the computation so we do this
12019 -- unconditionally.
12020
12021 and then
12022 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
761f7dcb 12023 or else
fdfcc663
AC
12024 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
12025
12026 -- Test for interesting operation, which includes addition,
5f3f175d
AC
12027 -- division, exponentiation, multiplication, subtraction, absolute
12028 -- value and unary negation. Unary "+" is omitted since it is a
12029 -- no-op and thus can't overflow.
fdfcc663 12030
5f3f175d
AC
12031 and then Nkind_In (Operand, N_Op_Abs,
12032 N_Op_Add,
fdfcc663
AC
12033 N_Op_Divide,
12034 N_Op_Expon,
12035 N_Op_Minus,
12036 N_Op_Multiply,
12037 N_Op_Subtract);
12038 end Integer_Promotion_Possible;
12039
70482933
RK
12040 ------------------------------
12041 -- Make_Array_Comparison_Op --
12042 ------------------------------
12043
12044 -- This is a hand-coded expansion of the following generic function:
12045
12046 -- generic
12047 -- type elem is (<>);
12048 -- type index is (<>);
12049 -- type a is array (index range <>) of elem;
20b5d666 12050
70482933
RK
12051 -- function Gnnn (X : a; Y: a) return boolean is
12052 -- J : index := Y'first;
20b5d666 12053
70482933
RK
12054 -- begin
12055 -- if X'length = 0 then
12056 -- return false;
20b5d666 12057
70482933
RK
12058 -- elsif Y'length = 0 then
12059 -- return true;
20b5d666 12060
70482933
RK
12061 -- else
12062 -- for I in X'range loop
12063 -- if X (I) = Y (J) then
12064 -- if J = Y'last then
12065 -- exit;
12066 -- else
12067 -- J := index'succ (J);
12068 -- end if;
20b5d666 12069
70482933
RK
12070 -- else
12071 -- return X (I) > Y (J);
12072 -- end if;
12073 -- end loop;
20b5d666 12074
70482933
RK
12075 -- return X'length > Y'length;
12076 -- end if;
12077 -- end Gnnn;
12078
12079 -- Note that since we are essentially doing this expansion by hand, we
12080 -- do not need to generate an actual or formal generic part, just the
12081 -- instantiated function itself.
12082
bb012790
AC
12083 -- Perhaps we could have the actual generic available in the run-time,
12084 -- obtained by rtsfind, and actually expand a real instantiation ???
12085
70482933 12086 function Make_Array_Comparison_Op
2e071734
AC
12087 (Typ : Entity_Id;
12088 Nod : Node_Id) return Node_Id
70482933
RK
12089 is
12090 Loc : constant Source_Ptr := Sloc (Nod);
12091
12092 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
12093 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
12094 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
12095 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
12096
12097 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
12098
12099 Loop_Statement : Node_Id;
12100 Loop_Body : Node_Id;
12101 If_Stat : Node_Id;
12102 Inner_If : Node_Id;
12103 Final_Expr : Node_Id;
12104 Func_Body : Node_Id;
12105 Func_Name : Entity_Id;
12106 Formals : List_Id;
12107 Length1 : Node_Id;
12108 Length2 : Node_Id;
12109
12110 begin
12111 -- if J = Y'last then
12112 -- exit;
12113 -- else
12114 -- J := index'succ (J);
12115 -- end if;
12116
12117 Inner_If :=
12118 Make_Implicit_If_Statement (Nod,
12119 Condition =>
12120 Make_Op_Eq (Loc,
e4494292 12121 Left_Opnd => New_Occurrence_Of (J, Loc),
70482933
RK
12122 Right_Opnd =>
12123 Make_Attribute_Reference (Loc,
e4494292 12124 Prefix => New_Occurrence_Of (Y, Loc),
70482933
RK
12125 Attribute_Name => Name_Last)),
12126
12127 Then_Statements => New_List (
12128 Make_Exit_Statement (Loc)),
12129
12130 Else_Statements =>
12131 New_List (
12132 Make_Assignment_Statement (Loc,
e4494292 12133 Name => New_Occurrence_Of (J, Loc),
70482933
RK
12134 Expression =>
12135 Make_Attribute_Reference (Loc,
e4494292 12136 Prefix => New_Occurrence_Of (Index, Loc),
70482933 12137 Attribute_Name => Name_Succ,
e4494292 12138 Expressions => New_List (New_Occurrence_Of (J, Loc))))));
70482933
RK
12139
12140 -- if X (I) = Y (J) then
12141 -- if ... end if;
12142 -- else
12143 -- return X (I) > Y (J);
12144 -- end if;
12145
12146 Loop_Body :=
12147 Make_Implicit_If_Statement (Nod,
12148 Condition =>
12149 Make_Op_Eq (Loc,
12150 Left_Opnd =>
12151 Make_Indexed_Component (Loc,
e4494292
RD
12152 Prefix => New_Occurrence_Of (X, Loc),
12153 Expressions => New_List (New_Occurrence_Of (I, Loc))),
70482933
RK
12154
12155 Right_Opnd =>
12156 Make_Indexed_Component (Loc,
e4494292
RD
12157 Prefix => New_Occurrence_Of (Y, Loc),
12158 Expressions => New_List (New_Occurrence_Of (J, Loc)))),
70482933
RK
12159
12160 Then_Statements => New_List (Inner_If),
12161
12162 Else_Statements => New_List (
d766cee3 12163 Make_Simple_Return_Statement (Loc,
70482933
RK
12164 Expression =>
12165 Make_Op_Gt (Loc,
12166 Left_Opnd =>
12167 Make_Indexed_Component (Loc,
e4494292
RD
12168 Prefix => New_Occurrence_Of (X, Loc),
12169 Expressions => New_List (New_Occurrence_Of (I, Loc))),
70482933
RK
12170
12171 Right_Opnd =>
12172 Make_Indexed_Component (Loc,
e4494292 12173 Prefix => New_Occurrence_Of (Y, Loc),
70482933 12174 Expressions => New_List (
e4494292 12175 New_Occurrence_Of (J, Loc)))))));
70482933
RK
12176
12177 -- for I in X'range loop
12178 -- if ... end if;
12179 -- end loop;
12180
12181 Loop_Statement :=
12182 Make_Implicit_Loop_Statement (Nod,
12183 Identifier => Empty,
12184
12185 Iteration_Scheme =>
12186 Make_Iteration_Scheme (Loc,
12187 Loop_Parameter_Specification =>
12188 Make_Loop_Parameter_Specification (Loc,
12189 Defining_Identifier => I,
12190 Discrete_Subtype_Definition =>
12191 Make_Attribute_Reference (Loc,
e4494292 12192 Prefix => New_Occurrence_Of (X, Loc),
70482933
RK
12193 Attribute_Name => Name_Range))),
12194
12195 Statements => New_List (Loop_Body));
12196
12197 -- if X'length = 0 then
12198 -- return false;
12199 -- elsif Y'length = 0 then
12200 -- return true;
12201 -- else
12202 -- for ... loop ... end loop;
12203 -- return X'length > Y'length;
12204 -- end if;
12205
12206 Length1 :=
12207 Make_Attribute_Reference (Loc,
e4494292 12208 Prefix => New_Occurrence_Of (X, Loc),
70482933
RK
12209 Attribute_Name => Name_Length);
12210
12211 Length2 :=
12212 Make_Attribute_Reference (Loc,
e4494292 12213 Prefix => New_Occurrence_Of (Y, Loc),
70482933
RK
12214 Attribute_Name => Name_Length);
12215
12216 Final_Expr :=
12217 Make_Op_Gt (Loc,
12218 Left_Opnd => Length1,
12219 Right_Opnd => Length2);
12220
12221 If_Stat :=
12222 Make_Implicit_If_Statement (Nod,
12223 Condition =>
12224 Make_Op_Eq (Loc,
12225 Left_Opnd =>
12226 Make_Attribute_Reference (Loc,
e4494292 12227 Prefix => New_Occurrence_Of (X, Loc),
70482933
RK
12228 Attribute_Name => Name_Length),
12229 Right_Opnd =>
12230 Make_Integer_Literal (Loc, 0)),
12231
12232 Then_Statements =>
12233 New_List (
d766cee3 12234 Make_Simple_Return_Statement (Loc,
e4494292 12235 Expression => New_Occurrence_Of (Standard_False, Loc))),
70482933
RK
12236
12237 Elsif_Parts => New_List (
12238 Make_Elsif_Part (Loc,
12239 Condition =>
12240 Make_Op_Eq (Loc,
12241 Left_Opnd =>
12242 Make_Attribute_Reference (Loc,
e4494292 12243 Prefix => New_Occurrence_Of (Y, Loc),
70482933
RK
12244 Attribute_Name => Name_Length),
12245 Right_Opnd =>
12246 Make_Integer_Literal (Loc, 0)),
12247
12248 Then_Statements =>
12249 New_List (
d766cee3 12250 Make_Simple_Return_Statement (Loc,
e4494292 12251 Expression => New_Occurrence_Of (Standard_True, Loc))))),
70482933
RK
12252
12253 Else_Statements => New_List (
12254 Loop_Statement,
d766cee3 12255 Make_Simple_Return_Statement (Loc,
70482933
RK
12256 Expression => Final_Expr)));
12257
12258 -- (X : a; Y: a)
12259
12260 Formals := New_List (
12261 Make_Parameter_Specification (Loc,
12262 Defining_Identifier => X,
e4494292 12263 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
70482933
RK
12264
12265 Make_Parameter_Specification (Loc,
12266 Defining_Identifier => Y,
e4494292 12267 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
70482933
RK
12268
12269 -- function Gnnn (...) return boolean is
12270 -- J : index := Y'first;
12271 -- begin
12272 -- if ... end if;
12273 -- end Gnnn;
12274
191fcb3a 12275 Func_Name := Make_Temporary (Loc, 'G');
70482933
RK
12276
12277 Func_Body :=
12278 Make_Subprogram_Body (Loc,
12279 Specification =>
12280 Make_Function_Specification (Loc,
12281 Defining_Unit_Name => Func_Name,
12282 Parameter_Specifications => Formals,
e4494292 12283 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
70482933
RK
12284
12285 Declarations => New_List (
12286 Make_Object_Declaration (Loc,
12287 Defining_Identifier => J,
e4494292 12288 Object_Definition => New_Occurrence_Of (Index, Loc),
70482933
RK
12289 Expression =>
12290 Make_Attribute_Reference (Loc,
e4494292 12291 Prefix => New_Occurrence_Of (Y, Loc),
70482933
RK
12292 Attribute_Name => Name_First))),
12293
12294 Handled_Statement_Sequence =>
12295 Make_Handled_Sequence_Of_Statements (Loc,
12296 Statements => New_List (If_Stat)));
12297
12298 return Func_Body;
70482933
RK
12299 end Make_Array_Comparison_Op;
12300
12301 ---------------------------
12302 -- Make_Boolean_Array_Op --
12303 ---------------------------
12304
685094bf
RD
12305 -- For logical operations on boolean arrays, expand in line the following,
12306 -- replacing 'and' with 'or' or 'xor' where needed:
70482933
RK
12307
12308 -- function Annn (A : typ; B: typ) return typ is
12309 -- C : typ;
12310 -- begin
12311 -- for J in A'range loop
12312 -- C (J) := A (J) op B (J);
12313 -- end loop;
12314 -- return C;
12315 -- end Annn;
12316
12317 -- Here typ is the boolean array type
12318
12319 function Make_Boolean_Array_Op
2e071734
AC
12320 (Typ : Entity_Id;
12321 N : Node_Id) return Node_Id
70482933
RK
12322 is
12323 Loc : constant Source_Ptr := Sloc (N);
12324
12325 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
12326 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
12327 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
12328 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
12329
12330 A_J : Node_Id;
12331 B_J : Node_Id;
12332 C_J : Node_Id;
12333 Op : Node_Id;
12334
12335 Formals : List_Id;
12336 Func_Name : Entity_Id;
12337 Func_Body : Node_Id;
12338 Loop_Statement : Node_Id;
12339
12340 begin
12341 A_J :=
12342 Make_Indexed_Component (Loc,
e4494292
RD
12343 Prefix => New_Occurrence_Of (A, Loc),
12344 Expressions => New_List (New_Occurrence_Of (J, Loc)));
70482933
RK
12345
12346 B_J :=
12347 Make_Indexed_Component (Loc,
e4494292
RD
12348 Prefix => New_Occurrence_Of (B, Loc),
12349 Expressions => New_List (New_Occurrence_Of (J, Loc)));
70482933
RK
12350
12351 C_J :=
12352 Make_Indexed_Component (Loc,
e4494292
RD
12353 Prefix => New_Occurrence_Of (C, Loc),
12354 Expressions => New_List (New_Occurrence_Of (J, Loc)));
70482933
RK
12355
12356 if Nkind (N) = N_Op_And then
12357 Op :=
12358 Make_Op_And (Loc,
12359 Left_Opnd => A_J,
12360 Right_Opnd => B_J);
12361
12362 elsif Nkind (N) = N_Op_Or then
12363 Op :=
12364 Make_Op_Or (Loc,
12365 Left_Opnd => A_J,
12366 Right_Opnd => B_J);
12367
12368 else
12369 Op :=
12370 Make_Op_Xor (Loc,
12371 Left_Opnd => A_J,
12372 Right_Opnd => B_J);
12373 end if;
12374
12375 Loop_Statement :=
12376 Make_Implicit_Loop_Statement (N,
12377 Identifier => Empty,
12378
12379 Iteration_Scheme =>
12380 Make_Iteration_Scheme (Loc,
12381 Loop_Parameter_Specification =>
12382 Make_Loop_Parameter_Specification (Loc,
12383 Defining_Identifier => J,
12384 Discrete_Subtype_Definition =>
12385 Make_Attribute_Reference (Loc,
e4494292 12386 Prefix => New_Occurrence_Of (A, Loc),
70482933
RK
12387 Attribute_Name => Name_Range))),
12388
12389 Statements => New_List (
12390 Make_Assignment_Statement (Loc,
12391 Name => C_J,
12392 Expression => Op)));
12393
12394 Formals := New_List (
12395 Make_Parameter_Specification (Loc,
12396 Defining_Identifier => A,
e4494292 12397 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
70482933
RK
12398
12399 Make_Parameter_Specification (Loc,
12400 Defining_Identifier => B,
e4494292 12401 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
70482933 12402
191fcb3a 12403 Func_Name := Make_Temporary (Loc, 'A');
70482933
RK
12404 Set_Is_Inlined (Func_Name);
12405
12406 Func_Body :=
12407 Make_Subprogram_Body (Loc,
12408 Specification =>
12409 Make_Function_Specification (Loc,
12410 Defining_Unit_Name => Func_Name,
12411 Parameter_Specifications => Formals,
e4494292 12412 Result_Definition => New_Occurrence_Of (Typ, Loc)),
70482933
RK
12413
12414 Declarations => New_List (
12415 Make_Object_Declaration (Loc,
12416 Defining_Identifier => C,
e4494292 12417 Object_Definition => New_Occurrence_Of (Typ, Loc))),
70482933
RK
12418
12419 Handled_Statement_Sequence =>
12420 Make_Handled_Sequence_Of_Statements (Loc,
12421 Statements => New_List (
12422 Loop_Statement,
d766cee3 12423 Make_Simple_Return_Statement (Loc,
e4494292 12424 Expression => New_Occurrence_Of (C, Loc)))));
70482933
RK
12425
12426 return Func_Body;
12427 end Make_Boolean_Array_Op;
12428
b6b5cca8
AC
12429 -----------------------------------------
12430 -- Minimized_Eliminated_Overflow_Check --
12431 -----------------------------------------
12432
12433 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is
12434 begin
12435 return
12436 Is_Signed_Integer_Type (Etype (N))
a7f1b24f 12437 and then Overflow_Check_Mode in Minimized_Or_Eliminated;
b6b5cca8
AC
12438 end Minimized_Eliminated_Overflow_Check;
12439
0580d807
AC
12440 --------------------------------
12441 -- Optimize_Length_Comparison --
12442 --------------------------------
12443
12444 procedure Optimize_Length_Comparison (N : Node_Id) is
12445 Loc : constant Source_Ptr := Sloc (N);
12446 Typ : constant Entity_Id := Etype (N);
12447 Result : Node_Id;
12448
12449 Left : Node_Id;
12450 Right : Node_Id;
12451 -- First and Last attribute reference nodes, which end up as left and
12452 -- right operands of the optimized result.
12453
12454 Is_Zero : Boolean;
12455 -- True for comparison operand of zero
12456
12457 Comp : Node_Id;
12458 -- Comparison operand, set only if Is_Zero is false
12459
12460 Ent : Entity_Id;
12461 -- Entity whose length is being compared
12462
12463 Index : Node_Id;
12464 -- Integer_Literal node for length attribute expression, or Empty
12465 -- if there is no such expression present.
12466
12467 Ityp : Entity_Id;
12468 -- Type of array index to which 'Length is applied
12469
12470 Op : Node_Kind := Nkind (N);
12471 -- Kind of comparison operator, gets flipped if operands backwards
12472
12473 function Is_Optimizable (N : Node_Id) return Boolean;
abcd9db2
AC
12474 -- Tests N to see if it is an optimizable comparison value (defined as
12475 -- constant zero or one, or something else where the value is known to
12476 -- be positive and in the range of 32-bits, and where the corresponding
12477 -- Length value is also known to be 32-bits. If result is true, sets
12478 -- Is_Zero, Ityp, and Comp accordingly.
0580d807
AC
12479
12480 function Is_Entity_Length (N : Node_Id) return Boolean;
12481 -- Tests if N is a length attribute applied to a simple entity. If so,
12482 -- returns True, and sets Ent to the entity, and Index to the integer
12483 -- literal provided as an attribute expression, or to Empty if none.
12484 -- Also returns True if the expression is a generated type conversion
12485 -- whose expression is of the desired form. This latter case arises
12486 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
12487 -- to check for being in range, which is not needed in this context.
12488 -- Returns False if neither condition holds.
12489
12490 function Prepare_64 (N : Node_Id) return Node_Id;
12491 -- Given a discrete expression, returns a Long_Long_Integer typed
12492 -- expression representing the underlying value of the expression.
12493 -- This is done with an unchecked conversion to the result type. We
12494 -- use unchecked conversion to handle the enumeration type case.
12495
12496 ----------------------
12497 -- Is_Entity_Length --
12498 ----------------------
12499
12500 function Is_Entity_Length (N : Node_Id) return Boolean is
12501 begin
12502 if Nkind (N) = N_Attribute_Reference
12503 and then Attribute_Name (N) = Name_Length
12504 and then Is_Entity_Name (Prefix (N))
12505 then
12506 Ent := Entity (Prefix (N));
12507
12508 if Present (Expressions (N)) then
12509 Index := First (Expressions (N));
12510 else
12511 Index := Empty;
12512 end if;
12513
12514 return True;
12515
12516 elsif Nkind (N) = N_Type_Conversion
12517 and then not Comes_From_Source (N)
12518 then
12519 return Is_Entity_Length (Expression (N));
12520
12521 else
12522 return False;
12523 end if;
12524 end Is_Entity_Length;
12525
12526 --------------------
12527 -- Is_Optimizable --
12528 --------------------
12529
12530 function Is_Optimizable (N : Node_Id) return Boolean is
12531 Val : Uint;
12532 OK : Boolean;
12533 Lo : Uint;
12534 Hi : Uint;
12535 Indx : Node_Id;
12536
12537 begin
12538 if Compile_Time_Known_Value (N) then
12539 Val := Expr_Value (N);
12540
12541 if Val = Uint_0 then
12542 Is_Zero := True;
12543 Comp := Empty;
12544 return True;
12545
12546 elsif Val = Uint_1 then
12547 Is_Zero := False;
12548 Comp := Empty;
12549 return True;
12550 end if;
12551 end if;
12552
12553 -- Here we have to make sure of being within 32-bits
12554
12555 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
12556
12557 if not OK
abcd9db2 12558 or else Lo < Uint_1
0580d807
AC
12559 or else Hi > UI_From_Int (Int'Last)
12560 then
12561 return False;
12562 end if;
12563
abcd9db2
AC
12564 -- Comparison value was within range, so now we must check the index
12565 -- value to make sure it is also within 32-bits.
0580d807
AC
12566
12567 Indx := First_Index (Etype (Ent));
12568
12569 if Present (Index) then
12570 for J in 2 .. UI_To_Int (Intval (Index)) loop
12571 Next_Index (Indx);
12572 end loop;
12573 end if;
12574
12575 Ityp := Etype (Indx);
12576
12577 if Esize (Ityp) > 32 then
12578 return False;
12579 end if;
12580
12581 Is_Zero := False;
12582 Comp := N;
12583 return True;
12584 end Is_Optimizable;
12585
12586 ----------------
12587 -- Prepare_64 --
12588 ----------------
12589
12590 function Prepare_64 (N : Node_Id) return Node_Id is
12591 begin
12592 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
12593 end Prepare_64;
12594
12595 -- Start of processing for Optimize_Length_Comparison
12596
12597 begin
12598 -- Nothing to do if not a comparison
12599
12600 if Op not in N_Op_Compare then
12601 return;
12602 end if;
12603
12604 -- Nothing to do if special -gnatd.P debug flag set
12605
12606 if Debug_Flag_Dot_PP then
12607 return;
12608 end if;
12609
12610 -- Ent'Length op 0/1
12611
12612 if Is_Entity_Length (Left_Opnd (N))
12613 and then Is_Optimizable (Right_Opnd (N))
12614 then
12615 null;
12616
12617 -- 0/1 op Ent'Length
12618
12619 elsif Is_Entity_Length (Right_Opnd (N))
12620 and then Is_Optimizable (Left_Opnd (N))
12621 then
12622 -- Flip comparison to opposite sense
12623
12624 case Op is
12625 when N_Op_Lt => Op := N_Op_Gt;
12626 when N_Op_Le => Op := N_Op_Ge;
12627 when N_Op_Gt => Op := N_Op_Lt;
12628 when N_Op_Ge => Op := N_Op_Le;
12629 when others => null;
12630 end case;
12631
12632 -- Else optimization not possible
12633
12634 else
12635 return;
12636 end if;
12637
12638 -- Fall through if we will do the optimization
12639
12640 -- Cases to handle:
12641
12642 -- X'Length = 0 => X'First > X'Last
12643 -- X'Length = 1 => X'First = X'Last
12644 -- X'Length = n => X'First + (n - 1) = X'Last
12645
12646 -- X'Length /= 0 => X'First <= X'Last
12647 -- X'Length /= 1 => X'First /= X'Last
12648 -- X'Length /= n => X'First + (n - 1) /= X'Last
12649
12650 -- X'Length >= 0 => always true, warn
12651 -- X'Length >= 1 => X'First <= X'Last
12652 -- X'Length >= n => X'First + (n - 1) <= X'Last
12653
12654 -- X'Length > 0 => X'First <= X'Last
12655 -- X'Length > 1 => X'First < X'Last
12656 -- X'Length > n => X'First + (n - 1) < X'Last
12657
12658 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
12659 -- X'Length <= 1 => X'First >= X'Last
12660 -- X'Length <= n => X'First + (n - 1) >= X'Last
12661
12662 -- X'Length < 0 => always false (warn)
12663 -- X'Length < 1 => X'First > X'Last
12664 -- X'Length < n => X'First + (n - 1) > X'Last
12665
12666 -- Note: for the cases of n (not constant 0,1), we require that the
12667 -- corresponding index type be integer or shorter (i.e. not 64-bit),
12668 -- and the same for the comparison value. Then we do the comparison
12669 -- using 64-bit arithmetic (actually long long integer), so that we
12670 -- cannot have overflow intefering with the result.
12671
12672 -- First deal with warning cases
12673
12674 if Is_Zero then
12675 case Op is
12676
12677 -- X'Length >= 0
12678
12679 when N_Op_Ge =>
12680 Rewrite (N,
12681 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
12682 Analyze_And_Resolve (N, Typ);
12683 Warn_On_Known_Condition (N);
12684 return;
12685
12686 -- X'Length < 0
12687
12688 when N_Op_Lt =>
12689 Rewrite (N,
12690 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
12691 Analyze_And_Resolve (N, Typ);
12692 Warn_On_Known_Condition (N);
12693 return;
12694
12695 when N_Op_Le =>
12696 if Constant_Condition_Warnings
12697 and then Comes_From_Source (Original_Node (N))
12698 then
324ac540 12699 Error_Msg_N ("could replace by ""'=""?c?", N);
0580d807
AC
12700 end if;
12701
12702 Op := N_Op_Eq;
12703
12704 when others =>
12705 null;
12706 end case;
12707 end if;
12708
12709 -- Build the First reference we will use
12710
12711 Left :=
12712 Make_Attribute_Reference (Loc,
12713 Prefix => New_Occurrence_Of (Ent, Loc),
12714 Attribute_Name => Name_First);
12715
12716 if Present (Index) then
12717 Set_Expressions (Left, New_List (New_Copy (Index)));
12718 end if;
12719
12720 -- If general value case, then do the addition of (n - 1), and
12721 -- also add the needed conversions to type Long_Long_Integer.
12722
12723 if Present (Comp) then
12724 Left :=
12725 Make_Op_Add (Loc,
12726 Left_Opnd => Prepare_64 (Left),
12727 Right_Opnd =>
12728 Make_Op_Subtract (Loc,
12729 Left_Opnd => Prepare_64 (Comp),
12730 Right_Opnd => Make_Integer_Literal (Loc, 1)));
12731 end if;
12732
12733 -- Build the Last reference we will use
12734
12735 Right :=
12736 Make_Attribute_Reference (Loc,
12737 Prefix => New_Occurrence_Of (Ent, Loc),
12738 Attribute_Name => Name_Last);
12739
12740 if Present (Index) then
12741 Set_Expressions (Right, New_List (New_Copy (Index)));
12742 end if;
12743
12744 -- If general operand, convert Last reference to Long_Long_Integer
12745
12746 if Present (Comp) then
12747 Right := Prepare_64 (Right);
12748 end if;
12749
12750 -- Check for cases to optimize
12751
12752 -- X'Length = 0 => X'First > X'Last
12753 -- X'Length < 1 => X'First > X'Last
12754 -- X'Length < n => X'First + (n - 1) > X'Last
12755
12756 if (Is_Zero and then Op = N_Op_Eq)
12757 or else (not Is_Zero and then Op = N_Op_Lt)
12758 then
12759 Result :=
12760 Make_Op_Gt (Loc,
12761 Left_Opnd => Left,
12762 Right_Opnd => Right);
12763
12764 -- X'Length = 1 => X'First = X'Last
12765 -- X'Length = n => X'First + (n - 1) = X'Last
12766
12767 elsif not Is_Zero and then Op = N_Op_Eq then
12768 Result :=
12769 Make_Op_Eq (Loc,
12770 Left_Opnd => Left,
12771 Right_Opnd => Right);
12772
12773 -- X'Length /= 0 => X'First <= X'Last
12774 -- X'Length > 0 => X'First <= X'Last
12775
12776 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
12777 Result :=
12778 Make_Op_Le (Loc,
12779 Left_Opnd => Left,
12780 Right_Opnd => Right);
12781
12782 -- X'Length /= 1 => X'First /= X'Last
12783 -- X'Length /= n => X'First + (n - 1) /= X'Last
12784
12785 elsif not Is_Zero and then Op = N_Op_Ne then
12786 Result :=
12787 Make_Op_Ne (Loc,
12788 Left_Opnd => Left,
12789 Right_Opnd => Right);
12790
12791 -- X'Length >= 1 => X'First <= X'Last
12792 -- X'Length >= n => X'First + (n - 1) <= X'Last
12793
12794 elsif not Is_Zero and then Op = N_Op_Ge then
12795 Result :=
12796 Make_Op_Le (Loc,
12797 Left_Opnd => Left,
12798 Right_Opnd => Right);
12799
12800 -- X'Length > 1 => X'First < X'Last
12801 -- X'Length > n => X'First + (n = 1) < X'Last
12802
12803 elsif not Is_Zero and then Op = N_Op_Gt then
12804 Result :=
12805 Make_Op_Lt (Loc,
12806 Left_Opnd => Left,
12807 Right_Opnd => Right);
12808
12809 -- X'Length <= 1 => X'First >= X'Last
12810 -- X'Length <= n => X'First + (n - 1) >= X'Last
12811
12812 elsif not Is_Zero and then Op = N_Op_Le then
12813 Result :=
12814 Make_Op_Ge (Loc,
12815 Left_Opnd => Left,
12816 Right_Opnd => Right);
12817
12818 -- Should not happen at this stage
12819
12820 else
12821 raise Program_Error;
12822 end if;
12823
12824 -- Rewrite and finish up
12825
12826 Rewrite (N, Result);
12827 Analyze_And_Resolve (N, Typ);
12828 return;
12829 end Optimize_Length_Comparison;
12830
b2c28399
AC
12831 ------------------------------
12832 -- Process_Transient_Object --
12833 ------------------------------
12834
12835 procedure Process_Transient_Object
12836 (Decl : Node_Id;
12837 Rel_Node : Node_Id)
12838 is
4b17187f
AC
12839 Loc : constant Source_Ptr := Sloc (Decl);
12840 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
12841 Obj_Typ : constant Node_Id := Etype (Obj_Id);
12842 Desig_Typ : Entity_Id;
12843 Expr : Node_Id;
12844 Hook_Id : Entity_Id;
12845 Hook_Insert : Node_Id;
12846 Ptr_Id : Entity_Id;
8942b30c 12847
9ab5d86b 12848 Hook_Context : constant Node_Id := Find_Hook_Context (Rel_Node);
4b17187f
AC
12849 -- The node on which to insert the hook as an action. This is usually
12850 -- the innermost enclosing non-transient construct.
064f4527 12851
4b17187f
AC
12852 Fin_Context : Node_Id;
12853 -- The node after which to insert the finalization actions of the
12854 -- transient controlled object.
b2c28399 12855
8942b30c 12856 begin
8942b30c 12857 if Is_Boolean_Type (Etype (Rel_Node)) then
4b17187f 12858 Fin_Context := Last (Actions (Rel_Node));
8942b30c 12859 else
4b17187f 12860 Fin_Context := Hook_Context;
8942b30c 12861 end if;
064f4527 12862
b2c28399
AC
12863 -- Step 1: Create the access type which provides a reference to the
12864 -- transient controlled object.
12865
12866 if Is_Access_Type (Obj_Typ) then
12867 Desig_Typ := Directly_Designated_Type (Obj_Typ);
12868 else
12869 Desig_Typ := Obj_Typ;
12870 end if;
12871
12872 Desig_Typ := Base_Type (Desig_Typ);
12873
12874 -- Generate:
12875 -- Ann : access [all] <Desig_Typ>;
12876
12877 Ptr_Id := Make_Temporary (Loc, 'A');
12878
064f4527 12879 Insert_Action (Hook_Context,
b2c28399
AC
12880 Make_Full_Type_Declaration (Loc,
12881 Defining_Identifier => Ptr_Id,
12882 Type_Definition =>
12883 Make_Access_To_Object_Definition (Loc,
12884 All_Present => Ekind (Obj_Typ) = E_General_Access_Type,
e4494292 12885 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc))));
b2c28399
AC
12886
12887 -- Step 2: Create a temporary which acts as a hook to the transient
12888 -- controlled object. Generate:
12889
4b17187f 12890 -- Hook : Ptr_Id := null;
b2c28399 12891
4b17187f 12892 Hook_Id := Make_Temporary (Loc, 'T');
b2c28399 12893
064f4527 12894 Insert_Action (Hook_Context,
b2c28399 12895 Make_Object_Declaration (Loc,
4b17187f 12896 Defining_Identifier => Hook_Id,
e4494292 12897 Object_Definition => New_Occurrence_Of (Ptr_Id, Loc)));
b2c28399 12898
4b17187f
AC
12899 -- Mark the hook as created for the purposes of exporting the transient
12900 -- controlled object out of the expression_with_action or if expression.
12901 -- This signals the machinery in Build_Finalizer to treat this case in
12902 -- a special manner.
b2c28399 12903
4b17187f 12904 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Decl);
b2c28399 12905
4b17187f 12906 -- Step 3: Associate the transient object to the hook
b2c28399 12907
a7d08a38
AC
12908 -- This must be inserted right after the object declaration, so that
12909 -- the assignment is executed if, and only if, the object is actually
12910 -- created (whereas the declaration of the hook pointer, and the
12911 -- finalization call, may be inserted at an outer level, and may
12912 -- remain unused for some executions, if the actual creation of
12913 -- the object is conditional).
12914
b2c28399
AC
12915 -- The use of unchecked conversion / unrestricted access is needed to
12916 -- avoid an accessibility violation. Note that the finalization code is
12917 -- structured in such a way that the "hook" is processed only when it
12918 -- points to an existing object.
12919
12920 if Is_Access_Type (Obj_Typ) then
e4494292 12921 Expr :=
4b17187f
AC
12922 Unchecked_Convert_To
12923 (Typ => Ptr_Id,
12924 Expr => New_Occurrence_Of (Obj_Id, Loc));
b2c28399
AC
12925 else
12926 Expr :=
12927 Make_Attribute_Reference (Loc,
e4494292 12928 Prefix => New_Occurrence_Of (Obj_Id, Loc),
b2c28399
AC
12929 Attribute_Name => Name_Unrestricted_Access);
12930 end if;
12931
12932 -- Generate:
4b17187f 12933 -- Hook := Ptr_Id (Obj_Id);
b2c28399 12934 -- <or>
4b17187f 12935 -- Hook := Obj_Id'Unrestricted_Access;
b2c28399 12936
97779c34
AC
12937 -- When the transient object is initialized by an aggregate, the hook
12938 -- must capture the object after the last component assignment takes
12939 -- place. Only then is the object fully initialized.
12940
12941 if Ekind (Obj_Id) = E_Variable
12942 and then Present (Last_Aggregate_Assignment (Obj_Id))
12943 then
4b17187f 12944 Hook_Insert := Last_Aggregate_Assignment (Obj_Id);
97779c34
AC
12945
12946 -- Otherwise the hook seizes the related object immediately
12947
12948 else
4b17187f 12949 Hook_Insert := Decl;
97779c34
AC
12950 end if;
12951
4b17187f 12952 Insert_After_And_Analyze (Hook_Insert,
a7d08a38 12953 Make_Assignment_Statement (Loc,
4b17187f 12954 Name => New_Occurrence_Of (Hook_Id, Loc),
a7d08a38 12955 Expression => Expr));
b2c28399 12956
4b17187f
AC
12957 -- Step 4: Finalize the hook after the context has been evaluated or
12958 -- elaborated. Generate:
b2c28399 12959
4b17187f
AC
12960 -- if Hook /= null then
12961 -- [Deep_]Finalize (Hook.all);
12962 -- Hook := null;
b2c28399
AC
12963 -- end if;
12964
12965 -- When the node is part of a return statement, there is no need to
12966 -- insert a finalization call, as the general finalization mechanism
12967 -- (see Build_Finalizer) would take care of the transient controlled
12968 -- object on subprogram exit. Note that it would also be impossible to
12969 -- insert the finalization code after the return statement as this will
12970 -- render it unreachable.
12971
4b17187f
AC
12972 if Nkind (Fin_Context) = N_Simple_Return_Statement then
12973 null;
b2c28399 12974
4b17187f 12975 -- Otherwise finalize the hook
b2c28399 12976
4b17187f
AC
12977 else
12978 Insert_Action_After (Fin_Context,
12979 Make_Implicit_If_Statement (Decl,
12980 Condition =>
12981 Make_Op_Ne (Loc,
12982 Left_Opnd => New_Occurrence_Of (Hook_Id, Loc),
12983 Right_Opnd => Make_Null (Loc)),
12984
12985 Then_Statements => New_List (
12986 Make_Final_Call
12987 (Obj_Ref =>
12988 Make_Explicit_Dereference (Loc,
12989 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
12990 Typ => Desig_Typ),
b2c28399 12991
4b17187f
AC
12992 Make_Assignment_Statement (Loc,
12993 Name => New_Occurrence_Of (Hook_Id, Loc),
12994 Expression => Make_Null (Loc)))));
b2c28399
AC
12995 end if;
12996 end Process_Transient_Object;
12997
70482933
RK
12998 ------------------------
12999 -- Rewrite_Comparison --
13000 ------------------------
13001
13002 procedure Rewrite_Comparison (N : Node_Id) is
c800f862
RD
13003 Warning_Generated : Boolean := False;
13004 -- Set to True if first pass with Assume_Valid generates a warning in
13005 -- which case we skip the second pass to avoid warning overloaded.
13006
13007 Result : Node_Id;
13008 -- Set to Standard_True or Standard_False
13009
d26dc4b5
AC
13010 begin
13011 if Nkind (N) = N_Type_Conversion then
13012 Rewrite_Comparison (Expression (N));
20b5d666 13013 return;
70482933 13014
d26dc4b5 13015 elsif Nkind (N) not in N_Op_Compare then
20b5d666
JM
13016 return;
13017 end if;
70482933 13018
c800f862
RD
13019 -- Now start looking at the comparison in detail. We potentially go
13020 -- through this loop twice. The first time, Assume_Valid is set False
13021 -- in the call to Compile_Time_Compare. If this call results in a
13022 -- clear result of always True or Always False, that's decisive and
13023 -- we are done. Otherwise we repeat the processing with Assume_Valid
e7e4d230 13024 -- set to True to generate additional warnings. We can skip that step
c800f862
RD
13025 -- if Constant_Condition_Warnings is False.
13026
13027 for AV in False .. True loop
13028 declare
13029 Typ : constant Entity_Id := Etype (N);
13030 Op1 : constant Node_Id := Left_Opnd (N);
13031 Op2 : constant Node_Id := Right_Opnd (N);
70482933 13032
c800f862
RD
13033 Res : constant Compare_Result :=
13034 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
13035 -- Res indicates if compare outcome can be compile time determined
f02b8bb8 13036
c800f862
RD
13037 True_Result : Boolean;
13038 False_Result : Boolean;
f02b8bb8 13039
c800f862
RD
13040 begin
13041 case N_Op_Compare (Nkind (N)) is
d26dc4b5
AC
13042 when N_Op_Eq =>
13043 True_Result := Res = EQ;
13044 False_Result := Res = LT or else Res = GT or else Res = NE;
13045
13046 when N_Op_Ge =>
13047 True_Result := Res in Compare_GE;
13048 False_Result := Res = LT;
13049
13050 if Res = LE
13051 and then Constant_Condition_Warnings
13052 and then Comes_From_Source (Original_Node (N))
13053 and then Nkind (Original_Node (N)) = N_Op_Ge
13054 and then not In_Instance
d26dc4b5 13055 and then Is_Integer_Type (Etype (Left_Opnd (N)))
59ae6391 13056 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
d26dc4b5 13057 then
ed2233dc 13058 Error_Msg_N
324ac540
AC
13059 ("can never be greater than, could replace by ""'=""?c?",
13060 N);
c800f862 13061 Warning_Generated := True;
d26dc4b5 13062 end if;
70482933 13063
d26dc4b5
AC
13064 when N_Op_Gt =>
13065 True_Result := Res = GT;
13066 False_Result := Res in Compare_LE;
13067
13068 when N_Op_Lt =>
13069 True_Result := Res = LT;
13070 False_Result := Res in Compare_GE;
13071
13072 when N_Op_Le =>
13073 True_Result := Res in Compare_LE;
13074 False_Result := Res = GT;
13075
13076 if Res = GE
13077 and then Constant_Condition_Warnings
13078 and then Comes_From_Source (Original_Node (N))
13079 and then Nkind (Original_Node (N)) = N_Op_Le
13080 and then not In_Instance
d26dc4b5 13081 and then Is_Integer_Type (Etype (Left_Opnd (N)))
59ae6391 13082 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
d26dc4b5 13083 then
ed2233dc 13084 Error_Msg_N
324ac540 13085 ("can never be less than, could replace by ""'=""?c?", N);
c800f862 13086 Warning_Generated := True;
d26dc4b5 13087 end if;
70482933 13088
d26dc4b5
AC
13089 when N_Op_Ne =>
13090 True_Result := Res = NE or else Res = GT or else Res = LT;
13091 False_Result := Res = EQ;
c800f862 13092 end case;
d26dc4b5 13093
c800f862
RD
13094 -- If this is the first iteration, then we actually convert the
13095 -- comparison into True or False, if the result is certain.
d26dc4b5 13096
c800f862
RD
13097 if AV = False then
13098 if True_Result or False_Result then
21791d97 13099 Result := Boolean_Literals (True_Result);
c800f862
RD
13100 Rewrite (N,
13101 Convert_To (Typ,
13102 New_Occurrence_Of (Result, Sloc (N))));
13103 Analyze_And_Resolve (N, Typ);
13104 Warn_On_Known_Condition (N);
13105 return;
13106 end if;
13107
13108 -- If this is the second iteration (AV = True), and the original
e7e4d230
AC
13109 -- node comes from source and we are not in an instance, then give
13110 -- a warning if we know result would be True or False. Note: we
13111 -- know Constant_Condition_Warnings is set if we get here.
c800f862
RD
13112
13113 elsif Comes_From_Source (Original_Node (N))
13114 and then not In_Instance
13115 then
13116 if True_Result then
ed2233dc 13117 Error_Msg_N
324ac540 13118 ("condition can only be False if invalid values present??",
c800f862
RD
13119 N);
13120 elsif False_Result then
ed2233dc 13121 Error_Msg_N
324ac540 13122 ("condition can only be True if invalid values present??",
c800f862
RD
13123 N);
13124 end if;
13125 end if;
13126 end;
13127
13128 -- Skip second iteration if not warning on constant conditions or
e7e4d230
AC
13129 -- if the first iteration already generated a warning of some kind or
13130 -- if we are in any case assuming all values are valid (so that the
13131 -- first iteration took care of the valid case).
c800f862
RD
13132
13133 exit when not Constant_Condition_Warnings;
13134 exit when Warning_Generated;
13135 exit when Assume_No_Invalid_Values;
13136 end loop;
70482933
RK
13137 end Rewrite_Comparison;
13138
fbf5a39b
AC
13139 ----------------------------
13140 -- Safe_In_Place_Array_Op --
13141 ----------------------------
13142
13143 function Safe_In_Place_Array_Op
2e071734
AC
13144 (Lhs : Node_Id;
13145 Op1 : Node_Id;
13146 Op2 : Node_Id) return Boolean
fbf5a39b
AC
13147 is
13148 Target : Entity_Id;
13149
13150 function Is_Safe_Operand (Op : Node_Id) return Boolean;
13151 -- Operand is safe if it cannot overlap part of the target of the
13152 -- operation. If the operand and the target are identical, the operand
13153 -- is safe. The operand can be empty in the case of negation.
13154
13155 function Is_Unaliased (N : Node_Id) return Boolean;
5e1c00fa 13156 -- Check that N is a stand-alone entity
fbf5a39b
AC
13157
13158 ------------------
13159 -- Is_Unaliased --
13160 ------------------
13161
13162 function Is_Unaliased (N : Node_Id) return Boolean is
13163 begin
13164 return
13165 Is_Entity_Name (N)
13166 and then No (Address_Clause (Entity (N)))
13167 and then No (Renamed_Object (Entity (N)));
13168 end Is_Unaliased;
13169
13170 ---------------------
13171 -- Is_Safe_Operand --
13172 ---------------------
13173
13174 function Is_Safe_Operand (Op : Node_Id) return Boolean is
13175 begin
13176 if No (Op) then
13177 return True;
13178
13179 elsif Is_Entity_Name (Op) then
13180 return Is_Unaliased (Op);
13181
303b4d58 13182 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
fbf5a39b
AC
13183 return Is_Unaliased (Prefix (Op));
13184
13185 elsif Nkind (Op) = N_Slice then
13186 return
13187 Is_Unaliased (Prefix (Op))
13188 and then Entity (Prefix (Op)) /= Target;
13189
13190 elsif Nkind (Op) = N_Op_Not then
13191 return Is_Safe_Operand (Right_Opnd (Op));
13192
13193 else
13194 return False;
13195 end if;
13196 end Is_Safe_Operand;
13197
b6b5cca8 13198 -- Start of processing for Safe_In_Place_Array_Op
fbf5a39b
AC
13199
13200 begin
685094bf
RD
13201 -- Skip this processing if the component size is different from system
13202 -- storage unit (since at least for NOT this would cause problems).
fbf5a39b 13203
eaa826f8 13204 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
fbf5a39b
AC
13205 return False;
13206
fbf5a39b
AC
13207 -- Cannot do in place stuff if non-standard Boolean representation
13208
eaa826f8 13209 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
fbf5a39b
AC
13210 return False;
13211
13212 elsif not Is_Unaliased (Lhs) then
13213 return False;
e7e4d230 13214
fbf5a39b
AC
13215 else
13216 Target := Entity (Lhs);
e7e4d230 13217 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
fbf5a39b
AC
13218 end if;
13219 end Safe_In_Place_Array_Op;
13220
70482933
RK
13221 -----------------------
13222 -- Tagged_Membership --
13223 -----------------------
13224
685094bf
RD
13225 -- There are two different cases to consider depending on whether the right
13226 -- operand is a class-wide type or not. If not we just compare the actual
13227 -- tag of the left expr to the target type tag:
70482933
RK
13228 --
13229 -- Left_Expr.Tag = Right_Type'Tag;
13230 --
685094bf
RD
13231 -- If it is a class-wide type we use the RT function CW_Membership which is
13232 -- usually implemented by looking in the ancestor tables contained in the
13233 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
70482933 13234
0669bebe
GB
13235 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13236 -- function IW_Membership which is usually implemented by looking in the
13237 -- table of abstract interface types plus the ancestor table contained in
13238 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13239
82878151
AC
13240 procedure Tagged_Membership
13241 (N : Node_Id;
13242 SCIL_Node : out Node_Id;
13243 Result : out Node_Id)
13244 is
70482933
RK
13245 Left : constant Node_Id := Left_Opnd (N);
13246 Right : constant Node_Id := Right_Opnd (N);
13247 Loc : constant Source_Ptr := Sloc (N);
13248
38171f43 13249 Full_R_Typ : Entity_Id;
70482933 13250 Left_Type : Entity_Id;
82878151 13251 New_Node : Node_Id;
70482933
RK
13252 Right_Type : Entity_Id;
13253 Obj_Tag : Node_Id;
13254
13255 begin
82878151
AC
13256 SCIL_Node := Empty;
13257
852dba80
AC
13258 -- Handle entities from the limited view
13259
13260 Left_Type := Available_View (Etype (Left));
13261 Right_Type := Available_View (Etype (Right));
70482933 13262
6cce2156
GD
13263 -- In the case where the type is an access type, the test is applied
13264 -- using the designated types (needed in Ada 2012 for implicit anonymous
13265 -- access conversions, for AI05-0149).
13266
13267 if Is_Access_Type (Right_Type) then
13268 Left_Type := Designated_Type (Left_Type);
13269 Right_Type := Designated_Type (Right_Type);
13270 end if;
13271
70482933
RK
13272 if Is_Class_Wide_Type (Left_Type) then
13273 Left_Type := Root_Type (Left_Type);
13274 end if;
13275
38171f43
AC
13276 if Is_Class_Wide_Type (Right_Type) then
13277 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
13278 else
13279 Full_R_Typ := Underlying_Type (Right_Type);
13280 end if;
13281
70482933
RK
13282 Obj_Tag :=
13283 Make_Selected_Component (Loc,
13284 Prefix => Relocate_Node (Left),
a9d8907c 13285 Selector_Name =>
e4494292 13286 New_Occurrence_Of (First_Tag_Component (Left_Type), Loc));
70482933
RK
13287
13288 if Is_Class_Wide_Type (Right_Type) then
758c442c 13289
0669bebe
GB
13290 -- No need to issue a run-time check if we statically know that the
13291 -- result of this membership test is always true. For example,
13292 -- considering the following declarations:
13293
13294 -- type Iface is interface;
13295 -- type T is tagged null record;
13296 -- type DT is new T and Iface with null record;
13297
13298 -- Obj1 : T;
13299 -- Obj2 : DT;
13300
13301 -- These membership tests are always true:
13302
13303 -- Obj1 in T'Class
13304 -- Obj2 in T'Class;
13305 -- Obj2 in Iface'Class;
13306
13307 -- We do not need to handle cases where the membership is illegal.
13308 -- For example:
13309
13310 -- Obj1 in DT'Class; -- Compile time error
13311 -- Obj1 in Iface'Class; -- Compile time error
13312
13313 if not Is_Class_Wide_Type (Left_Type)
4ac2477e
JM
13314 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
13315 Use_Full_View => True)
533369aa
AC
13316 or else (Is_Interface (Etype (Right_Type))
13317 and then Interface_Present_In_Ancestor
761f7dcb
AC
13318 (Typ => Left_Type,
13319 Iface => Etype (Right_Type))))
0669bebe 13320 then
e4494292 13321 Result := New_Occurrence_Of (Standard_True, Loc);
82878151 13322 return;
0669bebe
GB
13323 end if;
13324
758c442c
GD
13325 -- Ada 2005 (AI-251): Class-wide applied to interfaces
13326
630d30e9
RD
13327 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
13328
0669bebe 13329 -- Support to: "Iface_CW_Typ in Typ'Class"
630d30e9
RD
13330
13331 or else Is_Interface (Left_Type)
13332 then
dfd99a80
TQ
13333 -- Issue error if IW_Membership operation not available in a
13334 -- configurable run time setting.
13335
13336 if not RTE_Available (RE_IW_Membership) then
b4592168
GD
13337 Error_Msg_CRT
13338 ("dynamic membership test on interface types", N);
82878151
AC
13339 Result := Empty;
13340 return;
dfd99a80
TQ
13341 end if;
13342
82878151 13343 Result :=
758c442c
GD
13344 Make_Function_Call (Loc,
13345 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
13346 Parameter_Associations => New_List (
13347 Make_Attribute_Reference (Loc,
13348 Prefix => Obj_Tag,
13349 Attribute_Name => Name_Address),
e4494292 13350 New_Occurrence_Of (
38171f43 13351 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
758c442c
GD
13352 Loc)));
13353
13354 -- Ada 95: Normal case
13355
13356 else
82878151
AC
13357 Build_CW_Membership (Loc,
13358 Obj_Tag_Node => Obj_Tag,
13359 Typ_Tag_Node =>
e4494292 13360 New_Occurrence_Of (
38171f43 13361 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
82878151
AC
13362 Related_Nod => N,
13363 New_Node => New_Node);
13364
13365 -- Generate the SCIL node for this class-wide membership test.
13366 -- Done here because the previous call to Build_CW_Membership
13367 -- relocates Obj_Tag.
13368
13369 if Generate_SCIL then
13370 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
13371 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
13372 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
13373 end if;
13374
13375 Result := New_Node;
758c442c
GD
13376 end if;
13377
0669bebe
GB
13378 -- Right_Type is not a class-wide type
13379
70482933 13380 else
0669bebe
GB
13381 -- No need to check the tag of the object if Right_Typ is abstract
13382
13383 if Is_Abstract_Type (Right_Type) then
e4494292 13384 Result := New_Occurrence_Of (Standard_False, Loc);
0669bebe
GB
13385
13386 else
82878151 13387 Result :=
0669bebe
GB
13388 Make_Op_Eq (Loc,
13389 Left_Opnd => Obj_Tag,
13390 Right_Opnd =>
e4494292 13391 New_Occurrence_Of
38171f43 13392 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
0669bebe 13393 end if;
70482933 13394 end if;
70482933
RK
13395 end Tagged_Membership;
13396
13397 ------------------------------
13398 -- Unary_Op_Validity_Checks --
13399 ------------------------------
13400
13401 procedure Unary_Op_Validity_Checks (N : Node_Id) is
13402 begin
13403 if Validity_Checks_On and Validity_Check_Operands then
13404 Ensure_Valid (Right_Opnd (N));
13405 end if;
13406 end Unary_Op_Validity_Checks;
13407
13408end Exp_Ch4;