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