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