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