* lto.c (do_stream_out): Add PART parameter; open dump file.
[official-gcc.git] / gcc / ada / exp_ch4.adb
blobc29ba76f11328e9358348eedbe5e56c8aca1ea4a
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 Eq_Op : Entity_Id;
2340 function Find_Primitive_Eq return Node_Id;
2341 -- AI05-0123: Locate primitive equality for type if it exists, and
2342 -- build the corresponding call. If operation is abstract, replace
2343 -- call with an explicit raise. Return Empty if there is no primitive.
2345 -----------------------
2346 -- Find_Primitive_Eq --
2347 -----------------------
2349 function Find_Primitive_Eq return Node_Id is
2350 Prim_E : Elmt_Id;
2351 Prim : Node_Id;
2353 begin
2354 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2355 while Present (Prim_E) loop
2356 Prim := Node (Prim_E);
2358 -- Locate primitive equality with the right signature
2360 if Chars (Prim) = Name_Op_Eq
2361 and then Etype (First_Formal (Prim)) =
2362 Etype (Next_Formal (First_Formal (Prim)))
2363 and then Etype (Prim) = Standard_Boolean
2364 then
2365 if Is_Abstract_Subprogram (Prim) then
2366 return
2367 Make_Raise_Program_Error (Loc,
2368 Reason => PE_Explicit_Raise);
2370 else
2371 return
2372 Make_Function_Call (Loc,
2373 Name => New_Occurrence_Of (Prim, Loc),
2374 Parameter_Associations => New_List (Lhs, Rhs));
2375 end if;
2376 end if;
2378 Next_Elmt (Prim_E);
2379 end loop;
2381 -- If not found, predefined operation will be used
2383 return Empty;
2384 end Find_Primitive_Eq;
2386 -- Start of processing for Expand_Composite_Equality
2388 begin
2389 if Is_Private_Type (Typ) then
2390 Full_Type := Underlying_Type (Typ);
2391 else
2392 Full_Type := Typ;
2393 end if;
2395 -- If the private type has no completion the context may be the
2396 -- expansion of a composite equality for a composite type with some
2397 -- still incomplete components. The expression will not be analyzed
2398 -- until the enclosing type is completed, at which point this will be
2399 -- properly expanded, unless there is a bona fide completion error.
2401 if No (Full_Type) then
2402 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2403 end if;
2405 Full_Type := Base_Type (Full_Type);
2407 -- When the base type itself is private, use the full view to expand
2408 -- the composite equality.
2410 if Is_Private_Type (Full_Type) then
2411 Full_Type := Underlying_Type (Full_Type);
2412 end if;
2414 -- Case of array types
2416 if Is_Array_Type (Full_Type) then
2418 -- If the operand is an elementary type other than a floating-point
2419 -- type, then we can simply use the built-in block bitwise equality,
2420 -- since the predefined equality operators always apply and bitwise
2421 -- equality is fine for all these cases.
2423 if Is_Elementary_Type (Component_Type (Full_Type))
2424 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2425 then
2426 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2428 -- For composite component types, and floating-point types, use the
2429 -- expansion. This deals with tagged component types (where we use
2430 -- the applicable equality routine) and floating-point (where we
2431 -- need to worry about negative zeroes), and also the case of any
2432 -- composite type recursively containing such fields.
2434 else
2435 declare
2436 Comp_Typ : Entity_Id;
2437 Hi : Node_Id;
2438 Indx : Node_Id;
2439 Ityp : Entity_Id;
2440 Lo : Node_Id;
2442 begin
2443 -- Do the comparison in the type (or its full view) and not in
2444 -- its unconstrained base type, because the latter operation is
2445 -- more complex and would also require an unchecked conversion.
2447 if Is_Private_Type (Typ) then
2448 Comp_Typ := Underlying_Type (Typ);
2449 else
2450 Comp_Typ := Typ;
2451 end if;
2453 -- Except for the case where the bounds of the type depend on a
2454 -- discriminant, or else we would run into scoping issues.
2456 Indx := First_Index (Comp_Typ);
2457 while Present (Indx) loop
2458 Ityp := Etype (Indx);
2460 Lo := Type_Low_Bound (Ityp);
2461 Hi := Type_High_Bound (Ityp);
2463 if (Nkind (Lo) = N_Identifier
2464 and then Ekind (Entity (Lo)) = E_Discriminant)
2465 or else
2466 (Nkind (Hi) = N_Identifier
2467 and then Ekind (Entity (Hi)) = E_Discriminant)
2468 then
2469 Comp_Typ := Full_Type;
2470 exit;
2471 end if;
2473 Next_Index (Indx);
2474 end loop;
2476 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Comp_Typ);
2477 end;
2478 end if;
2480 -- Case of tagged record types
2482 elsif Is_Tagged_Type (Full_Type) then
2483 Eq_Op := Find_Primitive_Eq (Typ);
2484 pragma Assert (Present (Eq_Op));
2486 return
2487 Make_Function_Call (Loc,
2488 Name => New_Occurrence_Of (Eq_Op, Loc),
2489 Parameter_Associations =>
2490 New_List
2491 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2492 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2494 -- Case of untagged record types
2496 elsif Is_Record_Type (Full_Type) then
2497 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2499 if Present (Eq_Op) then
2500 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2502 -- Inherited equality from parent type. Convert the actuals to
2503 -- match signature of operation.
2505 declare
2506 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2508 begin
2509 return
2510 Make_Function_Call (Loc,
2511 Name => New_Occurrence_Of (Eq_Op, Loc),
2512 Parameter_Associations => New_List (
2513 OK_Convert_To (T, Lhs),
2514 OK_Convert_To (T, Rhs)));
2515 end;
2517 else
2518 -- Comparison between Unchecked_Union components
2520 if Is_Unchecked_Union (Full_Type) then
2521 declare
2522 Lhs_Type : Node_Id := Full_Type;
2523 Rhs_Type : Node_Id := Full_Type;
2524 Lhs_Discr_Val : Node_Id;
2525 Rhs_Discr_Val : Node_Id;
2527 begin
2528 -- Lhs subtype
2530 if Nkind (Lhs) = N_Selected_Component then
2531 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2532 end if;
2534 -- Rhs subtype
2536 if Nkind (Rhs) = N_Selected_Component then
2537 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2538 end if;
2540 -- Lhs of the composite equality
2542 if Is_Constrained (Lhs_Type) then
2544 -- Since the enclosing record type can never be an
2545 -- Unchecked_Union (this code is executed for records
2546 -- that do not have variants), we may reference its
2547 -- discriminant(s).
2549 if Nkind (Lhs) = N_Selected_Component
2550 and then Has_Per_Object_Constraint
2551 (Entity (Selector_Name (Lhs)))
2552 then
2553 Lhs_Discr_Val :=
2554 Make_Selected_Component (Loc,
2555 Prefix => Prefix (Lhs),
2556 Selector_Name =>
2557 New_Copy
2558 (Get_Discriminant_Value
2559 (First_Discriminant (Lhs_Type),
2560 Lhs_Type,
2561 Stored_Constraint (Lhs_Type))));
2563 else
2564 Lhs_Discr_Val :=
2565 New_Copy
2566 (Get_Discriminant_Value
2567 (First_Discriminant (Lhs_Type),
2568 Lhs_Type,
2569 Stored_Constraint (Lhs_Type)));
2571 end if;
2572 else
2573 -- It is not possible to infer the discriminant since
2574 -- the subtype is not constrained.
2576 return
2577 Make_Raise_Program_Error (Loc,
2578 Reason => PE_Unchecked_Union_Restriction);
2579 end if;
2581 -- Rhs of the composite equality
2583 if Is_Constrained (Rhs_Type) then
2584 if Nkind (Rhs) = N_Selected_Component
2585 and then Has_Per_Object_Constraint
2586 (Entity (Selector_Name (Rhs)))
2587 then
2588 Rhs_Discr_Val :=
2589 Make_Selected_Component (Loc,
2590 Prefix => Prefix (Rhs),
2591 Selector_Name =>
2592 New_Copy
2593 (Get_Discriminant_Value
2594 (First_Discriminant (Rhs_Type),
2595 Rhs_Type,
2596 Stored_Constraint (Rhs_Type))));
2598 else
2599 Rhs_Discr_Val :=
2600 New_Copy
2601 (Get_Discriminant_Value
2602 (First_Discriminant (Rhs_Type),
2603 Rhs_Type,
2604 Stored_Constraint (Rhs_Type)));
2606 end if;
2607 else
2608 return
2609 Make_Raise_Program_Error (Loc,
2610 Reason => PE_Unchecked_Union_Restriction);
2611 end if;
2613 -- Call the TSS equality function with the inferred
2614 -- discriminant values.
2616 return
2617 Make_Function_Call (Loc,
2618 Name => New_Occurrence_Of (Eq_Op, Loc),
2619 Parameter_Associations => New_List (
2620 Lhs,
2621 Rhs,
2622 Lhs_Discr_Val,
2623 Rhs_Discr_Val));
2624 end;
2626 -- All cases other than comparing Unchecked_Union types
2628 else
2629 declare
2630 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2631 begin
2632 return
2633 Make_Function_Call (Loc,
2634 Name =>
2635 New_Occurrence_Of (Eq_Op, Loc),
2636 Parameter_Associations => New_List (
2637 OK_Convert_To (T, Lhs),
2638 OK_Convert_To (T, Rhs)));
2639 end;
2640 end if;
2641 end if;
2643 -- Equality composes in Ada 2012 for untagged record types. It also
2644 -- composes for bounded strings, because they are part of the
2645 -- predefined environment. We could make it compose for bounded
2646 -- strings by making them tagged, or by making sure all subcomponents
2647 -- are set to the same value, even when not used. Instead, we have
2648 -- this special case in the compiler, because it's more efficient.
2650 elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then
2652 -- If no TSS has been created for the type, check whether there is
2653 -- a primitive equality declared for it.
2655 declare
2656 Op : constant Node_Id := Find_Primitive_Eq;
2658 begin
2659 -- Use user-defined primitive if it exists, otherwise use
2660 -- predefined equality.
2662 if Present (Op) then
2663 return Op;
2664 else
2665 return Make_Op_Eq (Loc, Lhs, Rhs);
2666 end if;
2667 end;
2669 else
2670 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2671 end if;
2673 -- Non-composite types (always use predefined equality)
2675 else
2676 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2677 end if;
2678 end Expand_Composite_Equality;
2680 ------------------------
2681 -- Expand_Concatenate --
2682 ------------------------
2684 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2685 Loc : constant Source_Ptr := Sloc (Cnode);
2687 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2688 -- Result type of concatenation
2690 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2691 -- Component type. Elements of this component type can appear as one
2692 -- of the operands of concatenation as well as arrays.
2694 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2695 -- Index subtype
2697 Ityp : constant Entity_Id := Base_Type (Istyp);
2698 -- Index type. This is the base type of the index subtype, and is used
2699 -- for all computed bounds (which may be out of range of Istyp in the
2700 -- case of null ranges).
2702 Artyp : Entity_Id;
2703 -- This is the type we use to do arithmetic to compute the bounds and
2704 -- lengths of operands. The choice of this type is a little subtle and
2705 -- is discussed in a separate section at the start of the body code.
2707 Concatenation_Error : exception;
2708 -- Raised if concatenation is sure to raise a CE
2710 Result_May_Be_Null : Boolean := True;
2711 -- Reset to False if at least one operand is encountered which is known
2712 -- at compile time to be non-null. Used for handling the special case
2713 -- of setting the high bound to the last operand high bound for a null
2714 -- result, thus ensuring a proper high bound in the super-flat case.
2716 N : constant Nat := List_Length (Opnds);
2717 -- Number of concatenation operands including possibly null operands
2719 NN : Nat := 0;
2720 -- Number of operands excluding any known to be null, except that the
2721 -- last operand is always retained, in case it provides the bounds for
2722 -- a null result.
2724 Opnd : Node_Id := Empty;
2725 -- Current operand being processed in the loop through operands. After
2726 -- this loop is complete, always contains the last operand (which is not
2727 -- the same as Operands (NN), since null operands are skipped).
2729 -- Arrays describing the operands, only the first NN entries of each
2730 -- array are set (NN < N when we exclude known null operands).
2732 Is_Fixed_Length : array (1 .. N) of Boolean;
2733 -- True if length of corresponding operand known at compile time
2735 Operands : array (1 .. N) of Node_Id;
2736 -- Set to the corresponding entry in the Opnds list (but note that null
2737 -- operands are excluded, so not all entries in the list are stored).
2739 Fixed_Length : array (1 .. N) of Uint;
2740 -- Set to length of operand. Entries in this array are set only if the
2741 -- corresponding entry in Is_Fixed_Length is True.
2743 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2744 -- Set to lower bound of operand. Either an integer literal in the case
2745 -- where the bound is known at compile time, else actual lower bound.
2746 -- The operand low bound is of type Ityp.
2748 Var_Length : array (1 .. N) of Entity_Id;
2749 -- Set to an entity of type Natural that contains the length of an
2750 -- operand whose length is not known at compile time. Entries in this
2751 -- array are set only if the corresponding entry in Is_Fixed_Length
2752 -- is False. The entity is of type Artyp.
2754 Aggr_Length : array (0 .. N) of Node_Id;
2755 -- The J'th entry in an expression node that represents the total length
2756 -- of operands 1 through J. It is either an integer literal node, or a
2757 -- reference to a constant entity with the right value, so it is fine
2758 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2759 -- entry always is set to zero. The length is of type Artyp.
2761 Low_Bound : Node_Id;
2762 -- A tree node representing the low bound of the result (of type Ityp).
2763 -- This is either an integer literal node, or an identifier reference to
2764 -- a constant entity initialized to the appropriate value.
2766 Last_Opnd_Low_Bound : Node_Id := Empty;
2767 -- A tree node representing the low bound of the last operand. This
2768 -- need only be set if the result could be null. It is used for the
2769 -- special case of setting the right low bound for a null result.
2770 -- This is of type Ityp.
2772 Last_Opnd_High_Bound : Node_Id := Empty;
2773 -- A tree node representing the high bound of the last operand. This
2774 -- need only be set if the result could be null. It is used for the
2775 -- special case of setting the right high bound for a null result.
2776 -- This is of type Ityp.
2778 High_Bound : Node_Id := Empty;
2779 -- A tree node representing the high bound of the result (of type Ityp)
2781 Result : Node_Id;
2782 -- Result of the concatenation (of type Ityp)
2784 Actions : constant List_Id := New_List;
2785 -- Collect actions to be inserted
2787 Known_Non_Null_Operand_Seen : Boolean;
2788 -- Set True during generation of the assignments of operands into
2789 -- result once an operand known to be non-null has been seen.
2791 function Library_Level_Target return Boolean;
2792 -- Return True if the concatenation is within the expression of the
2793 -- declaration of a library-level object.
2795 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2796 -- This function makes an N_Integer_Literal node that is returned in
2797 -- analyzed form with the type set to Artyp. Importantly this literal
2798 -- is not flagged as static, so that if we do computations with it that
2799 -- result in statically detected out of range conditions, we will not
2800 -- generate error messages but instead warning messages.
2802 function To_Artyp (X : Node_Id) return Node_Id;
2803 -- Given a node of type Ityp, returns the corresponding value of type
2804 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2805 -- For enum types, the Pos of the value is returned.
2807 function To_Ityp (X : Node_Id) return Node_Id;
2808 -- The inverse function (uses Val in the case of enumeration types)
2810 --------------------------
2811 -- Library_Level_Target --
2812 --------------------------
2814 function Library_Level_Target return Boolean is
2815 P : Node_Id := Parent (Cnode);
2817 begin
2818 while Present (P) loop
2819 if Nkind (P) = N_Object_Declaration then
2820 return Is_Library_Level_Entity (Defining_Identifier (P));
2822 -- Prevent the search from going too far
2824 elsif Is_Body_Or_Package_Declaration (P) then
2825 return False;
2826 end if;
2828 P := Parent (P);
2829 end loop;
2831 return False;
2832 end Library_Level_Target;
2834 ------------------------
2835 -- Make_Artyp_Literal --
2836 ------------------------
2838 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2839 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2840 begin
2841 Set_Etype (Result, Artyp);
2842 Set_Analyzed (Result, True);
2843 Set_Is_Static_Expression (Result, False);
2844 return Result;
2845 end Make_Artyp_Literal;
2847 --------------
2848 -- To_Artyp --
2849 --------------
2851 function To_Artyp (X : Node_Id) return Node_Id is
2852 begin
2853 if Ityp = Base_Type (Artyp) then
2854 return X;
2856 elsif Is_Enumeration_Type (Ityp) then
2857 return
2858 Make_Attribute_Reference (Loc,
2859 Prefix => New_Occurrence_Of (Ityp, Loc),
2860 Attribute_Name => Name_Pos,
2861 Expressions => New_List (X));
2863 else
2864 return Convert_To (Artyp, X);
2865 end if;
2866 end To_Artyp;
2868 -------------
2869 -- To_Ityp --
2870 -------------
2872 function To_Ityp (X : Node_Id) return Node_Id is
2873 begin
2874 if Is_Enumeration_Type (Ityp) then
2875 return
2876 Make_Attribute_Reference (Loc,
2877 Prefix => New_Occurrence_Of (Ityp, Loc),
2878 Attribute_Name => Name_Val,
2879 Expressions => New_List (X));
2881 -- Case where we will do a type conversion
2883 else
2884 if Ityp = Base_Type (Artyp) then
2885 return X;
2886 else
2887 return Convert_To (Ityp, X);
2888 end if;
2889 end if;
2890 end To_Ityp;
2892 -- Local Declarations
2894 Opnd_Typ : Entity_Id;
2895 Ent : Entity_Id;
2896 Len : Uint;
2897 J : Nat;
2898 Clen : Node_Id;
2899 Set : Boolean;
2901 -- Start of processing for Expand_Concatenate
2903 begin
2904 -- Choose an appropriate computational type
2906 -- We will be doing calculations of lengths and bounds in this routine
2907 -- and computing one from the other in some cases, e.g. getting the high
2908 -- bound by adding the length-1 to the low bound.
2910 -- We can't just use the index type, or even its base type for this
2911 -- purpose for two reasons. First it might be an enumeration type which
2912 -- is not suitable for computations of any kind, and second it may
2913 -- simply not have enough range. For example if the index type is
2914 -- -128..+127 then lengths can be up to 256, which is out of range of
2915 -- the type.
2917 -- For enumeration types, we can simply use Standard_Integer, this is
2918 -- sufficient since the actual number of enumeration literals cannot
2919 -- possibly exceed the range of integer (remember we will be doing the
2920 -- arithmetic with POS values, not representation values).
2922 if Is_Enumeration_Type (Ityp) then
2923 Artyp := Standard_Integer;
2925 -- If index type is Positive, we use the standard unsigned type, to give
2926 -- more room on the top of the range, obviating the need for an overflow
2927 -- check when creating the upper bound. This is needed to avoid junk
2928 -- overflow checks in the common case of String types.
2930 -- ??? Disabled for now
2932 -- elsif Istyp = Standard_Positive then
2933 -- Artyp := Standard_Unsigned;
2935 -- For modular types, we use a 32-bit modular type for types whose size
2936 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2937 -- identity type, and for larger unsigned types we use 64-bits.
2939 elsif Is_Modular_Integer_Type (Ityp) then
2940 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
2941 Artyp := Standard_Unsigned;
2942 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
2943 Artyp := Ityp;
2944 else
2945 Artyp := RTE (RE_Long_Long_Unsigned);
2946 end if;
2948 -- Similar treatment for signed types
2950 else
2951 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
2952 Artyp := Standard_Integer;
2953 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
2954 Artyp := Ityp;
2955 else
2956 Artyp := Standard_Long_Long_Integer;
2957 end if;
2958 end if;
2960 -- Supply dummy entry at start of length array
2962 Aggr_Length (0) := Make_Artyp_Literal (0);
2964 -- Go through operands setting up the above arrays
2966 J := 1;
2967 while J <= N loop
2968 Opnd := Remove_Head (Opnds);
2969 Opnd_Typ := Etype (Opnd);
2971 -- The parent got messed up when we put the operands in a list,
2972 -- so now put back the proper parent for the saved operand, that
2973 -- is to say the concatenation node, to make sure that each operand
2974 -- is seen as a subexpression, e.g. if actions must be inserted.
2976 Set_Parent (Opnd, Cnode);
2978 -- Set will be True when we have setup one entry in the array
2980 Set := False;
2982 -- Singleton element (or character literal) case
2984 if Base_Type (Opnd_Typ) = Ctyp then
2985 NN := NN + 1;
2986 Operands (NN) := Opnd;
2987 Is_Fixed_Length (NN) := True;
2988 Fixed_Length (NN) := Uint_1;
2989 Result_May_Be_Null := False;
2991 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2992 -- since we know that the result cannot be null).
2994 Opnd_Low_Bound (NN) :=
2995 Make_Attribute_Reference (Loc,
2996 Prefix => New_Occurrence_Of (Istyp, Loc),
2997 Attribute_Name => Name_First);
2999 Set := True;
3001 -- String literal case (can only occur for strings of course)
3003 elsif Nkind (Opnd) = N_String_Literal then
3004 Len := String_Literal_Length (Opnd_Typ);
3006 if Len /= 0 then
3007 Result_May_Be_Null := False;
3008 end if;
3010 -- Capture last operand low and high bound if result could be null
3012 if J = N and then Result_May_Be_Null then
3013 Last_Opnd_Low_Bound :=
3014 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3016 Last_Opnd_High_Bound :=
3017 Make_Op_Subtract (Loc,
3018 Left_Opnd =>
3019 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
3020 Right_Opnd => Make_Integer_Literal (Loc, 1));
3021 end if;
3023 -- Skip null string literal
3025 if J < N and then Len = 0 then
3026 goto Continue;
3027 end if;
3029 NN := NN + 1;
3030 Operands (NN) := Opnd;
3031 Is_Fixed_Length (NN) := True;
3033 -- Set length and bounds
3035 Fixed_Length (NN) := Len;
3037 Opnd_Low_Bound (NN) :=
3038 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3040 Set := True;
3042 -- All other cases
3044 else
3045 -- Check constrained case with known bounds
3047 if Is_Constrained (Opnd_Typ) then
3048 declare
3049 Index : constant Node_Id := First_Index (Opnd_Typ);
3050 Indx_Typ : constant Entity_Id := Etype (Index);
3051 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
3052 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
3054 begin
3055 -- Fixed length constrained array type with known at compile
3056 -- time bounds is last case of fixed length operand.
3058 if Compile_Time_Known_Value (Lo)
3059 and then
3060 Compile_Time_Known_Value (Hi)
3061 then
3062 declare
3063 Loval : constant Uint := Expr_Value (Lo);
3064 Hival : constant Uint := Expr_Value (Hi);
3065 Len : constant Uint :=
3066 UI_Max (Hival - Loval + 1, Uint_0);
3068 begin
3069 if Len > 0 then
3070 Result_May_Be_Null := False;
3071 end if;
3073 -- Capture last operand bounds if result could be null
3075 if J = N and then Result_May_Be_Null then
3076 Last_Opnd_Low_Bound :=
3077 Convert_To (Ityp,
3078 Make_Integer_Literal (Loc, Expr_Value (Lo)));
3080 Last_Opnd_High_Bound :=
3081 Convert_To (Ityp,
3082 Make_Integer_Literal (Loc, Expr_Value (Hi)));
3083 end if;
3085 -- Exclude null length case unless last operand
3087 if J < N and then Len = 0 then
3088 goto Continue;
3089 end if;
3091 NN := NN + 1;
3092 Operands (NN) := Opnd;
3093 Is_Fixed_Length (NN) := True;
3094 Fixed_Length (NN) := Len;
3096 Opnd_Low_Bound (NN) :=
3097 To_Ityp
3098 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
3099 Set := True;
3100 end;
3101 end if;
3102 end;
3103 end if;
3105 -- All cases where the length is not known at compile time, or the
3106 -- special case of an operand which is known to be null but has a
3107 -- lower bound other than 1 or is other than a string type.
3109 if not Set then
3110 NN := NN + 1;
3112 -- Capture operand bounds
3114 Opnd_Low_Bound (NN) :=
3115 Make_Attribute_Reference (Loc,
3116 Prefix =>
3117 Duplicate_Subexpr (Opnd, Name_Req => True),
3118 Attribute_Name => Name_First);
3120 -- Capture last operand bounds if result could be null
3122 if J = N and Result_May_Be_Null then
3123 Last_Opnd_Low_Bound :=
3124 Convert_To (Ityp,
3125 Make_Attribute_Reference (Loc,
3126 Prefix =>
3127 Duplicate_Subexpr (Opnd, Name_Req => True),
3128 Attribute_Name => Name_First));
3130 Last_Opnd_High_Bound :=
3131 Convert_To (Ityp,
3132 Make_Attribute_Reference (Loc,
3133 Prefix =>
3134 Duplicate_Subexpr (Opnd, Name_Req => True),
3135 Attribute_Name => Name_Last));
3136 end if;
3138 -- Capture length of operand in entity
3140 Operands (NN) := Opnd;
3141 Is_Fixed_Length (NN) := False;
3143 Var_Length (NN) := Make_Temporary (Loc, 'L');
3145 Append_To (Actions,
3146 Make_Object_Declaration (Loc,
3147 Defining_Identifier => Var_Length (NN),
3148 Constant_Present => True,
3149 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3150 Expression =>
3151 Make_Attribute_Reference (Loc,
3152 Prefix =>
3153 Duplicate_Subexpr (Opnd, Name_Req => True),
3154 Attribute_Name => Name_Length)));
3155 end if;
3156 end if;
3158 -- Set next entry in aggregate length array
3160 -- For first entry, make either integer literal for fixed length
3161 -- or a reference to the saved length for variable length.
3163 if NN = 1 then
3164 if Is_Fixed_Length (1) then
3165 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
3166 else
3167 Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc);
3168 end if;
3170 -- If entry is fixed length and only fixed lengths so far, make
3171 -- appropriate new integer literal adding new length.
3173 elsif Is_Fixed_Length (NN)
3174 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
3175 then
3176 Aggr_Length (NN) :=
3177 Make_Integer_Literal (Loc,
3178 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
3180 -- All other cases, construct an addition node for the length and
3181 -- create an entity initialized to this length.
3183 else
3184 Ent := Make_Temporary (Loc, 'L');
3186 if Is_Fixed_Length (NN) then
3187 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
3188 else
3189 Clen := New_Occurrence_Of (Var_Length (NN), Loc);
3190 end if;
3192 Append_To (Actions,
3193 Make_Object_Declaration (Loc,
3194 Defining_Identifier => Ent,
3195 Constant_Present => True,
3196 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3197 Expression =>
3198 Make_Op_Add (Loc,
3199 Left_Opnd => New_Copy_Tree (Aggr_Length (NN - 1)),
3200 Right_Opnd => Clen)));
3202 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
3203 end if;
3205 <<Continue>>
3206 J := J + 1;
3207 end loop;
3209 -- If we have only skipped null operands, return the last operand
3211 if NN = 0 then
3212 Result := Opnd;
3213 goto Done;
3214 end if;
3216 -- If we have only one non-null operand, return it and we are done.
3217 -- There is one case in which this cannot be done, and that is when
3218 -- the sole operand is of the element type, in which case it must be
3219 -- converted to an array, and the easiest way of doing that is to go
3220 -- through the normal general circuit.
3222 if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then
3223 Result := Operands (1);
3224 goto Done;
3225 end if;
3227 -- Cases where we have a real concatenation
3229 -- Next step is to find the low bound for the result array that we
3230 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3232 -- If the ultimate ancestor of the index subtype is a constrained array
3233 -- definition, then the lower bound is that of the index subtype as
3234 -- specified by (RM 4.5.3(6)).
3236 -- The right test here is to go to the root type, and then the ultimate
3237 -- ancestor is the first subtype of this root type.
3239 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
3240 Low_Bound :=
3241 Make_Attribute_Reference (Loc,
3242 Prefix =>
3243 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
3244 Attribute_Name => Name_First);
3246 -- If the first operand in the list has known length we know that
3247 -- the lower bound of the result is the lower bound of this operand.
3249 elsif Is_Fixed_Length (1) then
3250 Low_Bound := Opnd_Low_Bound (1);
3252 -- OK, we don't know the lower bound, we have to build a horrible
3253 -- if expression node of the form
3255 -- if Cond1'Length /= 0 then
3256 -- Opnd1 low bound
3257 -- else
3258 -- if Opnd2'Length /= 0 then
3259 -- Opnd2 low bound
3260 -- else
3261 -- ...
3263 -- The nesting ends either when we hit an operand whose length is known
3264 -- at compile time, or on reaching the last operand, whose low bound we
3265 -- take unconditionally whether or not it is null. It's easiest to do
3266 -- this with a recursive procedure:
3268 else
3269 declare
3270 function Get_Known_Bound (J : Nat) return Node_Id;
3271 -- Returns the lower bound determined by operands J .. NN
3273 ---------------------
3274 -- Get_Known_Bound --
3275 ---------------------
3277 function Get_Known_Bound (J : Nat) return Node_Id is
3278 begin
3279 if Is_Fixed_Length (J) or else J = NN then
3280 return New_Copy_Tree (Opnd_Low_Bound (J));
3282 else
3283 return
3284 Make_If_Expression (Loc,
3285 Expressions => New_List (
3287 Make_Op_Ne (Loc,
3288 Left_Opnd =>
3289 New_Occurrence_Of (Var_Length (J), Loc),
3290 Right_Opnd =>
3291 Make_Integer_Literal (Loc, 0)),
3293 New_Copy_Tree (Opnd_Low_Bound (J)),
3294 Get_Known_Bound (J + 1)));
3295 end if;
3296 end Get_Known_Bound;
3298 begin
3299 Ent := Make_Temporary (Loc, 'L');
3301 Append_To (Actions,
3302 Make_Object_Declaration (Loc,
3303 Defining_Identifier => Ent,
3304 Constant_Present => True,
3305 Object_Definition => New_Occurrence_Of (Ityp, Loc),
3306 Expression => Get_Known_Bound (1)));
3308 Low_Bound := New_Occurrence_Of (Ent, Loc);
3309 end;
3310 end if;
3312 -- Now we can safely compute the upper bound, normally
3313 -- Low_Bound + Length - 1.
3315 High_Bound :=
3316 To_Ityp
3317 (Make_Op_Add (Loc,
3318 Left_Opnd => To_Artyp (New_Copy_Tree (Low_Bound)),
3319 Right_Opnd =>
3320 Make_Op_Subtract (Loc,
3321 Left_Opnd => New_Copy_Tree (Aggr_Length (NN)),
3322 Right_Opnd => Make_Artyp_Literal (1))));
3324 -- Note that calculation of the high bound may cause overflow in some
3325 -- very weird cases, so in the general case we need an overflow check on
3326 -- the high bound. We can avoid this for the common case of string types
3327 -- and other types whose index is Positive, since we chose a wider range
3328 -- for the arithmetic type. If checks are suppressed we do not set the
3329 -- flag, and possibly superfluous warnings will be omitted.
3331 if Istyp /= Standard_Positive
3332 and then not Overflow_Checks_Suppressed (Istyp)
3333 then
3334 Activate_Overflow_Check (High_Bound);
3335 end if;
3337 -- Handle the exceptional case where the result is null, in which case
3338 -- case the bounds come from the last operand (so that we get the proper
3339 -- bounds if the last operand is super-flat).
3341 if Result_May_Be_Null then
3342 Low_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_Low_Bound,
3349 Low_Bound));
3351 High_Bound :=
3352 Make_If_Expression (Loc,
3353 Expressions => New_List (
3354 Make_Op_Eq (Loc,
3355 Left_Opnd => New_Copy_Tree (Aggr_Length (NN)),
3356 Right_Opnd => Make_Artyp_Literal (0)),
3357 Last_Opnd_High_Bound,
3358 High_Bound));
3359 end if;
3361 -- Here is where we insert the saved up actions
3363 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3365 -- Now we construct an array object with appropriate bounds. We mark
3366 -- the target as internal to prevent useless initialization when
3367 -- Initialize_Scalars is enabled. Also since this is the actual result
3368 -- entity, we make sure we have debug information for the result.
3370 Ent := Make_Temporary (Loc, 'S');
3371 Set_Is_Internal (Ent);
3372 Set_Needs_Debug_Info (Ent);
3374 -- If the bound is statically known to be out of range, we do not want
3375 -- to abort, we want a warning and a runtime constraint error. Note that
3376 -- we have arranged that the result will not be treated as a static
3377 -- constant, so we won't get an illegality during this insertion.
3379 Insert_Action (Cnode,
3380 Make_Object_Declaration (Loc,
3381 Defining_Identifier => Ent,
3382 Object_Definition =>
3383 Make_Subtype_Indication (Loc,
3384 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3385 Constraint =>
3386 Make_Index_Or_Discriminant_Constraint (Loc,
3387 Constraints => New_List (
3388 Make_Range (Loc,
3389 Low_Bound => Low_Bound,
3390 High_Bound => High_Bound))))),
3391 Suppress => All_Checks);
3393 -- If the result of the concatenation appears as the initializing
3394 -- expression of an object declaration, we can just rename the
3395 -- result, rather than copying it.
3397 Set_OK_To_Rename (Ent);
3399 -- Catch the static out of range case now
3401 if Raises_Constraint_Error (High_Bound) then
3402 raise Concatenation_Error;
3403 end if;
3405 -- Now we will generate the assignments to do the actual concatenation
3407 -- There is one case in which we will not do this, namely when all the
3408 -- following conditions are met:
3410 -- The result type is Standard.String
3412 -- There are nine or fewer retained (non-null) operands
3414 -- The optimization level is -O0 or the debug flag gnatd.C is set,
3415 -- and the debug flag gnatd.c is not set.
3417 -- The corresponding System.Concat_n.Str_Concat_n routine is
3418 -- available in the run time.
3420 -- If all these conditions are met then we generate a call to the
3421 -- relevant concatenation routine. The purpose of this is to avoid
3422 -- undesirable code bloat at -O0.
3424 -- If the concatenation is within the declaration of a library-level
3425 -- object, we call the built-in concatenation routines to prevent code
3426 -- bloat, regardless of the optimization level. This is space efficient
3427 -- and prevents linking problems when units are compiled with different
3428 -- optimization levels.
3430 if Atyp = Standard_String
3431 and then NN in 2 .. 9
3432 and then (((Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3433 and then not Debug_Flag_Dot_C)
3434 or else Library_Level_Target)
3435 then
3436 declare
3437 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3438 (RE_Str_Concat_2,
3439 RE_Str_Concat_3,
3440 RE_Str_Concat_4,
3441 RE_Str_Concat_5,
3442 RE_Str_Concat_6,
3443 RE_Str_Concat_7,
3444 RE_Str_Concat_8,
3445 RE_Str_Concat_9);
3447 begin
3448 if RTE_Available (RR (NN)) then
3449 declare
3450 Opnds : constant List_Id :=
3451 New_List (New_Occurrence_Of (Ent, Loc));
3453 begin
3454 for J in 1 .. NN loop
3455 if Is_List_Member (Operands (J)) then
3456 Remove (Operands (J));
3457 end if;
3459 if Base_Type (Etype (Operands (J))) = Ctyp then
3460 Append_To (Opnds,
3461 Make_Aggregate (Loc,
3462 Component_Associations => New_List (
3463 Make_Component_Association (Loc,
3464 Choices => New_List (
3465 Make_Integer_Literal (Loc, 1)),
3466 Expression => Operands (J)))));
3468 else
3469 Append_To (Opnds, Operands (J));
3470 end if;
3471 end loop;
3473 Insert_Action (Cnode,
3474 Make_Procedure_Call_Statement (Loc,
3475 Name => New_Occurrence_Of (RTE (RR (NN)), Loc),
3476 Parameter_Associations => Opnds));
3478 Result := New_Occurrence_Of (Ent, Loc);
3479 goto Done;
3480 end;
3481 end if;
3482 end;
3483 end if;
3485 -- Not special case so generate the assignments
3487 Known_Non_Null_Operand_Seen := False;
3489 for J in 1 .. NN loop
3490 declare
3491 Lo : constant Node_Id :=
3492 Make_Op_Add (Loc,
3493 Left_Opnd => To_Artyp (New_Copy_Tree (Low_Bound)),
3494 Right_Opnd => Aggr_Length (J - 1));
3496 Hi : constant Node_Id :=
3497 Make_Op_Add (Loc,
3498 Left_Opnd => To_Artyp (New_Copy_Tree (Low_Bound)),
3499 Right_Opnd =>
3500 Make_Op_Subtract (Loc,
3501 Left_Opnd => Aggr_Length (J),
3502 Right_Opnd => Make_Artyp_Literal (1)));
3504 begin
3505 -- Singleton case, simple assignment
3507 if Base_Type (Etype (Operands (J))) = Ctyp then
3508 Known_Non_Null_Operand_Seen := True;
3509 Insert_Action (Cnode,
3510 Make_Assignment_Statement (Loc,
3511 Name =>
3512 Make_Indexed_Component (Loc,
3513 Prefix => New_Occurrence_Of (Ent, Loc),
3514 Expressions => New_List (To_Ityp (Lo))),
3515 Expression => Operands (J)),
3516 Suppress => All_Checks);
3518 -- Array case, slice assignment, skipped when argument is fixed
3519 -- length and known to be null.
3521 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3522 declare
3523 Assign : Node_Id :=
3524 Make_Assignment_Statement (Loc,
3525 Name =>
3526 Make_Slice (Loc,
3527 Prefix =>
3528 New_Occurrence_Of (Ent, Loc),
3529 Discrete_Range =>
3530 Make_Range (Loc,
3531 Low_Bound => To_Ityp (Lo),
3532 High_Bound => To_Ityp (Hi))),
3533 Expression => Operands (J));
3534 begin
3535 if Is_Fixed_Length (J) then
3536 Known_Non_Null_Operand_Seen := True;
3538 elsif not Known_Non_Null_Operand_Seen then
3540 -- Here if operand length is not statically known and no
3541 -- operand known to be non-null has been processed yet.
3542 -- If operand length is 0, we do not need to perform the
3543 -- assignment, and we must avoid the evaluation of the
3544 -- high bound of the slice, since it may underflow if the
3545 -- low bound is Ityp'First.
3547 Assign :=
3548 Make_Implicit_If_Statement (Cnode,
3549 Condition =>
3550 Make_Op_Ne (Loc,
3551 Left_Opnd =>
3552 New_Occurrence_Of (Var_Length (J), Loc),
3553 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3554 Then_Statements => New_List (Assign));
3555 end if;
3557 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3558 end;
3559 end if;
3560 end;
3561 end loop;
3563 -- Finally we build the result, which is a reference to the array object
3565 Result := New_Occurrence_Of (Ent, Loc);
3567 <<Done>>
3568 Rewrite (Cnode, Result);
3569 Analyze_And_Resolve (Cnode, Atyp);
3571 exception
3572 when Concatenation_Error =>
3574 -- Kill warning generated for the declaration of the static out of
3575 -- range high bound, and instead generate a Constraint_Error with
3576 -- an appropriate specific message.
3578 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3579 Apply_Compile_Time_Constraint_Error
3580 (N => Cnode,
3581 Msg => "concatenation result upper bound out of range??",
3582 Reason => CE_Range_Check_Failed);
3583 end Expand_Concatenate;
3585 ---------------------------------------------------
3586 -- Expand_Membership_Minimize_Eliminate_Overflow --
3587 ---------------------------------------------------
3589 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is
3590 pragma Assert (Nkind (N) = N_In);
3591 -- Despite the name, this routine applies only to N_In, not to
3592 -- N_Not_In. The latter is always rewritten as not (X in Y).
3594 Result_Type : constant Entity_Id := Etype (N);
3595 -- Capture result type, may be a derived boolean type
3597 Loc : constant Source_Ptr := Sloc (N);
3598 Lop : constant Node_Id := Left_Opnd (N);
3599 Rop : constant Node_Id := Right_Opnd (N);
3601 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3602 -- is thus tempting to capture these values, but due to the rewrites
3603 -- that occur as a result of overflow checking, these values change
3604 -- as we go along, and it is safe just to always use Etype explicitly.
3606 Restype : constant Entity_Id := Etype (N);
3607 -- Save result type
3609 Lo, Hi : Uint;
3610 -- Bounds in Minimize calls, not used currently
3612 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
3613 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3615 begin
3616 Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False);
3618 -- If right operand is a subtype name, and the subtype name has no
3619 -- predicate, then we can just replace the right operand with an
3620 -- explicit range T'First .. T'Last, and use the explicit range code.
3622 if Nkind (Rop) /= N_Range
3623 and then No (Predicate_Function (Etype (Rop)))
3624 then
3625 declare
3626 Rtyp : constant Entity_Id := Etype (Rop);
3627 begin
3628 Rewrite (Rop,
3629 Make_Range (Loc,
3630 Low_Bound =>
3631 Make_Attribute_Reference (Loc,
3632 Attribute_Name => Name_First,
3633 Prefix => New_Occurrence_Of (Rtyp, Loc)),
3634 High_Bound =>
3635 Make_Attribute_Reference (Loc,
3636 Attribute_Name => Name_Last,
3637 Prefix => New_Occurrence_Of (Rtyp, Loc))));
3638 Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks);
3639 end;
3640 end if;
3642 -- Here for the explicit range case. Note that the bounds of the range
3643 -- have not been processed for minimized or eliminated checks.
3645 if Nkind (Rop) = N_Range then
3646 Minimize_Eliminate_Overflows
3647 (Low_Bound (Rop), Lo, Hi, Top_Level => False);
3648 Minimize_Eliminate_Overflows
3649 (High_Bound (Rop), Lo, Hi, Top_Level => False);
3651 -- We have A in B .. C, treated as A >= B and then A <= C
3653 -- Bignum case
3655 if Is_RTE (Etype (Lop), RE_Bignum)
3656 or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum)
3657 or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum)
3658 then
3659 declare
3660 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3661 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3662 L : constant Entity_Id :=
3663 Make_Defining_Identifier (Loc, Name_uL);
3664 Lopnd : constant Node_Id := Convert_To_Bignum (Lop);
3665 Lbound : constant Node_Id :=
3666 Convert_To_Bignum (Low_Bound (Rop));
3667 Hbound : constant Node_Id :=
3668 Convert_To_Bignum (High_Bound (Rop));
3670 -- Now we rewrite the membership test node to look like
3672 -- do
3673 -- Bnn : Result_Type;
3674 -- declare
3675 -- M : Mark_Id := SS_Mark;
3676 -- L : Bignum := Lopnd;
3677 -- begin
3678 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3679 -- SS_Release (M);
3680 -- end;
3681 -- in
3682 -- Bnn
3683 -- end
3685 begin
3686 -- Insert declaration of L into declarations of bignum block
3688 Insert_After
3689 (Last (Declarations (Blk)),
3690 Make_Object_Declaration (Loc,
3691 Defining_Identifier => L,
3692 Object_Definition =>
3693 New_Occurrence_Of (RTE (RE_Bignum), Loc),
3694 Expression => Lopnd));
3696 -- Insert assignment to Bnn into expressions of bignum block
3698 Insert_Before
3699 (First (Statements (Handled_Statement_Sequence (Blk))),
3700 Make_Assignment_Statement (Loc,
3701 Name => New_Occurrence_Of (Bnn, Loc),
3702 Expression =>
3703 Make_And_Then (Loc,
3704 Left_Opnd =>
3705 Make_Function_Call (Loc,
3706 Name =>
3707 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
3708 Parameter_Associations => New_List (
3709 New_Occurrence_Of (L, Loc),
3710 Lbound)),
3712 Right_Opnd =>
3713 Make_Function_Call (Loc,
3714 Name =>
3715 New_Occurrence_Of (RTE (RE_Big_LE), Loc),
3716 Parameter_Associations => New_List (
3717 New_Occurrence_Of (L, Loc),
3718 Hbound)))));
3720 -- Now rewrite the node
3722 Rewrite (N,
3723 Make_Expression_With_Actions (Loc,
3724 Actions => New_List (
3725 Make_Object_Declaration (Loc,
3726 Defining_Identifier => Bnn,
3727 Object_Definition =>
3728 New_Occurrence_Of (Result_Type, Loc)),
3729 Blk),
3730 Expression => New_Occurrence_Of (Bnn, Loc)));
3731 Analyze_And_Resolve (N, Result_Type);
3732 return;
3733 end;
3735 -- Here if no bignums around
3737 else
3738 -- Case where types are all the same
3740 if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop)))
3741 and then
3742 Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop)))
3743 then
3744 null;
3746 -- If types are not all the same, it means that we have rewritten
3747 -- at least one of them to be of type Long_Long_Integer, and we
3748 -- will convert the other operands to Long_Long_Integer.
3750 else
3751 Convert_To_And_Rewrite (LLIB, Lop);
3752 Set_Analyzed (Lop, False);
3753 Analyze_And_Resolve (Lop, LLIB);
3755 -- For the right operand, avoid unnecessary recursion into
3756 -- this routine, we know that overflow is not possible.
3758 Convert_To_And_Rewrite (LLIB, Low_Bound (Rop));
3759 Convert_To_And_Rewrite (LLIB, High_Bound (Rop));
3760 Set_Analyzed (Rop, False);
3761 Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check);
3762 end if;
3764 -- Now the three operands are of the same signed integer type,
3765 -- so we can use the normal expansion routine for membership,
3766 -- setting the flag to prevent recursion into this procedure.
3768 Set_No_Minimize_Eliminate (N);
3769 Expand_N_In (N);
3770 end if;
3772 -- Right operand is a subtype name and the subtype has a predicate. We
3773 -- have to make sure the predicate is checked, and for that we need to
3774 -- use the standard N_In circuitry with appropriate types.
3776 else
3777 pragma Assert (Present (Predicate_Function (Etype (Rop))));
3779 -- If types are "right", just call Expand_N_In preventing recursion
3781 if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then
3782 Set_No_Minimize_Eliminate (N);
3783 Expand_N_In (N);
3785 -- Bignum case
3787 elsif Is_RTE (Etype (Lop), RE_Bignum) then
3789 -- For X in T, we want to rewrite our node as
3791 -- do
3792 -- Bnn : Result_Type;
3794 -- declare
3795 -- M : Mark_Id := SS_Mark;
3796 -- Lnn : Long_Long_Integer'Base
3797 -- Nnn : Bignum;
3799 -- begin
3800 -- Nnn := X;
3802 -- if not Bignum_In_LLI_Range (Nnn) then
3803 -- Bnn := False;
3804 -- else
3805 -- Lnn := From_Bignum (Nnn);
3806 -- Bnn :=
3807 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3808 -- and then T'Base (Lnn) in T;
3809 -- end if;
3811 -- SS_Release (M);
3812 -- end
3813 -- in
3814 -- Bnn
3815 -- end
3817 -- A bit gruesome, but there doesn't seem to be a simpler way
3819 declare
3820 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3821 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3822 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N);
3823 Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N);
3824 T : constant Entity_Id := Etype (Rop);
3825 TB : constant Entity_Id := Base_Type (T);
3826 Nin : Node_Id;
3828 begin
3829 -- Mark the last membership operation to prevent recursion
3831 Nin :=
3832 Make_In (Loc,
3833 Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)),
3834 Right_Opnd => New_Occurrence_Of (T, Loc));
3835 Set_No_Minimize_Eliminate (Nin);
3837 -- Now decorate the block
3839 Insert_After
3840 (Last (Declarations (Blk)),
3841 Make_Object_Declaration (Loc,
3842 Defining_Identifier => Lnn,
3843 Object_Definition => New_Occurrence_Of (LLIB, Loc)));
3845 Insert_After
3846 (Last (Declarations (Blk)),
3847 Make_Object_Declaration (Loc,
3848 Defining_Identifier => Nnn,
3849 Object_Definition =>
3850 New_Occurrence_Of (RTE (RE_Bignum), Loc)));
3852 Insert_List_Before
3853 (First (Statements (Handled_Statement_Sequence (Blk))),
3854 New_List (
3855 Make_Assignment_Statement (Loc,
3856 Name => New_Occurrence_Of (Nnn, Loc),
3857 Expression => Relocate_Node (Lop)),
3859 Make_Implicit_If_Statement (N,
3860 Condition =>
3861 Make_Op_Not (Loc,
3862 Right_Opnd =>
3863 Make_Function_Call (Loc,
3864 Name =>
3865 New_Occurrence_Of
3866 (RTE (RE_Bignum_In_LLI_Range), Loc),
3867 Parameter_Associations => New_List (
3868 New_Occurrence_Of (Nnn, Loc)))),
3870 Then_Statements => New_List (
3871 Make_Assignment_Statement (Loc,
3872 Name => New_Occurrence_Of (Bnn, Loc),
3873 Expression =>
3874 New_Occurrence_Of (Standard_False, Loc))),
3876 Else_Statements => New_List (
3877 Make_Assignment_Statement (Loc,
3878 Name => New_Occurrence_Of (Lnn, Loc),
3879 Expression =>
3880 Make_Function_Call (Loc,
3881 Name =>
3882 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
3883 Parameter_Associations => New_List (
3884 New_Occurrence_Of (Nnn, Loc)))),
3886 Make_Assignment_Statement (Loc,
3887 Name => New_Occurrence_Of (Bnn, Loc),
3888 Expression =>
3889 Make_And_Then (Loc,
3890 Left_Opnd =>
3891 Make_In (Loc,
3892 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3893 Right_Opnd =>
3894 Make_Range (Loc,
3895 Low_Bound =>
3896 Convert_To (LLIB,
3897 Make_Attribute_Reference (Loc,
3898 Attribute_Name => Name_First,
3899 Prefix =>
3900 New_Occurrence_Of (TB, Loc))),
3902 High_Bound =>
3903 Convert_To (LLIB,
3904 Make_Attribute_Reference (Loc,
3905 Attribute_Name => Name_Last,
3906 Prefix =>
3907 New_Occurrence_Of (TB, Loc))))),
3909 Right_Opnd => Nin))))));
3911 -- Now we can do the rewrite
3913 Rewrite (N,
3914 Make_Expression_With_Actions (Loc,
3915 Actions => New_List (
3916 Make_Object_Declaration (Loc,
3917 Defining_Identifier => Bnn,
3918 Object_Definition =>
3919 New_Occurrence_Of (Result_Type, Loc)),
3920 Blk),
3921 Expression => New_Occurrence_Of (Bnn, Loc)));
3922 Analyze_And_Resolve (N, Result_Type);
3923 return;
3924 end;
3926 -- Not bignum case, but types don't match (this means we rewrote the
3927 -- left operand to be Long_Long_Integer).
3929 else
3930 pragma Assert (Base_Type (Etype (Lop)) = LLIB);
3932 -- We rewrite the membership test as (where T is the type with
3933 -- the predicate, i.e. the type of the right operand)
3935 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3936 -- and then T'Base (Lop) in T
3938 declare
3939 T : constant Entity_Id := Etype (Rop);
3940 TB : constant Entity_Id := Base_Type (T);
3941 Nin : Node_Id;
3943 begin
3944 -- The last membership test is marked to prevent recursion
3946 Nin :=
3947 Make_In (Loc,
3948 Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)),
3949 Right_Opnd => New_Occurrence_Of (T, Loc));
3950 Set_No_Minimize_Eliminate (Nin);
3952 -- Now do the rewrite
3954 Rewrite (N,
3955 Make_And_Then (Loc,
3956 Left_Opnd =>
3957 Make_In (Loc,
3958 Left_Opnd => Lop,
3959 Right_Opnd =>
3960 Make_Range (Loc,
3961 Low_Bound =>
3962 Convert_To (LLIB,
3963 Make_Attribute_Reference (Loc,
3964 Attribute_Name => Name_First,
3965 Prefix =>
3966 New_Occurrence_Of (TB, Loc))),
3967 High_Bound =>
3968 Convert_To (LLIB,
3969 Make_Attribute_Reference (Loc,
3970 Attribute_Name => Name_Last,
3971 Prefix =>
3972 New_Occurrence_Of (TB, Loc))))),
3973 Right_Opnd => Nin));
3974 Set_Analyzed (N, False);
3975 Analyze_And_Resolve (N, Restype);
3976 end;
3977 end if;
3978 end if;
3979 end Expand_Membership_Minimize_Eliminate_Overflow;
3981 ---------------------------------
3982 -- Expand_Nonbinary_Modular_Op --
3983 ---------------------------------
3985 procedure Expand_Nonbinary_Modular_Op (N : Node_Id) is
3986 Loc : constant Source_Ptr := Sloc (N);
3987 Typ : constant Entity_Id := Etype (N);
3989 procedure Expand_Modular_Addition;
3990 -- Expand the modular addition, handling the special case of adding a
3991 -- constant.
3993 procedure Expand_Modular_Op;
3994 -- Compute the general rule: (lhs OP rhs) mod Modulus
3996 procedure Expand_Modular_Subtraction;
3997 -- Expand the modular addition, handling the special case of subtracting
3998 -- a constant.
4000 -----------------------------
4001 -- Expand_Modular_Addition --
4002 -----------------------------
4004 procedure Expand_Modular_Addition is
4005 begin
4006 -- If this is not the addition of a constant then compute it using
4007 -- the general rule: (lhs + rhs) mod Modulus
4009 if Nkind (Right_Opnd (N)) /= N_Integer_Literal then
4010 Expand_Modular_Op;
4012 -- If this is an addition of a constant, convert it to a subtraction
4013 -- plus a conditional expression since we can compute it faster than
4014 -- computing the modulus.
4016 -- modMinusRhs = Modulus - rhs
4017 -- if lhs < modMinusRhs then lhs + rhs
4018 -- else lhs - modMinusRhs
4020 else
4021 declare
4022 Mod_Minus_Right : constant Uint :=
4023 Modulus (Typ) - Intval (Right_Opnd (N));
4025 Exprs : constant List_Id := New_List;
4026 Cond_Expr : constant Node_Id := New_Op_Node (N_Op_Lt, Loc);
4027 Then_Expr : constant Node_Id := New_Op_Node (N_Op_Add, Loc);
4028 Else_Expr : constant Node_Id := New_Op_Node (N_Op_Subtract,
4029 Loc);
4030 begin
4031 -- To prevent spurious visibility issues, convert all
4032 -- operands to Standard.Unsigned.
4034 Set_Left_Opnd (Cond_Expr,
4035 Unchecked_Convert_To (Standard_Unsigned,
4036 New_Copy_Tree (Left_Opnd (N))));
4037 Set_Right_Opnd (Cond_Expr,
4038 Make_Integer_Literal (Loc, Mod_Minus_Right));
4039 Append_To (Exprs, Cond_Expr);
4041 Set_Left_Opnd (Then_Expr,
4042 Unchecked_Convert_To (Standard_Unsigned,
4043 New_Copy_Tree (Left_Opnd (N))));
4044 Set_Right_Opnd (Then_Expr,
4045 Make_Integer_Literal (Loc, Intval (Right_Opnd (N))));
4046 Append_To (Exprs, Then_Expr);
4048 Set_Left_Opnd (Else_Expr,
4049 Unchecked_Convert_To (Standard_Unsigned,
4050 New_Copy_Tree (Left_Opnd (N))));
4051 Set_Right_Opnd (Else_Expr,
4052 Make_Integer_Literal (Loc, Mod_Minus_Right));
4053 Append_To (Exprs, Else_Expr);
4055 Rewrite (N,
4056 Unchecked_Convert_To (Typ,
4057 Make_If_Expression (Loc, Expressions => Exprs)));
4058 end;
4059 end if;
4060 end Expand_Modular_Addition;
4062 -----------------------
4063 -- Expand_Modular_Op --
4064 -----------------------
4066 procedure Expand_Modular_Op is
4067 Op_Expr : constant Node_Id := New_Op_Node (Nkind (N), Loc);
4068 Mod_Expr : constant Node_Id := New_Op_Node (N_Op_Mod, Loc);
4070 begin
4071 -- Convert nonbinary modular type operands into integer values. Thus
4072 -- we avoid never-ending loops expanding them, and we also ensure
4073 -- the back end never receives nonbinary modular type expressions.
4075 if Nkind_In (Nkind (N), N_Op_And, N_Op_Or) then
4076 Set_Left_Opnd (Op_Expr,
4077 Unchecked_Convert_To (Standard_Unsigned,
4078 New_Copy_Tree (Left_Opnd (N))));
4079 Set_Right_Opnd (Op_Expr,
4080 Unchecked_Convert_To (Standard_Unsigned,
4081 New_Copy_Tree (Right_Opnd (N))));
4082 Set_Left_Opnd (Mod_Expr,
4083 Unchecked_Convert_To (Standard_Integer, Op_Expr));
4085 else
4086 Set_Left_Opnd (Op_Expr,
4087 Unchecked_Convert_To (Standard_Integer,
4088 New_Copy_Tree (Left_Opnd (N))));
4089 Set_Right_Opnd (Op_Expr,
4090 Unchecked_Convert_To (Standard_Integer,
4091 New_Copy_Tree (Right_Opnd (N))));
4093 -- Link this node to the tree to analyze it
4095 -- If the parent node is an expression with actions we link it to
4096 -- N since otherwise Force_Evaluation cannot identify if this node
4097 -- comes from the Expression and rejects generating the temporary.
4099 if Nkind (Parent (N)) = N_Expression_With_Actions then
4100 Set_Parent (Op_Expr, N);
4102 -- Common case
4104 else
4105 Set_Parent (Op_Expr, Parent (N));
4106 end if;
4108 Analyze (Op_Expr);
4110 -- Force generating a temporary because in the expansion of this
4111 -- expression we may generate code that performs this computation
4112 -- several times.
4114 Force_Evaluation (Op_Expr, Mode => Strict);
4116 Set_Left_Opnd (Mod_Expr, Op_Expr);
4117 end if;
4119 Set_Right_Opnd (Mod_Expr,
4120 Make_Integer_Literal (Loc, Modulus (Typ)));
4122 Rewrite (N,
4123 Unchecked_Convert_To (Typ, Mod_Expr));
4124 end Expand_Modular_Op;
4126 --------------------------------
4127 -- Expand_Modular_Subtraction --
4128 --------------------------------
4130 procedure Expand_Modular_Subtraction is
4131 begin
4132 -- If this is not the addition of a constant then compute it using
4133 -- the general rule: (lhs + rhs) mod Modulus
4135 if Nkind (Right_Opnd (N)) /= N_Integer_Literal then
4136 Expand_Modular_Op;
4138 -- If this is an addition of a constant, convert it to a subtraction
4139 -- plus a conditional expression since we can compute it faster than
4140 -- computing the modulus.
4142 -- modMinusRhs = Modulus - rhs
4143 -- if lhs < rhs then lhs + modMinusRhs
4144 -- else lhs - rhs
4146 else
4147 declare
4148 Mod_Minus_Right : constant Uint :=
4149 Modulus (Typ) - Intval (Right_Opnd (N));
4151 Exprs : constant List_Id := New_List;
4152 Cond_Expr : constant Node_Id := New_Op_Node (N_Op_Lt, Loc);
4153 Then_Expr : constant Node_Id := New_Op_Node (N_Op_Add, Loc);
4154 Else_Expr : constant Node_Id := New_Op_Node (N_Op_Subtract,
4155 Loc);
4156 begin
4157 Set_Left_Opnd (Cond_Expr,
4158 Unchecked_Convert_To (Standard_Unsigned,
4159 New_Copy_Tree (Left_Opnd (N))));
4160 Set_Right_Opnd (Cond_Expr,
4161 Make_Integer_Literal (Loc, Intval (Right_Opnd (N))));
4162 Append_To (Exprs, Cond_Expr);
4164 Set_Left_Opnd (Then_Expr,
4165 Unchecked_Convert_To (Standard_Unsigned,
4166 New_Copy_Tree (Left_Opnd (N))));
4167 Set_Right_Opnd (Then_Expr,
4168 Make_Integer_Literal (Loc, Mod_Minus_Right));
4169 Append_To (Exprs, Then_Expr);
4171 Set_Left_Opnd (Else_Expr,
4172 Unchecked_Convert_To (Standard_Unsigned,
4173 New_Copy_Tree (Left_Opnd (N))));
4174 Set_Right_Opnd (Else_Expr,
4175 Unchecked_Convert_To (Standard_Unsigned,
4176 New_Copy_Tree (Right_Opnd (N))));
4177 Append_To (Exprs, Else_Expr);
4179 Rewrite (N,
4180 Unchecked_Convert_To (Typ,
4181 Make_If_Expression (Loc, Expressions => Exprs)));
4182 end;
4183 end if;
4184 end Expand_Modular_Subtraction;
4186 -- Start of processing for Expand_Nonbinary_Modular_Op
4188 begin
4189 -- No action needed if front-end expansion is not required or if we
4190 -- have a binary modular operand.
4192 if not Expand_Nonbinary_Modular_Ops
4193 or else not Non_Binary_Modulus (Typ)
4194 then
4195 return;
4196 end if;
4198 case Nkind (N) is
4199 when N_Op_Add =>
4200 Expand_Modular_Addition;
4202 when N_Op_Subtract =>
4203 Expand_Modular_Subtraction;
4205 when N_Op_Minus =>
4207 -- Expand -expr into (0 - expr)
4209 Rewrite (N,
4210 Make_Op_Subtract (Loc,
4211 Left_Opnd => Make_Integer_Literal (Loc, 0),
4212 Right_Opnd => Right_Opnd (N)));
4213 Analyze_And_Resolve (N, Typ);
4215 when others =>
4216 Expand_Modular_Op;
4217 end case;
4219 Analyze_And_Resolve (N, Typ);
4220 end Expand_Nonbinary_Modular_Op;
4222 ------------------------
4223 -- Expand_N_Allocator --
4224 ------------------------
4226 procedure Expand_N_Allocator (N : Node_Id) is
4227 Etyp : constant Entity_Id := Etype (Expression (N));
4228 Loc : constant Source_Ptr := Sloc (N);
4229 PtrT : constant Entity_Id := Etype (N);
4231 procedure Rewrite_Coextension (N : Node_Id);
4232 -- Static coextensions have the same lifetime as the entity they
4233 -- constrain. Such occurrences can be rewritten as aliased objects
4234 -- and their unrestricted access used instead of the coextension.
4236 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
4237 -- Given a constrained array type E, returns a node representing the
4238 -- code to compute the size in storage elements for the given type.
4239 -- This is done without using the attribute (which malfunctions for
4240 -- large sizes ???)
4242 -------------------------
4243 -- Rewrite_Coextension --
4244 -------------------------
4246 procedure Rewrite_Coextension (N : Node_Id) is
4247 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
4248 Temp_Decl : Node_Id;
4250 begin
4251 -- Generate:
4252 -- Cnn : aliased Etyp;
4254 Temp_Decl :=
4255 Make_Object_Declaration (Loc,
4256 Defining_Identifier => Temp_Id,
4257 Aliased_Present => True,
4258 Object_Definition => New_Occurrence_Of (Etyp, Loc));
4260 if Nkind (Expression (N)) = N_Qualified_Expression then
4261 Set_Expression (Temp_Decl, Expression (Expression (N)));
4262 end if;
4264 Insert_Action (N, Temp_Decl);
4265 Rewrite (N,
4266 Make_Attribute_Reference (Loc,
4267 Prefix => New_Occurrence_Of (Temp_Id, Loc),
4268 Attribute_Name => Name_Unrestricted_Access));
4270 Analyze_And_Resolve (N, PtrT);
4271 end Rewrite_Coextension;
4273 ------------------------------
4274 -- Size_In_Storage_Elements --
4275 ------------------------------
4277 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
4278 begin
4279 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4280 -- However, the reason for the existence of this function is
4281 -- to construct a test for sizes too large, which means near the
4282 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4283 -- is that we get overflows when sizes are greater than 2**31.
4285 -- So what we end up doing for array types is to use the expression:
4287 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4289 -- which avoids this problem. All this is a bit bogus, but it does
4290 -- mean we catch common cases of trying to allocate arrays that
4291 -- are too large, and which in the absence of a check results in
4292 -- undetected chaos ???
4294 -- Note in particular that this is a pessimistic estimate in the
4295 -- case of packed array types, where an array element might occupy
4296 -- just a fraction of a storage element???
4298 declare
4299 Len : Node_Id;
4300 Res : Node_Id;
4301 pragma Warnings (Off, Res);
4303 begin
4304 for J in 1 .. Number_Dimensions (E) loop
4305 Len :=
4306 Make_Attribute_Reference (Loc,
4307 Prefix => New_Occurrence_Of (E, Loc),
4308 Attribute_Name => Name_Length,
4309 Expressions => New_List (Make_Integer_Literal (Loc, J)));
4311 if J = 1 then
4312 Res := Len;
4314 else
4315 Res :=
4316 Make_Op_Multiply (Loc,
4317 Left_Opnd => Res,
4318 Right_Opnd => Len);
4319 end if;
4320 end loop;
4322 return
4323 Make_Op_Multiply (Loc,
4324 Left_Opnd => Len,
4325 Right_Opnd =>
4326 Make_Attribute_Reference (Loc,
4327 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
4328 Attribute_Name => Name_Max_Size_In_Storage_Elements));
4329 end;
4330 end Size_In_Storage_Elements;
4332 -- Local variables
4334 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
4335 Desig : Entity_Id;
4336 Nod : Node_Id;
4337 Pool : Entity_Id;
4338 Rel_Typ : Entity_Id;
4339 Temp : Entity_Id;
4341 -- Start of processing for Expand_N_Allocator
4343 begin
4344 -- RM E.2.3(22). We enforce that the expected type of an allocator
4345 -- shall not be a remote access-to-class-wide-limited-private type
4347 -- Why is this being done at expansion time, seems clearly wrong ???
4349 Validate_Remote_Access_To_Class_Wide_Type (N);
4351 -- Processing for anonymous access-to-controlled types. These access
4352 -- types receive a special finalization master which appears in the
4353 -- declarations of the enclosing semantic unit. This expansion is done
4354 -- now to ensure that any additional types generated by this routine or
4355 -- Expand_Allocator_Expression inherit the proper type attributes.
4357 if (Ekind (PtrT) = E_Anonymous_Access_Type
4358 or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT))))
4359 and then Needs_Finalization (Dtyp)
4360 then
4361 -- Detect the allocation of an anonymous controlled object where the
4362 -- type of the context is named. For example:
4364 -- procedure Proc (Ptr : Named_Access_Typ);
4365 -- Proc (new Designated_Typ);
4367 -- Regardless of the anonymous-to-named access type conversion, the
4368 -- lifetime of the object must be associated with the named access
4369 -- type. Use the finalization-related attributes of this type.
4371 if Nkind_In (Parent (N), N_Type_Conversion,
4372 N_Unchecked_Type_Conversion)
4373 and then Ekind_In (Etype (Parent (N)), E_Access_Subtype,
4374 E_Access_Type,
4375 E_General_Access_Type)
4376 then
4377 Rel_Typ := Etype (Parent (N));
4378 else
4379 Rel_Typ := Empty;
4380 end if;
4382 -- Anonymous access-to-controlled types allocate on the global pool.
4383 -- Note that this is a "root type only" attribute.
4385 if No (Associated_Storage_Pool (PtrT)) then
4386 if Present (Rel_Typ) then
4387 Set_Associated_Storage_Pool
4388 (Root_Type (PtrT), Associated_Storage_Pool (Rel_Typ));
4389 else
4390 Set_Associated_Storage_Pool
4391 (Root_Type (PtrT), RTE (RE_Global_Pool_Object));
4392 end if;
4393 end if;
4395 -- The finalization master must be inserted and analyzed as part of
4396 -- the current semantic unit. Note that the master is updated when
4397 -- analysis changes current units. Note that this is a "root type
4398 -- only" attribute.
4400 if Present (Rel_Typ) then
4401 Set_Finalization_Master
4402 (Root_Type (PtrT), Finalization_Master (Rel_Typ));
4403 else
4404 Build_Anonymous_Master (Root_Type (PtrT));
4405 end if;
4406 end if;
4408 -- Set the storage pool and find the appropriate version of Allocate to
4409 -- call. Do not overwrite the storage pool if it is already set, which
4410 -- can happen for build-in-place function returns (see
4411 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4413 if No (Storage_Pool (N)) then
4414 Pool := Associated_Storage_Pool (Root_Type (PtrT));
4416 if Present (Pool) then
4417 Set_Storage_Pool (N, Pool);
4419 if Is_RTE (Pool, RE_SS_Pool) then
4420 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4422 -- In the case of an allocator for a simple storage pool, locate
4423 -- and save a reference to the pool type's Allocate routine.
4425 elsif Present (Get_Rep_Pragma
4426 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4427 then
4428 declare
4429 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
4430 Alloc_Op : Entity_Id;
4431 begin
4432 Alloc_Op := Get_Name_Entity_Id (Name_Allocate);
4433 while Present (Alloc_Op) loop
4434 if Scope (Alloc_Op) = Scope (Pool_Type)
4435 and then Present (First_Formal (Alloc_Op))
4436 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4437 then
4438 Set_Procedure_To_Call (N, Alloc_Op);
4439 exit;
4440 else
4441 Alloc_Op := Homonym (Alloc_Op);
4442 end if;
4443 end loop;
4444 end;
4446 elsif Is_Class_Wide_Type (Etype (Pool)) then
4447 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
4449 else
4450 Set_Procedure_To_Call (N,
4451 Find_Prim_Op (Etype (Pool), Name_Allocate));
4452 end if;
4453 end if;
4454 end if;
4456 -- Under certain circumstances we can replace an allocator by an access
4457 -- to statically allocated storage. The conditions, as noted in AARM
4458 -- 3.10 (10c) are as follows:
4460 -- Size and initial value is known at compile time
4461 -- Access type is access-to-constant
4463 -- The allocator is not part of a constraint on a record component,
4464 -- because in that case the inserted actions are delayed until the
4465 -- record declaration is fully analyzed, which is too late for the
4466 -- analysis of the rewritten allocator.
4468 if Is_Access_Constant (PtrT)
4469 and then Nkind (Expression (N)) = N_Qualified_Expression
4470 and then Compile_Time_Known_Value (Expression (Expression (N)))
4471 and then Size_Known_At_Compile_Time
4472 (Etype (Expression (Expression (N))))
4473 and then not Is_Record_Type (Current_Scope)
4474 then
4475 -- Here we can do the optimization. For the allocator
4477 -- new x'(y)
4479 -- We insert an object declaration
4481 -- Tnn : aliased x := y;
4483 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4484 -- marked as requiring static allocation.
4486 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
4487 Desig := Subtype_Mark (Expression (N));
4489 -- If context is constrained, use constrained subtype directly,
4490 -- so that the constant is not labelled as having a nominally
4491 -- unconstrained subtype.
4493 if Entity (Desig) = Base_Type (Dtyp) then
4494 Desig := New_Occurrence_Of (Dtyp, Loc);
4495 end if;
4497 Insert_Action (N,
4498 Make_Object_Declaration (Loc,
4499 Defining_Identifier => Temp,
4500 Aliased_Present => True,
4501 Constant_Present => Is_Access_Constant (PtrT),
4502 Object_Definition => Desig,
4503 Expression => Expression (Expression (N))));
4505 Rewrite (N,
4506 Make_Attribute_Reference (Loc,
4507 Prefix => New_Occurrence_Of (Temp, Loc),
4508 Attribute_Name => Name_Unrestricted_Access));
4510 Analyze_And_Resolve (N, PtrT);
4512 -- We set the variable as statically allocated, since we don't want
4513 -- it going on the stack of the current procedure.
4515 Set_Is_Statically_Allocated (Temp);
4516 return;
4517 end if;
4519 -- Same if the allocator is an access discriminant for a local object:
4520 -- instead of an allocator we create a local value and constrain the
4521 -- enclosing object with the corresponding access attribute.
4523 if Is_Static_Coextension (N) then
4524 Rewrite_Coextension (N);
4525 return;
4526 end if;
4528 -- Check for size too large, we do this because the back end misses
4529 -- proper checks here and can generate rubbish allocation calls when
4530 -- we are near the limit. We only do this for the 32-bit address case
4531 -- since that is from a practical point of view where we see a problem.
4533 if System_Address_Size = 32
4534 and then not Storage_Checks_Suppressed (PtrT)
4535 and then not Storage_Checks_Suppressed (Dtyp)
4536 and then not Storage_Checks_Suppressed (Etyp)
4537 then
4538 -- The check we want to generate should look like
4540 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4541 -- raise Storage_Error;
4542 -- end if;
4544 -- where 3.5 gigabytes is a constant large enough to accommodate any
4545 -- reasonable request for. But we can't do it this way because at
4546 -- least at the moment we don't compute this attribute right, and
4547 -- can silently give wrong results when the result gets large. Since
4548 -- this is all about large results, that's bad, so instead we only
4549 -- apply the check for constrained arrays, and manually compute the
4550 -- value of the attribute ???
4552 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
4553 Insert_Action (N,
4554 Make_Raise_Storage_Error (Loc,
4555 Condition =>
4556 Make_Op_Gt (Loc,
4557 Left_Opnd => Size_In_Storage_Elements (Etyp),
4558 Right_Opnd =>
4559 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
4560 Reason => SE_Object_Too_Large));
4561 end if;
4562 end if;
4564 -- If no storage pool has been specified and we have the restriction
4565 -- No_Standard_Allocators_After_Elaboration is present, then generate
4566 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4568 if Nkind (N) = N_Allocator
4569 and then No (Storage_Pool (N))
4570 and then Restriction_Active (No_Standard_Allocators_After_Elaboration)
4571 then
4572 Insert_Action (N,
4573 Make_Procedure_Call_Statement (Loc,
4574 Name =>
4575 New_Occurrence_Of (RTE (RE_Check_Standard_Allocator), Loc)));
4576 end if;
4578 -- Handle case of qualified expression (other than optimization above)
4579 -- First apply constraint checks, because the bounds or discriminants
4580 -- in the aggregate might not match the subtype mark in the allocator.
4582 if Nkind (Expression (N)) = N_Qualified_Expression then
4583 declare
4584 Exp : constant Node_Id := Expression (Expression (N));
4585 Typ : constant Entity_Id := Etype (Expression (N));
4587 begin
4588 Apply_Constraint_Check (Exp, Typ);
4589 Apply_Predicate_Check (Exp, Typ);
4590 end;
4592 Expand_Allocator_Expression (N);
4593 return;
4594 end if;
4596 -- If the allocator is for a type which requires initialization, and
4597 -- there is no initial value (i.e. operand is a subtype indication
4598 -- rather than a qualified expression), then we must generate a call to
4599 -- the initialization routine using an expressions action node:
4601 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4603 -- Here ptr_T is the pointer type for the allocator, and T is the
4604 -- subtype of the allocator. A special case arises if the designated
4605 -- type of the access type is a task or contains tasks. In this case
4606 -- the call to Init (Temp.all ...) is replaced by code that ensures
4607 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4608 -- for details). In addition, if the type T is a task type, then the
4609 -- first argument to Init must be converted to the task record type.
4611 declare
4612 T : constant Entity_Id := Etype (Expression (N));
4613 Args : List_Id;
4614 Decls : List_Id;
4615 Decl : Node_Id;
4616 Discr : Elmt_Id;
4617 Init : Entity_Id;
4618 Init_Arg1 : Node_Id;
4619 Init_Call : Node_Id;
4620 Temp_Decl : Node_Id;
4621 Temp_Type : Entity_Id;
4623 begin
4624 if No_Initialization (N) then
4626 -- Even though this might be a simple allocation, create a custom
4627 -- Allocate if the context requires it.
4629 if Present (Finalization_Master (PtrT)) then
4630 Build_Allocate_Deallocate_Proc
4631 (N => N,
4632 Is_Allocate => True);
4633 end if;
4635 -- Optimize the default allocation of an array object when pragma
4636 -- Initialize_Scalars or Normalize_Scalars is in effect. Construct an
4637 -- in-place initialization aggregate which may be convert into a fast
4638 -- memset by the backend.
4640 elsif Init_Or_Norm_Scalars
4641 and then Is_Array_Type (T)
4643 -- The array must lack atomic components because they are treated
4644 -- as non-static, and as a result the backend will not initialize
4645 -- the memory in one go.
4647 and then not Has_Atomic_Components (T)
4649 -- The array must not be packed because the invalid values in
4650 -- System.Scalar_Values are multiples of Storage_Unit.
4652 and then not Is_Packed (T)
4654 -- The array must have static non-empty ranges, otherwise the
4655 -- backend cannot initialize the memory in one go.
4657 and then Has_Static_Non_Empty_Array_Bounds (T)
4659 -- The optimization is only relevant for arrays of scalar types
4661 and then Is_Scalar_Type (Component_Type (T))
4663 -- Similar to regular array initialization using a type init proc,
4664 -- predicate checks are not performed because the initialization
4665 -- values are intentionally invalid, and may violate the predicate.
4667 and then not Has_Predicates (Component_Type (T))
4669 -- The component type must have a single initialization value
4671 and then Needs_Simple_Initialization
4672 (Typ => Component_Type (T),
4673 Consider_IS => True)
4674 then
4675 Set_Analyzed (N);
4676 Temp := Make_Temporary (Loc, 'P');
4678 -- Generate:
4679 -- Temp : Ptr_Typ := new ...;
4681 Insert_Action
4682 (Assoc_Node => N,
4683 Ins_Action =>
4684 Make_Object_Declaration (Loc,
4685 Defining_Identifier => Temp,
4686 Object_Definition => New_Occurrence_Of (PtrT, Loc),
4687 Expression => Relocate_Node (N)),
4688 Suppress => All_Checks);
4690 -- Generate:
4691 -- Temp.all := (others => ...);
4693 Insert_Action
4694 (Assoc_Node => N,
4695 Ins_Action =>
4696 Make_Assignment_Statement (Loc,
4697 Name =>
4698 Make_Explicit_Dereference (Loc,
4699 Prefix => New_Occurrence_Of (Temp, Loc)),
4700 Expression =>
4701 Get_Simple_Init_Val
4702 (Typ => T,
4703 N => N,
4704 Size => Esize (Component_Type (T)))),
4705 Suppress => All_Checks);
4707 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4708 Analyze_And_Resolve (N, PtrT);
4710 -- Case of no initialization procedure present
4712 elsif not Has_Non_Null_Base_Init_Proc (T) then
4714 -- Case of simple initialization required
4716 if Needs_Simple_Initialization (T) then
4717 Check_Restriction (No_Default_Initialization, N);
4718 Rewrite (Expression (N),
4719 Make_Qualified_Expression (Loc,
4720 Subtype_Mark => New_Occurrence_Of (T, Loc),
4721 Expression => Get_Simple_Init_Val (T, N)));
4723 Analyze_And_Resolve (Expression (Expression (N)), T);
4724 Analyze_And_Resolve (Expression (N), T);
4725 Set_Paren_Count (Expression (Expression (N)), 1);
4726 Expand_N_Allocator (N);
4728 -- No initialization required
4730 else
4731 Build_Allocate_Deallocate_Proc
4732 (N => N,
4733 Is_Allocate => True);
4734 end if;
4736 -- Case of initialization procedure present, must be called
4738 else
4739 Check_Restriction (No_Default_Initialization, N);
4741 if not Restriction_Active (No_Default_Initialization) then
4742 Init := Base_Init_Proc (T);
4743 Nod := N;
4744 Temp := Make_Temporary (Loc, 'P');
4746 -- Construct argument list for the initialization routine call
4748 Init_Arg1 :=
4749 Make_Explicit_Dereference (Loc,
4750 Prefix =>
4751 New_Occurrence_Of (Temp, Loc));
4753 Set_Assignment_OK (Init_Arg1);
4754 Temp_Type := PtrT;
4756 -- The initialization procedure expects a specific type. if the
4757 -- context is access to class wide, indicate that the object
4758 -- being allocated has the right specific type.
4760 if Is_Class_Wide_Type (Dtyp) then
4761 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
4762 end if;
4764 -- If designated type is a concurrent type or if it is private
4765 -- type whose definition is a concurrent type, the first
4766 -- argument in the Init routine has to be unchecked conversion
4767 -- to the corresponding record type. If the designated type is
4768 -- a derived type, also convert the argument to its root type.
4770 if Is_Concurrent_Type (T) then
4771 Init_Arg1 :=
4772 Unchecked_Convert_To (
4773 Corresponding_Record_Type (T), Init_Arg1);
4775 elsif Is_Private_Type (T)
4776 and then Present (Full_View (T))
4777 and then Is_Concurrent_Type (Full_View (T))
4778 then
4779 Init_Arg1 :=
4780 Unchecked_Convert_To
4781 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
4783 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
4784 declare
4785 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
4787 begin
4788 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
4789 Set_Etype (Init_Arg1, Ftyp);
4790 end;
4791 end if;
4793 Args := New_List (Init_Arg1);
4795 -- For the task case, pass the Master_Id of the access type as
4796 -- the value of the _Master parameter, and _Chain as the value
4797 -- of the _Chain parameter (_Chain will be defined as part of
4798 -- the generated code for the allocator).
4800 -- In Ada 2005, the context may be a function that returns an
4801 -- anonymous access type. In that case the Master_Id has been
4802 -- created when expanding the function declaration.
4804 if Has_Task (T) then
4805 if No (Master_Id (Base_Type (PtrT))) then
4807 -- The designated type was an incomplete type, and the
4808 -- access type did not get expanded. Salvage it now.
4810 if not Restriction_Active (No_Task_Hierarchy) then
4811 if Present (Parent (Base_Type (PtrT))) then
4812 Expand_N_Full_Type_Declaration
4813 (Parent (Base_Type (PtrT)));
4815 -- The only other possibility is an itype. For this
4816 -- case, the master must exist in the context. This is
4817 -- the case when the allocator initializes an access
4818 -- component in an init-proc.
4820 else
4821 pragma Assert (Is_Itype (PtrT));
4822 Build_Master_Renaming (PtrT, N);
4823 end if;
4824 end if;
4825 end if;
4827 -- If the context of the allocator is a declaration or an
4828 -- assignment, we can generate a meaningful image for it,
4829 -- even though subsequent assignments might remove the
4830 -- connection between task and entity. We build this image
4831 -- when the left-hand side is a simple variable, a simple
4832 -- indexed assignment or a simple selected component.
4834 if Nkind (Parent (N)) = N_Assignment_Statement then
4835 declare
4836 Nam : constant Node_Id := Name (Parent (N));
4838 begin
4839 if Is_Entity_Name (Nam) then
4840 Decls :=
4841 Build_Task_Image_Decls
4842 (Loc,
4843 New_Occurrence_Of
4844 (Entity (Nam), Sloc (Nam)), T);
4846 elsif Nkind_In (Nam, N_Indexed_Component,
4847 N_Selected_Component)
4848 and then Is_Entity_Name (Prefix (Nam))
4849 then
4850 Decls :=
4851 Build_Task_Image_Decls
4852 (Loc, Nam, Etype (Prefix (Nam)));
4853 else
4854 Decls := Build_Task_Image_Decls (Loc, T, T);
4855 end if;
4856 end;
4858 elsif Nkind (Parent (N)) = N_Object_Declaration then
4859 Decls :=
4860 Build_Task_Image_Decls
4861 (Loc, Defining_Identifier (Parent (N)), T);
4863 else
4864 Decls := Build_Task_Image_Decls (Loc, T, T);
4865 end if;
4867 if Restriction_Active (No_Task_Hierarchy) then
4868 Append_To (Args,
4869 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
4870 else
4871 Append_To (Args,
4872 New_Occurrence_Of
4873 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
4874 end if;
4876 Append_To (Args, Make_Identifier (Loc, Name_uChain));
4878 Decl := Last (Decls);
4879 Append_To (Args,
4880 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
4882 -- Has_Task is false, Decls not used
4884 else
4885 Decls := No_List;
4886 end if;
4888 -- Add discriminants if discriminated type
4890 declare
4891 Dis : Boolean := False;
4892 Typ : Entity_Id := Empty;
4894 begin
4895 if Has_Discriminants (T) then
4896 Dis := True;
4897 Typ := T;
4899 -- Type may be a private type with no visible discriminants
4900 -- in which case check full view if in scope, or the
4901 -- underlying_full_view if dealing with a type whose full
4902 -- view may be derived from a private type whose own full
4903 -- view has discriminants.
4905 elsif Is_Private_Type (T) then
4906 if Present (Full_View (T))
4907 and then Has_Discriminants (Full_View (T))
4908 then
4909 Dis := True;
4910 Typ := Full_View (T);
4912 elsif Present (Underlying_Full_View (T))
4913 and then Has_Discriminants (Underlying_Full_View (T))
4914 then
4915 Dis := True;
4916 Typ := Underlying_Full_View (T);
4917 end if;
4918 end if;
4920 if Dis then
4922 -- If the allocated object will be constrained by the
4923 -- default values for discriminants, then build a subtype
4924 -- with those defaults, and change the allocated subtype
4925 -- to that. Note that this happens in fewer cases in Ada
4926 -- 2005 (AI-363).
4928 if not Is_Constrained (Typ)
4929 and then Present (Discriminant_Default_Value
4930 (First_Discriminant (Typ)))
4931 and then (Ada_Version < Ada_2005
4932 or else not
4933 Object_Type_Has_Constrained_Partial_View
4934 (Typ, Current_Scope))
4935 then
4936 Typ := Build_Default_Subtype (Typ, N);
4937 Set_Expression (N, New_Occurrence_Of (Typ, Loc));
4938 end if;
4940 Discr := First_Elmt (Discriminant_Constraint (Typ));
4941 while Present (Discr) loop
4942 Nod := Node (Discr);
4943 Append (New_Copy_Tree (Node (Discr)), Args);
4945 -- AI-416: when the discriminant constraint is an
4946 -- anonymous access type make sure an accessibility
4947 -- check is inserted if necessary (3.10.2(22.q/2))
4949 if Ada_Version >= Ada_2005
4950 and then
4951 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
4952 then
4953 Apply_Accessibility_Check
4954 (Nod, Typ, Insert_Node => Nod);
4955 end if;
4957 Next_Elmt (Discr);
4958 end loop;
4959 end if;
4960 end;
4962 -- We set the allocator as analyzed so that when we analyze
4963 -- the if expression node, we do not get an unwanted recursive
4964 -- expansion of the allocator expression.
4966 Set_Analyzed (N, True);
4967 Nod := Relocate_Node (N);
4969 -- Here is the transformation:
4970 -- input: new Ctrl_Typ
4971 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4972 -- Ctrl_TypIP (Temp.all, ...);
4973 -- [Deep_]Initialize (Temp.all);
4975 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4976 -- is the subtype of the allocator.
4978 Temp_Decl :=
4979 Make_Object_Declaration (Loc,
4980 Defining_Identifier => Temp,
4981 Constant_Present => True,
4982 Object_Definition => New_Occurrence_Of (Temp_Type, Loc),
4983 Expression => Nod);
4985 Set_Assignment_OK (Temp_Decl);
4986 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
4988 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
4990 -- If the designated type is a task type or contains tasks,
4991 -- create block to activate created tasks, and insert
4992 -- declaration for Task_Image variable ahead of call.
4994 if Has_Task (T) then
4995 declare
4996 L : constant List_Id := New_List;
4997 Blk : Node_Id;
4998 begin
4999 Build_Task_Allocate_Block (L, Nod, Args);
5000 Blk := Last (L);
5001 Insert_List_Before (First (Declarations (Blk)), Decls);
5002 Insert_Actions (N, L);
5003 end;
5005 else
5006 Insert_Action (N,
5007 Make_Procedure_Call_Statement (Loc,
5008 Name => New_Occurrence_Of (Init, Loc),
5009 Parameter_Associations => Args));
5010 end if;
5012 if Needs_Finalization (T) then
5014 -- Generate:
5015 -- [Deep_]Initialize (Init_Arg1);
5017 Init_Call :=
5018 Make_Init_Call
5019 (Obj_Ref => New_Copy_Tree (Init_Arg1),
5020 Typ => T);
5022 -- Guard against a missing [Deep_]Initialize when the
5023 -- designated type was not properly frozen.
5025 if Present (Init_Call) then
5026 Insert_Action (N, Init_Call);
5027 end if;
5028 end if;
5030 Rewrite (N, New_Occurrence_Of (Temp, Loc));
5031 Analyze_And_Resolve (N, PtrT);
5032 end if;
5033 end if;
5034 end;
5036 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
5037 -- object that has been rewritten as a reference, we displace "this"
5038 -- to reference properly its secondary dispatch table.
5040 if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then
5041 Displace_Allocator_Pointer (N);
5042 end if;
5044 exception
5045 when RE_Not_Available =>
5046 return;
5047 end Expand_N_Allocator;
5049 -----------------------
5050 -- Expand_N_And_Then --
5051 -----------------------
5053 procedure Expand_N_And_Then (N : Node_Id)
5054 renames Expand_Short_Circuit_Operator;
5056 ------------------------------
5057 -- Expand_N_Case_Expression --
5058 ------------------------------
5060 procedure Expand_N_Case_Expression (N : Node_Id) is
5062 function Is_Copy_Type (Typ : Entity_Id) return Boolean;
5063 -- Return True if we can copy objects of this type when expanding a case
5064 -- expression.
5066 ------------------
5067 -- Is_Copy_Type --
5068 ------------------
5070 function Is_Copy_Type (Typ : Entity_Id) return Boolean is
5071 begin
5072 -- If Minimize_Expression_With_Actions is True, we can afford to copy
5073 -- large objects, as long as they are constrained and not limited.
5075 return
5076 Is_Elementary_Type (Underlying_Type (Typ))
5077 or else
5078 (Minimize_Expression_With_Actions
5079 and then Is_Constrained (Underlying_Type (Typ))
5080 and then not Is_Limited_View (Underlying_Type (Typ)));
5081 end Is_Copy_Type;
5083 -- Local variables
5085 Loc : constant Source_Ptr := Sloc (N);
5086 Par : constant Node_Id := Parent (N);
5087 Typ : constant Entity_Id := Etype (N);
5089 Acts : List_Id;
5090 Alt : Node_Id;
5091 Case_Stmt : Node_Id;
5092 Decl : Node_Id;
5093 Expr : Node_Id;
5094 Target : Entity_Id;
5095 Target_Typ : Entity_Id;
5097 In_Predicate : Boolean := False;
5098 -- Flag set when the case expression appears within a predicate
5100 Optimize_Return_Stmt : Boolean := False;
5101 -- Flag set when the case expression can be optimized in the context of
5102 -- a simple return statement.
5104 -- Start of processing for Expand_N_Case_Expression
5106 begin
5107 -- Check for MINIMIZED/ELIMINATED overflow mode
5109 if Minimized_Eliminated_Overflow_Check (N) then
5110 Apply_Arithmetic_Overflow_Check (N);
5111 return;
5112 end if;
5114 -- If the case expression is a predicate specification, and the type
5115 -- to which it applies has a static predicate aspect, do not expand,
5116 -- because it will be converted to the proper predicate form later.
5118 if Ekind_In (Current_Scope, E_Function, E_Procedure)
5119 and then Is_Predicate_Function (Current_Scope)
5120 then
5121 In_Predicate := True;
5123 if Has_Static_Predicate_Aspect (Etype (First_Entity (Current_Scope)))
5124 then
5125 return;
5126 end if;
5127 end if;
5129 -- When the type of the case expression is elementary, expand
5131 -- (case X is when A => AX, when B => BX ...)
5133 -- into
5135 -- do
5136 -- Target : Typ;
5137 -- case X is
5138 -- when A =>
5139 -- Target := AX;
5140 -- when B =>
5141 -- Target := BX;
5142 -- ...
5143 -- end case;
5144 -- in Target end;
5146 -- In all other cases expand into
5148 -- do
5149 -- type Ptr_Typ is access all Typ;
5150 -- Target : Ptr_Typ;
5151 -- case X is
5152 -- when A =>
5153 -- Target := AX'Unrestricted_Access;
5154 -- when B =>
5155 -- Target := BX'Unrestricted_Access;
5156 -- ...
5157 -- end case;
5158 -- in Target.all end;
5160 -- This approach avoids extra copies of potentially large objects. It
5161 -- also allows handling of values of limited or unconstrained types.
5162 -- Note that we do the copy also for constrained, nonlimited types
5163 -- when minimizing expressions with actions (e.g. when generating C
5164 -- code) since it allows us to do the optimization below in more cases.
5166 -- Small optimization: when the case expression appears in the context
5167 -- of a simple return statement, expand into
5169 -- case X is
5170 -- when A =>
5171 -- return AX;
5172 -- when B =>
5173 -- return BX;
5174 -- ...
5175 -- end case;
5177 Case_Stmt :=
5178 Make_Case_Statement (Loc,
5179 Expression => Expression (N),
5180 Alternatives => New_List);
5182 -- Preserve the original context for which the case statement is being
5183 -- generated. This is needed by the finalization machinery to prevent
5184 -- the premature finalization of controlled objects found within the
5185 -- case statement.
5187 Set_From_Conditional_Expression (Case_Stmt);
5188 Acts := New_List;
5190 -- Scalar/Copy case
5192 if Is_Copy_Type (Typ) then
5193 Target_Typ := Typ;
5195 -- ??? Do not perform the optimization when the return statement is
5196 -- within a predicate function, as this causes spurious errors. Could
5197 -- this be a possible mismatch in handling this case somewhere else
5198 -- in semantic analysis?
5200 Optimize_Return_Stmt :=
5201 Nkind (Par) = N_Simple_Return_Statement and then not In_Predicate;
5203 -- Otherwise create an access type to handle the general case using
5204 -- 'Unrestricted_Access.
5206 -- Generate:
5207 -- type Ptr_Typ is access all Typ;
5209 else
5210 if Generate_C_Code then
5212 -- We cannot ensure that correct C code will be generated if any
5213 -- temporary is created down the line (to e.g. handle checks or
5214 -- capture values) since we might end up with dangling references
5215 -- to local variables, so better be safe and reject the construct.
5217 Error_Msg_N
5218 ("case expression too complex, use case statement instead", N);
5219 end if;
5221 Target_Typ := Make_Temporary (Loc, 'P');
5223 Append_To (Acts,
5224 Make_Full_Type_Declaration (Loc,
5225 Defining_Identifier => Target_Typ,
5226 Type_Definition =>
5227 Make_Access_To_Object_Definition (Loc,
5228 All_Present => True,
5229 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
5230 end if;
5232 -- Create the declaration of the target which captures the value of the
5233 -- expression.
5235 -- Generate:
5236 -- Target : [Ptr_]Typ;
5238 if not Optimize_Return_Stmt then
5239 Target := Make_Temporary (Loc, 'T');
5241 Decl :=
5242 Make_Object_Declaration (Loc,
5243 Defining_Identifier => Target,
5244 Object_Definition => New_Occurrence_Of (Target_Typ, Loc));
5245 Set_No_Initialization (Decl);
5247 Append_To (Acts, Decl);
5248 end if;
5250 -- Process the alternatives
5252 Alt := First (Alternatives (N));
5253 while Present (Alt) loop
5254 declare
5255 Alt_Expr : Node_Id := Expression (Alt);
5256 Alt_Loc : constant Source_Ptr := Sloc (Alt_Expr);
5257 Stmts : List_Id;
5259 begin
5260 -- Take the unrestricted access of the expression value for non-
5261 -- scalar types. This approach avoids big copies and covers the
5262 -- limited and unconstrained cases.
5264 -- Generate:
5265 -- AX'Unrestricted_Access
5267 if not Is_Copy_Type (Typ) then
5268 Alt_Expr :=
5269 Make_Attribute_Reference (Alt_Loc,
5270 Prefix => Relocate_Node (Alt_Expr),
5271 Attribute_Name => Name_Unrestricted_Access);
5272 end if;
5274 -- Generate:
5275 -- return AX['Unrestricted_Access];
5277 if Optimize_Return_Stmt then
5278 Stmts := New_List (
5279 Make_Simple_Return_Statement (Alt_Loc,
5280 Expression => Alt_Expr));
5282 -- Generate:
5283 -- Target := AX['Unrestricted_Access];
5285 else
5286 Stmts := New_List (
5287 Make_Assignment_Statement (Alt_Loc,
5288 Name => New_Occurrence_Of (Target, Loc),
5289 Expression => Alt_Expr));
5290 end if;
5292 -- Propagate declarations inserted in the node by Insert_Actions
5293 -- (for example, temporaries generated to remove side effects).
5294 -- These actions must remain attached to the alternative, given
5295 -- that they are generated by the corresponding expression.
5297 if Present (Actions (Alt)) then
5298 Prepend_List (Actions (Alt), Stmts);
5299 end if;
5301 -- Finalize any transient objects on exit from the alternative.
5302 -- This is done only in the return optimization case because
5303 -- otherwise the case expression is converted into an expression
5304 -- with actions which already contains this form of processing.
5306 if Optimize_Return_Stmt then
5307 Process_If_Case_Statements (N, Stmts);
5308 end if;
5310 Append_To
5311 (Alternatives (Case_Stmt),
5312 Make_Case_Statement_Alternative (Sloc (Alt),
5313 Discrete_Choices => Discrete_Choices (Alt),
5314 Statements => Stmts));
5315 end;
5317 Next (Alt);
5318 end loop;
5320 -- Rewrite the parent return statement as a case statement
5322 if Optimize_Return_Stmt then
5323 Rewrite (Par, Case_Stmt);
5324 Analyze (Par);
5326 -- Otherwise convert the case expression into an expression with actions
5328 else
5329 Append_To (Acts, Case_Stmt);
5331 if Is_Copy_Type (Typ) then
5332 Expr := New_Occurrence_Of (Target, Loc);
5334 else
5335 Expr :=
5336 Make_Explicit_Dereference (Loc,
5337 Prefix => New_Occurrence_Of (Target, Loc));
5338 end if;
5340 -- Generate:
5341 -- do
5342 -- ...
5343 -- in Target[.all] end;
5345 Rewrite (N,
5346 Make_Expression_With_Actions (Loc,
5347 Expression => Expr,
5348 Actions => Acts));
5350 Analyze_And_Resolve (N, Typ);
5351 end if;
5352 end Expand_N_Case_Expression;
5354 -----------------------------------
5355 -- Expand_N_Explicit_Dereference --
5356 -----------------------------------
5358 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
5359 begin
5360 -- Insert explicit dereference call for the checked storage pool case
5362 Insert_Dereference_Action (Prefix (N));
5364 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5365 -- we set the atomic sync flag.
5367 if Is_Atomic (Etype (N))
5368 and then not Atomic_Synchronization_Disabled (Etype (N))
5369 then
5370 Activate_Atomic_Synchronization (N);
5371 end if;
5372 end Expand_N_Explicit_Dereference;
5374 --------------------------------------
5375 -- Expand_N_Expression_With_Actions --
5376 --------------------------------------
5378 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
5379 Acts : constant List_Id := Actions (N);
5381 procedure Force_Boolean_Evaluation (Expr : Node_Id);
5382 -- Force the evaluation of Boolean expression Expr
5384 function Process_Action (Act : Node_Id) return Traverse_Result;
5385 -- Inspect and process a single action of an expression_with_actions for
5386 -- transient objects. If such objects are found, the routine generates
5387 -- code to clean them up when the context of the expression is evaluated
5388 -- or elaborated.
5390 ------------------------------
5391 -- Force_Boolean_Evaluation --
5392 ------------------------------
5394 procedure Force_Boolean_Evaluation (Expr : Node_Id) is
5395 Loc : constant Source_Ptr := Sloc (N);
5396 Flag_Decl : Node_Id;
5397 Flag_Id : Entity_Id;
5399 begin
5400 -- Relocate the expression to the actions list by capturing its value
5401 -- in a Boolean flag. Generate:
5402 -- Flag : constant Boolean := Expr;
5404 Flag_Id := Make_Temporary (Loc, 'F');
5406 Flag_Decl :=
5407 Make_Object_Declaration (Loc,
5408 Defining_Identifier => Flag_Id,
5409 Constant_Present => True,
5410 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
5411 Expression => Relocate_Node (Expr));
5413 Append (Flag_Decl, Acts);
5414 Analyze (Flag_Decl);
5416 -- Replace the expression with a reference to the flag
5418 Rewrite (Expression (N), New_Occurrence_Of (Flag_Id, Loc));
5419 Analyze (Expression (N));
5420 end Force_Boolean_Evaluation;
5422 --------------------
5423 -- Process_Action --
5424 --------------------
5426 function Process_Action (Act : Node_Id) return Traverse_Result is
5427 begin
5428 if Nkind (Act) = N_Object_Declaration
5429 and then Is_Finalizable_Transient (Act, N)
5430 then
5431 Process_Transient_In_Expression (Act, N, Acts);
5432 return Skip;
5434 -- Avoid processing temporary function results multiple times when
5435 -- dealing with nested expression_with_actions.
5437 elsif Nkind (Act) = N_Expression_With_Actions then
5438 return Abandon;
5440 -- Do not process temporary function results in loops. This is done
5441 -- by Expand_N_Loop_Statement and Build_Finalizer.
5443 elsif Nkind (Act) = N_Loop_Statement then
5444 return Abandon;
5445 end if;
5447 return OK;
5448 end Process_Action;
5450 procedure Process_Single_Action is new Traverse_Proc (Process_Action);
5452 -- Local variables
5454 Act : Node_Id;
5456 -- Start of processing for Expand_N_Expression_With_Actions
5458 begin
5459 -- Do not evaluate the expression when it denotes an entity because the
5460 -- expression_with_actions node will be replaced by the reference.
5462 if Is_Entity_Name (Expression (N)) then
5463 null;
5465 -- Do not evaluate the expression when there are no actions because the
5466 -- expression_with_actions node will be replaced by the expression.
5468 elsif No (Acts) or else Is_Empty_List (Acts) then
5469 null;
5471 -- Force the evaluation of the expression by capturing its value in a
5472 -- temporary. This ensures that aliases of transient objects do not leak
5473 -- to the expression of the expression_with_actions node:
5475 -- do
5476 -- Trans_Id : Ctrl_Typ := ...;
5477 -- Alias : ... := Trans_Id;
5478 -- in ... Alias ... end;
5480 -- In the example above, Trans_Id cannot be finalized at the end of the
5481 -- actions list because this may affect the alias and the final value of
5482 -- the expression_with_actions. Forcing the evaluation encapsulates the
5483 -- reference to the Alias within the actions list:
5485 -- do
5486 -- Trans_Id : Ctrl_Typ := ...;
5487 -- Alias : ... := Trans_Id;
5488 -- Val : constant Boolean := ... Alias ...;
5489 -- <finalize Trans_Id>
5490 -- in Val end;
5492 -- Once this transformation is performed, it is safe to finalize the
5493 -- transient object at the end of the actions list.
5495 -- Note that Force_Evaluation does not remove side effects in operators
5496 -- because it assumes that all operands are evaluated and side effect
5497 -- free. This is not the case when an operand depends implicitly on the
5498 -- transient object through the use of access types.
5500 elsif Is_Boolean_Type (Etype (Expression (N))) then
5501 Force_Boolean_Evaluation (Expression (N));
5503 -- The expression of an expression_with_actions node may not necessarily
5504 -- be Boolean when the node appears in an if expression. In this case do
5505 -- the usual forced evaluation to encapsulate potential aliasing.
5507 else
5508 Force_Evaluation (Expression (N));
5509 end if;
5511 -- Process all transient objects found within the actions of the EWA
5512 -- node.
5514 Act := First (Acts);
5515 while Present (Act) loop
5516 Process_Single_Action (Act);
5517 Next (Act);
5518 end loop;
5520 -- Deal with case where there are no actions. In this case we simply
5521 -- rewrite the node with its expression since we don't need the actions
5522 -- and the specification of this node does not allow a null action list.
5524 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5525 -- the expanded tree and relying on being able to retrieve the original
5526 -- tree in cases like this. This raises a whole lot of issues of whether
5527 -- we have problems elsewhere, which will be addressed in the future???
5529 if Is_Empty_List (Acts) then
5530 Rewrite (N, Relocate_Node (Expression (N)));
5531 end if;
5532 end Expand_N_Expression_With_Actions;
5534 ----------------------------
5535 -- Expand_N_If_Expression --
5536 ----------------------------
5538 -- Deal with limited types and condition actions
5540 procedure Expand_N_If_Expression (N : Node_Id) is
5541 Cond : constant Node_Id := First (Expressions (N));
5542 Loc : constant Source_Ptr := Sloc (N);
5543 Thenx : constant Node_Id := Next (Cond);
5544 Elsex : constant Node_Id := Next (Thenx);
5545 Typ : constant Entity_Id := Etype (N);
5547 Actions : List_Id;
5548 Decl : Node_Id;
5549 Expr : Node_Id;
5550 New_If : Node_Id;
5551 New_N : Node_Id;
5553 begin
5554 -- Check for MINIMIZED/ELIMINATED overflow mode
5556 if Minimized_Eliminated_Overflow_Check (N) then
5557 Apply_Arithmetic_Overflow_Check (N);
5558 return;
5559 end if;
5561 -- Fold at compile time if condition known. We have already folded
5562 -- static if expressions, but it is possible to fold any case in which
5563 -- the condition is known at compile time, even though the result is
5564 -- non-static.
5566 -- Note that we don't do the fold of such cases in Sem_Elab because
5567 -- it can cause infinite loops with the expander adding a conditional
5568 -- expression, and Sem_Elab circuitry removing it repeatedly.
5570 if Compile_Time_Known_Value (Cond) then
5571 declare
5572 function Fold_Known_Value (Cond : Node_Id) return Boolean;
5573 -- Fold at compile time. Assumes condition known. Return True if
5574 -- folding occurred, meaning we're done.
5576 ----------------------
5577 -- Fold_Known_Value --
5578 ----------------------
5580 function Fold_Known_Value (Cond : Node_Id) return Boolean is
5581 begin
5582 if Is_True (Expr_Value (Cond)) then
5583 Expr := Thenx;
5584 Actions := Then_Actions (N);
5585 else
5586 Expr := Elsex;
5587 Actions := Else_Actions (N);
5588 end if;
5590 Remove (Expr);
5592 if Present (Actions) then
5594 -- To minimize the use of Expression_With_Actions, just skip
5595 -- the optimization as it is not critical for correctness.
5597 if Minimize_Expression_With_Actions then
5598 return False;
5599 end if;
5601 Rewrite (N,
5602 Make_Expression_With_Actions (Loc,
5603 Expression => Relocate_Node (Expr),
5604 Actions => Actions));
5605 Analyze_And_Resolve (N, Typ);
5607 else
5608 Rewrite (N, Relocate_Node (Expr));
5609 end if;
5611 -- Note that the result is never static (legitimate cases of
5612 -- static if expressions were folded in Sem_Eval).
5614 Set_Is_Static_Expression (N, False);
5615 return True;
5616 end Fold_Known_Value;
5618 begin
5619 if Fold_Known_Value (Cond) then
5620 return;
5621 end if;
5622 end;
5623 end if;
5625 -- If the type is limited, and the back end does not handle limited
5626 -- types, then we expand as follows to avoid the possibility of
5627 -- improper copying.
5629 -- type Ptr is access all Typ;
5630 -- Cnn : Ptr;
5631 -- if cond then
5632 -- <<then actions>>
5633 -- Cnn := then-expr'Unrestricted_Access;
5634 -- else
5635 -- <<else actions>>
5636 -- Cnn := else-expr'Unrestricted_Access;
5637 -- end if;
5639 -- and replace the if expression by a reference to Cnn.all.
5641 -- This special case can be skipped if the back end handles limited
5642 -- types properly and ensures that no incorrect copies are made.
5644 if Is_By_Reference_Type (Typ)
5645 and then not Back_End_Handles_Limited_Types
5646 then
5647 -- When the "then" or "else" expressions involve controlled function
5648 -- calls, generated temporaries are chained on the corresponding list
5649 -- of actions. These temporaries need to be finalized after the if
5650 -- expression is evaluated.
5652 Process_If_Case_Statements (N, Then_Actions (N));
5653 Process_If_Case_Statements (N, Else_Actions (N));
5655 declare
5656 Cnn : constant Entity_Id := Make_Temporary (Loc, 'C', N);
5657 Ptr_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
5659 begin
5660 -- Generate:
5661 -- type Ann is access all Typ;
5663 Insert_Action (N,
5664 Make_Full_Type_Declaration (Loc,
5665 Defining_Identifier => Ptr_Typ,
5666 Type_Definition =>
5667 Make_Access_To_Object_Definition (Loc,
5668 All_Present => True,
5669 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
5671 -- Generate:
5672 -- Cnn : Ann;
5674 Decl :=
5675 Make_Object_Declaration (Loc,
5676 Defining_Identifier => Cnn,
5677 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
5679 -- Generate:
5680 -- if Cond then
5681 -- Cnn := <Thenx>'Unrestricted_Access;
5682 -- else
5683 -- Cnn := <Elsex>'Unrestricted_Access;
5684 -- end if;
5686 New_If :=
5687 Make_Implicit_If_Statement (N,
5688 Condition => Relocate_Node (Cond),
5689 Then_Statements => New_List (
5690 Make_Assignment_Statement (Sloc (Thenx),
5691 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
5692 Expression =>
5693 Make_Attribute_Reference (Loc,
5694 Prefix => Relocate_Node (Thenx),
5695 Attribute_Name => Name_Unrestricted_Access))),
5697 Else_Statements => New_List (
5698 Make_Assignment_Statement (Sloc (Elsex),
5699 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
5700 Expression =>
5701 Make_Attribute_Reference (Loc,
5702 Prefix => Relocate_Node (Elsex),
5703 Attribute_Name => Name_Unrestricted_Access))));
5705 -- Preserve the original context for which the if statement is
5706 -- being generated. This is needed by the finalization machinery
5707 -- to prevent the premature finalization of controlled objects
5708 -- found within the if statement.
5710 Set_From_Conditional_Expression (New_If);
5712 New_N :=
5713 Make_Explicit_Dereference (Loc,
5714 Prefix => New_Occurrence_Of (Cnn, Loc));
5715 end;
5717 -- If the result is an unconstrained array and the if expression is in a
5718 -- context other than the initializing expression of the declaration of
5719 -- an object, then we pull out the if expression as follows:
5721 -- Cnn : constant typ := if-expression
5723 -- and then replace the if expression with an occurrence of Cnn. This
5724 -- avoids the need in the back end to create on-the-fly variable length
5725 -- temporaries (which it cannot do!)
5727 -- Note that the test for being in an object declaration avoids doing an
5728 -- unnecessary expansion, and also avoids infinite recursion.
5730 elsif Is_Array_Type (Typ) and then not Is_Constrained (Typ)
5731 and then (Nkind (Parent (N)) /= N_Object_Declaration
5732 or else Expression (Parent (N)) /= N)
5733 then
5734 declare
5735 Cnn : constant Node_Id := Make_Temporary (Loc, 'C', N);
5737 begin
5738 Insert_Action (N,
5739 Make_Object_Declaration (Loc,
5740 Defining_Identifier => Cnn,
5741 Constant_Present => True,
5742 Object_Definition => New_Occurrence_Of (Typ, Loc),
5743 Expression => Relocate_Node (N),
5744 Has_Init_Expression => True));
5746 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
5747 return;
5748 end;
5750 -- For other types, we only need to expand if there are other actions
5751 -- associated with either branch.
5753 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
5755 -- We now wrap the actions into the appropriate expression
5757 if Minimize_Expression_With_Actions
5758 and then (Is_Elementary_Type (Underlying_Type (Typ))
5759 or else Is_Constrained (Underlying_Type (Typ)))
5760 then
5761 -- If we can't use N_Expression_With_Actions nodes, then we insert
5762 -- the following sequence of actions (using Insert_Actions):
5764 -- Cnn : typ;
5765 -- if cond then
5766 -- <<then actions>>
5767 -- Cnn := then-expr;
5768 -- else
5769 -- <<else actions>>
5770 -- Cnn := else-expr
5771 -- end if;
5773 -- and replace the if expression by a reference to Cnn
5775 declare
5776 Cnn : constant Node_Id := Make_Temporary (Loc, 'C', N);
5778 begin
5779 Decl :=
5780 Make_Object_Declaration (Loc,
5781 Defining_Identifier => Cnn,
5782 Object_Definition => New_Occurrence_Of (Typ, Loc));
5784 New_If :=
5785 Make_Implicit_If_Statement (N,
5786 Condition => Relocate_Node (Cond),
5788 Then_Statements => New_List (
5789 Make_Assignment_Statement (Sloc (Thenx),
5790 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
5791 Expression => Relocate_Node (Thenx))),
5793 Else_Statements => New_List (
5794 Make_Assignment_Statement (Sloc (Elsex),
5795 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
5796 Expression => Relocate_Node (Elsex))));
5798 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
5799 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
5801 New_N := New_Occurrence_Of (Cnn, Loc);
5802 end;
5804 -- Regular path using Expression_With_Actions
5806 else
5807 if Present (Then_Actions (N)) then
5808 Rewrite (Thenx,
5809 Make_Expression_With_Actions (Sloc (Thenx),
5810 Actions => Then_Actions (N),
5811 Expression => Relocate_Node (Thenx)));
5813 Set_Then_Actions (N, No_List);
5814 Analyze_And_Resolve (Thenx, Typ);
5815 end if;
5817 if Present (Else_Actions (N)) then
5818 Rewrite (Elsex,
5819 Make_Expression_With_Actions (Sloc (Elsex),
5820 Actions => Else_Actions (N),
5821 Expression => Relocate_Node (Elsex)));
5823 Set_Else_Actions (N, No_List);
5824 Analyze_And_Resolve (Elsex, Typ);
5825 end if;
5827 return;
5828 end if;
5830 -- If no actions then no expansion needed, gigi will handle it using the
5831 -- same approach as a C conditional expression.
5833 else
5834 return;
5835 end if;
5837 -- Fall through here for either the limited expansion, or the case of
5838 -- inserting actions for nonlimited types. In both these cases, we must
5839 -- move the SLOC of the parent If statement to the newly created one and
5840 -- change it to the SLOC of the expression which, after expansion, will
5841 -- correspond to what is being evaluated.
5843 if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then
5844 Set_Sloc (New_If, Sloc (Parent (N)));
5845 Set_Sloc (Parent (N), Loc);
5846 end if;
5848 -- Make sure Then_Actions and Else_Actions are appropriately moved
5849 -- to the new if statement.
5851 if Present (Then_Actions (N)) then
5852 Insert_List_Before
5853 (First (Then_Statements (New_If)), Then_Actions (N));
5854 end if;
5856 if Present (Else_Actions (N)) then
5857 Insert_List_Before
5858 (First (Else_Statements (New_If)), Else_Actions (N));
5859 end if;
5861 Insert_Action (N, Decl);
5862 Insert_Action (N, New_If);
5863 Rewrite (N, New_N);
5864 Analyze_And_Resolve (N, Typ);
5865 end Expand_N_If_Expression;
5867 -----------------
5868 -- Expand_N_In --
5869 -----------------
5871 procedure Expand_N_In (N : Node_Id) is
5872 Loc : constant Source_Ptr := Sloc (N);
5873 Restyp : constant Entity_Id := Etype (N);
5874 Lop : constant Node_Id := Left_Opnd (N);
5875 Rop : constant Node_Id := Right_Opnd (N);
5876 Static : constant Boolean := Is_OK_Static_Expression (N);
5878 procedure Substitute_Valid_Check;
5879 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5880 -- test for the left operand being in range of its subtype.
5882 ----------------------------
5883 -- Substitute_Valid_Check --
5884 ----------------------------
5886 procedure Substitute_Valid_Check is
5887 function Is_OK_Object_Reference (Nod : Node_Id) return Boolean;
5888 -- Determine whether arbitrary node Nod denotes a source object that
5889 -- may safely act as prefix of attribute 'Valid.
5891 ----------------------------
5892 -- Is_OK_Object_Reference --
5893 ----------------------------
5895 function Is_OK_Object_Reference (Nod : Node_Id) return Boolean is
5896 Obj_Ref : Node_Id;
5898 begin
5899 -- Inspect the original operand
5901 Obj_Ref := Original_Node (Nod);
5903 -- The object reference must be a source construct, otherwise the
5904 -- codefix suggestion may refer to nonexistent code from a user
5905 -- perspective.
5907 if Comes_From_Source (Obj_Ref) then
5909 -- Recover the actual object reference. There may be more cases
5910 -- to consider???
5912 loop
5913 if Nkind_In (Obj_Ref, N_Type_Conversion,
5914 N_Unchecked_Type_Conversion)
5915 then
5916 Obj_Ref := Expression (Obj_Ref);
5917 else
5918 exit;
5919 end if;
5920 end loop;
5922 return Is_Object_Reference (Obj_Ref);
5923 end if;
5925 return False;
5926 end Is_OK_Object_Reference;
5928 -- Start of processing for Substitute_Valid_Check
5930 begin
5931 Rewrite (N,
5932 Make_Attribute_Reference (Loc,
5933 Prefix => Relocate_Node (Lop),
5934 Attribute_Name => Name_Valid));
5936 Analyze_And_Resolve (N, Restyp);
5938 -- Emit a warning when the left-hand operand of the membership test
5939 -- is a source object, otherwise the use of attribute 'Valid would be
5940 -- illegal. The warning is not given when overflow checking is either
5941 -- MINIMIZED or ELIMINATED, as the danger of optimization has been
5942 -- eliminated above.
5944 if Is_OK_Object_Reference (Lop)
5945 and then Overflow_Check_Mode not in Minimized_Or_Eliminated
5946 then
5947 Error_Msg_N
5948 ("??explicit membership test may be optimized away", N);
5949 Error_Msg_N -- CODEFIX
5950 ("\??use ''Valid attribute instead", N);
5951 end if;
5952 end Substitute_Valid_Check;
5954 -- Local variables
5956 Ltyp : Entity_Id;
5957 Rtyp : Entity_Id;
5959 -- Start of processing for Expand_N_In
5961 begin
5962 -- If set membership case, expand with separate procedure
5964 if Present (Alternatives (N)) then
5965 Expand_Set_Membership (N);
5966 return;
5967 end if;
5969 -- Not set membership, proceed with expansion
5971 Ltyp := Etype (Left_Opnd (N));
5972 Rtyp := Etype (Right_Opnd (N));
5974 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5975 -- type, then expand with a separate procedure. Note the use of the
5976 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5978 if Overflow_Check_Mode in Minimized_Or_Eliminated
5979 and then Is_Signed_Integer_Type (Ltyp)
5980 and then not No_Minimize_Eliminate (N)
5981 then
5982 Expand_Membership_Minimize_Eliminate_Overflow (N);
5983 return;
5984 end if;
5986 -- Check case of explicit test for an expression in range of its
5987 -- subtype. This is suspicious usage and we replace it with a 'Valid
5988 -- test and give a warning for scalar types.
5990 if Is_Scalar_Type (Ltyp)
5992 -- Only relevant for source comparisons
5994 and then Comes_From_Source (N)
5996 -- In floating-point this is a standard way to check for finite values
5997 -- and using 'Valid would typically be a pessimization.
5999 and then not Is_Floating_Point_Type (Ltyp)
6001 -- Don't give the message unless right operand is a type entity and
6002 -- the type of the left operand matches this type. Note that this
6003 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
6004 -- checks have changed the type of the left operand.
6006 and then Nkind (Rop) in N_Has_Entity
6007 and then Ltyp = Entity (Rop)
6009 -- Skip this for predicated types, where such expressions are a
6010 -- reasonable way of testing if something meets the predicate.
6012 and then not Present (Predicate_Function (Ltyp))
6013 then
6014 Substitute_Valid_Check;
6015 return;
6016 end if;
6018 -- Do validity check on operands
6020 if Validity_Checks_On and Validity_Check_Operands then
6021 Ensure_Valid (Left_Opnd (N));
6022 Validity_Check_Range (Right_Opnd (N));
6023 end if;
6025 -- Case of explicit range
6027 if Nkind (Rop) = N_Range then
6028 declare
6029 Lo : constant Node_Id := Low_Bound (Rop);
6030 Hi : constant Node_Id := High_Bound (Rop);
6032 Lo_Orig : constant Node_Id := Original_Node (Lo);
6033 Hi_Orig : constant Node_Id := Original_Node (Hi);
6035 Lcheck : Compare_Result;
6036 Ucheck : Compare_Result;
6038 Warn1 : constant Boolean :=
6039 Constant_Condition_Warnings
6040 and then Comes_From_Source (N)
6041 and then not In_Instance;
6042 -- This must be true for any of the optimization warnings, we
6043 -- clearly want to give them only for source with the flag on. We
6044 -- also skip these warnings in an instance since it may be the
6045 -- case that different instantiations have different ranges.
6047 Warn2 : constant Boolean :=
6048 Warn1
6049 and then Nkind (Original_Node (Rop)) = N_Range
6050 and then Is_Integer_Type (Etype (Lo));
6051 -- For the case where only one bound warning is elided, we also
6052 -- insist on an explicit range and an integer type. The reason is
6053 -- that the use of enumeration ranges including an end point is
6054 -- common, as is the use of a subtype name, one of whose bounds is
6055 -- the same as the type of the expression.
6057 begin
6058 -- If test is explicit x'First .. x'Last, replace by valid check
6060 -- Could use some individual comments for this complex test ???
6062 if Is_Scalar_Type (Ltyp)
6064 -- And left operand is X'First where X matches left operand
6065 -- type (this eliminates cases of type mismatch, including
6066 -- the cases where ELIMINATED/MINIMIZED mode has changed the
6067 -- type of the left operand.
6069 and then Nkind (Lo_Orig) = N_Attribute_Reference
6070 and then Attribute_Name (Lo_Orig) = Name_First
6071 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
6072 and then Entity (Prefix (Lo_Orig)) = Ltyp
6074 -- Same tests for right operand
6076 and then Nkind (Hi_Orig) = N_Attribute_Reference
6077 and then Attribute_Name (Hi_Orig) = Name_Last
6078 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
6079 and then Entity (Prefix (Hi_Orig)) = Ltyp
6081 -- Relevant only for source cases
6083 and then Comes_From_Source (N)
6084 then
6085 Substitute_Valid_Check;
6086 goto Leave;
6087 end if;
6089 -- If bounds of type are known at compile time, and the end points
6090 -- are known at compile time and identical, this is another case
6091 -- for substituting a valid test. We only do this for discrete
6092 -- types, since it won't arise in practice for float types.
6094 if Comes_From_Source (N)
6095 and then Is_Discrete_Type (Ltyp)
6096 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
6097 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
6098 and then Compile_Time_Known_Value (Lo)
6099 and then Compile_Time_Known_Value (Hi)
6100 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
6101 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
6103 -- Kill warnings in instances, since they may be cases where we
6104 -- have a test in the generic that makes sense with some types
6105 -- and not with other types.
6107 -- Similarly, do not rewrite membership as a validity check if
6108 -- within the predicate function for the type.
6110 then
6111 if In_Instance
6112 or else (Ekind (Current_Scope) = E_Function
6113 and then Is_Predicate_Function (Current_Scope))
6114 then
6115 null;
6117 else
6118 Substitute_Valid_Check;
6119 goto Leave;
6120 end if;
6121 end if;
6123 -- If we have an explicit range, do a bit of optimization based on
6124 -- range analysis (we may be able to kill one or both checks).
6126 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
6127 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
6129 -- If either check is known to fail, replace result by False since
6130 -- the other check does not matter. Preserve the static flag for
6131 -- legality checks, because we are constant-folding beyond RM 4.9.
6133 if Lcheck = LT or else Ucheck = GT 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 out of range", N);
6137 end if;
6139 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
6140 Analyze_And_Resolve (N, Restyp);
6141 Set_Is_Static_Expression (N, Static);
6142 goto Leave;
6144 -- If both checks are known to succeed, replace result by True,
6145 -- since we know we are in range.
6147 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
6148 if Warn1 then
6149 Error_Msg_N ("?c?range test optimized away", N);
6150 Error_Msg_N ("\?c?value is known to be in range", N);
6151 end if;
6153 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6154 Analyze_And_Resolve (N, Restyp);
6155 Set_Is_Static_Expression (N, Static);
6156 goto Leave;
6158 -- If lower bound check succeeds and upper bound check is not
6159 -- known to succeed or fail, then replace the range check with
6160 -- a comparison against the upper bound.
6162 elsif Lcheck in Compare_GE then
6163 if Warn2 and then not In_Instance then
6164 Error_Msg_N ("??lower bound test optimized away", Lo);
6165 Error_Msg_N ("\??value is known to be in range", Lo);
6166 end if;
6168 Rewrite (N,
6169 Make_Op_Le (Loc,
6170 Left_Opnd => Lop,
6171 Right_Opnd => High_Bound (Rop)));
6172 Analyze_And_Resolve (N, Restyp);
6173 goto Leave;
6175 -- If upper bound check succeeds and lower bound check is not
6176 -- known to succeed or fail, then replace the range check with
6177 -- a comparison against the lower bound.
6179 elsif Ucheck in Compare_LE then
6180 if Warn2 and then not In_Instance then
6181 Error_Msg_N ("??upper bound test optimized away", Hi);
6182 Error_Msg_N ("\??value is known to be in range", Hi);
6183 end if;
6185 Rewrite (N,
6186 Make_Op_Ge (Loc,
6187 Left_Opnd => Lop,
6188 Right_Opnd => Low_Bound (Rop)));
6189 Analyze_And_Resolve (N, Restyp);
6190 goto Leave;
6191 end if;
6193 -- We couldn't optimize away the range check, but there is one
6194 -- more issue. If we are checking constant conditionals, then we
6195 -- see if we can determine the outcome assuming everything is
6196 -- valid, and if so give an appropriate warning.
6198 if Warn1 and then not Assume_No_Invalid_Values then
6199 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
6200 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
6202 -- Result is out of range for valid value
6204 if Lcheck = LT or else Ucheck = GT then
6205 Error_Msg_N
6206 ("?c?value can only be in range if it is invalid", N);
6208 -- Result is in range for valid value
6210 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
6211 Error_Msg_N
6212 ("?c?value can only be out of range if it is invalid", N);
6214 -- Lower bound check succeeds if value is valid
6216 elsif Warn2 and then Lcheck in Compare_GE then
6217 Error_Msg_N
6218 ("?c?lower bound check only fails if it is invalid", Lo);
6220 -- Upper bound check succeeds if value is valid
6222 elsif Warn2 and then Ucheck in Compare_LE then
6223 Error_Msg_N
6224 ("?c?upper bound check only fails for invalid values", Hi);
6225 end if;
6226 end if;
6227 end;
6229 -- For all other cases of an explicit range, nothing to be done
6231 goto Leave;
6233 -- Here right operand is a subtype mark
6235 else
6236 declare
6237 Typ : Entity_Id := Etype (Rop);
6238 Is_Acc : constant Boolean := Is_Access_Type (Typ);
6239 Cond : Node_Id := Empty;
6240 New_N : Node_Id;
6241 Obj : Node_Id := Lop;
6242 SCIL_Node : Node_Id;
6244 begin
6245 Remove_Side_Effects (Obj);
6247 -- For tagged type, do tagged membership operation
6249 if Is_Tagged_Type (Typ) then
6251 -- No expansion will be performed for VM targets, as the VM
6252 -- back ends will handle the membership tests directly.
6254 if Tagged_Type_Expansion then
6255 Tagged_Membership (N, SCIL_Node, New_N);
6256 Rewrite (N, New_N);
6257 Analyze_And_Resolve (N, Restyp, Suppress => All_Checks);
6259 -- Update decoration of relocated node referenced by the
6260 -- SCIL node.
6262 if Generate_SCIL and then Present (SCIL_Node) then
6263 Set_SCIL_Node (N, SCIL_Node);
6264 end if;
6265 end if;
6267 goto Leave;
6269 -- If type is scalar type, rewrite as x in t'First .. t'Last.
6270 -- This reason we do this is that the bounds may have the wrong
6271 -- type if they come from the original type definition. Also this
6272 -- way we get all the processing above for an explicit range.
6274 -- Don't do this for predicated types, since in this case we
6275 -- want to check the predicate.
6277 elsif Is_Scalar_Type (Typ) then
6278 if No (Predicate_Function (Typ)) then
6279 Rewrite (Rop,
6280 Make_Range (Loc,
6281 Low_Bound =>
6282 Make_Attribute_Reference (Loc,
6283 Attribute_Name => Name_First,
6284 Prefix => New_Occurrence_Of (Typ, Loc)),
6286 High_Bound =>
6287 Make_Attribute_Reference (Loc,
6288 Attribute_Name => Name_Last,
6289 Prefix => New_Occurrence_Of (Typ, Loc))));
6290 Analyze_And_Resolve (N, Restyp);
6291 end if;
6293 goto Leave;
6295 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
6296 -- a membership test if the subtype mark denotes a constrained
6297 -- Unchecked_Union subtype and the expression lacks inferable
6298 -- discriminants.
6300 elsif Is_Unchecked_Union (Base_Type (Typ))
6301 and then Is_Constrained (Typ)
6302 and then not Has_Inferable_Discriminants (Lop)
6303 then
6304 Insert_Action (N,
6305 Make_Raise_Program_Error (Loc,
6306 Reason => PE_Unchecked_Union_Restriction));
6308 -- Prevent Gigi from generating incorrect code by rewriting the
6309 -- test as False. What is this undocumented thing about ???
6311 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
6312 goto Leave;
6313 end if;
6315 -- Here we have a non-scalar type
6317 if Is_Acc then
6318 Typ := Designated_Type (Typ);
6319 end if;
6321 if not Is_Constrained (Typ) then
6322 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6323 Analyze_And_Resolve (N, Restyp);
6325 -- For the constrained array case, we have to check the subscripts
6326 -- for an exact match if the lengths are non-zero (the lengths
6327 -- must match in any case).
6329 elsif Is_Array_Type (Typ) then
6330 Check_Subscripts : declare
6331 function Build_Attribute_Reference
6332 (E : Node_Id;
6333 Nam : Name_Id;
6334 Dim : Nat) return Node_Id;
6335 -- Build attribute reference E'Nam (Dim)
6337 -------------------------------
6338 -- Build_Attribute_Reference --
6339 -------------------------------
6341 function Build_Attribute_Reference
6342 (E : Node_Id;
6343 Nam : Name_Id;
6344 Dim : Nat) return Node_Id
6346 begin
6347 return
6348 Make_Attribute_Reference (Loc,
6349 Prefix => E,
6350 Attribute_Name => Nam,
6351 Expressions => New_List (
6352 Make_Integer_Literal (Loc, Dim)));
6353 end Build_Attribute_Reference;
6355 -- Start of processing for Check_Subscripts
6357 begin
6358 for J in 1 .. Number_Dimensions (Typ) loop
6359 Evolve_And_Then (Cond,
6360 Make_Op_Eq (Loc,
6361 Left_Opnd =>
6362 Build_Attribute_Reference
6363 (Duplicate_Subexpr_No_Checks (Obj),
6364 Name_First, J),
6365 Right_Opnd =>
6366 Build_Attribute_Reference
6367 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
6369 Evolve_And_Then (Cond,
6370 Make_Op_Eq (Loc,
6371 Left_Opnd =>
6372 Build_Attribute_Reference
6373 (Duplicate_Subexpr_No_Checks (Obj),
6374 Name_Last, J),
6375 Right_Opnd =>
6376 Build_Attribute_Reference
6377 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
6378 end loop;
6380 if Is_Acc then
6381 Cond :=
6382 Make_Or_Else (Loc,
6383 Left_Opnd =>
6384 Make_Op_Eq (Loc,
6385 Left_Opnd => Obj,
6386 Right_Opnd => Make_Null (Loc)),
6387 Right_Opnd => Cond);
6388 end if;
6390 Rewrite (N, Cond);
6391 Analyze_And_Resolve (N, Restyp);
6392 end Check_Subscripts;
6394 -- These are the cases where constraint checks may be required,
6395 -- e.g. records with possible discriminants
6397 else
6398 -- Expand the test into a series of discriminant comparisons.
6399 -- The expression that is built is the negation of the one that
6400 -- is used for checking discriminant constraints.
6402 Obj := Relocate_Node (Left_Opnd (N));
6404 if Has_Discriminants (Typ) then
6405 Cond := Make_Op_Not (Loc,
6406 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
6408 if Is_Acc then
6409 Cond := Make_Or_Else (Loc,
6410 Left_Opnd =>
6411 Make_Op_Eq (Loc,
6412 Left_Opnd => Obj,
6413 Right_Opnd => Make_Null (Loc)),
6414 Right_Opnd => Cond);
6415 end if;
6417 else
6418 Cond := New_Occurrence_Of (Standard_True, Loc);
6419 end if;
6421 Rewrite (N, Cond);
6422 Analyze_And_Resolve (N, Restyp);
6423 end if;
6425 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
6426 -- expression of an anonymous access type. This can involve an
6427 -- accessibility test and a tagged type membership test in the
6428 -- case of tagged designated types.
6430 if Ada_Version >= Ada_2012
6431 and then Is_Acc
6432 and then Ekind (Ltyp) = E_Anonymous_Access_Type
6433 then
6434 declare
6435 Expr_Entity : Entity_Id := Empty;
6436 New_N : Node_Id;
6437 Param_Level : Node_Id;
6438 Type_Level : Node_Id;
6440 begin
6441 if Is_Entity_Name (Lop) then
6442 Expr_Entity := Param_Entity (Lop);
6444 if not Present (Expr_Entity) then
6445 Expr_Entity := Entity (Lop);
6446 end if;
6447 end if;
6449 -- If a conversion of the anonymous access value to the
6450 -- tested type would be illegal, then the result is False.
6452 if not Valid_Conversion
6453 (Lop, Rtyp, Lop, Report_Errs => False)
6454 then
6455 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
6456 Analyze_And_Resolve (N, Restyp);
6458 -- Apply an accessibility check if the access object has an
6459 -- associated access level and when the level of the type is
6460 -- less deep than the level of the access parameter. This
6461 -- only occur for access parameters and stand-alone objects
6462 -- of an anonymous access type.
6464 else
6465 if Present (Expr_Entity)
6466 and then
6467 Present
6468 (Effective_Extra_Accessibility (Expr_Entity))
6469 and then UI_Gt (Object_Access_Level (Lop),
6470 Type_Access_Level (Rtyp))
6471 then
6472 Param_Level :=
6473 New_Occurrence_Of
6474 (Effective_Extra_Accessibility (Expr_Entity), Loc);
6476 Type_Level :=
6477 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
6479 -- Return True only if the accessibility level of the
6480 -- expression entity is not deeper than the level of
6481 -- the tested access type.
6483 Rewrite (N,
6484 Make_And_Then (Loc,
6485 Left_Opnd => Relocate_Node (N),
6486 Right_Opnd => Make_Op_Le (Loc,
6487 Left_Opnd => Param_Level,
6488 Right_Opnd => Type_Level)));
6490 Analyze_And_Resolve (N);
6491 end if;
6493 -- If the designated type is tagged, do tagged membership
6494 -- operation.
6496 -- *** NOTE: we have to check not null before doing the
6497 -- tagged membership test (but maybe that can be done
6498 -- inside Tagged_Membership?).
6500 if Is_Tagged_Type (Typ) then
6501 Rewrite (N,
6502 Make_And_Then (Loc,
6503 Left_Opnd => Relocate_Node (N),
6504 Right_Opnd =>
6505 Make_Op_Ne (Loc,
6506 Left_Opnd => Obj,
6507 Right_Opnd => Make_Null (Loc))));
6509 -- No expansion will be performed for VM targets, as
6510 -- the VM back ends will handle the membership tests
6511 -- directly.
6513 if Tagged_Type_Expansion then
6515 -- Note that we have to pass Original_Node, because
6516 -- the membership test might already have been
6517 -- rewritten by earlier parts of membership test.
6519 Tagged_Membership
6520 (Original_Node (N), SCIL_Node, New_N);
6522 -- Update decoration of relocated node referenced
6523 -- by the SCIL node.
6525 if Generate_SCIL and then Present (SCIL_Node) then
6526 Set_SCIL_Node (New_N, SCIL_Node);
6527 end if;
6529 Rewrite (N,
6530 Make_And_Then (Loc,
6531 Left_Opnd => Relocate_Node (N),
6532 Right_Opnd => New_N));
6534 Analyze_And_Resolve (N, Restyp);
6535 end if;
6536 end if;
6537 end if;
6538 end;
6539 end if;
6540 end;
6541 end if;
6543 -- At this point, we have done the processing required for the basic
6544 -- membership test, but not yet dealt with the predicate.
6546 <<Leave>>
6548 -- If a predicate is present, then we do the predicate test, but we
6549 -- most certainly want to omit this if we are within the predicate
6550 -- function itself, since otherwise we have an infinite recursion.
6551 -- The check should also not be emitted when testing against a range
6552 -- (the check is only done when the right operand is a subtype; see
6553 -- RM12-4.5.2 (28.1/3-30/3)).
6555 Predicate_Check : declare
6556 function In_Range_Check return Boolean;
6557 -- Within an expanded range check that may raise Constraint_Error do
6558 -- not generate a predicate check as well. It is redundant because
6559 -- the context will add an explicit predicate check, and it will
6560 -- raise the wrong exception if it fails.
6562 --------------------
6563 -- In_Range_Check --
6564 --------------------
6566 function In_Range_Check return Boolean is
6567 P : Node_Id;
6568 begin
6569 P := Parent (N);
6570 while Present (P) loop
6571 if Nkind (P) = N_Raise_Constraint_Error then
6572 return True;
6574 elsif Nkind (P) in N_Statement_Other_Than_Procedure_Call
6575 or else Nkind (P) = N_Procedure_Call_Statement
6576 or else Nkind (P) in N_Declaration
6577 then
6578 return False;
6579 end if;
6581 P := Parent (P);
6582 end loop;
6584 return False;
6585 end In_Range_Check;
6587 -- Local variables
6589 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
6590 R_Op : Node_Id;
6592 -- Start of processing for Predicate_Check
6594 begin
6595 if Present (PFunc)
6596 and then Current_Scope /= PFunc
6597 and then Nkind (Rop) /= N_Range
6598 then
6599 if not In_Range_Check then
6600 R_Op := Make_Predicate_Call (Rtyp, Lop, Mem => True);
6601 else
6602 R_Op := New_Occurrence_Of (Standard_True, Loc);
6603 end if;
6605 Rewrite (N,
6606 Make_And_Then (Loc,
6607 Left_Opnd => Relocate_Node (N),
6608 Right_Opnd => R_Op));
6610 -- Analyze new expression, mark left operand as analyzed to
6611 -- avoid infinite recursion adding predicate calls. Similarly,
6612 -- suppress further range checks on the call.
6614 Set_Analyzed (Left_Opnd (N));
6615 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6617 -- All done, skip attempt at compile time determination of result
6619 return;
6620 end if;
6621 end Predicate_Check;
6622 end Expand_N_In;
6624 --------------------------------
6625 -- Expand_N_Indexed_Component --
6626 --------------------------------
6628 procedure Expand_N_Indexed_Component (N : Node_Id) is
6629 Loc : constant Source_Ptr := Sloc (N);
6630 Typ : constant Entity_Id := Etype (N);
6631 P : constant Node_Id := Prefix (N);
6632 T : constant Entity_Id := Etype (P);
6633 Atp : Entity_Id;
6635 begin
6636 -- A special optimization, if we have an indexed component that is
6637 -- selecting from a slice, then we can eliminate the slice, since, for
6638 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6639 -- the range check required by the slice. The range check for the slice
6640 -- itself has already been generated. The range check for the
6641 -- subscripting operation is ensured by converting the subject to
6642 -- the subtype of the slice.
6644 -- This optimization not only generates better code, avoiding slice
6645 -- messing especially in the packed case, but more importantly bypasses
6646 -- some problems in handling this peculiar case, for example, the issue
6647 -- of dealing specially with object renamings.
6649 if Nkind (P) = N_Slice
6651 -- This optimization is disabled for CodePeer because it can transform
6652 -- an index-check constraint_error into a range-check constraint_error
6653 -- and CodePeer cares about that distinction.
6655 and then not CodePeer_Mode
6656 then
6657 Rewrite (N,
6658 Make_Indexed_Component (Loc,
6659 Prefix => Prefix (P),
6660 Expressions => New_List (
6661 Convert_To
6662 (Etype (First_Index (Etype (P))),
6663 First (Expressions (N))))));
6664 Analyze_And_Resolve (N, Typ);
6665 return;
6666 end if;
6668 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6669 -- function, then additional actuals must be passed.
6671 if Is_Build_In_Place_Function_Call (P) then
6672 Make_Build_In_Place_Call_In_Anonymous_Context (P);
6674 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
6675 -- containing build-in-place function calls whose returned object covers
6676 -- interface types.
6678 elsif Present (Unqual_BIP_Iface_Function_Call (P)) then
6679 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (P);
6680 end if;
6682 -- If the prefix is an access type, then we unconditionally rewrite if
6683 -- as an explicit dereference. This simplifies processing for several
6684 -- cases, including packed array cases and certain cases in which checks
6685 -- must be generated. We used to try to do this only when it was
6686 -- necessary, but it cleans up the code to do it all the time.
6688 if Is_Access_Type (T) then
6689 Insert_Explicit_Dereference (P);
6690 Analyze_And_Resolve (P, Designated_Type (T));
6691 Atp := Designated_Type (T);
6692 else
6693 Atp := T;
6694 end if;
6696 -- Generate index and validity checks
6698 Generate_Index_Checks (N);
6700 if Validity_Checks_On and then Validity_Check_Subscripts then
6701 Apply_Subscript_Validity_Checks (N);
6702 end if;
6704 -- If selecting from an array with atomic components, and atomic sync
6705 -- is not suppressed for this array type, set atomic sync flag.
6707 if (Has_Atomic_Components (Atp)
6708 and then not Atomic_Synchronization_Disabled (Atp))
6709 or else (Is_Atomic (Typ)
6710 and then not Atomic_Synchronization_Disabled (Typ))
6711 or else (Is_Entity_Name (P)
6712 and then Has_Atomic_Components (Entity (P))
6713 and then not Atomic_Synchronization_Disabled (Entity (P)))
6714 then
6715 Activate_Atomic_Synchronization (N);
6716 end if;
6718 -- All done if the prefix is not a packed array implemented specially
6720 if not (Is_Packed (Etype (Prefix (N)))
6721 and then Present (Packed_Array_Impl_Type (Etype (Prefix (N)))))
6722 then
6723 return;
6724 end if;
6726 -- For packed arrays that are not bit-packed (i.e. the case of an array
6727 -- with one or more index types with a non-contiguous enumeration type),
6728 -- we can always use the normal packed element get circuit.
6730 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
6731 Expand_Packed_Element_Reference (N);
6732 return;
6733 end if;
6735 -- For a reference to a component of a bit packed array, we convert it
6736 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
6737 -- want to do this for simple references, and not for:
6739 -- Left side of assignment, or prefix of left side of assignment, or
6740 -- prefix of the prefix, to handle packed arrays of packed arrays,
6741 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6743 -- Renaming objects in renaming associations
6744 -- This case is handled when a use of the renamed variable occurs
6746 -- Actual parameters for a procedure call
6747 -- This case is handled in Exp_Ch6.Expand_Actuals
6749 -- The second expression in a 'Read attribute reference
6751 -- The prefix of an address or bit or size attribute reference
6753 -- The following circuit detects these exceptions. Note that we need to
6754 -- deal with implicit dereferences when climbing up the parent chain,
6755 -- with the additional difficulty that the type of parents may have yet
6756 -- to be resolved since prefixes are usually resolved first.
6758 declare
6759 Child : Node_Id := N;
6760 Parnt : Node_Id := Parent (N);
6762 begin
6763 loop
6764 if Nkind (Parnt) = N_Unchecked_Expression then
6765 null;
6767 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
6768 N_Procedure_Call_Statement)
6769 or else (Nkind (Parnt) = N_Parameter_Association
6770 and then
6771 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
6772 then
6773 return;
6775 elsif Nkind (Parnt) = N_Attribute_Reference
6776 and then Nam_In (Attribute_Name (Parnt), Name_Address,
6777 Name_Bit,
6778 Name_Size)
6779 and then Prefix (Parnt) = Child
6780 then
6781 return;
6783 elsif Nkind (Parnt) = N_Assignment_Statement
6784 and then Name (Parnt) = Child
6785 then
6786 return;
6788 -- If the expression is an index of an indexed component, it must
6789 -- be expanded regardless of context.
6791 elsif Nkind (Parnt) = N_Indexed_Component
6792 and then Child /= Prefix (Parnt)
6793 then
6794 Expand_Packed_Element_Reference (N);
6795 return;
6797 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
6798 and then Name (Parent (Parnt)) = Parnt
6799 then
6800 return;
6802 elsif Nkind (Parnt) = N_Attribute_Reference
6803 and then Attribute_Name (Parnt) = Name_Read
6804 and then Next (First (Expressions (Parnt))) = Child
6805 then
6806 return;
6808 elsif Nkind (Parnt) = N_Indexed_Component
6809 and then Prefix (Parnt) = Child
6810 then
6811 null;
6813 elsif Nkind (Parnt) = N_Selected_Component
6814 and then Prefix (Parnt) = Child
6815 and then not (Present (Etype (Selector_Name (Parnt)))
6816 and then
6817 Is_Access_Type (Etype (Selector_Name (Parnt))))
6818 then
6819 null;
6821 -- If the parent is a dereference, either implicit or explicit,
6822 -- then the packed reference needs to be expanded.
6824 else
6825 Expand_Packed_Element_Reference (N);
6826 return;
6827 end if;
6829 -- Keep looking up tree for unchecked expression, or if we are the
6830 -- prefix of a possible assignment left side.
6832 Child := Parnt;
6833 Parnt := Parent (Child);
6834 end loop;
6835 end;
6836 end Expand_N_Indexed_Component;
6838 ---------------------
6839 -- Expand_N_Not_In --
6840 ---------------------
6842 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6843 -- can be done. This avoids needing to duplicate this expansion code.
6845 procedure Expand_N_Not_In (N : Node_Id) is
6846 Loc : constant Source_Ptr := Sloc (N);
6847 Typ : constant Entity_Id := Etype (N);
6848 Cfs : constant Boolean := Comes_From_Source (N);
6850 begin
6851 Rewrite (N,
6852 Make_Op_Not (Loc,
6853 Right_Opnd =>
6854 Make_In (Loc,
6855 Left_Opnd => Left_Opnd (N),
6856 Right_Opnd => Right_Opnd (N))));
6858 -- If this is a set membership, preserve list of alternatives
6860 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
6862 -- We want this to appear as coming from source if original does (see
6863 -- transformations in Expand_N_In).
6865 Set_Comes_From_Source (N, Cfs);
6866 Set_Comes_From_Source (Right_Opnd (N), Cfs);
6868 -- Now analyze transformed node
6870 Analyze_And_Resolve (N, Typ);
6871 end Expand_N_Not_In;
6873 -------------------
6874 -- Expand_N_Null --
6875 -------------------
6877 -- The only replacement required is for the case of a null of a type that
6878 -- is an access to protected subprogram, or a subtype thereof. We represent
6879 -- such access values as a record, and so we must replace the occurrence of
6880 -- null by the equivalent record (with a null address and a null pointer in
6881 -- it), so that the back end creates the proper value.
6883 procedure Expand_N_Null (N : Node_Id) is
6884 Loc : constant Source_Ptr := Sloc (N);
6885 Typ : constant Entity_Id := Base_Type (Etype (N));
6886 Agg : Node_Id;
6888 begin
6889 if Is_Access_Protected_Subprogram_Type (Typ) then
6890 Agg :=
6891 Make_Aggregate (Loc,
6892 Expressions => New_List (
6893 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
6894 Make_Null (Loc)));
6896 Rewrite (N, Agg);
6897 Analyze_And_Resolve (N, Equivalent_Type (Typ));
6899 -- For subsequent semantic analysis, the node must retain its type.
6900 -- Gigi in any case replaces this type by the corresponding record
6901 -- type before processing the node.
6903 Set_Etype (N, Typ);
6904 end if;
6906 exception
6907 when RE_Not_Available =>
6908 return;
6909 end Expand_N_Null;
6911 ---------------------
6912 -- Expand_N_Op_Abs --
6913 ---------------------
6915 procedure Expand_N_Op_Abs (N : Node_Id) is
6916 Loc : constant Source_Ptr := Sloc (N);
6917 Expr : constant Node_Id := Right_Opnd (N);
6919 begin
6920 Unary_Op_Validity_Checks (N);
6922 -- Check for MINIMIZED/ELIMINATED overflow mode
6924 if Minimized_Eliminated_Overflow_Check (N) then
6925 Apply_Arithmetic_Overflow_Check (N);
6926 return;
6927 end if;
6929 -- Deal with software overflow checking
6931 if Is_Signed_Integer_Type (Etype (N))
6932 and then Do_Overflow_Check (N)
6933 then
6934 -- The only case to worry about is when the argument is equal to the
6935 -- largest negative number, so what we do is to insert the check:
6937 -- [constraint_error when Expr = typ'Base'First]
6939 -- with the usual Duplicate_Subexpr use coding for expr
6941 Insert_Action (N,
6942 Make_Raise_Constraint_Error (Loc,
6943 Condition =>
6944 Make_Op_Eq (Loc,
6945 Left_Opnd => Duplicate_Subexpr (Expr),
6946 Right_Opnd =>
6947 Make_Attribute_Reference (Loc,
6948 Prefix =>
6949 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
6950 Attribute_Name => Name_First)),
6951 Reason => CE_Overflow_Check_Failed));
6953 Set_Do_Overflow_Check (N, False);
6954 end if;
6955 end Expand_N_Op_Abs;
6957 ---------------------
6958 -- Expand_N_Op_Add --
6959 ---------------------
6961 procedure Expand_N_Op_Add (N : Node_Id) is
6962 Typ : constant Entity_Id := Etype (N);
6964 begin
6965 Binary_Op_Validity_Checks (N);
6967 -- Check for MINIMIZED/ELIMINATED overflow mode
6969 if Minimized_Eliminated_Overflow_Check (N) then
6970 Apply_Arithmetic_Overflow_Check (N);
6971 return;
6972 end if;
6974 -- N + 0 = 0 + N = N for integer types
6976 if Is_Integer_Type (Typ) then
6977 if Compile_Time_Known_Value (Right_Opnd (N))
6978 and then Expr_Value (Right_Opnd (N)) = Uint_0
6979 then
6980 Rewrite (N, Left_Opnd (N));
6981 return;
6983 elsif Compile_Time_Known_Value (Left_Opnd (N))
6984 and then Expr_Value (Left_Opnd (N)) = Uint_0
6985 then
6986 Rewrite (N, Right_Opnd (N));
6987 return;
6988 end if;
6989 end if;
6991 -- Arithmetic overflow checks for signed integer/fixed point types
6993 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
6994 Apply_Arithmetic_Overflow_Check (N);
6995 return;
6996 end if;
6998 -- Overflow checks for floating-point if -gnateF mode active
7000 Check_Float_Op_Overflow (N);
7002 Expand_Nonbinary_Modular_Op (N);
7003 end Expand_N_Op_Add;
7005 ---------------------
7006 -- Expand_N_Op_And --
7007 ---------------------
7009 procedure Expand_N_Op_And (N : Node_Id) is
7010 Typ : constant Entity_Id := Etype (N);
7012 begin
7013 Binary_Op_Validity_Checks (N);
7015 if Is_Array_Type (Etype (N)) then
7016 Expand_Boolean_Operator (N);
7018 elsif Is_Boolean_Type (Etype (N)) then
7019 Adjust_Condition (Left_Opnd (N));
7020 Adjust_Condition (Right_Opnd (N));
7021 Set_Etype (N, Standard_Boolean);
7022 Adjust_Result_Type (N, Typ);
7024 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7025 Expand_Intrinsic_Call (N, Entity (N));
7026 end if;
7028 Expand_Nonbinary_Modular_Op (N);
7029 end Expand_N_Op_And;
7031 ------------------------
7032 -- Expand_N_Op_Concat --
7033 ------------------------
7035 procedure Expand_N_Op_Concat (N : Node_Id) is
7036 Opnds : List_Id;
7037 -- List of operands to be concatenated
7039 Cnode : Node_Id;
7040 -- Node which is to be replaced by the result of concatenating the nodes
7041 -- in the list Opnds.
7043 begin
7044 -- Ensure validity of both operands
7046 Binary_Op_Validity_Checks (N);
7048 -- If we are the left operand of a concatenation higher up the tree,
7049 -- then do nothing for now, since we want to deal with a series of
7050 -- concatenations as a unit.
7052 if Nkind (Parent (N)) = N_Op_Concat
7053 and then N = Left_Opnd (Parent (N))
7054 then
7055 return;
7056 end if;
7058 -- We get here with a concatenation whose left operand may be a
7059 -- concatenation itself with a consistent type. We need to process
7060 -- these concatenation operands from left to right, which means
7061 -- from the deepest node in the tree to the highest node.
7063 Cnode := N;
7064 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
7065 Cnode := Left_Opnd (Cnode);
7066 end loop;
7068 -- Now Cnode is the deepest concatenation, and its parents are the
7069 -- concatenation nodes above, so now we process bottom up, doing the
7070 -- operands.
7072 -- The outer loop runs more than once if more than one concatenation
7073 -- type is involved.
7075 Outer : loop
7076 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
7077 Set_Parent (Opnds, N);
7079 -- The inner loop gathers concatenation operands
7081 Inner : while Cnode /= N
7082 and then Base_Type (Etype (Cnode)) =
7083 Base_Type (Etype (Parent (Cnode)))
7084 loop
7085 Cnode := Parent (Cnode);
7086 Append (Right_Opnd (Cnode), Opnds);
7087 end loop Inner;
7089 -- Note: The following code is a temporary workaround for N731-034
7090 -- and N829-028 and will be kept until the general issue of internal
7091 -- symbol serialization is addressed. The workaround is kept under a
7092 -- debug switch to avoid permiating into the general case.
7094 -- Wrap the node to concatenate into an expression actions node to
7095 -- keep it nicely packaged. This is useful in the case of an assert
7096 -- pragma with a concatenation where we want to be able to delete
7097 -- the concatenation and all its expansion stuff.
7099 if Debug_Flag_Dot_H then
7100 declare
7101 Cnod : constant Node_Id := New_Copy_Tree (Cnode);
7102 Typ : constant Entity_Id := Base_Type (Etype (Cnode));
7104 begin
7105 -- Note: use Rewrite rather than Replace here, so that for
7106 -- example Why_Not_Static can find the original concatenation
7107 -- node OK!
7109 Rewrite (Cnode,
7110 Make_Expression_With_Actions (Sloc (Cnode),
7111 Actions => New_List (Make_Null_Statement (Sloc (Cnode))),
7112 Expression => Cnod));
7114 Expand_Concatenate (Cnod, Opnds);
7115 Analyze_And_Resolve (Cnode, Typ);
7116 end;
7118 -- Default case
7120 else
7121 Expand_Concatenate (Cnode, Opnds);
7122 end if;
7124 exit Outer when Cnode = N;
7125 Cnode := Parent (Cnode);
7126 end loop Outer;
7127 end Expand_N_Op_Concat;
7129 ------------------------
7130 -- Expand_N_Op_Divide --
7131 ------------------------
7133 procedure Expand_N_Op_Divide (N : Node_Id) is
7134 Loc : constant Source_Ptr := Sloc (N);
7135 Lopnd : constant Node_Id := Left_Opnd (N);
7136 Ropnd : constant Node_Id := Right_Opnd (N);
7137 Ltyp : constant Entity_Id := Etype (Lopnd);
7138 Rtyp : constant Entity_Id := Etype (Ropnd);
7139 Typ : Entity_Id := Etype (N);
7140 Rknow : constant Boolean := Is_Integer_Type (Typ)
7141 and then
7142 Compile_Time_Known_Value (Ropnd);
7143 Rval : Uint;
7145 begin
7146 Binary_Op_Validity_Checks (N);
7148 -- Check for MINIMIZED/ELIMINATED overflow mode
7150 if Minimized_Eliminated_Overflow_Check (N) then
7151 Apply_Arithmetic_Overflow_Check (N);
7152 return;
7153 end if;
7155 -- Otherwise proceed with expansion of division
7157 if Rknow then
7158 Rval := Expr_Value (Ropnd);
7159 end if;
7161 -- N / 1 = N for integer types
7163 if Rknow and then Rval = Uint_1 then
7164 Rewrite (N, Lopnd);
7165 return;
7166 end if;
7168 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
7169 -- Is_Power_Of_2_For_Shift is set means that we know that our left
7170 -- operand is an unsigned integer, as required for this to work.
7172 if Nkind (Ropnd) = N_Op_Expon
7173 and then Is_Power_Of_2_For_Shift (Ropnd)
7175 -- We cannot do this transformation in configurable run time mode if we
7176 -- have 64-bit integers and long shifts are not available.
7178 and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target)
7179 then
7180 Rewrite (N,
7181 Make_Op_Shift_Right (Loc,
7182 Left_Opnd => Lopnd,
7183 Right_Opnd =>
7184 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
7185 Analyze_And_Resolve (N, Typ);
7186 return;
7187 end if;
7189 -- Do required fixup of universal fixed operation
7191 if Typ = Universal_Fixed then
7192 Fixup_Universal_Fixed_Operation (N);
7193 Typ := Etype (N);
7194 end if;
7196 -- Divisions with fixed-point results
7198 if Is_Fixed_Point_Type (Typ) then
7200 -- No special processing if Treat_Fixed_As_Integer is set, since
7201 -- from a semantic point of view such operations are simply integer
7202 -- operations and will be treated that way.
7204 if not Treat_Fixed_As_Integer (N) then
7205 if Is_Integer_Type (Rtyp) then
7206 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
7207 else
7208 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
7209 end if;
7210 end if;
7212 -- Deal with divide-by-zero check if back end cannot handle them
7213 -- and the flag is set indicating that we need such a check. Note
7214 -- that we don't need to bother here with the case of mixed-mode
7215 -- (Right operand an integer type), since these will be rewritten
7216 -- with conversions to a divide with a fixed-point right operand.
7218 if Nkind (N) = N_Op_Divide
7219 and then Do_Division_Check (N)
7220 and then not Backend_Divide_Checks_On_Target
7221 and then not Is_Integer_Type (Rtyp)
7222 then
7223 Set_Do_Division_Check (N, False);
7224 Insert_Action (N,
7225 Make_Raise_Constraint_Error (Loc,
7226 Condition =>
7227 Make_Op_Eq (Loc,
7228 Left_Opnd => Duplicate_Subexpr_Move_Checks (Ropnd),
7229 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
7230 Reason => CE_Divide_By_Zero));
7231 end if;
7233 -- Other cases of division of fixed-point operands. Again we exclude the
7234 -- case where Treat_Fixed_As_Integer is set.
7236 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
7237 and then not Treat_Fixed_As_Integer (N)
7238 then
7239 if Is_Integer_Type (Typ) then
7240 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
7241 else
7242 pragma Assert (Is_Floating_Point_Type (Typ));
7243 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
7244 end if;
7246 -- Mixed-mode operations can appear in a non-static universal context,
7247 -- in which case the integer argument must be converted explicitly.
7249 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
7250 Rewrite (Ropnd,
7251 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
7253 Analyze_And_Resolve (Ropnd, Universal_Real);
7255 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
7256 Rewrite (Lopnd,
7257 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
7259 Analyze_And_Resolve (Lopnd, Universal_Real);
7261 -- Non-fixed point cases, do integer zero divide and overflow checks
7263 elsif Is_Integer_Type (Typ) then
7264 Apply_Divide_Checks (N);
7265 end if;
7267 -- Overflow checks for floating-point if -gnateF mode active
7269 Check_Float_Op_Overflow (N);
7271 Expand_Nonbinary_Modular_Op (N);
7272 end Expand_N_Op_Divide;
7274 --------------------
7275 -- Expand_N_Op_Eq --
7276 --------------------
7278 procedure Expand_N_Op_Eq (N : Node_Id) is
7279 Loc : constant Source_Ptr := Sloc (N);
7280 Typ : constant Entity_Id := Etype (N);
7281 Lhs : constant Node_Id := Left_Opnd (N);
7282 Rhs : constant Node_Id := Right_Opnd (N);
7283 Bodies : constant List_Id := New_List;
7284 A_Typ : constant Entity_Id := Etype (Lhs);
7286 Typl : Entity_Id := A_Typ;
7287 Op_Name : Entity_Id;
7288 Prim : Elmt_Id;
7290 procedure Build_Equality_Call (Eq : Entity_Id);
7291 -- If a constructed equality exists for the type or for its parent,
7292 -- build and analyze call, adding conversions if the operation is
7293 -- inherited.
7295 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
7296 -- Determines whether a type has a subcomponent of an unconstrained
7297 -- Unchecked_Union subtype. Typ is a record type.
7299 -------------------------
7300 -- Build_Equality_Call --
7301 -------------------------
7303 procedure Build_Equality_Call (Eq : Entity_Id) is
7304 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
7305 L_Exp : Node_Id := Relocate_Node (Lhs);
7306 R_Exp : Node_Id := Relocate_Node (Rhs);
7308 begin
7309 -- Adjust operands if necessary to comparison type
7311 if Base_Type (Op_Type) /= Base_Type (A_Typ)
7312 and then not Is_Class_Wide_Type (A_Typ)
7313 then
7314 L_Exp := OK_Convert_To (Op_Type, L_Exp);
7315 R_Exp := OK_Convert_To (Op_Type, R_Exp);
7316 end if;
7318 -- If we have an Unchecked_Union, we need to add the inferred
7319 -- discriminant values as actuals in the function call. At this
7320 -- point, the expansion has determined that both operands have
7321 -- inferable discriminants.
7323 if Is_Unchecked_Union (Op_Type) then
7324 declare
7325 Lhs_Type : constant Node_Id := Etype (L_Exp);
7326 Rhs_Type : constant Node_Id := Etype (R_Exp);
7328 Lhs_Discr_Vals : Elist_Id;
7329 -- List of inferred discriminant values for left operand.
7331 Rhs_Discr_Vals : Elist_Id;
7332 -- List of inferred discriminant values for right operand.
7334 Discr : Entity_Id;
7336 begin
7337 Lhs_Discr_Vals := New_Elmt_List;
7338 Rhs_Discr_Vals := New_Elmt_List;
7340 -- Per-object constrained selected components require special
7341 -- attention. If the enclosing scope of the component is an
7342 -- Unchecked_Union, we cannot reference its discriminants
7343 -- directly. This is why we use the extra parameters of the
7344 -- equality function of the enclosing Unchecked_Union.
7346 -- type UU_Type (Discr : Integer := 0) is
7347 -- . . .
7348 -- end record;
7349 -- pragma Unchecked_Union (UU_Type);
7351 -- 1. Unchecked_Union enclosing record:
7353 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
7354 -- . . .
7355 -- Comp : UU_Type (Discr);
7356 -- . . .
7357 -- end Enclosing_UU_Type;
7358 -- pragma Unchecked_Union (Enclosing_UU_Type);
7360 -- Obj1 : Enclosing_UU_Type;
7361 -- Obj2 : Enclosing_UU_Type (1);
7363 -- [. . .] Obj1 = Obj2 [. . .]
7365 -- Generated code:
7367 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
7369 -- A and B are the formal parameters of the equality function
7370 -- of Enclosing_UU_Type. The function always has two extra
7371 -- formals to capture the inferred discriminant values for
7372 -- each discriminant of the type.
7374 -- 2. Non-Unchecked_Union enclosing record:
7376 -- type
7377 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
7378 -- is record
7379 -- . . .
7380 -- Comp : UU_Type (Discr);
7381 -- . . .
7382 -- end Enclosing_Non_UU_Type;
7384 -- Obj1 : Enclosing_Non_UU_Type;
7385 -- Obj2 : Enclosing_Non_UU_Type (1);
7387 -- ... Obj1 = Obj2 ...
7389 -- Generated code:
7391 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
7392 -- obj1.discr, obj2.discr)) then
7394 -- In this case we can directly reference the discriminants of
7395 -- the enclosing record.
7397 -- Process left operand of equality
7399 if Nkind (Lhs) = N_Selected_Component
7400 and then
7401 Has_Per_Object_Constraint (Entity (Selector_Name (Lhs)))
7402 then
7403 -- If enclosing record is an Unchecked_Union, use formals
7404 -- corresponding to each discriminant. The name of the
7405 -- formal is that of the discriminant, with added suffix,
7406 -- see Exp_Ch3.Build_Record_Equality for details.
7408 if Is_Unchecked_Union (Scope (Entity (Selector_Name (Lhs))))
7409 then
7410 Discr :=
7411 First_Discriminant
7412 (Scope (Entity (Selector_Name (Lhs))));
7413 while Present (Discr) loop
7414 Append_Elmt
7415 (Make_Identifier (Loc,
7416 Chars => New_External_Name (Chars (Discr), 'A')),
7417 To => Lhs_Discr_Vals);
7418 Next_Discriminant (Discr);
7419 end loop;
7421 -- If enclosing record is of a non-Unchecked_Union type, it
7422 -- is possible to reference its discriminants directly.
7424 else
7425 Discr := First_Discriminant (Lhs_Type);
7426 while Present (Discr) loop
7427 Append_Elmt
7428 (Make_Selected_Component (Loc,
7429 Prefix => Prefix (Lhs),
7430 Selector_Name =>
7431 New_Copy
7432 (Get_Discriminant_Value (Discr,
7433 Lhs_Type,
7434 Stored_Constraint (Lhs_Type)))),
7435 To => Lhs_Discr_Vals);
7436 Next_Discriminant (Discr);
7437 end loop;
7438 end if;
7440 -- Otherwise operand is on object with a constrained type.
7441 -- Infer the discriminant values from the constraint.
7443 else
7445 Discr := First_Discriminant (Lhs_Type);
7446 while Present (Discr) loop
7447 Append_Elmt
7448 (New_Copy
7449 (Get_Discriminant_Value (Discr,
7450 Lhs_Type,
7451 Stored_Constraint (Lhs_Type))),
7452 To => Lhs_Discr_Vals);
7453 Next_Discriminant (Discr);
7454 end loop;
7455 end if;
7457 -- Similar processing for right operand of equality
7459 if Nkind (Rhs) = N_Selected_Component
7460 and then
7461 Has_Per_Object_Constraint (Entity (Selector_Name (Rhs)))
7462 then
7463 if Is_Unchecked_Union
7464 (Scope (Entity (Selector_Name (Rhs))))
7465 then
7466 Discr :=
7467 First_Discriminant
7468 (Scope (Entity (Selector_Name (Rhs))));
7469 while Present (Discr) loop
7470 Append_Elmt
7471 (Make_Identifier (Loc,
7472 Chars => New_External_Name (Chars (Discr), 'B')),
7473 To => Rhs_Discr_Vals);
7474 Next_Discriminant (Discr);
7475 end loop;
7477 else
7478 Discr := First_Discriminant (Rhs_Type);
7479 while Present (Discr) loop
7480 Append_Elmt
7481 (Make_Selected_Component (Loc,
7482 Prefix => Prefix (Rhs),
7483 Selector_Name =>
7484 New_Copy (Get_Discriminant_Value
7485 (Discr,
7486 Rhs_Type,
7487 Stored_Constraint (Rhs_Type)))),
7488 To => Rhs_Discr_Vals);
7489 Next_Discriminant (Discr);
7490 end loop;
7491 end if;
7493 else
7494 Discr := First_Discriminant (Rhs_Type);
7495 while Present (Discr) loop
7496 Append_Elmt
7497 (New_Copy (Get_Discriminant_Value
7498 (Discr,
7499 Rhs_Type,
7500 Stored_Constraint (Rhs_Type))),
7501 To => Rhs_Discr_Vals);
7502 Next_Discriminant (Discr);
7503 end loop;
7504 end if;
7506 -- Now merge the list of discriminant values so that values
7507 -- of corresponding discriminants are adjacent.
7509 declare
7510 Params : List_Id;
7511 L_Elmt : Elmt_Id;
7512 R_Elmt : Elmt_Id;
7514 begin
7515 Params := New_List (L_Exp, R_Exp);
7516 L_Elmt := First_Elmt (Lhs_Discr_Vals);
7517 R_Elmt := First_Elmt (Rhs_Discr_Vals);
7518 while Present (L_Elmt) loop
7519 Append_To (Params, Node (L_Elmt));
7520 Append_To (Params, Node (R_Elmt));
7521 Next_Elmt (L_Elmt);
7522 Next_Elmt (R_Elmt);
7523 end loop;
7525 Rewrite (N,
7526 Make_Function_Call (Loc,
7527 Name => New_Occurrence_Of (Eq, Loc),
7528 Parameter_Associations => Params));
7529 end;
7530 end;
7532 -- Normal case, not an unchecked union
7534 else
7535 Rewrite (N,
7536 Make_Function_Call (Loc,
7537 Name => New_Occurrence_Of (Eq, Loc),
7538 Parameter_Associations => New_List (L_Exp, R_Exp)));
7539 end if;
7541 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7542 end Build_Equality_Call;
7544 ------------------------------------
7545 -- Has_Unconstrained_UU_Component --
7546 ------------------------------------
7548 function Has_Unconstrained_UU_Component
7549 (Typ : Node_Id) return Boolean
7551 Tdef : constant Node_Id :=
7552 Type_Definition (Declaration_Node (Base_Type (Typ)));
7553 Clist : Node_Id;
7554 Vpart : Node_Id;
7556 function Component_Is_Unconstrained_UU
7557 (Comp : Node_Id) return Boolean;
7558 -- Determines whether the subtype of the component is an
7559 -- unconstrained Unchecked_Union.
7561 function Variant_Is_Unconstrained_UU
7562 (Variant : Node_Id) return Boolean;
7563 -- Determines whether a component of the variant has an unconstrained
7564 -- Unchecked_Union subtype.
7566 -----------------------------------
7567 -- Component_Is_Unconstrained_UU --
7568 -----------------------------------
7570 function Component_Is_Unconstrained_UU
7571 (Comp : Node_Id) return Boolean
7573 begin
7574 if Nkind (Comp) /= N_Component_Declaration then
7575 return False;
7576 end if;
7578 declare
7579 Sindic : constant Node_Id :=
7580 Subtype_Indication (Component_Definition (Comp));
7582 begin
7583 -- Unconstrained nominal type. In the case of a constraint
7584 -- present, the node kind would have been N_Subtype_Indication.
7586 if Nkind (Sindic) = N_Identifier then
7587 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
7588 end if;
7590 return False;
7591 end;
7592 end Component_Is_Unconstrained_UU;
7594 ---------------------------------
7595 -- Variant_Is_Unconstrained_UU --
7596 ---------------------------------
7598 function Variant_Is_Unconstrained_UU
7599 (Variant : Node_Id) return Boolean
7601 Clist : constant Node_Id := Component_List (Variant);
7603 begin
7604 if Is_Empty_List (Component_Items (Clist)) then
7605 return False;
7606 end if;
7608 -- We only need to test one component
7610 declare
7611 Comp : Node_Id := First (Component_Items (Clist));
7613 begin
7614 while Present (Comp) loop
7615 if Component_Is_Unconstrained_UU (Comp) then
7616 return True;
7617 end if;
7619 Next (Comp);
7620 end loop;
7621 end;
7623 -- None of the components withing the variant were of
7624 -- unconstrained Unchecked_Union type.
7626 return False;
7627 end Variant_Is_Unconstrained_UU;
7629 -- Start of processing for Has_Unconstrained_UU_Component
7631 begin
7632 if Null_Present (Tdef) then
7633 return False;
7634 end if;
7636 Clist := Component_List (Tdef);
7637 Vpart := Variant_Part (Clist);
7639 -- Inspect available components
7641 if Present (Component_Items (Clist)) then
7642 declare
7643 Comp : Node_Id := First (Component_Items (Clist));
7645 begin
7646 while Present (Comp) loop
7648 -- One component is sufficient
7650 if Component_Is_Unconstrained_UU (Comp) then
7651 return True;
7652 end if;
7654 Next (Comp);
7655 end loop;
7656 end;
7657 end if;
7659 -- Inspect available components withing variants
7661 if Present (Vpart) then
7662 declare
7663 Variant : Node_Id := First (Variants (Vpart));
7665 begin
7666 while Present (Variant) loop
7668 -- One component within a variant is sufficient
7670 if Variant_Is_Unconstrained_UU (Variant) then
7671 return True;
7672 end if;
7674 Next (Variant);
7675 end loop;
7676 end;
7677 end if;
7679 -- Neither the available components, nor the components inside the
7680 -- variant parts were of an unconstrained Unchecked_Union subtype.
7682 return False;
7683 end Has_Unconstrained_UU_Component;
7685 -- Start of processing for Expand_N_Op_Eq
7687 begin
7688 Binary_Op_Validity_Checks (N);
7690 -- Deal with private types
7692 if Ekind (Typl) = E_Private_Type then
7693 Typl := Underlying_Type (Typl);
7694 elsif Ekind (Typl) = E_Private_Subtype then
7695 Typl := Underlying_Type (Base_Type (Typl));
7696 else
7697 null;
7698 end if;
7700 -- It may happen in error situations that the underlying type is not
7701 -- set. The error will be detected later, here we just defend the
7702 -- expander code.
7704 if No (Typl) then
7705 return;
7706 end if;
7708 -- Now get the implementation base type (note that plain Base_Type here
7709 -- might lead us back to the private type, which is not what we want!)
7711 Typl := Implementation_Base_Type (Typl);
7713 -- Equality between variant records results in a call to a routine
7714 -- that has conditional tests of the discriminant value(s), and hence
7715 -- violates the No_Implicit_Conditionals restriction.
7717 if Has_Variant_Part (Typl) then
7718 declare
7719 Msg : Boolean;
7721 begin
7722 Check_Restriction (Msg, No_Implicit_Conditionals, N);
7724 if Msg then
7725 Error_Msg_N
7726 ("\comparison of variant records tests discriminants", N);
7727 return;
7728 end if;
7729 end;
7730 end if;
7732 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7733 -- means we no longer have a comparison operation, we are all done.
7735 Expand_Compare_Minimize_Eliminate_Overflow (N);
7737 if Nkind (N) /= N_Op_Eq then
7738 return;
7739 end if;
7741 -- Boolean types (requiring handling of non-standard case)
7743 if Is_Boolean_Type (Typl) then
7744 Adjust_Condition (Left_Opnd (N));
7745 Adjust_Condition (Right_Opnd (N));
7746 Set_Etype (N, Standard_Boolean);
7747 Adjust_Result_Type (N, Typ);
7749 -- Array types
7751 elsif Is_Array_Type (Typl) then
7753 -- If we are doing full validity checking, and it is possible for the
7754 -- array elements to be invalid then expand out array comparisons to
7755 -- make sure that we check the array elements.
7757 if Validity_Check_Operands
7758 and then not Is_Known_Valid (Component_Type (Typl))
7759 then
7760 declare
7761 Save_Force_Validity_Checks : constant Boolean :=
7762 Force_Validity_Checks;
7763 begin
7764 Force_Validity_Checks := True;
7765 Rewrite (N,
7766 Expand_Array_Equality
7768 Relocate_Node (Lhs),
7769 Relocate_Node (Rhs),
7770 Bodies,
7771 Typl));
7772 Insert_Actions (N, Bodies);
7773 Analyze_And_Resolve (N, Standard_Boolean);
7774 Force_Validity_Checks := Save_Force_Validity_Checks;
7775 end;
7777 -- Packed case where both operands are known aligned
7779 elsif Is_Bit_Packed_Array (Typl)
7780 and then not Is_Possibly_Unaligned_Object (Lhs)
7781 and then not Is_Possibly_Unaligned_Object (Rhs)
7782 then
7783 Expand_Packed_Eq (N);
7785 -- Where the component type is elementary we can use a block bit
7786 -- comparison (if supported on the target) exception in the case
7787 -- of floating-point (negative zero issues require element by
7788 -- element comparison), and atomic/VFA types (where we must be sure
7789 -- to load elements independently) and possibly unaligned arrays.
7791 elsif Is_Elementary_Type (Component_Type (Typl))
7792 and then not Is_Floating_Point_Type (Component_Type (Typl))
7793 and then not Is_Atomic_Or_VFA (Component_Type (Typl))
7794 and then not Is_Possibly_Unaligned_Object (Lhs)
7795 and then not Is_Possibly_Unaligned_Object (Rhs)
7796 and then Support_Composite_Compare_On_Target
7797 then
7798 null;
7800 -- For composite and floating-point cases, expand equality loop to
7801 -- make sure of using proper comparisons for tagged types, and
7802 -- correctly handling the floating-point case.
7804 else
7805 Rewrite (N,
7806 Expand_Array_Equality
7808 Relocate_Node (Lhs),
7809 Relocate_Node (Rhs),
7810 Bodies,
7811 Typl));
7812 Insert_Actions (N, Bodies, Suppress => All_Checks);
7813 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7814 end if;
7816 -- Record Types
7818 elsif Is_Record_Type (Typl) then
7820 -- For tagged types, use the primitive "="
7822 if Is_Tagged_Type (Typl) then
7824 -- No need to do anything else compiling under restriction
7825 -- No_Dispatching_Calls. During the semantic analysis we
7826 -- already notified such violation.
7828 if Restriction_Active (No_Dispatching_Calls) then
7829 return;
7830 end if;
7832 -- If this is an untagged private type completed with a derivation
7833 -- of an untagged private type whose full view is a tagged type,
7834 -- we use the primitive operations of the private type (since it
7835 -- does not have a full view, and also because its equality
7836 -- primitive may have been overridden in its untagged full view).
7838 if Inherits_From_Tagged_Full_View (A_Typ) then
7840 -- Search for equality operation, checking that the operands
7841 -- have the same type. Note that we must find a matching entry,
7842 -- or something is very wrong.
7844 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
7846 while Present (Prim) loop
7847 exit when Chars (Node (Prim)) = Name_Op_Eq
7848 and then Etype (First_Formal (Node (Prim))) =
7849 Etype (Next_Formal (First_Formal (Node (Prim))))
7850 and then
7851 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7853 Next_Elmt (Prim);
7854 end loop;
7856 pragma Assert (Present (Prim));
7857 Op_Name := Node (Prim);
7859 -- Find the type's predefined equality or an overriding
7860 -- user-defined equality. The reason for not simply calling
7861 -- Find_Prim_Op here is that there may be a user-defined
7862 -- overloaded equality op that precedes the equality that we
7863 -- want, so we have to explicitly search (e.g., there could be
7864 -- an equality with two different parameter types).
7866 else
7867 if Is_Class_Wide_Type (Typl) then
7868 Typl := Find_Specific_Type (Typl);
7869 end if;
7871 Prim := First_Elmt (Primitive_Operations (Typl));
7872 while Present (Prim) loop
7873 exit when Chars (Node (Prim)) = Name_Op_Eq
7874 and then Etype (First_Formal (Node (Prim))) =
7875 Etype (Next_Formal (First_Formal (Node (Prim))))
7876 and then
7877 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7879 Next_Elmt (Prim);
7880 end loop;
7882 pragma Assert (Present (Prim));
7883 Op_Name := Node (Prim);
7884 end if;
7886 Build_Equality_Call (Op_Name);
7888 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7889 -- predefined equality operator for a type which has a subcomponent
7890 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7892 elsif Has_Unconstrained_UU_Component (Typl) then
7893 Insert_Action (N,
7894 Make_Raise_Program_Error (Loc,
7895 Reason => PE_Unchecked_Union_Restriction));
7897 -- Prevent Gigi from generating incorrect code by rewriting the
7898 -- equality as a standard False. (is this documented somewhere???)
7900 Rewrite (N,
7901 New_Occurrence_Of (Standard_False, Loc));
7903 elsif Is_Unchecked_Union (Typl) then
7905 -- If we can infer the discriminants of the operands, we make a
7906 -- call to the TSS equality function.
7908 if Has_Inferable_Discriminants (Lhs)
7909 and then
7910 Has_Inferable_Discriminants (Rhs)
7911 then
7912 Build_Equality_Call
7913 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7915 else
7916 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7917 -- the predefined equality operator for an Unchecked_Union type
7918 -- if either of the operands lack inferable discriminants.
7920 Insert_Action (N,
7921 Make_Raise_Program_Error (Loc,
7922 Reason => PE_Unchecked_Union_Restriction));
7924 -- Emit a warning on source equalities only, otherwise the
7925 -- message may appear out of place due to internal use. The
7926 -- warning is unconditional because it is required by the
7927 -- language.
7929 if Comes_From_Source (N) then
7930 Error_Msg_N
7931 ("Unchecked_Union discriminants cannot be determined??",
7933 Error_Msg_N
7934 ("\Program_Error will be raised for equality operation??",
7936 end if;
7938 -- Prevent Gigi from generating incorrect code by rewriting
7939 -- the equality as a standard False (documented where???).
7941 Rewrite (N,
7942 New_Occurrence_Of (Standard_False, Loc));
7943 end if;
7945 -- If a type support function is present (for complex cases), use it
7947 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
7948 Build_Equality_Call
7949 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7951 -- When comparing two Bounded_Strings, use the primitive equality of
7952 -- the root Super_String type.
7954 elsif Is_Bounded_String (Typl) then
7955 Prim :=
7956 First_Elmt (Collect_Primitive_Operations (Root_Type (Typl)));
7958 while Present (Prim) loop
7959 exit when Chars (Node (Prim)) = Name_Op_Eq
7960 and then Etype (First_Formal (Node (Prim))) =
7961 Etype (Next_Formal (First_Formal (Node (Prim))))
7962 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7964 Next_Elmt (Prim);
7965 end loop;
7967 -- A Super_String type should always have a primitive equality
7969 pragma Assert (Present (Prim));
7970 Build_Equality_Call (Node (Prim));
7972 -- Otherwise expand the component by component equality. Note that
7973 -- we never use block-bit comparisons for records, because of the
7974 -- problems with gaps. The back end will often be able to recombine
7975 -- the separate comparisons that we generate here.
7977 else
7978 Remove_Side_Effects (Lhs);
7979 Remove_Side_Effects (Rhs);
7980 Rewrite (N,
7981 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
7983 Insert_Actions (N, Bodies, Suppress => All_Checks);
7984 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7985 end if;
7986 end if;
7988 -- Test if result is known at compile time
7990 Rewrite_Comparison (N);
7992 -- Special optimization of length comparison
7994 Optimize_Length_Comparison (N);
7996 -- One more special case: if we have a comparison of X'Result = expr
7997 -- in floating-point, then if not already there, change expr to be
7998 -- f'Machine (expr) to eliminate surprise from extra precision.
8000 if Is_Floating_Point_Type (Typl)
8001 and then Nkind (Original_Node (Lhs)) = N_Attribute_Reference
8002 and then Attribute_Name (Original_Node (Lhs)) = Name_Result
8003 then
8004 -- Stick in the Typ'Machine call if not already there
8006 if Nkind (Rhs) /= N_Attribute_Reference
8007 or else Attribute_Name (Rhs) /= Name_Machine
8008 then
8009 Rewrite (Rhs,
8010 Make_Attribute_Reference (Loc,
8011 Prefix => New_Occurrence_Of (Typl, Loc),
8012 Attribute_Name => Name_Machine,
8013 Expressions => New_List (Relocate_Node (Rhs))));
8014 Analyze_And_Resolve (Rhs, Typl);
8015 end if;
8016 end if;
8017 end Expand_N_Op_Eq;
8019 -----------------------
8020 -- Expand_N_Op_Expon --
8021 -----------------------
8023 procedure Expand_N_Op_Expon (N : Node_Id) is
8024 Loc : constant Source_Ptr := Sloc (N);
8025 Ovflo : constant Boolean := Do_Overflow_Check (N);
8026 Typ : constant Entity_Id := Etype (N);
8027 Rtyp : constant Entity_Id := Root_Type (Typ);
8029 Bastyp : Entity_Id;
8031 function Wrap_MA (Exp : Node_Id) return Node_Id;
8032 -- Given an expression Exp, if the root type is Float or Long_Float,
8033 -- then wrap the expression in a call of Bastyp'Machine, to stop any
8034 -- extra precision. This is done to ensure that X**A = X**B when A is
8035 -- a static constant and B is a variable with the same value. For any
8036 -- other type, the node Exp is returned unchanged.
8038 -------------
8039 -- Wrap_MA --
8040 -------------
8042 function Wrap_MA (Exp : Node_Id) return Node_Id is
8043 Loc : constant Source_Ptr := Sloc (Exp);
8045 begin
8046 if Rtyp = Standard_Float or else Rtyp = Standard_Long_Float then
8047 return
8048 Make_Attribute_Reference (Loc,
8049 Attribute_Name => Name_Machine,
8050 Prefix => New_Occurrence_Of (Bastyp, Loc),
8051 Expressions => New_List (Relocate_Node (Exp)));
8052 else
8053 return Exp;
8054 end if;
8055 end Wrap_MA;
8057 -- Local variables
8059 Base : Node_Id;
8060 Ent : Entity_Id;
8061 Etyp : Entity_Id;
8062 Exp : Node_Id;
8063 Exptyp : Entity_Id;
8064 Expv : Uint;
8065 Rent : RE_Id;
8066 Temp : Node_Id;
8067 Xnode : Node_Id;
8069 -- Start of processing for Expand_N_Op_Expon
8071 begin
8072 Binary_Op_Validity_Checks (N);
8074 -- CodePeer wants to see the unexpanded N_Op_Expon node
8076 if CodePeer_Mode then
8077 return;
8078 end if;
8080 -- Relocation of left and right operands must be done after performing
8081 -- the validity checks since the generation of validation checks may
8082 -- remove side effects.
8084 Base := Relocate_Node (Left_Opnd (N));
8085 Bastyp := Etype (Base);
8086 Exp := Relocate_Node (Right_Opnd (N));
8087 Exptyp := Etype (Exp);
8089 -- If either operand is of a private type, then we have the use of an
8090 -- intrinsic operator, and we get rid of the privateness, by using root
8091 -- types of underlying types for the actual operation. Otherwise the
8092 -- private types will cause trouble if we expand multiplications or
8093 -- shifts etc. We also do this transformation if the result type is
8094 -- different from the base type.
8096 if Is_Private_Type (Etype (Base))
8097 or else Is_Private_Type (Typ)
8098 or else Is_Private_Type (Exptyp)
8099 or else Rtyp /= Root_Type (Bastyp)
8100 then
8101 declare
8102 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
8103 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
8104 begin
8105 Rewrite (N,
8106 Unchecked_Convert_To (Typ,
8107 Make_Op_Expon (Loc,
8108 Left_Opnd => Unchecked_Convert_To (Bt, Base),
8109 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
8110 Analyze_And_Resolve (N, Typ);
8111 return;
8112 end;
8113 end if;
8115 -- Check for MINIMIZED/ELIMINATED overflow mode
8117 if Minimized_Eliminated_Overflow_Check (N) then
8118 Apply_Arithmetic_Overflow_Check (N);
8119 return;
8120 end if;
8122 -- Test for case of known right argument where we can replace the
8123 -- exponentiation by an equivalent expression using multiplication.
8125 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
8126 -- configurable run-time mode, we may not have the exponentiation
8127 -- routine available, and we don't want the legality of the program
8128 -- to depend on how clever the compiler is in knowing values.
8130 if CRT_Safe_Compile_Time_Known_Value (Exp) then
8131 Expv := Expr_Value (Exp);
8133 -- We only fold small non-negative exponents. You might think we
8134 -- could fold small negative exponents for the real case, but we
8135 -- can't because we are required to raise Constraint_Error for
8136 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
8137 -- See ACVC test C4A012B, and it is not worth generating the test.
8139 -- For small negative exponents, we return the reciprocal of
8140 -- the folding of the exponentiation for the opposite (positive)
8141 -- exponent, as required by Ada RM 4.5.6(11/3).
8143 if abs Expv <= 4 then
8145 -- X ** 0 = 1 (or 1.0)
8147 if Expv = 0 then
8149 -- Call Remove_Side_Effects to ensure that any side effects
8150 -- in the ignored left operand (in particular function calls
8151 -- to user defined functions) are properly executed.
8153 Remove_Side_Effects (Base);
8155 if Ekind (Typ) in Integer_Kind then
8156 Xnode := Make_Integer_Literal (Loc, Intval => 1);
8157 else
8158 Xnode := Make_Real_Literal (Loc, Ureal_1);
8159 end if;
8161 -- X ** 1 = X
8163 elsif Expv = 1 then
8164 Xnode := Base;
8166 -- X ** 2 = X * X
8168 elsif Expv = 2 then
8169 Xnode :=
8170 Wrap_MA (
8171 Make_Op_Multiply (Loc,
8172 Left_Opnd => Duplicate_Subexpr (Base),
8173 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)));
8175 -- X ** 3 = X * X * X
8177 elsif Expv = 3 then
8178 Xnode :=
8179 Wrap_MA (
8180 Make_Op_Multiply (Loc,
8181 Left_Opnd =>
8182 Make_Op_Multiply (Loc,
8183 Left_Opnd => Duplicate_Subexpr (Base),
8184 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
8185 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)));
8187 -- X ** 4 ->
8189 -- do
8190 -- En : constant base'type := base * base;
8191 -- in
8192 -- En * En
8194 elsif Expv = 4 then
8195 Temp := Make_Temporary (Loc, 'E', Base);
8197 Xnode :=
8198 Make_Expression_With_Actions (Loc,
8199 Actions => New_List (
8200 Make_Object_Declaration (Loc,
8201 Defining_Identifier => Temp,
8202 Constant_Present => True,
8203 Object_Definition => New_Occurrence_Of (Typ, Loc),
8204 Expression =>
8205 Wrap_MA (
8206 Make_Op_Multiply (Loc,
8207 Left_Opnd =>
8208 Duplicate_Subexpr (Base),
8209 Right_Opnd =>
8210 Duplicate_Subexpr_No_Checks (Base))))),
8212 Expression =>
8213 Wrap_MA (
8214 Make_Op_Multiply (Loc,
8215 Left_Opnd => New_Occurrence_Of (Temp, Loc),
8216 Right_Opnd => New_Occurrence_Of (Temp, Loc))));
8218 -- X ** N = 1.0 / X ** (-N)
8219 -- N in -4 .. -1
8221 else
8222 pragma Assert
8223 (Expv = -1 or Expv = -2 or Expv = -3 or Expv = -4);
8225 Xnode :=
8226 Make_Op_Divide (Loc,
8227 Left_Opnd =>
8228 Make_Float_Literal (Loc,
8229 Radix => Uint_1,
8230 Significand => Uint_1,
8231 Exponent => Uint_0),
8232 Right_Opnd =>
8233 Make_Op_Expon (Loc,
8234 Left_Opnd => Duplicate_Subexpr (Base),
8235 Right_Opnd =>
8236 Make_Integer_Literal (Loc,
8237 Intval => -Expv)));
8238 end if;
8240 Rewrite (N, Xnode);
8241 Analyze_And_Resolve (N, Typ);
8242 return;
8243 end if;
8244 end if;
8246 -- Deal with optimizing 2 ** expression to shift where possible
8248 -- Note: we used to check that Exptyp was an unsigned type. But that is
8249 -- an unnecessary check, since if Exp is negative, we have a run-time
8250 -- error that is either caught (so we get the right result) or we have
8251 -- suppressed the check, in which case the code is erroneous anyway.
8253 if Is_Integer_Type (Rtyp)
8255 -- The base value must be "safe compile-time known", and exactly 2
8257 and then Nkind (Base) = N_Integer_Literal
8258 and then CRT_Safe_Compile_Time_Known_Value (Base)
8259 and then Expr_Value (Base) = Uint_2
8261 -- We only handle cases where the right type is a integer
8263 and then Is_Integer_Type (Root_Type (Exptyp))
8264 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
8266 -- This transformation is not applicable for a modular type with a
8267 -- nonbinary modulus because we do not handle modular reduction in
8268 -- a correct manner if we attempt this transformation in this case.
8270 and then not Non_Binary_Modulus (Typ)
8271 then
8272 -- Handle the cases where our parent is a division or multiplication
8273 -- specially. In these cases we can convert to using a shift at the
8274 -- parent level if we are not doing overflow checking, since it is
8275 -- too tricky to combine the overflow check at the parent level.
8277 if not Ovflo
8278 and then Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply)
8279 then
8280 declare
8281 P : constant Node_Id := Parent (N);
8282 L : constant Node_Id := Left_Opnd (P);
8283 R : constant Node_Id := Right_Opnd (P);
8285 begin
8286 if (Nkind (P) = N_Op_Multiply
8287 and then
8288 ((Is_Integer_Type (Etype (L)) and then R = N)
8289 or else
8290 (Is_Integer_Type (Etype (R)) and then L = N))
8291 and then not Do_Overflow_Check (P))
8293 or else
8294 (Nkind (P) = N_Op_Divide
8295 and then Is_Integer_Type (Etype (L))
8296 and then Is_Unsigned_Type (Etype (L))
8297 and then R = N
8298 and then not Do_Overflow_Check (P))
8299 then
8300 Set_Is_Power_Of_2_For_Shift (N);
8301 return;
8302 end if;
8303 end;
8305 -- Here we just have 2 ** N on its own, so we can convert this to a
8306 -- shift node. We are prepared to deal with overflow here, and we
8307 -- also have to handle proper modular reduction for binary modular.
8309 else
8310 declare
8311 OK : Boolean;
8312 Lo : Uint;
8313 Hi : Uint;
8315 MaxS : Uint;
8316 -- Maximum shift count with no overflow
8318 TestS : Boolean;
8319 -- Set True if we must test the shift count
8321 Test_Gt : Node_Id;
8322 -- Node for test against TestS
8324 begin
8325 -- Compute maximum shift based on the underlying size. For a
8326 -- modular type this is one less than the size.
8328 if Is_Modular_Integer_Type (Typ) then
8330 -- For modular integer types, this is the size of the value
8331 -- being shifted minus one. Any larger values will cause
8332 -- modular reduction to a result of zero. Note that we do
8333 -- want the RM_Size here (e.g. mod 2 ** 7, we want a result
8334 -- of 6, since 2**7 should be reduced to zero).
8336 MaxS := RM_Size (Rtyp) - 1;
8338 -- For signed integer types, we use the size of the value
8339 -- being shifted minus 2. Larger values cause overflow.
8341 else
8342 MaxS := Esize (Rtyp) - 2;
8343 end if;
8345 -- Determine range to see if it can be larger than MaxS
8347 Determine_Range
8348 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
8349 TestS := (not OK) or else Hi > MaxS;
8351 -- Signed integer case
8353 if Is_Signed_Integer_Type (Typ) then
8355 -- Generate overflow check if overflow is active. Note that
8356 -- we can simply ignore the possibility of overflow if the
8357 -- flag is not set (means that overflow cannot happen or
8358 -- that overflow checks are suppressed).
8360 if Ovflo and TestS then
8361 Insert_Action (N,
8362 Make_Raise_Constraint_Error (Loc,
8363 Condition =>
8364 Make_Op_Gt (Loc,
8365 Left_Opnd => Duplicate_Subexpr (Right_Opnd (N)),
8366 Right_Opnd => Make_Integer_Literal (Loc, MaxS)),
8367 Reason => CE_Overflow_Check_Failed));
8368 end if;
8370 -- Now rewrite node as Shift_Left (1, right-operand)
8372 Rewrite (N,
8373 Make_Op_Shift_Left (Loc,
8374 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
8375 Right_Opnd => Right_Opnd (N)));
8377 -- Modular integer case
8379 else pragma Assert (Is_Modular_Integer_Type (Typ));
8381 -- If shift count can be greater than MaxS, we need to wrap
8382 -- the shift in a test that will reduce the result value to
8383 -- zero if this shift count is exceeded.
8385 if TestS then
8387 -- Note: build node for the comparison first, before we
8388 -- reuse the Right_Opnd, so that we have proper parents
8389 -- in place for the Duplicate_Subexpr call.
8391 Test_Gt :=
8392 Make_Op_Gt (Loc,
8393 Left_Opnd => Duplicate_Subexpr (Right_Opnd (N)),
8394 Right_Opnd => Make_Integer_Literal (Loc, MaxS));
8396 Rewrite (N,
8397 Make_If_Expression (Loc,
8398 Expressions => New_List (
8399 Test_Gt,
8400 Make_Integer_Literal (Loc, Uint_0),
8401 Make_Op_Shift_Left (Loc,
8402 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
8403 Right_Opnd => Right_Opnd (N)))));
8405 -- If we know shift count cannot be greater than MaxS, then
8406 -- it is safe to just rewrite as a shift with no test.
8408 else
8409 Rewrite (N,
8410 Make_Op_Shift_Left (Loc,
8411 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
8412 Right_Opnd => Right_Opnd (N)));
8413 end if;
8414 end if;
8416 Analyze_And_Resolve (N, Typ);
8417 return;
8418 end;
8419 end if;
8420 end if;
8422 -- Fall through if exponentiation must be done using a runtime routine
8424 -- First deal with modular case
8426 if Is_Modular_Integer_Type (Rtyp) then
8428 -- Nonbinary modular case, we call the special exponentiation
8429 -- routine for the nonbinary case, converting the argument to
8430 -- Long_Long_Integer and passing the modulus value. Then the
8431 -- result is converted back to the base type.
8433 if Non_Binary_Modulus (Rtyp) then
8434 Rewrite (N,
8435 Convert_To (Typ,
8436 Make_Function_Call (Loc,
8437 Name =>
8438 New_Occurrence_Of (RTE (RE_Exp_Modular), Loc),
8439 Parameter_Associations => New_List (
8440 Convert_To (RTE (RE_Unsigned), Base),
8441 Make_Integer_Literal (Loc, Modulus (Rtyp)),
8442 Exp))));
8444 -- Binary modular case, in this case, we call one of two routines,
8445 -- either the unsigned integer case, or the unsigned long long
8446 -- integer case, with a final "and" operation to do the required mod.
8448 else
8449 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
8450 Ent := RTE (RE_Exp_Unsigned);
8451 else
8452 Ent := RTE (RE_Exp_Long_Long_Unsigned);
8453 end if;
8455 Rewrite (N,
8456 Convert_To (Typ,
8457 Make_Op_And (Loc,
8458 Left_Opnd =>
8459 Make_Function_Call (Loc,
8460 Name => New_Occurrence_Of (Ent, Loc),
8461 Parameter_Associations => New_List (
8462 Convert_To (Etype (First_Formal (Ent)), Base),
8463 Exp)),
8464 Right_Opnd =>
8465 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
8467 end if;
8469 -- Common exit point for modular type case
8471 Analyze_And_Resolve (N, Typ);
8472 return;
8474 -- Signed integer cases, done using either Integer or Long_Long_Integer.
8475 -- It is not worth having routines for Short_[Short_]Integer, since for
8476 -- most machines it would not help, and it would generate more code that
8477 -- might need certification when a certified run time is required.
8479 -- In the integer cases, we have two routines, one for when overflow
8480 -- checks are required, and one when they are not required, since there
8481 -- is a real gain in omitting checks on many machines.
8483 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
8484 or else (Rtyp = Base_Type (Standard_Long_Integer)
8485 and then
8486 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
8487 or else Rtyp = Universal_Integer
8488 then
8489 Etyp := Standard_Long_Long_Integer;
8491 if Ovflo then
8492 Rent := RE_Exp_Long_Long_Integer;
8493 else
8494 Rent := RE_Exn_Long_Long_Integer;
8495 end if;
8497 elsif Is_Signed_Integer_Type (Rtyp) then
8498 Etyp := Standard_Integer;
8500 if Ovflo then
8501 Rent := RE_Exp_Integer;
8502 else
8503 Rent := RE_Exn_Integer;
8504 end if;
8506 -- Floating-point cases. We do not need separate routines for the
8507 -- overflow case here, since in the case of floating-point, we generate
8508 -- infinities anyway as a rule (either that or we automatically trap
8509 -- overflow), and if there is an infinity generated and a range check
8510 -- is required, the check will fail anyway.
8512 -- Historical note: we used to convert everything to Long_Long_Float
8513 -- and call a single common routine, but this had the undesirable effect
8514 -- of giving different results for small static exponent values and the
8515 -- same dynamic values.
8517 else
8518 pragma Assert (Is_Floating_Point_Type (Rtyp));
8520 if Rtyp = Standard_Float then
8521 Etyp := Standard_Float;
8522 Rent := RE_Exn_Float;
8524 elsif Rtyp = Standard_Long_Float then
8525 Etyp := Standard_Long_Float;
8526 Rent := RE_Exn_Long_Float;
8528 else
8529 Etyp := Standard_Long_Long_Float;
8530 Rent := RE_Exn_Long_Long_Float;
8531 end if;
8532 end if;
8534 -- Common processing for integer cases and floating-point cases.
8535 -- If we are in the right type, we can call runtime routine directly
8537 if Typ = Etyp
8538 and then Rtyp /= Universal_Integer
8539 and then Rtyp /= Universal_Real
8540 then
8541 Rewrite (N,
8542 Wrap_MA (
8543 Make_Function_Call (Loc,
8544 Name => New_Occurrence_Of (RTE (Rent), Loc),
8545 Parameter_Associations => New_List (Base, Exp))));
8547 -- Otherwise we have to introduce conversions (conversions are also
8548 -- required in the universal cases, since the runtime routine is
8549 -- typed using one of the standard types).
8551 else
8552 Rewrite (N,
8553 Convert_To (Typ,
8554 Make_Function_Call (Loc,
8555 Name => New_Occurrence_Of (RTE (Rent), Loc),
8556 Parameter_Associations => New_List (
8557 Convert_To (Etyp, Base),
8558 Exp))));
8559 end if;
8561 Analyze_And_Resolve (N, Typ);
8562 return;
8564 exception
8565 when RE_Not_Available =>
8566 return;
8567 end Expand_N_Op_Expon;
8569 --------------------
8570 -- Expand_N_Op_Ge --
8571 --------------------
8573 procedure Expand_N_Op_Ge (N : Node_Id) is
8574 Typ : constant Entity_Id := Etype (N);
8575 Op1 : constant Node_Id := Left_Opnd (N);
8576 Op2 : constant Node_Id := Right_Opnd (N);
8577 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8579 begin
8580 Binary_Op_Validity_Checks (N);
8582 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8583 -- means we no longer have a comparison operation, we are all done.
8585 Expand_Compare_Minimize_Eliminate_Overflow (N);
8587 if Nkind (N) /= N_Op_Ge then
8588 return;
8589 end if;
8591 -- Array type case
8593 if Is_Array_Type (Typ1) then
8594 Expand_Array_Comparison (N);
8595 return;
8596 end if;
8598 -- Deal with boolean operands
8600 if Is_Boolean_Type (Typ1) then
8601 Adjust_Condition (Op1);
8602 Adjust_Condition (Op2);
8603 Set_Etype (N, Standard_Boolean);
8604 Adjust_Result_Type (N, Typ);
8605 end if;
8607 Rewrite_Comparison (N);
8609 Optimize_Length_Comparison (N);
8610 end Expand_N_Op_Ge;
8612 --------------------
8613 -- Expand_N_Op_Gt --
8614 --------------------
8616 procedure Expand_N_Op_Gt (N : Node_Id) is
8617 Typ : constant Entity_Id := Etype (N);
8618 Op1 : constant Node_Id := Left_Opnd (N);
8619 Op2 : constant Node_Id := Right_Opnd (N);
8620 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8622 begin
8623 Binary_Op_Validity_Checks (N);
8625 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8626 -- means we no longer have a comparison operation, we are all done.
8628 Expand_Compare_Minimize_Eliminate_Overflow (N);
8630 if Nkind (N) /= N_Op_Gt then
8631 return;
8632 end if;
8634 -- Deal with array type operands
8636 if Is_Array_Type (Typ1) then
8637 Expand_Array_Comparison (N);
8638 return;
8639 end if;
8641 -- Deal with boolean type operands
8643 if Is_Boolean_Type (Typ1) then
8644 Adjust_Condition (Op1);
8645 Adjust_Condition (Op2);
8646 Set_Etype (N, Standard_Boolean);
8647 Adjust_Result_Type (N, Typ);
8648 end if;
8650 Rewrite_Comparison (N);
8652 Optimize_Length_Comparison (N);
8653 end Expand_N_Op_Gt;
8655 --------------------
8656 -- Expand_N_Op_Le --
8657 --------------------
8659 procedure Expand_N_Op_Le (N : Node_Id) is
8660 Typ : constant Entity_Id := Etype (N);
8661 Op1 : constant Node_Id := Left_Opnd (N);
8662 Op2 : constant Node_Id := Right_Opnd (N);
8663 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8665 begin
8666 Binary_Op_Validity_Checks (N);
8668 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8669 -- means we no longer have a comparison operation, we are all done.
8671 Expand_Compare_Minimize_Eliminate_Overflow (N);
8673 if Nkind (N) /= N_Op_Le then
8674 return;
8675 end if;
8677 -- Deal with array type operands
8679 if Is_Array_Type (Typ1) then
8680 Expand_Array_Comparison (N);
8681 return;
8682 end if;
8684 -- Deal with Boolean type operands
8686 if Is_Boolean_Type (Typ1) then
8687 Adjust_Condition (Op1);
8688 Adjust_Condition (Op2);
8689 Set_Etype (N, Standard_Boolean);
8690 Adjust_Result_Type (N, Typ);
8691 end if;
8693 Rewrite_Comparison (N);
8695 Optimize_Length_Comparison (N);
8696 end Expand_N_Op_Le;
8698 --------------------
8699 -- Expand_N_Op_Lt --
8700 --------------------
8702 procedure Expand_N_Op_Lt (N : Node_Id) is
8703 Typ : constant Entity_Id := Etype (N);
8704 Op1 : constant Node_Id := Left_Opnd (N);
8705 Op2 : constant Node_Id := Right_Opnd (N);
8706 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8708 begin
8709 Binary_Op_Validity_Checks (N);
8711 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8712 -- means we no longer have a comparison operation, we are all done.
8714 Expand_Compare_Minimize_Eliminate_Overflow (N);
8716 if Nkind (N) /= N_Op_Lt then
8717 return;
8718 end if;
8720 -- Deal with array type operands
8722 if Is_Array_Type (Typ1) then
8723 Expand_Array_Comparison (N);
8724 return;
8725 end if;
8727 -- Deal with Boolean type operands
8729 if Is_Boolean_Type (Typ1) then
8730 Adjust_Condition (Op1);
8731 Adjust_Condition (Op2);
8732 Set_Etype (N, Standard_Boolean);
8733 Adjust_Result_Type (N, Typ);
8734 end if;
8736 Rewrite_Comparison (N);
8738 Optimize_Length_Comparison (N);
8739 end Expand_N_Op_Lt;
8741 -----------------------
8742 -- Expand_N_Op_Minus --
8743 -----------------------
8745 procedure Expand_N_Op_Minus (N : Node_Id) is
8746 Loc : constant Source_Ptr := Sloc (N);
8747 Typ : constant Entity_Id := Etype (N);
8749 begin
8750 Unary_Op_Validity_Checks (N);
8752 -- Check for MINIMIZED/ELIMINATED overflow mode
8754 if Minimized_Eliminated_Overflow_Check (N) then
8755 Apply_Arithmetic_Overflow_Check (N);
8756 return;
8757 end if;
8759 if not Backend_Overflow_Checks_On_Target
8760 and then Is_Signed_Integer_Type (Etype (N))
8761 and then Do_Overflow_Check (N)
8762 then
8763 -- Software overflow checking expands -expr into (0 - expr)
8765 Rewrite (N,
8766 Make_Op_Subtract (Loc,
8767 Left_Opnd => Make_Integer_Literal (Loc, 0),
8768 Right_Opnd => Right_Opnd (N)));
8770 Analyze_And_Resolve (N, Typ);
8771 end if;
8773 Expand_Nonbinary_Modular_Op (N);
8774 end Expand_N_Op_Minus;
8776 ---------------------
8777 -- Expand_N_Op_Mod --
8778 ---------------------
8780 procedure Expand_N_Op_Mod (N : Node_Id) is
8781 Loc : constant Source_Ptr := Sloc (N);
8782 Typ : constant Entity_Id := Etype (N);
8783 DDC : constant Boolean := Do_Division_Check (N);
8785 Left : Node_Id;
8786 Right : Node_Id;
8788 LLB : Uint;
8789 Llo : Uint;
8790 Lhi : Uint;
8791 LOK : Boolean;
8792 Rlo : Uint;
8793 Rhi : Uint;
8794 ROK : Boolean;
8796 pragma Warnings (Off, Lhi);
8798 begin
8799 Binary_Op_Validity_Checks (N);
8801 -- Check for MINIMIZED/ELIMINATED overflow mode
8803 if Minimized_Eliminated_Overflow_Check (N) then
8804 Apply_Arithmetic_Overflow_Check (N);
8805 return;
8806 end if;
8808 if Is_Integer_Type (Etype (N)) then
8809 Apply_Divide_Checks (N);
8811 -- All done if we don't have a MOD any more, which can happen as a
8812 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8814 if Nkind (N) /= N_Op_Mod then
8815 return;
8816 end if;
8817 end if;
8819 -- Proceed with expansion of mod operator
8821 Left := Left_Opnd (N);
8822 Right := Right_Opnd (N);
8824 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
8825 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
8827 -- Convert mod to rem if operands are both known to be non-negative, or
8828 -- both known to be non-positive (these are the cases in which rem and
8829 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8830 -- likely that this will improve the quality of code, (the operation now
8831 -- corresponds to the hardware remainder), and it does not seem likely
8832 -- that it could be harmful. It also avoids some cases of the elaborate
8833 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8835 if (LOK and ROK)
8836 and then ((Llo >= 0 and then Rlo >= 0)
8837 or else
8838 (Lhi <= 0 and then Rhi <= 0))
8839 then
8840 Rewrite (N,
8841 Make_Op_Rem (Sloc (N),
8842 Left_Opnd => Left_Opnd (N),
8843 Right_Opnd => Right_Opnd (N)));
8845 -- Instead of reanalyzing the node we do the analysis manually. This
8846 -- avoids anomalies when the replacement is done in an instance and
8847 -- is epsilon more efficient.
8849 Set_Entity (N, Standard_Entity (S_Op_Rem));
8850 Set_Etype (N, Typ);
8851 Set_Do_Division_Check (N, DDC);
8852 Expand_N_Op_Rem (N);
8853 Set_Analyzed (N);
8854 return;
8856 -- Otherwise, normal mod processing
8858 else
8859 -- Apply optimization x mod 1 = 0. We don't really need that with
8860 -- gcc, but it is useful with other back ends and is certainly
8861 -- harmless.
8863 if Is_Integer_Type (Etype (N))
8864 and then Compile_Time_Known_Value (Right)
8865 and then Expr_Value (Right) = Uint_1
8866 then
8867 -- Call Remove_Side_Effects to ensure that any side effects in
8868 -- the ignored left operand (in particular function calls to
8869 -- user defined functions) are properly executed.
8871 Remove_Side_Effects (Left);
8873 Rewrite (N, Make_Integer_Literal (Loc, 0));
8874 Analyze_And_Resolve (N, Typ);
8875 return;
8876 end if;
8878 -- If we still have a mod operator and we are in Modify_Tree_For_C
8879 -- mode, and we have a signed integer type, then here is where we do
8880 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8881 -- for the special handling of the annoying case of largest negative
8882 -- number mod minus one.
8884 if Nkind (N) = N_Op_Mod
8885 and then Is_Signed_Integer_Type (Typ)
8886 and then Modify_Tree_For_C
8887 then
8888 -- In the general case, we expand A mod B as
8890 -- Tnn : constant typ := A rem B;
8891 -- ..
8892 -- (if (A >= 0) = (B >= 0) then Tnn
8893 -- elsif Tnn = 0 then 0
8894 -- else Tnn + B)
8896 -- The comparison can be written simply as A >= 0 if we know that
8897 -- B >= 0 which is a very common case.
8899 -- An important optimization is when B is known at compile time
8900 -- to be 2**K for some constant. In this case we can simply AND
8901 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8902 -- and that works for both the positive and negative cases.
8904 declare
8905 P2 : constant Nat := Power_Of_Two (Right);
8907 begin
8908 if P2 /= 0 then
8909 Rewrite (N,
8910 Unchecked_Convert_To (Typ,
8911 Make_Op_And (Loc,
8912 Left_Opnd =>
8913 Unchecked_Convert_To
8914 (Corresponding_Unsigned_Type (Typ), Left),
8915 Right_Opnd =>
8916 Make_Integer_Literal (Loc, 2 ** P2 - 1))));
8917 Analyze_And_Resolve (N, Typ);
8918 return;
8919 end if;
8920 end;
8922 -- Here for the full rewrite
8924 declare
8925 Tnn : constant Entity_Id := Make_Temporary (Sloc (N), 'T', N);
8926 Cmp : Node_Id;
8928 begin
8929 Cmp :=
8930 Make_Op_Ge (Loc,
8931 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
8932 Right_Opnd => Make_Integer_Literal (Loc, 0));
8934 if not LOK or else Rlo < 0 then
8935 Cmp :=
8936 Make_Op_Eq (Loc,
8937 Left_Opnd => Cmp,
8938 Right_Opnd =>
8939 Make_Op_Ge (Loc,
8940 Left_Opnd => Duplicate_Subexpr_No_Checks (Right),
8941 Right_Opnd => Make_Integer_Literal (Loc, 0)));
8942 end if;
8944 Insert_Action (N,
8945 Make_Object_Declaration (Loc,
8946 Defining_Identifier => Tnn,
8947 Constant_Present => True,
8948 Object_Definition => New_Occurrence_Of (Typ, Loc),
8949 Expression =>
8950 Make_Op_Rem (Loc,
8951 Left_Opnd => Left,
8952 Right_Opnd => Right)));
8954 Rewrite (N,
8955 Make_If_Expression (Loc,
8956 Expressions => New_List (
8957 Cmp,
8958 New_Occurrence_Of (Tnn, Loc),
8959 Make_If_Expression (Loc,
8960 Is_Elsif => True,
8961 Expressions => New_List (
8962 Make_Op_Eq (Loc,
8963 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8964 Right_Opnd => Make_Integer_Literal (Loc, 0)),
8965 Make_Integer_Literal (Loc, 0),
8966 Make_Op_Add (Loc,
8967 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8968 Right_Opnd =>
8969 Duplicate_Subexpr_No_Checks (Right)))))));
8971 Analyze_And_Resolve (N, Typ);
8972 return;
8973 end;
8974 end if;
8976 -- Deal with annoying case of largest negative number mod minus one.
8977 -- Gigi may not handle this case correctly, because on some targets,
8978 -- the mod value is computed using a divide instruction which gives
8979 -- an overflow trap for this case.
8981 -- It would be a bit more efficient to figure out which targets
8982 -- this is really needed for, but in practice it is reasonable
8983 -- to do the following special check in all cases, since it means
8984 -- we get a clearer message, and also the overhead is minimal given
8985 -- that division is expensive in any case.
8987 -- In fact the check is quite easy, if the right operand is -1, then
8988 -- the mod value is always 0, and we can just ignore the left operand
8989 -- completely in this case.
8991 -- This only applies if we still have a mod operator. Skip if we
8992 -- have already rewritten this (e.g. in the case of eliminated
8993 -- overflow checks which have driven us into bignum mode).
8995 if Nkind (N) = N_Op_Mod then
8997 -- The operand type may be private (e.g. in the expansion of an
8998 -- intrinsic operation) so we must use the underlying type to get
8999 -- the bounds, and convert the literals explicitly.
9001 LLB :=
9002 Expr_Value
9003 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
9005 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
9006 and then ((not LOK) or else (Llo = LLB))
9007 then
9008 Rewrite (N,
9009 Make_If_Expression (Loc,
9010 Expressions => New_List (
9011 Make_Op_Eq (Loc,
9012 Left_Opnd => Duplicate_Subexpr (Right),
9013 Right_Opnd =>
9014 Unchecked_Convert_To (Typ,
9015 Make_Integer_Literal (Loc, -1))),
9016 Unchecked_Convert_To (Typ,
9017 Make_Integer_Literal (Loc, Uint_0)),
9018 Relocate_Node (N))));
9020 Set_Analyzed (Next (Next (First (Expressions (N)))));
9021 Analyze_And_Resolve (N, Typ);
9022 end if;
9023 end if;
9024 end if;
9025 end Expand_N_Op_Mod;
9027 --------------------------
9028 -- Expand_N_Op_Multiply --
9029 --------------------------
9031 procedure Expand_N_Op_Multiply (N : Node_Id) is
9032 Loc : constant Source_Ptr := Sloc (N);
9033 Lop : constant Node_Id := Left_Opnd (N);
9034 Rop : constant Node_Id := Right_Opnd (N);
9036 Lp2 : constant Boolean :=
9037 Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop);
9038 Rp2 : constant Boolean :=
9039 Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop);
9041 Ltyp : constant Entity_Id := Etype (Lop);
9042 Rtyp : constant Entity_Id := Etype (Rop);
9043 Typ : Entity_Id := Etype (N);
9045 begin
9046 Binary_Op_Validity_Checks (N);
9048 -- Check for MINIMIZED/ELIMINATED overflow mode
9050 if Minimized_Eliminated_Overflow_Check (N) then
9051 Apply_Arithmetic_Overflow_Check (N);
9052 return;
9053 end if;
9055 -- Special optimizations for integer types
9057 if Is_Integer_Type (Typ) then
9059 -- N * 0 = 0 for integer types
9061 if Compile_Time_Known_Value (Rop)
9062 and then Expr_Value (Rop) = Uint_0
9063 then
9064 -- Call Remove_Side_Effects to ensure that any side effects in
9065 -- the ignored left operand (in particular function calls to
9066 -- user defined functions) are properly executed.
9068 Remove_Side_Effects (Lop);
9070 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
9071 Analyze_And_Resolve (N, Typ);
9072 return;
9073 end if;
9075 -- Similar handling for 0 * N = 0
9077 if Compile_Time_Known_Value (Lop)
9078 and then Expr_Value (Lop) = Uint_0
9079 then
9080 Remove_Side_Effects (Rop);
9081 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
9082 Analyze_And_Resolve (N, Typ);
9083 return;
9084 end if;
9086 -- N * 1 = 1 * N = N for integer types
9088 -- This optimisation is not done if we are going to
9089 -- rewrite the product 1 * 2 ** N to a shift.
9091 if Compile_Time_Known_Value (Rop)
9092 and then Expr_Value (Rop) = Uint_1
9093 and then not Lp2
9094 then
9095 Rewrite (N, Lop);
9096 return;
9098 elsif Compile_Time_Known_Value (Lop)
9099 and then Expr_Value (Lop) = Uint_1
9100 and then not Rp2
9101 then
9102 Rewrite (N, Rop);
9103 return;
9104 end if;
9105 end if;
9107 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
9108 -- Is_Power_Of_2_For_Shift is set means that we know that our left
9109 -- operand is an integer, as required for this to work.
9111 if Rp2 then
9112 if Lp2 then
9114 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
9116 Rewrite (N,
9117 Make_Op_Expon (Loc,
9118 Left_Opnd => Make_Integer_Literal (Loc, 2),
9119 Right_Opnd =>
9120 Make_Op_Add (Loc,
9121 Left_Opnd => Right_Opnd (Lop),
9122 Right_Opnd => Right_Opnd (Rop))));
9123 Analyze_And_Resolve (N, Typ);
9124 return;
9126 else
9127 -- If the result is modular, perform the reduction of the result
9128 -- appropriately.
9130 if Is_Modular_Integer_Type (Typ)
9131 and then not Non_Binary_Modulus (Typ)
9132 then
9133 Rewrite (N,
9134 Make_Op_And (Loc,
9135 Left_Opnd =>
9136 Make_Op_Shift_Left (Loc,
9137 Left_Opnd => Lop,
9138 Right_Opnd =>
9139 Convert_To (Standard_Natural, Right_Opnd (Rop))),
9140 Right_Opnd =>
9141 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
9143 else
9144 Rewrite (N,
9145 Make_Op_Shift_Left (Loc,
9146 Left_Opnd => Lop,
9147 Right_Opnd =>
9148 Convert_To (Standard_Natural, Right_Opnd (Rop))));
9149 end if;
9151 Analyze_And_Resolve (N, Typ);
9152 return;
9153 end if;
9155 -- Same processing for the operands the other way round
9157 elsif Lp2 then
9158 if Is_Modular_Integer_Type (Typ)
9159 and then not Non_Binary_Modulus (Typ)
9160 then
9161 Rewrite (N,
9162 Make_Op_And (Loc,
9163 Left_Opnd =>
9164 Make_Op_Shift_Left (Loc,
9165 Left_Opnd => Rop,
9166 Right_Opnd =>
9167 Convert_To (Standard_Natural, Right_Opnd (Lop))),
9168 Right_Opnd =>
9169 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
9171 else
9172 Rewrite (N,
9173 Make_Op_Shift_Left (Loc,
9174 Left_Opnd => Rop,
9175 Right_Opnd =>
9176 Convert_To (Standard_Natural, Right_Opnd (Lop))));
9177 end if;
9179 Analyze_And_Resolve (N, Typ);
9180 return;
9181 end if;
9183 -- Do required fixup of universal fixed operation
9185 if Typ = Universal_Fixed then
9186 Fixup_Universal_Fixed_Operation (N);
9187 Typ := Etype (N);
9188 end if;
9190 -- Multiplications with fixed-point results
9192 if Is_Fixed_Point_Type (Typ) then
9194 -- No special processing if Treat_Fixed_As_Integer is set, since from
9195 -- a semantic point of view such operations are simply integer
9196 -- operations and will be treated that way.
9198 if not Treat_Fixed_As_Integer (N) then
9200 -- Case of fixed * integer => fixed
9202 if Is_Integer_Type (Rtyp) then
9203 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
9205 -- Case of integer * fixed => fixed
9207 elsif Is_Integer_Type (Ltyp) then
9208 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
9210 -- Case of fixed * fixed => fixed
9212 else
9213 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
9214 end if;
9215 end if;
9217 -- Other cases of multiplication of fixed-point operands. Again we
9218 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
9220 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
9221 and then not Treat_Fixed_As_Integer (N)
9222 then
9223 if Is_Integer_Type (Typ) then
9224 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
9225 else
9226 pragma Assert (Is_Floating_Point_Type (Typ));
9227 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
9228 end if;
9230 -- Mixed-mode operations can appear in a non-static universal context,
9231 -- in which case the integer argument must be converted explicitly.
9233 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
9234 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
9235 Analyze_And_Resolve (Rop, Universal_Real);
9237 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
9238 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
9239 Analyze_And_Resolve (Lop, Universal_Real);
9241 -- Non-fixed point cases, check software overflow checking required
9243 elsif Is_Signed_Integer_Type (Etype (N)) then
9244 Apply_Arithmetic_Overflow_Check (N);
9245 end if;
9247 -- Overflow checks for floating-point if -gnateF mode active
9249 Check_Float_Op_Overflow (N);
9251 Expand_Nonbinary_Modular_Op (N);
9252 end Expand_N_Op_Multiply;
9254 --------------------
9255 -- Expand_N_Op_Ne --
9256 --------------------
9258 procedure Expand_N_Op_Ne (N : Node_Id) is
9259 Typ : constant Entity_Id := Etype (Left_Opnd (N));
9261 begin
9262 -- Case of elementary type with standard operator
9264 if Is_Elementary_Type (Typ)
9265 and then Sloc (Entity (N)) = Standard_Location
9266 then
9267 Binary_Op_Validity_Checks (N);
9269 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
9270 -- means we no longer have a /= operation, we are all done.
9272 Expand_Compare_Minimize_Eliminate_Overflow (N);
9274 if Nkind (N) /= N_Op_Ne then
9275 return;
9276 end if;
9278 -- Boolean types (requiring handling of non-standard case)
9280 if Is_Boolean_Type (Typ) then
9281 Adjust_Condition (Left_Opnd (N));
9282 Adjust_Condition (Right_Opnd (N));
9283 Set_Etype (N, Standard_Boolean);
9284 Adjust_Result_Type (N, Typ);
9285 end if;
9287 Rewrite_Comparison (N);
9289 -- For all cases other than elementary types, we rewrite node as the
9290 -- negation of an equality operation, and reanalyze. The equality to be
9291 -- used is defined in the same scope and has the same signature. This
9292 -- signature must be set explicitly since in an instance it may not have
9293 -- the same visibility as in the generic unit. This avoids duplicating
9294 -- or factoring the complex code for record/array equality tests etc.
9296 -- This case is also used for the minimal expansion performed in
9297 -- GNATprove mode.
9299 else
9300 declare
9301 Loc : constant Source_Ptr := Sloc (N);
9302 Neg : Node_Id;
9303 Ne : constant Entity_Id := Entity (N);
9305 begin
9306 Binary_Op_Validity_Checks (N);
9308 Neg :=
9309 Make_Op_Not (Loc,
9310 Right_Opnd =>
9311 Make_Op_Eq (Loc,
9312 Left_Opnd => Left_Opnd (N),
9313 Right_Opnd => Right_Opnd (N)));
9315 -- The level of parentheses is useless in GNATprove mode, and
9316 -- bumping its level here leads to wrong columns being used in
9317 -- check messages, hence skip it in this mode.
9319 if not GNATprove_Mode then
9320 Set_Paren_Count (Right_Opnd (Neg), 1);
9321 end if;
9323 if Scope (Ne) /= Standard_Standard then
9324 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
9325 end if;
9327 -- For navigation purposes, we want to treat the inequality as an
9328 -- implicit reference to the corresponding equality. Preserve the
9329 -- Comes_From_ source flag to generate proper Xref entries.
9331 Preserve_Comes_From_Source (Neg, N);
9332 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
9333 Rewrite (N, Neg);
9334 Analyze_And_Resolve (N, Standard_Boolean);
9335 end;
9336 end if;
9338 -- No need for optimization in GNATprove mode, where we would rather see
9339 -- the original source expression.
9341 if not GNATprove_Mode then
9342 Optimize_Length_Comparison (N);
9343 end if;
9344 end Expand_N_Op_Ne;
9346 ---------------------
9347 -- Expand_N_Op_Not --
9348 ---------------------
9350 -- If the argument is other than a Boolean array type, there is no special
9351 -- expansion required, except for dealing with validity checks, and non-
9352 -- standard boolean representations.
9354 -- For the packed array case, we call the special routine in Exp_Pakd,
9355 -- except that if the component size is greater than one, we use the
9356 -- standard routine generating a gruesome loop (it is so peculiar to have
9357 -- packed arrays with non-standard Boolean representations anyway, so it
9358 -- does not matter that we do not handle this case efficiently).
9360 -- For the unpacked array case (and for the special packed case where we
9361 -- have non standard Booleans, as discussed above), we generate and insert
9362 -- into the tree the following function definition:
9364 -- function Nnnn (A : arr) is
9365 -- B : arr;
9366 -- begin
9367 -- for J in a'range loop
9368 -- B (J) := not A (J);
9369 -- end loop;
9370 -- return B;
9371 -- end Nnnn;
9373 -- Here arr is the actual subtype of the parameter (and hence always
9374 -- constrained). Then we replace the not with a call to this function.
9376 procedure Expand_N_Op_Not (N : Node_Id) is
9377 Loc : constant Source_Ptr := Sloc (N);
9378 Typ : constant Entity_Id := Etype (N);
9379 Opnd : Node_Id;
9380 Arr : Entity_Id;
9381 A : Entity_Id;
9382 B : Entity_Id;
9383 J : Entity_Id;
9384 A_J : Node_Id;
9385 B_J : Node_Id;
9387 Func_Name : Entity_Id;
9388 Loop_Statement : Node_Id;
9390 begin
9391 Unary_Op_Validity_Checks (N);
9393 -- For boolean operand, deal with non-standard booleans
9395 if Is_Boolean_Type (Typ) then
9396 Adjust_Condition (Right_Opnd (N));
9397 Set_Etype (N, Standard_Boolean);
9398 Adjust_Result_Type (N, Typ);
9399 return;
9400 end if;
9402 -- Only array types need any other processing
9404 if not Is_Array_Type (Typ) then
9405 return;
9406 end if;
9408 -- Case of array operand. If bit packed with a component size of 1,
9409 -- handle it in Exp_Pakd if the operand is known to be aligned.
9411 if Is_Bit_Packed_Array (Typ)
9412 and then Component_Size (Typ) = 1
9413 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
9414 then
9415 Expand_Packed_Not (N);
9416 return;
9417 end if;
9419 -- Case of array operand which is not bit-packed. If the context is
9420 -- a safe assignment, call in-place operation, If context is a larger
9421 -- boolean expression in the context of a safe assignment, expansion is
9422 -- done by enclosing operation.
9424 Opnd := Relocate_Node (Right_Opnd (N));
9425 Convert_To_Actual_Subtype (Opnd);
9426 Arr := Etype (Opnd);
9427 Ensure_Defined (Arr, N);
9428 Silly_Boolean_Array_Not_Test (N, Arr);
9430 if Nkind (Parent (N)) = N_Assignment_Statement then
9431 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
9432 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
9433 return;
9435 -- Special case the negation of a binary operation
9437 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
9438 and then Safe_In_Place_Array_Op
9439 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
9440 then
9441 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
9442 return;
9443 end if;
9445 elsif Nkind (Parent (N)) in N_Binary_Op
9446 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
9447 then
9448 declare
9449 Op1 : constant Node_Id := Left_Opnd (Parent (N));
9450 Op2 : constant Node_Id := Right_Opnd (Parent (N));
9451 Lhs : constant Node_Id := Name (Parent (Parent (N)));
9453 begin
9454 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
9456 -- (not A) op (not B) can be reduced to a single call
9458 if N = Op1 and then Nkind (Op2) = N_Op_Not then
9459 return;
9461 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
9462 return;
9464 -- A xor (not B) can also be special-cased
9466 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
9467 return;
9468 end if;
9469 end if;
9470 end;
9471 end if;
9473 A := Make_Defining_Identifier (Loc, Name_uA);
9474 B := Make_Defining_Identifier (Loc, Name_uB);
9475 J := Make_Defining_Identifier (Loc, Name_uJ);
9477 A_J :=
9478 Make_Indexed_Component (Loc,
9479 Prefix => New_Occurrence_Of (A, Loc),
9480 Expressions => New_List (New_Occurrence_Of (J, Loc)));
9482 B_J :=
9483 Make_Indexed_Component (Loc,
9484 Prefix => New_Occurrence_Of (B, Loc),
9485 Expressions => New_List (New_Occurrence_Of (J, Loc)));
9487 Loop_Statement :=
9488 Make_Implicit_Loop_Statement (N,
9489 Identifier => Empty,
9491 Iteration_Scheme =>
9492 Make_Iteration_Scheme (Loc,
9493 Loop_Parameter_Specification =>
9494 Make_Loop_Parameter_Specification (Loc,
9495 Defining_Identifier => J,
9496 Discrete_Subtype_Definition =>
9497 Make_Attribute_Reference (Loc,
9498 Prefix => Make_Identifier (Loc, Chars (A)),
9499 Attribute_Name => Name_Range))),
9501 Statements => New_List (
9502 Make_Assignment_Statement (Loc,
9503 Name => B_J,
9504 Expression => Make_Op_Not (Loc, A_J))));
9506 Func_Name := Make_Temporary (Loc, 'N');
9507 Set_Is_Inlined (Func_Name);
9509 Insert_Action (N,
9510 Make_Subprogram_Body (Loc,
9511 Specification =>
9512 Make_Function_Specification (Loc,
9513 Defining_Unit_Name => Func_Name,
9514 Parameter_Specifications => New_List (
9515 Make_Parameter_Specification (Loc,
9516 Defining_Identifier => A,
9517 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9518 Result_Definition => New_Occurrence_Of (Typ, Loc)),
9520 Declarations => New_List (
9521 Make_Object_Declaration (Loc,
9522 Defining_Identifier => B,
9523 Object_Definition => New_Occurrence_Of (Arr, Loc))),
9525 Handled_Statement_Sequence =>
9526 Make_Handled_Sequence_Of_Statements (Loc,
9527 Statements => New_List (
9528 Loop_Statement,
9529 Make_Simple_Return_Statement (Loc,
9530 Expression => Make_Identifier (Loc, Chars (B)))))));
9532 Rewrite (N,
9533 Make_Function_Call (Loc,
9534 Name => New_Occurrence_Of (Func_Name, Loc),
9535 Parameter_Associations => New_List (Opnd)));
9537 Analyze_And_Resolve (N, Typ);
9538 end Expand_N_Op_Not;
9540 --------------------
9541 -- Expand_N_Op_Or --
9542 --------------------
9544 procedure Expand_N_Op_Or (N : Node_Id) is
9545 Typ : constant Entity_Id := Etype (N);
9547 begin
9548 Binary_Op_Validity_Checks (N);
9550 if Is_Array_Type (Etype (N)) then
9551 Expand_Boolean_Operator (N);
9553 elsif Is_Boolean_Type (Etype (N)) then
9554 Adjust_Condition (Left_Opnd (N));
9555 Adjust_Condition (Right_Opnd (N));
9556 Set_Etype (N, Standard_Boolean);
9557 Adjust_Result_Type (N, Typ);
9559 elsif Is_Intrinsic_Subprogram (Entity (N)) then
9560 Expand_Intrinsic_Call (N, Entity (N));
9561 end if;
9563 Expand_Nonbinary_Modular_Op (N);
9564 end Expand_N_Op_Or;
9566 ----------------------
9567 -- Expand_N_Op_Plus --
9568 ----------------------
9570 procedure Expand_N_Op_Plus (N : Node_Id) is
9571 begin
9572 Unary_Op_Validity_Checks (N);
9574 -- Check for MINIMIZED/ELIMINATED overflow mode
9576 if Minimized_Eliminated_Overflow_Check (N) then
9577 Apply_Arithmetic_Overflow_Check (N);
9578 return;
9579 end if;
9580 end Expand_N_Op_Plus;
9582 ---------------------
9583 -- Expand_N_Op_Rem --
9584 ---------------------
9586 procedure Expand_N_Op_Rem (N : Node_Id) is
9587 Loc : constant Source_Ptr := Sloc (N);
9588 Typ : constant Entity_Id := Etype (N);
9590 Left : Node_Id;
9591 Right : Node_Id;
9593 Lo : Uint;
9594 Hi : Uint;
9595 OK : Boolean;
9597 Lneg : Boolean;
9598 Rneg : Boolean;
9599 -- Set if corresponding operand can be negative
9601 pragma Unreferenced (Hi);
9603 begin
9604 Binary_Op_Validity_Checks (N);
9606 -- Check for MINIMIZED/ELIMINATED overflow mode
9608 if Minimized_Eliminated_Overflow_Check (N) then
9609 Apply_Arithmetic_Overflow_Check (N);
9610 return;
9611 end if;
9613 if Is_Integer_Type (Etype (N)) then
9614 Apply_Divide_Checks (N);
9616 -- All done if we don't have a REM any more, which can happen as a
9617 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
9619 if Nkind (N) /= N_Op_Rem then
9620 return;
9621 end if;
9622 end if;
9624 -- Proceed with expansion of REM
9626 Left := Left_Opnd (N);
9627 Right := Right_Opnd (N);
9629 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
9630 -- but it is useful with other back ends, and is certainly harmless.
9632 if Is_Integer_Type (Etype (N))
9633 and then Compile_Time_Known_Value (Right)
9634 and then Expr_Value (Right) = Uint_1
9635 then
9636 -- Call Remove_Side_Effects to ensure that any side effects in the
9637 -- ignored left operand (in particular function calls to user defined
9638 -- functions) are properly executed.
9640 Remove_Side_Effects (Left);
9642 Rewrite (N, Make_Integer_Literal (Loc, 0));
9643 Analyze_And_Resolve (N, Typ);
9644 return;
9645 end if;
9647 -- Deal with annoying case of largest negative number remainder minus
9648 -- one. Gigi may not handle this case correctly, because on some
9649 -- targets, the mod value is computed using a divide instruction
9650 -- which gives an overflow trap for this case.
9652 -- It would be a bit more efficient to figure out which targets this
9653 -- is really needed for, but in practice it is reasonable to do the
9654 -- following special check in all cases, since it means we get a clearer
9655 -- message, and also the overhead is minimal given that division is
9656 -- expensive in any case.
9658 -- In fact the check is quite easy, if the right operand is -1, then
9659 -- the remainder is always 0, and we can just ignore the left operand
9660 -- completely in this case.
9662 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9663 Lneg := (not OK) or else Lo < 0;
9665 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
9666 Rneg := (not OK) or else Lo < 0;
9668 -- We won't mess with trying to find out if the left operand can really
9669 -- be the largest negative number (that's a pain in the case of private
9670 -- types and this is really marginal). We will just assume that we need
9671 -- the test if the left operand can be negative at all.
9673 if Lneg and Rneg then
9674 Rewrite (N,
9675 Make_If_Expression (Loc,
9676 Expressions => New_List (
9677 Make_Op_Eq (Loc,
9678 Left_Opnd => Duplicate_Subexpr (Right),
9679 Right_Opnd =>
9680 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
9682 Unchecked_Convert_To (Typ,
9683 Make_Integer_Literal (Loc, Uint_0)),
9685 Relocate_Node (N))));
9687 Set_Analyzed (Next (Next (First (Expressions (N)))));
9688 Analyze_And_Resolve (N, Typ);
9689 end if;
9690 end Expand_N_Op_Rem;
9692 -----------------------------
9693 -- Expand_N_Op_Rotate_Left --
9694 -----------------------------
9696 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
9697 begin
9698 Binary_Op_Validity_Checks (N);
9700 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
9701 -- so we rewrite in terms of logical shifts
9703 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
9705 -- where Bits is the shift count mod Esize (the mod operation here
9706 -- deals with ludicrous large shift counts, which are apparently OK).
9708 -- What about nonbinary modulus ???
9710 declare
9711 Loc : constant Source_Ptr := Sloc (N);
9712 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
9713 Typ : constant Entity_Id := Etype (N);
9715 begin
9716 if Modify_Tree_For_C then
9717 Rewrite (Right_Opnd (N),
9718 Make_Op_Rem (Loc,
9719 Left_Opnd => Relocate_Node (Right_Opnd (N)),
9720 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
9722 Analyze_And_Resolve (Right_Opnd (N), Rtp);
9724 Rewrite (N,
9725 Make_Op_Or (Loc,
9726 Left_Opnd =>
9727 Make_Op_Shift_Left (Loc,
9728 Left_Opnd => Left_Opnd (N),
9729 Right_Opnd => Right_Opnd (N)),
9731 Right_Opnd =>
9732 Make_Op_Shift_Right (Loc,
9733 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
9734 Right_Opnd =>
9735 Make_Op_Subtract (Loc,
9736 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9737 Right_Opnd =>
9738 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9740 Analyze_And_Resolve (N, Typ);
9741 end if;
9742 end;
9743 end Expand_N_Op_Rotate_Left;
9745 ------------------------------
9746 -- Expand_N_Op_Rotate_Right --
9747 ------------------------------
9749 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
9750 begin
9751 Binary_Op_Validity_Checks (N);
9753 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
9754 -- so we rewrite in terms of logical shifts
9756 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
9758 -- where Bits is the shift count mod Esize (the mod operation here
9759 -- deals with ludicrous large shift counts, which are apparently OK).
9761 -- What about nonbinary modulus ???
9763 declare
9764 Loc : constant Source_Ptr := Sloc (N);
9765 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
9766 Typ : constant Entity_Id := Etype (N);
9768 begin
9769 Rewrite (Right_Opnd (N),
9770 Make_Op_Rem (Loc,
9771 Left_Opnd => Relocate_Node (Right_Opnd (N)),
9772 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
9774 Analyze_And_Resolve (Right_Opnd (N), Rtp);
9776 if Modify_Tree_For_C then
9777 Rewrite (N,
9778 Make_Op_Or (Loc,
9779 Left_Opnd =>
9780 Make_Op_Shift_Right (Loc,
9781 Left_Opnd => Left_Opnd (N),
9782 Right_Opnd => Right_Opnd (N)),
9784 Right_Opnd =>
9785 Make_Op_Shift_Left (Loc,
9786 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
9787 Right_Opnd =>
9788 Make_Op_Subtract (Loc,
9789 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9790 Right_Opnd =>
9791 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9793 Analyze_And_Resolve (N, Typ);
9794 end if;
9795 end;
9796 end Expand_N_Op_Rotate_Right;
9798 ----------------------------
9799 -- Expand_N_Op_Shift_Left --
9800 ----------------------------
9802 -- Note: nothing in this routine depends on left as opposed to right shifts
9803 -- so we share the routine for expanding shift right operations.
9805 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
9806 begin
9807 Binary_Op_Validity_Checks (N);
9809 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9810 -- operand is not greater than the word size (since that would not
9811 -- be defined properly by the corresponding C shift operator).
9813 if Modify_Tree_For_C then
9814 declare
9815 Right : constant Node_Id := Right_Opnd (N);
9816 Loc : constant Source_Ptr := Sloc (Right);
9817 Typ : constant Entity_Id := Etype (N);
9818 Siz : constant Uint := Esize (Typ);
9819 Orig : Node_Id;
9820 OK : Boolean;
9821 Lo : Uint;
9822 Hi : Uint;
9824 begin
9825 if Compile_Time_Known_Value (Right) then
9826 if Expr_Value (Right) >= Siz then
9827 Rewrite (N, Make_Integer_Literal (Loc, 0));
9828 Analyze_And_Resolve (N, Typ);
9829 end if;
9831 -- Not compile time known, find range
9833 else
9834 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9836 -- Nothing to do if known to be OK range, otherwise expand
9838 if not OK or else Hi >= Siz then
9840 -- Prevent recursion on copy of shift node
9842 Orig := Relocate_Node (N);
9843 Set_Analyzed (Orig);
9845 -- Now do the rewrite
9847 Rewrite (N,
9848 Make_If_Expression (Loc,
9849 Expressions => New_List (
9850 Make_Op_Ge (Loc,
9851 Left_Opnd => Duplicate_Subexpr_Move_Checks (Right),
9852 Right_Opnd => Make_Integer_Literal (Loc, Siz)),
9853 Make_Integer_Literal (Loc, 0),
9854 Orig)));
9855 Analyze_And_Resolve (N, Typ);
9856 end if;
9857 end if;
9858 end;
9859 end if;
9860 end Expand_N_Op_Shift_Left;
9862 -----------------------------
9863 -- Expand_N_Op_Shift_Right --
9864 -----------------------------
9866 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
9867 begin
9868 -- Share shift left circuit
9870 Expand_N_Op_Shift_Left (N);
9871 end Expand_N_Op_Shift_Right;
9873 ----------------------------------------
9874 -- Expand_N_Op_Shift_Right_Arithmetic --
9875 ----------------------------------------
9877 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
9878 begin
9879 Binary_Op_Validity_Checks (N);
9881 -- If we are in Modify_Tree_For_C mode, there is no shift right
9882 -- arithmetic in C, so we rewrite in terms of logical shifts.
9884 -- Shift_Right (Num, Bits) or
9885 -- (if Num >= Sign
9886 -- then not (Shift_Right (Mask, bits))
9887 -- else 0)
9889 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9891 -- Note: in almost all C compilers it would work to just shift a
9892 -- signed integer right, but it's undefined and we cannot rely on it.
9894 -- Note: the above works fine for shift counts greater than or equal
9895 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9896 -- generates all 1'bits.
9898 -- What about nonbinary modulus ???
9900 declare
9901 Loc : constant Source_Ptr := Sloc (N);
9902 Typ : constant Entity_Id := Etype (N);
9903 Sign : constant Uint := 2 ** (Esize (Typ) - 1);
9904 Mask : constant Uint := (2 ** Esize (Typ)) - 1;
9905 Left : constant Node_Id := Left_Opnd (N);
9906 Right : constant Node_Id := Right_Opnd (N);
9907 Maskx : Node_Id;
9909 begin
9910 if Modify_Tree_For_C then
9912 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9913 -- compile time as a single constant.
9915 if Compile_Time_Known_Value (Right) then
9916 declare
9917 Val : constant Uint := Expr_Value (Right);
9919 begin
9920 if Val >= Esize (Typ) then
9921 Maskx := Make_Integer_Literal (Loc, Mask);
9923 else
9924 Maskx :=
9925 Make_Integer_Literal (Loc,
9926 Intval => Mask - (Mask / (2 ** Expr_Value (Right))));
9927 end if;
9928 end;
9930 else
9931 Maskx :=
9932 Make_Op_Not (Loc,
9933 Right_Opnd =>
9934 Make_Op_Shift_Right (Loc,
9935 Left_Opnd => Make_Integer_Literal (Loc, Mask),
9936 Right_Opnd => Duplicate_Subexpr_No_Checks (Right)));
9937 end if;
9939 -- Now do the rewrite
9941 Rewrite (N,
9942 Make_Op_Or (Loc,
9943 Left_Opnd =>
9944 Make_Op_Shift_Right (Loc,
9945 Left_Opnd => Left,
9946 Right_Opnd => Right),
9947 Right_Opnd =>
9948 Make_If_Expression (Loc,
9949 Expressions => New_List (
9950 Make_Op_Ge (Loc,
9951 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
9952 Right_Opnd => Make_Integer_Literal (Loc, Sign)),
9953 Maskx,
9954 Make_Integer_Literal (Loc, 0)))));
9955 Analyze_And_Resolve (N, Typ);
9956 end if;
9957 end;
9958 end Expand_N_Op_Shift_Right_Arithmetic;
9960 --------------------------
9961 -- Expand_N_Op_Subtract --
9962 --------------------------
9964 procedure Expand_N_Op_Subtract (N : Node_Id) is
9965 Typ : constant Entity_Id := Etype (N);
9967 begin
9968 Binary_Op_Validity_Checks (N);
9970 -- Check for MINIMIZED/ELIMINATED overflow mode
9972 if Minimized_Eliminated_Overflow_Check (N) then
9973 Apply_Arithmetic_Overflow_Check (N);
9974 return;
9975 end if;
9977 -- N - 0 = N for integer types
9979 if Is_Integer_Type (Typ)
9980 and then Compile_Time_Known_Value (Right_Opnd (N))
9981 and then Expr_Value (Right_Opnd (N)) = 0
9982 then
9983 Rewrite (N, Left_Opnd (N));
9984 return;
9985 end if;
9987 -- Arithmetic overflow checks for signed integer/fixed point types
9989 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
9990 Apply_Arithmetic_Overflow_Check (N);
9991 end if;
9993 -- Overflow checks for floating-point if -gnateF mode active
9995 Check_Float_Op_Overflow (N);
9997 Expand_Nonbinary_Modular_Op (N);
9998 end Expand_N_Op_Subtract;
10000 ---------------------
10001 -- Expand_N_Op_Xor --
10002 ---------------------
10004 procedure Expand_N_Op_Xor (N : Node_Id) is
10005 Typ : constant Entity_Id := Etype (N);
10007 begin
10008 Binary_Op_Validity_Checks (N);
10010 if Is_Array_Type (Etype (N)) then
10011 Expand_Boolean_Operator (N);
10013 elsif Is_Boolean_Type (Etype (N)) then
10014 Adjust_Condition (Left_Opnd (N));
10015 Adjust_Condition (Right_Opnd (N));
10016 Set_Etype (N, Standard_Boolean);
10017 Adjust_Result_Type (N, Typ);
10019 elsif Is_Intrinsic_Subprogram (Entity (N)) then
10020 Expand_Intrinsic_Call (N, Entity (N));
10021 end if;
10022 end Expand_N_Op_Xor;
10024 ----------------------
10025 -- Expand_N_Or_Else --
10026 ----------------------
10028 procedure Expand_N_Or_Else (N : Node_Id)
10029 renames Expand_Short_Circuit_Operator;
10031 -----------------------------------
10032 -- Expand_N_Qualified_Expression --
10033 -----------------------------------
10035 procedure Expand_N_Qualified_Expression (N : Node_Id) is
10036 Operand : constant Node_Id := Expression (N);
10037 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
10039 begin
10040 -- Do validity check if validity checking operands
10042 if Validity_Checks_On and Validity_Check_Operands then
10043 Ensure_Valid (Operand);
10044 end if;
10046 -- Apply possible constraint check
10048 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
10050 if Do_Range_Check (Operand) then
10051 Set_Do_Range_Check (Operand, False);
10052 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
10053 end if;
10054 end Expand_N_Qualified_Expression;
10056 ------------------------------------
10057 -- Expand_N_Quantified_Expression --
10058 ------------------------------------
10060 -- We expand:
10062 -- for all X in range => Cond
10064 -- into:
10066 -- T := True;
10067 -- for X in range loop
10068 -- if not Cond then
10069 -- T := False;
10070 -- exit;
10071 -- end if;
10072 -- end loop;
10074 -- Similarly, an existentially quantified expression:
10076 -- for some X in range => Cond
10078 -- becomes:
10080 -- T := False;
10081 -- for X in range loop
10082 -- if Cond then
10083 -- T := True;
10084 -- exit;
10085 -- end if;
10086 -- end loop;
10088 -- In both cases, the iteration may be over a container in which case it is
10089 -- given by an iterator specification, not a loop parameter specification.
10091 procedure Expand_N_Quantified_Expression (N : Node_Id) is
10092 Actions : constant List_Id := New_List;
10093 For_All : constant Boolean := All_Present (N);
10094 Iter_Spec : constant Node_Id := Iterator_Specification (N);
10095 Loc : constant Source_Ptr := Sloc (N);
10096 Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N);
10097 Cond : Node_Id;
10098 Flag : Entity_Id;
10099 Scheme : Node_Id;
10100 Stmts : List_Id;
10102 begin
10103 -- Create the declaration of the flag which tracks the status of the
10104 -- quantified expression. Generate:
10106 -- Flag : Boolean := (True | False);
10108 Flag := Make_Temporary (Loc, 'T', N);
10110 Append_To (Actions,
10111 Make_Object_Declaration (Loc,
10112 Defining_Identifier => Flag,
10113 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
10114 Expression =>
10115 New_Occurrence_Of (Boolean_Literals (For_All), Loc)));
10117 -- Construct the circuitry which tracks the status of the quantified
10118 -- expression. Generate:
10120 -- if [not] Cond then
10121 -- Flag := (False | True);
10122 -- exit;
10123 -- end if;
10125 Cond := Relocate_Node (Condition (N));
10127 if For_All then
10128 Cond := Make_Op_Not (Loc, Cond);
10129 end if;
10131 Stmts := New_List (
10132 Make_Implicit_If_Statement (N,
10133 Condition => Cond,
10134 Then_Statements => New_List (
10135 Make_Assignment_Statement (Loc,
10136 Name => New_Occurrence_Of (Flag, Loc),
10137 Expression =>
10138 New_Occurrence_Of (Boolean_Literals (not For_All), Loc)),
10139 Make_Exit_Statement (Loc))));
10141 -- Build the loop equivalent of the quantified expression
10143 if Present (Iter_Spec) then
10144 Scheme :=
10145 Make_Iteration_Scheme (Loc,
10146 Iterator_Specification => Iter_Spec);
10147 else
10148 Scheme :=
10149 Make_Iteration_Scheme (Loc,
10150 Loop_Parameter_Specification => Loop_Spec);
10151 end if;
10153 Append_To (Actions,
10154 Make_Loop_Statement (Loc,
10155 Iteration_Scheme => Scheme,
10156 Statements => Stmts,
10157 End_Label => Empty));
10159 -- Transform the quantified expression
10161 Rewrite (N,
10162 Make_Expression_With_Actions (Loc,
10163 Expression => New_Occurrence_Of (Flag, Loc),
10164 Actions => Actions));
10165 Analyze_And_Resolve (N, Standard_Boolean);
10166 end Expand_N_Quantified_Expression;
10168 ---------------------------------
10169 -- Expand_N_Selected_Component --
10170 ---------------------------------
10172 procedure Expand_N_Selected_Component (N : Node_Id) is
10173 Loc : constant Source_Ptr := Sloc (N);
10174 Par : constant Node_Id := Parent (N);
10175 P : constant Node_Id := Prefix (N);
10176 S : constant Node_Id := Selector_Name (N);
10177 Ptyp : Entity_Id := Underlying_Type (Etype (P));
10178 Disc : Entity_Id;
10179 New_N : Node_Id;
10180 Dcon : Elmt_Id;
10181 Dval : Node_Id;
10183 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
10184 -- Gigi needs a temporary for prefixes that depend on a discriminant,
10185 -- unless the context of an assignment can provide size information.
10186 -- Don't we have a general routine that does this???
10188 function Is_Subtype_Declaration return Boolean;
10189 -- The replacement of a discriminant reference by its value is required
10190 -- if this is part of the initialization of an temporary generated by a
10191 -- change of representation. This shows up as the construction of a
10192 -- discriminant constraint for a subtype declared at the same point as
10193 -- the entity in the prefix of the selected component. We recognize this
10194 -- case when the context of the reference is:
10195 -- subtype ST is T(Obj.D);
10196 -- where the entity for Obj comes from source, and ST has the same sloc.
10198 -----------------------
10199 -- In_Left_Hand_Side --
10200 -----------------------
10202 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
10203 begin
10204 return (Nkind (Parent (Comp)) = N_Assignment_Statement
10205 and then Comp = Name (Parent (Comp)))
10206 or else (Present (Parent (Comp))
10207 and then Nkind (Parent (Comp)) in N_Subexpr
10208 and then In_Left_Hand_Side (Parent (Comp)));
10209 end In_Left_Hand_Side;
10211 -----------------------------
10212 -- Is_Subtype_Declaration --
10213 -----------------------------
10215 function Is_Subtype_Declaration return Boolean is
10216 Par : constant Node_Id := Parent (N);
10217 begin
10218 return
10219 Nkind (Par) = N_Index_Or_Discriminant_Constraint
10220 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
10221 and then Comes_From_Source (Entity (Prefix (N)))
10222 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
10223 end Is_Subtype_Declaration;
10225 -- Start of processing for Expand_N_Selected_Component
10227 begin
10228 -- Insert explicit dereference if required
10230 if Is_Access_Type (Ptyp) then
10232 -- First set prefix type to proper access type, in case it currently
10233 -- has a private (non-access) view of this type.
10235 Set_Etype (P, Ptyp);
10237 Insert_Explicit_Dereference (P);
10238 Analyze_And_Resolve (P, Designated_Type (Ptyp));
10240 if Ekind (Etype (P)) = E_Private_Subtype
10241 and then Is_For_Access_Subtype (Etype (P))
10242 then
10243 Set_Etype (P, Base_Type (Etype (P)));
10244 end if;
10246 Ptyp := Etype (P);
10247 end if;
10249 -- Deal with discriminant check required
10251 if Do_Discriminant_Check (N) then
10252 if Present (Discriminant_Checking_Func
10253 (Original_Record_Component (Entity (S))))
10254 then
10255 -- Present the discriminant checking function to the backend, so
10256 -- that it can inline the call to the function.
10258 Add_Inlined_Body
10259 (Discriminant_Checking_Func
10260 (Original_Record_Component (Entity (S))),
10263 -- Now reset the flag and generate the call
10265 Set_Do_Discriminant_Check (N, False);
10266 Generate_Discriminant_Check (N);
10268 -- In the case of Unchecked_Union, no discriminant checking is
10269 -- actually performed.
10271 else
10272 Set_Do_Discriminant_Check (N, False);
10273 end if;
10274 end if;
10276 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10277 -- function, then additional actuals must be passed.
10279 if Is_Build_In_Place_Function_Call (P) then
10280 Make_Build_In_Place_Call_In_Anonymous_Context (P);
10282 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
10283 -- containing build-in-place function calls whose returned object covers
10284 -- interface types.
10286 elsif Present (Unqual_BIP_Iface_Function_Call (P)) then
10287 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (P);
10288 end if;
10290 -- Gigi cannot handle unchecked conversions that are the prefix of a
10291 -- selected component with discriminants. This must be checked during
10292 -- expansion, because during analysis the type of the selector is not
10293 -- known at the point the prefix is analyzed. If the conversion is the
10294 -- target of an assignment, then we cannot force the evaluation.
10296 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
10297 and then Has_Discriminants (Etype (N))
10298 and then not In_Left_Hand_Side (N)
10299 then
10300 Force_Evaluation (Prefix (N));
10301 end if;
10303 -- Remaining processing applies only if selector is a discriminant
10305 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
10307 -- If the selector is a discriminant of a constrained record type,
10308 -- we may be able to rewrite the expression with the actual value
10309 -- of the discriminant, a useful optimization in some cases.
10311 if Is_Record_Type (Ptyp)
10312 and then Has_Discriminants (Ptyp)
10313 and then Is_Constrained (Ptyp)
10314 then
10315 -- Do this optimization for discrete types only, and not for
10316 -- access types (access discriminants get us into trouble).
10318 if not Is_Discrete_Type (Etype (N)) then
10319 null;
10321 -- Don't do this on the left-hand side of an assignment statement.
10322 -- Normally one would think that references like this would not
10323 -- occur, but they do in generated code, and mean that we really
10324 -- do want to assign the discriminant.
10326 elsif Nkind (Par) = N_Assignment_Statement
10327 and then Name (Par) = N
10328 then
10329 null;
10331 -- Don't do this optimization for the prefix of an attribute or
10332 -- the name of an object renaming declaration since these are
10333 -- contexts where we do not want the value anyway.
10335 elsif (Nkind (Par) = N_Attribute_Reference
10336 and then Prefix (Par) = N)
10337 or else Is_Renamed_Object (N)
10338 then
10339 null;
10341 -- Don't do this optimization if we are within the code for a
10342 -- discriminant check, since the whole point of such a check may
10343 -- be to verify the condition on which the code below depends.
10345 elsif Is_In_Discriminant_Check (N) then
10346 null;
10348 -- Green light to see if we can do the optimization. There is
10349 -- still one condition that inhibits the optimization below but
10350 -- now is the time to check the particular discriminant.
10352 else
10353 -- Loop through discriminants to find the matching discriminant
10354 -- constraint to see if we can copy it.
10356 Disc := First_Discriminant (Ptyp);
10357 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
10358 Discr_Loop : while Present (Dcon) loop
10359 Dval := Node (Dcon);
10361 -- Check if this is the matching discriminant and if the
10362 -- discriminant value is simple enough to make sense to
10363 -- copy. We don't want to copy complex expressions, and
10364 -- indeed to do so can cause trouble (before we put in
10365 -- this guard, a discriminant expression containing an
10366 -- AND THEN was copied, causing problems for coverage
10367 -- analysis tools).
10369 -- However, if the reference is part of the initialization
10370 -- code generated for an object declaration, we must use
10371 -- the discriminant value from the subtype constraint,
10372 -- because the selected component may be a reference to the
10373 -- object being initialized, whose discriminant is not yet
10374 -- set. This only happens in complex cases involving changes
10375 -- or representation.
10377 if Disc = Entity (Selector_Name (N))
10378 and then (Is_Entity_Name (Dval)
10379 or else Compile_Time_Known_Value (Dval)
10380 or else Is_Subtype_Declaration)
10381 then
10382 -- Here we have the matching discriminant. Check for
10383 -- the case of a discriminant of a component that is
10384 -- constrained by an outer discriminant, which cannot
10385 -- be optimized away.
10387 if Denotes_Discriminant
10388 (Dval, Check_Concurrent => True)
10389 then
10390 exit Discr_Loop;
10392 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
10393 and then
10394 Denotes_Discriminant
10395 (Selector_Name (Original_Node (Dval)), True)
10396 then
10397 exit Discr_Loop;
10399 -- Do not retrieve value if constraint is not static. It
10400 -- is generally not useful, and the constraint may be a
10401 -- rewritten outer discriminant in which case it is in
10402 -- fact incorrect.
10404 elsif Is_Entity_Name (Dval)
10405 and then
10406 Nkind (Parent (Entity (Dval))) = N_Object_Declaration
10407 and then Present (Expression (Parent (Entity (Dval))))
10408 and then not
10409 Is_OK_Static_Expression
10410 (Expression (Parent (Entity (Dval))))
10411 then
10412 exit Discr_Loop;
10414 -- In the context of a case statement, the expression may
10415 -- have the base type of the discriminant, and we need to
10416 -- preserve the constraint to avoid spurious errors on
10417 -- missing cases.
10419 elsif Nkind (Parent (N)) = N_Case_Statement
10420 and then Etype (Dval) /= Etype (Disc)
10421 then
10422 Rewrite (N,
10423 Make_Qualified_Expression (Loc,
10424 Subtype_Mark =>
10425 New_Occurrence_Of (Etype (Disc), Loc),
10426 Expression =>
10427 New_Copy_Tree (Dval)));
10428 Analyze_And_Resolve (N, Etype (Disc));
10430 -- In case that comes out as a static expression,
10431 -- reset it (a selected component is never static).
10433 Set_Is_Static_Expression (N, False);
10434 return;
10436 -- Otherwise we can just copy the constraint, but the
10437 -- result is certainly not static. In some cases the
10438 -- discriminant constraint has been analyzed in the
10439 -- context of the original subtype indication, but for
10440 -- itypes the constraint might not have been analyzed
10441 -- yet, and this must be done now.
10443 else
10444 Rewrite (N, New_Copy_Tree (Dval));
10445 Analyze_And_Resolve (N);
10446 Set_Is_Static_Expression (N, False);
10447 return;
10448 end if;
10449 end if;
10451 Next_Elmt (Dcon);
10452 Next_Discriminant (Disc);
10453 end loop Discr_Loop;
10455 -- Note: the above loop should always find a matching
10456 -- discriminant, but if it does not, we just missed an
10457 -- optimization due to some glitch (perhaps a previous
10458 -- error), so ignore.
10460 end if;
10461 end if;
10463 -- The only remaining processing is in the case of a discriminant of
10464 -- a concurrent object, where we rewrite the prefix to denote the
10465 -- corresponding record type. If the type is derived and has renamed
10466 -- discriminants, use corresponding discriminant, which is the one
10467 -- that appears in the corresponding record.
10469 if not Is_Concurrent_Type (Ptyp) then
10470 return;
10471 end if;
10473 Disc := Entity (Selector_Name (N));
10475 if Is_Derived_Type (Ptyp)
10476 and then Present (Corresponding_Discriminant (Disc))
10477 then
10478 Disc := Corresponding_Discriminant (Disc);
10479 end if;
10481 New_N :=
10482 Make_Selected_Component (Loc,
10483 Prefix =>
10484 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
10485 New_Copy_Tree (P)),
10486 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
10488 Rewrite (N, New_N);
10489 Analyze (N);
10490 end if;
10492 -- Set Atomic_Sync_Required if necessary for atomic component
10494 if Nkind (N) = N_Selected_Component then
10495 declare
10496 E : constant Entity_Id := Entity (Selector_Name (N));
10497 Set : Boolean;
10499 begin
10500 -- If component is atomic, but type is not, setting depends on
10501 -- disable/enable state for the component.
10503 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
10504 Set := not Atomic_Synchronization_Disabled (E);
10506 -- If component is not atomic, but its type is atomic, setting
10507 -- depends on disable/enable state for the type.
10509 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
10510 Set := not Atomic_Synchronization_Disabled (Etype (E));
10512 -- If both component and type are atomic, we disable if either
10513 -- component or its type have sync disabled.
10515 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
10516 Set := (not Atomic_Synchronization_Disabled (E))
10517 and then
10518 (not Atomic_Synchronization_Disabled (Etype (E)));
10520 else
10521 Set := False;
10522 end if;
10524 -- Set flag if required
10526 if Set then
10527 Activate_Atomic_Synchronization (N);
10528 end if;
10529 end;
10530 end if;
10531 end Expand_N_Selected_Component;
10533 --------------------
10534 -- Expand_N_Slice --
10535 --------------------
10537 procedure Expand_N_Slice (N : Node_Id) is
10538 Loc : constant Source_Ptr := Sloc (N);
10539 Typ : constant Entity_Id := Etype (N);
10541 function Is_Procedure_Actual (N : Node_Id) return Boolean;
10542 -- Check whether the argument is an actual for a procedure call, in
10543 -- which case the expansion of a bit-packed slice is deferred until the
10544 -- call itself is expanded. The reason this is required is that we might
10545 -- have an IN OUT or OUT parameter, and the copy out is essential, and
10546 -- that copy out would be missed if we created a temporary here in
10547 -- Expand_N_Slice. Note that we don't bother to test specifically for an
10548 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
10549 -- is harmless to defer expansion in the IN case, since the call
10550 -- processing will still generate the appropriate copy in operation,
10551 -- which will take care of the slice.
10553 procedure Make_Temporary_For_Slice;
10554 -- Create a named variable for the value of the slice, in cases where
10555 -- the back end cannot handle it properly, e.g. when packed types or
10556 -- unaligned slices are involved.
10558 -------------------------
10559 -- Is_Procedure_Actual --
10560 -------------------------
10562 function Is_Procedure_Actual (N : Node_Id) return Boolean is
10563 Par : Node_Id := Parent (N);
10565 begin
10566 loop
10567 -- If our parent is a procedure call we can return
10569 if Nkind (Par) = N_Procedure_Call_Statement then
10570 return True;
10572 -- If our parent is a type conversion, keep climbing the tree,
10573 -- since a type conversion can be a procedure actual. Also keep
10574 -- climbing if parameter association or a qualified expression,
10575 -- since these are additional cases that do can appear on
10576 -- procedure actuals.
10578 elsif Nkind_In (Par, N_Type_Conversion,
10579 N_Parameter_Association,
10580 N_Qualified_Expression)
10581 then
10582 Par := Parent (Par);
10584 -- Any other case is not what we are looking for
10586 else
10587 return False;
10588 end if;
10589 end loop;
10590 end Is_Procedure_Actual;
10592 ------------------------------
10593 -- Make_Temporary_For_Slice --
10594 ------------------------------
10596 procedure Make_Temporary_For_Slice is
10597 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
10598 Decl : Node_Id;
10600 begin
10601 Decl :=
10602 Make_Object_Declaration (Loc,
10603 Defining_Identifier => Ent,
10604 Object_Definition => New_Occurrence_Of (Typ, Loc));
10606 Set_No_Initialization (Decl);
10608 Insert_Actions (N, New_List (
10609 Decl,
10610 Make_Assignment_Statement (Loc,
10611 Name => New_Occurrence_Of (Ent, Loc),
10612 Expression => Relocate_Node (N))));
10614 Rewrite (N, New_Occurrence_Of (Ent, Loc));
10615 Analyze_And_Resolve (N, Typ);
10616 end Make_Temporary_For_Slice;
10618 -- Local variables
10620 Pref : constant Node_Id := Prefix (N);
10621 Pref_Typ : Entity_Id := Etype (Pref);
10623 -- Start of processing for Expand_N_Slice
10625 begin
10626 -- Special handling for access types
10628 if Is_Access_Type (Pref_Typ) then
10629 Pref_Typ := Designated_Type (Pref_Typ);
10631 Rewrite (Pref,
10632 Make_Explicit_Dereference (Sloc (N),
10633 Prefix => Relocate_Node (Pref)));
10635 Analyze_And_Resolve (Pref, Pref_Typ);
10636 end if;
10638 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10639 -- function, then additional actuals must be passed.
10641 if Is_Build_In_Place_Function_Call (Pref) then
10642 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
10644 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
10645 -- containing build-in-place function calls whose returned object covers
10646 -- interface types.
10648 elsif Present (Unqual_BIP_Iface_Function_Call (Pref)) then
10649 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Pref);
10650 end if;
10652 -- The remaining case to be handled is packed slices. We can leave
10653 -- packed slices as they are in the following situations:
10655 -- 1. Right or left side of an assignment (we can handle this
10656 -- situation correctly in the assignment statement expansion).
10658 -- 2. Prefix of indexed component (the slide is optimized away in this
10659 -- case, see the start of Expand_N_Slice.)
10661 -- 3. Object renaming declaration, since we want the name of the
10662 -- slice, not the value.
10664 -- 4. Argument to procedure call, since copy-in/copy-out handling may
10665 -- be required, and this is handled in the expansion of call
10666 -- itself.
10668 -- 5. Prefix of an address attribute (this is an error which is caught
10669 -- elsewhere, and the expansion would interfere with generating the
10670 -- error message).
10672 if not Is_Packed (Typ) then
10674 -- Apply transformation for actuals of a function call, where
10675 -- Expand_Actuals is not used.
10677 if Nkind (Parent (N)) = N_Function_Call
10678 and then Is_Possibly_Unaligned_Slice (N)
10679 then
10680 Make_Temporary_For_Slice;
10681 end if;
10683 elsif Nkind (Parent (N)) = N_Assignment_Statement
10684 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
10685 and then Parent (N) = Name (Parent (Parent (N))))
10686 then
10687 return;
10689 elsif Nkind (Parent (N)) = N_Indexed_Component
10690 or else Is_Renamed_Object (N)
10691 or else Is_Procedure_Actual (N)
10692 then
10693 return;
10695 elsif Nkind (Parent (N)) = N_Attribute_Reference
10696 and then Attribute_Name (Parent (N)) = Name_Address
10697 then
10698 return;
10700 else
10701 Make_Temporary_For_Slice;
10702 end if;
10703 end Expand_N_Slice;
10705 ------------------------------
10706 -- Expand_N_Type_Conversion --
10707 ------------------------------
10709 procedure Expand_N_Type_Conversion (N : Node_Id) is
10710 Loc : constant Source_Ptr := Sloc (N);
10711 Operand : constant Node_Id := Expression (N);
10712 Target_Type : constant Entity_Id := Etype (N);
10713 Operand_Type : Entity_Id := Etype (Operand);
10715 procedure Handle_Changed_Representation;
10716 -- This is called in the case of record and array type conversions to
10717 -- see if there is a change of representation to be handled. Change of
10718 -- representation is actually handled at the assignment statement level,
10719 -- and what this procedure does is rewrite node N conversion as an
10720 -- assignment to temporary. If there is no change of representation,
10721 -- then the conversion node is unchanged.
10723 procedure Raise_Accessibility_Error;
10724 -- Called when we know that an accessibility check will fail. Rewrites
10725 -- node N to an appropriate raise statement and outputs warning msgs.
10726 -- The Etype of the raise node is set to Target_Type. Note that in this
10727 -- case the rest of the processing should be skipped (i.e. the call to
10728 -- this procedure will be followed by "goto Done").
10730 procedure Real_Range_Check;
10731 -- Handles generation of range check for real target value
10733 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
10734 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
10735 -- evaluates to True.
10737 -----------------------------------
10738 -- Handle_Changed_Representation --
10739 -----------------------------------
10741 procedure Handle_Changed_Representation is
10742 Temp : Entity_Id;
10743 Decl : Node_Id;
10744 Odef : Node_Id;
10745 N_Ix : Node_Id;
10746 Cons : List_Id;
10748 begin
10749 -- Nothing else to do if no change of representation
10751 if Same_Representation (Operand_Type, Target_Type) then
10752 return;
10754 -- The real change of representation work is done by the assignment
10755 -- statement processing. So if this type conversion is appearing as
10756 -- the expression of an assignment statement, nothing needs to be
10757 -- done to the conversion.
10759 elsif Nkind (Parent (N)) = N_Assignment_Statement then
10760 return;
10762 -- Otherwise we need to generate a temporary variable, and do the
10763 -- change of representation assignment into that temporary variable.
10764 -- The conversion is then replaced by a reference to this variable.
10766 else
10767 Cons := No_List;
10769 -- If type is unconstrained we have to add a constraint, copied
10770 -- from the actual value of the left-hand side.
10772 if not Is_Constrained (Target_Type) then
10773 if Has_Discriminants (Operand_Type) then
10775 -- A change of representation can only apply to untagged
10776 -- types. We need to build the constraint that applies to
10777 -- the target type, using the constraints of the operand.
10778 -- The analysis is complicated if there are both inherited
10779 -- discriminants and constrained discriminants.
10780 -- We iterate over the discriminants of the target, and
10781 -- find the discriminant of the same name:
10783 -- a) If there is a corresponding discriminant in the object
10784 -- then the value is a selected component of the operand.
10786 -- b) Otherwise the value of a constrained discriminant is
10787 -- found in the stored constraint of the operand.
10789 declare
10790 Stored : constant Elist_Id :=
10791 Stored_Constraint (Operand_Type);
10793 Elmt : Elmt_Id;
10795 Disc_O : Entity_Id;
10796 -- Discriminant of the operand type. Its value in the
10797 -- object is captured in a selected component.
10799 Disc_S : Entity_Id;
10800 -- Stored discriminant of the operand. If present, it
10801 -- corresponds to a constrained discriminant of the
10802 -- parent type.
10804 Disc_T : Entity_Id;
10805 -- Discriminant of the target type
10807 begin
10808 Disc_T := First_Discriminant (Target_Type);
10809 Disc_O := First_Discriminant (Operand_Type);
10810 Disc_S := First_Stored_Discriminant (Operand_Type);
10812 if Present (Stored) then
10813 Elmt := First_Elmt (Stored);
10814 else
10815 Elmt := No_Elmt; -- init to avoid warning
10816 end if;
10818 Cons := New_List;
10819 while Present (Disc_T) loop
10820 if Present (Disc_O)
10821 and then Chars (Disc_T) = Chars (Disc_O)
10822 then
10823 Append_To (Cons,
10824 Make_Selected_Component (Loc,
10825 Prefix =>
10826 Duplicate_Subexpr_Move_Checks (Operand),
10827 Selector_Name =>
10828 Make_Identifier (Loc, Chars (Disc_O))));
10829 Next_Discriminant (Disc_O);
10831 elsif Present (Disc_S) then
10832 Append_To (Cons, New_Copy_Tree (Node (Elmt)));
10833 Next_Elmt (Elmt);
10834 end if;
10836 Next_Discriminant (Disc_T);
10837 end loop;
10838 end;
10840 elsif Is_Array_Type (Operand_Type) then
10841 N_Ix := First_Index (Target_Type);
10842 Cons := New_List;
10844 for J in 1 .. Number_Dimensions (Operand_Type) loop
10846 -- We convert the bounds explicitly. We use an unchecked
10847 -- conversion because bounds checks are done elsewhere.
10849 Append_To (Cons,
10850 Make_Range (Loc,
10851 Low_Bound =>
10852 Unchecked_Convert_To (Etype (N_Ix),
10853 Make_Attribute_Reference (Loc,
10854 Prefix =>
10855 Duplicate_Subexpr_No_Checks
10856 (Operand, Name_Req => True),
10857 Attribute_Name => Name_First,
10858 Expressions => New_List (
10859 Make_Integer_Literal (Loc, J)))),
10861 High_Bound =>
10862 Unchecked_Convert_To (Etype (N_Ix),
10863 Make_Attribute_Reference (Loc,
10864 Prefix =>
10865 Duplicate_Subexpr_No_Checks
10866 (Operand, Name_Req => True),
10867 Attribute_Name => Name_Last,
10868 Expressions => New_List (
10869 Make_Integer_Literal (Loc, J))))));
10871 Next_Index (N_Ix);
10872 end loop;
10873 end if;
10874 end if;
10876 Odef := New_Occurrence_Of (Target_Type, Loc);
10878 if Present (Cons) then
10879 Odef :=
10880 Make_Subtype_Indication (Loc,
10881 Subtype_Mark => Odef,
10882 Constraint =>
10883 Make_Index_Or_Discriminant_Constraint (Loc,
10884 Constraints => Cons));
10885 end if;
10887 Temp := Make_Temporary (Loc, 'C');
10888 Decl :=
10889 Make_Object_Declaration (Loc,
10890 Defining_Identifier => Temp,
10891 Object_Definition => Odef);
10893 Set_No_Initialization (Decl, True);
10895 -- Insert required actions. It is essential to suppress checks
10896 -- since we have suppressed default initialization, which means
10897 -- that the variable we create may have no discriminants.
10899 Insert_Actions (N,
10900 New_List (
10901 Decl,
10902 Make_Assignment_Statement (Loc,
10903 Name => New_Occurrence_Of (Temp, Loc),
10904 Expression => Relocate_Node (N))),
10905 Suppress => All_Checks);
10907 Rewrite (N, New_Occurrence_Of (Temp, Loc));
10908 return;
10909 end if;
10910 end Handle_Changed_Representation;
10912 -------------------------------
10913 -- Raise_Accessibility_Error --
10914 -------------------------------
10916 procedure Raise_Accessibility_Error is
10917 begin
10918 Error_Msg_Warn := SPARK_Mode /= On;
10919 Rewrite (N,
10920 Make_Raise_Program_Error (Sloc (N),
10921 Reason => PE_Accessibility_Check_Failed));
10922 Set_Etype (N, Target_Type);
10924 Error_Msg_N ("<<accessibility check failure", N);
10925 Error_Msg_NE ("\<<& [", N, Standard_Program_Error);
10926 end Raise_Accessibility_Error;
10928 ----------------------
10929 -- Real_Range_Check --
10930 ----------------------
10932 -- Case of conversions to floating-point or fixed-point. If range checks
10933 -- are enabled and the target type has a range constraint, we convert:
10935 -- typ (x)
10937 -- to
10939 -- Tnn : typ'Base := typ'Base (x);
10940 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10941 -- Tnn
10943 -- This is necessary when there is a conversion of integer to float or
10944 -- to fixed-point to ensure that the correct checks are made. It is not
10945 -- necessary for float to float where it is enough to simply set the
10946 -- Do_Range_Check flag.
10948 procedure Real_Range_Check is
10949 Btyp : constant Entity_Id := Base_Type (Target_Type);
10950 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
10951 Hi : constant Node_Id := Type_High_Bound (Target_Type);
10952 Xtyp : constant Entity_Id := Etype (Operand);
10954 Conv : Node_Id;
10955 Hi_Arg : Node_Id;
10956 Hi_Val : Node_Id;
10957 Lo_Arg : Node_Id;
10958 Lo_Val : Node_Id;
10959 Tnn : Entity_Id;
10961 begin
10962 -- Nothing to do if conversion was rewritten
10964 if Nkind (N) /= N_Type_Conversion then
10965 return;
10966 end if;
10968 -- Nothing to do if range checks suppressed, or target has the same
10969 -- range as the base type (or is the base type).
10971 if Range_Checks_Suppressed (Target_Type)
10972 or else (Lo = Type_Low_Bound (Btyp)
10973 and then
10974 Hi = Type_High_Bound (Btyp))
10975 then
10976 return;
10977 end if;
10979 -- Nothing to do if expression is an entity on which checks have been
10980 -- suppressed.
10982 if Is_Entity_Name (Operand)
10983 and then Range_Checks_Suppressed (Entity (Operand))
10984 then
10985 return;
10986 end if;
10988 -- Nothing to do if bounds are all static and we can tell that the
10989 -- expression is within the bounds of the target. Note that if the
10990 -- operand is of an unconstrained floating-point type, then we do
10991 -- not trust it to be in range (might be infinite)
10993 declare
10994 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
10995 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
10997 begin
10998 if (not Is_Floating_Point_Type (Xtyp)
10999 or else Is_Constrained (Xtyp))
11000 and then Compile_Time_Known_Value (S_Lo)
11001 and then Compile_Time_Known_Value (S_Hi)
11002 and then Compile_Time_Known_Value (Hi)
11003 and then Compile_Time_Known_Value (Lo)
11004 then
11005 declare
11006 D_Lov : constant Ureal := Expr_Value_R (Lo);
11007 D_Hiv : constant Ureal := Expr_Value_R (Hi);
11008 S_Lov : Ureal;
11009 S_Hiv : Ureal;
11011 begin
11012 if Is_Real_Type (Xtyp) then
11013 S_Lov := Expr_Value_R (S_Lo);
11014 S_Hiv := Expr_Value_R (S_Hi);
11015 else
11016 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
11017 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
11018 end if;
11020 if D_Hiv > D_Lov
11021 and then S_Lov >= D_Lov
11022 and then S_Hiv <= D_Hiv
11023 then
11024 -- Unset the range check flag on the current value of
11025 -- Expression (N), since the captured Operand may have
11026 -- been rewritten (such as for the case of a conversion
11027 -- to a fixed-point type).
11029 Set_Do_Range_Check (Expression (N), False);
11031 return;
11032 end if;
11033 end;
11034 end if;
11035 end;
11037 -- For float to float conversions, we are done
11039 if Is_Floating_Point_Type (Xtyp)
11040 and then
11041 Is_Floating_Point_Type (Btyp)
11042 then
11043 return;
11044 end if;
11046 -- Otherwise rewrite the conversion as described above
11048 Conv := Relocate_Node (N);
11049 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
11050 Set_Etype (Conv, Btyp);
11052 -- Enable overflow except for case of integer to float conversions,
11053 -- where it is never required, since we can never have overflow in
11054 -- this case.
11056 if not Is_Integer_Type (Etype (Operand)) then
11057 Enable_Overflow_Check (Conv);
11058 end if;
11060 Tnn := Make_Temporary (Loc, 'T', Conv);
11062 -- For a conversion from Float to Fixed where the bounds of the
11063 -- fixed-point type are static, we can obtain a more accurate
11064 -- fixed-point value by converting the result of the floating-
11065 -- point expression to an appropriate integer type, and then
11066 -- performing an unchecked conversion to the target fixed-point
11067 -- type. The range check can then use the corresponding integer
11068 -- value of the bounds instead of requiring further conversions.
11069 -- This preserves the identity:
11071 -- Fix_Val = Fixed_Type (Float_Type (Fix_Val))
11073 -- which used to fail when Fix_Val was a bound of the type and
11074 -- the 'Small was not a representable number.
11075 -- This transformation requires an integer type large enough to
11076 -- accommodate a fixed-point value. This will not be the case
11077 -- in systems where Duration is larger than Long_Integer.
11079 if Is_Ordinary_Fixed_Point_Type (Target_Type)
11080 and then Is_Floating_Point_Type (Operand_Type)
11081 and then RM_Size (Base_Type (Target_Type)) <=
11082 RM_Size (Standard_Long_Integer)
11083 and then Nkind (Lo) = N_Real_Literal
11084 and then Nkind (Hi) = N_Real_Literal
11085 then
11086 -- Find the integer type of the right size to perform an unchecked
11087 -- conversion to the target fixed-point type.
11089 declare
11090 Bfx_Type : constant Entity_Id := Base_Type (Target_Type);
11091 Expr_Id : constant Entity_Id :=
11092 Make_Temporary (Loc, 'T', Conv);
11093 Int_Type : Entity_Id;
11095 begin
11096 if RM_Size (Bfx_Type) > RM_Size (Standard_Integer) then
11097 Int_Type := Standard_Long_Integer;
11099 elsif RM_Size (Bfx_Type) > RM_Size (Standard_Short_Integer) then
11100 Int_Type := Standard_Integer;
11102 else
11103 Int_Type := Standard_Short_Integer;
11104 end if;
11106 -- Generate a temporary with the integer value. Required in the
11107 -- CCG compiler to ensure that runtime checks reference this
11108 -- integer expression (instead of the resulting fixed-point
11109 -- value) because fixed-point values are handled by means of
11110 -- unsigned integer types).
11112 Insert_Action (N,
11113 Make_Object_Declaration (Loc,
11114 Defining_Identifier => Expr_Id,
11115 Object_Definition => New_Occurrence_Of (Int_Type, Loc),
11116 Constant_Present => True,
11117 Expression =>
11118 Convert_To (Int_Type, Expression (Conv))));
11120 -- Create integer objects for range checking of result.
11122 Lo_Arg :=
11123 Unchecked_Convert_To
11124 (Int_Type, New_Occurrence_Of (Expr_Id, Loc));
11126 Lo_Val :=
11127 Make_Integer_Literal (Loc, Corresponding_Integer_Value (Lo));
11129 Hi_Arg :=
11130 Unchecked_Convert_To
11131 (Int_Type, New_Occurrence_Of (Expr_Id, Loc));
11133 Hi_Val :=
11134 Make_Integer_Literal (Loc, Corresponding_Integer_Value (Hi));
11136 -- Rewrite conversion as an integer conversion of the
11137 -- original floating-point expression, followed by an
11138 -- unchecked conversion to the target fixed-point type.
11140 Conv :=
11141 Make_Unchecked_Type_Conversion (Loc,
11142 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
11143 Expression => New_Occurrence_Of (Expr_Id, Loc));
11144 end;
11146 -- All other conversions
11148 else
11149 Lo_Arg := New_Occurrence_Of (Tnn, Loc);
11150 Lo_Val :=
11151 Make_Attribute_Reference (Loc,
11152 Prefix => New_Occurrence_Of (Target_Type, Loc),
11153 Attribute_Name => Name_First);
11155 Hi_Arg := New_Occurrence_Of (Tnn, Loc);
11156 Hi_Val :=
11157 Make_Attribute_Reference (Loc,
11158 Prefix => New_Occurrence_Of (Target_Type, Loc),
11159 Attribute_Name => Name_Last);
11160 end if;
11162 -- Build code for range checking
11164 Insert_Actions (N, New_List (
11165 Make_Object_Declaration (Loc,
11166 Defining_Identifier => Tnn,
11167 Object_Definition => New_Occurrence_Of (Btyp, Loc),
11168 Constant_Present => True,
11169 Expression => Conv),
11171 Make_Raise_Constraint_Error (Loc,
11172 Condition =>
11173 Make_Or_Else (Loc,
11174 Left_Opnd =>
11175 Make_Op_Lt (Loc,
11176 Left_Opnd => Lo_Arg,
11177 Right_Opnd => Lo_Val),
11179 Right_Opnd =>
11180 Make_Op_Gt (Loc,
11181 Left_Opnd => Hi_Arg,
11182 Right_Opnd => Hi_Val)),
11183 Reason => CE_Range_Check_Failed)));
11185 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
11186 Analyze_And_Resolve (N, Btyp);
11187 end Real_Range_Check;
11189 -----------------------------
11190 -- Has_Extra_Accessibility --
11191 -----------------------------
11193 -- Returns true for a formal of an anonymous access type or for an Ada
11194 -- 2012-style stand-alone object of an anonymous access type.
11196 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
11197 begin
11198 if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then
11199 return Present (Effective_Extra_Accessibility (Id));
11200 else
11201 return False;
11202 end if;
11203 end Has_Extra_Accessibility;
11205 -- Start of processing for Expand_N_Type_Conversion
11207 begin
11208 -- First remove check marks put by the semantic analysis on the type
11209 -- conversion between array types. We need these checks, and they will
11210 -- be generated by this expansion routine, but we do not depend on these
11211 -- flags being set, and since we do intend to expand the checks in the
11212 -- front end, we don't want them on the tree passed to the back end.
11214 if Is_Array_Type (Target_Type) then
11215 if Is_Constrained (Target_Type) then
11216 Set_Do_Length_Check (N, False);
11217 else
11218 Set_Do_Range_Check (Operand, False);
11219 end if;
11220 end if;
11222 -- Nothing at all to do if conversion is to the identical type so remove
11223 -- the conversion completely, it is useless, except that it may carry
11224 -- an Assignment_OK attribute, which must be propagated to the operand.
11226 if Operand_Type = Target_Type then
11227 if Assignment_OK (N) then
11228 Set_Assignment_OK (Operand);
11229 end if;
11231 Rewrite (N, Relocate_Node (Operand));
11232 goto Done;
11233 end if;
11235 -- Nothing to do if this is the second argument of read. This is a
11236 -- "backwards" conversion that will be handled by the specialized code
11237 -- in attribute processing.
11239 if Nkind (Parent (N)) = N_Attribute_Reference
11240 and then Attribute_Name (Parent (N)) = Name_Read
11241 and then Next (First (Expressions (Parent (N)))) = N
11242 then
11243 goto Done;
11244 end if;
11246 -- Check for case of converting to a type that has an invariant
11247 -- associated with it. This requires an invariant check. We insert
11248 -- a call:
11250 -- invariant_check (typ (expr))
11252 -- in the code, after removing side effects from the expression.
11253 -- This is clearer than replacing the conversion into an expression
11254 -- with actions, because the context may impose additional actions
11255 -- (tag checks, membership tests, etc.) that conflict with this
11256 -- rewriting (used previously).
11258 -- Note: the Comes_From_Source check, and then the resetting of this
11259 -- flag prevents what would otherwise be an infinite recursion.
11261 if Has_Invariants (Target_Type)
11262 and then Present (Invariant_Procedure (Target_Type))
11263 and then Comes_From_Source (N)
11264 then
11265 Set_Comes_From_Source (N, False);
11266 Remove_Side_Effects (N);
11267 Insert_Action (N, Make_Invariant_Call (Duplicate_Subexpr (N)));
11268 goto Done;
11269 end if;
11271 -- Here if we may need to expand conversion
11273 -- If the operand of the type conversion is an arithmetic operation on
11274 -- signed integers, and the based type of the signed integer type in
11275 -- question is smaller than Standard.Integer, we promote both of the
11276 -- operands to type Integer.
11278 -- For example, if we have
11280 -- target-type (opnd1 + opnd2)
11282 -- and opnd1 and opnd2 are of type short integer, then we rewrite
11283 -- this as:
11285 -- target-type (integer(opnd1) + integer(opnd2))
11287 -- We do this because we are always allowed to compute in a larger type
11288 -- if we do the right thing with the result, and in this case we are
11289 -- going to do a conversion which will do an appropriate check to make
11290 -- sure that things are in range of the target type in any case. This
11291 -- avoids some unnecessary intermediate overflows.
11293 -- We might consider a similar transformation in the case where the
11294 -- target is a real type or a 64-bit integer type, and the operand
11295 -- is an arithmetic operation using a 32-bit integer type. However,
11296 -- we do not bother with this case, because it could cause significant
11297 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
11298 -- much cheaper, but we don't want different behavior on 32-bit and
11299 -- 64-bit machines. Note that the exclusion of the 64-bit case also
11300 -- handles the configurable run-time cases where 64-bit arithmetic
11301 -- may simply be unavailable.
11303 -- Note: this circuit is partially redundant with respect to the circuit
11304 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
11305 -- the processing here. Also we still need the Checks circuit, since we
11306 -- have to be sure not to generate junk overflow checks in the first
11307 -- place, since it would be trick to remove them here.
11309 if Integer_Promotion_Possible (N) then
11311 -- All conditions met, go ahead with transformation
11313 declare
11314 Opnd : Node_Id;
11315 L, R : Node_Id;
11317 begin
11318 R :=
11319 Make_Type_Conversion (Loc,
11320 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
11321 Expression => Relocate_Node (Right_Opnd (Operand)));
11323 Opnd := New_Op_Node (Nkind (Operand), Loc);
11324 Set_Right_Opnd (Opnd, R);
11326 if Nkind (Operand) in N_Binary_Op then
11327 L :=
11328 Make_Type_Conversion (Loc,
11329 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
11330 Expression => Relocate_Node (Left_Opnd (Operand)));
11332 Set_Left_Opnd (Opnd, L);
11333 end if;
11335 Rewrite (N,
11336 Make_Type_Conversion (Loc,
11337 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
11338 Expression => Opnd));
11340 Analyze_And_Resolve (N, Target_Type);
11341 goto Done;
11342 end;
11343 end if;
11345 -- Do validity check if validity checking operands
11347 if Validity_Checks_On and Validity_Check_Operands then
11348 Ensure_Valid (Operand);
11349 end if;
11351 -- Special case of converting from non-standard boolean type
11353 if Is_Boolean_Type (Operand_Type)
11354 and then (Nonzero_Is_True (Operand_Type))
11355 then
11356 Adjust_Condition (Operand);
11357 Set_Etype (Operand, Standard_Boolean);
11358 Operand_Type := Standard_Boolean;
11359 end if;
11361 -- Case of converting to an access type
11363 if Is_Access_Type (Target_Type) then
11365 -- If this type conversion was internally generated by the front end
11366 -- to displace the pointer to the object to reference an interface
11367 -- type and the original node was an Unrestricted_Access attribute,
11368 -- then skip applying accessibility checks (because, according to the
11369 -- GNAT Reference Manual, this attribute is similar to 'Access except
11370 -- that all accessibility and aliased view checks are omitted).
11372 if not Comes_From_Source (N)
11373 and then Is_Interface (Designated_Type (Target_Type))
11374 and then Nkind (Original_Node (N)) = N_Attribute_Reference
11375 and then Attribute_Name (Original_Node (N)) =
11376 Name_Unrestricted_Access
11377 then
11378 null;
11380 -- Apply an accessibility check when the conversion operand is an
11381 -- access parameter (or a renaming thereof), unless conversion was
11382 -- expanded from an Unchecked_ or Unrestricted_Access attribute,
11383 -- or for the actual of a class-wide interface parameter. Note that
11384 -- other checks may still need to be applied below (such as tagged
11385 -- type checks).
11387 elsif Is_Entity_Name (Operand)
11388 and then Has_Extra_Accessibility (Entity (Operand))
11389 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
11390 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
11391 or else Attribute_Name (Original_Node (N)) = Name_Access)
11392 then
11393 if not Comes_From_Source (N)
11394 and then Nkind_In (Parent (N), N_Function_Call,
11395 N_Procedure_Call_Statement)
11396 and then Is_Interface (Designated_Type (Target_Type))
11397 and then Is_Class_Wide_Type (Designated_Type (Target_Type))
11398 then
11399 null;
11401 else
11402 Apply_Accessibility_Check
11403 (Operand, Target_Type, Insert_Node => Operand);
11404 end if;
11406 -- If the level of the operand type is statically deeper than the
11407 -- level of the target type, then force Program_Error. Note that this
11408 -- can only occur for cases where the attribute is within the body of
11409 -- an instantiation, otherwise the conversion will already have been
11410 -- rejected as illegal.
11412 -- Note: warnings are issued by the analyzer for the instance cases
11414 elsif In_Instance_Body
11416 -- The case where the target type is an anonymous access type of
11417 -- a discriminant is excluded, because the level of such a type
11418 -- depends on the context and currently the level returned for such
11419 -- types is zero, resulting in warnings about about check failures
11420 -- in certain legal cases involving class-wide interfaces as the
11421 -- designated type (some cases, such as return statements, are
11422 -- checked at run time, but not clear if these are handled right
11423 -- in general, see 3.10.2(12/2-12.5/3) ???).
11425 and then
11426 not (Ekind (Target_Type) = E_Anonymous_Access_Type
11427 and then Present (Associated_Node_For_Itype (Target_Type))
11428 and then Nkind (Associated_Node_For_Itype (Target_Type)) =
11429 N_Discriminant_Specification)
11430 and then
11431 Type_Access_Level (Operand_Type) > Type_Access_Level (Target_Type)
11432 then
11433 Raise_Accessibility_Error;
11434 goto Done;
11436 -- When the operand is a selected access discriminant the check needs
11437 -- to be made against the level of the object denoted by the prefix
11438 -- of the selected name. Force Program_Error for this case as well
11439 -- (this accessibility violation can only happen if within the body
11440 -- of an instantiation).
11442 elsif In_Instance_Body
11443 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
11444 and then Nkind (Operand) = N_Selected_Component
11445 and then Ekind (Entity (Selector_Name (Operand))) = E_Discriminant
11446 and then Object_Access_Level (Operand) >
11447 Type_Access_Level (Target_Type)
11448 then
11449 Raise_Accessibility_Error;
11450 goto Done;
11451 end if;
11452 end if;
11454 -- Case of conversions of tagged types and access to tagged types
11456 -- When needed, that is to say when the expression is class-wide, Add
11457 -- runtime a tag check for (strict) downward conversion by using the
11458 -- membership test, generating:
11460 -- [constraint_error when Operand not in Target_Type'Class]
11462 -- or in the access type case
11464 -- [constraint_error
11465 -- when Operand /= null
11466 -- and then Operand.all not in
11467 -- Designated_Type (Target_Type)'Class]
11469 if (Is_Access_Type (Target_Type)
11470 and then Is_Tagged_Type (Designated_Type (Target_Type)))
11471 or else Is_Tagged_Type (Target_Type)
11472 then
11473 -- Do not do any expansion in the access type case if the parent is a
11474 -- renaming, since this is an error situation which will be caught by
11475 -- Sem_Ch8, and the expansion can interfere with this error check.
11477 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
11478 goto Done;
11479 end if;
11481 -- Otherwise, proceed with processing tagged conversion
11483 Tagged_Conversion : declare
11484 Actual_Op_Typ : Entity_Id;
11485 Actual_Targ_Typ : Entity_Id;
11486 Make_Conversion : Boolean := False;
11487 Root_Op_Typ : Entity_Id;
11489 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
11490 -- Create a membership check to test whether Operand is a member
11491 -- of Targ_Typ. If the original Target_Type is an access, include
11492 -- a test for null value. The check is inserted at N.
11494 --------------------
11495 -- Make_Tag_Check --
11496 --------------------
11498 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
11499 Cond : Node_Id;
11501 begin
11502 -- Generate:
11503 -- [Constraint_Error
11504 -- when Operand /= null
11505 -- and then Operand.all not in Targ_Typ]
11507 if Is_Access_Type (Target_Type) then
11508 Cond :=
11509 Make_And_Then (Loc,
11510 Left_Opnd =>
11511 Make_Op_Ne (Loc,
11512 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
11513 Right_Opnd => Make_Null (Loc)),
11515 Right_Opnd =>
11516 Make_Not_In (Loc,
11517 Left_Opnd =>
11518 Make_Explicit_Dereference (Loc,
11519 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
11520 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)));
11522 -- Generate:
11523 -- [Constraint_Error when Operand not in Targ_Typ]
11525 else
11526 Cond :=
11527 Make_Not_In (Loc,
11528 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
11529 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc));
11530 end if;
11532 Insert_Action (N,
11533 Make_Raise_Constraint_Error (Loc,
11534 Condition => Cond,
11535 Reason => CE_Tag_Check_Failed),
11536 Suppress => All_Checks);
11537 end Make_Tag_Check;
11539 -- Start of processing for Tagged_Conversion
11541 begin
11542 -- Handle entities from the limited view
11544 if Is_Access_Type (Operand_Type) then
11545 Actual_Op_Typ :=
11546 Available_View (Designated_Type (Operand_Type));
11547 else
11548 Actual_Op_Typ := Operand_Type;
11549 end if;
11551 if Is_Access_Type (Target_Type) then
11552 Actual_Targ_Typ :=
11553 Available_View (Designated_Type (Target_Type));
11554 else
11555 Actual_Targ_Typ := Target_Type;
11556 end if;
11558 Root_Op_Typ := Root_Type (Actual_Op_Typ);
11560 -- Ada 2005 (AI-251): Handle interface type conversion
11562 if Is_Interface (Actual_Op_Typ)
11563 or else
11564 Is_Interface (Actual_Targ_Typ)
11565 then
11566 Expand_Interface_Conversion (N);
11567 goto Done;
11568 end if;
11570 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
11572 -- Create a runtime tag check for a downward class-wide type
11573 -- conversion.
11575 if Is_Class_Wide_Type (Actual_Op_Typ)
11576 and then Actual_Op_Typ /= Actual_Targ_Typ
11577 and then Root_Op_Typ /= Actual_Targ_Typ
11578 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
11579 Use_Full_View => True)
11580 then
11581 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
11582 Make_Conversion := True;
11583 end if;
11585 -- AI05-0073: If the result subtype of the function is defined
11586 -- by an access_definition designating a specific tagged type
11587 -- T, a check is made that the result value is null or the tag
11588 -- of the object designated by the result value identifies T.
11589 -- Constraint_Error is raised if this check fails.
11591 if Nkind (Parent (N)) = N_Simple_Return_Statement then
11592 declare
11593 Func : Entity_Id;
11594 Func_Typ : Entity_Id;
11596 begin
11597 -- Climb scope stack looking for the enclosing function
11599 Func := Current_Scope;
11600 while Present (Func)
11601 and then Ekind (Func) /= E_Function
11602 loop
11603 Func := Scope (Func);
11604 end loop;
11606 -- The function's return subtype must be defined using
11607 -- an access definition.
11609 if Nkind (Result_Definition (Parent (Func))) =
11610 N_Access_Definition
11611 then
11612 Func_Typ := Directly_Designated_Type (Etype (Func));
11614 -- The return subtype denotes a specific tagged type,
11615 -- in other words, a non class-wide type.
11617 if Is_Tagged_Type (Func_Typ)
11618 and then not Is_Class_Wide_Type (Func_Typ)
11619 then
11620 Make_Tag_Check (Actual_Targ_Typ);
11621 Make_Conversion := True;
11622 end if;
11623 end if;
11624 end;
11625 end if;
11627 -- We have generated a tag check for either a class-wide type
11628 -- conversion or for AI05-0073.
11630 if Make_Conversion then
11631 declare
11632 Conv : Node_Id;
11633 begin
11634 Conv :=
11635 Make_Unchecked_Type_Conversion (Loc,
11636 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
11637 Expression => Relocate_Node (Expression (N)));
11638 Rewrite (N, Conv);
11639 Analyze_And_Resolve (N, Target_Type);
11640 end;
11641 end if;
11642 end if;
11643 end Tagged_Conversion;
11645 -- Case of other access type conversions
11647 elsif Is_Access_Type (Target_Type) then
11648 Apply_Constraint_Check (Operand, Target_Type);
11650 -- Case of conversions from a fixed-point type
11652 -- These conversions require special expansion and processing, found in
11653 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
11654 -- since from a semantic point of view, these are simple integer
11655 -- conversions, which do not need further processing.
11657 elsif Is_Fixed_Point_Type (Operand_Type)
11658 and then not Conversion_OK (N)
11659 then
11660 -- We should never see universal fixed at this case, since the
11661 -- expansion of the constituent divide or multiply should have
11662 -- eliminated the explicit mention of universal fixed.
11664 pragma Assert (Operand_Type /= Universal_Fixed);
11666 -- Check for special case of the conversion to universal real that
11667 -- occurs as a result of the use of a round attribute. In this case,
11668 -- the real type for the conversion is taken from the target type of
11669 -- the Round attribute and the result must be marked as rounded.
11671 if Target_Type = Universal_Real
11672 and then Nkind (Parent (N)) = N_Attribute_Reference
11673 and then Attribute_Name (Parent (N)) = Name_Round
11674 then
11675 Set_Rounded_Result (N);
11676 Set_Etype (N, Etype (Parent (N)));
11677 end if;
11679 -- Otherwise do correct fixed-conversion, but skip these if the
11680 -- Conversion_OK flag is set, because from a semantic point of view
11681 -- these are simple integer conversions needing no further processing
11682 -- (the backend will simply treat them as integers).
11684 if not Conversion_OK (N) then
11685 if Is_Fixed_Point_Type (Etype (N)) then
11686 Expand_Convert_Fixed_To_Fixed (N);
11687 Real_Range_Check;
11689 elsif Is_Integer_Type (Etype (N)) then
11690 Expand_Convert_Fixed_To_Integer (N);
11692 else
11693 pragma Assert (Is_Floating_Point_Type (Etype (N)));
11694 Expand_Convert_Fixed_To_Float (N);
11695 Real_Range_Check;
11696 end if;
11697 end if;
11699 -- Case of conversions to a fixed-point type
11701 -- These conversions require special expansion and processing, found in
11702 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
11703 -- since from a semantic point of view, these are simple integer
11704 -- conversions, which do not need further processing.
11706 elsif Is_Fixed_Point_Type (Target_Type)
11707 and then not Conversion_OK (N)
11708 then
11709 if Is_Integer_Type (Operand_Type) then
11710 Expand_Convert_Integer_To_Fixed (N);
11711 Real_Range_Check;
11712 else
11713 pragma Assert (Is_Floating_Point_Type (Operand_Type));
11714 Expand_Convert_Float_To_Fixed (N);
11715 Real_Range_Check;
11716 end if;
11718 -- Case of float-to-integer conversions
11720 -- We also handle float-to-fixed conversions with Conversion_OK set
11721 -- since semantically the fixed-point target is treated as though it
11722 -- were an integer in such cases.
11724 elsif Is_Floating_Point_Type (Operand_Type)
11725 and then
11726 (Is_Integer_Type (Target_Type)
11727 or else
11728 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
11729 then
11730 -- One more check here, gcc is still not able to do conversions of
11731 -- this type with proper overflow checking, and so gigi is doing an
11732 -- approximation of what is required by doing floating-point compares
11733 -- with the end-point. But that can lose precision in some cases, and
11734 -- give a wrong result. Converting the operand to Universal_Real is
11735 -- helpful, but still does not catch all cases with 64-bit integers
11736 -- on targets with only 64-bit floats.
11738 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
11739 -- Can this code be removed ???
11741 if Do_Range_Check (Operand) then
11742 Rewrite (Operand,
11743 Make_Type_Conversion (Loc,
11744 Subtype_Mark =>
11745 New_Occurrence_Of (Universal_Real, Loc),
11746 Expression =>
11747 Relocate_Node (Operand)));
11749 Set_Etype (Operand, Universal_Real);
11750 Enable_Range_Check (Operand);
11751 Set_Do_Range_Check (Expression (Operand), False);
11752 end if;
11754 -- Case of array conversions
11756 -- Expansion of array conversions, add required length/range checks but
11757 -- only do this if there is no change of representation. For handling of
11758 -- this case, see Handle_Changed_Representation.
11760 elsif Is_Array_Type (Target_Type) then
11761 if Is_Constrained (Target_Type) then
11762 Apply_Length_Check (Operand, Target_Type);
11763 else
11764 Apply_Range_Check (Operand, Target_Type);
11765 end if;
11767 Handle_Changed_Representation;
11769 -- Case of conversions of discriminated types
11771 -- Add required discriminant checks if target is constrained. Again this
11772 -- change is skipped if we have a change of representation.
11774 elsif Has_Discriminants (Target_Type)
11775 and then Is_Constrained (Target_Type)
11776 then
11777 Apply_Discriminant_Check (Operand, Target_Type);
11778 Handle_Changed_Representation;
11780 -- Case of all other record conversions. The only processing required
11781 -- is to check for a change of representation requiring the special
11782 -- assignment processing.
11784 elsif Is_Record_Type (Target_Type) then
11786 -- Ada 2005 (AI-216): Program_Error is raised when converting from
11787 -- a derived Unchecked_Union type to an unconstrained type that is
11788 -- not Unchecked_Union if the operand lacks inferable discriminants.
11790 if Is_Derived_Type (Operand_Type)
11791 and then Is_Unchecked_Union (Base_Type (Operand_Type))
11792 and then not Is_Constrained (Target_Type)
11793 and then not Is_Unchecked_Union (Base_Type (Target_Type))
11794 and then not Has_Inferable_Discriminants (Operand)
11795 then
11796 -- To prevent Gigi from generating illegal code, we generate a
11797 -- Program_Error node, but we give it the target type of the
11798 -- conversion (is this requirement documented somewhere ???)
11800 declare
11801 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
11802 Reason => PE_Unchecked_Union_Restriction);
11804 begin
11805 Set_Etype (PE, Target_Type);
11806 Rewrite (N, PE);
11808 end;
11809 else
11810 Handle_Changed_Representation;
11811 end if;
11813 -- Case of conversions of enumeration types
11815 elsif Is_Enumeration_Type (Target_Type) then
11817 -- Special processing is required if there is a change of
11818 -- representation (from enumeration representation clauses).
11820 if not Same_Representation (Target_Type, Operand_Type) then
11822 -- Convert: x(y) to x'val (ytyp'val (y))
11824 Rewrite (N,
11825 Make_Attribute_Reference (Loc,
11826 Prefix => New_Occurrence_Of (Target_Type, Loc),
11827 Attribute_Name => Name_Val,
11828 Expressions => New_List (
11829 Make_Attribute_Reference (Loc,
11830 Prefix => New_Occurrence_Of (Operand_Type, Loc),
11831 Attribute_Name => Name_Pos,
11832 Expressions => New_List (Operand)))));
11834 Analyze_And_Resolve (N, Target_Type);
11835 end if;
11837 -- Case of conversions to floating-point
11839 elsif Is_Floating_Point_Type (Target_Type) then
11840 Real_Range_Check;
11841 end if;
11843 -- At this stage, either the conversion node has been transformed into
11844 -- some other equivalent expression, or left as a conversion that can be
11845 -- handled by Gigi, in the following cases:
11847 -- Conversions with no change of representation or type
11849 -- Numeric conversions involving integer, floating- and fixed-point
11850 -- values. Fixed-point values are allowed only if Conversion_OK is
11851 -- set, i.e. if the fixed-point values are to be treated as integers.
11853 -- No other conversions should be passed to Gigi
11855 -- Check: are these rules stated in sinfo??? if so, why restate here???
11857 -- The only remaining step is to generate a range check if we still have
11858 -- a type conversion at this stage and Do_Range_Check is set. For now we
11859 -- do this only for conversions of discrete types and for float-to-float
11860 -- conversions.
11862 if Nkind (N) = N_Type_Conversion then
11864 -- For now we only support floating-point cases where both source
11865 -- and target are floating-point types. Conversions where the source
11866 -- and target involve integer or fixed-point types are still TBD,
11867 -- though not clear whether those can even happen at this point, due
11868 -- to transformations above. ???
11870 if Is_Floating_Point_Type (Etype (N))
11871 and then Is_Floating_Point_Type (Etype (Expression (N)))
11872 then
11873 if Do_Range_Check (Expression (N))
11874 and then Is_Floating_Point_Type (Target_Type)
11875 then
11876 Generate_Range_Check
11877 (Expression (N), Target_Type, CE_Range_Check_Failed);
11878 end if;
11880 -- Discrete-to-discrete conversions
11882 elsif Is_Discrete_Type (Etype (N)) then
11883 declare
11884 Expr : constant Node_Id := Expression (N);
11885 Ftyp : Entity_Id;
11886 Ityp : Entity_Id;
11888 begin
11889 if Do_Range_Check (Expr)
11890 and then Is_Discrete_Type (Etype (Expr))
11891 then
11892 Set_Do_Range_Check (Expr, False);
11894 -- Before we do a range check, we have to deal with treating
11895 -- a fixed-point operand as an integer. The way we do this
11896 -- is simply to do an unchecked conversion to an appropriate
11897 -- integer type large enough to hold the result.
11899 -- This code is not active yet, because we are only dealing
11900 -- with discrete types so far ???
11902 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
11903 and then Treat_Fixed_As_Integer (Expr)
11904 then
11905 Ftyp := Base_Type (Etype (Expr));
11907 if Esize (Ftyp) >= Esize (Standard_Integer) then
11908 Ityp := Standard_Long_Long_Integer;
11909 else
11910 Ityp := Standard_Integer;
11911 end if;
11913 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
11914 end if;
11916 -- Reset overflow flag, since the range check will include
11917 -- dealing with possible overflow, and generate the check.
11918 -- If Address is either a source type or target type,
11919 -- suppress range check to avoid typing anomalies when
11920 -- it is a visible integer type.
11922 Set_Do_Overflow_Check (N, False);
11924 if not Is_Descendant_Of_Address (Etype (Expr))
11925 and then not Is_Descendant_Of_Address (Target_Type)
11926 then
11927 Generate_Range_Check
11928 (Expr, Target_Type, CE_Range_Check_Failed);
11929 end if;
11930 end if;
11931 end;
11932 end if;
11933 end if;
11935 -- Here at end of processing
11937 <<Done>>
11938 -- Apply predicate check if required. Note that we can't just call
11939 -- Apply_Predicate_Check here, because the type looks right after
11940 -- the conversion and it would omit the check. The Comes_From_Source
11941 -- guard is necessary to prevent infinite recursions when we generate
11942 -- internal conversions for the purpose of checking predicates.
11944 if Present (Predicate_Function (Target_Type))
11945 and then not Predicates_Ignored (Target_Type)
11946 and then Target_Type /= Operand_Type
11947 and then Comes_From_Source (N)
11948 then
11949 declare
11950 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
11952 begin
11953 -- Avoid infinite recursion on the subsequent expansion of
11954 -- of the copy of the original type conversion.
11956 Set_Comes_From_Source (New_Expr, False);
11957 Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr));
11958 end;
11959 end if;
11960 end Expand_N_Type_Conversion;
11962 -----------------------------------
11963 -- Expand_N_Unchecked_Expression --
11964 -----------------------------------
11966 -- Remove the unchecked expression node from the tree. Its job was simply
11967 -- to make sure that its constituent expression was handled with checks
11968 -- off, and now that that is done, we can remove it from the tree, and
11969 -- indeed must, since Gigi does not expect to see these nodes.
11971 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
11972 Exp : constant Node_Id := Expression (N);
11973 begin
11974 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
11975 Rewrite (N, Exp);
11976 end Expand_N_Unchecked_Expression;
11978 ----------------------------------------
11979 -- Expand_N_Unchecked_Type_Conversion --
11980 ----------------------------------------
11982 -- If this cannot be handled by Gigi and we haven't already made a
11983 -- temporary for it, do it now.
11985 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
11986 Target_Type : constant Entity_Id := Etype (N);
11987 Operand : constant Node_Id := Expression (N);
11988 Operand_Type : constant Entity_Id := Etype (Operand);
11990 begin
11991 -- Nothing at all to do if conversion is to the identical type so remove
11992 -- the conversion completely, it is useless, except that it may carry
11993 -- an Assignment_OK indication which must be propagated to the operand.
11995 if Operand_Type = Target_Type then
11997 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
11999 if Assignment_OK (N) then
12000 Set_Assignment_OK (Operand);
12001 end if;
12003 Rewrite (N, Relocate_Node (Operand));
12004 return;
12005 end if;
12007 -- If we have a conversion of a compile time known value to a target
12008 -- type and the value is in range of the target type, then we can simply
12009 -- replace the construct by an integer literal of the correct type. We
12010 -- only apply this to integer types being converted. Possibly it may
12011 -- apply in other cases, but it is too much trouble to worry about.
12013 -- Note that we do not do this transformation if the Kill_Range_Check
12014 -- flag is set, since then the value may be outside the expected range.
12015 -- This happens in the Normalize_Scalars case.
12017 -- We also skip this if either the target or operand type is biased
12018 -- because in this case, the unchecked conversion is supposed to
12019 -- preserve the bit pattern, not the integer value.
12021 if Is_Integer_Type (Target_Type)
12022 and then not Has_Biased_Representation (Target_Type)
12023 and then Is_Integer_Type (Operand_Type)
12024 and then not Has_Biased_Representation (Operand_Type)
12025 and then Compile_Time_Known_Value (Operand)
12026 and then not Kill_Range_Check (N)
12027 then
12028 declare
12029 Val : constant Uint := Expr_Value (Operand);
12031 begin
12032 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
12033 and then
12034 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
12035 and then
12036 Val >= Expr_Value (Type_Low_Bound (Target_Type))
12037 and then
12038 Val <= Expr_Value (Type_High_Bound (Target_Type))
12039 then
12040 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
12042 -- If Address is the target type, just set the type to avoid a
12043 -- spurious type error on the literal when Address is a visible
12044 -- integer type.
12046 if Is_Descendant_Of_Address (Target_Type) then
12047 Set_Etype (N, Target_Type);
12048 else
12049 Analyze_And_Resolve (N, Target_Type);
12050 end if;
12052 return;
12053 end if;
12054 end;
12055 end if;
12057 -- Nothing to do if conversion is safe
12059 if Safe_Unchecked_Type_Conversion (N) then
12060 return;
12061 end if;
12063 -- Otherwise force evaluation unless Assignment_OK flag is set (this
12064 -- flag indicates ??? More comments needed here)
12066 if Assignment_OK (N) then
12067 null;
12068 else
12069 Force_Evaluation (N);
12070 end if;
12071 end Expand_N_Unchecked_Type_Conversion;
12073 ----------------------------
12074 -- Expand_Record_Equality --
12075 ----------------------------
12077 -- For non-variant records, Equality is expanded when needed into:
12079 -- and then Lhs.Discr1 = Rhs.Discr1
12080 -- and then ...
12081 -- and then Lhs.Discrn = Rhs.Discrn
12082 -- and then Lhs.Cmp1 = Rhs.Cmp1
12083 -- and then ...
12084 -- and then Lhs.Cmpn = Rhs.Cmpn
12086 -- The expression is folded by the back end for adjacent fields. This
12087 -- function is called for tagged record in only one occasion: for imple-
12088 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
12089 -- otherwise the primitive "=" is used directly.
12091 function Expand_Record_Equality
12092 (Nod : Node_Id;
12093 Typ : Entity_Id;
12094 Lhs : Node_Id;
12095 Rhs : Node_Id;
12096 Bodies : List_Id) return Node_Id
12098 Loc : constant Source_Ptr := Sloc (Nod);
12100 Result : Node_Id;
12101 C : Entity_Id;
12103 First_Time : Boolean := True;
12105 function Element_To_Compare (C : Entity_Id) return Entity_Id;
12106 -- Return the next discriminant or component to compare, starting with
12107 -- C, skipping inherited components.
12109 ------------------------
12110 -- Element_To_Compare --
12111 ------------------------
12113 function Element_To_Compare (C : Entity_Id) return Entity_Id is
12114 Comp : Entity_Id;
12116 begin
12117 Comp := C;
12118 loop
12119 -- Exit loop when the next element to be compared is found, or
12120 -- there is no more such element.
12122 exit when No (Comp);
12124 exit when Ekind_In (Comp, E_Discriminant, E_Component)
12125 and then not (
12127 -- Skip inherited components
12129 -- Note: for a tagged type, we always generate the "=" primitive
12130 -- for the base type (not on the first subtype), so the test for
12131 -- Comp /= Original_Record_Component (Comp) is True for
12132 -- inherited components only.
12134 (Is_Tagged_Type (Typ)
12135 and then Comp /= Original_Record_Component (Comp))
12137 -- Skip _Tag
12139 or else Chars (Comp) = Name_uTag
12141 -- Skip interface elements (secondary tags???)
12143 or else Is_Interface (Etype (Comp)));
12145 Next_Entity (Comp);
12146 end loop;
12148 return Comp;
12149 end Element_To_Compare;
12151 -- Start of processing for Expand_Record_Equality
12153 begin
12154 -- Generates the following code: (assuming that Typ has one Discr and
12155 -- component C2 is also a record)
12157 -- Lhs.Discr1 = Rhs.Discr1
12158 -- and then Lhs.C1 = Rhs.C1
12159 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
12160 -- and then ...
12161 -- and then Lhs.Cmpn = Rhs.Cmpn
12163 Result := New_Occurrence_Of (Standard_True, Loc);
12164 C := Element_To_Compare (First_Entity (Typ));
12165 while Present (C) loop
12166 declare
12167 New_Lhs : Node_Id;
12168 New_Rhs : Node_Id;
12169 Check : Node_Id;
12171 begin
12172 if First_Time then
12173 New_Lhs := Lhs;
12174 New_Rhs := Rhs;
12175 else
12176 New_Lhs := New_Copy_Tree (Lhs);
12177 New_Rhs := New_Copy_Tree (Rhs);
12178 end if;
12180 Check :=
12181 Expand_Composite_Equality (Nod, Etype (C),
12182 Lhs =>
12183 Make_Selected_Component (Loc,
12184 Prefix => New_Lhs,
12185 Selector_Name => New_Occurrence_Of (C, Loc)),
12186 Rhs =>
12187 Make_Selected_Component (Loc,
12188 Prefix => New_Rhs,
12189 Selector_Name => New_Occurrence_Of (C, Loc)),
12190 Bodies => Bodies);
12192 -- If some (sub)component is an unchecked_union, the whole
12193 -- operation will raise program error.
12195 if Nkind (Check) = N_Raise_Program_Error then
12196 Result := Check;
12197 Set_Etype (Result, Standard_Boolean);
12198 exit;
12199 else
12200 if First_Time then
12201 Result := Check;
12203 -- Generate logical "and" for CodePeer to simplify the
12204 -- generated code and analysis.
12206 elsif CodePeer_Mode then
12207 Result :=
12208 Make_Op_And (Loc,
12209 Left_Opnd => Result,
12210 Right_Opnd => Check);
12212 else
12213 Result :=
12214 Make_And_Then (Loc,
12215 Left_Opnd => Result,
12216 Right_Opnd => Check);
12217 end if;
12218 end if;
12219 end;
12221 First_Time := False;
12222 C := Element_To_Compare (Next_Entity (C));
12223 end loop;
12225 return Result;
12226 end Expand_Record_Equality;
12228 ---------------------------
12229 -- Expand_Set_Membership --
12230 ---------------------------
12232 procedure Expand_Set_Membership (N : Node_Id) is
12233 Lop : constant Node_Id := Left_Opnd (N);
12234 Alt : Node_Id;
12235 Res : Node_Id;
12237 function Make_Cond (Alt : Node_Id) return Node_Id;
12238 -- If the alternative is a subtype mark, create a simple membership
12239 -- test. Otherwise create an equality test for it.
12241 ---------------
12242 -- Make_Cond --
12243 ---------------
12245 function Make_Cond (Alt : Node_Id) return Node_Id is
12246 Cond : Node_Id;
12247 L : constant Node_Id := New_Copy (Lop);
12248 R : constant Node_Id := Relocate_Node (Alt);
12250 begin
12251 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
12252 or else Nkind (Alt) = N_Range
12253 then
12254 Cond :=
12255 Make_In (Sloc (Alt),
12256 Left_Opnd => L,
12257 Right_Opnd => R);
12258 else
12259 Cond :=
12260 Make_Op_Eq (Sloc (Alt),
12261 Left_Opnd => L,
12262 Right_Opnd => R);
12263 end if;
12265 return Cond;
12266 end Make_Cond;
12268 -- Start of processing for Expand_Set_Membership
12270 begin
12271 Remove_Side_Effects (Lop);
12273 Alt := Last (Alternatives (N));
12274 Res := Make_Cond (Alt);
12276 Prev (Alt);
12277 while Present (Alt) loop
12278 Res :=
12279 Make_Or_Else (Sloc (Alt),
12280 Left_Opnd => Make_Cond (Alt),
12281 Right_Opnd => Res);
12282 Prev (Alt);
12283 end loop;
12285 Rewrite (N, Res);
12286 Analyze_And_Resolve (N, Standard_Boolean);
12287 end Expand_Set_Membership;
12289 -----------------------------------
12290 -- Expand_Short_Circuit_Operator --
12291 -----------------------------------
12293 -- Deal with special expansion if actions are present for the right operand
12294 -- and deal with optimizing case of arguments being True or False. We also
12295 -- deal with the special case of non-standard boolean values.
12297 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
12298 Loc : constant Source_Ptr := Sloc (N);
12299 Typ : constant Entity_Id := Etype (N);
12300 Left : constant Node_Id := Left_Opnd (N);
12301 Right : constant Node_Id := Right_Opnd (N);
12302 LocR : constant Source_Ptr := Sloc (Right);
12303 Actlist : List_Id;
12305 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
12306 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
12307 -- If Left = Shortcut_Value then Right need not be evaluated
12309 function Make_Test_Expr (Opnd : Node_Id) return Node_Id;
12310 -- For Opnd a boolean expression, return a Boolean expression equivalent
12311 -- to Opnd /= Shortcut_Value.
12313 --------------------
12314 -- Make_Test_Expr --
12315 --------------------
12317 function Make_Test_Expr (Opnd : Node_Id) return Node_Id is
12318 begin
12319 if Shortcut_Value then
12320 return Make_Op_Not (Sloc (Opnd), Opnd);
12321 else
12322 return Opnd;
12323 end if;
12324 end Make_Test_Expr;
12326 -- Local variables
12328 Op_Var : Entity_Id;
12329 -- Entity for a temporary variable holding the value of the operator,
12330 -- used for expansion in the case where actions are present.
12332 -- Start of processing for Expand_Short_Circuit_Operator
12334 begin
12335 -- Deal with non-standard booleans
12337 if Is_Boolean_Type (Typ) then
12338 Adjust_Condition (Left);
12339 Adjust_Condition (Right);
12340 Set_Etype (N, Standard_Boolean);
12341 end if;
12343 -- Check for cases where left argument is known to be True or False
12345 if Compile_Time_Known_Value (Left) then
12347 -- Mark SCO for left condition as compile time known
12349 if Generate_SCO and then Comes_From_Source (Left) then
12350 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
12351 end if;
12353 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
12354 -- Any actions associated with Right will be executed unconditionally
12355 -- and can thus be inserted into the tree unconditionally.
12357 if Expr_Value_E (Left) /= Shortcut_Ent then
12358 if Present (Actions (N)) then
12359 Insert_Actions (N, Actions (N));
12360 end if;
12362 Rewrite (N, Right);
12364 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
12365 -- In this case we can forget the actions associated with Right,
12366 -- since they will never be executed.
12368 else
12369 Kill_Dead_Code (Right);
12370 Kill_Dead_Code (Actions (N));
12371 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
12372 end if;
12374 Adjust_Result_Type (N, Typ);
12375 return;
12376 end if;
12378 -- If Actions are present for the right operand, we have to do some
12379 -- special processing. We can't just let these actions filter back into
12380 -- code preceding the short circuit (which is what would have happened
12381 -- if we had not trapped them in the short-circuit form), since they
12382 -- must only be executed if the right operand of the short circuit is
12383 -- executed and not otherwise.
12385 if Present (Actions (N)) then
12386 Actlist := Actions (N);
12388 -- The old approach is to expand:
12390 -- left AND THEN right
12392 -- into
12394 -- C : Boolean := False;
12395 -- IF left THEN
12396 -- Actions;
12397 -- IF right THEN
12398 -- C := True;
12399 -- END IF;
12400 -- END IF;
12402 -- and finally rewrite the operator into a reference to C. Similarly
12403 -- for left OR ELSE right, with negated values. Note that this
12404 -- rewrite causes some difficulties for coverage analysis because
12405 -- of the introduction of the new variable C, which obscures the
12406 -- structure of the test.
12408 -- We use this "old approach" if Minimize_Expression_With_Actions
12409 -- is True.
12411 if Minimize_Expression_With_Actions then
12412 Op_Var := Make_Temporary (Loc, 'C', Related_Node => N);
12414 Insert_Action (N,
12415 Make_Object_Declaration (Loc,
12416 Defining_Identifier => Op_Var,
12417 Object_Definition =>
12418 New_Occurrence_Of (Standard_Boolean, Loc),
12419 Expression =>
12420 New_Occurrence_Of (Shortcut_Ent, Loc)));
12422 Append_To (Actlist,
12423 Make_Implicit_If_Statement (Right,
12424 Condition => Make_Test_Expr (Right),
12425 Then_Statements => New_List (
12426 Make_Assignment_Statement (LocR,
12427 Name => New_Occurrence_Of (Op_Var, LocR),
12428 Expression =>
12429 New_Occurrence_Of
12430 (Boolean_Literals (not Shortcut_Value), LocR)))));
12432 Insert_Action (N,
12433 Make_Implicit_If_Statement (Left,
12434 Condition => Make_Test_Expr (Left),
12435 Then_Statements => Actlist));
12437 Rewrite (N, New_Occurrence_Of (Op_Var, Loc));
12438 Analyze_And_Resolve (N, Standard_Boolean);
12440 -- The new approach (the default) is to use an
12441 -- Expression_With_Actions node for the right operand of the
12442 -- short-circuit form. Note that this solves the traceability
12443 -- problems for coverage analysis.
12445 else
12446 Rewrite (Right,
12447 Make_Expression_With_Actions (LocR,
12448 Expression => Relocate_Node (Right),
12449 Actions => Actlist));
12451 Set_Actions (N, No_List);
12452 Analyze_And_Resolve (Right, Standard_Boolean);
12453 end if;
12455 Adjust_Result_Type (N, Typ);
12456 return;
12457 end if;
12459 -- No actions present, check for cases of right argument True/False
12461 if Compile_Time_Known_Value (Right) then
12463 -- Mark SCO for left condition as compile time known
12465 if Generate_SCO and then Comes_From_Source (Right) then
12466 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
12467 end if;
12469 -- Change (Left and then True), (Left or else False) to Left. Note
12470 -- that we know there are no actions associated with the right
12471 -- operand, since we just checked for this case above.
12473 if Expr_Value_E (Right) /= Shortcut_Ent then
12474 Rewrite (N, Left);
12476 -- Change (Left and then False), (Left or else True) to Right,
12477 -- making sure to preserve any side effects associated with the Left
12478 -- operand.
12480 else
12481 Remove_Side_Effects (Left);
12482 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
12483 end if;
12484 end if;
12486 Adjust_Result_Type (N, Typ);
12487 end Expand_Short_Circuit_Operator;
12489 -------------------------------------
12490 -- Fixup_Universal_Fixed_Operation --
12491 -------------------------------------
12493 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
12494 Conv : constant Node_Id := Parent (N);
12496 begin
12497 -- We must have a type conversion immediately above us
12499 pragma Assert (Nkind (Conv) = N_Type_Conversion);
12501 -- Normally the type conversion gives our target type. The exception
12502 -- occurs in the case of the Round attribute, where the conversion
12503 -- will be to universal real, and our real type comes from the Round
12504 -- attribute (as well as an indication that we must round the result)
12506 if Nkind (Parent (Conv)) = N_Attribute_Reference
12507 and then Attribute_Name (Parent (Conv)) = Name_Round
12508 then
12509 Set_Etype (N, Etype (Parent (Conv)));
12510 Set_Rounded_Result (N);
12512 -- Normal case where type comes from conversion above us
12514 else
12515 Set_Etype (N, Etype (Conv));
12516 end if;
12517 end Fixup_Universal_Fixed_Operation;
12519 ---------------------------------
12520 -- Has_Inferable_Discriminants --
12521 ---------------------------------
12523 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
12525 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
12526 -- Determines whether the left-most prefix of a selected component is a
12527 -- formal parameter in a subprogram. Assumes N is a selected component.
12529 --------------------------------
12530 -- Prefix_Is_Formal_Parameter --
12531 --------------------------------
12533 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
12534 Sel_Comp : Node_Id;
12536 begin
12537 -- Move to the left-most prefix by climbing up the tree
12539 Sel_Comp := N;
12540 while Present (Parent (Sel_Comp))
12541 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
12542 loop
12543 Sel_Comp := Parent (Sel_Comp);
12544 end loop;
12546 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
12547 end Prefix_Is_Formal_Parameter;
12549 -- Start of processing for Has_Inferable_Discriminants
12551 begin
12552 -- For selected components, the subtype of the selector must be a
12553 -- constrained Unchecked_Union. If the component is subject to a
12554 -- per-object constraint, then the enclosing object must have inferable
12555 -- discriminants.
12557 if Nkind (N) = N_Selected_Component then
12558 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
12560 -- A small hack. If we have a per-object constrained selected
12561 -- component of a formal parameter, return True since we do not
12562 -- know the actual parameter association yet.
12564 if Prefix_Is_Formal_Parameter (N) then
12565 return True;
12567 -- Otherwise, check the enclosing object and the selector
12569 else
12570 return Has_Inferable_Discriminants (Prefix (N))
12571 and then Has_Inferable_Discriminants (Selector_Name (N));
12572 end if;
12574 -- The call to Has_Inferable_Discriminants will determine whether
12575 -- the selector has a constrained Unchecked_Union nominal type.
12577 else
12578 return Has_Inferable_Discriminants (Selector_Name (N));
12579 end if;
12581 -- A qualified expression has inferable discriminants if its subtype
12582 -- mark is a constrained Unchecked_Union subtype.
12584 elsif Nkind (N) = N_Qualified_Expression then
12585 return Is_Unchecked_Union (Etype (Subtype_Mark (N)))
12586 and then Is_Constrained (Etype (Subtype_Mark (N)));
12588 -- For all other names, it is sufficient to have a constrained
12589 -- Unchecked_Union nominal subtype.
12591 else
12592 return Is_Unchecked_Union (Base_Type (Etype (N)))
12593 and then Is_Constrained (Etype (N));
12594 end if;
12595 end Has_Inferable_Discriminants;
12597 -------------------------------
12598 -- Insert_Dereference_Action --
12599 -------------------------------
12601 procedure Insert_Dereference_Action (N : Node_Id) is
12602 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
12603 -- Return true if type of P is derived from Checked_Pool;
12605 -----------------------------
12606 -- Is_Checked_Storage_Pool --
12607 -----------------------------
12609 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
12610 T : Entity_Id;
12612 begin
12613 if No (P) then
12614 return False;
12615 end if;
12617 T := Etype (P);
12618 while T /= Etype (T) loop
12619 if Is_RTE (T, RE_Checked_Pool) then
12620 return True;
12621 else
12622 T := Etype (T);
12623 end if;
12624 end loop;
12626 return False;
12627 end Is_Checked_Storage_Pool;
12629 -- Local variables
12631 Context : constant Node_Id := Parent (N);
12632 Ptr_Typ : constant Entity_Id := Etype (N);
12633 Desig_Typ : constant Entity_Id :=
12634 Available_View (Designated_Type (Ptr_Typ));
12635 Loc : constant Source_Ptr := Sloc (N);
12636 Pool : constant Entity_Id := Associated_Storage_Pool (Ptr_Typ);
12638 Addr : Entity_Id;
12639 Alig : Entity_Id;
12640 Deref : Node_Id;
12641 Size : Entity_Id;
12642 Size_Bits : Node_Id;
12643 Stmt : Node_Id;
12645 -- Start of processing for Insert_Dereference_Action
12647 begin
12648 pragma Assert (Nkind (Context) = N_Explicit_Dereference);
12650 -- Do not re-expand a dereference which has already been processed by
12651 -- this routine.
12653 if Has_Dereference_Action (Context) then
12654 return;
12656 -- Do not perform this type of expansion for internally-generated
12657 -- dereferences.
12659 elsif not Comes_From_Source (Original_Node (Context)) then
12660 return;
12662 -- A dereference action is only applicable to objects which have been
12663 -- allocated on a checked pool.
12665 elsif not Is_Checked_Storage_Pool (Pool) then
12666 return;
12667 end if;
12669 -- Extract the address of the dereferenced object. Generate:
12671 -- Addr : System.Address := <N>'Pool_Address;
12673 Addr := Make_Temporary (Loc, 'P');
12675 Insert_Action (N,
12676 Make_Object_Declaration (Loc,
12677 Defining_Identifier => Addr,
12678 Object_Definition =>
12679 New_Occurrence_Of (RTE (RE_Address), Loc),
12680 Expression =>
12681 Make_Attribute_Reference (Loc,
12682 Prefix => Duplicate_Subexpr_Move_Checks (N),
12683 Attribute_Name => Name_Pool_Address)));
12685 -- Calculate the size of the dereferenced object. Generate:
12687 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
12689 Deref :=
12690 Make_Explicit_Dereference (Loc,
12691 Prefix => Duplicate_Subexpr_Move_Checks (N));
12692 Set_Has_Dereference_Action (Deref);
12694 Size_Bits :=
12695 Make_Attribute_Reference (Loc,
12696 Prefix => Deref,
12697 Attribute_Name => Name_Size);
12699 -- Special case of an unconstrained array: need to add descriptor size
12701 if Is_Array_Type (Desig_Typ)
12702 and then not Is_Constrained (First_Subtype (Desig_Typ))
12703 then
12704 Size_Bits :=
12705 Make_Op_Add (Loc,
12706 Left_Opnd =>
12707 Make_Attribute_Reference (Loc,
12708 Prefix =>
12709 New_Occurrence_Of (First_Subtype (Desig_Typ), Loc),
12710 Attribute_Name => Name_Descriptor_Size),
12711 Right_Opnd => Size_Bits);
12712 end if;
12714 Size := Make_Temporary (Loc, 'S');
12715 Insert_Action (N,
12716 Make_Object_Declaration (Loc,
12717 Defining_Identifier => Size,
12718 Object_Definition =>
12719 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
12720 Expression =>
12721 Make_Op_Divide (Loc,
12722 Left_Opnd => Size_Bits,
12723 Right_Opnd => Make_Integer_Literal (Loc, System_Storage_Unit))));
12725 -- Calculate the alignment of the dereferenced object. Generate:
12726 -- Alig : constant Storage_Count := <N>.all'Alignment;
12728 Deref :=
12729 Make_Explicit_Dereference (Loc,
12730 Prefix => Duplicate_Subexpr_Move_Checks (N));
12731 Set_Has_Dereference_Action (Deref);
12733 Alig := Make_Temporary (Loc, 'A');
12734 Insert_Action (N,
12735 Make_Object_Declaration (Loc,
12736 Defining_Identifier => Alig,
12737 Object_Definition =>
12738 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
12739 Expression =>
12740 Make_Attribute_Reference (Loc,
12741 Prefix => Deref,
12742 Attribute_Name => Name_Alignment)));
12744 -- A dereference of a controlled object requires special processing. The
12745 -- finalization machinery requests additional space from the underlying
12746 -- pool to allocate and hide two pointers. As a result, a checked pool
12747 -- may mark the wrong memory as valid. Since checked pools do not have
12748 -- knowledge of hidden pointers, we have to bring the two pointers back
12749 -- in view in order to restore the original state of the object.
12751 -- The address manipulation is not performed for access types that are
12752 -- subject to pragma No_Heap_Finalization because the two pointers do
12753 -- not exist in the first place.
12755 if No_Heap_Finalization (Ptr_Typ) then
12756 null;
12758 elsif Needs_Finalization (Desig_Typ) then
12760 -- Adjust the address and size of the dereferenced object. Generate:
12761 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
12763 Stmt :=
12764 Make_Procedure_Call_Statement (Loc,
12765 Name =>
12766 New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc),
12767 Parameter_Associations => New_List (
12768 New_Occurrence_Of (Addr, Loc),
12769 New_Occurrence_Of (Size, Loc),
12770 New_Occurrence_Of (Alig, Loc)));
12772 -- Class-wide types complicate things because we cannot determine
12773 -- statically whether the actual object is truly controlled. We must
12774 -- generate a runtime check to detect this property. Generate:
12776 -- if Needs_Finalization (<N>.all'Tag) then
12777 -- <Stmt>;
12778 -- end if;
12780 if Is_Class_Wide_Type (Desig_Typ) then
12781 Deref :=
12782 Make_Explicit_Dereference (Loc,
12783 Prefix => Duplicate_Subexpr_Move_Checks (N));
12784 Set_Has_Dereference_Action (Deref);
12786 Stmt :=
12787 Make_Implicit_If_Statement (N,
12788 Condition =>
12789 Make_Function_Call (Loc,
12790 Name =>
12791 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
12792 Parameter_Associations => New_List (
12793 Make_Attribute_Reference (Loc,
12794 Prefix => Deref,
12795 Attribute_Name => Name_Tag))),
12796 Then_Statements => New_List (Stmt));
12797 end if;
12799 Insert_Action (N, Stmt);
12800 end if;
12802 -- Generate:
12803 -- Dereference (Pool, Addr, Size, Alig);
12805 Insert_Action (N,
12806 Make_Procedure_Call_Statement (Loc,
12807 Name =>
12808 New_Occurrence_Of
12809 (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
12810 Parameter_Associations => New_List (
12811 New_Occurrence_Of (Pool, Loc),
12812 New_Occurrence_Of (Addr, Loc),
12813 New_Occurrence_Of (Size, Loc),
12814 New_Occurrence_Of (Alig, Loc))));
12816 -- Mark the explicit dereference as processed to avoid potential
12817 -- infinite expansion.
12819 Set_Has_Dereference_Action (Context);
12821 exception
12822 when RE_Not_Available =>
12823 return;
12824 end Insert_Dereference_Action;
12826 --------------------------------
12827 -- Integer_Promotion_Possible --
12828 --------------------------------
12830 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
12831 Operand : constant Node_Id := Expression (N);
12832 Operand_Type : constant Entity_Id := Etype (Operand);
12833 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
12835 begin
12836 pragma Assert (Nkind (N) = N_Type_Conversion);
12838 return
12840 -- We only do the transformation for source constructs. We assume
12841 -- that the expander knows what it is doing when it generates code.
12843 Comes_From_Source (N)
12845 -- If the operand type is Short_Integer or Short_Short_Integer,
12846 -- then we will promote to Integer, which is available on all
12847 -- targets, and is sufficient to ensure no intermediate overflow.
12848 -- Furthermore it is likely to be as efficient or more efficient
12849 -- than using the smaller type for the computation so we do this
12850 -- unconditionally.
12852 and then
12853 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
12854 or else
12855 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
12857 -- Test for interesting operation, which includes addition,
12858 -- division, exponentiation, multiplication, subtraction, absolute
12859 -- value and unary negation. Unary "+" is omitted since it is a
12860 -- no-op and thus can't overflow.
12862 and then Nkind_In (Operand, N_Op_Abs,
12863 N_Op_Add,
12864 N_Op_Divide,
12865 N_Op_Expon,
12866 N_Op_Minus,
12867 N_Op_Multiply,
12868 N_Op_Subtract);
12869 end Integer_Promotion_Possible;
12871 ------------------------------
12872 -- Make_Array_Comparison_Op --
12873 ------------------------------
12875 -- This is a hand-coded expansion of the following generic function:
12877 -- generic
12878 -- type elem is (<>);
12879 -- type index is (<>);
12880 -- type a is array (index range <>) of elem;
12882 -- function Gnnn (X : a; Y: a) return boolean is
12883 -- J : index := Y'first;
12885 -- begin
12886 -- if X'length = 0 then
12887 -- return false;
12889 -- elsif Y'length = 0 then
12890 -- return true;
12892 -- else
12893 -- for I in X'range loop
12894 -- if X (I) = Y (J) then
12895 -- if J = Y'last then
12896 -- exit;
12897 -- else
12898 -- J := index'succ (J);
12899 -- end if;
12901 -- else
12902 -- return X (I) > Y (J);
12903 -- end if;
12904 -- end loop;
12906 -- return X'length > Y'length;
12907 -- end if;
12908 -- end Gnnn;
12910 -- Note that since we are essentially doing this expansion by hand, we
12911 -- do not need to generate an actual or formal generic part, just the
12912 -- instantiated function itself.
12914 -- Perhaps we could have the actual generic available in the run-time,
12915 -- obtained by rtsfind, and actually expand a real instantiation ???
12917 function Make_Array_Comparison_Op
12918 (Typ : Entity_Id;
12919 Nod : Node_Id) return Node_Id
12921 Loc : constant Source_Ptr := Sloc (Nod);
12923 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
12924 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
12925 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
12926 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
12928 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
12930 Loop_Statement : Node_Id;
12931 Loop_Body : Node_Id;
12932 If_Stat : Node_Id;
12933 Inner_If : Node_Id;
12934 Final_Expr : Node_Id;
12935 Func_Body : Node_Id;
12936 Func_Name : Entity_Id;
12937 Formals : List_Id;
12938 Length1 : Node_Id;
12939 Length2 : Node_Id;
12941 begin
12942 -- if J = Y'last then
12943 -- exit;
12944 -- else
12945 -- J := index'succ (J);
12946 -- end if;
12948 Inner_If :=
12949 Make_Implicit_If_Statement (Nod,
12950 Condition =>
12951 Make_Op_Eq (Loc,
12952 Left_Opnd => New_Occurrence_Of (J, Loc),
12953 Right_Opnd =>
12954 Make_Attribute_Reference (Loc,
12955 Prefix => New_Occurrence_Of (Y, Loc),
12956 Attribute_Name => Name_Last)),
12958 Then_Statements => New_List (
12959 Make_Exit_Statement (Loc)),
12961 Else_Statements =>
12962 New_List (
12963 Make_Assignment_Statement (Loc,
12964 Name => New_Occurrence_Of (J, Loc),
12965 Expression =>
12966 Make_Attribute_Reference (Loc,
12967 Prefix => New_Occurrence_Of (Index, Loc),
12968 Attribute_Name => Name_Succ,
12969 Expressions => New_List (New_Occurrence_Of (J, Loc))))));
12971 -- if X (I) = Y (J) then
12972 -- if ... end if;
12973 -- else
12974 -- return X (I) > Y (J);
12975 -- end if;
12977 Loop_Body :=
12978 Make_Implicit_If_Statement (Nod,
12979 Condition =>
12980 Make_Op_Eq (Loc,
12981 Left_Opnd =>
12982 Make_Indexed_Component (Loc,
12983 Prefix => New_Occurrence_Of (X, Loc),
12984 Expressions => New_List (New_Occurrence_Of (I, Loc))),
12986 Right_Opnd =>
12987 Make_Indexed_Component (Loc,
12988 Prefix => New_Occurrence_Of (Y, Loc),
12989 Expressions => New_List (New_Occurrence_Of (J, Loc)))),
12991 Then_Statements => New_List (Inner_If),
12993 Else_Statements => New_List (
12994 Make_Simple_Return_Statement (Loc,
12995 Expression =>
12996 Make_Op_Gt (Loc,
12997 Left_Opnd =>
12998 Make_Indexed_Component (Loc,
12999 Prefix => New_Occurrence_Of (X, Loc),
13000 Expressions => New_List (New_Occurrence_Of (I, Loc))),
13002 Right_Opnd =>
13003 Make_Indexed_Component (Loc,
13004 Prefix => New_Occurrence_Of (Y, Loc),
13005 Expressions => New_List (
13006 New_Occurrence_Of (J, Loc)))))));
13008 -- for I in X'range loop
13009 -- if ... end if;
13010 -- end loop;
13012 Loop_Statement :=
13013 Make_Implicit_Loop_Statement (Nod,
13014 Identifier => Empty,
13016 Iteration_Scheme =>
13017 Make_Iteration_Scheme (Loc,
13018 Loop_Parameter_Specification =>
13019 Make_Loop_Parameter_Specification (Loc,
13020 Defining_Identifier => I,
13021 Discrete_Subtype_Definition =>
13022 Make_Attribute_Reference (Loc,
13023 Prefix => New_Occurrence_Of (X, Loc),
13024 Attribute_Name => Name_Range))),
13026 Statements => New_List (Loop_Body));
13028 -- if X'length = 0 then
13029 -- return false;
13030 -- elsif Y'length = 0 then
13031 -- return true;
13032 -- else
13033 -- for ... loop ... end loop;
13034 -- return X'length > Y'length;
13035 -- end if;
13037 Length1 :=
13038 Make_Attribute_Reference (Loc,
13039 Prefix => New_Occurrence_Of (X, Loc),
13040 Attribute_Name => Name_Length);
13042 Length2 :=
13043 Make_Attribute_Reference (Loc,
13044 Prefix => New_Occurrence_Of (Y, Loc),
13045 Attribute_Name => Name_Length);
13047 Final_Expr :=
13048 Make_Op_Gt (Loc,
13049 Left_Opnd => Length1,
13050 Right_Opnd => Length2);
13052 If_Stat :=
13053 Make_Implicit_If_Statement (Nod,
13054 Condition =>
13055 Make_Op_Eq (Loc,
13056 Left_Opnd =>
13057 Make_Attribute_Reference (Loc,
13058 Prefix => New_Occurrence_Of (X, Loc),
13059 Attribute_Name => Name_Length),
13060 Right_Opnd =>
13061 Make_Integer_Literal (Loc, 0)),
13063 Then_Statements =>
13064 New_List (
13065 Make_Simple_Return_Statement (Loc,
13066 Expression => New_Occurrence_Of (Standard_False, Loc))),
13068 Elsif_Parts => New_List (
13069 Make_Elsif_Part (Loc,
13070 Condition =>
13071 Make_Op_Eq (Loc,
13072 Left_Opnd =>
13073 Make_Attribute_Reference (Loc,
13074 Prefix => New_Occurrence_Of (Y, Loc),
13075 Attribute_Name => Name_Length),
13076 Right_Opnd =>
13077 Make_Integer_Literal (Loc, 0)),
13079 Then_Statements =>
13080 New_List (
13081 Make_Simple_Return_Statement (Loc,
13082 Expression => New_Occurrence_Of (Standard_True, Loc))))),
13084 Else_Statements => New_List (
13085 Loop_Statement,
13086 Make_Simple_Return_Statement (Loc,
13087 Expression => Final_Expr)));
13089 -- (X : a; Y: a)
13091 Formals := New_List (
13092 Make_Parameter_Specification (Loc,
13093 Defining_Identifier => X,
13094 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
13096 Make_Parameter_Specification (Loc,
13097 Defining_Identifier => Y,
13098 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
13100 -- function Gnnn (...) return boolean is
13101 -- J : index := Y'first;
13102 -- begin
13103 -- if ... end if;
13104 -- end Gnnn;
13106 Func_Name := Make_Temporary (Loc, 'G');
13108 Func_Body :=
13109 Make_Subprogram_Body (Loc,
13110 Specification =>
13111 Make_Function_Specification (Loc,
13112 Defining_Unit_Name => Func_Name,
13113 Parameter_Specifications => Formals,
13114 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
13116 Declarations => New_List (
13117 Make_Object_Declaration (Loc,
13118 Defining_Identifier => J,
13119 Object_Definition => New_Occurrence_Of (Index, Loc),
13120 Expression =>
13121 Make_Attribute_Reference (Loc,
13122 Prefix => New_Occurrence_Of (Y, Loc),
13123 Attribute_Name => Name_First))),
13125 Handled_Statement_Sequence =>
13126 Make_Handled_Sequence_Of_Statements (Loc,
13127 Statements => New_List (If_Stat)));
13129 return Func_Body;
13130 end Make_Array_Comparison_Op;
13132 ---------------------------
13133 -- Make_Boolean_Array_Op --
13134 ---------------------------
13136 -- For logical operations on boolean arrays, expand in line the following,
13137 -- replacing 'and' with 'or' or 'xor' where needed:
13139 -- function Annn (A : typ; B: typ) return typ is
13140 -- C : typ;
13141 -- begin
13142 -- for J in A'range loop
13143 -- C (J) := A (J) op B (J);
13144 -- end loop;
13145 -- return C;
13146 -- end Annn;
13148 -- Here typ is the boolean array type
13150 function Make_Boolean_Array_Op
13151 (Typ : Entity_Id;
13152 N : Node_Id) return Node_Id
13154 Loc : constant Source_Ptr := Sloc (N);
13156 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
13157 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
13158 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
13159 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
13161 A_J : Node_Id;
13162 B_J : Node_Id;
13163 C_J : Node_Id;
13164 Op : Node_Id;
13166 Formals : List_Id;
13167 Func_Name : Entity_Id;
13168 Func_Body : Node_Id;
13169 Loop_Statement : Node_Id;
13171 begin
13172 A_J :=
13173 Make_Indexed_Component (Loc,
13174 Prefix => New_Occurrence_Of (A, Loc),
13175 Expressions => New_List (New_Occurrence_Of (J, Loc)));
13177 B_J :=
13178 Make_Indexed_Component (Loc,
13179 Prefix => New_Occurrence_Of (B, Loc),
13180 Expressions => New_List (New_Occurrence_Of (J, Loc)));
13182 C_J :=
13183 Make_Indexed_Component (Loc,
13184 Prefix => New_Occurrence_Of (C, Loc),
13185 Expressions => New_List (New_Occurrence_Of (J, Loc)));
13187 if Nkind (N) = N_Op_And then
13188 Op :=
13189 Make_Op_And (Loc,
13190 Left_Opnd => A_J,
13191 Right_Opnd => B_J);
13193 elsif Nkind (N) = N_Op_Or then
13194 Op :=
13195 Make_Op_Or (Loc,
13196 Left_Opnd => A_J,
13197 Right_Opnd => B_J);
13199 else
13200 Op :=
13201 Make_Op_Xor (Loc,
13202 Left_Opnd => A_J,
13203 Right_Opnd => B_J);
13204 end if;
13206 Loop_Statement :=
13207 Make_Implicit_Loop_Statement (N,
13208 Identifier => Empty,
13210 Iteration_Scheme =>
13211 Make_Iteration_Scheme (Loc,
13212 Loop_Parameter_Specification =>
13213 Make_Loop_Parameter_Specification (Loc,
13214 Defining_Identifier => J,
13215 Discrete_Subtype_Definition =>
13216 Make_Attribute_Reference (Loc,
13217 Prefix => New_Occurrence_Of (A, Loc),
13218 Attribute_Name => Name_Range))),
13220 Statements => New_List (
13221 Make_Assignment_Statement (Loc,
13222 Name => C_J,
13223 Expression => Op)));
13225 Formals := New_List (
13226 Make_Parameter_Specification (Loc,
13227 Defining_Identifier => A,
13228 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
13230 Make_Parameter_Specification (Loc,
13231 Defining_Identifier => B,
13232 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
13234 Func_Name := Make_Temporary (Loc, 'A');
13235 Set_Is_Inlined (Func_Name);
13237 Func_Body :=
13238 Make_Subprogram_Body (Loc,
13239 Specification =>
13240 Make_Function_Specification (Loc,
13241 Defining_Unit_Name => Func_Name,
13242 Parameter_Specifications => Formals,
13243 Result_Definition => New_Occurrence_Of (Typ, Loc)),
13245 Declarations => New_List (
13246 Make_Object_Declaration (Loc,
13247 Defining_Identifier => C,
13248 Object_Definition => New_Occurrence_Of (Typ, Loc))),
13250 Handled_Statement_Sequence =>
13251 Make_Handled_Sequence_Of_Statements (Loc,
13252 Statements => New_List (
13253 Loop_Statement,
13254 Make_Simple_Return_Statement (Loc,
13255 Expression => New_Occurrence_Of (C, Loc)))));
13257 return Func_Body;
13258 end Make_Boolean_Array_Op;
13260 -----------------------------------------
13261 -- Minimized_Eliminated_Overflow_Check --
13262 -----------------------------------------
13264 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is
13265 begin
13266 return
13267 Is_Signed_Integer_Type (Etype (N))
13268 and then Overflow_Check_Mode in Minimized_Or_Eliminated;
13269 end Minimized_Eliminated_Overflow_Check;
13271 --------------------------------
13272 -- Optimize_Length_Comparison --
13273 --------------------------------
13275 procedure Optimize_Length_Comparison (N : Node_Id) is
13276 Loc : constant Source_Ptr := Sloc (N);
13277 Typ : constant Entity_Id := Etype (N);
13278 Result : Node_Id;
13280 Left : Node_Id;
13281 Right : Node_Id;
13282 -- First and Last attribute reference nodes, which end up as left and
13283 -- right operands of the optimized result.
13285 Is_Zero : Boolean;
13286 -- True for comparison operand of zero
13288 Comp : Node_Id;
13289 -- Comparison operand, set only if Is_Zero is false
13291 Ent : Entity_Id := Empty;
13292 -- Entity whose length is being compared
13294 Index : Node_Id := Empty;
13295 -- Integer_Literal node for length attribute expression, or Empty
13296 -- if there is no such expression present.
13298 Ityp : Entity_Id;
13299 -- Type of array index to which 'Length is applied
13301 Op : Node_Kind := Nkind (N);
13302 -- Kind of comparison operator, gets flipped if operands backwards
13304 function Is_Optimizable (N : Node_Id) return Boolean;
13305 -- Tests N to see if it is an optimizable comparison value (defined as
13306 -- constant zero or one, or something else where the value is known to
13307 -- be positive and in the range of 32-bits, and where the corresponding
13308 -- Length value is also known to be 32-bits. If result is true, sets
13309 -- Is_Zero, Ityp, and Comp accordingly.
13311 function Is_Entity_Length (N : Node_Id) return Boolean;
13312 -- Tests if N is a length attribute applied to a simple entity. If so,
13313 -- returns True, and sets Ent to the entity, and Index to the integer
13314 -- literal provided as an attribute expression, or to Empty if none.
13315 -- Also returns True if the expression is a generated type conversion
13316 -- whose expression is of the desired form. This latter case arises
13317 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
13318 -- to check for being in range, which is not needed in this context.
13319 -- Returns False if neither condition holds.
13321 function Prepare_64 (N : Node_Id) return Node_Id;
13322 -- Given a discrete expression, returns a Long_Long_Integer typed
13323 -- expression representing the underlying value of the expression.
13324 -- This is done with an unchecked conversion to the result type. We
13325 -- use unchecked conversion to handle the enumeration type case.
13327 ----------------------
13328 -- Is_Entity_Length --
13329 ----------------------
13331 function Is_Entity_Length (N : Node_Id) return Boolean is
13332 begin
13333 if Nkind (N) = N_Attribute_Reference
13334 and then Attribute_Name (N) = Name_Length
13335 and then Is_Entity_Name (Prefix (N))
13336 then
13337 Ent := Entity (Prefix (N));
13339 if Present (Expressions (N)) then
13340 Index := First (Expressions (N));
13341 else
13342 Index := Empty;
13343 end if;
13345 return True;
13347 elsif Nkind (N) = N_Type_Conversion
13348 and then not Comes_From_Source (N)
13349 then
13350 return Is_Entity_Length (Expression (N));
13352 else
13353 return False;
13354 end if;
13355 end Is_Entity_Length;
13357 --------------------
13358 -- Is_Optimizable --
13359 --------------------
13361 function Is_Optimizable (N : Node_Id) return Boolean is
13362 Val : Uint;
13363 OK : Boolean;
13364 Lo : Uint;
13365 Hi : Uint;
13366 Indx : Node_Id;
13368 begin
13369 if Compile_Time_Known_Value (N) then
13370 Val := Expr_Value (N);
13372 if Val = Uint_0 then
13373 Is_Zero := True;
13374 Comp := Empty;
13375 return True;
13377 elsif Val = Uint_1 then
13378 Is_Zero := False;
13379 Comp := Empty;
13380 return True;
13381 end if;
13382 end if;
13384 -- Here we have to make sure of being within 32-bits
13386 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
13388 if not OK
13389 or else Lo < Uint_1
13390 or else Hi > UI_From_Int (Int'Last)
13391 then
13392 return False;
13393 end if;
13395 -- Comparison value was within range, so now we must check the index
13396 -- value to make sure it is also within 32-bits.
13398 Indx := First_Index (Etype (Ent));
13400 if Present (Index) then
13401 for J in 2 .. UI_To_Int (Intval (Index)) loop
13402 Next_Index (Indx);
13403 end loop;
13404 end if;
13406 Ityp := Etype (Indx);
13408 if Esize (Ityp) > 32 then
13409 return False;
13410 end if;
13412 Is_Zero := False;
13413 Comp := N;
13414 return True;
13415 end Is_Optimizable;
13417 ----------------
13418 -- Prepare_64 --
13419 ----------------
13421 function Prepare_64 (N : Node_Id) return Node_Id is
13422 begin
13423 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
13424 end Prepare_64;
13426 -- Start of processing for Optimize_Length_Comparison
13428 begin
13429 -- Nothing to do if not a comparison
13431 if Op not in N_Op_Compare then
13432 return;
13433 end if;
13435 -- Nothing to do if special -gnatd.P debug flag set.
13437 if Debug_Flag_Dot_PP then
13438 return;
13439 end if;
13441 -- Ent'Length op 0/1
13443 if Is_Entity_Length (Left_Opnd (N))
13444 and then Is_Optimizable (Right_Opnd (N))
13445 then
13446 null;
13448 -- 0/1 op Ent'Length
13450 elsif Is_Entity_Length (Right_Opnd (N))
13451 and then Is_Optimizable (Left_Opnd (N))
13452 then
13453 -- Flip comparison to opposite sense
13455 case Op is
13456 when N_Op_Lt => Op := N_Op_Gt;
13457 when N_Op_Le => Op := N_Op_Ge;
13458 when N_Op_Gt => Op := N_Op_Lt;
13459 when N_Op_Ge => Op := N_Op_Le;
13460 when others => null;
13461 end case;
13463 -- Else optimization not possible
13465 else
13466 return;
13467 end if;
13469 -- Fall through if we will do the optimization
13471 -- Cases to handle:
13473 -- X'Length = 0 => X'First > X'Last
13474 -- X'Length = 1 => X'First = X'Last
13475 -- X'Length = n => X'First + (n - 1) = X'Last
13477 -- X'Length /= 0 => X'First <= X'Last
13478 -- X'Length /= 1 => X'First /= X'Last
13479 -- X'Length /= n => X'First + (n - 1) /= X'Last
13481 -- X'Length >= 0 => always true, warn
13482 -- X'Length >= 1 => X'First <= X'Last
13483 -- X'Length >= n => X'First + (n - 1) <= X'Last
13485 -- X'Length > 0 => X'First <= X'Last
13486 -- X'Length > 1 => X'First < X'Last
13487 -- X'Length > n => X'First + (n - 1) < X'Last
13489 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
13490 -- X'Length <= 1 => X'First >= X'Last
13491 -- X'Length <= n => X'First + (n - 1) >= X'Last
13493 -- X'Length < 0 => always false (warn)
13494 -- X'Length < 1 => X'First > X'Last
13495 -- X'Length < n => X'First + (n - 1) > X'Last
13497 -- Note: for the cases of n (not constant 0,1), we require that the
13498 -- corresponding index type be integer or shorter (i.e. not 64-bit),
13499 -- and the same for the comparison value. Then we do the comparison
13500 -- using 64-bit arithmetic (actually long long integer), so that we
13501 -- cannot have overflow intefering with the result.
13503 -- First deal with warning cases
13505 if Is_Zero then
13506 case Op is
13508 -- X'Length >= 0
13510 when N_Op_Ge =>
13511 Rewrite (N,
13512 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
13513 Analyze_And_Resolve (N, Typ);
13514 Warn_On_Known_Condition (N);
13515 return;
13517 -- X'Length < 0
13519 when N_Op_Lt =>
13520 Rewrite (N,
13521 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
13522 Analyze_And_Resolve (N, Typ);
13523 Warn_On_Known_Condition (N);
13524 return;
13526 when N_Op_Le =>
13527 if Constant_Condition_Warnings
13528 and then Comes_From_Source (Original_Node (N))
13529 then
13530 Error_Msg_N ("could replace by ""'=""?c?", N);
13531 end if;
13533 Op := N_Op_Eq;
13535 when others =>
13536 null;
13537 end case;
13538 end if;
13540 -- Build the First reference we will use
13542 Left :=
13543 Make_Attribute_Reference (Loc,
13544 Prefix => New_Occurrence_Of (Ent, Loc),
13545 Attribute_Name => Name_First);
13547 if Present (Index) then
13548 Set_Expressions (Left, New_List (New_Copy (Index)));
13549 end if;
13551 -- If general value case, then do the addition of (n - 1), and
13552 -- also add the needed conversions to type Long_Long_Integer.
13554 if Present (Comp) then
13555 Left :=
13556 Make_Op_Add (Loc,
13557 Left_Opnd => Prepare_64 (Left),
13558 Right_Opnd =>
13559 Make_Op_Subtract (Loc,
13560 Left_Opnd => Prepare_64 (Comp),
13561 Right_Opnd => Make_Integer_Literal (Loc, 1)));
13562 end if;
13564 -- Build the Last reference we will use
13566 Right :=
13567 Make_Attribute_Reference (Loc,
13568 Prefix => New_Occurrence_Of (Ent, Loc),
13569 Attribute_Name => Name_Last);
13571 if Present (Index) then
13572 Set_Expressions (Right, New_List (New_Copy (Index)));
13573 end if;
13575 -- If general operand, convert Last reference to Long_Long_Integer
13577 if Present (Comp) then
13578 Right := Prepare_64 (Right);
13579 end if;
13581 -- Check for cases to optimize
13583 -- X'Length = 0 => X'First > X'Last
13584 -- X'Length < 1 => X'First > X'Last
13585 -- X'Length < n => X'First + (n - 1) > X'Last
13587 if (Is_Zero and then Op = N_Op_Eq)
13588 or else (not Is_Zero and then Op = N_Op_Lt)
13589 then
13590 Result :=
13591 Make_Op_Gt (Loc,
13592 Left_Opnd => Left,
13593 Right_Opnd => Right);
13595 -- X'Length = 1 => X'First = X'Last
13596 -- X'Length = n => X'First + (n - 1) = X'Last
13598 elsif not Is_Zero and then Op = N_Op_Eq then
13599 Result :=
13600 Make_Op_Eq (Loc,
13601 Left_Opnd => Left,
13602 Right_Opnd => Right);
13604 -- X'Length /= 0 => X'First <= X'Last
13605 -- X'Length > 0 => X'First <= X'Last
13607 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
13608 Result :=
13609 Make_Op_Le (Loc,
13610 Left_Opnd => Left,
13611 Right_Opnd => Right);
13613 -- X'Length /= 1 => X'First /= X'Last
13614 -- X'Length /= n => X'First + (n - 1) /= X'Last
13616 elsif not Is_Zero and then Op = N_Op_Ne then
13617 Result :=
13618 Make_Op_Ne (Loc,
13619 Left_Opnd => Left,
13620 Right_Opnd => Right);
13622 -- X'Length >= 1 => X'First <= X'Last
13623 -- X'Length >= n => X'First + (n - 1) <= X'Last
13625 elsif not Is_Zero and then Op = N_Op_Ge then
13626 Result :=
13627 Make_Op_Le (Loc,
13628 Left_Opnd => Left,
13629 Right_Opnd => Right);
13631 -- X'Length > 1 => X'First < X'Last
13632 -- X'Length > n => X'First + (n = 1) < X'Last
13634 elsif not Is_Zero and then Op = N_Op_Gt then
13635 Result :=
13636 Make_Op_Lt (Loc,
13637 Left_Opnd => Left,
13638 Right_Opnd => Right);
13640 -- X'Length <= 1 => X'First >= X'Last
13641 -- X'Length <= n => X'First + (n - 1) >= X'Last
13643 elsif not Is_Zero and then Op = N_Op_Le then
13644 Result :=
13645 Make_Op_Ge (Loc,
13646 Left_Opnd => Left,
13647 Right_Opnd => Right);
13649 -- Should not happen at this stage
13651 else
13652 raise Program_Error;
13653 end if;
13655 -- Rewrite and finish up
13657 Rewrite (N, Result);
13658 Analyze_And_Resolve (N, Typ);
13659 return;
13660 end Optimize_Length_Comparison;
13662 --------------------------------
13663 -- Process_If_Case_Statements --
13664 --------------------------------
13666 procedure Process_If_Case_Statements (N : Node_Id; Stmts : List_Id) is
13667 Decl : Node_Id;
13669 begin
13670 Decl := First (Stmts);
13671 while Present (Decl) loop
13672 if Nkind (Decl) = N_Object_Declaration
13673 and then Is_Finalizable_Transient (Decl, N)
13674 then
13675 Process_Transient_In_Expression (Decl, N, Stmts);
13676 end if;
13678 Next (Decl);
13679 end loop;
13680 end Process_If_Case_Statements;
13682 -------------------------------------
13683 -- Process_Transient_In_Expression --
13684 -------------------------------------
13686 procedure Process_Transient_In_Expression
13687 (Obj_Decl : Node_Id;
13688 Expr : Node_Id;
13689 Stmts : List_Id)
13691 Loc : constant Source_Ptr := Sloc (Obj_Decl);
13692 Obj_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
13694 Hook_Context : constant Node_Id := Find_Hook_Context (Expr);
13695 -- The node on which to insert the hook as an action. This is usually
13696 -- the innermost enclosing non-transient construct.
13698 Fin_Call : Node_Id;
13699 Hook_Assign : Node_Id;
13700 Hook_Clear : Node_Id;
13701 Hook_Decl : Node_Id;
13702 Hook_Insert : Node_Id;
13703 Ptr_Decl : Node_Id;
13705 Fin_Context : Node_Id;
13706 -- The node after which to insert the finalization actions of the
13707 -- transient object.
13709 begin
13710 pragma Assert (Nkind_In (Expr, N_Case_Expression,
13711 N_Expression_With_Actions,
13712 N_If_Expression));
13714 -- When the context is a Boolean evaluation, all three nodes capture the
13715 -- result of their computation in a local temporary:
13717 -- do
13718 -- Trans_Id : Ctrl_Typ := ...;
13719 -- Result : constant Boolean := ... Trans_Id ...;
13720 -- <finalize Trans_Id>
13721 -- in Result end;
13723 -- As a result, the finalization of any transient objects can safely
13724 -- take place after the result capture.
13726 -- ??? could this be extended to elementary types?
13728 if Is_Boolean_Type (Etype (Expr)) then
13729 Fin_Context := Last (Stmts);
13731 -- Otherwise the immediate context may not be safe enough to carry
13732 -- out transient object finalization due to aliasing and nesting of
13733 -- constructs. Insert calls to [Deep_]Finalize after the innermost
13734 -- enclosing non-transient construct.
13736 else
13737 Fin_Context := Hook_Context;
13738 end if;
13740 -- Mark the transient object as successfully processed to avoid double
13741 -- finalization.
13743 Set_Is_Finalized_Transient (Obj_Id);
13745 -- Construct all the pieces necessary to hook and finalize a transient
13746 -- object.
13748 Build_Transient_Object_Statements
13749 (Obj_Decl => Obj_Decl,
13750 Fin_Call => Fin_Call,
13751 Hook_Assign => Hook_Assign,
13752 Hook_Clear => Hook_Clear,
13753 Hook_Decl => Hook_Decl,
13754 Ptr_Decl => Ptr_Decl,
13755 Finalize_Obj => False);
13757 -- Add the access type which provides a reference to the transient
13758 -- object. Generate:
13760 -- type Ptr_Typ is access all Desig_Typ;
13762 Insert_Action (Hook_Context, Ptr_Decl);
13764 -- Add the temporary which acts as a hook to the transient object.
13765 -- Generate:
13767 -- Hook : Ptr_Id := null;
13769 Insert_Action (Hook_Context, Hook_Decl);
13771 -- When the transient object is initialized by an aggregate, the hook
13772 -- must capture the object after the last aggregate assignment takes
13773 -- place. Only then is the object considered initialized. Generate:
13775 -- Hook := Ptr_Typ (Obj_Id);
13776 -- <or>
13777 -- Hook := Obj_Id'Unrestricted_Access;
13779 if Ekind_In (Obj_Id, E_Constant, E_Variable)
13780 and then Present (Last_Aggregate_Assignment (Obj_Id))
13781 then
13782 Hook_Insert := Last_Aggregate_Assignment (Obj_Id);
13784 -- Otherwise the hook seizes the related object immediately
13786 else
13787 Hook_Insert := Obj_Decl;
13788 end if;
13790 Insert_After_And_Analyze (Hook_Insert, Hook_Assign);
13792 -- When the node is part of a return statement, there is no need to
13793 -- insert a finalization call, as the general finalization mechanism
13794 -- (see Build_Finalizer) would take care of the transient object on
13795 -- subprogram exit. Note that it would also be impossible to insert the
13796 -- finalization code after the return statement as this will render it
13797 -- unreachable.
13799 if Nkind (Fin_Context) = N_Simple_Return_Statement then
13800 null;
13802 -- Finalize the hook after the context has been evaluated. Generate:
13804 -- if Hook /= null then
13805 -- [Deep_]Finalize (Hook.all);
13806 -- Hook := null;
13807 -- end if;
13809 else
13810 Insert_Action_After (Fin_Context,
13811 Make_Implicit_If_Statement (Obj_Decl,
13812 Condition =>
13813 Make_Op_Ne (Loc,
13814 Left_Opnd =>
13815 New_Occurrence_Of (Defining_Entity (Hook_Decl), Loc),
13816 Right_Opnd => Make_Null (Loc)),
13818 Then_Statements => New_List (
13819 Fin_Call,
13820 Hook_Clear)));
13821 end if;
13822 end Process_Transient_In_Expression;
13824 ------------------------
13825 -- Rewrite_Comparison --
13826 ------------------------
13828 procedure Rewrite_Comparison (N : Node_Id) is
13829 Typ : constant Entity_Id := Etype (N);
13831 False_Result : Boolean;
13832 True_Result : Boolean;
13834 begin
13835 if Nkind (N) = N_Type_Conversion then
13836 Rewrite_Comparison (Expression (N));
13837 return;
13839 elsif Nkind (N) not in N_Op_Compare then
13840 return;
13841 end if;
13843 -- Determine the potential outcome of the comparison assuming that the
13844 -- operands are valid and emit a warning when the comparison evaluates
13845 -- to True or False only in the presence of invalid values.
13847 Warn_On_Constant_Valid_Condition (N);
13849 -- Determine the potential outcome of the comparison assuming that the
13850 -- operands are not valid.
13852 Test_Comparison
13853 (Op => N,
13854 Assume_Valid => False,
13855 True_Result => True_Result,
13856 False_Result => False_Result);
13858 -- The outcome is a decisive False or True, rewrite the operator
13860 if False_Result or True_Result then
13861 Rewrite (N,
13862 Convert_To (Typ,
13863 New_Occurrence_Of (Boolean_Literals (True_Result), Sloc (N))));
13865 Analyze_And_Resolve (N, Typ);
13866 Warn_On_Known_Condition (N);
13867 end if;
13868 end Rewrite_Comparison;
13870 ----------------------------
13871 -- Safe_In_Place_Array_Op --
13872 ----------------------------
13874 function Safe_In_Place_Array_Op
13875 (Lhs : Node_Id;
13876 Op1 : Node_Id;
13877 Op2 : Node_Id) return Boolean
13879 Target : Entity_Id;
13881 function Is_Safe_Operand (Op : Node_Id) return Boolean;
13882 -- Operand is safe if it cannot overlap part of the target of the
13883 -- operation. If the operand and the target are identical, the operand
13884 -- is safe. The operand can be empty in the case of negation.
13886 function Is_Unaliased (N : Node_Id) return Boolean;
13887 -- Check that N is a stand-alone entity
13889 ------------------
13890 -- Is_Unaliased --
13891 ------------------
13893 function Is_Unaliased (N : Node_Id) return Boolean is
13894 begin
13895 return
13896 Is_Entity_Name (N)
13897 and then No (Address_Clause (Entity (N)))
13898 and then No (Renamed_Object (Entity (N)));
13899 end Is_Unaliased;
13901 ---------------------
13902 -- Is_Safe_Operand --
13903 ---------------------
13905 function Is_Safe_Operand (Op : Node_Id) return Boolean is
13906 begin
13907 if No (Op) then
13908 return True;
13910 elsif Is_Entity_Name (Op) then
13911 return Is_Unaliased (Op);
13913 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
13914 return Is_Unaliased (Prefix (Op));
13916 elsif Nkind (Op) = N_Slice then
13917 return
13918 Is_Unaliased (Prefix (Op))
13919 and then Entity (Prefix (Op)) /= Target;
13921 elsif Nkind (Op) = N_Op_Not then
13922 return Is_Safe_Operand (Right_Opnd (Op));
13924 else
13925 return False;
13926 end if;
13927 end Is_Safe_Operand;
13929 -- Start of processing for Safe_In_Place_Array_Op
13931 begin
13932 -- Skip this processing if the component size is different from system
13933 -- storage unit (since at least for NOT this would cause problems).
13935 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
13936 return False;
13938 -- Cannot do in place stuff if non-standard Boolean representation
13940 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
13941 return False;
13943 elsif not Is_Unaliased (Lhs) then
13944 return False;
13946 else
13947 Target := Entity (Lhs);
13948 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
13949 end if;
13950 end Safe_In_Place_Array_Op;
13952 -----------------------
13953 -- Tagged_Membership --
13954 -----------------------
13956 -- There are two different cases to consider depending on whether the right
13957 -- operand is a class-wide type or not. If not we just compare the actual
13958 -- tag of the left expr to the target type tag:
13960 -- Left_Expr.Tag = Right_Type'Tag;
13962 -- If it is a class-wide type we use the RT function CW_Membership which is
13963 -- usually implemented by looking in the ancestor tables contained in the
13964 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
13966 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13967 -- function IW_Membership which is usually implemented by looking in the
13968 -- table of abstract interface types plus the ancestor table contained in
13969 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13971 procedure Tagged_Membership
13972 (N : Node_Id;
13973 SCIL_Node : out Node_Id;
13974 Result : out Node_Id)
13976 Left : constant Node_Id := Left_Opnd (N);
13977 Right : constant Node_Id := Right_Opnd (N);
13978 Loc : constant Source_Ptr := Sloc (N);
13980 Full_R_Typ : Entity_Id;
13981 Left_Type : Entity_Id;
13982 New_Node : Node_Id;
13983 Right_Type : Entity_Id;
13984 Obj_Tag : Node_Id;
13986 begin
13987 SCIL_Node := Empty;
13989 -- Handle entities from the limited view
13991 Left_Type := Available_View (Etype (Left));
13992 Right_Type := Available_View (Etype (Right));
13994 -- In the case where the type is an access type, the test is applied
13995 -- using the designated types (needed in Ada 2012 for implicit anonymous
13996 -- access conversions, for AI05-0149).
13998 if Is_Access_Type (Right_Type) then
13999 Left_Type := Designated_Type (Left_Type);
14000 Right_Type := Designated_Type (Right_Type);
14001 end if;
14003 if Is_Class_Wide_Type (Left_Type) then
14004 Left_Type := Root_Type (Left_Type);
14005 end if;
14007 if Is_Class_Wide_Type (Right_Type) then
14008 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
14009 else
14010 Full_R_Typ := Underlying_Type (Right_Type);
14011 end if;
14013 Obj_Tag :=
14014 Make_Selected_Component (Loc,
14015 Prefix => Relocate_Node (Left),
14016 Selector_Name =>
14017 New_Occurrence_Of (First_Tag_Component (Left_Type), Loc));
14019 if Is_Class_Wide_Type (Right_Type) or else Is_Interface (Left_Type) then
14021 -- No need to issue a run-time check if we statically know that the
14022 -- result of this membership test is always true. For example,
14023 -- considering the following declarations:
14025 -- type Iface is interface;
14026 -- type T is tagged null record;
14027 -- type DT is new T and Iface with null record;
14029 -- Obj1 : T;
14030 -- Obj2 : DT;
14032 -- These membership tests are always true:
14034 -- Obj1 in T'Class
14035 -- Obj2 in T'Class;
14036 -- Obj2 in Iface'Class;
14038 -- We do not need to handle cases where the membership is illegal.
14039 -- For example:
14041 -- Obj1 in DT'Class; -- Compile time error
14042 -- Obj1 in Iface'Class; -- Compile time error
14044 if not Is_Class_Wide_Type (Left_Type)
14045 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
14046 Use_Full_View => True)
14047 or else (Is_Interface (Etype (Right_Type))
14048 and then Interface_Present_In_Ancestor
14049 (Typ => Left_Type,
14050 Iface => Etype (Right_Type))))
14051 then
14052 Result := New_Occurrence_Of (Standard_True, Loc);
14053 return;
14054 end if;
14056 -- Ada 2005 (AI-251): Class-wide applied to interfaces
14058 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
14060 -- Support to: "Iface_CW_Typ in Typ'Class"
14062 or else Is_Interface (Left_Type)
14063 then
14064 -- Issue error if IW_Membership operation not available in a
14065 -- configurable run time setting.
14067 if not RTE_Available (RE_IW_Membership) then
14068 Error_Msg_CRT
14069 ("dynamic membership test on interface types", N);
14070 Result := Empty;
14071 return;
14072 end if;
14074 Result :=
14075 Make_Function_Call (Loc,
14076 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
14077 Parameter_Associations => New_List (
14078 Make_Attribute_Reference (Loc,
14079 Prefix => Obj_Tag,
14080 Attribute_Name => Name_Address),
14081 New_Occurrence_Of (
14082 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
14083 Loc)));
14085 -- Ada 95: Normal case
14087 else
14088 Build_CW_Membership (Loc,
14089 Obj_Tag_Node => Obj_Tag,
14090 Typ_Tag_Node =>
14091 New_Occurrence_Of (
14092 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
14093 Related_Nod => N,
14094 New_Node => New_Node);
14096 -- Generate the SCIL node for this class-wide membership test.
14097 -- Done here because the previous call to Build_CW_Membership
14098 -- relocates Obj_Tag.
14100 if Generate_SCIL then
14101 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
14102 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
14103 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
14104 end if;
14106 Result := New_Node;
14107 end if;
14109 -- Right_Type is not a class-wide type
14111 else
14112 -- No need to check the tag of the object if Right_Typ is abstract
14114 if Is_Abstract_Type (Right_Type) then
14115 Result := New_Occurrence_Of (Standard_False, Loc);
14117 else
14118 Result :=
14119 Make_Op_Eq (Loc,
14120 Left_Opnd => Obj_Tag,
14121 Right_Opnd =>
14122 New_Occurrence_Of
14123 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
14124 end if;
14125 end if;
14126 end Tagged_Membership;
14128 ------------------------------
14129 -- Unary_Op_Validity_Checks --
14130 ------------------------------
14132 procedure Unary_Op_Validity_Checks (N : Node_Id) is
14133 begin
14134 if Validity_Checks_On and Validity_Check_Operands then
14135 Ensure_Valid (Right_Opnd (N));
14136 end if;
14137 end Unary_Op_Validity_Checks;
14139 end Exp_Ch4;