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