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