[NDS32] Implement bswapsi2 and bswaphi2 patterns.
[official-gcc.git] / gcc / ada / exp_ch4.adb
blob65de38e9e8332948e351a71accfb378e29d7c680
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 4 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
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- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
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 --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Atag; use Exp_Atag;
34 with Exp_Ch2; use Exp_Ch2;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Fixd; use Exp_Fixd;
41 with Exp_Intr; use Exp_Intr;
42 with Exp_Pakd; use Exp_Pakd;
43 with Exp_Tss; use Exp_Tss;
44 with Exp_Util; use Exp_Util;
45 with Freeze; use Freeze;
46 with Inline; use Inline;
47 with Namet; use Namet;
48 with Nlists; use Nlists;
49 with Nmake; use Nmake;
50 with Opt; use Opt;
51 with Par_SCO; use Par_SCO;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
54 with Rtsfind; use Rtsfind;
55 with Sem; use Sem;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch3; use Sem_Ch3;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Res; use Sem_Res;
62 with Sem_Type; use Sem_Type;
63 with Sem_Util; use Sem_Util;
64 with Sem_Warn; use Sem_Warn;
65 with Sinfo; use Sinfo;
66 with Snames; use Snames;
67 with Stand; use Stand;
68 with SCIL_LL; use SCIL_LL;
69 with Targparm; use Targparm;
70 with Tbuild; use Tbuild;
71 with Ttypes; use Ttypes;
72 with Uintp; use Uintp;
73 with Urealp; use Urealp;
74 with Validsw; use Validsw;
76 package body Exp_Ch4 is
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Binary_Op_Validity_Checks (N : Node_Id);
83 pragma Inline (Binary_Op_Validity_Checks);
84 -- Performs validity checks for a binary operator
86 procedure Build_Boolean_Array_Proc_Call
87 (N : Node_Id;
88 Op1 : Node_Id;
89 Op2 : Node_Id);
90 -- If a boolean array assignment can be done in place, build call to
91 -- corresponding library procedure.
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.
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.
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
107 -- the actual comparison call that is made. Special processing (call a
108 -- run-time routine)
110 function Expand_Array_Equality
111 (Nod : Node_Id;
112 Lhs : Node_Id;
113 Rhs : Node_Id;
114 Bodies : List_Id;
115 Typ : Entity_Id) return Node_Id;
116 -- Expand an array equality into a call to a function implementing this
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.
127 procedure Expand_Boolean_Operator (N : Node_Id);
128 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
129 -- case of array type arguments.
131 procedure Expand_Nonbinary_Modular_Op (N : Node_Id);
132 -- When generating C code, convert nonbinary modular arithmetic operations
133 -- into code that relies on the front-end expansion of operator Mod. No
134 -- expansion is performed if N is not a nonbinary modular operand.
136 procedure Expand_Short_Circuit_Operator (N : Node_Id);
137 -- Common expansion processing for short-circuit boolean operators
139 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id);
140 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
141 -- where we allow comparison of "out of range" values.
143 function Expand_Composite_Equality
144 (Nod : Node_Id;
145 Typ : Entity_Id;
146 Lhs : Node_Id;
147 Rhs : Node_Id;
148 Bodies : List_Id) return Node_Id;
149 -- Local recursive function used to expand equality for nested composite
150 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
151 -- to attach bodies of local functions that are created in the process. It
152 -- is the responsibility of the caller to insert those bodies at the right
153 -- place. Nod provides the Sloc value for generated code. Lhs and Rhs are
154 -- the left and right sides for the comparison, and Typ is the type of the
155 -- objects to compare.
157 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
158 -- Routine to expand concatenation of a sequence of two or more operands
159 -- (in the list Operands) and replace node Cnode with the result of the
160 -- concatenation. The operands can be of any appropriate type, and can
161 -- include both arrays and singleton elements.
163 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id);
164 -- N is an N_In membership test mode, with the overflow check mode set to
165 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
166 -- integer type. This is a case where top level processing is required to
167 -- handle overflow checks in subtrees.
169 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
170 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
171 -- fixed. We do not have such a type at runtime, so the purpose of this
172 -- routine is to find the real type by looking up the tree. We also
173 -- determine if the operation must be rounded.
175 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
176 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
177 -- discriminants if it has a constrained nominal type, unless the object
178 -- is a component of an enclosing Unchecked_Union object that is subject
179 -- to a per-object constraint and the enclosing object lacks inferable
180 -- discriminants.
182 -- An expression of an Unchecked_Union type has inferable discriminants
183 -- if it is either a name of an object with inferable discriminants or a
184 -- qualified expression whose subtype mark denotes a constrained subtype.
186 procedure Insert_Dereference_Action (N : Node_Id);
187 -- N is an expression whose type is an access. When the type of the
188 -- associated storage pool is derived from Checked_Pool, generate a
189 -- call to the 'Dereference' primitive operation.
191 function Make_Array_Comparison_Op
192 (Typ : Entity_Id;
193 Nod : Node_Id) return Node_Id;
194 -- Comparisons between arrays are expanded in line. This function produces
195 -- the body of the implementation of (a > b), where a and b are one-
196 -- dimensional arrays of some discrete type. The original node is then
197 -- expanded into the appropriate call to this function. Nod provides the
198 -- Sloc value for the generated code.
200 function Make_Boolean_Array_Op
201 (Typ : Entity_Id;
202 N : Node_Id) return Node_Id;
203 -- Boolean operations on boolean arrays are expanded in line. This function
204 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
205 -- b). It is used only the normal case and not the packed case. The type
206 -- involved, Typ, is the Boolean array type, and the logical operations in
207 -- the body are simple boolean operations. Note that Typ is always a
208 -- constrained type (the caller has ensured this by using
209 -- Convert_To_Actual_Subtype if necessary).
211 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean;
212 -- For signed arithmetic operations when the current overflow mode is
213 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
214 -- as the first thing we do. We then return. We count on the recursive
215 -- apparatus for overflow checks to call us back with an equivalent
216 -- operation that is in CHECKED mode, avoiding a recursive entry into this
217 -- routine, and that is when we will proceed with the expansion of the
218 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
219 -- these optimizations without first making this check, since there may be
220 -- operands further down the tree that are relying on the recursive calls
221 -- triggered by the top level nodes to properly process overflow checking
222 -- and remaining expansion on these nodes. Note that this call back may be
223 -- skipped if the operation is done in Bignum mode but that's fine, since
224 -- the Bignum call takes care of everything.
226 procedure Optimize_Length_Comparison (N : Node_Id);
227 -- Given an expression, if it is of the form X'Length op N (or the other
228 -- way round), where N is known at compile time to be 0 or 1, and X is a
229 -- simple entity, and op is a comparison operator, optimizes it into a
230 -- comparison of First and Last.
232 procedure Process_If_Case_Statements (N : Node_Id; Stmts : List_Id);
233 -- Inspect and process statement list Stmt of if or case expression N for
234 -- transient objects. If such objects are found, the routine generates code
235 -- to clean them up when the context of the expression is evaluated.
237 procedure Process_Transient_In_Expression
238 (Obj_Decl : Node_Id;
239 Expr : Node_Id;
240 Stmts : List_Id);
241 -- Subsidiary routine to the expansion of expression_with_actions, if and
242 -- case expressions. Generate all necessary code to finalize a transient
243 -- object when the enclosing context is elaborated or evaluated. Obj_Decl
244 -- denotes the declaration of the transient object, which is usually the
245 -- result of a controlled function call. Expr denotes the expression with
246 -- actions, if expression, or case expression node. Stmts denotes the
247 -- statement list which contains Decl, either at the top level or within a
248 -- nested construct.
250 procedure Rewrite_Comparison (N : Node_Id);
251 -- If N is the node for a comparison whose outcome can be determined at
252 -- compile time, then the node N can be rewritten with True or False. If
253 -- the outcome cannot be determined at compile time, the call has no
254 -- effect. If N is a type conversion, then this processing is applied to
255 -- its expression. If N is neither comparison nor a type conversion, the
256 -- call has no effect.
258 procedure Tagged_Membership
259 (N : Node_Id;
260 SCIL_Node : out Node_Id;
261 Result : out Node_Id);
262 -- Construct the expression corresponding to the tagged membership test.
263 -- Deals with a second operand being (or not) a class-wide type.
265 function Safe_In_Place_Array_Op
266 (Lhs : Node_Id;
267 Op1 : Node_Id;
268 Op2 : Node_Id) return Boolean;
269 -- In the context of an assignment, where the right-hand side is a boolean
270 -- operation on arrays, check whether operation can be performed in place.
272 procedure Unary_Op_Validity_Checks (N : Node_Id);
273 pragma Inline (Unary_Op_Validity_Checks);
274 -- Performs validity checks for a unary operator
276 -------------------------------
277 -- Binary_Op_Validity_Checks --
278 -------------------------------
280 procedure Binary_Op_Validity_Checks (N : Node_Id) is
281 begin
282 if Validity_Checks_On and Validity_Check_Operands then
283 Ensure_Valid (Left_Opnd (N));
284 Ensure_Valid (Right_Opnd (N));
285 end if;
286 end Binary_Op_Validity_Checks;
288 ------------------------------------
289 -- Build_Boolean_Array_Proc_Call --
290 ------------------------------------
292 procedure Build_Boolean_Array_Proc_Call
293 (N : Node_Id;
294 Op1 : Node_Id;
295 Op2 : Node_Id)
297 Loc : constant Source_Ptr := Sloc (N);
298 Kind : constant Node_Kind := Nkind (Expression (N));
299 Target : constant Node_Id :=
300 Make_Attribute_Reference (Loc,
301 Prefix => Name (N),
302 Attribute_Name => Name_Address);
304 Arg1 : Node_Id := Op1;
305 Arg2 : Node_Id := Op2;
306 Call_Node : Node_Id;
307 Proc_Name : Entity_Id;
309 begin
310 if Kind = N_Op_Not then
311 if Nkind (Op1) in N_Binary_Op then
313 -- Use negated version of the binary operators
315 if Nkind (Op1) = N_Op_And then
316 Proc_Name := RTE (RE_Vector_Nand);
318 elsif Nkind (Op1) = N_Op_Or then
319 Proc_Name := RTE (RE_Vector_Nor);
321 else pragma Assert (Nkind (Op1) = N_Op_Xor);
322 Proc_Name := RTE (RE_Vector_Xor);
323 end if;
325 Call_Node :=
326 Make_Procedure_Call_Statement (Loc,
327 Name => New_Occurrence_Of (Proc_Name, Loc),
329 Parameter_Associations => New_List (
330 Target,
331 Make_Attribute_Reference (Loc,
332 Prefix => Left_Opnd (Op1),
333 Attribute_Name => Name_Address),
335 Make_Attribute_Reference (Loc,
336 Prefix => Right_Opnd (Op1),
337 Attribute_Name => Name_Address),
339 Make_Attribute_Reference (Loc,
340 Prefix => Left_Opnd (Op1),
341 Attribute_Name => Name_Length)));
343 else
344 Proc_Name := RTE (RE_Vector_Not);
346 Call_Node :=
347 Make_Procedure_Call_Statement (Loc,
348 Name => New_Occurrence_Of (Proc_Name, Loc),
349 Parameter_Associations => New_List (
350 Target,
352 Make_Attribute_Reference (Loc,
353 Prefix => Op1,
354 Attribute_Name => Name_Address),
356 Make_Attribute_Reference (Loc,
357 Prefix => Op1,
358 Attribute_Name => Name_Length)));
359 end if;
361 else
362 -- We use the following equivalences:
364 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
365 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
366 -- (not X) xor (not Y) = X xor Y
367 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
369 if Nkind (Op1) = N_Op_Not then
370 Arg1 := Right_Opnd (Op1);
371 Arg2 := Right_Opnd (Op2);
373 if Kind = N_Op_And then
374 Proc_Name := RTE (RE_Vector_Nor);
375 elsif Kind = N_Op_Or then
376 Proc_Name := RTE (RE_Vector_Nand);
377 else
378 Proc_Name := RTE (RE_Vector_Xor);
379 end if;
381 else
382 if Kind = N_Op_And then
383 Proc_Name := RTE (RE_Vector_And);
384 elsif Kind = N_Op_Or then
385 Proc_Name := RTE (RE_Vector_Or);
386 elsif Nkind (Op2) = N_Op_Not then
387 Proc_Name := RTE (RE_Vector_Nxor);
388 Arg2 := Right_Opnd (Op2);
389 else
390 Proc_Name := RTE (RE_Vector_Xor);
391 end if;
392 end if;
394 Call_Node :=
395 Make_Procedure_Call_Statement (Loc,
396 Name => New_Occurrence_Of (Proc_Name, Loc),
397 Parameter_Associations => New_List (
398 Target,
399 Make_Attribute_Reference (Loc,
400 Prefix => Arg1,
401 Attribute_Name => Name_Address),
402 Make_Attribute_Reference (Loc,
403 Prefix => Arg2,
404 Attribute_Name => Name_Address),
405 Make_Attribute_Reference (Loc,
406 Prefix => Arg1,
407 Attribute_Name => Name_Length)));
408 end if;
410 Rewrite (N, Call_Node);
411 Analyze (N);
413 exception
414 when RE_Not_Available =>
415 return;
416 end Build_Boolean_Array_Proc_Call;
418 --------------------------------
419 -- Displace_Allocator_Pointer --
420 --------------------------------
422 procedure Displace_Allocator_Pointer (N : Node_Id) is
423 Loc : constant Source_Ptr := Sloc (N);
424 Orig_Node : constant Node_Id := Original_Node (N);
425 Dtyp : Entity_Id;
426 Etyp : Entity_Id;
427 PtrT : Entity_Id;
429 begin
430 -- Do nothing in case of VM targets: the virtual machine will handle
431 -- interfaces directly.
433 if not Tagged_Type_Expansion then
434 return;
435 end if;
437 pragma Assert (Nkind (N) = N_Identifier
438 and then Nkind (Orig_Node) = N_Allocator);
440 PtrT := Etype (Orig_Node);
441 Dtyp := Available_View (Designated_Type (PtrT));
442 Etyp := Etype (Expression (Orig_Node));
444 if Is_Class_Wide_Type (Dtyp) and then Is_Interface (Dtyp) then
446 -- If the type of the allocator expression is not an interface type
447 -- we can generate code to reference the record component containing
448 -- the pointer to the secondary dispatch table.
450 if not Is_Interface (Etyp) then
451 declare
452 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
454 begin
455 -- 1) Get access to the allocated object
457 Rewrite (N,
458 Make_Explicit_Dereference (Loc, Relocate_Node (N)));
459 Set_Etype (N, Etyp);
460 Set_Analyzed (N);
462 -- 2) Add the conversion to displace the pointer to reference
463 -- the secondary dispatch table.
465 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
466 Analyze_And_Resolve (N, Dtyp);
468 -- 3) The 'access to the secondary dispatch table will be used
469 -- as the value returned by the allocator.
471 Rewrite (N,
472 Make_Attribute_Reference (Loc,
473 Prefix => Relocate_Node (N),
474 Attribute_Name => Name_Access));
475 Set_Etype (N, Saved_Typ);
476 Set_Analyzed (N);
477 end;
479 -- If the type of the allocator expression is an interface type we
480 -- generate a run-time call to displace "this" to reference the
481 -- component containing the pointer to the secondary dispatch table
482 -- or else raise Constraint_Error if the actual object does not
483 -- implement the target interface. This case corresponds to the
484 -- following example:
486 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
487 -- begin
488 -- return new Iface_2'Class'(Obj);
489 -- end Op;
491 else
492 Rewrite (N,
493 Unchecked_Convert_To (PtrT,
494 Make_Function_Call (Loc,
495 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
496 Parameter_Associations => New_List (
497 Unchecked_Convert_To (RTE (RE_Address),
498 Relocate_Node (N)),
500 New_Occurrence_Of
501 (Elists.Node
502 (First_Elmt
503 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
504 Loc)))));
505 Analyze_And_Resolve (N, PtrT);
506 end if;
507 end if;
508 end Displace_Allocator_Pointer;
510 ---------------------------------
511 -- Expand_Allocator_Expression --
512 ---------------------------------
514 procedure Expand_Allocator_Expression (N : Node_Id) is
515 Loc : constant Source_Ptr := Sloc (N);
516 Exp : constant Node_Id := Expression (Expression (N));
517 PtrT : constant Entity_Id := Etype (N);
518 DesigT : constant Entity_Id := Designated_Type (PtrT);
520 procedure Apply_Accessibility_Check
521 (Ref : Node_Id;
522 Built_In_Place : Boolean := False);
523 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
524 -- type, generate an accessibility check to verify that the level of the
525 -- type of the created object is not deeper than the level of the access
526 -- type. If the type of the qualified expression is class-wide, then
527 -- always generate the check (except in the case where it is known to be
528 -- unnecessary, see comment below). Otherwise, only generate the check
529 -- if the level of the qualified expression type is statically deeper
530 -- than the access type.
532 -- Although the static accessibility will generally have been performed
533 -- as a legality check, it won't have been done in cases where the
534 -- allocator appears in generic body, so a run-time check is needed in
535 -- general. One special case is when the access type is declared in the
536 -- same scope as the class-wide allocator, in which case the check can
537 -- never fail, so it need not be generated.
539 -- As an open issue, there seem to be cases where the static level
540 -- associated with the class-wide object's underlying type is not
541 -- sufficient to perform the proper accessibility check, such as for
542 -- allocators in nested subprograms or accept statements initialized by
543 -- class-wide formals when the actual originates outside at a deeper
544 -- static level. The nested subprogram case might require passing
545 -- accessibility levels along with class-wide parameters, and the task
546 -- case seems to be an actual gap in the language rules that needs to
547 -- be fixed by the ARG. ???
549 -------------------------------
550 -- Apply_Accessibility_Check --
551 -------------------------------
553 procedure Apply_Accessibility_Check
554 (Ref : Node_Id;
555 Built_In_Place : Boolean := False)
557 Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT);
558 Cond : Node_Id;
559 Fin_Call : Node_Id;
560 Free_Stmt : Node_Id;
561 Obj_Ref : Node_Id;
562 Stmts : List_Id;
564 begin
565 if Ada_Version >= Ada_2005
566 and then Is_Class_Wide_Type (DesigT)
567 and then Tagged_Type_Expansion
568 and then not Scope_Suppress.Suppress (Accessibility_Check)
569 and then
570 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
571 or else
572 (Is_Class_Wide_Type (Etype (Exp))
573 and then Scope (PtrT) /= Current_Scope))
574 then
575 -- If the allocator was built in place, Ref is already a reference
576 -- to the access object initialized to the result of the allocator
577 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
578 -- Remove_Side_Effects for cases where the build-in-place call may
579 -- still be the prefix of the reference (to avoid generating
580 -- duplicate calls). Otherwise, it is the entity associated with
581 -- the object containing the address of the allocated object.
583 if Built_In_Place then
584 Remove_Side_Effects (Ref);
585 Obj_Ref := New_Copy_Tree (Ref);
586 else
587 Obj_Ref := New_Occurrence_Of (Ref, Loc);
588 end if;
590 -- For access to interface types we must generate code to displace
591 -- the pointer to the base of the object since the subsequent code
592 -- references components located in the TSD of the object (which
593 -- is associated with the primary dispatch table --see a-tags.ads)
594 -- and also generates code invoking Free, which requires also a
595 -- reference to the base of the unallocated object.
597 if Is_Interface (DesigT) and then Tagged_Type_Expansion then
598 Obj_Ref :=
599 Unchecked_Convert_To (Etype (Obj_Ref),
600 Make_Function_Call (Loc,
601 Name =>
602 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
603 Parameter_Associations => New_List (
604 Unchecked_Convert_To (RTE (RE_Address),
605 New_Copy_Tree (Obj_Ref)))));
606 end if;
608 -- Step 1: Create the object clean up code
610 Stmts := New_List;
612 -- Deallocate the object if the accessibility check fails. This
613 -- is done only on targets or profiles that support deallocation.
615 -- Free (Obj_Ref);
617 if RTE_Available (RE_Free) then
618 Free_Stmt := Make_Free_Statement (Loc, New_Copy_Tree (Obj_Ref));
619 Set_Storage_Pool (Free_Stmt, Pool_Id);
621 Append_To (Stmts, Free_Stmt);
623 -- The target or profile cannot deallocate objects
625 else
626 Free_Stmt := Empty;
627 end if;
629 -- Finalize the object if applicable. Generate:
631 -- [Deep_]Finalize (Obj_Ref.all);
633 if Needs_Finalization (DesigT)
634 and then not No_Heap_Finalization (PtrT)
635 then
636 Fin_Call :=
637 Make_Final_Call
638 (Obj_Ref =>
639 Make_Explicit_Dereference (Loc, New_Copy (Obj_Ref)),
640 Typ => DesigT);
642 -- Guard against a missing [Deep_]Finalize when the designated
643 -- type was not properly frozen.
645 if No (Fin_Call) then
646 Fin_Call := Make_Null_Statement (Loc);
647 end if;
649 -- When the target or profile supports deallocation, wrap the
650 -- finalization call in a block to ensure proper deallocation
651 -- even if finalization fails. Generate:
653 -- begin
654 -- <Fin_Call>
655 -- exception
656 -- when others =>
657 -- <Free_Stmt>
658 -- raise;
659 -- end;
661 if Present (Free_Stmt) then
662 Fin_Call :=
663 Make_Block_Statement (Loc,
664 Handled_Statement_Sequence =>
665 Make_Handled_Sequence_Of_Statements (Loc,
666 Statements => New_List (Fin_Call),
668 Exception_Handlers => New_List (
669 Make_Exception_Handler (Loc,
670 Exception_Choices => New_List (
671 Make_Others_Choice (Loc)),
672 Statements => New_List (
673 New_Copy_Tree (Free_Stmt),
674 Make_Raise_Statement (Loc))))));
675 end if;
677 Prepend_To (Stmts, Fin_Call);
678 end if;
680 -- Signal the accessibility failure through a Program_Error
682 Append_To (Stmts,
683 Make_Raise_Program_Error (Loc,
684 Condition => New_Occurrence_Of (Standard_True, Loc),
685 Reason => PE_Accessibility_Check_Failed));
687 -- Step 2: Create the accessibility comparison
689 -- Generate:
690 -- Ref'Tag
692 Obj_Ref :=
693 Make_Attribute_Reference (Loc,
694 Prefix => Obj_Ref,
695 Attribute_Name => Name_Tag);
697 -- For tagged types, determine the accessibility level by looking
698 -- at the type specific data of the dispatch table. Generate:
700 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
702 if Tagged_Type_Expansion then
703 Cond := Build_Get_Access_Level (Loc, Obj_Ref);
705 -- Use a runtime call to determine the accessibility level when
706 -- compiling on virtual machine targets. Generate:
708 -- Get_Access_Level (Ref'Tag)
710 else
711 Cond :=
712 Make_Function_Call (Loc,
713 Name =>
714 New_Occurrence_Of (RTE (RE_Get_Access_Level), Loc),
715 Parameter_Associations => New_List (Obj_Ref));
716 end if;
718 Cond :=
719 Make_Op_Gt (Loc,
720 Left_Opnd => Cond,
721 Right_Opnd =>
722 Make_Integer_Literal (Loc, Type_Access_Level (PtrT)));
724 -- Due to the complexity and side effects of the check, utilize an
725 -- if statement instead of the regular Program_Error circuitry.
727 Insert_Action (N,
728 Make_Implicit_If_Statement (N,
729 Condition => Cond,
730 Then_Statements => Stmts));
731 end if;
732 end Apply_Accessibility_Check;
734 -- Local variables
736 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
737 Indic : constant Node_Id := Subtype_Mark (Expression (N));
738 T : constant Entity_Id := Entity (Indic);
739 Adj_Call : Node_Id;
740 Node : Node_Id;
741 Tag_Assign : Node_Id;
742 Temp : Entity_Id;
743 Temp_Decl : Node_Id;
745 TagT : Entity_Id := Empty;
746 -- Type used as source for tag assignment
748 TagR : Node_Id := Empty;
749 -- Target reference for tag assignment
751 -- Start of processing for Expand_Allocator_Expression
753 begin
754 -- Handle call to C++ constructor
756 if Is_CPP_Constructor_Call (Exp) then
757 Make_CPP_Constructor_Call_In_Allocator
758 (Allocator => N,
759 Function_Call => Exp);
760 return;
761 end if;
763 -- In the case of an Ada 2012 allocator whose initial value comes from a
764 -- function call, pass "the accessibility level determined by the point
765 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
766 -- Expand_Call but it couldn't be done there (because the Etype of the
767 -- allocator wasn't set then) so we generate the parameter here. See
768 -- the Boolean variable Defer in (a block within) Expand_Call.
770 if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then
771 declare
772 Subp : Entity_Id;
774 begin
775 if Nkind (Name (Exp)) = N_Explicit_Dereference then
776 Subp := Designated_Type (Etype (Prefix (Name (Exp))));
777 else
778 Subp := Entity (Name (Exp));
779 end if;
781 Subp := Ultimate_Alias (Subp);
783 if Present (Extra_Accessibility_Of_Result (Subp)) then
784 Add_Extra_Actual_To_Call
785 (Subprogram_Call => Exp,
786 Extra_Formal => Extra_Accessibility_Of_Result (Subp),
787 Extra_Actual => Dynamic_Accessibility_Level (PtrT));
788 end if;
789 end;
790 end if;
792 -- Case of tagged type or type requiring finalization
794 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
796 -- Ada 2005 (AI-318-02): If the initialization expression is a call
797 -- to a build-in-place function, then access to the allocated object
798 -- must be passed to the function.
800 if Is_Build_In_Place_Function_Call (Exp) then
801 Make_Build_In_Place_Call_In_Allocator (N, Exp);
802 Apply_Accessibility_Check (N, Built_In_Place => True);
803 return;
805 -- Ada 2005 (AI-318-02): Specialization of the previous case for
806 -- expressions containing a build-in-place function call whose
807 -- returned object covers interface types, and Expr has calls to
808 -- Ada.Tags.Displace to displace the pointer to the returned build-
809 -- in-place object to reference the secondary dispatch table of a
810 -- covered interface type.
812 elsif Present (Unqual_BIP_Iface_Function_Call (Exp)) then
813 Make_Build_In_Place_Iface_Call_In_Allocator (N, Exp);
814 Apply_Accessibility_Check (N, Built_In_Place => True);
815 return;
816 end if;
818 -- Actions inserted before:
819 -- Temp : constant ptr_T := new T'(Expression);
820 -- Temp._tag = T'tag; -- when not class-wide
821 -- [Deep_]Adjust (Temp.all);
823 -- We analyze by hand the new internal allocator to avoid any
824 -- recursion and inappropriate call to Initialize.
826 -- We don't want to remove side effects when the expression must be
827 -- built in place. In the case of a build-in-place function call,
828 -- that could lead to a duplication of the call, which was already
829 -- substituted for the allocator.
831 if not Aggr_In_Place then
832 Remove_Side_Effects (Exp);
833 end if;
835 Temp := Make_Temporary (Loc, 'P', N);
837 -- For a class wide allocation generate the following code:
839 -- type Equiv_Record is record ... end record;
840 -- implicit subtype CW is <Class_Wide_Subytpe>;
841 -- temp : PtrT := new CW'(CW!(expr));
843 if Is_Class_Wide_Type (T) then
844 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
846 -- Ada 2005 (AI-251): If the expression is a class-wide interface
847 -- object we generate code to move up "this" to reference the
848 -- base of the object before allocating the new object.
850 -- Note that Exp'Address is recursively expanded into a call
851 -- to Base_Address (Exp.Tag)
853 if Is_Class_Wide_Type (Etype (Exp))
854 and then Is_Interface (Etype (Exp))
855 and then Tagged_Type_Expansion
856 then
857 Set_Expression
858 (Expression (N),
859 Unchecked_Convert_To (Entity (Indic),
860 Make_Explicit_Dereference (Loc,
861 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
862 Make_Attribute_Reference (Loc,
863 Prefix => Exp,
864 Attribute_Name => Name_Address)))));
865 else
866 Set_Expression
867 (Expression (N),
868 Unchecked_Convert_To (Entity (Indic), Exp));
869 end if;
871 Analyze_And_Resolve (Expression (N), Entity (Indic));
872 end if;
874 -- Processing for allocators returning non-interface types
876 if not Is_Interface (Directly_Designated_Type (PtrT)) then
877 if Aggr_In_Place then
878 Temp_Decl :=
879 Make_Object_Declaration (Loc,
880 Defining_Identifier => Temp,
881 Object_Definition => New_Occurrence_Of (PtrT, Loc),
882 Expression =>
883 Make_Allocator (Loc,
884 Expression =>
885 New_Occurrence_Of (Etype (Exp), Loc)));
887 -- Copy the Comes_From_Source flag for the allocator we just
888 -- built, since logically this allocator is a replacement of
889 -- the original allocator node. This is for proper handling of
890 -- restriction No_Implicit_Heap_Allocations.
892 Set_Comes_From_Source
893 (Expression (Temp_Decl), Comes_From_Source (N));
895 Set_No_Initialization (Expression (Temp_Decl));
896 Insert_Action (N, Temp_Decl);
898 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
899 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
901 else
902 Node := Relocate_Node (N);
903 Set_Analyzed (Node);
905 Temp_Decl :=
906 Make_Object_Declaration (Loc,
907 Defining_Identifier => Temp,
908 Constant_Present => True,
909 Object_Definition => New_Occurrence_Of (PtrT, Loc),
910 Expression => Node);
912 Insert_Action (N, Temp_Decl);
913 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
914 end if;
916 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
917 -- interface type. In this case we use the type of the qualified
918 -- expression to allocate the object.
920 else
921 declare
922 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
923 New_Decl : Node_Id;
925 begin
926 New_Decl :=
927 Make_Full_Type_Declaration (Loc,
928 Defining_Identifier => Def_Id,
929 Type_Definition =>
930 Make_Access_To_Object_Definition (Loc,
931 All_Present => True,
932 Null_Exclusion_Present => False,
933 Constant_Present =>
934 Is_Access_Constant (Etype (N)),
935 Subtype_Indication =>
936 New_Occurrence_Of (Etype (Exp), Loc)));
938 Insert_Action (N, New_Decl);
940 -- Inherit the allocation-related attributes from the original
941 -- access type.
943 Set_Finalization_Master
944 (Def_Id, Finalization_Master (PtrT));
946 Set_Associated_Storage_Pool
947 (Def_Id, Associated_Storage_Pool (PtrT));
949 -- Declare the object using the previous type declaration
951 if Aggr_In_Place then
952 Temp_Decl :=
953 Make_Object_Declaration (Loc,
954 Defining_Identifier => Temp,
955 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
956 Expression =>
957 Make_Allocator (Loc,
958 New_Occurrence_Of (Etype (Exp), Loc)));
960 -- Copy the Comes_From_Source flag for the allocator we just
961 -- built, since logically this allocator is a replacement of
962 -- the original allocator node. This is for proper handling
963 -- of restriction No_Implicit_Heap_Allocations.
965 Set_Comes_From_Source
966 (Expression (Temp_Decl), Comes_From_Source (N));
968 Set_No_Initialization (Expression (Temp_Decl));
969 Insert_Action (N, Temp_Decl);
971 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
972 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
974 else
975 Node := Relocate_Node (N);
976 Set_Analyzed (Node);
978 Temp_Decl :=
979 Make_Object_Declaration (Loc,
980 Defining_Identifier => Temp,
981 Constant_Present => True,
982 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
983 Expression => Node);
985 Insert_Action (N, Temp_Decl);
986 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
987 end if;
989 -- Generate an additional object containing the address of the
990 -- returned object. The type of this second object declaration
991 -- is the correct type required for the common processing that
992 -- is still performed by this subprogram. The displacement of
993 -- this pointer to reference the component associated with the
994 -- interface type will be done at the end of common processing.
996 New_Decl :=
997 Make_Object_Declaration (Loc,
998 Defining_Identifier => Make_Temporary (Loc, 'P'),
999 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1000 Expression =>
1001 Unchecked_Convert_To (PtrT,
1002 New_Occurrence_Of (Temp, Loc)));
1004 Insert_Action (N, New_Decl);
1006 Temp_Decl := New_Decl;
1007 Temp := Defining_Identifier (New_Decl);
1008 end;
1009 end if;
1011 -- Generate the tag assignment
1013 -- Suppress the tag assignment for VM targets because VM tags are
1014 -- represented implicitly in objects.
1016 if not Tagged_Type_Expansion then
1017 null;
1019 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1020 -- interface objects because in this case the tag does not change.
1022 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1023 pragma Assert (Is_Class_Wide_Type
1024 (Directly_Designated_Type (Etype (N))));
1025 null;
1027 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1028 TagT := T;
1029 TagR := New_Occurrence_Of (Temp, Loc);
1031 elsif Is_Private_Type (T)
1032 and then Is_Tagged_Type (Underlying_Type (T))
1033 then
1034 TagT := Underlying_Type (T);
1035 TagR :=
1036 Unchecked_Convert_To (Underlying_Type (T),
1037 Make_Explicit_Dereference (Loc,
1038 Prefix => New_Occurrence_Of (Temp, Loc)));
1039 end if;
1041 if Present (TagT) then
1042 declare
1043 Full_T : constant Entity_Id := Underlying_Type (TagT);
1045 begin
1046 Tag_Assign :=
1047 Make_Assignment_Statement (Loc,
1048 Name =>
1049 Make_Selected_Component (Loc,
1050 Prefix => TagR,
1051 Selector_Name =>
1052 New_Occurrence_Of
1053 (First_Tag_Component (Full_T), Loc)),
1055 Expression =>
1056 Unchecked_Convert_To (RTE (RE_Tag),
1057 New_Occurrence_Of
1058 (Elists.Node
1059 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1060 end;
1062 -- The previous assignment has to be done in any case
1064 Set_Assignment_OK (Name (Tag_Assign));
1065 Insert_Action (N, Tag_Assign);
1066 end if;
1068 -- Generate an Adjust call if the object will be moved. In Ada 2005,
1069 -- the object may be inherently limited, in which case there is no
1070 -- Adjust procedure, and the object is built in place. In Ada 95, the
1071 -- object can be limited but not inherently limited if this allocator
1072 -- came from a return statement (we're allocating the result on the
1073 -- secondary stack). In that case, the object will be moved, so we do
1074 -- want to Adjust. However, if it's a nonlimited build-in-place
1075 -- function call, Adjust is not wanted.
1077 if Needs_Finalization (DesigT)
1078 and then Needs_Finalization (T)
1079 and then not Aggr_In_Place
1080 and then not Is_Limited_View (T)
1081 and then not Alloc_For_BIP_Return (N)
1082 and then not Is_Build_In_Place_Function_Call (Expression (N))
1083 then
1084 -- An unchecked conversion is needed in the classwide case because
1085 -- the designated type can be an ancestor of the subtype mark of
1086 -- the allocator.
1088 Adj_Call :=
1089 Make_Adjust_Call
1090 (Obj_Ref =>
1091 Unchecked_Convert_To (T,
1092 Make_Explicit_Dereference (Loc,
1093 Prefix => New_Occurrence_Of (Temp, Loc))),
1094 Typ => T);
1096 if Present (Adj_Call) then
1097 Insert_Action (N, Adj_Call);
1098 end if;
1099 end if;
1101 -- Note: the accessibility check must be inserted after the call to
1102 -- [Deep_]Adjust to ensure proper completion of the assignment.
1104 Apply_Accessibility_Check (Temp);
1106 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1107 Analyze_And_Resolve (N, PtrT);
1109 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1110 -- component containing the secondary dispatch table of the interface
1111 -- type.
1113 if Is_Interface (Directly_Designated_Type (PtrT)) then
1114 Displace_Allocator_Pointer (N);
1115 end if;
1117 -- Always force the generation of a temporary for aggregates when
1118 -- generating C code, to simplify the work in the code generator.
1120 elsif Aggr_In_Place
1121 or else (Modify_Tree_For_C and then Nkind (Exp) = N_Aggregate)
1122 then
1123 Temp := Make_Temporary (Loc, 'P', N);
1124 Temp_Decl :=
1125 Make_Object_Declaration (Loc,
1126 Defining_Identifier => Temp,
1127 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1128 Expression =>
1129 Make_Allocator (Loc,
1130 Expression => New_Occurrence_Of (Etype (Exp), Loc)));
1132 -- Copy the Comes_From_Source flag for the allocator we just built,
1133 -- since logically this allocator is a replacement of the original
1134 -- allocator node. This is for proper handling of restriction
1135 -- No_Implicit_Heap_Allocations.
1137 Set_Comes_From_Source
1138 (Expression (Temp_Decl), Comes_From_Source (N));
1140 Set_No_Initialization (Expression (Temp_Decl));
1141 Insert_Action (N, Temp_Decl);
1143 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1144 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1146 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1147 Analyze_And_Resolve (N, PtrT);
1149 elsif Is_Access_Type (T) and then Can_Never_Be_Null (T) then
1150 Install_Null_Excluding_Check (Exp);
1152 elsif Is_Access_Type (DesigT)
1153 and then Nkind (Exp) = N_Allocator
1154 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1155 then
1156 -- Apply constraint to designated subtype indication
1158 Apply_Constraint_Check
1159 (Expression (Exp), Designated_Type (DesigT), No_Sliding => True);
1161 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1163 -- Propagate constraint_error to enclosing allocator
1165 Rewrite (Exp, New_Copy (Expression (Exp)));
1166 end if;
1168 else
1169 Build_Allocate_Deallocate_Proc (N, True);
1171 -- If we have:
1172 -- type A is access T1;
1173 -- X : A := new T2'(...);
1174 -- T1 and T2 can be different subtypes, and we might need to check
1175 -- both constraints. First check against the type of the qualified
1176 -- expression.
1178 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1180 if Do_Range_Check (Exp) then
1181 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1182 end if;
1184 -- A check is also needed in cases where the designated subtype is
1185 -- constrained and differs from the subtype given in the qualified
1186 -- expression. Note that the check on the qualified expression does
1187 -- not allow sliding, but this check does (a relaxation from Ada 83).
1189 if Is_Constrained (DesigT)
1190 and then not Subtypes_Statically_Match (T, DesigT)
1191 then
1192 Apply_Constraint_Check
1193 (Exp, DesigT, No_Sliding => False);
1195 if Do_Range_Check (Exp) then
1196 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1197 end if;
1198 end if;
1200 -- For an access to unconstrained packed array, GIGI needs to see an
1201 -- expression with a constrained subtype in order to compute the
1202 -- proper size for the allocator.
1204 if Is_Array_Type (T)
1205 and then not Is_Constrained (T)
1206 and then Is_Packed (T)
1207 then
1208 declare
1209 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1210 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1211 begin
1212 Insert_Action (Exp,
1213 Make_Subtype_Declaration (Loc,
1214 Defining_Identifier => ConstrT,
1215 Subtype_Indication =>
1216 Make_Subtype_From_Expr (Internal_Exp, T)));
1217 Freeze_Itype (ConstrT, Exp);
1218 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1219 end;
1220 end if;
1222 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1223 -- to a build-in-place function, then access to the allocated object
1224 -- must be passed to the function.
1226 if Is_Build_In_Place_Function_Call (Exp) then
1227 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1228 end if;
1229 end if;
1231 exception
1232 when RE_Not_Available =>
1233 return;
1234 end Expand_Allocator_Expression;
1236 -----------------------------
1237 -- Expand_Array_Comparison --
1238 -----------------------------
1240 -- Expansion is only required in the case of array types. For the unpacked
1241 -- case, an appropriate runtime routine is called. For packed cases, and
1242 -- also in some other cases where a runtime routine cannot be called, the
1243 -- form of the expansion is:
1245 -- [body for greater_nn; boolean_expression]
1247 -- The body is built by Make_Array_Comparison_Op, and the form of the
1248 -- Boolean expression depends on the operator involved.
1250 procedure Expand_Array_Comparison (N : Node_Id) is
1251 Loc : constant Source_Ptr := Sloc (N);
1252 Op1 : Node_Id := Left_Opnd (N);
1253 Op2 : Node_Id := Right_Opnd (N);
1254 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1255 Ctyp : constant Entity_Id := Component_Type (Typ1);
1257 Expr : Node_Id;
1258 Func_Body : Node_Id;
1259 Func_Name : Entity_Id;
1261 Comp : RE_Id;
1263 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1264 -- True for byte addressable target
1266 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1267 -- Returns True if the length of the given operand is known to be less
1268 -- than 4. Returns False if this length is known to be four or greater
1269 -- or is not known at compile time.
1271 ------------------------
1272 -- Length_Less_Than_4 --
1273 ------------------------
1275 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1276 Otyp : constant Entity_Id := Etype (Opnd);
1278 begin
1279 if Ekind (Otyp) = E_String_Literal_Subtype then
1280 return String_Literal_Length (Otyp) < 4;
1282 else
1283 declare
1284 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1285 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1286 Hi : constant Node_Id := Type_High_Bound (Ityp);
1287 Lov : Uint;
1288 Hiv : Uint;
1290 begin
1291 if Compile_Time_Known_Value (Lo) then
1292 Lov := Expr_Value (Lo);
1293 else
1294 return False;
1295 end if;
1297 if Compile_Time_Known_Value (Hi) then
1298 Hiv := Expr_Value (Hi);
1299 else
1300 return False;
1301 end if;
1303 return Hiv < Lov + 3;
1304 end;
1305 end if;
1306 end Length_Less_Than_4;
1308 -- Start of processing for Expand_Array_Comparison
1310 begin
1311 -- Deal first with unpacked case, where we can call a runtime routine
1312 -- except that we avoid this for targets for which are not addressable
1313 -- by bytes.
1315 if not Is_Bit_Packed_Array (Typ1)
1316 and then Byte_Addressable
1317 then
1318 -- The call we generate is:
1320 -- Compare_Array_xn[_Unaligned]
1321 -- (left'address, right'address, left'length, right'length) <op> 0
1323 -- x = U for unsigned, S for signed
1324 -- n = 8,16,32,64 for component size
1325 -- Add _Unaligned if length < 4 and component size is 8.
1326 -- <op> is the standard comparison operator
1328 if Component_Size (Typ1) = 8 then
1329 if Length_Less_Than_4 (Op1)
1330 or else
1331 Length_Less_Than_4 (Op2)
1332 then
1333 if Is_Unsigned_Type (Ctyp) then
1334 Comp := RE_Compare_Array_U8_Unaligned;
1335 else
1336 Comp := RE_Compare_Array_S8_Unaligned;
1337 end if;
1339 else
1340 if Is_Unsigned_Type (Ctyp) then
1341 Comp := RE_Compare_Array_U8;
1342 else
1343 Comp := RE_Compare_Array_S8;
1344 end if;
1345 end if;
1347 elsif Component_Size (Typ1) = 16 then
1348 if Is_Unsigned_Type (Ctyp) then
1349 Comp := RE_Compare_Array_U16;
1350 else
1351 Comp := RE_Compare_Array_S16;
1352 end if;
1354 elsif Component_Size (Typ1) = 32 then
1355 if Is_Unsigned_Type (Ctyp) then
1356 Comp := RE_Compare_Array_U32;
1357 else
1358 Comp := RE_Compare_Array_S32;
1359 end if;
1361 else pragma Assert (Component_Size (Typ1) = 64);
1362 if Is_Unsigned_Type (Ctyp) then
1363 Comp := RE_Compare_Array_U64;
1364 else
1365 Comp := RE_Compare_Array_S64;
1366 end if;
1367 end if;
1369 if RTE_Available (Comp) then
1371 -- Expand to a call only if the runtime function is available,
1372 -- otherwise fall back to inline code.
1374 Remove_Side_Effects (Op1, Name_Req => True);
1375 Remove_Side_Effects (Op2, Name_Req => True);
1377 Rewrite (Op1,
1378 Make_Function_Call (Sloc (Op1),
1379 Name => New_Occurrence_Of (RTE (Comp), Loc),
1381 Parameter_Associations => New_List (
1382 Make_Attribute_Reference (Loc,
1383 Prefix => Relocate_Node (Op1),
1384 Attribute_Name => Name_Address),
1386 Make_Attribute_Reference (Loc,
1387 Prefix => Relocate_Node (Op2),
1388 Attribute_Name => Name_Address),
1390 Make_Attribute_Reference (Loc,
1391 Prefix => Relocate_Node (Op1),
1392 Attribute_Name => Name_Length),
1394 Make_Attribute_Reference (Loc,
1395 Prefix => Relocate_Node (Op2),
1396 Attribute_Name => Name_Length))));
1398 Rewrite (Op2,
1399 Make_Integer_Literal (Sloc (Op2),
1400 Intval => Uint_0));
1402 Analyze_And_Resolve (Op1, Standard_Integer);
1403 Analyze_And_Resolve (Op2, Standard_Integer);
1404 return;
1405 end if;
1406 end if;
1408 -- Cases where we cannot make runtime call
1410 -- For (a <= b) we convert to not (a > b)
1412 if Chars (N) = Name_Op_Le then
1413 Rewrite (N,
1414 Make_Op_Not (Loc,
1415 Right_Opnd =>
1416 Make_Op_Gt (Loc,
1417 Left_Opnd => Op1,
1418 Right_Opnd => Op2)));
1419 Analyze_And_Resolve (N, Standard_Boolean);
1420 return;
1422 -- For < the Boolean expression is
1423 -- greater__nn (op2, op1)
1425 elsif Chars (N) = Name_Op_Lt then
1426 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1428 -- Switch operands
1430 Op1 := Right_Opnd (N);
1431 Op2 := Left_Opnd (N);
1433 -- For (a >= b) we convert to not (a < b)
1435 elsif Chars (N) = Name_Op_Ge then
1436 Rewrite (N,
1437 Make_Op_Not (Loc,
1438 Right_Opnd =>
1439 Make_Op_Lt (Loc,
1440 Left_Opnd => Op1,
1441 Right_Opnd => Op2)));
1442 Analyze_And_Resolve (N, Standard_Boolean);
1443 return;
1445 -- For > the Boolean expression is
1446 -- greater__nn (op1, op2)
1448 else
1449 pragma Assert (Chars (N) = Name_Op_Gt);
1450 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1451 end if;
1453 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1454 Expr :=
1455 Make_Function_Call (Loc,
1456 Name => New_Occurrence_Of (Func_Name, Loc),
1457 Parameter_Associations => New_List (Op1, Op2));
1459 Insert_Action (N, Func_Body);
1460 Rewrite (N, Expr);
1461 Analyze_And_Resolve (N, Standard_Boolean);
1462 end Expand_Array_Comparison;
1464 ---------------------------
1465 -- Expand_Array_Equality --
1466 ---------------------------
1468 -- Expand an equality function for multi-dimensional arrays. Here is an
1469 -- example of such a function for Nb_Dimension = 2
1471 -- function Enn (A : atyp; B : btyp) return boolean is
1472 -- begin
1473 -- if (A'length (1) = 0 or else A'length (2) = 0)
1474 -- and then
1475 -- (B'length (1) = 0 or else B'length (2) = 0)
1476 -- then
1477 -- return True; -- RM 4.5.2(22)
1478 -- end if;
1480 -- if A'length (1) /= B'length (1)
1481 -- or else
1482 -- A'length (2) /= B'length (2)
1483 -- then
1484 -- return False; -- RM 4.5.2(23)
1485 -- end if;
1487 -- declare
1488 -- A1 : Index_T1 := A'first (1);
1489 -- B1 : Index_T1 := B'first (1);
1490 -- begin
1491 -- loop
1492 -- declare
1493 -- A2 : Index_T2 := A'first (2);
1494 -- B2 : Index_T2 := B'first (2);
1495 -- begin
1496 -- loop
1497 -- if A (A1, A2) /= B (B1, B2) then
1498 -- return False;
1499 -- end if;
1501 -- exit when A2 = A'last (2);
1502 -- A2 := Index_T2'succ (A2);
1503 -- B2 := Index_T2'succ (B2);
1504 -- end loop;
1505 -- end;
1507 -- exit when A1 = A'last (1);
1508 -- A1 := Index_T1'succ (A1);
1509 -- B1 := Index_T1'succ (B1);
1510 -- end loop;
1511 -- end;
1513 -- return true;
1514 -- end Enn;
1516 -- Note on the formal types used (atyp and btyp). If either of the arrays
1517 -- is of a private type, we use the underlying type, and do an unchecked
1518 -- conversion of the actual. If either of the arrays has a bound depending
1519 -- on a discriminant, then we use the base type since otherwise we have an
1520 -- escaped discriminant in the function.
1522 -- If both arrays are constrained and have the same bounds, we can generate
1523 -- a loop with an explicit iteration scheme using a 'Range attribute over
1524 -- the first array.
1526 function Expand_Array_Equality
1527 (Nod : Node_Id;
1528 Lhs : Node_Id;
1529 Rhs : Node_Id;
1530 Bodies : List_Id;
1531 Typ : Entity_Id) return Node_Id
1533 Loc : constant Source_Ptr := Sloc (Nod);
1534 Decls : constant List_Id := New_List;
1535 Index_List1 : constant List_Id := New_List;
1536 Index_List2 : constant List_Id := New_List;
1538 Actuals : List_Id;
1539 Formals : List_Id;
1540 Func_Name : Entity_Id;
1541 Func_Body : Node_Id;
1543 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1544 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1546 Ltyp : Entity_Id;
1547 Rtyp : Entity_Id;
1548 -- The parameter types to be used for the formals
1550 function Arr_Attr
1551 (Arr : Entity_Id;
1552 Nam : Name_Id;
1553 Num : Int) return Node_Id;
1554 -- This builds the attribute reference Arr'Nam (Expr)
1556 function Component_Equality (Typ : Entity_Id) return Node_Id;
1557 -- Create one statement to compare corresponding components, designated
1558 -- by a full set of indexes.
1560 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1561 -- Given one of the arguments, computes the appropriate type to be used
1562 -- for that argument in the corresponding function formal
1564 function Handle_One_Dimension
1565 (N : Int;
1566 Index : Node_Id) return Node_Id;
1567 -- This procedure returns the following code
1569 -- declare
1570 -- Bn : Index_T := B'First (N);
1571 -- begin
1572 -- loop
1573 -- xxx
1574 -- exit when An = A'Last (N);
1575 -- An := Index_T'Succ (An)
1576 -- Bn := Index_T'Succ (Bn)
1577 -- end loop;
1578 -- end;
1580 -- If both indexes are constrained and identical, the procedure
1581 -- returns a simpler loop:
1583 -- for An in A'Range (N) loop
1584 -- xxx
1585 -- end loop
1587 -- N is the dimension for which we are generating a loop. Index is the
1588 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1589 -- xxx statement is either the loop or declare for the next dimension
1590 -- or if this is the last dimension the comparison of corresponding
1591 -- components of the arrays.
1593 -- The actual way the code works is to return the comparison of
1594 -- corresponding components for the N+1 call. That's neater.
1596 function Test_Empty_Arrays return Node_Id;
1597 -- This function constructs the test for both arrays being empty
1598 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1599 -- and then
1600 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1602 function Test_Lengths_Correspond return Node_Id;
1603 -- This function constructs the test for arrays having different lengths
1604 -- in at least one index position, in which case the resulting code is:
1606 -- A'length (1) /= B'length (1)
1607 -- or else
1608 -- A'length (2) /= B'length (2)
1609 -- or else
1610 -- ...
1612 --------------
1613 -- Arr_Attr --
1614 --------------
1616 function Arr_Attr
1617 (Arr : Entity_Id;
1618 Nam : Name_Id;
1619 Num : Int) return Node_Id
1621 begin
1622 return
1623 Make_Attribute_Reference (Loc,
1624 Attribute_Name => Nam,
1625 Prefix => New_Occurrence_Of (Arr, Loc),
1626 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1627 end Arr_Attr;
1629 ------------------------
1630 -- Component_Equality --
1631 ------------------------
1633 function Component_Equality (Typ : Entity_Id) return Node_Id is
1634 Test : Node_Id;
1635 L, R : Node_Id;
1637 begin
1638 -- if a(i1...) /= b(j1...) then return false; end if;
1640 L :=
1641 Make_Indexed_Component (Loc,
1642 Prefix => Make_Identifier (Loc, Chars (A)),
1643 Expressions => Index_List1);
1645 R :=
1646 Make_Indexed_Component (Loc,
1647 Prefix => Make_Identifier (Loc, Chars (B)),
1648 Expressions => Index_List2);
1650 Test := Expand_Composite_Equality
1651 (Nod, Component_Type (Typ), L, R, Decls);
1653 -- If some (sub)component is an unchecked_union, the whole operation
1654 -- will raise program error.
1656 if Nkind (Test) = N_Raise_Program_Error then
1658 -- This node is going to be inserted at a location where a
1659 -- statement is expected: clear its Etype so analysis will set
1660 -- it to the expected Standard_Void_Type.
1662 Set_Etype (Test, Empty);
1663 return Test;
1665 else
1666 return
1667 Make_Implicit_If_Statement (Nod,
1668 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1669 Then_Statements => New_List (
1670 Make_Simple_Return_Statement (Loc,
1671 Expression => New_Occurrence_Of (Standard_False, Loc))));
1672 end if;
1673 end Component_Equality;
1675 ------------------
1676 -- Get_Arg_Type --
1677 ------------------
1679 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1680 T : Entity_Id;
1681 X : Node_Id;
1683 begin
1684 T := Etype (N);
1686 if No (T) then
1687 return Typ;
1689 else
1690 T := Underlying_Type (T);
1692 X := First_Index (T);
1693 while Present (X) loop
1694 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1695 or else
1696 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1697 then
1698 T := Base_Type (T);
1699 exit;
1700 end if;
1702 Next_Index (X);
1703 end loop;
1705 return T;
1706 end if;
1707 end Get_Arg_Type;
1709 --------------------------
1710 -- Handle_One_Dimension --
1711 ---------------------------
1713 function Handle_One_Dimension
1714 (N : Int;
1715 Index : Node_Id) return Node_Id
1717 Need_Separate_Indexes : constant Boolean :=
1718 Ltyp /= Rtyp or else not Is_Constrained (Ltyp);
1719 -- If the index types are identical, and we are working with
1720 -- constrained types, then we can use the same index for both
1721 -- of the arrays.
1723 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1725 Bn : Entity_Id;
1726 Index_T : Entity_Id;
1727 Stm_List : List_Id;
1728 Loop_Stm : Node_Id;
1730 begin
1731 if N > Number_Dimensions (Ltyp) then
1732 return Component_Equality (Ltyp);
1733 end if;
1735 -- Case where we generate a loop
1737 Index_T := Base_Type (Etype (Index));
1739 if Need_Separate_Indexes then
1740 Bn := Make_Temporary (Loc, 'B');
1741 else
1742 Bn := An;
1743 end if;
1745 Append (New_Occurrence_Of (An, Loc), Index_List1);
1746 Append (New_Occurrence_Of (Bn, Loc), Index_List2);
1748 Stm_List := New_List (
1749 Handle_One_Dimension (N + 1, Next_Index (Index)));
1751 if Need_Separate_Indexes then
1753 -- Generate guard for loop, followed by increments of indexes
1755 Append_To (Stm_List,
1756 Make_Exit_Statement (Loc,
1757 Condition =>
1758 Make_Op_Eq (Loc,
1759 Left_Opnd => New_Occurrence_Of (An, Loc),
1760 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1762 Append_To (Stm_List,
1763 Make_Assignment_Statement (Loc,
1764 Name => New_Occurrence_Of (An, Loc),
1765 Expression =>
1766 Make_Attribute_Reference (Loc,
1767 Prefix => New_Occurrence_Of (Index_T, Loc),
1768 Attribute_Name => Name_Succ,
1769 Expressions => New_List (
1770 New_Occurrence_Of (An, Loc)))));
1772 Append_To (Stm_List,
1773 Make_Assignment_Statement (Loc,
1774 Name => New_Occurrence_Of (Bn, Loc),
1775 Expression =>
1776 Make_Attribute_Reference (Loc,
1777 Prefix => New_Occurrence_Of (Index_T, Loc),
1778 Attribute_Name => Name_Succ,
1779 Expressions => New_List (
1780 New_Occurrence_Of (Bn, Loc)))));
1781 end if;
1783 -- If separate indexes, we need a declare block for An and Bn, and a
1784 -- loop without an iteration scheme.
1786 if Need_Separate_Indexes then
1787 Loop_Stm :=
1788 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1790 return
1791 Make_Block_Statement (Loc,
1792 Declarations => New_List (
1793 Make_Object_Declaration (Loc,
1794 Defining_Identifier => An,
1795 Object_Definition => New_Occurrence_Of (Index_T, Loc),
1796 Expression => Arr_Attr (A, Name_First, N)),
1798 Make_Object_Declaration (Loc,
1799 Defining_Identifier => Bn,
1800 Object_Definition => New_Occurrence_Of (Index_T, Loc),
1801 Expression => Arr_Attr (B, Name_First, N))),
1803 Handled_Statement_Sequence =>
1804 Make_Handled_Sequence_Of_Statements (Loc,
1805 Statements => New_List (Loop_Stm)));
1807 -- If no separate indexes, return loop statement with explicit
1808 -- iteration scheme on its own
1810 else
1811 Loop_Stm :=
1812 Make_Implicit_Loop_Statement (Nod,
1813 Statements => Stm_List,
1814 Iteration_Scheme =>
1815 Make_Iteration_Scheme (Loc,
1816 Loop_Parameter_Specification =>
1817 Make_Loop_Parameter_Specification (Loc,
1818 Defining_Identifier => An,
1819 Discrete_Subtype_Definition =>
1820 Arr_Attr (A, Name_Range, N))));
1821 return Loop_Stm;
1822 end if;
1823 end Handle_One_Dimension;
1825 -----------------------
1826 -- Test_Empty_Arrays --
1827 -----------------------
1829 function Test_Empty_Arrays return Node_Id is
1830 Alist : Node_Id;
1831 Blist : Node_Id;
1833 Atest : Node_Id;
1834 Btest : Node_Id;
1836 begin
1837 Alist := Empty;
1838 Blist := Empty;
1839 for J in 1 .. Number_Dimensions (Ltyp) loop
1840 Atest :=
1841 Make_Op_Eq (Loc,
1842 Left_Opnd => Arr_Attr (A, Name_Length, J),
1843 Right_Opnd => Make_Integer_Literal (Loc, 0));
1845 Btest :=
1846 Make_Op_Eq (Loc,
1847 Left_Opnd => Arr_Attr (B, Name_Length, J),
1848 Right_Opnd => Make_Integer_Literal (Loc, 0));
1850 if No (Alist) then
1851 Alist := Atest;
1852 Blist := Btest;
1854 else
1855 Alist :=
1856 Make_Or_Else (Loc,
1857 Left_Opnd => Relocate_Node (Alist),
1858 Right_Opnd => Atest);
1860 Blist :=
1861 Make_Or_Else (Loc,
1862 Left_Opnd => Relocate_Node (Blist),
1863 Right_Opnd => Btest);
1864 end if;
1865 end loop;
1867 return
1868 Make_And_Then (Loc,
1869 Left_Opnd => Alist,
1870 Right_Opnd => Blist);
1871 end Test_Empty_Arrays;
1873 -----------------------------
1874 -- Test_Lengths_Correspond --
1875 -----------------------------
1877 function Test_Lengths_Correspond return Node_Id is
1878 Result : Node_Id;
1879 Rtest : Node_Id;
1881 begin
1882 Result := Empty;
1883 for J in 1 .. Number_Dimensions (Ltyp) loop
1884 Rtest :=
1885 Make_Op_Ne (Loc,
1886 Left_Opnd => Arr_Attr (A, Name_Length, J),
1887 Right_Opnd => Arr_Attr (B, Name_Length, J));
1889 if No (Result) then
1890 Result := Rtest;
1891 else
1892 Result :=
1893 Make_Or_Else (Loc,
1894 Left_Opnd => Relocate_Node (Result),
1895 Right_Opnd => Rtest);
1896 end if;
1897 end loop;
1899 return Result;
1900 end Test_Lengths_Correspond;
1902 -- Start of processing for Expand_Array_Equality
1904 begin
1905 Ltyp := Get_Arg_Type (Lhs);
1906 Rtyp := Get_Arg_Type (Rhs);
1908 -- For now, if the argument types are not the same, go to the base type,
1909 -- since the code assumes that the formals have the same type. This is
1910 -- fixable in future ???
1912 if Ltyp /= Rtyp then
1913 Ltyp := Base_Type (Ltyp);
1914 Rtyp := Base_Type (Rtyp);
1915 pragma Assert (Ltyp = Rtyp);
1916 end if;
1918 -- Build list of formals for function
1920 Formals := New_List (
1921 Make_Parameter_Specification (Loc,
1922 Defining_Identifier => A,
1923 Parameter_Type => New_Occurrence_Of (Ltyp, Loc)),
1925 Make_Parameter_Specification (Loc,
1926 Defining_Identifier => B,
1927 Parameter_Type => New_Occurrence_Of (Rtyp, Loc)));
1929 Func_Name := Make_Temporary (Loc, 'E');
1931 -- Build statement sequence for function
1933 Func_Body :=
1934 Make_Subprogram_Body (Loc,
1935 Specification =>
1936 Make_Function_Specification (Loc,
1937 Defining_Unit_Name => Func_Name,
1938 Parameter_Specifications => Formals,
1939 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
1941 Declarations => Decls,
1943 Handled_Statement_Sequence =>
1944 Make_Handled_Sequence_Of_Statements (Loc,
1945 Statements => New_List (
1947 Make_Implicit_If_Statement (Nod,
1948 Condition => Test_Empty_Arrays,
1949 Then_Statements => New_List (
1950 Make_Simple_Return_Statement (Loc,
1951 Expression =>
1952 New_Occurrence_Of (Standard_True, Loc)))),
1954 Make_Implicit_If_Statement (Nod,
1955 Condition => Test_Lengths_Correspond,
1956 Then_Statements => New_List (
1957 Make_Simple_Return_Statement (Loc,
1958 Expression => New_Occurrence_Of (Standard_False, Loc)))),
1960 Handle_One_Dimension (1, First_Index (Ltyp)),
1962 Make_Simple_Return_Statement (Loc,
1963 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1965 Set_Has_Completion (Func_Name, True);
1966 Set_Is_Inlined (Func_Name);
1968 -- If the array type is distinct from the type of the arguments, it
1969 -- is the full view of a private type. Apply an unchecked conversion
1970 -- to insure that analysis of the call succeeds.
1972 declare
1973 L, R : Node_Id;
1975 begin
1976 L := Lhs;
1977 R := Rhs;
1979 if No (Etype (Lhs))
1980 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1981 then
1982 L := OK_Convert_To (Ltyp, Lhs);
1983 end if;
1985 if No (Etype (Rhs))
1986 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1987 then
1988 R := OK_Convert_To (Rtyp, Rhs);
1989 end if;
1991 Actuals := New_List (L, R);
1992 end;
1994 Append_To (Bodies, Func_Body);
1996 return
1997 Make_Function_Call (Loc,
1998 Name => New_Occurrence_Of (Func_Name, Loc),
1999 Parameter_Associations => Actuals);
2000 end Expand_Array_Equality;
2002 -----------------------------
2003 -- Expand_Boolean_Operator --
2004 -----------------------------
2006 -- Note that we first get the actual subtypes of the operands, since we
2007 -- always want to deal with types that have bounds.
2009 procedure Expand_Boolean_Operator (N : Node_Id) is
2010 Typ : constant Entity_Id := Etype (N);
2012 begin
2013 -- Special case of bit packed array where both operands are known to be
2014 -- properly aligned. In this case we use an efficient run time routine
2015 -- to carry out the operation (see System.Bit_Ops).
2017 if Is_Bit_Packed_Array (Typ)
2018 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2019 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2020 then
2021 Expand_Packed_Boolean_Operator (N);
2022 return;
2023 end if;
2025 -- For the normal non-packed case, the general expansion is to build
2026 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2027 -- and then inserting it into the tree. The original operator node is
2028 -- then rewritten as a call to this function. We also use this in the
2029 -- packed case if either operand is a possibly unaligned object.
2031 declare
2032 Loc : constant Source_Ptr := Sloc (N);
2033 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2034 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2035 Func_Body : Node_Id;
2036 Func_Name : Entity_Id;
2038 begin
2039 Convert_To_Actual_Subtype (L);
2040 Convert_To_Actual_Subtype (R);
2041 Ensure_Defined (Etype (L), N);
2042 Ensure_Defined (Etype (R), N);
2043 Apply_Length_Check (R, Etype (L));
2045 if Nkind (N) = N_Op_Xor then
2046 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2047 end if;
2049 if Nkind (Parent (N)) = N_Assignment_Statement
2050 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2051 then
2052 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
2054 elsif Nkind (Parent (N)) = N_Op_Not
2055 and then Nkind (N) = N_Op_And
2056 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2057 and then Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
2058 then
2059 return;
2060 else
2062 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2063 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2064 Insert_Action (N, Func_Body);
2066 -- Now rewrite the expression with a call
2068 Rewrite (N,
2069 Make_Function_Call (Loc,
2070 Name => New_Occurrence_Of (Func_Name, Loc),
2071 Parameter_Associations =>
2072 New_List (
2074 Make_Type_Conversion
2075 (Loc, New_Occurrence_Of (Etype (L), Loc), R))));
2077 Analyze_And_Resolve (N, Typ);
2078 end if;
2079 end;
2080 end Expand_Boolean_Operator;
2082 ------------------------------------------------
2083 -- Expand_Compare_Minimize_Eliminate_Overflow --
2084 ------------------------------------------------
2086 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is
2087 Loc : constant Source_Ptr := Sloc (N);
2089 Result_Type : constant Entity_Id := Etype (N);
2090 -- Capture result type (could be a derived boolean type)
2092 Llo, Lhi : Uint;
2093 Rlo, Rhi : Uint;
2095 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
2096 -- Entity for Long_Long_Integer'Base
2098 Check : constant Overflow_Mode_Type := Overflow_Check_Mode;
2099 -- Current overflow checking mode
2101 procedure Set_True;
2102 procedure Set_False;
2103 -- These procedures rewrite N with an occurrence of Standard_True or
2104 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2106 ---------------
2107 -- Set_False --
2108 ---------------
2110 procedure Set_False is
2111 begin
2112 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2113 Warn_On_Known_Condition (N);
2114 end Set_False;
2116 --------------
2117 -- Set_True --
2118 --------------
2120 procedure Set_True is
2121 begin
2122 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2123 Warn_On_Known_Condition (N);
2124 end Set_True;
2126 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2128 begin
2129 -- Nothing to do unless we have a comparison operator with operands
2130 -- that are signed integer types, and we are operating in either
2131 -- MINIMIZED or ELIMINATED overflow checking mode.
2133 if Nkind (N) not in N_Op_Compare
2134 or else Check not in Minimized_Or_Eliminated
2135 or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N)))
2136 then
2137 return;
2138 end if;
2140 -- OK, this is the case we are interested in. First step is to process
2141 -- our operands using the Minimize_Eliminate circuitry which applies
2142 -- this processing to the two operand subtrees.
2144 Minimize_Eliminate_Overflows
2145 (Left_Opnd (N), Llo, Lhi, Top_Level => False);
2146 Minimize_Eliminate_Overflows
2147 (Right_Opnd (N), Rlo, Rhi, Top_Level => False);
2149 -- See if the range information decides the result of the comparison.
2150 -- We can only do this if we in fact have full range information (which
2151 -- won't be the case if either operand is bignum at this stage).
2153 if Llo /= No_Uint and then Rlo /= No_Uint then
2154 case N_Op_Compare (Nkind (N)) is
2155 when N_Op_Eq =>
2156 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2157 Set_True;
2158 elsif Llo > Rhi or else Lhi < Rlo then
2159 Set_False;
2160 end if;
2162 when N_Op_Ge =>
2163 if Llo >= Rhi then
2164 Set_True;
2165 elsif Lhi < Rlo then
2166 Set_False;
2167 end if;
2169 when N_Op_Gt =>
2170 if Llo > Rhi then
2171 Set_True;
2172 elsif Lhi <= Rlo then
2173 Set_False;
2174 end if;
2176 when N_Op_Le =>
2177 if Llo > Rhi then
2178 Set_False;
2179 elsif Lhi <= Rlo then
2180 Set_True;
2181 end if;
2183 when N_Op_Lt =>
2184 if Llo >= Rhi then
2185 Set_False;
2186 elsif Lhi < Rlo then
2187 Set_True;
2188 end if;
2190 when N_Op_Ne =>
2191 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2192 Set_False;
2193 elsif Llo > Rhi or else Lhi < Rlo then
2194 Set_True;
2195 end if;
2196 end case;
2198 -- All done if we did the rewrite
2200 if Nkind (N) not in N_Op_Compare then
2201 return;
2202 end if;
2203 end if;
2205 -- Otherwise, time to do the comparison
2207 declare
2208 Ltype : constant Entity_Id := Etype (Left_Opnd (N));
2209 Rtype : constant Entity_Id := Etype (Right_Opnd (N));
2211 begin
2212 -- If the two operands have the same signed integer type we are
2213 -- all set, nothing more to do. This is the case where either
2214 -- both operands were unchanged, or we rewrote both of them to
2215 -- be Long_Long_Integer.
2217 -- Note: Entity for the comparison may be wrong, but it's not worth
2218 -- the effort to change it, since the back end does not use it.
2220 if Is_Signed_Integer_Type (Ltype)
2221 and then Base_Type (Ltype) = Base_Type (Rtype)
2222 then
2223 return;
2225 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2227 elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then
2228 declare
2229 Left : Node_Id := Left_Opnd (N);
2230 Right : Node_Id := Right_Opnd (N);
2231 -- Bignum references for left and right operands
2233 begin
2234 if not Is_RTE (Ltype, RE_Bignum) then
2235 Left := Convert_To_Bignum (Left);
2236 elsif not Is_RTE (Rtype, RE_Bignum) then
2237 Right := Convert_To_Bignum (Right);
2238 end if;
2240 -- We rewrite our node with:
2242 -- do
2243 -- Bnn : Result_Type;
2244 -- declare
2245 -- M : Mark_Id := SS_Mark;
2246 -- begin
2247 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2248 -- SS_Release (M);
2249 -- end;
2250 -- in
2251 -- Bnn
2252 -- end
2254 declare
2255 Blk : constant Node_Id := Make_Bignum_Block (Loc);
2256 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
2257 Ent : RE_Id;
2259 begin
2260 case N_Op_Compare (Nkind (N)) is
2261 when N_Op_Eq => Ent := RE_Big_EQ;
2262 when N_Op_Ge => Ent := RE_Big_GE;
2263 when N_Op_Gt => Ent := RE_Big_GT;
2264 when N_Op_Le => Ent := RE_Big_LE;
2265 when N_Op_Lt => Ent := RE_Big_LT;
2266 when N_Op_Ne => Ent := RE_Big_NE;
2267 end case;
2269 -- Insert assignment to Bnn into the bignum block
2271 Insert_Before
2272 (First (Statements (Handled_Statement_Sequence (Blk))),
2273 Make_Assignment_Statement (Loc,
2274 Name => New_Occurrence_Of (Bnn, Loc),
2275 Expression =>
2276 Make_Function_Call (Loc,
2277 Name =>
2278 New_Occurrence_Of (RTE (Ent), Loc),
2279 Parameter_Associations => New_List (Left, Right))));
2281 -- Now do the rewrite with expression actions
2283 Rewrite (N,
2284 Make_Expression_With_Actions (Loc,
2285 Actions => New_List (
2286 Make_Object_Declaration (Loc,
2287 Defining_Identifier => Bnn,
2288 Object_Definition =>
2289 New_Occurrence_Of (Result_Type, Loc)),
2290 Blk),
2291 Expression => New_Occurrence_Of (Bnn, Loc)));
2292 Analyze_And_Resolve (N, Result_Type);
2293 end;
2294 end;
2296 -- No bignums involved, but types are different, so we must have
2297 -- rewritten one of the operands as a Long_Long_Integer but not
2298 -- the other one.
2300 -- If left operand is Long_Long_Integer, convert right operand
2301 -- and we are done (with a comparison of two Long_Long_Integers).
2303 elsif Ltype = LLIB then
2304 Convert_To_And_Rewrite (LLIB, Right_Opnd (N));
2305 Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks);
2306 return;
2308 -- If right operand is Long_Long_Integer, convert left operand
2309 -- and we are done (with a comparison of two Long_Long_Integers).
2311 -- This is the only remaining possibility
2313 else pragma Assert (Rtype = LLIB);
2314 Convert_To_And_Rewrite (LLIB, Left_Opnd (N));
2315 Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks);
2316 return;
2317 end if;
2318 end;
2319 end Expand_Compare_Minimize_Eliminate_Overflow;
2321 -------------------------------
2322 -- Expand_Composite_Equality --
2323 -------------------------------
2325 -- This function is only called for comparing internal fields of composite
2326 -- types when these fields are themselves composites. This is a special
2327 -- case because it is not possible to respect normal Ada visibility rules.
2329 function Expand_Composite_Equality
2330 (Nod : Node_Id;
2331 Typ : Entity_Id;
2332 Lhs : Node_Id;
2333 Rhs : Node_Id;
2334 Bodies : List_Id) return Node_Id
2336 Loc : constant Source_Ptr := Sloc (Nod);
2337 Full_Type : Entity_Id;
2338 Prim : Elmt_Id;
2339 Eq_Op : Entity_Id;
2341 function Find_Primitive_Eq return Node_Id;
2342 -- AI05-0123: Locate primitive equality for type if it exists, and
2343 -- build the corresponding call. If operation is abstract, replace
2344 -- call with an explicit raise. Return Empty if there is no primitive.
2346 -----------------------
2347 -- Find_Primitive_Eq --
2348 -----------------------
2350 function Find_Primitive_Eq return Node_Id is
2351 Prim_E : Elmt_Id;
2352 Prim : Node_Id;
2354 begin
2355 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2356 while Present (Prim_E) loop
2357 Prim := Node (Prim_E);
2359 -- Locate primitive equality with the right signature
2361 if Chars (Prim) = Name_Op_Eq
2362 and then Etype (First_Formal (Prim)) =
2363 Etype (Next_Formal (First_Formal (Prim)))
2364 and then Etype (Prim) = Standard_Boolean
2365 then
2366 if Is_Abstract_Subprogram (Prim) then
2367 return
2368 Make_Raise_Program_Error (Loc,
2369 Reason => PE_Explicit_Raise);
2371 else
2372 return
2373 Make_Function_Call (Loc,
2374 Name => New_Occurrence_Of (Prim, Loc),
2375 Parameter_Associations => New_List (Lhs, Rhs));
2376 end if;
2377 end if;
2379 Next_Elmt (Prim_E);
2380 end loop;
2382 -- If not found, predefined operation will be used
2384 return Empty;
2385 end Find_Primitive_Eq;
2387 -- Start of processing for Expand_Composite_Equality
2389 begin
2390 if Is_Private_Type (Typ) then
2391 Full_Type := Underlying_Type (Typ);
2392 else
2393 Full_Type := Typ;
2394 end if;
2396 -- If the private type has no completion the context may be the
2397 -- expansion of a composite equality for a composite type with some
2398 -- still incomplete components. The expression will not be analyzed
2399 -- until the enclosing type is completed, at which point this will be
2400 -- properly expanded, unless there is a bona fide completion error.
2402 if No (Full_Type) then
2403 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2404 end if;
2406 Full_Type := Base_Type (Full_Type);
2408 -- When the base type itself is private, use the full view to expand
2409 -- the composite equality.
2411 if Is_Private_Type (Full_Type) then
2412 Full_Type := Underlying_Type (Full_Type);
2413 end if;
2415 -- Case of array types
2417 if Is_Array_Type (Full_Type) then
2419 -- If the operand is an elementary type other than a floating-point
2420 -- type, then we can simply use the built-in block bitwise equality,
2421 -- since the predefined equality operators always apply and bitwise
2422 -- equality is fine for all these cases.
2424 if Is_Elementary_Type (Component_Type (Full_Type))
2425 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2426 then
2427 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2429 -- For composite component types, and floating-point types, use the
2430 -- expansion. This deals with tagged component types (where we use
2431 -- the applicable equality routine) and floating-point, (where we
2432 -- need to worry about negative zeroes), and also the case of any
2433 -- composite type recursively containing such fields.
2435 else
2436 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2437 end if;
2439 -- Case of tagged record types
2441 elsif Is_Tagged_Type (Full_Type) then
2443 -- Call the primitive operation "=" of this type
2445 if Is_Class_Wide_Type (Full_Type) then
2446 Full_Type := Root_Type (Full_Type);
2447 end if;
2449 -- If this is derived from an untagged private type completed with a
2450 -- tagged type, it does not have a full view, so we use the primitive
2451 -- operations of the private type. This check should no longer be
2452 -- necessary when these types receive their full views ???
2454 if Is_Private_Type (Typ)
2455 and then not Is_Tagged_Type (Typ)
2456 and then not Is_Controlled (Typ)
2457 and then Is_Derived_Type (Typ)
2458 and then No (Full_View (Typ))
2459 then
2460 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2461 else
2462 Prim := First_Elmt (Primitive_Operations (Full_Type));
2463 end if;
2465 loop
2466 Eq_Op := Node (Prim);
2467 exit when Chars (Eq_Op) = Name_Op_Eq
2468 and then Etype (First_Formal (Eq_Op)) =
2469 Etype (Next_Formal (First_Formal (Eq_Op)))
2470 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2471 Next_Elmt (Prim);
2472 pragma Assert (Present (Prim));
2473 end loop;
2475 Eq_Op := Node (Prim);
2477 return
2478 Make_Function_Call (Loc,
2479 Name => New_Occurrence_Of (Eq_Op, Loc),
2480 Parameter_Associations =>
2481 New_List
2482 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2483 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2485 -- Case of untagged record types
2487 elsif Is_Record_Type (Full_Type) then
2488 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2490 if Present (Eq_Op) then
2491 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2493 -- Inherited equality from parent type. Convert the actuals to
2494 -- match signature of operation.
2496 declare
2497 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2499 begin
2500 return
2501 Make_Function_Call (Loc,
2502 Name => New_Occurrence_Of (Eq_Op, Loc),
2503 Parameter_Associations => New_List (
2504 OK_Convert_To (T, Lhs),
2505 OK_Convert_To (T, Rhs)));
2506 end;
2508 else
2509 -- Comparison between Unchecked_Union components
2511 if Is_Unchecked_Union (Full_Type) then
2512 declare
2513 Lhs_Type : Node_Id := Full_Type;
2514 Rhs_Type : Node_Id := Full_Type;
2515 Lhs_Discr_Val : Node_Id;
2516 Rhs_Discr_Val : Node_Id;
2518 begin
2519 -- Lhs subtype
2521 if Nkind (Lhs) = N_Selected_Component then
2522 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2523 end if;
2525 -- Rhs subtype
2527 if Nkind (Rhs) = N_Selected_Component then
2528 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2529 end if;
2531 -- Lhs of the composite equality
2533 if Is_Constrained (Lhs_Type) then
2535 -- Since the enclosing record type can never be an
2536 -- Unchecked_Union (this code is executed for records
2537 -- that do not have variants), we may reference its
2538 -- discriminant(s).
2540 if Nkind (Lhs) = N_Selected_Component
2541 and then Has_Per_Object_Constraint
2542 (Entity (Selector_Name (Lhs)))
2543 then
2544 Lhs_Discr_Val :=
2545 Make_Selected_Component (Loc,
2546 Prefix => Prefix (Lhs),
2547 Selector_Name =>
2548 New_Copy
2549 (Get_Discriminant_Value
2550 (First_Discriminant (Lhs_Type),
2551 Lhs_Type,
2552 Stored_Constraint (Lhs_Type))));
2554 else
2555 Lhs_Discr_Val :=
2556 New_Copy
2557 (Get_Discriminant_Value
2558 (First_Discriminant (Lhs_Type),
2559 Lhs_Type,
2560 Stored_Constraint (Lhs_Type)));
2562 end if;
2563 else
2564 -- It is not possible to infer the discriminant since
2565 -- the subtype is not constrained.
2567 return
2568 Make_Raise_Program_Error (Loc,
2569 Reason => PE_Unchecked_Union_Restriction);
2570 end if;
2572 -- Rhs of the composite equality
2574 if Is_Constrained (Rhs_Type) then
2575 if Nkind (Rhs) = N_Selected_Component
2576 and then Has_Per_Object_Constraint
2577 (Entity (Selector_Name (Rhs)))
2578 then
2579 Rhs_Discr_Val :=
2580 Make_Selected_Component (Loc,
2581 Prefix => Prefix (Rhs),
2582 Selector_Name =>
2583 New_Copy
2584 (Get_Discriminant_Value
2585 (First_Discriminant (Rhs_Type),
2586 Rhs_Type,
2587 Stored_Constraint (Rhs_Type))));
2589 else
2590 Rhs_Discr_Val :=
2591 New_Copy
2592 (Get_Discriminant_Value
2593 (First_Discriminant (Rhs_Type),
2594 Rhs_Type,
2595 Stored_Constraint (Rhs_Type)));
2597 end if;
2598 else
2599 return
2600 Make_Raise_Program_Error (Loc,
2601 Reason => PE_Unchecked_Union_Restriction);
2602 end if;
2604 -- Call the TSS equality function with the inferred
2605 -- discriminant values.
2607 return
2608 Make_Function_Call (Loc,
2609 Name => New_Occurrence_Of (Eq_Op, Loc),
2610 Parameter_Associations => New_List (
2611 Lhs,
2612 Rhs,
2613 Lhs_Discr_Val,
2614 Rhs_Discr_Val));
2615 end;
2617 -- All cases other than comparing Unchecked_Union types
2619 else
2620 declare
2621 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2622 begin
2623 return
2624 Make_Function_Call (Loc,
2625 Name =>
2626 New_Occurrence_Of (Eq_Op, Loc),
2627 Parameter_Associations => New_List (
2628 OK_Convert_To (T, Lhs),
2629 OK_Convert_To (T, Rhs)));
2630 end;
2631 end if;
2632 end if;
2634 -- Equality composes in Ada 2012 for untagged record types. It also
2635 -- composes for bounded strings, because they are part of the
2636 -- predefined environment. We could make it compose for bounded
2637 -- strings by making them tagged, or by making sure all subcomponents
2638 -- are set to the same value, even when not used. Instead, we have
2639 -- this special case in the compiler, because it's more efficient.
2641 elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then
2643 -- If no TSS has been created for the type, check whether there is
2644 -- a primitive equality declared for it.
2646 declare
2647 Op : constant Node_Id := Find_Primitive_Eq;
2649 begin
2650 -- Use user-defined primitive if it exists, otherwise use
2651 -- predefined equality.
2653 if Present (Op) then
2654 return Op;
2655 else
2656 return Make_Op_Eq (Loc, Lhs, Rhs);
2657 end if;
2658 end;
2660 else
2661 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2662 end if;
2664 -- Non-composite types (always use predefined equality)
2666 else
2667 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2668 end if;
2669 end Expand_Composite_Equality;
2671 ------------------------
2672 -- Expand_Concatenate --
2673 ------------------------
2675 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2676 Loc : constant Source_Ptr := Sloc (Cnode);
2678 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2679 -- Result type of concatenation
2681 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2682 -- Component type. Elements of this component type can appear as one
2683 -- of the operands of concatenation as well as arrays.
2685 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2686 -- Index subtype
2688 Ityp : constant Entity_Id := Base_Type (Istyp);
2689 -- Index type. This is the base type of the index subtype, and is used
2690 -- for all computed bounds (which may be out of range of Istyp in the
2691 -- case of null ranges).
2693 Artyp : Entity_Id;
2694 -- This is the type we use to do arithmetic to compute the bounds and
2695 -- lengths of operands. The choice of this type is a little subtle and
2696 -- is discussed in a separate section at the start of the body code.
2698 Concatenation_Error : exception;
2699 -- Raised if concatenation is sure to raise a CE
2701 Result_May_Be_Null : Boolean := True;
2702 -- Reset to False if at least one operand is encountered which is known
2703 -- at compile time to be non-null. Used for handling the special case
2704 -- of setting the high bound to the last operand high bound for a null
2705 -- result, thus ensuring a proper high bound in the super-flat case.
2707 N : constant Nat := List_Length (Opnds);
2708 -- Number of concatenation operands including possibly null operands
2710 NN : Nat := 0;
2711 -- Number of operands excluding any known to be null, except that the
2712 -- last operand is always retained, in case it provides the bounds for
2713 -- a null result.
2715 Opnd : Node_Id := Empty;
2716 -- Current operand being processed in the loop through operands. After
2717 -- this loop is complete, always contains the last operand (which is not
2718 -- the same as Operands (NN), since null operands are skipped).
2720 -- Arrays describing the operands, only the first NN entries of each
2721 -- array are set (NN < N when we exclude known null operands).
2723 Is_Fixed_Length : array (1 .. N) of Boolean;
2724 -- True if length of corresponding operand known at compile time
2726 Operands : array (1 .. N) of Node_Id;
2727 -- Set to the corresponding entry in the Opnds list (but note that null
2728 -- operands are excluded, so not all entries in the list are stored).
2730 Fixed_Length : array (1 .. N) of Uint;
2731 -- Set to length of operand. Entries in this array are set only if the
2732 -- corresponding entry in Is_Fixed_Length is True.
2734 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2735 -- Set to lower bound of operand. Either an integer literal in the case
2736 -- where the bound is known at compile time, else actual lower bound.
2737 -- The operand low bound is of type Ityp.
2739 Var_Length : array (1 .. N) of Entity_Id;
2740 -- Set to an entity of type Natural that contains the length of an
2741 -- operand whose length is not known at compile time. Entries in this
2742 -- array are set only if the corresponding entry in Is_Fixed_Length
2743 -- is False. The entity is of type Artyp.
2745 Aggr_Length : array (0 .. N) of Node_Id;
2746 -- The J'th entry in an expression node that represents the total length
2747 -- of operands 1 through J. It is either an integer literal node, or a
2748 -- reference to a constant entity with the right value, so it is fine
2749 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2750 -- entry always is set to zero. The length is of type Artyp.
2752 Low_Bound : Node_Id;
2753 -- A tree node representing the low bound of the result (of type Ityp).
2754 -- This is either an integer literal node, or an identifier reference to
2755 -- a constant entity initialized to the appropriate value.
2757 Last_Opnd_Low_Bound : Node_Id := Empty;
2758 -- A tree node representing the low bound of the last operand. This
2759 -- need only be set if the result could be null. It is used for the
2760 -- special case of setting the right low bound for a null result.
2761 -- This is of type Ityp.
2763 Last_Opnd_High_Bound : Node_Id := Empty;
2764 -- A tree node representing the high bound of the last operand. This
2765 -- need only be set if the result could be null. It is used for the
2766 -- special case of setting the right high bound for a null result.
2767 -- This is of type Ityp.
2769 High_Bound : Node_Id := Empty;
2770 -- A tree node representing the high bound of the result (of type Ityp)
2772 Result : Node_Id;
2773 -- Result of the concatenation (of type Ityp)
2775 Actions : constant List_Id := New_List;
2776 -- Collect actions to be inserted
2778 Known_Non_Null_Operand_Seen : Boolean;
2779 -- Set True during generation of the assignments of operands into
2780 -- result once an operand known to be non-null has been seen.
2782 function Library_Level_Target return Boolean;
2783 -- Return True if the concatenation is within the expression of the
2784 -- declaration of a library-level object.
2786 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2787 -- This function makes an N_Integer_Literal node that is returned in
2788 -- analyzed form with the type set to Artyp. Importantly this literal
2789 -- is not flagged as static, so that if we do computations with it that
2790 -- result in statically detected out of range conditions, we will not
2791 -- generate error messages but instead warning messages.
2793 function To_Artyp (X : Node_Id) return Node_Id;
2794 -- Given a node of type Ityp, returns the corresponding value of type
2795 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2796 -- For enum types, the Pos of the value is returned.
2798 function To_Ityp (X : Node_Id) return Node_Id;
2799 -- The inverse function (uses Val in the case of enumeration types)
2801 --------------------------
2802 -- Library_Level_Target --
2803 --------------------------
2805 function Library_Level_Target return Boolean is
2806 P : Node_Id := Parent (Cnode);
2808 begin
2809 while Present (P) loop
2810 if Nkind (P) = N_Object_Declaration then
2811 return Is_Library_Level_Entity (Defining_Identifier (P));
2813 -- Prevent the search from going too far
2815 elsif Is_Body_Or_Package_Declaration (P) then
2816 return False;
2817 end if;
2819 P := Parent (P);
2820 end loop;
2822 return False;
2823 end Library_Level_Target;
2825 ------------------------
2826 -- Make_Artyp_Literal --
2827 ------------------------
2829 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2830 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2831 begin
2832 Set_Etype (Result, Artyp);
2833 Set_Analyzed (Result, True);
2834 Set_Is_Static_Expression (Result, False);
2835 return Result;
2836 end Make_Artyp_Literal;
2838 --------------
2839 -- To_Artyp --
2840 --------------
2842 function To_Artyp (X : Node_Id) return Node_Id is
2843 begin
2844 if Ityp = Base_Type (Artyp) then
2845 return X;
2847 elsif Is_Enumeration_Type (Ityp) then
2848 return
2849 Make_Attribute_Reference (Loc,
2850 Prefix => New_Occurrence_Of (Ityp, Loc),
2851 Attribute_Name => Name_Pos,
2852 Expressions => New_List (X));
2854 else
2855 return Convert_To (Artyp, X);
2856 end if;
2857 end To_Artyp;
2859 -------------
2860 -- To_Ityp --
2861 -------------
2863 function To_Ityp (X : Node_Id) return Node_Id is
2864 begin
2865 if Is_Enumeration_Type (Ityp) then
2866 return
2867 Make_Attribute_Reference (Loc,
2868 Prefix => New_Occurrence_Of (Ityp, Loc),
2869 Attribute_Name => Name_Val,
2870 Expressions => New_List (X));
2872 -- Case where we will do a type conversion
2874 else
2875 if Ityp = Base_Type (Artyp) then
2876 return X;
2877 else
2878 return Convert_To (Ityp, X);
2879 end if;
2880 end if;
2881 end To_Ityp;
2883 -- Local Declarations
2885 Opnd_Typ : Entity_Id;
2886 Ent : Entity_Id;
2887 Len : Uint;
2888 J : Nat;
2889 Clen : Node_Id;
2890 Set : Boolean;
2892 -- Start of processing for Expand_Concatenate
2894 begin
2895 -- Choose an appropriate computational type
2897 -- We will be doing calculations of lengths and bounds in this routine
2898 -- and computing one from the other in some cases, e.g. getting the high
2899 -- bound by adding the length-1 to the low bound.
2901 -- We can't just use the index type, or even its base type for this
2902 -- purpose for two reasons. First it might be an enumeration type which
2903 -- is not suitable for computations of any kind, and second it may
2904 -- simply not have enough range. For example if the index type is
2905 -- -128..+127 then lengths can be up to 256, which is out of range of
2906 -- the type.
2908 -- For enumeration types, we can simply use Standard_Integer, this is
2909 -- sufficient since the actual number of enumeration literals cannot
2910 -- possibly exceed the range of integer (remember we will be doing the
2911 -- arithmetic with POS values, not representation values).
2913 if Is_Enumeration_Type (Ityp) then
2914 Artyp := Standard_Integer;
2916 -- If index type is Positive, we use the standard unsigned type, to give
2917 -- more room on the top of the range, obviating the need for an overflow
2918 -- check when creating the upper bound. This is needed to avoid junk
2919 -- overflow checks in the common case of String types.
2921 -- ??? Disabled for now
2923 -- elsif Istyp = Standard_Positive then
2924 -- Artyp := Standard_Unsigned;
2926 -- For modular types, we use a 32-bit modular type for types whose size
2927 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2928 -- identity type, and for larger unsigned types we use 64-bits.
2930 elsif Is_Modular_Integer_Type (Ityp) then
2931 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
2932 Artyp := Standard_Unsigned;
2933 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
2934 Artyp := Ityp;
2935 else
2936 Artyp := RTE (RE_Long_Long_Unsigned);
2937 end if;
2939 -- Similar treatment for signed types
2941 else
2942 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
2943 Artyp := Standard_Integer;
2944 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
2945 Artyp := Ityp;
2946 else
2947 Artyp := Standard_Long_Long_Integer;
2948 end if;
2949 end if;
2951 -- Supply dummy entry at start of length array
2953 Aggr_Length (0) := Make_Artyp_Literal (0);
2955 -- Go through operands setting up the above arrays
2957 J := 1;
2958 while J <= N loop
2959 Opnd := Remove_Head (Opnds);
2960 Opnd_Typ := Etype (Opnd);
2962 -- The parent got messed up when we put the operands in a list,
2963 -- so now put back the proper parent for the saved operand, that
2964 -- is to say the concatenation node, to make sure that each operand
2965 -- is seen as a subexpression, e.g. if actions must be inserted.
2967 Set_Parent (Opnd, Cnode);
2969 -- Set will be True when we have setup one entry in the array
2971 Set := False;
2973 -- Singleton element (or character literal) case
2975 if Base_Type (Opnd_Typ) = Ctyp then
2976 NN := NN + 1;
2977 Operands (NN) := Opnd;
2978 Is_Fixed_Length (NN) := True;
2979 Fixed_Length (NN) := Uint_1;
2980 Result_May_Be_Null := False;
2982 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2983 -- since we know that the result cannot be null).
2985 Opnd_Low_Bound (NN) :=
2986 Make_Attribute_Reference (Loc,
2987 Prefix => New_Occurrence_Of (Istyp, Loc),
2988 Attribute_Name => Name_First);
2990 Set := True;
2992 -- String literal case (can only occur for strings of course)
2994 elsif Nkind (Opnd) = N_String_Literal then
2995 Len := String_Literal_Length (Opnd_Typ);
2997 if Len /= 0 then
2998 Result_May_Be_Null := False;
2999 end if;
3001 -- Capture last operand low and high bound if result could be null
3003 if J = N and then Result_May_Be_Null then
3004 Last_Opnd_Low_Bound :=
3005 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3007 Last_Opnd_High_Bound :=
3008 Make_Op_Subtract (Loc,
3009 Left_Opnd =>
3010 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
3011 Right_Opnd => Make_Integer_Literal (Loc, 1));
3012 end if;
3014 -- Skip null string literal
3016 if J < N and then Len = 0 then
3017 goto Continue;
3018 end if;
3020 NN := NN + 1;
3021 Operands (NN) := Opnd;
3022 Is_Fixed_Length (NN) := True;
3024 -- Set length and bounds
3026 Fixed_Length (NN) := Len;
3028 Opnd_Low_Bound (NN) :=
3029 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3031 Set := True;
3033 -- All other cases
3035 else
3036 -- Check constrained case with known bounds
3038 if Is_Constrained (Opnd_Typ) then
3039 declare
3040 Index : constant Node_Id := First_Index (Opnd_Typ);
3041 Indx_Typ : constant Entity_Id := Etype (Index);
3042 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
3043 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
3045 begin
3046 -- Fixed length constrained array type with known at compile
3047 -- time bounds is last case of fixed length operand.
3049 if Compile_Time_Known_Value (Lo)
3050 and then
3051 Compile_Time_Known_Value (Hi)
3052 then
3053 declare
3054 Loval : constant Uint := Expr_Value (Lo);
3055 Hival : constant Uint := Expr_Value (Hi);
3056 Len : constant Uint :=
3057 UI_Max (Hival - Loval + 1, Uint_0);
3059 begin
3060 if Len > 0 then
3061 Result_May_Be_Null := False;
3062 end if;
3064 -- Capture last operand bounds if result could be null
3066 if J = N and then Result_May_Be_Null then
3067 Last_Opnd_Low_Bound :=
3068 Convert_To (Ityp,
3069 Make_Integer_Literal (Loc, Expr_Value (Lo)));
3071 Last_Opnd_High_Bound :=
3072 Convert_To (Ityp,
3073 Make_Integer_Literal (Loc, Expr_Value (Hi)));
3074 end if;
3076 -- Exclude null length case unless last operand
3078 if J < N and then Len = 0 then
3079 goto Continue;
3080 end if;
3082 NN := NN + 1;
3083 Operands (NN) := Opnd;
3084 Is_Fixed_Length (NN) := True;
3085 Fixed_Length (NN) := Len;
3087 Opnd_Low_Bound (NN) :=
3088 To_Ityp
3089 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
3090 Set := True;
3091 end;
3092 end if;
3093 end;
3094 end if;
3096 -- All cases where the length is not known at compile time, or the
3097 -- special case of an operand which is known to be null but has a
3098 -- lower bound other than 1 or is other than a string type.
3100 if not Set then
3101 NN := NN + 1;
3103 -- Capture operand bounds
3105 Opnd_Low_Bound (NN) :=
3106 Make_Attribute_Reference (Loc,
3107 Prefix =>
3108 Duplicate_Subexpr (Opnd, Name_Req => True),
3109 Attribute_Name => Name_First);
3111 -- Capture last operand bounds if result could be null
3113 if J = N and Result_May_Be_Null then
3114 Last_Opnd_Low_Bound :=
3115 Convert_To (Ityp,
3116 Make_Attribute_Reference (Loc,
3117 Prefix =>
3118 Duplicate_Subexpr (Opnd, Name_Req => True),
3119 Attribute_Name => Name_First));
3121 Last_Opnd_High_Bound :=
3122 Convert_To (Ityp,
3123 Make_Attribute_Reference (Loc,
3124 Prefix =>
3125 Duplicate_Subexpr (Opnd, Name_Req => True),
3126 Attribute_Name => Name_Last));
3127 end if;
3129 -- Capture length of operand in entity
3131 Operands (NN) := Opnd;
3132 Is_Fixed_Length (NN) := False;
3134 Var_Length (NN) := Make_Temporary (Loc, 'L');
3136 Append_To (Actions,
3137 Make_Object_Declaration (Loc,
3138 Defining_Identifier => Var_Length (NN),
3139 Constant_Present => True,
3140 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3141 Expression =>
3142 Make_Attribute_Reference (Loc,
3143 Prefix =>
3144 Duplicate_Subexpr (Opnd, Name_Req => True),
3145 Attribute_Name => Name_Length)));
3146 end if;
3147 end if;
3149 -- Set next entry in aggregate length array
3151 -- For first entry, make either integer literal for fixed length
3152 -- or a reference to the saved length for variable length.
3154 if NN = 1 then
3155 if Is_Fixed_Length (1) then
3156 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
3157 else
3158 Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc);
3159 end if;
3161 -- If entry is fixed length and only fixed lengths so far, make
3162 -- appropriate new integer literal adding new length.
3164 elsif Is_Fixed_Length (NN)
3165 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
3166 then
3167 Aggr_Length (NN) :=
3168 Make_Integer_Literal (Loc,
3169 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
3171 -- All other cases, construct an addition node for the length and
3172 -- create an entity initialized to this length.
3174 else
3175 Ent := Make_Temporary (Loc, 'L');
3177 if Is_Fixed_Length (NN) then
3178 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
3179 else
3180 Clen := New_Occurrence_Of (Var_Length (NN), Loc);
3181 end if;
3183 Append_To (Actions,
3184 Make_Object_Declaration (Loc,
3185 Defining_Identifier => Ent,
3186 Constant_Present => True,
3187 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3188 Expression =>
3189 Make_Op_Add (Loc,
3190 Left_Opnd => New_Copy_Tree (Aggr_Length (NN - 1)),
3191 Right_Opnd => Clen)));
3193 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
3194 end if;
3196 <<Continue>>
3197 J := J + 1;
3198 end loop;
3200 -- If we have only skipped null operands, return the last operand
3202 if NN = 0 then
3203 Result := Opnd;
3204 goto Done;
3205 end if;
3207 -- If we have only one non-null operand, return it and we are done.
3208 -- There is one case in which this cannot be done, and that is when
3209 -- the sole operand is of the element type, in which case it must be
3210 -- converted to an array, and the easiest way of doing that is to go
3211 -- through the normal general circuit.
3213 if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then
3214 Result := Operands (1);
3215 goto Done;
3216 end if;
3218 -- Cases where we have a real concatenation
3220 -- Next step is to find the low bound for the result array that we
3221 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3223 -- If the ultimate ancestor of the index subtype is a constrained array
3224 -- definition, then the lower bound is that of the index subtype as
3225 -- specified by (RM 4.5.3(6)).
3227 -- The right test here is to go to the root type, and then the ultimate
3228 -- ancestor is the first subtype of this root type.
3230 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
3231 Low_Bound :=
3232 Make_Attribute_Reference (Loc,
3233 Prefix =>
3234 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
3235 Attribute_Name => Name_First);
3237 -- If the first operand in the list has known length we know that
3238 -- the lower bound of the result is the lower bound of this operand.
3240 elsif Is_Fixed_Length (1) then
3241 Low_Bound := Opnd_Low_Bound (1);
3243 -- OK, we don't know the lower bound, we have to build a horrible
3244 -- if expression node of the form
3246 -- if Cond1'Length /= 0 then
3247 -- Opnd1 low bound
3248 -- else
3249 -- if Opnd2'Length /= 0 then
3250 -- Opnd2 low bound
3251 -- else
3252 -- ...
3254 -- The nesting ends either when we hit an operand whose length is known
3255 -- at compile time, or on reaching the last operand, whose low bound we
3256 -- take unconditionally whether or not it is null. It's easiest to do
3257 -- this with a recursive procedure:
3259 else
3260 declare
3261 function Get_Known_Bound (J : Nat) return Node_Id;
3262 -- Returns the lower bound determined by operands J .. NN
3264 ---------------------
3265 -- Get_Known_Bound --
3266 ---------------------
3268 function Get_Known_Bound (J : Nat) return Node_Id is
3269 begin
3270 if Is_Fixed_Length (J) or else J = NN then
3271 return New_Copy_Tree (Opnd_Low_Bound (J));
3273 else
3274 return
3275 Make_If_Expression (Loc,
3276 Expressions => New_List (
3278 Make_Op_Ne (Loc,
3279 Left_Opnd =>
3280 New_Occurrence_Of (Var_Length (J), Loc),
3281 Right_Opnd =>
3282 Make_Integer_Literal (Loc, 0)),
3284 New_Copy_Tree (Opnd_Low_Bound (J)),
3285 Get_Known_Bound (J + 1)));
3286 end if;
3287 end Get_Known_Bound;
3289 begin
3290 Ent := Make_Temporary (Loc, 'L');
3292 Append_To (Actions,
3293 Make_Object_Declaration (Loc,
3294 Defining_Identifier => Ent,
3295 Constant_Present => True,
3296 Object_Definition => New_Occurrence_Of (Ityp, Loc),
3297 Expression => Get_Known_Bound (1)));
3299 Low_Bound := New_Occurrence_Of (Ent, Loc);
3300 end;
3301 end if;
3303 -- Now we can safely compute the upper bound, normally
3304 -- Low_Bound + Length - 1.
3306 High_Bound :=
3307 To_Ityp
3308 (Make_Op_Add (Loc,
3309 Left_Opnd => To_Artyp (New_Copy_Tree (Low_Bound)),
3310 Right_Opnd =>
3311 Make_Op_Subtract (Loc,
3312 Left_Opnd => New_Copy_Tree (Aggr_Length (NN)),
3313 Right_Opnd => Make_Artyp_Literal (1))));
3315 -- Note that calculation of the high bound may cause overflow in some
3316 -- very weird cases, so in the general case we need an overflow check on
3317 -- the high bound. We can avoid this for the common case of string types
3318 -- and other types whose index is Positive, since we chose a wider range
3319 -- for the arithmetic type. If checks are suppressed we do not set the
3320 -- flag, and possibly superfluous warnings will be omitted.
3322 if Istyp /= Standard_Positive
3323 and then not Overflow_Checks_Suppressed (Istyp)
3324 then
3325 Activate_Overflow_Check (High_Bound);
3326 end if;
3328 -- Handle the exceptional case where the result is null, in which case
3329 -- case the bounds come from the last operand (so that we get the proper
3330 -- bounds if the last operand is super-flat).
3332 if Result_May_Be_Null then
3333 Low_Bound :=
3334 Make_If_Expression (Loc,
3335 Expressions => New_List (
3336 Make_Op_Eq (Loc,
3337 Left_Opnd => New_Copy_Tree (Aggr_Length (NN)),
3338 Right_Opnd => Make_Artyp_Literal (0)),
3339 Last_Opnd_Low_Bound,
3340 Low_Bound));
3342 High_Bound :=
3343 Make_If_Expression (Loc,
3344 Expressions => New_List (
3345 Make_Op_Eq (Loc,
3346 Left_Opnd => New_Copy_Tree (Aggr_Length (NN)),
3347 Right_Opnd => Make_Artyp_Literal (0)),
3348 Last_Opnd_High_Bound,
3349 High_Bound));
3350 end if;
3352 -- Here is where we insert the saved up actions
3354 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3356 -- Now we construct an array object with appropriate bounds. We mark
3357 -- the target as internal to prevent useless initialization when
3358 -- Initialize_Scalars is enabled. Also since this is the actual result
3359 -- entity, we make sure we have debug information for the result.
3361 Ent := Make_Temporary (Loc, 'S');
3362 Set_Is_Internal (Ent);
3363 Set_Needs_Debug_Info (Ent);
3365 -- If the bound is statically known to be out of range, we do not want
3366 -- to abort, we want a warning and a runtime constraint error. Note that
3367 -- we have arranged that the result will not be treated as a static
3368 -- constant, so we won't get an illegality during this insertion.
3370 Insert_Action (Cnode,
3371 Make_Object_Declaration (Loc,
3372 Defining_Identifier => Ent,
3373 Object_Definition =>
3374 Make_Subtype_Indication (Loc,
3375 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3376 Constraint =>
3377 Make_Index_Or_Discriminant_Constraint (Loc,
3378 Constraints => New_List (
3379 Make_Range (Loc,
3380 Low_Bound => Low_Bound,
3381 High_Bound => High_Bound))))),
3382 Suppress => All_Checks);
3384 -- If the result of the concatenation appears as the initializing
3385 -- expression of an object declaration, we can just rename the
3386 -- result, rather than copying it.
3388 Set_OK_To_Rename (Ent);
3390 -- Catch the static out of range case now
3392 if Raises_Constraint_Error (High_Bound) then
3393 raise Concatenation_Error;
3394 end if;
3396 -- Now we will generate the assignments to do the actual concatenation
3398 -- There is one case in which we will not do this, namely when all the
3399 -- following conditions are met:
3401 -- The result type is Standard.String
3403 -- There are nine or fewer retained (non-null) operands
3405 -- The optimization level is -O0 or the debug flag gnatd.C is set,
3406 -- and the debug flag gnatd.c is not set.
3408 -- The corresponding System.Concat_n.Str_Concat_n routine is
3409 -- available in the run time.
3411 -- If all these conditions are met then we generate a call to the
3412 -- relevant concatenation routine. The purpose of this is to avoid
3413 -- undesirable code bloat at -O0.
3415 -- If the concatenation is within the declaration of a library-level
3416 -- object, we call the built-in concatenation routines to prevent code
3417 -- bloat, regardless of the optimization level. This is space efficient
3418 -- and prevents linking problems when units are compiled with different
3419 -- optimization levels.
3421 if Atyp = Standard_String
3422 and then NN in 2 .. 9
3423 and then (((Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3424 and then not Debug_Flag_Dot_C)
3425 or else Library_Level_Target)
3426 then
3427 declare
3428 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3429 (RE_Str_Concat_2,
3430 RE_Str_Concat_3,
3431 RE_Str_Concat_4,
3432 RE_Str_Concat_5,
3433 RE_Str_Concat_6,
3434 RE_Str_Concat_7,
3435 RE_Str_Concat_8,
3436 RE_Str_Concat_9);
3438 begin
3439 if RTE_Available (RR (NN)) then
3440 declare
3441 Opnds : constant List_Id :=
3442 New_List (New_Occurrence_Of (Ent, Loc));
3444 begin
3445 for J in 1 .. NN loop
3446 if Is_List_Member (Operands (J)) then
3447 Remove (Operands (J));
3448 end if;
3450 if Base_Type (Etype (Operands (J))) = Ctyp then
3451 Append_To (Opnds,
3452 Make_Aggregate (Loc,
3453 Component_Associations => New_List (
3454 Make_Component_Association (Loc,
3455 Choices => New_List (
3456 Make_Integer_Literal (Loc, 1)),
3457 Expression => Operands (J)))));
3459 else
3460 Append_To (Opnds, Operands (J));
3461 end if;
3462 end loop;
3464 Insert_Action (Cnode,
3465 Make_Procedure_Call_Statement (Loc,
3466 Name => New_Occurrence_Of (RTE (RR (NN)), Loc),
3467 Parameter_Associations => Opnds));
3469 Result := New_Occurrence_Of (Ent, Loc);
3470 goto Done;
3471 end;
3472 end if;
3473 end;
3474 end if;
3476 -- Not special case so generate the assignments
3478 Known_Non_Null_Operand_Seen := False;
3480 for J in 1 .. NN loop
3481 declare
3482 Lo : constant Node_Id :=
3483 Make_Op_Add (Loc,
3484 Left_Opnd => To_Artyp (New_Copy_Tree (Low_Bound)),
3485 Right_Opnd => Aggr_Length (J - 1));
3487 Hi : constant Node_Id :=
3488 Make_Op_Add (Loc,
3489 Left_Opnd => To_Artyp (New_Copy_Tree (Low_Bound)),
3490 Right_Opnd =>
3491 Make_Op_Subtract (Loc,
3492 Left_Opnd => Aggr_Length (J),
3493 Right_Opnd => Make_Artyp_Literal (1)));
3495 begin
3496 -- Singleton case, simple assignment
3498 if Base_Type (Etype (Operands (J))) = Ctyp then
3499 Known_Non_Null_Operand_Seen := True;
3500 Insert_Action (Cnode,
3501 Make_Assignment_Statement (Loc,
3502 Name =>
3503 Make_Indexed_Component (Loc,
3504 Prefix => New_Occurrence_Of (Ent, Loc),
3505 Expressions => New_List (To_Ityp (Lo))),
3506 Expression => Operands (J)),
3507 Suppress => All_Checks);
3509 -- Array case, slice assignment, skipped when argument is fixed
3510 -- length and known to be null.
3512 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3513 declare
3514 Assign : Node_Id :=
3515 Make_Assignment_Statement (Loc,
3516 Name =>
3517 Make_Slice (Loc,
3518 Prefix =>
3519 New_Occurrence_Of (Ent, Loc),
3520 Discrete_Range =>
3521 Make_Range (Loc,
3522 Low_Bound => To_Ityp (Lo),
3523 High_Bound => To_Ityp (Hi))),
3524 Expression => Operands (J));
3525 begin
3526 if Is_Fixed_Length (J) then
3527 Known_Non_Null_Operand_Seen := True;
3529 elsif not Known_Non_Null_Operand_Seen then
3531 -- Here if operand length is not statically known and no
3532 -- operand known to be non-null has been processed yet.
3533 -- If operand length is 0, we do not need to perform the
3534 -- assignment, and we must avoid the evaluation of the
3535 -- high bound of the slice, since it may underflow if the
3536 -- low bound is Ityp'First.
3538 Assign :=
3539 Make_Implicit_If_Statement (Cnode,
3540 Condition =>
3541 Make_Op_Ne (Loc,
3542 Left_Opnd =>
3543 New_Occurrence_Of (Var_Length (J), Loc),
3544 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3545 Then_Statements => New_List (Assign));
3546 end if;
3548 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3549 end;
3550 end if;
3551 end;
3552 end loop;
3554 -- Finally we build the result, which is a reference to the array object
3556 Result := New_Occurrence_Of (Ent, Loc);
3558 <<Done>>
3559 Rewrite (Cnode, Result);
3560 Analyze_And_Resolve (Cnode, Atyp);
3562 exception
3563 when Concatenation_Error =>
3565 -- Kill warning generated for the declaration of the static out of
3566 -- range high bound, and instead generate a Constraint_Error with
3567 -- an appropriate specific message.
3569 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3570 Apply_Compile_Time_Constraint_Error
3571 (N => Cnode,
3572 Msg => "concatenation result upper bound out of range??",
3573 Reason => CE_Range_Check_Failed);
3574 end Expand_Concatenate;
3576 ---------------------------------------------------
3577 -- Expand_Membership_Minimize_Eliminate_Overflow --
3578 ---------------------------------------------------
3580 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is
3581 pragma Assert (Nkind (N) = N_In);
3582 -- Despite the name, this routine applies only to N_In, not to
3583 -- N_Not_In. The latter is always rewritten as not (X in Y).
3585 Result_Type : constant Entity_Id := Etype (N);
3586 -- Capture result type, may be a derived boolean type
3588 Loc : constant Source_Ptr := Sloc (N);
3589 Lop : constant Node_Id := Left_Opnd (N);
3590 Rop : constant Node_Id := Right_Opnd (N);
3592 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3593 -- is thus tempting to capture these values, but due to the rewrites
3594 -- that occur as a result of overflow checking, these values change
3595 -- as we go along, and it is safe just to always use Etype explicitly.
3597 Restype : constant Entity_Id := Etype (N);
3598 -- Save result type
3600 Lo, Hi : Uint;
3601 -- Bounds in Minimize calls, not used currently
3603 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
3604 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3606 begin
3607 Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False);
3609 -- If right operand is a subtype name, and the subtype name has no
3610 -- predicate, then we can just replace the right operand with an
3611 -- explicit range T'First .. T'Last, and use the explicit range code.
3613 if Nkind (Rop) /= N_Range
3614 and then No (Predicate_Function (Etype (Rop)))
3615 then
3616 declare
3617 Rtyp : constant Entity_Id := Etype (Rop);
3618 begin
3619 Rewrite (Rop,
3620 Make_Range (Loc,
3621 Low_Bound =>
3622 Make_Attribute_Reference (Loc,
3623 Attribute_Name => Name_First,
3624 Prefix => New_Occurrence_Of (Rtyp, Loc)),
3625 High_Bound =>
3626 Make_Attribute_Reference (Loc,
3627 Attribute_Name => Name_Last,
3628 Prefix => New_Occurrence_Of (Rtyp, Loc))));
3629 Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks);
3630 end;
3631 end if;
3633 -- Here for the explicit range case. Note that the bounds of the range
3634 -- have not been processed for minimized or eliminated checks.
3636 if Nkind (Rop) = N_Range then
3637 Minimize_Eliminate_Overflows
3638 (Low_Bound (Rop), Lo, Hi, Top_Level => False);
3639 Minimize_Eliminate_Overflows
3640 (High_Bound (Rop), Lo, Hi, Top_Level => False);
3642 -- We have A in B .. C, treated as A >= B and then A <= C
3644 -- Bignum case
3646 if Is_RTE (Etype (Lop), RE_Bignum)
3647 or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum)
3648 or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum)
3649 then
3650 declare
3651 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3652 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3653 L : constant Entity_Id :=
3654 Make_Defining_Identifier (Loc, Name_uL);
3655 Lopnd : constant Node_Id := Convert_To_Bignum (Lop);
3656 Lbound : constant Node_Id :=
3657 Convert_To_Bignum (Low_Bound (Rop));
3658 Hbound : constant Node_Id :=
3659 Convert_To_Bignum (High_Bound (Rop));
3661 -- Now we rewrite the membership test node to look like
3663 -- do
3664 -- Bnn : Result_Type;
3665 -- declare
3666 -- M : Mark_Id := SS_Mark;
3667 -- L : Bignum := Lopnd;
3668 -- begin
3669 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3670 -- SS_Release (M);
3671 -- end;
3672 -- in
3673 -- Bnn
3674 -- end
3676 begin
3677 -- Insert declaration of L into declarations of bignum block
3679 Insert_After
3680 (Last (Declarations (Blk)),
3681 Make_Object_Declaration (Loc,
3682 Defining_Identifier => L,
3683 Object_Definition =>
3684 New_Occurrence_Of (RTE (RE_Bignum), Loc),
3685 Expression => Lopnd));
3687 -- Insert assignment to Bnn into expressions of bignum block
3689 Insert_Before
3690 (First (Statements (Handled_Statement_Sequence (Blk))),
3691 Make_Assignment_Statement (Loc,
3692 Name => New_Occurrence_Of (Bnn, Loc),
3693 Expression =>
3694 Make_And_Then (Loc,
3695 Left_Opnd =>
3696 Make_Function_Call (Loc,
3697 Name =>
3698 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
3699 Parameter_Associations => New_List (
3700 New_Occurrence_Of (L, Loc),
3701 Lbound)),
3703 Right_Opnd =>
3704 Make_Function_Call (Loc,
3705 Name =>
3706 New_Occurrence_Of (RTE (RE_Big_LE), Loc),
3707 Parameter_Associations => New_List (
3708 New_Occurrence_Of (L, Loc),
3709 Hbound)))));
3711 -- Now rewrite the node
3713 Rewrite (N,
3714 Make_Expression_With_Actions (Loc,
3715 Actions => New_List (
3716 Make_Object_Declaration (Loc,
3717 Defining_Identifier => Bnn,
3718 Object_Definition =>
3719 New_Occurrence_Of (Result_Type, Loc)),
3720 Blk),
3721 Expression => New_Occurrence_Of (Bnn, Loc)));
3722 Analyze_And_Resolve (N, Result_Type);
3723 return;
3724 end;
3726 -- Here if no bignums around
3728 else
3729 -- Case where types are all the same
3731 if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop)))
3732 and then
3733 Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop)))
3734 then
3735 null;
3737 -- If types are not all the same, it means that we have rewritten
3738 -- at least one of them to be of type Long_Long_Integer, and we
3739 -- will convert the other operands to Long_Long_Integer.
3741 else
3742 Convert_To_And_Rewrite (LLIB, Lop);
3743 Set_Analyzed (Lop, False);
3744 Analyze_And_Resolve (Lop, LLIB);
3746 -- For the right operand, avoid unnecessary recursion into
3747 -- this routine, we know that overflow is not possible.
3749 Convert_To_And_Rewrite (LLIB, Low_Bound (Rop));
3750 Convert_To_And_Rewrite (LLIB, High_Bound (Rop));
3751 Set_Analyzed (Rop, False);
3752 Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check);
3753 end if;
3755 -- Now the three operands are of the same signed integer type,
3756 -- so we can use the normal expansion routine for membership,
3757 -- setting the flag to prevent recursion into this procedure.
3759 Set_No_Minimize_Eliminate (N);
3760 Expand_N_In (N);
3761 end if;
3763 -- Right operand is a subtype name and the subtype has a predicate. We
3764 -- have to make sure the predicate is checked, and for that we need to
3765 -- use the standard N_In circuitry with appropriate types.
3767 else
3768 pragma Assert (Present (Predicate_Function (Etype (Rop))));
3770 -- If types are "right", just call Expand_N_In preventing recursion
3772 if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then
3773 Set_No_Minimize_Eliminate (N);
3774 Expand_N_In (N);
3776 -- Bignum case
3778 elsif Is_RTE (Etype (Lop), RE_Bignum) then
3780 -- For X in T, we want to rewrite our node as
3782 -- do
3783 -- Bnn : Result_Type;
3785 -- declare
3786 -- M : Mark_Id := SS_Mark;
3787 -- Lnn : Long_Long_Integer'Base
3788 -- Nnn : Bignum;
3790 -- begin
3791 -- Nnn := X;
3793 -- if not Bignum_In_LLI_Range (Nnn) then
3794 -- Bnn := False;
3795 -- else
3796 -- Lnn := From_Bignum (Nnn);
3797 -- Bnn :=
3798 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3799 -- and then T'Base (Lnn) in T;
3800 -- end if;
3802 -- SS_Release (M);
3803 -- end
3804 -- in
3805 -- Bnn
3806 -- end
3808 -- A bit gruesome, but there doesn't seem to be a simpler way
3810 declare
3811 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3812 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3813 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N);
3814 Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N);
3815 T : constant Entity_Id := Etype (Rop);
3816 TB : constant Entity_Id := Base_Type (T);
3817 Nin : Node_Id;
3819 begin
3820 -- Mark the last membership operation to prevent recursion
3822 Nin :=
3823 Make_In (Loc,
3824 Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)),
3825 Right_Opnd => New_Occurrence_Of (T, Loc));
3826 Set_No_Minimize_Eliminate (Nin);
3828 -- Now decorate the block
3830 Insert_After
3831 (Last (Declarations (Blk)),
3832 Make_Object_Declaration (Loc,
3833 Defining_Identifier => Lnn,
3834 Object_Definition => New_Occurrence_Of (LLIB, Loc)));
3836 Insert_After
3837 (Last (Declarations (Blk)),
3838 Make_Object_Declaration (Loc,
3839 Defining_Identifier => Nnn,
3840 Object_Definition =>
3841 New_Occurrence_Of (RTE (RE_Bignum), Loc)));
3843 Insert_List_Before
3844 (First (Statements (Handled_Statement_Sequence (Blk))),
3845 New_List (
3846 Make_Assignment_Statement (Loc,
3847 Name => New_Occurrence_Of (Nnn, Loc),
3848 Expression => Relocate_Node (Lop)),
3850 Make_Implicit_If_Statement (N,
3851 Condition =>
3852 Make_Op_Not (Loc,
3853 Right_Opnd =>
3854 Make_Function_Call (Loc,
3855 Name =>
3856 New_Occurrence_Of
3857 (RTE (RE_Bignum_In_LLI_Range), Loc),
3858 Parameter_Associations => New_List (
3859 New_Occurrence_Of (Nnn, Loc)))),
3861 Then_Statements => New_List (
3862 Make_Assignment_Statement (Loc,
3863 Name => New_Occurrence_Of (Bnn, Loc),
3864 Expression =>
3865 New_Occurrence_Of (Standard_False, Loc))),
3867 Else_Statements => New_List (
3868 Make_Assignment_Statement (Loc,
3869 Name => New_Occurrence_Of (Lnn, Loc),
3870 Expression =>
3871 Make_Function_Call (Loc,
3872 Name =>
3873 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
3874 Parameter_Associations => New_List (
3875 New_Occurrence_Of (Nnn, Loc)))),
3877 Make_Assignment_Statement (Loc,
3878 Name => New_Occurrence_Of (Bnn, Loc),
3879 Expression =>
3880 Make_And_Then (Loc,
3881 Left_Opnd =>
3882 Make_In (Loc,
3883 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3884 Right_Opnd =>
3885 Make_Range (Loc,
3886 Low_Bound =>
3887 Convert_To (LLIB,
3888 Make_Attribute_Reference (Loc,
3889 Attribute_Name => Name_First,
3890 Prefix =>
3891 New_Occurrence_Of (TB, Loc))),
3893 High_Bound =>
3894 Convert_To (LLIB,
3895 Make_Attribute_Reference (Loc,
3896 Attribute_Name => Name_Last,
3897 Prefix =>
3898 New_Occurrence_Of (TB, Loc))))),
3900 Right_Opnd => Nin))))));
3902 -- Now we can do the rewrite
3904 Rewrite (N,
3905 Make_Expression_With_Actions (Loc,
3906 Actions => New_List (
3907 Make_Object_Declaration (Loc,
3908 Defining_Identifier => Bnn,
3909 Object_Definition =>
3910 New_Occurrence_Of (Result_Type, Loc)),
3911 Blk),
3912 Expression => New_Occurrence_Of (Bnn, Loc)));
3913 Analyze_And_Resolve (N, Result_Type);
3914 return;
3915 end;
3917 -- Not bignum case, but types don't match (this means we rewrote the
3918 -- left operand to be Long_Long_Integer).
3920 else
3921 pragma Assert (Base_Type (Etype (Lop)) = LLIB);
3923 -- We rewrite the membership test as (where T is the type with
3924 -- the predicate, i.e. the type of the right operand)
3926 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3927 -- and then T'Base (Lop) in T
3929 declare
3930 T : constant Entity_Id := Etype (Rop);
3931 TB : constant Entity_Id := Base_Type (T);
3932 Nin : Node_Id;
3934 begin
3935 -- The last membership test is marked to prevent recursion
3937 Nin :=
3938 Make_In (Loc,
3939 Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)),
3940 Right_Opnd => New_Occurrence_Of (T, Loc));
3941 Set_No_Minimize_Eliminate (Nin);
3943 -- Now do the rewrite
3945 Rewrite (N,
3946 Make_And_Then (Loc,
3947 Left_Opnd =>
3948 Make_In (Loc,
3949 Left_Opnd => Lop,
3950 Right_Opnd =>
3951 Make_Range (Loc,
3952 Low_Bound =>
3953 Convert_To (LLIB,
3954 Make_Attribute_Reference (Loc,
3955 Attribute_Name => Name_First,
3956 Prefix =>
3957 New_Occurrence_Of (TB, Loc))),
3958 High_Bound =>
3959 Convert_To (LLIB,
3960 Make_Attribute_Reference (Loc,
3961 Attribute_Name => Name_Last,
3962 Prefix =>
3963 New_Occurrence_Of (TB, Loc))))),
3964 Right_Opnd => Nin));
3965 Set_Analyzed (N, False);
3966 Analyze_And_Resolve (N, Restype);
3967 end;
3968 end if;
3969 end if;
3970 end Expand_Membership_Minimize_Eliminate_Overflow;
3972 ---------------------------------
3973 -- Expand_Nonbinary_Modular_Op --
3974 ---------------------------------
3976 procedure Expand_Nonbinary_Modular_Op (N : Node_Id) is
3977 Loc : constant Source_Ptr := Sloc (N);
3978 Typ : constant Entity_Id := Etype (N);
3980 procedure Expand_Modular_Addition;
3981 -- Expand the modular addition, handling the special case of adding a
3982 -- constant.
3984 procedure Expand_Modular_Op;
3985 -- Compute the general rule: (lhs OP rhs) mod Modulus
3987 procedure Expand_Modular_Subtraction;
3988 -- Expand the modular addition, handling the special case of subtracting
3989 -- a constant.
3991 -----------------------------
3992 -- Expand_Modular_Addition --
3993 -----------------------------
3995 procedure Expand_Modular_Addition is
3996 begin
3997 -- If this is not the addition of a constant then compute it using
3998 -- the general rule: (lhs + rhs) mod Modulus
4000 if Nkind (Right_Opnd (N)) /= N_Integer_Literal then
4001 Expand_Modular_Op;
4003 -- If this is an addition of a constant, convert it to a subtraction
4004 -- plus a conditional expression since we can compute it faster than
4005 -- computing the modulus.
4007 -- modMinusRhs = Modulus - rhs
4008 -- if lhs < modMinusRhs then lhs + rhs
4009 -- else lhs - modMinusRhs
4011 else
4012 declare
4013 Mod_Minus_Right : constant Uint :=
4014 Modulus (Typ) - Intval (Right_Opnd (N));
4016 Exprs : constant List_Id := New_List;
4017 Cond_Expr : constant Node_Id := New_Op_Node (N_Op_Lt, Loc);
4018 Then_Expr : constant Node_Id := New_Op_Node (N_Op_Add, Loc);
4019 Else_Expr : constant Node_Id := New_Op_Node (N_Op_Subtract,
4020 Loc);
4021 begin
4022 Set_Left_Opnd (Cond_Expr,
4023 New_Copy_Tree (Left_Opnd (N)));
4024 Set_Right_Opnd (Cond_Expr,
4025 Make_Integer_Literal (Loc, Mod_Minus_Right));
4026 Append_To (Exprs, Cond_Expr);
4028 Set_Left_Opnd (Then_Expr,
4029 Unchecked_Convert_To (Standard_Unsigned,
4030 New_Copy_Tree (Left_Opnd (N))));
4031 Set_Right_Opnd (Then_Expr,
4032 Make_Integer_Literal (Loc, Intval (Right_Opnd (N))));
4033 Append_To (Exprs, Then_Expr);
4035 Set_Left_Opnd (Else_Expr,
4036 Unchecked_Convert_To (Standard_Unsigned,
4037 New_Copy_Tree (Left_Opnd (N))));
4038 Set_Right_Opnd (Else_Expr,
4039 Make_Integer_Literal (Loc, Mod_Minus_Right));
4040 Append_To (Exprs, Else_Expr);
4042 Rewrite (N,
4043 Unchecked_Convert_To (Typ,
4044 Make_If_Expression (Loc, Expressions => Exprs)));
4045 end;
4046 end if;
4047 end Expand_Modular_Addition;
4049 -----------------------
4050 -- Expand_Modular_Op --
4051 -----------------------
4053 procedure Expand_Modular_Op is
4054 Op_Expr : constant Node_Id := New_Op_Node (Nkind (N), Loc);
4055 Mod_Expr : constant Node_Id := New_Op_Node (N_Op_Mod, Loc);
4057 begin
4058 -- Convert nonbinary modular type operands into integer values. Thus
4059 -- we avoid never-ending loops expanding them, and we also ensure
4060 -- the back end never receives nonbinary modular type expressions.
4062 if Nkind_In (Nkind (N), N_Op_And, N_Op_Or) then
4063 Set_Left_Opnd (Op_Expr,
4064 Unchecked_Convert_To (Standard_Unsigned,
4065 New_Copy_Tree (Left_Opnd (N))));
4066 Set_Right_Opnd (Op_Expr,
4067 Unchecked_Convert_To (Standard_Unsigned,
4068 New_Copy_Tree (Right_Opnd (N))));
4069 Set_Left_Opnd (Mod_Expr,
4070 Unchecked_Convert_To (Standard_Integer, Op_Expr));
4072 else
4073 Set_Left_Opnd (Op_Expr,
4074 Unchecked_Convert_To (Standard_Integer,
4075 New_Copy_Tree (Left_Opnd (N))));
4076 Set_Right_Opnd (Op_Expr,
4077 Unchecked_Convert_To (Standard_Integer,
4078 New_Copy_Tree (Right_Opnd (N))));
4080 -- Link this node to the tree to analyze it
4082 -- If the parent node is an expression with actions we link it to
4083 -- N since otherwise Force_Evaluation cannot identify if this node
4084 -- comes from the Expression and rejects generating the temporary.
4086 if Nkind (Parent (N)) = N_Expression_With_Actions then
4087 Set_Parent (Op_Expr, N);
4089 -- Common case
4091 else
4092 Set_Parent (Op_Expr, Parent (N));
4093 end if;
4095 Analyze (Op_Expr);
4097 -- Force generating a temporary because in the expansion of this
4098 -- expression we may generate code that performs this computation
4099 -- several times.
4101 Force_Evaluation (Op_Expr, Mode => Strict);
4103 Set_Left_Opnd (Mod_Expr, Op_Expr);
4104 end if;
4106 Set_Right_Opnd (Mod_Expr,
4107 Make_Integer_Literal (Loc, Modulus (Typ)));
4109 Rewrite (N,
4110 Unchecked_Convert_To (Typ, Mod_Expr));
4111 end Expand_Modular_Op;
4113 --------------------------------
4114 -- Expand_Modular_Subtraction --
4115 --------------------------------
4117 procedure Expand_Modular_Subtraction is
4118 begin
4119 -- If this is not the addition of a constant then compute it using
4120 -- the general rule: (lhs + rhs) mod Modulus
4122 if Nkind (Right_Opnd (N)) /= N_Integer_Literal then
4123 Expand_Modular_Op;
4125 -- If this is an addition of a constant, convert it to a subtraction
4126 -- plus a conditional expression since we can compute it faster than
4127 -- computing the modulus.
4129 -- modMinusRhs = Modulus - rhs
4130 -- if lhs < rhs then lhs + modMinusRhs
4131 -- else lhs - rhs
4133 else
4134 declare
4135 Mod_Minus_Right : constant Uint :=
4136 Modulus (Typ) - Intval (Right_Opnd (N));
4138 Exprs : constant List_Id := New_List;
4139 Cond_Expr : constant Node_Id := New_Op_Node (N_Op_Lt, Loc);
4140 Then_Expr : constant Node_Id := New_Op_Node (N_Op_Add, Loc);
4141 Else_Expr : constant Node_Id := New_Op_Node (N_Op_Subtract,
4142 Loc);
4143 begin
4144 Set_Left_Opnd (Cond_Expr,
4145 New_Copy_Tree (Left_Opnd (N)));
4146 Set_Right_Opnd (Cond_Expr,
4147 Make_Integer_Literal (Loc, Intval (Right_Opnd (N))));
4148 Append_To (Exprs, Cond_Expr);
4150 Set_Left_Opnd (Then_Expr,
4151 Unchecked_Convert_To (Standard_Unsigned,
4152 New_Copy_Tree (Left_Opnd (N))));
4153 Set_Right_Opnd (Then_Expr,
4154 Make_Integer_Literal (Loc, Mod_Minus_Right));
4155 Append_To (Exprs, Then_Expr);
4157 Set_Left_Opnd (Else_Expr,
4158 Unchecked_Convert_To (Standard_Unsigned,
4159 New_Copy_Tree (Left_Opnd (N))));
4160 Set_Right_Opnd (Else_Expr,
4161 Unchecked_Convert_To (Standard_Unsigned,
4162 New_Copy_Tree (Right_Opnd (N))));
4163 Append_To (Exprs, Else_Expr);
4165 Rewrite (N,
4166 Unchecked_Convert_To (Typ,
4167 Make_If_Expression (Loc, Expressions => Exprs)));
4168 end;
4169 end if;
4170 end Expand_Modular_Subtraction;
4172 -- Start of processing for Expand_Nonbinary_Modular_Op
4174 begin
4175 -- No action needed if front-end expansion is not required or if we
4176 -- have a binary modular operand.
4178 if not Expand_Nonbinary_Modular_Ops
4179 or else not Non_Binary_Modulus (Typ)
4180 then
4181 return;
4182 end if;
4184 case Nkind (N) is
4185 when N_Op_Add =>
4186 Expand_Modular_Addition;
4188 when N_Op_Subtract =>
4189 Expand_Modular_Subtraction;
4191 when N_Op_Minus =>
4193 -- Expand -expr into (0 - expr)
4195 Rewrite (N,
4196 Make_Op_Subtract (Loc,
4197 Left_Opnd => Make_Integer_Literal (Loc, 0),
4198 Right_Opnd => Right_Opnd (N)));
4199 Analyze_And_Resolve (N, Typ);
4201 when others =>
4202 Expand_Modular_Op;
4203 end case;
4205 Analyze_And_Resolve (N, Typ);
4206 end Expand_Nonbinary_Modular_Op;
4208 ------------------------
4209 -- Expand_N_Allocator --
4210 ------------------------
4212 procedure Expand_N_Allocator (N : Node_Id) is
4213 Etyp : constant Entity_Id := Etype (Expression (N));
4214 Loc : constant Source_Ptr := Sloc (N);
4215 PtrT : constant Entity_Id := Etype (N);
4217 procedure Rewrite_Coextension (N : Node_Id);
4218 -- Static coextensions have the same lifetime as the entity they
4219 -- constrain. Such occurrences can be rewritten as aliased objects
4220 -- and their unrestricted access used instead of the coextension.
4222 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
4223 -- Given a constrained array type E, returns a node representing the
4224 -- code to compute the size in storage elements for the given type.
4225 -- This is done without using the attribute (which malfunctions for
4226 -- large sizes ???)
4228 -------------------------
4229 -- Rewrite_Coextension --
4230 -------------------------
4232 procedure Rewrite_Coextension (N : Node_Id) is
4233 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
4234 Temp_Decl : Node_Id;
4236 begin
4237 -- Generate:
4238 -- Cnn : aliased Etyp;
4240 Temp_Decl :=
4241 Make_Object_Declaration (Loc,
4242 Defining_Identifier => Temp_Id,
4243 Aliased_Present => True,
4244 Object_Definition => New_Occurrence_Of (Etyp, Loc));
4246 if Nkind (Expression (N)) = N_Qualified_Expression then
4247 Set_Expression (Temp_Decl, Expression (Expression (N)));
4248 end if;
4250 Insert_Action (N, Temp_Decl);
4251 Rewrite (N,
4252 Make_Attribute_Reference (Loc,
4253 Prefix => New_Occurrence_Of (Temp_Id, Loc),
4254 Attribute_Name => Name_Unrestricted_Access));
4256 Analyze_And_Resolve (N, PtrT);
4257 end Rewrite_Coextension;
4259 ------------------------------
4260 -- Size_In_Storage_Elements --
4261 ------------------------------
4263 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
4264 begin
4265 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4266 -- However, the reason for the existence of this function is
4267 -- to construct a test for sizes too large, which means near the
4268 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4269 -- is that we get overflows when sizes are greater than 2**31.
4271 -- So what we end up doing for array types is to use the expression:
4273 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4275 -- which avoids this problem. All this is a bit bogus, but it does
4276 -- mean we catch common cases of trying to allocate arrays that
4277 -- are too large, and which in the absence of a check results in
4278 -- undetected chaos ???
4280 -- Note in particular that this is a pessimistic estimate in the
4281 -- case of packed array types, where an array element might occupy
4282 -- just a fraction of a storage element???
4284 declare
4285 Len : Node_Id;
4286 Res : Node_Id;
4287 pragma Warnings (Off, Res);
4289 begin
4290 for J in 1 .. Number_Dimensions (E) loop
4291 Len :=
4292 Make_Attribute_Reference (Loc,
4293 Prefix => New_Occurrence_Of (E, Loc),
4294 Attribute_Name => Name_Length,
4295 Expressions => New_List (Make_Integer_Literal (Loc, J)));
4297 if J = 1 then
4298 Res := Len;
4300 else
4301 Res :=
4302 Make_Op_Multiply (Loc,
4303 Left_Opnd => Res,
4304 Right_Opnd => Len);
4305 end if;
4306 end loop;
4308 return
4309 Make_Op_Multiply (Loc,
4310 Left_Opnd => Len,
4311 Right_Opnd =>
4312 Make_Attribute_Reference (Loc,
4313 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
4314 Attribute_Name => Name_Max_Size_In_Storage_Elements));
4315 end;
4316 end Size_In_Storage_Elements;
4318 -- Local variables
4320 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
4321 Desig : Entity_Id;
4322 Nod : Node_Id;
4323 Pool : Entity_Id;
4324 Rel_Typ : Entity_Id;
4325 Temp : Entity_Id;
4327 -- Start of processing for Expand_N_Allocator
4329 begin
4330 -- RM E.2.3(22). We enforce that the expected type of an allocator
4331 -- shall not be a remote access-to-class-wide-limited-private type
4333 -- Why is this being done at expansion time, seems clearly wrong ???
4335 Validate_Remote_Access_To_Class_Wide_Type (N);
4337 -- Processing for anonymous access-to-controlled types. These access
4338 -- types receive a special finalization master which appears in the
4339 -- declarations of the enclosing semantic unit. This expansion is done
4340 -- now to ensure that any additional types generated by this routine or
4341 -- Expand_Allocator_Expression inherit the proper type attributes.
4343 if (Ekind (PtrT) = E_Anonymous_Access_Type
4344 or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT))))
4345 and then Needs_Finalization (Dtyp)
4346 then
4347 -- Detect the allocation of an anonymous controlled object where the
4348 -- type of the context is named. For example:
4350 -- procedure Proc (Ptr : Named_Access_Typ);
4351 -- Proc (new Designated_Typ);
4353 -- Regardless of the anonymous-to-named access type conversion, the
4354 -- lifetime of the object must be associated with the named access
4355 -- type. Use the finalization-related attributes of this type.
4357 if Nkind_In (Parent (N), N_Type_Conversion,
4358 N_Unchecked_Type_Conversion)
4359 and then Ekind_In (Etype (Parent (N)), E_Access_Subtype,
4360 E_Access_Type,
4361 E_General_Access_Type)
4362 then
4363 Rel_Typ := Etype (Parent (N));
4364 else
4365 Rel_Typ := Empty;
4366 end if;
4368 -- Anonymous access-to-controlled types allocate on the global pool.
4369 -- Note that this is a "root type only" attribute.
4371 if No (Associated_Storage_Pool (PtrT)) then
4372 if Present (Rel_Typ) then
4373 Set_Associated_Storage_Pool
4374 (Root_Type (PtrT), Associated_Storage_Pool (Rel_Typ));
4375 else
4376 Set_Associated_Storage_Pool
4377 (Root_Type (PtrT), RTE (RE_Global_Pool_Object));
4378 end if;
4379 end if;
4381 -- The finalization master must be inserted and analyzed as part of
4382 -- the current semantic unit. Note that the master is updated when
4383 -- analysis changes current units. Note that this is a "root type
4384 -- only" attribute.
4386 if Present (Rel_Typ) then
4387 Set_Finalization_Master
4388 (Root_Type (PtrT), Finalization_Master (Rel_Typ));
4389 else
4390 Build_Anonymous_Master (Root_Type (PtrT));
4391 end if;
4392 end if;
4394 -- Set the storage pool and find the appropriate version of Allocate to
4395 -- call. Do not overwrite the storage pool if it is already set, which
4396 -- can happen for build-in-place function returns (see
4397 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4399 if No (Storage_Pool (N)) then
4400 Pool := Associated_Storage_Pool (Root_Type (PtrT));
4402 if Present (Pool) then
4403 Set_Storage_Pool (N, Pool);
4405 if Is_RTE (Pool, RE_SS_Pool) then
4406 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4408 -- In the case of an allocator for a simple storage pool, locate
4409 -- and save a reference to the pool type's Allocate routine.
4411 elsif Present (Get_Rep_Pragma
4412 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4413 then
4414 declare
4415 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
4416 Alloc_Op : Entity_Id;
4417 begin
4418 Alloc_Op := Get_Name_Entity_Id (Name_Allocate);
4419 while Present (Alloc_Op) loop
4420 if Scope (Alloc_Op) = Scope (Pool_Type)
4421 and then Present (First_Formal (Alloc_Op))
4422 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4423 then
4424 Set_Procedure_To_Call (N, Alloc_Op);
4425 exit;
4426 else
4427 Alloc_Op := Homonym (Alloc_Op);
4428 end if;
4429 end loop;
4430 end;
4432 elsif Is_Class_Wide_Type (Etype (Pool)) then
4433 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
4435 else
4436 Set_Procedure_To_Call (N,
4437 Find_Prim_Op (Etype (Pool), Name_Allocate));
4438 end if;
4439 end if;
4440 end if;
4442 -- Under certain circumstances we can replace an allocator by an access
4443 -- to statically allocated storage. The conditions, as noted in AARM
4444 -- 3.10 (10c) are as follows:
4446 -- Size and initial value is known at compile time
4447 -- Access type is access-to-constant
4449 -- The allocator is not part of a constraint on a record component,
4450 -- because in that case the inserted actions are delayed until the
4451 -- record declaration is fully analyzed, which is too late for the
4452 -- analysis of the rewritten allocator.
4454 if Is_Access_Constant (PtrT)
4455 and then Nkind (Expression (N)) = N_Qualified_Expression
4456 and then Compile_Time_Known_Value (Expression (Expression (N)))
4457 and then Size_Known_At_Compile_Time
4458 (Etype (Expression (Expression (N))))
4459 and then not Is_Record_Type (Current_Scope)
4460 then
4461 -- Here we can do the optimization. For the allocator
4463 -- new x'(y)
4465 -- We insert an object declaration
4467 -- Tnn : aliased x := y;
4469 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4470 -- marked as requiring static allocation.
4472 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
4473 Desig := Subtype_Mark (Expression (N));
4475 -- If context is constrained, use constrained subtype directly,
4476 -- so that the constant is not labelled as having a nominally
4477 -- unconstrained subtype.
4479 if Entity (Desig) = Base_Type (Dtyp) then
4480 Desig := New_Occurrence_Of (Dtyp, Loc);
4481 end if;
4483 Insert_Action (N,
4484 Make_Object_Declaration (Loc,
4485 Defining_Identifier => Temp,
4486 Aliased_Present => True,
4487 Constant_Present => Is_Access_Constant (PtrT),
4488 Object_Definition => Desig,
4489 Expression => Expression (Expression (N))));
4491 Rewrite (N,
4492 Make_Attribute_Reference (Loc,
4493 Prefix => New_Occurrence_Of (Temp, Loc),
4494 Attribute_Name => Name_Unrestricted_Access));
4496 Analyze_And_Resolve (N, PtrT);
4498 -- We set the variable as statically allocated, since we don't want
4499 -- it going on the stack of the current procedure.
4501 Set_Is_Statically_Allocated (Temp);
4502 return;
4503 end if;
4505 -- Same if the allocator is an access discriminant for a local object:
4506 -- instead of an allocator we create a local value and constrain the
4507 -- enclosing object with the corresponding access attribute.
4509 if Is_Static_Coextension (N) then
4510 Rewrite_Coextension (N);
4511 return;
4512 end if;
4514 -- Check for size too large, we do this because the back end misses
4515 -- proper checks here and can generate rubbish allocation calls when
4516 -- we are near the limit. We only do this for the 32-bit address case
4517 -- since that is from a practical point of view where we see a problem.
4519 if System_Address_Size = 32
4520 and then not Storage_Checks_Suppressed (PtrT)
4521 and then not Storage_Checks_Suppressed (Dtyp)
4522 and then not Storage_Checks_Suppressed (Etyp)
4523 then
4524 -- The check we want to generate should look like
4526 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4527 -- raise Storage_Error;
4528 -- end if;
4530 -- where 3.5 gigabytes is a constant large enough to accommodate any
4531 -- reasonable request for. But we can't do it this way because at
4532 -- least at the moment we don't compute this attribute right, and
4533 -- can silently give wrong results when the result gets large. Since
4534 -- this is all about large results, that's bad, so instead we only
4535 -- apply the check for constrained arrays, and manually compute the
4536 -- value of the attribute ???
4538 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
4539 Insert_Action (N,
4540 Make_Raise_Storage_Error (Loc,
4541 Condition =>
4542 Make_Op_Gt (Loc,
4543 Left_Opnd => Size_In_Storage_Elements (Etyp),
4544 Right_Opnd =>
4545 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
4546 Reason => SE_Object_Too_Large));
4547 end if;
4548 end if;
4550 -- If no storage pool has been specified and we have the restriction
4551 -- No_Standard_Allocators_After_Elaboration is present, then generate
4552 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4554 if Nkind (N) = N_Allocator
4555 and then No (Storage_Pool (N))
4556 and then Restriction_Active (No_Standard_Allocators_After_Elaboration)
4557 then
4558 Insert_Action (N,
4559 Make_Procedure_Call_Statement (Loc,
4560 Name =>
4561 New_Occurrence_Of (RTE (RE_Check_Standard_Allocator), Loc)));
4562 end if;
4564 -- Handle case of qualified expression (other than optimization above)
4565 -- First apply constraint checks, because the bounds or discriminants
4566 -- in the aggregate might not match the subtype mark in the allocator.
4568 if Nkind (Expression (N)) = N_Qualified_Expression then
4569 declare
4570 Exp : constant Node_Id := Expression (Expression (N));
4571 Typ : constant Entity_Id := Etype (Expression (N));
4573 begin
4574 Apply_Constraint_Check (Exp, Typ);
4575 Apply_Predicate_Check (Exp, Typ);
4576 end;
4578 Expand_Allocator_Expression (N);
4579 return;
4580 end if;
4582 -- If the allocator is for a type which requires initialization, and
4583 -- there is no initial value (i.e. operand is a subtype indication
4584 -- rather than a qualified expression), then we must generate a call to
4585 -- the initialization routine using an expressions action node:
4587 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4589 -- Here ptr_T is the pointer type for the allocator, and T is the
4590 -- subtype of the allocator. A special case arises if the designated
4591 -- type of the access type is a task or contains tasks. In this case
4592 -- the call to Init (Temp.all ...) is replaced by code that ensures
4593 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4594 -- for details). In addition, if the type T is a task type, then the
4595 -- first argument to Init must be converted to the task record type.
4597 declare
4598 T : constant Entity_Id := Etype (Expression (N));
4599 Args : List_Id;
4600 Decls : List_Id;
4601 Decl : Node_Id;
4602 Discr : Elmt_Id;
4603 Init : Entity_Id;
4604 Init_Arg1 : Node_Id;
4605 Init_Call : Node_Id;
4606 Temp_Decl : Node_Id;
4607 Temp_Type : Entity_Id;
4609 begin
4610 if No_Initialization (N) then
4612 -- Even though this might be a simple allocation, create a custom
4613 -- Allocate if the context requires it.
4615 if Present (Finalization_Master (PtrT)) then
4616 Build_Allocate_Deallocate_Proc
4617 (N => N,
4618 Is_Allocate => True);
4619 end if;
4621 -- Optimize the default allocation of an array object when pragma
4622 -- Initialize_Scalars or Normalize_Scalars is in effect. Construct an
4623 -- in-place initialization aggregate which may be convert into a fast
4624 -- memset by the backend.
4626 elsif Init_Or_Norm_Scalars
4627 and then Is_Array_Type (T)
4629 -- The array must lack atomic components because they are treated
4630 -- as non-static, and as a result the backend will not initialize
4631 -- the memory in one go.
4633 and then not Has_Atomic_Components (T)
4635 -- The array must not be packed because the invalid values in
4636 -- System.Scalar_Values are multiples of Storage_Unit.
4638 and then not Is_Packed (T)
4640 -- The array must have static non-empty ranges, otherwise the
4641 -- backend cannot initialize the memory in one go.
4643 and then Has_Static_Non_Empty_Array_Bounds (T)
4645 -- The optimization is only relevant for arrays of scalar types
4647 and then Is_Scalar_Type (Component_Type (T))
4649 -- Similar to regular array initialization using a type init proc,
4650 -- predicate checks are not performed because the initialization
4651 -- values are intentionally invalid, and may violate the predicate.
4653 and then not Has_Predicates (Component_Type (T))
4655 -- The component type must have a single initialization value
4657 and then Needs_Simple_Initialization
4658 (Typ => Component_Type (T),
4659 Consider_IS => True)
4660 then
4661 Set_Analyzed (N);
4662 Temp := Make_Temporary (Loc, 'P');
4664 -- Generate:
4665 -- Temp : Ptr_Typ := new ...;
4667 Insert_Action
4668 (Assoc_Node => N,
4669 Ins_Action =>
4670 Make_Object_Declaration (Loc,
4671 Defining_Identifier => Temp,
4672 Object_Definition => New_Occurrence_Of (PtrT, Loc),
4673 Expression => Relocate_Node (N)),
4674 Suppress => All_Checks);
4676 -- Generate:
4677 -- Temp.all := (others => ...);
4679 Insert_Action
4680 (Assoc_Node => N,
4681 Ins_Action =>
4682 Make_Assignment_Statement (Loc,
4683 Name =>
4684 Make_Explicit_Dereference (Loc,
4685 Prefix => New_Occurrence_Of (Temp, Loc)),
4686 Expression =>
4687 Get_Simple_Init_Val
4688 (Typ => T,
4689 N => N,
4690 Size => Esize (Component_Type (T)))),
4691 Suppress => All_Checks);
4693 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4694 Analyze_And_Resolve (N, PtrT);
4696 -- Case of no initialization procedure present
4698 elsif not Has_Non_Null_Base_Init_Proc (T) then
4700 -- Case of simple initialization required
4702 if Needs_Simple_Initialization (T) then
4703 Check_Restriction (No_Default_Initialization, N);
4704 Rewrite (Expression (N),
4705 Make_Qualified_Expression (Loc,
4706 Subtype_Mark => New_Occurrence_Of (T, Loc),
4707 Expression => Get_Simple_Init_Val (T, N)));
4709 Analyze_And_Resolve (Expression (Expression (N)), T);
4710 Analyze_And_Resolve (Expression (N), T);
4711 Set_Paren_Count (Expression (Expression (N)), 1);
4712 Expand_N_Allocator (N);
4714 -- No initialization required
4716 else
4717 Build_Allocate_Deallocate_Proc
4718 (N => N,
4719 Is_Allocate => True);
4720 end if;
4722 -- Case of initialization procedure present, must be called
4724 else
4725 Check_Restriction (No_Default_Initialization, N);
4727 if not Restriction_Active (No_Default_Initialization) then
4728 Init := Base_Init_Proc (T);
4729 Nod := N;
4730 Temp := Make_Temporary (Loc, 'P');
4732 -- Construct argument list for the initialization routine call
4734 Init_Arg1 :=
4735 Make_Explicit_Dereference (Loc,
4736 Prefix =>
4737 New_Occurrence_Of (Temp, Loc));
4739 Set_Assignment_OK (Init_Arg1);
4740 Temp_Type := PtrT;
4742 -- The initialization procedure expects a specific type. if the
4743 -- context is access to class wide, indicate that the object
4744 -- being allocated has the right specific type.
4746 if Is_Class_Wide_Type (Dtyp) then
4747 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
4748 end if;
4750 -- If designated type is a concurrent type or if it is private
4751 -- type whose definition is a concurrent type, the first
4752 -- argument in the Init routine has to be unchecked conversion
4753 -- to the corresponding record type. If the designated type is
4754 -- a derived type, also convert the argument to its root type.
4756 if Is_Concurrent_Type (T) then
4757 Init_Arg1 :=
4758 Unchecked_Convert_To (
4759 Corresponding_Record_Type (T), Init_Arg1);
4761 elsif Is_Private_Type (T)
4762 and then Present (Full_View (T))
4763 and then Is_Concurrent_Type (Full_View (T))
4764 then
4765 Init_Arg1 :=
4766 Unchecked_Convert_To
4767 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
4769 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
4770 declare
4771 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
4773 begin
4774 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
4775 Set_Etype (Init_Arg1, Ftyp);
4776 end;
4777 end if;
4779 Args := New_List (Init_Arg1);
4781 -- For the task case, pass the Master_Id of the access type as
4782 -- the value of the _Master parameter, and _Chain as the value
4783 -- of the _Chain parameter (_Chain will be defined as part of
4784 -- the generated code for the allocator).
4786 -- In Ada 2005, the context may be a function that returns an
4787 -- anonymous access type. In that case the Master_Id has been
4788 -- created when expanding the function declaration.
4790 if Has_Task (T) then
4791 if No (Master_Id (Base_Type (PtrT))) then
4793 -- The designated type was an incomplete type, and the
4794 -- access type did not get expanded. Salvage it now.
4796 if not Restriction_Active (No_Task_Hierarchy) then
4797 if Present (Parent (Base_Type (PtrT))) then
4798 Expand_N_Full_Type_Declaration
4799 (Parent (Base_Type (PtrT)));
4801 -- The only other possibility is an itype. For this
4802 -- case, the master must exist in the context. This is
4803 -- the case when the allocator initializes an access
4804 -- component in an init-proc.
4806 else
4807 pragma Assert (Is_Itype (PtrT));
4808 Build_Master_Renaming (PtrT, N);
4809 end if;
4810 end if;
4811 end if;
4813 -- If the context of the allocator is a declaration or an
4814 -- assignment, we can generate a meaningful image for it,
4815 -- even though subsequent assignments might remove the
4816 -- connection between task and entity. We build this image
4817 -- when the left-hand side is a simple variable, a simple
4818 -- indexed assignment or a simple selected component.
4820 if Nkind (Parent (N)) = N_Assignment_Statement then
4821 declare
4822 Nam : constant Node_Id := Name (Parent (N));
4824 begin
4825 if Is_Entity_Name (Nam) then
4826 Decls :=
4827 Build_Task_Image_Decls
4828 (Loc,
4829 New_Occurrence_Of
4830 (Entity (Nam), Sloc (Nam)), T);
4832 elsif Nkind_In (Nam, N_Indexed_Component,
4833 N_Selected_Component)
4834 and then Is_Entity_Name (Prefix (Nam))
4835 then
4836 Decls :=
4837 Build_Task_Image_Decls
4838 (Loc, Nam, Etype (Prefix (Nam)));
4839 else
4840 Decls := Build_Task_Image_Decls (Loc, T, T);
4841 end if;
4842 end;
4844 elsif Nkind (Parent (N)) = N_Object_Declaration then
4845 Decls :=
4846 Build_Task_Image_Decls
4847 (Loc, Defining_Identifier (Parent (N)), T);
4849 else
4850 Decls := Build_Task_Image_Decls (Loc, T, T);
4851 end if;
4853 if Restriction_Active (No_Task_Hierarchy) then
4854 Append_To (Args,
4855 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
4856 else
4857 Append_To (Args,
4858 New_Occurrence_Of
4859 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
4860 end if;
4862 Append_To (Args, Make_Identifier (Loc, Name_uChain));
4864 Decl := Last (Decls);
4865 Append_To (Args,
4866 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
4868 -- Has_Task is false, Decls not used
4870 else
4871 Decls := No_List;
4872 end if;
4874 -- Add discriminants if discriminated type
4876 declare
4877 Dis : Boolean := False;
4878 Typ : Entity_Id := Empty;
4880 begin
4881 if Has_Discriminants (T) then
4882 Dis := True;
4883 Typ := T;
4885 -- Type may be a private type with no visible discriminants
4886 -- in which case check full view if in scope, or the
4887 -- underlying_full_view if dealing with a type whose full
4888 -- view may be derived from a private type whose own full
4889 -- view has discriminants.
4891 elsif Is_Private_Type (T) then
4892 if Present (Full_View (T))
4893 and then Has_Discriminants (Full_View (T))
4894 then
4895 Dis := True;
4896 Typ := Full_View (T);
4898 elsif Present (Underlying_Full_View (T))
4899 and then Has_Discriminants (Underlying_Full_View (T))
4900 then
4901 Dis := True;
4902 Typ := Underlying_Full_View (T);
4903 end if;
4904 end if;
4906 if Dis then
4908 -- If the allocated object will be constrained by the
4909 -- default values for discriminants, then build a subtype
4910 -- with those defaults, and change the allocated subtype
4911 -- to that. Note that this happens in fewer cases in Ada
4912 -- 2005 (AI-363).
4914 if not Is_Constrained (Typ)
4915 and then Present (Discriminant_Default_Value
4916 (First_Discriminant (Typ)))
4917 and then (Ada_Version < Ada_2005
4918 or else not
4919 Object_Type_Has_Constrained_Partial_View
4920 (Typ, Current_Scope))
4921 then
4922 Typ := Build_Default_Subtype (Typ, N);
4923 Set_Expression (N, New_Occurrence_Of (Typ, Loc));
4924 end if;
4926 Discr := First_Elmt (Discriminant_Constraint (Typ));
4927 while Present (Discr) loop
4928 Nod := Node (Discr);
4929 Append (New_Copy_Tree (Node (Discr)), Args);
4931 -- AI-416: when the discriminant constraint is an
4932 -- anonymous access type make sure an accessibility
4933 -- check is inserted if necessary (3.10.2(22.q/2))
4935 if Ada_Version >= Ada_2005
4936 and then
4937 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
4938 then
4939 Apply_Accessibility_Check
4940 (Nod, Typ, Insert_Node => Nod);
4941 end if;
4943 Next_Elmt (Discr);
4944 end loop;
4945 end if;
4946 end;
4948 -- We set the allocator as analyzed so that when we analyze
4949 -- the if expression node, we do not get an unwanted recursive
4950 -- expansion of the allocator expression.
4952 Set_Analyzed (N, True);
4953 Nod := Relocate_Node (N);
4955 -- Here is the transformation:
4956 -- input: new Ctrl_Typ
4957 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4958 -- Ctrl_TypIP (Temp.all, ...);
4959 -- [Deep_]Initialize (Temp.all);
4961 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4962 -- is the subtype of the allocator.
4964 Temp_Decl :=
4965 Make_Object_Declaration (Loc,
4966 Defining_Identifier => Temp,
4967 Constant_Present => True,
4968 Object_Definition => New_Occurrence_Of (Temp_Type, Loc),
4969 Expression => Nod);
4971 Set_Assignment_OK (Temp_Decl);
4972 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
4974 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
4976 -- If the designated type is a task type or contains tasks,
4977 -- create block to activate created tasks, and insert
4978 -- declaration for Task_Image variable ahead of call.
4980 if Has_Task (T) then
4981 declare
4982 L : constant List_Id := New_List;
4983 Blk : Node_Id;
4984 begin
4985 Build_Task_Allocate_Block (L, Nod, Args);
4986 Blk := Last (L);
4987 Insert_List_Before (First (Declarations (Blk)), Decls);
4988 Insert_Actions (N, L);
4989 end;
4991 else
4992 Insert_Action (N,
4993 Make_Procedure_Call_Statement (Loc,
4994 Name => New_Occurrence_Of (Init, Loc),
4995 Parameter_Associations => Args));
4996 end if;
4998 if Needs_Finalization (T) then
5000 -- Generate:
5001 -- [Deep_]Initialize (Init_Arg1);
5003 Init_Call :=
5004 Make_Init_Call
5005 (Obj_Ref => New_Copy_Tree (Init_Arg1),
5006 Typ => T);
5008 -- Guard against a missing [Deep_]Initialize when the
5009 -- designated type was not properly frozen.
5011 if Present (Init_Call) then
5012 Insert_Action (N, Init_Call);
5013 end if;
5014 end if;
5016 Rewrite (N, New_Occurrence_Of (Temp, Loc));
5017 Analyze_And_Resolve (N, PtrT);
5018 end if;
5019 end if;
5020 end;
5022 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
5023 -- object that has been rewritten as a reference, we displace "this"
5024 -- to reference properly its secondary dispatch table.
5026 if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then
5027 Displace_Allocator_Pointer (N);
5028 end if;
5030 exception
5031 when RE_Not_Available =>
5032 return;
5033 end Expand_N_Allocator;
5035 -----------------------
5036 -- Expand_N_And_Then --
5037 -----------------------
5039 procedure Expand_N_And_Then (N : Node_Id)
5040 renames Expand_Short_Circuit_Operator;
5042 ------------------------------
5043 -- Expand_N_Case_Expression --
5044 ------------------------------
5046 procedure Expand_N_Case_Expression (N : Node_Id) is
5048 function Is_Copy_Type (Typ : Entity_Id) return Boolean;
5049 -- Return True if we can copy objects of this type when expanding a case
5050 -- expression.
5052 ------------------
5053 -- Is_Copy_Type --
5054 ------------------
5056 function Is_Copy_Type (Typ : Entity_Id) return Boolean is
5057 begin
5058 -- If Minimize_Expression_With_Actions is True, we can afford to copy
5059 -- large objects, as long as they are constrained and not limited.
5061 return
5062 Is_Elementary_Type (Underlying_Type (Typ))
5063 or else
5064 (Minimize_Expression_With_Actions
5065 and then Is_Constrained (Underlying_Type (Typ))
5066 and then not Is_Limited_View (Underlying_Type (Typ)));
5067 end Is_Copy_Type;
5069 -- Local variables
5071 Loc : constant Source_Ptr := Sloc (N);
5072 Par : constant Node_Id := Parent (N);
5073 Typ : constant Entity_Id := Etype (N);
5075 Acts : List_Id;
5076 Alt : Node_Id;
5077 Case_Stmt : Node_Id;
5078 Decl : Node_Id;
5079 Expr : Node_Id;
5080 Target : Entity_Id;
5081 Target_Typ : Entity_Id;
5083 In_Predicate : Boolean := False;
5084 -- Flag set when the case expression appears within a predicate
5086 Optimize_Return_Stmt : Boolean := False;
5087 -- Flag set when the case expression can be optimized in the context of
5088 -- a simple return statement.
5090 -- Start of processing for Expand_N_Case_Expression
5092 begin
5093 -- Check for MINIMIZED/ELIMINATED overflow mode
5095 if Minimized_Eliminated_Overflow_Check (N) then
5096 Apply_Arithmetic_Overflow_Check (N);
5097 return;
5098 end if;
5100 -- If the case expression is a predicate specification, and the type
5101 -- to which it applies has a static predicate aspect, do not expand,
5102 -- because it will be converted to the proper predicate form later.
5104 if Ekind_In (Current_Scope, E_Function, E_Procedure)
5105 and then Is_Predicate_Function (Current_Scope)
5106 then
5107 In_Predicate := True;
5109 if Has_Static_Predicate_Aspect (Etype (First_Entity (Current_Scope)))
5110 then
5111 return;
5112 end if;
5113 end if;
5115 -- When the type of the case expression is elementary, expand
5117 -- (case X is when A => AX, when B => BX ...)
5119 -- into
5121 -- do
5122 -- Target : Typ;
5123 -- case X is
5124 -- when A =>
5125 -- Target := AX;
5126 -- when B =>
5127 -- Target := BX;
5128 -- ...
5129 -- end case;
5130 -- in Target end;
5132 -- In all other cases expand into
5134 -- do
5135 -- type Ptr_Typ is access all Typ;
5136 -- Target : Ptr_Typ;
5137 -- case X is
5138 -- when A =>
5139 -- Target := AX'Unrestricted_Access;
5140 -- when B =>
5141 -- Target := BX'Unrestricted_Access;
5142 -- ...
5143 -- end case;
5144 -- in Target.all end;
5146 -- This approach avoids extra copies of potentially large objects. It
5147 -- also allows handling of values of limited or unconstrained types.
5148 -- Note that we do the copy also for constrained, nonlimited types
5149 -- when minimizing expressions with actions (e.g. when generating C
5150 -- code) since it allows us to do the optimization below in more cases.
5152 -- Small optimization: when the case expression appears in the context
5153 -- of a simple return statement, expand into
5155 -- case X is
5156 -- when A =>
5157 -- return AX;
5158 -- when B =>
5159 -- return BX;
5160 -- ...
5161 -- end case;
5163 Case_Stmt :=
5164 Make_Case_Statement (Loc,
5165 Expression => Expression (N),
5166 Alternatives => New_List);
5168 -- Preserve the original context for which the case statement is being
5169 -- generated. This is needed by the finalization machinery to prevent
5170 -- the premature finalization of controlled objects found within the
5171 -- case statement.
5173 Set_From_Conditional_Expression (Case_Stmt);
5174 Acts := New_List;
5176 -- Scalar/Copy case
5178 if Is_Copy_Type (Typ) then
5179 Target_Typ := Typ;
5181 -- ??? Do not perform the optimization when the return statement is
5182 -- within a predicate function, as this causes spurious errors. Could
5183 -- this be a possible mismatch in handling this case somewhere else
5184 -- in semantic analysis?
5186 Optimize_Return_Stmt :=
5187 Nkind (Par) = N_Simple_Return_Statement and then not In_Predicate;
5189 -- Otherwise create an access type to handle the general case using
5190 -- 'Unrestricted_Access.
5192 -- Generate:
5193 -- type Ptr_Typ is access all Typ;
5195 else
5196 if Generate_C_Code then
5198 -- We cannot ensure that correct C code will be generated if any
5199 -- temporary is created down the line (to e.g. handle checks or
5200 -- capture values) since we might end up with dangling references
5201 -- to local variables, so better be safe and reject the construct.
5203 Error_Msg_N
5204 ("case expression too complex, use case statement instead", N);
5205 end if;
5207 Target_Typ := Make_Temporary (Loc, 'P');
5209 Append_To (Acts,
5210 Make_Full_Type_Declaration (Loc,
5211 Defining_Identifier => Target_Typ,
5212 Type_Definition =>
5213 Make_Access_To_Object_Definition (Loc,
5214 All_Present => True,
5215 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
5216 end if;
5218 -- Create the declaration of the target which captures the value of the
5219 -- expression.
5221 -- Generate:
5222 -- Target : [Ptr_]Typ;
5224 if not Optimize_Return_Stmt then
5225 Target := Make_Temporary (Loc, 'T');
5227 Decl :=
5228 Make_Object_Declaration (Loc,
5229 Defining_Identifier => Target,
5230 Object_Definition => New_Occurrence_Of (Target_Typ, Loc));
5231 Set_No_Initialization (Decl);
5233 Append_To (Acts, Decl);
5234 end if;
5236 -- Process the alternatives
5238 Alt := First (Alternatives (N));
5239 while Present (Alt) loop
5240 declare
5241 Alt_Expr : Node_Id := Expression (Alt);
5242 Alt_Loc : constant Source_Ptr := Sloc (Alt_Expr);
5243 Stmts : List_Id;
5245 begin
5246 -- Take the unrestricted access of the expression value for non-
5247 -- scalar types. This approach avoids big copies and covers the
5248 -- limited and unconstrained cases.
5250 -- Generate:
5251 -- AX'Unrestricted_Access
5253 if not Is_Copy_Type (Typ) then
5254 Alt_Expr :=
5255 Make_Attribute_Reference (Alt_Loc,
5256 Prefix => Relocate_Node (Alt_Expr),
5257 Attribute_Name => Name_Unrestricted_Access);
5258 end if;
5260 -- Generate:
5261 -- return AX['Unrestricted_Access];
5263 if Optimize_Return_Stmt then
5264 Stmts := New_List (
5265 Make_Simple_Return_Statement (Alt_Loc,
5266 Expression => Alt_Expr));
5268 -- Generate:
5269 -- Target := AX['Unrestricted_Access];
5271 else
5272 Stmts := New_List (
5273 Make_Assignment_Statement (Alt_Loc,
5274 Name => New_Occurrence_Of (Target, Loc),
5275 Expression => Alt_Expr));
5276 end if;
5278 -- Propagate declarations inserted in the node by Insert_Actions
5279 -- (for example, temporaries generated to remove side effects).
5280 -- These actions must remain attached to the alternative, given
5281 -- that they are generated by the corresponding expression.
5283 if Present (Actions (Alt)) then
5284 Prepend_List (Actions (Alt), Stmts);
5285 end if;
5287 -- Finalize any transient objects on exit from the alternative.
5288 -- This is done only in the return optimization case because
5289 -- otherwise the case expression is converted into an expression
5290 -- with actions which already contains this form of processing.
5292 if Optimize_Return_Stmt then
5293 Process_If_Case_Statements (N, Stmts);
5294 end if;
5296 Append_To
5297 (Alternatives (Case_Stmt),
5298 Make_Case_Statement_Alternative (Sloc (Alt),
5299 Discrete_Choices => Discrete_Choices (Alt),
5300 Statements => Stmts));
5301 end;
5303 Next (Alt);
5304 end loop;
5306 -- Rewrite the parent return statement as a case statement
5308 if Optimize_Return_Stmt then
5309 Rewrite (Par, Case_Stmt);
5310 Analyze (Par);
5312 -- Otherwise convert the case expression into an expression with actions
5314 else
5315 Append_To (Acts, Case_Stmt);
5317 if Is_Copy_Type (Typ) then
5318 Expr := New_Occurrence_Of (Target, Loc);
5320 else
5321 Expr :=
5322 Make_Explicit_Dereference (Loc,
5323 Prefix => New_Occurrence_Of (Target, Loc));
5324 end if;
5326 -- Generate:
5327 -- do
5328 -- ...
5329 -- in Target[.all] end;
5331 Rewrite (N,
5332 Make_Expression_With_Actions (Loc,
5333 Expression => Expr,
5334 Actions => Acts));
5336 Analyze_And_Resolve (N, Typ);
5337 end if;
5338 end Expand_N_Case_Expression;
5340 -----------------------------------
5341 -- Expand_N_Explicit_Dereference --
5342 -----------------------------------
5344 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
5345 begin
5346 -- Insert explicit dereference call for the checked storage pool case
5348 Insert_Dereference_Action (Prefix (N));
5350 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5351 -- we set the atomic sync flag.
5353 if Is_Atomic (Etype (N))
5354 and then not Atomic_Synchronization_Disabled (Etype (N))
5355 then
5356 Activate_Atomic_Synchronization (N);
5357 end if;
5358 end Expand_N_Explicit_Dereference;
5360 --------------------------------------
5361 -- Expand_N_Expression_With_Actions --
5362 --------------------------------------
5364 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
5365 Acts : constant List_Id := Actions (N);
5367 procedure Force_Boolean_Evaluation (Expr : Node_Id);
5368 -- Force the evaluation of Boolean expression Expr
5370 function Process_Action (Act : Node_Id) return Traverse_Result;
5371 -- Inspect and process a single action of an expression_with_actions for
5372 -- transient objects. If such objects are found, the routine generates
5373 -- code to clean them up when the context of the expression is evaluated
5374 -- or elaborated.
5376 ------------------------------
5377 -- Force_Boolean_Evaluation --
5378 ------------------------------
5380 procedure Force_Boolean_Evaluation (Expr : Node_Id) is
5381 Loc : constant Source_Ptr := Sloc (N);
5382 Flag_Decl : Node_Id;
5383 Flag_Id : Entity_Id;
5385 begin
5386 -- Relocate the expression to the actions list by capturing its value
5387 -- in a Boolean flag. Generate:
5388 -- Flag : constant Boolean := Expr;
5390 Flag_Id := Make_Temporary (Loc, 'F');
5392 Flag_Decl :=
5393 Make_Object_Declaration (Loc,
5394 Defining_Identifier => Flag_Id,
5395 Constant_Present => True,
5396 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
5397 Expression => Relocate_Node (Expr));
5399 Append (Flag_Decl, Acts);
5400 Analyze (Flag_Decl);
5402 -- Replace the expression with a reference to the flag
5404 Rewrite (Expression (N), New_Occurrence_Of (Flag_Id, Loc));
5405 Analyze (Expression (N));
5406 end Force_Boolean_Evaluation;
5408 --------------------
5409 -- Process_Action --
5410 --------------------
5412 function Process_Action (Act : Node_Id) return Traverse_Result is
5413 begin
5414 if Nkind (Act) = N_Object_Declaration
5415 and then Is_Finalizable_Transient (Act, N)
5416 then
5417 Process_Transient_In_Expression (Act, N, Acts);
5418 return Skip;
5420 -- Avoid processing temporary function results multiple times when
5421 -- dealing with nested expression_with_actions.
5423 elsif Nkind (Act) = N_Expression_With_Actions then
5424 return Abandon;
5426 -- Do not process temporary function results in loops. This is done
5427 -- by Expand_N_Loop_Statement and Build_Finalizer.
5429 elsif Nkind (Act) = N_Loop_Statement then
5430 return Abandon;
5431 end if;
5433 return OK;
5434 end Process_Action;
5436 procedure Process_Single_Action is new Traverse_Proc (Process_Action);
5438 -- Local variables
5440 Act : Node_Id;
5442 -- Start of processing for Expand_N_Expression_With_Actions
5444 begin
5445 -- Do not evaluate the expression when it denotes an entity because the
5446 -- expression_with_actions node will be replaced by the reference.
5448 if Is_Entity_Name (Expression (N)) then
5449 null;
5451 -- Do not evaluate the expression when there are no actions because the
5452 -- expression_with_actions node will be replaced by the expression.
5454 elsif No (Acts) or else Is_Empty_List (Acts) then
5455 null;
5457 -- Force the evaluation of the expression by capturing its value in a
5458 -- temporary. This ensures that aliases of transient objects do not leak
5459 -- to the expression of the expression_with_actions node:
5461 -- do
5462 -- Trans_Id : Ctrl_Typ := ...;
5463 -- Alias : ... := Trans_Id;
5464 -- in ... Alias ... end;
5466 -- In the example above, Trans_Id cannot be finalized at the end of the
5467 -- actions list because this may affect the alias and the final value of
5468 -- the expression_with_actions. Forcing the evaluation encapsulates the
5469 -- reference to the Alias within the actions list:
5471 -- do
5472 -- Trans_Id : Ctrl_Typ := ...;
5473 -- Alias : ... := Trans_Id;
5474 -- Val : constant Boolean := ... Alias ...;
5475 -- <finalize Trans_Id>
5476 -- in Val end;
5478 -- Once this transformation is performed, it is safe to finalize the
5479 -- transient object at the end of the actions list.
5481 -- Note that Force_Evaluation does not remove side effects in operators
5482 -- because it assumes that all operands are evaluated and side effect
5483 -- free. This is not the case when an operand depends implicitly on the
5484 -- transient object through the use of access types.
5486 elsif Is_Boolean_Type (Etype (Expression (N))) then
5487 Force_Boolean_Evaluation (Expression (N));
5489 -- The expression of an expression_with_actions node may not necessarily
5490 -- be Boolean when the node appears in an if expression. In this case do
5491 -- the usual forced evaluation to encapsulate potential aliasing.
5493 else
5494 Force_Evaluation (Expression (N));
5495 end if;
5497 -- Process all transient objects found within the actions of the EWA
5498 -- node.
5500 Act := First (Acts);
5501 while Present (Act) loop
5502 Process_Single_Action (Act);
5503 Next (Act);
5504 end loop;
5506 -- Deal with case where there are no actions. In this case we simply
5507 -- rewrite the node with its expression since we don't need the actions
5508 -- and the specification of this node does not allow a null action list.
5510 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5511 -- the expanded tree and relying on being able to retrieve the original
5512 -- tree in cases like this. This raises a whole lot of issues of whether
5513 -- we have problems elsewhere, which will be addressed in the future???
5515 if Is_Empty_List (Acts) then
5516 Rewrite (N, Relocate_Node (Expression (N)));
5517 end if;
5518 end Expand_N_Expression_With_Actions;
5520 ----------------------------
5521 -- Expand_N_If_Expression --
5522 ----------------------------
5524 -- Deal with limited types and condition actions
5526 procedure Expand_N_If_Expression (N : Node_Id) is
5527 Cond : constant Node_Id := First (Expressions (N));
5528 Loc : constant Source_Ptr := Sloc (N);
5529 Thenx : constant Node_Id := Next (Cond);
5530 Elsex : constant Node_Id := Next (Thenx);
5531 Typ : constant Entity_Id := Etype (N);
5533 Actions : List_Id;
5534 Decl : Node_Id;
5535 Expr : Node_Id;
5536 New_If : Node_Id;
5537 New_N : Node_Id;
5539 begin
5540 -- Check for MINIMIZED/ELIMINATED overflow mode
5542 if Minimized_Eliminated_Overflow_Check (N) then
5543 Apply_Arithmetic_Overflow_Check (N);
5544 return;
5545 end if;
5547 -- Fold at compile time if condition known. We have already folded
5548 -- static if expressions, but it is possible to fold any case in which
5549 -- the condition is known at compile time, even though the result is
5550 -- non-static.
5552 -- Note that we don't do the fold of such cases in Sem_Elab because
5553 -- it can cause infinite loops with the expander adding a conditional
5554 -- expression, and Sem_Elab circuitry removing it repeatedly.
5556 if Compile_Time_Known_Value (Cond) then
5557 declare
5558 function Fold_Known_Value (Cond : Node_Id) return Boolean;
5559 -- Fold at compile time. Assumes condition known. Return True if
5560 -- folding occurred, meaning we're done.
5562 ----------------------
5563 -- Fold_Known_Value --
5564 ----------------------
5566 function Fold_Known_Value (Cond : Node_Id) return Boolean is
5567 begin
5568 if Is_True (Expr_Value (Cond)) then
5569 Expr := Thenx;
5570 Actions := Then_Actions (N);
5571 else
5572 Expr := Elsex;
5573 Actions := Else_Actions (N);
5574 end if;
5576 Remove (Expr);
5578 if Present (Actions) then
5580 -- To minimize the use of Expression_With_Actions, just skip
5581 -- the optimization as it is not critical for correctness.
5583 if Minimize_Expression_With_Actions then
5584 return False;
5585 end if;
5587 Rewrite (N,
5588 Make_Expression_With_Actions (Loc,
5589 Expression => Relocate_Node (Expr),
5590 Actions => Actions));
5591 Analyze_And_Resolve (N, Typ);
5593 else
5594 Rewrite (N, Relocate_Node (Expr));
5595 end if;
5597 -- Note that the result is never static (legitimate cases of
5598 -- static if expressions were folded in Sem_Eval).
5600 Set_Is_Static_Expression (N, False);
5601 return True;
5602 end Fold_Known_Value;
5604 begin
5605 if Fold_Known_Value (Cond) then
5606 return;
5607 end if;
5608 end;
5609 end if;
5611 -- If the type is limited, and the back end does not handle limited
5612 -- types, then we expand as follows to avoid the possibility of
5613 -- improper copying.
5615 -- type Ptr is access all Typ;
5616 -- Cnn : Ptr;
5617 -- if cond then
5618 -- <<then actions>>
5619 -- Cnn := then-expr'Unrestricted_Access;
5620 -- else
5621 -- <<else actions>>
5622 -- Cnn := else-expr'Unrestricted_Access;
5623 -- end if;
5625 -- and replace the if expression by a reference to Cnn.all.
5627 -- This special case can be skipped if the back end handles limited
5628 -- types properly and ensures that no incorrect copies are made.
5630 if Is_By_Reference_Type (Typ)
5631 and then not Back_End_Handles_Limited_Types
5632 then
5633 -- When the "then" or "else" expressions involve controlled function
5634 -- calls, generated temporaries are chained on the corresponding list
5635 -- of actions. These temporaries need to be finalized after the if
5636 -- expression is evaluated.
5638 Process_If_Case_Statements (N, Then_Actions (N));
5639 Process_If_Case_Statements (N, Else_Actions (N));
5641 declare
5642 Cnn : constant Entity_Id := Make_Temporary (Loc, 'C', N);
5643 Ptr_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
5645 begin
5646 -- Generate:
5647 -- type Ann is access all Typ;
5649 Insert_Action (N,
5650 Make_Full_Type_Declaration (Loc,
5651 Defining_Identifier => Ptr_Typ,
5652 Type_Definition =>
5653 Make_Access_To_Object_Definition (Loc,
5654 All_Present => True,
5655 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
5657 -- Generate:
5658 -- Cnn : Ann;
5660 Decl :=
5661 Make_Object_Declaration (Loc,
5662 Defining_Identifier => Cnn,
5663 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
5665 -- Generate:
5666 -- if Cond then
5667 -- Cnn := <Thenx>'Unrestricted_Access;
5668 -- else
5669 -- Cnn := <Elsex>'Unrestricted_Access;
5670 -- end if;
5672 New_If :=
5673 Make_Implicit_If_Statement (N,
5674 Condition => Relocate_Node (Cond),
5675 Then_Statements => New_List (
5676 Make_Assignment_Statement (Sloc (Thenx),
5677 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
5678 Expression =>
5679 Make_Attribute_Reference (Loc,
5680 Prefix => Relocate_Node (Thenx),
5681 Attribute_Name => Name_Unrestricted_Access))),
5683 Else_Statements => New_List (
5684 Make_Assignment_Statement (Sloc (Elsex),
5685 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
5686 Expression =>
5687 Make_Attribute_Reference (Loc,
5688 Prefix => Relocate_Node (Elsex),
5689 Attribute_Name => Name_Unrestricted_Access))));
5691 -- Preserve the original context for which the if statement is
5692 -- being generated. This is needed by the finalization machinery
5693 -- to prevent the premature finalization of controlled objects
5694 -- found within the if statement.
5696 Set_From_Conditional_Expression (New_If);
5698 New_N :=
5699 Make_Explicit_Dereference (Loc,
5700 Prefix => New_Occurrence_Of (Cnn, Loc));
5701 end;
5703 -- If the result is an unconstrained array and the if expression is in a
5704 -- context other than the initializing expression of the declaration of
5705 -- an object, then we pull out the if expression as follows:
5707 -- Cnn : constant typ := if-expression
5709 -- and then replace the if expression with an occurrence of Cnn. This
5710 -- avoids the need in the back end to create on-the-fly variable length
5711 -- temporaries (which it cannot do!)
5713 -- Note that the test for being in an object declaration avoids doing an
5714 -- unnecessary expansion, and also avoids infinite recursion.
5716 elsif Is_Array_Type (Typ) and then not Is_Constrained (Typ)
5717 and then (Nkind (Parent (N)) /= N_Object_Declaration
5718 or else Expression (Parent (N)) /= N)
5719 then
5720 declare
5721 Cnn : constant Node_Id := Make_Temporary (Loc, 'C', N);
5723 begin
5724 Insert_Action (N,
5725 Make_Object_Declaration (Loc,
5726 Defining_Identifier => Cnn,
5727 Constant_Present => True,
5728 Object_Definition => New_Occurrence_Of (Typ, Loc),
5729 Expression => Relocate_Node (N),
5730 Has_Init_Expression => True));
5732 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
5733 return;
5734 end;
5736 -- For other types, we only need to expand if there are other actions
5737 -- associated with either branch.
5739 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
5741 -- We now wrap the actions into the appropriate expression
5743 if Minimize_Expression_With_Actions
5744 and then (Is_Elementary_Type (Underlying_Type (Typ))
5745 or else Is_Constrained (Underlying_Type (Typ)))
5746 then
5747 -- If we can't use N_Expression_With_Actions nodes, then we insert
5748 -- the following sequence of actions (using Insert_Actions):
5750 -- Cnn : typ;
5751 -- if cond then
5752 -- <<then actions>>
5753 -- Cnn := then-expr;
5754 -- else
5755 -- <<else actions>>
5756 -- Cnn := else-expr
5757 -- end if;
5759 -- and replace the if expression by a reference to Cnn
5761 declare
5762 Cnn : constant Node_Id := Make_Temporary (Loc, 'C', N);
5764 begin
5765 Decl :=
5766 Make_Object_Declaration (Loc,
5767 Defining_Identifier => Cnn,
5768 Object_Definition => New_Occurrence_Of (Typ, Loc));
5770 New_If :=
5771 Make_Implicit_If_Statement (N,
5772 Condition => Relocate_Node (Cond),
5774 Then_Statements => New_List (
5775 Make_Assignment_Statement (Sloc (Thenx),
5776 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
5777 Expression => Relocate_Node (Thenx))),
5779 Else_Statements => New_List (
5780 Make_Assignment_Statement (Sloc (Elsex),
5781 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
5782 Expression => Relocate_Node (Elsex))));
5784 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
5785 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
5787 New_N := New_Occurrence_Of (Cnn, Loc);
5788 end;
5790 -- Regular path using Expression_With_Actions
5792 else
5793 if Present (Then_Actions (N)) then
5794 Rewrite (Thenx,
5795 Make_Expression_With_Actions (Sloc (Thenx),
5796 Actions => Then_Actions (N),
5797 Expression => Relocate_Node (Thenx)));
5799 Set_Then_Actions (N, No_List);
5800 Analyze_And_Resolve (Thenx, Typ);
5801 end if;
5803 if Present (Else_Actions (N)) then
5804 Rewrite (Elsex,
5805 Make_Expression_With_Actions (Sloc (Elsex),
5806 Actions => Else_Actions (N),
5807 Expression => Relocate_Node (Elsex)));
5809 Set_Else_Actions (N, No_List);
5810 Analyze_And_Resolve (Elsex, Typ);
5811 end if;
5813 return;
5814 end if;
5816 -- If no actions then no expansion needed, gigi will handle it using the
5817 -- same approach as a C conditional expression.
5819 else
5820 return;
5821 end if;
5823 -- Fall through here for either the limited expansion, or the case of
5824 -- inserting actions for nonlimited types. In both these cases, we must
5825 -- move the SLOC of the parent If statement to the newly created one and
5826 -- change it to the SLOC of the expression which, after expansion, will
5827 -- correspond to what is being evaluated.
5829 if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then
5830 Set_Sloc (New_If, Sloc (Parent (N)));
5831 Set_Sloc (Parent (N), Loc);
5832 end if;
5834 -- Make sure Then_Actions and Else_Actions are appropriately moved
5835 -- to the new if statement.
5837 if Present (Then_Actions (N)) then
5838 Insert_List_Before
5839 (First (Then_Statements (New_If)), Then_Actions (N));
5840 end if;
5842 if Present (Else_Actions (N)) then
5843 Insert_List_Before
5844 (First (Else_Statements (New_If)), Else_Actions (N));
5845 end if;
5847 Insert_Action (N, Decl);
5848 Insert_Action (N, New_If);
5849 Rewrite (N, New_N);
5850 Analyze_And_Resolve (N, Typ);
5851 end Expand_N_If_Expression;
5853 -----------------
5854 -- Expand_N_In --
5855 -----------------
5857 procedure Expand_N_In (N : Node_Id) is
5858 Loc : constant Source_Ptr := Sloc (N);
5859 Restyp : constant Entity_Id := Etype (N);
5860 Lop : constant Node_Id := Left_Opnd (N);
5861 Rop : constant Node_Id := Right_Opnd (N);
5862 Static : constant Boolean := Is_OK_Static_Expression (N);
5864 procedure Substitute_Valid_Check;
5865 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5866 -- test for the left operand being in range of its subtype.
5868 ----------------------------
5869 -- Substitute_Valid_Check --
5870 ----------------------------
5872 procedure Substitute_Valid_Check is
5873 function Is_OK_Object_Reference (Nod : Node_Id) return Boolean;
5874 -- Determine whether arbitrary node Nod denotes a source object that
5875 -- may safely act as prefix of attribute 'Valid.
5877 ----------------------------
5878 -- Is_OK_Object_Reference --
5879 ----------------------------
5881 function Is_OK_Object_Reference (Nod : Node_Id) return Boolean is
5882 Obj_Ref : Node_Id;
5884 begin
5885 -- Inspect the original operand
5887 Obj_Ref := Original_Node (Nod);
5889 -- The object reference must be a source construct, otherwise the
5890 -- codefix suggestion may refer to nonexistent code from a user
5891 -- perspective.
5893 if Comes_From_Source (Obj_Ref) then
5895 -- Recover the actual object reference. There may be more cases
5896 -- to consider???
5898 loop
5899 if Nkind_In (Obj_Ref, N_Type_Conversion,
5900 N_Unchecked_Type_Conversion)
5901 then
5902 Obj_Ref := Expression (Obj_Ref);
5903 else
5904 exit;
5905 end if;
5906 end loop;
5908 return Is_Object_Reference (Obj_Ref);
5909 end if;
5911 return False;
5912 end Is_OK_Object_Reference;
5914 -- Start of processing for Substitute_Valid_Check
5916 begin
5917 Rewrite (N,
5918 Make_Attribute_Reference (Loc,
5919 Prefix => Relocate_Node (Lop),
5920 Attribute_Name => Name_Valid));
5922 Analyze_And_Resolve (N, Restyp);
5924 -- Emit a warning when the left-hand operand of the membership test
5925 -- is a source object, otherwise the use of attribute 'Valid would be
5926 -- illegal. The warning is not given when overflow checking is either
5927 -- MINIMIZED or ELIMINATED, as the danger of optimization has been
5928 -- eliminated above.
5930 if Is_OK_Object_Reference (Lop)
5931 and then Overflow_Check_Mode not in Minimized_Or_Eliminated
5932 then
5933 Error_Msg_N
5934 ("??explicit membership test may be optimized away", N);
5935 Error_Msg_N -- CODEFIX
5936 ("\??use ''Valid attribute instead", N);
5937 end if;
5938 end Substitute_Valid_Check;
5940 -- Local variables
5942 Ltyp : Entity_Id;
5943 Rtyp : Entity_Id;
5945 -- Start of processing for Expand_N_In
5947 begin
5948 -- If set membership case, expand with separate procedure
5950 if Present (Alternatives (N)) then
5951 Expand_Set_Membership (N);
5952 return;
5953 end if;
5955 -- Not set membership, proceed with expansion
5957 Ltyp := Etype (Left_Opnd (N));
5958 Rtyp := Etype (Right_Opnd (N));
5960 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5961 -- type, then expand with a separate procedure. Note the use of the
5962 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5964 if Overflow_Check_Mode in Minimized_Or_Eliminated
5965 and then Is_Signed_Integer_Type (Ltyp)
5966 and then not No_Minimize_Eliminate (N)
5967 then
5968 Expand_Membership_Minimize_Eliminate_Overflow (N);
5969 return;
5970 end if;
5972 -- Check case of explicit test for an expression in range of its
5973 -- subtype. This is suspicious usage and we replace it with a 'Valid
5974 -- test and give a warning for scalar types.
5976 if Is_Scalar_Type (Ltyp)
5978 -- Only relevant for source comparisons
5980 and then Comes_From_Source (N)
5982 -- In floating-point this is a standard way to check for finite values
5983 -- and using 'Valid would typically be a pessimization.
5985 and then not Is_Floating_Point_Type (Ltyp)
5987 -- Don't give the message unless right operand is a type entity and
5988 -- the type of the left operand matches this type. Note that this
5989 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5990 -- checks have changed the type of the left operand.
5992 and then Nkind (Rop) in N_Has_Entity
5993 and then Ltyp = Entity (Rop)
5995 -- Skip this for predicated types, where such expressions are a
5996 -- reasonable way of testing if something meets the predicate.
5998 and then not Present (Predicate_Function (Ltyp))
5999 then
6000 Substitute_Valid_Check;
6001 return;
6002 end if;
6004 -- Do validity check on operands
6006 if Validity_Checks_On and Validity_Check_Operands then
6007 Ensure_Valid (Left_Opnd (N));
6008 Validity_Check_Range (Right_Opnd (N));
6009 end if;
6011 -- Case of explicit range
6013 if Nkind (Rop) = N_Range then
6014 declare
6015 Lo : constant Node_Id := Low_Bound (Rop);
6016 Hi : constant Node_Id := High_Bound (Rop);
6018 Lo_Orig : constant Node_Id := Original_Node (Lo);
6019 Hi_Orig : constant Node_Id := Original_Node (Hi);
6021 Lcheck : Compare_Result;
6022 Ucheck : Compare_Result;
6024 Warn1 : constant Boolean :=
6025 Constant_Condition_Warnings
6026 and then Comes_From_Source (N)
6027 and then not In_Instance;
6028 -- This must be true for any of the optimization warnings, we
6029 -- clearly want to give them only for source with the flag on. We
6030 -- also skip these warnings in an instance since it may be the
6031 -- case that different instantiations have different ranges.
6033 Warn2 : constant Boolean :=
6034 Warn1
6035 and then Nkind (Original_Node (Rop)) = N_Range
6036 and then Is_Integer_Type (Etype (Lo));
6037 -- For the case where only one bound warning is elided, we also
6038 -- insist on an explicit range and an integer type. The reason is
6039 -- that the use of enumeration ranges including an end point is
6040 -- common, as is the use of a subtype name, one of whose bounds is
6041 -- the same as the type of the expression.
6043 begin
6044 -- If test is explicit x'First .. x'Last, replace by valid check
6046 -- Could use some individual comments for this complex test ???
6048 if Is_Scalar_Type (Ltyp)
6050 -- And left operand is X'First where X matches left operand
6051 -- type (this eliminates cases of type mismatch, including
6052 -- the cases where ELIMINATED/MINIMIZED mode has changed the
6053 -- type of the left operand.
6055 and then Nkind (Lo_Orig) = N_Attribute_Reference
6056 and then Attribute_Name (Lo_Orig) = Name_First
6057 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
6058 and then Entity (Prefix (Lo_Orig)) = Ltyp
6060 -- Same tests for right operand
6062 and then Nkind (Hi_Orig) = N_Attribute_Reference
6063 and then Attribute_Name (Hi_Orig) = Name_Last
6064 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
6065 and then Entity (Prefix (Hi_Orig)) = Ltyp
6067 -- Relevant only for source cases
6069 and then Comes_From_Source (N)
6070 then
6071 Substitute_Valid_Check;
6072 goto Leave;
6073 end if;
6075 -- If bounds of type are known at compile time, and the end points
6076 -- are known at compile time and identical, this is another case
6077 -- for substituting a valid test. We only do this for discrete
6078 -- types, since it won't arise in practice for float types.
6080 if Comes_From_Source (N)
6081 and then Is_Discrete_Type (Ltyp)
6082 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
6083 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
6084 and then Compile_Time_Known_Value (Lo)
6085 and then Compile_Time_Known_Value (Hi)
6086 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
6087 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
6089 -- Kill warnings in instances, since they may be cases where we
6090 -- have a test in the generic that makes sense with some types
6091 -- and not with other types.
6093 -- Similarly, do not rewrite membership as a validity check if
6094 -- within the predicate function for the type.
6096 then
6097 if In_Instance
6098 or else (Ekind (Current_Scope) = E_Function
6099 and then Is_Predicate_Function (Current_Scope))
6100 then
6101 null;
6103 else
6104 Substitute_Valid_Check;
6105 goto Leave;
6106 end if;
6107 end if;
6109 -- If we have an explicit range, do a bit of optimization based on
6110 -- range analysis (we may be able to kill one or both checks).
6112 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
6113 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
6115 -- If either check is known to fail, replace result by False since
6116 -- the other check does not matter. Preserve the static flag for
6117 -- legality checks, because we are constant-folding beyond RM 4.9.
6119 if Lcheck = LT or else Ucheck = GT then
6120 if Warn1 then
6121 Error_Msg_N ("?c?range test optimized away", N);
6122 Error_Msg_N ("\?c?value is known to be out of range", N);
6123 end if;
6125 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
6126 Analyze_And_Resolve (N, Restyp);
6127 Set_Is_Static_Expression (N, Static);
6128 goto Leave;
6130 -- If both checks are known to succeed, replace result by True,
6131 -- since we know we are in range.
6133 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
6134 if Warn1 then
6135 Error_Msg_N ("?c?range test optimized away", N);
6136 Error_Msg_N ("\?c?value is known to be in range", N);
6137 end if;
6139 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6140 Analyze_And_Resolve (N, Restyp);
6141 Set_Is_Static_Expression (N, Static);
6142 goto Leave;
6144 -- If lower bound check succeeds and upper bound check is not
6145 -- known to succeed or fail, then replace the range check with
6146 -- a comparison against the upper bound.
6148 elsif Lcheck in Compare_GE then
6149 if Warn2 and then not In_Instance then
6150 Error_Msg_N ("??lower bound test optimized away", Lo);
6151 Error_Msg_N ("\??value is known to be in range", Lo);
6152 end if;
6154 Rewrite (N,
6155 Make_Op_Le (Loc,
6156 Left_Opnd => Lop,
6157 Right_Opnd => High_Bound (Rop)));
6158 Analyze_And_Resolve (N, Restyp);
6159 goto Leave;
6161 -- If upper bound check succeeds and lower bound check is not
6162 -- known to succeed or fail, then replace the range check with
6163 -- a comparison against the lower bound.
6165 elsif Ucheck in Compare_LE then
6166 if Warn2 and then not In_Instance then
6167 Error_Msg_N ("??upper bound test optimized away", Hi);
6168 Error_Msg_N ("\??value is known to be in range", Hi);
6169 end if;
6171 Rewrite (N,
6172 Make_Op_Ge (Loc,
6173 Left_Opnd => Lop,
6174 Right_Opnd => Low_Bound (Rop)));
6175 Analyze_And_Resolve (N, Restyp);
6176 goto Leave;
6177 end if;
6179 -- We couldn't optimize away the range check, but there is one
6180 -- more issue. If we are checking constant conditionals, then we
6181 -- see if we can determine the outcome assuming everything is
6182 -- valid, and if so give an appropriate warning.
6184 if Warn1 and then not Assume_No_Invalid_Values then
6185 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
6186 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
6188 -- Result is out of range for valid value
6190 if Lcheck = LT or else Ucheck = GT then
6191 Error_Msg_N
6192 ("?c?value can only be in range if it is invalid", N);
6194 -- Result is in range for valid value
6196 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
6197 Error_Msg_N
6198 ("?c?value can only be out of range if it is invalid", N);
6200 -- Lower bound check succeeds if value is valid
6202 elsif Warn2 and then Lcheck in Compare_GE then
6203 Error_Msg_N
6204 ("?c?lower bound check only fails if it is invalid", Lo);
6206 -- Upper bound check succeeds if value is valid
6208 elsif Warn2 and then Ucheck in Compare_LE then
6209 Error_Msg_N
6210 ("?c?upper bound check only fails for invalid values", Hi);
6211 end if;
6212 end if;
6213 end;
6215 -- For all other cases of an explicit range, nothing to be done
6217 goto Leave;
6219 -- Here right operand is a subtype mark
6221 else
6222 declare
6223 Typ : Entity_Id := Etype (Rop);
6224 Is_Acc : constant Boolean := Is_Access_Type (Typ);
6225 Cond : Node_Id := Empty;
6226 New_N : Node_Id;
6227 Obj : Node_Id := Lop;
6228 SCIL_Node : Node_Id;
6230 begin
6231 Remove_Side_Effects (Obj);
6233 -- For tagged type, do tagged membership operation
6235 if Is_Tagged_Type (Typ) then
6237 -- No expansion will be performed for VM targets, as the VM
6238 -- back ends will handle the membership tests directly.
6240 if Tagged_Type_Expansion then
6241 Tagged_Membership (N, SCIL_Node, New_N);
6242 Rewrite (N, New_N);
6243 Analyze_And_Resolve (N, Restyp, Suppress => All_Checks);
6245 -- Update decoration of relocated node referenced by the
6246 -- SCIL node.
6248 if Generate_SCIL and then Present (SCIL_Node) then
6249 Set_SCIL_Node (N, SCIL_Node);
6250 end if;
6251 end if;
6253 goto Leave;
6255 -- If type is scalar type, rewrite as x in t'First .. t'Last.
6256 -- This reason we do this is that the bounds may have the wrong
6257 -- type if they come from the original type definition. Also this
6258 -- way we get all the processing above for an explicit range.
6260 -- Don't do this for predicated types, since in this case we
6261 -- want to check the predicate.
6263 elsif Is_Scalar_Type (Typ) then
6264 if No (Predicate_Function (Typ)) then
6265 Rewrite (Rop,
6266 Make_Range (Loc,
6267 Low_Bound =>
6268 Make_Attribute_Reference (Loc,
6269 Attribute_Name => Name_First,
6270 Prefix => New_Occurrence_Of (Typ, Loc)),
6272 High_Bound =>
6273 Make_Attribute_Reference (Loc,
6274 Attribute_Name => Name_Last,
6275 Prefix => New_Occurrence_Of (Typ, Loc))));
6276 Analyze_And_Resolve (N, Restyp);
6277 end if;
6279 goto Leave;
6281 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
6282 -- a membership test if the subtype mark denotes a constrained
6283 -- Unchecked_Union subtype and the expression lacks inferable
6284 -- discriminants.
6286 elsif Is_Unchecked_Union (Base_Type (Typ))
6287 and then Is_Constrained (Typ)
6288 and then not Has_Inferable_Discriminants (Lop)
6289 then
6290 Insert_Action (N,
6291 Make_Raise_Program_Error (Loc,
6292 Reason => PE_Unchecked_Union_Restriction));
6294 -- Prevent Gigi from generating incorrect code by rewriting the
6295 -- test as False. What is this undocumented thing about ???
6297 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
6298 goto Leave;
6299 end if;
6301 -- Here we have a non-scalar type
6303 if Is_Acc then
6304 Typ := Designated_Type (Typ);
6305 end if;
6307 if not Is_Constrained (Typ) then
6308 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6309 Analyze_And_Resolve (N, Restyp);
6311 -- For the constrained array case, we have to check the subscripts
6312 -- for an exact match if the lengths are non-zero (the lengths
6313 -- must match in any case).
6315 elsif Is_Array_Type (Typ) then
6316 Check_Subscripts : declare
6317 function Build_Attribute_Reference
6318 (E : Node_Id;
6319 Nam : Name_Id;
6320 Dim : Nat) return Node_Id;
6321 -- Build attribute reference E'Nam (Dim)
6323 -------------------------------
6324 -- Build_Attribute_Reference --
6325 -------------------------------
6327 function Build_Attribute_Reference
6328 (E : Node_Id;
6329 Nam : Name_Id;
6330 Dim : Nat) return Node_Id
6332 begin
6333 return
6334 Make_Attribute_Reference (Loc,
6335 Prefix => E,
6336 Attribute_Name => Nam,
6337 Expressions => New_List (
6338 Make_Integer_Literal (Loc, Dim)));
6339 end Build_Attribute_Reference;
6341 -- Start of processing for Check_Subscripts
6343 begin
6344 for J in 1 .. Number_Dimensions (Typ) loop
6345 Evolve_And_Then (Cond,
6346 Make_Op_Eq (Loc,
6347 Left_Opnd =>
6348 Build_Attribute_Reference
6349 (Duplicate_Subexpr_No_Checks (Obj),
6350 Name_First, J),
6351 Right_Opnd =>
6352 Build_Attribute_Reference
6353 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
6355 Evolve_And_Then (Cond,
6356 Make_Op_Eq (Loc,
6357 Left_Opnd =>
6358 Build_Attribute_Reference
6359 (Duplicate_Subexpr_No_Checks (Obj),
6360 Name_Last, J),
6361 Right_Opnd =>
6362 Build_Attribute_Reference
6363 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
6364 end loop;
6366 if Is_Acc then
6367 Cond :=
6368 Make_Or_Else (Loc,
6369 Left_Opnd =>
6370 Make_Op_Eq (Loc,
6371 Left_Opnd => Obj,
6372 Right_Opnd => Make_Null (Loc)),
6373 Right_Opnd => Cond);
6374 end if;
6376 Rewrite (N, Cond);
6377 Analyze_And_Resolve (N, Restyp);
6378 end Check_Subscripts;
6380 -- These are the cases where constraint checks may be required,
6381 -- e.g. records with possible discriminants
6383 else
6384 -- Expand the test into a series of discriminant comparisons.
6385 -- The expression that is built is the negation of the one that
6386 -- is used for checking discriminant constraints.
6388 Obj := Relocate_Node (Left_Opnd (N));
6390 if Has_Discriminants (Typ) then
6391 Cond := Make_Op_Not (Loc,
6392 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
6394 if Is_Acc then
6395 Cond := Make_Or_Else (Loc,
6396 Left_Opnd =>
6397 Make_Op_Eq (Loc,
6398 Left_Opnd => Obj,
6399 Right_Opnd => Make_Null (Loc)),
6400 Right_Opnd => Cond);
6401 end if;
6403 else
6404 Cond := New_Occurrence_Of (Standard_True, Loc);
6405 end if;
6407 Rewrite (N, Cond);
6408 Analyze_And_Resolve (N, Restyp);
6409 end if;
6411 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
6412 -- expression of an anonymous access type. This can involve an
6413 -- accessibility test and a tagged type membership test in the
6414 -- case of tagged designated types.
6416 if Ada_Version >= Ada_2012
6417 and then Is_Acc
6418 and then Ekind (Ltyp) = E_Anonymous_Access_Type
6419 then
6420 declare
6421 Expr_Entity : Entity_Id := Empty;
6422 New_N : Node_Id;
6423 Param_Level : Node_Id;
6424 Type_Level : Node_Id;
6426 begin
6427 if Is_Entity_Name (Lop) then
6428 Expr_Entity := Param_Entity (Lop);
6430 if not Present (Expr_Entity) then
6431 Expr_Entity := Entity (Lop);
6432 end if;
6433 end if;
6435 -- If a conversion of the anonymous access value to the
6436 -- tested type would be illegal, then the result is False.
6438 if not Valid_Conversion
6439 (Lop, Rtyp, Lop, Report_Errs => False)
6440 then
6441 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
6442 Analyze_And_Resolve (N, Restyp);
6444 -- Apply an accessibility check if the access object has an
6445 -- associated access level and when the level of the type is
6446 -- less deep than the level of the access parameter. This
6447 -- only occur for access parameters and stand-alone objects
6448 -- of an anonymous access type.
6450 else
6451 if Present (Expr_Entity)
6452 and then
6453 Present
6454 (Effective_Extra_Accessibility (Expr_Entity))
6455 and then UI_Gt (Object_Access_Level (Lop),
6456 Type_Access_Level (Rtyp))
6457 then
6458 Param_Level :=
6459 New_Occurrence_Of
6460 (Effective_Extra_Accessibility (Expr_Entity), Loc);
6462 Type_Level :=
6463 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
6465 -- Return True only if the accessibility level of the
6466 -- expression entity is not deeper than the level of
6467 -- the tested access type.
6469 Rewrite (N,
6470 Make_And_Then (Loc,
6471 Left_Opnd => Relocate_Node (N),
6472 Right_Opnd => Make_Op_Le (Loc,
6473 Left_Opnd => Param_Level,
6474 Right_Opnd => Type_Level)));
6476 Analyze_And_Resolve (N);
6477 end if;
6479 -- If the designated type is tagged, do tagged membership
6480 -- operation.
6482 -- *** NOTE: we have to check not null before doing the
6483 -- tagged membership test (but maybe that can be done
6484 -- inside Tagged_Membership?).
6486 if Is_Tagged_Type (Typ) then
6487 Rewrite (N,
6488 Make_And_Then (Loc,
6489 Left_Opnd => Relocate_Node (N),
6490 Right_Opnd =>
6491 Make_Op_Ne (Loc,
6492 Left_Opnd => Obj,
6493 Right_Opnd => Make_Null (Loc))));
6495 -- No expansion will be performed for VM targets, as
6496 -- the VM back ends will handle the membership tests
6497 -- directly.
6499 if Tagged_Type_Expansion then
6501 -- Note that we have to pass Original_Node, because
6502 -- the membership test might already have been
6503 -- rewritten by earlier parts of membership test.
6505 Tagged_Membership
6506 (Original_Node (N), SCIL_Node, New_N);
6508 -- Update decoration of relocated node referenced
6509 -- by the SCIL node.
6511 if Generate_SCIL and then Present (SCIL_Node) then
6512 Set_SCIL_Node (New_N, SCIL_Node);
6513 end if;
6515 Rewrite (N,
6516 Make_And_Then (Loc,
6517 Left_Opnd => Relocate_Node (N),
6518 Right_Opnd => New_N));
6520 Analyze_And_Resolve (N, Restyp);
6521 end if;
6522 end if;
6523 end if;
6524 end;
6525 end if;
6526 end;
6527 end if;
6529 -- At this point, we have done the processing required for the basic
6530 -- membership test, but not yet dealt with the predicate.
6532 <<Leave>>
6534 -- If a predicate is present, then we do the predicate test, but we
6535 -- most certainly want to omit this if we are within the predicate
6536 -- function itself, since otherwise we have an infinite recursion.
6537 -- The check should also not be emitted when testing against a range
6538 -- (the check is only done when the right operand is a subtype; see
6539 -- RM12-4.5.2 (28.1/3-30/3)).
6541 Predicate_Check : declare
6542 function In_Range_Check return Boolean;
6543 -- Within an expanded range check that may raise Constraint_Error do
6544 -- not generate a predicate check as well. It is redundant because
6545 -- the context will add an explicit predicate check, and it will
6546 -- raise the wrong exception if it fails.
6548 --------------------
6549 -- In_Range_Check --
6550 --------------------
6552 function In_Range_Check return Boolean is
6553 P : Node_Id;
6554 begin
6555 P := Parent (N);
6556 while Present (P) loop
6557 if Nkind (P) = N_Raise_Constraint_Error then
6558 return True;
6560 elsif Nkind (P) in N_Statement_Other_Than_Procedure_Call
6561 or else Nkind (P) = N_Procedure_Call_Statement
6562 or else Nkind (P) in N_Declaration
6563 then
6564 return False;
6565 end if;
6567 P := Parent (P);
6568 end loop;
6570 return False;
6571 end In_Range_Check;
6573 -- Local variables
6575 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
6576 R_Op : Node_Id;
6578 -- Start of processing for Predicate_Check
6580 begin
6581 if Present (PFunc)
6582 and then Current_Scope /= PFunc
6583 and then Nkind (Rop) /= N_Range
6584 then
6585 if not In_Range_Check then
6586 R_Op := Make_Predicate_Call (Rtyp, Lop, Mem => True);
6587 else
6588 R_Op := New_Occurrence_Of (Standard_True, Loc);
6589 end if;
6591 Rewrite (N,
6592 Make_And_Then (Loc,
6593 Left_Opnd => Relocate_Node (N),
6594 Right_Opnd => R_Op));
6596 -- Analyze new expression, mark left operand as analyzed to
6597 -- avoid infinite recursion adding predicate calls. Similarly,
6598 -- suppress further range checks on the call.
6600 Set_Analyzed (Left_Opnd (N));
6601 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6603 -- All done, skip attempt at compile time determination of result
6605 return;
6606 end if;
6607 end Predicate_Check;
6608 end Expand_N_In;
6610 --------------------------------
6611 -- Expand_N_Indexed_Component --
6612 --------------------------------
6614 procedure Expand_N_Indexed_Component (N : Node_Id) is
6615 Loc : constant Source_Ptr := Sloc (N);
6616 Typ : constant Entity_Id := Etype (N);
6617 P : constant Node_Id := Prefix (N);
6618 T : constant Entity_Id := Etype (P);
6619 Atp : Entity_Id;
6621 begin
6622 -- A special optimization, if we have an indexed component that is
6623 -- selecting from a slice, then we can eliminate the slice, since, for
6624 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6625 -- the range check required by the slice. The range check for the slice
6626 -- itself has already been generated. The range check for the
6627 -- subscripting operation is ensured by converting the subject to
6628 -- the subtype of the slice.
6630 -- This optimization not only generates better code, avoiding slice
6631 -- messing especially in the packed case, but more importantly bypasses
6632 -- some problems in handling this peculiar case, for example, the issue
6633 -- of dealing specially with object renamings.
6635 if Nkind (P) = N_Slice
6637 -- This optimization is disabled for CodePeer because it can transform
6638 -- an index-check constraint_error into a range-check constraint_error
6639 -- and CodePeer cares about that distinction.
6641 and then not CodePeer_Mode
6642 then
6643 Rewrite (N,
6644 Make_Indexed_Component (Loc,
6645 Prefix => Prefix (P),
6646 Expressions => New_List (
6647 Convert_To
6648 (Etype (First_Index (Etype (P))),
6649 First (Expressions (N))))));
6650 Analyze_And_Resolve (N, Typ);
6651 return;
6652 end if;
6654 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6655 -- function, then additional actuals must be passed.
6657 if Is_Build_In_Place_Function_Call (P) then
6658 Make_Build_In_Place_Call_In_Anonymous_Context (P);
6660 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
6661 -- containing build-in-place function calls whose returned object covers
6662 -- interface types.
6664 elsif Present (Unqual_BIP_Iface_Function_Call (P)) then
6665 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (P);
6666 end if;
6668 -- If the prefix is an access type, then we unconditionally rewrite if
6669 -- as an explicit dereference. This simplifies processing for several
6670 -- cases, including packed array cases and certain cases in which checks
6671 -- must be generated. We used to try to do this only when it was
6672 -- necessary, but it cleans up the code to do it all the time.
6674 if Is_Access_Type (T) then
6675 Insert_Explicit_Dereference (P);
6676 Analyze_And_Resolve (P, Designated_Type (T));
6677 Atp := Designated_Type (T);
6678 else
6679 Atp := T;
6680 end if;
6682 -- Generate index and validity checks
6684 Generate_Index_Checks (N);
6686 if Validity_Checks_On and then Validity_Check_Subscripts then
6687 Apply_Subscript_Validity_Checks (N);
6688 end if;
6690 -- If selecting from an array with atomic components, and atomic sync
6691 -- is not suppressed for this array type, set atomic sync flag.
6693 if (Has_Atomic_Components (Atp)
6694 and then not Atomic_Synchronization_Disabled (Atp))
6695 or else (Is_Atomic (Typ)
6696 and then not Atomic_Synchronization_Disabled (Typ))
6697 or else (Is_Entity_Name (P)
6698 and then Has_Atomic_Components (Entity (P))
6699 and then not Atomic_Synchronization_Disabled (Entity (P)))
6700 then
6701 Activate_Atomic_Synchronization (N);
6702 end if;
6704 -- All done if the prefix is not a packed array implemented specially
6706 if not (Is_Packed (Etype (Prefix (N)))
6707 and then Present (Packed_Array_Impl_Type (Etype (Prefix (N)))))
6708 then
6709 return;
6710 end if;
6712 -- For packed arrays that are not bit-packed (i.e. the case of an array
6713 -- with one or more index types with a non-contiguous enumeration type),
6714 -- we can always use the normal packed element get circuit.
6716 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
6717 Expand_Packed_Element_Reference (N);
6718 return;
6719 end if;
6721 -- For a reference to a component of a bit packed array, we convert it
6722 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
6723 -- want to do this for simple references, and not for:
6725 -- Left side of assignment, or prefix of left side of assignment, or
6726 -- prefix of the prefix, to handle packed arrays of packed arrays,
6727 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6729 -- Renaming objects in renaming associations
6730 -- This case is handled when a use of the renamed variable occurs
6732 -- Actual parameters for a procedure call
6733 -- This case is handled in Exp_Ch6.Expand_Actuals
6735 -- The second expression in a 'Read attribute reference
6737 -- The prefix of an address or bit or size attribute reference
6739 -- The following circuit detects these exceptions. Note that we need to
6740 -- deal with implicit dereferences when climbing up the parent chain,
6741 -- with the additional difficulty that the type of parents may have yet
6742 -- to be resolved since prefixes are usually resolved first.
6744 declare
6745 Child : Node_Id := N;
6746 Parnt : Node_Id := Parent (N);
6748 begin
6749 loop
6750 if Nkind (Parnt) = N_Unchecked_Expression then
6751 null;
6753 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
6754 N_Procedure_Call_Statement)
6755 or else (Nkind (Parnt) = N_Parameter_Association
6756 and then
6757 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
6758 then
6759 return;
6761 elsif Nkind (Parnt) = N_Attribute_Reference
6762 and then Nam_In (Attribute_Name (Parnt), Name_Address,
6763 Name_Bit,
6764 Name_Size)
6765 and then Prefix (Parnt) = Child
6766 then
6767 return;
6769 elsif Nkind (Parnt) = N_Assignment_Statement
6770 and then Name (Parnt) = Child
6771 then
6772 return;
6774 -- If the expression is an index of an indexed component, it must
6775 -- be expanded regardless of context.
6777 elsif Nkind (Parnt) = N_Indexed_Component
6778 and then Child /= Prefix (Parnt)
6779 then
6780 Expand_Packed_Element_Reference (N);
6781 return;
6783 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
6784 and then Name (Parent (Parnt)) = Parnt
6785 then
6786 return;
6788 elsif Nkind (Parnt) = N_Attribute_Reference
6789 and then Attribute_Name (Parnt) = Name_Read
6790 and then Next (First (Expressions (Parnt))) = Child
6791 then
6792 return;
6794 elsif Nkind (Parnt) = N_Indexed_Component
6795 and then Prefix (Parnt) = Child
6796 then
6797 null;
6799 elsif Nkind (Parnt) = N_Selected_Component
6800 and then Prefix (Parnt) = Child
6801 and then not (Present (Etype (Selector_Name (Parnt)))
6802 and then
6803 Is_Access_Type (Etype (Selector_Name (Parnt))))
6804 then
6805 null;
6807 -- If the parent is a dereference, either implicit or explicit,
6808 -- then the packed reference needs to be expanded.
6810 else
6811 Expand_Packed_Element_Reference (N);
6812 return;
6813 end if;
6815 -- Keep looking up tree for unchecked expression, or if we are the
6816 -- prefix of a possible assignment left side.
6818 Child := Parnt;
6819 Parnt := Parent (Child);
6820 end loop;
6821 end;
6822 end Expand_N_Indexed_Component;
6824 ---------------------
6825 -- Expand_N_Not_In --
6826 ---------------------
6828 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6829 -- can be done. This avoids needing to duplicate this expansion code.
6831 procedure Expand_N_Not_In (N : Node_Id) is
6832 Loc : constant Source_Ptr := Sloc (N);
6833 Typ : constant Entity_Id := Etype (N);
6834 Cfs : constant Boolean := Comes_From_Source (N);
6836 begin
6837 Rewrite (N,
6838 Make_Op_Not (Loc,
6839 Right_Opnd =>
6840 Make_In (Loc,
6841 Left_Opnd => Left_Opnd (N),
6842 Right_Opnd => Right_Opnd (N))));
6844 -- If this is a set membership, preserve list of alternatives
6846 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
6848 -- We want this to appear as coming from source if original does (see
6849 -- transformations in Expand_N_In).
6851 Set_Comes_From_Source (N, Cfs);
6852 Set_Comes_From_Source (Right_Opnd (N), Cfs);
6854 -- Now analyze transformed node
6856 Analyze_And_Resolve (N, Typ);
6857 end Expand_N_Not_In;
6859 -------------------
6860 -- Expand_N_Null --
6861 -------------------
6863 -- The only replacement required is for the case of a null of a type that
6864 -- is an access to protected subprogram, or a subtype thereof. We represent
6865 -- such access values as a record, and so we must replace the occurrence of
6866 -- null by the equivalent record (with a null address and a null pointer in
6867 -- it), so that the back end creates the proper value.
6869 procedure Expand_N_Null (N : Node_Id) is
6870 Loc : constant Source_Ptr := Sloc (N);
6871 Typ : constant Entity_Id := Base_Type (Etype (N));
6872 Agg : Node_Id;
6874 begin
6875 if Is_Access_Protected_Subprogram_Type (Typ) then
6876 Agg :=
6877 Make_Aggregate (Loc,
6878 Expressions => New_List (
6879 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
6880 Make_Null (Loc)));
6882 Rewrite (N, Agg);
6883 Analyze_And_Resolve (N, Equivalent_Type (Typ));
6885 -- For subsequent semantic analysis, the node must retain its type.
6886 -- Gigi in any case replaces this type by the corresponding record
6887 -- type before processing the node.
6889 Set_Etype (N, Typ);
6890 end if;
6892 exception
6893 when RE_Not_Available =>
6894 return;
6895 end Expand_N_Null;
6897 ---------------------
6898 -- Expand_N_Op_Abs --
6899 ---------------------
6901 procedure Expand_N_Op_Abs (N : Node_Id) is
6902 Loc : constant Source_Ptr := Sloc (N);
6903 Expr : constant Node_Id := Right_Opnd (N);
6905 begin
6906 Unary_Op_Validity_Checks (N);
6908 -- Check for MINIMIZED/ELIMINATED overflow mode
6910 if Minimized_Eliminated_Overflow_Check (N) then
6911 Apply_Arithmetic_Overflow_Check (N);
6912 return;
6913 end if;
6915 -- Deal with software overflow checking
6917 if not Backend_Overflow_Checks_On_Target
6918 and then Is_Signed_Integer_Type (Etype (N))
6919 and then Do_Overflow_Check (N)
6920 then
6921 -- The only case to worry about is when the argument is equal to the
6922 -- largest negative number, so what we do is to insert the check:
6924 -- [constraint_error when Expr = typ'Base'First]
6926 -- with the usual Duplicate_Subexpr use coding for expr
6928 Insert_Action (N,
6929 Make_Raise_Constraint_Error (Loc,
6930 Condition =>
6931 Make_Op_Eq (Loc,
6932 Left_Opnd => Duplicate_Subexpr (Expr),
6933 Right_Opnd =>
6934 Make_Attribute_Reference (Loc,
6935 Prefix =>
6936 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
6937 Attribute_Name => Name_First)),
6938 Reason => CE_Overflow_Check_Failed));
6939 end if;
6940 end Expand_N_Op_Abs;
6942 ---------------------
6943 -- Expand_N_Op_Add --
6944 ---------------------
6946 procedure Expand_N_Op_Add (N : Node_Id) is
6947 Typ : constant Entity_Id := Etype (N);
6949 begin
6950 Binary_Op_Validity_Checks (N);
6952 -- Check for MINIMIZED/ELIMINATED overflow mode
6954 if Minimized_Eliminated_Overflow_Check (N) then
6955 Apply_Arithmetic_Overflow_Check (N);
6956 return;
6957 end if;
6959 -- N + 0 = 0 + N = N for integer types
6961 if Is_Integer_Type (Typ) then
6962 if Compile_Time_Known_Value (Right_Opnd (N))
6963 and then Expr_Value (Right_Opnd (N)) = Uint_0
6964 then
6965 Rewrite (N, Left_Opnd (N));
6966 return;
6968 elsif Compile_Time_Known_Value (Left_Opnd (N))
6969 and then Expr_Value (Left_Opnd (N)) = Uint_0
6970 then
6971 Rewrite (N, Right_Opnd (N));
6972 return;
6973 end if;
6974 end if;
6976 -- Arithmetic overflow checks for signed integer/fixed point types
6978 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
6979 Apply_Arithmetic_Overflow_Check (N);
6980 return;
6981 end if;
6983 -- Overflow checks for floating-point if -gnateF mode active
6985 Check_Float_Op_Overflow (N);
6987 Expand_Nonbinary_Modular_Op (N);
6988 end Expand_N_Op_Add;
6990 ---------------------
6991 -- Expand_N_Op_And --
6992 ---------------------
6994 procedure Expand_N_Op_And (N : Node_Id) is
6995 Typ : constant Entity_Id := Etype (N);
6997 begin
6998 Binary_Op_Validity_Checks (N);
7000 if Is_Array_Type (Etype (N)) then
7001 Expand_Boolean_Operator (N);
7003 elsif Is_Boolean_Type (Etype (N)) then
7004 Adjust_Condition (Left_Opnd (N));
7005 Adjust_Condition (Right_Opnd (N));
7006 Set_Etype (N, Standard_Boolean);
7007 Adjust_Result_Type (N, Typ);
7009 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7010 Expand_Intrinsic_Call (N, Entity (N));
7011 end if;
7013 Expand_Nonbinary_Modular_Op (N);
7014 end Expand_N_Op_And;
7016 ------------------------
7017 -- Expand_N_Op_Concat --
7018 ------------------------
7020 procedure Expand_N_Op_Concat (N : Node_Id) is
7021 Opnds : List_Id;
7022 -- List of operands to be concatenated
7024 Cnode : Node_Id;
7025 -- Node which is to be replaced by the result of concatenating the nodes
7026 -- in the list Opnds.
7028 begin
7029 -- Ensure validity of both operands
7031 Binary_Op_Validity_Checks (N);
7033 -- If we are the left operand of a concatenation higher up the tree,
7034 -- then do nothing for now, since we want to deal with a series of
7035 -- concatenations as a unit.
7037 if Nkind (Parent (N)) = N_Op_Concat
7038 and then N = Left_Opnd (Parent (N))
7039 then
7040 return;
7041 end if;
7043 -- We get here with a concatenation whose left operand may be a
7044 -- concatenation itself with a consistent type. We need to process
7045 -- these concatenation operands from left to right, which means
7046 -- from the deepest node in the tree to the highest node.
7048 Cnode := N;
7049 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
7050 Cnode := Left_Opnd (Cnode);
7051 end loop;
7053 -- Now Cnode is the deepest concatenation, and its parents are the
7054 -- concatenation nodes above, so now we process bottom up, doing the
7055 -- operands.
7057 -- The outer loop runs more than once if more than one concatenation
7058 -- type is involved.
7060 Outer : loop
7061 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
7062 Set_Parent (Opnds, N);
7064 -- The inner loop gathers concatenation operands
7066 Inner : while Cnode /= N
7067 and then Base_Type (Etype (Cnode)) =
7068 Base_Type (Etype (Parent (Cnode)))
7069 loop
7070 Cnode := Parent (Cnode);
7071 Append (Right_Opnd (Cnode), Opnds);
7072 end loop Inner;
7074 -- Note: The following code is a temporary workaround for N731-034
7075 -- and N829-028 and will be kept until the general issue of internal
7076 -- symbol serialization is addressed. The workaround is kept under a
7077 -- debug switch to avoid permiating into the general case.
7079 -- Wrap the node to concatenate into an expression actions node to
7080 -- keep it nicely packaged. This is useful in the case of an assert
7081 -- pragma with a concatenation where we want to be able to delete
7082 -- the concatenation and all its expansion stuff.
7084 if Debug_Flag_Dot_H then
7085 declare
7086 Cnod : constant Node_Id := New_Copy_Tree (Cnode);
7087 Typ : constant Entity_Id := Base_Type (Etype (Cnode));
7089 begin
7090 -- Note: use Rewrite rather than Replace here, so that for
7091 -- example Why_Not_Static can find the original concatenation
7092 -- node OK!
7094 Rewrite (Cnode,
7095 Make_Expression_With_Actions (Sloc (Cnode),
7096 Actions => New_List (Make_Null_Statement (Sloc (Cnode))),
7097 Expression => Cnod));
7099 Expand_Concatenate (Cnod, Opnds);
7100 Analyze_And_Resolve (Cnode, Typ);
7101 end;
7103 -- Default case
7105 else
7106 Expand_Concatenate (Cnode, Opnds);
7107 end if;
7109 exit Outer when Cnode = N;
7110 Cnode := Parent (Cnode);
7111 end loop Outer;
7112 end Expand_N_Op_Concat;
7114 ------------------------
7115 -- Expand_N_Op_Divide --
7116 ------------------------
7118 procedure Expand_N_Op_Divide (N : Node_Id) is
7119 Loc : constant Source_Ptr := Sloc (N);
7120 Lopnd : constant Node_Id := Left_Opnd (N);
7121 Ropnd : constant Node_Id := Right_Opnd (N);
7122 Ltyp : constant Entity_Id := Etype (Lopnd);
7123 Rtyp : constant Entity_Id := Etype (Ropnd);
7124 Typ : Entity_Id := Etype (N);
7125 Rknow : constant Boolean := Is_Integer_Type (Typ)
7126 and then
7127 Compile_Time_Known_Value (Ropnd);
7128 Rval : Uint;
7130 begin
7131 Binary_Op_Validity_Checks (N);
7133 -- Check for MINIMIZED/ELIMINATED overflow mode
7135 if Minimized_Eliminated_Overflow_Check (N) then
7136 Apply_Arithmetic_Overflow_Check (N);
7137 return;
7138 end if;
7140 -- Otherwise proceed with expansion of division
7142 if Rknow then
7143 Rval := Expr_Value (Ropnd);
7144 end if;
7146 -- N / 1 = N for integer types
7148 if Rknow and then Rval = Uint_1 then
7149 Rewrite (N, Lopnd);
7150 return;
7151 end if;
7153 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
7154 -- Is_Power_Of_2_For_Shift is set means that we know that our left
7155 -- operand is an unsigned integer, as required for this to work.
7157 if Nkind (Ropnd) = N_Op_Expon
7158 and then Is_Power_Of_2_For_Shift (Ropnd)
7160 -- We cannot do this transformation in configurable run time mode if we
7161 -- have 64-bit integers and long shifts are not available.
7163 and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target)
7164 then
7165 Rewrite (N,
7166 Make_Op_Shift_Right (Loc,
7167 Left_Opnd => Lopnd,
7168 Right_Opnd =>
7169 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
7170 Analyze_And_Resolve (N, Typ);
7171 return;
7172 end if;
7174 -- Do required fixup of universal fixed operation
7176 if Typ = Universal_Fixed then
7177 Fixup_Universal_Fixed_Operation (N);
7178 Typ := Etype (N);
7179 end if;
7181 -- Divisions with fixed-point results
7183 if Is_Fixed_Point_Type (Typ) then
7185 -- No special processing if Treat_Fixed_As_Integer is set, since
7186 -- from a semantic point of view such operations are simply integer
7187 -- operations and will be treated that way.
7189 if not Treat_Fixed_As_Integer (N) then
7190 if Is_Integer_Type (Rtyp) then
7191 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
7192 else
7193 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
7194 end if;
7195 end if;
7197 -- Deal with divide-by-zero check if back end cannot handle them
7198 -- and the flag is set indicating that we need such a check. Note
7199 -- that we don't need to bother here with the case of mixed-mode
7200 -- (Right operand an integer type), since these will be rewritten
7201 -- with conversions to a divide with a fixed-point right operand.
7203 if Nkind (N) = N_Op_Divide
7204 and then Do_Division_Check (N)
7205 and then not Backend_Divide_Checks_On_Target
7206 and then not Is_Integer_Type (Rtyp)
7207 then
7208 Set_Do_Division_Check (N, False);
7209 Insert_Action (N,
7210 Make_Raise_Constraint_Error (Loc,
7211 Condition =>
7212 Make_Op_Eq (Loc,
7213 Left_Opnd => Duplicate_Subexpr_Move_Checks (Ropnd),
7214 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
7215 Reason => CE_Divide_By_Zero));
7216 end if;
7218 -- Other cases of division of fixed-point operands. Again we exclude the
7219 -- case where Treat_Fixed_As_Integer is set.
7221 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
7222 and then not Treat_Fixed_As_Integer (N)
7223 then
7224 if Is_Integer_Type (Typ) then
7225 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
7226 else
7227 pragma Assert (Is_Floating_Point_Type (Typ));
7228 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
7229 end if;
7231 -- Mixed-mode operations can appear in a non-static universal context,
7232 -- in which case the integer argument must be converted explicitly.
7234 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
7235 Rewrite (Ropnd,
7236 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
7238 Analyze_And_Resolve (Ropnd, Universal_Real);
7240 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
7241 Rewrite (Lopnd,
7242 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
7244 Analyze_And_Resolve (Lopnd, Universal_Real);
7246 -- Non-fixed point cases, do integer zero divide and overflow checks
7248 elsif Is_Integer_Type (Typ) then
7249 Apply_Divide_Checks (N);
7250 end if;
7252 -- Overflow checks for floating-point if -gnateF mode active
7254 Check_Float_Op_Overflow (N);
7256 Expand_Nonbinary_Modular_Op (N);
7257 end Expand_N_Op_Divide;
7259 --------------------
7260 -- Expand_N_Op_Eq --
7261 --------------------
7263 procedure Expand_N_Op_Eq (N : Node_Id) is
7264 Loc : constant Source_Ptr := Sloc (N);
7265 Typ : constant Entity_Id := Etype (N);
7266 Lhs : constant Node_Id := Left_Opnd (N);
7267 Rhs : constant Node_Id := Right_Opnd (N);
7268 Bodies : constant List_Id := New_List;
7269 A_Typ : constant Entity_Id := Etype (Lhs);
7271 Typl : Entity_Id := A_Typ;
7272 Op_Name : Entity_Id;
7273 Prim : Elmt_Id;
7275 procedure Build_Equality_Call (Eq : Entity_Id);
7276 -- If a constructed equality exists for the type or for its parent,
7277 -- build and analyze call, adding conversions if the operation is
7278 -- inherited.
7280 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
7281 -- Determines whether a type has a subcomponent of an unconstrained
7282 -- Unchecked_Union subtype. Typ is a record type.
7284 -------------------------
7285 -- Build_Equality_Call --
7286 -------------------------
7288 procedure Build_Equality_Call (Eq : Entity_Id) is
7289 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
7290 L_Exp : Node_Id := Relocate_Node (Lhs);
7291 R_Exp : Node_Id := Relocate_Node (Rhs);
7293 begin
7294 -- Adjust operands if necessary to comparison type
7296 if Base_Type (Op_Type) /= Base_Type (A_Typ)
7297 and then not Is_Class_Wide_Type (A_Typ)
7298 then
7299 L_Exp := OK_Convert_To (Op_Type, L_Exp);
7300 R_Exp := OK_Convert_To (Op_Type, R_Exp);
7301 end if;
7303 -- If we have an Unchecked_Union, we need to add the inferred
7304 -- discriminant values as actuals in the function call. At this
7305 -- point, the expansion has determined that both operands have
7306 -- inferable discriminants.
7308 if Is_Unchecked_Union (Op_Type) then
7309 declare
7310 Lhs_Type : constant Node_Id := Etype (L_Exp);
7311 Rhs_Type : constant Node_Id := Etype (R_Exp);
7313 Lhs_Discr_Vals : Elist_Id;
7314 -- List of inferred discriminant values for left operand.
7316 Rhs_Discr_Vals : Elist_Id;
7317 -- List of inferred discriminant values for right operand.
7319 Discr : Entity_Id;
7321 begin
7322 Lhs_Discr_Vals := New_Elmt_List;
7323 Rhs_Discr_Vals := New_Elmt_List;
7325 -- Per-object constrained selected components require special
7326 -- attention. If the enclosing scope of the component is an
7327 -- Unchecked_Union, we cannot reference its discriminants
7328 -- directly. This is why we use the extra parameters of the
7329 -- equality function of the enclosing Unchecked_Union.
7331 -- type UU_Type (Discr : Integer := 0) is
7332 -- . . .
7333 -- end record;
7334 -- pragma Unchecked_Union (UU_Type);
7336 -- 1. Unchecked_Union enclosing record:
7338 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
7339 -- . . .
7340 -- Comp : UU_Type (Discr);
7341 -- . . .
7342 -- end Enclosing_UU_Type;
7343 -- pragma Unchecked_Union (Enclosing_UU_Type);
7345 -- Obj1 : Enclosing_UU_Type;
7346 -- Obj2 : Enclosing_UU_Type (1);
7348 -- [. . .] Obj1 = Obj2 [. . .]
7350 -- Generated code:
7352 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
7354 -- A and B are the formal parameters of the equality function
7355 -- of Enclosing_UU_Type. The function always has two extra
7356 -- formals to capture the inferred discriminant values for
7357 -- each discriminant of the type.
7359 -- 2. Non-Unchecked_Union enclosing record:
7361 -- type
7362 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
7363 -- is record
7364 -- . . .
7365 -- Comp : UU_Type (Discr);
7366 -- . . .
7367 -- end Enclosing_Non_UU_Type;
7369 -- Obj1 : Enclosing_Non_UU_Type;
7370 -- Obj2 : Enclosing_Non_UU_Type (1);
7372 -- ... Obj1 = Obj2 ...
7374 -- Generated code:
7376 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
7377 -- obj1.discr, obj2.discr)) then
7379 -- In this case we can directly reference the discriminants of
7380 -- the enclosing record.
7382 -- Process left operand of equality
7384 if Nkind (Lhs) = N_Selected_Component
7385 and then
7386 Has_Per_Object_Constraint (Entity (Selector_Name (Lhs)))
7387 then
7388 -- If enclosing record is an Unchecked_Union, use formals
7389 -- corresponding to each discriminant. The name of the
7390 -- formal is that of the discriminant, with added suffix,
7391 -- see Exp_Ch3.Build_Record_Equality for details.
7393 if Is_Unchecked_Union (Scope (Entity (Selector_Name (Lhs))))
7394 then
7395 Discr :=
7396 First_Discriminant
7397 (Scope (Entity (Selector_Name (Lhs))));
7398 while Present (Discr) loop
7399 Append_Elmt
7400 (Make_Identifier (Loc,
7401 Chars => New_External_Name (Chars (Discr), 'A')),
7402 To => Lhs_Discr_Vals);
7403 Next_Discriminant (Discr);
7404 end loop;
7406 -- If enclosing record is of a non-Unchecked_Union type, it
7407 -- is possible to reference its discriminants directly.
7409 else
7410 Discr := First_Discriminant (Lhs_Type);
7411 while Present (Discr) loop
7412 Append_Elmt
7413 (Make_Selected_Component (Loc,
7414 Prefix => Prefix (Lhs),
7415 Selector_Name =>
7416 New_Copy
7417 (Get_Discriminant_Value (Discr,
7418 Lhs_Type,
7419 Stored_Constraint (Lhs_Type)))),
7420 To => Lhs_Discr_Vals);
7421 Next_Discriminant (Discr);
7422 end loop;
7423 end if;
7425 -- Otherwise operand is on object with a constrained type.
7426 -- Infer the discriminant values from the constraint.
7428 else
7430 Discr := First_Discriminant (Lhs_Type);
7431 while Present (Discr) loop
7432 Append_Elmt
7433 (New_Copy
7434 (Get_Discriminant_Value (Discr,
7435 Lhs_Type,
7436 Stored_Constraint (Lhs_Type))),
7437 To => Lhs_Discr_Vals);
7438 Next_Discriminant (Discr);
7439 end loop;
7440 end if;
7442 -- Similar processing for right operand of equality
7444 if Nkind (Rhs) = N_Selected_Component
7445 and then
7446 Has_Per_Object_Constraint (Entity (Selector_Name (Rhs)))
7447 then
7448 if Is_Unchecked_Union
7449 (Scope (Entity (Selector_Name (Rhs))))
7450 then
7451 Discr :=
7452 First_Discriminant
7453 (Scope (Entity (Selector_Name (Rhs))));
7454 while Present (Discr) loop
7455 Append_Elmt
7456 (Make_Identifier (Loc,
7457 Chars => New_External_Name (Chars (Discr), 'B')),
7458 To => Rhs_Discr_Vals);
7459 Next_Discriminant (Discr);
7460 end loop;
7462 else
7463 Discr := First_Discriminant (Rhs_Type);
7464 while Present (Discr) loop
7465 Append_Elmt
7466 (Make_Selected_Component (Loc,
7467 Prefix => Prefix (Rhs),
7468 Selector_Name =>
7469 New_Copy (Get_Discriminant_Value
7470 (Discr,
7471 Rhs_Type,
7472 Stored_Constraint (Rhs_Type)))),
7473 To => Rhs_Discr_Vals);
7474 Next_Discriminant (Discr);
7475 end loop;
7476 end if;
7478 else
7479 Discr := First_Discriminant (Rhs_Type);
7480 while Present (Discr) loop
7481 Append_Elmt
7482 (New_Copy (Get_Discriminant_Value
7483 (Discr,
7484 Rhs_Type,
7485 Stored_Constraint (Rhs_Type))),
7486 To => Rhs_Discr_Vals);
7487 Next_Discriminant (Discr);
7488 end loop;
7489 end if;
7491 -- Now merge the list of discriminant values so that values
7492 -- of corresponding discriminants are adjacent.
7494 declare
7495 Params : List_Id;
7496 L_Elmt : Elmt_Id;
7497 R_Elmt : Elmt_Id;
7499 begin
7500 Params := New_List (L_Exp, R_Exp);
7501 L_Elmt := First_Elmt (Lhs_Discr_Vals);
7502 R_Elmt := First_Elmt (Rhs_Discr_Vals);
7503 while Present (L_Elmt) loop
7504 Append_To (Params, Node (L_Elmt));
7505 Append_To (Params, Node (R_Elmt));
7506 Next_Elmt (L_Elmt);
7507 Next_Elmt (R_Elmt);
7508 end loop;
7510 Rewrite (N,
7511 Make_Function_Call (Loc,
7512 Name => New_Occurrence_Of (Eq, Loc),
7513 Parameter_Associations => Params));
7514 end;
7515 end;
7517 -- Normal case, not an unchecked union
7519 else
7520 Rewrite (N,
7521 Make_Function_Call (Loc,
7522 Name => New_Occurrence_Of (Eq, Loc),
7523 Parameter_Associations => New_List (L_Exp, R_Exp)));
7524 end if;
7526 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7527 end Build_Equality_Call;
7529 ------------------------------------
7530 -- Has_Unconstrained_UU_Component --
7531 ------------------------------------
7533 function Has_Unconstrained_UU_Component
7534 (Typ : Node_Id) return Boolean
7536 Tdef : constant Node_Id :=
7537 Type_Definition (Declaration_Node (Base_Type (Typ)));
7538 Clist : Node_Id;
7539 Vpart : Node_Id;
7541 function Component_Is_Unconstrained_UU
7542 (Comp : Node_Id) return Boolean;
7543 -- Determines whether the subtype of the component is an
7544 -- unconstrained Unchecked_Union.
7546 function Variant_Is_Unconstrained_UU
7547 (Variant : Node_Id) return Boolean;
7548 -- Determines whether a component of the variant has an unconstrained
7549 -- Unchecked_Union subtype.
7551 -----------------------------------
7552 -- Component_Is_Unconstrained_UU --
7553 -----------------------------------
7555 function Component_Is_Unconstrained_UU
7556 (Comp : Node_Id) return Boolean
7558 begin
7559 if Nkind (Comp) /= N_Component_Declaration then
7560 return False;
7561 end if;
7563 declare
7564 Sindic : constant Node_Id :=
7565 Subtype_Indication (Component_Definition (Comp));
7567 begin
7568 -- Unconstrained nominal type. In the case of a constraint
7569 -- present, the node kind would have been N_Subtype_Indication.
7571 if Nkind (Sindic) = N_Identifier then
7572 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
7573 end if;
7575 return False;
7576 end;
7577 end Component_Is_Unconstrained_UU;
7579 ---------------------------------
7580 -- Variant_Is_Unconstrained_UU --
7581 ---------------------------------
7583 function Variant_Is_Unconstrained_UU
7584 (Variant : Node_Id) return Boolean
7586 Clist : constant Node_Id := Component_List (Variant);
7588 begin
7589 if Is_Empty_List (Component_Items (Clist)) then
7590 return False;
7591 end if;
7593 -- We only need to test one component
7595 declare
7596 Comp : Node_Id := First (Component_Items (Clist));
7598 begin
7599 while Present (Comp) loop
7600 if Component_Is_Unconstrained_UU (Comp) then
7601 return True;
7602 end if;
7604 Next (Comp);
7605 end loop;
7606 end;
7608 -- None of the components withing the variant were of
7609 -- unconstrained Unchecked_Union type.
7611 return False;
7612 end Variant_Is_Unconstrained_UU;
7614 -- Start of processing for Has_Unconstrained_UU_Component
7616 begin
7617 if Null_Present (Tdef) then
7618 return False;
7619 end if;
7621 Clist := Component_List (Tdef);
7622 Vpart := Variant_Part (Clist);
7624 -- Inspect available components
7626 if Present (Component_Items (Clist)) then
7627 declare
7628 Comp : Node_Id := First (Component_Items (Clist));
7630 begin
7631 while Present (Comp) loop
7633 -- One component is sufficient
7635 if Component_Is_Unconstrained_UU (Comp) then
7636 return True;
7637 end if;
7639 Next (Comp);
7640 end loop;
7641 end;
7642 end if;
7644 -- Inspect available components withing variants
7646 if Present (Vpart) then
7647 declare
7648 Variant : Node_Id := First (Variants (Vpart));
7650 begin
7651 while Present (Variant) loop
7653 -- One component within a variant is sufficient
7655 if Variant_Is_Unconstrained_UU (Variant) then
7656 return True;
7657 end if;
7659 Next (Variant);
7660 end loop;
7661 end;
7662 end if;
7664 -- Neither the available components, nor the components inside the
7665 -- variant parts were of an unconstrained Unchecked_Union subtype.
7667 return False;
7668 end Has_Unconstrained_UU_Component;
7670 -- Start of processing for Expand_N_Op_Eq
7672 begin
7673 Binary_Op_Validity_Checks (N);
7675 -- Deal with private types
7677 if Ekind (Typl) = E_Private_Type then
7678 Typl := Underlying_Type (Typl);
7679 elsif Ekind (Typl) = E_Private_Subtype then
7680 Typl := Underlying_Type (Base_Type (Typl));
7681 else
7682 null;
7683 end if;
7685 -- It may happen in error situations that the underlying type is not
7686 -- set. The error will be detected later, here we just defend the
7687 -- expander code.
7689 if No (Typl) then
7690 return;
7691 end if;
7693 -- Now get the implementation base type (note that plain Base_Type here
7694 -- might lead us back to the private type, which is not what we want!)
7696 Typl := Implementation_Base_Type (Typl);
7698 -- Equality between variant records results in a call to a routine
7699 -- that has conditional tests of the discriminant value(s), and hence
7700 -- violates the No_Implicit_Conditionals restriction.
7702 if Has_Variant_Part (Typl) then
7703 declare
7704 Msg : Boolean;
7706 begin
7707 Check_Restriction (Msg, No_Implicit_Conditionals, N);
7709 if Msg then
7710 Error_Msg_N
7711 ("\comparison of variant records tests discriminants", N);
7712 return;
7713 end if;
7714 end;
7715 end if;
7717 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7718 -- means we no longer have a comparison operation, we are all done.
7720 Expand_Compare_Minimize_Eliminate_Overflow (N);
7722 if Nkind (N) /= N_Op_Eq then
7723 return;
7724 end if;
7726 -- Boolean types (requiring handling of non-standard case)
7728 if Is_Boolean_Type (Typl) then
7729 Adjust_Condition (Left_Opnd (N));
7730 Adjust_Condition (Right_Opnd (N));
7731 Set_Etype (N, Standard_Boolean);
7732 Adjust_Result_Type (N, Typ);
7734 -- Array types
7736 elsif Is_Array_Type (Typl) then
7738 -- If we are doing full validity checking, and it is possible for the
7739 -- array elements to be invalid then expand out array comparisons to
7740 -- make sure that we check the array elements.
7742 if Validity_Check_Operands
7743 and then not Is_Known_Valid (Component_Type (Typl))
7744 then
7745 declare
7746 Save_Force_Validity_Checks : constant Boolean :=
7747 Force_Validity_Checks;
7748 begin
7749 Force_Validity_Checks := True;
7750 Rewrite (N,
7751 Expand_Array_Equality
7753 Relocate_Node (Lhs),
7754 Relocate_Node (Rhs),
7755 Bodies,
7756 Typl));
7757 Insert_Actions (N, Bodies);
7758 Analyze_And_Resolve (N, Standard_Boolean);
7759 Force_Validity_Checks := Save_Force_Validity_Checks;
7760 end;
7762 -- Packed case where both operands are known aligned
7764 elsif Is_Bit_Packed_Array (Typl)
7765 and then not Is_Possibly_Unaligned_Object (Lhs)
7766 and then not Is_Possibly_Unaligned_Object (Rhs)
7767 then
7768 Expand_Packed_Eq (N);
7770 -- Where the component type is elementary we can use a block bit
7771 -- comparison (if supported on the target) exception in the case
7772 -- of floating-point (negative zero issues require element by
7773 -- element comparison), and atomic/VFA types (where we must be sure
7774 -- to load elements independently) and possibly unaligned arrays.
7776 elsif Is_Elementary_Type (Component_Type (Typl))
7777 and then not Is_Floating_Point_Type (Component_Type (Typl))
7778 and then not Is_Atomic_Or_VFA (Component_Type (Typl))
7779 and then not Is_Possibly_Unaligned_Object (Lhs)
7780 and then not Is_Possibly_Unaligned_Object (Rhs)
7781 and then Support_Composite_Compare_On_Target
7782 then
7783 null;
7785 -- For composite and floating-point cases, expand equality loop to
7786 -- make sure of using proper comparisons for tagged types, and
7787 -- correctly handling the floating-point case.
7789 else
7790 Rewrite (N,
7791 Expand_Array_Equality
7793 Relocate_Node (Lhs),
7794 Relocate_Node (Rhs),
7795 Bodies,
7796 Typl));
7797 Insert_Actions (N, Bodies, Suppress => All_Checks);
7798 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7799 end if;
7801 -- Record Types
7803 elsif Is_Record_Type (Typl) then
7805 -- For tagged types, use the primitive "="
7807 if Is_Tagged_Type (Typl) then
7809 -- No need to do anything else compiling under restriction
7810 -- No_Dispatching_Calls. During the semantic analysis we
7811 -- already notified such violation.
7813 if Restriction_Active (No_Dispatching_Calls) then
7814 return;
7815 end if;
7817 -- If this is derived from an untagged private type completed with
7818 -- a tagged type, it does not have a full view, so we use the
7819 -- primitive operations of the private type. This check should no
7820 -- longer be necessary when these types get their full views???
7822 if Is_Private_Type (A_Typ)
7823 and then not Is_Tagged_Type (A_Typ)
7824 and then Is_Derived_Type (A_Typ)
7825 and then No (Full_View (A_Typ))
7826 then
7827 -- Search for equality operation, checking that the operands
7828 -- have the same type. Note that we must find a matching entry,
7829 -- or something is very wrong.
7831 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
7833 while Present (Prim) loop
7834 exit when Chars (Node (Prim)) = Name_Op_Eq
7835 and then Etype (First_Formal (Node (Prim))) =
7836 Etype (Next_Formal (First_Formal (Node (Prim))))
7837 and then
7838 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7840 Next_Elmt (Prim);
7841 end loop;
7843 pragma Assert (Present (Prim));
7844 Op_Name := Node (Prim);
7846 -- Find the type's predefined equality or an overriding
7847 -- user-defined equality. The reason for not simply calling
7848 -- Find_Prim_Op here is that there may be a user-defined
7849 -- overloaded equality op that precedes the equality that we
7850 -- want, so we have to explicitly search (e.g., there could be
7851 -- an equality with two different parameter types).
7853 else
7854 if Is_Class_Wide_Type (Typl) then
7855 Typl := Find_Specific_Type (Typl);
7856 end if;
7858 Prim := First_Elmt (Primitive_Operations (Typl));
7859 while Present (Prim) loop
7860 exit when Chars (Node (Prim)) = Name_Op_Eq
7861 and then Etype (First_Formal (Node (Prim))) =
7862 Etype (Next_Formal (First_Formal (Node (Prim))))
7863 and then
7864 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7866 Next_Elmt (Prim);
7867 end loop;
7869 pragma Assert (Present (Prim));
7870 Op_Name := Node (Prim);
7871 end if;
7873 Build_Equality_Call (Op_Name);
7875 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7876 -- predefined equality operator for a type which has a subcomponent
7877 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7879 elsif Has_Unconstrained_UU_Component (Typl) then
7880 Insert_Action (N,
7881 Make_Raise_Program_Error (Loc,
7882 Reason => PE_Unchecked_Union_Restriction));
7884 -- Prevent Gigi from generating incorrect code by rewriting the
7885 -- equality as a standard False. (is this documented somewhere???)
7887 Rewrite (N,
7888 New_Occurrence_Of (Standard_False, Loc));
7890 elsif Is_Unchecked_Union (Typl) then
7892 -- If we can infer the discriminants of the operands, we make a
7893 -- call to the TSS equality function.
7895 if Has_Inferable_Discriminants (Lhs)
7896 and then
7897 Has_Inferable_Discriminants (Rhs)
7898 then
7899 Build_Equality_Call
7900 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7902 else
7903 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7904 -- the predefined equality operator for an Unchecked_Union type
7905 -- if either of the operands lack inferable discriminants.
7907 Insert_Action (N,
7908 Make_Raise_Program_Error (Loc,
7909 Reason => PE_Unchecked_Union_Restriction));
7911 -- Emit a warning on source equalities only, otherwise the
7912 -- message may appear out of place due to internal use. The
7913 -- warning is unconditional because it is required by the
7914 -- language.
7916 if Comes_From_Source (N) then
7917 Error_Msg_N
7918 ("Unchecked_Union discriminants cannot be determined??",
7920 Error_Msg_N
7921 ("\Program_Error will be raised for equality operation??",
7923 end if;
7925 -- Prevent Gigi from generating incorrect code by rewriting
7926 -- the equality as a standard False (documented where???).
7928 Rewrite (N,
7929 New_Occurrence_Of (Standard_False, Loc));
7930 end if;
7932 -- If a type support function is present (for complex cases), use it
7934 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
7935 Build_Equality_Call
7936 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7938 -- When comparing two Bounded_Strings, use the primitive equality of
7939 -- the root Super_String type.
7941 elsif Is_Bounded_String (Typl) then
7942 Prim :=
7943 First_Elmt (Collect_Primitive_Operations (Root_Type (Typl)));
7945 while Present (Prim) loop
7946 exit when Chars (Node (Prim)) = Name_Op_Eq
7947 and then Etype (First_Formal (Node (Prim))) =
7948 Etype (Next_Formal (First_Formal (Node (Prim))))
7949 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7951 Next_Elmt (Prim);
7952 end loop;
7954 -- A Super_String type should always have a primitive equality
7956 pragma Assert (Present (Prim));
7957 Build_Equality_Call (Node (Prim));
7959 -- Otherwise expand the component by component equality. Note that
7960 -- we never use block-bit comparisons for records, because of the
7961 -- problems with gaps. The back end will often be able to recombine
7962 -- the separate comparisons that we generate here.
7964 else
7965 Remove_Side_Effects (Lhs);
7966 Remove_Side_Effects (Rhs);
7967 Rewrite (N,
7968 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
7970 Insert_Actions (N, Bodies, Suppress => All_Checks);
7971 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7972 end if;
7973 end if;
7975 -- Test if result is known at compile time
7977 Rewrite_Comparison (N);
7979 -- Special optimization of length comparison
7981 Optimize_Length_Comparison (N);
7983 -- One more special case: if we have a comparison of X'Result = expr
7984 -- in floating-point, then if not already there, change expr to be
7985 -- f'Machine (expr) to eliminate surprise from extra precision.
7987 if Is_Floating_Point_Type (Typl)
7988 and then Nkind (Original_Node (Lhs)) = N_Attribute_Reference
7989 and then Attribute_Name (Original_Node (Lhs)) = Name_Result
7990 then
7991 -- Stick in the Typ'Machine call if not already there
7993 if Nkind (Rhs) /= N_Attribute_Reference
7994 or else Attribute_Name (Rhs) /= Name_Machine
7995 then
7996 Rewrite (Rhs,
7997 Make_Attribute_Reference (Loc,
7998 Prefix => New_Occurrence_Of (Typl, Loc),
7999 Attribute_Name => Name_Machine,
8000 Expressions => New_List (Relocate_Node (Rhs))));
8001 Analyze_And_Resolve (Rhs, Typl);
8002 end if;
8003 end if;
8004 end Expand_N_Op_Eq;
8006 -----------------------
8007 -- Expand_N_Op_Expon --
8008 -----------------------
8010 procedure Expand_N_Op_Expon (N : Node_Id) is
8011 Loc : constant Source_Ptr := Sloc (N);
8012 Ovflo : constant Boolean := Do_Overflow_Check (N);
8013 Typ : constant Entity_Id := Etype (N);
8014 Rtyp : constant Entity_Id := Root_Type (Typ);
8016 Bastyp : Entity_Id;
8018 function Wrap_MA (Exp : Node_Id) return Node_Id;
8019 -- Given an expression Exp, if the root type is Float or Long_Float,
8020 -- then wrap the expression in a call of Bastyp'Machine, to stop any
8021 -- extra precision. This is done to ensure that X**A = X**B when A is
8022 -- a static constant and B is a variable with the same value. For any
8023 -- other type, the node Exp is returned unchanged.
8025 -------------
8026 -- Wrap_MA --
8027 -------------
8029 function Wrap_MA (Exp : Node_Id) return Node_Id is
8030 Loc : constant Source_Ptr := Sloc (Exp);
8032 begin
8033 if Rtyp = Standard_Float or else Rtyp = Standard_Long_Float then
8034 return
8035 Make_Attribute_Reference (Loc,
8036 Attribute_Name => Name_Machine,
8037 Prefix => New_Occurrence_Of (Bastyp, Loc),
8038 Expressions => New_List (Relocate_Node (Exp)));
8039 else
8040 return Exp;
8041 end if;
8042 end Wrap_MA;
8044 -- Local variables
8046 Base : Node_Id;
8047 Ent : Entity_Id;
8048 Etyp : Entity_Id;
8049 Exp : Node_Id;
8050 Exptyp : Entity_Id;
8051 Expv : Uint;
8052 Rent : RE_Id;
8053 Temp : Node_Id;
8054 Xnode : Node_Id;
8056 -- Start of processing for Expand_N_Op_Expon
8058 begin
8059 Binary_Op_Validity_Checks (N);
8061 -- CodePeer wants to see the unexpanded N_Op_Expon node
8063 if CodePeer_Mode then
8064 return;
8065 end if;
8067 -- Relocation of left and right operands must be done after performing
8068 -- the validity checks since the generation of validation checks may
8069 -- remove side effects.
8071 Base := Relocate_Node (Left_Opnd (N));
8072 Bastyp := Etype (Base);
8073 Exp := Relocate_Node (Right_Opnd (N));
8074 Exptyp := Etype (Exp);
8076 -- If either operand is of a private type, then we have the use of an
8077 -- intrinsic operator, and we get rid of the privateness, by using root
8078 -- types of underlying types for the actual operation. Otherwise the
8079 -- private types will cause trouble if we expand multiplications or
8080 -- shifts etc. We also do this transformation if the result type is
8081 -- different from the base type.
8083 if Is_Private_Type (Etype (Base))
8084 or else Is_Private_Type (Typ)
8085 or else Is_Private_Type (Exptyp)
8086 or else Rtyp /= Root_Type (Bastyp)
8087 then
8088 declare
8089 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
8090 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
8091 begin
8092 Rewrite (N,
8093 Unchecked_Convert_To (Typ,
8094 Make_Op_Expon (Loc,
8095 Left_Opnd => Unchecked_Convert_To (Bt, Base),
8096 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
8097 Analyze_And_Resolve (N, Typ);
8098 return;
8099 end;
8100 end if;
8102 -- Check for MINIMIZED/ELIMINATED overflow mode
8104 if Minimized_Eliminated_Overflow_Check (N) then
8105 Apply_Arithmetic_Overflow_Check (N);
8106 return;
8107 end if;
8109 -- Test for case of known right argument where we can replace the
8110 -- exponentiation by an equivalent expression using multiplication.
8112 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
8113 -- configurable run-time mode, we may not have the exponentiation
8114 -- routine available, and we don't want the legality of the program
8115 -- to depend on how clever the compiler is in knowing values.
8117 if CRT_Safe_Compile_Time_Known_Value (Exp) then
8118 Expv := Expr_Value (Exp);
8120 -- We only fold small non-negative exponents. You might think we
8121 -- could fold small negative exponents for the real case, but we
8122 -- can't because we are required to raise Constraint_Error for
8123 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
8124 -- See ACVC test C4A012B, and it is not worth generating the test.
8126 -- For small negative exponents, we return the reciprocal of
8127 -- the folding of the exponentiation for the opposite (positive)
8128 -- exponent, as required by Ada RM 4.5.6(11/3).
8130 if abs Expv <= 4 then
8132 -- X ** 0 = 1 (or 1.0)
8134 if Expv = 0 then
8136 -- Call Remove_Side_Effects to ensure that any side effects
8137 -- in the ignored left operand (in particular function calls
8138 -- to user defined functions) are properly executed.
8140 Remove_Side_Effects (Base);
8142 if Ekind (Typ) in Integer_Kind then
8143 Xnode := Make_Integer_Literal (Loc, Intval => 1);
8144 else
8145 Xnode := Make_Real_Literal (Loc, Ureal_1);
8146 end if;
8148 -- X ** 1 = X
8150 elsif Expv = 1 then
8151 Xnode := Base;
8153 -- X ** 2 = X * X
8155 elsif Expv = 2 then
8156 Xnode :=
8157 Wrap_MA (
8158 Make_Op_Multiply (Loc,
8159 Left_Opnd => Duplicate_Subexpr (Base),
8160 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)));
8162 -- X ** 3 = X * X * X
8164 elsif Expv = 3 then
8165 Xnode :=
8166 Wrap_MA (
8167 Make_Op_Multiply (Loc,
8168 Left_Opnd =>
8169 Make_Op_Multiply (Loc,
8170 Left_Opnd => Duplicate_Subexpr (Base),
8171 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
8172 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)));
8174 -- X ** 4 ->
8176 -- do
8177 -- En : constant base'type := base * base;
8178 -- in
8179 -- En * En
8181 elsif Expv = 4 then
8182 Temp := Make_Temporary (Loc, 'E', Base);
8184 Xnode :=
8185 Make_Expression_With_Actions (Loc,
8186 Actions => New_List (
8187 Make_Object_Declaration (Loc,
8188 Defining_Identifier => Temp,
8189 Constant_Present => True,
8190 Object_Definition => New_Occurrence_Of (Typ, Loc),
8191 Expression =>
8192 Wrap_MA (
8193 Make_Op_Multiply (Loc,
8194 Left_Opnd =>
8195 Duplicate_Subexpr (Base),
8196 Right_Opnd =>
8197 Duplicate_Subexpr_No_Checks (Base))))),
8199 Expression =>
8200 Wrap_MA (
8201 Make_Op_Multiply (Loc,
8202 Left_Opnd => New_Occurrence_Of (Temp, Loc),
8203 Right_Opnd => New_Occurrence_Of (Temp, Loc))));
8205 -- X ** N = 1.0 / X ** (-N)
8206 -- N in -4 .. -1
8208 else
8209 pragma Assert
8210 (Expv = -1 or Expv = -2 or Expv = -3 or Expv = -4);
8212 Xnode :=
8213 Make_Op_Divide (Loc,
8214 Left_Opnd =>
8215 Make_Float_Literal (Loc,
8216 Radix => Uint_1,
8217 Significand => Uint_1,
8218 Exponent => Uint_0),
8219 Right_Opnd =>
8220 Make_Op_Expon (Loc,
8221 Left_Opnd => Duplicate_Subexpr (Base),
8222 Right_Opnd =>
8223 Make_Integer_Literal (Loc,
8224 Intval => -Expv)));
8225 end if;
8227 Rewrite (N, Xnode);
8228 Analyze_And_Resolve (N, Typ);
8229 return;
8230 end if;
8231 end if;
8233 -- Deal with optimizing 2 ** expression to shift where possible
8235 -- Note: we used to check that Exptyp was an unsigned type. But that is
8236 -- an unnecessary check, since if Exp is negative, we have a run-time
8237 -- error that is either caught (so we get the right result) or we have
8238 -- suppressed the check, in which case the code is erroneous anyway.
8240 if Is_Integer_Type (Rtyp)
8242 -- The base value must be "safe compile-time known", and exactly 2
8244 and then Nkind (Base) = N_Integer_Literal
8245 and then CRT_Safe_Compile_Time_Known_Value (Base)
8246 and then Expr_Value (Base) = Uint_2
8248 -- We only handle cases where the right type is a integer
8250 and then Is_Integer_Type (Root_Type (Exptyp))
8251 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
8253 -- This transformation is not applicable for a modular type with a
8254 -- nonbinary modulus because we do not handle modular reduction in
8255 -- a correct manner if we attempt this transformation in this case.
8257 and then not Non_Binary_Modulus (Typ)
8258 then
8259 -- Handle the cases where our parent is a division or multiplication
8260 -- specially. In these cases we can convert to using a shift at the
8261 -- parent level if we are not doing overflow checking, since it is
8262 -- too tricky to combine the overflow check at the parent level.
8264 if not Ovflo
8265 and then Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply)
8266 then
8267 declare
8268 P : constant Node_Id := Parent (N);
8269 L : constant Node_Id := Left_Opnd (P);
8270 R : constant Node_Id := Right_Opnd (P);
8272 begin
8273 if (Nkind (P) = N_Op_Multiply
8274 and then
8275 ((Is_Integer_Type (Etype (L)) and then R = N)
8276 or else
8277 (Is_Integer_Type (Etype (R)) and then L = N))
8278 and then not Do_Overflow_Check (P))
8280 or else
8281 (Nkind (P) = N_Op_Divide
8282 and then Is_Integer_Type (Etype (L))
8283 and then Is_Unsigned_Type (Etype (L))
8284 and then R = N
8285 and then not Do_Overflow_Check (P))
8286 then
8287 Set_Is_Power_Of_2_For_Shift (N);
8288 return;
8289 end if;
8290 end;
8292 -- Here we just have 2 ** N on its own, so we can convert this to a
8293 -- shift node. We are prepared to deal with overflow here, and we
8294 -- also have to handle proper modular reduction for binary modular.
8296 else
8297 declare
8298 OK : Boolean;
8299 Lo : Uint;
8300 Hi : Uint;
8302 MaxS : Uint;
8303 -- Maximum shift count with no overflow
8305 TestS : Boolean;
8306 -- Set True if we must test the shift count
8308 Test_Gt : Node_Id;
8309 -- Node for test against TestS
8311 begin
8312 -- Compute maximum shift based on the underlying size. For a
8313 -- modular type this is one less than the size.
8315 if Is_Modular_Integer_Type (Typ) then
8317 -- For modular integer types, this is the size of the value
8318 -- being shifted minus one. Any larger values will cause
8319 -- modular reduction to a result of zero. Note that we do
8320 -- want the RM_Size here (e.g. mod 2 ** 7, we want a result
8321 -- of 6, since 2**7 should be reduced to zero).
8323 MaxS := RM_Size (Rtyp) - 1;
8325 -- For signed integer types, we use the size of the value
8326 -- being shifted minus 2. Larger values cause overflow.
8328 else
8329 MaxS := Esize (Rtyp) - 2;
8330 end if;
8332 -- Determine range to see if it can be larger than MaxS
8334 Determine_Range
8335 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
8336 TestS := (not OK) or else Hi > MaxS;
8338 -- Signed integer case
8340 if Is_Signed_Integer_Type (Typ) then
8342 -- Generate overflow check if overflow is active. Note that
8343 -- we can simply ignore the possibility of overflow if the
8344 -- flag is not set (means that overflow cannot happen or
8345 -- that overflow checks are suppressed).
8347 if Ovflo and TestS then
8348 Insert_Action (N,
8349 Make_Raise_Constraint_Error (Loc,
8350 Condition =>
8351 Make_Op_Gt (Loc,
8352 Left_Opnd => Duplicate_Subexpr (Right_Opnd (N)),
8353 Right_Opnd => Make_Integer_Literal (Loc, MaxS)),
8354 Reason => CE_Overflow_Check_Failed));
8355 end if;
8357 -- Now rewrite node as Shift_Left (1, right-operand)
8359 Rewrite (N,
8360 Make_Op_Shift_Left (Loc,
8361 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
8362 Right_Opnd => Right_Opnd (N)));
8364 -- Modular integer case
8366 else pragma Assert (Is_Modular_Integer_Type (Typ));
8368 -- If shift count can be greater than MaxS, we need to wrap
8369 -- the shift in a test that will reduce the result value to
8370 -- zero if this shift count is exceeded.
8372 if TestS then
8374 -- Note: build node for the comparison first, before we
8375 -- reuse the Right_Opnd, so that we have proper parents
8376 -- in place for the Duplicate_Subexpr call.
8378 Test_Gt :=
8379 Make_Op_Gt (Loc,
8380 Left_Opnd => Duplicate_Subexpr (Right_Opnd (N)),
8381 Right_Opnd => Make_Integer_Literal (Loc, MaxS));
8383 Rewrite (N,
8384 Make_If_Expression (Loc,
8385 Expressions => New_List (
8386 Test_Gt,
8387 Make_Integer_Literal (Loc, Uint_0),
8388 Make_Op_Shift_Left (Loc,
8389 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
8390 Right_Opnd => Right_Opnd (N)))));
8392 -- If we know shift count cannot be greater than MaxS, then
8393 -- it is safe to just rewrite as a shift with no test.
8395 else
8396 Rewrite (N,
8397 Make_Op_Shift_Left (Loc,
8398 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
8399 Right_Opnd => Right_Opnd (N)));
8400 end if;
8401 end if;
8403 Analyze_And_Resolve (N, Typ);
8404 return;
8405 end;
8406 end if;
8407 end if;
8409 -- Fall through if exponentiation must be done using a runtime routine
8411 -- First deal with modular case
8413 if Is_Modular_Integer_Type (Rtyp) then
8415 -- Nonbinary modular case, we call the special exponentiation
8416 -- routine for the nonbinary case, converting the argument to
8417 -- Long_Long_Integer and passing the modulus value. Then the
8418 -- result is converted back to the base type.
8420 if Non_Binary_Modulus (Rtyp) then
8421 Rewrite (N,
8422 Convert_To (Typ,
8423 Make_Function_Call (Loc,
8424 Name =>
8425 New_Occurrence_Of (RTE (RE_Exp_Modular), Loc),
8426 Parameter_Associations => New_List (
8427 Convert_To (RTE (RE_Unsigned), Base),
8428 Make_Integer_Literal (Loc, Modulus (Rtyp)),
8429 Exp))));
8431 -- Binary modular case, in this case, we call one of two routines,
8432 -- either the unsigned integer case, or the unsigned long long
8433 -- integer case, with a final "and" operation to do the required mod.
8435 else
8436 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
8437 Ent := RTE (RE_Exp_Unsigned);
8438 else
8439 Ent := RTE (RE_Exp_Long_Long_Unsigned);
8440 end if;
8442 Rewrite (N,
8443 Convert_To (Typ,
8444 Make_Op_And (Loc,
8445 Left_Opnd =>
8446 Make_Function_Call (Loc,
8447 Name => New_Occurrence_Of (Ent, Loc),
8448 Parameter_Associations => New_List (
8449 Convert_To (Etype (First_Formal (Ent)), Base),
8450 Exp)),
8451 Right_Opnd =>
8452 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
8454 end if;
8456 -- Common exit point for modular type case
8458 Analyze_And_Resolve (N, Typ);
8459 return;
8461 -- Signed integer cases, done using either Integer or Long_Long_Integer.
8462 -- It is not worth having routines for Short_[Short_]Integer, since for
8463 -- most machines it would not help, and it would generate more code that
8464 -- might need certification when a certified run time is required.
8466 -- In the integer cases, we have two routines, one for when overflow
8467 -- checks are required, and one when they are not required, since there
8468 -- is a real gain in omitting checks on many machines.
8470 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
8471 or else (Rtyp = Base_Type (Standard_Long_Integer)
8472 and then
8473 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
8474 or else Rtyp = Universal_Integer
8475 then
8476 Etyp := Standard_Long_Long_Integer;
8478 if Ovflo then
8479 Rent := RE_Exp_Long_Long_Integer;
8480 else
8481 Rent := RE_Exn_Long_Long_Integer;
8482 end if;
8484 elsif Is_Signed_Integer_Type (Rtyp) then
8485 Etyp := Standard_Integer;
8487 if Ovflo then
8488 Rent := RE_Exp_Integer;
8489 else
8490 Rent := RE_Exn_Integer;
8491 end if;
8493 -- Floating-point cases. We do not need separate routines for the
8494 -- overflow case here, since in the case of floating-point, we generate
8495 -- infinities anyway as a rule (either that or we automatically trap
8496 -- overflow), and if there is an infinity generated and a range check
8497 -- is required, the check will fail anyway.
8499 -- Historical note: we used to convert everything to Long_Long_Float
8500 -- and call a single common routine, but this had the undesirable effect
8501 -- of giving different results for small static exponent values and the
8502 -- same dynamic values.
8504 else
8505 pragma Assert (Is_Floating_Point_Type (Rtyp));
8507 if Rtyp = Standard_Float then
8508 Etyp := Standard_Float;
8509 Rent := RE_Exn_Float;
8511 elsif Rtyp = Standard_Long_Float then
8512 Etyp := Standard_Long_Float;
8513 Rent := RE_Exn_Long_Float;
8515 else
8516 Etyp := Standard_Long_Long_Float;
8517 Rent := RE_Exn_Long_Long_Float;
8518 end if;
8519 end if;
8521 -- Common processing for integer cases and floating-point cases.
8522 -- If we are in the right type, we can call runtime routine directly
8524 if Typ = Etyp
8525 and then Rtyp /= Universal_Integer
8526 and then Rtyp /= Universal_Real
8527 then
8528 Rewrite (N,
8529 Wrap_MA (
8530 Make_Function_Call (Loc,
8531 Name => New_Occurrence_Of (RTE (Rent), Loc),
8532 Parameter_Associations => New_List (Base, Exp))));
8534 -- Otherwise we have to introduce conversions (conversions are also
8535 -- required in the universal cases, since the runtime routine is
8536 -- typed using one of the standard types).
8538 else
8539 Rewrite (N,
8540 Convert_To (Typ,
8541 Make_Function_Call (Loc,
8542 Name => New_Occurrence_Of (RTE (Rent), Loc),
8543 Parameter_Associations => New_List (
8544 Convert_To (Etyp, Base),
8545 Exp))));
8546 end if;
8548 Analyze_And_Resolve (N, Typ);
8549 return;
8551 exception
8552 when RE_Not_Available =>
8553 return;
8554 end Expand_N_Op_Expon;
8556 --------------------
8557 -- Expand_N_Op_Ge --
8558 --------------------
8560 procedure Expand_N_Op_Ge (N : Node_Id) is
8561 Typ : constant Entity_Id := Etype (N);
8562 Op1 : constant Node_Id := Left_Opnd (N);
8563 Op2 : constant Node_Id := Right_Opnd (N);
8564 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8566 begin
8567 Binary_Op_Validity_Checks (N);
8569 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8570 -- means we no longer have a comparison operation, we are all done.
8572 Expand_Compare_Minimize_Eliminate_Overflow (N);
8574 if Nkind (N) /= N_Op_Ge then
8575 return;
8576 end if;
8578 -- Array type case
8580 if Is_Array_Type (Typ1) then
8581 Expand_Array_Comparison (N);
8582 return;
8583 end if;
8585 -- Deal with boolean operands
8587 if Is_Boolean_Type (Typ1) then
8588 Adjust_Condition (Op1);
8589 Adjust_Condition (Op2);
8590 Set_Etype (N, Standard_Boolean);
8591 Adjust_Result_Type (N, Typ);
8592 end if;
8594 Rewrite_Comparison (N);
8596 Optimize_Length_Comparison (N);
8597 end Expand_N_Op_Ge;
8599 --------------------
8600 -- Expand_N_Op_Gt --
8601 --------------------
8603 procedure Expand_N_Op_Gt (N : Node_Id) is
8604 Typ : constant Entity_Id := Etype (N);
8605 Op1 : constant Node_Id := Left_Opnd (N);
8606 Op2 : constant Node_Id := Right_Opnd (N);
8607 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8609 begin
8610 Binary_Op_Validity_Checks (N);
8612 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8613 -- means we no longer have a comparison operation, we are all done.
8615 Expand_Compare_Minimize_Eliminate_Overflow (N);
8617 if Nkind (N) /= N_Op_Gt then
8618 return;
8619 end if;
8621 -- Deal with array type operands
8623 if Is_Array_Type (Typ1) then
8624 Expand_Array_Comparison (N);
8625 return;
8626 end if;
8628 -- Deal with boolean type operands
8630 if Is_Boolean_Type (Typ1) then
8631 Adjust_Condition (Op1);
8632 Adjust_Condition (Op2);
8633 Set_Etype (N, Standard_Boolean);
8634 Adjust_Result_Type (N, Typ);
8635 end if;
8637 Rewrite_Comparison (N);
8639 Optimize_Length_Comparison (N);
8640 end Expand_N_Op_Gt;
8642 --------------------
8643 -- Expand_N_Op_Le --
8644 --------------------
8646 procedure Expand_N_Op_Le (N : Node_Id) is
8647 Typ : constant Entity_Id := Etype (N);
8648 Op1 : constant Node_Id := Left_Opnd (N);
8649 Op2 : constant Node_Id := Right_Opnd (N);
8650 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8652 begin
8653 Binary_Op_Validity_Checks (N);
8655 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8656 -- means we no longer have a comparison operation, we are all done.
8658 Expand_Compare_Minimize_Eliminate_Overflow (N);
8660 if Nkind (N) /= N_Op_Le then
8661 return;
8662 end if;
8664 -- Deal with array type operands
8666 if Is_Array_Type (Typ1) then
8667 Expand_Array_Comparison (N);
8668 return;
8669 end if;
8671 -- Deal with Boolean type operands
8673 if Is_Boolean_Type (Typ1) then
8674 Adjust_Condition (Op1);
8675 Adjust_Condition (Op2);
8676 Set_Etype (N, Standard_Boolean);
8677 Adjust_Result_Type (N, Typ);
8678 end if;
8680 Rewrite_Comparison (N);
8682 Optimize_Length_Comparison (N);
8683 end Expand_N_Op_Le;
8685 --------------------
8686 -- Expand_N_Op_Lt --
8687 --------------------
8689 procedure Expand_N_Op_Lt (N : Node_Id) is
8690 Typ : constant Entity_Id := Etype (N);
8691 Op1 : constant Node_Id := Left_Opnd (N);
8692 Op2 : constant Node_Id := Right_Opnd (N);
8693 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8695 begin
8696 Binary_Op_Validity_Checks (N);
8698 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8699 -- means we no longer have a comparison operation, we are all done.
8701 Expand_Compare_Minimize_Eliminate_Overflow (N);
8703 if Nkind (N) /= N_Op_Lt then
8704 return;
8705 end if;
8707 -- Deal with array type operands
8709 if Is_Array_Type (Typ1) then
8710 Expand_Array_Comparison (N);
8711 return;
8712 end if;
8714 -- Deal with Boolean type operands
8716 if Is_Boolean_Type (Typ1) then
8717 Adjust_Condition (Op1);
8718 Adjust_Condition (Op2);
8719 Set_Etype (N, Standard_Boolean);
8720 Adjust_Result_Type (N, Typ);
8721 end if;
8723 Rewrite_Comparison (N);
8725 Optimize_Length_Comparison (N);
8726 end Expand_N_Op_Lt;
8728 -----------------------
8729 -- Expand_N_Op_Minus --
8730 -----------------------
8732 procedure Expand_N_Op_Minus (N : Node_Id) is
8733 Loc : constant Source_Ptr := Sloc (N);
8734 Typ : constant Entity_Id := Etype (N);
8736 begin
8737 Unary_Op_Validity_Checks (N);
8739 -- Check for MINIMIZED/ELIMINATED overflow mode
8741 if Minimized_Eliminated_Overflow_Check (N) then
8742 Apply_Arithmetic_Overflow_Check (N);
8743 return;
8744 end if;
8746 if not Backend_Overflow_Checks_On_Target
8747 and then Is_Signed_Integer_Type (Etype (N))
8748 and then Do_Overflow_Check (N)
8749 then
8750 -- Software overflow checking expands -expr into (0 - expr)
8752 Rewrite (N,
8753 Make_Op_Subtract (Loc,
8754 Left_Opnd => Make_Integer_Literal (Loc, 0),
8755 Right_Opnd => Right_Opnd (N)));
8757 Analyze_And_Resolve (N, Typ);
8758 end if;
8760 Expand_Nonbinary_Modular_Op (N);
8761 end Expand_N_Op_Minus;
8763 ---------------------
8764 -- Expand_N_Op_Mod --
8765 ---------------------
8767 procedure Expand_N_Op_Mod (N : Node_Id) is
8768 Loc : constant Source_Ptr := Sloc (N);
8769 Typ : constant Entity_Id := Etype (N);
8770 DDC : constant Boolean := Do_Division_Check (N);
8772 Left : Node_Id;
8773 Right : Node_Id;
8775 LLB : Uint;
8776 Llo : Uint;
8777 Lhi : Uint;
8778 LOK : Boolean;
8779 Rlo : Uint;
8780 Rhi : Uint;
8781 ROK : Boolean;
8783 pragma Warnings (Off, Lhi);
8785 begin
8786 Binary_Op_Validity_Checks (N);
8788 -- Check for MINIMIZED/ELIMINATED overflow mode
8790 if Minimized_Eliminated_Overflow_Check (N) then
8791 Apply_Arithmetic_Overflow_Check (N);
8792 return;
8793 end if;
8795 if Is_Integer_Type (Etype (N)) then
8796 Apply_Divide_Checks (N);
8798 -- All done if we don't have a MOD any more, which can happen as a
8799 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8801 if Nkind (N) /= N_Op_Mod then
8802 return;
8803 end if;
8804 end if;
8806 -- Proceed with expansion of mod operator
8808 Left := Left_Opnd (N);
8809 Right := Right_Opnd (N);
8811 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
8812 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
8814 -- Convert mod to rem if operands are both known to be non-negative, or
8815 -- both known to be non-positive (these are the cases in which rem and
8816 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8817 -- likely that this will improve the quality of code, (the operation now
8818 -- corresponds to the hardware remainder), and it does not seem likely
8819 -- that it could be harmful. It also avoids some cases of the elaborate
8820 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8822 if (LOK and ROK)
8823 and then ((Llo >= 0 and then Rlo >= 0)
8824 or else
8825 (Lhi <= 0 and then Rhi <= 0))
8826 then
8827 Rewrite (N,
8828 Make_Op_Rem (Sloc (N),
8829 Left_Opnd => Left_Opnd (N),
8830 Right_Opnd => Right_Opnd (N)));
8832 -- Instead of reanalyzing the node we do the analysis manually. This
8833 -- avoids anomalies when the replacement is done in an instance and
8834 -- is epsilon more efficient.
8836 Set_Entity (N, Standard_Entity (S_Op_Rem));
8837 Set_Etype (N, Typ);
8838 Set_Do_Division_Check (N, DDC);
8839 Expand_N_Op_Rem (N);
8840 Set_Analyzed (N);
8841 return;
8843 -- Otherwise, normal mod processing
8845 else
8846 -- Apply optimization x mod 1 = 0. We don't really need that with
8847 -- gcc, but it is useful with other back ends and is certainly
8848 -- harmless.
8850 if Is_Integer_Type (Etype (N))
8851 and then Compile_Time_Known_Value (Right)
8852 and then Expr_Value (Right) = Uint_1
8853 then
8854 -- Call Remove_Side_Effects to ensure that any side effects in
8855 -- the ignored left operand (in particular function calls to
8856 -- user defined functions) are properly executed.
8858 Remove_Side_Effects (Left);
8860 Rewrite (N, Make_Integer_Literal (Loc, 0));
8861 Analyze_And_Resolve (N, Typ);
8862 return;
8863 end if;
8865 -- If we still have a mod operator and we are in Modify_Tree_For_C
8866 -- mode, and we have a signed integer type, then here is where we do
8867 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8868 -- for the special handling of the annoying case of largest negative
8869 -- number mod minus one.
8871 if Nkind (N) = N_Op_Mod
8872 and then Is_Signed_Integer_Type (Typ)
8873 and then Modify_Tree_For_C
8874 then
8875 -- In the general case, we expand A mod B as
8877 -- Tnn : constant typ := A rem B;
8878 -- ..
8879 -- (if (A >= 0) = (B >= 0) then Tnn
8880 -- elsif Tnn = 0 then 0
8881 -- else Tnn + B)
8883 -- The comparison can be written simply as A >= 0 if we know that
8884 -- B >= 0 which is a very common case.
8886 -- An important optimization is when B is known at compile time
8887 -- to be 2**K for some constant. In this case we can simply AND
8888 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8889 -- and that works for both the positive and negative cases.
8891 declare
8892 P2 : constant Nat := Power_Of_Two (Right);
8894 begin
8895 if P2 /= 0 then
8896 Rewrite (N,
8897 Unchecked_Convert_To (Typ,
8898 Make_Op_And (Loc,
8899 Left_Opnd =>
8900 Unchecked_Convert_To
8901 (Corresponding_Unsigned_Type (Typ), Left),
8902 Right_Opnd =>
8903 Make_Integer_Literal (Loc, 2 ** P2 - 1))));
8904 Analyze_And_Resolve (N, Typ);
8905 return;
8906 end if;
8907 end;
8909 -- Here for the full rewrite
8911 declare
8912 Tnn : constant Entity_Id := Make_Temporary (Sloc (N), 'T', N);
8913 Cmp : Node_Id;
8915 begin
8916 Cmp :=
8917 Make_Op_Ge (Loc,
8918 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
8919 Right_Opnd => Make_Integer_Literal (Loc, 0));
8921 if not LOK or else Rlo < 0 then
8922 Cmp :=
8923 Make_Op_Eq (Loc,
8924 Left_Opnd => Cmp,
8925 Right_Opnd =>
8926 Make_Op_Ge (Loc,
8927 Left_Opnd => Duplicate_Subexpr_No_Checks (Right),
8928 Right_Opnd => Make_Integer_Literal (Loc, 0)));
8929 end if;
8931 Insert_Action (N,
8932 Make_Object_Declaration (Loc,
8933 Defining_Identifier => Tnn,
8934 Constant_Present => True,
8935 Object_Definition => New_Occurrence_Of (Typ, Loc),
8936 Expression =>
8937 Make_Op_Rem (Loc,
8938 Left_Opnd => Left,
8939 Right_Opnd => Right)));
8941 Rewrite (N,
8942 Make_If_Expression (Loc,
8943 Expressions => New_List (
8944 Cmp,
8945 New_Occurrence_Of (Tnn, Loc),
8946 Make_If_Expression (Loc,
8947 Is_Elsif => True,
8948 Expressions => New_List (
8949 Make_Op_Eq (Loc,
8950 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8951 Right_Opnd => Make_Integer_Literal (Loc, 0)),
8952 Make_Integer_Literal (Loc, 0),
8953 Make_Op_Add (Loc,
8954 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8955 Right_Opnd =>
8956 Duplicate_Subexpr_No_Checks (Right)))))));
8958 Analyze_And_Resolve (N, Typ);
8959 return;
8960 end;
8961 end if;
8963 -- Deal with annoying case of largest negative number mod minus one.
8964 -- Gigi may not handle this case correctly, because on some targets,
8965 -- the mod value is computed using a divide instruction which gives
8966 -- an overflow trap for this case.
8968 -- It would be a bit more efficient to figure out which targets
8969 -- this is really needed for, but in practice it is reasonable
8970 -- to do the following special check in all cases, since it means
8971 -- we get a clearer message, and also the overhead is minimal given
8972 -- that division is expensive in any case.
8974 -- In fact the check is quite easy, if the right operand is -1, then
8975 -- the mod value is always 0, and we can just ignore the left operand
8976 -- completely in this case.
8978 -- This only applies if we still have a mod operator. Skip if we
8979 -- have already rewritten this (e.g. in the case of eliminated
8980 -- overflow checks which have driven us into bignum mode).
8982 if Nkind (N) = N_Op_Mod then
8984 -- The operand type may be private (e.g. in the expansion of an
8985 -- intrinsic operation) so we must use the underlying type to get
8986 -- the bounds, and convert the literals explicitly.
8988 LLB :=
8989 Expr_Value
8990 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
8992 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
8993 and then ((not LOK) or else (Llo = LLB))
8994 then
8995 Rewrite (N,
8996 Make_If_Expression (Loc,
8997 Expressions => New_List (
8998 Make_Op_Eq (Loc,
8999 Left_Opnd => Duplicate_Subexpr (Right),
9000 Right_Opnd =>
9001 Unchecked_Convert_To (Typ,
9002 Make_Integer_Literal (Loc, -1))),
9003 Unchecked_Convert_To (Typ,
9004 Make_Integer_Literal (Loc, Uint_0)),
9005 Relocate_Node (N))));
9007 Set_Analyzed (Next (Next (First (Expressions (N)))));
9008 Analyze_And_Resolve (N, Typ);
9009 end if;
9010 end if;
9011 end if;
9012 end Expand_N_Op_Mod;
9014 --------------------------
9015 -- Expand_N_Op_Multiply --
9016 --------------------------
9018 procedure Expand_N_Op_Multiply (N : Node_Id) is
9019 Loc : constant Source_Ptr := Sloc (N);
9020 Lop : constant Node_Id := Left_Opnd (N);
9021 Rop : constant Node_Id := Right_Opnd (N);
9023 Lp2 : constant Boolean :=
9024 Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop);
9025 Rp2 : constant Boolean :=
9026 Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop);
9028 Ltyp : constant Entity_Id := Etype (Lop);
9029 Rtyp : constant Entity_Id := Etype (Rop);
9030 Typ : Entity_Id := Etype (N);
9032 begin
9033 Binary_Op_Validity_Checks (N);
9035 -- Check for MINIMIZED/ELIMINATED overflow mode
9037 if Minimized_Eliminated_Overflow_Check (N) then
9038 Apply_Arithmetic_Overflow_Check (N);
9039 return;
9040 end if;
9042 -- Special optimizations for integer types
9044 if Is_Integer_Type (Typ) then
9046 -- N * 0 = 0 for integer types
9048 if Compile_Time_Known_Value (Rop)
9049 and then Expr_Value (Rop) = Uint_0
9050 then
9051 -- Call Remove_Side_Effects to ensure that any side effects in
9052 -- the ignored left operand (in particular function calls to
9053 -- user defined functions) are properly executed.
9055 Remove_Side_Effects (Lop);
9057 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
9058 Analyze_And_Resolve (N, Typ);
9059 return;
9060 end if;
9062 -- Similar handling for 0 * N = 0
9064 if Compile_Time_Known_Value (Lop)
9065 and then Expr_Value (Lop) = Uint_0
9066 then
9067 Remove_Side_Effects (Rop);
9068 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
9069 Analyze_And_Resolve (N, Typ);
9070 return;
9071 end if;
9073 -- N * 1 = 1 * N = N for integer types
9075 -- This optimisation is not done if we are going to
9076 -- rewrite the product 1 * 2 ** N to a shift.
9078 if Compile_Time_Known_Value (Rop)
9079 and then Expr_Value (Rop) = Uint_1
9080 and then not Lp2
9081 then
9082 Rewrite (N, Lop);
9083 return;
9085 elsif Compile_Time_Known_Value (Lop)
9086 and then Expr_Value (Lop) = Uint_1
9087 and then not Rp2
9088 then
9089 Rewrite (N, Rop);
9090 return;
9091 end if;
9092 end if;
9094 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
9095 -- Is_Power_Of_2_For_Shift is set means that we know that our left
9096 -- operand is an integer, as required for this to work.
9098 if Rp2 then
9099 if Lp2 then
9101 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
9103 Rewrite (N,
9104 Make_Op_Expon (Loc,
9105 Left_Opnd => Make_Integer_Literal (Loc, 2),
9106 Right_Opnd =>
9107 Make_Op_Add (Loc,
9108 Left_Opnd => Right_Opnd (Lop),
9109 Right_Opnd => Right_Opnd (Rop))));
9110 Analyze_And_Resolve (N, Typ);
9111 return;
9113 else
9114 -- If the result is modular, perform the reduction of the result
9115 -- appropriately.
9117 if Is_Modular_Integer_Type (Typ)
9118 and then not Non_Binary_Modulus (Typ)
9119 then
9120 Rewrite (N,
9121 Make_Op_And (Loc,
9122 Left_Opnd =>
9123 Make_Op_Shift_Left (Loc,
9124 Left_Opnd => Lop,
9125 Right_Opnd =>
9126 Convert_To (Standard_Natural, Right_Opnd (Rop))),
9127 Right_Opnd =>
9128 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
9130 else
9131 Rewrite (N,
9132 Make_Op_Shift_Left (Loc,
9133 Left_Opnd => Lop,
9134 Right_Opnd =>
9135 Convert_To (Standard_Natural, Right_Opnd (Rop))));
9136 end if;
9138 Analyze_And_Resolve (N, Typ);
9139 return;
9140 end if;
9142 -- Same processing for the operands the other way round
9144 elsif Lp2 then
9145 if Is_Modular_Integer_Type (Typ)
9146 and then not Non_Binary_Modulus (Typ)
9147 then
9148 Rewrite (N,
9149 Make_Op_And (Loc,
9150 Left_Opnd =>
9151 Make_Op_Shift_Left (Loc,
9152 Left_Opnd => Rop,
9153 Right_Opnd =>
9154 Convert_To (Standard_Natural, Right_Opnd (Lop))),
9155 Right_Opnd =>
9156 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
9158 else
9159 Rewrite (N,
9160 Make_Op_Shift_Left (Loc,
9161 Left_Opnd => Rop,
9162 Right_Opnd =>
9163 Convert_To (Standard_Natural, Right_Opnd (Lop))));
9164 end if;
9166 Analyze_And_Resolve (N, Typ);
9167 return;
9168 end if;
9170 -- Do required fixup of universal fixed operation
9172 if Typ = Universal_Fixed then
9173 Fixup_Universal_Fixed_Operation (N);
9174 Typ := Etype (N);
9175 end if;
9177 -- Multiplications with fixed-point results
9179 if Is_Fixed_Point_Type (Typ) then
9181 -- No special processing if Treat_Fixed_As_Integer is set, since from
9182 -- a semantic point of view such operations are simply integer
9183 -- operations and will be treated that way.
9185 if not Treat_Fixed_As_Integer (N) then
9187 -- Case of fixed * integer => fixed
9189 if Is_Integer_Type (Rtyp) then
9190 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
9192 -- Case of integer * fixed => fixed
9194 elsif Is_Integer_Type (Ltyp) then
9195 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
9197 -- Case of fixed * fixed => fixed
9199 else
9200 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
9201 end if;
9202 end if;
9204 -- Other cases of multiplication of fixed-point operands. Again we
9205 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
9207 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
9208 and then not Treat_Fixed_As_Integer (N)
9209 then
9210 if Is_Integer_Type (Typ) then
9211 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
9212 else
9213 pragma Assert (Is_Floating_Point_Type (Typ));
9214 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
9215 end if;
9217 -- Mixed-mode operations can appear in a non-static universal context,
9218 -- in which case the integer argument must be converted explicitly.
9220 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
9221 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
9222 Analyze_And_Resolve (Rop, Universal_Real);
9224 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
9225 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
9226 Analyze_And_Resolve (Lop, Universal_Real);
9228 -- Non-fixed point cases, check software overflow checking required
9230 elsif Is_Signed_Integer_Type (Etype (N)) then
9231 Apply_Arithmetic_Overflow_Check (N);
9232 end if;
9234 -- Overflow checks for floating-point if -gnateF mode active
9236 Check_Float_Op_Overflow (N);
9238 Expand_Nonbinary_Modular_Op (N);
9239 end Expand_N_Op_Multiply;
9241 --------------------
9242 -- Expand_N_Op_Ne --
9243 --------------------
9245 procedure Expand_N_Op_Ne (N : Node_Id) is
9246 Typ : constant Entity_Id := Etype (Left_Opnd (N));
9248 begin
9249 -- Case of elementary type with standard operator
9251 if Is_Elementary_Type (Typ)
9252 and then Sloc (Entity (N)) = Standard_Location
9253 then
9254 Binary_Op_Validity_Checks (N);
9256 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
9257 -- means we no longer have a /= operation, we are all done.
9259 Expand_Compare_Minimize_Eliminate_Overflow (N);
9261 if Nkind (N) /= N_Op_Ne then
9262 return;
9263 end if;
9265 -- Boolean types (requiring handling of non-standard case)
9267 if Is_Boolean_Type (Typ) then
9268 Adjust_Condition (Left_Opnd (N));
9269 Adjust_Condition (Right_Opnd (N));
9270 Set_Etype (N, Standard_Boolean);
9271 Adjust_Result_Type (N, Typ);
9272 end if;
9274 Rewrite_Comparison (N);
9276 -- For all cases other than elementary types, we rewrite node as the
9277 -- negation of an equality operation, and reanalyze. The equality to be
9278 -- used is defined in the same scope and has the same signature. This
9279 -- signature must be set explicitly since in an instance it may not have
9280 -- the same visibility as in the generic unit. This avoids duplicating
9281 -- or factoring the complex code for record/array equality tests etc.
9283 -- This case is also used for the minimal expansion performed in
9284 -- GNATprove mode.
9286 else
9287 declare
9288 Loc : constant Source_Ptr := Sloc (N);
9289 Neg : Node_Id;
9290 Ne : constant Entity_Id := Entity (N);
9292 begin
9293 Binary_Op_Validity_Checks (N);
9295 Neg :=
9296 Make_Op_Not (Loc,
9297 Right_Opnd =>
9298 Make_Op_Eq (Loc,
9299 Left_Opnd => Left_Opnd (N),
9300 Right_Opnd => Right_Opnd (N)));
9302 -- The level of parentheses is useless in GNATprove mode, and
9303 -- bumping its level here leads to wrong columns being used in
9304 -- check messages, hence skip it in this mode.
9306 if not GNATprove_Mode then
9307 Set_Paren_Count (Right_Opnd (Neg), 1);
9308 end if;
9310 if Scope (Ne) /= Standard_Standard then
9311 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
9312 end if;
9314 -- For navigation purposes, we want to treat the inequality as an
9315 -- implicit reference to the corresponding equality. Preserve the
9316 -- Comes_From_ source flag to generate proper Xref entries.
9318 Preserve_Comes_From_Source (Neg, N);
9319 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
9320 Rewrite (N, Neg);
9321 Analyze_And_Resolve (N, Standard_Boolean);
9322 end;
9323 end if;
9325 -- No need for optimization in GNATprove mode, where we would rather see
9326 -- the original source expression.
9328 if not GNATprove_Mode then
9329 Optimize_Length_Comparison (N);
9330 end if;
9331 end Expand_N_Op_Ne;
9333 ---------------------
9334 -- Expand_N_Op_Not --
9335 ---------------------
9337 -- If the argument is other than a Boolean array type, there is no special
9338 -- expansion required, except for dealing with validity checks, and non-
9339 -- standard boolean representations.
9341 -- For the packed array case, we call the special routine in Exp_Pakd,
9342 -- except that if the component size is greater than one, we use the
9343 -- standard routine generating a gruesome loop (it is so peculiar to have
9344 -- packed arrays with non-standard Boolean representations anyway, so it
9345 -- does not matter that we do not handle this case efficiently).
9347 -- For the unpacked array case (and for the special packed case where we
9348 -- have non standard Booleans, as discussed above), we generate and insert
9349 -- into the tree the following function definition:
9351 -- function Nnnn (A : arr) is
9352 -- B : arr;
9353 -- begin
9354 -- for J in a'range loop
9355 -- B (J) := not A (J);
9356 -- end loop;
9357 -- return B;
9358 -- end Nnnn;
9360 -- Here arr is the actual subtype of the parameter (and hence always
9361 -- constrained). Then we replace the not with a call to this function.
9363 procedure Expand_N_Op_Not (N : Node_Id) is
9364 Loc : constant Source_Ptr := Sloc (N);
9365 Typ : constant Entity_Id := Etype (N);
9366 Opnd : Node_Id;
9367 Arr : Entity_Id;
9368 A : Entity_Id;
9369 B : Entity_Id;
9370 J : Entity_Id;
9371 A_J : Node_Id;
9372 B_J : Node_Id;
9374 Func_Name : Entity_Id;
9375 Loop_Statement : Node_Id;
9377 begin
9378 Unary_Op_Validity_Checks (N);
9380 -- For boolean operand, deal with non-standard booleans
9382 if Is_Boolean_Type (Typ) then
9383 Adjust_Condition (Right_Opnd (N));
9384 Set_Etype (N, Standard_Boolean);
9385 Adjust_Result_Type (N, Typ);
9386 return;
9387 end if;
9389 -- Only array types need any other processing
9391 if not Is_Array_Type (Typ) then
9392 return;
9393 end if;
9395 -- Case of array operand. If bit packed with a component size of 1,
9396 -- handle it in Exp_Pakd if the operand is known to be aligned.
9398 if Is_Bit_Packed_Array (Typ)
9399 and then Component_Size (Typ) = 1
9400 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
9401 then
9402 Expand_Packed_Not (N);
9403 return;
9404 end if;
9406 -- Case of array operand which is not bit-packed. If the context is
9407 -- a safe assignment, call in-place operation, If context is a larger
9408 -- boolean expression in the context of a safe assignment, expansion is
9409 -- done by enclosing operation.
9411 Opnd := Relocate_Node (Right_Opnd (N));
9412 Convert_To_Actual_Subtype (Opnd);
9413 Arr := Etype (Opnd);
9414 Ensure_Defined (Arr, N);
9415 Silly_Boolean_Array_Not_Test (N, Arr);
9417 if Nkind (Parent (N)) = N_Assignment_Statement then
9418 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
9419 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
9420 return;
9422 -- Special case the negation of a binary operation
9424 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
9425 and then Safe_In_Place_Array_Op
9426 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
9427 then
9428 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
9429 return;
9430 end if;
9432 elsif Nkind (Parent (N)) in N_Binary_Op
9433 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
9434 then
9435 declare
9436 Op1 : constant Node_Id := Left_Opnd (Parent (N));
9437 Op2 : constant Node_Id := Right_Opnd (Parent (N));
9438 Lhs : constant Node_Id := Name (Parent (Parent (N)));
9440 begin
9441 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
9443 -- (not A) op (not B) can be reduced to a single call
9445 if N = Op1 and then Nkind (Op2) = N_Op_Not then
9446 return;
9448 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
9449 return;
9451 -- A xor (not B) can also be special-cased
9453 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
9454 return;
9455 end if;
9456 end if;
9457 end;
9458 end if;
9460 A := Make_Defining_Identifier (Loc, Name_uA);
9461 B := Make_Defining_Identifier (Loc, Name_uB);
9462 J := Make_Defining_Identifier (Loc, Name_uJ);
9464 A_J :=
9465 Make_Indexed_Component (Loc,
9466 Prefix => New_Occurrence_Of (A, Loc),
9467 Expressions => New_List (New_Occurrence_Of (J, Loc)));
9469 B_J :=
9470 Make_Indexed_Component (Loc,
9471 Prefix => New_Occurrence_Of (B, Loc),
9472 Expressions => New_List (New_Occurrence_Of (J, Loc)));
9474 Loop_Statement :=
9475 Make_Implicit_Loop_Statement (N,
9476 Identifier => Empty,
9478 Iteration_Scheme =>
9479 Make_Iteration_Scheme (Loc,
9480 Loop_Parameter_Specification =>
9481 Make_Loop_Parameter_Specification (Loc,
9482 Defining_Identifier => J,
9483 Discrete_Subtype_Definition =>
9484 Make_Attribute_Reference (Loc,
9485 Prefix => Make_Identifier (Loc, Chars (A)),
9486 Attribute_Name => Name_Range))),
9488 Statements => New_List (
9489 Make_Assignment_Statement (Loc,
9490 Name => B_J,
9491 Expression => Make_Op_Not (Loc, A_J))));
9493 Func_Name := Make_Temporary (Loc, 'N');
9494 Set_Is_Inlined (Func_Name);
9496 Insert_Action (N,
9497 Make_Subprogram_Body (Loc,
9498 Specification =>
9499 Make_Function_Specification (Loc,
9500 Defining_Unit_Name => Func_Name,
9501 Parameter_Specifications => New_List (
9502 Make_Parameter_Specification (Loc,
9503 Defining_Identifier => A,
9504 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9505 Result_Definition => New_Occurrence_Of (Typ, Loc)),
9507 Declarations => New_List (
9508 Make_Object_Declaration (Loc,
9509 Defining_Identifier => B,
9510 Object_Definition => New_Occurrence_Of (Arr, Loc))),
9512 Handled_Statement_Sequence =>
9513 Make_Handled_Sequence_Of_Statements (Loc,
9514 Statements => New_List (
9515 Loop_Statement,
9516 Make_Simple_Return_Statement (Loc,
9517 Expression => Make_Identifier (Loc, Chars (B)))))));
9519 Rewrite (N,
9520 Make_Function_Call (Loc,
9521 Name => New_Occurrence_Of (Func_Name, Loc),
9522 Parameter_Associations => New_List (Opnd)));
9524 Analyze_And_Resolve (N, Typ);
9525 end Expand_N_Op_Not;
9527 --------------------
9528 -- Expand_N_Op_Or --
9529 --------------------
9531 procedure Expand_N_Op_Or (N : Node_Id) is
9532 Typ : constant Entity_Id := Etype (N);
9534 begin
9535 Binary_Op_Validity_Checks (N);
9537 if Is_Array_Type (Etype (N)) then
9538 Expand_Boolean_Operator (N);
9540 elsif Is_Boolean_Type (Etype (N)) then
9541 Adjust_Condition (Left_Opnd (N));
9542 Adjust_Condition (Right_Opnd (N));
9543 Set_Etype (N, Standard_Boolean);
9544 Adjust_Result_Type (N, Typ);
9546 elsif Is_Intrinsic_Subprogram (Entity (N)) then
9547 Expand_Intrinsic_Call (N, Entity (N));
9548 end if;
9550 Expand_Nonbinary_Modular_Op (N);
9551 end Expand_N_Op_Or;
9553 ----------------------
9554 -- Expand_N_Op_Plus --
9555 ----------------------
9557 procedure Expand_N_Op_Plus (N : Node_Id) is
9558 begin
9559 Unary_Op_Validity_Checks (N);
9561 -- Check for MINIMIZED/ELIMINATED overflow mode
9563 if Minimized_Eliminated_Overflow_Check (N) then
9564 Apply_Arithmetic_Overflow_Check (N);
9565 return;
9566 end if;
9567 end Expand_N_Op_Plus;
9569 ---------------------
9570 -- Expand_N_Op_Rem --
9571 ---------------------
9573 procedure Expand_N_Op_Rem (N : Node_Id) is
9574 Loc : constant Source_Ptr := Sloc (N);
9575 Typ : constant Entity_Id := Etype (N);
9577 Left : Node_Id;
9578 Right : Node_Id;
9580 Lo : Uint;
9581 Hi : Uint;
9582 OK : Boolean;
9584 Lneg : Boolean;
9585 Rneg : Boolean;
9586 -- Set if corresponding operand can be negative
9588 pragma Unreferenced (Hi);
9590 begin
9591 Binary_Op_Validity_Checks (N);
9593 -- Check for MINIMIZED/ELIMINATED overflow mode
9595 if Minimized_Eliminated_Overflow_Check (N) then
9596 Apply_Arithmetic_Overflow_Check (N);
9597 return;
9598 end if;
9600 if Is_Integer_Type (Etype (N)) then
9601 Apply_Divide_Checks (N);
9603 -- All done if we don't have a REM any more, which can happen as a
9604 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
9606 if Nkind (N) /= N_Op_Rem then
9607 return;
9608 end if;
9609 end if;
9611 -- Proceed with expansion of REM
9613 Left := Left_Opnd (N);
9614 Right := Right_Opnd (N);
9616 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
9617 -- but it is useful with other back ends, and is certainly harmless.
9619 if Is_Integer_Type (Etype (N))
9620 and then Compile_Time_Known_Value (Right)
9621 and then Expr_Value (Right) = Uint_1
9622 then
9623 -- Call Remove_Side_Effects to ensure that any side effects in the
9624 -- ignored left operand (in particular function calls to user defined
9625 -- functions) are properly executed.
9627 Remove_Side_Effects (Left);
9629 Rewrite (N, Make_Integer_Literal (Loc, 0));
9630 Analyze_And_Resolve (N, Typ);
9631 return;
9632 end if;
9634 -- Deal with annoying case of largest negative number remainder minus
9635 -- one. Gigi may not handle this case correctly, because on some
9636 -- targets, the mod value is computed using a divide instruction
9637 -- which gives an overflow trap for this case.
9639 -- It would be a bit more efficient to figure out which targets this
9640 -- is really needed for, but in practice it is reasonable to do the
9641 -- following special check in all cases, since it means we get a clearer
9642 -- message, and also the overhead is minimal given that division is
9643 -- expensive in any case.
9645 -- In fact the check is quite easy, if the right operand is -1, then
9646 -- the remainder is always 0, and we can just ignore the left operand
9647 -- completely in this case.
9649 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9650 Lneg := (not OK) or else Lo < 0;
9652 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
9653 Rneg := (not OK) or else Lo < 0;
9655 -- We won't mess with trying to find out if the left operand can really
9656 -- be the largest negative number (that's a pain in the case of private
9657 -- types and this is really marginal). We will just assume that we need
9658 -- the test if the left operand can be negative at all.
9660 if Lneg and Rneg then
9661 Rewrite (N,
9662 Make_If_Expression (Loc,
9663 Expressions => New_List (
9664 Make_Op_Eq (Loc,
9665 Left_Opnd => Duplicate_Subexpr (Right),
9666 Right_Opnd =>
9667 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
9669 Unchecked_Convert_To (Typ,
9670 Make_Integer_Literal (Loc, Uint_0)),
9672 Relocate_Node (N))));
9674 Set_Analyzed (Next (Next (First (Expressions (N)))));
9675 Analyze_And_Resolve (N, Typ);
9676 end if;
9677 end Expand_N_Op_Rem;
9679 -----------------------------
9680 -- Expand_N_Op_Rotate_Left --
9681 -----------------------------
9683 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
9684 begin
9685 Binary_Op_Validity_Checks (N);
9687 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
9688 -- so we rewrite in terms of logical shifts
9690 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
9692 -- where Bits is the shift count mod Esize (the mod operation here
9693 -- deals with ludicrous large shift counts, which are apparently OK).
9695 -- What about nonbinary modulus ???
9697 declare
9698 Loc : constant Source_Ptr := Sloc (N);
9699 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
9700 Typ : constant Entity_Id := Etype (N);
9702 begin
9703 if Modify_Tree_For_C then
9704 Rewrite (Right_Opnd (N),
9705 Make_Op_Rem (Loc,
9706 Left_Opnd => Relocate_Node (Right_Opnd (N)),
9707 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
9709 Analyze_And_Resolve (Right_Opnd (N), Rtp);
9711 Rewrite (N,
9712 Make_Op_Or (Loc,
9713 Left_Opnd =>
9714 Make_Op_Shift_Left (Loc,
9715 Left_Opnd => Left_Opnd (N),
9716 Right_Opnd => Right_Opnd (N)),
9718 Right_Opnd =>
9719 Make_Op_Shift_Right (Loc,
9720 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
9721 Right_Opnd =>
9722 Make_Op_Subtract (Loc,
9723 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9724 Right_Opnd =>
9725 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9727 Analyze_And_Resolve (N, Typ);
9728 end if;
9729 end;
9730 end Expand_N_Op_Rotate_Left;
9732 ------------------------------
9733 -- Expand_N_Op_Rotate_Right --
9734 ------------------------------
9736 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
9737 begin
9738 Binary_Op_Validity_Checks (N);
9740 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
9741 -- so we rewrite in terms of logical shifts
9743 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
9745 -- where Bits is the shift count mod Esize (the mod operation here
9746 -- deals with ludicrous large shift counts, which are apparently OK).
9748 -- What about nonbinary modulus ???
9750 declare
9751 Loc : constant Source_Ptr := Sloc (N);
9752 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
9753 Typ : constant Entity_Id := Etype (N);
9755 begin
9756 Rewrite (Right_Opnd (N),
9757 Make_Op_Rem (Loc,
9758 Left_Opnd => Relocate_Node (Right_Opnd (N)),
9759 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
9761 Analyze_And_Resolve (Right_Opnd (N), Rtp);
9763 if Modify_Tree_For_C then
9764 Rewrite (N,
9765 Make_Op_Or (Loc,
9766 Left_Opnd =>
9767 Make_Op_Shift_Right (Loc,
9768 Left_Opnd => Left_Opnd (N),
9769 Right_Opnd => Right_Opnd (N)),
9771 Right_Opnd =>
9772 Make_Op_Shift_Left (Loc,
9773 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
9774 Right_Opnd =>
9775 Make_Op_Subtract (Loc,
9776 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9777 Right_Opnd =>
9778 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9780 Analyze_And_Resolve (N, Typ);
9781 end if;
9782 end;
9783 end Expand_N_Op_Rotate_Right;
9785 ----------------------------
9786 -- Expand_N_Op_Shift_Left --
9787 ----------------------------
9789 -- Note: nothing in this routine depends on left as opposed to right shifts
9790 -- so we share the routine for expanding shift right operations.
9792 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
9793 begin
9794 Binary_Op_Validity_Checks (N);
9796 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9797 -- operand is not greater than the word size (since that would not
9798 -- be defined properly by the corresponding C shift operator).
9800 if Modify_Tree_For_C then
9801 declare
9802 Right : constant Node_Id := Right_Opnd (N);
9803 Loc : constant Source_Ptr := Sloc (Right);
9804 Typ : constant Entity_Id := Etype (N);
9805 Siz : constant Uint := Esize (Typ);
9806 Orig : Node_Id;
9807 OK : Boolean;
9808 Lo : Uint;
9809 Hi : Uint;
9811 begin
9812 if Compile_Time_Known_Value (Right) then
9813 if Expr_Value (Right) >= Siz then
9814 Rewrite (N, Make_Integer_Literal (Loc, 0));
9815 Analyze_And_Resolve (N, Typ);
9816 end if;
9818 -- Not compile time known, find range
9820 else
9821 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9823 -- Nothing to do if known to be OK range, otherwise expand
9825 if not OK or else Hi >= Siz then
9827 -- Prevent recursion on copy of shift node
9829 Orig := Relocate_Node (N);
9830 Set_Analyzed (Orig);
9832 -- Now do the rewrite
9834 Rewrite (N,
9835 Make_If_Expression (Loc,
9836 Expressions => New_List (
9837 Make_Op_Ge (Loc,
9838 Left_Opnd => Duplicate_Subexpr_Move_Checks (Right),
9839 Right_Opnd => Make_Integer_Literal (Loc, Siz)),
9840 Make_Integer_Literal (Loc, 0),
9841 Orig)));
9842 Analyze_And_Resolve (N, Typ);
9843 end if;
9844 end if;
9845 end;
9846 end if;
9847 end Expand_N_Op_Shift_Left;
9849 -----------------------------
9850 -- Expand_N_Op_Shift_Right --
9851 -----------------------------
9853 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
9854 begin
9855 -- Share shift left circuit
9857 Expand_N_Op_Shift_Left (N);
9858 end Expand_N_Op_Shift_Right;
9860 ----------------------------------------
9861 -- Expand_N_Op_Shift_Right_Arithmetic --
9862 ----------------------------------------
9864 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
9865 begin
9866 Binary_Op_Validity_Checks (N);
9868 -- If we are in Modify_Tree_For_C mode, there is no shift right
9869 -- arithmetic in C, so we rewrite in terms of logical shifts.
9871 -- Shift_Right (Num, Bits) or
9872 -- (if Num >= Sign
9873 -- then not (Shift_Right (Mask, bits))
9874 -- else 0)
9876 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9878 -- Note: in almost all C compilers it would work to just shift a
9879 -- signed integer right, but it's undefined and we cannot rely on it.
9881 -- Note: the above works fine for shift counts greater than or equal
9882 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9883 -- generates all 1'bits.
9885 -- What about nonbinary modulus ???
9887 declare
9888 Loc : constant Source_Ptr := Sloc (N);
9889 Typ : constant Entity_Id := Etype (N);
9890 Sign : constant Uint := 2 ** (Esize (Typ) - 1);
9891 Mask : constant Uint := (2 ** Esize (Typ)) - 1;
9892 Left : constant Node_Id := Left_Opnd (N);
9893 Right : constant Node_Id := Right_Opnd (N);
9894 Maskx : Node_Id;
9896 begin
9897 if Modify_Tree_For_C then
9899 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9900 -- compile time as a single constant.
9902 if Compile_Time_Known_Value (Right) then
9903 declare
9904 Val : constant Uint := Expr_Value (Right);
9906 begin
9907 if Val >= Esize (Typ) then
9908 Maskx := Make_Integer_Literal (Loc, Mask);
9910 else
9911 Maskx :=
9912 Make_Integer_Literal (Loc,
9913 Intval => Mask - (Mask / (2 ** Expr_Value (Right))));
9914 end if;
9915 end;
9917 else
9918 Maskx :=
9919 Make_Op_Not (Loc,
9920 Right_Opnd =>
9921 Make_Op_Shift_Right (Loc,
9922 Left_Opnd => Make_Integer_Literal (Loc, Mask),
9923 Right_Opnd => Duplicate_Subexpr_No_Checks (Right)));
9924 end if;
9926 -- Now do the rewrite
9928 Rewrite (N,
9929 Make_Op_Or (Loc,
9930 Left_Opnd =>
9931 Make_Op_Shift_Right (Loc,
9932 Left_Opnd => Left,
9933 Right_Opnd => Right),
9934 Right_Opnd =>
9935 Make_If_Expression (Loc,
9936 Expressions => New_List (
9937 Make_Op_Ge (Loc,
9938 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
9939 Right_Opnd => Make_Integer_Literal (Loc, Sign)),
9940 Maskx,
9941 Make_Integer_Literal (Loc, 0)))));
9942 Analyze_And_Resolve (N, Typ);
9943 end if;
9944 end;
9945 end Expand_N_Op_Shift_Right_Arithmetic;
9947 --------------------------
9948 -- Expand_N_Op_Subtract --
9949 --------------------------
9951 procedure Expand_N_Op_Subtract (N : Node_Id) is
9952 Typ : constant Entity_Id := Etype (N);
9954 begin
9955 Binary_Op_Validity_Checks (N);
9957 -- Check for MINIMIZED/ELIMINATED overflow mode
9959 if Minimized_Eliminated_Overflow_Check (N) then
9960 Apply_Arithmetic_Overflow_Check (N);
9961 return;
9962 end if;
9964 -- N - 0 = N for integer types
9966 if Is_Integer_Type (Typ)
9967 and then Compile_Time_Known_Value (Right_Opnd (N))
9968 and then Expr_Value (Right_Opnd (N)) = 0
9969 then
9970 Rewrite (N, Left_Opnd (N));
9971 return;
9972 end if;
9974 -- Arithmetic overflow checks for signed integer/fixed point types
9976 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
9977 Apply_Arithmetic_Overflow_Check (N);
9978 end if;
9980 -- Overflow checks for floating-point if -gnateF mode active
9982 Check_Float_Op_Overflow (N);
9984 Expand_Nonbinary_Modular_Op (N);
9985 end Expand_N_Op_Subtract;
9987 ---------------------
9988 -- Expand_N_Op_Xor --
9989 ---------------------
9991 procedure Expand_N_Op_Xor (N : Node_Id) is
9992 Typ : constant Entity_Id := Etype (N);
9994 begin
9995 Binary_Op_Validity_Checks (N);
9997 if Is_Array_Type (Etype (N)) then
9998 Expand_Boolean_Operator (N);
10000 elsif Is_Boolean_Type (Etype (N)) then
10001 Adjust_Condition (Left_Opnd (N));
10002 Adjust_Condition (Right_Opnd (N));
10003 Set_Etype (N, Standard_Boolean);
10004 Adjust_Result_Type (N, Typ);
10006 elsif Is_Intrinsic_Subprogram (Entity (N)) then
10007 Expand_Intrinsic_Call (N, Entity (N));
10008 end if;
10009 end Expand_N_Op_Xor;
10011 ----------------------
10012 -- Expand_N_Or_Else --
10013 ----------------------
10015 procedure Expand_N_Or_Else (N : Node_Id)
10016 renames Expand_Short_Circuit_Operator;
10018 -----------------------------------
10019 -- Expand_N_Qualified_Expression --
10020 -----------------------------------
10022 procedure Expand_N_Qualified_Expression (N : Node_Id) is
10023 Operand : constant Node_Id := Expression (N);
10024 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
10026 begin
10027 -- Do validity check if validity checking operands
10029 if Validity_Checks_On and Validity_Check_Operands then
10030 Ensure_Valid (Operand);
10031 end if;
10033 -- Apply possible constraint check
10035 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
10037 if Do_Range_Check (Operand) then
10038 Set_Do_Range_Check (Operand, False);
10039 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
10040 end if;
10041 end Expand_N_Qualified_Expression;
10043 ------------------------------------
10044 -- Expand_N_Quantified_Expression --
10045 ------------------------------------
10047 -- We expand:
10049 -- for all X in range => Cond
10051 -- into:
10053 -- T := True;
10054 -- for X in range loop
10055 -- if not Cond then
10056 -- T := False;
10057 -- exit;
10058 -- end if;
10059 -- end loop;
10061 -- Similarly, an existentially quantified expression:
10063 -- for some X in range => Cond
10065 -- becomes:
10067 -- T := False;
10068 -- for X in range loop
10069 -- if Cond then
10070 -- T := True;
10071 -- exit;
10072 -- end if;
10073 -- end loop;
10075 -- In both cases, the iteration may be over a container in which case it is
10076 -- given by an iterator specification, not a loop parameter specification.
10078 procedure Expand_N_Quantified_Expression (N : Node_Id) is
10079 Actions : constant List_Id := New_List;
10080 For_All : constant Boolean := All_Present (N);
10081 Iter_Spec : constant Node_Id := Iterator_Specification (N);
10082 Loc : constant Source_Ptr := Sloc (N);
10083 Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N);
10084 Cond : Node_Id;
10085 Flag : Entity_Id;
10086 Scheme : Node_Id;
10087 Stmts : List_Id;
10089 begin
10090 -- Create the declaration of the flag which tracks the status of the
10091 -- quantified expression. Generate:
10093 -- Flag : Boolean := (True | False);
10095 Flag := Make_Temporary (Loc, 'T', N);
10097 Append_To (Actions,
10098 Make_Object_Declaration (Loc,
10099 Defining_Identifier => Flag,
10100 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
10101 Expression =>
10102 New_Occurrence_Of (Boolean_Literals (For_All), Loc)));
10104 -- Construct the circuitry which tracks the status of the quantified
10105 -- expression. Generate:
10107 -- if [not] Cond then
10108 -- Flag := (False | True);
10109 -- exit;
10110 -- end if;
10112 Cond := Relocate_Node (Condition (N));
10114 if For_All then
10115 Cond := Make_Op_Not (Loc, Cond);
10116 end if;
10118 Stmts := New_List (
10119 Make_Implicit_If_Statement (N,
10120 Condition => Cond,
10121 Then_Statements => New_List (
10122 Make_Assignment_Statement (Loc,
10123 Name => New_Occurrence_Of (Flag, Loc),
10124 Expression =>
10125 New_Occurrence_Of (Boolean_Literals (not For_All), Loc)),
10126 Make_Exit_Statement (Loc))));
10128 -- Build the loop equivalent of the quantified expression
10130 if Present (Iter_Spec) then
10131 Scheme :=
10132 Make_Iteration_Scheme (Loc,
10133 Iterator_Specification => Iter_Spec);
10134 else
10135 Scheme :=
10136 Make_Iteration_Scheme (Loc,
10137 Loop_Parameter_Specification => Loop_Spec);
10138 end if;
10140 Append_To (Actions,
10141 Make_Loop_Statement (Loc,
10142 Iteration_Scheme => Scheme,
10143 Statements => Stmts,
10144 End_Label => Empty));
10146 -- Transform the quantified expression
10148 Rewrite (N,
10149 Make_Expression_With_Actions (Loc,
10150 Expression => New_Occurrence_Of (Flag, Loc),
10151 Actions => Actions));
10152 Analyze_And_Resolve (N, Standard_Boolean);
10153 end Expand_N_Quantified_Expression;
10155 ---------------------------------
10156 -- Expand_N_Selected_Component --
10157 ---------------------------------
10159 procedure Expand_N_Selected_Component (N : Node_Id) is
10160 Loc : constant Source_Ptr := Sloc (N);
10161 Par : constant Node_Id := Parent (N);
10162 P : constant Node_Id := Prefix (N);
10163 S : constant Node_Id := Selector_Name (N);
10164 Ptyp : Entity_Id := Underlying_Type (Etype (P));
10165 Disc : Entity_Id;
10166 New_N : Node_Id;
10167 Dcon : Elmt_Id;
10168 Dval : Node_Id;
10170 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
10171 -- Gigi needs a temporary for prefixes that depend on a discriminant,
10172 -- unless the context of an assignment can provide size information.
10173 -- Don't we have a general routine that does this???
10175 function Is_Subtype_Declaration return Boolean;
10176 -- The replacement of a discriminant reference by its value is required
10177 -- if this is part of the initialization of an temporary generated by a
10178 -- change of representation. This shows up as the construction of a
10179 -- discriminant constraint for a subtype declared at the same point as
10180 -- the entity in the prefix of the selected component. We recognize this
10181 -- case when the context of the reference is:
10182 -- subtype ST is T(Obj.D);
10183 -- where the entity for Obj comes from source, and ST has the same sloc.
10185 -----------------------
10186 -- In_Left_Hand_Side --
10187 -----------------------
10189 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
10190 begin
10191 return (Nkind (Parent (Comp)) = N_Assignment_Statement
10192 and then Comp = Name (Parent (Comp)))
10193 or else (Present (Parent (Comp))
10194 and then Nkind (Parent (Comp)) in N_Subexpr
10195 and then In_Left_Hand_Side (Parent (Comp)));
10196 end In_Left_Hand_Side;
10198 -----------------------------
10199 -- Is_Subtype_Declaration --
10200 -----------------------------
10202 function Is_Subtype_Declaration return Boolean is
10203 Par : constant Node_Id := Parent (N);
10204 begin
10205 return
10206 Nkind (Par) = N_Index_Or_Discriminant_Constraint
10207 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
10208 and then Comes_From_Source (Entity (Prefix (N)))
10209 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
10210 end Is_Subtype_Declaration;
10212 -- Start of processing for Expand_N_Selected_Component
10214 begin
10215 -- Insert explicit dereference if required
10217 if Is_Access_Type (Ptyp) then
10219 -- First set prefix type to proper access type, in case it currently
10220 -- has a private (non-access) view of this type.
10222 Set_Etype (P, Ptyp);
10224 Insert_Explicit_Dereference (P);
10225 Analyze_And_Resolve (P, Designated_Type (Ptyp));
10227 if Ekind (Etype (P)) = E_Private_Subtype
10228 and then Is_For_Access_Subtype (Etype (P))
10229 then
10230 Set_Etype (P, Base_Type (Etype (P)));
10231 end if;
10233 Ptyp := Etype (P);
10234 end if;
10236 -- Deal with discriminant check required
10238 if Do_Discriminant_Check (N) then
10239 if Present (Discriminant_Checking_Func
10240 (Original_Record_Component (Entity (S))))
10241 then
10242 -- Present the discriminant checking function to the backend, so
10243 -- that it can inline the call to the function.
10245 Add_Inlined_Body
10246 (Discriminant_Checking_Func
10247 (Original_Record_Component (Entity (S))),
10250 -- Now reset the flag and generate the call
10252 Set_Do_Discriminant_Check (N, False);
10253 Generate_Discriminant_Check (N);
10255 -- In the case of Unchecked_Union, no discriminant checking is
10256 -- actually performed.
10258 else
10259 Set_Do_Discriminant_Check (N, False);
10260 end if;
10261 end if;
10263 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10264 -- function, then additional actuals must be passed.
10266 if Is_Build_In_Place_Function_Call (P) then
10267 Make_Build_In_Place_Call_In_Anonymous_Context (P);
10269 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
10270 -- containing build-in-place function calls whose returned object covers
10271 -- interface types.
10273 elsif Present (Unqual_BIP_Iface_Function_Call (P)) then
10274 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (P);
10275 end if;
10277 -- Gigi cannot handle unchecked conversions that are the prefix of a
10278 -- selected component with discriminants. This must be checked during
10279 -- expansion, because during analysis the type of the selector is not
10280 -- known at the point the prefix is analyzed. If the conversion is the
10281 -- target of an assignment, then we cannot force the evaluation.
10283 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
10284 and then Has_Discriminants (Etype (N))
10285 and then not In_Left_Hand_Side (N)
10286 then
10287 Force_Evaluation (Prefix (N));
10288 end if;
10290 -- Remaining processing applies only if selector is a discriminant
10292 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
10294 -- If the selector is a discriminant of a constrained record type,
10295 -- we may be able to rewrite the expression with the actual value
10296 -- of the discriminant, a useful optimization in some cases.
10298 if Is_Record_Type (Ptyp)
10299 and then Has_Discriminants (Ptyp)
10300 and then Is_Constrained (Ptyp)
10301 then
10302 -- Do this optimization for discrete types only, and not for
10303 -- access types (access discriminants get us into trouble).
10305 if not Is_Discrete_Type (Etype (N)) then
10306 null;
10308 -- Don't do this on the left-hand side of an assignment statement.
10309 -- Normally one would think that references like this would not
10310 -- occur, but they do in generated code, and mean that we really
10311 -- do want to assign the discriminant.
10313 elsif Nkind (Par) = N_Assignment_Statement
10314 and then Name (Par) = N
10315 then
10316 null;
10318 -- Don't do this optimization for the prefix of an attribute or
10319 -- the name of an object renaming declaration since these are
10320 -- contexts where we do not want the value anyway.
10322 elsif (Nkind (Par) = N_Attribute_Reference
10323 and then Prefix (Par) = N)
10324 or else Is_Renamed_Object (N)
10325 then
10326 null;
10328 -- Don't do this optimization if we are within the code for a
10329 -- discriminant check, since the whole point of such a check may
10330 -- be to verify the condition on which the code below depends.
10332 elsif Is_In_Discriminant_Check (N) then
10333 null;
10335 -- Green light to see if we can do the optimization. There is
10336 -- still one condition that inhibits the optimization below but
10337 -- now is the time to check the particular discriminant.
10339 else
10340 -- Loop through discriminants to find the matching discriminant
10341 -- constraint to see if we can copy it.
10343 Disc := First_Discriminant (Ptyp);
10344 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
10345 Discr_Loop : while Present (Dcon) loop
10346 Dval := Node (Dcon);
10348 -- Check if this is the matching discriminant and if the
10349 -- discriminant value is simple enough to make sense to
10350 -- copy. We don't want to copy complex expressions, and
10351 -- indeed to do so can cause trouble (before we put in
10352 -- this guard, a discriminant expression containing an
10353 -- AND THEN was copied, causing problems for coverage
10354 -- analysis tools).
10356 -- However, if the reference is part of the initialization
10357 -- code generated for an object declaration, we must use
10358 -- the discriminant value from the subtype constraint,
10359 -- because the selected component may be a reference to the
10360 -- object being initialized, whose discriminant is not yet
10361 -- set. This only happens in complex cases involving changes
10362 -- or representation.
10364 if Disc = Entity (Selector_Name (N))
10365 and then (Is_Entity_Name (Dval)
10366 or else Compile_Time_Known_Value (Dval)
10367 or else Is_Subtype_Declaration)
10368 then
10369 -- Here we have the matching discriminant. Check for
10370 -- the case of a discriminant of a component that is
10371 -- constrained by an outer discriminant, which cannot
10372 -- be optimized away.
10374 if Denotes_Discriminant
10375 (Dval, Check_Concurrent => True)
10376 then
10377 exit Discr_Loop;
10379 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
10380 and then
10381 Denotes_Discriminant
10382 (Selector_Name (Original_Node (Dval)), True)
10383 then
10384 exit Discr_Loop;
10386 -- Do not retrieve value if constraint is not static. It
10387 -- is generally not useful, and the constraint may be a
10388 -- rewritten outer discriminant in which case it is in
10389 -- fact incorrect.
10391 elsif Is_Entity_Name (Dval)
10392 and then
10393 Nkind (Parent (Entity (Dval))) = N_Object_Declaration
10394 and then Present (Expression (Parent (Entity (Dval))))
10395 and then not
10396 Is_OK_Static_Expression
10397 (Expression (Parent (Entity (Dval))))
10398 then
10399 exit Discr_Loop;
10401 -- In the context of a case statement, the expression may
10402 -- have the base type of the discriminant, and we need to
10403 -- preserve the constraint to avoid spurious errors on
10404 -- missing cases.
10406 elsif Nkind (Parent (N)) = N_Case_Statement
10407 and then Etype (Dval) /= Etype (Disc)
10408 then
10409 Rewrite (N,
10410 Make_Qualified_Expression (Loc,
10411 Subtype_Mark =>
10412 New_Occurrence_Of (Etype (Disc), Loc),
10413 Expression =>
10414 New_Copy_Tree (Dval)));
10415 Analyze_And_Resolve (N, Etype (Disc));
10417 -- In case that comes out as a static expression,
10418 -- reset it (a selected component is never static).
10420 Set_Is_Static_Expression (N, False);
10421 return;
10423 -- Otherwise we can just copy the constraint, but the
10424 -- result is certainly not static. In some cases the
10425 -- discriminant constraint has been analyzed in the
10426 -- context of the original subtype indication, but for
10427 -- itypes the constraint might not have been analyzed
10428 -- yet, and this must be done now.
10430 else
10431 Rewrite (N, New_Copy_Tree (Dval));
10432 Analyze_And_Resolve (N);
10433 Set_Is_Static_Expression (N, False);
10434 return;
10435 end if;
10436 end if;
10438 Next_Elmt (Dcon);
10439 Next_Discriminant (Disc);
10440 end loop Discr_Loop;
10442 -- Note: the above loop should always find a matching
10443 -- discriminant, but if it does not, we just missed an
10444 -- optimization due to some glitch (perhaps a previous
10445 -- error), so ignore.
10447 end if;
10448 end if;
10450 -- The only remaining processing is in the case of a discriminant of
10451 -- a concurrent object, where we rewrite the prefix to denote the
10452 -- corresponding record type. If the type is derived and has renamed
10453 -- discriminants, use corresponding discriminant, which is the one
10454 -- that appears in the corresponding record.
10456 if not Is_Concurrent_Type (Ptyp) then
10457 return;
10458 end if;
10460 Disc := Entity (Selector_Name (N));
10462 if Is_Derived_Type (Ptyp)
10463 and then Present (Corresponding_Discriminant (Disc))
10464 then
10465 Disc := Corresponding_Discriminant (Disc);
10466 end if;
10468 New_N :=
10469 Make_Selected_Component (Loc,
10470 Prefix =>
10471 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
10472 New_Copy_Tree (P)),
10473 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
10475 Rewrite (N, New_N);
10476 Analyze (N);
10477 end if;
10479 -- Set Atomic_Sync_Required if necessary for atomic component
10481 if Nkind (N) = N_Selected_Component then
10482 declare
10483 E : constant Entity_Id := Entity (Selector_Name (N));
10484 Set : Boolean;
10486 begin
10487 -- If component is atomic, but type is not, setting depends on
10488 -- disable/enable state for the component.
10490 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
10491 Set := not Atomic_Synchronization_Disabled (E);
10493 -- If component is not atomic, but its type is atomic, setting
10494 -- depends on disable/enable state for the type.
10496 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
10497 Set := not Atomic_Synchronization_Disabled (Etype (E));
10499 -- If both component and type are atomic, we disable if either
10500 -- component or its type have sync disabled.
10502 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
10503 Set := (not Atomic_Synchronization_Disabled (E))
10504 and then
10505 (not Atomic_Synchronization_Disabled (Etype (E)));
10507 else
10508 Set := False;
10509 end if;
10511 -- Set flag if required
10513 if Set then
10514 Activate_Atomic_Synchronization (N);
10515 end if;
10516 end;
10517 end if;
10518 end Expand_N_Selected_Component;
10520 --------------------
10521 -- Expand_N_Slice --
10522 --------------------
10524 procedure Expand_N_Slice (N : Node_Id) is
10525 Loc : constant Source_Ptr := Sloc (N);
10526 Typ : constant Entity_Id := Etype (N);
10528 function Is_Procedure_Actual (N : Node_Id) return Boolean;
10529 -- Check whether the argument is an actual for a procedure call, in
10530 -- which case the expansion of a bit-packed slice is deferred until the
10531 -- call itself is expanded. The reason this is required is that we might
10532 -- have an IN OUT or OUT parameter, and the copy out is essential, and
10533 -- that copy out would be missed if we created a temporary here in
10534 -- Expand_N_Slice. Note that we don't bother to test specifically for an
10535 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
10536 -- is harmless to defer expansion in the IN case, since the call
10537 -- processing will still generate the appropriate copy in operation,
10538 -- which will take care of the slice.
10540 procedure Make_Temporary_For_Slice;
10541 -- Create a named variable for the value of the slice, in cases where
10542 -- the back end cannot handle it properly, e.g. when packed types or
10543 -- unaligned slices are involved.
10545 -------------------------
10546 -- Is_Procedure_Actual --
10547 -------------------------
10549 function Is_Procedure_Actual (N : Node_Id) return Boolean is
10550 Par : Node_Id := Parent (N);
10552 begin
10553 loop
10554 -- If our parent is a procedure call we can return
10556 if Nkind (Par) = N_Procedure_Call_Statement then
10557 return True;
10559 -- If our parent is a type conversion, keep climbing the tree,
10560 -- since a type conversion can be a procedure actual. Also keep
10561 -- climbing if parameter association or a qualified expression,
10562 -- since these are additional cases that do can appear on
10563 -- procedure actuals.
10565 elsif Nkind_In (Par, N_Type_Conversion,
10566 N_Parameter_Association,
10567 N_Qualified_Expression)
10568 then
10569 Par := Parent (Par);
10571 -- Any other case is not what we are looking for
10573 else
10574 return False;
10575 end if;
10576 end loop;
10577 end Is_Procedure_Actual;
10579 ------------------------------
10580 -- Make_Temporary_For_Slice --
10581 ------------------------------
10583 procedure Make_Temporary_For_Slice is
10584 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
10585 Decl : Node_Id;
10587 begin
10588 Decl :=
10589 Make_Object_Declaration (Loc,
10590 Defining_Identifier => Ent,
10591 Object_Definition => New_Occurrence_Of (Typ, Loc));
10593 Set_No_Initialization (Decl);
10595 Insert_Actions (N, New_List (
10596 Decl,
10597 Make_Assignment_Statement (Loc,
10598 Name => New_Occurrence_Of (Ent, Loc),
10599 Expression => Relocate_Node (N))));
10601 Rewrite (N, New_Occurrence_Of (Ent, Loc));
10602 Analyze_And_Resolve (N, Typ);
10603 end Make_Temporary_For_Slice;
10605 -- Local variables
10607 Pref : constant Node_Id := Prefix (N);
10608 Pref_Typ : Entity_Id := Etype (Pref);
10610 -- Start of processing for Expand_N_Slice
10612 begin
10613 -- Special handling for access types
10615 if Is_Access_Type (Pref_Typ) then
10616 Pref_Typ := Designated_Type (Pref_Typ);
10618 Rewrite (Pref,
10619 Make_Explicit_Dereference (Sloc (N),
10620 Prefix => Relocate_Node (Pref)));
10622 Analyze_And_Resolve (Pref, Pref_Typ);
10623 end if;
10625 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10626 -- function, then additional actuals must be passed.
10628 if Is_Build_In_Place_Function_Call (Pref) then
10629 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
10631 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
10632 -- containing build-in-place function calls whose returned object covers
10633 -- interface types.
10635 elsif Present (Unqual_BIP_Iface_Function_Call (Pref)) then
10636 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Pref);
10637 end if;
10639 -- The remaining case to be handled is packed slices. We can leave
10640 -- packed slices as they are in the following situations:
10642 -- 1. Right or left side of an assignment (we can handle this
10643 -- situation correctly in the assignment statement expansion).
10645 -- 2. Prefix of indexed component (the slide is optimized away in this
10646 -- case, see the start of Expand_N_Slice.)
10648 -- 3. Object renaming declaration, since we want the name of the
10649 -- slice, not the value.
10651 -- 4. Argument to procedure call, since copy-in/copy-out handling may
10652 -- be required, and this is handled in the expansion of call
10653 -- itself.
10655 -- 5. Prefix of an address attribute (this is an error which is caught
10656 -- elsewhere, and the expansion would interfere with generating the
10657 -- error message).
10659 if not Is_Packed (Typ) then
10661 -- Apply transformation for actuals of a function call, where
10662 -- Expand_Actuals is not used.
10664 if Nkind (Parent (N)) = N_Function_Call
10665 and then Is_Possibly_Unaligned_Slice (N)
10666 then
10667 Make_Temporary_For_Slice;
10668 end if;
10670 elsif Nkind (Parent (N)) = N_Assignment_Statement
10671 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
10672 and then Parent (N) = Name (Parent (Parent (N))))
10673 then
10674 return;
10676 elsif Nkind (Parent (N)) = N_Indexed_Component
10677 or else Is_Renamed_Object (N)
10678 or else Is_Procedure_Actual (N)
10679 then
10680 return;
10682 elsif Nkind (Parent (N)) = N_Attribute_Reference
10683 and then Attribute_Name (Parent (N)) = Name_Address
10684 then
10685 return;
10687 else
10688 Make_Temporary_For_Slice;
10689 end if;
10690 end Expand_N_Slice;
10692 ------------------------------
10693 -- Expand_N_Type_Conversion --
10694 ------------------------------
10696 procedure Expand_N_Type_Conversion (N : Node_Id) is
10697 Loc : constant Source_Ptr := Sloc (N);
10698 Operand : constant Node_Id := Expression (N);
10699 Target_Type : constant Entity_Id := Etype (N);
10700 Operand_Type : Entity_Id := Etype (Operand);
10702 procedure Handle_Changed_Representation;
10703 -- This is called in the case of record and array type conversions to
10704 -- see if there is a change of representation to be handled. Change of
10705 -- representation is actually handled at the assignment statement level,
10706 -- and what this procedure does is rewrite node N conversion as an
10707 -- assignment to temporary. If there is no change of representation,
10708 -- then the conversion node is unchanged.
10710 procedure Raise_Accessibility_Error;
10711 -- Called when we know that an accessibility check will fail. Rewrites
10712 -- node N to an appropriate raise statement and outputs warning msgs.
10713 -- The Etype of the raise node is set to Target_Type. Note that in this
10714 -- case the rest of the processing should be skipped (i.e. the call to
10715 -- this procedure will be followed by "goto Done").
10717 procedure Real_Range_Check;
10718 -- Handles generation of range check for real target value
10720 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
10721 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
10722 -- evaluates to True.
10724 -----------------------------------
10725 -- Handle_Changed_Representation --
10726 -----------------------------------
10728 procedure Handle_Changed_Representation is
10729 Temp : Entity_Id;
10730 Decl : Node_Id;
10731 Odef : Node_Id;
10732 N_Ix : Node_Id;
10733 Cons : List_Id;
10735 begin
10736 -- Nothing else to do if no change of representation
10738 if Same_Representation (Operand_Type, Target_Type) then
10739 return;
10741 -- The real change of representation work is done by the assignment
10742 -- statement processing. So if this type conversion is appearing as
10743 -- the expression of an assignment statement, nothing needs to be
10744 -- done to the conversion.
10746 elsif Nkind (Parent (N)) = N_Assignment_Statement then
10747 return;
10749 -- Otherwise we need to generate a temporary variable, and do the
10750 -- change of representation assignment into that temporary variable.
10751 -- The conversion is then replaced by a reference to this variable.
10753 else
10754 Cons := No_List;
10756 -- If type is unconstrained we have to add a constraint, copied
10757 -- from the actual value of the left-hand side.
10759 if not Is_Constrained (Target_Type) then
10760 if Has_Discriminants (Operand_Type) then
10762 -- A change of representation can only apply to untagged
10763 -- types. We need to build the constraint that applies to
10764 -- the target type, using the constraints of the operand.
10765 -- The analysis is complicated if there are both inherited
10766 -- discriminants and constrained discriminants.
10767 -- We iterate over the discriminants of the target, and
10768 -- find the discriminant of the same name:
10770 -- a) If there is a corresponding discriminant in the object
10771 -- then the value is a selected component of the operand.
10773 -- b) Otherwise the value of a constrained discriminant is
10774 -- found in the stored constraint of the operand.
10776 declare
10777 Stored : constant Elist_Id :=
10778 Stored_Constraint (Operand_Type);
10780 Elmt : Elmt_Id;
10782 Disc_O : Entity_Id;
10783 -- Discriminant of the operand type. Its value in the
10784 -- object is captured in a selected component.
10786 Disc_S : Entity_Id;
10787 -- Stored discriminant of the operand. If present, it
10788 -- corresponds to a constrained discriminant of the
10789 -- parent type.
10791 Disc_T : Entity_Id;
10792 -- Discriminant of the target type
10794 begin
10795 Disc_T := First_Discriminant (Target_Type);
10796 Disc_O := First_Discriminant (Operand_Type);
10797 Disc_S := First_Stored_Discriminant (Operand_Type);
10799 if Present (Stored) then
10800 Elmt := First_Elmt (Stored);
10801 else
10802 Elmt := No_Elmt; -- init to avoid warning
10803 end if;
10805 Cons := New_List;
10806 while Present (Disc_T) loop
10807 if Present (Disc_O)
10808 and then Chars (Disc_T) = Chars (Disc_O)
10809 then
10810 Append_To (Cons,
10811 Make_Selected_Component (Loc,
10812 Prefix =>
10813 Duplicate_Subexpr_Move_Checks (Operand),
10814 Selector_Name =>
10815 Make_Identifier (Loc, Chars (Disc_O))));
10816 Next_Discriminant (Disc_O);
10818 elsif Present (Disc_S) then
10819 Append_To (Cons, New_Copy_Tree (Node (Elmt)));
10820 Next_Elmt (Elmt);
10821 end if;
10823 Next_Discriminant (Disc_T);
10824 end loop;
10825 end;
10827 elsif Is_Array_Type (Operand_Type) then
10828 N_Ix := First_Index (Target_Type);
10829 Cons := New_List;
10831 for J in 1 .. Number_Dimensions (Operand_Type) loop
10833 -- We convert the bounds explicitly. We use an unchecked
10834 -- conversion because bounds checks are done elsewhere.
10836 Append_To (Cons,
10837 Make_Range (Loc,
10838 Low_Bound =>
10839 Unchecked_Convert_To (Etype (N_Ix),
10840 Make_Attribute_Reference (Loc,
10841 Prefix =>
10842 Duplicate_Subexpr_No_Checks
10843 (Operand, Name_Req => True),
10844 Attribute_Name => Name_First,
10845 Expressions => New_List (
10846 Make_Integer_Literal (Loc, J)))),
10848 High_Bound =>
10849 Unchecked_Convert_To (Etype (N_Ix),
10850 Make_Attribute_Reference (Loc,
10851 Prefix =>
10852 Duplicate_Subexpr_No_Checks
10853 (Operand, Name_Req => True),
10854 Attribute_Name => Name_Last,
10855 Expressions => New_List (
10856 Make_Integer_Literal (Loc, J))))));
10858 Next_Index (N_Ix);
10859 end loop;
10860 end if;
10861 end if;
10863 Odef := New_Occurrence_Of (Target_Type, Loc);
10865 if Present (Cons) then
10866 Odef :=
10867 Make_Subtype_Indication (Loc,
10868 Subtype_Mark => Odef,
10869 Constraint =>
10870 Make_Index_Or_Discriminant_Constraint (Loc,
10871 Constraints => Cons));
10872 end if;
10874 Temp := Make_Temporary (Loc, 'C');
10875 Decl :=
10876 Make_Object_Declaration (Loc,
10877 Defining_Identifier => Temp,
10878 Object_Definition => Odef);
10880 Set_No_Initialization (Decl, True);
10882 -- Insert required actions. It is essential to suppress checks
10883 -- since we have suppressed default initialization, which means
10884 -- that the variable we create may have no discriminants.
10886 Insert_Actions (N,
10887 New_List (
10888 Decl,
10889 Make_Assignment_Statement (Loc,
10890 Name => New_Occurrence_Of (Temp, Loc),
10891 Expression => Relocate_Node (N))),
10892 Suppress => All_Checks);
10894 Rewrite (N, New_Occurrence_Of (Temp, Loc));
10895 return;
10896 end if;
10897 end Handle_Changed_Representation;
10899 -------------------------------
10900 -- Raise_Accessibility_Error --
10901 -------------------------------
10903 procedure Raise_Accessibility_Error is
10904 begin
10905 Error_Msg_Warn := SPARK_Mode /= On;
10906 Rewrite (N,
10907 Make_Raise_Program_Error (Sloc (N),
10908 Reason => PE_Accessibility_Check_Failed));
10909 Set_Etype (N, Target_Type);
10911 Error_Msg_N ("<<accessibility check failure", N);
10912 Error_Msg_NE ("\<<& [", N, Standard_Program_Error);
10913 end Raise_Accessibility_Error;
10915 ----------------------
10916 -- Real_Range_Check --
10917 ----------------------
10919 -- Case of conversions to floating-point or fixed-point. If range checks
10920 -- are enabled and the target type has a range constraint, we convert:
10922 -- typ (x)
10924 -- to
10926 -- Tnn : typ'Base := typ'Base (x);
10927 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10928 -- Tnn
10930 -- This is necessary when there is a conversion of integer to float or
10931 -- to fixed-point to ensure that the correct checks are made. It is not
10932 -- necessary for float to float where it is enough to simply set the
10933 -- Do_Range_Check flag.
10935 procedure Real_Range_Check is
10936 Btyp : constant Entity_Id := Base_Type (Target_Type);
10937 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
10938 Hi : constant Node_Id := Type_High_Bound (Target_Type);
10939 Xtyp : constant Entity_Id := Etype (Operand);
10940 Conv : Node_Id;
10941 Tnn : Entity_Id;
10943 begin
10944 -- Nothing to do if conversion was rewritten
10946 if Nkind (N) /= N_Type_Conversion then
10947 return;
10948 end if;
10950 -- Nothing to do if range checks suppressed, or target has the same
10951 -- range as the base type (or is the base type).
10953 if Range_Checks_Suppressed (Target_Type)
10954 or else (Lo = Type_Low_Bound (Btyp)
10955 and then
10956 Hi = Type_High_Bound (Btyp))
10957 then
10958 return;
10959 end if;
10961 -- Nothing to do if expression is an entity on which checks have been
10962 -- suppressed.
10964 if Is_Entity_Name (Operand)
10965 and then Range_Checks_Suppressed (Entity (Operand))
10966 then
10967 return;
10968 end if;
10970 -- Nothing to do if bounds are all static and we can tell that the
10971 -- expression is within the bounds of the target. Note that if the
10972 -- operand is of an unconstrained floating-point type, then we do
10973 -- not trust it to be in range (might be infinite)
10975 declare
10976 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
10977 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
10979 begin
10980 if (not Is_Floating_Point_Type (Xtyp)
10981 or else Is_Constrained (Xtyp))
10982 and then Compile_Time_Known_Value (S_Lo)
10983 and then Compile_Time_Known_Value (S_Hi)
10984 and then Compile_Time_Known_Value (Hi)
10985 and then Compile_Time_Known_Value (Lo)
10986 then
10987 declare
10988 D_Lov : constant Ureal := Expr_Value_R (Lo);
10989 D_Hiv : constant Ureal := Expr_Value_R (Hi);
10990 S_Lov : Ureal;
10991 S_Hiv : Ureal;
10993 begin
10994 if Is_Real_Type (Xtyp) then
10995 S_Lov := Expr_Value_R (S_Lo);
10996 S_Hiv := Expr_Value_R (S_Hi);
10997 else
10998 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
10999 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
11000 end if;
11002 if D_Hiv > D_Lov
11003 and then S_Lov >= D_Lov
11004 and then S_Hiv <= D_Hiv
11005 then
11006 -- Unset the range check flag on the current value of
11007 -- Expression (N), since the captured Operand may have
11008 -- been rewritten (such as for the case of a conversion
11009 -- to a fixed-point type).
11011 Set_Do_Range_Check (Expression (N), False);
11013 return;
11014 end if;
11015 end;
11016 end if;
11017 end;
11019 -- For float to float conversions, we are done
11021 if Is_Floating_Point_Type (Xtyp)
11022 and then
11023 Is_Floating_Point_Type (Btyp)
11024 then
11025 return;
11026 end if;
11028 -- Otherwise rewrite the conversion as described above
11030 Conv := Relocate_Node (N);
11031 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
11032 Set_Etype (Conv, Btyp);
11034 -- Enable overflow except for case of integer to float conversions,
11035 -- where it is never required, since we can never have overflow in
11036 -- this case.
11038 if not Is_Integer_Type (Etype (Operand)) then
11039 Enable_Overflow_Check (Conv);
11040 end if;
11042 Tnn := Make_Temporary (Loc, 'T', Conv);
11044 Insert_Actions (N, New_List (
11045 Make_Object_Declaration (Loc,
11046 Defining_Identifier => Tnn,
11047 Object_Definition => New_Occurrence_Of (Btyp, Loc),
11048 Constant_Present => True,
11049 Expression => Conv),
11051 Make_Raise_Constraint_Error (Loc,
11052 Condition =>
11053 Make_Or_Else (Loc,
11054 Left_Opnd =>
11055 Make_Op_Lt (Loc,
11056 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
11057 Right_Opnd =>
11058 Make_Attribute_Reference (Loc,
11059 Attribute_Name => Name_First,
11060 Prefix =>
11061 New_Occurrence_Of (Target_Type, Loc))),
11063 Right_Opnd =>
11064 Make_Op_Gt (Loc,
11065 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
11066 Right_Opnd =>
11067 Make_Attribute_Reference (Loc,
11068 Attribute_Name => Name_Last,
11069 Prefix =>
11070 New_Occurrence_Of (Target_Type, Loc)))),
11071 Reason => CE_Range_Check_Failed)));
11073 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
11074 Analyze_And_Resolve (N, Btyp);
11075 end Real_Range_Check;
11077 -----------------------------
11078 -- Has_Extra_Accessibility --
11079 -----------------------------
11081 -- Returns true for a formal of an anonymous access type or for
11082 -- an Ada 2012-style stand-alone object of an anonymous access type.
11084 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
11085 begin
11086 if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then
11087 return Present (Effective_Extra_Accessibility (Id));
11088 else
11089 return False;
11090 end if;
11091 end Has_Extra_Accessibility;
11093 -- Start of processing for Expand_N_Type_Conversion
11095 begin
11096 -- First remove check marks put by the semantic analysis on the type
11097 -- conversion between array types. We need these checks, and they will
11098 -- be generated by this expansion routine, but we do not depend on these
11099 -- flags being set, and since we do intend to expand the checks in the
11100 -- front end, we don't want them on the tree passed to the back end.
11102 if Is_Array_Type (Target_Type) then
11103 if Is_Constrained (Target_Type) then
11104 Set_Do_Length_Check (N, False);
11105 else
11106 Set_Do_Range_Check (Operand, False);
11107 end if;
11108 end if;
11110 -- Nothing at all to do if conversion is to the identical type so remove
11111 -- the conversion completely, it is useless, except that it may carry
11112 -- an Assignment_OK attribute, which must be propagated to the operand.
11114 if Operand_Type = Target_Type then
11115 if Assignment_OK (N) then
11116 Set_Assignment_OK (Operand);
11117 end if;
11119 Rewrite (N, Relocate_Node (Operand));
11120 goto Done;
11121 end if;
11123 -- Nothing to do if this is the second argument of read. This is a
11124 -- "backwards" conversion that will be handled by the specialized code
11125 -- in attribute processing.
11127 if Nkind (Parent (N)) = N_Attribute_Reference
11128 and then Attribute_Name (Parent (N)) = Name_Read
11129 and then Next (First (Expressions (Parent (N)))) = N
11130 then
11131 goto Done;
11132 end if;
11134 -- Check for case of converting to a type that has an invariant
11135 -- associated with it. This requires an invariant check. We insert
11136 -- a call:
11138 -- invariant_check (typ (expr))
11140 -- in the code, after removing side effects from the expression.
11141 -- This is clearer than replacing the conversion into an expression
11142 -- with actions, because the context may impose additional actions
11143 -- (tag checks, membership tests, etc.) that conflict with this
11144 -- rewriting (used previously).
11146 -- Note: the Comes_From_Source check, and then the resetting of this
11147 -- flag prevents what would otherwise be an infinite recursion.
11149 if Has_Invariants (Target_Type)
11150 and then Present (Invariant_Procedure (Target_Type))
11151 and then Comes_From_Source (N)
11152 then
11153 Set_Comes_From_Source (N, False);
11154 Remove_Side_Effects (N);
11155 Insert_Action (N, Make_Invariant_Call (Duplicate_Subexpr (N)));
11156 goto Done;
11157 end if;
11159 -- Here if we may need to expand conversion
11161 -- If the operand of the type conversion is an arithmetic operation on
11162 -- signed integers, and the based type of the signed integer type in
11163 -- question is smaller than Standard.Integer, we promote both of the
11164 -- operands to type Integer.
11166 -- For example, if we have
11168 -- target-type (opnd1 + opnd2)
11170 -- and opnd1 and opnd2 are of type short integer, then we rewrite
11171 -- this as:
11173 -- target-type (integer(opnd1) + integer(opnd2))
11175 -- We do this because we are always allowed to compute in a larger type
11176 -- if we do the right thing with the result, and in this case we are
11177 -- going to do a conversion which will do an appropriate check to make
11178 -- sure that things are in range of the target type in any case. This
11179 -- avoids some unnecessary intermediate overflows.
11181 -- We might consider a similar transformation in the case where the
11182 -- target is a real type or a 64-bit integer type, and the operand
11183 -- is an arithmetic operation using a 32-bit integer type. However,
11184 -- we do not bother with this case, because it could cause significant
11185 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
11186 -- much cheaper, but we don't want different behavior on 32-bit and
11187 -- 64-bit machines. Note that the exclusion of the 64-bit case also
11188 -- handles the configurable run-time cases where 64-bit arithmetic
11189 -- may simply be unavailable.
11191 -- Note: this circuit is partially redundant with respect to the circuit
11192 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
11193 -- the processing here. Also we still need the Checks circuit, since we
11194 -- have to be sure not to generate junk overflow checks in the first
11195 -- place, since it would be trick to remove them here.
11197 if Integer_Promotion_Possible (N) then
11199 -- All conditions met, go ahead with transformation
11201 declare
11202 Opnd : Node_Id;
11203 L, R : Node_Id;
11205 begin
11206 R :=
11207 Make_Type_Conversion (Loc,
11208 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
11209 Expression => Relocate_Node (Right_Opnd (Operand)));
11211 Opnd := New_Op_Node (Nkind (Operand), Loc);
11212 Set_Right_Opnd (Opnd, R);
11214 if Nkind (Operand) in N_Binary_Op then
11215 L :=
11216 Make_Type_Conversion (Loc,
11217 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
11218 Expression => Relocate_Node (Left_Opnd (Operand)));
11220 Set_Left_Opnd (Opnd, L);
11221 end if;
11223 Rewrite (N,
11224 Make_Type_Conversion (Loc,
11225 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
11226 Expression => Opnd));
11228 Analyze_And_Resolve (N, Target_Type);
11229 goto Done;
11230 end;
11231 end if;
11233 -- Do validity check if validity checking operands
11235 if Validity_Checks_On and Validity_Check_Operands then
11236 Ensure_Valid (Operand);
11237 end if;
11239 -- Special case of converting from non-standard boolean type
11241 if Is_Boolean_Type (Operand_Type)
11242 and then (Nonzero_Is_True (Operand_Type))
11243 then
11244 Adjust_Condition (Operand);
11245 Set_Etype (Operand, Standard_Boolean);
11246 Operand_Type := Standard_Boolean;
11247 end if;
11249 -- Case of converting to an access type
11251 if Is_Access_Type (Target_Type) then
11253 -- If this type conversion was internally generated by the front end
11254 -- to displace the pointer to the object to reference an interface
11255 -- type and the original node was an Unrestricted_Access attribute,
11256 -- then skip applying accessibility checks (because, according to the
11257 -- GNAT Reference Manual, this attribute is similar to 'Access except
11258 -- that all accessibility and aliased view checks are omitted).
11260 if not Comes_From_Source (N)
11261 and then Is_Interface (Designated_Type (Target_Type))
11262 and then Nkind (Original_Node (N)) = N_Attribute_Reference
11263 and then Attribute_Name (Original_Node (N)) =
11264 Name_Unrestricted_Access
11265 then
11266 null;
11268 -- Apply an accessibility check when the conversion operand is an
11269 -- access parameter (or a renaming thereof), unless conversion was
11270 -- expanded from an Unchecked_ or Unrestricted_Access attribute,
11271 -- or for the actual of a class-wide interface parameter. Note that
11272 -- other checks may still need to be applied below (such as tagged
11273 -- type checks).
11275 elsif Is_Entity_Name (Operand)
11276 and then Has_Extra_Accessibility (Entity (Operand))
11277 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
11278 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
11279 or else Attribute_Name (Original_Node (N)) = Name_Access)
11280 then
11281 if not Comes_From_Source (N)
11282 and then Nkind_In (Parent (N), N_Function_Call,
11283 N_Procedure_Call_Statement)
11284 and then Is_Interface (Designated_Type (Target_Type))
11285 and then Is_Class_Wide_Type (Designated_Type (Target_Type))
11286 then
11287 null;
11289 else
11290 Apply_Accessibility_Check
11291 (Operand, Target_Type, Insert_Node => Operand);
11292 end if;
11294 -- If the level of the operand type is statically deeper than the
11295 -- level of the target type, then force Program_Error. Note that this
11296 -- can only occur for cases where the attribute is within the body of
11297 -- an instantiation, otherwise the conversion will already have been
11298 -- rejected as illegal.
11300 -- Note: warnings are issued by the analyzer for the instance cases
11302 elsif In_Instance_Body
11304 -- The case where the target type is an anonymous access type of
11305 -- a discriminant is excluded, because the level of such a type
11306 -- depends on the context and currently the level returned for such
11307 -- types is zero, resulting in warnings about about check failures
11308 -- in certain legal cases involving class-wide interfaces as the
11309 -- designated type (some cases, such as return statements, are
11310 -- checked at run time, but not clear if these are handled right
11311 -- in general, see 3.10.2(12/2-12.5/3) ???).
11313 and then
11314 not (Ekind (Target_Type) = E_Anonymous_Access_Type
11315 and then Present (Associated_Node_For_Itype (Target_Type))
11316 and then Nkind (Associated_Node_For_Itype (Target_Type)) =
11317 N_Discriminant_Specification)
11318 and then
11319 Type_Access_Level (Operand_Type) > Type_Access_Level (Target_Type)
11320 then
11321 Raise_Accessibility_Error;
11322 goto Done;
11324 -- When the operand is a selected access discriminant the check needs
11325 -- to be made against the level of the object denoted by the prefix
11326 -- of the selected name. Force Program_Error for this case as well
11327 -- (this accessibility violation can only happen if within the body
11328 -- of an instantiation).
11330 elsif In_Instance_Body
11331 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
11332 and then Nkind (Operand) = N_Selected_Component
11333 and then Ekind (Entity (Selector_Name (Operand))) = E_Discriminant
11334 and then Object_Access_Level (Operand) >
11335 Type_Access_Level (Target_Type)
11336 then
11337 Raise_Accessibility_Error;
11338 goto Done;
11339 end if;
11340 end if;
11342 -- Case of conversions of tagged types and access to tagged types
11344 -- When needed, that is to say when the expression is class-wide, Add
11345 -- runtime a tag check for (strict) downward conversion by using the
11346 -- membership test, generating:
11348 -- [constraint_error when Operand not in Target_Type'Class]
11350 -- or in the access type case
11352 -- [constraint_error
11353 -- when Operand /= null
11354 -- and then Operand.all not in
11355 -- Designated_Type (Target_Type)'Class]
11357 if (Is_Access_Type (Target_Type)
11358 and then Is_Tagged_Type (Designated_Type (Target_Type)))
11359 or else Is_Tagged_Type (Target_Type)
11360 then
11361 -- Do not do any expansion in the access type case if the parent is a
11362 -- renaming, since this is an error situation which will be caught by
11363 -- Sem_Ch8, and the expansion can interfere with this error check.
11365 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
11366 goto Done;
11367 end if;
11369 -- Otherwise, proceed with processing tagged conversion
11371 Tagged_Conversion : declare
11372 Actual_Op_Typ : Entity_Id;
11373 Actual_Targ_Typ : Entity_Id;
11374 Make_Conversion : Boolean := False;
11375 Root_Op_Typ : Entity_Id;
11377 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
11378 -- Create a membership check to test whether Operand is a member
11379 -- of Targ_Typ. If the original Target_Type is an access, include
11380 -- a test for null value. The check is inserted at N.
11382 --------------------
11383 -- Make_Tag_Check --
11384 --------------------
11386 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
11387 Cond : Node_Id;
11389 begin
11390 -- Generate:
11391 -- [Constraint_Error
11392 -- when Operand /= null
11393 -- and then Operand.all not in Targ_Typ]
11395 if Is_Access_Type (Target_Type) then
11396 Cond :=
11397 Make_And_Then (Loc,
11398 Left_Opnd =>
11399 Make_Op_Ne (Loc,
11400 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
11401 Right_Opnd => Make_Null (Loc)),
11403 Right_Opnd =>
11404 Make_Not_In (Loc,
11405 Left_Opnd =>
11406 Make_Explicit_Dereference (Loc,
11407 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
11408 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)));
11410 -- Generate:
11411 -- [Constraint_Error when Operand not in Targ_Typ]
11413 else
11414 Cond :=
11415 Make_Not_In (Loc,
11416 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
11417 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc));
11418 end if;
11420 Insert_Action (N,
11421 Make_Raise_Constraint_Error (Loc,
11422 Condition => Cond,
11423 Reason => CE_Tag_Check_Failed),
11424 Suppress => All_Checks);
11425 end Make_Tag_Check;
11427 -- Start of processing for Tagged_Conversion
11429 begin
11430 -- Handle entities from the limited view
11432 if Is_Access_Type (Operand_Type) then
11433 Actual_Op_Typ :=
11434 Available_View (Designated_Type (Operand_Type));
11435 else
11436 Actual_Op_Typ := Operand_Type;
11437 end if;
11439 if Is_Access_Type (Target_Type) then
11440 Actual_Targ_Typ :=
11441 Available_View (Designated_Type (Target_Type));
11442 else
11443 Actual_Targ_Typ := Target_Type;
11444 end if;
11446 Root_Op_Typ := Root_Type (Actual_Op_Typ);
11448 -- Ada 2005 (AI-251): Handle interface type conversion
11450 if Is_Interface (Actual_Op_Typ)
11451 or else
11452 Is_Interface (Actual_Targ_Typ)
11453 then
11454 Expand_Interface_Conversion (N);
11455 goto Done;
11456 end if;
11458 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
11460 -- Create a runtime tag check for a downward class-wide type
11461 -- conversion.
11463 if Is_Class_Wide_Type (Actual_Op_Typ)
11464 and then Actual_Op_Typ /= Actual_Targ_Typ
11465 and then Root_Op_Typ /= Actual_Targ_Typ
11466 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
11467 Use_Full_View => True)
11468 then
11469 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
11470 Make_Conversion := True;
11471 end if;
11473 -- AI05-0073: If the result subtype of the function is defined
11474 -- by an access_definition designating a specific tagged type
11475 -- T, a check is made that the result value is null or the tag
11476 -- of the object designated by the result value identifies T.
11477 -- Constraint_Error is raised if this check fails.
11479 if Nkind (Parent (N)) = N_Simple_Return_Statement then
11480 declare
11481 Func : Entity_Id;
11482 Func_Typ : Entity_Id;
11484 begin
11485 -- Climb scope stack looking for the enclosing function
11487 Func := Current_Scope;
11488 while Present (Func)
11489 and then Ekind (Func) /= E_Function
11490 loop
11491 Func := Scope (Func);
11492 end loop;
11494 -- The function's return subtype must be defined using
11495 -- an access definition.
11497 if Nkind (Result_Definition (Parent (Func))) =
11498 N_Access_Definition
11499 then
11500 Func_Typ := Directly_Designated_Type (Etype (Func));
11502 -- The return subtype denotes a specific tagged type,
11503 -- in other words, a non class-wide type.
11505 if Is_Tagged_Type (Func_Typ)
11506 and then not Is_Class_Wide_Type (Func_Typ)
11507 then
11508 Make_Tag_Check (Actual_Targ_Typ);
11509 Make_Conversion := True;
11510 end if;
11511 end if;
11512 end;
11513 end if;
11515 -- We have generated a tag check for either a class-wide type
11516 -- conversion or for AI05-0073.
11518 if Make_Conversion then
11519 declare
11520 Conv : Node_Id;
11521 begin
11522 Conv :=
11523 Make_Unchecked_Type_Conversion (Loc,
11524 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
11525 Expression => Relocate_Node (Expression (N)));
11526 Rewrite (N, Conv);
11527 Analyze_And_Resolve (N, Target_Type);
11528 end;
11529 end if;
11530 end if;
11531 end Tagged_Conversion;
11533 -- Case of other access type conversions
11535 elsif Is_Access_Type (Target_Type) then
11536 Apply_Constraint_Check (Operand, Target_Type);
11538 -- Case of conversions from a fixed-point type
11540 -- These conversions require special expansion and processing, found in
11541 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
11542 -- since from a semantic point of view, these are simple integer
11543 -- conversions, which do not need further processing.
11545 elsif Is_Fixed_Point_Type (Operand_Type)
11546 and then not Conversion_OK (N)
11547 then
11548 -- We should never see universal fixed at this case, since the
11549 -- expansion of the constituent divide or multiply should have
11550 -- eliminated the explicit mention of universal fixed.
11552 pragma Assert (Operand_Type /= Universal_Fixed);
11554 -- Check for special case of the conversion to universal real that
11555 -- occurs as a result of the use of a round attribute. In this case,
11556 -- the real type for the conversion is taken from the target type of
11557 -- the Round attribute and the result must be marked as rounded.
11559 if Target_Type = Universal_Real
11560 and then Nkind (Parent (N)) = N_Attribute_Reference
11561 and then Attribute_Name (Parent (N)) = Name_Round
11562 then
11563 Set_Rounded_Result (N);
11564 Set_Etype (N, Etype (Parent (N)));
11565 end if;
11567 -- Otherwise do correct fixed-conversion, but skip these if the
11568 -- Conversion_OK flag is set, because from a semantic point of view
11569 -- these are simple integer conversions needing no further processing
11570 -- (the backend will simply treat them as integers).
11572 if not Conversion_OK (N) then
11573 if Is_Fixed_Point_Type (Etype (N)) then
11574 Expand_Convert_Fixed_To_Fixed (N);
11575 Real_Range_Check;
11577 elsif Is_Integer_Type (Etype (N)) then
11578 Expand_Convert_Fixed_To_Integer (N);
11580 else
11581 pragma Assert (Is_Floating_Point_Type (Etype (N)));
11582 Expand_Convert_Fixed_To_Float (N);
11583 Real_Range_Check;
11584 end if;
11585 end if;
11587 -- Case of conversions to a fixed-point type
11589 -- These conversions require special expansion and processing, found in
11590 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
11591 -- since from a semantic point of view, these are simple integer
11592 -- conversions, which do not need further processing.
11594 elsif Is_Fixed_Point_Type (Target_Type)
11595 and then not Conversion_OK (N)
11596 then
11597 if Is_Integer_Type (Operand_Type) then
11598 Expand_Convert_Integer_To_Fixed (N);
11599 Real_Range_Check;
11600 else
11601 pragma Assert (Is_Floating_Point_Type (Operand_Type));
11602 Expand_Convert_Float_To_Fixed (N);
11603 Real_Range_Check;
11604 end if;
11606 -- Case of float-to-integer conversions
11608 -- We also handle float-to-fixed conversions with Conversion_OK set
11609 -- since semantically the fixed-point target is treated as though it
11610 -- were an integer in such cases.
11612 elsif Is_Floating_Point_Type (Operand_Type)
11613 and then
11614 (Is_Integer_Type (Target_Type)
11615 or else
11616 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
11617 then
11618 -- One more check here, gcc is still not able to do conversions of
11619 -- this type with proper overflow checking, and so gigi is doing an
11620 -- approximation of what is required by doing floating-point compares
11621 -- with the end-point. But that can lose precision in some cases, and
11622 -- give a wrong result. Converting the operand to Universal_Real is
11623 -- helpful, but still does not catch all cases with 64-bit integers
11624 -- on targets with only 64-bit floats.
11626 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
11627 -- Can this code be removed ???
11629 if Do_Range_Check (Operand) then
11630 Rewrite (Operand,
11631 Make_Type_Conversion (Loc,
11632 Subtype_Mark =>
11633 New_Occurrence_Of (Universal_Real, Loc),
11634 Expression =>
11635 Relocate_Node (Operand)));
11637 Set_Etype (Operand, Universal_Real);
11638 Enable_Range_Check (Operand);
11639 Set_Do_Range_Check (Expression (Operand), False);
11640 end if;
11642 -- Case of array conversions
11644 -- Expansion of array conversions, add required length/range checks but
11645 -- only do this if there is no change of representation. For handling of
11646 -- this case, see Handle_Changed_Representation.
11648 elsif Is_Array_Type (Target_Type) then
11649 if Is_Constrained (Target_Type) then
11650 Apply_Length_Check (Operand, Target_Type);
11651 else
11652 Apply_Range_Check (Operand, Target_Type);
11653 end if;
11655 Handle_Changed_Representation;
11657 -- Case of conversions of discriminated types
11659 -- Add required discriminant checks if target is constrained. Again this
11660 -- change is skipped if we have a change of representation.
11662 elsif Has_Discriminants (Target_Type)
11663 and then Is_Constrained (Target_Type)
11664 then
11665 Apply_Discriminant_Check (Operand, Target_Type);
11666 Handle_Changed_Representation;
11668 -- Case of all other record conversions. The only processing required
11669 -- is to check for a change of representation requiring the special
11670 -- assignment processing.
11672 elsif Is_Record_Type (Target_Type) then
11674 -- Ada 2005 (AI-216): Program_Error is raised when converting from
11675 -- a derived Unchecked_Union type to an unconstrained type that is
11676 -- not Unchecked_Union if the operand lacks inferable discriminants.
11678 if Is_Derived_Type (Operand_Type)
11679 and then Is_Unchecked_Union (Base_Type (Operand_Type))
11680 and then not Is_Constrained (Target_Type)
11681 and then not Is_Unchecked_Union (Base_Type (Target_Type))
11682 and then not Has_Inferable_Discriminants (Operand)
11683 then
11684 -- To prevent Gigi from generating illegal code, we generate a
11685 -- Program_Error node, but we give it the target type of the
11686 -- conversion (is this requirement documented somewhere ???)
11688 declare
11689 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
11690 Reason => PE_Unchecked_Union_Restriction);
11692 begin
11693 Set_Etype (PE, Target_Type);
11694 Rewrite (N, PE);
11696 end;
11697 else
11698 Handle_Changed_Representation;
11699 end if;
11701 -- Case of conversions of enumeration types
11703 elsif Is_Enumeration_Type (Target_Type) then
11705 -- Special processing is required if there is a change of
11706 -- representation (from enumeration representation clauses).
11708 if not Same_Representation (Target_Type, Operand_Type) then
11710 -- Convert: x(y) to x'val (ytyp'val (y))
11712 Rewrite (N,
11713 Make_Attribute_Reference (Loc,
11714 Prefix => New_Occurrence_Of (Target_Type, Loc),
11715 Attribute_Name => Name_Val,
11716 Expressions => New_List (
11717 Make_Attribute_Reference (Loc,
11718 Prefix => New_Occurrence_Of (Operand_Type, Loc),
11719 Attribute_Name => Name_Pos,
11720 Expressions => New_List (Operand)))));
11722 Analyze_And_Resolve (N, Target_Type);
11723 end if;
11725 -- Case of conversions to floating-point
11727 elsif Is_Floating_Point_Type (Target_Type) then
11728 Real_Range_Check;
11729 end if;
11731 -- At this stage, either the conversion node has been transformed into
11732 -- some other equivalent expression, or left as a conversion that can be
11733 -- handled by Gigi, in the following cases:
11735 -- Conversions with no change of representation or type
11737 -- Numeric conversions involving integer, floating- and fixed-point
11738 -- values. Fixed-point values are allowed only if Conversion_OK is
11739 -- set, i.e. if the fixed-point values are to be treated as integers.
11741 -- No other conversions should be passed to Gigi
11743 -- Check: are these rules stated in sinfo??? if so, why restate here???
11745 -- The only remaining step is to generate a range check if we still have
11746 -- a type conversion at this stage and Do_Range_Check is set. For now we
11747 -- do this only for conversions of discrete types and for float-to-float
11748 -- conversions.
11750 if Nkind (N) = N_Type_Conversion then
11752 -- For now we only support floating-point cases where both source
11753 -- and target are floating-point types. Conversions where the source
11754 -- and target involve integer or fixed-point types are still TBD,
11755 -- though not clear whether those can even happen at this point, due
11756 -- to transformations above. ???
11758 if Is_Floating_Point_Type (Etype (N))
11759 and then Is_Floating_Point_Type (Etype (Expression (N)))
11760 then
11761 if Do_Range_Check (Expression (N))
11762 and then Is_Floating_Point_Type (Target_Type)
11763 then
11764 Generate_Range_Check
11765 (Expression (N), Target_Type, CE_Range_Check_Failed);
11766 end if;
11768 -- Discrete-to-discrete conversions
11770 elsif Is_Discrete_Type (Etype (N)) then
11771 declare
11772 Expr : constant Node_Id := Expression (N);
11773 Ftyp : Entity_Id;
11774 Ityp : Entity_Id;
11776 begin
11777 if Do_Range_Check (Expr)
11778 and then Is_Discrete_Type (Etype (Expr))
11779 then
11780 Set_Do_Range_Check (Expr, False);
11782 -- Before we do a range check, we have to deal with treating
11783 -- a fixed-point operand as an integer. The way we do this
11784 -- is simply to do an unchecked conversion to an appropriate
11785 -- integer type large enough to hold the result.
11787 -- This code is not active yet, because we are only dealing
11788 -- with discrete types so far ???
11790 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
11791 and then Treat_Fixed_As_Integer (Expr)
11792 then
11793 Ftyp := Base_Type (Etype (Expr));
11795 if Esize (Ftyp) >= Esize (Standard_Integer) then
11796 Ityp := Standard_Long_Long_Integer;
11797 else
11798 Ityp := Standard_Integer;
11799 end if;
11801 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
11802 end if;
11804 -- Reset overflow flag, since the range check will include
11805 -- dealing with possible overflow, and generate the check.
11806 -- If Address is either a source type or target type,
11807 -- suppress range check to avoid typing anomalies when
11808 -- it is a visible integer type.
11810 Set_Do_Overflow_Check (N, False);
11812 if not Is_Descendant_Of_Address (Etype (Expr))
11813 and then not Is_Descendant_Of_Address (Target_Type)
11814 then
11815 Generate_Range_Check
11816 (Expr, Target_Type, CE_Range_Check_Failed);
11817 end if;
11818 end if;
11819 end;
11820 end if;
11821 end if;
11823 -- Here at end of processing
11825 <<Done>>
11826 -- Apply predicate check if required. Note that we can't just call
11827 -- Apply_Predicate_Check here, because the type looks right after
11828 -- the conversion and it would omit the check. The Comes_From_Source
11829 -- guard is necessary to prevent infinite recursions when we generate
11830 -- internal conversions for the purpose of checking predicates.
11832 if Present (Predicate_Function (Target_Type))
11833 and then not Predicates_Ignored (Target_Type)
11834 and then Target_Type /= Operand_Type
11835 and then Comes_From_Source (N)
11836 then
11837 declare
11838 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
11840 begin
11841 -- Avoid infinite recursion on the subsequent expansion of
11842 -- of the copy of the original type conversion.
11844 Set_Comes_From_Source (New_Expr, False);
11845 Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr));
11846 end;
11847 end if;
11848 end Expand_N_Type_Conversion;
11850 -----------------------------------
11851 -- Expand_N_Unchecked_Expression --
11852 -----------------------------------
11854 -- Remove the unchecked expression node from the tree. Its job was simply
11855 -- to make sure that its constituent expression was handled with checks
11856 -- off, and now that that is done, we can remove it from the tree, and
11857 -- indeed must, since Gigi does not expect to see these nodes.
11859 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
11860 Exp : constant Node_Id := Expression (N);
11861 begin
11862 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
11863 Rewrite (N, Exp);
11864 end Expand_N_Unchecked_Expression;
11866 ----------------------------------------
11867 -- Expand_N_Unchecked_Type_Conversion --
11868 ----------------------------------------
11870 -- If this cannot be handled by Gigi and we haven't already made a
11871 -- temporary for it, do it now.
11873 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
11874 Target_Type : constant Entity_Id := Etype (N);
11875 Operand : constant Node_Id := Expression (N);
11876 Operand_Type : constant Entity_Id := Etype (Operand);
11878 begin
11879 -- Nothing at all to do if conversion is to the identical type so remove
11880 -- the conversion completely, it is useless, except that it may carry
11881 -- an Assignment_OK indication which must be propagated to the operand.
11883 if Operand_Type = Target_Type then
11885 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
11887 if Assignment_OK (N) then
11888 Set_Assignment_OK (Operand);
11889 end if;
11891 Rewrite (N, Relocate_Node (Operand));
11892 return;
11893 end if;
11895 -- If we have a conversion of a compile time known value to a target
11896 -- type and the value is in range of the target type, then we can simply
11897 -- replace the construct by an integer literal of the correct type. We
11898 -- only apply this to integer types being converted. Possibly it may
11899 -- apply in other cases, but it is too much trouble to worry about.
11901 -- Note that we do not do this transformation if the Kill_Range_Check
11902 -- flag is set, since then the value may be outside the expected range.
11903 -- This happens in the Normalize_Scalars case.
11905 -- We also skip this if either the target or operand type is biased
11906 -- because in this case, the unchecked conversion is supposed to
11907 -- preserve the bit pattern, not the integer value.
11909 if Is_Integer_Type (Target_Type)
11910 and then not Has_Biased_Representation (Target_Type)
11911 and then Is_Integer_Type (Operand_Type)
11912 and then not Has_Biased_Representation (Operand_Type)
11913 and then Compile_Time_Known_Value (Operand)
11914 and then not Kill_Range_Check (N)
11915 then
11916 declare
11917 Val : constant Uint := Expr_Value (Operand);
11919 begin
11920 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
11921 and then
11922 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
11923 and then
11924 Val >= Expr_Value (Type_Low_Bound (Target_Type))
11925 and then
11926 Val <= Expr_Value (Type_High_Bound (Target_Type))
11927 then
11928 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
11930 -- If Address is the target type, just set the type to avoid a
11931 -- spurious type error on the literal when Address is a visible
11932 -- integer type.
11934 if Is_Descendant_Of_Address (Target_Type) then
11935 Set_Etype (N, Target_Type);
11936 else
11937 Analyze_And_Resolve (N, Target_Type);
11938 end if;
11940 return;
11941 end if;
11942 end;
11943 end if;
11945 -- Nothing to do if conversion is safe
11947 if Safe_Unchecked_Type_Conversion (N) then
11948 return;
11949 end if;
11951 -- Otherwise force evaluation unless Assignment_OK flag is set (this
11952 -- flag indicates ??? More comments needed here)
11954 if Assignment_OK (N) then
11955 null;
11956 else
11957 Force_Evaluation (N);
11958 end if;
11959 end Expand_N_Unchecked_Type_Conversion;
11961 ----------------------------
11962 -- Expand_Record_Equality --
11963 ----------------------------
11965 -- For non-variant records, Equality is expanded when needed into:
11967 -- and then Lhs.Discr1 = Rhs.Discr1
11968 -- and then ...
11969 -- and then Lhs.Discrn = Rhs.Discrn
11970 -- and then Lhs.Cmp1 = Rhs.Cmp1
11971 -- and then ...
11972 -- and then Lhs.Cmpn = Rhs.Cmpn
11974 -- The expression is folded by the back end for adjacent fields. This
11975 -- function is called for tagged record in only one occasion: for imple-
11976 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
11977 -- otherwise the primitive "=" is used directly.
11979 function Expand_Record_Equality
11980 (Nod : Node_Id;
11981 Typ : Entity_Id;
11982 Lhs : Node_Id;
11983 Rhs : Node_Id;
11984 Bodies : List_Id) return Node_Id
11986 Loc : constant Source_Ptr := Sloc (Nod);
11988 Result : Node_Id;
11989 C : Entity_Id;
11991 First_Time : Boolean := True;
11993 function Element_To_Compare (C : Entity_Id) return Entity_Id;
11994 -- Return the next discriminant or component to compare, starting with
11995 -- C, skipping inherited components.
11997 ------------------------
11998 -- Element_To_Compare --
11999 ------------------------
12001 function Element_To_Compare (C : Entity_Id) return Entity_Id is
12002 Comp : Entity_Id;
12004 begin
12005 Comp := C;
12006 loop
12007 -- Exit loop when the next element to be compared is found, or
12008 -- there is no more such element.
12010 exit when No (Comp);
12012 exit when Ekind_In (Comp, E_Discriminant, E_Component)
12013 and then not (
12015 -- Skip inherited components
12017 -- Note: for a tagged type, we always generate the "=" primitive
12018 -- for the base type (not on the first subtype), so the test for
12019 -- Comp /= Original_Record_Component (Comp) is True for
12020 -- inherited components only.
12022 (Is_Tagged_Type (Typ)
12023 and then Comp /= Original_Record_Component (Comp))
12025 -- Skip _Tag
12027 or else Chars (Comp) = Name_uTag
12029 -- Skip interface elements (secondary tags???)
12031 or else Is_Interface (Etype (Comp)));
12033 Next_Entity (Comp);
12034 end loop;
12036 return Comp;
12037 end Element_To_Compare;
12039 -- Start of processing for Expand_Record_Equality
12041 begin
12042 -- Generates the following code: (assuming that Typ has one Discr and
12043 -- component C2 is also a record)
12045 -- True
12046 -- and then Lhs.Discr1 = Rhs.Discr1
12047 -- and then Lhs.C1 = Rhs.C1
12048 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
12049 -- and then ...
12050 -- and then Lhs.Cmpn = Rhs.Cmpn
12052 Result := New_Occurrence_Of (Standard_True, Loc);
12053 C := Element_To_Compare (First_Entity (Typ));
12054 while Present (C) loop
12055 declare
12056 New_Lhs : Node_Id;
12057 New_Rhs : Node_Id;
12058 Check : Node_Id;
12060 begin
12061 if First_Time then
12062 First_Time := False;
12063 New_Lhs := Lhs;
12064 New_Rhs := Rhs;
12065 else
12066 New_Lhs := New_Copy_Tree (Lhs);
12067 New_Rhs := New_Copy_Tree (Rhs);
12068 end if;
12070 Check :=
12071 Expand_Composite_Equality (Nod, Etype (C),
12072 Lhs =>
12073 Make_Selected_Component (Loc,
12074 Prefix => New_Lhs,
12075 Selector_Name => New_Occurrence_Of (C, Loc)),
12076 Rhs =>
12077 Make_Selected_Component (Loc,
12078 Prefix => New_Rhs,
12079 Selector_Name => New_Occurrence_Of (C, Loc)),
12080 Bodies => Bodies);
12082 -- If some (sub)component is an unchecked_union, the whole
12083 -- operation will raise program error.
12085 if Nkind (Check) = N_Raise_Program_Error then
12086 Result := Check;
12087 Set_Etype (Result, Standard_Boolean);
12088 exit;
12089 else
12090 Result :=
12091 Make_And_Then (Loc,
12092 Left_Opnd => Result,
12093 Right_Opnd => Check);
12094 end if;
12095 end;
12097 C := Element_To_Compare (Next_Entity (C));
12098 end loop;
12100 return Result;
12101 end Expand_Record_Equality;
12103 ---------------------------
12104 -- Expand_Set_Membership --
12105 ---------------------------
12107 procedure Expand_Set_Membership (N : Node_Id) is
12108 Lop : constant Node_Id := Left_Opnd (N);
12109 Alt : Node_Id;
12110 Res : Node_Id;
12112 function Make_Cond (Alt : Node_Id) return Node_Id;
12113 -- If the alternative is a subtype mark, create a simple membership
12114 -- test. Otherwise create an equality test for it.
12116 ---------------
12117 -- Make_Cond --
12118 ---------------
12120 function Make_Cond (Alt : Node_Id) return Node_Id is
12121 Cond : Node_Id;
12122 L : constant Node_Id := New_Copy (Lop);
12123 R : constant Node_Id := Relocate_Node (Alt);
12125 begin
12126 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
12127 or else Nkind (Alt) = N_Range
12128 then
12129 Cond :=
12130 Make_In (Sloc (Alt),
12131 Left_Opnd => L,
12132 Right_Opnd => R);
12133 else
12134 Cond :=
12135 Make_Op_Eq (Sloc (Alt),
12136 Left_Opnd => L,
12137 Right_Opnd => R);
12138 end if;
12140 return Cond;
12141 end Make_Cond;
12143 -- Start of processing for Expand_Set_Membership
12145 begin
12146 Remove_Side_Effects (Lop);
12148 Alt := Last (Alternatives (N));
12149 Res := Make_Cond (Alt);
12151 Prev (Alt);
12152 while Present (Alt) loop
12153 Res :=
12154 Make_Or_Else (Sloc (Alt),
12155 Left_Opnd => Make_Cond (Alt),
12156 Right_Opnd => Res);
12157 Prev (Alt);
12158 end loop;
12160 Rewrite (N, Res);
12161 Analyze_And_Resolve (N, Standard_Boolean);
12162 end Expand_Set_Membership;
12164 -----------------------------------
12165 -- Expand_Short_Circuit_Operator --
12166 -----------------------------------
12168 -- Deal with special expansion if actions are present for the right operand
12169 -- and deal with optimizing case of arguments being True or False. We also
12170 -- deal with the special case of non-standard boolean values.
12172 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
12173 Loc : constant Source_Ptr := Sloc (N);
12174 Typ : constant Entity_Id := Etype (N);
12175 Left : constant Node_Id := Left_Opnd (N);
12176 Right : constant Node_Id := Right_Opnd (N);
12177 LocR : constant Source_Ptr := Sloc (Right);
12178 Actlist : List_Id;
12180 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
12181 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
12182 -- If Left = Shortcut_Value then Right need not be evaluated
12184 function Make_Test_Expr (Opnd : Node_Id) return Node_Id;
12185 -- For Opnd a boolean expression, return a Boolean expression equivalent
12186 -- to Opnd /= Shortcut_Value.
12188 --------------------
12189 -- Make_Test_Expr --
12190 --------------------
12192 function Make_Test_Expr (Opnd : Node_Id) return Node_Id is
12193 begin
12194 if Shortcut_Value then
12195 return Make_Op_Not (Sloc (Opnd), Opnd);
12196 else
12197 return Opnd;
12198 end if;
12199 end Make_Test_Expr;
12201 -- Local variables
12203 Op_Var : Entity_Id;
12204 -- Entity for a temporary variable holding the value of the operator,
12205 -- used for expansion in the case where actions are present.
12207 -- Start of processing for Expand_Short_Circuit_Operator
12209 begin
12210 -- Deal with non-standard booleans
12212 if Is_Boolean_Type (Typ) then
12213 Adjust_Condition (Left);
12214 Adjust_Condition (Right);
12215 Set_Etype (N, Standard_Boolean);
12216 end if;
12218 -- Check for cases where left argument is known to be True or False
12220 if Compile_Time_Known_Value (Left) then
12222 -- Mark SCO for left condition as compile time known
12224 if Generate_SCO and then Comes_From_Source (Left) then
12225 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
12226 end if;
12228 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
12229 -- Any actions associated with Right will be executed unconditionally
12230 -- and can thus be inserted into the tree unconditionally.
12232 if Expr_Value_E (Left) /= Shortcut_Ent then
12233 if Present (Actions (N)) then
12234 Insert_Actions (N, Actions (N));
12235 end if;
12237 Rewrite (N, Right);
12239 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
12240 -- In this case we can forget the actions associated with Right,
12241 -- since they will never be executed.
12243 else
12244 Kill_Dead_Code (Right);
12245 Kill_Dead_Code (Actions (N));
12246 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
12247 end if;
12249 Adjust_Result_Type (N, Typ);
12250 return;
12251 end if;
12253 -- If Actions are present for the right operand, we have to do some
12254 -- special processing. We can't just let these actions filter back into
12255 -- code preceding the short circuit (which is what would have happened
12256 -- if we had not trapped them in the short-circuit form), since they
12257 -- must only be executed if the right operand of the short circuit is
12258 -- executed and not otherwise.
12260 if Present (Actions (N)) then
12261 Actlist := Actions (N);
12263 -- The old approach is to expand:
12265 -- left AND THEN right
12267 -- into
12269 -- C : Boolean := False;
12270 -- IF left THEN
12271 -- Actions;
12272 -- IF right THEN
12273 -- C := True;
12274 -- END IF;
12275 -- END IF;
12277 -- and finally rewrite the operator into a reference to C. Similarly
12278 -- for left OR ELSE right, with negated values. Note that this
12279 -- rewrite causes some difficulties for coverage analysis because
12280 -- of the introduction of the new variable C, which obscures the
12281 -- structure of the test.
12283 -- We use this "old approach" if Minimize_Expression_With_Actions
12284 -- is True.
12286 if Minimize_Expression_With_Actions then
12287 Op_Var := Make_Temporary (Loc, 'C', Related_Node => N);
12289 Insert_Action (N,
12290 Make_Object_Declaration (Loc,
12291 Defining_Identifier => Op_Var,
12292 Object_Definition =>
12293 New_Occurrence_Of (Standard_Boolean, Loc),
12294 Expression =>
12295 New_Occurrence_Of (Shortcut_Ent, Loc)));
12297 Append_To (Actlist,
12298 Make_Implicit_If_Statement (Right,
12299 Condition => Make_Test_Expr (Right),
12300 Then_Statements => New_List (
12301 Make_Assignment_Statement (LocR,
12302 Name => New_Occurrence_Of (Op_Var, LocR),
12303 Expression =>
12304 New_Occurrence_Of
12305 (Boolean_Literals (not Shortcut_Value), LocR)))));
12307 Insert_Action (N,
12308 Make_Implicit_If_Statement (Left,
12309 Condition => Make_Test_Expr (Left),
12310 Then_Statements => Actlist));
12312 Rewrite (N, New_Occurrence_Of (Op_Var, Loc));
12313 Analyze_And_Resolve (N, Standard_Boolean);
12315 -- The new approach (the default) is to use an
12316 -- Expression_With_Actions node for the right operand of the
12317 -- short-circuit form. Note that this solves the traceability
12318 -- problems for coverage analysis.
12320 else
12321 Rewrite (Right,
12322 Make_Expression_With_Actions (LocR,
12323 Expression => Relocate_Node (Right),
12324 Actions => Actlist));
12326 Set_Actions (N, No_List);
12327 Analyze_And_Resolve (Right, Standard_Boolean);
12328 end if;
12330 Adjust_Result_Type (N, Typ);
12331 return;
12332 end if;
12334 -- No actions present, check for cases of right argument True/False
12336 if Compile_Time_Known_Value (Right) then
12338 -- Mark SCO for left condition as compile time known
12340 if Generate_SCO and then Comes_From_Source (Right) then
12341 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
12342 end if;
12344 -- Change (Left and then True), (Left or else False) to Left. Note
12345 -- that we know there are no actions associated with the right
12346 -- operand, since we just checked for this case above.
12348 if Expr_Value_E (Right) /= Shortcut_Ent then
12349 Rewrite (N, Left);
12351 -- Change (Left and then False), (Left or else True) to Right,
12352 -- making sure to preserve any side effects associated with the Left
12353 -- operand.
12355 else
12356 Remove_Side_Effects (Left);
12357 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
12358 end if;
12359 end if;
12361 Adjust_Result_Type (N, Typ);
12362 end Expand_Short_Circuit_Operator;
12364 -------------------------------------
12365 -- Fixup_Universal_Fixed_Operation --
12366 -------------------------------------
12368 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
12369 Conv : constant Node_Id := Parent (N);
12371 begin
12372 -- We must have a type conversion immediately above us
12374 pragma Assert (Nkind (Conv) = N_Type_Conversion);
12376 -- Normally the type conversion gives our target type. The exception
12377 -- occurs in the case of the Round attribute, where the conversion
12378 -- will be to universal real, and our real type comes from the Round
12379 -- attribute (as well as an indication that we must round the result)
12381 if Nkind (Parent (Conv)) = N_Attribute_Reference
12382 and then Attribute_Name (Parent (Conv)) = Name_Round
12383 then
12384 Set_Etype (N, Etype (Parent (Conv)));
12385 Set_Rounded_Result (N);
12387 -- Normal case where type comes from conversion above us
12389 else
12390 Set_Etype (N, Etype (Conv));
12391 end if;
12392 end Fixup_Universal_Fixed_Operation;
12394 ---------------------------------
12395 -- Has_Inferable_Discriminants --
12396 ---------------------------------
12398 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
12400 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
12401 -- Determines whether the left-most prefix of a selected component is a
12402 -- formal parameter in a subprogram. Assumes N is a selected component.
12404 --------------------------------
12405 -- Prefix_Is_Formal_Parameter --
12406 --------------------------------
12408 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
12409 Sel_Comp : Node_Id;
12411 begin
12412 -- Move to the left-most prefix by climbing up the tree
12414 Sel_Comp := N;
12415 while Present (Parent (Sel_Comp))
12416 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
12417 loop
12418 Sel_Comp := Parent (Sel_Comp);
12419 end loop;
12421 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
12422 end Prefix_Is_Formal_Parameter;
12424 -- Start of processing for Has_Inferable_Discriminants
12426 begin
12427 -- For selected components, the subtype of the selector must be a
12428 -- constrained Unchecked_Union. If the component is subject to a
12429 -- per-object constraint, then the enclosing object must have inferable
12430 -- discriminants.
12432 if Nkind (N) = N_Selected_Component then
12433 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
12435 -- A small hack. If we have a per-object constrained selected
12436 -- component of a formal parameter, return True since we do not
12437 -- know the actual parameter association yet.
12439 if Prefix_Is_Formal_Parameter (N) then
12440 return True;
12442 -- Otherwise, check the enclosing object and the selector
12444 else
12445 return Has_Inferable_Discriminants (Prefix (N))
12446 and then Has_Inferable_Discriminants (Selector_Name (N));
12447 end if;
12449 -- The call to Has_Inferable_Discriminants will determine whether
12450 -- the selector has a constrained Unchecked_Union nominal type.
12452 else
12453 return Has_Inferable_Discriminants (Selector_Name (N));
12454 end if;
12456 -- A qualified expression has inferable discriminants if its subtype
12457 -- mark is a constrained Unchecked_Union subtype.
12459 elsif Nkind (N) = N_Qualified_Expression then
12460 return Is_Unchecked_Union (Etype (Subtype_Mark (N)))
12461 and then Is_Constrained (Etype (Subtype_Mark (N)));
12463 -- For all other names, it is sufficient to have a constrained
12464 -- Unchecked_Union nominal subtype.
12466 else
12467 return Is_Unchecked_Union (Base_Type (Etype (N)))
12468 and then Is_Constrained (Etype (N));
12469 end if;
12470 end Has_Inferable_Discriminants;
12472 -------------------------------
12473 -- Insert_Dereference_Action --
12474 -------------------------------
12476 procedure Insert_Dereference_Action (N : Node_Id) is
12477 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
12478 -- Return true if type of P is derived from Checked_Pool;
12480 -----------------------------
12481 -- Is_Checked_Storage_Pool --
12482 -----------------------------
12484 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
12485 T : Entity_Id;
12487 begin
12488 if No (P) then
12489 return False;
12490 end if;
12492 T := Etype (P);
12493 while T /= Etype (T) loop
12494 if Is_RTE (T, RE_Checked_Pool) then
12495 return True;
12496 else
12497 T := Etype (T);
12498 end if;
12499 end loop;
12501 return False;
12502 end Is_Checked_Storage_Pool;
12504 -- Local variables
12506 Context : constant Node_Id := Parent (N);
12507 Ptr_Typ : constant Entity_Id := Etype (N);
12508 Desig_Typ : constant Entity_Id :=
12509 Available_View (Designated_Type (Ptr_Typ));
12510 Loc : constant Source_Ptr := Sloc (N);
12511 Pool : constant Entity_Id := Associated_Storage_Pool (Ptr_Typ);
12513 Addr : Entity_Id;
12514 Alig : Entity_Id;
12515 Deref : Node_Id;
12516 Size : Entity_Id;
12517 Size_Bits : Node_Id;
12518 Stmt : Node_Id;
12520 -- Start of processing for Insert_Dereference_Action
12522 begin
12523 pragma Assert (Nkind (Context) = N_Explicit_Dereference);
12525 -- Do not re-expand a dereference which has already been processed by
12526 -- this routine.
12528 if Has_Dereference_Action (Context) then
12529 return;
12531 -- Do not perform this type of expansion for internally-generated
12532 -- dereferences.
12534 elsif not Comes_From_Source (Original_Node (Context)) then
12535 return;
12537 -- A dereference action is only applicable to objects which have been
12538 -- allocated on a checked pool.
12540 elsif not Is_Checked_Storage_Pool (Pool) then
12541 return;
12542 end if;
12544 -- Extract the address of the dereferenced object. Generate:
12546 -- Addr : System.Address := <N>'Pool_Address;
12548 Addr := Make_Temporary (Loc, 'P');
12550 Insert_Action (N,
12551 Make_Object_Declaration (Loc,
12552 Defining_Identifier => Addr,
12553 Object_Definition =>
12554 New_Occurrence_Of (RTE (RE_Address), Loc),
12555 Expression =>
12556 Make_Attribute_Reference (Loc,
12557 Prefix => Duplicate_Subexpr_Move_Checks (N),
12558 Attribute_Name => Name_Pool_Address)));
12560 -- Calculate the size of the dereferenced object. Generate:
12562 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
12564 Deref :=
12565 Make_Explicit_Dereference (Loc,
12566 Prefix => Duplicate_Subexpr_Move_Checks (N));
12567 Set_Has_Dereference_Action (Deref);
12569 Size_Bits :=
12570 Make_Attribute_Reference (Loc,
12571 Prefix => Deref,
12572 Attribute_Name => Name_Size);
12574 -- Special case of an unconstrained array: need to add descriptor size
12576 if Is_Array_Type (Desig_Typ)
12577 and then not Is_Constrained (First_Subtype (Desig_Typ))
12578 then
12579 Size_Bits :=
12580 Make_Op_Add (Loc,
12581 Left_Opnd =>
12582 Make_Attribute_Reference (Loc,
12583 Prefix =>
12584 New_Occurrence_Of (First_Subtype (Desig_Typ), Loc),
12585 Attribute_Name => Name_Descriptor_Size),
12586 Right_Opnd => Size_Bits);
12587 end if;
12589 Size := Make_Temporary (Loc, 'S');
12590 Insert_Action (N,
12591 Make_Object_Declaration (Loc,
12592 Defining_Identifier => Size,
12593 Object_Definition =>
12594 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
12595 Expression =>
12596 Make_Op_Divide (Loc,
12597 Left_Opnd => Size_Bits,
12598 Right_Opnd => Make_Integer_Literal (Loc, System_Storage_Unit))));
12600 -- Calculate the alignment of the dereferenced object. Generate:
12601 -- Alig : constant Storage_Count := <N>.all'Alignment;
12603 Deref :=
12604 Make_Explicit_Dereference (Loc,
12605 Prefix => Duplicate_Subexpr_Move_Checks (N));
12606 Set_Has_Dereference_Action (Deref);
12608 Alig := Make_Temporary (Loc, 'A');
12609 Insert_Action (N,
12610 Make_Object_Declaration (Loc,
12611 Defining_Identifier => Alig,
12612 Object_Definition =>
12613 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
12614 Expression =>
12615 Make_Attribute_Reference (Loc,
12616 Prefix => Deref,
12617 Attribute_Name => Name_Alignment)));
12619 -- A dereference of a controlled object requires special processing. The
12620 -- finalization machinery requests additional space from the underlying
12621 -- pool to allocate and hide two pointers. As a result, a checked pool
12622 -- may mark the wrong memory as valid. Since checked pools do not have
12623 -- knowledge of hidden pointers, we have to bring the two pointers back
12624 -- in view in order to restore the original state of the object.
12626 -- The address manipulation is not performed for access types that are
12627 -- subject to pragma No_Heap_Finalization because the two pointers do
12628 -- not exist in the first place.
12630 if No_Heap_Finalization (Ptr_Typ) then
12631 null;
12633 elsif Needs_Finalization (Desig_Typ) then
12635 -- Adjust the address and size of the dereferenced object. Generate:
12636 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
12638 Stmt :=
12639 Make_Procedure_Call_Statement (Loc,
12640 Name =>
12641 New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc),
12642 Parameter_Associations => New_List (
12643 New_Occurrence_Of (Addr, Loc),
12644 New_Occurrence_Of (Size, Loc),
12645 New_Occurrence_Of (Alig, Loc)));
12647 -- Class-wide types complicate things because we cannot determine
12648 -- statically whether the actual object is truly controlled. We must
12649 -- generate a runtime check to detect this property. Generate:
12651 -- if Needs_Finalization (<N>.all'Tag) then
12652 -- <Stmt>;
12653 -- end if;
12655 if Is_Class_Wide_Type (Desig_Typ) then
12656 Deref :=
12657 Make_Explicit_Dereference (Loc,
12658 Prefix => Duplicate_Subexpr_Move_Checks (N));
12659 Set_Has_Dereference_Action (Deref);
12661 Stmt :=
12662 Make_Implicit_If_Statement (N,
12663 Condition =>
12664 Make_Function_Call (Loc,
12665 Name =>
12666 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
12667 Parameter_Associations => New_List (
12668 Make_Attribute_Reference (Loc,
12669 Prefix => Deref,
12670 Attribute_Name => Name_Tag))),
12671 Then_Statements => New_List (Stmt));
12672 end if;
12674 Insert_Action (N, Stmt);
12675 end if;
12677 -- Generate:
12678 -- Dereference (Pool, Addr, Size, Alig);
12680 Insert_Action (N,
12681 Make_Procedure_Call_Statement (Loc,
12682 Name =>
12683 New_Occurrence_Of
12684 (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
12685 Parameter_Associations => New_List (
12686 New_Occurrence_Of (Pool, Loc),
12687 New_Occurrence_Of (Addr, Loc),
12688 New_Occurrence_Of (Size, Loc),
12689 New_Occurrence_Of (Alig, Loc))));
12691 -- Mark the explicit dereference as processed to avoid potential
12692 -- infinite expansion.
12694 Set_Has_Dereference_Action (Context);
12696 exception
12697 when RE_Not_Available =>
12698 return;
12699 end Insert_Dereference_Action;
12701 --------------------------------
12702 -- Integer_Promotion_Possible --
12703 --------------------------------
12705 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
12706 Operand : constant Node_Id := Expression (N);
12707 Operand_Type : constant Entity_Id := Etype (Operand);
12708 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
12710 begin
12711 pragma Assert (Nkind (N) = N_Type_Conversion);
12713 return
12715 -- We only do the transformation for source constructs. We assume
12716 -- that the expander knows what it is doing when it generates code.
12718 Comes_From_Source (N)
12720 -- If the operand type is Short_Integer or Short_Short_Integer,
12721 -- then we will promote to Integer, which is available on all
12722 -- targets, and is sufficient to ensure no intermediate overflow.
12723 -- Furthermore it is likely to be as efficient or more efficient
12724 -- than using the smaller type for the computation so we do this
12725 -- unconditionally.
12727 and then
12728 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
12729 or else
12730 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
12732 -- Test for interesting operation, which includes addition,
12733 -- division, exponentiation, multiplication, subtraction, absolute
12734 -- value and unary negation. Unary "+" is omitted since it is a
12735 -- no-op and thus can't overflow.
12737 and then Nkind_In (Operand, N_Op_Abs,
12738 N_Op_Add,
12739 N_Op_Divide,
12740 N_Op_Expon,
12741 N_Op_Minus,
12742 N_Op_Multiply,
12743 N_Op_Subtract);
12744 end Integer_Promotion_Possible;
12746 ------------------------------
12747 -- Make_Array_Comparison_Op --
12748 ------------------------------
12750 -- This is a hand-coded expansion of the following generic function:
12752 -- generic
12753 -- type elem is (<>);
12754 -- type index is (<>);
12755 -- type a is array (index range <>) of elem;
12757 -- function Gnnn (X : a; Y: a) return boolean is
12758 -- J : index := Y'first;
12760 -- begin
12761 -- if X'length = 0 then
12762 -- return false;
12764 -- elsif Y'length = 0 then
12765 -- return true;
12767 -- else
12768 -- for I in X'range loop
12769 -- if X (I) = Y (J) then
12770 -- if J = Y'last then
12771 -- exit;
12772 -- else
12773 -- J := index'succ (J);
12774 -- end if;
12776 -- else
12777 -- return X (I) > Y (J);
12778 -- end if;
12779 -- end loop;
12781 -- return X'length > Y'length;
12782 -- end if;
12783 -- end Gnnn;
12785 -- Note that since we are essentially doing this expansion by hand, we
12786 -- do not need to generate an actual or formal generic part, just the
12787 -- instantiated function itself.
12789 -- Perhaps we could have the actual generic available in the run-time,
12790 -- obtained by rtsfind, and actually expand a real instantiation ???
12792 function Make_Array_Comparison_Op
12793 (Typ : Entity_Id;
12794 Nod : Node_Id) return Node_Id
12796 Loc : constant Source_Ptr := Sloc (Nod);
12798 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
12799 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
12800 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
12801 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
12803 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
12805 Loop_Statement : Node_Id;
12806 Loop_Body : Node_Id;
12807 If_Stat : Node_Id;
12808 Inner_If : Node_Id;
12809 Final_Expr : Node_Id;
12810 Func_Body : Node_Id;
12811 Func_Name : Entity_Id;
12812 Formals : List_Id;
12813 Length1 : Node_Id;
12814 Length2 : Node_Id;
12816 begin
12817 -- if J = Y'last then
12818 -- exit;
12819 -- else
12820 -- J := index'succ (J);
12821 -- end if;
12823 Inner_If :=
12824 Make_Implicit_If_Statement (Nod,
12825 Condition =>
12826 Make_Op_Eq (Loc,
12827 Left_Opnd => New_Occurrence_Of (J, Loc),
12828 Right_Opnd =>
12829 Make_Attribute_Reference (Loc,
12830 Prefix => New_Occurrence_Of (Y, Loc),
12831 Attribute_Name => Name_Last)),
12833 Then_Statements => New_List (
12834 Make_Exit_Statement (Loc)),
12836 Else_Statements =>
12837 New_List (
12838 Make_Assignment_Statement (Loc,
12839 Name => New_Occurrence_Of (J, Loc),
12840 Expression =>
12841 Make_Attribute_Reference (Loc,
12842 Prefix => New_Occurrence_Of (Index, Loc),
12843 Attribute_Name => Name_Succ,
12844 Expressions => New_List (New_Occurrence_Of (J, Loc))))));
12846 -- if X (I) = Y (J) then
12847 -- if ... end if;
12848 -- else
12849 -- return X (I) > Y (J);
12850 -- end if;
12852 Loop_Body :=
12853 Make_Implicit_If_Statement (Nod,
12854 Condition =>
12855 Make_Op_Eq (Loc,
12856 Left_Opnd =>
12857 Make_Indexed_Component (Loc,
12858 Prefix => New_Occurrence_Of (X, Loc),
12859 Expressions => New_List (New_Occurrence_Of (I, Loc))),
12861 Right_Opnd =>
12862 Make_Indexed_Component (Loc,
12863 Prefix => New_Occurrence_Of (Y, Loc),
12864 Expressions => New_List (New_Occurrence_Of (J, Loc)))),
12866 Then_Statements => New_List (Inner_If),
12868 Else_Statements => New_List (
12869 Make_Simple_Return_Statement (Loc,
12870 Expression =>
12871 Make_Op_Gt (Loc,
12872 Left_Opnd =>
12873 Make_Indexed_Component (Loc,
12874 Prefix => New_Occurrence_Of (X, Loc),
12875 Expressions => New_List (New_Occurrence_Of (I, Loc))),
12877 Right_Opnd =>
12878 Make_Indexed_Component (Loc,
12879 Prefix => New_Occurrence_Of (Y, Loc),
12880 Expressions => New_List (
12881 New_Occurrence_Of (J, Loc)))))));
12883 -- for I in X'range loop
12884 -- if ... end if;
12885 -- end loop;
12887 Loop_Statement :=
12888 Make_Implicit_Loop_Statement (Nod,
12889 Identifier => Empty,
12891 Iteration_Scheme =>
12892 Make_Iteration_Scheme (Loc,
12893 Loop_Parameter_Specification =>
12894 Make_Loop_Parameter_Specification (Loc,
12895 Defining_Identifier => I,
12896 Discrete_Subtype_Definition =>
12897 Make_Attribute_Reference (Loc,
12898 Prefix => New_Occurrence_Of (X, Loc),
12899 Attribute_Name => Name_Range))),
12901 Statements => New_List (Loop_Body));
12903 -- if X'length = 0 then
12904 -- return false;
12905 -- elsif Y'length = 0 then
12906 -- return true;
12907 -- else
12908 -- for ... loop ... end loop;
12909 -- return X'length > Y'length;
12910 -- end if;
12912 Length1 :=
12913 Make_Attribute_Reference (Loc,
12914 Prefix => New_Occurrence_Of (X, Loc),
12915 Attribute_Name => Name_Length);
12917 Length2 :=
12918 Make_Attribute_Reference (Loc,
12919 Prefix => New_Occurrence_Of (Y, Loc),
12920 Attribute_Name => Name_Length);
12922 Final_Expr :=
12923 Make_Op_Gt (Loc,
12924 Left_Opnd => Length1,
12925 Right_Opnd => Length2);
12927 If_Stat :=
12928 Make_Implicit_If_Statement (Nod,
12929 Condition =>
12930 Make_Op_Eq (Loc,
12931 Left_Opnd =>
12932 Make_Attribute_Reference (Loc,
12933 Prefix => New_Occurrence_Of (X, Loc),
12934 Attribute_Name => Name_Length),
12935 Right_Opnd =>
12936 Make_Integer_Literal (Loc, 0)),
12938 Then_Statements =>
12939 New_List (
12940 Make_Simple_Return_Statement (Loc,
12941 Expression => New_Occurrence_Of (Standard_False, Loc))),
12943 Elsif_Parts => New_List (
12944 Make_Elsif_Part (Loc,
12945 Condition =>
12946 Make_Op_Eq (Loc,
12947 Left_Opnd =>
12948 Make_Attribute_Reference (Loc,
12949 Prefix => New_Occurrence_Of (Y, Loc),
12950 Attribute_Name => Name_Length),
12951 Right_Opnd =>
12952 Make_Integer_Literal (Loc, 0)),
12954 Then_Statements =>
12955 New_List (
12956 Make_Simple_Return_Statement (Loc,
12957 Expression => New_Occurrence_Of (Standard_True, Loc))))),
12959 Else_Statements => New_List (
12960 Loop_Statement,
12961 Make_Simple_Return_Statement (Loc,
12962 Expression => Final_Expr)));
12964 -- (X : a; Y: a)
12966 Formals := New_List (
12967 Make_Parameter_Specification (Loc,
12968 Defining_Identifier => X,
12969 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
12971 Make_Parameter_Specification (Loc,
12972 Defining_Identifier => Y,
12973 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
12975 -- function Gnnn (...) return boolean is
12976 -- J : index := Y'first;
12977 -- begin
12978 -- if ... end if;
12979 -- end Gnnn;
12981 Func_Name := Make_Temporary (Loc, 'G');
12983 Func_Body :=
12984 Make_Subprogram_Body (Loc,
12985 Specification =>
12986 Make_Function_Specification (Loc,
12987 Defining_Unit_Name => Func_Name,
12988 Parameter_Specifications => Formals,
12989 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
12991 Declarations => New_List (
12992 Make_Object_Declaration (Loc,
12993 Defining_Identifier => J,
12994 Object_Definition => New_Occurrence_Of (Index, Loc),
12995 Expression =>
12996 Make_Attribute_Reference (Loc,
12997 Prefix => New_Occurrence_Of (Y, Loc),
12998 Attribute_Name => Name_First))),
13000 Handled_Statement_Sequence =>
13001 Make_Handled_Sequence_Of_Statements (Loc,
13002 Statements => New_List (If_Stat)));
13004 return Func_Body;
13005 end Make_Array_Comparison_Op;
13007 ---------------------------
13008 -- Make_Boolean_Array_Op --
13009 ---------------------------
13011 -- For logical operations on boolean arrays, expand in line the following,
13012 -- replacing 'and' with 'or' or 'xor' where needed:
13014 -- function Annn (A : typ; B: typ) return typ is
13015 -- C : typ;
13016 -- begin
13017 -- for J in A'range loop
13018 -- C (J) := A (J) op B (J);
13019 -- end loop;
13020 -- return C;
13021 -- end Annn;
13023 -- Here typ is the boolean array type
13025 function Make_Boolean_Array_Op
13026 (Typ : Entity_Id;
13027 N : Node_Id) return Node_Id
13029 Loc : constant Source_Ptr := Sloc (N);
13031 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
13032 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
13033 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
13034 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
13036 A_J : Node_Id;
13037 B_J : Node_Id;
13038 C_J : Node_Id;
13039 Op : Node_Id;
13041 Formals : List_Id;
13042 Func_Name : Entity_Id;
13043 Func_Body : Node_Id;
13044 Loop_Statement : Node_Id;
13046 begin
13047 A_J :=
13048 Make_Indexed_Component (Loc,
13049 Prefix => New_Occurrence_Of (A, Loc),
13050 Expressions => New_List (New_Occurrence_Of (J, Loc)));
13052 B_J :=
13053 Make_Indexed_Component (Loc,
13054 Prefix => New_Occurrence_Of (B, Loc),
13055 Expressions => New_List (New_Occurrence_Of (J, Loc)));
13057 C_J :=
13058 Make_Indexed_Component (Loc,
13059 Prefix => New_Occurrence_Of (C, Loc),
13060 Expressions => New_List (New_Occurrence_Of (J, Loc)));
13062 if Nkind (N) = N_Op_And then
13063 Op :=
13064 Make_Op_And (Loc,
13065 Left_Opnd => A_J,
13066 Right_Opnd => B_J);
13068 elsif Nkind (N) = N_Op_Or then
13069 Op :=
13070 Make_Op_Or (Loc,
13071 Left_Opnd => A_J,
13072 Right_Opnd => B_J);
13074 else
13075 Op :=
13076 Make_Op_Xor (Loc,
13077 Left_Opnd => A_J,
13078 Right_Opnd => B_J);
13079 end if;
13081 Loop_Statement :=
13082 Make_Implicit_Loop_Statement (N,
13083 Identifier => Empty,
13085 Iteration_Scheme =>
13086 Make_Iteration_Scheme (Loc,
13087 Loop_Parameter_Specification =>
13088 Make_Loop_Parameter_Specification (Loc,
13089 Defining_Identifier => J,
13090 Discrete_Subtype_Definition =>
13091 Make_Attribute_Reference (Loc,
13092 Prefix => New_Occurrence_Of (A, Loc),
13093 Attribute_Name => Name_Range))),
13095 Statements => New_List (
13096 Make_Assignment_Statement (Loc,
13097 Name => C_J,
13098 Expression => Op)));
13100 Formals := New_List (
13101 Make_Parameter_Specification (Loc,
13102 Defining_Identifier => A,
13103 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
13105 Make_Parameter_Specification (Loc,
13106 Defining_Identifier => B,
13107 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
13109 Func_Name := Make_Temporary (Loc, 'A');
13110 Set_Is_Inlined (Func_Name);
13112 Func_Body :=
13113 Make_Subprogram_Body (Loc,
13114 Specification =>
13115 Make_Function_Specification (Loc,
13116 Defining_Unit_Name => Func_Name,
13117 Parameter_Specifications => Formals,
13118 Result_Definition => New_Occurrence_Of (Typ, Loc)),
13120 Declarations => New_List (
13121 Make_Object_Declaration (Loc,
13122 Defining_Identifier => C,
13123 Object_Definition => New_Occurrence_Of (Typ, Loc))),
13125 Handled_Statement_Sequence =>
13126 Make_Handled_Sequence_Of_Statements (Loc,
13127 Statements => New_List (
13128 Loop_Statement,
13129 Make_Simple_Return_Statement (Loc,
13130 Expression => New_Occurrence_Of (C, Loc)))));
13132 return Func_Body;
13133 end Make_Boolean_Array_Op;
13135 -----------------------------------------
13136 -- Minimized_Eliminated_Overflow_Check --
13137 -----------------------------------------
13139 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is
13140 begin
13141 return
13142 Is_Signed_Integer_Type (Etype (N))
13143 and then Overflow_Check_Mode in Minimized_Or_Eliminated;
13144 end Minimized_Eliminated_Overflow_Check;
13146 --------------------------------
13147 -- Optimize_Length_Comparison --
13148 --------------------------------
13150 procedure Optimize_Length_Comparison (N : Node_Id) is
13151 Loc : constant Source_Ptr := Sloc (N);
13152 Typ : constant Entity_Id := Etype (N);
13153 Result : Node_Id;
13155 Left : Node_Id;
13156 Right : Node_Id;
13157 -- First and Last attribute reference nodes, which end up as left and
13158 -- right operands of the optimized result.
13160 Is_Zero : Boolean;
13161 -- True for comparison operand of zero
13163 Comp : Node_Id;
13164 -- Comparison operand, set only if Is_Zero is false
13166 Ent : Entity_Id := Empty;
13167 -- Entity whose length is being compared
13169 Index : Node_Id := Empty;
13170 -- Integer_Literal node for length attribute expression, or Empty
13171 -- if there is no such expression present.
13173 Ityp : Entity_Id;
13174 -- Type of array index to which 'Length is applied
13176 Op : Node_Kind := Nkind (N);
13177 -- Kind of comparison operator, gets flipped if operands backwards
13179 function Is_Optimizable (N : Node_Id) return Boolean;
13180 -- Tests N to see if it is an optimizable comparison value (defined as
13181 -- constant zero or one, or something else where the value is known to
13182 -- be positive and in the range of 32-bits, and where the corresponding
13183 -- Length value is also known to be 32-bits. If result is true, sets
13184 -- Is_Zero, Ityp, and Comp accordingly.
13186 function Is_Entity_Length (N : Node_Id) return Boolean;
13187 -- Tests if N is a length attribute applied to a simple entity. If so,
13188 -- returns True, and sets Ent to the entity, and Index to the integer
13189 -- literal provided as an attribute expression, or to Empty if none.
13190 -- Also returns True if the expression is a generated type conversion
13191 -- whose expression is of the desired form. This latter case arises
13192 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
13193 -- to check for being in range, which is not needed in this context.
13194 -- Returns False if neither condition holds.
13196 function Prepare_64 (N : Node_Id) return Node_Id;
13197 -- Given a discrete expression, returns a Long_Long_Integer typed
13198 -- expression representing the underlying value of the expression.
13199 -- This is done with an unchecked conversion to the result type. We
13200 -- use unchecked conversion to handle the enumeration type case.
13202 ----------------------
13203 -- Is_Entity_Length --
13204 ----------------------
13206 function Is_Entity_Length (N : Node_Id) return Boolean is
13207 begin
13208 if Nkind (N) = N_Attribute_Reference
13209 and then Attribute_Name (N) = Name_Length
13210 and then Is_Entity_Name (Prefix (N))
13211 then
13212 Ent := Entity (Prefix (N));
13214 if Present (Expressions (N)) then
13215 Index := First (Expressions (N));
13216 else
13217 Index := Empty;
13218 end if;
13220 return True;
13222 elsif Nkind (N) = N_Type_Conversion
13223 and then not Comes_From_Source (N)
13224 then
13225 return Is_Entity_Length (Expression (N));
13227 else
13228 return False;
13229 end if;
13230 end Is_Entity_Length;
13232 --------------------
13233 -- Is_Optimizable --
13234 --------------------
13236 function Is_Optimizable (N : Node_Id) return Boolean is
13237 Val : Uint;
13238 OK : Boolean;
13239 Lo : Uint;
13240 Hi : Uint;
13241 Indx : Node_Id;
13243 begin
13244 if Compile_Time_Known_Value (N) then
13245 Val := Expr_Value (N);
13247 if Val = Uint_0 then
13248 Is_Zero := True;
13249 Comp := Empty;
13250 return True;
13252 elsif Val = Uint_1 then
13253 Is_Zero := False;
13254 Comp := Empty;
13255 return True;
13256 end if;
13257 end if;
13259 -- Here we have to make sure of being within 32-bits
13261 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
13263 if not OK
13264 or else Lo < Uint_1
13265 or else Hi > UI_From_Int (Int'Last)
13266 then
13267 return False;
13268 end if;
13270 -- Comparison value was within range, so now we must check the index
13271 -- value to make sure it is also within 32-bits.
13273 Indx := First_Index (Etype (Ent));
13275 if Present (Index) then
13276 for J in 2 .. UI_To_Int (Intval (Index)) loop
13277 Next_Index (Indx);
13278 end loop;
13279 end if;
13281 Ityp := Etype (Indx);
13283 if Esize (Ityp) > 32 then
13284 return False;
13285 end if;
13287 Is_Zero := False;
13288 Comp := N;
13289 return True;
13290 end Is_Optimizable;
13292 ----------------
13293 -- Prepare_64 --
13294 ----------------
13296 function Prepare_64 (N : Node_Id) return Node_Id is
13297 begin
13298 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
13299 end Prepare_64;
13301 -- Start of processing for Optimize_Length_Comparison
13303 begin
13304 -- Nothing to do if not a comparison
13306 if Op not in N_Op_Compare then
13307 return;
13308 end if;
13310 -- Nothing to do if special -gnatd.P debug flag set.
13312 if Debug_Flag_Dot_PP then
13313 return;
13314 end if;
13316 -- Ent'Length op 0/1
13318 if Is_Entity_Length (Left_Opnd (N))
13319 and then Is_Optimizable (Right_Opnd (N))
13320 then
13321 null;
13323 -- 0/1 op Ent'Length
13325 elsif Is_Entity_Length (Right_Opnd (N))
13326 and then Is_Optimizable (Left_Opnd (N))
13327 then
13328 -- Flip comparison to opposite sense
13330 case Op is
13331 when N_Op_Lt => Op := N_Op_Gt;
13332 when N_Op_Le => Op := N_Op_Ge;
13333 when N_Op_Gt => Op := N_Op_Lt;
13334 when N_Op_Ge => Op := N_Op_Le;
13335 when others => null;
13336 end case;
13338 -- Else optimization not possible
13340 else
13341 return;
13342 end if;
13344 -- Fall through if we will do the optimization
13346 -- Cases to handle:
13348 -- X'Length = 0 => X'First > X'Last
13349 -- X'Length = 1 => X'First = X'Last
13350 -- X'Length = n => X'First + (n - 1) = X'Last
13352 -- X'Length /= 0 => X'First <= X'Last
13353 -- X'Length /= 1 => X'First /= X'Last
13354 -- X'Length /= n => X'First + (n - 1) /= X'Last
13356 -- X'Length >= 0 => always true, warn
13357 -- X'Length >= 1 => X'First <= X'Last
13358 -- X'Length >= n => X'First + (n - 1) <= X'Last
13360 -- X'Length > 0 => X'First <= X'Last
13361 -- X'Length > 1 => X'First < X'Last
13362 -- X'Length > n => X'First + (n - 1) < X'Last
13364 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
13365 -- X'Length <= 1 => X'First >= X'Last
13366 -- X'Length <= n => X'First + (n - 1) >= X'Last
13368 -- X'Length < 0 => always false (warn)
13369 -- X'Length < 1 => X'First > X'Last
13370 -- X'Length < n => X'First + (n - 1) > X'Last
13372 -- Note: for the cases of n (not constant 0,1), we require that the
13373 -- corresponding index type be integer or shorter (i.e. not 64-bit),
13374 -- and the same for the comparison value. Then we do the comparison
13375 -- using 64-bit arithmetic (actually long long integer), so that we
13376 -- cannot have overflow intefering with the result.
13378 -- First deal with warning cases
13380 if Is_Zero then
13381 case Op is
13383 -- X'Length >= 0
13385 when N_Op_Ge =>
13386 Rewrite (N,
13387 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
13388 Analyze_And_Resolve (N, Typ);
13389 Warn_On_Known_Condition (N);
13390 return;
13392 -- X'Length < 0
13394 when N_Op_Lt =>
13395 Rewrite (N,
13396 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
13397 Analyze_And_Resolve (N, Typ);
13398 Warn_On_Known_Condition (N);
13399 return;
13401 when N_Op_Le =>
13402 if Constant_Condition_Warnings
13403 and then Comes_From_Source (Original_Node (N))
13404 then
13405 Error_Msg_N ("could replace by ""'=""?c?", N);
13406 end if;
13408 Op := N_Op_Eq;
13410 when others =>
13411 null;
13412 end case;
13413 end if;
13415 -- Build the First reference we will use
13417 Left :=
13418 Make_Attribute_Reference (Loc,
13419 Prefix => New_Occurrence_Of (Ent, Loc),
13420 Attribute_Name => Name_First);
13422 if Present (Index) then
13423 Set_Expressions (Left, New_List (New_Copy (Index)));
13424 end if;
13426 -- If general value case, then do the addition of (n - 1), and
13427 -- also add the needed conversions to type Long_Long_Integer.
13429 if Present (Comp) then
13430 Left :=
13431 Make_Op_Add (Loc,
13432 Left_Opnd => Prepare_64 (Left),
13433 Right_Opnd =>
13434 Make_Op_Subtract (Loc,
13435 Left_Opnd => Prepare_64 (Comp),
13436 Right_Opnd => Make_Integer_Literal (Loc, 1)));
13437 end if;
13439 -- Build the Last reference we will use
13441 Right :=
13442 Make_Attribute_Reference (Loc,
13443 Prefix => New_Occurrence_Of (Ent, Loc),
13444 Attribute_Name => Name_Last);
13446 if Present (Index) then
13447 Set_Expressions (Right, New_List (New_Copy (Index)));
13448 end if;
13450 -- If general operand, convert Last reference to Long_Long_Integer
13452 if Present (Comp) then
13453 Right := Prepare_64 (Right);
13454 end if;
13456 -- Check for cases to optimize
13458 -- X'Length = 0 => X'First > X'Last
13459 -- X'Length < 1 => X'First > X'Last
13460 -- X'Length < n => X'First + (n - 1) > X'Last
13462 if (Is_Zero and then Op = N_Op_Eq)
13463 or else (not Is_Zero and then Op = N_Op_Lt)
13464 then
13465 Result :=
13466 Make_Op_Gt (Loc,
13467 Left_Opnd => Left,
13468 Right_Opnd => Right);
13470 -- X'Length = 1 => X'First = X'Last
13471 -- X'Length = n => X'First + (n - 1) = X'Last
13473 elsif not Is_Zero and then Op = N_Op_Eq then
13474 Result :=
13475 Make_Op_Eq (Loc,
13476 Left_Opnd => Left,
13477 Right_Opnd => Right);
13479 -- X'Length /= 0 => X'First <= X'Last
13480 -- X'Length > 0 => X'First <= X'Last
13482 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
13483 Result :=
13484 Make_Op_Le (Loc,
13485 Left_Opnd => Left,
13486 Right_Opnd => Right);
13488 -- X'Length /= 1 => X'First /= X'Last
13489 -- X'Length /= n => X'First + (n - 1) /= X'Last
13491 elsif not Is_Zero and then Op = N_Op_Ne then
13492 Result :=
13493 Make_Op_Ne (Loc,
13494 Left_Opnd => Left,
13495 Right_Opnd => Right);
13497 -- X'Length >= 1 => X'First <= X'Last
13498 -- X'Length >= n => X'First + (n - 1) <= X'Last
13500 elsif not Is_Zero and then Op = N_Op_Ge then
13501 Result :=
13502 Make_Op_Le (Loc,
13503 Left_Opnd => Left,
13504 Right_Opnd => Right);
13506 -- X'Length > 1 => X'First < X'Last
13507 -- X'Length > n => X'First + (n = 1) < X'Last
13509 elsif not Is_Zero and then Op = N_Op_Gt then
13510 Result :=
13511 Make_Op_Lt (Loc,
13512 Left_Opnd => Left,
13513 Right_Opnd => Right);
13515 -- X'Length <= 1 => X'First >= X'Last
13516 -- X'Length <= n => X'First + (n - 1) >= X'Last
13518 elsif not Is_Zero and then Op = N_Op_Le then
13519 Result :=
13520 Make_Op_Ge (Loc,
13521 Left_Opnd => Left,
13522 Right_Opnd => Right);
13524 -- Should not happen at this stage
13526 else
13527 raise Program_Error;
13528 end if;
13530 -- Rewrite and finish up
13532 Rewrite (N, Result);
13533 Analyze_And_Resolve (N, Typ);
13534 return;
13535 end Optimize_Length_Comparison;
13537 --------------------------------
13538 -- Process_If_Case_Statements --
13539 --------------------------------
13541 procedure Process_If_Case_Statements (N : Node_Id; Stmts : List_Id) is
13542 Decl : Node_Id;
13544 begin
13545 Decl := First (Stmts);
13546 while Present (Decl) loop
13547 if Nkind (Decl) = N_Object_Declaration
13548 and then Is_Finalizable_Transient (Decl, N)
13549 then
13550 Process_Transient_In_Expression (Decl, N, Stmts);
13551 end if;
13553 Next (Decl);
13554 end loop;
13555 end Process_If_Case_Statements;
13557 -------------------------------------
13558 -- Process_Transient_In_Expression --
13559 -------------------------------------
13561 procedure Process_Transient_In_Expression
13562 (Obj_Decl : Node_Id;
13563 Expr : Node_Id;
13564 Stmts : List_Id)
13566 Loc : constant Source_Ptr := Sloc (Obj_Decl);
13567 Obj_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
13569 Hook_Context : constant Node_Id := Find_Hook_Context (Expr);
13570 -- The node on which to insert the hook as an action. This is usually
13571 -- the innermost enclosing non-transient construct.
13573 Fin_Call : Node_Id;
13574 Hook_Assign : Node_Id;
13575 Hook_Clear : Node_Id;
13576 Hook_Decl : Node_Id;
13577 Hook_Insert : Node_Id;
13578 Ptr_Decl : Node_Id;
13580 Fin_Context : Node_Id;
13581 -- The node after which to insert the finalization actions of the
13582 -- transient object.
13584 begin
13585 pragma Assert (Nkind_In (Expr, N_Case_Expression,
13586 N_Expression_With_Actions,
13587 N_If_Expression));
13589 -- When the context is a Boolean evaluation, all three nodes capture the
13590 -- result of their computation in a local temporary:
13592 -- do
13593 -- Trans_Id : Ctrl_Typ := ...;
13594 -- Result : constant Boolean := ... Trans_Id ...;
13595 -- <finalize Trans_Id>
13596 -- in Result end;
13598 -- As a result, the finalization of any transient objects can safely
13599 -- take place after the result capture.
13601 -- ??? could this be extended to elementary types?
13603 if Is_Boolean_Type (Etype (Expr)) then
13604 Fin_Context := Last (Stmts);
13606 -- Otherwise the immediate context may not be safe enough to carry
13607 -- out transient object finalization due to aliasing and nesting of
13608 -- constructs. Insert calls to [Deep_]Finalize after the innermost
13609 -- enclosing non-transient construct.
13611 else
13612 Fin_Context := Hook_Context;
13613 end if;
13615 -- Mark the transient object as successfully processed to avoid double
13616 -- finalization.
13618 Set_Is_Finalized_Transient (Obj_Id);
13620 -- Construct all the pieces necessary to hook and finalize a transient
13621 -- object.
13623 Build_Transient_Object_Statements
13624 (Obj_Decl => Obj_Decl,
13625 Fin_Call => Fin_Call,
13626 Hook_Assign => Hook_Assign,
13627 Hook_Clear => Hook_Clear,
13628 Hook_Decl => Hook_Decl,
13629 Ptr_Decl => Ptr_Decl,
13630 Finalize_Obj => False);
13632 -- Add the access type which provides a reference to the transient
13633 -- object. Generate:
13635 -- type Ptr_Typ is access all Desig_Typ;
13637 Insert_Action (Hook_Context, Ptr_Decl);
13639 -- Add the temporary which acts as a hook to the transient object.
13640 -- Generate:
13642 -- Hook : Ptr_Id := null;
13644 Insert_Action (Hook_Context, Hook_Decl);
13646 -- When the transient object is initialized by an aggregate, the hook
13647 -- must capture the object after the last aggregate assignment takes
13648 -- place. Only then is the object considered initialized. Generate:
13650 -- Hook := Ptr_Typ (Obj_Id);
13651 -- <or>
13652 -- Hook := Obj_Id'Unrestricted_Access;
13654 if Ekind_In (Obj_Id, E_Constant, E_Variable)
13655 and then Present (Last_Aggregate_Assignment (Obj_Id))
13656 then
13657 Hook_Insert := Last_Aggregate_Assignment (Obj_Id);
13659 -- Otherwise the hook seizes the related object immediately
13661 else
13662 Hook_Insert := Obj_Decl;
13663 end if;
13665 Insert_After_And_Analyze (Hook_Insert, Hook_Assign);
13667 -- When the node is part of a return statement, there is no need to
13668 -- insert a finalization call, as the general finalization mechanism
13669 -- (see Build_Finalizer) would take care of the transient object on
13670 -- subprogram exit. Note that it would also be impossible to insert the
13671 -- finalization code after the return statement as this will render it
13672 -- unreachable.
13674 if Nkind (Fin_Context) = N_Simple_Return_Statement then
13675 null;
13677 -- Finalize the hook after the context has been evaluated. Generate:
13679 -- if Hook /= null then
13680 -- [Deep_]Finalize (Hook.all);
13681 -- Hook := null;
13682 -- end if;
13684 else
13685 Insert_Action_After (Fin_Context,
13686 Make_Implicit_If_Statement (Obj_Decl,
13687 Condition =>
13688 Make_Op_Ne (Loc,
13689 Left_Opnd =>
13690 New_Occurrence_Of (Defining_Entity (Hook_Decl), Loc),
13691 Right_Opnd => Make_Null (Loc)),
13693 Then_Statements => New_List (
13694 Fin_Call,
13695 Hook_Clear)));
13696 end if;
13697 end Process_Transient_In_Expression;
13699 ------------------------
13700 -- Rewrite_Comparison --
13701 ------------------------
13703 procedure Rewrite_Comparison (N : Node_Id) is
13704 Typ : constant Entity_Id := Etype (N);
13706 False_Result : Boolean;
13707 True_Result : Boolean;
13709 begin
13710 if Nkind (N) = N_Type_Conversion then
13711 Rewrite_Comparison (Expression (N));
13712 return;
13714 elsif Nkind (N) not in N_Op_Compare then
13715 return;
13716 end if;
13718 -- Determine the potential outcome of the comparison assuming that the
13719 -- operands are valid and emit a warning when the comparison evaluates
13720 -- to True or False only in the presence of invalid values.
13722 Warn_On_Constant_Valid_Condition (N);
13724 -- Determine the potential outcome of the comparison assuming that the
13725 -- operands are not valid.
13727 Test_Comparison
13728 (Op => N,
13729 Assume_Valid => False,
13730 True_Result => True_Result,
13731 False_Result => False_Result);
13733 -- The outcome is a decisive False or True, rewrite the operator
13735 if False_Result or True_Result then
13736 Rewrite (N,
13737 Convert_To (Typ,
13738 New_Occurrence_Of (Boolean_Literals (True_Result), Sloc (N))));
13740 Analyze_And_Resolve (N, Typ);
13741 Warn_On_Known_Condition (N);
13742 end if;
13743 end Rewrite_Comparison;
13745 ----------------------------
13746 -- Safe_In_Place_Array_Op --
13747 ----------------------------
13749 function Safe_In_Place_Array_Op
13750 (Lhs : Node_Id;
13751 Op1 : Node_Id;
13752 Op2 : Node_Id) return Boolean
13754 Target : Entity_Id;
13756 function Is_Safe_Operand (Op : Node_Id) return Boolean;
13757 -- Operand is safe if it cannot overlap part of the target of the
13758 -- operation. If the operand and the target are identical, the operand
13759 -- is safe. The operand can be empty in the case of negation.
13761 function Is_Unaliased (N : Node_Id) return Boolean;
13762 -- Check that N is a stand-alone entity
13764 ------------------
13765 -- Is_Unaliased --
13766 ------------------
13768 function Is_Unaliased (N : Node_Id) return Boolean is
13769 begin
13770 return
13771 Is_Entity_Name (N)
13772 and then No (Address_Clause (Entity (N)))
13773 and then No (Renamed_Object (Entity (N)));
13774 end Is_Unaliased;
13776 ---------------------
13777 -- Is_Safe_Operand --
13778 ---------------------
13780 function Is_Safe_Operand (Op : Node_Id) return Boolean is
13781 begin
13782 if No (Op) then
13783 return True;
13785 elsif Is_Entity_Name (Op) then
13786 return Is_Unaliased (Op);
13788 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
13789 return Is_Unaliased (Prefix (Op));
13791 elsif Nkind (Op) = N_Slice then
13792 return
13793 Is_Unaliased (Prefix (Op))
13794 and then Entity (Prefix (Op)) /= Target;
13796 elsif Nkind (Op) = N_Op_Not then
13797 return Is_Safe_Operand (Right_Opnd (Op));
13799 else
13800 return False;
13801 end if;
13802 end Is_Safe_Operand;
13804 -- Start of processing for Safe_In_Place_Array_Op
13806 begin
13807 -- Skip this processing if the component size is different from system
13808 -- storage unit (since at least for NOT this would cause problems).
13810 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
13811 return False;
13813 -- Cannot do in place stuff if non-standard Boolean representation
13815 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
13816 return False;
13818 elsif not Is_Unaliased (Lhs) then
13819 return False;
13821 else
13822 Target := Entity (Lhs);
13823 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
13824 end if;
13825 end Safe_In_Place_Array_Op;
13827 -----------------------
13828 -- Tagged_Membership --
13829 -----------------------
13831 -- There are two different cases to consider depending on whether the right
13832 -- operand is a class-wide type or not. If not we just compare the actual
13833 -- tag of the left expr to the target type tag:
13835 -- Left_Expr.Tag = Right_Type'Tag;
13837 -- If it is a class-wide type we use the RT function CW_Membership which is
13838 -- usually implemented by looking in the ancestor tables contained in the
13839 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
13841 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13842 -- function IW_Membership which is usually implemented by looking in the
13843 -- table of abstract interface types plus the ancestor table contained in
13844 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13846 procedure Tagged_Membership
13847 (N : Node_Id;
13848 SCIL_Node : out Node_Id;
13849 Result : out Node_Id)
13851 Left : constant Node_Id := Left_Opnd (N);
13852 Right : constant Node_Id := Right_Opnd (N);
13853 Loc : constant Source_Ptr := Sloc (N);
13855 Full_R_Typ : Entity_Id;
13856 Left_Type : Entity_Id;
13857 New_Node : Node_Id;
13858 Right_Type : Entity_Id;
13859 Obj_Tag : Node_Id;
13861 begin
13862 SCIL_Node := Empty;
13864 -- Handle entities from the limited view
13866 Left_Type := Available_View (Etype (Left));
13867 Right_Type := Available_View (Etype (Right));
13869 -- In the case where the type is an access type, the test is applied
13870 -- using the designated types (needed in Ada 2012 for implicit anonymous
13871 -- access conversions, for AI05-0149).
13873 if Is_Access_Type (Right_Type) then
13874 Left_Type := Designated_Type (Left_Type);
13875 Right_Type := Designated_Type (Right_Type);
13876 end if;
13878 if Is_Class_Wide_Type (Left_Type) then
13879 Left_Type := Root_Type (Left_Type);
13880 end if;
13882 if Is_Class_Wide_Type (Right_Type) then
13883 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
13884 else
13885 Full_R_Typ := Underlying_Type (Right_Type);
13886 end if;
13888 Obj_Tag :=
13889 Make_Selected_Component (Loc,
13890 Prefix => Relocate_Node (Left),
13891 Selector_Name =>
13892 New_Occurrence_Of (First_Tag_Component (Left_Type), Loc));
13894 if Is_Class_Wide_Type (Right_Type) or else Is_Interface (Left_Type) then
13896 -- No need to issue a run-time check if we statically know that the
13897 -- result of this membership test is always true. For example,
13898 -- considering the following declarations:
13900 -- type Iface is interface;
13901 -- type T is tagged null record;
13902 -- type DT is new T and Iface with null record;
13904 -- Obj1 : T;
13905 -- Obj2 : DT;
13907 -- These membership tests are always true:
13909 -- Obj1 in T'Class
13910 -- Obj2 in T'Class;
13911 -- Obj2 in Iface'Class;
13913 -- We do not need to handle cases where the membership is illegal.
13914 -- For example:
13916 -- Obj1 in DT'Class; -- Compile time error
13917 -- Obj1 in Iface'Class; -- Compile time error
13919 if not Is_Class_Wide_Type (Left_Type)
13920 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
13921 Use_Full_View => True)
13922 or else (Is_Interface (Etype (Right_Type))
13923 and then Interface_Present_In_Ancestor
13924 (Typ => Left_Type,
13925 Iface => Etype (Right_Type))))
13926 then
13927 Result := New_Occurrence_Of (Standard_True, Loc);
13928 return;
13929 end if;
13931 -- Ada 2005 (AI-251): Class-wide applied to interfaces
13933 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
13935 -- Support to: "Iface_CW_Typ in Typ'Class"
13937 or else Is_Interface (Left_Type)
13938 then
13939 -- Issue error if IW_Membership operation not available in a
13940 -- configurable run time setting.
13942 if not RTE_Available (RE_IW_Membership) then
13943 Error_Msg_CRT
13944 ("dynamic membership test on interface types", N);
13945 Result := Empty;
13946 return;
13947 end if;
13949 Result :=
13950 Make_Function_Call (Loc,
13951 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
13952 Parameter_Associations => New_List (
13953 Make_Attribute_Reference (Loc,
13954 Prefix => Obj_Tag,
13955 Attribute_Name => Name_Address),
13956 New_Occurrence_Of (
13957 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
13958 Loc)));
13960 -- Ada 95: Normal case
13962 else
13963 Build_CW_Membership (Loc,
13964 Obj_Tag_Node => Obj_Tag,
13965 Typ_Tag_Node =>
13966 New_Occurrence_Of (
13967 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
13968 Related_Nod => N,
13969 New_Node => New_Node);
13971 -- Generate the SCIL node for this class-wide membership test.
13972 -- Done here because the previous call to Build_CW_Membership
13973 -- relocates Obj_Tag.
13975 if Generate_SCIL then
13976 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
13977 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
13978 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
13979 end if;
13981 Result := New_Node;
13982 end if;
13984 -- Right_Type is not a class-wide type
13986 else
13987 -- No need to check the tag of the object if Right_Typ is abstract
13989 if Is_Abstract_Type (Right_Type) then
13990 Result := New_Occurrence_Of (Standard_False, Loc);
13992 else
13993 Result :=
13994 Make_Op_Eq (Loc,
13995 Left_Opnd => Obj_Tag,
13996 Right_Opnd =>
13997 New_Occurrence_Of
13998 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
13999 end if;
14000 end if;
14001 end Tagged_Membership;
14003 ------------------------------
14004 -- Unary_Op_Validity_Checks --
14005 ------------------------------
14007 procedure Unary_Op_Validity_Checks (N : Node_Id) is
14008 begin
14009 if Validity_Checks_On and Validity_Check_Operands then
14010 Ensure_Valid (Right_Opnd (N));
14011 end if;
14012 end Unary_Op_Validity_Checks;
14014 end Exp_Ch4;