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