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