]> 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-- --
4c7e0990 9-- Copyright (C) 1992-2013, 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;
45with Exp_VFpt; use Exp_VFpt;
f02b8bb8 46with Freeze; use Freeze;
70482933 47with Inline; use Inline;
df3e68b1 48with Lib; use Lib;
26bff3d9 49with Namet; use Namet;
70482933
RK
50with Nlists; use Nlists;
51with Nmake; use Nmake;
52with Opt; use Opt;
25adc5fb 53with Par_SCO; use Par_SCO;
0669bebe
GB
54with Restrict; use Restrict;
55with Rident; use Rident;
70482933
RK
56with Rtsfind; use Rtsfind;
57with Sem; use Sem;
a4100e55 58with Sem_Aux; use Sem_Aux;
70482933 59with Sem_Cat; use Sem_Cat;
5d09245e 60with Sem_Ch3; use Sem_Ch3;
11fa950b 61with Sem_Ch8; use Sem_Ch8;
70482933
RK
62with Sem_Ch13; use Sem_Ch13;
63with Sem_Eval; use Sem_Eval;
64with Sem_Res; use Sem_Res;
65with Sem_Type; use Sem_Type;
66with Sem_Util; use Sem_Util;
07fc65c4 67with Sem_Warn; use Sem_Warn;
70482933 68with Sinfo; use Sinfo;
70482933
RK
69with Snames; use Snames;
70with Stand; use Stand;
7665e4bd 71with SCIL_LL; use SCIL_LL;
07fc65c4 72with Targparm; use Targparm;
70482933
RK
73with Tbuild; use Tbuild;
74with Ttypes; use Ttypes;
75with Uintp; use Uintp;
76with Urealp; use Urealp;
77with Validsw; use Validsw;
78
79package body Exp_Ch4 is
80
15ce9ca2
AC
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
70482933
RK
84
85 procedure Binary_Op_Validity_Checks (N : Node_Id);
86 pragma Inline (Binary_Op_Validity_Checks);
87 -- Performs validity checks for a binary operator
88
fbf5a39b
AC
89 procedure Build_Boolean_Array_Proc_Call
90 (N : Node_Id;
91 Op1 : Node_Id;
92 Op2 : Node_Id);
303b4d58 93 -- If a boolean array assignment can be done in place, build call to
fbf5a39b
AC
94 -- corresponding library procedure.
95
11fa950b
AC
96 function Current_Anonymous_Master return Entity_Id;
97 -- Return the entity of the heterogeneous finalization master belonging to
98 -- the current unit (either function, package or procedure). This master
99 -- services all anonymous access-to-controlled types. If the current unit
100 -- does not have such master, create one.
df3e68b1 101
26bff3d9
JM
102 procedure Displace_Allocator_Pointer (N : Node_Id);
103 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
104 -- Expand_Allocator_Expression. Allocating class-wide interface objects
105 -- this routine displaces the pointer to the allocated object to reference
106 -- the component referencing the corresponding secondary dispatch table.
107
fbf5a39b
AC
108 procedure Expand_Allocator_Expression (N : Node_Id);
109 -- Subsidiary to Expand_N_Allocator, for the case when the expression
110 -- is a qualified expression or an aggregate.
111
70482933
RK
112 procedure Expand_Array_Comparison (N : Node_Id);
113 -- This routine handles expansion of the comparison operators (N_Op_Lt,
114 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
115 -- code for these operators is similar, differing only in the details of
fbf5a39b
AC
116 -- the actual comparison call that is made. Special processing (call a
117 -- run-time routine)
70482933
RK
118
119 function Expand_Array_Equality
120 (Nod : Node_Id;
70482933
RK
121 Lhs : Node_Id;
122 Rhs : Node_Id;
0da2c8ac
AC
123 Bodies : List_Id;
124 Typ : Entity_Id) return Node_Id;
70482933 125 -- Expand an array equality into a call to a function implementing this
685094bf
RD
126 -- equality, and a call to it. Loc is the location for the generated nodes.
127 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
128 -- on which to attach bodies of local functions that are created in the
129 -- process. It is the responsibility of the caller to insert those bodies
130 -- at the right place. Nod provides the Sloc value for the generated code.
131 -- Normally the types used for the generated equality routine are taken
132 -- from Lhs and Rhs. However, in some situations of generated code, the
133 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
134 -- the type to be used for the formal parameters.
70482933
RK
135
136 procedure Expand_Boolean_Operator (N : Node_Id);
685094bf
RD
137 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
138 -- case of array type arguments.
70482933 139
5875f8d6
AC
140 procedure Expand_Short_Circuit_Operator (N : Node_Id);
141 -- Common expansion processing for short-circuit boolean operators
142
456cbfa5 143 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id);
5707e389
AC
144 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
145 -- where we allow comparison of "out of range" values.
456cbfa5 146
70482933
RK
147 function Expand_Composite_Equality
148 (Nod : Node_Id;
149 Typ : Entity_Id;
150 Lhs : Node_Id;
151 Rhs : Node_Id;
2e071734 152 Bodies : List_Id) return Node_Id;
685094bf
RD
153 -- Local recursive function used to expand equality for nested composite
154 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
155 -- to attach bodies of local functions that are created in the process.
3058f181 156 -- It is the responsibility of the caller to insert those bodies at the
685094bf
RD
157 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
158 -- are the left and right sides for the comparison, and Typ is the type of
3058f181 159 -- the objects to compare.
70482933 160
fdac1f80
AC
161 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
162 -- Routine to expand concatenation of a sequence of two or more operands
163 -- (in the list Operands) and replace node Cnode with the result of the
164 -- concatenation. The operands can be of any appropriate type, and can
165 -- include both arrays and singleton elements.
70482933 166
f6194278 167 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id);
5707e389
AC
168 -- N is an N_In membership test mode, with the overflow check mode set to
169 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
170 -- integer type. This is a case where top level processing is required to
171 -- handle overflow checks in subtrees.
f6194278 172
70482933 173 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
685094bf
RD
174 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
175 -- fixed. We do not have such a type at runtime, so the purpose of this
176 -- routine is to find the real type by looking up the tree. We also
177 -- determine if the operation must be rounded.
70482933 178
5d09245e
AC
179 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
180 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
181 -- discriminants if it has a constrained nominal type, unless the object
182 -- is a component of an enclosing Unchecked_Union object that is subject
183 -- to a per-object constraint and the enclosing object lacks inferable
184 -- discriminants.
185 --
186 -- An expression of an Unchecked_Union type has inferable discriminants
187 -- if it is either a name of an object with inferable discriminants or a
188 -- qualified expression whose subtype mark denotes a constrained subtype.
189
70482933 190 procedure Insert_Dereference_Action (N : Node_Id);
e6f69614
AC
191 -- N is an expression whose type is an access. When the type of the
192 -- associated storage pool is derived from Checked_Pool, generate a
193 -- call to the 'Dereference' primitive operation.
70482933
RK
194
195 function Make_Array_Comparison_Op
2e071734
AC
196 (Typ : Entity_Id;
197 Nod : Node_Id) return Node_Id;
685094bf
RD
198 -- Comparisons between arrays are expanded in line. This function produces
199 -- the body of the implementation of (a > b), where a and b are one-
200 -- dimensional arrays of some discrete type. The original node is then
201 -- expanded into the appropriate call to this function. Nod provides the
202 -- Sloc value for the generated code.
70482933
RK
203
204 function Make_Boolean_Array_Op
2e071734
AC
205 (Typ : Entity_Id;
206 N : Node_Id) return Node_Id;
685094bf
RD
207 -- Boolean operations on boolean arrays are expanded in line. This function
208 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
209 -- b). It is used only the normal case and not the packed case. The type
210 -- involved, Typ, is the Boolean array type, and the logical operations in
211 -- the body are simple boolean operations. Note that Typ is always a
212 -- constrained type (the caller has ensured this by using
213 -- Convert_To_Actual_Subtype if necessary).
70482933 214
b6b5cca8 215 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean;
a7f1b24f
RD
216 -- For signed arithmetic operations when the current overflow mode is
217 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
218 -- as the first thing we do. We then return. We count on the recursive
219 -- apparatus for overflow checks to call us back with an equivalent
220 -- operation that is in CHECKED mode, avoiding a recursive entry into this
221 -- routine, and that is when we will proceed with the expansion of the
222 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
223 -- these optimizations without first making this check, since there may be
224 -- operands further down the tree that are relying on the recursive calls
225 -- triggered by the top level nodes to properly process overflow checking
226 -- and remaining expansion on these nodes. Note that this call back may be
227 -- skipped if the operation is done in Bignum mode but that's fine, since
228 -- the Bignum call takes care of everything.
b6b5cca8 229
0580d807
AC
230 procedure Optimize_Length_Comparison (N : Node_Id);
231 -- Given an expression, if it is of the form X'Length op N (or the other
232 -- way round), where N is known at compile time to be 0 or 1, and X is a
233 -- simple entity, and op is a comparison operator, optimizes it into a
234 -- comparison of First and Last.
235
b2c28399
AC
236 procedure Process_Transient_Object
237 (Decl : Node_Id;
238 Rel_Node : Node_Id);
239 -- Subsidiary routine to the expansion of expression_with_actions and if
240 -- expressions. Generate all the necessary code to finalize a transient
241 -- controlled object when the enclosing context is elaborated or evaluated.
242 -- Decl denotes the declaration of the transient controlled object which is
243 -- usually the result of a controlled function call. Rel_Node denotes the
244 -- context, either an expression_with_actions or an if expression.
245
70482933 246 procedure Rewrite_Comparison (N : Node_Id);
20b5d666 247 -- If N is the node for a comparison whose outcome can be determined at
d26dc4b5
AC
248 -- compile time, then the node N can be rewritten with True or False. If
249 -- the outcome cannot be determined at compile time, the call has no
250 -- effect. If N is a type conversion, then this processing is applied to
251 -- its expression. If N is neither comparison nor a type conversion, the
252 -- call has no effect.
70482933 253
82878151
AC
254 procedure Tagged_Membership
255 (N : Node_Id;
256 SCIL_Node : out Node_Id;
257 Result : out Node_Id);
70482933
RK
258 -- Construct the expression corresponding to the tagged membership test.
259 -- Deals with a second operand being (or not) a class-wide type.
260
fbf5a39b 261 function Safe_In_Place_Array_Op
2e071734
AC
262 (Lhs : Node_Id;
263 Op1 : Node_Id;
264 Op2 : Node_Id) return Boolean;
685094bf
RD
265 -- In the context of an assignment, where the right-hand side is a boolean
266 -- operation on arrays, check whether operation can be performed in place.
fbf5a39b 267
70482933
RK
268 procedure Unary_Op_Validity_Checks (N : Node_Id);
269 pragma Inline (Unary_Op_Validity_Checks);
270 -- Performs validity checks for a unary operator
271
272 -------------------------------
273 -- Binary_Op_Validity_Checks --
274 -------------------------------
275
276 procedure Binary_Op_Validity_Checks (N : Node_Id) is
277 begin
278 if Validity_Checks_On and Validity_Check_Operands then
279 Ensure_Valid (Left_Opnd (N));
280 Ensure_Valid (Right_Opnd (N));
281 end if;
282 end Binary_Op_Validity_Checks;
283
fbf5a39b
AC
284 ------------------------------------
285 -- Build_Boolean_Array_Proc_Call --
286 ------------------------------------
287
288 procedure Build_Boolean_Array_Proc_Call
289 (N : Node_Id;
290 Op1 : Node_Id;
291 Op2 : Node_Id)
292 is
293 Loc : constant Source_Ptr := Sloc (N);
294 Kind : constant Node_Kind := Nkind (Expression (N));
295 Target : constant Node_Id :=
296 Make_Attribute_Reference (Loc,
297 Prefix => Name (N),
298 Attribute_Name => Name_Address);
299
bed8af19 300 Arg1 : Node_Id := Op1;
fbf5a39b
AC
301 Arg2 : Node_Id := Op2;
302 Call_Node : Node_Id;
303 Proc_Name : Entity_Id;
304
305 begin
306 if Kind = N_Op_Not then
307 if Nkind (Op1) in N_Binary_Op then
308
5e1c00fa 309 -- Use negated version of the binary operators
fbf5a39b
AC
310
311 if Nkind (Op1) = N_Op_And then
312 Proc_Name := RTE (RE_Vector_Nand);
313
314 elsif Nkind (Op1) = N_Op_Or then
315 Proc_Name := RTE (RE_Vector_Nor);
316
317 else pragma Assert (Nkind (Op1) = N_Op_Xor);
318 Proc_Name := RTE (RE_Vector_Xor);
319 end if;
320
321 Call_Node :=
322 Make_Procedure_Call_Statement (Loc,
323 Name => New_Occurrence_Of (Proc_Name, Loc),
324
325 Parameter_Associations => New_List (
326 Target,
327 Make_Attribute_Reference (Loc,
328 Prefix => Left_Opnd (Op1),
329 Attribute_Name => Name_Address),
330
331 Make_Attribute_Reference (Loc,
332 Prefix => Right_Opnd (Op1),
333 Attribute_Name => Name_Address),
334
335 Make_Attribute_Reference (Loc,
336 Prefix => Left_Opnd (Op1),
337 Attribute_Name => Name_Length)));
338
339 else
340 Proc_Name := RTE (RE_Vector_Not);
341
342 Call_Node :=
343 Make_Procedure_Call_Statement (Loc,
344 Name => New_Occurrence_Of (Proc_Name, Loc),
345 Parameter_Associations => New_List (
346 Target,
347
348 Make_Attribute_Reference (Loc,
349 Prefix => Op1,
350 Attribute_Name => Name_Address),
351
352 Make_Attribute_Reference (Loc,
353 Prefix => Op1,
354 Attribute_Name => Name_Length)));
355 end if;
356
357 else
358 -- We use the following equivalences:
359
360 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
361 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
362 -- (not X) xor (not Y) = X xor Y
363 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
364
365 if Nkind (Op1) = N_Op_Not then
bed8af19
AC
366 Arg1 := Right_Opnd (Op1);
367 Arg2 := Right_Opnd (Op2);
533369aa 368
fbf5a39b
AC
369 if Kind = N_Op_And then
370 Proc_Name := RTE (RE_Vector_Nor);
fbf5a39b
AC
371 elsif Kind = N_Op_Or then
372 Proc_Name := RTE (RE_Vector_Nand);
fbf5a39b
AC
373 else
374 Proc_Name := RTE (RE_Vector_Xor);
375 end if;
376
377 else
378 if Kind = N_Op_And then
379 Proc_Name := RTE (RE_Vector_And);
fbf5a39b
AC
380 elsif Kind = N_Op_Or then
381 Proc_Name := RTE (RE_Vector_Or);
fbf5a39b
AC
382 elsif Nkind (Op2) = N_Op_Not then
383 Proc_Name := RTE (RE_Vector_Nxor);
384 Arg2 := Right_Opnd (Op2);
fbf5a39b
AC
385 else
386 Proc_Name := RTE (RE_Vector_Xor);
387 end if;
388 end if;
389
390 Call_Node :=
391 Make_Procedure_Call_Statement (Loc,
392 Name => New_Occurrence_Of (Proc_Name, Loc),
393 Parameter_Associations => New_List (
394 Target,
955871d3
AC
395 Make_Attribute_Reference (Loc,
396 Prefix => Arg1,
397 Attribute_Name => Name_Address),
398 Make_Attribute_Reference (Loc,
399 Prefix => Arg2,
400 Attribute_Name => Name_Address),
401 Make_Attribute_Reference (Loc,
a8ef12e5 402 Prefix => Arg1,
955871d3 403 Attribute_Name => Name_Length)));
fbf5a39b
AC
404 end if;
405
406 Rewrite (N, Call_Node);
407 Analyze (N);
408
409 exception
410 when RE_Not_Available =>
411 return;
412 end Build_Boolean_Array_Proc_Call;
413
11fa950b
AC
414 ------------------------------
415 -- Current_Anonymous_Master --
416 ------------------------------
df3e68b1 417
11fa950b 418 function Current_Anonymous_Master return Entity_Id is
2c17ca0a
AC
419 Decls : List_Id;
420 Loc : Source_Ptr;
421 Subp_Body : Node_Id;
422 Unit_Decl : Node_Id;
423 Unit_Id : Entity_Id;
df3e68b1 424
ca5af305 425 begin
11fa950b
AC
426 Unit_Id := Cunit_Entity (Current_Sem_Unit);
427
428 -- Find the entity of the current unit
429
430 if Ekind (Unit_Id) = E_Subprogram_Body then
431
432 -- When processing subprogram bodies, the proper scope is always that
433 -- of the spec.
434
435 Subp_Body := Unit_Id;
436 while Present (Subp_Body)
437 and then Nkind (Subp_Body) /= N_Subprogram_Body
438 loop
439 Subp_Body := Parent (Subp_Body);
440 end loop;
441
442 Unit_Id := Corresponding_Spec (Subp_Body);
443 end if;
444
445 Loc := Sloc (Unit_Id);
446 Unit_Decl := Unit (Cunit (Current_Sem_Unit));
447
448 -- Find the declarations list of the current unit
449
450 if Nkind (Unit_Decl) = N_Package_Declaration then
451 Unit_Decl := Specification (Unit_Decl);
452 Decls := Visible_Declarations (Unit_Decl);
df3e68b1 453
ca5af305 454 if No (Decls) then
11fa950b
AC
455 Decls := New_List (Make_Null_Statement (Loc));
456 Set_Visible_Declarations (Unit_Decl, Decls);
df3e68b1 457
ca5af305 458 elsif Is_Empty_List (Decls) then
11fa950b 459 Append_To (Decls, Make_Null_Statement (Loc));
df3e68b1
HK
460 end if;
461
ca5af305 462 else
11fa950b 463 Decls := Declarations (Unit_Decl);
f553e7bc 464
ca5af305 465 if No (Decls) then
11fa950b
AC
466 Decls := New_List (Make_Null_Statement (Loc));
467 Set_Declarations (Unit_Decl, Decls);
df3e68b1 468
ca5af305 469 elsif Is_Empty_List (Decls) then
11fa950b 470 Append_To (Decls, Make_Null_Statement (Loc));
ca5af305 471 end if;
df3e68b1
HK
472 end if;
473
11fa950b
AC
474 -- The current unit has an existing anonymous master, traverse its
475 -- declarations and locate the entity.
df3e68b1 476
11fa950b 477 if Has_Anonymous_Master (Unit_Id) then
2c17ca0a
AC
478 declare
479 Decl : Node_Id;
480 Fin_Mas_Id : Entity_Id;
df3e68b1 481
2c17ca0a
AC
482 begin
483 Decl := First (Decls);
484 while Present (Decl) loop
df3e68b1 485
2c17ca0a
AC
486 -- Look for the first variable in the declarations whole type
487 -- is Finalization_Master.
df3e68b1 488
2c17ca0a
AC
489 if Nkind (Decl) = N_Object_Declaration then
490 Fin_Mas_Id := Defining_Identifier (Decl);
491
492 if Ekind (Fin_Mas_Id) = E_Variable
493 and then Etype (Fin_Mas_Id) = RTE (RE_Finalization_Master)
494 then
495 return Fin_Mas_Id;
496 end if;
497 end if;
498
499 Next (Decl);
500 end loop;
501
502 -- The master was not found even though the unit was labeled as
503 -- having one.
df3e68b1 504
2c17ca0a
AC
505 raise Program_Error;
506 end;
11fa950b
AC
507
508 -- Create a new anonymous master
509
510 else
511 declare
512 First_Decl : constant Node_Id := First (Decls);
513 Action : Node_Id;
2c17ca0a 514 Fin_Mas_Id : Entity_Id;
df3e68b1 515
11fa950b
AC
516 begin
517 -- Since the master and its associated initialization is inserted
518 -- at top level, use the scope of the unit when analyzing.
519
520 Push_Scope (Unit_Id);
521
522 -- Create the finalization master
523
524 Fin_Mas_Id :=
525 Make_Defining_Identifier (Loc,
526 Chars => New_External_Name (Chars (Unit_Id), "AM"));
527
528 -- Generate:
529 -- <Fin_Mas_Id> : Finalization_Master;
530
531 Action :=
532 Make_Object_Declaration (Loc,
533 Defining_Identifier => Fin_Mas_Id,
534 Object_Definition =>
535 New_Reference_To (RTE (RE_Finalization_Master), Loc));
536
537 Insert_Before_And_Analyze (First_Decl, Action);
538
539 -- Mark the unit to prevent the generation of multiple masters
540
541 Set_Has_Anonymous_Master (Unit_Id);
542
543 -- Do not set the base pool and mode of operation on .NET/JVM
544 -- since those targets do not support pools and all VM masters
545 -- are heterogeneous by default.
546
547 if VM_Target = No_VM then
548
549 -- Generate:
550 -- Set_Base_Pool
551 -- (<Fin_Mas_Id>, Global_Pool_Object'Unrestricted_Access);
552
553 Action :=
554 Make_Procedure_Call_Statement (Loc,
555 Name =>
556 New_Reference_To (RTE (RE_Set_Base_Pool), Loc),
557
558 Parameter_Associations => New_List (
559 New_Reference_To (Fin_Mas_Id, Loc),
560 Make_Attribute_Reference (Loc,
561 Prefix =>
562 New_Reference_To (RTE (RE_Global_Pool_Object), Loc),
563 Attribute_Name => Name_Unrestricted_Access)));
564
565 Insert_Before_And_Analyze (First_Decl, Action);
566
567 -- Generate:
568 -- Set_Is_Heterogeneous (<Fin_Mas_Id>);
569
570 Action :=
571 Make_Procedure_Call_Statement (Loc,
572 Name =>
573 New_Reference_To (RTE (RE_Set_Is_Heterogeneous), Loc),
574 Parameter_Associations => New_List (
575 New_Reference_To (Fin_Mas_Id, Loc)));
576
577 Insert_Before_And_Analyze (First_Decl, Action);
578 end if;
579
580 -- Restore the original state of the scope stack
581
582 Pop_Scope;
583
584 return Fin_Mas_Id;
585 end;
586 end if;
587 end Current_Anonymous_Master;
df3e68b1 588
26bff3d9
JM
589 --------------------------------
590 -- Displace_Allocator_Pointer --
591 --------------------------------
592
593 procedure Displace_Allocator_Pointer (N : Node_Id) is
594 Loc : constant Source_Ptr := Sloc (N);
595 Orig_Node : constant Node_Id := Original_Node (N);
596 Dtyp : Entity_Id;
597 Etyp : Entity_Id;
598 PtrT : Entity_Id;
599
600 begin
303b4d58
AC
601 -- Do nothing in case of VM targets: the virtual machine will handle
602 -- interfaces directly.
603
1f110335 604 if not Tagged_Type_Expansion then
303b4d58
AC
605 return;
606 end if;
607
26bff3d9
JM
608 pragma Assert (Nkind (N) = N_Identifier
609 and then Nkind (Orig_Node) = N_Allocator);
610
611 PtrT := Etype (Orig_Node);
d6a24cdb 612 Dtyp := Available_View (Designated_Type (PtrT));
26bff3d9
JM
613 Etyp := Etype (Expression (Orig_Node));
614
533369aa
AC
615 if Is_Class_Wide_Type (Dtyp) and then Is_Interface (Dtyp) then
616
26bff3d9
JM
617 -- If the type of the allocator expression is not an interface type
618 -- we can generate code to reference the record component containing
619 -- the pointer to the secondary dispatch table.
620
621 if not Is_Interface (Etyp) then
622 declare
623 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
624
625 begin
626 -- 1) Get access to the allocated object
627
628 Rewrite (N,
5972791c 629 Make_Explicit_Dereference (Loc, Relocate_Node (N)));
26bff3d9
JM
630 Set_Etype (N, Etyp);
631 Set_Analyzed (N);
632
633 -- 2) Add the conversion to displace the pointer to reference
634 -- the secondary dispatch table.
635
636 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
637 Analyze_And_Resolve (N, Dtyp);
638
639 -- 3) The 'access to the secondary dispatch table will be used
640 -- as the value returned by the allocator.
641
642 Rewrite (N,
643 Make_Attribute_Reference (Loc,
644 Prefix => Relocate_Node (N),
645 Attribute_Name => Name_Access));
646 Set_Etype (N, Saved_Typ);
647 Set_Analyzed (N);
648 end;
649
650 -- If the type of the allocator expression is an interface type we
651 -- generate a run-time call to displace "this" to reference the
652 -- component containing the pointer to the secondary dispatch table
653 -- or else raise Constraint_Error if the actual object does not
533369aa 654 -- implement the target interface. This case corresponds to the
26bff3d9
JM
655 -- following example:
656
8fc789c8 657 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
26bff3d9
JM
658 -- begin
659 -- return new Iface_2'Class'(Obj);
660 -- end Op;
661
662 else
663 Rewrite (N,
664 Unchecked_Convert_To (PtrT,
665 Make_Function_Call (Loc,
666 Name => New_Reference_To (RTE (RE_Displace), Loc),
667 Parameter_Associations => New_List (
668 Unchecked_Convert_To (RTE (RE_Address),
669 Relocate_Node (N)),
670
671 New_Occurrence_Of
672 (Elists.Node
673 (First_Elmt
674 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
675 Loc)))));
676 Analyze_And_Resolve (N, PtrT);
677 end if;
678 end if;
679 end Displace_Allocator_Pointer;
680
fbf5a39b
AC
681 ---------------------------------
682 -- Expand_Allocator_Expression --
683 ---------------------------------
684
685 procedure Expand_Allocator_Expression (N : Node_Id) is
f02b8bb8
RD
686 Loc : constant Source_Ptr := Sloc (N);
687 Exp : constant Node_Id := Expression (Expression (N));
f02b8bb8
RD
688 PtrT : constant Entity_Id := Etype (N);
689 DesigT : constant Entity_Id := Designated_Type (PtrT);
26bff3d9
JM
690
691 procedure Apply_Accessibility_Check
692 (Ref : Node_Id;
693 Built_In_Place : Boolean := False);
694 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
685094bf
RD
695 -- type, generate an accessibility check to verify that the level of the
696 -- type of the created object is not deeper than the level of the access
50878404 697 -- type. If the type of the qualified expression is class-wide, then
685094bf
RD
698 -- always generate the check (except in the case where it is known to be
699 -- unnecessary, see comment below). Otherwise, only generate the check
700 -- if the level of the qualified expression type is statically deeper
701 -- than the access type.
702 --
703 -- Although the static accessibility will generally have been performed
704 -- as a legality check, it won't have been done in cases where the
705 -- allocator appears in generic body, so a run-time check is needed in
706 -- general. One special case is when the access type is declared in the
707 -- same scope as the class-wide allocator, in which case the check can
708 -- never fail, so it need not be generated.
709 --
710 -- As an open issue, there seem to be cases where the static level
711 -- associated with the class-wide object's underlying type is not
712 -- sufficient to perform the proper accessibility check, such as for
713 -- allocators in nested subprograms or accept statements initialized by
714 -- class-wide formals when the actual originates outside at a deeper
715 -- static level. The nested subprogram case might require passing
716 -- accessibility levels along with class-wide parameters, and the task
717 -- case seems to be an actual gap in the language rules that needs to
718 -- be fixed by the ARG. ???
26bff3d9
JM
719
720 -------------------------------
721 -- Apply_Accessibility_Check --
722 -------------------------------
723
724 procedure Apply_Accessibility_Check
725 (Ref : Node_Id;
726 Built_In_Place : Boolean := False)
727 is
a98838ff
HK
728 Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT);
729 Cond : Node_Id;
730 Fin_Call : Node_Id;
731 Free_Stmt : Node_Id;
732 Obj_Ref : Node_Id;
733 Stmts : List_Id;
26bff3d9
JM
734
735 begin
0791fbe9 736 if Ada_Version >= Ada_2005
26bff3d9 737 and then Is_Class_Wide_Type (DesigT)
a98838ff 738 and then (Tagged_Type_Expansion or else VM_Target /= No_VM)
3217f71e 739 and then not Scope_Suppress.Suppress (Accessibility_Check)
26bff3d9
JM
740 and then
741 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
742 or else
743 (Is_Class_Wide_Type (Etype (Exp))
744 and then Scope (PtrT) /= Current_Scope))
745 then
e761d11c 746 -- If the allocator was built in place, Ref is already a reference
26bff3d9 747 -- to the access object initialized to the result of the allocator
e761d11c
AC
748 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
749 -- Remove_Side_Effects for cases where the build-in-place call may
750 -- still be the prefix of the reference (to avoid generating
751 -- duplicate calls). Otherwise, it is the entity associated with
752 -- the object containing the address of the allocated object.
26bff3d9
JM
753
754 if Built_In_Place then
e761d11c 755 Remove_Side_Effects (Ref);
a98838ff 756 Obj_Ref := New_Copy_Tree (Ref);
26bff3d9 757 else
50878404
AC
758 Obj_Ref := New_Reference_To (Ref, Loc);
759 end if;
760
761 -- Step 1: Create the object clean up code
762
763 Stmts := New_List;
764
a98838ff
HK
765 -- Deallocate the object if the accessibility check fails. This
766 -- is done only on targets or profiles that support deallocation.
767
768 -- Free (Obj_Ref);
769
770 if RTE_Available (RE_Free) then
771 Free_Stmt := Make_Free_Statement (Loc, New_Copy_Tree (Obj_Ref));
772 Set_Storage_Pool (Free_Stmt, Pool_Id);
773
774 Append_To (Stmts, Free_Stmt);
775
776 -- The target or profile cannot deallocate objects
777
778 else
779 Free_Stmt := Empty;
780 end if;
781
782 -- Finalize the object if applicable. Generate:
a530b8bb
AC
783
784 -- [Deep_]Finalize (Obj_Ref.all);
785
2cbac6c6 786 if Needs_Finalization (DesigT) then
a98838ff 787 Fin_Call :=
2cbac6c6
AC
788 Make_Final_Call (
789 Obj_Ref =>
790 Make_Explicit_Dereference (Loc, New_Copy (Obj_Ref)),
a98838ff
HK
791 Typ => DesigT);
792
793 -- When the target or profile supports deallocation, wrap the
794 -- finalization call in a block to ensure proper deallocation
795 -- even if finalization fails. Generate:
796
797 -- begin
798 -- <Fin_Call>
799 -- exception
800 -- when others =>
801 -- <Free_Stmt>
802 -- raise;
803 -- end;
804
805 if Present (Free_Stmt) then
806 Fin_Call :=
807 Make_Block_Statement (Loc,
808 Handled_Statement_Sequence =>
809 Make_Handled_Sequence_Of_Statements (Loc,
810 Statements => New_List (Fin_Call),
811
812 Exception_Handlers => New_List (
813 Make_Exception_Handler (Loc,
814 Exception_Choices => New_List (
815 Make_Others_Choice (Loc)),
816
817 Statements => New_List (
818 New_Copy_Tree (Free_Stmt),
819 Make_Raise_Statement (Loc))))));
820 end if;
821
822 Prepend_To (Stmts, Fin_Call);
f46faa08
AC
823 end if;
824
50878404
AC
825 -- Signal the accessibility failure through a Program_Error
826
827 Append_To (Stmts,
828 Make_Raise_Program_Error (Loc,
829 Condition => New_Reference_To (Standard_True, Loc),
830 Reason => PE_Accessibility_Check_Failed));
831
832 -- Step 2: Create the accessibility comparison
833
834 -- Generate:
835 -- Ref'Tag
836
837 Obj_Ref :=
f46faa08 838 Make_Attribute_Reference (Loc,
50878404 839 Prefix => Obj_Ref,
f46faa08
AC
840 Attribute_Name => Name_Tag);
841
50878404
AC
842 -- For tagged types, determine the accessibility level by looking
843 -- at the type specific data of the dispatch table. Generate:
844
845 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
846
f46faa08 847 if Tagged_Type_Expansion then
50878404 848 Cond := Build_Get_Access_Level (Loc, Obj_Ref);
f46faa08 849
50878404
AC
850 -- Use a runtime call to determine the accessibility level when
851 -- compiling on virtual machine targets. Generate:
f46faa08 852
50878404 853 -- Get_Access_Level (Ref'Tag)
f46faa08
AC
854
855 else
50878404
AC
856 Cond :=
857 Make_Function_Call (Loc,
858 Name =>
859 New_Reference_To (RTE (RE_Get_Access_Level), Loc),
860 Parameter_Associations => New_List (Obj_Ref));
26bff3d9
JM
861 end if;
862
50878404
AC
863 Cond :=
864 Make_Op_Gt (Loc,
865 Left_Opnd => Cond,
866 Right_Opnd =>
867 Make_Integer_Literal (Loc, Type_Access_Level (PtrT)));
868
869 -- Due to the complexity and side effects of the check, utilize an
870 -- if statement instead of the regular Program_Error circuitry.
871
26bff3d9 872 Insert_Action (N,
8b1011c0 873 Make_Implicit_If_Statement (N,
50878404
AC
874 Condition => Cond,
875 Then_Statements => Stmts));
26bff3d9
JM
876 end if;
877 end Apply_Accessibility_Check;
878
879 -- Local variables
880
df3e68b1
HK
881 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
882 Indic : constant Node_Id := Subtype_Mark (Expression (N));
883 T : constant Entity_Id := Entity (Indic);
884 Node : Node_Id;
885 Tag_Assign : Node_Id;
886 Temp : Entity_Id;
887 Temp_Decl : Node_Id;
fbf5a39b 888
d26dc4b5
AC
889 TagT : Entity_Id := Empty;
890 -- Type used as source for tag assignment
891
892 TagR : Node_Id := Empty;
893 -- Target reference for tag assignment
894
26bff3d9
JM
895 -- Start of processing for Expand_Allocator_Expression
896
fbf5a39b 897 begin
3bfb3c03
JM
898 -- Handle call to C++ constructor
899
900 if Is_CPP_Constructor_Call (Exp) then
901 Make_CPP_Constructor_Call_In_Allocator
902 (Allocator => N,
903 Function_Call => Exp);
904 return;
905 end if;
906
885c4871 907 -- In the case of an Ada 2012 allocator whose initial value comes from a
63585f75
SB
908 -- function call, pass "the accessibility level determined by the point
909 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
910 -- Expand_Call but it couldn't be done there (because the Etype of the
911 -- allocator wasn't set then) so we generate the parameter here. See
912 -- the Boolean variable Defer in (a block within) Expand_Call.
913
914 if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then
915 declare
916 Subp : Entity_Id;
917
918 begin
919 if Nkind (Name (Exp)) = N_Explicit_Dereference then
920 Subp := Designated_Type (Etype (Prefix (Name (Exp))));
921 else
922 Subp := Entity (Name (Exp));
923 end if;
924
57a3fca9
AC
925 Subp := Ultimate_Alias (Subp);
926
63585f75
SB
927 if Present (Extra_Accessibility_Of_Result (Subp)) then
928 Add_Extra_Actual_To_Call
929 (Subprogram_Call => Exp,
930 Extra_Formal => Extra_Accessibility_Of_Result (Subp),
931 Extra_Actual => Dynamic_Accessibility_Level (PtrT));
932 end if;
933 end;
934 end if;
935
f6194278 936 -- Case of tagged type or type requiring finalization
63585f75
SB
937
938 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
fadcf313 939
685094bf
RD
940 -- Ada 2005 (AI-318-02): If the initialization expression is a call
941 -- to a build-in-place function, then access to the allocated object
942 -- must be passed to the function. Currently we limit such functions
943 -- to those with constrained limited result subtypes, but eventually
944 -- we plan to expand the allowed forms of functions that are treated
945 -- as build-in-place.
20b5d666 946
0791fbe9 947 if Ada_Version >= Ada_2005
20b5d666
JM
948 and then Is_Build_In_Place_Function_Call (Exp)
949 then
950 Make_Build_In_Place_Call_In_Allocator (N, Exp);
26bff3d9
JM
951 Apply_Accessibility_Check (N, Built_In_Place => True);
952 return;
20b5d666
JM
953 end if;
954
ca5af305
AC
955 -- Actions inserted before:
956 -- Temp : constant ptr_T := new T'(Expression);
957 -- Temp._tag = T'tag; -- when not class-wide
958 -- [Deep_]Adjust (Temp.all);
fbf5a39b 959
ca5af305
AC
960 -- We analyze by hand the new internal allocator to avoid any
961 -- recursion and inappropriate call to Initialize
7324bf49 962
20b5d666
JM
963 -- We don't want to remove side effects when the expression must be
964 -- built in place. In the case of a build-in-place function call,
965 -- that could lead to a duplication of the call, which was already
966 -- substituted for the allocator.
967
26bff3d9 968 if not Aggr_In_Place then
fbf5a39b
AC
969 Remove_Side_Effects (Exp);
970 end if;
971
e86a3a7e 972 Temp := Make_Temporary (Loc, 'P', N);
fbf5a39b
AC
973
974 -- For a class wide allocation generate the following code:
975
976 -- type Equiv_Record is record ... end record;
977 -- implicit subtype CW is <Class_Wide_Subytpe>;
978 -- temp : PtrT := new CW'(CW!(expr));
979
980 if Is_Class_Wide_Type (T) then
981 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
982
26bff3d9
JM
983 -- Ada 2005 (AI-251): If the expression is a class-wide interface
984 -- object we generate code to move up "this" to reference the
985 -- base of the object before allocating the new object.
986
987 -- Note that Exp'Address is recursively expanded into a call
988 -- to Base_Address (Exp.Tag)
989
990 if Is_Class_Wide_Type (Etype (Exp))
991 and then Is_Interface (Etype (Exp))
1f110335 992 and then Tagged_Type_Expansion
26bff3d9
JM
993 then
994 Set_Expression
995 (Expression (N),
996 Unchecked_Convert_To (Entity (Indic),
997 Make_Explicit_Dereference (Loc,
998 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
999 Make_Attribute_Reference (Loc,
1000 Prefix => Exp,
1001 Attribute_Name => Name_Address)))));
26bff3d9
JM
1002 else
1003 Set_Expression
1004 (Expression (N),
1005 Unchecked_Convert_To (Entity (Indic), Exp));
1006 end if;
fbf5a39b
AC
1007
1008 Analyze_And_Resolve (Expression (N), Entity (Indic));
1009 end if;
1010
df3e68b1 1011 -- Processing for allocators returning non-interface types
fbf5a39b 1012
26bff3d9
JM
1013 if not Is_Interface (Directly_Designated_Type (PtrT)) then
1014 if Aggr_In_Place then
df3e68b1 1015 Temp_Decl :=
26bff3d9
JM
1016 Make_Object_Declaration (Loc,
1017 Defining_Identifier => Temp,
1018 Object_Definition => New_Reference_To (PtrT, Loc),
1019 Expression =>
1020 Make_Allocator (Loc,
df3e68b1
HK
1021 Expression =>
1022 New_Reference_To (Etype (Exp), Loc)));
fbf5a39b 1023
fad0600d
AC
1024 -- Copy the Comes_From_Source flag for the allocator we just
1025 -- built, since logically this allocator is a replacement of
1026 -- the original allocator node. This is for proper handling of
1027 -- restriction No_Implicit_Heap_Allocations.
1028
26bff3d9 1029 Set_Comes_From_Source
df3e68b1 1030 (Expression (Temp_Decl), Comes_From_Source (N));
fbf5a39b 1031
df3e68b1
HK
1032 Set_No_Initialization (Expression (Temp_Decl));
1033 Insert_Action (N, Temp_Decl);
fbf5a39b 1034
ca5af305 1035 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
df3e68b1 1036 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
fad0600d 1037
d3f70b35 1038 -- Attach the object to the associated finalization master.
deb8dacc
HK
1039 -- This is done manually on .NET/JVM since those compilers do
1040 -- no support pools and can't benefit from internally generated
1041 -- Allocate / Deallocate procedures.
1042
1043 if VM_Target /= No_VM
1044 and then Is_Controlled (DesigT)
d3f70b35 1045 and then Present (Finalization_Master (PtrT))
deb8dacc
HK
1046 then
1047 Insert_Action (N,
1048 Make_Attach_Call (
1049 Obj_Ref =>
1050 New_Reference_To (Temp, Loc),
1051 Ptr_Typ => PtrT));
1052 end if;
1053
26bff3d9
JM
1054 else
1055 Node := Relocate_Node (N);
1056 Set_Analyzed (Node);
df3e68b1
HK
1057
1058 Temp_Decl :=
26bff3d9
JM
1059 Make_Object_Declaration (Loc,
1060 Defining_Identifier => Temp,
1061 Constant_Present => True,
1062 Object_Definition => New_Reference_To (PtrT, Loc),
df3e68b1
HK
1063 Expression => Node);
1064
1065 Insert_Action (N, Temp_Decl);
ca5af305 1066 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
deb8dacc 1067
d3f70b35 1068 -- Attach the object to the associated finalization master.
deb8dacc
HK
1069 -- This is done manually on .NET/JVM since those compilers do
1070 -- no support pools and can't benefit from internally generated
1071 -- Allocate / Deallocate procedures.
1072
1073 if VM_Target /= No_VM
1074 and then Is_Controlled (DesigT)
d3f70b35 1075 and then Present (Finalization_Master (PtrT))
deb8dacc
HK
1076 then
1077 Insert_Action (N,
1078 Make_Attach_Call (
1079 Obj_Ref =>
1080 New_Reference_To (Temp, Loc),
1081 Ptr_Typ => PtrT));
1082 end if;
fbf5a39b
AC
1083 end if;
1084
26bff3d9
JM
1085 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
1086 -- interface type. In this case we use the type of the qualified
1087 -- expression to allocate the object.
1088
fbf5a39b 1089 else
26bff3d9 1090 declare
191fcb3a 1091 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
26bff3d9 1092 New_Decl : Node_Id;
fbf5a39b 1093
26bff3d9
JM
1094 begin
1095 New_Decl :=
1096 Make_Full_Type_Declaration (Loc,
1097 Defining_Identifier => Def_Id,
1098 Type_Definition =>
1099 Make_Access_To_Object_Definition (Loc,
1100 All_Present => True,
1101 Null_Exclusion_Present => False,
0929eaeb
AC
1102 Constant_Present =>
1103 Is_Access_Constant (Etype (N)),
26bff3d9
JM
1104 Subtype_Indication =>
1105 New_Reference_To (Etype (Exp), Loc)));
1106
1107 Insert_Action (N, New_Decl);
1108
df3e68b1
HK
1109 -- Inherit the allocation-related attributes from the original
1110 -- access type.
26bff3d9 1111
d3f70b35 1112 Set_Finalization_Master (Def_Id, Finalization_Master (PtrT));
df3e68b1
HK
1113
1114 Set_Associated_Storage_Pool (Def_Id,
1115 Associated_Storage_Pool (PtrT));
758c442c 1116
26bff3d9
JM
1117 -- Declare the object using the previous type declaration
1118
1119 if Aggr_In_Place then
df3e68b1 1120 Temp_Decl :=
26bff3d9
JM
1121 Make_Object_Declaration (Loc,
1122 Defining_Identifier => Temp,
1123 Object_Definition => New_Reference_To (Def_Id, Loc),
1124 Expression =>
1125 Make_Allocator (Loc,
1126 New_Reference_To (Etype (Exp), Loc)));
1127
fad0600d
AC
1128 -- Copy the Comes_From_Source flag for the allocator we just
1129 -- built, since logically this allocator is a replacement of
1130 -- the original allocator node. This is for proper handling
1131 -- of restriction No_Implicit_Heap_Allocations.
1132
26bff3d9 1133 Set_Comes_From_Source
df3e68b1 1134 (Expression (Temp_Decl), Comes_From_Source (N));
26bff3d9 1135
df3e68b1
HK
1136 Set_No_Initialization (Expression (Temp_Decl));
1137 Insert_Action (N, Temp_Decl);
26bff3d9 1138
ca5af305 1139 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
df3e68b1 1140 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
26bff3d9 1141
26bff3d9
JM
1142 else
1143 Node := Relocate_Node (N);
1144 Set_Analyzed (Node);
df3e68b1
HK
1145
1146 Temp_Decl :=
26bff3d9
JM
1147 Make_Object_Declaration (Loc,
1148 Defining_Identifier => Temp,
1149 Constant_Present => True,
1150 Object_Definition => New_Reference_To (Def_Id, Loc),
df3e68b1
HK
1151 Expression => Node);
1152
1153 Insert_Action (N, Temp_Decl);
ca5af305 1154 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
26bff3d9
JM
1155 end if;
1156
1157 -- Generate an additional object containing the address of the
1158 -- returned object. The type of this second object declaration
685094bf
RD
1159 -- is the correct type required for the common processing that
1160 -- is still performed by this subprogram. The displacement of
1161 -- this pointer to reference the component associated with the
1162 -- interface type will be done at the end of common processing.
26bff3d9
JM
1163
1164 New_Decl :=
1165 Make_Object_Declaration (Loc,
243cae0a
AC
1166 Defining_Identifier => Make_Temporary (Loc, 'P'),
1167 Object_Definition => New_Reference_To (PtrT, Loc),
1168 Expression =>
df3e68b1
HK
1169 Unchecked_Convert_To (PtrT,
1170 New_Reference_To (Temp, Loc)));
26bff3d9
JM
1171
1172 Insert_Action (N, New_Decl);
1173
df3e68b1
HK
1174 Temp_Decl := New_Decl;
1175 Temp := Defining_Identifier (New_Decl);
26bff3d9 1176 end;
758c442c
GD
1177 end if;
1178
26bff3d9
JM
1179 Apply_Accessibility_Check (Temp);
1180
1181 -- Generate the tag assignment
1182
1183 -- Suppress the tag assignment when VM_Target because VM tags are
1184 -- represented implicitly in objects.
1185
1f110335 1186 if not Tagged_Type_Expansion then
26bff3d9 1187 null;
fbf5a39b 1188
26bff3d9
JM
1189 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1190 -- interface objects because in this case the tag does not change.
d26dc4b5 1191
26bff3d9
JM
1192 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1193 pragma Assert (Is_Class_Wide_Type
1194 (Directly_Designated_Type (Etype (N))));
d26dc4b5
AC
1195 null;
1196
1197 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1198 TagT := T;
1199 TagR := New_Reference_To (Temp, Loc);
1200
1201 elsif Is_Private_Type (T)
1202 and then Is_Tagged_Type (Underlying_Type (T))
fbf5a39b 1203 then
d26dc4b5 1204 TagT := Underlying_Type (T);
dfd99a80
TQ
1205 TagR :=
1206 Unchecked_Convert_To (Underlying_Type (T),
1207 Make_Explicit_Dereference (Loc,
1208 Prefix => New_Reference_To (Temp, Loc)));
d26dc4b5
AC
1209 end if;
1210
1211 if Present (TagT) then
38171f43
AC
1212 declare
1213 Full_T : constant Entity_Id := Underlying_Type (TagT);
38171f43
AC
1214 begin
1215 Tag_Assign :=
1216 Make_Assignment_Statement (Loc,
1217 Name =>
1218 Make_Selected_Component (Loc,
1219 Prefix => TagR,
1220 Selector_Name =>
1221 New_Reference_To (First_Tag_Component (Full_T), Loc)),
1222 Expression =>
1223 Unchecked_Convert_To (RTE (RE_Tag),
1224 New_Reference_To
1225 (Elists.Node
1226 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1227 end;
fbf5a39b
AC
1228
1229 -- The previous assignment has to be done in any case
1230
1231 Set_Assignment_OK (Name (Tag_Assign));
1232 Insert_Action (N, Tag_Assign);
fbf5a39b
AC
1233 end if;
1234
533369aa
AC
1235 if Needs_Finalization (DesigT) and then Needs_Finalization (T) then
1236
df3e68b1
HK
1237 -- Generate an Adjust call if the object will be moved. In Ada
1238 -- 2005, the object may be inherently limited, in which case
1239 -- there is no Adjust procedure, and the object is built in
1240 -- place. In Ada 95, the object can be limited but not
1241 -- inherently limited if this allocator came from a return
1242 -- statement (we're allocating the result on the secondary
1243 -- stack). In that case, the object will be moved, so we _do_
1244 -- want to Adjust.
1245
1246 if not Aggr_In_Place
1247 and then not Is_Immutably_Limited_Type (T)
1248 then
1249 Insert_Action (N,
fbf5a39b 1250
533369aa
AC
1251 -- An unchecked conversion is needed in the classwide case
1252 -- because the designated type can be an ancestor of the
1253 -- subtype mark of the allocator.
fbf5a39b 1254
533369aa
AC
1255 Make_Adjust_Call
1256 (Obj_Ref =>
1257 Unchecked_Convert_To (T,
1258 Make_Explicit_Dereference (Loc,
1259 Prefix => New_Reference_To (Temp, Loc))),
1260 Typ => T));
df3e68b1 1261 end if;
b254da66
AC
1262
1263 -- Generate:
1264 -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access);
1265
2bfa5484 1266 -- Do not generate this call in the following cases:
c5f5123f 1267
2bfa5484
HK
1268 -- * .NET/JVM - these targets do not support address arithmetic
1269 -- and unchecked conversion, key elements of Finalize_Address.
c5f5123f 1270
06b599fd 1271 -- * SPARK mode - the call is useless and results in unwanted
2bfa5484 1272 -- expansion.
c5f5123f 1273
2bfa5484
HK
1274 -- * CodePeer mode - TSS primitive Finalize_Address is not
1275 -- created in this mode.
b254da66
AC
1276
1277 if VM_Target = No_VM
06b599fd 1278 and then not SPARK_Mode
b254da66
AC
1279 and then not CodePeer_Mode
1280 and then Present (Finalization_Master (PtrT))
f7bb41af
AC
1281 and then Present (Temp_Decl)
1282 and then Nkind (Expression (Temp_Decl)) = N_Allocator
b254da66
AC
1283 then
1284 Insert_Action (N,
1285 Make_Set_Finalize_Address_Call
1286 (Loc => Loc,
1287 Typ => T,
1288 Ptr_Typ => PtrT));
1289 end if;
fbf5a39b
AC
1290 end if;
1291
1292 Rewrite (N, New_Reference_To (Temp, Loc));
1293 Analyze_And_Resolve (N, PtrT);
1294
685094bf
RD
1295 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1296 -- component containing the secondary dispatch table of the interface
1297 -- type.
26bff3d9
JM
1298
1299 if Is_Interface (Directly_Designated_Type (PtrT)) then
1300 Displace_Allocator_Pointer (N);
1301 end if;
1302
fbf5a39b 1303 elsif Aggr_In_Place then
e86a3a7e 1304 Temp := Make_Temporary (Loc, 'P', N);
df3e68b1 1305 Temp_Decl :=
fbf5a39b
AC
1306 Make_Object_Declaration (Loc,
1307 Defining_Identifier => Temp,
1308 Object_Definition => New_Reference_To (PtrT, Loc),
df3e68b1
HK
1309 Expression =>
1310 Make_Allocator (Loc,
243cae0a 1311 Expression => New_Reference_To (Etype (Exp), Loc)));
fbf5a39b 1312
fad0600d
AC
1313 -- Copy the Comes_From_Source flag for the allocator we just built,
1314 -- since logically this allocator is a replacement of the original
1315 -- allocator node. This is for proper handling of restriction
1316 -- No_Implicit_Heap_Allocations.
1317
fbf5a39b 1318 Set_Comes_From_Source
df3e68b1
HK
1319 (Expression (Temp_Decl), Comes_From_Source (N));
1320
1321 Set_No_Initialization (Expression (Temp_Decl));
1322 Insert_Action (N, Temp_Decl);
1323
ca5af305 1324 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
df3e68b1 1325 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
fbf5a39b 1326
d3f70b35
AC
1327 -- Attach the object to the associated finalization master. Thisis
1328 -- done manually on .NET/JVM since those compilers do no support
deb8dacc
HK
1329 -- pools and cannot benefit from internally generated Allocate and
1330 -- Deallocate procedures.
1331
1332 if VM_Target /= No_VM
1333 and then Is_Controlled (DesigT)
d3f70b35 1334 and then Present (Finalization_Master (PtrT))
deb8dacc
HK
1335 then
1336 Insert_Action (N,
243cae0a
AC
1337 Make_Attach_Call
1338 (Obj_Ref => New_Reference_To (Temp, Loc),
1339 Ptr_Typ => PtrT));
deb8dacc
HK
1340 end if;
1341
fbf5a39b
AC
1342 Rewrite (N, New_Reference_To (Temp, Loc));
1343 Analyze_And_Resolve (N, PtrT);
1344
533369aa 1345 elsif Is_Access_Type (T) and then Can_Never_Be_Null (T) then
51e4c4b9
AC
1346 Install_Null_Excluding_Check (Exp);
1347
f02b8bb8 1348 elsif Is_Access_Type (DesigT)
fbf5a39b
AC
1349 and then Nkind (Exp) = N_Allocator
1350 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1351 then
0da2c8ac 1352 -- Apply constraint to designated subtype indication
fbf5a39b
AC
1353
1354 Apply_Constraint_Check (Expression (Exp),
f02b8bb8 1355 Designated_Type (DesigT),
fbf5a39b
AC
1356 No_Sliding => True);
1357
1358 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1359
1360 -- Propagate constraint_error to enclosing allocator
1361
1362 Rewrite (Exp, New_Copy (Expression (Exp)));
1363 end if;
1df4f514 1364
fbf5a39b 1365 else
14f0f659
AC
1366 Build_Allocate_Deallocate_Proc (N, True);
1367
36c73552
AC
1368 -- If we have:
1369 -- type A is access T1;
1370 -- X : A := new T2'(...);
1371 -- T1 and T2 can be different subtypes, and we might need to check
1372 -- both constraints. First check against the type of the qualified
1373 -- expression.
1374
1375 Apply_Constraint_Check (Exp, T, No_Sliding => True);
fbf5a39b 1376
d79e621a
GD
1377 if Do_Range_Check (Exp) then
1378 Set_Do_Range_Check (Exp, False);
1379 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1380 end if;
1381
685094bf
RD
1382 -- A check is also needed in cases where the designated subtype is
1383 -- constrained and differs from the subtype given in the qualified
1384 -- expression. Note that the check on the qualified expression does
1385 -- not allow sliding, but this check does (a relaxation from Ada 83).
fbf5a39b 1386
f02b8bb8 1387 if Is_Constrained (DesigT)
9450205a 1388 and then not Subtypes_Statically_Match (T, DesigT)
fbf5a39b
AC
1389 then
1390 Apply_Constraint_Check
f02b8bb8 1391 (Exp, DesigT, No_Sliding => False);
d79e621a
GD
1392
1393 if Do_Range_Check (Exp) then
1394 Set_Do_Range_Check (Exp, False);
1395 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1396 end if;
f02b8bb8
RD
1397 end if;
1398
685094bf
RD
1399 -- For an access to unconstrained packed array, GIGI needs to see an
1400 -- expression with a constrained subtype in order to compute the
1401 -- proper size for the allocator.
f02b8bb8
RD
1402
1403 if Is_Array_Type (T)
1404 and then not Is_Constrained (T)
1405 and then Is_Packed (T)
1406 then
1407 declare
191fcb3a 1408 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
f02b8bb8
RD
1409 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1410 begin
1411 Insert_Action (Exp,
1412 Make_Subtype_Declaration (Loc,
1413 Defining_Identifier => ConstrT,
25ebc085
AC
1414 Subtype_Indication =>
1415 Make_Subtype_From_Expr (Internal_Exp, T)));
f02b8bb8
RD
1416 Freeze_Itype (ConstrT, Exp);
1417 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1418 end;
fbf5a39b 1419 end if;
f02b8bb8 1420
685094bf
RD
1421 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1422 -- to a build-in-place function, then access to the allocated object
1423 -- must be passed to the function. Currently we limit such functions
1424 -- to those with constrained limited result subtypes, but eventually
1425 -- we plan to expand the allowed forms of functions that are treated
1426 -- as build-in-place.
20b5d666 1427
0791fbe9 1428 if Ada_Version >= Ada_2005
20b5d666
JM
1429 and then Is_Build_In_Place_Function_Call (Exp)
1430 then
1431 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1432 end if;
fbf5a39b
AC
1433 end if;
1434
1435 exception
1436 when RE_Not_Available =>
1437 return;
1438 end Expand_Allocator_Expression;
1439
70482933
RK
1440 -----------------------------
1441 -- Expand_Array_Comparison --
1442 -----------------------------
1443
685094bf
RD
1444 -- Expansion is only required in the case of array types. For the unpacked
1445 -- case, an appropriate runtime routine is called. For packed cases, and
1446 -- also in some other cases where a runtime routine cannot be called, the
1447 -- form of the expansion is:
70482933
RK
1448
1449 -- [body for greater_nn; boolean_expression]
1450
1451 -- The body is built by Make_Array_Comparison_Op, and the form of the
1452 -- Boolean expression depends on the operator involved.
1453
1454 procedure Expand_Array_Comparison (N : Node_Id) is
1455 Loc : constant Source_Ptr := Sloc (N);
1456 Op1 : Node_Id := Left_Opnd (N);
1457 Op2 : Node_Id := Right_Opnd (N);
1458 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
fbf5a39b 1459 Ctyp : constant Entity_Id := Component_Type (Typ1);
70482933
RK
1460
1461 Expr : Node_Id;
1462 Func_Body : Node_Id;
1463 Func_Name : Entity_Id;
1464
fbf5a39b
AC
1465 Comp : RE_Id;
1466
9bc43c53
AC
1467 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1468 -- True for byte addressable target
91b1417d 1469
fbf5a39b 1470 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
685094bf
RD
1471 -- Returns True if the length of the given operand is known to be less
1472 -- than 4. Returns False if this length is known to be four or greater
1473 -- or is not known at compile time.
fbf5a39b
AC
1474
1475 ------------------------
1476 -- Length_Less_Than_4 --
1477 ------------------------
1478
1479 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1480 Otyp : constant Entity_Id := Etype (Opnd);
1481
1482 begin
1483 if Ekind (Otyp) = E_String_Literal_Subtype then
1484 return String_Literal_Length (Otyp) < 4;
1485
1486 else
1487 declare
1488 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1489 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1490 Hi : constant Node_Id := Type_High_Bound (Ityp);
1491 Lov : Uint;
1492 Hiv : Uint;
1493
1494 begin
1495 if Compile_Time_Known_Value (Lo) then
1496 Lov := Expr_Value (Lo);
1497 else
1498 return False;
1499 end if;
1500
1501 if Compile_Time_Known_Value (Hi) then
1502 Hiv := Expr_Value (Hi);
1503 else
1504 return False;
1505 end if;
1506
1507 return Hiv < Lov + 3;
1508 end;
1509 end if;
1510 end Length_Less_Than_4;
1511
1512 -- Start of processing for Expand_Array_Comparison
1513
70482933 1514 begin
fbf5a39b
AC
1515 -- Deal first with unpacked case, where we can call a runtime routine
1516 -- except that we avoid this for targets for which are not addressable
26bff3d9 1517 -- by bytes, and for the JVM/CIL, since they do not support direct
fbf5a39b
AC
1518 -- addressing of array components.
1519
1520 if not Is_Bit_Packed_Array (Typ1)
9bc43c53 1521 and then Byte_Addressable
26bff3d9 1522 and then VM_Target = No_VM
fbf5a39b
AC
1523 then
1524 -- The call we generate is:
1525
1526 -- Compare_Array_xn[_Unaligned]
1527 -- (left'address, right'address, left'length, right'length) <op> 0
1528
1529 -- x = U for unsigned, S for signed
1530 -- n = 8,16,32,64 for component size
1531 -- Add _Unaligned if length < 4 and component size is 8.
1532 -- <op> is the standard comparison operator
1533
1534 if Component_Size (Typ1) = 8 then
1535 if Length_Less_Than_4 (Op1)
1536 or else
1537 Length_Less_Than_4 (Op2)
1538 then
1539 if Is_Unsigned_Type (Ctyp) then
1540 Comp := RE_Compare_Array_U8_Unaligned;
1541 else
1542 Comp := RE_Compare_Array_S8_Unaligned;
1543 end if;
1544
1545 else
1546 if Is_Unsigned_Type (Ctyp) then
1547 Comp := RE_Compare_Array_U8;
1548 else
1549 Comp := RE_Compare_Array_S8;
1550 end if;
1551 end if;
1552
1553 elsif Component_Size (Typ1) = 16 then
1554 if Is_Unsigned_Type (Ctyp) then
1555 Comp := RE_Compare_Array_U16;
1556 else
1557 Comp := RE_Compare_Array_S16;
1558 end if;
1559
1560 elsif Component_Size (Typ1) = 32 then
1561 if Is_Unsigned_Type (Ctyp) then
1562 Comp := RE_Compare_Array_U32;
1563 else
1564 Comp := RE_Compare_Array_S32;
1565 end if;
1566
1567 else pragma Assert (Component_Size (Typ1) = 64);
1568 if Is_Unsigned_Type (Ctyp) then
1569 Comp := RE_Compare_Array_U64;
1570 else
1571 Comp := RE_Compare_Array_S64;
1572 end if;
1573 end if;
1574
1575 Remove_Side_Effects (Op1, Name_Req => True);
1576 Remove_Side_Effects (Op2, Name_Req => True);
1577
1578 Rewrite (Op1,
1579 Make_Function_Call (Sloc (Op1),
1580 Name => New_Occurrence_Of (RTE (Comp), Loc),
1581
1582 Parameter_Associations => New_List (
1583 Make_Attribute_Reference (Loc,
1584 Prefix => Relocate_Node (Op1),
1585 Attribute_Name => Name_Address),
1586
1587 Make_Attribute_Reference (Loc,
1588 Prefix => Relocate_Node (Op2),
1589 Attribute_Name => Name_Address),
1590
1591 Make_Attribute_Reference (Loc,
1592 Prefix => Relocate_Node (Op1),
1593 Attribute_Name => Name_Length),
1594
1595 Make_Attribute_Reference (Loc,
1596 Prefix => Relocate_Node (Op2),
1597 Attribute_Name => Name_Length))));
1598
1599 Rewrite (Op2,
1600 Make_Integer_Literal (Sloc (Op2),
1601 Intval => Uint_0));
1602
1603 Analyze_And_Resolve (Op1, Standard_Integer);
1604 Analyze_And_Resolve (Op2, Standard_Integer);
1605 return;
1606 end if;
1607
1608 -- Cases where we cannot make runtime call
1609
70482933
RK
1610 -- For (a <= b) we convert to not (a > b)
1611
1612 if Chars (N) = Name_Op_Le then
1613 Rewrite (N,
1614 Make_Op_Not (Loc,
1615 Right_Opnd =>
1616 Make_Op_Gt (Loc,
1617 Left_Opnd => Op1,
1618 Right_Opnd => Op2)));
1619 Analyze_And_Resolve (N, Standard_Boolean);
1620 return;
1621
1622 -- For < the Boolean expression is
1623 -- greater__nn (op2, op1)
1624
1625 elsif Chars (N) = Name_Op_Lt then
1626 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1627
1628 -- Switch operands
1629
1630 Op1 := Right_Opnd (N);
1631 Op2 := Left_Opnd (N);
1632
1633 -- For (a >= b) we convert to not (a < b)
1634
1635 elsif Chars (N) = Name_Op_Ge then
1636 Rewrite (N,
1637 Make_Op_Not (Loc,
1638 Right_Opnd =>
1639 Make_Op_Lt (Loc,
1640 Left_Opnd => Op1,
1641 Right_Opnd => Op2)));
1642 Analyze_And_Resolve (N, Standard_Boolean);
1643 return;
1644
1645 -- For > the Boolean expression is
1646 -- greater__nn (op1, op2)
1647
1648 else
1649 pragma Assert (Chars (N) = Name_Op_Gt);
1650 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1651 end if;
1652
1653 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1654 Expr :=
1655 Make_Function_Call (Loc,
1656 Name => New_Reference_To (Func_Name, Loc),
1657 Parameter_Associations => New_List (Op1, Op2));
1658
1659 Insert_Action (N, Func_Body);
1660 Rewrite (N, Expr);
1661 Analyze_And_Resolve (N, Standard_Boolean);
1662
fbf5a39b
AC
1663 exception
1664 when RE_Not_Available =>
1665 return;
70482933
RK
1666 end Expand_Array_Comparison;
1667
1668 ---------------------------
1669 -- Expand_Array_Equality --
1670 ---------------------------
1671
685094bf
RD
1672 -- Expand an equality function for multi-dimensional arrays. Here is an
1673 -- example of such a function for Nb_Dimension = 2
70482933 1674
0da2c8ac 1675 -- function Enn (A : atyp; B : btyp) return boolean is
70482933 1676 -- begin
fbf5a39b
AC
1677 -- if (A'length (1) = 0 or else A'length (2) = 0)
1678 -- and then
1679 -- (B'length (1) = 0 or else B'length (2) = 0)
1680 -- then
1681 -- return True; -- RM 4.5.2(22)
1682 -- end if;
0da2c8ac 1683
fbf5a39b
AC
1684 -- if A'length (1) /= B'length (1)
1685 -- or else
1686 -- A'length (2) /= B'length (2)
1687 -- then
1688 -- return False; -- RM 4.5.2(23)
1689 -- end if;
0da2c8ac 1690
fbf5a39b 1691 -- declare
523456db
AC
1692 -- A1 : Index_T1 := A'first (1);
1693 -- B1 : Index_T1 := B'first (1);
fbf5a39b 1694 -- begin
523456db 1695 -- loop
fbf5a39b 1696 -- declare
523456db
AC
1697 -- A2 : Index_T2 := A'first (2);
1698 -- B2 : Index_T2 := B'first (2);
fbf5a39b 1699 -- begin
523456db 1700 -- loop
fbf5a39b
AC
1701 -- if A (A1, A2) /= B (B1, B2) then
1702 -- return False;
70482933 1703 -- end if;
0da2c8ac 1704
523456db
AC
1705 -- exit when A2 = A'last (2);
1706 -- A2 := Index_T2'succ (A2);
0da2c8ac 1707 -- B2 := Index_T2'succ (B2);
70482933 1708 -- end loop;
fbf5a39b 1709 -- end;
0da2c8ac 1710
523456db
AC
1711 -- exit when A1 = A'last (1);
1712 -- A1 := Index_T1'succ (A1);
0da2c8ac 1713 -- B1 := Index_T1'succ (B1);
70482933 1714 -- end loop;
fbf5a39b 1715 -- end;
0da2c8ac 1716
70482933
RK
1717 -- return true;
1718 -- end Enn;
1719
685094bf
RD
1720 -- Note on the formal types used (atyp and btyp). If either of the arrays
1721 -- is of a private type, we use the underlying type, and do an unchecked
1722 -- conversion of the actual. If either of the arrays has a bound depending
1723 -- on a discriminant, then we use the base type since otherwise we have an
1724 -- escaped discriminant in the function.
0da2c8ac 1725
685094bf
RD
1726 -- If both arrays are constrained and have the same bounds, we can generate
1727 -- a loop with an explicit iteration scheme using a 'Range attribute over
1728 -- the first array.
523456db 1729
70482933
RK
1730 function Expand_Array_Equality
1731 (Nod : Node_Id;
70482933
RK
1732 Lhs : Node_Id;
1733 Rhs : Node_Id;
0da2c8ac
AC
1734 Bodies : List_Id;
1735 Typ : Entity_Id) return Node_Id
70482933
RK
1736 is
1737 Loc : constant Source_Ptr := Sloc (Nod);
fbf5a39b
AC
1738 Decls : constant List_Id := New_List;
1739 Index_List1 : constant List_Id := New_List;
1740 Index_List2 : constant List_Id := New_List;
1741
1742 Actuals : List_Id;
1743 Formals : List_Id;
1744 Func_Name : Entity_Id;
1745 Func_Body : Node_Id;
70482933
RK
1746
1747 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1748 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1749
0da2c8ac
AC
1750 Ltyp : Entity_Id;
1751 Rtyp : Entity_Id;
1752 -- The parameter types to be used for the formals
1753
fbf5a39b
AC
1754 function Arr_Attr
1755 (Arr : Entity_Id;
1756 Nam : Name_Id;
2e071734 1757 Num : Int) return Node_Id;
5e1c00fa 1758 -- This builds the attribute reference Arr'Nam (Expr)
fbf5a39b 1759
70482933 1760 function Component_Equality (Typ : Entity_Id) return Node_Id;
685094bf 1761 -- Create one statement to compare corresponding components, designated
3b42c566 1762 -- by a full set of indexes.
70482933 1763
0da2c8ac 1764 function Get_Arg_Type (N : Node_Id) return Entity_Id;
685094bf
RD
1765 -- Given one of the arguments, computes the appropriate type to be used
1766 -- for that argument in the corresponding function formal
0da2c8ac 1767
fbf5a39b 1768 function Handle_One_Dimension
70482933 1769 (N : Int;
2e071734 1770 Index : Node_Id) return Node_Id;
0da2c8ac 1771 -- This procedure returns the following code
fbf5a39b
AC
1772 --
1773 -- declare
523456db 1774 -- Bn : Index_T := B'First (N);
fbf5a39b 1775 -- begin
523456db 1776 -- loop
fbf5a39b 1777 -- xxx
523456db
AC
1778 -- exit when An = A'Last (N);
1779 -- An := Index_T'Succ (An)
0da2c8ac 1780 -- Bn := Index_T'Succ (Bn)
fbf5a39b
AC
1781 -- end loop;
1782 -- end;
1783 --
3b42c566 1784 -- If both indexes are constrained and identical, the procedure
523456db
AC
1785 -- returns a simpler loop:
1786 --
1787 -- for An in A'Range (N) loop
1788 -- xxx
1789 -- end loop
0da2c8ac 1790 --
523456db 1791 -- N is the dimension for which we are generating a loop. Index is the
685094bf
RD
1792 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1793 -- xxx statement is either the loop or declare for the next dimension
1794 -- or if this is the last dimension the comparison of corresponding
1795 -- components of the arrays.
fbf5a39b 1796 --
685094bf
RD
1797 -- The actual way the code works is to return the comparison of
1798 -- corresponding components for the N+1 call. That's neater!
fbf5a39b
AC
1799
1800 function Test_Empty_Arrays return Node_Id;
1801 -- This function constructs the test for both arrays being empty
1802 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1803 -- and then
1804 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1805
1806 function Test_Lengths_Correspond return Node_Id;
685094bf
RD
1807 -- This function constructs the test for arrays having different lengths
1808 -- in at least one index position, in which case the resulting code is:
fbf5a39b
AC
1809
1810 -- A'length (1) /= B'length (1)
1811 -- or else
1812 -- A'length (2) /= B'length (2)
1813 -- or else
1814 -- ...
1815
1816 --------------
1817 -- Arr_Attr --
1818 --------------
1819
1820 function Arr_Attr
1821 (Arr : Entity_Id;
1822 Nam : Name_Id;
2e071734 1823 Num : Int) return Node_Id
fbf5a39b
AC
1824 is
1825 begin
1826 return
1827 Make_Attribute_Reference (Loc,
1828 Attribute_Name => Nam,
1829 Prefix => New_Reference_To (Arr, Loc),
1830 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1831 end Arr_Attr;
70482933
RK
1832
1833 ------------------------
1834 -- Component_Equality --
1835 ------------------------
1836
1837 function Component_Equality (Typ : Entity_Id) return Node_Id is
1838 Test : Node_Id;
1839 L, R : Node_Id;
1840
1841 begin
1842 -- if a(i1...) /= b(j1...) then return false; end if;
1843
1844 L :=
1845 Make_Indexed_Component (Loc,
7675ad4f 1846 Prefix => Make_Identifier (Loc, Chars (A)),
70482933
RK
1847 Expressions => Index_List1);
1848
1849 R :=
1850 Make_Indexed_Component (Loc,
7675ad4f 1851 Prefix => Make_Identifier (Loc, Chars (B)),
70482933
RK
1852 Expressions => Index_List2);
1853
1854 Test := Expand_Composite_Equality
1855 (Nod, Component_Type (Typ), L, R, Decls);
1856
a9d8907c
JM
1857 -- If some (sub)component is an unchecked_union, the whole operation
1858 -- will raise program error.
8aceda64
AC
1859
1860 if Nkind (Test) = N_Raise_Program_Error then
a9d8907c
JM
1861
1862 -- This node is going to be inserted at a location where a
685094bf
RD
1863 -- statement is expected: clear its Etype so analysis will set
1864 -- it to the expected Standard_Void_Type.
a9d8907c
JM
1865
1866 Set_Etype (Test, Empty);
8aceda64
AC
1867 return Test;
1868
1869 else
1870 return
1871 Make_Implicit_If_Statement (Nod,
1872 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1873 Then_Statements => New_List (
d766cee3 1874 Make_Simple_Return_Statement (Loc,
8aceda64
AC
1875 Expression => New_Occurrence_Of (Standard_False, Loc))));
1876 end if;
70482933
RK
1877 end Component_Equality;
1878
0da2c8ac
AC
1879 ------------------
1880 -- Get_Arg_Type --
1881 ------------------
1882
1883 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1884 T : Entity_Id;
1885 X : Node_Id;
1886
1887 begin
1888 T := Etype (N);
1889
1890 if No (T) then
1891 return Typ;
1892
1893 else
1894 T := Underlying_Type (T);
1895
1896 X := First_Index (T);
1897 while Present (X) loop
761f7dcb
AC
1898 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1899 or else
1900 Denotes_Discriminant (Type_High_Bound (Etype (X)))
0da2c8ac
AC
1901 then
1902 T := Base_Type (T);
1903 exit;
1904 end if;
1905
1906 Next_Index (X);
1907 end loop;
1908
1909 return T;
1910 end if;
1911 end Get_Arg_Type;
1912
fbf5a39b
AC
1913 --------------------------
1914 -- Handle_One_Dimension --
1915 ---------------------------
70482933 1916
fbf5a39b 1917 function Handle_One_Dimension
70482933 1918 (N : Int;
2e071734 1919 Index : Node_Id) return Node_Id
70482933 1920 is
0da2c8ac 1921 Need_Separate_Indexes : constant Boolean :=
761f7dcb 1922 Ltyp /= Rtyp or else not Is_Constrained (Ltyp);
0da2c8ac 1923 -- If the index types are identical, and we are working with
685094bf
RD
1924 -- constrained types, then we can use the same index for both
1925 -- of the arrays.
0da2c8ac 1926
191fcb3a 1927 An : constant Entity_Id := Make_Temporary (Loc, 'A');
0da2c8ac
AC
1928
1929 Bn : Entity_Id;
1930 Index_T : Entity_Id;
1931 Stm_List : List_Id;
1932 Loop_Stm : Node_Id;
70482933
RK
1933
1934 begin
0da2c8ac
AC
1935 if N > Number_Dimensions (Ltyp) then
1936 return Component_Equality (Ltyp);
fbf5a39b 1937 end if;
70482933 1938
0da2c8ac
AC
1939 -- Case where we generate a loop
1940
1941 Index_T := Base_Type (Etype (Index));
1942
1943 if Need_Separate_Indexes then
191fcb3a 1944 Bn := Make_Temporary (Loc, 'B');
0da2c8ac
AC
1945 else
1946 Bn := An;
1947 end if;
70482933 1948
fbf5a39b
AC
1949 Append (New_Reference_To (An, Loc), Index_List1);
1950 Append (New_Reference_To (Bn, Loc), Index_List2);
70482933 1951
0da2c8ac
AC
1952 Stm_List := New_List (
1953 Handle_One_Dimension (N + 1, Next_Index (Index)));
70482933 1954
0da2c8ac 1955 if Need_Separate_Indexes then
a9d8907c 1956
3b42c566 1957 -- Generate guard for loop, followed by increments of indexes
523456db
AC
1958
1959 Append_To (Stm_List,
1960 Make_Exit_Statement (Loc,
1961 Condition =>
1962 Make_Op_Eq (Loc,
1963 Left_Opnd => New_Reference_To (An, Loc),
1964 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1965
1966 Append_To (Stm_List,
1967 Make_Assignment_Statement (Loc,
1968 Name => New_Reference_To (An, Loc),
1969 Expression =>
1970 Make_Attribute_Reference (Loc,
1971 Prefix => New_Reference_To (Index_T, Loc),
1972 Attribute_Name => Name_Succ,
1973 Expressions => New_List (New_Reference_To (An, Loc)))));
1974
0da2c8ac
AC
1975 Append_To (Stm_List,
1976 Make_Assignment_Statement (Loc,
1977 Name => New_Reference_To (Bn, Loc),
1978 Expression =>
1979 Make_Attribute_Reference (Loc,
1980 Prefix => New_Reference_To (Index_T, Loc),
1981 Attribute_Name => Name_Succ,
1982 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1983 end if;
1984
a9d8907c
JM
1985 -- If separate indexes, we need a declare block for An and Bn, and a
1986 -- loop without an iteration scheme.
0da2c8ac
AC
1987
1988 if Need_Separate_Indexes then
523456db
AC
1989 Loop_Stm :=
1990 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1991
0da2c8ac
AC
1992 return
1993 Make_Block_Statement (Loc,
1994 Declarations => New_List (
523456db
AC
1995 Make_Object_Declaration (Loc,
1996 Defining_Identifier => An,
1997 Object_Definition => New_Reference_To (Index_T, Loc),
1998 Expression => Arr_Attr (A, Name_First, N)),
1999
0da2c8ac
AC
2000 Make_Object_Declaration (Loc,
2001 Defining_Identifier => Bn,
2002 Object_Definition => New_Reference_To (Index_T, Loc),
2003 Expression => Arr_Attr (B, Name_First, N))),
523456db 2004
0da2c8ac
AC
2005 Handled_Statement_Sequence =>
2006 Make_Handled_Sequence_Of_Statements (Loc,
2007 Statements => New_List (Loop_Stm)));
2008
523456db
AC
2009 -- If no separate indexes, return loop statement with explicit
2010 -- iteration scheme on its own
0da2c8ac
AC
2011
2012 else
523456db
AC
2013 Loop_Stm :=
2014 Make_Implicit_Loop_Statement (Nod,
2015 Statements => Stm_List,
2016 Iteration_Scheme =>
2017 Make_Iteration_Scheme (Loc,
2018 Loop_Parameter_Specification =>
2019 Make_Loop_Parameter_Specification (Loc,
2020 Defining_Identifier => An,
2021 Discrete_Subtype_Definition =>
2022 Arr_Attr (A, Name_Range, N))));
0da2c8ac
AC
2023 return Loop_Stm;
2024 end if;
fbf5a39b
AC
2025 end Handle_One_Dimension;
2026
2027 -----------------------
2028 -- Test_Empty_Arrays --
2029 -----------------------
2030
2031 function Test_Empty_Arrays return Node_Id is
2032 Alist : Node_Id;
2033 Blist : Node_Id;
2034
2035 Atest : Node_Id;
2036 Btest : Node_Id;
70482933 2037
fbf5a39b
AC
2038 begin
2039 Alist := Empty;
2040 Blist := Empty;
0da2c8ac 2041 for J in 1 .. Number_Dimensions (Ltyp) loop
fbf5a39b
AC
2042 Atest :=
2043 Make_Op_Eq (Loc,
2044 Left_Opnd => Arr_Attr (A, Name_Length, J),
2045 Right_Opnd => Make_Integer_Literal (Loc, 0));
2046
2047 Btest :=
2048 Make_Op_Eq (Loc,
2049 Left_Opnd => Arr_Attr (B, Name_Length, J),
2050 Right_Opnd => Make_Integer_Literal (Loc, 0));
2051
2052 if No (Alist) then
2053 Alist := Atest;
2054 Blist := Btest;
70482933 2055
fbf5a39b
AC
2056 else
2057 Alist :=
2058 Make_Or_Else (Loc,
2059 Left_Opnd => Relocate_Node (Alist),
2060 Right_Opnd => Atest);
2061
2062 Blist :=
2063 Make_Or_Else (Loc,
2064 Left_Opnd => Relocate_Node (Blist),
2065 Right_Opnd => Btest);
2066 end if;
2067 end loop;
70482933 2068
fbf5a39b
AC
2069 return
2070 Make_And_Then (Loc,
2071 Left_Opnd => Alist,
2072 Right_Opnd => Blist);
2073 end Test_Empty_Arrays;
70482933 2074
fbf5a39b
AC
2075 -----------------------------
2076 -- Test_Lengths_Correspond --
2077 -----------------------------
70482933 2078
fbf5a39b
AC
2079 function Test_Lengths_Correspond return Node_Id is
2080 Result : Node_Id;
2081 Rtest : Node_Id;
2082
2083 begin
2084 Result := Empty;
0da2c8ac 2085 for J in 1 .. Number_Dimensions (Ltyp) loop
fbf5a39b
AC
2086 Rtest :=
2087 Make_Op_Ne (Loc,
2088 Left_Opnd => Arr_Attr (A, Name_Length, J),
2089 Right_Opnd => Arr_Attr (B, Name_Length, J));
2090
2091 if No (Result) then
2092 Result := Rtest;
2093 else
2094 Result :=
2095 Make_Or_Else (Loc,
2096 Left_Opnd => Relocate_Node (Result),
2097 Right_Opnd => Rtest);
2098 end if;
2099 end loop;
2100
2101 return Result;
2102 end Test_Lengths_Correspond;
70482933
RK
2103
2104 -- Start of processing for Expand_Array_Equality
2105
2106 begin
0da2c8ac
AC
2107 Ltyp := Get_Arg_Type (Lhs);
2108 Rtyp := Get_Arg_Type (Rhs);
2109
685094bf
RD
2110 -- For now, if the argument types are not the same, go to the base type,
2111 -- since the code assumes that the formals have the same type. This is
2112 -- fixable in future ???
0da2c8ac
AC
2113
2114 if Ltyp /= Rtyp then
2115 Ltyp := Base_Type (Ltyp);
2116 Rtyp := Base_Type (Rtyp);
2117 pragma Assert (Ltyp = Rtyp);
2118 end if;
2119
2120 -- Build list of formals for function
2121
70482933
RK
2122 Formals := New_List (
2123 Make_Parameter_Specification (Loc,
2124 Defining_Identifier => A,
0da2c8ac 2125 Parameter_Type => New_Reference_To (Ltyp, Loc)),
70482933
RK
2126
2127 Make_Parameter_Specification (Loc,
2128 Defining_Identifier => B,
0da2c8ac 2129 Parameter_Type => New_Reference_To (Rtyp, Loc)));
70482933 2130
191fcb3a 2131 Func_Name := Make_Temporary (Loc, 'E');
70482933 2132
fbf5a39b 2133 -- Build statement sequence for function
70482933
RK
2134
2135 Func_Body :=
2136 Make_Subprogram_Body (Loc,
2137 Specification =>
2138 Make_Function_Specification (Loc,
2139 Defining_Unit_Name => Func_Name,
2140 Parameter_Specifications => Formals,
630d30e9 2141 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
fbf5a39b
AC
2142
2143 Declarations => Decls,
2144
70482933
RK
2145 Handled_Statement_Sequence =>
2146 Make_Handled_Sequence_Of_Statements (Loc,
2147 Statements => New_List (
fbf5a39b
AC
2148
2149 Make_Implicit_If_Statement (Nod,
2150 Condition => Test_Empty_Arrays,
2151 Then_Statements => New_List (
d766cee3 2152 Make_Simple_Return_Statement (Loc,
fbf5a39b
AC
2153 Expression =>
2154 New_Occurrence_Of (Standard_True, Loc)))),
2155
2156 Make_Implicit_If_Statement (Nod,
2157 Condition => Test_Lengths_Correspond,
2158 Then_Statements => New_List (
d766cee3 2159 Make_Simple_Return_Statement (Loc,
fbf5a39b
AC
2160 Expression =>
2161 New_Occurrence_Of (Standard_False, Loc)))),
2162
0da2c8ac 2163 Handle_One_Dimension (1, First_Index (Ltyp)),
fbf5a39b 2164
d766cee3 2165 Make_Simple_Return_Statement (Loc,
70482933
RK
2166 Expression => New_Occurrence_Of (Standard_True, Loc)))));
2167
2168 Set_Has_Completion (Func_Name, True);
0da2c8ac 2169 Set_Is_Inlined (Func_Name);
70482933 2170
685094bf
RD
2171 -- If the array type is distinct from the type of the arguments, it
2172 -- is the full view of a private type. Apply an unchecked conversion
2173 -- to insure that analysis of the call succeeds.
70482933 2174
0da2c8ac
AC
2175 declare
2176 L, R : Node_Id;
2177
2178 begin
2179 L := Lhs;
2180 R := Rhs;
2181
2182 if No (Etype (Lhs))
2183 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
2184 then
2185 L := OK_Convert_To (Ltyp, Lhs);
2186 end if;
2187
2188 if No (Etype (Rhs))
2189 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
2190 then
2191 R := OK_Convert_To (Rtyp, Rhs);
2192 end if;
2193
2194 Actuals := New_List (L, R);
2195 end;
70482933
RK
2196
2197 Append_To (Bodies, Func_Body);
2198
2199 return
2200 Make_Function_Call (Loc,
0da2c8ac 2201 Name => New_Reference_To (Func_Name, Loc),
70482933
RK
2202 Parameter_Associations => Actuals);
2203 end Expand_Array_Equality;
2204
2205 -----------------------------
2206 -- Expand_Boolean_Operator --
2207 -----------------------------
2208
685094bf
RD
2209 -- Note that we first get the actual subtypes of the operands, since we
2210 -- always want to deal with types that have bounds.
70482933
RK
2211
2212 procedure Expand_Boolean_Operator (N : Node_Id) is
fbf5a39b 2213 Typ : constant Entity_Id := Etype (N);
70482933
RK
2214
2215 begin
685094bf
RD
2216 -- Special case of bit packed array where both operands are known to be
2217 -- properly aligned. In this case we use an efficient run time routine
2218 -- to carry out the operation (see System.Bit_Ops).
a9d8907c
JM
2219
2220 if Is_Bit_Packed_Array (Typ)
2221 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2222 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2223 then
70482933 2224 Expand_Packed_Boolean_Operator (N);
a9d8907c
JM
2225 return;
2226 end if;
70482933 2227
a9d8907c
JM
2228 -- For the normal non-packed case, the general expansion is to build
2229 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2230 -- and then inserting it into the tree. The original operator node is
2231 -- then rewritten as a call to this function. We also use this in the
2232 -- packed case if either operand is a possibly unaligned object.
70482933 2233
a9d8907c
JM
2234 declare
2235 Loc : constant Source_Ptr := Sloc (N);
2236 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2237 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2238 Func_Body : Node_Id;
2239 Func_Name : Entity_Id;
fbf5a39b 2240
a9d8907c
JM
2241 begin
2242 Convert_To_Actual_Subtype (L);
2243 Convert_To_Actual_Subtype (R);
2244 Ensure_Defined (Etype (L), N);
2245 Ensure_Defined (Etype (R), N);
2246 Apply_Length_Check (R, Etype (L));
2247
b4592168
GD
2248 if Nkind (N) = N_Op_Xor then
2249 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2250 end if;
2251
a9d8907c
JM
2252 if Nkind (Parent (N)) = N_Assignment_Statement
2253 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2254 then
2255 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
fbf5a39b 2256
a9d8907c
JM
2257 elsif Nkind (Parent (N)) = N_Op_Not
2258 and then Nkind (N) = N_Op_And
2259 and then
b4592168 2260 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
a9d8907c
JM
2261 then
2262 return;
2263 else
fbf5a39b 2264
a9d8907c
JM
2265 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2266 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2267 Insert_Action (N, Func_Body);
70482933 2268
a9d8907c 2269 -- Now rewrite the expression with a call
70482933 2270
a9d8907c
JM
2271 Rewrite (N,
2272 Make_Function_Call (Loc,
2273 Name => New_Reference_To (Func_Name, Loc),
2274 Parameter_Associations =>
2275 New_List (
2276 L,
2277 Make_Type_Conversion
2278 (Loc, New_Reference_To (Etype (L), Loc), R))));
70482933 2279
a9d8907c
JM
2280 Analyze_And_Resolve (N, Typ);
2281 end if;
2282 end;
70482933
RK
2283 end Expand_Boolean_Operator;
2284
456cbfa5
AC
2285 ------------------------------------------------
2286 -- Expand_Compare_Minimize_Eliminate_Overflow --
2287 ------------------------------------------------
2288
2289 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is
2290 Loc : constant Source_Ptr := Sloc (N);
2291
71fb4dc8
AC
2292 Result_Type : constant Entity_Id := Etype (N);
2293 -- Capture result type (could be a derived boolean type)
2294
456cbfa5
AC
2295 Llo, Lhi : Uint;
2296 Rlo, Rhi : Uint;
2297
2298 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
2299 -- Entity for Long_Long_Integer'Base
2300
15c94a55 2301 Check : constant Overflow_Mode_Type := Overflow_Check_Mode;
a7f1b24f 2302 -- Current overflow checking mode
456cbfa5
AC
2303
2304 procedure Set_True;
2305 procedure Set_False;
2306 -- These procedures rewrite N with an occurrence of Standard_True or
2307 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2308
2309 ---------------
2310 -- Set_False --
2311 ---------------
2312
2313 procedure Set_False is
2314 begin
2315 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2316 Warn_On_Known_Condition (N);
2317 end Set_False;
2318
2319 --------------
2320 -- Set_True --
2321 --------------
2322
2323 procedure Set_True is
2324 begin
2325 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2326 Warn_On_Known_Condition (N);
2327 end Set_True;
2328
2329 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2330
2331 begin
2332 -- Nothing to do unless we have a comparison operator with operands
2333 -- that are signed integer types, and we are operating in either
2334 -- MINIMIZED or ELIMINATED overflow checking mode.
2335
2336 if Nkind (N) not in N_Op_Compare
2337 or else Check not in Minimized_Or_Eliminated
2338 or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N)))
2339 then
2340 return;
2341 end if;
2342
2343 -- OK, this is the case we are interested in. First step is to process
2344 -- our operands using the Minimize_Eliminate circuitry which applies
2345 -- this processing to the two operand subtrees.
2346
a7f1b24f 2347 Minimize_Eliminate_Overflows
c7e152b5 2348 (Left_Opnd (N), Llo, Lhi, Top_Level => False);
a7f1b24f 2349 Minimize_Eliminate_Overflows
c7e152b5 2350 (Right_Opnd (N), Rlo, Rhi, Top_Level => False);
456cbfa5 2351
65f7ed64
AC
2352 -- See if the range information decides the result of the comparison.
2353 -- We can only do this if we in fact have full range information (which
2354 -- won't be the case if either operand is bignum at this stage).
456cbfa5 2355
65f7ed64
AC
2356 if Llo /= No_Uint and then Rlo /= No_Uint then
2357 case N_Op_Compare (Nkind (N)) is
456cbfa5
AC
2358 when N_Op_Eq =>
2359 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2360 Set_True;
a40ada7e 2361 elsif Llo > Rhi or else Lhi < Rlo then
456cbfa5
AC
2362 Set_False;
2363 end if;
2364
2365 when N_Op_Ge =>
2366 if Llo >= Rhi then
2367 Set_True;
2368 elsif Lhi < Rlo then
2369 Set_False;
2370 end if;
2371
2372 when N_Op_Gt =>
2373 if Llo > Rhi then
2374 Set_True;
2375 elsif Lhi <= Rlo then
2376 Set_False;
2377 end if;
2378
2379 when N_Op_Le =>
2380 if Llo > Rhi then
2381 Set_False;
2382 elsif Lhi <= Rlo then
2383 Set_True;
2384 end if;
2385
2386 when N_Op_Lt =>
2387 if Llo >= Rhi then
456cbfa5 2388 Set_False;
b6b5cca8
AC
2389 elsif Lhi < Rlo then
2390 Set_True;
456cbfa5
AC
2391 end if;
2392
2393 when N_Op_Ne =>
2394 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
456cbfa5 2395 Set_False;
a40ada7e
RD
2396 elsif Llo > Rhi or else Lhi < Rlo then
2397 Set_True;
456cbfa5 2398 end if;
65f7ed64 2399 end case;
456cbfa5 2400
65f7ed64 2401 -- All done if we did the rewrite
456cbfa5 2402
65f7ed64
AC
2403 if Nkind (N) not in N_Op_Compare then
2404 return;
2405 end if;
456cbfa5
AC
2406 end if;
2407
2408 -- Otherwise, time to do the comparison
2409
2410 declare
2411 Ltype : constant Entity_Id := Etype (Left_Opnd (N));
2412 Rtype : constant Entity_Id := Etype (Right_Opnd (N));
2413
2414 begin
2415 -- If the two operands have the same signed integer type we are
2416 -- all set, nothing more to do. This is the case where either
2417 -- both operands were unchanged, or we rewrote both of them to
2418 -- be Long_Long_Integer.
2419
2420 -- Note: Entity for the comparison may be wrong, but it's not worth
2421 -- the effort to change it, since the back end does not use it.
2422
2423 if Is_Signed_Integer_Type (Ltype)
2424 and then Base_Type (Ltype) = Base_Type (Rtype)
2425 then
2426 return;
2427
2428 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2429
2430 elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then
2431 declare
2432 Left : Node_Id := Left_Opnd (N);
2433 Right : Node_Id := Right_Opnd (N);
2434 -- Bignum references for left and right operands
2435
2436 begin
2437 if not Is_RTE (Ltype, RE_Bignum) then
2438 Left := Convert_To_Bignum (Left);
2439 elsif not Is_RTE (Rtype, RE_Bignum) then
2440 Right := Convert_To_Bignum (Right);
2441 end if;
2442
71fb4dc8 2443 -- We rewrite our node with:
456cbfa5 2444
71fb4dc8
AC
2445 -- do
2446 -- Bnn : Result_Type;
2447 -- declare
2448 -- M : Mark_Id := SS_Mark;
2449 -- begin
2450 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2451 -- SS_Release (M);
2452 -- end;
2453 -- in
2454 -- Bnn
2455 -- end
456cbfa5
AC
2456
2457 declare
71fb4dc8 2458 Blk : constant Node_Id := Make_Bignum_Block (Loc);
456cbfa5
AC
2459 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
2460 Ent : RE_Id;
2461
2462 begin
2463 case N_Op_Compare (Nkind (N)) is
2464 when N_Op_Eq => Ent := RE_Big_EQ;
2465 when N_Op_Ge => Ent := RE_Big_GE;
2466 when N_Op_Gt => Ent := RE_Big_GT;
2467 when N_Op_Le => Ent := RE_Big_LE;
2468 when N_Op_Lt => Ent := RE_Big_LT;
2469 when N_Op_Ne => Ent := RE_Big_NE;
2470 end case;
2471
71fb4dc8 2472 -- Insert assignment to Bnn into the bignum block
456cbfa5
AC
2473
2474 Insert_Before
2475 (First (Statements (Handled_Statement_Sequence (Blk))),
2476 Make_Assignment_Statement (Loc,
2477 Name => New_Occurrence_Of (Bnn, Loc),
2478 Expression =>
2479 Make_Function_Call (Loc,
2480 Name =>
2481 New_Occurrence_Of (RTE (Ent), Loc),
2482 Parameter_Associations => New_List (Left, Right))));
2483
71fb4dc8
AC
2484 -- Now do the rewrite with expression actions
2485
2486 Rewrite (N,
2487 Make_Expression_With_Actions (Loc,
2488 Actions => New_List (
2489 Make_Object_Declaration (Loc,
2490 Defining_Identifier => Bnn,
2491 Object_Definition =>
2492 New_Occurrence_Of (Result_Type, Loc)),
2493 Blk),
2494 Expression => New_Occurrence_Of (Bnn, Loc)));
2495 Analyze_And_Resolve (N, Result_Type);
456cbfa5
AC
2496 end;
2497 end;
2498
2499 -- No bignums involved, but types are different, so we must have
2500 -- rewritten one of the operands as a Long_Long_Integer but not
2501 -- the other one.
2502
2503 -- If left operand is Long_Long_Integer, convert right operand
2504 -- and we are done (with a comparison of two Long_Long_Integers).
2505
2506 elsif Ltype = LLIB then
2507 Convert_To_And_Rewrite (LLIB, Right_Opnd (N));
2508 Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks);
2509 return;
2510
2511 -- If right operand is Long_Long_Integer, convert left operand
2512 -- and we are done (with a comparison of two Long_Long_Integers).
2513
2514 -- This is the only remaining possibility
2515
2516 else pragma Assert (Rtype = LLIB);
2517 Convert_To_And_Rewrite (LLIB, Left_Opnd (N));
2518 Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks);
2519 return;
2520 end if;
2521 end;
2522 end Expand_Compare_Minimize_Eliminate_Overflow;
2523
70482933
RK
2524 -------------------------------
2525 -- Expand_Composite_Equality --
2526 -------------------------------
2527
2528 -- This function is only called for comparing internal fields of composite
2529 -- types when these fields are themselves composites. This is a special
2530 -- case because it is not possible to respect normal Ada visibility rules.
2531
2532 function Expand_Composite_Equality
2533 (Nod : Node_Id;
2534 Typ : Entity_Id;
2535 Lhs : Node_Id;
2536 Rhs : Node_Id;
2e071734 2537 Bodies : List_Id) return Node_Id
70482933
RK
2538 is
2539 Loc : constant Source_Ptr := Sloc (Nod);
2540 Full_Type : Entity_Id;
2541 Prim : Elmt_Id;
2542 Eq_Op : Entity_Id;
2543
7efc3f2d
AC
2544 function Find_Primitive_Eq return Node_Id;
2545 -- AI05-0123: Locate primitive equality for type if it exists, and
2546 -- build the corresponding call. If operation is abstract, replace
2547 -- call with an explicit raise. Return Empty if there is no primitive.
2548
2549 -----------------------
2550 -- Find_Primitive_Eq --
2551 -----------------------
2552
2553 function Find_Primitive_Eq return Node_Id is
2554 Prim_E : Elmt_Id;
2555 Prim : Node_Id;
2556
2557 begin
2558 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2559 while Present (Prim_E) loop
2560 Prim := Node (Prim_E);
2561
2562 -- Locate primitive equality with the right signature
2563
2564 if Chars (Prim) = Name_Op_Eq
2565 and then Etype (First_Formal (Prim)) =
39ade2f9 2566 Etype (Next_Formal (First_Formal (Prim)))
7efc3f2d
AC
2567 and then Etype (Prim) = Standard_Boolean
2568 then
2569 if Is_Abstract_Subprogram (Prim) then
2570 return
2571 Make_Raise_Program_Error (Loc,
2572 Reason => PE_Explicit_Raise);
2573
2574 else
2575 return
2576 Make_Function_Call (Loc,
39ade2f9 2577 Name => New_Reference_To (Prim, Loc),
7efc3f2d
AC
2578 Parameter_Associations => New_List (Lhs, Rhs));
2579 end if;
2580 end if;
2581
2582 Next_Elmt (Prim_E);
2583 end loop;
2584
2585 -- If not found, predefined operation will be used
2586
2587 return Empty;
2588 end Find_Primitive_Eq;
2589
2590 -- Start of processing for Expand_Composite_Equality
2591
70482933
RK
2592 begin
2593 if Is_Private_Type (Typ) then
2594 Full_Type := Underlying_Type (Typ);
2595 else
2596 Full_Type := Typ;
2597 end if;
2598
ced8450b
ES
2599 -- If the private type has no completion the context may be the
2600 -- expansion of a composite equality for a composite type with some
2601 -- still incomplete components. The expression will not be analyzed
2602 -- until the enclosing type is completed, at which point this will be
2603 -- properly expanded, unless there is a bona fide completion error.
70482933
RK
2604
2605 if No (Full_Type) then
ced8450b 2606 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
70482933
RK
2607 end if;
2608
2609 Full_Type := Base_Type (Full_Type);
2610
da1b76c1
HK
2611 -- When the base type itself is private, use the full view to expand
2612 -- the composite equality.
2613
2614 if Is_Private_Type (Full_Type) then
2615 Full_Type := Underlying_Type (Full_Type);
2616 end if;
2617
16788d44
RD
2618 -- Case of array types
2619
70482933
RK
2620 if Is_Array_Type (Full_Type) then
2621
2622 -- If the operand is an elementary type other than a floating-point
2623 -- type, then we can simply use the built-in block bitwise equality,
2624 -- since the predefined equality operators always apply and bitwise
2625 -- equality is fine for all these cases.
2626
2627 if Is_Elementary_Type (Component_Type (Full_Type))
2628 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2629 then
39ade2f9 2630 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
70482933 2631
685094bf
RD
2632 -- For composite component types, and floating-point types, use the
2633 -- expansion. This deals with tagged component types (where we use
2634 -- the applicable equality routine) and floating-point, (where we
2635 -- need to worry about negative zeroes), and also the case of any
2636 -- composite type recursively containing such fields.
70482933
RK
2637
2638 else
0da2c8ac 2639 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
70482933
RK
2640 end if;
2641
16788d44
RD
2642 -- Case of tagged record types
2643
70482933
RK
2644 elsif Is_Tagged_Type (Full_Type) then
2645
2646 -- Call the primitive operation "=" of this type
2647
2648 if Is_Class_Wide_Type (Full_Type) then
2649 Full_Type := Root_Type (Full_Type);
2650 end if;
2651
685094bf
RD
2652 -- If this is derived from an untagged private type completed with a
2653 -- tagged type, it does not have a full view, so we use the primitive
2654 -- operations of the private type. This check should no longer be
2655 -- necessary when these types receive their full views ???
70482933
RK
2656
2657 if Is_Private_Type (Typ)
2658 and then not Is_Tagged_Type (Typ)
2659 and then not Is_Controlled (Typ)
2660 and then Is_Derived_Type (Typ)
2661 and then No (Full_View (Typ))
2662 then
2663 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2664 else
2665 Prim := First_Elmt (Primitive_Operations (Full_Type));
2666 end if;
2667
2668 loop
2669 Eq_Op := Node (Prim);
2670 exit when Chars (Eq_Op) = Name_Op_Eq
2671 and then Etype (First_Formal (Eq_Op)) =
e6f69614
AC
2672 Etype (Next_Formal (First_Formal (Eq_Op)))
2673 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
70482933
RK
2674 Next_Elmt (Prim);
2675 pragma Assert (Present (Prim));
2676 end loop;
2677
2678 Eq_Op := Node (Prim);
2679
2680 return
2681 Make_Function_Call (Loc,
2682 Name => New_Reference_To (Eq_Op, Loc),
2683 Parameter_Associations =>
2684 New_List
2685 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2686 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2687
16788d44
RD
2688 -- Case of untagged record types
2689
70482933 2690 elsif Is_Record_Type (Full_Type) then
fbf5a39b 2691 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
70482933
RK
2692
2693 if Present (Eq_Op) then
2694 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2695
685094bf
RD
2696 -- Inherited equality from parent type. Convert the actuals to
2697 -- match signature of operation.
70482933
RK
2698
2699 declare
fbf5a39b 2700 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
70482933
RK
2701
2702 begin
2703 return
2704 Make_Function_Call (Loc,
39ade2f9
AC
2705 Name => New_Reference_To (Eq_Op, Loc),
2706 Parameter_Associations => New_List (
2707 OK_Convert_To (T, Lhs),
2708 OK_Convert_To (T, Rhs)));
70482933
RK
2709 end;
2710
2711 else
5d09245e
AC
2712 -- Comparison between Unchecked_Union components
2713
2714 if Is_Unchecked_Union (Full_Type) then
2715 declare
2716 Lhs_Type : Node_Id := Full_Type;
2717 Rhs_Type : Node_Id := Full_Type;
2718 Lhs_Discr_Val : Node_Id;
2719 Rhs_Discr_Val : Node_Id;
2720
2721 begin
2722 -- Lhs subtype
2723
2724 if Nkind (Lhs) = N_Selected_Component then
2725 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2726 end if;
2727
2728 -- Rhs subtype
2729
2730 if Nkind (Rhs) = N_Selected_Component then
2731 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2732 end if;
2733
2734 -- Lhs of the composite equality
2735
2736 if Is_Constrained (Lhs_Type) then
2737
685094bf 2738 -- Since the enclosing record type can never be an
5d09245e
AC
2739 -- Unchecked_Union (this code is executed for records
2740 -- that do not have variants), we may reference its
2741 -- discriminant(s).
2742
2743 if Nkind (Lhs) = N_Selected_Component
533369aa
AC
2744 and then Has_Per_Object_Constraint
2745 (Entity (Selector_Name (Lhs)))
5d09245e
AC
2746 then
2747 Lhs_Discr_Val :=
2748 Make_Selected_Component (Loc,
39ade2f9 2749 Prefix => Prefix (Lhs),
5d09245e 2750 Selector_Name =>
39ade2f9
AC
2751 New_Copy
2752 (Get_Discriminant_Value
2753 (First_Discriminant (Lhs_Type),
2754 Lhs_Type,
2755 Stored_Constraint (Lhs_Type))));
5d09245e
AC
2756
2757 else
39ade2f9
AC
2758 Lhs_Discr_Val :=
2759 New_Copy
2760 (Get_Discriminant_Value
2761 (First_Discriminant (Lhs_Type),
2762 Lhs_Type,
2763 Stored_Constraint (Lhs_Type)));
5d09245e
AC
2764
2765 end if;
2766 else
2767 -- It is not possible to infer the discriminant since
2768 -- the subtype is not constrained.
2769
8aceda64 2770 return
5d09245e 2771 Make_Raise_Program_Error (Loc,
8aceda64 2772 Reason => PE_Unchecked_Union_Restriction);
5d09245e
AC
2773 end if;
2774
2775 -- Rhs of the composite equality
2776
2777 if Is_Constrained (Rhs_Type) then
2778 if Nkind (Rhs) = N_Selected_Component
39ade2f9
AC
2779 and then Has_Per_Object_Constraint
2780 (Entity (Selector_Name (Rhs)))
5d09245e
AC
2781 then
2782 Rhs_Discr_Val :=
2783 Make_Selected_Component (Loc,
39ade2f9 2784 Prefix => Prefix (Rhs),
5d09245e 2785 Selector_Name =>
39ade2f9
AC
2786 New_Copy
2787 (Get_Discriminant_Value
2788 (First_Discriminant (Rhs_Type),
2789 Rhs_Type,
2790 Stored_Constraint (Rhs_Type))));
5d09245e
AC
2791
2792 else
39ade2f9
AC
2793 Rhs_Discr_Val :=
2794 New_Copy
2795 (Get_Discriminant_Value
2796 (First_Discriminant (Rhs_Type),
2797 Rhs_Type,
2798 Stored_Constraint (Rhs_Type)));
5d09245e
AC
2799
2800 end if;
2801 else
8aceda64 2802 return
5d09245e 2803 Make_Raise_Program_Error (Loc,
8aceda64 2804 Reason => PE_Unchecked_Union_Restriction);
5d09245e
AC
2805 end if;
2806
2807 -- Call the TSS equality function with the inferred
2808 -- discriminant values.
2809
2810 return
2811 Make_Function_Call (Loc,
2812 Name => New_Reference_To (Eq_Op, Loc),
2813 Parameter_Associations => New_List (
2814 Lhs,
2815 Rhs,
2816 Lhs_Discr_Val,
2817 Rhs_Discr_Val));
2818 end;
d151d6a3
AC
2819
2820 else
2821 return
2822 Make_Function_Call (Loc,
2823 Name => New_Reference_To (Eq_Op, Loc),
2824 Parameter_Associations => New_List (Lhs, Rhs));
5d09245e 2825 end if;
d151d6a3 2826 end if;
5d09245e 2827
3058f181
BD
2828 -- Equality composes in Ada 2012 for untagged record types. It also
2829 -- composes for bounded strings, because they are part of the
2830 -- predefined environment. We could make it compose for bounded
2831 -- strings by making them tagged, or by making sure all subcomponents
2832 -- are set to the same value, even when not used. Instead, we have
2833 -- this special case in the compiler, because it's more efficient.
2834
2835 elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then
5d09245e 2836
08daa782 2837 -- If no TSS has been created for the type, check whether there is
7efc3f2d 2838 -- a primitive equality declared for it.
d151d6a3
AC
2839
2840 declare
3058f181 2841 Op : constant Node_Id := Find_Primitive_Eq;
d151d6a3
AC
2842
2843 begin
a1fc903a
AC
2844 -- Use user-defined primitive if it exists, otherwise use
2845 -- predefined equality.
2846
3058f181
BD
2847 if Present (Op) then
2848 return Op;
7efc3f2d 2849 else
7efc3f2d
AC
2850 return Make_Op_Eq (Loc, Lhs, Rhs);
2851 end if;
d151d6a3
AC
2852 end;
2853
70482933
RK
2854 else
2855 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2856 end if;
2857
16788d44 2858 -- Non-composite types (always use predefined equality)
70482933 2859
16788d44 2860 else
70482933
RK
2861 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2862 end if;
2863 end Expand_Composite_Equality;
2864
fdac1f80
AC
2865 ------------------------
2866 -- Expand_Concatenate --
2867 ------------------------
70482933 2868
fdac1f80
AC
2869 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2870 Loc : constant Source_Ptr := Sloc (Cnode);
70482933 2871
fdac1f80
AC
2872 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2873 -- Result type of concatenation
70482933 2874
fdac1f80
AC
2875 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2876 -- Component type. Elements of this component type can appear as one
2877 -- of the operands of concatenation as well as arrays.
70482933 2878
ecc4ddde
AC
2879 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2880 -- Index subtype
2881
2882 Ityp : constant Entity_Id := Base_Type (Istyp);
2883 -- Index type. This is the base type of the index subtype, and is used
2884 -- for all computed bounds (which may be out of range of Istyp in the
2885 -- case of null ranges).
70482933 2886
46ff89f3 2887 Artyp : Entity_Id;
fdac1f80
AC
2888 -- This is the type we use to do arithmetic to compute the bounds and
2889 -- lengths of operands. The choice of this type is a little subtle and
2890 -- is discussed in a separate section at the start of the body code.
70482933 2891
fdac1f80
AC
2892 Concatenation_Error : exception;
2893 -- Raised if concatenation is sure to raise a CE
70482933 2894
0ac73189
AC
2895 Result_May_Be_Null : Boolean := True;
2896 -- Reset to False if at least one operand is encountered which is known
2897 -- at compile time to be non-null. Used for handling the special case
2898 -- of setting the high bound to the last operand high bound for a null
2899 -- result, thus ensuring a proper high bound in the super-flat case.
2900
df46b832 2901 N : constant Nat := List_Length (Opnds);
fdac1f80 2902 -- Number of concatenation operands including possibly null operands
df46b832
AC
2903
2904 NN : Nat := 0;
a29262fd
AC
2905 -- Number of operands excluding any known to be null, except that the
2906 -- last operand is always retained, in case it provides the bounds for
2907 -- a null result.
2908
2909 Opnd : Node_Id;
2910 -- Current operand being processed in the loop through operands. After
2911 -- this loop is complete, always contains the last operand (which is not
2912 -- the same as Operands (NN), since null operands are skipped).
df46b832
AC
2913
2914 -- Arrays describing the operands, only the first NN entries of each
2915 -- array are set (NN < N when we exclude known null operands).
2916
2917 Is_Fixed_Length : array (1 .. N) of Boolean;
2918 -- True if length of corresponding operand known at compile time
2919
2920 Operands : array (1 .. N) of Node_Id;
a29262fd
AC
2921 -- Set to the corresponding entry in the Opnds list (but note that null
2922 -- operands are excluded, so not all entries in the list are stored).
df46b832
AC
2923
2924 Fixed_Length : array (1 .. N) of Uint;
fdac1f80
AC
2925 -- Set to length of operand. Entries in this array are set only if the
2926 -- corresponding entry in Is_Fixed_Length is True.
df46b832 2927
0ac73189
AC
2928 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2929 -- Set to lower bound of operand. Either an integer literal in the case
2930 -- where the bound is known at compile time, else actual lower bound.
2931 -- The operand low bound is of type Ityp.
2932
df46b832
AC
2933 Var_Length : array (1 .. N) of Entity_Id;
2934 -- Set to an entity of type Natural that contains the length of an
2935 -- operand whose length is not known at compile time. Entries in this
2936 -- array are set only if the corresponding entry in Is_Fixed_Length
46ff89f3 2937 -- is False. The entity is of type Artyp.
df46b832
AC
2938
2939 Aggr_Length : array (0 .. N) of Node_Id;
fdac1f80
AC
2940 -- The J'th entry in an expression node that represents the total length
2941 -- of operands 1 through J. It is either an integer literal node, or a
2942 -- reference to a constant entity with the right value, so it is fine
2943 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
46ff89f3 2944 -- entry always is set to zero. The length is of type Artyp.
df46b832
AC
2945
2946 Low_Bound : Node_Id;
0ac73189
AC
2947 -- A tree node representing the low bound of the result (of type Ityp).
2948 -- This is either an integer literal node, or an identifier reference to
2949 -- a constant entity initialized to the appropriate value.
2950
88a27b18
AC
2951 Last_Opnd_Low_Bound : Node_Id;
2952 -- A tree node representing the low bound of the last operand. This
2953 -- need only be set if the result could be null. It is used for the
2954 -- special case of setting the right low bound for a null result.
2955 -- This is of type Ityp.
2956
a29262fd
AC
2957 Last_Opnd_High_Bound : Node_Id;
2958 -- A tree node representing the high bound of the last operand. This
2959 -- need only be set if the result could be null. It is used for the
2960 -- special case of setting the right high bound for a null result.
2961 -- This is of type Ityp.
2962
0ac73189
AC
2963 High_Bound : Node_Id;
2964 -- A tree node representing the high bound of the result (of type Ityp)
df46b832
AC
2965
2966 Result : Node_Id;
0ac73189 2967 -- Result of the concatenation (of type Ityp)
df46b832 2968
d0f8d157 2969 Actions : constant List_Id := New_List;
4c9fe6c7 2970 -- Collect actions to be inserted
d0f8d157 2971
fa969310 2972 Known_Non_Null_Operand_Seen : Boolean;
308e6f3a 2973 -- Set True during generation of the assignments of operands into
fa969310
AC
2974 -- result once an operand known to be non-null has been seen.
2975
2976 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2977 -- This function makes an N_Integer_Literal node that is returned in
2978 -- analyzed form with the type set to Artyp. Importantly this literal
2979 -- is not flagged as static, so that if we do computations with it that
2980 -- result in statically detected out of range conditions, we will not
2981 -- generate error messages but instead warning messages.
2982
46ff89f3 2983 function To_Artyp (X : Node_Id) return Node_Id;
fdac1f80 2984 -- Given a node of type Ityp, returns the corresponding value of type
76c597a1
AC
2985 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2986 -- For enum types, the Pos of the value is returned.
fdac1f80
AC
2987
2988 function To_Ityp (X : Node_Id) return Node_Id;
0ac73189 2989 -- The inverse function (uses Val in the case of enumeration types)
fdac1f80 2990
fa969310
AC
2991 ------------------------
2992 -- Make_Artyp_Literal --
2993 ------------------------
2994
2995 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2996 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2997 begin
2998 Set_Etype (Result, Artyp);
2999 Set_Analyzed (Result, True);
3000 Set_Is_Static_Expression (Result, False);
3001 return Result;
3002 end Make_Artyp_Literal;
76c597a1 3003
fdac1f80 3004 --------------
46ff89f3 3005 -- To_Artyp --
fdac1f80
AC
3006 --------------
3007
46ff89f3 3008 function To_Artyp (X : Node_Id) return Node_Id is
fdac1f80 3009 begin
46ff89f3 3010 if Ityp = Base_Type (Artyp) then
fdac1f80
AC
3011 return X;
3012
3013 elsif Is_Enumeration_Type (Ityp) then
3014 return
3015 Make_Attribute_Reference (Loc,
3016 Prefix => New_Occurrence_Of (Ityp, Loc),
3017 Attribute_Name => Name_Pos,
3018 Expressions => New_List (X));
3019
3020 else
46ff89f3 3021 return Convert_To (Artyp, X);
fdac1f80 3022 end if;
46ff89f3 3023 end To_Artyp;
fdac1f80
AC
3024
3025 -------------
3026 -- To_Ityp --
3027 -------------
3028
3029 function To_Ityp (X : Node_Id) return Node_Id is
3030 begin
2fc05e3d 3031 if Is_Enumeration_Type (Ityp) then
fdac1f80
AC
3032 return
3033 Make_Attribute_Reference (Loc,
3034 Prefix => New_Occurrence_Of (Ityp, Loc),
3035 Attribute_Name => Name_Val,
3036 Expressions => New_List (X));
3037
3038 -- Case where we will do a type conversion
3039
3040 else
76c597a1
AC
3041 if Ityp = Base_Type (Artyp) then
3042 return X;
fdac1f80 3043 else
76c597a1 3044 return Convert_To (Ityp, X);
fdac1f80
AC
3045 end if;
3046 end if;
3047 end To_Ityp;
3048
3049 -- Local Declarations
3050
0ac73189
AC
3051 Opnd_Typ : Entity_Id;
3052 Ent : Entity_Id;
3053 Len : Uint;
3054 J : Nat;
3055 Clen : Node_Id;
3056 Set : Boolean;
70482933 3057
f46faa08
AC
3058 -- Start of processing for Expand_Concatenate
3059
70482933 3060 begin
fdac1f80
AC
3061 -- Choose an appropriate computational type
3062
3063 -- We will be doing calculations of lengths and bounds in this routine
3064 -- and computing one from the other in some cases, e.g. getting the high
3065 -- bound by adding the length-1 to the low bound.
3066
3067 -- We can't just use the index type, or even its base type for this
3068 -- purpose for two reasons. First it might be an enumeration type which
308e6f3a
RW
3069 -- is not suitable for computations of any kind, and second it may
3070 -- simply not have enough range. For example if the index type is
3071 -- -128..+127 then lengths can be up to 256, which is out of range of
3072 -- the type.
fdac1f80
AC
3073
3074 -- For enumeration types, we can simply use Standard_Integer, this is
3075 -- sufficient since the actual number of enumeration literals cannot
3076 -- possibly exceed the range of integer (remember we will be doing the
0ac73189 3077 -- arithmetic with POS values, not representation values).
fdac1f80
AC
3078
3079 if Is_Enumeration_Type (Ityp) then
46ff89f3 3080 Artyp := Standard_Integer;
fdac1f80 3081
59262ebb
AC
3082 -- If index type is Positive, we use the standard unsigned type, to give
3083 -- more room on the top of the range, obviating the need for an overflow
3084 -- check when creating the upper bound. This is needed to avoid junk
3085 -- overflow checks in the common case of String types.
3086
3087 -- ??? Disabled for now
3088
3089 -- elsif Istyp = Standard_Positive then
3090 -- Artyp := Standard_Unsigned;
3091
2fc05e3d
AC
3092 -- For modular types, we use a 32-bit modular type for types whose size
3093 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
3094 -- identity type, and for larger unsigned types we use 64-bits.
fdac1f80 3095
2fc05e3d 3096 elsif Is_Modular_Integer_Type (Ityp) then
ecc4ddde 3097 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
46ff89f3 3098 Artyp := Standard_Unsigned;
ecc4ddde 3099 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
46ff89f3 3100 Artyp := Ityp;
fdac1f80 3101 else
46ff89f3 3102 Artyp := RTE (RE_Long_Long_Unsigned);
fdac1f80
AC
3103 end if;
3104
2fc05e3d 3105 -- Similar treatment for signed types
fdac1f80
AC
3106
3107 else
ecc4ddde 3108 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
46ff89f3 3109 Artyp := Standard_Integer;
ecc4ddde 3110 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
46ff89f3 3111 Artyp := Ityp;
fdac1f80 3112 else
46ff89f3 3113 Artyp := Standard_Long_Long_Integer;
fdac1f80
AC
3114 end if;
3115 end if;
3116
fa969310
AC
3117 -- Supply dummy entry at start of length array
3118
3119 Aggr_Length (0) := Make_Artyp_Literal (0);
3120
fdac1f80 3121 -- Go through operands setting up the above arrays
70482933 3122
df46b832
AC
3123 J := 1;
3124 while J <= N loop
3125 Opnd := Remove_Head (Opnds);
0ac73189 3126 Opnd_Typ := Etype (Opnd);
fdac1f80
AC
3127
3128 -- The parent got messed up when we put the operands in a list,
d347f572
AC
3129 -- so now put back the proper parent for the saved operand, that
3130 -- is to say the concatenation node, to make sure that each operand
3131 -- is seen as a subexpression, e.g. if actions must be inserted.
fdac1f80 3132
d347f572 3133 Set_Parent (Opnd, Cnode);
fdac1f80
AC
3134
3135 -- Set will be True when we have setup one entry in the array
3136
df46b832
AC
3137 Set := False;
3138
fdac1f80 3139 -- Singleton element (or character literal) case
df46b832 3140
0ac73189 3141 if Base_Type (Opnd_Typ) = Ctyp then
df46b832
AC
3142 NN := NN + 1;
3143 Operands (NN) := Opnd;
3144 Is_Fixed_Length (NN) := True;
3145 Fixed_Length (NN) := Uint_1;
0ac73189 3146 Result_May_Be_Null := False;
fdac1f80 3147
a29262fd
AC
3148 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
3149 -- since we know that the result cannot be null).
fdac1f80 3150
0ac73189
AC
3151 Opnd_Low_Bound (NN) :=
3152 Make_Attribute_Reference (Loc,
ecc4ddde 3153 Prefix => New_Reference_To (Istyp, Loc),
0ac73189
AC
3154 Attribute_Name => Name_First);
3155
df46b832
AC
3156 Set := True;
3157
fdac1f80 3158 -- String literal case (can only occur for strings of course)
df46b832
AC
3159
3160 elsif Nkind (Opnd) = N_String_Literal then
0ac73189 3161 Len := String_Literal_Length (Opnd_Typ);
df46b832 3162
a29262fd
AC
3163 if Len /= 0 then
3164 Result_May_Be_Null := False;
3165 end if;
3166
88a27b18 3167 -- Capture last operand low and high bound if result could be null
a29262fd
AC
3168
3169 if J = N and then Result_May_Be_Null then
88a27b18
AC
3170 Last_Opnd_Low_Bound :=
3171 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3172
a29262fd 3173 Last_Opnd_High_Bound :=
88a27b18 3174 Make_Op_Subtract (Loc,
a29262fd
AC
3175 Left_Opnd =>
3176 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
59262ebb 3177 Right_Opnd => Make_Integer_Literal (Loc, 1));
a29262fd
AC
3178 end if;
3179
3180 -- Skip null string literal
fdac1f80 3181
0ac73189 3182 if J < N and then Len = 0 then
df46b832
AC
3183 goto Continue;
3184 end if;
3185
3186 NN := NN + 1;
3187 Operands (NN) := Opnd;
3188 Is_Fixed_Length (NN) := True;
0ac73189
AC
3189
3190 -- Set length and bounds
3191
df46b832 3192 Fixed_Length (NN) := Len;
0ac73189
AC
3193
3194 Opnd_Low_Bound (NN) :=
3195 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3196
df46b832
AC
3197 Set := True;
3198
3199 -- All other cases
3200
3201 else
3202 -- Check constrained case with known bounds
3203
0ac73189 3204 if Is_Constrained (Opnd_Typ) then
df46b832 3205 declare
df46b832
AC
3206 Index : constant Node_Id := First_Index (Opnd_Typ);
3207 Indx_Typ : constant Entity_Id := Etype (Index);
3208 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
3209 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
3210
3211 begin
fdac1f80
AC
3212 -- Fixed length constrained array type with known at compile
3213 -- time bounds is last case of fixed length operand.
df46b832
AC
3214
3215 if Compile_Time_Known_Value (Lo)
3216 and then
3217 Compile_Time_Known_Value (Hi)
3218 then
3219 declare
3220 Loval : constant Uint := Expr_Value (Lo);
3221 Hival : constant Uint := Expr_Value (Hi);
3222 Len : constant Uint :=
3223 UI_Max (Hival - Loval + 1, Uint_0);
3224
3225 begin
0ac73189
AC
3226 if Len > 0 then
3227 Result_May_Be_Null := False;
df46b832 3228 end if;
0ac73189 3229
88a27b18 3230 -- Capture last operand bounds if result could be null
a29262fd
AC
3231
3232 if J = N and then Result_May_Be_Null then
88a27b18
AC
3233 Last_Opnd_Low_Bound :=
3234 Convert_To (Ityp,
3235 Make_Integer_Literal (Loc, Expr_Value (Lo)));
3236
a29262fd
AC
3237 Last_Opnd_High_Bound :=
3238 Convert_To (Ityp,
39ade2f9 3239 Make_Integer_Literal (Loc, Expr_Value (Hi)));
a29262fd
AC
3240 end if;
3241
3242 -- Exclude null length case unless last operand
0ac73189 3243
a29262fd 3244 if J < N and then Len = 0 then
0ac73189
AC
3245 goto Continue;
3246 end if;
3247
3248 NN := NN + 1;
3249 Operands (NN) := Opnd;
3250 Is_Fixed_Length (NN) := True;
3251 Fixed_Length (NN) := Len;
3252
39ade2f9
AC
3253 Opnd_Low_Bound (NN) :=
3254 To_Ityp
3255 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
0ac73189 3256 Set := True;
df46b832
AC
3257 end;
3258 end if;
3259 end;
3260 end if;
3261
0ac73189
AC
3262 -- All cases where the length is not known at compile time, or the
3263 -- special case of an operand which is known to be null but has a
3264 -- lower bound other than 1 or is other than a string type.
df46b832
AC
3265
3266 if not Set then
3267 NN := NN + 1;
0ac73189
AC
3268
3269 -- Capture operand bounds
3270
3271 Opnd_Low_Bound (NN) :=
3272 Make_Attribute_Reference (Loc,
3273 Prefix =>
3274 Duplicate_Subexpr (Opnd, Name_Req => True),
3275 Attribute_Name => Name_First);
3276
88a27b18
AC
3277 -- Capture last operand bounds if result could be null
3278
a29262fd 3279 if J = N and Result_May_Be_Null then
88a27b18
AC
3280 Last_Opnd_Low_Bound :=
3281 Convert_To (Ityp,
3282 Make_Attribute_Reference (Loc,
3283 Prefix =>
3284 Duplicate_Subexpr (Opnd, Name_Req => True),
3285 Attribute_Name => Name_First));
3286
a29262fd
AC
3287 Last_Opnd_High_Bound :=
3288 Convert_To (Ityp,
3289 Make_Attribute_Reference (Loc,
3290 Prefix =>
3291 Duplicate_Subexpr (Opnd, Name_Req => True),
3292 Attribute_Name => Name_Last));
3293 end if;
0ac73189
AC
3294
3295 -- Capture length of operand in entity
3296
df46b832
AC
3297 Operands (NN) := Opnd;
3298 Is_Fixed_Length (NN) := False;
3299
191fcb3a 3300 Var_Length (NN) := Make_Temporary (Loc, 'L');
df46b832 3301
d0f8d157 3302 Append_To (Actions,
df46b832
AC
3303 Make_Object_Declaration (Loc,
3304 Defining_Identifier => Var_Length (NN),
3305 Constant_Present => True,
39ade2f9 3306 Object_Definition => New_Occurrence_Of (Artyp, Loc),
df46b832
AC
3307 Expression =>
3308 Make_Attribute_Reference (Loc,
3309 Prefix =>
3310 Duplicate_Subexpr (Opnd, Name_Req => True),
d0f8d157 3311 Attribute_Name => Name_Length)));
df46b832
AC
3312 end if;
3313 end if;
3314
3315 -- Set next entry in aggregate length array
3316
3317 -- For first entry, make either integer literal for fixed length
0ac73189 3318 -- or a reference to the saved length for variable length.
df46b832
AC
3319
3320 if NN = 1 then
3321 if Is_Fixed_Length (1) then
39ade2f9 3322 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
df46b832 3323 else
39ade2f9 3324 Aggr_Length (1) := New_Reference_To (Var_Length (1), Loc);
df46b832
AC
3325 end if;
3326
3327 -- If entry is fixed length and only fixed lengths so far, make
3328 -- appropriate new integer literal adding new length.
3329
3330 elsif Is_Fixed_Length (NN)
3331 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
3332 then
3333 Aggr_Length (NN) :=
3334 Make_Integer_Literal (Loc,
3335 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
3336
d0f8d157
AC
3337 -- All other cases, construct an addition node for the length and
3338 -- create an entity initialized to this length.
df46b832
AC
3339
3340 else
191fcb3a 3341 Ent := Make_Temporary (Loc, 'L');
df46b832
AC
3342
3343 if Is_Fixed_Length (NN) then
3344 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
3345 else
3346 Clen := New_Reference_To (Var_Length (NN), Loc);
3347 end if;
3348
d0f8d157 3349 Append_To (Actions,
df46b832
AC
3350 Make_Object_Declaration (Loc,
3351 Defining_Identifier => Ent,
3352 Constant_Present => True,
39ade2f9 3353 Object_Definition => New_Occurrence_Of (Artyp, Loc),
df46b832
AC
3354 Expression =>
3355 Make_Op_Add (Loc,
3356 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
d0f8d157 3357 Right_Opnd => Clen)));
df46b832 3358
76c597a1 3359 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
df46b832
AC
3360 end if;
3361
3362 <<Continue>>
3363 J := J + 1;
3364 end loop;
3365
a29262fd 3366 -- If we have only skipped null operands, return the last operand
df46b832
AC
3367
3368 if NN = 0 then
a29262fd 3369 Result := Opnd;
df46b832
AC
3370 goto Done;
3371 end if;
3372
3373 -- If we have only one non-null operand, return it and we are done.
3374 -- There is one case in which this cannot be done, and that is when
fdac1f80
AC
3375 -- the sole operand is of the element type, in which case it must be
3376 -- converted to an array, and the easiest way of doing that is to go
df46b832
AC
3377 -- through the normal general circuit.
3378
533369aa 3379 if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then
df46b832
AC
3380 Result := Operands (1);
3381 goto Done;
3382 end if;
3383
3384 -- Cases where we have a real concatenation
3385
fdac1f80
AC
3386 -- Next step is to find the low bound for the result array that we
3387 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3388
3389 -- If the ultimate ancestor of the index subtype is a constrained array
3390 -- definition, then the lower bound is that of the index subtype as
3391 -- specified by (RM 4.5.3(6)).
3392
3393 -- The right test here is to go to the root type, and then the ultimate
3394 -- ancestor is the first subtype of this root type.
3395
3396 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
0ac73189 3397 Low_Bound :=
fdac1f80
AC
3398 Make_Attribute_Reference (Loc,
3399 Prefix =>
3400 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
0ac73189 3401 Attribute_Name => Name_First);
df46b832
AC
3402
3403 -- If the first operand in the list has known length we know that
3404 -- the lower bound of the result is the lower bound of this operand.
3405
fdac1f80 3406 elsif Is_Fixed_Length (1) then
0ac73189 3407 Low_Bound := Opnd_Low_Bound (1);
df46b832
AC
3408
3409 -- OK, we don't know the lower bound, we have to build a horrible
9b16cb57 3410 -- if expression node of the form
df46b832
AC
3411
3412 -- if Cond1'Length /= 0 then
0ac73189 3413 -- Opnd1 low bound
df46b832
AC
3414 -- else
3415 -- if Opnd2'Length /= 0 then
0ac73189 3416 -- Opnd2 low bound
df46b832
AC
3417 -- else
3418 -- ...
3419
3420 -- The nesting ends either when we hit an operand whose length is known
3421 -- at compile time, or on reaching the last operand, whose low bound we
3422 -- take unconditionally whether or not it is null. It's easiest to do
3423 -- this with a recursive procedure:
3424
3425 else
3426 declare
3427 function Get_Known_Bound (J : Nat) return Node_Id;
3428 -- Returns the lower bound determined by operands J .. NN
3429
3430 ---------------------
3431 -- Get_Known_Bound --
3432 ---------------------
3433
3434 function Get_Known_Bound (J : Nat) return Node_Id is
df46b832 3435 begin
0ac73189
AC
3436 if Is_Fixed_Length (J) or else J = NN then
3437 return New_Copy (Opnd_Low_Bound (J));
70482933
RK
3438
3439 else
df46b832 3440 return
9b16cb57 3441 Make_If_Expression (Loc,
df46b832
AC
3442 Expressions => New_List (
3443
3444 Make_Op_Ne (Loc,
3445 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
3446 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3447
0ac73189 3448 New_Copy (Opnd_Low_Bound (J)),
df46b832 3449 Get_Known_Bound (J + 1)));
70482933 3450 end if;
df46b832 3451 end Get_Known_Bound;
70482933 3452
df46b832 3453 begin
191fcb3a 3454 Ent := Make_Temporary (Loc, 'L');
df46b832 3455
d0f8d157 3456 Append_To (Actions,
df46b832
AC
3457 Make_Object_Declaration (Loc,
3458 Defining_Identifier => Ent,
3459 Constant_Present => True,
0ac73189 3460 Object_Definition => New_Occurrence_Of (Ityp, Loc),
d0f8d157 3461 Expression => Get_Known_Bound (1)));
df46b832
AC
3462
3463 Low_Bound := New_Reference_To (Ent, Loc);
3464 end;
3465 end if;
70482933 3466
76c597a1
AC
3467 -- Now we can safely compute the upper bound, normally
3468 -- Low_Bound + Length - 1.
0ac73189
AC
3469
3470 High_Bound :=
3471 To_Ityp (
3472 Make_Op_Add (Loc,
46ff89f3 3473 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
0ac73189
AC
3474 Right_Opnd =>
3475 Make_Op_Subtract (Loc,
3476 Left_Opnd => New_Copy (Aggr_Length (NN)),
fa969310 3477 Right_Opnd => Make_Artyp_Literal (1))));
0ac73189 3478
59262ebb 3479 -- Note that calculation of the high bound may cause overflow in some
bded454f
RD
3480 -- very weird cases, so in the general case we need an overflow check on
3481 -- the high bound. We can avoid this for the common case of string types
3482 -- and other types whose index is Positive, since we chose a wider range
3483 -- for the arithmetic type.
76c597a1 3484
59262ebb
AC
3485 if Istyp /= Standard_Positive then
3486 Activate_Overflow_Check (High_Bound);
3487 end if;
76c597a1
AC
3488
3489 -- Handle the exceptional case where the result is null, in which case
a29262fd
AC
3490 -- case the bounds come from the last operand (so that we get the proper
3491 -- bounds if the last operand is super-flat).
3492
0ac73189 3493 if Result_May_Be_Null then
88a27b18 3494 Low_Bound :=
9b16cb57 3495 Make_If_Expression (Loc,
88a27b18
AC
3496 Expressions => New_List (
3497 Make_Op_Eq (Loc,
3498 Left_Opnd => New_Copy (Aggr_Length (NN)),
3499 Right_Opnd => Make_Artyp_Literal (0)),
3500 Last_Opnd_Low_Bound,
3501 Low_Bound));
3502
0ac73189 3503 High_Bound :=
9b16cb57 3504 Make_If_Expression (Loc,
0ac73189
AC
3505 Expressions => New_List (
3506 Make_Op_Eq (Loc,
3507 Left_Opnd => New_Copy (Aggr_Length (NN)),
fa969310 3508 Right_Opnd => Make_Artyp_Literal (0)),
a29262fd 3509 Last_Opnd_High_Bound,
0ac73189
AC
3510 High_Bound));
3511 end if;
3512
d0f8d157
AC
3513 -- Here is where we insert the saved up actions
3514
3515 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3516
602a7ec0
AC
3517 -- Now we construct an array object with appropriate bounds. We mark
3518 -- the target as internal to prevent useless initialization when
e526d0c7
AC
3519 -- Initialize_Scalars is enabled. Also since this is the actual result
3520 -- entity, we make sure we have debug information for the result.
70482933 3521
191fcb3a 3522 Ent := Make_Temporary (Loc, 'S');
008f6fd3 3523 Set_Is_Internal (Ent);
e526d0c7 3524 Set_Needs_Debug_Info (Ent);
70482933 3525
76c597a1 3526 -- If the bound is statically known to be out of range, we do not want
fa969310
AC
3527 -- to abort, we want a warning and a runtime constraint error. Note that
3528 -- we have arranged that the result will not be treated as a static
3529 -- constant, so we won't get an illegality during this insertion.
76c597a1 3530
df46b832
AC
3531 Insert_Action (Cnode,
3532 Make_Object_Declaration (Loc,
3533 Defining_Identifier => Ent,
df46b832
AC
3534 Object_Definition =>
3535 Make_Subtype_Indication (Loc,
fdac1f80 3536 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
df46b832
AC
3537 Constraint =>
3538 Make_Index_Or_Discriminant_Constraint (Loc,
3539 Constraints => New_List (
3540 Make_Range (Loc,
0ac73189
AC
3541 Low_Bound => Low_Bound,
3542 High_Bound => High_Bound))))),
df46b832
AC
3543 Suppress => All_Checks);
3544
d1f453b7
RD
3545 -- If the result of the concatenation appears as the initializing
3546 -- expression of an object declaration, we can just rename the
3547 -- result, rather than copying it.
3548
3549 Set_OK_To_Rename (Ent);
3550
76c597a1
AC
3551 -- Catch the static out of range case now
3552
3553 if Raises_Constraint_Error (High_Bound) then
3554 raise Concatenation_Error;
3555 end if;
3556
df46b832
AC
3557 -- Now we will generate the assignments to do the actual concatenation
3558
bded454f
RD
3559 -- There is one case in which we will not do this, namely when all the
3560 -- following conditions are met:
3561
3562 -- The result type is Standard.String
3563
3564 -- There are nine or fewer retained (non-null) operands
3565
ffec8e81 3566 -- The optimization level is -O0
bded454f
RD
3567
3568 -- The corresponding System.Concat_n.Str_Concat_n routine is
3569 -- available in the run time.
3570
3571 -- The debug flag gnatd.c is not set
3572
3573 -- If all these conditions are met then we generate a call to the
3574 -- relevant concatenation routine. The purpose of this is to avoid
3575 -- undesirable code bloat at -O0.
3576
3577 if Atyp = Standard_String
3578 and then NN in 2 .. 9
ffec8e81 3579 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
bded454f
RD
3580 and then not Debug_Flag_Dot_C
3581 then
3582 declare
3583 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3584 (RE_Str_Concat_2,
3585 RE_Str_Concat_3,
3586 RE_Str_Concat_4,
3587 RE_Str_Concat_5,
3588 RE_Str_Concat_6,
3589 RE_Str_Concat_7,
3590 RE_Str_Concat_8,
3591 RE_Str_Concat_9);
3592
3593 begin
3594 if RTE_Available (RR (NN)) then
3595 declare
3596 Opnds : constant List_Id :=
3597 New_List (New_Occurrence_Of (Ent, Loc));
3598
3599 begin
3600 for J in 1 .. NN loop
3601 if Is_List_Member (Operands (J)) then
3602 Remove (Operands (J));
3603 end if;
3604
3605 if Base_Type (Etype (Operands (J))) = Ctyp then
3606 Append_To (Opnds,
3607 Make_Aggregate (Loc,
3608 Component_Associations => New_List (
3609 Make_Component_Association (Loc,
3610 Choices => New_List (
3611 Make_Integer_Literal (Loc, 1)),
3612 Expression => Operands (J)))));
3613
3614 else
3615 Append_To (Opnds, Operands (J));
3616 end if;
3617 end loop;
3618
3619 Insert_Action (Cnode,
3620 Make_Procedure_Call_Statement (Loc,
3621 Name => New_Reference_To (RTE (RR (NN)), Loc),
3622 Parameter_Associations => Opnds));
3623
3624 Result := New_Reference_To (Ent, Loc);
3625 goto Done;
3626 end;
3627 end if;
3628 end;
3629 end if;
3630
3631 -- Not special case so generate the assignments
3632
76c597a1
AC
3633 Known_Non_Null_Operand_Seen := False;
3634
df46b832
AC
3635 for J in 1 .. NN loop
3636 declare
3637 Lo : constant Node_Id :=
3638 Make_Op_Add (Loc,
46ff89f3 3639 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
df46b832
AC
3640 Right_Opnd => Aggr_Length (J - 1));
3641
3642 Hi : constant Node_Id :=
3643 Make_Op_Add (Loc,
46ff89f3 3644 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
df46b832
AC
3645 Right_Opnd =>
3646 Make_Op_Subtract (Loc,
3647 Left_Opnd => Aggr_Length (J),
fa969310 3648 Right_Opnd => Make_Artyp_Literal (1)));
70482933 3649
df46b832 3650 begin
fdac1f80
AC
3651 -- Singleton case, simple assignment
3652
3653 if Base_Type (Etype (Operands (J))) = Ctyp then
76c597a1 3654 Known_Non_Null_Operand_Seen := True;
df46b832
AC
3655 Insert_Action (Cnode,
3656 Make_Assignment_Statement (Loc,
3657 Name =>
3658 Make_Indexed_Component (Loc,
3659 Prefix => New_Occurrence_Of (Ent, Loc),
fdac1f80 3660 Expressions => New_List (To_Ityp (Lo))),
df46b832
AC
3661 Expression => Operands (J)),
3662 Suppress => All_Checks);
70482933 3663
76c597a1
AC
3664 -- Array case, slice assignment, skipped when argument is fixed
3665 -- length and known to be null.
fdac1f80 3666
76c597a1
AC
3667 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3668 declare
3669 Assign : Node_Id :=
3670 Make_Assignment_Statement (Loc,
3671 Name =>
3672 Make_Slice (Loc,
3673 Prefix =>
3674 New_Occurrence_Of (Ent, Loc),
3675 Discrete_Range =>
3676 Make_Range (Loc,
3677 Low_Bound => To_Ityp (Lo),
3678 High_Bound => To_Ityp (Hi))),
3679 Expression => Operands (J));
3680 begin
3681 if Is_Fixed_Length (J) then
3682 Known_Non_Null_Operand_Seen := True;
3683
3684 elsif not Known_Non_Null_Operand_Seen then
3685
3686 -- Here if operand length is not statically known and no
3687 -- operand known to be non-null has been processed yet.
3688 -- If operand length is 0, we do not need to perform the
3689 -- assignment, and we must avoid the evaluation of the
3690 -- high bound of the slice, since it may underflow if the
3691 -- low bound is Ityp'First.
3692
3693 Assign :=
3694 Make_Implicit_If_Statement (Cnode,
39ade2f9 3695 Condition =>
76c597a1 3696 Make_Op_Ne (Loc,
39ade2f9 3697 Left_Opnd =>
76c597a1
AC
3698 New_Occurrence_Of (Var_Length (J), Loc),
3699 Right_Opnd => Make_Integer_Literal (Loc, 0)),
39ade2f9 3700 Then_Statements => New_List (Assign));
76c597a1 3701 end if;
fa969310 3702
76c597a1
AC
3703 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3704 end;
df46b832
AC
3705 end if;
3706 end;
3707 end loop;
70482933 3708
0ac73189
AC
3709 -- Finally we build the result, which is a reference to the array object
3710
df46b832 3711 Result := New_Reference_To (Ent, Loc);
70482933 3712
df46b832
AC
3713 <<Done>>
3714 Rewrite (Cnode, Result);
fdac1f80
AC
3715 Analyze_And_Resolve (Cnode, Atyp);
3716
3717 exception
3718 when Concatenation_Error =>
76c597a1
AC
3719
3720 -- Kill warning generated for the declaration of the static out of
3721 -- range high bound, and instead generate a Constraint_Error with
3722 -- an appropriate specific message.
3723
3724 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3725 Apply_Compile_Time_Constraint_Error
3726 (N => Cnode,
324ac540 3727 Msg => "concatenation result upper bound out of range??",
76c597a1 3728 Reason => CE_Range_Check_Failed);
fdac1f80 3729 end Expand_Concatenate;
70482933 3730
f6194278
RD
3731 ---------------------------------------------------
3732 -- Expand_Membership_Minimize_Eliminate_Overflow --
3733 ---------------------------------------------------
3734
3735 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is
3736 pragma Assert (Nkind (N) = N_In);
3737 -- Despite the name, this routine applies only to N_In, not to
3738 -- N_Not_In. The latter is always rewritten as not (X in Y).
3739
71fb4dc8
AC
3740 Result_Type : constant Entity_Id := Etype (N);
3741 -- Capture result type, may be a derived boolean type
3742
b6b5cca8
AC
3743 Loc : constant Source_Ptr := Sloc (N);
3744 Lop : constant Node_Id := Left_Opnd (N);
3745 Rop : constant Node_Id := Right_Opnd (N);
3746
3747 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3748 -- is thus tempting to capture these values, but due to the rewrites
3749 -- that occur as a result of overflow checking, these values change
3750 -- as we go along, and it is safe just to always use Etype explicitly.
f6194278
RD
3751
3752 Restype : constant Entity_Id := Etype (N);
3753 -- Save result type
3754
3755 Lo, Hi : Uint;
d8192289 3756 -- Bounds in Minimize calls, not used currently
f6194278
RD
3757
3758 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
3759 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3760
3761 begin
a7f1b24f 3762 Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False);
f6194278
RD
3763
3764 -- If right operand is a subtype name, and the subtype name has no
3765 -- predicate, then we can just replace the right operand with an
3766 -- explicit range T'First .. T'Last, and use the explicit range code.
3767
b6b5cca8
AC
3768 if Nkind (Rop) /= N_Range
3769 and then No (Predicate_Function (Etype (Rop)))
3770 then
3771 declare
3772 Rtyp : constant Entity_Id := Etype (Rop);
3773 begin
3774 Rewrite (Rop,
3775 Make_Range (Loc,
3776 Low_Bound =>
3777 Make_Attribute_Reference (Loc,
3778 Attribute_Name => Name_First,
3779 Prefix => New_Reference_To (Rtyp, Loc)),
3780 High_Bound =>
3781 Make_Attribute_Reference (Loc,
3782 Attribute_Name => Name_Last,
3783 Prefix => New_Reference_To (Rtyp, Loc))));
3784 Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks);
3785 end;
f6194278
RD
3786 end if;
3787
3788 -- Here for the explicit range case. Note that the bounds of the range
3789 -- have not been processed for minimized or eliminated checks.
3790
3791 if Nkind (Rop) = N_Range then
a7f1b24f 3792 Minimize_Eliminate_Overflows
b6b5cca8 3793 (Low_Bound (Rop), Lo, Hi, Top_Level => False);
a7f1b24f 3794 Minimize_Eliminate_Overflows
c7e152b5 3795 (High_Bound (Rop), Lo, Hi, Top_Level => False);
f6194278
RD
3796
3797 -- We have A in B .. C, treated as A >= B and then A <= C
3798
3799 -- Bignum case
3800
b6b5cca8 3801 if Is_RTE (Etype (Lop), RE_Bignum)
f6194278
RD
3802 or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum)
3803 or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum)
3804 then
3805 declare
3806 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3807 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
71fb4dc8
AC
3808 L : constant Entity_Id :=
3809 Make_Defining_Identifier (Loc, Name_uL);
f6194278
RD
3810 Lopnd : constant Node_Id := Convert_To_Bignum (Lop);
3811 Lbound : constant Node_Id :=
3812 Convert_To_Bignum (Low_Bound (Rop));
3813 Hbound : constant Node_Id :=
3814 Convert_To_Bignum (High_Bound (Rop));
3815
71fb4dc8
AC
3816 -- Now we rewrite the membership test node to look like
3817
3818 -- do
3819 -- Bnn : Result_Type;
3820 -- declare
3821 -- M : Mark_Id := SS_Mark;
3822 -- L : Bignum := Lopnd;
3823 -- begin
3824 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3825 -- SS_Release (M);
3826 -- end;
3827 -- in
3828 -- Bnn
3829 -- end
f6194278
RD
3830
3831 begin
71fb4dc8
AC
3832 -- Insert declaration of L into declarations of bignum block
3833
f6194278
RD
3834 Insert_After
3835 (Last (Declarations (Blk)),
3836 Make_Object_Declaration (Loc,
71fb4dc8 3837 Defining_Identifier => L,
f6194278
RD
3838 Object_Definition =>
3839 New_Occurrence_Of (RTE (RE_Bignum), Loc),
3840 Expression => Lopnd));
3841
71fb4dc8
AC
3842 -- Insert assignment to Bnn into expressions of bignum block
3843
f6194278
RD
3844 Insert_Before
3845 (First (Statements (Handled_Statement_Sequence (Blk))),
3846 Make_Assignment_Statement (Loc,
3847 Name => New_Occurrence_Of (Bnn, Loc),
3848 Expression =>
3849 Make_And_Then (Loc,
3850 Left_Opnd =>
3851 Make_Function_Call (Loc,
3852 Name =>
3853 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
71fb4dc8
AC
3854 Parameter_Associations => New_List (
3855 New_Occurrence_Of (L, Loc),
3856 Lbound)),
f6194278
RD
3857 Right_Opnd =>
3858 Make_Function_Call (Loc,
3859 Name =>
71fb4dc8
AC
3860 New_Occurrence_Of (RTE (RE_Big_LE), Loc),
3861 Parameter_Associations => New_List (
3862 New_Occurrence_Of (L, Loc),
3863 Hbound)))));
f6194278 3864
71fb4dc8 3865 -- Now rewrite the node
f6194278 3866
71fb4dc8
AC
3867 Rewrite (N,
3868 Make_Expression_With_Actions (Loc,
3869 Actions => New_List (
3870 Make_Object_Declaration (Loc,
3871 Defining_Identifier => Bnn,
3872 Object_Definition =>
3873 New_Occurrence_Of (Result_Type, Loc)),
3874 Blk),
3875 Expression => New_Occurrence_Of (Bnn, Loc)));
3876 Analyze_And_Resolve (N, Result_Type);
f6194278
RD
3877 return;
3878 end;
3879
3880 -- Here if no bignums around
3881
3882 else
3883 -- Case where types are all the same
3884
b6b5cca8 3885 if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop)))
f6194278 3886 and then
b6b5cca8 3887 Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop)))
f6194278
RD
3888 then
3889 null;
3890
3891 -- If types are not all the same, it means that we have rewritten
3892 -- at least one of them to be of type Long_Long_Integer, and we
3893 -- will convert the other operands to Long_Long_Integer.
3894
3895 else
3896 Convert_To_And_Rewrite (LLIB, Lop);
71fb4dc8
AC
3897 Set_Analyzed (Lop, False);
3898 Analyze_And_Resolve (Lop, LLIB);
3899
3900 -- For the right operand, avoid unnecessary recursion into
3901 -- this routine, we know that overflow is not possible.
f6194278
RD
3902
3903 Convert_To_And_Rewrite (LLIB, Low_Bound (Rop));
3904 Convert_To_And_Rewrite (LLIB, High_Bound (Rop));
3905 Set_Analyzed (Rop, False);
71fb4dc8 3906 Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check);
f6194278
RD
3907 end if;
3908
3909 -- Now the three operands are of the same signed integer type,
b6b5cca8
AC
3910 -- so we can use the normal expansion routine for membership,
3911 -- setting the flag to prevent recursion into this procedure.
f6194278
RD
3912
3913 Set_No_Minimize_Eliminate (N);
3914 Expand_N_In (N);
3915 end if;
3916
3917 -- Right operand is a subtype name and the subtype has a predicate. We
f6636994
AC
3918 -- have to make sure the predicate is checked, and for that we need to
3919 -- use the standard N_In circuitry with appropriate types.
f6194278
RD
3920
3921 else
b6b5cca8 3922 pragma Assert (Present (Predicate_Function (Etype (Rop))));
f6194278
RD
3923
3924 -- If types are "right", just call Expand_N_In preventing recursion
3925
b6b5cca8 3926 if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then
f6194278
RD
3927 Set_No_Minimize_Eliminate (N);
3928 Expand_N_In (N);
3929
3930 -- Bignum case
3931
b6b5cca8 3932 elsif Is_RTE (Etype (Lop), RE_Bignum) then
f6194278 3933
71fb4dc8 3934 -- For X in T, we want to rewrite our node as
f6194278 3935
71fb4dc8
AC
3936 -- do
3937 -- Bnn : Result_Type;
f6194278 3938
71fb4dc8
AC
3939 -- declare
3940 -- M : Mark_Id := SS_Mark;
3941 -- Lnn : Long_Long_Integer'Base
3942 -- Nnn : Bignum;
f6194278 3943
71fb4dc8
AC
3944 -- begin
3945 -- Nnn := X;
3946
3947 -- if not Bignum_In_LLI_Range (Nnn) then
3948 -- Bnn := False;
3949 -- else
3950 -- Lnn := From_Bignum (Nnn);
3951 -- Bnn :=
3952 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3953 -- and then T'Base (Lnn) in T;
3954 -- end if;
f6194278 3955 --
71fb4dc8
AC
3956 -- SS_Release (M);
3957 -- end
3958 -- in
3959 -- Bnn
3960 -- end
f6194278 3961
f6636994 3962 -- A bit gruesome, but there doesn't seem to be a simpler way
f6194278
RD
3963
3964 declare
b6b5cca8
AC
3965 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3966 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3967 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N);
3968 Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N);
71fb4dc8
AC
3969 T : constant Entity_Id := Etype (Rop);
3970 TB : constant Entity_Id := Base_Type (T);
b6b5cca8 3971 Nin : Node_Id;
f6194278
RD
3972
3973 begin
71fb4dc8 3974 -- Mark the last membership operation to prevent recursion
f6194278
RD
3975
3976 Nin :=
3977 Make_In (Loc,
f6636994
AC
3978 Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)),
3979 Right_Opnd => New_Occurrence_Of (T, Loc));
f6194278
RD
3980 Set_No_Minimize_Eliminate (Nin);
3981
3982 -- Now decorate the block
3983
3984 Insert_After
3985 (Last (Declarations (Blk)),
3986 Make_Object_Declaration (Loc,
3987 Defining_Identifier => Lnn,
3988 Object_Definition => New_Occurrence_Of (LLIB, Loc)));
3989
3990 Insert_After
3991 (Last (Declarations (Blk)),
3992 Make_Object_Declaration (Loc,
3993 Defining_Identifier => Nnn,
3994 Object_Definition =>
3995 New_Occurrence_Of (RTE (RE_Bignum), Loc)));
3996
3997 Insert_List_Before
3998 (First (Statements (Handled_Statement_Sequence (Blk))),
3999 New_List (
4000 Make_Assignment_Statement (Loc,
4001 Name => New_Occurrence_Of (Nnn, Loc),
4002 Expression => Relocate_Node (Lop)),
4003
8b1011c0 4004 Make_Implicit_If_Statement (N,
f6194278 4005 Condition =>
71fb4dc8
AC
4006 Make_Op_Not (Loc,
4007 Right_Opnd =>
4008 Make_Function_Call (Loc,
4009 Name =>
4010 New_Occurrence_Of
4011 (RTE (RE_Bignum_In_LLI_Range), Loc),
4012 Parameter_Associations => New_List (
4013 New_Occurrence_Of (Nnn, Loc)))),
f6194278
RD
4014
4015 Then_Statements => New_List (
4016 Make_Assignment_Statement (Loc,
4017 Name => New_Occurrence_Of (Bnn, Loc),
4018 Expression =>
4019 New_Occurrence_Of (Standard_False, Loc))),
4020
4021 Else_Statements => New_List (
4022 Make_Assignment_Statement (Loc,
4023 Name => New_Occurrence_Of (Lnn, Loc),
4024 Expression =>
4025 Make_Function_Call (Loc,
4026 Name =>
4027 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
4028 Parameter_Associations => New_List (
4029 New_Occurrence_Of (Nnn, Loc)))),
4030
4031 Make_Assignment_Statement (Loc,
71fb4dc8 4032 Name => New_Occurrence_Of (Bnn, Loc),
f6194278
RD
4033 Expression =>
4034 Make_And_Then (Loc,
71fb4dc8 4035 Left_Opnd =>
f6194278 4036 Make_In (Loc,
71fb4dc8 4037 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
f6194278 4038 Right_Opnd =>
71fb4dc8
AC
4039 Make_Range (Loc,
4040 Low_Bound =>
4041 Convert_To (LLIB,
4042 Make_Attribute_Reference (Loc,
4043 Attribute_Name => Name_First,
4044 Prefix =>
4045 New_Occurrence_Of (TB, Loc))),
4046
4047 High_Bound =>
4048 Convert_To (LLIB,
4049 Make_Attribute_Reference (Loc,
4050 Attribute_Name => Name_Last,
4051 Prefix =>
4052 New_Occurrence_Of (TB, Loc))))),
4053
f6194278
RD
4054 Right_Opnd => Nin))))));
4055
71fb4dc8 4056 -- Now we can do the rewrite
f6194278 4057
71fb4dc8
AC
4058 Rewrite (N,
4059 Make_Expression_With_Actions (Loc,
4060 Actions => New_List (
4061 Make_Object_Declaration (Loc,
4062 Defining_Identifier => Bnn,
4063 Object_Definition =>
4064 New_Occurrence_Of (Result_Type, Loc)),
4065 Blk),
4066 Expression => New_Occurrence_Of (Bnn, Loc)));
4067 Analyze_And_Resolve (N, Result_Type);
f6194278
RD
4068 return;
4069 end;
4070
4071 -- Not bignum case, but types don't match (this means we rewrote the
b6b5cca8 4072 -- left operand to be Long_Long_Integer).
f6194278
RD
4073
4074 else
b6b5cca8 4075 pragma Assert (Base_Type (Etype (Lop)) = LLIB);
f6194278 4076
71fb4dc8
AC
4077 -- We rewrite the membership test as (where T is the type with
4078 -- the predicate, i.e. the type of the right operand)
f6194278 4079
71fb4dc8
AC
4080 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
4081 -- and then T'Base (Lop) in T
f6194278
RD
4082
4083 declare
71fb4dc8
AC
4084 T : constant Entity_Id := Etype (Rop);
4085 TB : constant Entity_Id := Base_Type (T);
f6194278
RD
4086 Nin : Node_Id;
4087
4088 begin
4089 -- The last membership test is marked to prevent recursion
4090
4091 Nin :=
4092 Make_In (Loc,
71fb4dc8
AC
4093 Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)),
4094 Right_Opnd => New_Occurrence_Of (T, Loc));
f6194278
RD
4095 Set_No_Minimize_Eliminate (Nin);
4096
4097 -- Now do the rewrite
4098
4099 Rewrite (N,
4100 Make_And_Then (Loc,
71fb4dc8 4101 Left_Opnd =>
f6194278
RD
4102 Make_In (Loc,
4103 Left_Opnd => Lop,
4104 Right_Opnd =>
71fb4dc8
AC
4105 Make_Range (Loc,
4106 Low_Bound =>
4107 Convert_To (LLIB,
4108 Make_Attribute_Reference (Loc,
4109 Attribute_Name => Name_First,
4110 Prefix => New_Occurrence_Of (TB, Loc))),
4111 High_Bound =>
4112 Convert_To (LLIB,
4113 Make_Attribute_Reference (Loc,
4114 Attribute_Name => Name_Last,
4115 Prefix => New_Occurrence_Of (TB, Loc))))),
f6194278 4116 Right_Opnd => Nin));
71fb4dc8
AC
4117 Set_Analyzed (N, False);
4118 Analyze_And_Resolve (N, Restype);
f6194278
RD
4119 end;
4120 end if;
4121 end if;
4122 end Expand_Membership_Minimize_Eliminate_Overflow;
4123
70482933
RK
4124 ------------------------
4125 -- Expand_N_Allocator --
4126 ------------------------
4127
4128 procedure Expand_N_Allocator (N : Node_Id) is
8b1011c0
AC
4129 Etyp : constant Entity_Id := Etype (Expression (N));
4130 Loc : constant Source_Ptr := Sloc (N);
4131 PtrT : constant Entity_Id := Etype (N);
70482933 4132
26bff3d9
JM
4133 procedure Rewrite_Coextension (N : Node_Id);
4134 -- Static coextensions have the same lifetime as the entity they
8fc789c8 4135 -- constrain. Such occurrences can be rewritten as aliased objects
26bff3d9 4136 -- and their unrestricted access used instead of the coextension.
0669bebe 4137
8aec446b 4138 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
507ed3fd
AC
4139 -- Given a constrained array type E, returns a node representing the
4140 -- code to compute the size in storage elements for the given type.
205c14b0 4141 -- This is done without using the attribute (which malfunctions for
507ed3fd 4142 -- large sizes ???)
8aec446b 4143
26bff3d9
JM
4144 -------------------------
4145 -- Rewrite_Coextension --
4146 -------------------------
4147
4148 procedure Rewrite_Coextension (N : Node_Id) is
e5a22243
AC
4149 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
4150 Temp_Decl : Node_Id;
26bff3d9 4151
df3e68b1 4152 begin
26bff3d9
JM
4153 -- Generate:
4154 -- Cnn : aliased Etyp;
4155
df3e68b1
HK
4156 Temp_Decl :=
4157 Make_Object_Declaration (Loc,
4158 Defining_Identifier => Temp_Id,
243cae0a
AC
4159 Aliased_Present => True,
4160 Object_Definition => New_Occurrence_Of (Etyp, Loc));
26bff3d9 4161
26bff3d9 4162 if Nkind (Expression (N)) = N_Qualified_Expression then
df3e68b1 4163 Set_Expression (Temp_Decl, Expression (Expression (N)));
0669bebe 4164 end if;
26bff3d9 4165
e5a22243 4166 Insert_Action (N, Temp_Decl);
26bff3d9
JM
4167 Rewrite (N,
4168 Make_Attribute_Reference (Loc,
243cae0a 4169 Prefix => New_Occurrence_Of (Temp_Id, Loc),
26bff3d9
JM
4170 Attribute_Name => Name_Unrestricted_Access));
4171
4172 Analyze_And_Resolve (N, PtrT);
4173 end Rewrite_Coextension;
0669bebe 4174
8aec446b
AC
4175 ------------------------------
4176 -- Size_In_Storage_Elements --
4177 ------------------------------
4178
4179 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
4180 begin
4181 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4182 -- However, the reason for the existence of this function is
4183 -- to construct a test for sizes too large, which means near the
4184 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4185 -- is that we get overflows when sizes are greater than 2**31.
4186
507ed3fd 4187 -- So what we end up doing for array types is to use the expression:
8aec446b
AC
4188
4189 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4190
46202729 4191 -- which avoids this problem. All this is a bit bogus, but it does
8aec446b
AC
4192 -- mean we catch common cases of trying to allocate arrays that
4193 -- are too large, and which in the absence of a check results in
4194 -- undetected chaos ???
4195
ce532f42
AC
4196 -- Note in particular that this is a pessimistic estimate in the
4197 -- case of packed array types, where an array element might occupy
4198 -- just a fraction of a storage element???
4199
507ed3fd
AC
4200 declare
4201 Len : Node_Id;
4202 Res : Node_Id;
8aec446b 4203
507ed3fd
AC
4204 begin
4205 for J in 1 .. Number_Dimensions (E) loop
4206 Len :=
4207 Make_Attribute_Reference (Loc,
4208 Prefix => New_Occurrence_Of (E, Loc),
4209 Attribute_Name => Name_Length,
243cae0a 4210 Expressions => New_List (Make_Integer_Literal (Loc, J)));
8aec446b 4211
507ed3fd
AC
4212 if J = 1 then
4213 Res := Len;
8aec446b 4214
507ed3fd
AC
4215 else
4216 Res :=
4217 Make_Op_Multiply (Loc,
4218 Left_Opnd => Res,
4219 Right_Opnd => Len);
4220 end if;
4221 end loop;
8aec446b 4222
8aec446b 4223 return
507ed3fd
AC
4224 Make_Op_Multiply (Loc,
4225 Left_Opnd => Len,
4226 Right_Opnd =>
4227 Make_Attribute_Reference (Loc,
4228 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
4229 Attribute_Name => Name_Max_Size_In_Storage_Elements));
4230 end;
8aec446b
AC
4231 end Size_In_Storage_Elements;
4232
8b1011c0
AC
4233 -- Local variables
4234
70861157 4235 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
8b1011c0
AC
4236 Desig : Entity_Id;
4237 Nod : Node_Id;
4238 Pool : Entity_Id;
4239 Rel_Typ : Entity_Id;
4240 Temp : Entity_Id;
4241
0669bebe
GB
4242 -- Start of processing for Expand_N_Allocator
4243
70482933
RK
4244 begin
4245 -- RM E.2.3(22). We enforce that the expected type of an allocator
4246 -- shall not be a remote access-to-class-wide-limited-private type
4247
4248 -- Why is this being done at expansion time, seems clearly wrong ???
4249
4250 Validate_Remote_Access_To_Class_Wide_Type (N);
4251
ca5af305
AC
4252 -- Processing for anonymous access-to-controlled types. These access
4253 -- types receive a special finalization master which appears in the
4254 -- declarations of the enclosing semantic unit. This expansion is done
84f4072a
JM
4255 -- now to ensure that any additional types generated by this routine or
4256 -- Expand_Allocator_Expression inherit the proper type attributes.
ca5af305 4257
84f4072a 4258 if (Ekind (PtrT) = E_Anonymous_Access_Type
533369aa 4259 or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT))))
ca5af305
AC
4260 and then Needs_Finalization (Dtyp)
4261 then
8b1011c0
AC
4262 -- Detect the allocation of an anonymous controlled object where the
4263 -- type of the context is named. For example:
4264
4265 -- procedure Proc (Ptr : Named_Access_Typ);
4266 -- Proc (new Designated_Typ);
4267
4268 -- Regardless of the anonymous-to-named access type conversion, the
4269 -- lifetime of the object must be associated with the named access
0088ba92 4270 -- type. Use the finalization-related attributes of this type.
8b1011c0
AC
4271
4272 if Nkind_In (Parent (N), N_Type_Conversion,
4273 N_Unchecked_Type_Conversion)
4274 and then Ekind_In (Etype (Parent (N)), E_Access_Subtype,
4275 E_Access_Type,
4276 E_General_Access_Type)
4277 then
4278 Rel_Typ := Etype (Parent (N));
4279 else
4280 Rel_Typ := Empty;
4281 end if;
4282
b254da66
AC
4283 -- Anonymous access-to-controlled types allocate on the global pool.
4284 -- Do not set this attribute on .NET/JVM since those targets do not
4285 -- support pools.
ca5af305 4286
bde73c6b 4287 if No (Associated_Storage_Pool (PtrT)) and then VM_Target = No_VM then
8b1011c0
AC
4288 if Present (Rel_Typ) then
4289 Set_Associated_Storage_Pool (PtrT,
4290 Associated_Storage_Pool (Rel_Typ));
4291 else
4292 Set_Associated_Storage_Pool (PtrT,
4293 Get_Global_Pool_For_Access_Type (PtrT));
4294 end if;
ca5af305
AC
4295 end if;
4296
4297 -- The finalization master must be inserted and analyzed as part of
2bfa5484 4298 -- the current semantic unit. This form of expansion is not carried
06b599fd 4299 -- out in SPARK mode because it is useless. Note that the master is
ad5a445d 4300 -- updated when analysis changes current units.
ca5af305 4301
06b599fd 4302 if not SPARK_Mode then
8b1011c0
AC
4303 if Present (Rel_Typ) then
4304 Set_Finalization_Master (PtrT, Finalization_Master (Rel_Typ));
4305 else
4306 Set_Finalization_Master (PtrT, Current_Anonymous_Master);
4307 end if;
ca5af305
AC
4308 end if;
4309 end if;
4310
4311 -- Set the storage pool and find the appropriate version of Allocate to
8417f4b2
AC
4312 -- call. Do not overwrite the storage pool if it is already set, which
4313 -- can happen for build-in-place function returns (see
200b7162 4314 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
70482933 4315
200b7162
BD
4316 if No (Storage_Pool (N)) then
4317 Pool := Associated_Storage_Pool (Root_Type (PtrT));
70482933 4318
200b7162
BD
4319 if Present (Pool) then
4320 Set_Storage_Pool (N, Pool);
fbf5a39b 4321
200b7162
BD
4322 if Is_RTE (Pool, RE_SS_Pool) then
4323 if VM_Target = No_VM then
4324 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4325 end if;
fbf5a39b 4326
a8551b5f
AC
4327 -- In the case of an allocator for a simple storage pool, locate
4328 -- and save a reference to the pool type's Allocate routine.
4329
4330 elsif Present (Get_Rep_Pragma
f6205414 4331 (Etype (Pool), Name_Simple_Storage_Pool_Type))
a8551b5f
AC
4332 then
4333 declare
a8551b5f 4334 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
260359e3 4335 Alloc_Op : Entity_Id;
a8551b5f 4336 begin
260359e3 4337 Alloc_Op := Get_Name_Entity_Id (Name_Allocate);
a8551b5f
AC
4338 while Present (Alloc_Op) loop
4339 if Scope (Alloc_Op) = Scope (Pool_Type)
4340 and then Present (First_Formal (Alloc_Op))
4341 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4342 then
4343 Set_Procedure_To_Call (N, Alloc_Op);
a8551b5f 4344 exit;
260359e3
AC
4345 else
4346 Alloc_Op := Homonym (Alloc_Op);
a8551b5f 4347 end if;
a8551b5f
AC
4348 end loop;
4349 end;
4350
200b7162
BD
4351 elsif Is_Class_Wide_Type (Etype (Pool)) then
4352 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
4353
4354 else
4355 Set_Procedure_To_Call (N,
4356 Find_Prim_Op (Etype (Pool), Name_Allocate));
4357 end if;
70482933
RK
4358 end if;
4359 end if;
4360
685094bf
RD
4361 -- Under certain circumstances we can replace an allocator by an access
4362 -- to statically allocated storage. The conditions, as noted in AARM
4363 -- 3.10 (10c) are as follows:
70482933
RK
4364
4365 -- Size and initial value is known at compile time
4366 -- Access type is access-to-constant
4367
fbf5a39b
AC
4368 -- The allocator is not part of a constraint on a record component,
4369 -- because in that case the inserted actions are delayed until the
4370 -- record declaration is fully analyzed, which is too late for the
4371 -- analysis of the rewritten allocator.
4372
70482933
RK
4373 if Is_Access_Constant (PtrT)
4374 and then Nkind (Expression (N)) = N_Qualified_Expression
4375 and then Compile_Time_Known_Value (Expression (Expression (N)))
243cae0a
AC
4376 and then Size_Known_At_Compile_Time
4377 (Etype (Expression (Expression (N))))
fbf5a39b 4378 and then not Is_Record_Type (Current_Scope)
70482933
RK
4379 then
4380 -- Here we can do the optimization. For the allocator
4381
4382 -- new x'(y)
4383
4384 -- We insert an object declaration
4385
4386 -- Tnn : aliased x := y;
4387
685094bf
RD
4388 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4389 -- marked as requiring static allocation.
70482933 4390
df3e68b1 4391 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
70482933
RK
4392 Desig := Subtype_Mark (Expression (N));
4393
4394 -- If context is constrained, use constrained subtype directly,
8fc789c8 4395 -- so that the constant is not labelled as having a nominally
70482933
RK
4396 -- unconstrained subtype.
4397
0da2c8ac
AC
4398 if Entity (Desig) = Base_Type (Dtyp) then
4399 Desig := New_Occurrence_Of (Dtyp, Loc);
70482933
RK
4400 end if;
4401
4402 Insert_Action (N,
4403 Make_Object_Declaration (Loc,
4404 Defining_Identifier => Temp,
4405 Aliased_Present => True,
4406 Constant_Present => Is_Access_Constant (PtrT),
4407 Object_Definition => Desig,
4408 Expression => Expression (Expression (N))));
4409
4410 Rewrite (N,
4411 Make_Attribute_Reference (Loc,
243cae0a 4412 Prefix => New_Occurrence_Of (Temp, Loc),
70482933
RK
4413 Attribute_Name => Name_Unrestricted_Access));
4414
4415 Analyze_And_Resolve (N, PtrT);
4416
685094bf
RD
4417 -- We set the variable as statically allocated, since we don't want
4418 -- it going on the stack of the current procedure!
70482933
RK
4419
4420 Set_Is_Statically_Allocated (Temp);
4421 return;
4422 end if;
4423
0669bebe
GB
4424 -- Same if the allocator is an access discriminant for a local object:
4425 -- instead of an allocator we create a local value and constrain the
308e6f3a 4426 -- enclosing object with the corresponding access attribute.
0669bebe 4427
26bff3d9
JM
4428 if Is_Static_Coextension (N) then
4429 Rewrite_Coextension (N);
0669bebe
GB
4430 return;
4431 end if;
4432
8aec446b
AC
4433 -- Check for size too large, we do this because the back end misses
4434 -- proper checks here and can generate rubbish allocation calls when
4435 -- we are near the limit. We only do this for the 32-bit address case
4436 -- since that is from a practical point of view where we see a problem.
4437
4438 if System_Address_Size = 32
4439 and then not Storage_Checks_Suppressed (PtrT)
4440 and then not Storage_Checks_Suppressed (Dtyp)
4441 and then not Storage_Checks_Suppressed (Etyp)
4442 then
4443 -- The check we want to generate should look like
4444
4445 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4446 -- raise Storage_Error;
4447 -- end if;
4448
308e6f3a 4449 -- where 3.5 gigabytes is a constant large enough to accommodate any
507ed3fd
AC
4450 -- reasonable request for. But we can't do it this way because at
4451 -- least at the moment we don't compute this attribute right, and
4452 -- can silently give wrong results when the result gets large. Since
4453 -- this is all about large results, that's bad, so instead we only
205c14b0 4454 -- apply the check for constrained arrays, and manually compute the
507ed3fd 4455 -- value of the attribute ???
8aec446b 4456
507ed3fd
AC
4457 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
4458 Insert_Action (N,
4459 Make_Raise_Storage_Error (Loc,
4460 Condition =>
4461 Make_Op_Gt (Loc,
4462 Left_Opnd => Size_In_Storage_Elements (Etyp),
4463 Right_Opnd =>
243cae0a 4464 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
507ed3fd
AC
4465 Reason => SE_Object_Too_Large));
4466 end if;
8aec446b
AC
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
4493 -- for details). In addition, if the type T is a task T, then the
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
deb8dacc
HK
4511 -- Allocate if the context requires it. Since .NET/JVM compilers
4512 -- do not support pools, this step is skipped.
df3e68b1 4513
deb8dacc 4514 if VM_Target = No_VM
d3f70b35 4515 and then Present (Finalization_Master (PtrT))
deb8dacc 4516 then
df3e68b1 4517 Build_Allocate_Deallocate_Proc
ca5af305 4518 (N => N,
df3e68b1
HK
4519 Is_Allocate => True);
4520 end if;
70482933 4521
26bff3d9 4522 -- Case of no initialization procedure present
70482933 4523
26bff3d9 4524 elsif not Has_Non_Null_Base_Init_Proc (T) then
70482933 4525
26bff3d9 4526 -- Case of simple initialization required
70482933 4527
26bff3d9 4528 if Needs_Simple_Initialization (T) then
b4592168 4529 Check_Restriction (No_Default_Initialization, N);
26bff3d9
JM
4530 Rewrite (Expression (N),
4531 Make_Qualified_Expression (Loc,
4532 Subtype_Mark => New_Occurrence_Of (T, Loc),
b4592168 4533 Expression => Get_Simple_Init_Val (T, N)));
70482933 4534
26bff3d9
JM
4535 Analyze_And_Resolve (Expression (Expression (N)), T);
4536 Analyze_And_Resolve (Expression (N), T);
4537 Set_Paren_Count (Expression (Expression (N)), 1);
4538 Expand_N_Allocator (N);
70482933 4539
26bff3d9 4540 -- No initialization required
70482933
RK
4541
4542 else
26bff3d9
JM
4543 null;
4544 end if;
70482933 4545
26bff3d9 4546 -- Case of initialization procedure present, must be called
70482933 4547
26bff3d9 4548 else
b4592168 4549 Check_Restriction (No_Default_Initialization, N);
70482933 4550
b4592168
GD
4551 if not Restriction_Active (No_Default_Initialization) then
4552 Init := Base_Init_Proc (T);
4553 Nod := N;
191fcb3a 4554 Temp := Make_Temporary (Loc, 'P');
70482933 4555
b4592168 4556 -- Construct argument list for the initialization routine call
70482933 4557
df3e68b1 4558 Init_Arg1 :=
b4592168 4559 Make_Explicit_Dereference (Loc,
df3e68b1
HK
4560 Prefix =>
4561 New_Reference_To (Temp, Loc));
4562
4563 Set_Assignment_OK (Init_Arg1);
b4592168 4564 Temp_Type := PtrT;
26bff3d9 4565
b4592168
GD
4566 -- The initialization procedure expects a specific type. if the
4567 -- context is access to class wide, indicate that the object
4568 -- being allocated has the right specific type.
70482933 4569
b4592168 4570 if Is_Class_Wide_Type (Dtyp) then
df3e68b1 4571 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
b4592168 4572 end if;
70482933 4573
b4592168
GD
4574 -- If designated type is a concurrent type or if it is private
4575 -- type whose definition is a concurrent type, the first
4576 -- argument in the Init routine has to be unchecked conversion
4577 -- to the corresponding record type. If the designated type is
243cae0a 4578 -- a derived type, also convert the argument to its root type.
20b5d666 4579
b4592168 4580 if Is_Concurrent_Type (T) then
df3e68b1
HK
4581 Init_Arg1 :=
4582 Unchecked_Convert_To (
4583 Corresponding_Record_Type (T), Init_Arg1);
70482933 4584
b4592168
GD
4585 elsif Is_Private_Type (T)
4586 and then Present (Full_View (T))
4587 and then Is_Concurrent_Type (Full_View (T))
4588 then
df3e68b1 4589 Init_Arg1 :=
b4592168 4590 Unchecked_Convert_To
df3e68b1 4591 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
70482933 4592
b4592168
GD
4593 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
4594 declare
4595 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
df3e68b1 4596
b4592168 4597 begin
df3e68b1
HK
4598 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
4599 Set_Etype (Init_Arg1, Ftyp);
b4592168
GD
4600 end;
4601 end if;
70482933 4602
df3e68b1 4603 Args := New_List (Init_Arg1);
70482933 4604
b4592168
GD
4605 -- For the task case, pass the Master_Id of the access type as
4606 -- the value of the _Master parameter, and _Chain as the value
4607 -- of the _Chain parameter (_Chain will be defined as part of
4608 -- the generated code for the allocator).
70482933 4609
b4592168
GD
4610 -- In Ada 2005, the context may be a function that returns an
4611 -- anonymous access type. In that case the Master_Id has been
4612 -- created when expanding the function declaration.
70482933 4613
b4592168
GD
4614 if Has_Task (T) then
4615 if No (Master_Id (Base_Type (PtrT))) then
70482933 4616
b4592168
GD
4617 -- The designated type was an incomplete type, and the
4618 -- access type did not get expanded. Salvage it now.
70482933 4619
b941ae65 4620 if not Restriction_Active (No_Task_Hierarchy) then
3d67b239
AC
4621 if Present (Parent (Base_Type (PtrT))) then
4622 Expand_N_Full_Type_Declaration
4623 (Parent (Base_Type (PtrT)));
4624
0d5fbf52
AC
4625 -- The only other possibility is an itype. For this
4626 -- case, the master must exist in the context. This is
4627 -- the case when the allocator initializes an access
4628 -- component in an init-proc.
3d67b239 4629
0d5fbf52 4630 else
3d67b239
AC
4631 pragma Assert (Is_Itype (PtrT));
4632 Build_Master_Renaming (PtrT, N);
4633 end if;
b941ae65 4634 end if;
b4592168 4635 end if;
70482933 4636
b4592168
GD
4637 -- If the context of the allocator is a declaration or an
4638 -- assignment, we can generate a meaningful image for it,
4639 -- even though subsequent assignments might remove the
4640 -- connection between task and entity. We build this image
4641 -- when the left-hand side is a simple variable, a simple
4642 -- indexed assignment or a simple selected component.
4643
4644 if Nkind (Parent (N)) = N_Assignment_Statement then
4645 declare
4646 Nam : constant Node_Id := Name (Parent (N));
4647
4648 begin
4649 if Is_Entity_Name (Nam) then
4650 Decls :=
4651 Build_Task_Image_Decls
4652 (Loc,
4653 New_Occurrence_Of
4654 (Entity (Nam), Sloc (Nam)), T);
4655
243cae0a
AC
4656 elsif Nkind_In (Nam, N_Indexed_Component,
4657 N_Selected_Component)
b4592168
GD
4658 and then Is_Entity_Name (Prefix (Nam))
4659 then
4660 Decls :=
4661 Build_Task_Image_Decls
4662 (Loc, Nam, Etype (Prefix (Nam)));
4663 else
4664 Decls := Build_Task_Image_Decls (Loc, T, T);
4665 end if;
4666 end;
70482933 4667
b4592168
GD
4668 elsif Nkind (Parent (N)) = N_Object_Declaration then
4669 Decls :=
4670 Build_Task_Image_Decls
4671 (Loc, Defining_Identifier (Parent (N)), T);
70482933 4672
b4592168
GD
4673 else
4674 Decls := Build_Task_Image_Decls (Loc, T, T);
4675 end if;
26bff3d9 4676
87dc09cb 4677 if Restriction_Active (No_Task_Hierarchy) then
3c1ecd7e
AC
4678 Append_To (Args,
4679 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
87dc09cb
AC
4680 else
4681 Append_To (Args,
4682 New_Reference_To
4683 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
4684 end if;
4685
b4592168 4686 Append_To (Args, Make_Identifier (Loc, Name_uChain));
26bff3d9 4687
b4592168
GD
4688 Decl := Last (Decls);
4689 Append_To (Args,
4690 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
26bff3d9 4691
87dc09cb 4692 -- Has_Task is false, Decls not used
26bff3d9 4693
b4592168
GD
4694 else
4695 Decls := No_List;
26bff3d9
JM
4696 end if;
4697
b4592168
GD
4698 -- Add discriminants if discriminated type
4699
4700 declare
4701 Dis : Boolean := False;
4702 Typ : Entity_Id;
4703
4704 begin
4705 if Has_Discriminants (T) then
4706 Dis := True;
4707 Typ := T;
4708
4709 elsif Is_Private_Type (T)
4710 and then Present (Full_View (T))
4711 and then Has_Discriminants (Full_View (T))
20b5d666 4712 then
b4592168
GD
4713 Dis := True;
4714 Typ := Full_View (T);
20b5d666 4715 end if;
70482933 4716
b4592168 4717 if Dis then
26bff3d9 4718
b4592168 4719 -- If the allocated object will be constrained by the
685094bf
RD
4720 -- default values for discriminants, then build a subtype
4721 -- with those defaults, and change the allocated subtype
4722 -- to that. Note that this happens in fewer cases in Ada
4723 -- 2005 (AI-363).
26bff3d9 4724
b4592168
GD
4725 if not Is_Constrained (Typ)
4726 and then Present (Discriminant_Default_Value
df3e68b1 4727 (First_Discriminant (Typ)))
0791fbe9 4728 and then (Ada_Version < Ada_2005
cc96a1b8 4729 or else not
0fbcb11c
ES
4730 Object_Type_Has_Constrained_Partial_View
4731 (Typ, Current_Scope))
20b5d666 4732 then
b4592168
GD
4733 Typ := Build_Default_Subtype (Typ, N);
4734 Set_Expression (N, New_Reference_To (Typ, Loc));
20b5d666
JM
4735 end if;
4736
b4592168
GD
4737 Discr := First_Elmt (Discriminant_Constraint (Typ));
4738 while Present (Discr) loop
4739 Nod := Node (Discr);
4740 Append (New_Copy_Tree (Node (Discr)), Args);
20b5d666 4741
b4592168
GD
4742 -- AI-416: when the discriminant constraint is an
4743 -- anonymous access type make sure an accessibility
4744 -- check is inserted if necessary (3.10.2(22.q/2))
20b5d666 4745
0791fbe9 4746 if Ada_Version >= Ada_2005
b4592168
GD
4747 and then
4748 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
4749 then
e84e11ba
GD
4750 Apply_Accessibility_Check
4751 (Nod, Typ, Insert_Node => Nod);
b4592168 4752 end if;
20b5d666 4753
b4592168
GD
4754 Next_Elmt (Discr);
4755 end loop;
4756 end if;
4757 end;
70482933 4758
4b985e20 4759 -- We set the allocator as analyzed so that when we analyze
9b16cb57
RD
4760 -- the if expression node, we do not get an unwanted recursive
4761 -- expansion of the allocator expression.
70482933 4762
b4592168
GD
4763 Set_Analyzed (N, True);
4764 Nod := Relocate_Node (N);
70482933 4765
b4592168 4766 -- Here is the transformation:
ca5af305
AC
4767 -- input: new Ctrl_Typ
4768 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4769 -- Ctrl_TypIP (Temp.all, ...);
4770 -- [Deep_]Initialize (Temp.all);
70482933 4771
ca5af305
AC
4772 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4773 -- is the subtype of the allocator.
70482933 4774
b4592168
GD
4775 Temp_Decl :=
4776 Make_Object_Declaration (Loc,
4777 Defining_Identifier => Temp,
4778 Constant_Present => True,
4779 Object_Definition => New_Reference_To (Temp_Type, Loc),
4780 Expression => Nod);
70482933 4781
b4592168
GD
4782 Set_Assignment_OK (Temp_Decl);
4783 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
70482933 4784
ca5af305 4785 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
df3e68b1 4786
b4592168
GD
4787 -- If the designated type is a task type or contains tasks,
4788 -- create block to activate created tasks, and insert
4789 -- declaration for Task_Image variable ahead of call.
70482933 4790
b4592168
GD
4791 if Has_Task (T) then
4792 declare
4793 L : constant List_Id := New_List;
4794 Blk : Node_Id;
4795 begin
4796 Build_Task_Allocate_Block (L, Nod, Args);
4797 Blk := Last (L);
4798 Insert_List_Before (First (Declarations (Blk)), Decls);
4799 Insert_Actions (N, L);
4800 end;
70482933 4801
b4592168
GD
4802 else
4803 Insert_Action (N,
4804 Make_Procedure_Call_Statement (Loc,
243cae0a 4805 Name => New_Reference_To (Init, Loc),
b4592168
GD
4806 Parameter_Associations => Args));
4807 end if;
70482933 4808
048e5cef 4809 if Needs_Finalization (T) then
70482933 4810
df3e68b1
HK
4811 -- Generate:
4812 -- [Deep_]Initialize (Init_Arg1);
70482933 4813
df3e68b1 4814 Insert_Action (N,
243cae0a
AC
4815 Make_Init_Call
4816 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4817 Typ => T));
b4592168 4818
b254da66 4819 if Present (Finalization_Master (PtrT)) then
deb8dacc 4820
b254da66
AC
4821 -- Special processing for .NET/JVM, the allocated object
4822 -- is attached to the finalization master. Generate:
deb8dacc 4823
b254da66 4824 -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1));
deb8dacc 4825
b254da66
AC
4826 -- Types derived from [Limited_]Controlled are the only
4827 -- ones considered since they have fields Prev and Next.
4828
e0c32166
AC
4829 if VM_Target /= No_VM then
4830 if Is_Controlled (T) then
4831 Insert_Action (N,
4832 Make_Attach_Call
4833 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4834 Ptr_Typ => PtrT));
4835 end if;
b254da66
AC
4836
4837 -- Default case, generate:
4838
4839 -- Set_Finalize_Address
4840 -- (<PtrT>FM, <T>FD'Unrestricted_Access);
4841
2bfa5484
HK
4842 -- Do not generate this call in the following cases:
4843 --
06b599fd 4844 -- * SPARK mode - the call is useless and results in
2bfa5484
HK
4845 -- unwanted expansion.
4846 --
4847 -- * CodePeer mode - TSS primitive Finalize_Address is
4848 -- not created in this mode.
b254da66 4849
06b599fd 4850 elsif not (SPARK_Mode or CodePeer_Mode) then
b254da66
AC
4851 Insert_Action (N,
4852 Make_Set_Finalize_Address_Call
4853 (Loc => Loc,
4854 Typ => T,
4855 Ptr_Typ => PtrT));
4856 end if;
b4592168 4857 end if;
70482933
RK
4858 end if;
4859
b4592168
GD
4860 Rewrite (N, New_Reference_To (Temp, Loc));
4861 Analyze_And_Resolve (N, PtrT);
4862 end if;
26bff3d9
JM
4863 end if;
4864 end;
f82944b7 4865
26bff3d9
JM
4866 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4867 -- object that has been rewritten as a reference, we displace "this"
4868 -- to reference properly its secondary dispatch table.
4869
533369aa 4870 if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then
26bff3d9 4871 Displace_Allocator_Pointer (N);
f82944b7
JM
4872 end if;
4873
fbf5a39b
AC
4874 exception
4875 when RE_Not_Available =>
4876 return;
70482933
RK
4877 end Expand_N_Allocator;
4878
4879 -----------------------
4880 -- Expand_N_And_Then --
4881 -----------------------
4882
5875f8d6
AC
4883 procedure Expand_N_And_Then (N : Node_Id)
4884 renames Expand_Short_Circuit_Operator;
70482933 4885
19d846a0
RD
4886 ------------------------------
4887 -- Expand_N_Case_Expression --
4888 ------------------------------
4889
4890 procedure Expand_N_Case_Expression (N : Node_Id) is
4891 Loc : constant Source_Ptr := Sloc (N);
4892 Typ : constant Entity_Id := Etype (N);
4893 Cstmt : Node_Id;
27a8f150 4894 Decl : Node_Id;
19d846a0
RD
4895 Tnn : Entity_Id;
4896 Pnn : Entity_Id;
4897 Actions : List_Id;
4898 Ttyp : Entity_Id;
4899 Alt : Node_Id;
4900 Fexp : Node_Id;
4901
4902 begin
b6b5cca8
AC
4903 -- Check for MINIMIZED/ELIMINATED overflow mode
4904
4905 if Minimized_Eliminated_Overflow_Check (N) then
4b1c4f20
RD
4906 Apply_Arithmetic_Overflow_Check (N);
4907 return;
4908 end if;
4909
19d846a0
RD
4910 -- We expand
4911
4912 -- case X is when A => AX, when B => BX ...
4913
4914 -- to
4915
4916 -- do
4917 -- Tnn : typ;
4918 -- case X is
4919 -- when A =>
4920 -- Tnn := AX;
4921 -- when B =>
4922 -- Tnn := BX;
4923 -- ...
4924 -- end case;
4925 -- in Tnn end;
4926
4927 -- However, this expansion is wrong for limited types, and also
4928 -- wrong for unconstrained types (since the bounds may not be the
4929 -- same in all branches). Furthermore it involves an extra copy
4930 -- for large objects. So we take care of this by using the following
2492305b 4931 -- modified expansion for non-elementary types:
19d846a0
RD
4932
4933 -- do
4934 -- type Pnn is access all typ;
4935 -- Tnn : Pnn;
4936 -- case X is
4937 -- when A =>
4938 -- T := AX'Unrestricted_Access;
4939 -- when B =>
4940 -- T := BX'Unrestricted_Access;
4941 -- ...
4942 -- end case;
4943 -- in Tnn.all end;
4944
4945 Cstmt :=
4946 Make_Case_Statement (Loc,
4947 Expression => Expression (N),
4948 Alternatives => New_List);
4949
4950 Actions := New_List;
4951
4952 -- Scalar case
4953
2492305b 4954 if Is_Elementary_Type (Typ) then
19d846a0
RD
4955 Ttyp := Typ;
4956
4957 else
4958 Pnn := Make_Temporary (Loc, 'P');
4959 Append_To (Actions,
4960 Make_Full_Type_Declaration (Loc,
4961 Defining_Identifier => Pnn,
4962 Type_Definition =>
4963 Make_Access_To_Object_Definition (Loc,
4964 All_Present => True,
4965 Subtype_Indication =>
4966 New_Reference_To (Typ, Loc))));
4967 Ttyp := Pnn;
4968 end if;
4969
4970 Tnn := Make_Temporary (Loc, 'T');
27a8f150
AC
4971
4972 -- Create declaration for target of expression, and indicate that it
4973 -- does not require initialization.
4974
4975 Decl := Make_Object_Declaration (Loc,
19d846a0 4976 Defining_Identifier => Tnn,
27a8f150
AC
4977 Object_Definition => New_Occurrence_Of (Ttyp, Loc));
4978 Set_No_Initialization (Decl);
4979 Append_To (Actions, Decl);
19d846a0
RD
4980
4981 -- Now process the alternatives
4982
4983 Alt := First (Alternatives (N));
4984 while Present (Alt) loop
4985 declare
eaed0c37
AC
4986 Aexp : Node_Id := Expression (Alt);
4987 Aloc : constant Source_Ptr := Sloc (Aexp);
4988 Stats : List_Id;
19d846a0
RD
4989
4990 begin
eaed0c37
AC
4991 -- As described above, take Unrestricted_Access for case of non-
4992 -- scalar types, to avoid big copies, and special cases.
05dbd302 4993
2492305b 4994 if not Is_Elementary_Type (Typ) then
19d846a0
RD
4995 Aexp :=
4996 Make_Attribute_Reference (Aloc,
4997 Prefix => Relocate_Node (Aexp),
4998 Attribute_Name => Name_Unrestricted_Access);
4999 end if;
5000
eaed0c37
AC
5001 Stats := New_List (
5002 Make_Assignment_Statement (Aloc,
5003 Name => New_Occurrence_Of (Tnn, Loc),
5004 Expression => Aexp));
5005
5006 -- Propagate declarations inserted in the node by Insert_Actions
5007 -- (for example, temporaries generated to remove side effects).
5008 -- These actions must remain attached to the alternative, given
5009 -- that they are generated by the corresponding expression.
5010
5011 if Present (Sinfo.Actions (Alt)) then
5012 Prepend_List (Sinfo.Actions (Alt), Stats);
5013 end if;
5014
19d846a0
RD
5015 Append_To
5016 (Alternatives (Cstmt),
5017 Make_Case_Statement_Alternative (Sloc (Alt),
5018 Discrete_Choices => Discrete_Choices (Alt),
eaed0c37 5019 Statements => Stats));
19d846a0
RD
5020 end;
5021
5022 Next (Alt);
5023 end loop;
5024
5025 Append_To (Actions, Cstmt);
5026
5027 -- Construct and return final expression with actions
5028
2492305b 5029 if Is_Elementary_Type (Typ) then
19d846a0
RD
5030 Fexp := New_Occurrence_Of (Tnn, Loc);
5031 else
5032 Fexp :=
5033 Make_Explicit_Dereference (Loc,
5034 Prefix => New_Occurrence_Of (Tnn, Loc));
5035 end if;
5036
5037 Rewrite (N,
5038 Make_Expression_With_Actions (Loc,
5039 Expression => Fexp,
5040 Actions => Actions));
5041
5042 Analyze_And_Resolve (N, Typ);
5043 end Expand_N_Case_Expression;
5044
9b16cb57
RD
5045 -----------------------------------
5046 -- Expand_N_Explicit_Dereference --
5047 -----------------------------------
5048
5049 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
5050 begin
5051 -- Insert explicit dereference call for the checked storage pool case
5052
5053 Insert_Dereference_Action (Prefix (N));
5054
5055 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5056 -- we set the atomic sync flag.
5057
5058 if Is_Atomic (Etype (N))
5059 and then not Atomic_Synchronization_Disabled (Etype (N))
5060 then
5061 Activate_Atomic_Synchronization (N);
5062 end if;
5063 end Expand_N_Explicit_Dereference;
5064
5065 --------------------------------------
5066 -- Expand_N_Expression_With_Actions --
5067 --------------------------------------
5068
5069 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
4c7e0990 5070 function Process_Action (Act : Node_Id) return Traverse_Result;
b2c28399
AC
5071 -- Inspect and process a single action of an expression_with_actions for
5072 -- transient controlled objects. If such objects are found, the routine
5073 -- generates code to clean them up when the context of the expression is
5074 -- evaluated or elaborated.
9b16cb57 5075
4c7e0990
AC
5076 --------------------
5077 -- Process_Action --
5078 --------------------
5079
5080 function Process_Action (Act : Node_Id) return Traverse_Result is
4c7e0990
AC
5081 begin
5082 if Nkind (Act) = N_Object_Declaration
5083 and then Is_Finalizable_Transient (Act, N)
5084 then
b2c28399
AC
5085 Process_Transient_Object (Act, N);
5086 return Abandon;
9b16cb57 5087
4c7e0990
AC
5088 -- Avoid processing temporary function results multiple times when
5089 -- dealing with nested expression_with_actions.
9b16cb57 5090
4c7e0990
AC
5091 elsif Nkind (Act) = N_Expression_With_Actions then
5092 return Abandon;
5093
b2c28399
AC
5094 -- Do not process temporary function results in loops. This is done
5095 -- by Expand_N_Loop_Statement and Build_Finalizer.
4c7e0990
AC
5096
5097 elsif Nkind (Act) = N_Loop_Statement then
5098 return Abandon;
9b16cb57
RD
5099 end if;
5100
4c7e0990
AC
5101 return OK;
5102 end Process_Action;
9b16cb57 5103
4c7e0990 5104 procedure Process_Single_Action is new Traverse_Proc (Process_Action);
9b16cb57
RD
5105
5106 -- Local variables
5107
4c7e0990 5108 Act : Node_Id;
9b16cb57
RD
5109
5110 -- Start of processing for Expand_N_Expression_With_Actions
5111
5112 begin
4c7e0990
AC
5113 Act := First (Actions (N));
5114 while Present (Act) loop
5115 Process_Single_Action (Act);
9b16cb57 5116
4c7e0990 5117 Next (Act);
9b16cb57
RD
5118 end loop;
5119 end Expand_N_Expression_With_Actions;
5120
5121 ----------------------------
5122 -- Expand_N_If_Expression --
5123 ----------------------------
70482933 5124
4b985e20 5125 -- Deal with limited types and condition actions
70482933 5126
9b16cb57 5127 procedure Expand_N_If_Expression (N : Node_Id) is
b2c28399
AC
5128 procedure Process_Actions (Actions : List_Id);
5129 -- Inspect and process a single action list of an if expression for
5130 -- transient controlled objects. If such objects are found, the routine
5131 -- generates code to clean them up when the context of the expression is
5132 -- evaluated or elaborated.
3cebd1c0 5133
b2c28399
AC
5134 ---------------------
5135 -- Process_Actions --
5136 ---------------------
3cebd1c0 5137
b2c28399
AC
5138 procedure Process_Actions (Actions : List_Id) is
5139 Act : Node_Id;
3cebd1c0
AC
5140
5141 begin
b2c28399
AC
5142 Act := First (Actions);
5143 while Present (Act) loop
5144 if Nkind (Act) = N_Object_Declaration
5145 and then Is_Finalizable_Transient (Act, N)
5146 then
5147 Process_Transient_Object (Act, N);
5148 end if;
3cebd1c0 5149
b2c28399
AC
5150 Next (Act);
5151 end loop;
5152 end Process_Actions;
3cebd1c0
AC
5153
5154 -- Local variables
5155
70482933
RK
5156 Loc : constant Source_Ptr := Sloc (N);
5157 Cond : constant Node_Id := First (Expressions (N));
5158 Thenx : constant Node_Id := Next (Cond);
5159 Elsex : constant Node_Id := Next (Thenx);
5160 Typ : constant Entity_Id := Etype (N);
c471e2da 5161
3cebd1c0 5162 Actions : List_Id;
602a7ec0
AC
5163 Cnn : Entity_Id;
5164 Decl : Node_Id;
3cebd1c0 5165 Expr : Node_Id;
602a7ec0
AC
5166 New_If : Node_Id;
5167 New_N : Node_Id;
b2c28399 5168 Ptr_Typ : Entity_Id;
70482933 5169
a53c5613
AC
5170 -- Start of processing for Expand_N_If_Expression
5171
70482933 5172 begin
b6b5cca8
AC
5173 -- Check for MINIMIZED/ELIMINATED overflow mode
5174
5175 if Minimized_Eliminated_Overflow_Check (N) then
5176 Apply_Arithmetic_Overflow_Check (N);
5177 return;
5178 end if;
5179
602a7ec0 5180 -- Fold at compile time if condition known. We have already folded
9b16cb57
RD
5181 -- static if expressions, but it is possible to fold any case in which
5182 -- the condition is known at compile time, even though the result is
5183 -- non-static.
602a7ec0
AC
5184
5185 -- Note that we don't do the fold of such cases in Sem_Elab because
5186 -- it can cause infinite loops with the expander adding a conditional
5187 -- expression, and Sem_Elab circuitry removing it repeatedly.
5188
5189 if Compile_Time_Known_Value (Cond) then
5190 if Is_True (Expr_Value (Cond)) then
5191 Expr := Thenx;
5192 Actions := Then_Actions (N);
5193 else
5194 Expr := Elsex;
5195 Actions := Else_Actions (N);
5196 end if;
5197
5198 Remove (Expr);
ae77c68b
AC
5199
5200 if Present (Actions) then
ae77c68b
AC
5201 Rewrite (N,
5202 Make_Expression_With_Actions (Loc,
5203 Expression => Relocate_Node (Expr),
5204 Actions => Actions));
5205 Analyze_And_Resolve (N, Typ);
ae77c68b
AC
5206 else
5207 Rewrite (N, Relocate_Node (Expr));
5208 end if;
602a7ec0
AC
5209
5210 -- Note that the result is never static (legitimate cases of static
9b16cb57 5211 -- if expressions were folded in Sem_Eval).
602a7ec0
AC
5212
5213 Set_Is_Static_Expression (N, False);
5214 return;
5215 end if;
5216
305caf42
AC
5217 -- If the type is limited or unconstrained, we expand as follows to
5218 -- avoid any possibility of improper copies.
70482933 5219
305caf42
AC
5220 -- Note: it may be possible to avoid this special processing if the
5221 -- back end uses its own mechanisms for handling by-reference types ???
ac7120ce 5222
c471e2da
AC
5223 -- type Ptr is access all Typ;
5224 -- Cnn : Ptr;
ac7120ce
RD
5225 -- if cond then
5226 -- <<then actions>>
5227 -- Cnn := then-expr'Unrestricted_Access;
5228 -- else
5229 -- <<else actions>>
5230 -- Cnn := else-expr'Unrestricted_Access;
5231 -- end if;
5232
9b16cb57 5233 -- and replace the if expression by a reference to Cnn.all.
ac7120ce 5234
305caf42
AC
5235 -- This special case can be skipped if the back end handles limited
5236 -- types properly and ensures that no incorrect copies are made.
5237
5238 if Is_By_Reference_Type (Typ)
5239 and then not Back_End_Handles_Limited_Types
5240 then
b2c28399
AC
5241 -- When the "then" or "else" expressions involve controlled function
5242 -- calls, generated temporaries are chained on the corresponding list
5243 -- of actions. These temporaries need to be finalized after the if
5244 -- expression is evaluated.
3cebd1c0 5245
b2c28399
AC
5246 Process_Actions (Then_Actions (N));
5247 Process_Actions (Else_Actions (N));
3cebd1c0 5248
b2c28399
AC
5249 -- Generate:
5250 -- type Ann is access all Typ;
3cebd1c0 5251
b2c28399 5252 Ptr_Typ := Make_Temporary (Loc, 'A');
3cebd1c0 5253
b2c28399
AC
5254 Insert_Action (N,
5255 Make_Full_Type_Declaration (Loc,
5256 Defining_Identifier => Ptr_Typ,
5257 Type_Definition =>
5258 Make_Access_To_Object_Definition (Loc,
5259 All_Present => True,
5260 Subtype_Indication => New_Reference_To (Typ, Loc))));
3cebd1c0 5261
b2c28399
AC
5262 -- Generate:
5263 -- Cnn : Ann;
3cebd1c0 5264
b2c28399 5265 Cnn := Make_Temporary (Loc, 'C', N);
3cebd1c0 5266
b2c28399
AC
5267 Decl :=
5268 Make_Object_Declaration (Loc,
5269 Defining_Identifier => Cnn,
5270 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
3cebd1c0 5271
b2c28399
AC
5272 -- Generate:
5273 -- if Cond then
5274 -- Cnn := <Thenx>'Unrestricted_Access;
5275 -- else
5276 -- Cnn := <Elsex>'Unrestricted_Access;
5277 -- end if;
3cebd1c0 5278
b2c28399
AC
5279 New_If :=
5280 Make_Implicit_If_Statement (N,
5281 Condition => Relocate_Node (Cond),
5282 Then_Statements => New_List (
5283 Make_Assignment_Statement (Sloc (Thenx),
5284 Name => New_Reference_To (Cnn, Sloc (Thenx)),
5285 Expression =>
5286 Make_Attribute_Reference (Loc,
5287 Prefix => Relocate_Node (Thenx),
5288 Attribute_Name => Name_Unrestricted_Access))),
3cebd1c0 5289
b2c28399
AC
5290 Else_Statements => New_List (
5291 Make_Assignment_Statement (Sloc (Elsex),
5292 Name => New_Reference_To (Cnn, Sloc (Elsex)),
5293 Expression =>
5294 Make_Attribute_Reference (Loc,
5295 Prefix => Relocate_Node (Elsex),
5296 Attribute_Name => Name_Unrestricted_Access))));
3cebd1c0
AC
5297
5298 New_N :=
5299 Make_Explicit_Dereference (Loc,
5300 Prefix => New_Occurrence_Of (Cnn, Loc));
fb1949a0 5301
c471e2da
AC
5302 -- For other types, we only need to expand if there are other actions
5303 -- associated with either branch.
5304
5305 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
c471e2da 5306
0812b84e 5307 -- We now wrap the actions into the appropriate expression
fb1949a0 5308
0812b84e
AC
5309 if Present (Then_Actions (N)) then
5310 Rewrite (Thenx,
b2c28399
AC
5311 Make_Expression_With_Actions (Sloc (Thenx),
5312 Actions => Then_Actions (N),
5313 Expression => Relocate_Node (Thenx)));
5314
0812b84e
AC
5315 Set_Then_Actions (N, No_List);
5316 Analyze_And_Resolve (Thenx, Typ);
5317 end if;
305caf42 5318
0812b84e
AC
5319 if Present (Else_Actions (N)) then
5320 Rewrite (Elsex,
b2c28399
AC
5321 Make_Expression_With_Actions (Sloc (Elsex),
5322 Actions => Else_Actions (N),
5323 Expression => Relocate_Node (Elsex)));
5324
0812b84e
AC
5325 Set_Else_Actions (N, No_List);
5326 Analyze_And_Resolve (Elsex, Typ);
305caf42
AC
5327 end if;
5328
0812b84e
AC
5329 return;
5330
b2c28399
AC
5331 -- If no actions then no expansion needed, gigi will handle it using the
5332 -- same approach as a C conditional expression.
305caf42
AC
5333
5334 else
c471e2da
AC
5335 return;
5336 end if;
5337
305caf42
AC
5338 -- Fall through here for either the limited expansion, or the case of
5339 -- inserting actions for non-limited types. In both these cases, we must
5340 -- move the SLOC of the parent If statement to the newly created one and
3fc5d116
RD
5341 -- change it to the SLOC of the expression which, after expansion, will
5342 -- correspond to what is being evaluated.
c471e2da 5343
533369aa 5344 if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then
c471e2da
AC
5345 Set_Sloc (New_If, Sloc (Parent (N)));
5346 Set_Sloc (Parent (N), Loc);
5347 end if;
70482933 5348
3fc5d116
RD
5349 -- Make sure Then_Actions and Else_Actions are appropriately moved
5350 -- to the new if statement.
5351
c471e2da
AC
5352 if Present (Then_Actions (N)) then
5353 Insert_List_Before
5354 (First (Then_Statements (New_If)), Then_Actions (N));
70482933 5355 end if;
c471e2da
AC
5356
5357 if Present (Else_Actions (N)) then
5358 Insert_List_Before
5359 (First (Else_Statements (New_If)), Else_Actions (N));
5360 end if;
5361
5362 Insert_Action (N, Decl);
5363 Insert_Action (N, New_If);
5364 Rewrite (N, New_N);
5365 Analyze_And_Resolve (N, Typ);
9b16cb57 5366 end Expand_N_If_Expression;
35a1c212 5367
70482933
RK
5368 -----------------
5369 -- Expand_N_In --
5370 -----------------
5371
5372 procedure Expand_N_In (N : Node_Id) is
7324bf49 5373 Loc : constant Source_Ptr := Sloc (N);
4818e7b9 5374 Restyp : constant Entity_Id := Etype (N);
7324bf49
AC
5375 Lop : constant Node_Id := Left_Opnd (N);
5376 Rop : constant Node_Id := Right_Opnd (N);
5377 Static : constant Boolean := Is_OK_Static_Expression (N);
70482933 5378
4818e7b9
RD
5379 Ltyp : Entity_Id;
5380 Rtyp : Entity_Id;
5381
630d30e9
RD
5382 procedure Substitute_Valid_Check;
5383 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5384 -- test for the left operand being in range of its subtype.
5385
5386 ----------------------------
5387 -- Substitute_Valid_Check --
5388 ----------------------------
5389
5390 procedure Substitute_Valid_Check is
5391 begin
c7532b2d
AC
5392 Rewrite (N,
5393 Make_Attribute_Reference (Loc,
5394 Prefix => Relocate_Node (Lop),
5395 Attribute_Name => Name_Valid));
630d30e9 5396
c7532b2d 5397 Analyze_And_Resolve (N, Restyp);
630d30e9 5398
acad3c0a
AC
5399 -- Give warning unless overflow checking is MINIMIZED or ELIMINATED,
5400 -- in which case, this usage makes sense, and in any case, we have
5401 -- actually eliminated the danger of optimization above.
5402
a7f1b24f 5403 if Overflow_Check_Mode not in Minimized_Or_Eliminated then
324ac540
AC
5404 Error_Msg_N
5405 ("??explicit membership test may be optimized away", N);
acad3c0a 5406 Error_Msg_N -- CODEFIX
324ac540 5407 ("\??use ''Valid attribute instead", N);
acad3c0a
AC
5408 end if;
5409
c7532b2d 5410 return;
630d30e9
RD
5411 end Substitute_Valid_Check;
5412
5413 -- Start of processing for Expand_N_In
5414
70482933 5415 begin
308e6f3a 5416 -- If set membership case, expand with separate procedure
4818e7b9 5417
197e4514 5418 if Present (Alternatives (N)) then
a3068ca6 5419 Expand_Set_Membership (N);
197e4514
AC
5420 return;
5421 end if;
5422
4818e7b9
RD
5423 -- Not set membership, proceed with expansion
5424
5425 Ltyp := Etype (Left_Opnd (N));
5426 Rtyp := Etype (Right_Opnd (N));
5427
5707e389 5428 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
f6194278
RD
5429 -- type, then expand with a separate procedure. Note the use of the
5430 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5431
a7f1b24f 5432 if Overflow_Check_Mode in Minimized_Or_Eliminated
f6194278
RD
5433 and then Is_Signed_Integer_Type (Ltyp)
5434 and then not No_Minimize_Eliminate (N)
5435 then
5436 Expand_Membership_Minimize_Eliminate_Overflow (N);
5437 return;
5438 end if;
5439
630d30e9
RD
5440 -- Check case of explicit test for an expression in range of its
5441 -- subtype. This is suspicious usage and we replace it with a 'Valid
b6b5cca8 5442 -- test and give a warning for scalar types.
630d30e9 5443
4818e7b9 5444 if Is_Scalar_Type (Ltyp)
b6b5cca8
AC
5445
5446 -- Only relevant for source comparisons
5447
5448 and then Comes_From_Source (N)
5449
5450 -- In floating-point this is a standard way to check for finite values
5451 -- and using 'Valid would typically be a pessimization.
5452
4818e7b9 5453 and then not Is_Floating_Point_Type (Ltyp)
b6b5cca8
AC
5454
5455 -- Don't give the message unless right operand is a type entity and
5456 -- the type of the left operand matches this type. Note that this
5457 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5458 -- checks have changed the type of the left operand.
5459
630d30e9 5460 and then Nkind (Rop) in N_Has_Entity
4818e7b9 5461 and then Ltyp = Entity (Rop)
b6b5cca8
AC
5462
5463 -- Skip in VM mode, where we have no sense of invalid values. The
5464 -- warning still seems relevant, but not important enough to worry.
5465
26bff3d9 5466 and then VM_Target = No_VM
b6b5cca8
AC
5467
5468 -- Skip this for predicated types, where such expressions are a
5469 -- reasonable way of testing if something meets the predicate.
5470
3d6db7f8 5471 and then not Present (Predicate_Function (Ltyp))
630d30e9
RD
5472 then
5473 Substitute_Valid_Check;
5474 return;
5475 end if;
5476
20b5d666
JM
5477 -- Do validity check on operands
5478
5479 if Validity_Checks_On and Validity_Check_Operands then
5480 Ensure_Valid (Left_Opnd (N));
5481 Validity_Check_Range (Right_Opnd (N));
5482 end if;
5483
630d30e9 5484 -- Case of explicit range
fbf5a39b
AC
5485
5486 if Nkind (Rop) = N_Range then
5487 declare
630d30e9
RD
5488 Lo : constant Node_Id := Low_Bound (Rop);
5489 Hi : constant Node_Id := High_Bound (Rop);
5490
5491 Lo_Orig : constant Node_Id := Original_Node (Lo);
5492 Hi_Orig : constant Node_Id := Original_Node (Hi);
5493
c800f862
RD
5494 Lcheck : Compare_Result;
5495 Ucheck : Compare_Result;
fbf5a39b 5496
d766cee3
RD
5497 Warn1 : constant Boolean :=
5498 Constant_Condition_Warnings
c800f862
RD
5499 and then Comes_From_Source (N)
5500 and then not In_Instance;
d766cee3 5501 -- This must be true for any of the optimization warnings, we
9a0ddeee
AC
5502 -- clearly want to give them only for source with the flag on. We
5503 -- also skip these warnings in an instance since it may be the
5504 -- case that different instantiations have different ranges.
d766cee3
RD
5505
5506 Warn2 : constant Boolean :=
5507 Warn1
5508 and then Nkind (Original_Node (Rop)) = N_Range
5509 and then Is_Integer_Type (Etype (Lo));
5510 -- For the case where only one bound warning is elided, we also
5511 -- insist on an explicit range and an integer type. The reason is
5512 -- that the use of enumeration ranges including an end point is
9a0ddeee
AC
5513 -- common, as is the use of a subtype name, one of whose bounds is
5514 -- the same as the type of the expression.
d766cee3 5515
fbf5a39b 5516 begin
c95e0edc 5517 -- If test is explicit x'First .. x'Last, replace by valid check
630d30e9 5518
e606088a
AC
5519 -- Could use some individual comments for this complex test ???
5520
d766cee3 5521 if Is_Scalar_Type (Ltyp)
b6b5cca8
AC
5522
5523 -- And left operand is X'First where X matches left operand
5524 -- type (this eliminates cases of type mismatch, including
5525 -- the cases where ELIMINATED/MINIMIZED mode has changed the
5526 -- type of the left operand.
5527
630d30e9
RD
5528 and then Nkind (Lo_Orig) = N_Attribute_Reference
5529 and then Attribute_Name (Lo_Orig) = Name_First
5530 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
d766cee3 5531 and then Entity (Prefix (Lo_Orig)) = Ltyp
b6b5cca8
AC
5532
5533 -- Same tests for right operand
5534
630d30e9
RD
5535 and then Nkind (Hi_Orig) = N_Attribute_Reference
5536 and then Attribute_Name (Hi_Orig) = Name_Last
5537 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
d766cee3 5538 and then Entity (Prefix (Hi_Orig)) = Ltyp
b6b5cca8
AC
5539
5540 -- Relevant only for source cases
5541
630d30e9 5542 and then Comes_From_Source (N)
b6b5cca8
AC
5543
5544 -- Omit for VM cases, where we don't have invalid values
5545
26bff3d9 5546 and then VM_Target = No_VM
630d30e9
RD
5547 then
5548 Substitute_Valid_Check;
4818e7b9 5549 goto Leave;
630d30e9
RD
5550 end if;
5551
d766cee3
RD
5552 -- If bounds of type are known at compile time, and the end points
5553 -- are known at compile time and identical, this is another case
5554 -- for substituting a valid test. We only do this for discrete
5555 -- types, since it won't arise in practice for float types.
5556
5557 if Comes_From_Source (N)
5558 and then Is_Discrete_Type (Ltyp)
5559 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
5560 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
5561 and then Compile_Time_Known_Value (Lo)
5562 and then Compile_Time_Known_Value (Hi)
5563 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
5564 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
94eefd2e 5565
f6194278
RD
5566 -- Kill warnings in instances, since they may be cases where we
5567 -- have a test in the generic that makes sense with some types
5568 -- and not with other types.
94eefd2e
RD
5569
5570 and then not In_Instance
d766cee3
RD
5571 then
5572 Substitute_Valid_Check;
4818e7b9 5573 goto Leave;
d766cee3
RD
5574 end if;
5575
9a0ddeee
AC
5576 -- If we have an explicit range, do a bit of optimization based on
5577 -- range analysis (we may be able to kill one or both checks).
630d30e9 5578
c800f862
RD
5579 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
5580 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
5581
630d30e9
RD
5582 -- If either check is known to fail, replace result by False since
5583 -- the other check does not matter. Preserve the static flag for
5584 -- legality checks, because we are constant-folding beyond RM 4.9.
fbf5a39b
AC
5585
5586 if Lcheck = LT or else Ucheck = GT then
c800f862 5587 if Warn1 then
685bc70f
AC
5588 Error_Msg_N ("?c?range test optimized away", N);
5589 Error_Msg_N ("\?c?value is known to be out of range", N);
d766cee3
RD
5590 end if;
5591
9a0ddeee 5592 Rewrite (N, New_Reference_To (Standard_False, Loc));
4818e7b9 5593 Analyze_And_Resolve (N, Restyp);
7324bf49 5594 Set_Is_Static_Expression (N, Static);
4818e7b9 5595 goto Leave;
fbf5a39b 5596
685094bf
RD
5597 -- If both checks are known to succeed, replace result by True,
5598 -- since we know we are in range.
fbf5a39b
AC
5599
5600 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
c800f862 5601 if Warn1 then
685bc70f
AC
5602 Error_Msg_N ("?c?range test optimized away", N);
5603 Error_Msg_N ("\?c?value is known to be in range", N);
d766cee3
RD
5604 end if;
5605
9a0ddeee 5606 Rewrite (N, New_Reference_To (Standard_True, Loc));
4818e7b9 5607 Analyze_And_Resolve (N, Restyp);
7324bf49 5608 Set_Is_Static_Expression (N, Static);
4818e7b9 5609 goto Leave;
fbf5a39b 5610
d766cee3
RD
5611 -- If lower bound check succeeds and upper bound check is not
5612 -- known to succeed or fail, then replace the range check with
5613 -- a comparison against the upper bound.
fbf5a39b
AC
5614
5615 elsif Lcheck in Compare_GE then
94eefd2e 5616 if Warn2 and then not In_Instance then
324ac540
AC
5617 Error_Msg_N ("??lower bound test optimized away", Lo);
5618 Error_Msg_N ("\??value is known to be in range", Lo);
d766cee3
RD
5619 end if;
5620
fbf5a39b
AC
5621 Rewrite (N,
5622 Make_Op_Le (Loc,
5623 Left_Opnd => Lop,
5624 Right_Opnd => High_Bound (Rop)));
4818e7b9
RD
5625 Analyze_And_Resolve (N, Restyp);
5626 goto Leave;
fbf5a39b 5627
d766cee3
RD
5628 -- If upper bound check succeeds and lower bound check is not
5629 -- known to succeed or fail, then replace the range check with
5630 -- a comparison against the lower bound.
fbf5a39b
AC
5631
5632 elsif Ucheck in Compare_LE then
94eefd2e 5633 if Warn2 and then not In_Instance then
324ac540
AC
5634 Error_Msg_N ("??upper bound test optimized away", Hi);
5635 Error_Msg_N ("\??value is known to be in range", Hi);
d766cee3
RD
5636 end if;
5637
fbf5a39b
AC
5638 Rewrite (N,
5639 Make_Op_Ge (Loc,
5640 Left_Opnd => Lop,
5641 Right_Opnd => Low_Bound (Rop)));
4818e7b9
RD
5642 Analyze_And_Resolve (N, Restyp);
5643 goto Leave;
fbf5a39b 5644 end if;
c800f862
RD
5645
5646 -- We couldn't optimize away the range check, but there is one
5647 -- more issue. If we are checking constant conditionals, then we
5648 -- see if we can determine the outcome assuming everything is
5649 -- valid, and if so give an appropriate warning.
5650
5651 if Warn1 and then not Assume_No_Invalid_Values then
5652 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
5653 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
5654
5655 -- Result is out of range for valid value
5656
5657 if Lcheck = LT or else Ucheck = GT then
ed2233dc 5658 Error_Msg_N
685bc70f 5659 ("?c?value can only be in range if it is invalid", N);
c800f862
RD
5660
5661 -- Result is in range for valid value
5662
5663 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
ed2233dc 5664 Error_Msg_N
685bc70f 5665 ("?c?value can only be out of range if it is invalid", N);
c800f862
RD
5666
5667 -- Lower bound check succeeds if value is valid
5668
5669 elsif Warn2 and then Lcheck in Compare_GE then
ed2233dc 5670 Error_Msg_N
685bc70f 5671 ("?c?lower bound check only fails if it is invalid", Lo);
c800f862
RD
5672
5673 -- Upper bound check succeeds if value is valid
5674
5675 elsif Warn2 and then Ucheck in Compare_LE then
ed2233dc 5676 Error_Msg_N
685bc70f 5677 ("?c?upper bound check only fails for invalid values", Hi);
c800f862
RD
5678 end if;
5679 end if;
fbf5a39b
AC
5680 end;
5681
5682 -- For all other cases of an explicit range, nothing to be done
70482933 5683
4818e7b9 5684 goto Leave;
70482933
RK
5685
5686 -- Here right operand is a subtype mark
5687
5688 else
5689 declare
82878151
AC
5690 Typ : Entity_Id := Etype (Rop);
5691 Is_Acc : constant Boolean := Is_Access_Type (Typ);
5692 Cond : Node_Id := Empty;
5693 New_N : Node_Id;
5694 Obj : Node_Id := Lop;
5695 SCIL_Node : Node_Id;
70482933
RK
5696
5697 begin
5698 Remove_Side_Effects (Obj);
5699
5700 -- For tagged type, do tagged membership operation
5701
5702 if Is_Tagged_Type (Typ) then
fbf5a39b 5703
26bff3d9
JM
5704 -- No expansion will be performed when VM_Target, as the VM
5705 -- back-ends will handle the membership tests directly (tags
5706 -- are not explicitly represented in Java objects, so the
5707 -- normal tagged membership expansion is not what we want).
70482933 5708
1f110335 5709 if Tagged_Type_Expansion then
82878151
AC
5710 Tagged_Membership (N, SCIL_Node, New_N);
5711 Rewrite (N, New_N);
4818e7b9 5712 Analyze_And_Resolve (N, Restyp);
82878151
AC
5713
5714 -- Update decoration of relocated node referenced by the
5715 -- SCIL node.
5716
9a0ddeee 5717 if Generate_SCIL and then Present (SCIL_Node) then
7665e4bd 5718 Set_SCIL_Node (N, SCIL_Node);
82878151 5719 end if;
70482933
RK
5720 end if;
5721
4818e7b9 5722 goto Leave;
70482933 5723
c95e0edc 5724 -- If type is scalar type, rewrite as x in t'First .. t'Last.
70482933 5725 -- This reason we do this is that the bounds may have the wrong
c800f862
RD
5726 -- type if they come from the original type definition. Also this
5727 -- way we get all the processing above for an explicit range.
70482933 5728
f6194278
RD
5729 -- Don't do this for predicated types, since in this case we
5730 -- want to check the predicate!
c0f136cd 5731
c7532b2d
AC
5732 elsif Is_Scalar_Type (Typ) then
5733 if No (Predicate_Function (Typ)) then
5734 Rewrite (Rop,
5735 Make_Range (Loc,
5736 Low_Bound =>
5737 Make_Attribute_Reference (Loc,
5738 Attribute_Name => Name_First,
f6194278 5739 Prefix => New_Reference_To (Typ, Loc)),
c7532b2d
AC
5740
5741 High_Bound =>
5742 Make_Attribute_Reference (Loc,
5743 Attribute_Name => Name_Last,
f6194278 5744 Prefix => New_Reference_To (Typ, Loc))));
c7532b2d
AC
5745 Analyze_And_Resolve (N, Restyp);
5746 end if;
70482933 5747
4818e7b9 5748 goto Leave;
5d09245e
AC
5749
5750 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5751 -- a membership test if the subtype mark denotes a constrained
5752 -- Unchecked_Union subtype and the expression lacks inferable
5753 -- discriminants.
5754
5755 elsif Is_Unchecked_Union (Base_Type (Typ))
5756 and then Is_Constrained (Typ)
5757 and then not Has_Inferable_Discriminants (Lop)
5758 then
5759 Insert_Action (N,
5760 Make_Raise_Program_Error (Loc,
5761 Reason => PE_Unchecked_Union_Restriction));
5762
9a0ddeee 5763 -- Prevent Gigi from generating incorrect code by rewriting the
f6194278 5764 -- test as False. What is this undocumented thing about ???
5d09245e 5765
9a0ddeee 5766 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4818e7b9 5767 goto Leave;
70482933
RK
5768 end if;
5769
fbf5a39b
AC
5770 -- Here we have a non-scalar type
5771
70482933
RK
5772 if Is_Acc then
5773 Typ := Designated_Type (Typ);
5774 end if;
5775
5776 if not Is_Constrained (Typ) then
9a0ddeee 5777 Rewrite (N, New_Reference_To (Standard_True, Loc));
4818e7b9 5778 Analyze_And_Resolve (N, Restyp);
70482933 5779
685094bf
RD
5780 -- For the constrained array case, we have to check the subscripts
5781 -- for an exact match if the lengths are non-zero (the lengths
5782 -- must match in any case).
70482933
RK
5783
5784 elsif Is_Array_Type (Typ) then
fbf5a39b 5785 Check_Subscripts : declare
9a0ddeee 5786 function Build_Attribute_Reference
2e071734
AC
5787 (E : Node_Id;
5788 Nam : Name_Id;
5789 Dim : Nat) return Node_Id;
9a0ddeee 5790 -- Build attribute reference E'Nam (Dim)
70482933 5791
9a0ddeee
AC
5792 -------------------------------
5793 -- Build_Attribute_Reference --
5794 -------------------------------
fbf5a39b 5795
9a0ddeee 5796 function Build_Attribute_Reference
2e071734
AC
5797 (E : Node_Id;
5798 Nam : Name_Id;
5799 Dim : Nat) return Node_Id
70482933
RK
5800 is
5801 begin
5802 return
5803 Make_Attribute_Reference (Loc,
9a0ddeee 5804 Prefix => E,
70482933 5805 Attribute_Name => Nam,
9a0ddeee 5806 Expressions => New_List (
70482933 5807 Make_Integer_Literal (Loc, Dim)));
9a0ddeee 5808 end Build_Attribute_Reference;
70482933 5809
fad0600d 5810 -- Start of processing for Check_Subscripts
fbf5a39b 5811
70482933
RK
5812 begin
5813 for J in 1 .. Number_Dimensions (Typ) loop
5814 Evolve_And_Then (Cond,
5815 Make_Op_Eq (Loc,
5816 Left_Opnd =>
9a0ddeee 5817 Build_Attribute_Reference
fbf5a39b
AC
5818 (Duplicate_Subexpr_No_Checks (Obj),
5819 Name_First, J),
70482933 5820 Right_Opnd =>
9a0ddeee 5821 Build_Attribute_Reference
70482933
RK
5822 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
5823
5824 Evolve_And_Then (Cond,
5825 Make_Op_Eq (Loc,
5826 Left_Opnd =>
9a0ddeee 5827 Build_Attribute_Reference
fbf5a39b
AC
5828 (Duplicate_Subexpr_No_Checks (Obj),
5829 Name_Last, J),
70482933 5830 Right_Opnd =>
9a0ddeee 5831 Build_Attribute_Reference
70482933
RK
5832 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
5833 end loop;
5834
5835 if Is_Acc then
fbf5a39b
AC
5836 Cond :=
5837 Make_Or_Else (Loc,
5838 Left_Opnd =>
5839 Make_Op_Eq (Loc,
5840 Left_Opnd => Obj,
5841 Right_Opnd => Make_Null (Loc)),
5842 Right_Opnd => Cond);
70482933
RK
5843 end if;
5844
5845 Rewrite (N, Cond);
4818e7b9 5846 Analyze_And_Resolve (N, Restyp);
fbf5a39b 5847 end Check_Subscripts;
70482933 5848
685094bf
RD
5849 -- These are the cases where constraint checks may be required,
5850 -- e.g. records with possible discriminants
70482933
RK
5851
5852 else
5853 -- Expand the test into a series of discriminant comparisons.
685094bf
RD
5854 -- The expression that is built is the negation of the one that
5855 -- is used for checking discriminant constraints.
70482933
RK
5856
5857 Obj := Relocate_Node (Left_Opnd (N));
5858
5859 if Has_Discriminants (Typ) then
5860 Cond := Make_Op_Not (Loc,
5861 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
5862
5863 if Is_Acc then
5864 Cond := Make_Or_Else (Loc,
5865 Left_Opnd =>
5866 Make_Op_Eq (Loc,
5867 Left_Opnd => Obj,
5868 Right_Opnd => Make_Null (Loc)),
5869 Right_Opnd => Cond);
5870 end if;
5871
5872 else
5873 Cond := New_Occurrence_Of (Standard_True, Loc);
5874 end if;
5875
5876 Rewrite (N, Cond);
4818e7b9 5877 Analyze_And_Resolve (N, Restyp);
70482933 5878 end if;
6cce2156
GD
5879
5880 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
5881 -- expression of an anonymous access type. This can involve an
5882 -- accessibility test and a tagged type membership test in the
5883 -- case of tagged designated types.
5884
5885 if Ada_Version >= Ada_2012
5886 and then Is_Acc
5887 and then Ekind (Ltyp) = E_Anonymous_Access_Type
5888 then
5889 declare
5890 Expr_Entity : Entity_Id := Empty;
5891 New_N : Node_Id;
5892 Param_Level : Node_Id;
5893 Type_Level : Node_Id;
996c8821 5894
6cce2156
GD
5895 begin
5896 if Is_Entity_Name (Lop) then
5897 Expr_Entity := Param_Entity (Lop);
996c8821 5898
6cce2156
GD
5899 if not Present (Expr_Entity) then
5900 Expr_Entity := Entity (Lop);
5901 end if;
5902 end if;
5903
5904 -- If a conversion of the anonymous access value to the
5905 -- tested type would be illegal, then the result is False.
5906
5907 if not Valid_Conversion
5908 (Lop, Rtyp, Lop, Report_Errs => False)
5909 then
5910 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5911 Analyze_And_Resolve (N, Restyp);
5912
5913 -- Apply an accessibility check if the access object has an
5914 -- associated access level and when the level of the type is
5915 -- less deep than the level of the access parameter. This
5916 -- only occur for access parameters and stand-alone objects
5917 -- of an anonymous access type.
5918
5919 else
5920 if Present (Expr_Entity)
996c8821
RD
5921 and then
5922 Present
5923 (Effective_Extra_Accessibility (Expr_Entity))
5924 and then UI_Gt (Object_Access_Level (Lop),
5925 Type_Access_Level (Rtyp))
6cce2156
GD
5926 then
5927 Param_Level :=
5928 New_Occurrence_Of
d15f9422 5929 (Effective_Extra_Accessibility (Expr_Entity), Loc);
6cce2156
GD
5930
5931 Type_Level :=
5932 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
5933
5934 -- Return True only if the accessibility level of the
5935 -- expression entity is not deeper than the level of
5936 -- the tested access type.
5937
5938 Rewrite (N,
5939 Make_And_Then (Loc,
5940 Left_Opnd => Relocate_Node (N),
5941 Right_Opnd => Make_Op_Le (Loc,
5942 Left_Opnd => Param_Level,
5943 Right_Opnd => Type_Level)));
5944
5945 Analyze_And_Resolve (N);
5946 end if;
5947
5948 -- If the designated type is tagged, do tagged membership
5949 -- operation.
5950
5951 -- *** NOTE: we have to check not null before doing the
5952 -- tagged membership test (but maybe that can be done
5953 -- inside Tagged_Membership?).
5954
5955 if Is_Tagged_Type (Typ) then
5956 Rewrite (N,
5957 Make_And_Then (Loc,
5958 Left_Opnd => Relocate_Node (N),
5959 Right_Opnd =>
5960 Make_Op_Ne (Loc,
5961 Left_Opnd => Obj,
5962 Right_Opnd => Make_Null (Loc))));
5963
5964 -- No expansion will be performed when VM_Target, as
5965 -- the VM back-ends will handle the membership tests
5966 -- directly (tags are not explicitly represented in
5967 -- Java objects, so the normal tagged membership
5968 -- expansion is not what we want).
5969
5970 if Tagged_Type_Expansion then
5971
5972 -- Note that we have to pass Original_Node, because
5973 -- the membership test might already have been
5974 -- rewritten by earlier parts of membership test.
5975
5976 Tagged_Membership
5977 (Original_Node (N), SCIL_Node, New_N);
5978
5979 -- Update decoration of relocated node referenced
5980 -- by the SCIL node.
5981
5982 if Generate_SCIL and then Present (SCIL_Node) then
5983 Set_SCIL_Node (New_N, SCIL_Node);
5984 end if;
5985
5986 Rewrite (N,
5987 Make_And_Then (Loc,
5988 Left_Opnd => Relocate_Node (N),
5989 Right_Opnd => New_N));
5990
5991 Analyze_And_Resolve (N, Restyp);
5992 end if;
5993 end if;
5994 end if;
5995 end;
5996 end if;
70482933
RK
5997 end;
5998 end if;
4818e7b9
RD
5999
6000 -- At this point, we have done the processing required for the basic
6001 -- membership test, but not yet dealt with the predicate.
6002
6003 <<Leave>>
6004
c7532b2d
AC
6005 -- If a predicate is present, then we do the predicate test, but we
6006 -- most certainly want to omit this if we are within the predicate
6007 -- function itself, since otherwise we have an infinite recursion!
3d6db7f8
GD
6008 -- The check should also not be emitted when testing against a range
6009 -- (the check is only done when the right operand is a subtype; see
6010 -- RM12-4.5.2 (28.1/3-30/3)).
4818e7b9 6011
c7532b2d
AC
6012 declare
6013 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
4818e7b9 6014
c7532b2d
AC
6015 begin
6016 if Present (PFunc)
6017 and then Current_Scope /= PFunc
3d6db7f8 6018 and then Nkind (Rop) /= N_Range
c7532b2d
AC
6019 then
6020 Rewrite (N,
6021 Make_And_Then (Loc,
6022 Left_Opnd => Relocate_Node (N),
fc142f63 6023 Right_Opnd => Make_Predicate_Call (Rtyp, Lop, Mem => True)));
4818e7b9 6024
c7532b2d 6025 -- Analyze new expression, mark left operand as analyzed to
b2009d46
AC
6026 -- avoid infinite recursion adding predicate calls. Similarly,
6027 -- suppress further range checks on the call.
4818e7b9 6028
c7532b2d 6029 Set_Analyzed (Left_Opnd (N));
b2009d46 6030 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4818e7b9 6031
c7532b2d
AC
6032 -- All done, skip attempt at compile time determination of result
6033
6034 return;
6035 end if;
6036 end;
70482933
RK
6037 end Expand_N_In;
6038
6039 --------------------------------
6040 -- Expand_N_Indexed_Component --
6041 --------------------------------
6042
6043 procedure Expand_N_Indexed_Component (N : Node_Id) is
6044 Loc : constant Source_Ptr := Sloc (N);
6045 Typ : constant Entity_Id := Etype (N);
6046 P : constant Node_Id := Prefix (N);
6047 T : constant Entity_Id := Etype (P);
5972791c 6048 Atp : Entity_Id;
70482933
RK
6049
6050 begin
685094bf
RD
6051 -- A special optimization, if we have an indexed component that is
6052 -- selecting from a slice, then we can eliminate the slice, since, for
6053 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6054 -- the range check required by the slice. The range check for the slice
6055 -- itself has already been generated. The range check for the
6056 -- subscripting operation is ensured by converting the subject to
6057 -- the subtype of the slice.
6058
6059 -- This optimization not only generates better code, avoiding slice
6060 -- messing especially in the packed case, but more importantly bypasses
6061 -- some problems in handling this peculiar case, for example, the issue
6062 -- of dealing specially with object renamings.
70482933
RK
6063
6064 if Nkind (P) = N_Slice then
6065 Rewrite (N,
6066 Make_Indexed_Component (Loc,
6067 Prefix => Prefix (P),
6068 Expressions => New_List (
6069 Convert_To
6070 (Etype (First_Index (Etype (P))),
6071 First (Expressions (N))))));
6072 Analyze_And_Resolve (N, Typ);
6073 return;
6074 end if;
6075
b4592168
GD
6076 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6077 -- function, then additional actuals must be passed.
6078
0791fbe9 6079 if Ada_Version >= Ada_2005
b4592168
GD
6080 and then Is_Build_In_Place_Function_Call (P)
6081 then
6082 Make_Build_In_Place_Call_In_Anonymous_Context (P);
6083 end if;
6084
685094bf 6085 -- If the prefix is an access type, then we unconditionally rewrite if
09494c32 6086 -- as an explicit dereference. This simplifies processing for several
685094bf
RD
6087 -- cases, including packed array cases and certain cases in which checks
6088 -- must be generated. We used to try to do this only when it was
6089 -- necessary, but it cleans up the code to do it all the time.
70482933
RK
6090
6091 if Is_Access_Type (T) then
2717634d 6092 Insert_Explicit_Dereference (P);
70482933 6093 Analyze_And_Resolve (P, Designated_Type (T));
5972791c
AC
6094 Atp := Designated_Type (T);
6095 else
6096 Atp := T;
70482933
RK
6097 end if;
6098
fbf5a39b
AC
6099 -- Generate index and validity checks
6100
6101 Generate_Index_Checks (N);
6102
70482933
RK
6103 if Validity_Checks_On and then Validity_Check_Subscripts then
6104 Apply_Subscript_Validity_Checks (N);
6105 end if;
6106
5972791c
AC
6107 -- If selecting from an array with atomic components, and atomic sync
6108 -- is not suppressed for this array type, set atomic sync flag.
6109
6110 if (Has_Atomic_Components (Atp)
6111 and then not Atomic_Synchronization_Disabled (Atp))
6112 or else (Is_Atomic (Typ)
6113 and then not Atomic_Synchronization_Disabled (Typ))
6114 then
4c318253 6115 Activate_Atomic_Synchronization (N);
5972791c
AC
6116 end if;
6117
70482933
RK
6118 -- All done for the non-packed case
6119
6120 if not Is_Packed (Etype (Prefix (N))) then
6121 return;
6122 end if;
6123
6124 -- For packed arrays that are not bit-packed (i.e. the case of an array
8fc789c8 6125 -- with one or more index types with a non-contiguous enumeration type),
70482933
RK
6126 -- we can always use the normal packed element get circuit.
6127
6128 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
6129 Expand_Packed_Element_Reference (N);
6130 return;
6131 end if;
6132
6133 -- For a reference to a component of a bit packed array, we have to
6134 -- convert it to a reference to the corresponding Packed_Array_Type.
6135 -- We only want to do this for simple references, and not for:
6136
685094bf
RD
6137 -- Left side of assignment, or prefix of left side of assignment, or
6138 -- prefix of the prefix, to handle packed arrays of packed arrays,
70482933
RK
6139 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6140
6141 -- Renaming objects in renaming associations
6142 -- This case is handled when a use of the renamed variable occurs
6143
6144 -- Actual parameters for a procedure call
6145 -- This case is handled in Exp_Ch6.Expand_Actuals
6146
6147 -- The second expression in a 'Read attribute reference
6148
47d3b920 6149 -- The prefix of an address or bit or size attribute reference
70482933
RK
6150
6151 -- The following circuit detects these exceptions
6152
6153 declare
6154 Child : Node_Id := N;
6155 Parnt : Node_Id := Parent (N);
6156
6157 begin
6158 loop
6159 if Nkind (Parnt) = N_Unchecked_Expression then
6160 null;
6161
303b4d58
AC
6162 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
6163 N_Procedure_Call_Statement)
70482933
RK
6164 or else (Nkind (Parnt) = N_Parameter_Association
6165 and then
6166 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
6167 then
6168 return;
6169
6170 elsif Nkind (Parnt) = N_Attribute_Reference
b69cd36a
AC
6171 and then Nam_In (Attribute_Name (Parnt), Name_Address,
6172 Name_Bit,
6173 Name_Size)
70482933
RK
6174 and then Prefix (Parnt) = Child
6175 then
6176 return;
6177
6178 elsif Nkind (Parnt) = N_Assignment_Statement
6179 and then Name (Parnt) = Child
6180 then
6181 return;
6182
685094bf
RD
6183 -- If the expression is an index of an indexed component, it must
6184 -- be expanded regardless of context.
fbf5a39b
AC
6185
6186 elsif Nkind (Parnt) = N_Indexed_Component
6187 and then Child /= Prefix (Parnt)
6188 then
6189 Expand_Packed_Element_Reference (N);
6190 return;
6191
6192 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
6193 and then Name (Parent (Parnt)) = Parnt
6194 then
6195 return;
6196
70482933
RK
6197 elsif Nkind (Parnt) = N_Attribute_Reference
6198 and then Attribute_Name (Parnt) = Name_Read
6199 and then Next (First (Expressions (Parnt))) = Child
6200 then
6201 return;
6202
303b4d58 6203 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
533369aa 6204 and then Prefix (Parnt) = Child
70482933
RK
6205 then
6206 null;
6207
6208 else
6209 Expand_Packed_Element_Reference (N);
6210 return;
6211 end if;
6212
685094bf
RD
6213 -- Keep looking up tree for unchecked expression, or if we are the
6214 -- prefix of a possible assignment left side.
70482933
RK
6215
6216 Child := Parnt;
6217 Parnt := Parent (Child);
6218 end loop;
6219 end;
70482933
RK
6220 end Expand_N_Indexed_Component;
6221
6222 ---------------------
6223 -- Expand_N_Not_In --
6224 ---------------------
6225
6226 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6227 -- can be done. This avoids needing to duplicate this expansion code.
6228
6229 procedure Expand_N_Not_In (N : Node_Id) is
630d30e9
RD
6230 Loc : constant Source_Ptr := Sloc (N);
6231 Typ : constant Entity_Id := Etype (N);
6232 Cfs : constant Boolean := Comes_From_Source (N);
70482933
RK
6233
6234 begin
6235 Rewrite (N,
6236 Make_Op_Not (Loc,
6237 Right_Opnd =>
6238 Make_In (Loc,
6239 Left_Opnd => Left_Opnd (N),
d766cee3 6240 Right_Opnd => Right_Opnd (N))));
630d30e9 6241
197e4514
AC
6242 -- If this is a set membership, preserve list of alternatives
6243
6244 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
6245
d766cee3 6246 -- We want this to appear as coming from source if original does (see
8fc789c8 6247 -- transformations in Expand_N_In).
630d30e9
RD
6248
6249 Set_Comes_From_Source (N, Cfs);
6250 Set_Comes_From_Source (Right_Opnd (N), Cfs);
6251
8fc789c8 6252 -- Now analyze transformed node
630d30e9 6253
70482933
RK
6254 Analyze_And_Resolve (N, Typ);
6255 end Expand_N_Not_In;
6256
6257 -------------------
6258 -- Expand_N_Null --
6259 -------------------
6260
a3f2babd
AC
6261 -- The only replacement required is for the case of a null of a type that
6262 -- is an access to protected subprogram, or a subtype thereof. We represent
6263 -- such access values as a record, and so we must replace the occurrence of
6264 -- null by the equivalent record (with a null address and a null pointer in
6265 -- it), so that the backend creates the proper value.
70482933
RK
6266
6267 procedure Expand_N_Null (N : Node_Id) is
6268 Loc : constant Source_Ptr := Sloc (N);
a3f2babd 6269 Typ : constant Entity_Id := Base_Type (Etype (N));
70482933
RK
6270 Agg : Node_Id;
6271
6272 begin
26bff3d9 6273 if Is_Access_Protected_Subprogram_Type (Typ) then
70482933
RK
6274 Agg :=
6275 Make_Aggregate (Loc,
6276 Expressions => New_List (
6277 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
6278 Make_Null (Loc)));
6279
6280 Rewrite (N, Agg);
6281 Analyze_And_Resolve (N, Equivalent_Type (Typ));
6282
685094bf
RD
6283 -- For subsequent semantic analysis, the node must retain its type.
6284 -- Gigi in any case replaces this type by the corresponding record
6285 -- type before processing the node.
70482933
RK
6286
6287 Set_Etype (N, Typ);
6288 end if;
fbf5a39b
AC
6289
6290 exception
6291 when RE_Not_Available =>
6292 return;
70482933
RK
6293 end Expand_N_Null;
6294
6295 ---------------------
6296 -- Expand_N_Op_Abs --
6297 ---------------------
6298
6299 procedure Expand_N_Op_Abs (N : Node_Id) is
6300 Loc : constant Source_Ptr := Sloc (N);
6301 Expr : constant Node_Id := Right_Opnd (N);
6302
6303 begin
6304 Unary_Op_Validity_Checks (N);
6305
b6b5cca8
AC
6306 -- Check for MINIMIZED/ELIMINATED overflow mode
6307
6308 if Minimized_Eliminated_Overflow_Check (N) then
6309 Apply_Arithmetic_Overflow_Check (N);
6310 return;
6311 end if;
6312
70482933
RK
6313 -- Deal with software overflow checking
6314
07fc65c4 6315 if not Backend_Overflow_Checks_On_Target
533369aa
AC
6316 and then Is_Signed_Integer_Type (Etype (N))
6317 and then Do_Overflow_Check (N)
70482933 6318 then
685094bf
RD
6319 -- The only case to worry about is when the argument is equal to the
6320 -- largest negative number, so what we do is to insert the check:
70482933 6321
fbf5a39b 6322 -- [constraint_error when Expr = typ'Base'First]
70482933
RK
6323
6324 -- with the usual Duplicate_Subexpr use coding for expr
6325
fbf5a39b
AC
6326 Insert_Action (N,
6327 Make_Raise_Constraint_Error (Loc,
6328 Condition =>
6329 Make_Op_Eq (Loc,
70482933 6330 Left_Opnd => Duplicate_Subexpr (Expr),
fbf5a39b
AC
6331 Right_Opnd =>
6332 Make_Attribute_Reference (Loc,
6333 Prefix =>
6334 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
6335 Attribute_Name => Name_First)),
6336 Reason => CE_Overflow_Check_Failed));
6337 end if;
70482933
RK
6338
6339 -- Vax floating-point types case
6340
fbf5a39b 6341 if Vax_Float (Etype (N)) then
70482933
RK
6342 Expand_Vax_Arith (N);
6343 end if;
6344 end Expand_N_Op_Abs;
6345
6346 ---------------------
6347 -- Expand_N_Op_Add --
6348 ---------------------
6349
6350 procedure Expand_N_Op_Add (N : Node_Id) is
6351 Typ : constant Entity_Id := Etype (N);
6352
6353 begin
6354 Binary_Op_Validity_Checks (N);
6355
b6b5cca8
AC
6356 -- Check for MINIMIZED/ELIMINATED overflow mode
6357
6358 if Minimized_Eliminated_Overflow_Check (N) then
6359 Apply_Arithmetic_Overflow_Check (N);
6360 return;
6361 end if;
6362
70482933
RK
6363 -- N + 0 = 0 + N = N for integer types
6364
6365 if Is_Integer_Type (Typ) then
6366 if Compile_Time_Known_Value (Right_Opnd (N))
6367 and then Expr_Value (Right_Opnd (N)) = Uint_0
6368 then
6369 Rewrite (N, Left_Opnd (N));
6370 return;
6371
6372 elsif Compile_Time_Known_Value (Left_Opnd (N))
6373 and then Expr_Value (Left_Opnd (N)) = Uint_0
6374 then
6375 Rewrite (N, Right_Opnd (N));
6376 return;
6377 end if;
6378 end if;
6379
fbf5a39b 6380 -- Arithmetic overflow checks for signed integer/fixed point types
70482933 6381
761f7dcb 6382 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
70482933
RK
6383 Apply_Arithmetic_Overflow_Check (N);
6384 return;
6385
6386 -- Vax floating-point types case
6387
6388 elsif Vax_Float (Typ) then
6389 Expand_Vax_Arith (N);
6390 end if;
6391 end Expand_N_Op_Add;
6392
6393 ---------------------
6394 -- Expand_N_Op_And --
6395 ---------------------
6396
6397 procedure Expand_N_Op_And (N : Node_Id) is
6398 Typ : constant Entity_Id := Etype (N);
6399
6400 begin
6401 Binary_Op_Validity_Checks (N);
6402
6403 if Is_Array_Type (Etype (N)) then
6404 Expand_Boolean_Operator (N);
6405
6406 elsif Is_Boolean_Type (Etype (N)) then
f2d10a02
AC
6407 Adjust_Condition (Left_Opnd (N));
6408 Adjust_Condition (Right_Opnd (N));
6409 Set_Etype (N, Standard_Boolean);
6410 Adjust_Result_Type (N, Typ);
437f8c1e
AC
6411
6412 elsif Is_Intrinsic_Subprogram (Entity (N)) then
6413 Expand_Intrinsic_Call (N, Entity (N));
6414
70482933
RK
6415 end if;
6416 end Expand_N_Op_And;
6417
6418 ------------------------
6419 -- Expand_N_Op_Concat --
6420 ------------------------
6421
6422 procedure Expand_N_Op_Concat (N : Node_Id) is
70482933
RK
6423 Opnds : List_Id;
6424 -- List of operands to be concatenated
6425
70482933 6426 Cnode : Node_Id;
685094bf
RD
6427 -- Node which is to be replaced by the result of concatenating the nodes
6428 -- in the list Opnds.
70482933 6429
70482933 6430 begin
fbf5a39b
AC
6431 -- Ensure validity of both operands
6432
70482933
RK
6433 Binary_Op_Validity_Checks (N);
6434
685094bf
RD
6435 -- If we are the left operand of a concatenation higher up the tree,
6436 -- then do nothing for now, since we want to deal with a series of
6437 -- concatenations as a unit.
70482933
RK
6438
6439 if Nkind (Parent (N)) = N_Op_Concat
6440 and then N = Left_Opnd (Parent (N))
6441 then
6442 return;
6443 end if;
6444
6445 -- We get here with a concatenation whose left operand may be a
6446 -- concatenation itself with a consistent type. We need to process
6447 -- these concatenation operands from left to right, which means
6448 -- from the deepest node in the tree to the highest node.
6449
6450 Cnode := N;
6451 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
6452 Cnode := Left_Opnd (Cnode);
6453 end loop;
6454
64425dff
BD
6455 -- Now Cnode is the deepest concatenation, and its parents are the
6456 -- concatenation nodes above, so now we process bottom up, doing the
64425dff 6457 -- operands.
70482933 6458
df46b832
AC
6459 -- The outer loop runs more than once if more than one concatenation
6460 -- type is involved.
70482933
RK
6461
6462 Outer : loop
6463 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
6464 Set_Parent (Opnds, N);
6465
df46b832 6466 -- The inner loop gathers concatenation operands
70482933
RK
6467
6468 Inner : while Cnode /= N
70482933
RK
6469 and then Base_Type (Etype (Cnode)) =
6470 Base_Type (Etype (Parent (Cnode)))
6471 loop
6472 Cnode := Parent (Cnode);
6473 Append (Right_Opnd (Cnode), Opnds);
6474 end loop Inner;
6475
68bab0fd 6476 Expand_Concatenate (Cnode, Opnds);
70482933
RK
6477
6478 exit Outer when Cnode = N;
6479 Cnode := Parent (Cnode);
6480 end loop Outer;
6481 end Expand_N_Op_Concat;
6482
6483 ------------------------
6484 -- Expand_N_Op_Divide --
6485 ------------------------
6486
6487 procedure Expand_N_Op_Divide (N : Node_Id) is
f82944b7
JM
6488 Loc : constant Source_Ptr := Sloc (N);
6489 Lopnd : constant Node_Id := Left_Opnd (N);
6490 Ropnd : constant Node_Id := Right_Opnd (N);
6491 Ltyp : constant Entity_Id := Etype (Lopnd);
6492 Rtyp : constant Entity_Id := Etype (Ropnd);
6493 Typ : Entity_Id := Etype (N);
6494 Rknow : constant Boolean := Is_Integer_Type (Typ)
6495 and then
6496 Compile_Time_Known_Value (Ropnd);
6497 Rval : Uint;
70482933
RK
6498
6499 begin
6500 Binary_Op_Validity_Checks (N);
6501
b6b5cca8
AC
6502 -- Check for MINIMIZED/ELIMINATED overflow mode
6503
6504 if Minimized_Eliminated_Overflow_Check (N) then
6505 Apply_Arithmetic_Overflow_Check (N);
6506 return;
6507 end if;
6508
6509 -- Otherwise proceed with expansion of division
6510
f82944b7
JM
6511 if Rknow then
6512 Rval := Expr_Value (Ropnd);
6513 end if;
6514
70482933
RK
6515 -- N / 1 = N for integer types
6516
f82944b7
JM
6517 if Rknow and then Rval = Uint_1 then
6518 Rewrite (N, Lopnd);
70482933
RK
6519 return;
6520 end if;
6521
6522 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6523 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6524 -- operand is an unsigned integer, as required for this to work.
6525
f82944b7
JM
6526 if Nkind (Ropnd) = N_Op_Expon
6527 and then Is_Power_Of_2_For_Shift (Ropnd)
fbf5a39b
AC
6528
6529 -- We cannot do this transformation in configurable run time mode if we
51bf9bdf 6530 -- have 64-bit integers and long shifts are not available.
fbf5a39b 6531
761f7dcb 6532 and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target)
70482933
RK
6533 then
6534 Rewrite (N,
6535 Make_Op_Shift_Right (Loc,
f82944b7 6536 Left_Opnd => Lopnd,
70482933 6537 Right_Opnd =>
f82944b7 6538 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
70482933
RK
6539 Analyze_And_Resolve (N, Typ);
6540 return;
6541 end if;
6542
6543 -- Do required fixup of universal fixed operation
6544
6545 if Typ = Universal_Fixed then
6546 Fixup_Universal_Fixed_Operation (N);
6547 Typ := Etype (N);
6548 end if;
6549
6550 -- Divisions with fixed-point results
6551
6552 if Is_Fixed_Point_Type (Typ) then
6553
685094bf
RD
6554 -- No special processing if Treat_Fixed_As_Integer is set, since
6555 -- from a semantic point of view such operations are simply integer
6556 -- operations and will be treated that way.
70482933
RK
6557
6558 if not Treat_Fixed_As_Integer (N) then
6559 if Is_Integer_Type (Rtyp) then
6560 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
6561 else
6562 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
6563 end if;
6564 end if;
6565
685094bf
RD
6566 -- Other cases of division of fixed-point operands. Again we exclude the
6567 -- case where Treat_Fixed_As_Integer is set.
70482933 6568
761f7dcb 6569 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
70482933
RK
6570 and then not Treat_Fixed_As_Integer (N)
6571 then
6572 if Is_Integer_Type (Typ) then
6573 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
6574 else
6575 pragma Assert (Is_Floating_Point_Type (Typ));
6576 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
6577 end if;
6578
685094bf
RD
6579 -- Mixed-mode operations can appear in a non-static universal context,
6580 -- in which case the integer argument must be converted explicitly.
70482933 6581
533369aa 6582 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
f82944b7
JM
6583 Rewrite (Ropnd,
6584 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
70482933 6585
f82944b7 6586 Analyze_And_Resolve (Ropnd, Universal_Real);
70482933 6587
533369aa 6588 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
f82944b7
JM
6589 Rewrite (Lopnd,
6590 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
70482933 6591
f82944b7 6592 Analyze_And_Resolve (Lopnd, Universal_Real);
70482933 6593
f02b8bb8 6594 -- Non-fixed point cases, do integer zero divide and overflow checks
70482933
RK
6595
6596 elsif Is_Integer_Type (Typ) then
a91e9ac7 6597 Apply_Divide_Checks (N);
fbf5a39b 6598
f02b8bb8
RD
6599 -- Deal with Vax_Float
6600
6601 elsif Vax_Float (Typ) then
6602 Expand_Vax_Arith (N);
6603 return;
70482933
RK
6604 end if;
6605 end Expand_N_Op_Divide;
6606
6607 --------------------
6608 -- Expand_N_Op_Eq --
6609 --------------------
6610
6611 procedure Expand_N_Op_Eq (N : Node_Id) is
fbf5a39b
AC
6612 Loc : constant Source_Ptr := Sloc (N);
6613 Typ : constant Entity_Id := Etype (N);
6614 Lhs : constant Node_Id := Left_Opnd (N);
6615 Rhs : constant Node_Id := Right_Opnd (N);
6616 Bodies : constant List_Id := New_List;
6617 A_Typ : constant Entity_Id := Etype (Lhs);
6618
70482933
RK
6619 Typl : Entity_Id := A_Typ;
6620 Op_Name : Entity_Id;
6621 Prim : Elmt_Id;
70482933
RK
6622
6623 procedure Build_Equality_Call (Eq : Entity_Id);
6624 -- If a constructed equality exists for the type or for its parent,
6625 -- build and analyze call, adding conversions if the operation is
6626 -- inherited.
6627
5d09245e 6628 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
8fc789c8 6629 -- Determines whether a type has a subcomponent of an unconstrained
5d09245e
AC
6630 -- Unchecked_Union subtype. Typ is a record type.
6631
70482933
RK
6632 -------------------------
6633 -- Build_Equality_Call --
6634 -------------------------
6635
6636 procedure Build_Equality_Call (Eq : Entity_Id) is
6637 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
6638 L_Exp : Node_Id := Relocate_Node (Lhs);
6639 R_Exp : Node_Id := Relocate_Node (Rhs);
6640
6641 begin
6642 if Base_Type (Op_Type) /= Base_Type (A_Typ)
6643 and then not Is_Class_Wide_Type (A_Typ)
6644 then
6645 L_Exp := OK_Convert_To (Op_Type, L_Exp);
6646 R_Exp := OK_Convert_To (Op_Type, R_Exp);
6647 end if;
6648
5d09245e
AC
6649 -- If we have an Unchecked_Union, we need to add the inferred
6650 -- discriminant values as actuals in the function call. At this
6651 -- point, the expansion has determined that both operands have
6652 -- inferable discriminants.
6653
6654 if Is_Unchecked_Union (Op_Type) then
6655 declare
fa1608c2
ES
6656 Lhs_Type : constant Node_Id := Etype (L_Exp);
6657 Rhs_Type : constant Node_Id := Etype (R_Exp);
6658
6659 Lhs_Discr_Vals : Elist_Id;
6660 -- List of inferred discriminant values for left operand.
6661
6662 Rhs_Discr_Vals : Elist_Id;
6663 -- List of inferred discriminant values for right operand.
6664
6665 Discr : Entity_Id;
5d09245e
AC
6666
6667 begin
fa1608c2
ES
6668 Lhs_Discr_Vals := New_Elmt_List;
6669 Rhs_Discr_Vals := New_Elmt_List;
6670
5d09245e
AC
6671 -- Per-object constrained selected components require special
6672 -- attention. If the enclosing scope of the component is an
f02b8bb8 6673 -- Unchecked_Union, we cannot reference its discriminants
fa1608c2
ES
6674 -- directly. This is why we use the extra parameters of the
6675 -- equality function of the enclosing Unchecked_Union.
5d09245e
AC
6676
6677 -- type UU_Type (Discr : Integer := 0) is
6678 -- . . .
6679 -- end record;
6680 -- pragma Unchecked_Union (UU_Type);
6681
6682 -- 1. Unchecked_Union enclosing record:
6683
6684 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6685 -- . . .
6686 -- Comp : UU_Type (Discr);
6687 -- . . .
6688 -- end Enclosing_UU_Type;
6689 -- pragma Unchecked_Union (Enclosing_UU_Type);
6690
6691 -- Obj1 : Enclosing_UU_Type;
6692 -- Obj2 : Enclosing_UU_Type (1);
6693
2717634d 6694 -- [. . .] Obj1 = Obj2 [. . .]
5d09245e
AC
6695
6696 -- Generated code:
6697
6698 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6699
6700 -- A and B are the formal parameters of the equality function
6701 -- of Enclosing_UU_Type. The function always has two extra
fa1608c2
ES
6702 -- formals to capture the inferred discriminant values for
6703 -- each discriminant of the type.
5d09245e
AC
6704
6705 -- 2. Non-Unchecked_Union enclosing record:
6706
6707 -- type
6708 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6709 -- is record
6710 -- . . .
6711 -- Comp : UU_Type (Discr);
6712 -- . . .
6713 -- end Enclosing_Non_UU_Type;
6714
6715 -- Obj1 : Enclosing_Non_UU_Type;
6716 -- Obj2 : Enclosing_Non_UU_Type (1);
6717
630d30e9 6718 -- ... Obj1 = Obj2 ...
5d09245e
AC
6719
6720 -- Generated code:
6721
6722 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6723 -- obj1.discr, obj2.discr)) then
6724
6725 -- In this case we can directly reference the discriminants of
6726 -- the enclosing record.
6727
fa1608c2 6728 -- Process left operand of equality
5d09245e
AC
6729
6730 if Nkind (Lhs) = N_Selected_Component
533369aa
AC
6731 and then
6732 Has_Per_Object_Constraint (Entity (Selector_Name (Lhs)))
5d09245e 6733 then
fa1608c2
ES
6734 -- If enclosing record is an Unchecked_Union, use formals
6735 -- corresponding to each discriminant. The name of the
6736 -- formal is that of the discriminant, with added suffix,
6737 -- see Exp_Ch3.Build_Record_Equality for details.
5d09245e 6738
7675ad4f
AC
6739 if Is_Unchecked_Union
6740 (Scope (Entity (Selector_Name (Lhs))))
5d09245e 6741 then
fa1608c2
ES
6742 Discr :=
6743 First_Discriminant
6744 (Scope (Entity (Selector_Name (Lhs))));
6745 while Present (Discr) loop
6746 Append_Elmt (
6747 Make_Identifier (Loc,
6748 Chars => New_External_Name (Chars (Discr), 'A')),
6749 To => Lhs_Discr_Vals);
6750 Next_Discriminant (Discr);
6751 end loop;
5d09245e 6752
fa1608c2
ES
6753 -- If enclosing record is of a non-Unchecked_Union type, it
6754 -- is possible to reference its discriminants directly.
5d09245e
AC
6755
6756 else
fa1608c2
ES
6757 Discr := First_Discriminant (Lhs_Type);
6758 while Present (Discr) loop
6759 Append_Elmt (
6760 Make_Selected_Component (Loc,
6761 Prefix => Prefix (Lhs),
6762 Selector_Name =>
6763 New_Copy
6764 (Get_Discriminant_Value (Discr,
6765 Lhs_Type,
6766 Stored_Constraint (Lhs_Type)))),
6767 To => Lhs_Discr_Vals);
6768 Next_Discriminant (Discr);
6769 end loop;
5d09245e
AC
6770 end if;
6771
fa1608c2
ES
6772 -- Otherwise operand is on object with a constrained type.
6773 -- Infer the discriminant values from the constraint.
5d09245e
AC
6774
6775 else
fa1608c2
ES
6776
6777 Discr := First_Discriminant (Lhs_Type);
6778 while Present (Discr) loop
6779 Append_Elmt (
6780 New_Copy
6781 (Get_Discriminant_Value (Discr,
6782 Lhs_Type,
6783 Stored_Constraint (Lhs_Type))),
6784 To => Lhs_Discr_Vals);
6785 Next_Discriminant (Discr);
6786 end loop;
5d09245e
AC
6787 end if;
6788
fa1608c2 6789 -- Similar processing for right operand of equality
5d09245e
AC
6790
6791 if Nkind (Rhs) = N_Selected_Component
533369aa
AC
6792 and then
6793 Has_Per_Object_Constraint (Entity (Selector_Name (Rhs)))
5d09245e 6794 then
5e1c00fa 6795 if Is_Unchecked_Union
fa1608c2 6796 (Scope (Entity (Selector_Name (Rhs))))
5d09245e 6797 then
fa1608c2
ES
6798 Discr :=
6799 First_Discriminant
6800 (Scope (Entity (Selector_Name (Rhs))));
6801 while Present (Discr) loop
6802 Append_Elmt (
6803 Make_Identifier (Loc,
6804 Chars => New_External_Name (Chars (Discr), 'B')),
6805 To => Rhs_Discr_Vals);
6806 Next_Discriminant (Discr);
6807 end loop;
5d09245e
AC
6808
6809 else
fa1608c2
ES
6810 Discr := First_Discriminant (Rhs_Type);
6811 while Present (Discr) loop
6812 Append_Elmt (
6813 Make_Selected_Component (Loc,
6814 Prefix => Prefix (Rhs),
6815 Selector_Name =>
6816 New_Copy (Get_Discriminant_Value
6817 (Discr,
6818 Rhs_Type,
6819 Stored_Constraint (Rhs_Type)))),
6820 To => Rhs_Discr_Vals);
6821 Next_Discriminant (Discr);
6822 end loop;
5d09245e 6823 end if;
5d09245e 6824
fa1608c2
ES
6825 else
6826 Discr := First_Discriminant (Rhs_Type);
6827 while Present (Discr) loop
6828 Append_Elmt (
6829 New_Copy (Get_Discriminant_Value
6830 (Discr,
6831 Rhs_Type,
6832 Stored_Constraint (Rhs_Type))),
6833 To => Rhs_Discr_Vals);
6834 Next_Discriminant (Discr);
6835 end loop;
5d09245e
AC
6836 end if;
6837
fa1608c2
ES
6838 -- Now merge the list of discriminant values so that values
6839 -- of corresponding discriminants are adjacent.
6840
6841 declare
6842 Params : List_Id;
6843 L_Elmt : Elmt_Id;
6844 R_Elmt : Elmt_Id;
6845
6846 begin
6847 Params := New_List (L_Exp, R_Exp);
6848 L_Elmt := First_Elmt (Lhs_Discr_Vals);
6849 R_Elmt := First_Elmt (Rhs_Discr_Vals);
6850 while Present (L_Elmt) loop
6851 Append_To (Params, Node (L_Elmt));
6852 Append_To (Params, Node (R_Elmt));
6853 Next_Elmt (L_Elmt);
6854 Next_Elmt (R_Elmt);
6855 end loop;
6856
6857 Rewrite (N,
6858 Make_Function_Call (Loc,
6859 Name => New_Reference_To (Eq, Loc),
6860 Parameter_Associations => Params));
6861 end;
5d09245e
AC
6862 end;
6863
6864 -- Normal case, not an unchecked union
6865
6866 else
6867 Rewrite (N,
6868 Make_Function_Call (Loc,
fa1608c2 6869 Name => New_Reference_To (Eq, Loc),
5d09245e
AC
6870 Parameter_Associations => New_List (L_Exp, R_Exp)));
6871 end if;
70482933
RK
6872
6873 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6874 end Build_Equality_Call;
6875
5d09245e
AC
6876 ------------------------------------
6877 -- Has_Unconstrained_UU_Component --
6878 ------------------------------------
6879
6880 function Has_Unconstrained_UU_Component
6881 (Typ : Node_Id) return Boolean
6882 is
6883 Tdef : constant Node_Id :=
57848bf7 6884 Type_Definition (Declaration_Node (Base_Type (Typ)));
5d09245e
AC
6885 Clist : Node_Id;
6886 Vpart : Node_Id;
6887
6888 function Component_Is_Unconstrained_UU
6889 (Comp : Node_Id) return Boolean;
6890 -- Determines whether the subtype of the component is an
6891 -- unconstrained Unchecked_Union.
6892
6893 function Variant_Is_Unconstrained_UU
6894 (Variant : Node_Id) return Boolean;
6895 -- Determines whether a component of the variant has an unconstrained
6896 -- Unchecked_Union subtype.
6897
6898 -----------------------------------
6899 -- Component_Is_Unconstrained_UU --
6900 -----------------------------------
6901
6902 function Component_Is_Unconstrained_UU
6903 (Comp : Node_Id) return Boolean
6904 is
6905 begin
6906 if Nkind (Comp) /= N_Component_Declaration then
6907 return False;
6908 end if;
6909
6910 declare
6911 Sindic : constant Node_Id :=
6912 Subtype_Indication (Component_Definition (Comp));
6913
6914 begin
6915 -- Unconstrained nominal type. In the case of a constraint
6916 -- present, the node kind would have been N_Subtype_Indication.
6917
6918 if Nkind (Sindic) = N_Identifier then
6919 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
6920 end if;
6921
6922 return False;
6923 end;
6924 end Component_Is_Unconstrained_UU;
6925
6926 ---------------------------------
6927 -- Variant_Is_Unconstrained_UU --
6928 ---------------------------------
6929
6930 function Variant_Is_Unconstrained_UU
6931 (Variant : Node_Id) return Boolean
6932 is
6933 Clist : constant Node_Id := Component_List (Variant);
6934
6935 begin
6936 if Is_Empty_List (Component_Items (Clist)) then
6937 return False;
6938 end if;
6939
f02b8bb8
RD
6940 -- We only need to test one component
6941
5d09245e
AC
6942 declare
6943 Comp : Node_Id := First (Component_Items (Clist));
6944
6945 begin
6946 while Present (Comp) loop
5d09245e
AC
6947 if Component_Is_Unconstrained_UU (Comp) then
6948 return True;
6949 end if;
6950
6951 Next (Comp);
6952 end loop;
6953 end;
6954
6955 -- None of the components withing the variant were of
6956 -- unconstrained Unchecked_Union type.
6957
6958 return False;
6959 end Variant_Is_Unconstrained_UU;
6960
6961 -- Start of processing for Has_Unconstrained_UU_Component
6962
6963 begin
6964 if Null_Present (Tdef) then
6965 return False;
6966 end if;
6967
6968 Clist := Component_List (Tdef);
6969 Vpart := Variant_Part (Clist);
6970
6971 -- Inspect available components
6972
6973 if Present (Component_Items (Clist)) then
6974 declare
6975 Comp : Node_Id := First (Component_Items (Clist));
6976
6977 begin
6978 while Present (Comp) loop
6979
8fc789c8 6980 -- One component is sufficient
5d09245e
AC
6981
6982 if Component_Is_Unconstrained_UU (Comp) then
6983 return True;
6984 end if;
6985
6986 Next (Comp);
6987 end loop;
6988 end;
6989 end if;
6990
6991 -- Inspect available components withing variants
6992
6993 if Present (Vpart) then
6994 declare
6995 Variant : Node_Id := First (Variants (Vpart));
6996
6997 begin
6998 while Present (Variant) loop
6999
8fc789c8 7000 -- One component within a variant is sufficient
5d09245e
AC
7001
7002 if Variant_Is_Unconstrained_UU (Variant) then
7003 return True;
7004 end if;
7005
7006 Next (Variant);
7007 end loop;
7008 end;
7009 end if;
7010
7011 -- Neither the available components, nor the components inside the
7012 -- variant parts were of an unconstrained Unchecked_Union subtype.
7013
7014 return False;
7015 end Has_Unconstrained_UU_Component;
7016
70482933
RK
7017 -- Start of processing for Expand_N_Op_Eq
7018
7019 begin
7020 Binary_Op_Validity_Checks (N);
7021
456cbfa5
AC
7022 -- Deal with private types
7023
70482933
RK
7024 if Ekind (Typl) = E_Private_Type then
7025 Typl := Underlying_Type (Typl);
70482933
RK
7026 elsif Ekind (Typl) = E_Private_Subtype then
7027 Typl := Underlying_Type (Base_Type (Typl));
f02b8bb8
RD
7028 else
7029 null;
70482933
RK
7030 end if;
7031
7032 -- It may happen in error situations that the underlying type is not
7033 -- set. The error will be detected later, here we just defend the
7034 -- expander code.
7035
7036 if No (Typl) then
7037 return;
7038 end if;
7039
7040 Typl := Base_Type (Typl);
7041
456cbfa5 7042 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
60b68e56 7043 -- means we no longer have a comparison operation, we are all done.
456cbfa5
AC
7044
7045 Expand_Compare_Minimize_Eliminate_Overflow (N);
7046
7047 if Nkind (N) /= N_Op_Eq then
7048 return;
7049 end if;
7050
70482933
RK
7051 -- Boolean types (requiring handling of non-standard case)
7052
f02b8bb8 7053 if Is_Boolean_Type (Typl) then
70482933
RK
7054 Adjust_Condition (Left_Opnd (N));
7055 Adjust_Condition (Right_Opnd (N));
7056 Set_Etype (N, Standard_Boolean);
7057 Adjust_Result_Type (N, Typ);
7058
7059 -- Array types
7060
7061 elsif Is_Array_Type (Typl) then
7062
1033834f
RD
7063 -- If we are doing full validity checking, and it is possible for the
7064 -- array elements to be invalid then expand out array comparisons to
7065 -- make sure that we check the array elements.
fbf5a39b 7066
1033834f
RD
7067 if Validity_Check_Operands
7068 and then not Is_Known_Valid (Component_Type (Typl))
7069 then
fbf5a39b
AC
7070 declare
7071 Save_Force_Validity_Checks : constant Boolean :=
7072 Force_Validity_Checks;
7073 begin
7074 Force_Validity_Checks := True;
7075 Rewrite (N,
0da2c8ac
AC
7076 Expand_Array_Equality
7077 (N,
7078 Relocate_Node (Lhs),
7079 Relocate_Node (Rhs),
7080 Bodies,
7081 Typl));
7082 Insert_Actions (N, Bodies);
fbf5a39b
AC
7083 Analyze_And_Resolve (N, Standard_Boolean);
7084 Force_Validity_Checks := Save_Force_Validity_Checks;
7085 end;
7086
a9d8907c 7087 -- Packed case where both operands are known aligned
70482933 7088
a9d8907c
JM
7089 elsif Is_Bit_Packed_Array (Typl)
7090 and then not Is_Possibly_Unaligned_Object (Lhs)
7091 and then not Is_Possibly_Unaligned_Object (Rhs)
7092 then
70482933
RK
7093 Expand_Packed_Eq (N);
7094
5e1c00fa
RD
7095 -- Where the component type is elementary we can use a block bit
7096 -- comparison (if supported on the target) exception in the case
7097 -- of floating-point (negative zero issues require element by
7098 -- element comparison), and atomic types (where we must be sure
a9d8907c 7099 -- to load elements independently) and possibly unaligned arrays.
70482933 7100
70482933
RK
7101 elsif Is_Elementary_Type (Component_Type (Typl))
7102 and then not Is_Floating_Point_Type (Component_Type (Typl))
5e1c00fa 7103 and then not Is_Atomic (Component_Type (Typl))
a9d8907c
JM
7104 and then not Is_Possibly_Unaligned_Object (Lhs)
7105 and then not Is_Possibly_Unaligned_Object (Rhs)
fbf5a39b 7106 and then Support_Composite_Compare_On_Target
70482933
RK
7107 then
7108 null;
7109
685094bf
RD
7110 -- For composite and floating-point cases, expand equality loop to
7111 -- make sure of using proper comparisons for tagged types, and
7112 -- correctly handling the floating-point case.
70482933
RK
7113
7114 else
7115 Rewrite (N,
0da2c8ac
AC
7116 Expand_Array_Equality
7117 (N,
7118 Relocate_Node (Lhs),
7119 Relocate_Node (Rhs),
7120 Bodies,
7121 Typl));
70482933
RK
7122 Insert_Actions (N, Bodies, Suppress => All_Checks);
7123 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7124 end if;
7125
7126 -- Record Types
7127
7128 elsif Is_Record_Type (Typl) then
7129
7130 -- For tagged types, use the primitive "="
7131
7132 if Is_Tagged_Type (Typl) then
7133
0669bebe
GB
7134 -- No need to do anything else compiling under restriction
7135 -- No_Dispatching_Calls. During the semantic analysis we
7136 -- already notified such violation.
7137
7138 if Restriction_Active (No_Dispatching_Calls) then
7139 return;
7140 end if;
7141
685094bf
RD
7142 -- If this is derived from an untagged private type completed with
7143 -- a tagged type, it does not have a full view, so we use the
7144 -- primitive operations of the private type. This check should no
7145 -- longer be necessary when these types get their full views???
70482933
RK
7146
7147 if Is_Private_Type (A_Typ)
7148 and then not Is_Tagged_Type (A_Typ)
7149 and then Is_Derived_Type (A_Typ)
7150 and then No (Full_View (A_Typ))
7151 then
685094bf
RD
7152 -- Search for equality operation, checking that the operands
7153 -- have the same type. Note that we must find a matching entry,
7154 -- or something is very wrong!
2e071734 7155
70482933
RK
7156 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
7157
2e071734
AC
7158 while Present (Prim) loop
7159 exit when Chars (Node (Prim)) = Name_Op_Eq
7160 and then Etype (First_Formal (Node (Prim))) =
7161 Etype (Next_Formal (First_Formal (Node (Prim))))
7162 and then
7163 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7164
70482933 7165 Next_Elmt (Prim);
70482933
RK
7166 end loop;
7167
2e071734 7168 pragma Assert (Present (Prim));
70482933 7169 Op_Name := Node (Prim);
fbf5a39b
AC
7170
7171 -- Find the type's predefined equality or an overriding
685094bf 7172 -- user- defined equality. The reason for not simply calling
fbf5a39b 7173 -- Find_Prim_Op here is that there may be a user-defined
685094bf
RD
7174 -- overloaded equality op that precedes the equality that we want,
7175 -- so we have to explicitly search (e.g., there could be an
7176 -- equality with two different parameter types).
fbf5a39b 7177
70482933 7178 else
fbf5a39b
AC
7179 if Is_Class_Wide_Type (Typl) then
7180 Typl := Root_Type (Typl);
7181 end if;
7182
7183 Prim := First_Elmt (Primitive_Operations (Typl));
fbf5a39b
AC
7184 while Present (Prim) loop
7185 exit when Chars (Node (Prim)) = Name_Op_Eq
7186 and then Etype (First_Formal (Node (Prim))) =
7187 Etype (Next_Formal (First_Formal (Node (Prim))))
12e0c41c
AC
7188 and then
7189 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
fbf5a39b
AC
7190
7191 Next_Elmt (Prim);
fbf5a39b
AC
7192 end loop;
7193
2e071734 7194 pragma Assert (Present (Prim));
fbf5a39b 7195 Op_Name := Node (Prim);
70482933
RK
7196 end if;
7197
7198 Build_Equality_Call (Op_Name);
7199
5d09245e
AC
7200 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7201 -- predefined equality operator for a type which has a subcomponent
7202 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7203
7204 elsif Has_Unconstrained_UU_Component (Typl) then
7205 Insert_Action (N,
7206 Make_Raise_Program_Error (Loc,
7207 Reason => PE_Unchecked_Union_Restriction));
7208
7209 -- Prevent Gigi from generating incorrect code by rewriting the
6cb3037c 7210 -- equality as a standard False. (is this documented somewhere???)
5d09245e
AC
7211
7212 Rewrite (N,
7213 New_Occurrence_Of (Standard_False, Loc));
7214
7215 elsif Is_Unchecked_Union (Typl) then
7216
7217 -- If we can infer the discriminants of the operands, we make a
7218 -- call to the TSS equality function.
7219
7220 if Has_Inferable_Discriminants (Lhs)
7221 and then
7222 Has_Inferable_Discriminants (Rhs)
7223 then
7224 Build_Equality_Call
7225 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7226
7227 else
7228 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7229 -- the predefined equality operator for an Unchecked_Union type
7230 -- if either of the operands lack inferable discriminants.
7231
7232 Insert_Action (N,
7233 Make_Raise_Program_Error (Loc,
7234 Reason => PE_Unchecked_Union_Restriction));
7235
7236 -- Prevent Gigi from generating incorrect code by rewriting
6cb3037c 7237 -- the equality as a standard False (documented where???).
5d09245e
AC
7238
7239 Rewrite (N,
7240 New_Occurrence_Of (Standard_False, Loc));
7241
7242 end if;
7243
70482933
RK
7244 -- If a type support function is present (for complex cases), use it
7245
fbf5a39b
AC
7246 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
7247 Build_Equality_Call
7248 (TSS (Root_Type (Typl), TSS_Composite_Equality));
70482933 7249
8d80ff64
AC
7250 -- When comparing two Bounded_Strings, use the primitive equality of
7251 -- the root Super_String type.
7252
7253 elsif Is_Bounded_String (Typl) then
7254 Prim :=
7255 First_Elmt (Collect_Primitive_Operations (Root_Type (Typl)));
7256
7257 while Present (Prim) loop
7258 exit when Chars (Node (Prim)) = Name_Op_Eq
7259 and then Etype (First_Formal (Node (Prim))) =
7260 Etype (Next_Formal (First_Formal (Node (Prim))))
7261 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7262
7263 Next_Elmt (Prim);
7264 end loop;
7265
7266 -- A Super_String type should always have a primitive equality
7267
7268 pragma Assert (Present (Prim));
7269 Build_Equality_Call (Node (Prim));
7270
70482933 7271 -- Otherwise expand the component by component equality. Note that
8fc789c8 7272 -- we never use block-bit comparisons for records, because of the
70482933
RK
7273 -- problems with gaps. The backend will often be able to recombine
7274 -- the separate comparisons that we generate here.
7275
7276 else
7277 Remove_Side_Effects (Lhs);
7278 Remove_Side_Effects (Rhs);
7279 Rewrite (N,
7280 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
7281
7282 Insert_Actions (N, Bodies, Suppress => All_Checks);
7283 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7284 end if;
7285 end if;
7286
d26dc4b5 7287 -- Test if result is known at compile time
70482933 7288
d26dc4b5 7289 Rewrite_Comparison (N);
f02b8bb8
RD
7290
7291 -- If we still have comparison for Vax_Float, process it
7292
7293 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
7294 Expand_Vax_Comparison (N);
7295 return;
7296 end if;
0580d807
AC
7297
7298 Optimize_Length_Comparison (N);
70482933
RK
7299 end Expand_N_Op_Eq;
7300
7301 -----------------------
7302 -- Expand_N_Op_Expon --
7303 -----------------------
7304
7305 procedure Expand_N_Op_Expon (N : Node_Id) is
7306 Loc : constant Source_Ptr := Sloc (N);
7307 Typ : constant Entity_Id := Etype (N);
7308 Rtyp : constant Entity_Id := Root_Type (Typ);
7309 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
07fc65c4 7310 Bastyp : constant Node_Id := Etype (Base);
70482933
RK
7311 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
7312 Exptyp : constant Entity_Id := Etype (Exp);
7313 Ovflo : constant Boolean := Do_Overflow_Check (N);
7314 Expv : Uint;
70482933
RK
7315 Temp : Node_Id;
7316 Rent : RE_Id;
7317 Ent : Entity_Id;
fbf5a39b 7318 Etyp : Entity_Id;
cb42ba5d 7319 Xnode : Node_Id;
70482933
RK
7320
7321 begin
7322 Binary_Op_Validity_Checks (N);
7323
8f66cda7
AC
7324 -- CodePeer and GNATprove want to see the unexpanded N_Op_Expon node
7325
06b599fd 7326 if CodePeer_Mode or SPARK_Mode then
8f66cda7
AC
7327 return;
7328 end if;
7329
685094bf
RD
7330 -- If either operand is of a private type, then we have the use of an
7331 -- intrinsic operator, and we get rid of the privateness, by using root
7332 -- types of underlying types for the actual operation. Otherwise the
7333 -- private types will cause trouble if we expand multiplications or
7334 -- shifts etc. We also do this transformation if the result type is
7335 -- different from the base type.
07fc65c4
GB
7336
7337 if Is_Private_Type (Etype (Base))
8f66cda7
AC
7338 or else Is_Private_Type (Typ)
7339 or else Is_Private_Type (Exptyp)
7340 or else Rtyp /= Root_Type (Bastyp)
07fc65c4
GB
7341 then
7342 declare
7343 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
7344 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
7345
7346 begin
7347 Rewrite (N,
7348 Unchecked_Convert_To (Typ,
7349 Make_Op_Expon (Loc,
7350 Left_Opnd => Unchecked_Convert_To (Bt, Base),
7351 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
7352 Analyze_And_Resolve (N, Typ);
7353 return;
7354 end;
7355 end if;
7356
b6b5cca8 7357 -- Check for MINIMIZED/ELIMINATED overflow mode
6cb3037c 7358
b6b5cca8 7359 if Minimized_Eliminated_Overflow_Check (N) then
6cb3037c
AC
7360 Apply_Arithmetic_Overflow_Check (N);
7361 return;
7362 end if;
7363
cb42ba5d
AC
7364 -- Test for case of known right argument where we can replace the
7365 -- exponentiation by an equivalent expression using multiplication.
70482933
RK
7366
7367 if Compile_Time_Known_Value (Exp) then
7368 Expv := Expr_Value (Exp);
7369
7370 -- We only fold small non-negative exponents. You might think we
7371 -- could fold small negative exponents for the real case, but we
7372 -- can't because we are required to raise Constraint_Error for
7373 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
7374 -- See ACVC test C4A012B.
7375
7376 if Expv >= 0 and then Expv <= 4 then
7377
7378 -- X ** 0 = 1 (or 1.0)
7379
7380 if Expv = 0 then
abcbd24c
ST
7381
7382 -- Call Remove_Side_Effects to ensure that any side effects
7383 -- in the ignored left operand (in particular function calls
7384 -- to user defined functions) are properly executed.
7385
7386 Remove_Side_Effects (Base);
7387
70482933
RK
7388 if Ekind (Typ) in Integer_Kind then
7389 Xnode := Make_Integer_Literal (Loc, Intval => 1);
7390 else
7391 Xnode := Make_Real_Literal (Loc, Ureal_1);
7392 end if;
7393
7394 -- X ** 1 = X
7395
7396 elsif Expv = 1 then
7397 Xnode := Base;
7398
7399 -- X ** 2 = X * X
7400
7401 elsif Expv = 2 then
7402 Xnode :=
7403 Make_Op_Multiply (Loc,
7404 Left_Opnd => Duplicate_Subexpr (Base),
fbf5a39b 7405 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
70482933
RK
7406
7407 -- X ** 3 = X * X * X
7408
7409 elsif Expv = 3 then
7410 Xnode :=
7411 Make_Op_Multiply (Loc,
7412 Left_Opnd =>
7413 Make_Op_Multiply (Loc,
7414 Left_Opnd => Duplicate_Subexpr (Base),
fbf5a39b
AC
7415 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
7416 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
70482933
RK
7417
7418 -- X ** 4 ->
cb42ba5d
AC
7419
7420 -- do
70482933 7421 -- En : constant base'type := base * base;
cb42ba5d 7422 -- in
70482933
RK
7423 -- En * En
7424
cb42ba5d
AC
7425 else
7426 pragma Assert (Expv = 4);
191fcb3a 7427 Temp := Make_Temporary (Loc, 'E', Base);
70482933 7428
cb42ba5d
AC
7429 Xnode :=
7430 Make_Expression_With_Actions (Loc,
7431 Actions => New_List (
7432 Make_Object_Declaration (Loc,
7433 Defining_Identifier => Temp,
7434 Constant_Present => True,
7435 Object_Definition => New_Reference_To (Typ, Loc),
7436 Expression =>
7437 Make_Op_Multiply (Loc,
7438 Left_Opnd =>
7439 Duplicate_Subexpr (Base),
7440 Right_Opnd =>
7441 Duplicate_Subexpr_No_Checks (Base)))),
7442
70482933
RK
7443 Expression =>
7444 Make_Op_Multiply (Loc,
cb42ba5d
AC
7445 Left_Opnd => New_Reference_To (Temp, Loc),
7446 Right_Opnd => New_Reference_To (Temp, Loc)));
70482933
RK
7447 end if;
7448
7449 Rewrite (N, Xnode);
7450 Analyze_And_Resolve (N, Typ);
7451 return;
7452 end if;
7453 end if;
7454
7455 -- Case of (2 ** expression) appearing as an argument of an integer
7456 -- multiplication, or as the right argument of a division of a non-
fbf5a39b 7457 -- negative integer. In such cases we leave the node untouched, setting
70482933
RK
7458 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
7459 -- of the higher level node converts it into a shift.
7460
51bf9bdf
AC
7461 -- Another case is 2 ** N in any other context. We simply convert
7462 -- this to 1 * 2 ** N, and then the above transformation applies.
7463
685094bf
RD
7464 -- Note: this transformation is not applicable for a modular type with
7465 -- a non-binary modulus in the multiplication case, since we get a wrong
7466 -- result if the shift causes an overflow before the modular reduction.
7467
70482933
RK
7468 if Nkind (Base) = N_Integer_Literal
7469 and then Intval (Base) = 2
7470 and then Is_Integer_Type (Root_Type (Exptyp))
7471 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
7472 and then Is_Unsigned_Type (Exptyp)
7473 and then not Ovflo
70482933 7474 then
51bf9bdf 7475 -- First the multiply and divide cases
70482933 7476
51bf9bdf
AC
7477 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
7478 declare
7479 P : constant Node_Id := Parent (N);
7480 L : constant Node_Id := Left_Opnd (P);
7481 R : constant Node_Id := Right_Opnd (P);
7482
7483 begin
7484 if (Nkind (P) = N_Op_Multiply
7485 and then not Non_Binary_Modulus (Typ)
7486 and then
7487 ((Is_Integer_Type (Etype (L)) and then R = N)
7488 or else
7489 (Is_Integer_Type (Etype (R)) and then L = N))
7490 and then not Do_Overflow_Check (P))
7491 or else
7492 (Nkind (P) = N_Op_Divide
533369aa
AC
7493 and then Is_Integer_Type (Etype (L))
7494 and then Is_Unsigned_Type (Etype (L))
7495 and then R = N
7496 and then not Do_Overflow_Check (P))
51bf9bdf
AC
7497 then
7498 Set_Is_Power_Of_2_For_Shift (N);
7499 return;
7500 end if;
7501 end;
7502
7503 -- Now the other cases
7504
7505 elsif not Non_Binary_Modulus (Typ) then
7506 Rewrite (N,
7507 Make_Op_Multiply (Loc,
7508 Left_Opnd => Make_Integer_Literal (Loc, 1),
7509 Right_Opnd => Relocate_Node (N)));
7510 Analyze_And_Resolve (N, Typ);
7511 return;
7512 end if;
70482933
RK
7513 end if;
7514
07fc65c4
GB
7515 -- Fall through if exponentiation must be done using a runtime routine
7516
07fc65c4 7517 -- First deal with modular case
70482933
RK
7518
7519 if Is_Modular_Integer_Type (Rtyp) then
7520
7521 -- Non-binary case, we call the special exponentiation routine for
7522 -- the non-binary case, converting the argument to Long_Long_Integer
7523 -- and passing the modulus value. Then the result is converted back
7524 -- to the base type.
7525
7526 if Non_Binary_Modulus (Rtyp) then
70482933
RK
7527 Rewrite (N,
7528 Convert_To (Typ,
7529 Make_Function_Call (Loc,
7530 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
7531 Parameter_Associations => New_List (
7532 Convert_To (Standard_Integer, Base),
7533 Make_Integer_Literal (Loc, Modulus (Rtyp)),
7534 Exp))));
7535
685094bf
RD
7536 -- Binary case, in this case, we call one of two routines, either the
7537 -- unsigned integer case, or the unsigned long long integer case,
7538 -- with a final "and" operation to do the required mod.
70482933
RK
7539
7540 else
7541 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
7542 Ent := RTE (RE_Exp_Unsigned);
7543 else
7544 Ent := RTE (RE_Exp_Long_Long_Unsigned);
7545 end if;
7546
7547 Rewrite (N,
7548 Convert_To (Typ,
7549 Make_Op_And (Loc,
7550 Left_Opnd =>
7551 Make_Function_Call (Loc,
7552 Name => New_Reference_To (Ent, Loc),
7553 Parameter_Associations => New_List (
7554 Convert_To (Etype (First_Formal (Ent)), Base),
7555 Exp)),
7556 Right_Opnd =>
7557 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
7558
7559 end if;
7560
7561 -- Common exit point for modular type case
7562
7563 Analyze_And_Resolve (N, Typ);
7564 return;
7565
fbf5a39b
AC
7566 -- Signed integer cases, done using either Integer or Long_Long_Integer.
7567 -- It is not worth having routines for Short_[Short_]Integer, since for
7568 -- most machines it would not help, and it would generate more code that
dfd99a80 7569 -- might need certification when a certified run time is required.
70482933 7570
fbf5a39b 7571 -- In the integer cases, we have two routines, one for when overflow
dfd99a80
TQ
7572 -- checks are required, and one when they are not required, since there
7573 -- is a real gain in omitting checks on many machines.
70482933 7574
fbf5a39b
AC
7575 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
7576 or else (Rtyp = Base_Type (Standard_Long_Integer)
761f7dcb
AC
7577 and then
7578 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
7579 or else Rtyp = Universal_Integer
70482933 7580 then
fbf5a39b
AC
7581 Etyp := Standard_Long_Long_Integer;
7582
70482933
RK
7583 if Ovflo then
7584 Rent := RE_Exp_Long_Long_Integer;
7585 else
7586 Rent := RE_Exn_Long_Long_Integer;
7587 end if;
7588
fbf5a39b
AC
7589 elsif Is_Signed_Integer_Type (Rtyp) then
7590 Etyp := Standard_Integer;
70482933
RK
7591
7592 if Ovflo then
fbf5a39b 7593 Rent := RE_Exp_Integer;
70482933 7594 else
fbf5a39b 7595 Rent := RE_Exn_Integer;
70482933 7596 end if;
fbf5a39b
AC
7597
7598 -- Floating-point cases, always done using Long_Long_Float. We do not
7599 -- need separate routines for the overflow case here, since in the case
7600 -- of floating-point, we generate infinities anyway as a rule (either
7601 -- that or we automatically trap overflow), and if there is an infinity
7602 -- generated and a range check is required, the check will fail anyway.
7603
7604 else
7605 pragma Assert (Is_Floating_Point_Type (Rtyp));
7606 Etyp := Standard_Long_Long_Float;
7607 Rent := RE_Exn_Long_Long_Float;
70482933
RK
7608 end if;
7609
7610 -- Common processing for integer cases and floating-point cases.
fbf5a39b 7611 -- If we are in the right type, we can call runtime routine directly
70482933 7612
fbf5a39b 7613 if Typ = Etyp
70482933
RK
7614 and then Rtyp /= Universal_Integer
7615 and then Rtyp /= Universal_Real
7616 then
7617 Rewrite (N,
7618 Make_Function_Call (Loc,
7619 Name => New_Reference_To (RTE (Rent), Loc),
7620 Parameter_Associations => New_List (Base, Exp)));
7621
7622 -- Otherwise we have to introduce conversions (conversions are also
fbf5a39b 7623 -- required in the universal cases, since the runtime routine is
1147c704 7624 -- typed using one of the standard types).
70482933
RK
7625
7626 else
7627 Rewrite (N,
7628 Convert_To (Typ,
7629 Make_Function_Call (Loc,
7630 Name => New_Reference_To (RTE (Rent), Loc),
7631 Parameter_Associations => New_List (
fbf5a39b 7632 Convert_To (Etyp, Base),
70482933
RK
7633 Exp))));
7634 end if;
7635
7636 Analyze_And_Resolve (N, Typ);
7637 return;
7638
fbf5a39b
AC
7639 exception
7640 when RE_Not_Available =>
7641 return;
70482933
RK
7642 end Expand_N_Op_Expon;
7643
7644 --------------------
7645 -- Expand_N_Op_Ge --
7646 --------------------
7647
7648 procedure Expand_N_Op_Ge (N : Node_Id) is
7649 Typ : constant Entity_Id := Etype (N);
7650 Op1 : constant Node_Id := Left_Opnd (N);
7651 Op2 : constant Node_Id := Right_Opnd (N);
7652 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7653
7654 begin
7655 Binary_Op_Validity_Checks (N);
7656
456cbfa5 7657 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
60b68e56 7658 -- means we no longer have a comparison operation, we are all done.
456cbfa5
AC
7659
7660 Expand_Compare_Minimize_Eliminate_Overflow (N);
7661
7662 if Nkind (N) /= N_Op_Ge then
7663 return;
7664 end if;
7665
7666 -- Array type case
7667
f02b8bb8 7668 if Is_Array_Type (Typ1) then
70482933
RK
7669 Expand_Array_Comparison (N);
7670 return;
7671 end if;
7672
456cbfa5
AC
7673 -- Deal with boolean operands
7674
70482933
RK
7675 if Is_Boolean_Type (Typ1) then
7676 Adjust_Condition (Op1);
7677 Adjust_Condition (Op2);
7678 Set_Etype (N, Standard_Boolean);
7679 Adjust_Result_Type (N, Typ);
7680 end if;
7681
7682 Rewrite_Comparison (N);
f02b8bb8
RD
7683
7684 -- If we still have comparison, and Vax_Float type, process it
7685
7686 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7687 Expand_Vax_Comparison (N);
7688 return;
7689 end if;
0580d807
AC
7690
7691 Optimize_Length_Comparison (N);
70482933
RK
7692 end Expand_N_Op_Ge;
7693
7694 --------------------
7695 -- Expand_N_Op_Gt --
7696 --------------------
7697
7698 procedure Expand_N_Op_Gt (N : Node_Id) is
7699 Typ : constant Entity_Id := Etype (N);
7700 Op1 : constant Node_Id := Left_Opnd (N);
7701 Op2 : constant Node_Id := Right_Opnd (N);
7702 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7703
7704 begin
7705 Binary_Op_Validity_Checks (N);
7706
456cbfa5 7707 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
60b68e56 7708 -- means we no longer have a comparison operation, we are all done.
456cbfa5
AC
7709
7710 Expand_Compare_Minimize_Eliminate_Overflow (N);
7711
7712 if Nkind (N) /= N_Op_Gt then
7713 return;
7714 end if;
7715
7716 -- Deal with array type operands
7717
f02b8bb8 7718 if Is_Array_Type (Typ1) then
70482933
RK
7719 Expand_Array_Comparison (N);
7720 return;
7721 end if;
7722
456cbfa5
AC
7723 -- Deal with boolean type operands
7724
70482933
RK
7725 if Is_Boolean_Type (Typ1) then
7726 Adjust_Condition (Op1);
7727 Adjust_Condition (Op2);
7728 Set_Etype (N, Standard_Boolean);
7729 Adjust_Result_Type (N, Typ);
7730 end if;
7731
7732 Rewrite_Comparison (N);
f02b8bb8
RD
7733
7734 -- If we still have comparison, and Vax_Float type, process it
7735
7736 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7737 Expand_Vax_Comparison (N);
7738 return;
7739 end if;
0580d807
AC
7740
7741 Optimize_Length_Comparison (N);
70482933
RK
7742 end Expand_N_Op_Gt;
7743
7744 --------------------
7745 -- Expand_N_Op_Le --
7746 --------------------
7747
7748 procedure Expand_N_Op_Le (N : Node_Id) is
7749 Typ : constant Entity_Id := Etype (N);
7750 Op1 : constant Node_Id := Left_Opnd (N);
7751 Op2 : constant Node_Id := Right_Opnd (N);
7752 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7753
7754 begin
7755 Binary_Op_Validity_Checks (N);
7756
456cbfa5 7757 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
60b68e56 7758 -- means we no longer have a comparison operation, we are all done.
456cbfa5
AC
7759
7760 Expand_Compare_Minimize_Eliminate_Overflow (N);
7761
7762 if Nkind (N) /= N_Op_Le then
7763 return;
7764 end if;
7765
7766 -- Deal with array type operands
7767
f02b8bb8 7768 if Is_Array_Type (Typ1) then
70482933
RK
7769 Expand_Array_Comparison (N);
7770 return;
7771 end if;
7772
456cbfa5
AC
7773 -- Deal with Boolean type operands
7774
70482933
RK
7775 if Is_Boolean_Type (Typ1) then
7776 Adjust_Condition (Op1);
7777 Adjust_Condition (Op2);
7778 Set_Etype (N, Standard_Boolean);
7779 Adjust_Result_Type (N, Typ);
7780 end if;
7781
7782 Rewrite_Comparison (N);
f02b8bb8
RD
7783
7784 -- If we still have comparison, and Vax_Float type, process it
7785
7786 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7787 Expand_Vax_Comparison (N);
7788 return;
7789 end if;
0580d807
AC
7790
7791 Optimize_Length_Comparison (N);
70482933
RK
7792 end Expand_N_Op_Le;
7793
7794 --------------------
7795 -- Expand_N_Op_Lt --
7796 --------------------
7797
7798 procedure Expand_N_Op_Lt (N : Node_Id) is
7799 Typ : constant Entity_Id := Etype (N);
7800 Op1 : constant Node_Id := Left_Opnd (N);
7801 Op2 : constant Node_Id := Right_Opnd (N);
7802 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7803
7804 begin
7805 Binary_Op_Validity_Checks (N);
7806
456cbfa5 7807 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
60b68e56 7808 -- means we no longer have a comparison operation, we are all done.
456cbfa5
AC
7809
7810 Expand_Compare_Minimize_Eliminate_Overflow (N);
7811
7812 if Nkind (N) /= N_Op_Lt then
7813 return;
7814 end if;
7815
7816 -- Deal with array type operands
7817
f02b8bb8 7818 if Is_Array_Type (Typ1) then
70482933
RK
7819 Expand_Array_Comparison (N);
7820 return;
7821 end if;
7822
456cbfa5
AC
7823 -- Deal with Boolean type operands
7824
70482933
RK
7825 if Is_Boolean_Type (Typ1) then
7826 Adjust_Condition (Op1);
7827 Adjust_Condition (Op2);
7828 Set_Etype (N, Standard_Boolean);
7829 Adjust_Result_Type (N, Typ);
7830 end if;
7831
7832 Rewrite_Comparison (N);
f02b8bb8
RD
7833
7834 -- If we still have comparison, and Vax_Float type, process it
7835
7836 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7837 Expand_Vax_Comparison (N);
7838 return;
7839 end if;
0580d807
AC
7840
7841 Optimize_Length_Comparison (N);
70482933
RK
7842 end Expand_N_Op_Lt;
7843
7844 -----------------------
7845 -- Expand_N_Op_Minus --
7846 -----------------------
7847
7848 procedure Expand_N_Op_Minus (N : Node_Id) is
7849 Loc : constant Source_Ptr := Sloc (N);
7850 Typ : constant Entity_Id := Etype (N);
7851
7852 begin
7853 Unary_Op_Validity_Checks (N);
7854
b6b5cca8
AC
7855 -- Check for MINIMIZED/ELIMINATED overflow mode
7856
7857 if Minimized_Eliminated_Overflow_Check (N) then
7858 Apply_Arithmetic_Overflow_Check (N);
7859 return;
7860 end if;
7861
07fc65c4 7862 if not Backend_Overflow_Checks_On_Target
70482933
RK
7863 and then Is_Signed_Integer_Type (Etype (N))
7864 and then Do_Overflow_Check (N)
7865 then
7866 -- Software overflow checking expands -expr into (0 - expr)
7867
7868 Rewrite (N,
7869 Make_Op_Subtract (Loc,
7870 Left_Opnd => Make_Integer_Literal (Loc, 0),
7871 Right_Opnd => Right_Opnd (N)));
7872
7873 Analyze_And_Resolve (N, Typ);
7874
7875 -- Vax floating-point types case
7876
7877 elsif Vax_Float (Etype (N)) then
7878 Expand_Vax_Arith (N);
7879 end if;
7880 end Expand_N_Op_Minus;
7881
7882 ---------------------
7883 -- Expand_N_Op_Mod --
7884 ---------------------
7885
7886 procedure Expand_N_Op_Mod (N : Node_Id) is
7887 Loc : constant Source_Ptr := Sloc (N);
fbf5a39b 7888 Typ : constant Entity_Id := Etype (N);
70482933
RK
7889 DDC : constant Boolean := Do_Division_Check (N);
7890
b6b5cca8
AC
7891 Left : Node_Id;
7892 Right : Node_Id;
7893
70482933
RK
7894 LLB : Uint;
7895 Llo : Uint;
7896 Lhi : Uint;
7897 LOK : Boolean;
7898 Rlo : Uint;
7899 Rhi : Uint;
7900 ROK : Boolean;
7901
1033834f
RD
7902 pragma Warnings (Off, Lhi);
7903
70482933
RK
7904 begin
7905 Binary_Op_Validity_Checks (N);
7906
b6b5cca8
AC
7907 -- Check for MINIMIZED/ELIMINATED overflow mode
7908
7909 if Minimized_Eliminated_Overflow_Check (N) then
7910 Apply_Arithmetic_Overflow_Check (N);
7911 return;
7912 end if;
7913
9a6dc470
RD
7914 if Is_Integer_Type (Etype (N)) then
7915 Apply_Divide_Checks (N);
b6b5cca8
AC
7916
7917 -- All done if we don't have a MOD any more, which can happen as a
7918 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
7919
7920 if Nkind (N) /= N_Op_Mod then
7921 return;
7922 end if;
9a6dc470
RD
7923 end if;
7924
b6b5cca8
AC
7925 -- Proceed with expansion of mod operator
7926
7927 Left := Left_Opnd (N);
7928 Right := Right_Opnd (N);
7929
5d5e9775
AC
7930 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
7931 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
70482933
RK
7932
7933 -- Convert mod to rem if operands are known non-negative. We do this
7934 -- since it is quite likely that this will improve the quality of code,
7935 -- (the operation now corresponds to the hardware remainder), and it
7936 -- does not seem likely that it could be harmful.
7937
533369aa 7938 if LOK and then Llo >= 0 and then ROK and then Rlo >= 0 then
70482933
RK
7939 Rewrite (N,
7940 Make_Op_Rem (Sloc (N),
7941 Left_Opnd => Left_Opnd (N),
7942 Right_Opnd => Right_Opnd (N)));
7943
685094bf
RD
7944 -- Instead of reanalyzing the node we do the analysis manually. This
7945 -- avoids anomalies when the replacement is done in an instance and
7946 -- is epsilon more efficient.
70482933
RK
7947
7948 Set_Entity (N, Standard_Entity (S_Op_Rem));
fbf5a39b 7949 Set_Etype (N, Typ);
70482933
RK
7950 Set_Do_Division_Check (N, DDC);
7951 Expand_N_Op_Rem (N);
7952 Set_Analyzed (N);
7953
7954 -- Otherwise, normal mod processing
7955
7956 else
fbf5a39b
AC
7957 -- Apply optimization x mod 1 = 0. We don't really need that with
7958 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
7959 -- certainly harmless.
7960
7961 if Is_Integer_Type (Etype (N))
7962 and then Compile_Time_Known_Value (Right)
7963 and then Expr_Value (Right) = Uint_1
7964 then
abcbd24c
ST
7965 -- Call Remove_Side_Effects to ensure that any side effects in
7966 -- the ignored left operand (in particular function calls to
7967 -- user defined functions) are properly executed.
7968
7969 Remove_Side_Effects (Left);
7970
fbf5a39b
AC
7971 Rewrite (N, Make_Integer_Literal (Loc, 0));
7972 Analyze_And_Resolve (N, Typ);
7973 return;
7974 end if;
7975
70482933 7976 -- Deal with annoying case of largest negative number remainder
b9daa96e
AC
7977 -- minus one. Gigi may not handle this case correctly, because
7978 -- on some targets, the mod value is computed using a divide
7979 -- instruction which gives an overflow trap for this case.
7980
7981 -- It would be a bit more efficient to figure out which targets
7982 -- this is really needed for, but in practice it is reasonable
7983 -- to do the following special check in all cases, since it means
7984 -- we get a clearer message, and also the overhead is minimal given
7985 -- that division is expensive in any case.
70482933 7986
685094bf
RD
7987 -- In fact the check is quite easy, if the right operand is -1, then
7988 -- the mod value is always 0, and we can just ignore the left operand
7989 -- completely in this case.
70482933 7990
9a6dc470
RD
7991 -- This only applies if we still have a mod operator. Skip if we
7992 -- have already rewritten this (e.g. in the case of eliminated
7993 -- overflow checks which have driven us into bignum mode).
fbf5a39b 7994
9a6dc470 7995 if Nkind (N) = N_Op_Mod then
70482933 7996
9a6dc470
RD
7997 -- The operand type may be private (e.g. in the expansion of an
7998 -- intrinsic operation) so we must use the underlying type to get
7999 -- the bounds, and convert the literals explicitly.
70482933 8000
9a6dc470
RD
8001 LLB :=
8002 Expr_Value
8003 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
8004
8005 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
761f7dcb 8006 and then ((not LOK) or else (Llo = LLB))
9a6dc470
RD
8007 then
8008 Rewrite (N,
9b16cb57 8009 Make_If_Expression (Loc,
9a6dc470
RD
8010 Expressions => New_List (
8011 Make_Op_Eq (Loc,
8012 Left_Opnd => Duplicate_Subexpr (Right),
8013 Right_Opnd =>
8014 Unchecked_Convert_To (Typ,
8015 Make_Integer_Literal (Loc, -1))),
8016 Unchecked_Convert_To (Typ,
8017 Make_Integer_Literal (Loc, Uint_0)),
8018 Relocate_Node (N))));
8019
8020 Set_Analyzed (Next (Next (First (Expressions (N)))));
8021 Analyze_And_Resolve (N, Typ);
8022 end if;
70482933
RK
8023 end if;
8024 end if;
8025 end Expand_N_Op_Mod;
8026
8027 --------------------------
8028 -- Expand_N_Op_Multiply --
8029 --------------------------
8030
8031 procedure Expand_N_Op_Multiply (N : Node_Id) is
abcbd24c
ST
8032 Loc : constant Source_Ptr := Sloc (N);
8033 Lop : constant Node_Id := Left_Opnd (N);
8034 Rop : constant Node_Id := Right_Opnd (N);
fbf5a39b 8035
abcbd24c 8036 Lp2 : constant Boolean :=
533369aa 8037 Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop);
abcbd24c 8038 Rp2 : constant Boolean :=
533369aa 8039 Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop);
fbf5a39b 8040
70482933
RK
8041 Ltyp : constant Entity_Id := Etype (Lop);
8042 Rtyp : constant Entity_Id := Etype (Rop);
8043 Typ : Entity_Id := Etype (N);
8044
8045 begin
8046 Binary_Op_Validity_Checks (N);
8047
b6b5cca8
AC
8048 -- Check for MINIMIZED/ELIMINATED overflow mode
8049
8050 if Minimized_Eliminated_Overflow_Check (N) then
8051 Apply_Arithmetic_Overflow_Check (N);
8052 return;
8053 end if;
8054
70482933
RK
8055 -- Special optimizations for integer types
8056
8057 if Is_Integer_Type (Typ) then
8058
abcbd24c 8059 -- N * 0 = 0 for integer types
70482933 8060
abcbd24c
ST
8061 if Compile_Time_Known_Value (Rop)
8062 and then Expr_Value (Rop) = Uint_0
70482933 8063 then
abcbd24c
ST
8064 -- Call Remove_Side_Effects to ensure that any side effects in
8065 -- the ignored left operand (in particular function calls to
8066 -- user defined functions) are properly executed.
8067
8068 Remove_Side_Effects (Lop);
8069
8070 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8071 Analyze_And_Resolve (N, Typ);
8072 return;
8073 end if;
8074
8075 -- Similar handling for 0 * N = 0
8076
8077 if Compile_Time_Known_Value (Lop)
8078 and then Expr_Value (Lop) = Uint_0
8079 then
8080 Remove_Side_Effects (Rop);
70482933
RK
8081 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8082 Analyze_And_Resolve (N, Typ);
8083 return;
8084 end if;
8085
8086 -- N * 1 = 1 * N = N for integer types
8087
fbf5a39b
AC
8088 -- This optimisation is not done if we are going to
8089 -- rewrite the product 1 * 2 ** N to a shift.
8090
8091 if Compile_Time_Known_Value (Rop)
8092 and then Expr_Value (Rop) = Uint_1
8093 and then not Lp2
70482933 8094 then
fbf5a39b 8095 Rewrite (N, Lop);
70482933
RK
8096 return;
8097
fbf5a39b
AC
8098 elsif Compile_Time_Known_Value (Lop)
8099 and then Expr_Value (Lop) = Uint_1
8100 and then not Rp2
70482933 8101 then
fbf5a39b 8102 Rewrite (N, Rop);
70482933
RK
8103 return;
8104 end if;
8105 end if;
8106
70482933
RK
8107 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8108 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8109 -- operand is an integer, as required for this to work.
8110
fbf5a39b
AC
8111 if Rp2 then
8112 if Lp2 then
70482933 8113
fbf5a39b 8114 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
70482933
RK
8115
8116 Rewrite (N,
8117 Make_Op_Expon (Loc,
8118 Left_Opnd => Make_Integer_Literal (Loc, 2),
8119 Right_Opnd =>
8120 Make_Op_Add (Loc,
8121 Left_Opnd => Right_Opnd (Lop),
8122 Right_Opnd => Right_Opnd (Rop))));
8123 Analyze_And_Resolve (N, Typ);
8124 return;
8125
8126 else
eefe3761
AC
8127 -- If the result is modular, perform the reduction of the result
8128 -- appropriately.
8129
8130 if Is_Modular_Integer_Type (Typ)
8131 and then not Non_Binary_Modulus (Typ)
8132 then
8133 Rewrite (N,
573e5dd6
RD
8134 Make_Op_And (Loc,
8135 Left_Opnd =>
8136 Make_Op_Shift_Left (Loc,
8137 Left_Opnd => Lop,
8138 Right_Opnd =>
8139 Convert_To (Standard_Natural, Right_Opnd (Rop))),
8140 Right_Opnd =>
eefe3761 8141 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
573e5dd6 8142
eefe3761
AC
8143 else
8144 Rewrite (N,
8145 Make_Op_Shift_Left (Loc,
8146 Left_Opnd => Lop,
8147 Right_Opnd =>
8148 Convert_To (Standard_Natural, Right_Opnd (Rop))));
8149 end if;
8150
70482933
RK
8151 Analyze_And_Resolve (N, Typ);
8152 return;
8153 end if;
8154
8155 -- Same processing for the operands the other way round
8156
fbf5a39b 8157 elsif Lp2 then
eefe3761
AC
8158 if Is_Modular_Integer_Type (Typ)
8159 and then not Non_Binary_Modulus (Typ)
8160 then
8161 Rewrite (N,
573e5dd6
RD
8162 Make_Op_And (Loc,
8163 Left_Opnd =>
8164 Make_Op_Shift_Left (Loc,
8165 Left_Opnd => Rop,
8166 Right_Opnd =>
8167 Convert_To (Standard_Natural, Right_Opnd (Lop))),
8168 Right_Opnd =>
8169 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
8170
eefe3761
AC
8171 else
8172 Rewrite (N,
8173 Make_Op_Shift_Left (Loc,
8174 Left_Opnd => Rop,
8175 Right_Opnd =>
8176 Convert_To (Standard_Natural, Right_Opnd (Lop))));
8177 end if;
8178
70482933
RK
8179 Analyze_And_Resolve (N, Typ);
8180 return;
8181 end if;
8182
8183 -- Do required fixup of universal fixed operation
8184
8185 if Typ = Universal_Fixed then
8186 Fixup_Universal_Fixed_Operation (N);
8187 Typ := Etype (N);
8188 end if;
8189
8190 -- Multiplications with fixed-point results
8191
8192 if Is_Fixed_Point_Type (Typ) then
8193
685094bf
RD
8194 -- No special processing if Treat_Fixed_As_Integer is set, since from
8195 -- a semantic point of view such operations are simply integer
8196 -- operations and will be treated that way.
70482933
RK
8197
8198 if not Treat_Fixed_As_Integer (N) then
8199
8200 -- Case of fixed * integer => fixed
8201
8202 if Is_Integer_Type (Rtyp) then
8203 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
8204
8205 -- Case of integer * fixed => fixed
8206
8207 elsif Is_Integer_Type (Ltyp) then
8208 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
8209
8210 -- Case of fixed * fixed => fixed
8211
8212 else
8213 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
8214 end if;
8215 end if;
8216
685094bf
RD
8217 -- Other cases of multiplication of fixed-point operands. Again we
8218 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
70482933
RK
8219
8220 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
8221 and then not Treat_Fixed_As_Integer (N)
8222 then
8223 if Is_Integer_Type (Typ) then
8224 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
8225 else
8226 pragma Assert (Is_Floating_Point_Type (Typ));
8227 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
8228 end if;
8229
685094bf
RD
8230 -- Mixed-mode operations can appear in a non-static universal context,
8231 -- in which case the integer argument must be converted explicitly.
70482933 8232
533369aa 8233 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
70482933 8234 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
70482933
RK
8235 Analyze_And_Resolve (Rop, Universal_Real);
8236
533369aa 8237 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
70482933 8238 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
70482933
RK
8239 Analyze_And_Resolve (Lop, Universal_Real);
8240
8241 -- Non-fixed point cases, check software overflow checking required
8242
8243 elsif Is_Signed_Integer_Type (Etype (N)) then
8244 Apply_Arithmetic_Overflow_Check (N);
f02b8bb8
RD
8245
8246 -- Deal with VAX float case
8247
8248 elsif Vax_Float (Typ) then
8249 Expand_Vax_Arith (N);
8250 return;
70482933
RK
8251 end if;
8252 end Expand_N_Op_Multiply;
8253
8254 --------------------
8255 -- Expand_N_Op_Ne --
8256 --------------------
8257
70482933 8258 procedure Expand_N_Op_Ne (N : Node_Id) is
f02b8bb8 8259 Typ : constant Entity_Id := Etype (Left_Opnd (N));
70482933
RK
8260
8261 begin
f02b8bb8 8262 -- Case of elementary type with standard operator
70482933 8263
f02b8bb8
RD
8264 if Is_Elementary_Type (Typ)
8265 and then Sloc (Entity (N)) = Standard_Location
8266 then
8267 Binary_Op_Validity_Checks (N);
70482933 8268
456cbfa5 8269 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
60b68e56 8270 -- means we no longer have a /= operation, we are all done.
456cbfa5
AC
8271
8272 Expand_Compare_Minimize_Eliminate_Overflow (N);
8273
8274 if Nkind (N) /= N_Op_Ne then
8275 return;
8276 end if;
8277
f02b8bb8 8278 -- Boolean types (requiring handling of non-standard case)
70482933 8279
f02b8bb8
RD
8280 if Is_Boolean_Type (Typ) then
8281 Adjust_Condition (Left_Opnd (N));
8282 Adjust_Condition (Right_Opnd (N));
8283 Set_Etype (N, Standard_Boolean);
8284 Adjust_Result_Type (N, Typ);
8285 end if;
fbf5a39b 8286
f02b8bb8
RD
8287 Rewrite_Comparison (N);
8288
8289 -- If we still have comparison for Vax_Float, process it
8290
8291 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
8292 Expand_Vax_Comparison (N);
8293 return;
8294 end if;
8295
8296 -- For all cases other than elementary types, we rewrite node as the
8297 -- negation of an equality operation, and reanalyze. The equality to be
8298 -- used is defined in the same scope and has the same signature. This
8299 -- signature must be set explicitly since in an instance it may not have
8300 -- the same visibility as in the generic unit. This avoids duplicating
8301 -- or factoring the complex code for record/array equality tests etc.
8302
8303 else
8304 declare
8305 Loc : constant Source_Ptr := Sloc (N);
8306 Neg : Node_Id;
8307 Ne : constant Entity_Id := Entity (N);
8308
8309 begin
8310 Binary_Op_Validity_Checks (N);
8311
8312 Neg :=
8313 Make_Op_Not (Loc,
8314 Right_Opnd =>
8315 Make_Op_Eq (Loc,
8316 Left_Opnd => Left_Opnd (N),
8317 Right_Opnd => Right_Opnd (N)));
8318 Set_Paren_Count (Right_Opnd (Neg), 1);
8319
8320 if Scope (Ne) /= Standard_Standard then
8321 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
8322 end if;
8323
4637729f 8324 -- For navigation purposes, we want to treat the inequality as an
f02b8bb8 8325 -- implicit reference to the corresponding equality. Preserve the
4637729f 8326 -- Comes_From_ source flag to generate proper Xref entries.
f02b8bb8
RD
8327
8328 Preserve_Comes_From_Source (Neg, N);
8329 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
8330 Rewrite (N, Neg);
8331 Analyze_And_Resolve (N, Standard_Boolean);
8332 end;
8333 end if;
0580d807
AC
8334
8335 Optimize_Length_Comparison (N);
70482933
RK
8336 end Expand_N_Op_Ne;
8337
8338 ---------------------
8339 -- Expand_N_Op_Not --
8340 ---------------------
8341
685094bf 8342 -- If the argument is other than a Boolean array type, there is no special
c77599d5 8343 -- expansion required, except for VMS operations on signed integers.
70482933
RK
8344
8345 -- For the packed case, we call the special routine in Exp_Pakd, except
8346 -- that if the component size is greater than one, we use the standard
8347 -- routine generating a gruesome loop (it is so peculiar to have packed
685094bf
RD
8348 -- arrays with non-standard Boolean representations anyway, so it does not
8349 -- matter that we do not handle this case efficiently).
70482933 8350
685094bf
RD
8351 -- For the unpacked case (and for the special packed case where we have non
8352 -- standard Booleans, as discussed above), we generate and insert into the
8353 -- tree the following function definition:
70482933
RK
8354
8355 -- function Nnnn (A : arr) is
8356 -- B : arr;
8357 -- begin
8358 -- for J in a'range loop
8359 -- B (J) := not A (J);
8360 -- end loop;
8361 -- return B;
8362 -- end Nnnn;
8363
8364 -- Here arr is the actual subtype of the parameter (and hence always
8365 -- constrained). Then we replace the not with a call to this function.
8366
8367 procedure Expand_N_Op_Not (N : Node_Id) is
8368 Loc : constant Source_Ptr := Sloc (N);
8369 Typ : constant Entity_Id := Etype (N);
8370 Opnd : Node_Id;
8371 Arr : Entity_Id;
8372 A : Entity_Id;
8373 B : Entity_Id;
8374 J : Entity_Id;
8375 A_J : Node_Id;
8376 B_J : Node_Id;
8377
8378 Func_Name : Entity_Id;
8379 Loop_Statement : Node_Id;
8380
8381 begin
8382 Unary_Op_Validity_Checks (N);
8383
8384 -- For boolean operand, deal with non-standard booleans
8385
8386 if Is_Boolean_Type (Typ) then
8387 Adjust_Condition (Right_Opnd (N));
8388 Set_Etype (N, Standard_Boolean);
8389 Adjust_Result_Type (N, Typ);
8390 return;
8391 end if;
8392
880dabb5
AC
8393 -- For the VMS "not" on signed integer types, use conversion to and from
8394 -- a predefined modular type.
c77599d5
AC
8395
8396 if Is_VMS_Operator (Entity (N)) then
8397 declare
9bebf0e9
AC
8398 Rtyp : Entity_Id;
8399 Utyp : Entity_Id;
8400
c77599d5 8401 begin
9bebf0e9
AC
8402 -- If this is a derived type, retrieve original VMS type so that
8403 -- the proper sized type is used for intermediate values.
8404
8405 if Is_Derived_Type (Typ) then
8406 Rtyp := First_Subtype (Etype (Typ));
8407 else
8408 Rtyp := Typ;
8409 end if;
8410
0d901290
AC
8411 -- The proper unsigned type must have a size compatible with the
8412 -- operand, to prevent misalignment.
9bebf0e9
AC
8413
8414 if RM_Size (Rtyp) <= 8 then
8415 Utyp := RTE (RE_Unsigned_8);
8416
8417 elsif RM_Size (Rtyp) <= 16 then
8418 Utyp := RTE (RE_Unsigned_16);
8419
8420 elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then
bc20523f 8421 Utyp := RTE (RE_Unsigned_32);
9bebf0e9
AC
8422
8423 else
8424 Utyp := RTE (RE_Long_Long_Unsigned);
8425 end if;
8426
c77599d5
AC
8427 Rewrite (N,
8428 Unchecked_Convert_To (Typ,
9bebf0e9
AC
8429 Make_Op_Not (Loc,
8430 Unchecked_Convert_To (Utyp, Right_Opnd (N)))));
c77599d5
AC
8431 Analyze_And_Resolve (N, Typ);
8432 return;
8433 end;
8434 end if;
8435
da94696d 8436 -- Only array types need any other processing
70482933 8437
da94696d 8438 if not Is_Array_Type (Typ) then
70482933
RK
8439 return;
8440 end if;
8441
a9d8907c
JM
8442 -- Case of array operand. If bit packed with a component size of 1,
8443 -- handle it in Exp_Pakd if the operand is known to be aligned.
70482933 8444
a9d8907c
JM
8445 if Is_Bit_Packed_Array (Typ)
8446 and then Component_Size (Typ) = 1
8447 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
8448 then
70482933
RK
8449 Expand_Packed_Not (N);
8450 return;
8451 end if;
8452
fbf5a39b
AC
8453 -- Case of array operand which is not bit-packed. If the context is
8454 -- a safe assignment, call in-place operation, If context is a larger
8455 -- boolean expression in the context of a safe assignment, expansion is
8456 -- done by enclosing operation.
70482933
RK
8457
8458 Opnd := Relocate_Node (Right_Opnd (N));
8459 Convert_To_Actual_Subtype (Opnd);
8460 Arr := Etype (Opnd);
8461 Ensure_Defined (Arr, N);
b4592168 8462 Silly_Boolean_Array_Not_Test (N, Arr);
70482933 8463
fbf5a39b
AC
8464 if Nkind (Parent (N)) = N_Assignment_Statement then
8465 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
8466 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8467 return;
8468
5e1c00fa 8469 -- Special case the negation of a binary operation
fbf5a39b 8470
303b4d58 8471 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
fbf5a39b 8472 and then Safe_In_Place_Array_Op
303b4d58 8473 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
fbf5a39b
AC
8474 then
8475 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8476 return;
8477 end if;
8478
8479 elsif Nkind (Parent (N)) in N_Binary_Op
8480 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
8481 then
8482 declare
8483 Op1 : constant Node_Id := Left_Opnd (Parent (N));
8484 Op2 : constant Node_Id := Right_Opnd (Parent (N));
8485 Lhs : constant Node_Id := Name (Parent (Parent (N)));
8486
8487 begin
8488 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
fbf5a39b 8489
aa9a7dd7
AC
8490 -- (not A) op (not B) can be reduced to a single call
8491
8492 if N = Op1 and then Nkind (Op2) = N_Op_Not then
fbf5a39b
AC
8493 return;
8494
bed8af19
AC
8495 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
8496 return;
8497
aa9a7dd7 8498 -- A xor (not B) can also be special-cased
fbf5a39b 8499
aa9a7dd7 8500 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
fbf5a39b
AC
8501 return;
8502 end if;
8503 end if;
8504 end;
8505 end if;
8506
70482933
RK
8507 A := Make_Defining_Identifier (Loc, Name_uA);
8508 B := Make_Defining_Identifier (Loc, Name_uB);
8509 J := Make_Defining_Identifier (Loc, Name_uJ);
8510
8511 A_J :=
8512 Make_Indexed_Component (Loc,
8513 Prefix => New_Reference_To (A, Loc),
8514 Expressions => New_List (New_Reference_To (J, Loc)));
8515
8516 B_J :=
8517 Make_Indexed_Component (Loc,
8518 Prefix => New_Reference_To (B, Loc),
8519 Expressions => New_List (New_Reference_To (J, Loc)));
8520
8521 Loop_Statement :=
8522 Make_Implicit_Loop_Statement (N,
8523 Identifier => Empty,
8524
8525 Iteration_Scheme =>
8526 Make_Iteration_Scheme (Loc,
8527 Loop_Parameter_Specification =>
8528 Make_Loop_Parameter_Specification (Loc,
0d901290 8529 Defining_Identifier => J,
70482933
RK
8530 Discrete_Subtype_Definition =>
8531 Make_Attribute_Reference (Loc,
0d901290 8532 Prefix => Make_Identifier (Loc, Chars (A)),
70482933
RK
8533 Attribute_Name => Name_Range))),
8534
8535 Statements => New_List (
8536 Make_Assignment_Statement (Loc,
8537 Name => B_J,
8538 Expression => Make_Op_Not (Loc, A_J))));
8539
191fcb3a 8540 Func_Name := Make_Temporary (Loc, 'N');
70482933
RK
8541 Set_Is_Inlined (Func_Name);
8542
8543 Insert_Action (N,
8544 Make_Subprogram_Body (Loc,
8545 Specification =>
8546 Make_Function_Specification (Loc,
8547 Defining_Unit_Name => Func_Name,
8548 Parameter_Specifications => New_List (
8549 Make_Parameter_Specification (Loc,
8550 Defining_Identifier => A,
8551 Parameter_Type => New_Reference_To (Typ, Loc))),
630d30e9 8552 Result_Definition => New_Reference_To (Typ, Loc)),
70482933
RK
8553
8554 Declarations => New_List (
8555 Make_Object_Declaration (Loc,
8556 Defining_Identifier => B,
8557 Object_Definition => New_Reference_To (Arr, Loc))),
8558
8559 Handled_Statement_Sequence =>
8560 Make_Handled_Sequence_Of_Statements (Loc,
8561 Statements => New_List (
8562 Loop_Statement,
d766cee3 8563 Make_Simple_Return_Statement (Loc,
0d901290 8564 Expression => Make_Identifier (Loc, Chars (B)))))));
70482933
RK
8565
8566 Rewrite (N,
8567 Make_Function_Call (Loc,
0d901290 8568 Name => New_Reference_To (Func_Name, Loc),
70482933
RK
8569 Parameter_Associations => New_List (Opnd)));
8570
8571 Analyze_And_Resolve (N, Typ);
8572 end Expand_N_Op_Not;
8573
8574 --------------------
8575 -- Expand_N_Op_Or --
8576 --------------------
8577
8578 procedure Expand_N_Op_Or (N : Node_Id) is
8579 Typ : constant Entity_Id := Etype (N);
8580
8581 begin
8582 Binary_Op_Validity_Checks (N);
8583
8584 if Is_Array_Type (Etype (N)) then
8585 Expand_Boolean_Operator (N);
8586
8587 elsif Is_Boolean_Type (Etype (N)) then
f2d10a02
AC
8588 Adjust_Condition (Left_Opnd (N));
8589 Adjust_Condition (Right_Opnd (N));
8590 Set_Etype (N, Standard_Boolean);
8591 Adjust_Result_Type (N, Typ);
437f8c1e
AC
8592
8593 elsif Is_Intrinsic_Subprogram (Entity (N)) then
8594 Expand_Intrinsic_Call (N, Entity (N));
8595
70482933
RK
8596 end if;
8597 end Expand_N_Op_Or;
8598
8599 ----------------------
8600 -- Expand_N_Op_Plus --
8601 ----------------------
8602
8603 procedure Expand_N_Op_Plus (N : Node_Id) is
8604 begin
8605 Unary_Op_Validity_Checks (N);
b6b5cca8
AC
8606
8607 -- Check for MINIMIZED/ELIMINATED overflow mode
8608
8609 if Minimized_Eliminated_Overflow_Check (N) then
8610 Apply_Arithmetic_Overflow_Check (N);
8611 return;
8612 end if;
70482933
RK
8613 end Expand_N_Op_Plus;
8614
8615 ---------------------
8616 -- Expand_N_Op_Rem --
8617 ---------------------
8618
8619 procedure Expand_N_Op_Rem (N : Node_Id) is
8620 Loc : constant Source_Ptr := Sloc (N);
fbf5a39b 8621 Typ : constant Entity_Id := Etype (N);
70482933 8622
b6b5cca8
AC
8623 Left : Node_Id;
8624 Right : Node_Id;
70482933 8625
5d5e9775
AC
8626 Lo : Uint;
8627 Hi : Uint;
8628 OK : Boolean;
70482933 8629
5d5e9775
AC
8630 Lneg : Boolean;
8631 Rneg : Boolean;
8632 -- Set if corresponding operand can be negative
8633
8634 pragma Unreferenced (Hi);
1033834f 8635
70482933
RK
8636 begin
8637 Binary_Op_Validity_Checks (N);
8638
b6b5cca8
AC
8639 -- Check for MINIMIZED/ELIMINATED overflow mode
8640
8641 if Minimized_Eliminated_Overflow_Check (N) then
8642 Apply_Arithmetic_Overflow_Check (N);
8643 return;
8644 end if;
8645
70482933 8646 if Is_Integer_Type (Etype (N)) then
a91e9ac7 8647 Apply_Divide_Checks (N);
b6b5cca8
AC
8648
8649 -- All done if we don't have a REM any more, which can happen as a
8650 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8651
8652 if Nkind (N) /= N_Op_Rem then
8653 return;
8654 end if;
70482933
RK
8655 end if;
8656
b6b5cca8
AC
8657 -- Proceed with expansion of REM
8658
8659 Left := Left_Opnd (N);
8660 Right := Right_Opnd (N);
8661
685094bf
RD
8662 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
8663 -- but it is useful with other back ends (e.g. AAMP), and is certainly
8664 -- harmless.
fbf5a39b
AC
8665
8666 if Is_Integer_Type (Etype (N))
8667 and then Compile_Time_Known_Value (Right)
8668 and then Expr_Value (Right) = Uint_1
8669 then
abcbd24c
ST
8670 -- Call Remove_Side_Effects to ensure that any side effects in the
8671 -- ignored left operand (in particular function calls to user defined
8672 -- functions) are properly executed.
8673
8674 Remove_Side_Effects (Left);
8675
fbf5a39b
AC
8676 Rewrite (N, Make_Integer_Literal (Loc, 0));
8677 Analyze_And_Resolve (N, Typ);
8678 return;
8679 end if;
8680
685094bf 8681 -- Deal with annoying case of largest negative number remainder minus
b9daa96e
AC
8682 -- one. Gigi may not handle this case correctly, because on some
8683 -- targets, the mod value is computed using a divide instruction
8684 -- which gives an overflow trap for this case.
8685
8686 -- It would be a bit more efficient to figure out which targets this
8687 -- is really needed for, but in practice it is reasonable to do the
8688 -- following special check in all cases, since it means we get a clearer
8689 -- message, and also the overhead is minimal given that division is
8690 -- expensive in any case.
70482933 8691
685094bf
RD
8692 -- In fact the check is quite easy, if the right operand is -1, then
8693 -- the remainder is always 0, and we can just ignore the left operand
8694 -- completely in this case.
70482933 8695
5d5e9775
AC
8696 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
8697 Lneg := (not OK) or else Lo < 0;
fbf5a39b 8698
5d5e9775
AC
8699 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
8700 Rneg := (not OK) or else Lo < 0;
fbf5a39b 8701
5d5e9775
AC
8702 -- We won't mess with trying to find out if the left operand can really
8703 -- be the largest negative number (that's a pain in the case of private
8704 -- types and this is really marginal). We will just assume that we need
8705 -- the test if the left operand can be negative at all.
fbf5a39b 8706
5d5e9775 8707 if Lneg and Rneg then
70482933 8708 Rewrite (N,
9b16cb57 8709 Make_If_Expression (Loc,
70482933
RK
8710 Expressions => New_List (
8711 Make_Op_Eq (Loc,
0d901290 8712 Left_Opnd => Duplicate_Subexpr (Right),
70482933 8713 Right_Opnd =>
0d901290 8714 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
70482933 8715
fbf5a39b
AC
8716 Unchecked_Convert_To (Typ,
8717 Make_Integer_Literal (Loc, Uint_0)),
70482933
RK
8718
8719 Relocate_Node (N))));
8720
8721 Set_Analyzed (Next (Next (First (Expressions (N)))));
8722 Analyze_And_Resolve (N, Typ);
8723 end if;
8724 end Expand_N_Op_Rem;
8725
8726 -----------------------------
8727 -- Expand_N_Op_Rotate_Left --
8728 -----------------------------
8729
8730 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
8731 begin
8732 Binary_Op_Validity_Checks (N);
8733 end Expand_N_Op_Rotate_Left;
8734
8735 ------------------------------
8736 -- Expand_N_Op_Rotate_Right --
8737 ------------------------------
8738
8739 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
8740 begin
8741 Binary_Op_Validity_Checks (N);
8742 end Expand_N_Op_Rotate_Right;
8743
8744 ----------------------------
8745 -- Expand_N_Op_Shift_Left --
8746 ----------------------------
8747
8748 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
8749 begin
8750 Binary_Op_Validity_Checks (N);
8751 end Expand_N_Op_Shift_Left;
8752
8753 -----------------------------
8754 -- Expand_N_Op_Shift_Right --
8755 -----------------------------
8756
8757 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
8758 begin
8759 Binary_Op_Validity_Checks (N);
8760 end Expand_N_Op_Shift_Right;
8761
8762 ----------------------------------------
8763 -- Expand_N_Op_Shift_Right_Arithmetic --
8764 ----------------------------------------
8765
8766 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
8767 begin
8768 Binary_Op_Validity_Checks (N);
8769 end Expand_N_Op_Shift_Right_Arithmetic;
8770
8771 --------------------------
8772 -- Expand_N_Op_Subtract --
8773 --------------------------
8774
8775 procedure Expand_N_Op_Subtract (N : Node_Id) is
8776 Typ : constant Entity_Id := Etype (N);
8777
8778 begin
8779 Binary_Op_Validity_Checks (N);
8780
b6b5cca8
AC
8781 -- Check for MINIMIZED/ELIMINATED overflow mode
8782
8783 if Minimized_Eliminated_Overflow_Check (N) then
8784 Apply_Arithmetic_Overflow_Check (N);
8785 return;
8786 end if;
8787
70482933
RK
8788 -- N - 0 = N for integer types
8789
8790 if Is_Integer_Type (Typ)
8791 and then Compile_Time_Known_Value (Right_Opnd (N))
8792 and then Expr_Value (Right_Opnd (N)) = 0
8793 then
8794 Rewrite (N, Left_Opnd (N));
8795 return;
8796 end if;
8797
8fc789c8 8798 -- Arithmetic overflow checks for signed integer/fixed point types
70482933 8799
761f7dcb 8800 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
70482933
RK
8801 Apply_Arithmetic_Overflow_Check (N);
8802
0d901290 8803 -- VAX floating-point types case
70482933
RK
8804
8805 elsif Vax_Float (Typ) then
8806 Expand_Vax_Arith (N);
8807 end if;
8808 end Expand_N_Op_Subtract;
8809
8810 ---------------------
8811 -- Expand_N_Op_Xor --
8812 ---------------------
8813
8814 procedure Expand_N_Op_Xor (N : Node_Id) is
8815 Typ : constant Entity_Id := Etype (N);
8816
8817 begin
8818 Binary_Op_Validity_Checks (N);
8819
8820 if Is_Array_Type (Etype (N)) then
8821 Expand_Boolean_Operator (N);
8822
8823 elsif Is_Boolean_Type (Etype (N)) then
8824 Adjust_Condition (Left_Opnd (N));
8825 Adjust_Condition (Right_Opnd (N));
8826 Set_Etype (N, Standard_Boolean);
8827 Adjust_Result_Type (N, Typ);
437f8c1e
AC
8828
8829 elsif Is_Intrinsic_Subprogram (Entity (N)) then
8830 Expand_Intrinsic_Call (N, Entity (N));
8831
70482933
RK
8832 end if;
8833 end Expand_N_Op_Xor;
8834
8835 ----------------------
8836 -- Expand_N_Or_Else --
8837 ----------------------
8838
5875f8d6
AC
8839 procedure Expand_N_Or_Else (N : Node_Id)
8840 renames Expand_Short_Circuit_Operator;
70482933
RK
8841
8842 -----------------------------------
8843 -- Expand_N_Qualified_Expression --
8844 -----------------------------------
8845
8846 procedure Expand_N_Qualified_Expression (N : Node_Id) is
8847 Operand : constant Node_Id := Expression (N);
8848 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
8849
8850 begin
f82944b7
JM
8851 -- Do validity check if validity checking operands
8852
533369aa 8853 if Validity_Checks_On and Validity_Check_Operands then
f82944b7
JM
8854 Ensure_Valid (Operand);
8855 end if;
8856
8857 -- Apply possible constraint check
8858
70482933 8859 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
d79e621a
GD
8860
8861 if Do_Range_Check (Operand) then
8862 Set_Do_Range_Check (Operand, False);
8863 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
8864 end if;
70482933
RK
8865 end Expand_N_Qualified_Expression;
8866
a961aa79
AC
8867 ------------------------------------
8868 -- Expand_N_Quantified_Expression --
8869 ------------------------------------
8870
c0f136cd
AC
8871 -- We expand:
8872
8873 -- for all X in range => Cond
a961aa79 8874
c0f136cd 8875 -- into:
a961aa79 8876
c0f136cd
AC
8877 -- T := True;
8878 -- for X in range loop
8879 -- if not Cond then
8880 -- T := False;
8881 -- exit;
8882 -- end if;
8883 -- end loop;
90c63b09 8884
36504e5f 8885 -- Similarly, an existentially quantified expression:
90c63b09 8886
c0f136cd 8887 -- for some X in range => Cond
90c63b09 8888
c0f136cd 8889 -- becomes:
90c63b09 8890
c0f136cd
AC
8891 -- T := False;
8892 -- for X in range loop
8893 -- if Cond then
8894 -- T := True;
8895 -- exit;
8896 -- end if;
8897 -- end loop;
90c63b09 8898
c0f136cd
AC
8899 -- In both cases, the iteration may be over a container in which case it is
8900 -- given by an iterator specification, not a loop parameter specification.
a961aa79 8901
c0f136cd 8902 procedure Expand_N_Quantified_Expression (N : Node_Id) is
804670f1
AC
8903 Actions : constant List_Id := New_List;
8904 For_All : constant Boolean := All_Present (N);
8905 Iter_Spec : constant Node_Id := Iterator_Specification (N);
8906 Loc : constant Source_Ptr := Sloc (N);
8907 Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N);
8908 Cond : Node_Id;
8909 Flag : Entity_Id;
8910 Scheme : Node_Id;
8911 Stmts : List_Id;
c56a9ba4 8912
a961aa79 8913 begin
804670f1
AC
8914 -- Create the declaration of the flag which tracks the status of the
8915 -- quantified expression. Generate:
011f9d5d 8916
804670f1 8917 -- Flag : Boolean := (True | False);
011f9d5d 8918
804670f1 8919 Flag := Make_Temporary (Loc, 'T', N);
011f9d5d 8920
804670f1 8921 Append_To (Actions,
90c63b09 8922 Make_Object_Declaration (Loc,
804670f1 8923 Defining_Identifier => Flag,
c0f136cd
AC
8924 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
8925 Expression =>
804670f1
AC
8926 New_Occurrence_Of (Boolean_Literals (For_All), Loc)));
8927
8928 -- Construct the circuitry which tracks the status of the quantified
8929 -- expression. Generate:
8930
8931 -- if [not] Cond then
8932 -- Flag := (False | True);
8933 -- exit;
8934 -- end if;
a961aa79 8935
c0f136cd 8936 Cond := Relocate_Node (Condition (N));
a961aa79 8937
804670f1 8938 if For_All then
c0f136cd 8939 Cond := Make_Op_Not (Loc, Cond);
a961aa79
AC
8940 end if;
8941
804670f1 8942 Stmts := New_List (
c0f136cd
AC
8943 Make_Implicit_If_Statement (N,
8944 Condition => Cond,
8945 Then_Statements => New_List (
8946 Make_Assignment_Statement (Loc,
804670f1 8947 Name => New_Occurrence_Of (Flag, Loc),
c0f136cd 8948 Expression =>
804670f1
AC
8949 New_Occurrence_Of (Boolean_Literals (not For_All), Loc)),
8950 Make_Exit_Statement (Loc))));
8951
8952 -- Build the loop equivalent of the quantified expression
c0f136cd 8953
804670f1
AC
8954 if Present (Iter_Spec) then
8955 Scheme :=
011f9d5d 8956 Make_Iteration_Scheme (Loc,
804670f1 8957 Iterator_Specification => Iter_Spec);
c56a9ba4 8958 else
804670f1 8959 Scheme :=
011f9d5d 8960 Make_Iteration_Scheme (Loc,
804670f1 8961 Loop_Parameter_Specification => Loop_Spec);
c56a9ba4
AC
8962 end if;
8963
a961aa79
AC
8964 Append_To (Actions,
8965 Make_Loop_Statement (Loc,
804670f1
AC
8966 Iteration_Scheme => Scheme,
8967 Statements => Stmts,
c0f136cd 8968 End_Label => Empty));
a961aa79 8969
804670f1
AC
8970 -- Transform the quantified expression
8971
a961aa79
AC
8972 Rewrite (N,
8973 Make_Expression_With_Actions (Loc,
804670f1 8974 Expression => New_Occurrence_Of (Flag, Loc),
a961aa79 8975 Actions => Actions));
a961aa79
AC
8976 Analyze_And_Resolve (N, Standard_Boolean);
8977 end Expand_N_Quantified_Expression;
8978
70482933
RK
8979 ---------------------------------
8980 -- Expand_N_Selected_Component --
8981 ---------------------------------
8982
70482933
RK
8983 procedure Expand_N_Selected_Component (N : Node_Id) is
8984 Loc : constant Source_Ptr := Sloc (N);
8985 Par : constant Node_Id := Parent (N);
8986 P : constant Node_Id := Prefix (N);
03eb6036 8987 S : constant Node_Id := Selector_Name (N);
fbf5a39b 8988 Ptyp : Entity_Id := Underlying_Type (Etype (P));
70482933 8989 Disc : Entity_Id;
70482933 8990 New_N : Node_Id;
fbf5a39b 8991 Dcon : Elmt_Id;
d606f1df 8992 Dval : Node_Id;
70482933
RK
8993
8994 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
8995 -- Gigi needs a temporary for prefixes that depend on a discriminant,
8996 -- unless the context of an assignment can provide size information.
fbf5a39b
AC
8997 -- Don't we have a general routine that does this???
8998
53f29d4f
AC
8999 function Is_Subtype_Declaration return Boolean;
9000 -- The replacement of a discriminant reference by its value is required
4317e442
AC
9001 -- if this is part of the initialization of an temporary generated by a
9002 -- change of representation. This shows up as the construction of a
53f29d4f 9003 -- discriminant constraint for a subtype declared at the same point as
4317e442
AC
9004 -- the entity in the prefix of the selected component. We recognize this
9005 -- case when the context of the reference is:
9006 -- subtype ST is T(Obj.D);
9007 -- where the entity for Obj comes from source, and ST has the same sloc.
53f29d4f 9008
fbf5a39b
AC
9009 -----------------------
9010 -- In_Left_Hand_Side --
9011 -----------------------
70482933
RK
9012
9013 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
9014 begin
fbf5a39b 9015 return (Nkind (Parent (Comp)) = N_Assignment_Statement
90c63b09 9016 and then Comp = Name (Parent (Comp)))
fbf5a39b 9017 or else (Present (Parent (Comp))
90c63b09
AC
9018 and then Nkind (Parent (Comp)) in N_Subexpr
9019 and then In_Left_Hand_Side (Parent (Comp)));
70482933
RK
9020 end In_Left_Hand_Side;
9021
53f29d4f
AC
9022 -----------------------------
9023 -- Is_Subtype_Declaration --
9024 -----------------------------
9025
9026 function Is_Subtype_Declaration return Boolean is
9027 Par : constant Node_Id := Parent (N);
53f29d4f
AC
9028 begin
9029 return
9030 Nkind (Par) = N_Index_Or_Discriminant_Constraint
9031 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
9032 and then Comes_From_Source (Entity (Prefix (N)))
9033 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
9034 end Is_Subtype_Declaration;
9035
fbf5a39b
AC
9036 -- Start of processing for Expand_N_Selected_Component
9037
70482933 9038 begin
fbf5a39b
AC
9039 -- Insert explicit dereference if required
9040
9041 if Is_Access_Type (Ptyp) then
702d2020
AC
9042
9043 -- First set prefix type to proper access type, in case it currently
9044 -- has a private (non-access) view of this type.
9045
9046 Set_Etype (P, Ptyp);
9047
fbf5a39b 9048 Insert_Explicit_Dereference (P);
e6f69614 9049 Analyze_And_Resolve (P, Designated_Type (Ptyp));
fbf5a39b
AC
9050
9051 if Ekind (Etype (P)) = E_Private_Subtype
9052 and then Is_For_Access_Subtype (Etype (P))
9053 then
9054 Set_Etype (P, Base_Type (Etype (P)));
9055 end if;
9056
9057 Ptyp := Etype (P);
9058 end if;
9059
9060 -- Deal with discriminant check required
9061
70482933 9062 if Do_Discriminant_Check (N) then
03eb6036
AC
9063 if Present (Discriminant_Checking_Func
9064 (Original_Record_Component (Entity (S))))
9065 then
9066 -- Present the discriminant checking function to the backend, so
9067 -- that it can inline the call to the function.
9068
9069 Add_Inlined_Body
9070 (Discriminant_Checking_Func
9071 (Original_Record_Component (Entity (S))));
70482933 9072
03eb6036 9073 -- Now reset the flag and generate the call
70482933 9074
03eb6036
AC
9075 Set_Do_Discriminant_Check (N, False);
9076 Generate_Discriminant_Check (N);
70482933 9077
03eb6036
AC
9078 -- In the case of Unchecked_Union, no discriminant checking is
9079 -- actually performed.
70482933 9080
03eb6036
AC
9081 else
9082 Set_Do_Discriminant_Check (N, False);
9083 end if;
70482933
RK
9084 end if;
9085
b4592168
GD
9086 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9087 -- function, then additional actuals must be passed.
9088
0791fbe9 9089 if Ada_Version >= Ada_2005
b4592168
GD
9090 and then Is_Build_In_Place_Function_Call (P)
9091 then
9092 Make_Build_In_Place_Call_In_Anonymous_Context (P);
9093 end if;
9094
fbf5a39b
AC
9095 -- Gigi cannot handle unchecked conversions that are the prefix of a
9096 -- selected component with discriminants. This must be checked during
9097 -- expansion, because during analysis the type of the selector is not
9098 -- known at the point the prefix is analyzed. If the conversion is the
9099 -- target of an assignment, then we cannot force the evaluation.
70482933
RK
9100
9101 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
9102 and then Has_Discriminants (Etype (N))
9103 and then not In_Left_Hand_Side (N)
9104 then
9105 Force_Evaluation (Prefix (N));
9106 end if;
9107
9108 -- Remaining processing applies only if selector is a discriminant
9109
9110 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
9111
9112 -- If the selector is a discriminant of a constrained record type,
fbf5a39b
AC
9113 -- we may be able to rewrite the expression with the actual value
9114 -- of the discriminant, a useful optimization in some cases.
70482933
RK
9115
9116 if Is_Record_Type (Ptyp)
9117 and then Has_Discriminants (Ptyp)
9118 and then Is_Constrained (Ptyp)
70482933 9119 then
fbf5a39b
AC
9120 -- Do this optimization for discrete types only, and not for
9121 -- access types (access discriminants get us into trouble!)
70482933 9122
fbf5a39b
AC
9123 if not Is_Discrete_Type (Etype (N)) then
9124 null;
9125
9126 -- Don't do this on the left hand of an assignment statement.
0d901290
AC
9127 -- Normally one would think that references like this would not
9128 -- occur, but they do in generated code, and mean that we really
9129 -- do want to assign the discriminant!
fbf5a39b
AC
9130
9131 elsif Nkind (Par) = N_Assignment_Statement
9132 and then Name (Par) = N
9133 then
9134 null;
9135
685094bf 9136 -- Don't do this optimization for the prefix of an attribute or
e2534738 9137 -- the name of an object renaming declaration since these are
685094bf 9138 -- contexts where we do not want the value anyway.
fbf5a39b
AC
9139
9140 elsif (Nkind (Par) = N_Attribute_Reference
533369aa 9141 and then Prefix (Par) = N)
fbf5a39b
AC
9142 or else Is_Renamed_Object (N)
9143 then
9144 null;
9145
9146 -- Don't do this optimization if we are within the code for a
9147 -- discriminant check, since the whole point of such a check may
9148 -- be to verify the condition on which the code below depends!
9149
9150 elsif Is_In_Discriminant_Check (N) then
9151 null;
9152
9153 -- Green light to see if we can do the optimization. There is
685094bf
RD
9154 -- still one condition that inhibits the optimization below but
9155 -- now is the time to check the particular discriminant.
fbf5a39b
AC
9156
9157 else
685094bf
RD
9158 -- Loop through discriminants to find the matching discriminant
9159 -- constraint to see if we can copy it.
fbf5a39b
AC
9160
9161 Disc := First_Discriminant (Ptyp);
9162 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
9163 Discr_Loop : while Present (Dcon) loop
d606f1df 9164 Dval := Node (Dcon);
fbf5a39b 9165
bd949ee2
RD
9166 -- Check if this is the matching discriminant and if the
9167 -- discriminant value is simple enough to make sense to
9168 -- copy. We don't want to copy complex expressions, and
9169 -- indeed to do so can cause trouble (before we put in
9170 -- this guard, a discriminant expression containing an
e7d897b8 9171 -- AND THEN was copied, causing problems for coverage
c228a069 9172 -- analysis tools).
bd949ee2 9173
53f29d4f
AC
9174 -- However, if the reference is part of the initialization
9175 -- code generated for an object declaration, we must use
9176 -- the discriminant value from the subtype constraint,
9177 -- because the selected component may be a reference to the
9178 -- object being initialized, whose discriminant is not yet
9179 -- set. This only happens in complex cases involving changes
9180 -- or representation.
9181
bd949ee2
RD
9182 if Disc = Entity (Selector_Name (N))
9183 and then (Is_Entity_Name (Dval)
170b2989
AC
9184 or else Compile_Time_Known_Value (Dval)
9185 or else Is_Subtype_Declaration)
bd949ee2 9186 then
fbf5a39b
AC
9187 -- Here we have the matching discriminant. Check for
9188 -- the case of a discriminant of a component that is
9189 -- constrained by an outer discriminant, which cannot
9190 -- be optimized away.
9191
d606f1df
AC
9192 if Denotes_Discriminant
9193 (Dval, Check_Concurrent => True)
9194 then
9195 exit Discr_Loop;
9196
9197 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
9198 and then
9199 Denotes_Discriminant
9200 (Selector_Name (Original_Node (Dval)), True)
9201 then
9202 exit Discr_Loop;
9203
9204 -- Do not retrieve value if constraint is not static. It
9205 -- is generally not useful, and the constraint may be a
9206 -- rewritten outer discriminant in which case it is in
9207 -- fact incorrect.
9208
9209 elsif Is_Entity_Name (Dval)
d606f1df 9210 and then
533369aa
AC
9211 Nkind (Parent (Entity (Dval))) = N_Object_Declaration
9212 and then Present (Expression (Parent (Entity (Dval))))
9213 and then not
9214 Is_Static_Expression
d606f1df 9215 (Expression (Parent (Entity (Dval))))
fbf5a39b
AC
9216 then
9217 exit Discr_Loop;
70482933 9218
685094bf
RD
9219 -- In the context of a case statement, the expression may
9220 -- have the base type of the discriminant, and we need to
9221 -- preserve the constraint to avoid spurious errors on
9222 -- missing cases.
70482933 9223
fbf5a39b 9224 elsif Nkind (Parent (N)) = N_Case_Statement
d606f1df 9225 and then Etype (Dval) /= Etype (Disc)
70482933
RK
9226 then
9227 Rewrite (N,
9228 Make_Qualified_Expression (Loc,
fbf5a39b
AC
9229 Subtype_Mark =>
9230 New_Occurrence_Of (Etype (Disc), Loc),
9231 Expression =>
d606f1df 9232 New_Copy_Tree (Dval)));
ffe9aba8 9233 Analyze_And_Resolve (N, Etype (Disc));
fbf5a39b
AC
9234
9235 -- In case that comes out as a static expression,
9236 -- reset it (a selected component is never static).
9237
9238 Set_Is_Static_Expression (N, False);
9239 return;
9240
9241 -- Otherwise we can just copy the constraint, but the
ffe9aba8
AC
9242 -- result is certainly not static! In some cases the
9243 -- discriminant constraint has been analyzed in the
9244 -- context of the original subtype indication, but for
9245 -- itypes the constraint might not have been analyzed
9246 -- yet, and this must be done now.
fbf5a39b 9247
70482933 9248 else
d606f1df 9249 Rewrite (N, New_Copy_Tree (Dval));
ffe9aba8 9250 Analyze_And_Resolve (N);
fbf5a39b
AC
9251 Set_Is_Static_Expression (N, False);
9252 return;
70482933 9253 end if;
70482933
RK
9254 end if;
9255
fbf5a39b
AC
9256 Next_Elmt (Dcon);
9257 Next_Discriminant (Disc);
9258 end loop Discr_Loop;
70482933 9259
fbf5a39b
AC
9260 -- Note: the above loop should always find a matching
9261 -- discriminant, but if it does not, we just missed an
c228a069
AC
9262 -- optimization due to some glitch (perhaps a previous
9263 -- error), so ignore.
fbf5a39b
AC
9264
9265 end if;
70482933
RK
9266 end if;
9267
9268 -- The only remaining processing is in the case of a discriminant of
9269 -- a concurrent object, where we rewrite the prefix to denote the
9270 -- corresponding record type. If the type is derived and has renamed
9271 -- discriminants, use corresponding discriminant, which is the one
9272 -- that appears in the corresponding record.
9273
9274 if not Is_Concurrent_Type (Ptyp) then
9275 return;
9276 end if;
9277
9278 Disc := Entity (Selector_Name (N));
9279
9280 if Is_Derived_Type (Ptyp)
9281 and then Present (Corresponding_Discriminant (Disc))
9282 then
9283 Disc := Corresponding_Discriminant (Disc);
9284 end if;
9285
9286 New_N :=
9287 Make_Selected_Component (Loc,
9288 Prefix =>
9289 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
9290 New_Copy_Tree (P)),
9291 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
9292
9293 Rewrite (N, New_N);
9294 Analyze (N);
9295 end if;
5972791c 9296
73fe1679 9297 -- Set Atomic_Sync_Required if necessary for atomic component
5972791c 9298
73fe1679
AC
9299 if Nkind (N) = N_Selected_Component then
9300 declare
9301 E : constant Entity_Id := Entity (Selector_Name (N));
9302 Set : Boolean;
9303
9304 begin
9305 -- If component is atomic, but type is not, setting depends on
9306 -- disable/enable state for the component.
9307
9308 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
9309 Set := not Atomic_Synchronization_Disabled (E);
9310
9311 -- If component is not atomic, but its type is atomic, setting
9312 -- depends on disable/enable state for the type.
9313
9314 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9315 Set := not Atomic_Synchronization_Disabled (Etype (E));
9316
9317 -- If both component and type are atomic, we disable if either
9318 -- component or its type have sync disabled.
9319
9320 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9321 Set := (not Atomic_Synchronization_Disabled (E))
9322 and then
9323 (not Atomic_Synchronization_Disabled (Etype (E)));
9324
9325 else
9326 Set := False;
9327 end if;
9328
9329 -- Set flag if required
9330
9331 if Set then
9332 Activate_Atomic_Synchronization (N);
9333 end if;
9334 end;
5972791c 9335 end if;
70482933
RK
9336 end Expand_N_Selected_Component;
9337
9338 --------------------
9339 -- Expand_N_Slice --
9340 --------------------
9341
9342 procedure Expand_N_Slice (N : Node_Id) is
9343 Loc : constant Source_Ptr := Sloc (N);
9344 Typ : constant Entity_Id := Etype (N);
9345 Pfx : constant Node_Id := Prefix (N);
9346 Ptp : Entity_Id := Etype (Pfx);
fbf5a39b 9347
81a5b587 9348 function Is_Procedure_Actual (N : Node_Id) return Boolean;
685094bf
RD
9349 -- Check whether the argument is an actual for a procedure call, in
9350 -- which case the expansion of a bit-packed slice is deferred until the
9351 -- call itself is expanded. The reason this is required is that we might
9352 -- have an IN OUT or OUT parameter, and the copy out is essential, and
9353 -- that copy out would be missed if we created a temporary here in
9354 -- Expand_N_Slice. Note that we don't bother to test specifically for an
9355 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
9356 -- is harmless to defer expansion in the IN case, since the call
9357 -- processing will still generate the appropriate copy in operation,
9358 -- which will take care of the slice.
81a5b587 9359
b01bf852 9360 procedure Make_Temporary_For_Slice;
685094bf
RD
9361 -- Create a named variable for the value of the slice, in cases where
9362 -- the back-end cannot handle it properly, e.g. when packed types or
9363 -- unaligned slices are involved.
fbf5a39b 9364
81a5b587
AC
9365 -------------------------
9366 -- Is_Procedure_Actual --
9367 -------------------------
9368
9369 function Is_Procedure_Actual (N : Node_Id) return Boolean is
9370 Par : Node_Id := Parent (N);
08aa9a4a 9371
81a5b587 9372 begin
81a5b587 9373 loop
c6a60aa1
RD
9374 -- If our parent is a procedure call we can return
9375
81a5b587
AC
9376 if Nkind (Par) = N_Procedure_Call_Statement then
9377 return True;
6b6fcd3e 9378
685094bf
RD
9379 -- If our parent is a type conversion, keep climbing the tree,
9380 -- since a type conversion can be a procedure actual. Also keep
9381 -- climbing if parameter association or a qualified expression,
9382 -- since these are additional cases that do can appear on
9383 -- procedure actuals.
6b6fcd3e 9384
303b4d58
AC
9385 elsif Nkind_In (Par, N_Type_Conversion,
9386 N_Parameter_Association,
9387 N_Qualified_Expression)
c6a60aa1 9388 then
81a5b587 9389 Par := Parent (Par);
c6a60aa1
RD
9390
9391 -- Any other case is not what we are looking for
9392
9393 else
9394 return False;
81a5b587
AC
9395 end if;
9396 end loop;
81a5b587
AC
9397 end Is_Procedure_Actual;
9398
b01bf852
AC
9399 ------------------------------
9400 -- Make_Temporary_For_Slice --
9401 ------------------------------
fbf5a39b 9402
b01bf852 9403 procedure Make_Temporary_For_Slice is
fbf5a39b 9404 Decl : Node_Id;
b01bf852 9405 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
13d923cc 9406
fbf5a39b
AC
9407 begin
9408 Decl :=
9409 Make_Object_Declaration (Loc,
9410 Defining_Identifier => Ent,
9411 Object_Definition => New_Occurrence_Of (Typ, Loc));
9412
9413 Set_No_Initialization (Decl);
9414
9415 Insert_Actions (N, New_List (
9416 Decl,
9417 Make_Assignment_Statement (Loc,
9418 Name => New_Occurrence_Of (Ent, Loc),
9419 Expression => Relocate_Node (N))));
9420
9421 Rewrite (N, New_Occurrence_Of (Ent, Loc));
9422 Analyze_And_Resolve (N, Typ);
b01bf852 9423 end Make_Temporary_For_Slice;
fbf5a39b
AC
9424
9425 -- Start of processing for Expand_N_Slice
70482933
RK
9426
9427 begin
9428 -- Special handling for access types
9429
9430 if Is_Access_Type (Ptp) then
9431
70482933
RK
9432 Ptp := Designated_Type (Ptp);
9433
e6f69614
AC
9434 Rewrite (Pfx,
9435 Make_Explicit_Dereference (Sloc (N),
9436 Prefix => Relocate_Node (Pfx)));
70482933 9437
e6f69614 9438 Analyze_And_Resolve (Pfx, Ptp);
70482933
RK
9439 end if;
9440
b4592168
GD
9441 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9442 -- function, then additional actuals must be passed.
9443
0791fbe9 9444 if Ada_Version >= Ada_2005
b4592168
GD
9445 and then Is_Build_In_Place_Function_Call (Pfx)
9446 then
9447 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
9448 end if;
9449
70482933
RK
9450 -- The remaining case to be handled is packed slices. We can leave
9451 -- packed slices as they are in the following situations:
9452
9453 -- 1. Right or left side of an assignment (we can handle this
9454 -- situation correctly in the assignment statement expansion).
9455
685094bf
RD
9456 -- 2. Prefix of indexed component (the slide is optimized away in this
9457 -- case, see the start of Expand_N_Slice.)
70482933 9458
685094bf
RD
9459 -- 3. Object renaming declaration, since we want the name of the
9460 -- slice, not the value.
70482933 9461
685094bf
RD
9462 -- 4. Argument to procedure call, since copy-in/copy-out handling may
9463 -- be required, and this is handled in the expansion of call
9464 -- itself.
70482933 9465
685094bf
RD
9466 -- 5. Prefix of an address attribute (this is an error which is caught
9467 -- elsewhere, and the expansion would interfere with generating the
9468 -- error message).
70482933 9469
81a5b587 9470 if not Is_Packed (Typ) then
08aa9a4a 9471
685094bf
RD
9472 -- Apply transformation for actuals of a function call, where
9473 -- Expand_Actuals is not used.
81a5b587
AC
9474
9475 if Nkind (Parent (N)) = N_Function_Call
9476 and then Is_Possibly_Unaligned_Slice (N)
9477 then
b01bf852 9478 Make_Temporary_For_Slice;
81a5b587
AC
9479 end if;
9480
9481 elsif Nkind (Parent (N)) = N_Assignment_Statement
9482 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
533369aa 9483 and then Parent (N) = Name (Parent (Parent (N))))
70482933 9484 then
81a5b587 9485 return;
70482933 9486
81a5b587
AC
9487 elsif Nkind (Parent (N)) = N_Indexed_Component
9488 or else Is_Renamed_Object (N)
9489 or else Is_Procedure_Actual (N)
9490 then
9491 return;
70482933 9492
91b1417d
AC
9493 elsif Nkind (Parent (N)) = N_Attribute_Reference
9494 and then Attribute_Name (Parent (N)) = Name_Address
fbf5a39b 9495 then
81a5b587
AC
9496 return;
9497
9498 else
b01bf852 9499 Make_Temporary_For_Slice;
70482933
RK
9500 end if;
9501 end Expand_N_Slice;
9502
9503 ------------------------------
9504 -- Expand_N_Type_Conversion --
9505 ------------------------------
9506
9507 procedure Expand_N_Type_Conversion (N : Node_Id) is
9508 Loc : constant Source_Ptr := Sloc (N);
9509 Operand : constant Node_Id := Expression (N);
9510 Target_Type : constant Entity_Id := Etype (N);
9511 Operand_Type : Entity_Id := Etype (Operand);
9512
9513 procedure Handle_Changed_Representation;
685094bf
RD
9514 -- This is called in the case of record and array type conversions to
9515 -- see if there is a change of representation to be handled. Change of
9516 -- representation is actually handled at the assignment statement level,
9517 -- and what this procedure does is rewrite node N conversion as an
9518 -- assignment to temporary. If there is no change of representation,
9519 -- then the conversion node is unchanged.
70482933 9520
426908f8
RD
9521 procedure Raise_Accessibility_Error;
9522 -- Called when we know that an accessibility check will fail. Rewrites
9523 -- node N to an appropriate raise statement and outputs warning msgs.
9524 -- The Etype of the raise node is set to Target_Type.
9525
70482933
RK
9526 procedure Real_Range_Check;
9527 -- Handles generation of range check for real target value
9528
d15f9422
AC
9529 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
9530 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
9531 -- evaluates to True.
9532
70482933
RK
9533 -----------------------------------
9534 -- Handle_Changed_Representation --
9535 -----------------------------------
9536
9537 procedure Handle_Changed_Representation is
9538 Temp : Entity_Id;
9539 Decl : Node_Id;
9540 Odef : Node_Id;
9541 Disc : Node_Id;
9542 N_Ix : Node_Id;
9543 Cons : List_Id;
9544
9545 begin
f82944b7 9546 -- Nothing else to do if no change of representation
70482933
RK
9547
9548 if Same_Representation (Operand_Type, Target_Type) then
9549 return;
9550
9551 -- The real change of representation work is done by the assignment
9552 -- statement processing. So if this type conversion is appearing as
9553 -- the expression of an assignment statement, nothing needs to be
9554 -- done to the conversion.
9555
9556 elsif Nkind (Parent (N)) = N_Assignment_Statement then
9557 return;
9558
9559 -- Otherwise we need to generate a temporary variable, and do the
9560 -- change of representation assignment into that temporary variable.
9561 -- The conversion is then replaced by a reference to this variable.
9562
9563 else
9564 Cons := No_List;
9565
685094bf
RD
9566 -- If type is unconstrained we have to add a constraint, copied
9567 -- from the actual value of the left hand side.
70482933
RK
9568
9569 if not Is_Constrained (Target_Type) then
9570 if Has_Discriminants (Operand_Type) then
9571 Disc := First_Discriminant (Operand_Type);
fbf5a39b
AC
9572
9573 if Disc /= First_Stored_Discriminant (Operand_Type) then
9574 Disc := First_Stored_Discriminant (Operand_Type);
9575 end if;
9576
70482933
RK
9577 Cons := New_List;
9578 while Present (Disc) loop
9579 Append_To (Cons,
9580 Make_Selected_Component (Loc,
7675ad4f
AC
9581 Prefix =>
9582 Duplicate_Subexpr_Move_Checks (Operand),
70482933
RK
9583 Selector_Name =>
9584 Make_Identifier (Loc, Chars (Disc))));
9585 Next_Discriminant (Disc);
9586 end loop;
9587
9588 elsif Is_Array_Type (Operand_Type) then
9589 N_Ix := First_Index (Target_Type);
9590 Cons := New_List;
9591
9592 for J in 1 .. Number_Dimensions (Operand_Type) loop
9593
9594 -- We convert the bounds explicitly. We use an unchecked
9595 -- conversion because bounds checks are done elsewhere.
9596
9597 Append_To (Cons,
9598 Make_Range (Loc,
9599 Low_Bound =>
9600 Unchecked_Convert_To (Etype (N_Ix),
9601 Make_Attribute_Reference (Loc,
9602 Prefix =>
fbf5a39b 9603 Duplicate_Subexpr_No_Checks
70482933
RK
9604 (Operand, Name_Req => True),
9605 Attribute_Name => Name_First,
9606 Expressions => New_List (
9607 Make_Integer_Literal (Loc, J)))),
9608
9609 High_Bound =>
9610 Unchecked_Convert_To (Etype (N_Ix),
9611 Make_Attribute_Reference (Loc,
9612 Prefix =>
fbf5a39b 9613 Duplicate_Subexpr_No_Checks
70482933
RK
9614 (Operand, Name_Req => True),
9615 Attribute_Name => Name_Last,
9616 Expressions => New_List (
9617 Make_Integer_Literal (Loc, J))))));
9618
9619 Next_Index (N_Ix);
9620 end loop;
9621 end if;
9622 end if;
9623
9624 Odef := New_Occurrence_Of (Target_Type, Loc);
9625
9626 if Present (Cons) then
9627 Odef :=
9628 Make_Subtype_Indication (Loc,
9629 Subtype_Mark => Odef,
9630 Constraint =>
9631 Make_Index_Or_Discriminant_Constraint (Loc,
9632 Constraints => Cons));
9633 end if;
9634
191fcb3a 9635 Temp := Make_Temporary (Loc, 'C');
70482933
RK
9636 Decl :=
9637 Make_Object_Declaration (Loc,
9638 Defining_Identifier => Temp,
9639 Object_Definition => Odef);
9640
9641 Set_No_Initialization (Decl, True);
9642
9643 -- Insert required actions. It is essential to suppress checks
9644 -- since we have suppressed default initialization, which means
9645 -- that the variable we create may have no discriminants.
9646
9647 Insert_Actions (N,
9648 New_List (
9649 Decl,
9650 Make_Assignment_Statement (Loc,
9651 Name => New_Occurrence_Of (Temp, Loc),
9652 Expression => Relocate_Node (N))),
9653 Suppress => All_Checks);
9654
9655 Rewrite (N, New_Occurrence_Of (Temp, Loc));
9656 return;
9657 end if;
9658 end Handle_Changed_Representation;
9659
426908f8
RD
9660 -------------------------------
9661 -- Raise_Accessibility_Error --
9662 -------------------------------
9663
9664 procedure Raise_Accessibility_Error is
9665 begin
9666 Rewrite (N,
9667 Make_Raise_Program_Error (Sloc (N),
9668 Reason => PE_Accessibility_Check_Failed));
9669 Set_Etype (N, Target_Type);
9670
324ac540
AC
9671 Error_Msg_N
9672 ("??accessibility check failure", N);
426908f8 9673 Error_Msg_NE
324ac540 9674 ("\??& will be raised at run time", N, Standard_Program_Error);
426908f8
RD
9675 end Raise_Accessibility_Error;
9676
70482933
RK
9677 ----------------------
9678 -- Real_Range_Check --
9679 ----------------------
9680
685094bf
RD
9681 -- Case of conversions to floating-point or fixed-point. If range checks
9682 -- are enabled and the target type has a range constraint, we convert:
70482933
RK
9683
9684 -- typ (x)
9685
9686 -- to
9687
9688 -- Tnn : typ'Base := typ'Base (x);
9689 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
9690 -- Tnn
9691
685094bf
RD
9692 -- This is necessary when there is a conversion of integer to float or
9693 -- to fixed-point to ensure that the correct checks are made. It is not
9694 -- necessary for float to float where it is enough to simply set the
9695 -- Do_Range_Check flag.
fbf5a39b 9696
70482933
RK
9697 procedure Real_Range_Check is
9698 Btyp : constant Entity_Id := Base_Type (Target_Type);
9699 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
9700 Hi : constant Node_Id := Type_High_Bound (Target_Type);
fbf5a39b 9701 Xtyp : constant Entity_Id := Etype (Operand);
70482933
RK
9702 Conv : Node_Id;
9703 Tnn : Entity_Id;
9704
9705 begin
9706 -- Nothing to do if conversion was rewritten
9707
9708 if Nkind (N) /= N_Type_Conversion then
9709 return;
9710 end if;
9711
685094bf
RD
9712 -- Nothing to do if range checks suppressed, or target has the same
9713 -- range as the base type (or is the base type).
70482933
RK
9714
9715 if Range_Checks_Suppressed (Target_Type)
533369aa 9716 or else (Lo = Type_Low_Bound (Btyp)
70482933
RK
9717 and then
9718 Hi = Type_High_Bound (Btyp))
9719 then
9720 return;
9721 end if;
9722
685094bf
RD
9723 -- Nothing to do if expression is an entity on which checks have been
9724 -- suppressed.
70482933 9725
fbf5a39b
AC
9726 if Is_Entity_Name (Operand)
9727 and then Range_Checks_Suppressed (Entity (Operand))
9728 then
9729 return;
9730 end if;
9731
685094bf
RD
9732 -- Nothing to do if bounds are all static and we can tell that the
9733 -- expression is within the bounds of the target. Note that if the
9734 -- operand is of an unconstrained floating-point type, then we do
9735 -- not trust it to be in range (might be infinite)
fbf5a39b
AC
9736
9737 declare
f02b8bb8
RD
9738 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
9739 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
fbf5a39b
AC
9740
9741 begin
9742 if (not Is_Floating_Point_Type (Xtyp)
9743 or else Is_Constrained (Xtyp))
9744 and then Compile_Time_Known_Value (S_Lo)
9745 and then Compile_Time_Known_Value (S_Hi)
9746 and then Compile_Time_Known_Value (Hi)
9747 and then Compile_Time_Known_Value (Lo)
9748 then
9749 declare
9750 D_Lov : constant Ureal := Expr_Value_R (Lo);
9751 D_Hiv : constant Ureal := Expr_Value_R (Hi);
9752 S_Lov : Ureal;
9753 S_Hiv : Ureal;
9754
9755 begin
9756 if Is_Real_Type (Xtyp) then
9757 S_Lov := Expr_Value_R (S_Lo);
9758 S_Hiv := Expr_Value_R (S_Hi);
9759 else
9760 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
9761 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
9762 end if;
9763
9764 if D_Hiv > D_Lov
9765 and then S_Lov >= D_Lov
9766 and then S_Hiv <= D_Hiv
9767 then
9768 Set_Do_Range_Check (Operand, False);
9769 return;
9770 end if;
9771 end;
9772 end if;
9773 end;
9774
9775 -- For float to float conversions, we are done
9776
9777 if Is_Floating_Point_Type (Xtyp)
9778 and then
9779 Is_Floating_Point_Type (Btyp)
70482933
RK
9780 then
9781 return;
9782 end if;
9783
fbf5a39b 9784 -- Otherwise rewrite the conversion as described above
70482933
RK
9785
9786 Conv := Relocate_Node (N);
eaa826f8 9787 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
70482933
RK
9788 Set_Etype (Conv, Btyp);
9789
f02b8bb8
RD
9790 -- Enable overflow except for case of integer to float conversions,
9791 -- where it is never required, since we can never have overflow in
9792 -- this case.
70482933 9793
fbf5a39b
AC
9794 if not Is_Integer_Type (Etype (Operand)) then
9795 Enable_Overflow_Check (Conv);
70482933
RK
9796 end if;
9797
191fcb3a 9798 Tnn := Make_Temporary (Loc, 'T', Conv);
70482933
RK
9799
9800 Insert_Actions (N, New_List (
9801 Make_Object_Declaration (Loc,
9802 Defining_Identifier => Tnn,
9803 Object_Definition => New_Occurrence_Of (Btyp, Loc),
0ac2a660
AC
9804 Constant_Present => True,
9805 Expression => Conv),
70482933
RK
9806
9807 Make_Raise_Constraint_Error (Loc,
07fc65c4
GB
9808 Condition =>
9809 Make_Or_Else (Loc,
9810 Left_Opnd =>
9811 Make_Op_Lt (Loc,
9812 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
9813 Right_Opnd =>
9814 Make_Attribute_Reference (Loc,
9815 Attribute_Name => Name_First,
9816 Prefix =>
9817 New_Occurrence_Of (Target_Type, Loc))),
70482933 9818
07fc65c4
GB
9819 Right_Opnd =>
9820 Make_Op_Gt (Loc,
9821 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
9822 Right_Opnd =>
9823 Make_Attribute_Reference (Loc,
9824 Attribute_Name => Name_Last,
9825 Prefix =>
9826 New_Occurrence_Of (Target_Type, Loc)))),
9827 Reason => CE_Range_Check_Failed)));
70482933
RK
9828
9829 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
9830 Analyze_And_Resolve (N, Btyp);
9831 end Real_Range_Check;
9832
d15f9422
AC
9833 -----------------------------
9834 -- Has_Extra_Accessibility --
9835 -----------------------------
9836
9837 -- Returns true for a formal of an anonymous access type or for
9838 -- an Ada 2012-style stand-alone object of an anonymous access type.
9839
9840 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
9841 begin
9842 if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then
9843 return Present (Effective_Extra_Accessibility (Id));
9844 else
9845 return False;
9846 end if;
9847 end Has_Extra_Accessibility;
9848
70482933
RK
9849 -- Start of processing for Expand_N_Type_Conversion
9850
9851 begin
83851b23 9852 -- First remove check marks put by the semantic analysis on the type
b2502161
AC
9853 -- conversion between array types. We need these checks, and they will
9854 -- be generated by this expansion routine, but we do not depend on these
9855 -- flags being set, and since we do intend to expand the checks in the
9856 -- front end, we don't want them on the tree passed to the back end.
83851b23
AC
9857
9858 if Is_Array_Type (Target_Type) then
9859 if Is_Constrained (Target_Type) then
9860 Set_Do_Length_Check (N, False);
9861 else
9862 Set_Do_Range_Check (Operand, False);
9863 end if;
9864 end if;
9865
685094bf 9866 -- Nothing at all to do if conversion is to the identical type so remove
76efd572
AC
9867 -- the conversion completely, it is useless, except that it may carry
9868 -- an Assignment_OK attribute, which must be propagated to the operand.
70482933
RK
9869
9870 if Operand_Type = Target_Type then
7b00e31d
AC
9871 if Assignment_OK (N) then
9872 Set_Assignment_OK (Operand);
9873 end if;
9874
fbf5a39b 9875 Rewrite (N, Relocate_Node (Operand));
e606088a 9876 goto Done;
70482933
RK
9877 end if;
9878
685094bf
RD
9879 -- Nothing to do if this is the second argument of read. This is a
9880 -- "backwards" conversion that will be handled by the specialized code
9881 -- in attribute processing.
70482933
RK
9882
9883 if Nkind (Parent (N)) = N_Attribute_Reference
9884 and then Attribute_Name (Parent (N)) = Name_Read
9885 and then Next (First (Expressions (Parent (N)))) = N
9886 then
e606088a
AC
9887 goto Done;
9888 end if;
9889
9890 -- Check for case of converting to a type that has an invariant
9891 -- associated with it. This required an invariant check. We convert
9892
9893 -- typ (expr)
9894
9895 -- into
9896
9897 -- do invariant_check (typ (expr)) in typ (expr);
9898
9899 -- using Duplicate_Subexpr to avoid multiple side effects
9900
9901 -- Note: the Comes_From_Source check, and then the resetting of this
9902 -- flag prevents what would otherwise be an infinite recursion.
9903
fd0ff1cf
RD
9904 if Has_Invariants (Target_Type)
9905 and then Present (Invariant_Procedure (Target_Type))
e606088a
AC
9906 and then Comes_From_Source (N)
9907 then
9908 Set_Comes_From_Source (N, False);
9909 Rewrite (N,
9910 Make_Expression_With_Actions (Loc,
9911 Actions => New_List (
9912 Make_Invariant_Call (Duplicate_Subexpr (N))),
9913 Expression => Duplicate_Subexpr_No_Checks (N)));
9914 Analyze_And_Resolve (N, Target_Type);
9915 goto Done;
70482933
RK
9916 end if;
9917
9918 -- Here if we may need to expand conversion
9919
eaa826f8
RD
9920 -- If the operand of the type conversion is an arithmetic operation on
9921 -- signed integers, and the based type of the signed integer type in
9922 -- question is smaller than Standard.Integer, we promote both of the
9923 -- operands to type Integer.
9924
9925 -- For example, if we have
9926
9927 -- target-type (opnd1 + opnd2)
9928
9929 -- and opnd1 and opnd2 are of type short integer, then we rewrite
9930 -- this as:
9931
9932 -- target-type (integer(opnd1) + integer(opnd2))
9933
9934 -- We do this because we are always allowed to compute in a larger type
9935 -- if we do the right thing with the result, and in this case we are
9936 -- going to do a conversion which will do an appropriate check to make
9937 -- sure that things are in range of the target type in any case. This
9938 -- avoids some unnecessary intermediate overflows.
9939
dfcfdc0a
AC
9940 -- We might consider a similar transformation in the case where the
9941 -- target is a real type or a 64-bit integer type, and the operand
9942 -- is an arithmetic operation using a 32-bit integer type. However,
9943 -- we do not bother with this case, because it could cause significant
308e6f3a 9944 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
dfcfdc0a
AC
9945 -- much cheaper, but we don't want different behavior on 32-bit and
9946 -- 64-bit machines. Note that the exclusion of the 64-bit case also
9947 -- handles the configurable run-time cases where 64-bit arithmetic
9948 -- may simply be unavailable.
eaa826f8
RD
9949
9950 -- Note: this circuit is partially redundant with respect to the circuit
9951 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
9952 -- the processing here. Also we still need the Checks circuit, since we
9953 -- have to be sure not to generate junk overflow checks in the first
9954 -- place, since it would be trick to remove them here!
9955
fdfcc663 9956 if Integer_Promotion_Possible (N) then
eaa826f8 9957
fdfcc663 9958 -- All conditions met, go ahead with transformation
eaa826f8 9959
fdfcc663
AC
9960 declare
9961 Opnd : Node_Id;
9962 L, R : Node_Id;
dfcfdc0a 9963
fdfcc663
AC
9964 begin
9965 R :=
9966 Make_Type_Conversion (Loc,
9967 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
9968 Expression => Relocate_Node (Right_Opnd (Operand)));
eaa826f8 9969
5f3f175d
AC
9970 Opnd := New_Op_Node (Nkind (Operand), Loc);
9971 Set_Right_Opnd (Opnd, R);
eaa826f8 9972
5f3f175d 9973 if Nkind (Operand) in N_Binary_Op then
fdfcc663 9974 L :=
eaa826f8 9975 Make_Type_Conversion (Loc,
dfcfdc0a 9976 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
fdfcc663
AC
9977 Expression => Relocate_Node (Left_Opnd (Operand)));
9978
5f3f175d
AC
9979 Set_Left_Opnd (Opnd, L);
9980 end if;
eaa826f8 9981
5f3f175d
AC
9982 Rewrite (N,
9983 Make_Type_Conversion (Loc,
9984 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
9985 Expression => Opnd));
dfcfdc0a 9986
5f3f175d 9987 Analyze_And_Resolve (N, Target_Type);
e606088a 9988 goto Done;
fdfcc663
AC
9989 end;
9990 end if;
eaa826f8 9991
f82944b7
JM
9992 -- Do validity check if validity checking operands
9993
533369aa 9994 if Validity_Checks_On and Validity_Check_Operands then
f82944b7
JM
9995 Ensure_Valid (Operand);
9996 end if;
9997
70482933
RK
9998 -- Special case of converting from non-standard boolean type
9999
10000 if Is_Boolean_Type (Operand_Type)
10001 and then (Nonzero_Is_True (Operand_Type))
10002 then
10003 Adjust_Condition (Operand);
10004 Set_Etype (Operand, Standard_Boolean);
10005 Operand_Type := Standard_Boolean;
10006 end if;
10007
10008 -- Case of converting to an access type
10009
10010 if Is_Access_Type (Target_Type) then
10011
d766cee3
RD
10012 -- Apply an accessibility check when the conversion operand is an
10013 -- access parameter (or a renaming thereof), unless conversion was
e84e11ba
GD
10014 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
10015 -- Note that other checks may still need to be applied below (such
10016 -- as tagged type checks).
70482933
RK
10017
10018 if Is_Entity_Name (Operand)
d15f9422 10019 and then Has_Extra_Accessibility (Entity (Operand))
70482933 10020 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
d766cee3
RD
10021 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
10022 or else Attribute_Name (Original_Node (N)) = Name_Access)
70482933 10023 then
e84e11ba
GD
10024 Apply_Accessibility_Check
10025 (Operand, Target_Type, Insert_Node => Operand);
70482933 10026
e84e11ba 10027 -- If the level of the operand type is statically deeper than the
685094bf
RD
10028 -- level of the target type, then force Program_Error. Note that this
10029 -- can only occur for cases where the attribute is within the body of
10030 -- an instantiation (otherwise the conversion will already have been
10031 -- rejected as illegal). Note: warnings are issued by the analyzer
10032 -- for the instance cases.
70482933
RK
10033
10034 elsif In_Instance_Body
07fc65c4
GB
10035 and then Type_Access_Level (Operand_Type) >
10036 Type_Access_Level (Target_Type)
70482933 10037 then
426908f8 10038 Raise_Accessibility_Error;
70482933 10039
685094bf
RD
10040 -- When the operand is a selected access discriminant the check needs
10041 -- to be made against the level of the object denoted by the prefix
10042 -- of the selected name. Force Program_Error for this case as well
10043 -- (this accessibility violation can only happen if within the body
10044 -- of an instantiation).
70482933
RK
10045
10046 elsif In_Instance_Body
10047 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
10048 and then Nkind (Operand) = N_Selected_Component
10049 and then Object_Access_Level (Operand) >
10050 Type_Access_Level (Target_Type)
10051 then
426908f8 10052 Raise_Accessibility_Error;
e606088a 10053 goto Done;
70482933
RK
10054 end if;
10055 end if;
10056
10057 -- Case of conversions of tagged types and access to tagged types
10058
685094bf
RD
10059 -- When needed, that is to say when the expression is class-wide, Add
10060 -- runtime a tag check for (strict) downward conversion by using the
10061 -- membership test, generating:
70482933
RK
10062
10063 -- [constraint_error when Operand not in Target_Type'Class]
10064
10065 -- or in the access type case
10066
10067 -- [constraint_error
10068 -- when Operand /= null
10069 -- and then Operand.all not in
10070 -- Designated_Type (Target_Type)'Class]
10071
10072 if (Is_Access_Type (Target_Type)
10073 and then Is_Tagged_Type (Designated_Type (Target_Type)))
10074 or else Is_Tagged_Type (Target_Type)
10075 then
685094bf
RD
10076 -- Do not do any expansion in the access type case if the parent is a
10077 -- renaming, since this is an error situation which will be caught by
10078 -- Sem_Ch8, and the expansion can interfere with this error check.
70482933 10079
e7e4d230 10080 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
e606088a 10081 goto Done;
70482933
RK
10082 end if;
10083
0669bebe 10084 -- Otherwise, proceed with processing tagged conversion
70482933 10085
e7e4d230 10086 Tagged_Conversion : declare
8cea7b64
HK
10087 Actual_Op_Typ : Entity_Id;
10088 Actual_Targ_Typ : Entity_Id;
10089 Make_Conversion : Boolean := False;
10090 Root_Op_Typ : Entity_Id;
70482933 10091
8cea7b64
HK
10092 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
10093 -- Create a membership check to test whether Operand is a member
10094 -- of Targ_Typ. If the original Target_Type is an access, include
10095 -- a test for null value. The check is inserted at N.
10096
10097 --------------------
10098 -- Make_Tag_Check --
10099 --------------------
10100
10101 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
10102 Cond : Node_Id;
10103
10104 begin
10105 -- Generate:
10106 -- [Constraint_Error
10107 -- when Operand /= null
10108 -- and then Operand.all not in Targ_Typ]
10109
10110 if Is_Access_Type (Target_Type) then
10111 Cond :=
10112 Make_And_Then (Loc,
10113 Left_Opnd =>
10114 Make_Op_Ne (Loc,
10115 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10116 Right_Opnd => Make_Null (Loc)),
10117
10118 Right_Opnd =>
10119 Make_Not_In (Loc,
10120 Left_Opnd =>
10121 Make_Explicit_Dereference (Loc,
10122 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
10123 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
10124
10125 -- Generate:
10126 -- [Constraint_Error when Operand not in Targ_Typ]
10127
10128 else
10129 Cond :=
10130 Make_Not_In (Loc,
10131 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10132 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
10133 end if;
10134
10135 Insert_Action (N,
10136 Make_Raise_Constraint_Error (Loc,
10137 Condition => Cond,
10138 Reason => CE_Tag_Check_Failed));
10139 end Make_Tag_Check;
10140
e7e4d230 10141 -- Start of processing for Tagged_Conversion
70482933
RK
10142
10143 begin
9732e886 10144 -- Handle entities from the limited view
852dba80 10145
9732e886 10146 if Is_Access_Type (Operand_Type) then
852dba80
AC
10147 Actual_Op_Typ :=
10148 Available_View (Designated_Type (Operand_Type));
9732e886
JM
10149 else
10150 Actual_Op_Typ := Operand_Type;
10151 end if;
10152
10153 if Is_Access_Type (Target_Type) then
852dba80
AC
10154 Actual_Targ_Typ :=
10155 Available_View (Designated_Type (Target_Type));
70482933 10156 else
8cea7b64 10157 Actual_Targ_Typ := Target_Type;
70482933
RK
10158 end if;
10159
8cea7b64
HK
10160 Root_Op_Typ := Root_Type (Actual_Op_Typ);
10161
20b5d666
JM
10162 -- Ada 2005 (AI-251): Handle interface type conversion
10163
8cea7b64 10164 if Is_Interface (Actual_Op_Typ) then
f6f4d8d4 10165 Expand_Interface_Conversion (N);
e606088a 10166 goto Done;
20b5d666
JM
10167 end if;
10168
8cea7b64 10169 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
70482933 10170
8cea7b64
HK
10171 -- Create a runtime tag check for a downward class-wide type
10172 -- conversion.
70482933 10173
8cea7b64 10174 if Is_Class_Wide_Type (Actual_Op_Typ)
852dba80 10175 and then Actual_Op_Typ /= Actual_Targ_Typ
8cea7b64 10176 and then Root_Op_Typ /= Actual_Targ_Typ
4ac2477e
JM
10177 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
10178 Use_Full_View => True)
8cea7b64
HK
10179 then
10180 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
10181 Make_Conversion := True;
10182 end if;
70482933 10183
8cea7b64
HK
10184 -- AI05-0073: If the result subtype of the function is defined
10185 -- by an access_definition designating a specific tagged type
10186 -- T, a check is made that the result value is null or the tag
10187 -- of the object designated by the result value identifies T.
10188 -- Constraint_Error is raised if this check fails.
70482933 10189
92a7cd46 10190 if Nkind (Parent (N)) = N_Simple_Return_Statement then
8cea7b64 10191 declare
e886436a 10192 Func : Entity_Id;
8cea7b64
HK
10193 Func_Typ : Entity_Id;
10194
10195 begin
e886436a 10196 -- Climb scope stack looking for the enclosing function
8cea7b64 10197
e886436a 10198 Func := Current_Scope;
8cea7b64
HK
10199 while Present (Func)
10200 and then Ekind (Func) /= E_Function
10201 loop
10202 Func := Scope (Func);
10203 end loop;
10204
10205 -- The function's return subtype must be defined using
10206 -- an access definition.
10207
10208 if Nkind (Result_Definition (Parent (Func))) =
10209 N_Access_Definition
10210 then
10211 Func_Typ := Directly_Designated_Type (Etype (Func));
10212
10213 -- The return subtype denotes a specific tagged type,
10214 -- in other words, a non class-wide type.
10215
10216 if Is_Tagged_Type (Func_Typ)
10217 and then not Is_Class_Wide_Type (Func_Typ)
10218 then
10219 Make_Tag_Check (Actual_Targ_Typ);
10220 Make_Conversion := True;
10221 end if;
10222 end if;
10223 end;
70482933
RK
10224 end if;
10225
8cea7b64
HK
10226 -- We have generated a tag check for either a class-wide type
10227 -- conversion or for AI05-0073.
70482933 10228
8cea7b64
HK
10229 if Make_Conversion then
10230 declare
10231 Conv : Node_Id;
10232 begin
10233 Conv :=
10234 Make_Unchecked_Type_Conversion (Loc,
10235 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
10236 Expression => Relocate_Node (Expression (N)));
10237 Rewrite (N, Conv);
10238 Analyze_And_Resolve (N, Target_Type);
10239 end;
10240 end if;
70482933 10241 end if;
e7e4d230 10242 end Tagged_Conversion;
70482933
RK
10243
10244 -- Case of other access type conversions
10245
10246 elsif Is_Access_Type (Target_Type) then
10247 Apply_Constraint_Check (Operand, Target_Type);
10248
10249 -- Case of conversions from a fixed-point type
10250
685094bf
RD
10251 -- These conversions require special expansion and processing, found in
10252 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
10253 -- since from a semantic point of view, these are simple integer
70482933
RK
10254 -- conversions, which do not need further processing.
10255
10256 elsif Is_Fixed_Point_Type (Operand_Type)
10257 and then not Conversion_OK (N)
10258 then
10259 -- We should never see universal fixed at this case, since the
10260 -- expansion of the constituent divide or multiply should have
10261 -- eliminated the explicit mention of universal fixed.
10262
10263 pragma Assert (Operand_Type /= Universal_Fixed);
10264
685094bf
RD
10265 -- Check for special case of the conversion to universal real that
10266 -- occurs as a result of the use of a round attribute. In this case,
10267 -- the real type for the conversion is taken from the target type of
10268 -- the Round attribute and the result must be marked as rounded.
70482933
RK
10269
10270 if Target_Type = Universal_Real
10271 and then Nkind (Parent (N)) = N_Attribute_Reference
10272 and then Attribute_Name (Parent (N)) = Name_Round
10273 then
10274 Set_Rounded_Result (N);
10275 Set_Etype (N, Etype (Parent (N)));
10276 end if;
10277
10278 -- Otherwise do correct fixed-conversion, but skip these if the
e7e4d230
AC
10279 -- Conversion_OK flag is set, because from a semantic point of view
10280 -- these are simple integer conversions needing no further processing
10281 -- (the backend will simply treat them as integers).
70482933
RK
10282
10283 if not Conversion_OK (N) then
10284 if Is_Fixed_Point_Type (Etype (N)) then
10285 Expand_Convert_Fixed_To_Fixed (N);
10286 Real_Range_Check;
10287
10288 elsif Is_Integer_Type (Etype (N)) then
10289 Expand_Convert_Fixed_To_Integer (N);
10290
10291 else
10292 pragma Assert (Is_Floating_Point_Type (Etype (N)));
10293 Expand_Convert_Fixed_To_Float (N);
10294 Real_Range_Check;
10295 end if;
10296 end if;
10297
10298 -- Case of conversions to a fixed-point type
10299
685094bf
RD
10300 -- These conversions require special expansion and processing, found in
10301 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
10302 -- since from a semantic point of view, these are simple integer
10303 -- conversions, which do not need further processing.
70482933
RK
10304
10305 elsif Is_Fixed_Point_Type (Target_Type)
10306 and then not Conversion_OK (N)
10307 then
10308 if Is_Integer_Type (Operand_Type) then
10309 Expand_Convert_Integer_To_Fixed (N);
10310 Real_Range_Check;
10311 else
10312 pragma Assert (Is_Floating_Point_Type (Operand_Type));
10313 Expand_Convert_Float_To_Fixed (N);
10314 Real_Range_Check;
10315 end if;
10316
10317 -- Case of float-to-integer conversions
10318
10319 -- We also handle float-to-fixed conversions with Conversion_OK set
10320 -- since semantically the fixed-point target is treated as though it
10321 -- were an integer in such cases.
10322
10323 elsif Is_Floating_Point_Type (Operand_Type)
10324 and then
10325 (Is_Integer_Type (Target_Type)
10326 or else
10327 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
10328 then
70482933
RK
10329 -- One more check here, gcc is still not able to do conversions of
10330 -- this type with proper overflow checking, and so gigi is doing an
10331 -- approximation of what is required by doing floating-point compares
10332 -- with the end-point. But that can lose precision in some cases, and
f02b8bb8 10333 -- give a wrong result. Converting the operand to Universal_Real is
70482933 10334 -- helpful, but still does not catch all cases with 64-bit integers
e7e4d230 10335 -- on targets with only 64-bit floats.
0669bebe
GB
10336
10337 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
10338 -- Can this code be removed ???
70482933 10339
fbf5a39b
AC
10340 if Do_Range_Check (Operand) then
10341 Rewrite (Operand,
70482933
RK
10342 Make_Type_Conversion (Loc,
10343 Subtype_Mark =>
f02b8bb8 10344 New_Occurrence_Of (Universal_Real, Loc),
70482933 10345 Expression =>
fbf5a39b 10346 Relocate_Node (Operand)));
70482933 10347
f02b8bb8 10348 Set_Etype (Operand, Universal_Real);
fbf5a39b
AC
10349 Enable_Range_Check (Operand);
10350 Set_Do_Range_Check (Expression (Operand), False);
70482933
RK
10351 end if;
10352
10353 -- Case of array conversions
10354
685094bf
RD
10355 -- Expansion of array conversions, add required length/range checks but
10356 -- only do this if there is no change of representation. For handling of
10357 -- this case, see Handle_Changed_Representation.
70482933
RK
10358
10359 elsif Is_Array_Type (Target_Type) then
70482933
RK
10360 if Is_Constrained (Target_Type) then
10361 Apply_Length_Check (Operand, Target_Type);
10362 else
10363 Apply_Range_Check (Operand, Target_Type);
10364 end if;
10365
10366 Handle_Changed_Representation;
10367
10368 -- Case of conversions of discriminated types
10369
685094bf
RD
10370 -- Add required discriminant checks if target is constrained. Again this
10371 -- change is skipped if we have a change of representation.
70482933
RK
10372
10373 elsif Has_Discriminants (Target_Type)
10374 and then Is_Constrained (Target_Type)
10375 then
10376 Apply_Discriminant_Check (Operand, Target_Type);
10377 Handle_Changed_Representation;
10378
10379 -- Case of all other record conversions. The only processing required
10380 -- is to check for a change of representation requiring the special
10381 -- assignment processing.
10382
10383 elsif Is_Record_Type (Target_Type) then
5d09245e
AC
10384
10385 -- Ada 2005 (AI-216): Program_Error is raised when converting from
685094bf
RD
10386 -- a derived Unchecked_Union type to an unconstrained type that is
10387 -- not Unchecked_Union if the operand lacks inferable discriminants.
5d09245e
AC
10388
10389 if Is_Derived_Type (Operand_Type)
10390 and then Is_Unchecked_Union (Base_Type (Operand_Type))
10391 and then not Is_Constrained (Target_Type)
10392 and then not Is_Unchecked_Union (Base_Type (Target_Type))
10393 and then not Has_Inferable_Discriminants (Operand)
10394 then
685094bf 10395 -- To prevent Gigi from generating illegal code, we generate a
5d09245e 10396 -- Program_Error node, but we give it the target type of the
6cb3037c 10397 -- conversion (is this requirement documented somewhere ???)
5d09245e
AC
10398
10399 declare
10400 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
10401 Reason => PE_Unchecked_Union_Restriction);
10402
10403 begin
10404 Set_Etype (PE, Target_Type);
10405 Rewrite (N, PE);
10406
10407 end;
10408 else
10409 Handle_Changed_Representation;
10410 end if;
70482933
RK
10411
10412 -- Case of conversions of enumeration types
10413
10414 elsif Is_Enumeration_Type (Target_Type) then
10415
10416 -- Special processing is required if there is a change of
e7e4d230 10417 -- representation (from enumeration representation clauses).
70482933
RK
10418
10419 if not Same_Representation (Target_Type, Operand_Type) then
10420
10421 -- Convert: x(y) to x'val (ytyp'val (y))
10422
10423 Rewrite (N,
1c66c4f5
AC
10424 Make_Attribute_Reference (Loc,
10425 Prefix => New_Occurrence_Of (Target_Type, Loc),
10426 Attribute_Name => Name_Val,
10427 Expressions => New_List (
10428 Make_Attribute_Reference (Loc,
10429 Prefix => New_Occurrence_Of (Operand_Type, Loc),
10430 Attribute_Name => Name_Pos,
10431 Expressions => New_List (Operand)))));
70482933
RK
10432
10433 Analyze_And_Resolve (N, Target_Type);
10434 end if;
10435
10436 -- Case of conversions to floating-point
10437
10438 elsif Is_Floating_Point_Type (Target_Type) then
10439 Real_Range_Check;
70482933
RK
10440 end if;
10441
685094bf 10442 -- At this stage, either the conversion node has been transformed into
e7e4d230
AC
10443 -- some other equivalent expression, or left as a conversion that can be
10444 -- handled by Gigi, in the following cases:
70482933
RK
10445
10446 -- Conversions with no change of representation or type
10447
685094bf
RD
10448 -- Numeric conversions involving integer, floating- and fixed-point
10449 -- values. Fixed-point values are allowed only if Conversion_OK is
10450 -- set, i.e. if the fixed-point values are to be treated as integers.
70482933 10451
5e1c00fa
RD
10452 -- No other conversions should be passed to Gigi
10453
10454 -- Check: are these rules stated in sinfo??? if so, why restate here???
70482933 10455
685094bf
RD
10456 -- The only remaining step is to generate a range check if we still have
10457 -- a type conversion at this stage and Do_Range_Check is set. For now we
10458 -- do this only for conversions of discrete types.
fbf5a39b
AC
10459
10460 if Nkind (N) = N_Type_Conversion
10461 and then Is_Discrete_Type (Etype (N))
10462 then
10463 declare
10464 Expr : constant Node_Id := Expression (N);
10465 Ftyp : Entity_Id;
10466 Ityp : Entity_Id;
10467
10468 begin
10469 if Do_Range_Check (Expr)
10470 and then Is_Discrete_Type (Etype (Expr))
10471 then
10472 Set_Do_Range_Check (Expr, False);
10473
685094bf
RD
10474 -- Before we do a range check, we have to deal with treating a
10475 -- fixed-point operand as an integer. The way we do this is
10476 -- simply to do an unchecked conversion to an appropriate
fbf5a39b
AC
10477 -- integer type large enough to hold the result.
10478
10479 -- This code is not active yet, because we are only dealing
10480 -- with discrete types so far ???
10481
10482 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
10483 and then Treat_Fixed_As_Integer (Expr)
10484 then
10485 Ftyp := Base_Type (Etype (Expr));
10486
10487 if Esize (Ftyp) >= Esize (Standard_Integer) then
10488 Ityp := Standard_Long_Long_Integer;
10489 else
10490 Ityp := Standard_Integer;
10491 end if;
10492
10493 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
10494 end if;
10495
10496 -- Reset overflow flag, since the range check will include
e7e4d230 10497 -- dealing with possible overflow, and generate the check. If
685094bf 10498 -- Address is either a source type or target type, suppress
8a36a0cc
AC
10499 -- range check to avoid typing anomalies when it is a visible
10500 -- integer type.
fbf5a39b
AC
10501
10502 Set_Do_Overflow_Check (N, False);
8a36a0cc
AC
10503 if not Is_Descendent_Of_Address (Etype (Expr))
10504 and then not Is_Descendent_Of_Address (Target_Type)
10505 then
10506 Generate_Range_Check
10507 (Expr, Target_Type, CE_Range_Check_Failed);
10508 end if;
fbf5a39b
AC
10509 end if;
10510 end;
10511 end if;
f02b8bb8
RD
10512
10513 -- Final step, if the result is a type conversion involving Vax_Float
10514 -- types, then it is subject for further special processing.
10515
10516 if Nkind (N) = N_Type_Conversion
10517 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
10518 then
10519 Expand_Vax_Conversion (N);
e606088a 10520 goto Done;
f02b8bb8 10521 end if;
e606088a
AC
10522
10523 -- Here at end of processing
10524
48f91b44
RD
10525 <<Done>>
10526 -- Apply predicate check if required. Note that we can't just call
10527 -- Apply_Predicate_Check here, because the type looks right after
10528 -- the conversion and it would omit the check. The Comes_From_Source
10529 -- guard is necessary to prevent infinite recursions when we generate
10530 -- internal conversions for the purpose of checking predicates.
10531
10532 if Present (Predicate_Function (Target_Type))
10533 and then Target_Type /= Operand_Type
10534 and then Comes_From_Source (N)
10535 then
00332244
AC
10536 declare
10537 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
10538
10539 begin
10540 -- Avoid infinite recursion on the subsequent expansion of
10541 -- of the copy of the original type conversion.
10542
10543 Set_Comes_From_Source (New_Expr, False);
10544 Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr));
10545 end;
48f91b44 10546 end if;
70482933
RK
10547 end Expand_N_Type_Conversion;
10548
10549 -----------------------------------
10550 -- Expand_N_Unchecked_Expression --
10551 -----------------------------------
10552
e7e4d230 10553 -- Remove the unchecked expression node from the tree. Its job was simply
70482933
RK
10554 -- to make sure that its constituent expression was handled with checks
10555 -- off, and now that that is done, we can remove it from the tree, and
e7e4d230 10556 -- indeed must, since Gigi does not expect to see these nodes.
70482933
RK
10557
10558 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
10559 Exp : constant Node_Id := Expression (N);
70482933 10560 begin
e7e4d230 10561 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
70482933
RK
10562 Rewrite (N, Exp);
10563 end Expand_N_Unchecked_Expression;
10564
10565 ----------------------------------------
10566 -- Expand_N_Unchecked_Type_Conversion --
10567 ----------------------------------------
10568
685094bf
RD
10569 -- If this cannot be handled by Gigi and we haven't already made a
10570 -- temporary for it, do it now.
70482933
RK
10571
10572 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
10573 Target_Type : constant Entity_Id := Etype (N);
10574 Operand : constant Node_Id := Expression (N);
10575 Operand_Type : constant Entity_Id := Etype (Operand);
10576
10577 begin
7b00e31d 10578 -- Nothing at all to do if conversion is to the identical type so remove
76efd572 10579 -- the conversion completely, it is useless, except that it may carry
e7e4d230 10580 -- an Assignment_OK indication which must be propagated to the operand.
7b00e31d
AC
10581
10582 if Operand_Type = Target_Type then
13d923cc 10583
e7e4d230
AC
10584 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
10585
7b00e31d
AC
10586 if Assignment_OK (N) then
10587 Set_Assignment_OK (Operand);
10588 end if;
10589
10590 Rewrite (N, Relocate_Node (Operand));
10591 return;
10592 end if;
10593
70482933
RK
10594 -- If we have a conversion of a compile time known value to a target
10595 -- type and the value is in range of the target type, then we can simply
10596 -- replace the construct by an integer literal of the correct type. We
10597 -- only apply this to integer types being converted. Possibly it may
10598 -- apply in other cases, but it is too much trouble to worry about.
10599
10600 -- Note that we do not do this transformation if the Kill_Range_Check
10601 -- flag is set, since then the value may be outside the expected range.
10602 -- This happens in the Normalize_Scalars case.
10603
20b5d666
JM
10604 -- We also skip this if either the target or operand type is biased
10605 -- because in this case, the unchecked conversion is supposed to
10606 -- preserve the bit pattern, not the integer value.
10607
70482933 10608 if Is_Integer_Type (Target_Type)
20b5d666 10609 and then not Has_Biased_Representation (Target_Type)
70482933 10610 and then Is_Integer_Type (Operand_Type)
20b5d666 10611 and then not Has_Biased_Representation (Operand_Type)
70482933
RK
10612 and then Compile_Time_Known_Value (Operand)
10613 and then not Kill_Range_Check (N)
10614 then
10615 declare
10616 Val : constant Uint := Expr_Value (Operand);
10617
10618 begin
10619 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
10620 and then
10621 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
10622 and then
10623 Val >= Expr_Value (Type_Low_Bound (Target_Type))
10624 and then
10625 Val <= Expr_Value (Type_High_Bound (Target_Type))
10626 then
10627 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
8a36a0cc 10628
685094bf
RD
10629 -- If Address is the target type, just set the type to avoid a
10630 -- spurious type error on the literal when Address is a visible
10631 -- integer type.
8a36a0cc
AC
10632
10633 if Is_Descendent_Of_Address (Target_Type) then
10634 Set_Etype (N, Target_Type);
10635 else
10636 Analyze_And_Resolve (N, Target_Type);
10637 end if;
10638
70482933
RK
10639 return;
10640 end if;
10641 end;
10642 end if;
10643
10644 -- Nothing to do if conversion is safe
10645
10646 if Safe_Unchecked_Type_Conversion (N) then
10647 return;
10648 end if;
10649
10650 -- Otherwise force evaluation unless Assignment_OK flag is set (this
324ac540 10651 -- flag indicates ??? More comments needed here)
70482933
RK
10652
10653 if Assignment_OK (N) then
10654 null;
10655 else
10656 Force_Evaluation (N);
10657 end if;
10658 end Expand_N_Unchecked_Type_Conversion;
10659
10660 ----------------------------
10661 -- Expand_Record_Equality --
10662 ----------------------------
10663
10664 -- For non-variant records, Equality is expanded when needed into:
10665
10666 -- and then Lhs.Discr1 = Rhs.Discr1
10667 -- and then ...
10668 -- and then Lhs.Discrn = Rhs.Discrn
10669 -- and then Lhs.Cmp1 = Rhs.Cmp1
10670 -- and then ...
10671 -- and then Lhs.Cmpn = Rhs.Cmpn
10672
10673 -- The expression is folded by the back-end for adjacent fields. This
10674 -- function is called for tagged record in only one occasion: for imple-
10675 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
10676 -- otherwise the primitive "=" is used directly.
10677
10678 function Expand_Record_Equality
10679 (Nod : Node_Id;
10680 Typ : Entity_Id;
10681 Lhs : Node_Id;
10682 Rhs : Node_Id;
2e071734 10683 Bodies : List_Id) return Node_Id
70482933
RK
10684 is
10685 Loc : constant Source_Ptr := Sloc (Nod);
10686
0ab80019
AC
10687 Result : Node_Id;
10688 C : Entity_Id;
10689
10690 First_Time : Boolean := True;
10691
6b670dcf
AC
10692 function Element_To_Compare (C : Entity_Id) return Entity_Id;
10693 -- Return the next discriminant or component to compare, starting with
10694 -- C, skipping inherited components.
0ab80019 10695
6b670dcf
AC
10696 ------------------------
10697 -- Element_To_Compare --
10698 ------------------------
70482933 10699
6b670dcf
AC
10700 function Element_To_Compare (C : Entity_Id) return Entity_Id is
10701 Comp : Entity_Id;
28270211 10702
70482933 10703 begin
6b670dcf 10704 Comp := C;
6b670dcf
AC
10705 loop
10706 -- Exit loop when the next element to be compared is found, or
10707 -- there is no more such element.
70482933 10708
6b670dcf 10709 exit when No (Comp);
8190087e 10710
6b670dcf
AC
10711 exit when Ekind_In (Comp, E_Discriminant, E_Component)
10712 and then not (
70482933 10713
6b670dcf 10714 -- Skip inherited components
70482933 10715
6b670dcf
AC
10716 -- Note: for a tagged type, we always generate the "=" primitive
10717 -- for the base type (not on the first subtype), so the test for
10718 -- Comp /= Original_Record_Component (Comp) is True for
10719 -- inherited components only.
24558db8 10720
6b670dcf 10721 (Is_Tagged_Type (Typ)
28270211 10722 and then Comp /= Original_Record_Component (Comp))
24558db8 10723
6b670dcf 10724 -- Skip _Tag
26bff3d9 10725
6b670dcf
AC
10726 or else Chars (Comp) = Name_uTag
10727
10728 -- The .NET/JVM version of type Root_Controlled contains two
10729 -- fields which should not be considered part of the object. To
10730 -- achieve proper equiality between two controlled objects on
10731 -- .NET/JVM, skip _Parent whenever it has type Root_Controlled.
10732
10733 or else (Chars (Comp) = Name_uParent
28270211
AC
10734 and then VM_Target /= No_VM
10735 and then Etype (Comp) = RTE (RE_Root_Controlled))
6b670dcf
AC
10736
10737 -- Skip interface elements (secondary tags???)
10738
10739 or else Is_Interface (Etype (Comp)));
10740
10741 Next_Entity (Comp);
10742 end loop;
10743
10744 return Comp;
10745 end Element_To_Compare;
70482933 10746
70482933
RK
10747 -- Start of processing for Expand_Record_Equality
10748
10749 begin
70482933
RK
10750 -- Generates the following code: (assuming that Typ has one Discr and
10751 -- component C2 is also a record)
10752
10753 -- True
10754 -- and then Lhs.Discr1 = Rhs.Discr1
10755 -- and then Lhs.C1 = Rhs.C1
10756 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
10757 -- and then ...
10758 -- and then Lhs.Cmpn = Rhs.Cmpn
10759
10760 Result := New_Reference_To (Standard_True, Loc);
6b670dcf 10761 C := Element_To_Compare (First_Entity (Typ));
70482933 10762 while Present (C) loop
70482933
RK
10763 declare
10764 New_Lhs : Node_Id;
10765 New_Rhs : Node_Id;
8aceda64 10766 Check : Node_Id;
70482933
RK
10767
10768 begin
10769 if First_Time then
10770 First_Time := False;
10771 New_Lhs := Lhs;
10772 New_Rhs := Rhs;
70482933
RK
10773 else
10774 New_Lhs := New_Copy_Tree (Lhs);
10775 New_Rhs := New_Copy_Tree (Rhs);
10776 end if;
10777
8aceda64
AC
10778 Check :=
10779 Expand_Composite_Equality (Nod, Etype (C),
10780 Lhs =>
10781 Make_Selected_Component (Loc,
8d80ff64 10782 Prefix => New_Lhs,
8aceda64
AC
10783 Selector_Name => New_Reference_To (C, Loc)),
10784 Rhs =>
10785 Make_Selected_Component (Loc,
8d80ff64 10786 Prefix => New_Rhs,
8aceda64
AC
10787 Selector_Name => New_Reference_To (C, Loc)),
10788 Bodies => Bodies);
10789
10790 -- If some (sub)component is an unchecked_union, the whole
10791 -- operation will raise program error.
10792
10793 if Nkind (Check) = N_Raise_Program_Error then
10794 Result := Check;
10795 Set_Etype (Result, Standard_Boolean);
10796 exit;
10797 else
10798 Result :=
10799 Make_And_Then (Loc,
10800 Left_Opnd => Result,
10801 Right_Opnd => Check);
10802 end if;
70482933
RK
10803 end;
10804
6b670dcf 10805 C := Element_To_Compare (Next_Entity (C));
70482933
RK
10806 end loop;
10807
10808 return Result;
10809 end Expand_Record_Equality;
10810
a3068ca6
AC
10811 ---------------------------
10812 -- Expand_Set_Membership --
10813 ---------------------------
10814
10815 procedure Expand_Set_Membership (N : Node_Id) is
10816 Lop : constant Node_Id := Left_Opnd (N);
10817 Alt : Node_Id;
10818 Res : Node_Id;
10819
10820 function Make_Cond (Alt : Node_Id) return Node_Id;
10821 -- If the alternative is a subtype mark, create a simple membership
10822 -- test. Otherwise create an equality test for it.
10823
10824 ---------------
10825 -- Make_Cond --
10826 ---------------
10827
10828 function Make_Cond (Alt : Node_Id) return Node_Id is
10829 Cond : Node_Id;
10830 L : constant Node_Id := New_Copy (Lop);
10831 R : constant Node_Id := Relocate_Node (Alt);
10832
10833 begin
10834 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
10835 or else Nkind (Alt) = N_Range
10836 then
10837 Cond :=
10838 Make_In (Sloc (Alt),
10839 Left_Opnd => L,
10840 Right_Opnd => R);
10841 else
10842 Cond :=
10843 Make_Op_Eq (Sloc (Alt),
10844 Left_Opnd => L,
10845 Right_Opnd => R);
10846 end if;
10847
10848 return Cond;
10849 end Make_Cond;
10850
10851 -- Start of processing for Expand_Set_Membership
10852
10853 begin
10854 Remove_Side_Effects (Lop);
10855
10856 Alt := Last (Alternatives (N));
10857 Res := Make_Cond (Alt);
10858
10859 Prev (Alt);
10860 while Present (Alt) loop
10861 Res :=
10862 Make_Or_Else (Sloc (Alt),
10863 Left_Opnd => Make_Cond (Alt),
10864 Right_Opnd => Res);
10865 Prev (Alt);
10866 end loop;
10867
10868 Rewrite (N, Res);
10869 Analyze_And_Resolve (N, Standard_Boolean);
10870 end Expand_Set_Membership;
10871
5875f8d6
AC
10872 -----------------------------------
10873 -- Expand_Short_Circuit_Operator --
10874 -----------------------------------
10875
955871d3
AC
10876 -- Deal with special expansion if actions are present for the right operand
10877 -- and deal with optimizing case of arguments being True or False. We also
10878 -- deal with the special case of non-standard boolean values.
5875f8d6
AC
10879
10880 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
10881 Loc : constant Source_Ptr := Sloc (N);
10882 Typ : constant Entity_Id := Etype (N);
5875f8d6
AC
10883 Left : constant Node_Id := Left_Opnd (N);
10884 Right : constant Node_Id := Right_Opnd (N);
955871d3 10885 LocR : constant Source_Ptr := Sloc (Right);
5875f8d6
AC
10886 Actlist : List_Id;
10887
10888 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
10889 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
10890 -- If Left = Shortcut_Value then Right need not be evaluated
10891
5875f8d6
AC
10892 begin
10893 -- Deal with non-standard booleans
10894
10895 if Is_Boolean_Type (Typ) then
10896 Adjust_Condition (Left);
10897 Adjust_Condition (Right);
10898 Set_Etype (N, Standard_Boolean);
10899 end if;
10900
10901 -- Check for cases where left argument is known to be True or False
10902
10903 if Compile_Time_Known_Value (Left) then
25adc5fb
AC
10904
10905 -- Mark SCO for left condition as compile time known
10906
10907 if Generate_SCO and then Comes_From_Source (Left) then
10908 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
10909 end if;
10910
5875f8d6
AC
10911 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
10912 -- Any actions associated with Right will be executed unconditionally
10913 -- and can thus be inserted into the tree unconditionally.
10914
10915 if Expr_Value_E (Left) /= Shortcut_Ent then
10916 if Present (Actions (N)) then
10917 Insert_Actions (N, Actions (N));
10918 end if;
10919
10920 Rewrite (N, Right);
10921
10922 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
10923 -- In this case we can forget the actions associated with Right,
10924 -- since they will never be executed.
10925
10926 else
10927 Kill_Dead_Code (Right);
10928 Kill_Dead_Code (Actions (N));
10929 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
10930 end if;
10931
10932 Adjust_Result_Type (N, Typ);
10933 return;
10934 end if;
10935
955871d3
AC
10936 -- If Actions are present for the right operand, we have to do some
10937 -- special processing. We can't just let these actions filter back into
10938 -- code preceding the short circuit (which is what would have happened
10939 -- if we had not trapped them in the short-circuit form), since they
10940 -- must only be executed if the right operand of the short circuit is
10941 -- executed and not otherwise.
5875f8d6 10942
955871d3
AC
10943 if Present (Actions (N)) then
10944 Actlist := Actions (N);
5875f8d6 10945
0812b84e
AC
10946 -- We now use an Expression_With_Actions node for the right operand
10947 -- of the short-circuit form. Note that this solves the traceability
10948 -- problems for coverage analysis.
5875f8d6 10949
0812b84e
AC
10950 Rewrite (Right,
10951 Make_Expression_With_Actions (LocR,
10952 Expression => Relocate_Node (Right),
10953 Actions => Actlist));
10954 Set_Actions (N, No_List);
10955 Analyze_And_Resolve (Right, Standard_Boolean);
955871d3 10956
5875f8d6
AC
10957 Adjust_Result_Type (N, Typ);
10958 return;
10959 end if;
10960
10961 -- No actions present, check for cases of right argument True/False
10962
10963 if Compile_Time_Known_Value (Right) then
25adc5fb
AC
10964
10965 -- Mark SCO for left condition as compile time known
10966
10967 if Generate_SCO and then Comes_From_Source (Right) then
10968 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
10969 end if;
10970
5875f8d6
AC
10971 -- Change (Left and then True), (Left or else False) to Left.
10972 -- Note that we know there are no actions associated with the right
10973 -- operand, since we just checked for this case above.
10974
10975 if Expr_Value_E (Right) /= Shortcut_Ent then
10976 Rewrite (N, Left);
10977
10978 -- Change (Left and then False), (Left or else True) to Right,
10979 -- making sure to preserve any side effects associated with the Left
10980 -- operand.
10981
10982 else
10983 Remove_Side_Effects (Left);
10984 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
10985 end if;
10986 end if;
10987
10988 Adjust_Result_Type (N, Typ);
10989 end Expand_Short_Circuit_Operator;
10990
70482933
RK
10991 -------------------------------------
10992 -- Fixup_Universal_Fixed_Operation --
10993 -------------------------------------
10994
10995 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
10996 Conv : constant Node_Id := Parent (N);
10997
10998 begin
10999 -- We must have a type conversion immediately above us
11000
11001 pragma Assert (Nkind (Conv) = N_Type_Conversion);
11002
11003 -- Normally the type conversion gives our target type. The exception
11004 -- occurs in the case of the Round attribute, where the conversion
11005 -- will be to universal real, and our real type comes from the Round
11006 -- attribute (as well as an indication that we must round the result)
11007
11008 if Nkind (Parent (Conv)) = N_Attribute_Reference
11009 and then Attribute_Name (Parent (Conv)) = Name_Round
11010 then
11011 Set_Etype (N, Etype (Parent (Conv)));
11012 Set_Rounded_Result (N);
11013
11014 -- Normal case where type comes from conversion above us
11015
11016 else
11017 Set_Etype (N, Etype (Conv));
11018 end if;
11019 end Fixup_Universal_Fixed_Operation;
11020
5d09245e
AC
11021 ---------------------------------
11022 -- Has_Inferable_Discriminants --
11023 ---------------------------------
11024
11025 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
11026
11027 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
11028 -- Determines whether the left-most prefix of a selected component is a
11029 -- formal parameter in a subprogram. Assumes N is a selected component.
11030
11031 --------------------------------
11032 -- Prefix_Is_Formal_Parameter --
11033 --------------------------------
11034
11035 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
83bb90af 11036 Sel_Comp : Node_Id;
5d09245e
AC
11037
11038 begin
11039 -- Move to the left-most prefix by climbing up the tree
11040
83bb90af 11041 Sel_Comp := N;
5d09245e
AC
11042 while Present (Parent (Sel_Comp))
11043 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
11044 loop
11045 Sel_Comp := Parent (Sel_Comp);
11046 end loop;
11047
11048 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
11049 end Prefix_Is_Formal_Parameter;
11050
11051 -- Start of processing for Has_Inferable_Discriminants
11052
11053 begin
5d09245e
AC
11054 -- For selected components, the subtype of the selector must be a
11055 -- constrained Unchecked_Union. If the component is subject to a
11056 -- per-object constraint, then the enclosing object must have inferable
11057 -- discriminants.
11058
83bb90af 11059 if Nkind (N) = N_Selected_Component then
5d09245e
AC
11060 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
11061
11062 -- A small hack. If we have a per-object constrained selected
11063 -- component of a formal parameter, return True since we do not
11064 -- know the actual parameter association yet.
11065
11066 if Prefix_Is_Formal_Parameter (N) then
11067 return True;
5d09245e
AC
11068
11069 -- Otherwise, check the enclosing object and the selector
11070
83bb90af
TQ
11071 else
11072 return Has_Inferable_Discriminants (Prefix (N))
11073 and then Has_Inferable_Discriminants (Selector_Name (N));
11074 end if;
5d09245e
AC
11075
11076 -- The call to Has_Inferable_Discriminants will determine whether
11077 -- the selector has a constrained Unchecked_Union nominal type.
11078
83bb90af
TQ
11079 else
11080 return Has_Inferable_Discriminants (Selector_Name (N));
11081 end if;
5d09245e
AC
11082
11083 -- A qualified expression has inferable discriminants if its subtype
11084 -- mark is a constrained Unchecked_Union subtype.
11085
11086 elsif Nkind (N) = N_Qualified_Expression then
053cf994 11087 return Is_Unchecked_Union (Etype (Subtype_Mark (N)))
5b5b27ad 11088 and then Is_Constrained (Etype (Subtype_Mark (N)));
5d09245e 11089
83bb90af
TQ
11090 -- For all other names, it is sufficient to have a constrained
11091 -- Unchecked_Union nominal subtype.
11092
11093 else
11094 return Is_Unchecked_Union (Base_Type (Etype (N)))
11095 and then Is_Constrained (Etype (N));
11096 end if;
5d09245e
AC
11097 end Has_Inferable_Discriminants;
11098
70482933
RK
11099 -------------------------------
11100 -- Insert_Dereference_Action --
11101 -------------------------------
11102
11103 procedure Insert_Dereference_Action (N : Node_Id) is
8777c5a6 11104
70482933 11105 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
2e071734
AC
11106 -- Return true if type of P is derived from Checked_Pool;
11107
11108 -----------------------------
11109 -- Is_Checked_Storage_Pool --
11110 -----------------------------
70482933
RK
11111
11112 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
11113 T : Entity_Id;
761f7dcb 11114
70482933
RK
11115 begin
11116 if No (P) then
11117 return False;
11118 end if;
11119
11120 T := Etype (P);
11121 while T /= Etype (T) loop
11122 if Is_RTE (T, RE_Checked_Pool) then
11123 return True;
11124 else
11125 T := Etype (T);
11126 end if;
11127 end loop;
11128
11129 return False;
11130 end Is_Checked_Storage_Pool;
11131
b0d71355
HK
11132 -- Local variables
11133
11134 Typ : constant Entity_Id := Etype (N);
11135 Desig : constant Entity_Id := Available_View (Designated_Type (Typ));
11136 Loc : constant Source_Ptr := Sloc (N);
11137 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
11138 Pnod : constant Node_Id := Parent (N);
11139
11140 Addr : Entity_Id;
11141 Alig : Entity_Id;
11142 Deref : Node_Id;
11143 Size : Entity_Id;
11144 Stmt : Node_Id;
11145
70482933
RK
11146 -- Start of processing for Insert_Dereference_Action
11147
11148 begin
e6f69614
AC
11149 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
11150
b0d71355
HK
11151 -- Do not re-expand a dereference which has already been processed by
11152 -- this routine.
11153
11154 if Has_Dereference_Action (Pnod) then
70482933 11155 return;
70482933 11156
b0d71355
HK
11157 -- Do not perform this type of expansion for internally-generated
11158 -- dereferences.
70482933 11159
b0d71355
HK
11160 elsif not Comes_From_Source (Original_Node (Pnod)) then
11161 return;
70482933 11162
b0d71355
HK
11163 -- A dereference action is only applicable to objects which have been
11164 -- allocated on a checked pool.
70482933 11165
b0d71355
HK
11166 elsif not Is_Checked_Storage_Pool (Pool) then
11167 return;
11168 end if;
70482933 11169
b0d71355 11170 -- Extract the address of the dereferenced object. Generate:
8777c5a6 11171
b0d71355 11172 -- Addr : System.Address := <N>'Pool_Address;
70482933 11173
b0d71355 11174 Addr := Make_Temporary (Loc, 'P');
70482933 11175
b0d71355
HK
11176 Insert_Action (N,
11177 Make_Object_Declaration (Loc,
11178 Defining_Identifier => Addr,
11179 Object_Definition =>
11180 New_Reference_To (RTE (RE_Address), Loc),
11181 Expression =>
11182 Make_Attribute_Reference (Loc,
11183 Prefix => Duplicate_Subexpr_Move_Checks (N),
11184 Attribute_Name => Name_Pool_Address)));
11185
11186 -- Calculate the size of the dereferenced object. Generate:
8777c5a6 11187
b0d71355
HK
11188 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
11189
11190 Deref :=
11191 Make_Explicit_Dereference (Loc,
11192 Prefix => Duplicate_Subexpr_Move_Checks (N));
11193 Set_Has_Dereference_Action (Deref);
70482933 11194
b0d71355
HK
11195 Size := Make_Temporary (Loc, 'S');
11196
11197 Insert_Action (N,
11198 Make_Object_Declaration (Loc,
11199 Defining_Identifier => Size,
8777c5a6 11200
b0d71355
HK
11201 Object_Definition =>
11202 New_Reference_To (RTE (RE_Storage_Count), Loc),
8777c5a6 11203
b0d71355
HK
11204 Expression =>
11205 Make_Op_Divide (Loc,
11206 Left_Opnd =>
70482933 11207 Make_Attribute_Reference (Loc,
b0d71355 11208 Prefix => Deref,
70482933
RK
11209 Attribute_Name => Name_Size),
11210 Right_Opnd =>
b0d71355 11211 Make_Integer_Literal (Loc, System_Storage_Unit))));
70482933 11212
b0d71355
HK
11213 -- Calculate the alignment of the dereferenced object. Generate:
11214 -- Alig : constant Storage_Count := <N>.all'Alignment;
70482933 11215
b0d71355
HK
11216 Deref :=
11217 Make_Explicit_Dereference (Loc,
11218 Prefix => Duplicate_Subexpr_Move_Checks (N));
11219 Set_Has_Dereference_Action (Deref);
11220
11221 Alig := Make_Temporary (Loc, 'A');
11222
11223 Insert_Action (N,
11224 Make_Object_Declaration (Loc,
11225 Defining_Identifier => Alig,
11226 Object_Definition =>
11227 New_Reference_To (RTE (RE_Storage_Count), Loc),
11228 Expression =>
11229 Make_Attribute_Reference (Loc,
11230 Prefix => Deref,
11231 Attribute_Name => Name_Alignment)));
11232
11233 -- A dereference of a controlled object requires special processing. The
11234 -- finalization machinery requests additional space from the underlying
11235 -- pool to allocate and hide two pointers. As a result, a checked pool
11236 -- may mark the wrong memory as valid. Since checked pools do not have
11237 -- knowledge of hidden pointers, we have to bring the two pointers back
11238 -- in view in order to restore the original state of the object.
11239
11240 if Needs_Finalization (Desig) then
11241
11242 -- Adjust the address and size of the dereferenced object. Generate:
11243 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
11244
11245 Stmt :=
11246 Make_Procedure_Call_Statement (Loc,
11247 Name =>
11248 New_Reference_To (RTE (RE_Adjust_Controlled_Dereference), Loc),
11249 Parameter_Associations => New_List (
11250 New_Reference_To (Addr, Loc),
11251 New_Reference_To (Size, Loc),
11252 New_Reference_To (Alig, Loc)));
11253
11254 -- Class-wide types complicate things because we cannot determine
11255 -- statically whether the actual object is truly controlled. We must
11256 -- generate a runtime check to detect this property. Generate:
11257 --
11258 -- if Needs_Finalization (<N>.all'Tag) then
11259 -- <Stmt>;
11260 -- end if;
11261
11262 if Is_Class_Wide_Type (Desig) then
11263 Deref :=
11264 Make_Explicit_Dereference (Loc,
11265 Prefix => Duplicate_Subexpr_Move_Checks (N));
11266 Set_Has_Dereference_Action (Deref);
11267
11268 Stmt :=
8b1011c0 11269 Make_Implicit_If_Statement (N,
b0d71355
HK
11270 Condition =>
11271 Make_Function_Call (Loc,
11272 Name =>
11273 New_Reference_To (RTE (RE_Needs_Finalization), Loc),
11274 Parameter_Associations => New_List (
11275 Make_Attribute_Reference (Loc,
11276 Prefix => Deref,
11277 Attribute_Name => Name_Tag))),
11278 Then_Statements => New_List (Stmt));
11279 end if;
11280
11281 Insert_Action (N, Stmt);
11282 end if;
11283
11284 -- Generate:
11285 -- Dereference (Pool, Addr, Size, Alig);
11286
11287 Insert_Action (N,
11288 Make_Procedure_Call_Statement (Loc,
11289 Name =>
11290 New_Reference_To
11291 (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
11292 Parameter_Associations => New_List (
11293 New_Reference_To (Pool, Loc),
11294 New_Reference_To (Addr, Loc),
11295 New_Reference_To (Size, Loc),
11296 New_Reference_To (Alig, Loc))));
11297
11298 -- Mark the explicit dereference as processed to avoid potential
11299 -- infinite expansion.
11300
11301 Set_Has_Dereference_Action (Pnod);
70482933 11302
fbf5a39b
AC
11303 exception
11304 when RE_Not_Available =>
11305 return;
70482933
RK
11306 end Insert_Dereference_Action;
11307
fdfcc663
AC
11308 --------------------------------
11309 -- Integer_Promotion_Possible --
11310 --------------------------------
11311
11312 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
11313 Operand : constant Node_Id := Expression (N);
11314 Operand_Type : constant Entity_Id := Etype (Operand);
11315 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
11316
11317 begin
11318 pragma Assert (Nkind (N) = N_Type_Conversion);
11319
11320 return
11321
11322 -- We only do the transformation for source constructs. We assume
11323 -- that the expander knows what it is doing when it generates code.
11324
11325 Comes_From_Source (N)
11326
11327 -- If the operand type is Short_Integer or Short_Short_Integer,
11328 -- then we will promote to Integer, which is available on all
11329 -- targets, and is sufficient to ensure no intermediate overflow.
11330 -- Furthermore it is likely to be as efficient or more efficient
11331 -- than using the smaller type for the computation so we do this
11332 -- unconditionally.
11333
11334 and then
11335 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
761f7dcb 11336 or else
fdfcc663
AC
11337 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
11338
11339 -- Test for interesting operation, which includes addition,
5f3f175d
AC
11340 -- division, exponentiation, multiplication, subtraction, absolute
11341 -- value and unary negation. Unary "+" is omitted since it is a
11342 -- no-op and thus can't overflow.
fdfcc663 11343
5f3f175d
AC
11344 and then Nkind_In (Operand, N_Op_Abs,
11345 N_Op_Add,
fdfcc663
AC
11346 N_Op_Divide,
11347 N_Op_Expon,
11348 N_Op_Minus,
11349 N_Op_Multiply,
11350 N_Op_Subtract);
11351 end Integer_Promotion_Possible;
11352
70482933
RK
11353 ------------------------------
11354 -- Make_Array_Comparison_Op --
11355 ------------------------------
11356
11357 -- This is a hand-coded expansion of the following generic function:
11358
11359 -- generic
11360 -- type elem is (<>);
11361 -- type index is (<>);
11362 -- type a is array (index range <>) of elem;
20b5d666 11363
70482933
RK
11364 -- function Gnnn (X : a; Y: a) return boolean is
11365 -- J : index := Y'first;
20b5d666 11366
70482933
RK
11367 -- begin
11368 -- if X'length = 0 then
11369 -- return false;
20b5d666 11370
70482933
RK
11371 -- elsif Y'length = 0 then
11372 -- return true;
20b5d666 11373
70482933
RK
11374 -- else
11375 -- for I in X'range loop
11376 -- if X (I) = Y (J) then
11377 -- if J = Y'last then
11378 -- exit;
11379 -- else
11380 -- J := index'succ (J);
11381 -- end if;
20b5d666 11382
70482933
RK
11383 -- else
11384 -- return X (I) > Y (J);
11385 -- end if;
11386 -- end loop;
20b5d666 11387
70482933
RK
11388 -- return X'length > Y'length;
11389 -- end if;
11390 -- end Gnnn;
11391
11392 -- Note that since we are essentially doing this expansion by hand, we
11393 -- do not need to generate an actual or formal generic part, just the
11394 -- instantiated function itself.
11395
11396 function Make_Array_Comparison_Op
2e071734
AC
11397 (Typ : Entity_Id;
11398 Nod : Node_Id) return Node_Id
70482933
RK
11399 is
11400 Loc : constant Source_Ptr := Sloc (Nod);
11401
11402 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
11403 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
11404 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
11405 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
11406
11407 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
11408
11409 Loop_Statement : Node_Id;
11410 Loop_Body : Node_Id;
11411 If_Stat : Node_Id;
11412 Inner_If : Node_Id;
11413 Final_Expr : Node_Id;
11414 Func_Body : Node_Id;
11415 Func_Name : Entity_Id;
11416 Formals : List_Id;
11417 Length1 : Node_Id;
11418 Length2 : Node_Id;
11419
11420 begin
11421 -- if J = Y'last then
11422 -- exit;
11423 -- else
11424 -- J := index'succ (J);
11425 -- end if;
11426
11427 Inner_If :=
11428 Make_Implicit_If_Statement (Nod,
11429 Condition =>
11430 Make_Op_Eq (Loc,
11431 Left_Opnd => New_Reference_To (J, Loc),
11432 Right_Opnd =>
11433 Make_Attribute_Reference (Loc,
11434 Prefix => New_Reference_To (Y, Loc),
11435 Attribute_Name => Name_Last)),
11436
11437 Then_Statements => New_List (
11438 Make_Exit_Statement (Loc)),
11439
11440 Else_Statements =>
11441 New_List (
11442 Make_Assignment_Statement (Loc,
11443 Name => New_Reference_To (J, Loc),
11444 Expression =>
11445 Make_Attribute_Reference (Loc,
11446 Prefix => New_Reference_To (Index, Loc),
11447 Attribute_Name => Name_Succ,
11448 Expressions => New_List (New_Reference_To (J, Loc))))));
11449
11450 -- if X (I) = Y (J) then
11451 -- if ... end if;
11452 -- else
11453 -- return X (I) > Y (J);
11454 -- end if;
11455
11456 Loop_Body :=
11457 Make_Implicit_If_Statement (Nod,
11458 Condition =>
11459 Make_Op_Eq (Loc,
11460 Left_Opnd =>
11461 Make_Indexed_Component (Loc,
11462 Prefix => New_Reference_To (X, Loc),
11463 Expressions => New_List (New_Reference_To (I, Loc))),
11464
11465 Right_Opnd =>
11466 Make_Indexed_Component (Loc,
11467 Prefix => New_Reference_To (Y, Loc),
11468 Expressions => New_List (New_Reference_To (J, Loc)))),
11469
11470 Then_Statements => New_List (Inner_If),
11471
11472 Else_Statements => New_List (
d766cee3 11473 Make_Simple_Return_Statement (Loc,
70482933
RK
11474 Expression =>
11475 Make_Op_Gt (Loc,
11476 Left_Opnd =>
11477 Make_Indexed_Component (Loc,
11478 Prefix => New_Reference_To (X, Loc),
11479 Expressions => New_List (New_Reference_To (I, Loc))),
11480
11481 Right_Opnd =>
11482 Make_Indexed_Component (Loc,
11483 Prefix => New_Reference_To (Y, Loc),
11484 Expressions => New_List (
11485 New_Reference_To (J, Loc)))))));
11486
11487 -- for I in X'range loop
11488 -- if ... end if;
11489 -- end loop;
11490
11491 Loop_Statement :=
11492 Make_Implicit_Loop_Statement (Nod,
11493 Identifier => Empty,
11494
11495 Iteration_Scheme =>
11496 Make_Iteration_Scheme (Loc,
11497 Loop_Parameter_Specification =>
11498 Make_Loop_Parameter_Specification (Loc,
11499 Defining_Identifier => I,
11500 Discrete_Subtype_Definition =>
11501 Make_Attribute_Reference (Loc,
11502 Prefix => New_Reference_To (X, Loc),
11503 Attribute_Name => Name_Range))),
11504
11505 Statements => New_List (Loop_Body));
11506
11507 -- if X'length = 0 then
11508 -- return false;
11509 -- elsif Y'length = 0 then
11510 -- return true;
11511 -- else
11512 -- for ... loop ... end loop;
11513 -- return X'length > Y'length;
11514 -- end if;
11515
11516 Length1 :=
11517 Make_Attribute_Reference (Loc,
11518 Prefix => New_Reference_To (X, Loc),
11519 Attribute_Name => Name_Length);
11520
11521 Length2 :=
11522 Make_Attribute_Reference (Loc,
11523 Prefix => New_Reference_To (Y, Loc),
11524 Attribute_Name => Name_Length);
11525
11526 Final_Expr :=
11527 Make_Op_Gt (Loc,
11528 Left_Opnd => Length1,
11529 Right_Opnd => Length2);
11530
11531 If_Stat :=
11532 Make_Implicit_If_Statement (Nod,
11533 Condition =>
11534 Make_Op_Eq (Loc,
11535 Left_Opnd =>
11536 Make_Attribute_Reference (Loc,
11537 Prefix => New_Reference_To (X, Loc),
11538 Attribute_Name => Name_Length),
11539 Right_Opnd =>
11540 Make_Integer_Literal (Loc, 0)),
11541
11542 Then_Statements =>
11543 New_List (
d766cee3 11544 Make_Simple_Return_Statement (Loc,
70482933
RK
11545 Expression => New_Reference_To (Standard_False, Loc))),
11546
11547 Elsif_Parts => New_List (
11548 Make_Elsif_Part (Loc,
11549 Condition =>
11550 Make_Op_Eq (Loc,
11551 Left_Opnd =>
11552 Make_Attribute_Reference (Loc,
11553 Prefix => New_Reference_To (Y, Loc),
11554 Attribute_Name => Name_Length),
11555 Right_Opnd =>
11556 Make_Integer_Literal (Loc, 0)),
11557
11558 Then_Statements =>
11559 New_List (
d766cee3 11560 Make_Simple_Return_Statement (Loc,
70482933
RK
11561 Expression => New_Reference_To (Standard_True, Loc))))),
11562
11563 Else_Statements => New_List (
11564 Loop_Statement,
d766cee3 11565 Make_Simple_Return_Statement (Loc,
70482933
RK
11566 Expression => Final_Expr)));
11567
11568 -- (X : a; Y: a)
11569
11570 Formals := New_List (
11571 Make_Parameter_Specification (Loc,
11572 Defining_Identifier => X,
11573 Parameter_Type => New_Reference_To (Typ, Loc)),
11574
11575 Make_Parameter_Specification (Loc,
11576 Defining_Identifier => Y,
11577 Parameter_Type => New_Reference_To (Typ, Loc)));
11578
11579 -- function Gnnn (...) return boolean is
11580 -- J : index := Y'first;
11581 -- begin
11582 -- if ... end if;
11583 -- end Gnnn;
11584
191fcb3a 11585 Func_Name := Make_Temporary (Loc, 'G');
70482933
RK
11586
11587 Func_Body :=
11588 Make_Subprogram_Body (Loc,
11589 Specification =>
11590 Make_Function_Specification (Loc,
11591 Defining_Unit_Name => Func_Name,
11592 Parameter_Specifications => Formals,
630d30e9 11593 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
70482933
RK
11594
11595 Declarations => New_List (
11596 Make_Object_Declaration (Loc,
11597 Defining_Identifier => J,
11598 Object_Definition => New_Reference_To (Index, Loc),
11599 Expression =>
11600 Make_Attribute_Reference (Loc,
11601 Prefix => New_Reference_To (Y, Loc),
11602 Attribute_Name => Name_First))),
11603
11604 Handled_Statement_Sequence =>
11605 Make_Handled_Sequence_Of_Statements (Loc,
11606 Statements => New_List (If_Stat)));
11607
11608 return Func_Body;
70482933
RK
11609 end Make_Array_Comparison_Op;
11610
11611 ---------------------------
11612 -- Make_Boolean_Array_Op --
11613 ---------------------------
11614
685094bf
RD
11615 -- For logical operations on boolean arrays, expand in line the following,
11616 -- replacing 'and' with 'or' or 'xor' where needed:
70482933
RK
11617
11618 -- function Annn (A : typ; B: typ) return typ is
11619 -- C : typ;
11620 -- begin
11621 -- for J in A'range loop
11622 -- C (J) := A (J) op B (J);
11623 -- end loop;
11624 -- return C;
11625 -- end Annn;
11626
11627 -- Here typ is the boolean array type
11628
11629 function Make_Boolean_Array_Op
2e071734
AC
11630 (Typ : Entity_Id;
11631 N : Node_Id) return Node_Id
70482933
RK
11632 is
11633 Loc : constant Source_Ptr := Sloc (N);
11634
11635 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
11636 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
11637 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
11638 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
11639
11640 A_J : Node_Id;
11641 B_J : Node_Id;
11642 C_J : Node_Id;
11643 Op : Node_Id;
11644
11645 Formals : List_Id;
11646 Func_Name : Entity_Id;
11647 Func_Body : Node_Id;
11648 Loop_Statement : Node_Id;
11649
11650 begin
11651 A_J :=
11652 Make_Indexed_Component (Loc,
11653 Prefix => New_Reference_To (A, Loc),
11654 Expressions => New_List (New_Reference_To (J, Loc)));
11655
11656 B_J :=
11657 Make_Indexed_Component (Loc,
11658 Prefix => New_Reference_To (B, Loc),
11659 Expressions => New_List (New_Reference_To (J, Loc)));
11660
11661 C_J :=
11662 Make_Indexed_Component (Loc,
11663 Prefix => New_Reference_To (C, Loc),
11664 Expressions => New_List (New_Reference_To (J, Loc)));
11665
11666 if Nkind (N) = N_Op_And then
11667 Op :=
11668 Make_Op_And (Loc,
11669 Left_Opnd => A_J,
11670 Right_Opnd => B_J);
11671
11672 elsif Nkind (N) = N_Op_Or then
11673 Op :=
11674 Make_Op_Or (Loc,
11675 Left_Opnd => A_J,
11676 Right_Opnd => B_J);
11677
11678 else
11679 Op :=
11680 Make_Op_Xor (Loc,
11681 Left_Opnd => A_J,
11682 Right_Opnd => B_J);
11683 end if;
11684
11685 Loop_Statement :=
11686 Make_Implicit_Loop_Statement (N,
11687 Identifier => Empty,
11688
11689 Iteration_Scheme =>
11690 Make_Iteration_Scheme (Loc,
11691 Loop_Parameter_Specification =>
11692 Make_Loop_Parameter_Specification (Loc,
11693 Defining_Identifier => J,
11694 Discrete_Subtype_Definition =>
11695 Make_Attribute_Reference (Loc,
11696 Prefix => New_Reference_To (A, Loc),
11697 Attribute_Name => Name_Range))),
11698
11699 Statements => New_List (
11700 Make_Assignment_Statement (Loc,
11701 Name => C_J,
11702 Expression => Op)));
11703
11704 Formals := New_List (
11705 Make_Parameter_Specification (Loc,
11706 Defining_Identifier => A,
11707 Parameter_Type => New_Reference_To (Typ, Loc)),
11708
11709 Make_Parameter_Specification (Loc,
11710 Defining_Identifier => B,
11711 Parameter_Type => New_Reference_To (Typ, Loc)));
11712
191fcb3a 11713 Func_Name := Make_Temporary (Loc, 'A');
70482933
RK
11714 Set_Is_Inlined (Func_Name);
11715
11716 Func_Body :=
11717 Make_Subprogram_Body (Loc,
11718 Specification =>
11719 Make_Function_Specification (Loc,
11720 Defining_Unit_Name => Func_Name,
11721 Parameter_Specifications => Formals,
630d30e9 11722 Result_Definition => New_Reference_To (Typ, Loc)),
70482933
RK
11723
11724 Declarations => New_List (
11725 Make_Object_Declaration (Loc,
11726 Defining_Identifier => C,
11727 Object_Definition => New_Reference_To (Typ, Loc))),
11728
11729 Handled_Statement_Sequence =>
11730 Make_Handled_Sequence_Of_Statements (Loc,
11731 Statements => New_List (
11732 Loop_Statement,
d766cee3 11733 Make_Simple_Return_Statement (Loc,
70482933
RK
11734 Expression => New_Reference_To (C, Loc)))));
11735
11736 return Func_Body;
11737 end Make_Boolean_Array_Op;
11738
b6b5cca8
AC
11739 -----------------------------------------
11740 -- Minimized_Eliminated_Overflow_Check --
11741 -----------------------------------------
11742
11743 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is
11744 begin
11745 return
11746 Is_Signed_Integer_Type (Etype (N))
a7f1b24f 11747 and then Overflow_Check_Mode in Minimized_Or_Eliminated;
b6b5cca8
AC
11748 end Minimized_Eliminated_Overflow_Check;
11749
0580d807
AC
11750 --------------------------------
11751 -- Optimize_Length_Comparison --
11752 --------------------------------
11753
11754 procedure Optimize_Length_Comparison (N : Node_Id) is
11755 Loc : constant Source_Ptr := Sloc (N);
11756 Typ : constant Entity_Id := Etype (N);
11757 Result : Node_Id;
11758
11759 Left : Node_Id;
11760 Right : Node_Id;
11761 -- First and Last attribute reference nodes, which end up as left and
11762 -- right operands of the optimized result.
11763
11764 Is_Zero : Boolean;
11765 -- True for comparison operand of zero
11766
11767 Comp : Node_Id;
11768 -- Comparison operand, set only if Is_Zero is false
11769
11770 Ent : Entity_Id;
11771 -- Entity whose length is being compared
11772
11773 Index : Node_Id;
11774 -- Integer_Literal node for length attribute expression, or Empty
11775 -- if there is no such expression present.
11776
11777 Ityp : Entity_Id;
11778 -- Type of array index to which 'Length is applied
11779
11780 Op : Node_Kind := Nkind (N);
11781 -- Kind of comparison operator, gets flipped if operands backwards
11782
11783 function Is_Optimizable (N : Node_Id) return Boolean;
abcd9db2
AC
11784 -- Tests N to see if it is an optimizable comparison value (defined as
11785 -- constant zero or one, or something else where the value is known to
11786 -- be positive and in the range of 32-bits, and where the corresponding
11787 -- Length value is also known to be 32-bits. If result is true, sets
11788 -- Is_Zero, Ityp, and Comp accordingly.
0580d807
AC
11789
11790 function Is_Entity_Length (N : Node_Id) return Boolean;
11791 -- Tests if N is a length attribute applied to a simple entity. If so,
11792 -- returns True, and sets Ent to the entity, and Index to the integer
11793 -- literal provided as an attribute expression, or to Empty if none.
11794 -- Also returns True if the expression is a generated type conversion
11795 -- whose expression is of the desired form. This latter case arises
11796 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
11797 -- to check for being in range, which is not needed in this context.
11798 -- Returns False if neither condition holds.
11799
11800 function Prepare_64 (N : Node_Id) return Node_Id;
11801 -- Given a discrete expression, returns a Long_Long_Integer typed
11802 -- expression representing the underlying value of the expression.
11803 -- This is done with an unchecked conversion to the result type. We
11804 -- use unchecked conversion to handle the enumeration type case.
11805
11806 ----------------------
11807 -- Is_Entity_Length --
11808 ----------------------
11809
11810 function Is_Entity_Length (N : Node_Id) return Boolean is
11811 begin
11812 if Nkind (N) = N_Attribute_Reference
11813 and then Attribute_Name (N) = Name_Length
11814 and then Is_Entity_Name (Prefix (N))
11815 then
11816 Ent := Entity (Prefix (N));
11817
11818 if Present (Expressions (N)) then
11819 Index := First (Expressions (N));
11820 else
11821 Index := Empty;
11822 end if;
11823
11824 return True;
11825
11826 elsif Nkind (N) = N_Type_Conversion
11827 and then not Comes_From_Source (N)
11828 then
11829 return Is_Entity_Length (Expression (N));
11830
11831 else
11832 return False;
11833 end if;
11834 end Is_Entity_Length;
11835
11836 --------------------
11837 -- Is_Optimizable --
11838 --------------------
11839
11840 function Is_Optimizable (N : Node_Id) return Boolean is
11841 Val : Uint;
11842 OK : Boolean;
11843 Lo : Uint;
11844 Hi : Uint;
11845 Indx : Node_Id;
11846
11847 begin
11848 if Compile_Time_Known_Value (N) then
11849 Val := Expr_Value (N);
11850
11851 if Val = Uint_0 then
11852 Is_Zero := True;
11853 Comp := Empty;
11854 return True;
11855
11856 elsif Val = Uint_1 then
11857 Is_Zero := False;
11858 Comp := Empty;
11859 return True;
11860 end if;
11861 end if;
11862
11863 -- Here we have to make sure of being within 32-bits
11864
11865 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
11866
11867 if not OK
abcd9db2 11868 or else Lo < Uint_1
0580d807
AC
11869 or else Hi > UI_From_Int (Int'Last)
11870 then
11871 return False;
11872 end if;
11873
abcd9db2
AC
11874 -- Comparison value was within range, so now we must check the index
11875 -- value to make sure it is also within 32-bits.
0580d807
AC
11876
11877 Indx := First_Index (Etype (Ent));
11878
11879 if Present (Index) then
11880 for J in 2 .. UI_To_Int (Intval (Index)) loop
11881 Next_Index (Indx);
11882 end loop;
11883 end if;
11884
11885 Ityp := Etype (Indx);
11886
11887 if Esize (Ityp) > 32 then
11888 return False;
11889 end if;
11890
11891 Is_Zero := False;
11892 Comp := N;
11893 return True;
11894 end Is_Optimizable;
11895
11896 ----------------
11897 -- Prepare_64 --
11898 ----------------
11899
11900 function Prepare_64 (N : Node_Id) return Node_Id is
11901 begin
11902 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
11903 end Prepare_64;
11904
11905 -- Start of processing for Optimize_Length_Comparison
11906
11907 begin
11908 -- Nothing to do if not a comparison
11909
11910 if Op not in N_Op_Compare then
11911 return;
11912 end if;
11913
11914 -- Nothing to do if special -gnatd.P debug flag set
11915
11916 if Debug_Flag_Dot_PP then
11917 return;
11918 end if;
11919
11920 -- Ent'Length op 0/1
11921
11922 if Is_Entity_Length (Left_Opnd (N))
11923 and then Is_Optimizable (Right_Opnd (N))
11924 then
11925 null;
11926
11927 -- 0/1 op Ent'Length
11928
11929 elsif Is_Entity_Length (Right_Opnd (N))
11930 and then Is_Optimizable (Left_Opnd (N))
11931 then
11932 -- Flip comparison to opposite sense
11933
11934 case Op is
11935 when N_Op_Lt => Op := N_Op_Gt;
11936 when N_Op_Le => Op := N_Op_Ge;
11937 when N_Op_Gt => Op := N_Op_Lt;
11938 when N_Op_Ge => Op := N_Op_Le;
11939 when others => null;
11940 end case;
11941
11942 -- Else optimization not possible
11943
11944 else
11945 return;
11946 end if;
11947
11948 -- Fall through if we will do the optimization
11949
11950 -- Cases to handle:
11951
11952 -- X'Length = 0 => X'First > X'Last
11953 -- X'Length = 1 => X'First = X'Last
11954 -- X'Length = n => X'First + (n - 1) = X'Last
11955
11956 -- X'Length /= 0 => X'First <= X'Last
11957 -- X'Length /= 1 => X'First /= X'Last
11958 -- X'Length /= n => X'First + (n - 1) /= X'Last
11959
11960 -- X'Length >= 0 => always true, warn
11961 -- X'Length >= 1 => X'First <= X'Last
11962 -- X'Length >= n => X'First + (n - 1) <= X'Last
11963
11964 -- X'Length > 0 => X'First <= X'Last
11965 -- X'Length > 1 => X'First < X'Last
11966 -- X'Length > n => X'First + (n - 1) < X'Last
11967
11968 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
11969 -- X'Length <= 1 => X'First >= X'Last
11970 -- X'Length <= n => X'First + (n - 1) >= X'Last
11971
11972 -- X'Length < 0 => always false (warn)
11973 -- X'Length < 1 => X'First > X'Last
11974 -- X'Length < n => X'First + (n - 1) > X'Last
11975
11976 -- Note: for the cases of n (not constant 0,1), we require that the
11977 -- corresponding index type be integer or shorter (i.e. not 64-bit),
11978 -- and the same for the comparison value. Then we do the comparison
11979 -- using 64-bit arithmetic (actually long long integer), so that we
11980 -- cannot have overflow intefering with the result.
11981
11982 -- First deal with warning cases
11983
11984 if Is_Zero then
11985 case Op is
11986
11987 -- X'Length >= 0
11988
11989 when N_Op_Ge =>
11990 Rewrite (N,
11991 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
11992 Analyze_And_Resolve (N, Typ);
11993 Warn_On_Known_Condition (N);
11994 return;
11995
11996 -- X'Length < 0
11997
11998 when N_Op_Lt =>
11999 Rewrite (N,
12000 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
12001 Analyze_And_Resolve (N, Typ);
12002 Warn_On_Known_Condition (N);
12003 return;
12004
12005 when N_Op_Le =>
12006 if Constant_Condition_Warnings
12007 and then Comes_From_Source (Original_Node (N))
12008 then
324ac540 12009 Error_Msg_N ("could replace by ""'=""?c?", N);
0580d807
AC
12010 end if;
12011
12012 Op := N_Op_Eq;
12013
12014 when others =>
12015 null;
12016 end case;
12017 end if;
12018
12019 -- Build the First reference we will use
12020
12021 Left :=
12022 Make_Attribute_Reference (Loc,
12023 Prefix => New_Occurrence_Of (Ent, Loc),
12024 Attribute_Name => Name_First);
12025
12026 if Present (Index) then
12027 Set_Expressions (Left, New_List (New_Copy (Index)));
12028 end if;
12029
12030 -- If general value case, then do the addition of (n - 1), and
12031 -- also add the needed conversions to type Long_Long_Integer.
12032
12033 if Present (Comp) then
12034 Left :=
12035 Make_Op_Add (Loc,
12036 Left_Opnd => Prepare_64 (Left),
12037 Right_Opnd =>
12038 Make_Op_Subtract (Loc,
12039 Left_Opnd => Prepare_64 (Comp),
12040 Right_Opnd => Make_Integer_Literal (Loc, 1)));
12041 end if;
12042
12043 -- Build the Last reference we will use
12044
12045 Right :=
12046 Make_Attribute_Reference (Loc,
12047 Prefix => New_Occurrence_Of (Ent, Loc),
12048 Attribute_Name => Name_Last);
12049
12050 if Present (Index) then
12051 Set_Expressions (Right, New_List (New_Copy (Index)));
12052 end if;
12053
12054 -- If general operand, convert Last reference to Long_Long_Integer
12055
12056 if Present (Comp) then
12057 Right := Prepare_64 (Right);
12058 end if;
12059
12060 -- Check for cases to optimize
12061
12062 -- X'Length = 0 => X'First > X'Last
12063 -- X'Length < 1 => X'First > X'Last
12064 -- X'Length < n => X'First + (n - 1) > X'Last
12065
12066 if (Is_Zero and then Op = N_Op_Eq)
12067 or else (not Is_Zero and then Op = N_Op_Lt)
12068 then
12069 Result :=
12070 Make_Op_Gt (Loc,
12071 Left_Opnd => Left,
12072 Right_Opnd => Right);
12073
12074 -- X'Length = 1 => X'First = X'Last
12075 -- X'Length = n => X'First + (n - 1) = X'Last
12076
12077 elsif not Is_Zero and then Op = N_Op_Eq then
12078 Result :=
12079 Make_Op_Eq (Loc,
12080 Left_Opnd => Left,
12081 Right_Opnd => Right);
12082
12083 -- X'Length /= 0 => X'First <= X'Last
12084 -- X'Length > 0 => X'First <= X'Last
12085
12086 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
12087 Result :=
12088 Make_Op_Le (Loc,
12089 Left_Opnd => Left,
12090 Right_Opnd => Right);
12091
12092 -- X'Length /= 1 => X'First /= X'Last
12093 -- X'Length /= n => X'First + (n - 1) /= X'Last
12094
12095 elsif not Is_Zero and then Op = N_Op_Ne then
12096 Result :=
12097 Make_Op_Ne (Loc,
12098 Left_Opnd => Left,
12099 Right_Opnd => Right);
12100
12101 -- X'Length >= 1 => X'First <= X'Last
12102 -- X'Length >= n => X'First + (n - 1) <= X'Last
12103
12104 elsif not Is_Zero and then Op = N_Op_Ge then
12105 Result :=
12106 Make_Op_Le (Loc,
12107 Left_Opnd => Left,
12108 Right_Opnd => Right);
12109
12110 -- X'Length > 1 => X'First < X'Last
12111 -- X'Length > n => X'First + (n = 1) < X'Last
12112
12113 elsif not Is_Zero and then Op = N_Op_Gt then
12114 Result :=
12115 Make_Op_Lt (Loc,
12116 Left_Opnd => Left,
12117 Right_Opnd => Right);
12118
12119 -- X'Length <= 1 => X'First >= X'Last
12120 -- X'Length <= n => X'First + (n - 1) >= X'Last
12121
12122 elsif not Is_Zero and then Op = N_Op_Le then
12123 Result :=
12124 Make_Op_Ge (Loc,
12125 Left_Opnd => Left,
12126 Right_Opnd => Right);
12127
12128 -- Should not happen at this stage
12129
12130 else
12131 raise Program_Error;
12132 end if;
12133
12134 -- Rewrite and finish up
12135
12136 Rewrite (N, Result);
12137 Analyze_And_Resolve (N, Typ);
12138 return;
12139 end Optimize_Length_Comparison;
12140
b2c28399
AC
12141 ------------------------------
12142 -- Process_Transient_Object --
12143 ------------------------------
12144
12145 procedure Process_Transient_Object
12146 (Decl : Node_Id;
12147 Rel_Node : Node_Id)
12148 is
12149 function Find_Enclosing_Context (N : Node_Id) return Node_Id;
12150 -- Find the logical context where N appears. The context is chosen such
12151 -- that it is possible to insert before and after it.
12152
12153 ----------------------------
12154 -- Find_Enclosing_Context --
12155 ----------------------------
12156
12157 function Find_Enclosing_Context (N : Node_Id) return Node_Id is
12158 Par : Node_Id;
12159 Top : Node_Id;
12160
12161 begin
12162 -- When the node is inside a case/if expression, the lifetime of any
12163 -- temporary controlled object is extended. Find a suitable insertion
12164 -- node by locating the topmost case or if expressions.
12165
12166 if Within_Case_Or_If_Expression (N) then
12167 Par := N;
12168 Top := N;
12169 while Present (Par) loop
12170 if Nkind_In (Original_Node (Par), N_Case_Expression,
12171 N_If_Expression)
12172 then
12173 Top := Par;
12174
12175 -- Prevent the search from going too far
12176
12177 elsif Is_Body_Or_Package_Declaration (Par) then
12178 exit;
12179 end if;
12180
12181 Par := Parent (Par);
12182 end loop;
12183
12184 -- The topmost case or if expression is now recovered, but it may
12185 -- still not be the correct place to add generated code. Climb to
12186 -- find a parent that is part of a declarative or statement list.
12187
12188 Par := Top;
12189 while Present (Par) loop
12190 if Is_List_Member (Par)
12191 and then not Nkind_In (Par, N_Component_Association,
12192 N_Discriminant_Association,
12193 N_Parameter_Association,
12194 N_Pragma_Argument_Association)
12195 then
12196 return Par;
12197
12198 -- Prevent the search from going too far
12199
12200 elsif Is_Body_Or_Package_Declaration (Par) then
12201 exit;
12202 end if;
12203
12204 Par := Parent (Par);
12205 end loop;
12206
12207 return Par;
12208
12209 -- Short circuit operators in complex expressions are converted into
12210 -- expression_with_actions.
12211
12212 else
12213 -- Handle the case where the node is buried deep inside an if
12214 -- statement. The temporary controlled object must be finalized
12215 -- before the then, elsif or else statements are evaluated.
12216
12217 -- if Something
12218 -- and then Ctrl_Func_Call
12219 -- then
12220 -- <result must be finalized at this point>
12221 -- <statements>
12222 -- end if;
12223
12224 -- To achieve this, find the topmost logical operator. Generated
12225 -- actions are then inserted before/after it.
12226
12227 Par := N;
12228 while Present (Par) loop
12229
12230 -- Keep climbing past various operators
12231
12232 if Nkind (Parent (Par)) in N_Op
12233 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
12234 then
12235 Par := Parent (Par);
12236 else
12237 exit;
12238 end if;
12239 end loop;
12240
12241 Top := Par;
12242
12243 -- The node may be located in a pragma in which case return the
12244 -- pragma itself:
12245
12246 -- pragma Precondition (... and then Ctrl_Func_Call ...);
12247
12248 -- Similar case occurs when the node is related to an object
12249 -- declaration or assignment:
12250
12251 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
12252
12253 -- Another case to consider is when the node is part of a return
12254 -- statement:
12255
12256 -- return ... and then Ctrl_Func_Call ...;
12257
12258 -- Another case is when the node acts as a formal in a procedure
12259 -- call statement:
12260
12261 -- Proc (... and then Ctrl_Func_Call ...);
12262
12263 while Present (Par) loop
12264 if Nkind_In (Par, N_Assignment_Statement,
12265 N_Object_Declaration,
12266 N_Pragma,
12267 N_Procedure_Call_Statement,
12268 N_Simple_Return_Statement)
12269 then
12270 return Par;
12271
12272 -- Prevent the search from going too far
12273
12274 elsif Is_Body_Or_Package_Declaration (Par) then
12275 exit;
12276 end if;
12277
12278 Par := Parent (Par);
12279 end loop;
12280
12281 -- Return the topmost short circuit operator
12282
12283 return Top;
12284 end if;
12285 end Find_Enclosing_Context;
12286
12287 -- Local variables
12288
12289 Context : constant Node_Id := Find_Enclosing_Context (Rel_Node);
12290 Loc : constant Source_Ptr := Sloc (Decl);
12291 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
12292 Obj_Typ : constant Node_Id := Etype (Obj_Id);
12293 Desig_Typ : Entity_Id;
12294 Expr : Node_Id;
12295 Fin_Call : Node_Id;
12296 Ptr_Id : Entity_Id;
12297 Temp_Id : Entity_Id;
12298
12299 -- Start of processing for Process_Transient_Object
12300
12301 begin
12302 -- Step 1: Create the access type which provides a reference to the
12303 -- transient controlled object.
12304
12305 if Is_Access_Type (Obj_Typ) then
12306 Desig_Typ := Directly_Designated_Type (Obj_Typ);
12307 else
12308 Desig_Typ := Obj_Typ;
12309 end if;
12310
12311 Desig_Typ := Base_Type (Desig_Typ);
12312
12313 -- Generate:
12314 -- Ann : access [all] <Desig_Typ>;
12315
12316 Ptr_Id := Make_Temporary (Loc, 'A');
12317
12318 Insert_Action (Context,
12319 Make_Full_Type_Declaration (Loc,
12320 Defining_Identifier => Ptr_Id,
12321 Type_Definition =>
12322 Make_Access_To_Object_Definition (Loc,
12323 All_Present => Ekind (Obj_Typ) = E_General_Access_Type,
12324 Subtype_Indication => New_Reference_To (Desig_Typ, Loc))));
12325
12326 -- Step 2: Create a temporary which acts as a hook to the transient
12327 -- controlled object. Generate:
12328
12329 -- Temp : Ptr_Id := null;
12330
12331 Temp_Id := Make_Temporary (Loc, 'T');
12332
12333 Insert_Action (Context,
12334 Make_Object_Declaration (Loc,
12335 Defining_Identifier => Temp_Id,
12336 Object_Definition => New_Reference_To (Ptr_Id, Loc)));
12337
12338 -- Mark the temporary as created for the purposes of exporting the
12339 -- transient controlled object out of the expression_with_action or if
12340 -- expression. This signals the machinery in Build_Finalizer to treat
12341 -- this case specially.
12342
12343 Set_Status_Flag_Or_Transient_Decl (Temp_Id, Decl);
12344
12345 -- Step 3: Hook the transient object to the temporary
12346
12347 -- The use of unchecked conversion / unrestricted access is needed to
12348 -- avoid an accessibility violation. Note that the finalization code is
12349 -- structured in such a way that the "hook" is processed only when it
12350 -- points to an existing object.
12351
12352 if Is_Access_Type (Obj_Typ) then
12353 Expr := Unchecked_Convert_To (Ptr_Id, New_Reference_To (Obj_Id, Loc));
12354 else
12355 Expr :=
12356 Make_Attribute_Reference (Loc,
12357 Prefix => New_Reference_To (Obj_Id, Loc),
12358 Attribute_Name => Name_Unrestricted_Access);
12359 end if;
12360
12361 -- Generate:
12362 -- Temp := Ptr_Id (Obj_Id);
12363 -- <or>
12364 -- Temp := Obj_Id'Unrestricted_Access;
12365
12366 Insert_After_And_Analyze (Decl,
12367 Make_Assignment_Statement (Loc,
12368 Name => New_Reference_To (Temp_Id, Loc),
12369 Expression => Expr));
12370
12371 -- Step 4: Finalize the transient controlled object after the context
12372 -- has been evaluated/elaborated. Generate:
12373
12374 -- if Temp /= null then
12375 -- [Deep_]Finalize (Temp.all);
12376 -- Temp := null;
12377 -- end if;
12378
12379 -- When the node is part of a return statement, there is no need to
12380 -- insert a finalization call, as the general finalization mechanism
12381 -- (see Build_Finalizer) would take care of the transient controlled
12382 -- object on subprogram exit. Note that it would also be impossible to
12383 -- insert the finalization code after the return statement as this will
12384 -- render it unreachable.
12385
12386 if Nkind (Context) /= N_Simple_Return_Statement then
12387 Fin_Call :=
12388 Make_Implicit_If_Statement (Decl,
12389 Condition =>
12390 Make_Op_Ne (Loc,
12391 Left_Opnd => New_Reference_To (Temp_Id, Loc),
12392 Right_Opnd => Make_Null (Loc)),
12393
12394 Then_Statements => New_List (
12395 Make_Final_Call
12396 (Obj_Ref =>
12397 Make_Explicit_Dereference (Loc,
12398 Prefix => New_Reference_To (Temp_Id, Loc)),
12399 Typ => Desig_Typ),
12400
12401 Make_Assignment_Statement (Loc,
12402 Name => New_Reference_To (Temp_Id, Loc),
12403 Expression => Make_Null (Loc))));
12404
12405 -- Use the Actions list of logical operators when inserting the
12406 -- finalization call. This ensures that all transient controlled
12407 -- objects are finalized after the operators are evaluated.
12408
12409 if Nkind_In (Context, N_And_Then, N_Or_Else) then
12410 Insert_Action (Context, Fin_Call);
12411 else
12412 Insert_Action_After (Context, Fin_Call);
12413 end if;
12414 end if;
12415 end Process_Transient_Object;
12416
70482933
RK
12417 ------------------------
12418 -- Rewrite_Comparison --
12419 ------------------------
12420
12421 procedure Rewrite_Comparison (N : Node_Id) is
c800f862
RD
12422 Warning_Generated : Boolean := False;
12423 -- Set to True if first pass with Assume_Valid generates a warning in
12424 -- which case we skip the second pass to avoid warning overloaded.
12425
12426 Result : Node_Id;
12427 -- Set to Standard_True or Standard_False
12428
d26dc4b5
AC
12429 begin
12430 if Nkind (N) = N_Type_Conversion then
12431 Rewrite_Comparison (Expression (N));
20b5d666 12432 return;
70482933 12433
d26dc4b5 12434 elsif Nkind (N) not in N_Op_Compare then
20b5d666
JM
12435 return;
12436 end if;
70482933 12437
c800f862
RD
12438 -- Now start looking at the comparison in detail. We potentially go
12439 -- through this loop twice. The first time, Assume_Valid is set False
12440 -- in the call to Compile_Time_Compare. If this call results in a
12441 -- clear result of always True or Always False, that's decisive and
12442 -- we are done. Otherwise we repeat the processing with Assume_Valid
e7e4d230 12443 -- set to True to generate additional warnings. We can skip that step
c800f862
RD
12444 -- if Constant_Condition_Warnings is False.
12445
12446 for AV in False .. True loop
12447 declare
12448 Typ : constant Entity_Id := Etype (N);
12449 Op1 : constant Node_Id := Left_Opnd (N);
12450 Op2 : constant Node_Id := Right_Opnd (N);
70482933 12451
c800f862
RD
12452 Res : constant Compare_Result :=
12453 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
12454 -- Res indicates if compare outcome can be compile time determined
f02b8bb8 12455
c800f862
RD
12456 True_Result : Boolean;
12457 False_Result : Boolean;
f02b8bb8 12458
c800f862
RD
12459 begin
12460 case N_Op_Compare (Nkind (N)) is
d26dc4b5
AC
12461 when N_Op_Eq =>
12462 True_Result := Res = EQ;
12463 False_Result := Res = LT or else Res = GT or else Res = NE;
12464
12465 when N_Op_Ge =>
12466 True_Result := Res in Compare_GE;
12467 False_Result := Res = LT;
12468
12469 if Res = LE
12470 and then Constant_Condition_Warnings
12471 and then Comes_From_Source (Original_Node (N))
12472 and then Nkind (Original_Node (N)) = N_Op_Ge
12473 and then not In_Instance
d26dc4b5 12474 and then Is_Integer_Type (Etype (Left_Opnd (N)))
59ae6391 12475 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
d26dc4b5 12476 then
ed2233dc 12477 Error_Msg_N
324ac540
AC
12478 ("can never be greater than, could replace by ""'=""?c?",
12479 N);
c800f862 12480 Warning_Generated := True;
d26dc4b5 12481 end if;
70482933 12482
d26dc4b5
AC
12483 when N_Op_Gt =>
12484 True_Result := Res = GT;
12485 False_Result := Res in Compare_LE;
12486
12487 when N_Op_Lt =>
12488 True_Result := Res = LT;
12489 False_Result := Res in Compare_GE;
12490
12491 when N_Op_Le =>
12492 True_Result := Res in Compare_LE;
12493 False_Result := Res = GT;
12494
12495 if Res = GE
12496 and then Constant_Condition_Warnings
12497 and then Comes_From_Source (Original_Node (N))
12498 and then Nkind (Original_Node (N)) = N_Op_Le
12499 and then not In_Instance
d26dc4b5 12500 and then Is_Integer_Type (Etype (Left_Opnd (N)))
59ae6391 12501 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
d26dc4b5 12502 then
ed2233dc 12503 Error_Msg_N
324ac540 12504 ("can never be less than, could replace by ""'=""?c?", N);
c800f862 12505 Warning_Generated := True;
d26dc4b5 12506 end if;
70482933 12507
d26dc4b5
AC
12508 when N_Op_Ne =>
12509 True_Result := Res = NE or else Res = GT or else Res = LT;
12510 False_Result := Res = EQ;
c800f862 12511 end case;
d26dc4b5 12512
c800f862
RD
12513 -- If this is the first iteration, then we actually convert the
12514 -- comparison into True or False, if the result is certain.
d26dc4b5 12515
c800f862
RD
12516 if AV = False then
12517 if True_Result or False_Result then
21791d97 12518 Result := Boolean_Literals (True_Result);
c800f862
RD
12519 Rewrite (N,
12520 Convert_To (Typ,
12521 New_Occurrence_Of (Result, Sloc (N))));
12522 Analyze_And_Resolve (N, Typ);
12523 Warn_On_Known_Condition (N);
12524 return;
12525 end if;
12526
12527 -- If this is the second iteration (AV = True), and the original
e7e4d230
AC
12528 -- node comes from source and we are not in an instance, then give
12529 -- a warning if we know result would be True or False. Note: we
12530 -- know Constant_Condition_Warnings is set if we get here.
c800f862
RD
12531
12532 elsif Comes_From_Source (Original_Node (N))
12533 and then not In_Instance
12534 then
12535 if True_Result then
ed2233dc 12536 Error_Msg_N
324ac540 12537 ("condition can only be False if invalid values present??",
c800f862
RD
12538 N);
12539 elsif False_Result then
ed2233dc 12540 Error_Msg_N
324ac540 12541 ("condition can only be True if invalid values present??",
c800f862
RD
12542 N);
12543 end if;
12544 end if;
12545 end;
12546
12547 -- Skip second iteration if not warning on constant conditions or
e7e4d230
AC
12548 -- if the first iteration already generated a warning of some kind or
12549 -- if we are in any case assuming all values are valid (so that the
12550 -- first iteration took care of the valid case).
c800f862
RD
12551
12552 exit when not Constant_Condition_Warnings;
12553 exit when Warning_Generated;
12554 exit when Assume_No_Invalid_Values;
12555 end loop;
70482933
RK
12556 end Rewrite_Comparison;
12557
fbf5a39b
AC
12558 ----------------------------
12559 -- Safe_In_Place_Array_Op --
12560 ----------------------------
12561
12562 function Safe_In_Place_Array_Op
2e071734
AC
12563 (Lhs : Node_Id;
12564 Op1 : Node_Id;
12565 Op2 : Node_Id) return Boolean
fbf5a39b
AC
12566 is
12567 Target : Entity_Id;
12568
12569 function Is_Safe_Operand (Op : Node_Id) return Boolean;
12570 -- Operand is safe if it cannot overlap part of the target of the
12571 -- operation. If the operand and the target are identical, the operand
12572 -- is safe. The operand can be empty in the case of negation.
12573
12574 function Is_Unaliased (N : Node_Id) return Boolean;
5e1c00fa 12575 -- Check that N is a stand-alone entity
fbf5a39b
AC
12576
12577 ------------------
12578 -- Is_Unaliased --
12579 ------------------
12580
12581 function Is_Unaliased (N : Node_Id) return Boolean is
12582 begin
12583 return
12584 Is_Entity_Name (N)
12585 and then No (Address_Clause (Entity (N)))
12586 and then No (Renamed_Object (Entity (N)));
12587 end Is_Unaliased;
12588
12589 ---------------------
12590 -- Is_Safe_Operand --
12591 ---------------------
12592
12593 function Is_Safe_Operand (Op : Node_Id) return Boolean is
12594 begin
12595 if No (Op) then
12596 return True;
12597
12598 elsif Is_Entity_Name (Op) then
12599 return Is_Unaliased (Op);
12600
303b4d58 12601 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
fbf5a39b
AC
12602 return Is_Unaliased (Prefix (Op));
12603
12604 elsif Nkind (Op) = N_Slice then
12605 return
12606 Is_Unaliased (Prefix (Op))
12607 and then Entity (Prefix (Op)) /= Target;
12608
12609 elsif Nkind (Op) = N_Op_Not then
12610 return Is_Safe_Operand (Right_Opnd (Op));
12611
12612 else
12613 return False;
12614 end if;
12615 end Is_Safe_Operand;
12616
b6b5cca8 12617 -- Start of processing for Safe_In_Place_Array_Op
fbf5a39b
AC
12618
12619 begin
685094bf
RD
12620 -- Skip this processing if the component size is different from system
12621 -- storage unit (since at least for NOT this would cause problems).
fbf5a39b 12622
eaa826f8 12623 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
fbf5a39b
AC
12624 return False;
12625
26bff3d9 12626 -- Cannot do in place stuff on VM_Target since cannot pass addresses
fbf5a39b 12627
26bff3d9 12628 elsif VM_Target /= No_VM then
fbf5a39b
AC
12629 return False;
12630
12631 -- Cannot do in place stuff if non-standard Boolean representation
12632
eaa826f8 12633 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
fbf5a39b
AC
12634 return False;
12635
12636 elsif not Is_Unaliased (Lhs) then
12637 return False;
e7e4d230 12638
fbf5a39b
AC
12639 else
12640 Target := Entity (Lhs);
e7e4d230 12641 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
fbf5a39b
AC
12642 end if;
12643 end Safe_In_Place_Array_Op;
12644
70482933
RK
12645 -----------------------
12646 -- Tagged_Membership --
12647 -----------------------
12648
685094bf
RD
12649 -- There are two different cases to consider depending on whether the right
12650 -- operand is a class-wide type or not. If not we just compare the actual
12651 -- tag of the left expr to the target type tag:
70482933
RK
12652 --
12653 -- Left_Expr.Tag = Right_Type'Tag;
12654 --
685094bf
RD
12655 -- If it is a class-wide type we use the RT function CW_Membership which is
12656 -- usually implemented by looking in the ancestor tables contained in the
12657 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
70482933 12658
0669bebe
GB
12659 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
12660 -- function IW_Membership which is usually implemented by looking in the
12661 -- table of abstract interface types plus the ancestor table contained in
12662 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
12663
82878151
AC
12664 procedure Tagged_Membership
12665 (N : Node_Id;
12666 SCIL_Node : out Node_Id;
12667 Result : out Node_Id)
12668 is
70482933
RK
12669 Left : constant Node_Id := Left_Opnd (N);
12670 Right : constant Node_Id := Right_Opnd (N);
12671 Loc : constant Source_Ptr := Sloc (N);
12672
38171f43 12673 Full_R_Typ : Entity_Id;
70482933 12674 Left_Type : Entity_Id;
82878151 12675 New_Node : Node_Id;
70482933
RK
12676 Right_Type : Entity_Id;
12677 Obj_Tag : Node_Id;
12678
12679 begin
82878151
AC
12680 SCIL_Node := Empty;
12681
852dba80
AC
12682 -- Handle entities from the limited view
12683
12684 Left_Type := Available_View (Etype (Left));
12685 Right_Type := Available_View (Etype (Right));
70482933 12686
6cce2156
GD
12687 -- In the case where the type is an access type, the test is applied
12688 -- using the designated types (needed in Ada 2012 for implicit anonymous
12689 -- access conversions, for AI05-0149).
12690
12691 if Is_Access_Type (Right_Type) then
12692 Left_Type := Designated_Type (Left_Type);
12693 Right_Type := Designated_Type (Right_Type);
12694 end if;
12695
70482933
RK
12696 if Is_Class_Wide_Type (Left_Type) then
12697 Left_Type := Root_Type (Left_Type);
12698 end if;
12699
38171f43
AC
12700 if Is_Class_Wide_Type (Right_Type) then
12701 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
12702 else
12703 Full_R_Typ := Underlying_Type (Right_Type);
12704 end if;
12705
70482933
RK
12706 Obj_Tag :=
12707 Make_Selected_Component (Loc,
12708 Prefix => Relocate_Node (Left),
a9d8907c
JM
12709 Selector_Name =>
12710 New_Reference_To (First_Tag_Component (Left_Type), Loc));
70482933
RK
12711
12712 if Is_Class_Wide_Type (Right_Type) then
758c442c 12713
0669bebe
GB
12714 -- No need to issue a run-time check if we statically know that the
12715 -- result of this membership test is always true. For example,
12716 -- considering the following declarations:
12717
12718 -- type Iface is interface;
12719 -- type T is tagged null record;
12720 -- type DT is new T and Iface with null record;
12721
12722 -- Obj1 : T;
12723 -- Obj2 : DT;
12724
12725 -- These membership tests are always true:
12726
12727 -- Obj1 in T'Class
12728 -- Obj2 in T'Class;
12729 -- Obj2 in Iface'Class;
12730
12731 -- We do not need to handle cases where the membership is illegal.
12732 -- For example:
12733
12734 -- Obj1 in DT'Class; -- Compile time error
12735 -- Obj1 in Iface'Class; -- Compile time error
12736
12737 if not Is_Class_Wide_Type (Left_Type)
4ac2477e
JM
12738 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
12739 Use_Full_View => True)
533369aa
AC
12740 or else (Is_Interface (Etype (Right_Type))
12741 and then Interface_Present_In_Ancestor
761f7dcb
AC
12742 (Typ => Left_Type,
12743 Iface => Etype (Right_Type))))
0669bebe 12744 then
82878151
AC
12745 Result := New_Reference_To (Standard_True, Loc);
12746 return;
0669bebe
GB
12747 end if;
12748
758c442c
GD
12749 -- Ada 2005 (AI-251): Class-wide applied to interfaces
12750
630d30e9
RD
12751 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
12752
0669bebe 12753 -- Support to: "Iface_CW_Typ in Typ'Class"
630d30e9
RD
12754
12755 or else Is_Interface (Left_Type)
12756 then
dfd99a80
TQ
12757 -- Issue error if IW_Membership operation not available in a
12758 -- configurable run time setting.
12759
12760 if not RTE_Available (RE_IW_Membership) then
b4592168
GD
12761 Error_Msg_CRT
12762 ("dynamic membership test on interface types", N);
82878151
AC
12763 Result := Empty;
12764 return;
dfd99a80
TQ
12765 end if;
12766
82878151 12767 Result :=
758c442c
GD
12768 Make_Function_Call (Loc,
12769 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
12770 Parameter_Associations => New_List (
12771 Make_Attribute_Reference (Loc,
12772 Prefix => Obj_Tag,
12773 Attribute_Name => Name_Address),
12774 New_Reference_To (
38171f43 12775 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
758c442c
GD
12776 Loc)));
12777
12778 -- Ada 95: Normal case
12779
12780 else
82878151
AC
12781 Build_CW_Membership (Loc,
12782 Obj_Tag_Node => Obj_Tag,
12783 Typ_Tag_Node =>
12784 New_Reference_To (
38171f43 12785 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
82878151
AC
12786 Related_Nod => N,
12787 New_Node => New_Node);
12788
12789 -- Generate the SCIL node for this class-wide membership test.
12790 -- Done here because the previous call to Build_CW_Membership
12791 -- relocates Obj_Tag.
12792
12793 if Generate_SCIL then
12794 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
12795 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
12796 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
12797 end if;
12798
12799 Result := New_Node;
758c442c
GD
12800 end if;
12801
0669bebe
GB
12802 -- Right_Type is not a class-wide type
12803
70482933 12804 else
0669bebe
GB
12805 -- No need to check the tag of the object if Right_Typ is abstract
12806
12807 if Is_Abstract_Type (Right_Type) then
82878151 12808 Result := New_Reference_To (Standard_False, Loc);
0669bebe
GB
12809
12810 else
82878151 12811 Result :=
0669bebe
GB
12812 Make_Op_Eq (Loc,
12813 Left_Opnd => Obj_Tag,
12814 Right_Opnd =>
12815 New_Reference_To
38171f43 12816 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
0669bebe 12817 end if;
70482933 12818 end if;
70482933
RK
12819 end Tagged_Membership;
12820
12821 ------------------------------
12822 -- Unary_Op_Validity_Checks --
12823 ------------------------------
12824
12825 procedure Unary_Op_Validity_Checks (N : Node_Id) is
12826 begin
12827 if Validity_Checks_On and Validity_Check_Operands then
12828 Ensure_Valid (Right_Opnd (N));
12829 end if;
12830 end Unary_Op_Validity_Checks;
12831
12832end Exp_Ch4;